Skip to content

Commit dfd9fe7

Browse files
committed
Use vertex interface blocks for vertex shader attributes
This is a minor cleanup of how the engine passes vertex attribute data from one shader stage to the next. The current code uses standalone attribute variables which require a unique name over the entire shader program which means that the variable names need to be different when a geometry shader is used which makes the code more complicated and harder to understand.
1 parent be9a881 commit dfd9fe7

3 files changed

Lines changed: 163 additions & 105 deletions

File tree

code/def_files/data/effects/main-f.sdr

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ layout (std140) uniform modelData {
9696
int sMiscmapIndex;
9797
};
9898

99+
in VertexOutput {
100+
#ifdef FLAG_ENV_MAP
101+
vec3 envReflect;
102+
#endif
103+
#ifdef FLAG_NORMAL_MAP
104+
mat3 tangentMatrix;
105+
#endif
106+
#ifdef FLAG_FOG
107+
float fogDist;
108+
#endif
109+
#ifdef WORKAROUND_CLIPPING_PLANES
110+
#ifdef FLAG_TRANSFORM
111+
float notVisible;
112+
#endif
113+
#endif
114+
vec4 position;
115+
vec3 normal;
116+
vec4 texCoord;
117+
#ifdef FLAG_SHADOWS
118+
vec4 shadowUV[4];
119+
vec4 shadowPos;
120+
#endif
121+
} vertIn;
122+
99123
#ifdef FLAG_DIFFUSE_MAP
100124
uniform sampler2DArray sBasemap;
101125
#endif
@@ -107,41 +131,27 @@ uniform sampler2DArray sSpecmap;
107131
#endif
108132
#ifdef FLAG_ENV_MAP
109133
uniform samplerCube sEnvmap;
110-
in vec3 fragEnvReflect;
111134
#endif
112135
#ifdef FLAG_NORMAL_MAP
113136
uniform sampler2DArray sNormalmap;
114-
in mat3 fragTangentMatrix;
115137
#endif
116138
#ifdef FLAG_AMBIENT_MAP
117139
uniform sampler2DArray sAmbientmap;
118140
#endif
119-
#ifdef FLAG_FOG
120-
in float fragFogDist;
121-
#endif
122141
#ifdef FLAG_ANIMATED
123142
uniform sampler2D sFramebuffer;
124143
#endif
125-
#ifdef WORKAROUND_CLIPPING_PLANES
126-
#ifdef FLAG_TRANSFORM
127-
in float fragNotVisible;
128-
#endif
129-
#endif
130144
#ifdef FLAG_TEAMCOLOR
131145
vec2 teamMask = vec2(0.0, 0.0);
132146
#endif
133147
#ifdef FLAG_MISC_MAP
134148
uniform sampler2DArray sMiscmap;
135149
#endif
136150
#ifdef FLAG_SHADOWS
137-
in vec4 fragShadowUV[4];
138-
in vec4 fragShadowPos;
139151
uniform sampler2DArray shadow_map;
140152
#endif
141153
#define SRGB_GAMMA 2.2
142-
in vec4 fragPosition;
143-
in vec3 fragNormal;
144-
in vec4 fragTexCoord;
154+
145155
out vec4 fragOut0;
146156
out vec4 fragOut1;
147157
out vec4 fragOut2;
@@ -161,17 +171,17 @@ void GetLightInfo(int i, out vec3 lightDir, out float attenuation)
161171
attenuation = 1.0;
162172
if (lights[i].light_type != LT_DIRECTIONAL) {
163173
// Positional light source
164-
float dist = distance(lights[i].position.xyz, fragPosition.xyz);
165-
lightDir = (lights[i].position.xyz - fragPosition.xyz);
174+
float dist = distance(lights[i].position.xyz, vertIn.position.xyz);
175+
lightDir = (lights[i].position.xyz - vertIn.position.xyz);
166176

167177
if (lights[i].light_type == LT_TUBE) { // Tube light
168178
float beamlength = length(lights[i].direction);
169179
vec3 beamDir = normalize(lights[i].direction);
170180
// Get nearest point on line
171-
float neardist = dot(fragPosition.xyz - lights[i].position.xyz, beamDir);
181+
float neardist = dot(vertIn.position.xyz - lights[i].position.xyz, beamDir);
172182
// Move back from the endpoint of the beam along the beam by the distance we calculated
173183
vec3 nearest = lights[i].position.xyz - beamDir * abs(neardist);
174-
lightDir = nearest - fragPosition.xyz;
184+
lightDir = nearest - vertIn.position.xyz;
175185
dist = length(lightDir);
176186
}
177187

@@ -181,7 +191,7 @@ void GetLightInfo(int i, out vec3 lightDir, out float attenuation)
181191
}
182192
vec3 CalculateLighting(vec3 normal, vec3 diffuseMaterial, vec3 specularMaterial, float gloss, float fresnel, float shadow, float aoFactor)
183193
{
184-
vec3 eyeDir = vec3(normalize(-fragPosition).xyz);
194+
vec3 eyeDir = vec3(normalize(-vertIn.position).xyz);
185195
vec3 lightAmbient = (emissionFactor + ambientFactor * ambientFactor) * aoFactor; // ambientFactor^2 due to legacy OpenGL compatibility behavior
186196
vec3 lightDiffuse = vec3(0.0, 0.0, 0.0);
187197
vec3 lightSpecular = vec3(0.0, 0.0, 0.0);
@@ -208,18 +218,18 @@ void main()
208218
{
209219
#ifdef WORKAROUND_CLIPPING_PLANES
210220
#ifdef FLAG_TRANSFORM
211-
if(fragNotVisible >= 0.9) { discard; }
221+
if(vertIn.notVisible >= 0.9) { discard; }
212222
#endif
213223
#endif
214224
#ifdef FLAG_SHADOW_MAP
215225
// need depth and depth squared for variance shadow maps
216-
fragOut0 = vec4(fragPosition.z, fragPosition.z * fragPosition.z * VARIANCE_SHADOW_SCALE_INV, 0.0, 1.0);
226+
fragOut0 = vec4(vertIn.position.z, vertIn.position.z * vertIn.position.z * VARIANCE_SHADOW_SCALE_INV, 0.0, 1.0);
217227
if (true) {
218228
return;
219229
}
220230
#endif
221-
vec3 eyeDir = vec3(normalize(-fragPosition).xyz);
222-
vec2 texCoord = fragTexCoord.xy;
231+
vec3 eyeDir = vec3(normalize(-vertIn.position).xyz);
232+
vec2 texCoord = vertIn.texCoord.xy;
223233
vec4 baseColor = color;
224234
vec4 specColor = vec4(0.0, 0.0, 0.0, 1.0);
225235
vec4 emissiveColor = vec4(0.0, 0.0, 0.0, 1.0);
@@ -234,14 +244,14 @@ void main()
234244
// green is cavity occlusion factor which only affects diffuse and specular lighting.
235245
aoFactors = texture(sAmbientmap, vec3(texCoord, float(sAmbientmapIndex))).xy;
236246
#endif
237-
vec3 unitNormal = normalize(fragNormal);
247+
vec3 unitNormal = normalize(vertIn.normal);
238248
vec3 normal = unitNormal;
239249
#ifdef FLAG_NORMAL_MAP
240250
// Normal map - convert from DXT5nm
241251
vec2 normalSample;
242252
normal.rg = normalSample = (texture(sNormalmap, vec3(texCoord, float(sNormalmapIndex))).ag * 2.0) - 1.0;
243253
normal.b = clamp(sqrt(1.0 - dot(normal.rg, normal.rg)), 0.0001, 1.0);
244-
normal = fragTangentMatrix * normal;
254+
normal = vertIn.tangentMatrix * normal;
245255
float norm = length(normal);
246256
// prevent breaking of normal maps
247257
if (norm > 0.0)
@@ -251,7 +261,7 @@ void main()
251261
#endif
252262

253263
#ifdef FLAG_ANIMATED
254-
vec2 distort = vec2(cos(fragPosition.x*fragPosition.w*0.005+anim_timer*20.0)*sin(fragPosition.y*fragPosition.w*0.005),sin(fragPosition.x*fragPosition.w*0.005+anim_timer*20.0)*cos(fragPosition.y*fragPosition.w*0.005))*0.03;
264+
vec2 distort = vec2(cos(vertIn.position.x*vertIn.position.w*0.005+anim_timer*20.0)*sin(vertIn.position.y*vertIn.position.w*0.005),sin(vertIn.position.x*vertIn.position.w*0.005+anim_timer*20.0)*cos(vertIn.position.y*vertIn.position.w*0.005))*0.03;
255265
#endif
256266

257267
#ifdef FLAG_DIFFUSE_MAP
@@ -316,7 +326,7 @@ void main()
316326
#ifdef FLAG_LIGHT
317327
float shadow = 1.0;
318328
#ifdef FLAG_SHADOWS
319-
shadow = getShadowValue(shadow_map, -fragPosition.z, fragShadowPos.z, fragShadowUV, fardist, middist, neardist, veryneardist);
329+
shadow = getShadowValue(shadow_map, -vertIn.position.z, vertIn.shadowPos.z, vertIn.shadowUV, fardist, middist, neardist, veryneardist);
320330
#endif
321331
baseColor.rgb = CalculateLighting(normal, baseColor.rgb, specColor.rgb, glossData, fresnelFactor, shadow, aoFactors.x);
322332
#else
@@ -326,7 +336,7 @@ void main()
326336
#endif
327337
#endif
328338
#ifdef FLAG_ENV_MAP
329-
vec3 envReflectNM = fragEnvReflect;
339+
vec3 envReflectNM = vertIn.envReflect;
330340
#ifdef FLAG_NORMAL_MAP
331341
envReflectNM += vec3(normalSample, 0.0);
332342
envReflectNM = normalize(envReflectNM);
@@ -362,8 +372,8 @@ void main()
362372
#ifdef FLAG_DIFFUSE_MAP
363373
if(blend_alpha == 1) finalFogColor *= baseColor.a;
364374
#endif
365-
baseColor.rgb = mix(baseColor.rgb, finalFogColor, fragFogDist);
366-
specColor.rgb *= fragFogDist;
375+
baseColor.rgb = mix(baseColor.rgb, finalFogColor, vertIn.fogDist);
376+
specColor.rgb *= vertIn.fogDist;
367377
#endif
368378

369379
#ifdef FLAG_DIFFUSE_MAP
@@ -378,21 +388,21 @@ void main()
378388
baseColor.a = baseColor.a * clamp(shinefactor * (fract(abs(texCoord.x))-anim_timer) * -10000.0,0.0,1.0);
379389
}
380390
if (effect_num == 1) {
381-
float shinefactor = 1.0/(1.0 + pow(abs(fragPosition.y-anim_timer), 2.0));
391+
float shinefactor = 1.0/(1.0 + pow(abs(vertIn.position.y-anim_timer), 2.0));
382392
emissiveColor.rgb += vec3(shinefactor);
383393
#ifndef FLAG_LIGHT
384394
// ATI Wireframe fix *grumble*
385-
baseColor.a = clamp((fragPosition.y-anim_timer) * 10000.0,0.0,1.0);
395+
baseColor.a = clamp((vertIn.position.y-anim_timer) * 10000.0,0.0,1.0);
386396
#endif
387397
}
388398
if (effect_num == 2) {
389399
vec2 screenPos = gl_FragCoord.xy * vec2(vpwidth,vpheight);
390400
baseColor.a = baseColor.a;
391-
float cloak_interp = (sin(fragPosition.x*fragPosition.w*0.005+anim_timer*20.0)*sin(fragPosition.y*fragPosition.w*0.005)*0.5)-0.5;
401+
float cloak_interp = (sin(vertIn.position.x*vertIn.position.w*0.005+anim_timer*20.0)*sin(vertIn.position.y*vertIn.position.w*0.005)*0.5)-0.5;
392402
#ifdef FLAG_LIGHT
393403
baseColor.rgb = mix(texture(sFramebuffer, screenPos + distort*anim_timer + anim_timer*0.1*normal.xy).rgb,baseColor.rgb,clamp(cloak_interp+anim_timer*2.0,0.0,1.0));
394404
#else
395-
baseColor.rgb = mix(texture(sFramebuffer, screenPos + distort*anim_timer + anim_timer*0.1*fragNormal.xy).rgb,baseColor.rgb,clamp(cloak_interp+anim_timer*2.0,0.0,1.0));
405+
baseColor.rgb = mix(texture(sFramebuffer, screenPos + distort*anim_timer + anim_timer*0.1*vertIn.normal.xy).rgb,baseColor.rgb,clamp(cloak_interp+anim_timer*2.0,0.0,1.0));
396406
#endif
397407
}
398408
#endif
@@ -406,7 +416,7 @@ void main()
406416
#endif
407417
fragOut0 = baseColor;
408418
#ifdef FLAG_DEFERRED
409-
fragOut1 = vec4(fragPosition.xyz, 1.0);
419+
fragOut1 = vec4(vertIn.position.xyz, 1.0);
410420
fragOut2 = vec4(normal, glossData);
411421
fragOut3 = vec4(specColor.rgb, fresnelFactor);
412422
fragOut4 = emissiveColor;

code/def_files/data/effects/main-g.sdr

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,42 +96,96 @@ layout (std140) uniform modelData {
9696
int sMiscmapIndex;
9797
};
9898

99-
#if !defined(GL_ARB_gpu_shader5)
100-
in float geoInstance[];
99+
in VertexOutput {
100+
#ifdef FLAG_ENV_MAP
101+
vec3 envReflect;
101102
#endif
102-
in vec3 geoNormal[];
103-
in vec4 geoTexCoord[];
104-
out vec4 fragPosition;
105-
out vec3 fragNormal;
106-
out vec4 fragTexCoord;
103+
#ifdef FLAG_NORMAL_MAP
104+
mat3 tangentMatrix;
105+
#endif
106+
#ifdef FLAG_FOG
107+
float fogDist;
108+
#endif
109+
#ifdef WORKAROUND_CLIPPING_PLANES
110+
#ifdef FLAG_TRANSFORM
111+
float notVisible;
112+
#endif
113+
#endif
114+
#ifdef FLAG_SHADOW_MAP
115+
#if !defined(GL_ARB_gpu_shader5)
116+
float instance;
117+
#endif
118+
#else
119+
vec4 position;
120+
#endif
121+
vec3 normal;
122+
vec4 texCoord;
123+
#ifdef FLAG_SHADOWS
124+
vec4 shadowUV[4];
125+
vec4 shadowPos;
126+
#endif
127+
} vertIn[];
107128

129+
out VertexOutput {
130+
#ifdef FLAG_ENV_MAP
131+
vec3 envReflect;
132+
#endif
133+
#ifdef FLAG_NORMAL_MAP
134+
mat3 tangentMatrix;
135+
#endif
136+
#ifdef FLAG_FOG
137+
float fogDist;
138+
#endif
108139
#ifdef WORKAROUND_CLIPPING_PLANES
109-
#ifdef FLAG_TRANSFORM
110-
in float geoNotVisible[];
111-
out float fragNotVisible;
140+
#ifdef FLAG_TRANSFORM
141+
float notVisible;
142+
#endif
112143
#endif
144+
vec4 position;
145+
vec3 normal;
146+
vec4 texCoord;
147+
#ifdef FLAG_SHADOWS
148+
vec4 shadowUV[4];
149+
vec4 shadowPos;
113150
#endif
151+
} vertOut;
114152

115153
void main(void)
116154
{
117155
#ifdef GL_ARB_gpu_shader5
118156
int instanceID = gl_InvocationID;
119157
#else
120-
int instanceID = int(geoInstance[0]);
158+
int instanceID = int(vertIn[0].instance);
121159
#endif
122160
for(int vert = 0; vert < gl_in.length(); vert++)
123161
{
124162
gl_Position = shadow_proj_matrix[instanceID] * gl_in[vert].gl_Position;
125163
if(gl_Position.z < -1.0)
126164
gl_Position.z = -1.0;
127-
fragPosition = gl_in[vert].gl_Position;
128-
fragNormal = geoNormal[vert];
129-
fragTexCoord = geoTexCoord[vert];
165+
vertOut.position = gl_in[vert].gl_Position;
166+
vertOut.normal = vertIn[vert].normal;
167+
vertOut.texCoord = vertIn[vert].texCoord;
130168

131169
gl_Layer = instanceID;
170+
#ifdef FLAG_ENV_MAP
171+
vertOut.envReflect = vertIn[vert].envReflect;
172+
#endif
173+
#ifdef FLAG_NORMAL_MAP
174+
vertOut.tangentMatrix = vertIn[vert].tangentMatrix;
175+
#endif
176+
#ifdef FLAG_FOG
177+
vertOut.fogDist = vertIn[vert].fogDist;
178+
#endif
179+
#ifdef FLAG_SHADOWS
180+
vertOut.shadowUV[0] = vertIn[vert].shadowUV[0];
181+
vertOut.shadowUV[1] = vertIn[vert].shadowUV[1];
182+
vertOut.shadowUV[2] = vertIn[vert].shadowUV[2];
183+
vertOut.shadowUV[3] = vertIn[vert].shadowUV[3];
184+
vertOut.shadowPos = vertIn[vert].shadowPos;
185+
#endif
132186
#ifdef WORKAROUND_CLIPPING_PLANES
133187
#ifdef FLAG_TRANSFORM
134-
fragNotVisible = geoNotVisible[0];
188+
vertOut.notVisible = vertIn[vert].notVisible;
135189
#endif
136190
#ifdef FLAG_CLIP
137191
gl_ClipDistance[0] = gl_in[vert].gl_ClipDistance[0];

0 commit comments

Comments
 (0)