Skip to content

Commit 393c718

Browse files
committed
Ensure correct alignment for buffer writes
1 parent be42dff commit 393c718

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,7 @@ void GLShader::PostProcessUniforms() {
21302130
size = components ? PAD( size, 4 ) * components : size;
21312131
GLuint alignmentConsume = 4 - size % 4;
21322132

2133+
GLUniform* tmpUniform = _materialSystemUniforms[0];
21332134
tmp.emplace_back( _materialSystemUniforms[0] );
21342135
_materialSystemUniforms.erase( _materialSystemUniforms.begin() );
21352136

@@ -2138,10 +2139,16 @@ void GLShader::PostProcessUniforms() {
21382139
&& ( uniform = FindUniformForAlignment( _materialSystemUniforms, alignmentConsume ) ) != -1 ) {
21392140
alignmentConsume -= _materialSystemUniforms[uniform]->GetSTD430Size();
21402141

2142+
tmpUniform = _materialSystemUniforms[uniform];
2143+
21412144
tmp.emplace_back( _materialSystemUniforms[uniform] );
21422145
_materialSystemUniforms.erase( _materialSystemUniforms.begin() + uniform );
21432146
}
21442147

2148+
if ( alignmentConsume ) {
2149+
tmpUniform->SetSTD430Size( tmpUniform->GetSTD430Size() + alignmentConsume );
2150+
}
2151+
21452152
size = PAD( size, 4 );
21462153
std430Size += size;
21472154
padding = alignmentConsume;

src/engine/renderer/gl_shader.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ class GLUniform
414414
const std::string _type;
415415

416416
// In multiples of 4 bytes
417-
const GLuint _std430Size;
417+
GLuint _std430Size;
418418
const GLuint _std430Alignment;
419419

420420
const bool _global; // This uniform won't go into the materials UBO if true
@@ -460,6 +460,10 @@ class GLUniform
460460
return _type;
461461
}
462462

463+
void SetSTD430Size( const GLuint size ) {
464+
_std430Size = size;
465+
}
466+
463467
GLuint GetSTD430Size() const;
464468

465469
GLuint GetSTD430Alignment() const;
@@ -525,15 +529,13 @@ class GLUniformSampler : protected GLUniform {
525529
}
526530

527531
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
528-
uint32_t* bufferNext = buffer;
529532
if ( glConfig2.usingBindlessTextures ) {
530533
memcpy( buffer, &currentValueBindless, sizeof( GLuint64 ) );
531-
bufferNext += 2;
532534
} else {
533535
memcpy( buffer, &currentValue, sizeof( GLint ) );
534-
bufferNext += 1;
535536
}
536-
return bufferNext;
537+
538+
return buffer + _std430Size;
537539
}
538540

539541
private:
@@ -692,7 +694,7 @@ class GLUniform1i : protected GLUniform
692694

693695
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
694696
memcpy( buffer, &currentValue, sizeof( int ) );
695-
return buffer + 1;
697+
return buffer + _std430Size;
696698
}
697699

698700
private:
@@ -735,7 +737,7 @@ class GLUniform1ui : protected GLUniform {
735737

736738
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
737739
memcpy( buffer, &currentValue, sizeof( uint ) );
738-
return buffer + 1;
740+
return buffer + _std430Size;
739741
}
740742

741743
private:
@@ -780,7 +782,7 @@ class GLUniform1Bool : protected GLUniform {
780782

781783
uint32_t* WriteToBuffer( uint32_t *buffer ) override {
782784
memcpy( buffer, &currentValue, sizeof( bool ) );
783-
return buffer + 1;
785+
return buffer + _std430Size;
784786
}
785787

786788
private:
@@ -828,7 +830,7 @@ class GLUniform1f : protected GLUniform
828830

829831
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
830832
memcpy( buffer, &currentValue, sizeof( float ) );
831-
return buffer + 1;
833+
return buffer + _std430Size;
832834
}
833835

834836
private:
@@ -862,7 +864,7 @@ class GLUniform1fv : protected GLUniform
862864

863865
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
864866
memcpy( buffer, currentValue.data(), currentValue.size() * sizeof( float ) );
865-
return buffer + _components;
867+
return buffer + _components * _std430Size;
866868
}
867869

868870
private:
@@ -913,7 +915,7 @@ class GLUniform2f : protected GLUniform
913915

914916
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
915917
memcpy( buffer, &currentValue, sizeof( vec2_t ) );
916-
return buffer + 2;
918+
return buffer + _std430Size;
917919
}
918920

919921
private:
@@ -964,7 +966,7 @@ class GLUniform3f : protected GLUniform
964966

965967
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
966968
memcpy( buffer, &currentValue, sizeof( vec3_t ) );
967-
return buffer + 3;
969+
return buffer + _std430Size;
968970
}
969971

970972
private:
@@ -1016,7 +1018,7 @@ class GLUniform4f : protected GLUniform
10161018

10171019
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
10181020
memcpy( buffer, &currentValue, sizeof( vec4_t ) );
1019-
return buffer + 4;
1021+
return buffer + _std430Size;
10201022
}
10211023

10221024
private:
@@ -1051,7 +1053,7 @@ class GLUniform4fv : protected GLUniform
10511053
public:
10521054
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
10531055
memcpy( buffer, currentValue.data(), currentValue.size() * sizeof( float ) );
1054-
return buffer + 4 * _components;
1056+
return buffer + _std430Size * _components;
10551057
}
10561058

10571059
private:
@@ -1100,7 +1102,7 @@ class GLUniformMatrix4f : protected GLUniform
11001102

11011103
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
11021104
memcpy( buffer, &currentValue, sizeof( matrix_t ) );
1103-
return buffer + 16;
1105+
return buffer + _std430Size;
11041106
}
11051107

11061108
private:
@@ -1134,7 +1136,7 @@ class GLUniformMatrix32f : protected GLUniform {
11341136

11351137
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
11361138
memcpy( buffer, currentValue, 6 * sizeof( float ) );
1137-
return buffer + 6 * _components;
1139+
return buffer + _std430Size * _components;
11381140
}
11391141

11401142
private:
@@ -1169,7 +1171,7 @@ class GLUniformMatrix4fv : protected GLUniform
11691171
public:
11701172
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
11711173
memcpy( buffer, currentValue.data(), currentValue.size() * sizeof( float ) );
1172-
return buffer + 16 * _components;
1174+
return buffer + _std430Size * _components;
11731175
}
11741176

11751177
private:
@@ -1203,7 +1205,7 @@ class GLUniformMatrix34fv : protected GLUniform
12031205
public:
12041206
uint32_t* WriteToBuffer( uint32_t* buffer ) override {
12051207
memcpy( buffer, currentValue.data(), currentValue.size() * sizeof( float ) );
1206-
return buffer + 12 * _components;
1208+
return buffer + _std430Size * _components;
12071209
}
12081210

12091211
private:

0 commit comments

Comments
 (0)