Skip to content

Commit

Permalink
Fix u_TextureMatrix with material system
Browse files Browse the repository at this point in the history
  • Loading branch information
VReaperV committed Jan 12, 2025
1 parent e261abf commit 232b0b3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,8 @@ void MaterialSystem::GenerateTexturesBuffer( std::vector<TextureData>& textures,
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[bundle][12] * 32768.0f;
textureBundles->textureMatrix2[1] = tess.svars.texMatrices[bundle][13] * 32768.0f;
textureBundles->textureMatrix[4] = tess.svars.texMatrices[bundle][12];
textureBundles->textureMatrix[5] = tess.svars.texMatrices[bundle][13];
textureBundles++;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ struct Material {
};

struct TexBundle {
vec_t textureMatrix[4];
uint32_t textureMatrix2[2];
vec_t textureMatrix[6];
GLuint64 textures[MAX_TEXTURE_BUNDLES];
};

Expand Down
23 changes: 13 additions & 10 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,20 +1372,23 @@ std::string GLShaderManager::ShaderPostProcess( GLShader *shader, const std::str
: "layout(std430, binding = 7) restrict readonly buffer texDataSSBO {\n"
" TexData texData[];\n"
"};\n\n";
// We have to store these as a bunch of vec4/uvec4 because std140 pads everything to a vec4
// We have to store u_TextureMatrix as vec4 + vec2 because otherwise it would be aligned to a vec4 under std140
std::string texDataBlock = "struct TexData {\n"
" vec4 u_TextureMatrix;\n"
" uvec4 u_TextureDiffuseMap;\n"
" uvec4 u_NormalHeightMap;\n"
" uvec4 u_MaterialGlowMap;\n"
" vec2 u_TextureMatrix2;\n"
" uvec2 u_DiffuseMap;\n"
" uvec2 u_NormalMap;\n"
" uvec2 u_HeightMap;\n"
" uvec2 u_MaterialMap;\n"
" uvec2 u_GlowMap;\n"
"};\n\n"
+ texBuf +
"#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"
"#define u_MaterialMap_initial uvec2( texData[( baseInstance >> 12 ) & 0xFFF].u_MaterialGlowMap.xy )\n"
"#define u_GlowMap_initial uvec2( texData[( baseInstance >> 12 ) & 0xFFF].u_MaterialGlowMap.zw )\n\n"
"#define u_TextureMatrix mat3x2( texData[( baseInstance >> 12 ) & 0xFFF].u_TextureMatrix.xy, texData[( baseInstance >> 12 ) & 0xFFF].u_TextureMatrix.zw, texData[( baseInstance >> 12 ) & 0xFFF].u_TextureMatrix2 )\n"
"#define u_DiffuseMap_initial texData[( baseInstance >> 12 ) & 0xFFF].u_DiffuseMap\n"
"#define u_NormalMap_initial texData[( baseInstance >> 12 ) & 0xFFF].u_NormalMap\n"
"#define u_HeightMap_initial texData[( baseInstance >> 12 ) & 0xFFF].u_HeightMap\n"
"#define u_MaterialMap_initial texData[( baseInstance >> 12 ) & 0xFFF].u_MaterialMap\n"
"#define u_GlowMap_initial texData[( baseInstance >> 12 ) & 0xFFF].u_GlowMap\n\n"
"struct LightMapData {\n"
" uvec2 u_LightMap;\n"
" uvec2 u_DeluxeMap;\n"
Expand Down

0 comments on commit 232b0b3

Please sign in to comment.