From 65fababd8d36b47c85e7d6b43f649ed88c7e8bcf Mon Sep 17 00:00:00 2001 From: jussi Date: Tue, 31 Oct 2023 02:11:46 +0200 Subject: Shaders management functions. --- API.md | 65 ++++++++++++++++++++- ReiLua_API.lua | 62 +++++++++++++++++++- changelog | 1 + devnotes | 3 +- include/lrlgl.h | 11 ++++ include/state.h | 1 + src/core.c | 1 - src/lua_core.c | 11 ++++ src/rlgl.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++------ src/state.c | 7 +++ 10 files changed, 310 insertions(+), 23 deletions(-) diff --git a/API.md b/API.md index ee4173a..43b20fc 100644 --- a/API.md +++ b/API.md @@ -6854,15 +6854,76 @@ Delete framebuffer from GPU --- -> success = RL.rlLoadShaderCode( string vsCode, string fsCode ) +> shaderId = RL.rlLoadShaderCode( string vsCode, string fsCode ) Load shader from code strings -- Failure return nil - Success return int --- +> shaderId = RL.rlCompileShader( string shaderCode, int type ) + +Compile custom shader and return shader id (type: RL_VERTEX_SHADER, RL_FRAGMENT_SHADER, RL_COMPUTE_SHADER) + +- Success return int + +--- + +> shaderProgramId = RL.rlLoadShaderProgram( int vShaderId, int fShaderId ) + +Load custom shader program + +- Success return int + +--- + +> RL.rlUnloadShaderProgram( int id ) + +Unload shader program + +--- + +> location = RL.rlGetLocationUniform( int shaderId, string uniformName ) + +Get shader location uniform + +- Success return int + +--- + +> location = RL.rlGetLocationAttrib( int shaderId, string attribName ) + +Get shader location attribute + +- Success return int + +--- + +> RL.rlSetUniform( int locIndex, Buffer value, int uniformType, int count ) + +Set shader value uniform + +--- + +> RL.rlSetUniformMatrix( int locIndex, Matrix mat ) + +Set shader value matrix + +--- + +> RL.rlSetUniformSampler( int locIndex, int textureId ) + +Set shader value sampler + +--- + +> RL.rlSetShader( int id, int{} locs ) + +Set shader currently active (id and locations) + +--- + ## RLGL - Matrix state management --- diff --git a/ReiLua_API.lua b/ReiLua_API.lua index 7996ad3..b792a6d 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -5496,13 +5496,71 @@ function RL.rlUnloadFramebuffer( id ) end -- RLGL - Shaders management ---Load shader from code strings ----- Failure return nil ---- Success return int ---@param vsCode string ---@param fsCode string ----@return any success +---@return any shaderId function RL.rlLoadShaderCode( vsCode, fsCode ) end +---Compile custom shader and return shader id (type: RL_VERTEX_SHADER, RL_FRAGMENT_SHADER, RL_COMPUTE_SHADER) +---- Success return int +---@param shaderCode string +---@param type integer +---@return any shaderId +function RL.rlCompileShader( shaderCode, type ) end + +---Load custom shader program +---- Success return int +---@param vShaderId integer +---@param fShaderId integer +---@return any shaderProgramId +function RL.rlLoadShaderProgram( vShaderId, fShaderId ) end + +---Unload shader program +---@param id integer +---@return any RL.rlUnloadShaderProgram +function RL.rlUnloadShaderProgram( id ) end + +---Get shader location uniform +---- Success return int +---@param shaderId integer +---@param uniformName string +---@return any location +function RL.rlGetLocationUniform( shaderId, uniformName ) end + +---Get shader location attribute +---- Success return int +---@param shaderId integer +---@param attribName string +---@return any location +function RL.rlGetLocationAttrib( shaderId, attribName ) end + +---Set shader value uniform +---@param locIndex integer +---@param value any +---@param uniformType integer +---@param count integer +---@return any RL.rlSetUniform +function RL.rlSetUniform( locIndex, value, uniformType, count ) end + +---Set shader value matrix +---@param locIndex integer +---@param mat table +---@return any RL.rlSetUniformMatrix +function RL.rlSetUniformMatrix( locIndex, mat ) end + +---Set shader value sampler +---@param locIndex integer +---@param textureId integer +---@return any RL.rlSetUniformSampler +function RL.rlSetUniformSampler( locIndex, textureId ) end + +---Set shader currently active (id and locations) +---@param id integer +---@param locs any +---@return any RL.rlSetShader +function RL.rlSetShader( id, locs ) end + -- RLGL - Matrix state management ---Get internal modelview matrix diff --git a/changelog b/changelog index fac991a..24a63cb 100644 --- a/changelog +++ b/changelog @@ -9,6 +9,7 @@ KEY CHANGES: - ADDED: Userdata objects can be referenced with lightuserdata. - ADDED: GC_UNLOAD build time define for enabling/disabling Lua garbage collection for objects. Can be checked with IsGCUnloadEnabled + - ADDED: Shaders management functions. DETAILED CHANGES: - CHANGED: GenImageColor now takes Vector2 as size. diff --git a/devnotes b/devnotes index 5e80ae2..3eb51a8 100644 --- a/devnotes +++ b/devnotes @@ -3,7 +3,6 @@ Current { Backlog { * rlgl - * Shaders management * Compute shader management * Text * Codepoints? @@ -38,4 +37,6 @@ Needs Testing { * UpdateMesh * LoadModelFromMesh * RLGL - Matrix state management + * rlSetUniform + * rlSetShader } diff --git a/include/lrlgl.h b/include/lrlgl.h index e414215..672aadc 100644 --- a/include/lrlgl.h +++ b/include/lrlgl.h @@ -104,6 +104,17 @@ int lrlglLoadFramebuffer( lua_State *L ); int lrlglFramebufferAttach( lua_State *L ); int lrlglFramebufferComplete( lua_State *L ); int lrlglUnloadFramebuffer( lua_State *L ); +/* Shaders management */ +int lrlglLoadShaderCode( lua_State *L ); +int lrlglCompileShader( lua_State *L ); +int lrlglLoadShaderProgram( lua_State *L ); +int lrlglUnloadShaderProgram( lua_State *L ); +int lrlglGetLocationUniform( lua_State *L ); +int lrlglGetLocationAttrib( lua_State *L ); +int lrlglSetUniform( lua_State *L ); +int lrlglSetUniformMatrix( lua_State *L ); +int lrlglSetUniformSampler( lua_State *L ); +int lrlglSetShader( lua_State *L ); /* Matrix state management */ int lrlglGetMatrixModelview( lua_State *L ); int lrlglGetMatrixProjection( lua_State *L ); diff --git a/include/state.h b/include/state.h index d7e1052..1172086 100644 --- a/include/state.h +++ b/include/state.h @@ -12,6 +12,7 @@ typedef struct { int logLevelInvalid; Font defaultFont; Material defaultMaterial; + int *RLGLcurrentShaderLocs; /* Raylib GLFW input callback events. */ /* Window events. */ GLFWwindowsizefun raylibWindowSizeCallback; diff --git a/src/core.c b/src/core.c index 92eb5bd..bd1ffe0 100644 --- a/src/core.c +++ b/src/core.c @@ -553,7 +553,6 @@ int lcoreLoadBuffer( lua_State *L ) { int type = luaL_checkinteger( L, 2 ); Buffer buffer = { 0 }; - // Buffer *buffer = lua_newuserdata( L, sizeof( Buffer ) ); int len = uluaGetTableLenIndex( L, 1 ); switch ( type ) { diff --git a/src/lua_core.c b/src/lua_core.c index 10f48b8..7c447f6 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -2285,6 +2285,17 @@ void luaRegister() { assingGlobalFunction( "rlFramebufferAttach", lrlglFramebufferAttach ); assingGlobalFunction( "rlFramebufferComplete", lrlglFramebufferComplete ); assingGlobalFunction( "rlUnloadFramebuffer", lrlglUnloadFramebuffer ); + /* Shaders management */ + assingGlobalFunction( "rlLoadShaderCode", lrlglLoadShaderCode ); + assingGlobalFunction( "rlCompileShader", lrlglCompileShader ); + assingGlobalFunction( "rlLoadShaderProgram", lrlglLoadShaderProgram ); + assingGlobalFunction( "rlUnloadShaderProgram", lrlglUnloadShaderProgram ); + assingGlobalFunction( "rlGetLocationUniform", lrlglGetLocationUniform ); + assingGlobalFunction( "rlGetLocationAttrib", lrlglGetLocationAttrib ); + assingGlobalFunction( "rlSetUniform", lrlglSetUniform ); + assingGlobalFunction( "rlSetUniformMatrix", lrlglSetUniformMatrix ); + assingGlobalFunction( "rlSetUniformSampler", lrlglSetUniformSampler ); + assingGlobalFunction( "rlSetShader", lrlglSetShader ); /* Matrix state management. */ assingGlobalFunction( "rlGetMatrixModelview", lrlglGetMatrixModelview ); assingGlobalFunction( "rlGetMatrixProjection", lrlglGetMatrixProjection ); diff --git a/src/rlgl.c b/src/rlgl.c index 8fba3a1..4a13c81 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -928,7 +928,7 @@ Load a vertex buffer attribute - Success return int */ int lrlglLoadVertexBuffer( lua_State *L ) { - Buffer *buffer = luaL_checkudata( L, 1, "Buffer" ); + Buffer *buffer = uluaGetBuffer( L, 1 ); bool dynamic = uluaGetBoolean( L, 2 ); lua_pushinteger( L, rlLoadVertexBuffer( buffer->data, buffer->size, dynamic ) ); @@ -944,7 +944,7 @@ Load a new attributes element buffer - Success return int */ int lrlglLoadVertexBufferElement( lua_State *L ) { - Buffer *buffer = luaL_checkudata( L, 1, "Buffer" ); + Buffer *buffer = uluaGetBuffer( L, 1 ); bool dynamic = uluaGetBoolean( L, 2 ); lua_pushinteger( L, rlLoadVertexBufferElement( buffer->data, buffer->size, dynamic ) ); @@ -959,7 +959,7 @@ Update GPU buffer with new data */ int lrlglUpdateVertexBuffer( lua_State *L ) { int bufferId = luaL_checkinteger( L, 1 ); - Buffer *buffer = luaL_checkudata( L, 2, "Buffer" ); + Buffer *buffer = uluaGetBuffer( L, 2 ); int offset = luaL_checkinteger( L, 3 ); rlUpdateVertexBuffer( bufferId, buffer->data, buffer->size, offset ); @@ -974,7 +974,7 @@ Update vertex buffer elements with new data */ int lrlglUpdateVertexBufferElements( lua_State *L ) { int bufferId = luaL_checkinteger( L, 1 ); - Buffer *buffer = luaL_checkudata( L, 2, "Buffer" ); + Buffer *buffer = uluaGetBuffer( L, 2 ); int offset = luaL_checkinteger( L, 3 ); rlUpdateVertexBufferElements( bufferId, buffer->data, buffer->size, offset ); @@ -1083,7 +1083,7 @@ Draw vertex array elements int lrlglDrawVertexArrayElements( lua_State *L ) { int offset = luaL_checkinteger( L, 1 ); int count = luaL_checkinteger( L, 2 ); - Buffer *buffer = luaL_checkudata( L, 3, "Buffer" ); + Buffer *buffer = uluaGetBuffer( L, 3 ); rlDrawVertexArrayElements( offset, count, buffer->data ); @@ -1113,7 +1113,7 @@ Draw vertex array elements instanced int lrlglDrawVertexArrayElementsInstanced( lua_State *L ) { int offset = luaL_checkinteger( L, 1 ); int count = luaL_checkinteger( L, 2 ); - Buffer *buffer = luaL_checkudata( L, 3, "Buffer" ); + Buffer *buffer = uluaGetBuffer( L, 3 ); int instances = luaL_checkinteger( L, 4 ); rlDrawVertexArrayElementsInstanced( offset, count, buffer->data, instances ); @@ -1238,23 +1238,160 @@ int lrlglUnloadFramebuffer( lua_State *L ) { */ /* -> success = RL.rlLoadShaderCode( string vsCode, string fsCode ) +> shaderId = RL.rlLoadShaderCode( string vsCode, string fsCode ) Load shader from code strings -- Failure return nil - Success return int */ -// int lrlglUnloadFramebuffer( lua_State *L ) { -// if ( !lua_isstring( L, 1 ) || !lua_isstring( L, 2 ) ) { -// TraceLog( state->logLevelInvalid, "%s", "Bad call of function. RL.rlLoadShaderCode( string vsCode, string fsCode )" ); -// lua_pushnil( L ); -// return 1; -// } -// lua_pushinteger( L, rlLoadShaderCode( luaL_checkstring( L, 1 ), luaL_checkstring( L, 2 ) ) ); +int lrlglLoadShaderCode( lua_State *L ) { + lua_pushinteger( L, rlLoadShaderCode( luaL_checkstring( L, 1 ), luaL_checkstring( L, 2 ) ) ); -// return 1; -// } + return 1; +} + +/* +> shaderId = RL.rlCompileShader( string shaderCode, int type ) + +Compile custom shader and return shader id (type: RL_VERTEX_SHADER, RL_FRAGMENT_SHADER, RL_COMPUTE_SHADER) + +- Success return int +*/ +int lrlglCompileShader( lua_State *L ) { + int type = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, rlCompileShader( luaL_checkstring( L, 1 ), type ) ); + + return 1; +} + +/* +> shaderProgramId = RL.rlLoadShaderProgram( int vShaderId, int fShaderId ) + +Load custom shader program + +- Success return int +*/ +int lrlglLoadShaderProgram( lua_State *L ) { + unsigned int vShaderId = (unsigned int)luaL_checkinteger( L, 1 ); + unsigned int fShaderId = (unsigned int)luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, rlLoadShaderProgram( vShaderId, fShaderId ) ); + + return 1; +} + +/* +> RL.rlUnloadShaderProgram( int id ) + +Unload shader program +*/ +int lrlglUnloadShaderProgram( lua_State *L ) { + unsigned int id = (unsigned int)luaL_checkinteger( L, 1 ); + + rlUnloadShaderProgram( id ); + + return 0; +} + +/* +> location = RL.rlGetLocationUniform( int shaderId, string uniformName ) + +Get shader location uniform + +- Success return int +*/ +int lrlglGetLocationUniform( lua_State *L ) { + unsigned int shaderId = (unsigned int)luaL_checkinteger( L, 1 ); + + lua_pushinteger( L, rlGetLocationUniform( shaderId, luaL_checkstring( L, 2 ) ) ); + + return 1; +} + +/* +> location = RL.rlGetLocationAttrib( int shaderId, string attribName ) + +Get shader location attribute + +- Success return int +*/ +int lrlglGetLocationAttrib( lua_State *L ) { + unsigned int shaderId = (unsigned int)luaL_checkinteger( L, 1 ); + + lua_pushinteger( L, rlGetLocationAttrib( shaderId, luaL_checkstring( L, 2 ) ) ); + + return 1; +} + +/* +> RL.rlSetUniform( int locIndex, Buffer value, int uniformType, int count ) + +Set shader value uniform +*/ +int lrlglSetUniform( lua_State *L ) { + int locIndex = luaL_checkinteger( L, 1 ); + Buffer *value = uluaGetBuffer( L, 2 ); + int uniformType = luaL_checkinteger( L, 3 ); + int count = luaL_checkinteger( L, 4 ); + + rlSetUniform( locIndex, value->data, uniformType, count ); + + return 0; +} + +/* +> RL.rlSetUniformMatrix( int locIndex, Matrix mat ) + +Set shader value matrix +*/ +int lrlglSetUniformMatrix( lua_State *L ) { + int locIndex = luaL_checkinteger( L, 1 ); + Matrix mat = uluaGetMatrixIndex( L, 2 ); + + rlSetUniformMatrix( locIndex, mat ); + + return 0; +} + +/* +> RL.rlSetUniformSampler( int locIndex, int textureId ) + +Set shader value sampler +*/ +int lrlglSetUniformSampler( lua_State *L ) { + int locIndex = luaL_checkinteger( L, 1 ); + unsigned int textureId = (unsigned int)luaL_checkinteger( L, 2 ); + + rlSetUniformSampler( locIndex, textureId ); + + return 0; +} + +/* +> RL.rlSetShader( int id, int{} locs ) + +Set shader currently active (id and locations) +*/ +int lrlglSetShader( lua_State *L ) { + unsigned int id = (unsigned int)luaL_checkinteger( L, 1 ); + + int t = 2, i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + if ( lua_isnumber( L, -1 ) ) { + if ( i < RL_MAX_SHADER_LOCATIONS ) { + state->RLGLcurrentShaderLocs[i] = lua_tointeger( L, -1 ); + } + } + i++; + lua_pop( L, 1 ); + } + rlSetShader( id, state->RLGLcurrentShaderLocs ); + + return 0; +} /* ## RLGL - Matrix state management diff --git a/src/state.c b/src/state.c index b96b557..228cbce 100644 --- a/src/state.c +++ b/src/state.c @@ -36,6 +36,12 @@ bool stateInit( int argn, const char **argc, const char *exePath ) { } state->defaultFont = GetFontDefault(); state->defaultMaterial = LoadMaterialDefault(); + state->RLGLcurrentShaderLocs = malloc( RL_MAX_SHADER_LOCATIONS * sizeof( int ) ); + int *defaultShaderLocs = rlGetShaderLocsDefault(); + + for ( int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++ ) { + state->RLGLcurrentShaderLocs[i] = defaultShaderLocs[i]; + } return state->run; } @@ -57,5 +63,6 @@ void stateFree() { CloseWindow(); } free( state->exePath ); + free( state->RLGLcurrentShaderLocs ); free( state ); } -- cgit v1.2.3