Mesh:setMaterial takes texture too;

This commit is contained in:
bjorn 2023-10-31 16:08:00 -07:00
parent 55a27d0a69
commit a28d66e3a1
6 changed files with 42 additions and 31 deletions

View File

@ -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);
@ -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

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
@ -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) {
@ -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);
@ -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
});
@ -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
});
@ -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,

View File

@ -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
@ -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);