godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.57k stars 20.43k forks source link

GLES2 fragment shader compilation failed errors on project start #23588

Closed Tobi-La closed 5 years ago

Tobi-La commented 5 years ago

Hi there!

Godot version: 3.1 alpha2

OS/device including version: Windows 10 Professional

Issue description: I tried the alpha2 of godot 3.1 with my project, but I get plenty of errors when I try to start the game: 1 - E 0:00:10:0767 SceneShaderGLES2: Fragment shader compilation failed: 0(876) : error C7532: global function inverse requires "#version 140" or later 0(876) : error C0000: ... or #extension GL_ARB_gpu_shader5 : enable

drivers/gles2/shader_gles2.cpp:196 @ _display_error_with_code() 2 - E 0:00:10:0809 Method/Function Failed, returning: __null drivers/gles2/shader_gles2.cpp:420 @ get_current_version() 3 - E 0:00:10:0863 Condition ' !version ' is true. returned: false drivers/gles2/shader_gles2.cpp:133 @ bind() After that, mostly the same errors occur again. **Steps to reproduce:** I don't know which part of my project is causing this and I, unfortunately, can't upload my project. But of course, I can try to provide more information if the issue can't be investigated with the information provided.
akien-mga commented 5 years ago

What is your GPU and driver version?

Tobi-La commented 5 years ago

GPU: NVidia GTX 1050 Driver: 23.21.13.8808 Wow, that answer was quick, thanks for the fast reply. Let me know if you need anything else.

akien-mga commented 5 years ago

0(876) : error C7532: global function inverse requires "#version 140" or later references line number 876, there should be a dump of the complete shader code above it. Could you paste that here?

Tobi-La commented 5 years ago

Somehow godot crashes now before the I can start my game, but it seems the same error is printed when just starting the editor. So I hope this is helpfull, too:

ERROR: _display_error_with_code: SceneShaderGLES2: Fragment shader compilation failed: 0(876) : error C7532: global function inverse requires "#version 140" or later 0(876) : error C0000: ... or #extension GL_ARB_gpu_shader5 : enable

At: drivers/gles2/shader_gles2.cpp:196 ERROR: get_current_version: Method/Function Failed, returning: __null At: drivers/gles2/shader_gles2.cpp:420 ERROR: bind: Condition ' !version ' is true. returned: false At: drivers/gles2/shader_gles2.cpp:133

1: #version 120
2: #define USE_GLES_OVER_GL
3: #define DIFFUSE_BURLEY
4: #define SPECULAR_SCHLICK_GGX
5: #define ENABLE_UV_INTERP
6: #extension GL_ARB_shader_texture_lod : enable
7:
8: #ifndef GL_ARB_shader_texture_lod
9: #define texture2DLod(img, coord, lod) texture2D(img, coord)
10: #define textureCubeLod(img, coord, lod) textureCube(img, coord)
11: #endif
12:
13: #ifdef USE_GLES_OVER_GL
14: #define mediump
15: #define highp
16: #else
17: precision mediump float;
18: precision highp int;
19: #endif
20:
21:
22: vec2 select2(vec2 a, vec2 b, bvec2 c) {
23:     vec2 ret;
24:
25:     ret.x = c.x ? b.x : a.x;
26:     ret.y = c.y ? b.y : a.y;
27:
28:     return ret;
29: }
30:
31: vec3 select3(vec3 a, vec3 b, bvec3 c) {
32:     vec3 ret;
33:
34:     ret.x = c.x ? b.x : a.x;
35:     ret.y = c.y ? b.y : a.y;
36:     ret.z = c.z ? b.z : a.z;
37:
38:     return ret;
39: }
40:
41: vec4 select4(vec4 a, vec4 b, bvec4 c) {
42:     vec4 ret;
43:
44:     ret.x = c.x ? b.x : a.x;
45:     ret.y = c.y ? b.y : a.y;
46:     ret.z = c.z ? b.z : a.z;
47:     ret.w = c.w ? b.w : a.w;
48:
49:     return ret;
50: }
51:
52: highp vec4 texel2DFetch(highp sampler2D tex, ivec2 size, ivec2 coord) {
53:     float x_coord = float(2 * coord.x + 1) / float(size.x * 2);
54:     float y_coord = float(2 * coord.y + 1) / float(size.y * 2);
55:
56:     return texture2DLod(tex, vec2(x_coord, y_coord), 0.0);
57: }
58:
59: #ifndef USE_GLES_OVER_GL
60: highp mat4 transpose(highp mat4 src) {
61:     return mat4(
62:                     vec4(src[0].x, src[1].x, src[2].x, src[3].x),
63:                     vec4(src[0].y, src[1].y, src[2].y, src[3].y),
64:                     vec4(src[0].z, src[1].z, src[2].z, src[3].z),
65:                     vec4(src[0].w, src[1].w, src[2].w, src[3].w));
66: }
67: #endif
68:
69: #define M_PI 3.14159265359
70: #define SHADER_IS_SRGB true
71:
72: //
73: // uniforms
74: //
75:
76: uniform highp mat4 camera_matrix;
77: /* clang-format on */
78: uniform highp mat4 camera_inverse_matrix;
79: uniform highp mat4 projection_matrix;
80: uniform highp mat4 projection_inverse_matrix;
81:
82: uniform highp mat4 world_transform;
83:
84: uniform highp float time;
85:
86: #if defined(SCREEN_UV_USED)
87: uniform vec2 screen_pixel_size;
88: #endif
89:
90: // I think supporting this in GLES2 is difficult
91: // uniform highp sampler2D depth_buffer;
92:
93: #if defined(SCREEN_TEXTURE_USED)
94: uniform highp sampler2D screen_texture; //texunit:-4
95: #endif
96:
97: #ifdef USE_REFLECTION_PROBE1
98:
99: #ifdef USE_VERTEX_LIGHTING
100:
101: varying mediump vec4 refprobe1_reflection_normal_blend;
102: #ifndef USE_LIGHTMAP
103: varying mediump vec3 refprobe1_ambient_normal;
104: #endif
105:
106: #else
107:
108: uniform bool refprobe1_use_box_project;
109: uniform highp vec3 refprobe1_box_extents;
110: uniform vec3 refprobe1_box_offset;
111: uniform highp mat4 refprobe1_local_matrix;
112:
113: #endif //use vertex lighting
114:
115: uniform bool refprobe1_exterior;
116:
117: uniform highp samplerCube reflection_probe1; //texunit:-5
118:
119: uniform float refprobe1_intensity;
120: uniform vec4 refprobe1_ambient;
121:
122: #endif //USE_REFLECTION_PROBE1
123:
124: #ifdef USE_REFLECTION_PROBE2
125:
126: #ifdef USE_VERTEX_LIGHTING
127:
128: varying mediump vec4 refprobe2_reflection_normal_blend;
129: #ifndef USE_LIGHTMAP
130: varying mediump vec3 refprobe2_ambient_normal;
131: #endif
132:
133: #else
134:
135: uniform bool refprobe2_use_box_project;
136: uniform highp vec3 refprobe2_box_extents;
137: uniform vec3 refprobe2_box_offset;
138: uniform highp mat4 refprobe2_local_matrix;
139:
140: #endif //use vertex lighting
141:
142: uniform bool refprobe2_exterior;
143:
144: uniform highp samplerCube reflection_probe2; //texunit:-6
145:
146: uniform float refprobe2_intensity;
147: uniform vec4 refprobe2_ambient;
148:
149: #endif //USE_REFLECTION_PROBE2
150:
151: #define RADIANCE_MAX_LOD 6.0
152:
153: #if defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2)
154:
155: void reflection_process(samplerCube reflection_map,
156: #ifdef USE_VERTEX_LIGHTING
157:            vec3 ref_normal,
158: #ifndef USE_LIGHTMAP
159:            vec3 amb_normal,
160: #endif
161:            float ref_blend,
162:
163: #else //no vertex lighting
164:            vec3 normal, vec3 vertex,
165:            mat4 local_matrix,
166:            bool use_box_project, vec3 box_extents, vec3 box_offset,
167: #endif //vertex lighting
168:            bool exterior, float intensity, vec4 ref_ambient, float roughness, vec3 ambient, vec3 skybox, inout highp vec4 reflection_accum, inout highp vec4 ambient_accum) {
169:
170:    vec4 reflection;
171:
172: #ifdef USE_VERTEX_LIGHTING
173:
174:    reflection.rgb = textureCubeLod(reflection_map, ref_normal, roughness * RADIANCE_MAX_LOD).rgb;
175:
176:    float blend = ref_blend; //crappier blend formula for vertex
177:    blend *= blend;
178:    blend = max(0.0, 1.0 - blend);
179:
180: #else //fragment lighting
181:
182:    vec3 local_pos = (local_matrix * vec4(vertex, 1.0)).xyz;
183:
184:    if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
185:            return;
186:    }
187:
188:    vec3 inner_pos = abs(local_pos / box_extents);
189:    float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z));
190:    blend = mix(length(inner_pos), blend, blend);
191:    blend *= blend;
192:    blend = max(0.0, 1.0 - blend);
193:
194:    //reflect and make local
195:    vec3 ref_normal = normalize(reflect(vertex, normal));
196:    ref_normal = (local_matrix * vec4(ref_normal, 0.0)).xyz;
197:
198:    if (use_box_project) { //box project
199:
200:            vec3 nrdir = normalize(ref_normal);
201:            vec3 rbmax = (box_extents - local_pos) / nrdir;
202:            vec3 rbmin = (-box_extents - local_pos) / nrdir;
203:
204:            vec3 rbminmax = mix(rbmin, rbmax, vec3(greaterThan(nrdir, vec3(0.0, 0.0, 0.0))));
205:
206:            float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
207:            vec3 posonbox = local_pos + nrdir * fa;
208:            ref_normal = posonbox - box_offset.xyz;
209:    }
210:
211:    reflection.rgb = textureCubeLod(reflection_map, ref_normal, roughness * RADIANCE_MAX_LOD).rgb;
212: #endif
213:
214:    if (exterior) {
215:            reflection.rgb = mix(skybox, reflection.rgb, blend);
216:    }
217:    reflection.rgb *= intensity;
218:    reflection.a = blend;
219:    reflection.rgb *= blend;
220:
221:    reflection_accum += reflection;
222:
223: #ifndef USE_LIGHTMAP
224:
225:    vec4 ambient_out;
226: #ifndef USE_VERTEX_LIGHTING
227:
228:    vec3 amb_normal = (local_matrix * vec4(normal, 0.0)).xyz;
229: #endif
230:
231:    ambient_out.rgb = textureCubeLod(reflection_map, amb_normal, RADIANCE_MAX_LOD).rgb;
232:    ambient_out.rgb = mix(ref_ambient.rgb, ambient_out.rgb, ref_ambient.a);
233:    if (exterior) {
234:            ambient_out.rgb = mix(ambient, ambient_out.rgb, blend);
235:    }
236:
237:    ambient_out.a = blend;
238:    ambient_out.rgb *= blend;
239:    ambient_accum += ambient_out;
240:
241: #endif
242: }
243:
244: #endif //use refprobe 1 or 2
245:
246: #ifdef USE_LIGHTMAP
247: uniform mediump sampler2D lightmap; //texunit:-4
248: uniform mediump float lightmap_energy;
249: #endif
250:
251: #ifdef USE_LIGHTMAP_CAPTURE
252: uniform mediump vec4[12] lightmap_captures;
253: uniform bool lightmap_capture_sky;
254:
255: #endif
256:
257: #ifdef USE_RADIANCE_MAP
258:
259: uniform samplerCube radiance_map; // texunit:-2
260:
261: uniform mat4 radiance_inverse_xform;
262:
263: #endif
264:
265: uniform float bg_energy;
266:
267: uniform float ambient_sky_contribution;
268: uniform vec4 ambient_color;
269: uniform float ambient_energy;
270:
271: #ifdef USE_LIGHTING
272:
273: #ifdef USE_VERTEX_LIGHTING
274:
275: //get from vertex
276: varying highp vec3 diffuse_interp;
277: varying highp vec3 specular_interp;
278:
279: uniform highp vec3 light_direction; //may be used by fog, so leave here
280:
281: #else
282: //done in fragment
283: // general for all lights
284: uniform highp vec4 light_color;
285: uniform highp float light_specular;
286:
287: // directional
288: uniform highp vec3 light_direction;
289: // omni
290: uniform highp vec3 light_position;
291:
292: uniform highp float light_attenuation;
293:
294: // spot
295: uniform highp float light_spot_attenuation;
296: uniform highp float light_spot_range;
297: uniform highp float light_spot_angle;
298: #endif
299:
300: //this is needed outside above if because dual paraboloid wants it
301: uniform highp float light_range;
302:
303: #ifdef USE_SHADOW
304:
305: uniform highp vec2 shadow_pixel_size;
306:
307: #if defined(LIGHT_MODE_OMNI) || defined(LIGHT_MODE_SPOT)
308: uniform highp sampler2D light_shadow_atlas; //texunit:-3
309: #endif
310:
311: #ifdef LIGHT_MODE_DIRECTIONAL
312: uniform highp sampler2D light_directional_shadow; // texunit:-3
313: uniform highp vec4 light_split_offsets;
314: #endif
315:
316: varying highp vec4 shadow_coord;
317:
318: #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
319: varying highp vec4 shadow_coord2;
320: #endif
321:
322: #if defined(LIGHT_USE_PSSM4)
323:
324: varying highp vec4 shadow_coord3;
325: varying highp vec4 shadow_coord4;
326:
327: #endif
328:
329: uniform vec4 light_clamp;
330:
331: #endif // light shadow
332:
333: // directional shadow
334:
335: #endif
336:
337: //
338: // varyings
339: //
340:
341: varying highp vec3 vertex_interp;
342: varying vec3 normal_interp;
343:
344: #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
345: varying vec3 tangent_interp;
346: varying vec3 binormal_interp;
347: #endif
348:
349: #if defined(ENABLE_COLOR_INTERP)
350: varying vec4 color_interp;
351: #endif
352:
353: #if defined(ENABLE_UV_INTERP)
354: varying vec2 uv_interp;
355: #endif
356:
357: #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP)
358: varying vec2 uv2_interp;
359: #endif
360:
361: varying vec3 view_interp;
362:
363: vec3 F0(float metallic, float specular, vec3 albedo) {
364:    float dielectric = 0.16 * specular * specular;
365:    // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
366:    // see https://google.github.io/filament/Filament.md.html
367:    return mix(vec3(dielectric), albedo, vec3(metallic));
368: }
369:
370: /* clang-format off */
371: uniform int m_axis;
372: uniform float m_roughness;
373: uniform float m_specular;
374: uniform float m_point_size;
375: uniform float m_metallic;
376: uniform vec4 m_albedo;
377: uniform vec4 m_roughness_texture_channel;
378: uniform vec4 m_metallic_texture_channel;
379: uniform vec3 m_uv2_offset;
380: uniform vec3 m_uv2_scale;
381: uniform vec3 m_uv1_scale;
382: uniform vec3 m_uv1_offset;
383: uniform sampler2D m_texture_roughness;
384: uniform sampler2D m_texture_albedo;
385: uniform sampler2D m_texture_metallic;
386: uniform float m_percent;
387: uniform bool m_invertAxis;
388: uniform float m_clip_height;
389: uniform float m_fade_offset;
390:
391:
392: /* clang-format on */
393:
394: #ifdef RENDER_DEPTH_DUAL_PARABOLOID
395:
396: varying highp float dp_clip;
397:
398: #endif
399:
400: #ifdef USE_LIGHTING
401:
402: // This returns the G_GGX function divided by 2 cos_theta_m, where in practice cos_theta_m is either N.L or N.V.
403: // We're dividing this factor off because the overall term we'll end up looks like
404: // (see, for example, the first unnumbered equation in B. Burley, "Physically Based Shading at Disney", SIGGRAPH 2012):
405: //
406: //   F(L.V) D(N.H) G(N.L) G(N.V) / (4 N.L N.V)
407: //
408: // We're basically regouping this as
409: //
410: //   F(L.V) D(N.H) [G(N.L)/(2 N.L)] [G(N.V) / (2 N.V)]
411: //
412: // and thus, this function implements the [G(N.m)/(2 N.m)] part with m = L or V.
413: //
414: // The contents of the D and G (G1) functions (GGX) are taken from
415: // E. Heitz, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs", J. Comp. Graph. Tech. 3 (2) (2014).
416: // Eqns 71-72 and 85-86 (see also Eqns 43 and 80).
417:
418: /*
419: float G_GGX_2cos(float cos_theta_m, float alpha) {
420:    // Schlick's approximation
421:    // C. Schlick, "An Inexpensive BRDF Model for Physically-based Rendering", Computer Graphics Forum. 13 (3): 233 (1994)
422:    // Eq. (19), although see Heitz (2014) the about the problems with his derivation.
423:    // It nevertheless approximates GGX well with k = alpha/2.
424:    float k = 0.5 * alpha;
425:    return 0.5 / (cos_theta_m * (1.0 - k) + k);
426:
427:    // float cos2 = cos_theta_m * cos_theta_m;
428:    // float sin2 = (1.0 - cos2);
429:    // return 1.0 / (cos_theta_m + sqrt(cos2 + alpha * alpha * sin2));
430: }
431: */
432:
433: // This approximates G_GGX_2cos(cos_theta_l, alpha) * G_GGX_2cos(cos_theta_v, alpha)
434: // See Filament docs, Specular G section.
435: float V_GGX(float cos_theta_l, float cos_theta_v, float alpha) {
436:    float v = cos_theta_l * (cos_theta_v * (1.0 - alpha) + alpha);
437:    float l = cos_theta_v * (cos_theta_l * (1.0 - alpha) + alpha);
438:    return 0.5 / (v + l);
439: }
440:
441: float D_GGX(float cos_theta_m, float alpha) {
442:    float alpha2 = alpha * alpha;
443:    float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m;
444:    return alpha2 / (M_PI * d * d);
445: }
446:
447: /*
448: float G_GGX_anisotropic_2cos(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
449:    float cos2 = cos_theta_m * cos_theta_m;
450:    float sin2 = (1.0 - cos2);
451:    float s_x = alpha_x * cos_phi;
452:    float s_y = alpha_y * sin_phi;
453:    return 1.0 / max(cos_theta_m + sqrt(cos2 + (s_x * s_x + s_y * s_y) * sin2), 0.001);
454: }
455: */
456:
457: // This approximates G_GGX_anisotropic_2cos(cos_theta_l, ...) * G_GGX_anisotropic_2cos(cos_theta_v, ...)
458: // See Filament docs, Anisotropic specular BRDF section.
459: float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) {
460:    float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV));
461:    float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL));
462:    return 0.5 / (Lambda_V + Lambda_L);
463: }
464:
465: float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi, float NdotH) {
466:    float alpha2 = alpha_x * alpha_y;
467:    highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * NdotH);
468:    highp float v2 = dot(v, v);
469:    float w2 = alpha2 / v2;
470:    float D = alpha2 * w2 * w2 * (1.0 / M_PI);
471:    return D;
472:
473:    /* float cos2 = cos_theta_m * cos_theta_m;
474:    float sin2 = (1.0 - cos2);
475:    float r_x = cos_phi / alpha_x;
476:    float r_y = sin_phi / alpha_y;
477:    float d = cos2 + sin2 * (r_x * r_x + r_y * r_y);
478:    return 1.0 / max(M_PI * alpha_x * alpha_y * d * d, 0.001); */
479: }
480:
481: float SchlickFresnel(float u) {
482:    float m = 1.0 - u;
483:    float m2 = m * m;
484:    return m2 * m2 * m; // pow(m,5)
485: }
486:
487: float GTR1(float NdotH, float a) {
488:    if (a >= 1.0) return 1.0 / M_PI;
489:    float a2 = a * a;
490:    float t = 1.0 + (a2 - 1.0) * NdotH * NdotH;
491:    return (a2 - 1.0) / (M_PI * log(a2) * t);
492: }
493:
494: void light_compute(
495:            vec3 N,
496:            vec3 L,
497:            vec3 V,
498:            vec3 B,
499:            vec3 T,
500:            vec3 light_color,
501:            vec3 attenuation,
502:            vec3 diffuse_color,
503:            vec3 transmission,
504:            float specular_blob_intensity,
505:            float roughness,
506:            float metallic,
507:            float specular,
508:            float rim,
509:            float rim_tint,
510:            float clearcoat,
511:            float clearcoat_gloss,
512:            float anisotropy,
513:            inout vec3 diffuse_light,
514:            inout vec3 specular_light) {
515:
516: //this makes lights behave closer to linear, but then addition of lights looks bad
517: //better left disabled
518:
519: //#define SRGB_APPROX(m_var) m_var = pow(m_var,0.4545454545);
520: /*
521: #define SRGB_APPROX(m_var) {\
522:    float S1 = sqrt(m_var);\
523:    float S2 = sqrt(S1);\
524:    float S3 = sqrt(S2);\
525:    m_var = 0.662002687 * S1 + 0.684122060 * S2 - 0.323583601 * S3 - 0.0225411470 * m_var;\
526:    }
527: */
528: #define SRGB_APPROX(m_var)
529:
530: #if defined(USE_LIGHT_SHADER_CODE)
531:    // light is written by the light shader
532:
533:    vec3 normal = N;
534:    vec3 albedo = diffuse_color;
535:    vec3 light = L;
536:    vec3 view = V;
537:
538:    /* clang-format off */
539:
540: LIGHT_SHADER_CODE
541:
542:    /* clang-format on */
543:
544: #else
545:    float NdotL = dot(N, L);
546:    float cNdotL = max(NdotL, 0.0); // clamped NdotL
547:    float NdotV = dot(N, V);
548:    float cNdotV = max(NdotV, 0.0);
549:
550:    if (metallic < 1.0) {
551: #if defined(DIFFUSE_OREN_NAYAR)
552:            vec3 diffuse_brdf_NL;
553: #else
554:            float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
555: #endif
556:
557: #if defined(DIFFUSE_LAMBERT_WRAP)
558:            // energy conserving lambert wrap shader
559:            diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness)));
560:
561: #elif defined(DIFFUSE_OREN_NAYAR)
562:
563:            {
564:                    // see http://mimosa-pudica.net/improved-oren-nayar.html
565:                    float LdotV = dot(L, V);
566:
567:                    float s = LdotV - NdotL * NdotV;
568:                    float t = mix(1.0, max(NdotL, NdotV), step(0.0, s));
569:
570:                    float sigma2 = roughness * roughness; // TODO: this needs checking
571:                    vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse_color / (sigma2 + 0.13));
572:                    float B = 0.45 * sigma2 / (sigma2 + 0.09);
573:
574:                    diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI);
575:            }
576:
577: #elif defined(DIFFUSE_TOON)
578:
579:            diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL);
580:
581: #elif defined(DIFFUSE_BURLEY)
582:
583:            {
584:
585:                    vec3 H = normalize(V + L);
586:                    float cLdotH = max(0.0, dot(L, H));
587:
588:                    float FD90 = 0.5 + 2.0 * cLdotH * cLdotH * roughness;
589:                    float FdV = 1.0 + (FD90 - 1.0) * SchlickFresnel(cNdotV);
590:                    float FdL = 1.0 + (FD90 - 1.0) * SchlickFresnel(cNdotL);
591:                    diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
592:                    /*
593:                    float energyBias = mix(roughness, 0.0, 0.5);
594:                    float energyFactor = mix(roughness, 1.0, 1.0 / 1.51);
595:                    float fd90 = energyBias + 2.0 * VoH * VoH * roughness;
596:                    float f0 = 1.0;
597:                    float lightScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotL, 5.0);
598:                    float viewScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotV, 5.0);
599:
600:                    diffuse_brdf_NL = lightScatter * viewScatter * energyFactor;
601:                    */
602:            }
603: #else
604:            // lambert
605:            diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
606: #endif
607:
608:            SRGB_APPROX(diffuse_brdf_NL)
609:
610:            diffuse_light += light_color * diffuse_color * diffuse_brdf_NL * attenuation;
611:
612: #if defined(TRANSMISSION_USED)
613:            diffuse_light += light_color * diffuse_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * transmission * attenuation;
614: #endif
615:
616: #if defined(LIGHT_USE_RIM)
617:            float rim_light = pow(max(0.0, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
618:            diffuse_light += rim_light * rim * mix(vec3(1.0), diffuse_color, rim_tint) * light_color;
619: #endif
620:    }
621:
622:    if (roughness > 0.0) {
623:
624: #if defined(SPECULAR_SCHLICK_GGX)
625:            vec3 specular_brdf_NL = vec3(0.0);
626: #else
627:            float specular_brdf_NL = 0.0;
628: #endif
629:
630: #if defined(SPECULAR_BLINN)
631:
632:            //normalized blinn
633:            vec3 H = normalize(V + L);
634:            float cNdotH = max(dot(N, H), 0.0);
635:            float cVdotH = max(dot(V, H), 0.0);
636:            float cLdotH = max(dot(L, H), 0.0);
637:            float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
638:            float blinn = pow(cNdotH, shininess);
639:            blinn *= (shininess + 8.0) / (8.0 * 3.141592654);
640:            specular_brdf_NL = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
641:
642: #elif defined(SPECULAR_PHONG)
643:
644:            vec3 R = normalize(-reflect(L, N));
645:            float cRdotV = max(0.0, dot(R, V));
646:            float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
647:            float phong = pow(cRdotV, shininess);
648:            phong *= (shininess + 8.0) / (8.0 * 3.141592654);
649:            specular_brdf_NL = (phong) / max(4.0 * cNdotV * cNdotL, 0.75);
650:
651: #elif defined(SPECULAR_TOON)
652:
653:            vec3 R = normalize(-reflect(L, N));
654:            float RdotV = dot(R, V);
655:            float mid = 1.0 - roughness;
656:            mid *= mid;
657:            specular_brdf_NL = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
658:
659: #elif defined(SPECULAR_DISABLED)
660:            // none..
661: #elif defined(SPECULAR_SCHLICK_GGX)
662:            // shlick+ggx as default
663:
664:            vec3 H = normalize(V + L);
665:
666:            float cNdotH = max(dot(N, H), 0.0);
667:            float cLdotH = max(dot(L, H), 0.0);
668:
669: #if defined(LIGHT_USE_ANISOTROPY)
670:            float alpha = roughness * roughness;
671:            float aspect = sqrt(1.0 - anisotropy * 0.9);
672:            float ax = alpha / aspect;
673:            float ay = alpha * aspect;
674:            float XdotH = dot(T, H);
675:            float YdotH = dot(B, H);
676:            float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH, cNdotH);
677:            //float G = G_GGX_anisotropic_2cos(cNdotL, ax, ay, XdotH, YdotH) * G_GGX_anisotropic_2cos(cNdotV, ax, ay, XdotH, YdotH);
678:            float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL))
679:
680: #else
681:            float alpha = roughness * roughness;
682:            float D = D_GGX(cNdotH, alpha);
683:            //float G = G_GGX_2cos(cNdotL, alpha) * G_GGX_2cos(cNdotV, alpha);
684:            float G = V_GGX(cNdotL, cNdotV, alpha);
685: #endif
686:            // F
687:            vec3 f0 = F0(metallic, specular, diffuse_color);
688:            float cLdotH5 = SchlickFresnel(cLdotH);
689:            vec3 F = mix(vec3(cLdotH5), vec3(1.0), f0);
690:
691:            specular_brdf_NL = cNdotL * D * F * G;
692:
693: #endif
694:
695:            SRGB_APPROX(specular_brdf_NL)
696:            specular_light += specular_brdf_NL * light_color * specular_blob_intensity * attenuation;
697:
698: #if defined(LIGHT_USE_CLEARCOAT)
699:            if (clearcoat_gloss > 0.0) {
700: #if !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_BLINN)
701:                    vec3 H = normalize(V + L);
702: #endif
703: #if !defined(SPECULAR_SCHLICK_GGX)
704:                    float cNdotH = max(dot(N, H), 0.0);
705:                    float cLdotH = max(dot(L, H), 0.0);
706:                    float cLdotH5 = SchlickFresnel(cLdotH);
707: #endif
708:                    float Dr = GTR1(cNdotH, mix(.1, .001, clearcoat_gloss));
709:                    float Fr = mix(.04, 1.0, cLdotH5);
710:                    //float Gr = G_GGX_2cos(cNdotL, .25) * G_GGX_2cos(cNdotV, .25);
711:                    float Gr = V_GGX(cNdotL, cNdotV, 0.25);
712:
713:                    float clearcoat_specular_brdf_NL = 0.25 * clearcoat * Gr * Fr * Dr * cNdotL;
714:
715:                    specular_light += clearcoat_specular_brdf_NL * light_color * specular_blob_intensity * attenuation;
716:            }
717: #endif
718:    }
719:
720: #endif //defined(USE_LIGHT_SHADER_CODE)
721: }
722:
723: #endif
724: // shadows
725:
726: #ifdef USE_SHADOW
727:
728: #define SAMPLE_SHADOW_TEXEL(p_shadow, p_pos, p_depth) step(p_depth, texture2D(p_shadow, p_pos).r)
729: #define SAMPLE_SHADOW_TEXEL_PROJ(p_shadow, p_pos) step(p_pos.z, texture2DProj(p_shadow, p_pos).r)
730:
731: float sample_shadow(highp sampler2D shadow, highp vec4 spos) {
732:
733: #ifdef SHADOW_MODE_PCF_13
734:
735:    spos.xyz /= spos.w;
736:    vec2 pos = spos.xy;
737:    float depth = spos.z;
738:
739:    float avg = SAMPLE_SHADOW_TEXEL(shadow, pos, depth);
740:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, 0.0), depth);
741:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, 0.0), depth);
742:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y), depth);
743:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y), depth);
744:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, shadow_pixel_size.y), depth);
745:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, shadow_pixel_size.y), depth);
746:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, -shadow_pixel_size.y), depth);
747:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, -shadow_pixel_size.y), depth);
748:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x * 2.0, 0.0), depth);
749:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x * 2.0, 0.0), depth);
750:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y * 2.0), depth);
751:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y * 2.0), depth);
752:    return avg * (1.0 / 13.0);
753: #endif
754:
755: #ifdef SHADOW_MODE_PCF_5
756:
757:    spos.xyz /= spos.w;
758:    vec2 pos = spos.xy;
759:    float depth = spos.z;
760:
761:    float avg = SAMPLE_SHADOW_TEXEL(shadow, pos, depth);
762:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, 0.0), depth);
763:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, 0.0), depth);
764:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y), depth);
765:    avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y), depth);
766:    return avg * (1.0 / 5.0);
767:
768: #endif
769:
770: #if !defined(SHADOW_MODE_PCF_5) || !defined(SHADOW_MODE_PCF_13)
771:
772:    return SAMPLE_SHADOW_TEXEL_PROJ(shadow, spos);
773: #endif
774: }
775:
776: #endif
777:
778: #if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED)
779:
780: #if defined(USE_VERTEX_LIGHTING)
781:
782: varying vec4 fog_interp;
783:
784: #else
785: uniform mediump vec4 fog_color_base;
786: #ifdef LIGHT_MODE_DIRECTIONAL
787: uniform mediump vec4 fog_sun_color_amount;
788: #endif
789:
790: uniform bool fog_transmit_enabled;
791: uniform mediump float fog_transmit_curve;
792:
793: #ifdef FOG_DEPTH_ENABLED
794: uniform highp float fog_depth_begin;
795: uniform mediump float fog_depth_curve;
796: uniform mediump float fog_max_distance;
797: #endif
798:
799: #ifdef FOG_HEIGHT_ENABLED
800: uniform highp float fog_height_min;
801: uniform highp float fog_height_max;
802: uniform mediump float fog_height_curve;
803: #endif
804:
805: #endif //vertex lit
806: #endif //fog
807:
808: void main() {
809:
810: #ifdef RENDER_DEPTH_DUAL_PARABOLOID
811:
812:    if (dp_clip > 0.0)
813:            discard;
814: #endif
815:    highp vec3 vertex = vertex_interp;
816:    vec3 albedo = vec3(1.0);
817:    vec3 transmission = vec3(0.0);
818:    float metallic = 0.0;
819:    float specular = 0.5;
820:    vec3 emission = vec3(0.0);
821:    float roughness = 1.0;
822:    float rim = 0.0;
823:    float rim_tint = 0.0;
824:    float clearcoat = 0.0;
825:    float clearcoat_gloss = 0.0;
826:    float anisotropy = 0.0;
827:    vec2 anisotropy_flow = vec2(1.0, 0.0);
828:    float sss_strength = 0.0; //unused
829:
830:    float alpha = 1.0;
831:    float side = 1.0;
832:
833:    float specular_blob_intensity = 1.0;
834: #if defined(SPECULAR_TOON)
835:    specular_blob_intensity *= specular * 2.0;
836: #endif
837:
838: #if defined(ENABLE_AO)
839:    float ao = 1.0;
840:    float ao_light_affect = 0.0;
841: #endif
842:
843: #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
844:    vec3 binormal = normalize(binormal_interp) * side;
845:    vec3 tangent = normalize(tangent_interp) * side;
846: #else
847:    vec3 binormal = vec3(0.0);
848:    vec3 tangent = vec3(0.0);
849: #endif
850:    vec3 normal = normalize(normal_interp) * side;
851:
852: #if defined(ENABLE_NORMALMAP)
853:    vec3 normalmap = vec3(0.5);
854: #endif
855:    float normaldepth = 1.0;
856:
857: #if defined(ALPHA_SCISSOR_USED)
858:    float alpha_scissor = 0.5;
859: #endif
860:
861: #if defined(SCREEN_UV_USED)
862:    vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
863: #endif
864:
865:    {
866:            /* clang-format off */
867: {
868:    vec2 m_base_uv = uv_interp;
869:    vec4 m_albedo_tex = texture2D(m_texture_albedo, m_base_uv);
870:    albedo = (m_albedo.rgb * m_albedo_tex.rgb);
871:    float m_metallic_tex = dot(texture2D(m_texture_metallic, m_base_uv), m_metallic_texture_channel);
872:    metallic = (m_metallic_tex * m_metallic);
873:    float m_roughness_tex = dot(texture2D(m_texture_roughness, m_base_uv), m_roughness_texture_channel);
874:    roughness = (m_roughness_tex * m_roughness);
875:    specular = m_specular;
876:    vec4 m_world_vertex = (inverse(camera_matrix) * vec4(vertex.xyz, 1.0));
877:    float m_from;
878:    float m_to;
879:    if ((m_invertAxis == true))
880:    {
881:                    {
882:            m_from = 0.0;
883:            m_to = 1.0;
884:    }
885: ;
886:    }
887:    else
888:    {
889:                    {
890:            m_from = 1.0;
891:            m_to = 0.0;
892:    }
893: ;
894:    }
895:    if ((m_axis == 1))
896:    {
897:                    {
898:            alpha = smoothstep(m_from, m_to, ((m_world_vertex.x - m_clip_height) / m_fade_offset));
899:    }
900: ;
901:    }
902:    else
903:    {
904:            if ((m_axis == 2))
905:            {
906:                                    {
907:                    alpha = smoothstep(m_from, m_to, ((m_world_vertex.y - m_clip_height) / m_fade_offset));
908:            }
909: ;
910:            }
911:            else
912:            {
913:                    if ((m_axis == 3))
914:                    {
915:                                                    {
916:                            alpha = smoothstep(m_from, m_to, ((m_world_vertex.z - m_clip_height) / m_fade_offset));
917:                    }
918: ;
919:                    }
920:            }
921:    }
922: }
923:
924:
925:            /* clang-format on */
926:    }
927:
928: #if defined(ENABLE_NORMALMAP)
929:    normalmap.xy = normalmap.xy * 2.0 - 1.0;
930:    normalmap.z = sqrt(max(0.0, 1.0 - dot(normalmap.xy, normalmap.xy)));
931:
932:    normal = normalize(mix(normal_interp, tangent * normalmap.x + binormal * normalmap.y + normal * normalmap.z, normaldepth)) * side;
933:    //normal = normalmap;
934: #endif
935:
936:    normal = normalize(normal);
937:
938:    vec3 N = normal;
939:
940:    vec3 specular_light = vec3(0.0, 0.0, 0.0);
941:    vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
942:    vec3 ambient_light = vec3(0.0, 0.0, 0.0);
943:
944:    vec3 eye_position = -normalize(vertex_interp);
945:
946: #if defined(ALPHA_SCISSOR_USED)
947:    if (alpha < alpha_scissor) {
948:            discard;
949:    }
950: #endif
951:
952: #ifdef BASE_PASS
953:    //none
954: #ifdef USE_RADIANCE_MAP
955:
956:    vec3 ref_vec = reflect(-eye_position, N);
957:    ref_vec = normalize((radiance_inverse_xform * vec4(ref_vec, 0.0)).xyz);
958:
959:    ref_vec.z *= -1.0;
960:
961:    specular_light = textureCubeLod(radiance_map, ref_vec, roughness * RADIANCE_MAX_LOD).xyz * bg_energy;
962:
963:    {
964:            vec3 ambient_dir = normalize((radiance_inverse_xform * vec4(normal, 0.0)).xyz);
965:            vec3 env_ambient = textureCubeLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).xyz * bg_energy;
966:
967:            ambient_light = mix(ambient_color.rgb, env_ambient, ambient_sky_contribution);
968:    }
969:
970: #else
971:
972:    ambient_light = ambient_color.rgb;
973:
974: #endif
975:
976:    ambient_light *= ambient_energy;
977:
978: #if defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2)
979:
980:    vec4 ambient_accum = vec4(0.0);
981:    vec4 reflection_accum = vec4(0.0);
982:
983: #ifdef USE_REFLECTION_PROBE1
984:
985:    reflection_process(reflection_probe1,
986: #ifdef USE_VERTEX_LIGHTING
987:                    refprobe1_reflection_normal_blend.rgb,
988: #ifndef USE_LIGHTMAP
989:                    refprobe1_ambient_normal,
990: #endif
991:                    refprobe1_reflection_normal_blend.a,
992: #else
993:                    normal_interp, vertex_interp, refprobe1_local_matrix,
994:                    refprobe1_use_box_project, refprobe1_box_extents, refprobe1_box_offset,
995: #endif
996:                    refprobe1_exterior, refprobe1_intensity, refprobe1_ambient, roughness,
997:                    ambient_light, specular_light, reflection_accum, ambient_accum);
998:
999: #endif // USE_REFLECTION_PROBE1
1000:
1001: #ifdef USE_REFLECTION_PROBE2
1002:
1003:   reflection_process(reflection_probe2,
1004: #ifdef USE_VERTEX_LIGHTING
1005:                   refprobe2_reflection_normal_blend.rgb,
1006: #ifndef USE_LIGHTMAP
1007:                   refprobe2_ambient_normal,
1008: #endif
1009:                   refprobe2_reflection_normal_blend.a,
1010: #else
1011:                   normal_interp, vertex_interp, refprobe2_local_matrix,
1012:                   refprobe2_use_box_project, refprobe2_box_extents, refprobe2_box_offset,
1013: #endif
1014:                   refprobe2_exterior, refprobe2_intensity, refprobe2_ambient, roughness,
1015:                   ambient_light, specular_light, reflection_accum, ambient_accum);
1016:
1017: #endif // USE_REFLECTION_PROBE2
1018:
1019:   if (reflection_accum.a > 0.0) {
1020:           specular_light = reflection_accum.rgb / reflection_accum.a;
1021:   }
1022:
1023: #ifndef USE_LIGHTMAP
1024:   if (ambient_accum.a > 0.0) {
1025:           ambient_light = ambient_accum.rgb / ambient_accum.a;
1026:   }
1027: #endif
1028:
1029: #endif // defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2)
1030:
1031: #ifdef USE_LIGHTMAP
1032:   //ambient light will come entirely from lightmap is lightmap is used
1033:   ambient_light = texture2D(lightmap, uv2_interp).rgb * lightmap_energy;
1034: #endif
1035:
1036: #ifdef USE_LIGHTMAP_CAPTURE
1037:   {
1038:           vec3 cone_dirs[12] = vec3[](
1039:                           vec3(0, 0, 1),
1040:                           vec3(0.866025, 0, 0.5),
1041:                           vec3(0.267617, 0.823639, 0.5),
1042:                           vec3(-0.700629, 0.509037, 0.5),
1043:                           vec3(-0.700629, -0.509037, 0.5),
1044:                           vec3(0.267617, -0.823639, 0.5),
1045:                           vec3(0, 0, -1),
1046:                           vec3(0.866025, 0, -0.5),
1047:                           vec3(0.267617, 0.823639, -0.5),
1048:                           vec3(-0.700629, 0.509037, -0.5),
1049:                           vec3(-0.700629, -0.509037, -0.5),
1050:                           vec3(0.267617, -0.823639, -0.5));
1051:
1052:           vec3 local_normal = normalize(camera_matrix * vec4(normal, 0.0)).xyz;
1053:           vec4 captured = vec4(0.0);
1054:           float sum = 0.0;
1055:           for (int i = 0; i < 12; i++) {
1056:                   float amount = max(0.0, dot(local_normal, cone_dirs[i])); //not correct, but creates a nice wrap around effect
1057:                   captured += lightmap_captures[i] * amount;
1058:                   sum += amount;
1059:           }
1060:
1061:           captured /= sum;
1062:
1063:           if (lightmap_capture_sky) {
1064:                   ambient_light = mix(ambient_light, captured.rgb, captured.a);
1065:           } else {
1066:                   ambient_light = captured.rgb;
1067:           }
1068:   }
1069: #endif
1070:
1071: #endif //BASE PASS
1072:
1073: //
1074: // Lighting
1075: //
1076: #ifdef USE_LIGHTING
1077:
1078: #ifndef USE_VERTEX_LIGHTING
1079:   vec3 L;
1080: #endif
1081:   vec3 light_att = vec3(1.0);
1082:
1083: #ifdef LIGHT_MODE_OMNI
1084:
1085: #ifndef USE_VERTEX_LIGHTING
1086:   vec3 light_vec = light_position - vertex;
1087:   float light_length = length(light_vec);
1088:
1089:   float normalized_distance = light_length / light_range;
1090:   if (normalized_distance < 1.0) {
1091:
1092:           float omni_attenuation = pow(1.0 - normalized_distance, light_attenuation);
1093:
1094:           light_att = vec3(omni_attenuation);
1095:   } else {
1096:           light_att = vec3(0.0);
1097:   }
1098:   L = normalize(light_vec);
1099:
1100: #endif
1101:
1102: #ifdef USE_SHADOW
1103:   {
1104:           highp vec4 splane = shadow_coord;
1105:           float shadow_len = length(splane.xyz);
1106:
1107:           splane.xyz = normalize(splane.xyz);
1108:
1109:           vec4 clamp_rect = light_clamp;
1110:
1111:           if (splane.z >= 0.0) {
1112:                   splane.z += 1.0;
1113:
1114:                   clamp_rect.y += clamp_rect.w;
1115:           } else {
1116:                   splane.z = 1.0 - splane.z;
1117:           }
1118:
1119:           splane.xy /= splane.z;
1120:           splane.xy = splane.xy * 0.5 + 0.5;
1121:           splane.z = shadow_len / light_range;
1122:
1123:           splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
1124:           splane.w = 1.0;
1125:
1126:           float shadow = sample_shadow(light_shadow_atlas, splane);
1127:
1128:           light_att *= shadow;
1129:   }
1130: #endif
1131:
1132: #endif //type omni
1133:
1134: #ifdef LIGHT_MODE_DIRECTIONAL
1135:
1136: #ifndef USE_VERTEX_LIGHTING
1137:   vec3 light_vec = -light_direction;
1138:   L = normalize(light_vec);
1139: #endif
1140:   float depth_z = -vertex.z;
1141:
1142: #ifdef USE_SHADOW
1143:
1144: #ifdef USE_VERTEX_LIGHTING
1145:   //compute shadows in a mobile friendly way
1146:
1147: #ifdef LIGHT_USE_PSSM4
1148:   //take advantage of prefetch
1149:   float shadow1 = sample_shadow(light_directional_shadow, shadow_coord);
1150:   float shadow2 = sample_shadow(light_directional_shadow, shadow_coord2);
1151:   float shadow3 = sample_shadow(light_directional_shadow, shadow_coord3);
1152:   float shadow4 = sample_shadow(light_directional_shadow, shadow_coord4);
1153:
1154:   if (depth_z < light_split_offsets.w) {
1155:           float pssm_fade = 0.0;
1156:           float shadow_att = 1.0;
1157: #ifdef LIGHT_USE_PSSM_BLEND
1158:           float shadow_att2 = 1.0;
1159:           float pssm_blend = 0.0;
1160:           bool use_blend = true;
1161: #endif
1162:           if (depth_z < light_split_offsets.y) {
1163:                   if (depth_z < light_split_offsets.x) {
1164:                           shadow_att = shadow1;
1165:
1166: #ifdef LIGHT_USE_PSSM_BLEND
1167:                           shadow_att2 = shadow2;
1168:
1169:                           pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
1170: #endif
1171:                   } else {
1172:                           shadow_att = shadow2;
1173:
1174: #ifdef LIGHT_USE_PSSM_BLEND
1175:                           shadow_att2 = shadow3;
1176:
1177:                           pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
1178: #endif
1179:                   }
1180:           } else {
1181:                   if (depth_z < light_split_offsets.z) {
1182:
1183:                           shadow_att = shadow3;
1184:
1185: #if defined(LIGHT_USE_PSSM_BLEND)
1186:                           shadow_att2 = shadow4;
1187:                           pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z);
1188: #endif
1189:
1190:                   } else {
1191:
1192:                           shadow_att = shadow4;
1193:                           pssm_fade = smoothstep(light_split_offsets.z, light_split_offsets.w, depth_z);
1194:
1195: #if defined(LIGHT_USE_PSSM_BLEND)
1196:                           use_blend = false;
1197: #endif
1198:                   }
1199:           }
1200: #if defined(LIGHT_USE_PSSM_BLEND)
1201:           if (use_blend) {
1202:                   shadow_att = mix(shadow_att, shadow_att2, pssm_blend);
1203:           }
1204: #endif
1205:           light_att *= shadow_att;
1206:   }
1207:
1208: #endif //LIGHT_USE_PSSM4
1209:
1210: #ifdef LIGHT_USE_PSSM2
1211:
1212:   //take advantage of prefetch
1213:   float shadow1 = sample_shadow(light_directional_shadow, shadow_coord);
1214:   float shadow2 = sample_shadow(light_directional_shadow, shadow_coord2);
1215:
1216:   if (depth_z < light_split_offsets.y) {
1217:           float shadow_att = 1.0;
1218:           float pssm_fade = 0.0;
1219:
1220: #ifdef LIGHT_USE_PSSM_BLEND
1221:           float shadow_att2 = 1.0;
1222:           float pssm_blend = 0.0;
1223:           bool use_blend = true;
1224: #endif
1225:           if (depth_z < light_split_offsets.x) {
1226:                   float pssm_fade = 0.0;
1227:                   shadow_att = shadow1;
1228:
1229: #ifdef LIGHT_USE_PSSM_BLEND
1230:                   shadow_att2 = shadow2;
1231:                   pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
1232: #endif
1233:           } else {
1234:
1235:                   shadow_att = shadow2;
1236:                   pssm_fade = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
1237: #ifdef LIGHT_USE_PSSM_BLEND
1238:                   use_blend = false;
1239: #endif
1240:           }
1241: #ifdef LIGHT_USE_PSSM_BLEND
1242:           if (use_blend) {
1243:                   shadow_att = mix(shadow_att, shadow_att2, pssm_blend);
1244:           }
1245: #endif
1246:           light_att *= shadow_att;
1247:   }
1248:
1249: #endif //LIGHT_USE_PSSM2
1250:
1251: #if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2)
1252:
1253:   light_att *= sample_shadow(light_directional_shadow, shadow_coord);
1254: #endif //orthogonal
1255:
1256: #else //fragment version of pssm
1257:
1258:   {
1259: #ifdef LIGHT_USE_PSSM4
1260:           if (depth_z < light_split_offsets.w) {
1261: #elif defined(LIGHT_USE_PSSM2)
1262:           if (depth_z < light_split_offsets.y) {
1263: #else
1264:           if (depth_z < light_split_offsets.x) {
1265: #endif //pssm2
1266:
1267:                   highp vec4 pssm_coord;
1268:                   float pssm_fade = 0.0;
1269:
1270: #ifdef LIGHT_USE_PSSM_BLEND
1271:                   float pssm_blend;
1272:                   highp vec4 pssm_coord2;
1273:                   bool use_blend = true;
1274: #endif
1275:
1276: #ifdef LIGHT_USE_PSSM4
1277:
1278:                   if (depth_z < light_split_offsets.y) {
1279:                           if (depth_z < light_split_offsets.x) {
1280:                                   pssm_coord = shadow_coord;
1281:
1282: #ifdef LIGHT_USE_PSSM_BLEND
1283:                                   pssm_coord2 = shadow_coord2;
1284:
1285:                                   pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
1286: #endif
1287:                           } else {
1288:                                   pssm_coord = shadow_coord2;
1289:
1290: #ifdef LIGHT_USE_PSSM_BLEND
1291:                                   pssm_coord2 = shadow_coord3;
1292:
1293:                                   pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
1294: #endif
1295:                           }
1296:                   } else {
1297:                           if (depth_z < light_split_offsets.z) {
1298:
1299:                                   pssm_coord = shadow_coord3;
1300:
1301: #if defined(LIGHT_USE_PSSM_BLEND)
1302:                                   pssm_coord2 = shadow_coord4;
1303:                                   pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z);
1304: #endif
1305:
1306:                           } else {
1307:
1308:                                   pssm_coord = shadow_coord4;
1309:                                   pssm_fade = smoothstep(light_split_offsets.z, light_split_offsets.w, depth_z);
1310:
1311: #if defined(LIGHT_USE_PSSM_BLEND)
1312:                                   use_blend = false;
1313: #endif
1314:                           }
1315:                   }
1316:
1317: #endif // LIGHT_USE_PSSM4
1318:
1319: #ifdef LIGHT_USE_PSSM2
1320:                   if (depth_z < light_split_offsets.x) {
1321:
1322:                           pssm_coord = shadow_coord;
1323:
1324: #ifdef LIGHT_USE_PSSM_BLEND
1325:                           pssm_coord2 = shadow_coord2;
1326:                           pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
1327: #endif
1328:                   } else {
1329:
1330:                           pssm_coord = shadow_coord2;
1331:                           pssm_fade = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
1332: #ifdef LIGHT_USE_PSSM_BLEND
1333:                           use_blend = false;
1334: #endif
1335:                   }
1336:
1337: #endif // LIGHT_USE_PSSM2
1338:
1339: #if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2)
1340:                   {
1341:                           pssm_coord = shadow_coord;
1342:                   }
1343: #endif
1344:
1345:                   float shadow = sample_shadow(light_directional_shadow, pssm_coord);
1346:
1347: #ifdef LIGHT_USE_PSSM_BLEND
1348:                   if (use_blend) {
1349:                           shadow = mix(shadow, sample_shadow(light_directional_shadow, pssm_coord2), pssm_blend);
1350:                   }
1351: #endif
1352:
1353:                   light_att *= shadow;
1354:           }
1355:   }
1356: #endif //use vertex lighting
1357:
1358: #endif //use shadow
1359:
1360: #endif
1361:
1362: #ifdef LIGHT_MODE_SPOT
1363:
1364:   light_att = vec3(1.0);
1365:
1366: #ifndef USE_VERTEX_LIGHTING
1367:
1368:   vec3 light_rel_vec = light_position - vertex;
1369:   float light_length = length(light_rel_vec);
1370:   float normalized_distance = light_length / light_range;
1371:
1372:   if (normalized_distance < 1.0) {
1373:           float spot_attenuation = pow(1.0 - normalized_distance, light_attenuation);
1374:           vec3 spot_dir = light_direction;
1375:
1376:           float spot_cutoff = light_spot_angle;
1377:           float angle = dot(-normalize(light_rel_vec), spot_dir);
1378:
1379:           if (angle > spot_cutoff) {
1380:                   float scos = max(angle, spot_cutoff);
1381:                   float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_cutoff));
1382:                   spot_attenuation *= 1.0 - pow(spot_rim, light_spot_attenuation);
1383:
1384:                   light_att = vec3(spot_attenuation);
1385:           } else {
1386:                   light_att = vec3(0.0);
1387:           }
1388:   } else {
1389:           light_att = vec3(0.0);
1390:   }
1391:
1392:   L = normalize(light_rel_vec);
1393:
1394: #endif
1395:
1396: #ifdef USE_SHADOW
1397:   {
1398:           highp vec4 splane = shadow_coord;
1399:           splane.xyz /= splane.w;
1400:
1401:           float shadow = sample_shadow(light_shadow_atlas, splane);
1402:           light_att *= shadow;
1403:   }
1404: #endif
1405:
1406: #endif // LIGHT_MODE_SPOT
1407:
1408: #ifdef USE_VERTEX_LIGHTING
1409:   //vertex lighting
1410:
1411:   specular_light += specular_interp * specular_blob_intensity * light_att;
1412:   diffuse_light += diffuse_interp * albedo * light_att;
1413:
1414: #else
1415:   //fragment lighting
1416:   light_compute(
1417:                   normal,
1418:                   L,
1419:                   eye_position,
1420:                   binormal,
1421:                   tangent,
1422:                   light_color.xyz,
1423:                   light_att,
1424:                   albedo,
1425:                   transmission,
1426:                   specular_blob_intensity * light_specular,
1427:                   roughness,
1428:                   metallic,
1429:                   specular,
1430:                   rim,
1431:                   rim_tint,
1432:                   clearcoat,
1433:                   clearcoat_gloss,
1434:                   anisotropy,
1435:                   diffuse_light,
1436:                   specular_light);
1437:
1438: #endif //vertex lighting
1439:
1440: #endif //USE_LIGHTING
1441:   //compute and merge
1442:
1443: #ifndef RENDER_DEPTH
1444:
1445: #ifdef SHADELESS
1446:
1447:   gl_FragColor = vec4(albedo, alpha);
1448: #else
1449:
1450:   ambient_light *= albedo;
1451:
1452: #if defined(ENABLE_AO)
1453:   ambient_light *= ao;
1454:   ao_light_affect = mix(1.0, ao, ao_light_affect);
1455:   specular_light *= ao_light_affect;
1456:   diffuse_light *= ao_light_affect;
1457: #endif
1458:
1459:   diffuse_light *= 1.0 - metallic;
1460:   ambient_light *= 1.0 - metallic;
1461:
1462:   // environment BRDF approximation
1463:
1464:   {
1465:
1466: #if defined(DIFFUSE_TOON)
1467:           //simplify for toon, as
1468:           specular_light *= specular * metallic * albedo * 2.0;
1469: #else
1470:           //TODO: this curve is not really designed for gammaspace, should be adjusted
1471:           const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
1472:           const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
1473:           vec4 r = roughness * c0 + c1;
1474:           float ndotv = clamp(dot(normal, eye_position), 0.0, 1.0);
1475:           float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
1476:           vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
1477:
1478:           vec3 f0 = F0(metallic, specular, albedo);
1479:           specular_light *= env.x * f0 + env.y;
1480: #endif
1481:   }
1482:
1483:   gl_FragColor = vec4(ambient_light + diffuse_light + specular_light, alpha);
1484:
1485:   //add emission if in base pass
1486: #ifdef BASE_PASS
1487:   gl_FragColor.rgb += emission;
1488: #endif
1489:   // gl_FragColor = vec4(normal, 1.0);
1490:
1491: #endif //unshaded
1492:
1493: //apply fog
1494: #if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED)
1495:
1496: #if defined(USE_VERTEX_LIGHTING)
1497:
1498:   gl_FragColor.rgb = mix(gl_FragColor.rgb, fog_interp.rgb, fog_interp.a);
1499: #else //pixel based fog
1500:   float fog_amount = 0.0;
1501:
1502: #ifdef LIGHT_MODE_DIRECTIONAL
1503:
1504:   vec3 fog_color = mix(fog_color_base.rgb, fog_sun_color_amount.rgb, fog_sun_color_amount.a * pow(max(dot(eye_position, light_direction), 0.0), 8.0));
1505: #else
1506:   vec3 fog_color = fog_color_base.rgb;
1507: #endif
1508:
1509: #ifdef FOG_DEPTH_ENABLED
1510:
1511:   {
1512:
1513:           float fog_z = smoothstep(fog_depth_begin, fog_max_distance, length(vertex));
1514:
1515:           fog_amount = pow(fog_z, fog_depth_curve);
1516:
1517:           if (fog_transmit_enabled) {
1518:                   vec3 total_light = gl_FragColor.rgb;
1519:                   float transmit = pow(fog_z, fog_transmit_curve);
1520:                   fog_color = mix(max(total_light, fog_color), fog_color, transmit);
1521:           }
1522:   }
1523: #endif
1524:
1525: #ifdef FOG_HEIGHT_ENABLED
1526:   {
1527:           float y = (camera_matrix * vec4(vertex, 1.0)).y;
1528:           fog_amount = max(fog_amount, pow(smoothstep(fog_height_min, fog_height_max, y), fog_height_curve));
1529:   }
1530: #endif
1531:
1532:   gl_FragColor.rgb = mix(gl_FragColor.rgb, fog_color, fog_amount);
1533:
1534: #endif //use vertex lit
1535:
1536: #endif // defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED)
1537:
1538: #endif // not RENDER_DEPTH
1539: }
1540:
akien-mga commented 5 years ago

Thanks, that's perfect :)

akien-mga commented 5 years ago

The problematic line is

876:    vec4 m_world_vertex = (inverse(camera_matrix) * vec4(vertex.xyz, 1.0));

which uses inverse(), while it's not available in GLES2's GLSL. Is this something you wrote in your own shader, or is it generate by Godot automatically?

Tobi-La commented 5 years ago

That's a line of a custom shader code. I've taken it from https://godotengine.org/qa/28784/fade-in-animation-of-3d-object

akien-mga commented 5 years ago

Right, so this can't work on GLES2. If you switch back to GLES3 (you can edit it in project.godot) it should work again.

This bug still needs to be fixed though, Godot should not crash when a custom shader is invalid.

Tobi-La commented 5 years ago

Jeah no problem, I don't intend to use 3.1 alpha for my game yet. I just wanted to check it out and thought maybe I could contribute to the project by leaving the issue here. Thank you very much! :)

Does this mean that I will have to rewrite my shader code when 3.1 gets released and I want to use GLES2?

Zireael07 commented 5 years ago

@Tobi-La: unfortunately yes, you'll have to either change the line as a whole or provide two line versions depending on GLES version.

akien-mga commented 5 years ago

Does this mean that I will have to rewrite my shader code when 3.1 gets released and I want to use GLES2?

Most should stay compatible, but if using some functions not available in GLES2, you'll have to rewrite them yeah.

akien-mga commented 5 years ago

Here's an example project using inverse() and GLES2 and thus triggering relatively cryptic error messages: gles2_invalid_shader.zip

We should improve that to have a compilation error shown in the shader editor instead. It doesn't crash for me though.

Error output on Linux/Nvidia (printed out several times in a row):

1: #version 120
2: #define USE_GLES_OVER_GL
3: #define USE_TEXTURE_RECT
4: 
5: #ifdef USE_GLES_OVER_GL
6: #define mediump
7: #define highp
8: #else
9: precision mediump float;
10: precision mediump int;
11: #endif
12: 
13: uniform sampler2D color_texture; // texunit:-1
14: /* clang-format on */
15: uniform highp vec2 color_texpixel_size;
16: uniform mediump sampler2D normal_texture; // texunit:-2
17: 
18: varying mediump vec2 uv_interp;
19: varying mediump vec4 color_interp;
20: 
21: uniform highp float time;
22: 
23: uniform vec4 final_modulate;
24: 
25: #ifdef SCREEN_TEXTURE_USED
26: 
27: uniform sampler2D screen_texture; // texunit:-3
28: 
29: #endif
30: 
31: #ifdef SCREEN_UV_USED
32: 
33: uniform vec2 screen_pixel_size;
34: 
35: #endif
36: 
37: /* clang-format off */
38: 
39: 
40: /* clang-format on */
41: 
42: void main() {
43: 
44:     vec4 color = color_interp;
45: 
46: #if !defined(COLOR_USED)
47:     //default behavior, texture by color
48:     color *= texture2D(color_texture, uv_interp);
49: #endif
50: 
51: #ifdef SCREEN_UV_USED
52:     vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
53: #endif
54:     {
55:             /* clang-format off */
56: {
57:     mat4 m_id = mat4(1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0);
58:     mat4 m_inv_id = inverse(m_id);
59: }
60: 
61: 
62:             /* clang-format on */
63:     }
64: 
65:     color *= final_modulate;
66: 
67:     gl_FragColor = color;
68: }
69: 
ERROR: _display_error_with_code: CanvasShaderGLES2: Fragment shader compilation failed:
0(58) : error C7532: global function inverse requires "#version 140" or later
0(58) : error C0000: ... or #extension GL_ARB_gpu_shader5 : enable

   At: drivers/gles2/shader_gles2.cpp:196.
ERROR: get_current_version: Method/Function Failed, returning: __null
   At: drivers/gles2/shader_gles2.cpp:420.
ERROR: bind: Condition ' !version ' is true. returned: false
   At: drivers/gles2/shader_gles2.cpp:133.
Zireael07 commented 5 years ago

Shader compilation errors are really cryptic indeed - I have one in my spare copy of the racer project that I am using to test alpha2 (and GLES2) and I can't figure out which shader it is :(

Maybe if the shader is saved to a resource file (this is the case for all of mine), the message could at least tell me WHICH file it pertains to?

Tobi-La commented 5 years ago

@akien-mga @Zireael07

How would I rewrite inverse() to be compatible with GLES2 ?

SIsilicon commented 5 years ago

@akien-mga @Zireael07

How would I rewrite inverse() to be compatible with GLES2 ?

You can port a custom inverse function from here. https://github.com/glslify/glsl-inverse/blob/master/index.glsl

akien-mga commented 5 years ago

As I understand it, the issue was due to using inverse in your own shaders, while it's not compatible with GLES2. If so, it's normal that it errors out (the shader validation could maybe be improved on the editor side, but that would be better tracked in a new issue).

Please comment if that inverse() was actually not added by you but by Godot itself, that would be a bug in the GLES2 backend.