Skip to content

Commit

Permalink
Fix material system texture matrices for non-colour bundles
Browse files Browse the repository at this point in the history
Also fixes an incorrect matrix constructor in shaders.
  • Loading branch information
VReaperV committed Jan 11, 2025
1 parent 2edf931 commit 217028e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,17 +527,17 @@ void MaterialSystem::GenerateTexturesBuffer( std::vector<TextureData>& textures,
}
}

// While reflection, liquid and heatHaze shaders use the matrix from TB_NORMALMAP bundle, it's never actually parsed
RB_CalcTexMatrix( textureData.texBundles[0], tess.svars.texMatrices[TB_COLORMAP] );
const int bundle = textureData.textureMatrixBundle;
RB_CalcTexMatrix( textureData.texBundles[bundle], tess.svars.texMatrices[bundle] );
/* We only actually need these 6 components to get the correct texture transformation,
the other ones are unused */
textureBundles->textureMatrix[0] = tess.svars.texMatrices[TB_COLORMAP][0];
textureBundles->textureMatrix[1] = tess.svars.texMatrices[TB_COLORMAP][1];
textureBundles->textureMatrix[2] = tess.svars.texMatrices[TB_COLORMAP][4];
textureBundles->textureMatrix[3] = tess.svars.texMatrices[TB_COLORMAP][5];
textureBundles->textureMatrix[0] = tess.svars.texMatrices[bundle][0];
textureBundles->textureMatrix[1] = tess.svars.texMatrices[bundle][1];
textureBundles->textureMatrix[2] = tess.svars.texMatrices[bundle][4];
textureBundles->textureMatrix[3] = tess.svars.texMatrices[bundle][5];
// These are stored as part of a vec4 in a UBO, so we need to convert them here as well
textureBundles->textureMatrix2[0] = tess.svars.texMatrices[TB_COLORMAP][12] * 32768.0f;
textureBundles->textureMatrix2[1] = tess.svars.texMatrices[TB_COLORMAP][13] * 32768.0f;
textureBundles->textureMatrix2[0] = tess.svars.texMatrices[bundle][12] * 32768.0f;
textureBundles->textureMatrix2[1] = tess.svars.texMatrices[bundle][13] * 32768.0f;
textureBundles++;
}
}
Expand Down Expand Up @@ -1553,7 +1553,9 @@ void MaterialSystem::AddStageTextures( drawSurf_t* drawSurf, const uint32_t stag

int bundleNum = 0;
bool dynamic = false;
for ( const textureBundle_t& bundle : pStage->bundle ) {
for ( int i = 0; i < MAX_TEXTURE_BUNDLES; i++ ) {
const textureBundle_t& bundle = pStage->bundle[i];

if ( bundle.isVideoMap ) {
material->AddTexture( tr.cinematicImage[bundle.videoMapHandle]->texture );
continue;
Expand All @@ -1566,6 +1568,7 @@ void MaterialSystem::AddStageTextures( drawSurf_t* drawSurf, const uint32_t stag
}

if ( bundle.numImages > 1 || bundle.numTexMods > 0 ) {
textureData.textureMatrixBundle = i;
dynamic = true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ struct TextureData {
const textureBundle_t* texBundles[MAX_TEXTURE_BUNDLES] = { nullptr, nullptr, nullptr, nullptr, nullptr };
// For ST_STYLELIGHTMAP stages
image_t* texBundlesOverride[MAX_TEXTURE_BUNDLES] = { nullptr, nullptr, nullptr, nullptr, nullptr };
int textureMatrixBundle = 0;

bool operator==( const TextureData& other ) const {
for ( int i = 0; i < MAX_TEXTURE_BUNDLES; i++ ) {
Expand Down Expand Up @@ -195,6 +196,7 @@ struct TextureData {
TextureData( const TextureData& other ) {
memcpy( texBundles, other.texBundles, MAX_TEXTURE_BUNDLES * sizeof( textureBundle_t* ) );
memcpy( texBundlesOverride, other.texBundlesOverride, MAX_TEXTURE_BUNDLES * sizeof( image_t* ) );
textureMatrixBundle = other.textureMatrixBundle;
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ std::string GLShaderManager::ShaderPostProcess( GLShader *shader, const std::str
" uvec4 u_MaterialGlowMap;\n"
"};\n\n"
+ texBuf +
"#define u_TextureMatrix mat3x2( texData[( baseInstance >> 12 ) & 0xFFF].u_TextureMatrix, vec2( texData[( baseInstance >> 12 ) & 0xFFF].u_TextureDiffuseMap.xy ) * vec2( 1.0f / 32768.0f ) )\n"
"#define u_TextureMatrix mat3x2( texData[( baseInstance >> 12 ) & 0xFFF].u_TextureMatrix.xy, texData[( baseInstance >> 12 ) & 0xFFF].u_TextureMatrix.zw, vec2( texData[( baseInstance >> 12 ) & 0xFFF].u_TextureDiffuseMap.xy ) * vec2( 1.0f / 32768.0f ) )\n"
"#define u_DiffuseMap_initial uvec2( texData[( baseInstance >> 12 ) & 0xFFF].u_TextureDiffuseMap.zw )\n"
"#define u_NormalMap_initial uvec2( texData[( baseInstance >> 12 ) & 0xFFF].u_NormalHeightMap.xy )\n"
"#define u_HeightMap_initial uvec2( texData[( baseInstance >> 12 ) & 0xFFF].u_NormalHeightMap.zw )\n"
Expand Down

0 comments on commit 217028e

Please sign in to comment.