From a7d9df1dc2c52f80f8f48f3e70097d96df5c3595 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sat, 11 May 2024 23:33:32 -0700 Subject: [PATCH] Texture:getLabel; Shader:getLabel; --- src/api/l_graphics_shader.c | 8 ++++++++ src/api/l_graphics_texture.c | 8 ++++++++ src/modules/graphics/graphics.c | 28 ++++++++++++++++++++++++---- src/modules/graphics/graphics.h | 1 - 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/api/l_graphics_shader.c b/src/api/l_graphics_shader.c index 021c10dc1..c3a4d65e7 100644 --- a/src/api/l_graphics_shader.c +++ b/src/api/l_graphics_shader.c @@ -33,6 +33,13 @@ static int l_lovrShaderClone(lua_State* L) { return 1; } +static int l_lovrShaderGetLabel(lua_State* L) { + Shader* shader = luax_checktype(L, 1, Shader); + const ShaderInfo* info = lovrShaderGetInfo(shader); + lua_pushstring(L, info->label); + return 1; +} + static int l_lovrShaderGetType(lua_State* L) { Shader* shader = luax_checktype(L, 1, Shader); const ShaderInfo* info = lovrShaderGetInfo(shader); @@ -107,6 +114,7 @@ static int l_lovrShaderGetBufferFormat(lua_State* L) { const luaL_Reg lovrShader[] = { { "clone", l_lovrShaderClone }, + { "getLabel", l_lovrShaderGetLabel }, { "getType", l_lovrShaderGetType }, { "hasStage", l_lovrShaderHasStage }, { "hasAttribute", l_lovrShaderHasAttribute }, diff --git a/src/api/l_graphics_texture.c b/src/api/l_graphics_texture.c index 2443d3081..bd2d5f3b6 100644 --- a/src/api/l_graphics_texture.c +++ b/src/api/l_graphics_texture.c @@ -4,6 +4,13 @@ #include "data/image.h" #include +static int l_lovrTextureGetLabel(lua_State* L) { + Texture* texture = luax_checktype(L, 1, Texture); + const TextureInfo* info = lovrTextureGetInfo(texture); + lua_pushstring(L, info->label); + return 1; +} + static int l_lovrTextureGetType(lua_State* L) { Texture* texture = luax_checktype(L, 1, Texture); const TextureInfo* info = lovrTextureGetInfo(texture); @@ -199,6 +206,7 @@ static int l_lovrTextureGenerateMipmaps(lua_State* L) { } const luaL_Reg lovrTexture[] = { + { "getLabel", l_lovrTextureGetLabel }, { "getType", l_lovrTextureGetType }, { "getFormat", l_lovrTextureGetFormat }, { "getWidth", l_lovrTextureGetWidth }, diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index df7f9b448..574bdb5a5 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -727,10 +727,7 @@ bool lovrGraphicsInit(GraphicsConfig* config) { float data[] = { 0.f, 0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f }; - state.defaultBuffer = lovrBufferCreate(&(BufferInfo) { - .size = sizeof(data), - .label = "Default Buffer" - }, NULL); + state.defaultBuffer = lovrBufferCreate(&(BufferInfo) { .size = sizeof(data), }, NULL); beginFrame(); BufferView view = getBuffer(GPU_BUFFER_UPLOAD, sizeof(data), 4); @@ -2165,6 +2162,13 @@ Texture* lovrTextureCreate(const TextureInfo* info) { texture->info.mipmaps = mipmaps; texture->info.srgb = srgb; + if (info->label) { + size_t size = strlen(info->label) + 1; + char* label = lovrMalloc(size); + memcpy(label, info->label, size); + texture->info.label = label; + } + uint32_t levelCount = 0; uint32_t levelOffsets[16]; uint32_t levelSizes[16]; @@ -2296,6 +2300,13 @@ Texture* lovrTextureCreateView(Texture* parent, const TextureViewInfo* info) { texture->gpu = (gpu_texture*) (texture + 1); texture->info = *base; + if (info->label) { + size_t size = strlen(info->label) + 1; + char* label = lovrMalloc(size); + memcpy(label, info->label, size); + texture->info.label = label; + } + texture->root = parent->root; texture->baseLayer = parent->baseLayer + info->layerIndex; texture->baseLevel = parent->baseLevel + info->levelIndex; @@ -2372,6 +2383,7 @@ void lovrTextureDestroy(void* ref) { if (texture->storageView && texture->storageView != texture->gpu) gpu_texture_destroy(texture->storageView); if (texture->gpu) gpu_texture_destroy(texture->gpu); } + lovrFree((char*) texture->info.label); lovrFree(texture); } @@ -2856,6 +2868,13 @@ Shader* lovrShaderCreate(const ShaderInfo* info) { shader->gpu = (gpu_shader*) (shader + 1); shader->info = *info; + if (info->label) { + size_t size = strlen(info->label) + 1; + char* label = lovrMalloc(size); + memcpy(label, info->label, size); + shader->info.label = label; + } + // Validate stage combinations for (uint32_t i = 0; i < info->stageCount; i++) { shader->stageMask |= (1 << info->stages[i].stage); @@ -3265,6 +3284,7 @@ void lovrShaderDestroy(void* ref) { lovrFree(shader->resources); lovrFree(shader->fields); lovrFree(shader->names); + lovrFree((char*) shader->info.label); } lovrFree(shader->flags); lovrFree(shader->flagLookup); diff --git a/src/modules/graphics/graphics.h b/src/modules/graphics/graphics.h index 72f4913cd..d180936c2 100644 --- a/src/modules/graphics/graphics.h +++ b/src/modules/graphics/graphics.h @@ -173,7 +173,6 @@ typedef struct { uint32_t size; uint32_t fieldCount; DataField* format; - const char* label; uintptr_t handle; } BufferInfo;