Skip to content

Commit d4d98e2

Browse files
authored
Fix shader issues (jMonkeyEngine#2828)
* Fix GLSL int/float type mismatches in shaders * fix uninitialized variables * add defensive initialization * Fix floating-point in Downsample shader texture sampling * more fixes
1 parent d5cdad6 commit d4d98e2

6 files changed

Lines changed: 71 additions & 49 deletions

File tree

jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter15.frag

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ void main() {
140140
for (int i = 0; i < m_NumSamples; i++){
141141
color += main_multiSample(i);
142142
}
143-
gl_FragColor = color / m_NumSamples;
143+
gl_FragColor = color / float(m_NumSamples);
144144
#else
145145
gl_FragColor = main_multiSample(0);
146146
#endif
147147
}
148148

149149

150-

jme3-core/src/main/resources/Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,24 @@
214214

215215

216216
#if defined(ENABLE_PBRLightingUtils_computeDirectLightContribution) || defined(ENABLE_PBRLightingUtils_newLight)
217-
Light PBRLightingUtils_newLight(vec4 color, vec3 position, float type, float invRadius, float spotAngleCos, vec3 spotDirection){
218-
Light l;
219-
l.color = color;
220-
l.position = position;
221-
l.type = type;
222-
l.invRadius = invRadius;
223-
l.spotAngleCos = spotAngleCos;
224-
l.spotDirection = spotDirection;
225-
l.ready = false;
226-
return l;
227-
}
217+
Light PBRLightingUtils_newLight(vec4 color, vec3 position, float type, float invRadius, float spotAngleCos, vec3 spotDirection){
218+
Light l;
219+
l.color = color;
220+
l.position = position;
221+
l.type = type;
222+
l.invRadius = invRadius;
223+
l.spotAngleCos = spotAngleCos;
224+
l.spotDirection = spotDirection;
225+
l.NdotL = 0.0;
226+
l.NdotH = 0.0;
227+
l.LdotH = 0.0;
228+
l.HdotV = 0.0;
229+
l.vector = vec3(0.0);
230+
l.dir = vec3(0.0);
231+
l.fallOff = 0.0;
232+
l.ready = false;
233+
return l;
234+
}
228235
#endif
229236

230237

@@ -282,23 +289,33 @@
282289

283290
PBRSurface surface; //creates a new PBRSurface
284291

285-
surface.position = wPosition;
286-
surface.viewDir = wViewDir;
287-
surface.geometryNormal = normalize(wNormal);
288-
289-
//set default values
290-
surface.hasTangents = false;
291-
surface.hasBasicLightMap = false;
292-
surface.albedo = vec3(1.0);
293-
surface.normal = surface.geometryNormal;
294-
surface.emission = vec3(0.0);
292+
surface.position = wPosition;
293+
surface.viewDir = wViewDir;
294+
surface.geometryNormal = normalize(wNormal);
295+
296+
//set default values
297+
surface.frontFacing = true;
298+
surface.depth = 0.0;
299+
surface.tbnMat = mat3(1.0);
300+
surface.hasTangents = false;
301+
surface.hasBasicLightMap = false;
302+
surface.albedo = vec3(1.0);
303+
surface.normal = surface.geometryNormal;
304+
surface.emission = vec3(0.0);
295305
surface.ao = vec3(1.0);
296306
surface.lightMapColor = vec3(0.0);
297-
surface.alpha = 1.0;
298-
surface.roughness = 1.0;
299-
surface.metallic = 0.0;
300-
surface.alpha = 1.0;
301-
surface.exposure = 1.0;
307+
surface.alpha = 1.0;
308+
surface.roughness = 1.0;
309+
surface.metallic = 0.0;
310+
surface.exposure = 1.0;
311+
surface.diffuseColor = vec3(1.0);
312+
surface.specularColor = vec3(0.04);
313+
surface.fZero = vec3(0.04);
314+
surface.NdotV = 1.0;
315+
surface.bakedLightContribution = vec3(0.0);
316+
surface.directLightContribution = vec3(0.0);
317+
surface.envLightContribution = vec3(0.0);
318+
surface.brightestNonGlobalLightStrength = 0.0;
302319

303320
return surface;
304321
}
@@ -495,7 +512,11 @@
495512

496513

497514
void PBRLightingUtils_calculatePreLightingValues(inout PBRSurface surface){
498-
#ifdef SPECGLOSSPIPELINE
515+
surface.frontFacing = gl_FrontFacing;
516+
surface.depth = gl_FragCoord.z;
517+
surface.NdotV = clamp(abs(dot(surface.normal, surface.viewDir)), 0.001, 1.0);
518+
519+
#ifdef SPECGLOSSPIPELINE
499520
float maxSpecular = max(max(surface.specularColor.r, surface.specularColor.g), surface.specularColor.b);
500521
surface.diffuseColor = surface.albedo * (1.0 - clamp(maxSpecular, 0.0, 1.0));
501522
surface.fZero = surface.specularColor.xyz;

jme3-effects/src/main/resources/Common/MatDefs/Post/Downsample.frag

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ void main() {
2020
// - l - m -
2121
// g - h - i
2222
// === ('e' is the current texel) ===
23-
vec3 a = getColor(m_Texture, vec2(texCoord.x - 2*x, texCoord.y + 2*y)).rgb;
24-
vec3 b = getColor(m_Texture, vec2(texCoord.x, texCoord.y + 2*y)).rgb;
25-
vec3 c = getColor(m_Texture, vec2(texCoord.x + 2*x, texCoord.y + 2*y)).rgb;
23+
vec3 a = getColor(m_Texture, vec2(texCoord.x - 2.0*x, texCoord.y + 2.0*y)).rgb;
24+
vec3 b = getColor(m_Texture, vec2(texCoord.x, texCoord.y + 2.0*y)).rgb;
25+
vec3 c = getColor(m_Texture, vec2(texCoord.x + 2.0*x, texCoord.y + 2.0*y)).rgb;
2626

27-
vec3 d = getColor(m_Texture, vec2(texCoord.x - 2*x, texCoord.y)).rgb;
27+
vec3 d = getColor(m_Texture, vec2(texCoord.x - 2.0*x, texCoord.y)).rgb;
2828
vec3 e = getColor(m_Texture, vec2(texCoord.x, texCoord.y)).rgb;
29-
vec3 f = getColor(m_Texture, vec2(texCoord.x + 2*x, texCoord.y)).rgb;
29+
vec3 f = getColor(m_Texture, vec2(texCoord.x + 2.0*x, texCoord.y)).rgb;
3030

31-
vec3 g = getColor(m_Texture, vec2(texCoord.x - 2*x, texCoord.y - 2*y)).rgb;
32-
vec3 h = getColor(m_Texture, vec2(texCoord.x, texCoord.y - 2*y)).rgb;
33-
vec3 i = getColor(m_Texture, vec2(texCoord.x + 2*x, texCoord.y - 2*y)).rgb;
31+
vec3 g = getColor(m_Texture, vec2(texCoord.x - 2.0*x, texCoord.y - 2.0*y)).rgb;
32+
vec3 h = getColor(m_Texture, vec2(texCoord.x, texCoord.y - 2.0*y)).rgb;
33+
vec3 i = getColor(m_Texture, vec2(texCoord.x + 2.0*x, texCoord.y - 2.0*y)).rgb;
3434

3535
vec3 j = getColor(m_Texture, vec2(texCoord.x - x, texCoord.y + y)).rgb;
3636
vec3 k = getColor(m_Texture, vec2(texCoord.x + x, texCoord.y + y)).rgb;

jme3-effects/src/main/resources/Common/MatDefs/Post/Posterization.frag

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ void main() {
1313
vec4 texVal = vec4(color);
1414

1515
texVal = pow(texVal, vec4(m_Gamma));
16-
texVal = texVal * vec4(m_NumColors);
17-
texVal = floor(texVal);
18-
texVal = texVal / vec4(m_NumColors);
16+
texVal = texVal * vec4(float(m_NumColors));
17+
texVal = floor(texVal);
18+
texVal = texVal / vec4(float(m_NumColors));
1919
texVal = pow(texVal, vec4(1.0/m_Gamma));
2020

2121
gl_FragColor = mix(color, texVal, m_Strength);
22-
}
22+
}

jme3-terrain/src/main/resources/Common/MatDefs/Terrain/Modular/AfflictionLib.glsllib

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
//0 means dont scale to be desaturated (bricks, stones, etc)
5656
if(mode > 0){ //1 means almost fully desaturated.. 1 is less alive, and 2 is slightly less, etc etc
57-
deathVar -= mode * 0.033;
57+
deathVar -= float(mode) * 0.033;
5858

5959
deathVar = max(0.0, deathVar);
6060
deathVar = min(0.99, deathVar);
@@ -361,15 +361,15 @@
361361
#ifdef AFFLICTIONTEXTURE
362362
vec4 afflictionAlbedo;
363363

364-
float newAfflictionScale = m_AfflictionSplatScale;
364+
float newAfflictionScale = float(m_AfflictionSplatScale);
365365
vec2 newScaledCoords;
366366

367367
#ifdef AFFLICTIONALBEDOMAP
368368
#ifdef TRI_PLANAR_MAPPING
369369
newAfflictionScale = newAfflictionScale / 256;
370370
afflictionAlbedo = getTriPlanarBlend(lPosition, m_SplatAlbedoMap , newAfflictionScale);
371371
#else
372-
newScaledCoords = mod(wPosition.xz / m_AfflictionSplatScale, 0.985);
372+
newScaledCoords = mod(wPosition.xz / float(m_AfflictionSplatScale), 0.985);
373373
afflictionAlbedo = texture2D(m_SplatAlbedoMap , newScaledCoords);
374374
#endif
375375

@@ -459,4 +459,3 @@
459459

460460

461461

462-

jme3-terrain/src/main/resources/Common/MatDefs/Terrain/Modular/PBRTerrainUtils.glsllib

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
vec4 alphaBlend_0, alphaBlend_1, alphaBlend_2;
6262

6363
void PBRTerrainUtils_readAlphaMaps(){
64+
alphaBlend_0 = vec4(1.0, 0.0, 0.0, 0.0);
65+
alphaBlend_1 = vec4(0.0);
66+
alphaBlend_2 = vec4(0.0);
6467

6568
#ifdef ALPHAMAP
6669
alphaBlend_0 = texture2D( m_AlphaMap, texCoord.xy );
@@ -75,12 +78,12 @@
7578

7679
float PBRTerrainUtils_getAlphaBlendFromChannel(int layer){
7780
float finalAlphaBlendForLayer = 0.0;
78-
vec4 alphaBlend;
79-
if(layer <= 3.0){
81+
vec4 alphaBlend = vec4(0.0);
82+
if(layer <= 3){
8083
alphaBlend = alphaBlend_0;
81-
}else if(layer <= 7.0){
84+
}else if(layer <= 7){
8285
alphaBlend = alphaBlend_1;
83-
}else if(layer <= 11.0){
86+
}else if(layer <= 11){
8487
alphaBlend = alphaBlend_2;
8588
}
8689
int texChannelForAlphaBlending = int(mod(float(layer), 4.0)); //pick the correct channel (r g b or a) based on the layer's index

0 commit comments

Comments
 (0)