Skip to content

Commit

Permalink
Texture:getLabel; Shader:getLabel;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed May 12, 2024
1 parent 875d9d6 commit a7d9df1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/api/l_graphics_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 },
Expand Down
8 changes: 8 additions & 0 deletions src/api/l_graphics_texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
#include "data/image.h"
#include <string.h>

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);
Expand Down Expand Up @@ -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 },
Expand Down
28 changes: 24 additions & 4 deletions src/modules/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/modules/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ typedef struct {
uint32_t size;
uint32_t fieldCount;
DataField* format;
const char* label;
uintptr_t handle;
} BufferInfo;

Expand Down

0 comments on commit a7d9df1

Please sign in to comment.