Skip to content

Commit

Permalink
Mesh:setMaterial takes texture too;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed Oct 31, 2023
1 parent 55a27d0 commit a28d66e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ bool luax_writefile(const char* filename, const void* data, size_t size);

#ifndef LOVR_DISABLE_GRAPHICS
struct DataField;
struct Material;
struct ColoredString;
void luax_checkfieldn(lua_State* L, int index, int type, void* data);
void luax_checkfieldv(lua_State* L, int index, int type, void* data);
Expand All @@ -170,6 +171,7 @@ int luax_pushbufferdata(lua_State* L, const struct DataField* format, uint32_t c
void luax_pushbufferformat(lua_State* L, const struct DataField* fields, uint32_t count);
uint32_t luax_gettablestride(lua_State* L, int index, int subindex, struct DataField* fields, uint32_t count);
uint32_t luax_checkcomparemode(lua_State* L, int index);
struct Material* luax_optmaterial(lua_State* L, int index);
struct ColoredString* luax_checkcoloredstrings(lua_State* L, int index, uint32_t* count, struct ColoredString* stack);
#endif

Expand Down
13 changes: 13 additions & 0 deletions src/api/l_graphics_material.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
#include "graphics/graphics.h"
#include "util.h"

Material* luax_optmaterial(lua_State* L, int index) {
if (lua_isnoneornil(L, index)) {
return NULL;
} else {
Texture* texture = luax_totype(L, index, Texture);
if (texture) {
return lovrTextureToMaterial(texture);
} else {
return luax_checktype(L, index, Material);
}
}
}

static int l_lovrMaterialGetProperties(lua_State* L) {
Material* material = luax_checktype(L, 1, Material);
const MaterialInfo* info = lovrMaterialGetInfo(material);
Expand Down
2 changes: 1 addition & 1 deletion src/api/l_graphics_mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ static int l_lovrMeshGetMaterial(lua_State* L) {

static int l_lovrMeshSetMaterial(lua_State* L) {
Mesh* mesh = luax_checktype(L, 1, Mesh);
Material* material = luax_checktype(L, 2, Material);
Material* material = luax_optmaterial(L, 2);
lovrMeshSetMaterial(mesh, material);
return 0;
}
Expand Down
5 changes: 2 additions & 3 deletions src/api/l_graphics_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,8 @@ static int l_lovrPassSetFont(lua_State* L) {

static int l_lovrPassSetMaterial(lua_State* L) {
Pass* pass = luax_checktype(L, 1, Pass);
Material* material = luax_totype(L, 2, Material);
Texture* texture = luax_totype(L, 2, Texture);
lovrPassSetMaterial(pass, material, texture);
Material* material = luax_optmaterial(L, 2);
lovrPassSetMaterial(pass, material);
return 0;
}

Expand Down
48 changes: 22 additions & 26 deletions src/modules/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1938,23 +1938,6 @@ void lovrBufferClear(Buffer* buffer, uint32_t offset, uint32_t extent, uint32_t

// Texture

static Material* lovrTextureGetMaterial(Texture* texture) {
if (!texture->material) {
texture->material = lovrMaterialCreate(&(MaterialInfo) {
.data.color = { 1.f, 1.f, 1.f, 1.f },
.data.uvScale = { 1.f, 1.f },
.texture = texture
});

// Since the Material refcounts the Texture, this creates a cycle. Release the texture to make
// sure this is a weak relationship (the automaterial does not keep the texture refcounted).
lovrRelease(texture, lovrTextureDestroy);
texture->material->info.texture = NULL;
}

return texture->material;
}

Texture* lovrGraphicsGetWindowTexture(void) {
if (!state.window && os_window_is_open()) {
uint32_t width, height;
Expand Down Expand Up @@ -2377,6 +2360,23 @@ void lovrTextureGenerateMipmaps(Texture* texture, uint32_t base, uint32_t count)
mipmapTexture(state.stream, texture, base, count);
}

Material* lovrTextureToMaterial(Texture* texture) {
if (!texture->material) {
texture->material = lovrMaterialCreate(&(MaterialInfo) {
.data.color = { 1.f, 1.f, 1.f, 1.f },
.data.uvScale = { 1.f, 1.f },
.texture = texture
});

// Since the Material refcounts the Texture, this creates a cycle. Release the texture to make
// sure this is a weak relationship (the automaterial does not keep the texture refcounted).
lovrRelease(texture, lovrTextureDestroy);
texture->material->info.texture = NULL;
}

return texture->material;
}

// Sampler

Sampler* lovrGraphicsGetDefaultSampler(FilterMode mode) {
Expand Down Expand Up @@ -5652,12 +5652,8 @@ void lovrPassSetFont(Pass* pass, Font* font) {
}
}

void lovrPassSetMaterial(Pass* pass, Material* material, Texture* texture) {
if (texture) {
material = lovrTextureGetMaterial(texture);
}

material = material ? material : state.defaultMaterial;
void lovrPassSetMaterial(Pass* pass, Material* material) {
if (!material) material = state.defaultMaterial;

if (pass->pipeline->material != material) {
lovrRetain(material);
Expand Down Expand Up @@ -6985,7 +6981,7 @@ void lovrPassSkybox(Pass* pass, Texture* texture) {
lovrPassDraw(pass, &(DrawInfo) {
.mode = DRAW_TRIANGLES,
.shader = !texture || texture->info.type == TEXTURE_2D ? SHADER_EQUIRECT : SHADER_CUBEMAP,
.material = texture ? lovrTextureGetMaterial(texture) : NULL,
.material = texture ? lovrTextureToMaterial(texture) : NULL,
.vertex.format = VERTEX_EMPTY,
.count = 6
});
Expand All @@ -6995,7 +6991,7 @@ void lovrPassFill(Pass* pass, Texture* texture) {
lovrPassDraw(pass, &(DrawInfo) {
.mode = DRAW_TRIANGLES,
.shader = texture && texture->info.type == TEXTURE_ARRAY ? SHADER_FILL_ARRAY : SHADER_FILL_2D,
.material = texture ? lovrTextureGetMaterial(texture) : NULL,
.material = texture ? lovrTextureToMaterial(texture) : NULL,
.vertex.format = VERTEX_EMPTY,
.count = 3
});
Expand Down Expand Up @@ -7106,7 +7102,7 @@ void lovrPassDrawTexture(Pass* pass, Texture* texture, float* transform) {
.mode = DRAW_TRIANGLES,
.transform = transform,
.bounds = (float[6]) { 0.f, 0.f, 0.f, .5f, .5f, 0.f },
.material = lovrTextureGetMaterial(texture),
.material = lovrTextureToMaterial(texture),
.vertex.pointer = (void**) &vertices,
.vertex.count = vertexCount,
.index.pointer = (void**) &indices,
Expand Down
3 changes: 2 additions & 1 deletion src/modules/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ void lovrTextureCopy(Texture* src, Texture* dst, uint32_t srcOffset[4], uint32_t
void lovrTextureBlit(Texture* src, Texture* dst, uint32_t srcOffset[4], uint32_t dstOffset[4], uint32_t srcExtent[3], uint32_t dstExtent[3], FilterMode filter);
void lovrTextureClear(Texture* texture, float value[4], uint32_t layer, uint32_t layerCount, uint32_t level, uint32_t levelCount);
void lovrTextureGenerateMipmaps(Texture* texture, uint32_t base, uint32_t count);
Material* lovrTextureToMaterial(Texture* texture);

// Sampler

Expand Down Expand Up @@ -606,7 +607,7 @@ void lovrPassSetDepthOffset(Pass* pass, float offset, float sloped);
void lovrPassSetDepthClamp(Pass* pass, bool clamp);
void lovrPassSetFaceCull(Pass* pass, CullMode mode);
void lovrPassSetFont(Pass* pass, Font* font);
void lovrPassSetMaterial(Pass* pass, Material* material, Texture* texture);
void lovrPassSetMaterial(Pass* pass, Material* material);
void lovrPassSetMeshMode(Pass* pass, DrawMode mode);
void lovrPassSetSampler(Pass* pass, Sampler* sampler);
void lovrPassSetShader(Pass* pass, Shader* shader);
Expand Down

0 comments on commit a28d66e

Please sign in to comment.