From c8d2f3d3ac275dccaaf10d082f0933748f03fda6 Mon Sep 17 00:00:00 2001 From: Saga port Date: Tue, 17 Nov 2026 10:22:54 +0100 Subject: [PATCH 1/2] Model uniforms: let a -reflect map win over a -shine map again Both maps are loaded when a texture has them. The OpenGL and Vulkan binds put the spec-gloss (-reflect) map in the specular slot, but convert_model_material() then let the legacy -shine map overwrite sSpecmapIndex and clear gammaSpec and alphaGloss. The bound -reflect texture was therefore shaded as a legacy shine map: its alpha was not read as gloss, and the spec-gloss path (minimum specular, full fresnel) was skipped. The array layer also came from the -shine map, so an animated -reflect map could sample the wrong frame. The two checks were an if/else until #5055 split them into two ifs. Restore the else. --- code/graphics/uniforms.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/code/graphics/uniforms.cpp b/code/graphics/uniforms.cpp index ea36b05dfad..855fcd145bc 100644 --- a/code/graphics/uniforms.cpp +++ b/code/graphics/uniforms.cpp @@ -145,15 +145,14 @@ void convert_model_material(model_uniform_data* data_out, } + // A -reflect (spec-gloss) map wins over a legacy -shine map, as both texture binds do if (material.get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) { data_out->sSpecmapIndex = bm_get_array_index(material.get_texture_map(TM_SPEC_GLOSS_TYPE)); data_out->gammaSpec = 1; data_out->alphaGloss = 1; - } - - if (material.get_texture_map(TM_SPECULAR_TYPE) >= 0) { + } else if (material.get_texture_map(TM_SPECULAR_TYPE) >= 0) { data_out->sSpecmapIndex = bm_get_array_index(material.get_texture_map(TM_SPECULAR_TYPE)); data_out->gammaSpec = 0; From 2c67d87661b6519a939cd9cb114694860ddfd334 Mon Sep 17 00:00:00 2001 From: Saga port Date: Wed, 18 Nov 2026 08:23:51 +0100 Subject: [PATCH 2/2] Share specular map selection between texture binds and uniforms The renderer binds and uniform conversion selected the specular map independently, which let their precedence drift apart. Share the choice and encoding flag through material::get_specular_map(), preserving -reflect precedence and the raw -shine fallback at every render site. Pin selection and encoding in the material test, including reflect handle 0, and document the fallback beside the shared accessor. --- code/graphics/material.cpp | 11 ++++++++++- code/graphics/material.h | 5 +++++ code/graphics/opengl/gropengltnl.cpp | 25 ++++++++----------------- code/graphics/uniforms.cpp | 18 ++++++------------ code/graphics/vulkan/VulkanDraw.cpp | 13 ++----------- test/src/graphics/test_material.cpp | 26 ++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 41 deletions(-) diff --git a/code/graphics/material.cpp b/code/graphics/material.cpp index 5a57d0359e6..d770af7b6c1 100644 --- a/code/graphics/material.cpp +++ b/code/graphics/material.cpp @@ -301,6 +301,15 @@ int material::get_texture_map(int tex_type) const return Texture_maps[tex_type]; } +int material::get_specular_map(bool* is_spec_gloss) const +{ + const auto reflect_map = get_texture_map(TM_SPEC_GLOSS_TYPE); + if (is_spec_gloss != nullptr) { + *is_spec_gloss = reflect_map >= 0; + } + return reflect_map >= 0 ? reflect_map : get_texture_map(TM_SPECULAR_TYPE); +} + bool material::is_textured() const { return @@ -787,7 +796,7 @@ int model_material::get_shader_runtime_flags() const { flags |= MODEL_SDR_FLAG_DIFFUSE; if (get_texture_map(TM_GLOW_TYPE) >= 0) flags |= MODEL_SDR_FLAG_GLOW; - if (get_texture_map(TM_SPECULAR_TYPE) >= 0 || get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) + if (get_specular_map() >= 0) flags |= MODEL_SDR_FLAG_SPEC; if (get_texture_map(TM_NORMAL_TYPE) >= 0) flags |= MODEL_SDR_FLAG_NORMAL; diff --git a/code/graphics/material.h b/code/graphics/material.h index 3c67ff821a3..3e42be4a513 100644 --- a/code/graphics/material.h +++ b/code/graphics/material.h @@ -100,6 +100,11 @@ class material void set_texture_map(int tex_type, int texture_num); int get_texture_map(int tex_type) const; + + // Returns the -reflect map if present; otherwise returns the -shine slot value (-1 when absent). + // If supplied, is_spec_gloss reports whether the chosen texture uses specular/gloss encoding. + int get_specular_map(bool* is_spec_gloss = nullptr) const; + bool is_textured() const; void set_texture_type(texture_type t_type); diff --git a/code/graphics/opengl/gropengltnl.cpp b/code/graphics/opengl/gropengltnl.cpp index 2c8397c10b7..31c8c28681f 100644 --- a/code/graphics/opengl/gropengltnl.cpp +++ b/code/graphics/opengl/gropengltnl.cpp @@ -1004,23 +1004,14 @@ void opengl_tnl_set_model_material(model_material *material_info) 1); } - if (material_info->get_texture_map(TM_SPECULAR_TYPE) >= 0 || - material_info->get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) { - if (material_info->get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) { - gr_opengl_tcache_set(material_info->get_texture_map(TM_SPEC_GLOSS_TYPE), - TCACHE_TYPE_NORMAL, - &u_scale, - &v_scale, - &array_index, - 2); - } else { - gr_opengl_tcache_set(material_info->get_texture_map(TM_SPECULAR_TYPE), - TCACHE_TYPE_NORMAL, - &u_scale, - &v_scale, - &array_index, - 2); - } + const auto specular_map = material_info->get_specular_map(); + if (specular_map >= 0) { + gr_opengl_tcache_set(specular_map, + TCACHE_TYPE_NORMAL, + &u_scale, + &v_scale, + &array_index, + 2); } if (material_info->get_texture_map(TM_NORMAL_TYPE) >= 0) { diff --git a/code/graphics/uniforms.cpp b/code/graphics/uniforms.cpp index 855fcd145bc..edf65a0a218 100644 --- a/code/graphics/uniforms.cpp +++ b/code/graphics/uniforms.cpp @@ -145,18 +145,12 @@ void convert_model_material(model_uniform_data* data_out, } - // A -reflect (spec-gloss) map wins over a legacy -shine map, as both texture binds do - if (material.get_texture_map(TM_SPEC_GLOSS_TYPE) >= 0) { - data_out->sSpecmapIndex = bm_get_array_index(material.get_texture_map(TM_SPEC_GLOSS_TYPE)); - - data_out->gammaSpec = 1; - data_out->alphaGloss = 1; - - } else if (material.get_texture_map(TM_SPECULAR_TYPE) >= 0) { - data_out->sSpecmapIndex = bm_get_array_index(material.get_texture_map(TM_SPECULAR_TYPE)); - - data_out->gammaSpec = 0; - data_out->alphaGloss = 0; + bool is_spec_gloss; + const auto specular_map = material.get_specular_map(&is_spec_gloss); + if (specular_map >= 0) { + data_out->sSpecmapIndex = bm_get_array_index(specular_map); + data_out->gammaSpec = is_spec_gloss ? 1 : 0; + data_out->alphaGloss = is_spec_gloss ? 1 : 0; } if (material.get_texture_map(TM_NORMAL_TYPE) >= 0) { diff --git a/code/graphics/vulkan/VulkanDraw.cpp b/code/graphics/vulkan/VulkanDraw.cpp index 8a5eeaecd68..5d741571f13 100644 --- a/code/graphics/vulkan/VulkanDraw.cpp +++ b/code/graphics/vulkan/VulkanDraw.cpp @@ -1304,12 +1304,7 @@ bool VulkanDrawManager::bindMaterialTextures(material* mat, DescriptorWriter* wr setTexture(mat->get_texture_map(TM_BASE_TYPE), TextureSlot::BaseMap, true); setTexture(mat->get_texture_map(TM_GLOW_TYPE), TextureSlot::GlowMap); - // Specular - prefer spec_gloss if available - int specMap = mat->get_texture_map(TM_SPEC_GLOSS_TYPE); - if (specMap < 0) { - specMap = mat->get_texture_map(TM_SPECULAR_TYPE); - } - setTexture(specMap, TextureSlot::SpecMap); + setTexture(mat->get_specular_map(), TextureSlot::SpecMap); setTexture(mat->get_texture_map(TM_NORMAL_TYPE), TextureSlot::NormalMap); setTexture(mat->get_texture_map(TM_HEIGHT_TYPE), TextureSlot::HeightMap); @@ -1453,11 +1448,7 @@ bool VulkanDrawManager::applyMaterial(material* mat, primitive_type prim_type, v MaterialSetInputs matInputs; matInputs.texHandles[0] = mat->get_texture_map(TM_BASE_TYPE); matInputs.texHandles[1] = mat->get_texture_map(TM_GLOW_TYPE); - int specMap = mat->get_texture_map(TM_SPEC_GLOSS_TYPE); - if (specMap < 0) { - specMap = mat->get_texture_map(TM_SPECULAR_TYPE); - } - matInputs.texHandles[2] = specMap; + matInputs.texHandles[2] = mat->get_specular_map(); matInputs.texHandles[3] = mat->get_texture_map(TM_NORMAL_TYPE); matInputs.texHandles[4] = mat->get_texture_map(TM_HEIGHT_TYPE); matInputs.texHandles[5] = mat->get_texture_map(TM_AMBIENT_TYPE); diff --git a/test/src/graphics/test_material.cpp b/test/src/graphics/test_material.cpp index eec20b2cc5c..7267adc1ae2 100644 --- a/test/src/graphics/test_material.cpp +++ b/test/src/graphics/test_material.cpp @@ -14,3 +14,29 @@ TEST(DecalMaterial, SceneNormalFlagSetOnlyWithoutDecalNormalMap) with_normal.set_texture_map(TM_NORMAL_TYPE, 1); EXPECT_EQ(with_normal.get_shader_flags(), 0u); } + +TEST(ModelMaterial, SpecularSelectionPrefersReflectAndReportsEncoding) +{ + model_material mat; + bool is_spec_gloss = true; + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), -1); + EXPECT_FALSE(is_spec_gloss); + + mat.set_texture_map(TM_SPECULAR_TYPE, 0); + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 0); + EXPECT_FALSE(is_spec_gloss); + + mat.set_texture_map(TM_SPEC_GLOSS_TYPE, 1); + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 1); + EXPECT_TRUE(is_spec_gloss); + EXPECT_EQ(mat.get_specular_map(), 1); + + mat.set_texture_map(TM_SPECULAR_TYPE, -1); + mat.set_texture_map(TM_SPEC_GLOSS_TYPE, 0); + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), 0); + EXPECT_TRUE(is_spec_gloss); + + mat.set_texture_map(TM_SPEC_GLOSS_TYPE, -1); + EXPECT_EQ(mat.get_specular_map(&is_spec_gloss), -1); + EXPECT_FALSE(is_spec_gloss); +}