From 4379d25e1cc66bf140e92d5c85973a2d1aa7aa90 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 26 Jan 2020 16:33:20 -0800 Subject: [PATCH] Organization; --- src/api/l_graphics.c | 120 +++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/src/api/l_graphics.c b/src/api/l_graphics.c index 4ae06985..20de887a 100644 --- a/src/api/l_graphics.c +++ b/src/api/l_graphics.c @@ -1026,66 +1026,6 @@ static void luax_checkuniformtype(lua_State* L, int index, UniformType* baseType } } -static int l_lovrGraphicsNewShaderBlock(lua_State* L) { - arr_uniform_t uniforms; - arr_init(&uniforms); - - BlockType type = luaL_checkoption(L, 1, NULL, BlockTypes); - - luaL_checktype(L, 2, LUA_TTABLE); - lua_pushnil(L); - while (lua_next(L, 2) != 0) { - Uniform uniform; - - // Name - strncpy(uniform.name, luaL_checkstring(L, -2), LOVR_MAX_UNIFORM_LENGTH - 1); - - if (lua_type(L, -1) == LUA_TSTRING) { - uniform.count = 1; - luax_checkuniformtype(L, -1, &uniform.type, &uniform.components); - } else { - luaL_checktype(L, -1, LUA_TTABLE); - - lua_rawgeti(L, -1, 1); - luax_checkuniformtype(L, -1, &uniform.type, &uniform.components); - lua_pop(L, 1); - - lua_rawgeti(L, -1, 2); - uniform.count = luaL_optinteger(L, -1, 1); - lua_pop(L, 1); - } - - lovrAssert(uniform.count >= 1, "Uniform count must be positive, got %d for '%s'", uniform.count, uniform.name); - arr_push(&uniforms, uniform); - - // Pop the table, leaving the key for lua_next to nom - lua_pop(L, 1); - } - - BufferUsage usage = USAGE_DYNAMIC; - bool readable = false; - - if (lua_istable(L, 3)) { - lua_getfield(L, 3, "usage"); - usage = luaL_checkoption(L, -1, "dynamic", BufferUsages); - lua_pop(L, 1); - - lua_getfield(L, 3, "readable"); - readable = lua_toboolean(L, -1); - lua_pop(L, 1); - } - - lovrAssert(type == BLOCK_UNIFORM || lovrGraphicsGetFeatures()->compute, "Compute blocks are not supported on this system"); - size_t size = lovrShaderComputeUniformLayout(&uniforms); - Buffer* buffer = lovrBufferCreate(size, NULL, type == BLOCK_COMPUTE ? BUFFER_SHADER_STORAGE : BUFFER_UNIFORM, usage, readable); - ShaderBlock* block = lovrShaderBlockCreate(type, buffer, &uniforms); - luax_pushtype(L, ShaderBlock, block); - arr_free(&uniforms); - lovrRelease(Buffer, buffer); - lovrRelease(ShaderBlock, block); - return 1; -} - static int l_lovrGraphicsNewCanvas(lua_State* L) { Attachment attachments[MAX_CANVAS_ATTACHMENTS]; int attachmentCount = 0; @@ -1534,6 +1474,66 @@ static int l_lovrGraphicsNewComputeShader(lua_State* L) { return 1; } +static int l_lovrGraphicsNewShaderBlock(lua_State* L) { + arr_uniform_t uniforms; + arr_init(&uniforms); + + BlockType type = luaL_checkoption(L, 1, NULL, BlockTypes); + + luaL_checktype(L, 2, LUA_TTABLE); + lua_pushnil(L); + while (lua_next(L, 2) != 0) { + Uniform uniform; + + // Name + strncpy(uniform.name, luaL_checkstring(L, -2), LOVR_MAX_UNIFORM_LENGTH - 1); + + if (lua_type(L, -1) == LUA_TSTRING) { + uniform.count = 1; + luax_checkuniformtype(L, -1, &uniform.type, &uniform.components); + } else { + luaL_checktype(L, -1, LUA_TTABLE); + + lua_rawgeti(L, -1, 1); + luax_checkuniformtype(L, -1, &uniform.type, &uniform.components); + lua_pop(L, 1); + + lua_rawgeti(L, -1, 2); + uniform.count = luaL_optinteger(L, -1, 1); + lua_pop(L, 1); + } + + lovrAssert(uniform.count >= 1, "Uniform count must be positive, got %d for '%s'", uniform.count, uniform.name); + arr_push(&uniforms, uniform); + + // Pop the table, leaving the key for lua_next to nom + lua_pop(L, 1); + } + + BufferUsage usage = USAGE_DYNAMIC; + bool readable = false; + + if (lua_istable(L, 3)) { + lua_getfield(L, 3, "usage"); + usage = luaL_checkoption(L, -1, "dynamic", BufferUsages); + lua_pop(L, 1); + + lua_getfield(L, 3, "readable"); + readable = lua_toboolean(L, -1); + lua_pop(L, 1); + } + + lovrAssert(type == BLOCK_UNIFORM || lovrGraphicsGetFeatures()->compute, "Compute blocks are not supported on this system"); + size_t size = lovrShaderComputeUniformLayout(&uniforms); + Buffer* buffer = lovrBufferCreate(size, NULL, type == BLOCK_COMPUTE ? BUFFER_SHADER_STORAGE : BUFFER_UNIFORM, usage, readable); + ShaderBlock* block = lovrShaderBlockCreate(type, buffer, &uniforms); + luax_pushtype(L, ShaderBlock, block); + arr_free(&uniforms); + lovrRelease(Buffer, buffer); + lovrRelease(ShaderBlock, block); + return 1; +} + static int l_lovrGraphicsNewTexture(lua_State* L) { int index = 1; int width, height, depth;