Debian Codegraph @

drivers/gles3/shaders/canvas.glsl

raw
1 /* clang-format off */
2 #[modes]
3
4 mode_default =
5
6 #[specializations]
7
8 DISABLE_LIGHTING = true
9 USE_RGBA_SHADOWS = false
10 USE_NINEPATCH = false
11 USE_PRIMITIVE = false
12 USE_ATTRIBUTES = false
13 USE_INSTANCING = false
14
15 #[vertex]
16
17 #ifdef USE_ATTRIBUTES
18 layout(location = 0) in vec2 vertex_attrib;
19 layout(location = 3) in vec4 color_attrib;
20 layout(location = 4) in vec2 uv_attrib;
21
22 #ifdef USE_INSTANCING
23
24 layout(location = 1) in highp vec4 instance_xform0;
25 layout(location = 2) in highp vec4 instance_xform1;
26 layout(location = 5) in highp uvec4 instance_color_custom_data; // Color packed into xy, custom_data packed into zw for compatibility with 3D
27
28 #endif // USE_INSTANCING
29
30 #endif // USE_ATTRIBUTES
31
32 #include "stdlib_inc.glsl"
33
34 #if defined(CUSTOM0_USED)
35 layout(location = 6) in highp vec4 custom0_attrib;
36 #endif
37
38 #if defined(CUSTOM1_USED)
39 layout(location = 7) in highp vec4 custom1_attrib;
40 #endif
41
42 layout(location = 8) in highp vec4 attrib_A;
43 layout(location = 9) in highp vec4 attrib_B;
44 layout(location = 10) in highp vec4 attrib_C;
45 layout(location = 11) in highp vec4 attrib_D;
46 layout(location = 12) in highp vec4 attrib_E;
47 #ifdef USE_PRIMITIVE
48 layout(location = 13) in highp uvec4 attrib_F;
49 #else
50 layout(location = 13) in highp vec4 attrib_F;
51 #endif
52 layout(location = 14) in highp uvec4 attrib_G;
53 layout(location = 15) in highp uvec4 attrib_H;
54
55 #define read_draw_data_world_x attrib_A.xy
56 #define read_draw_data_world_y attrib_A.zw
57 #define read_draw_data_world_ofs attrib_B.xy
58 #define read_draw_data_color_texture_pixel_size attrib_B.zw
59
60 #ifdef USE_PRIMITIVE
61
62 #define read_draw_data_point_a attrib_C.xy
63 #define read_draw_data_point_b attrib_C.zw
64 #define read_draw_data_point_c attrib_D.xy
65 #define read_draw_data_uv_a attrib_D.zw
66 #define read_draw_data_uv_b attrib_E.xy
67 #define read_draw_data_uv_c attrib_E.zw
68
69 #define read_draw_data_color_a_rg attrib_F.x
70 #define read_draw_data_color_a_ba attrib_F.y
71 #define read_draw_data_color_b_rg attrib_F.z
72 #define read_draw_data_color_b_ba attrib_F.w
73 #define read_draw_data_color_c_rg attrib_G.x
74 #define read_draw_data_color_c_ba attrib_G.y
75
76 #else
77
78 #define read_draw_data_modulation attrib_C
79 #define read_draw_data_ninepatch_margins attrib_D
80 #define read_draw_data_dst_rect attrib_E
81 #define read_draw_data_src_rect attrib_F
82
83 #endif
84
85 #define read_draw_data_flags attrib_G.z
86 #define read_draw_data_instance_offset attrib_G.w
87 #define read_draw_data_lights attrib_H
88
89 // Varyings so the per-instance info can be used in the fragment shader
90 flat out vec4 varying_A;
91 flat out vec2 varying_B;
92 #ifndef USE_PRIMITIVE
93 flat out vec4 varying_C;
94 #ifndef USE_ATTRIBUTES
95 #ifdef USE_NINEPATCH
96
97 flat out vec2 varying_D;
98 #endif
99 flat out vec4 varying_E;
100 #endif
101 #endif
102 flat out uvec2 varying_F;
103 flat out uvec4 varying_G;
104
105 // This needs to be outside clang-format so the ubo comment is in the right place
106 #ifdef MATERIAL_UNIFORMS_USED
107 layout(std140) uniform MaterialUniforms{ //ubo:4
108
109 #MATERIAL_UNIFORMS
110
111 };
112 #endif
113
114 uniform mediump uint batch_flags;
115
116 /* clang-format on */
117 #include "canvas_uniforms_inc.glsl"
118
119 out vec2 uv_interp;
120 out vec4 color_interp;
121 out vec2 vertex_interp;
122
123 #ifdef USE_NINEPATCH
124
125 out vec2 pixel_size_interp;
126
127 #endif
128
129 #GLOBALS
130
131 void main() {
132 varying_A = vec4(read_draw_data_world_x, read_draw_data_world_y);
133 varying_B = read_draw_data_color_texture_pixel_size;
134 #ifndef USE_PRIMITIVE
135 varying_C = read_draw_data_ninepatch_margins;
136
137 #ifndef USE_ATTRIBUTES
138 #ifdef USE_NINEPATCH
139 varying_D = vec2(read_draw_data_dst_rect.z, read_draw_data_dst_rect.w);
140 #endif // USE_NINEPATCH
141 varying_E = read_draw_data_src_rect;
142 #endif // !USE_ATTRIBUTES
143 #endif // USE_PRIMITIVE
144
145 varying_F = uvec2(read_draw_data_flags, read_draw_data_instance_offset);
146 varying_G = read_draw_data_lights;
147
148 vec4 instance_custom = vec4(0.0);
149
150 #if defined(CUSTOM0_USED)
151 vec4 custom0 = vec4(0.0);
152 #endif
153 #if defined(CUSTOM1_USED)
154 vec4 custom1 = vec4(0.0);
155 #endif
156
157 #ifdef USE_PRIMITIVE
158 vec2 vertex;
159 vec2 uv;
160 vec4 color;
161
162 if (gl_VertexID % 3 == 0) {
163 vertex = read_draw_data_point_a;
164 uv = read_draw_data_uv_a;
165 color.xy = unpackHalf2x16(read_draw_data_color_a_rg);
166 color.zw = unpackHalf2x16(read_draw_data_color_a_ba);
167 } else if (gl_VertexID % 3 == 1) {
168 vertex = read_draw_data_point_b;
169 uv = read_draw_data_uv_b;
170 color.xy = unpackHalf2x16(read_draw_data_color_b_rg);
171 color.zw = unpackHalf2x16(read_draw_data_color_b_ba);
172 } else {
173 vertex = read_draw_data_point_c;
174 uv = read_draw_data_uv_c;
175 color.xy = unpackHalf2x16(read_draw_data_color_c_rg);
176 color.zw = unpackHalf2x16(read_draw_data_color_c_ba);
177 }
178
179 #elif defined(USE_ATTRIBUTES)
180 vec2 vertex = vertex_attrib;
181 vec4 color = color_attrib * read_draw_data_modulation;
182 vec2 uv = uv_attrib;
183
184 #ifdef USE_INSTANCING
185 if (bool(batch_flags & BATCH_FLAGS_INSTANCING_HAS_COLORS)) {
186 vec4 instance_color;
187 instance_color.xy = unpackHalf2x16(uint(instance_color_custom_data.x));
188 instance_color.zw = unpackHalf2x16(uint(instance_color_custom_data.y));
189 color *= instance_color;
190 }
191 if (bool(batch_flags & BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA)) {
192 instance_custom.xy = unpackHalf2x16(instance_color_custom_data.z);
193 instance_custom.zw = unpackHalf2x16(instance_color_custom_data.w);
194 }
195 #endif // !USE_INSTANCING
196
197 #else // !USE_ATTRIBUTES
198
199 // crash on Adreno 320/330
200 //vec2 vertex_base_arr[6] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0), vec2(0.0, 0.0), vec2(1.0, 1.0));
201 //vec2 vertex_base = vertex_base_arr[gl_VertexID % 6];
202 //-----------------------------------------
203 // ID | 0 | 1 | 2 | 3 | 4 | 5 |
204 //-----------------------------------------
205 // X | 0.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 |
206 // Y | 0.0 | 1.0 | 1.0 | 0.0 | 0.0 | 1.0 |
207 //-----------------------------------------
208 // no crash or freeze on all Adreno 3xx with 'if / else if' and slightly faster!
209 int vertex_id = gl_VertexID % 6;
210 vec2 vertex_base;
211 if (vertex_id == 0) {
212 vertex_base = vec2(0.0, 0.0);
213 } else if (vertex_id == 1) {
214 vertex_base = vec2(0.0, 1.0);
215 } else if (vertex_id == 2) {
216 vertex_base = vec2(1.0, 1.0);
217 } else if (vertex_id == 3) {
218 vertex_base = vec2(1.0, 0.0);
219 } else if (vertex_id == 4) {
220 vertex_base = vec2(0.0, 0.0);
221 } else if (vertex_id == 5) {
222 vertex_base = vec2(1.0, 1.0);
223 }
224
225 vec2 uv = read_draw_data_src_rect.xy + abs(read_draw_data_src_rect.zw) * ((read_draw_data_flags & INSTANCE_FLAGS_TRANSPOSE_RECT) != uint(0) ? vertex_base.yx : vertex_base.xy);
226 vec4 color = read_draw_data_modulation;
227 vec2 vertex = read_draw_data_dst_rect.xy + abs(read_draw_data_dst_rect.zw) * mix(vertex_base, vec2(1.0, 1.0) - vertex_base, lessThan(read_draw_data_src_rect.zw, vec2(0.0, 0.0)));
228
229 #endif // USE_ATTRIBUTES
230
231 #if defined(CUSTOM0_USED)
232 custom0 = custom0_attrib;
233 #endif
234
235 #if defined(CUSTOM1_USED)
236 custom1 = custom1_attrib;
237 #endif
238
239 mat4 model_matrix = mat4(vec4(read_draw_data_world_x, 0.0, 0.0), vec4(read_draw_data_world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(read_draw_data_world_ofs, 0.0, 1.0));
240
241 #ifdef USE_INSTANCING
242 model_matrix = model_matrix * transpose(mat4(instance_xform0, instance_xform1, vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0)));
243 #endif // USE_INSTANCING
244
245 vec2 color_texture_pixel_size = read_draw_data_color_texture_pixel_size;
246
247 #ifdef USE_POINT_SIZE
248 float point_size = 1.0;
249 #endif
250
251 #ifdef USE_WORLD_VERTEX_COORDS
252 vertex = (model_matrix * vec4(vertex, 0.0, 1.0)).xy;
253 #endif
254 {
255 #CODE : VERTEX
256 }
257
258 #ifdef USE_NINEPATCH
259 pixel_size_interp = abs(read_draw_data_dst_rect.zw) * vertex_base;
260 #endif
261
262 #if !defined(SKIP_TRANSFORM_USED) && !defined(USE_WORLD_VERTEX_COORDS)
263 vertex = (model_matrix * vec4(vertex, 0.0, 1.0)).xy;
264 #endif
265
266 color_interp = color;
267
268 vertex = (canvas_transform * vec4(vertex, 0.0, 1.0)).xy;
269
270 if (use_pixel_snap) {
271 vertex = floor(vertex + 0.5);
272 // precision issue on some hardware creates artifacts within texture
273 // offset uv by a small amount to avoid
274 uv += 1e-5;
275 }
276
277 vertex_interp = vertex;
278 uv_interp = uv;
279
280 gl_Position = screen_transform * vec4(vertex, 0.0, 1.0);
281
282 #ifdef USE_POINT_SIZE
283 gl_PointSize = point_size;
284 #endif
285 }
286
287 #[fragment]
288
289 #include "canvas_uniforms_inc.glsl"
290 #include "stdlib_inc.glsl"
291
292 in vec2 uv_interp;
293 in vec2 vertex_interp;
294 in vec4 color_interp;
295
296 #ifdef USE_NINEPATCH
297
298 in vec2 pixel_size_interp;
299
300 #endif
301
302 // Can all be flat as they are the same for the whole batched instance
303 flat in vec4 varying_A;
304 flat in vec2 varying_B;
305 #define read_draw_data_world_x varying_A.xy
306 #define read_draw_data_world_y varying_A.zw
307 #define read_draw_data_color_texture_pixel_size varying_B
308
309 #ifndef USE_PRIMITIVE
310 flat in vec4 varying_C;
311 #define read_draw_data_ninepatch_margins varying_C
312
313 #ifndef USE_ATTRIBUTES
314 #ifdef USE_NINEPATCH
315
316 flat in vec2 varying_D;
317 #define read_draw_data_dst_rect_z varying_D.x
318 #define read_draw_data_dst_rect_w varying_D.y
319 #endif
320
321 flat in vec4 varying_E;
322 #define read_draw_data_src_rect varying_E
323 #endif // USE_ATTRIBUTES
324 #endif // USE_PRIMITIVE
325
326 flat in uvec2 varying_F;
327 flat in uvec4 varying_G;
328 #define read_draw_data_flags varying_F.x
329 #define read_draw_data_instance_offset varying_F.y
330 #define read_draw_data_lights varying_G
331
332 #ifndef DISABLE_LIGHTING
333 uniform sampler2D atlas_texture; //texunit:-2
334 uniform sampler2D shadow_atlas_texture; //texunit:-3
335 #endif // DISABLE_LIGHTING
336 uniform sampler2D color_buffer; //texunit:-4
337 uniform sampler2D sdf_texture; //texunit:-5
338 uniform sampler2D normal_texture; //texunit:-6
339 uniform sampler2D specular_texture; //texunit:-7
340
341 uniform sampler2D color_texture; //texunit:0
342
343 uniform mediump uint batch_flags;
344 uniform highp uint specular_shininess_in;
345
346 layout(location = 0) out vec4 frag_color;
347
348 /* clang-format off */
349 // This needs to be outside clang-format so the ubo comment is in the right place
350 #ifdef MATERIAL_UNIFORMS_USED
351 layout(std140) uniform MaterialUniforms{ //ubo:4
352
353 #MATERIAL_UNIFORMS
354
355 };
356 #endif
357 /* clang-format on */
358
359 #GLOBALS
360
361 float vec4_to_float(vec4 p_vec) {
362 return dot(p_vec, vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0)) * 2.0 - 1.0;
363 }
364
365 vec2 screen_uv_to_sdf(vec2 p_uv) {
366 return screen_to_sdf * p_uv;
367 }
368
369 float texture_sdf(vec2 p_sdf) {
370 vec2 uv = p_sdf * sdf_to_tex.xy + sdf_to_tex.zw;
371 float d = vec4_to_float(texture(sdf_texture, uv));
372 d *= SDF_MAX_LENGTH;
373 return d * tex_to_sdf;
374 }
375
376 vec2 texture_sdf_normal(vec2 p_sdf) {
377 vec2 uv = p_sdf * sdf_to_tex.xy + sdf_to_tex.zw;
378
379 const float EPSILON = 0.001;
380 return normalize(vec2(
381 vec4_to_float(texture(sdf_texture, uv + vec2(EPSILON, 0.0))) - vec4_to_float(texture(sdf_texture, uv - vec2(EPSILON, 0.0))),
382 vec4_to_float(texture(sdf_texture, uv + vec2(0.0, EPSILON))) - vec4_to_float(texture(sdf_texture, uv - vec2(0.0, EPSILON)))));
383 }
384
385 vec2 sdf_to_screen_uv(vec2 p_sdf) {
386 return p_sdf * sdf_to_screen;
387 }
388
389 #ifndef DISABLE_LIGHTING
390 #ifdef LIGHT_CODE_USED
391
392 vec4 light_compute(
393 vec3 light_vertex,
394 vec3 light_position,
395 vec3 normal,
396 vec4 light_color,
397 float light_energy,
398 vec4 specular_shininess,
399 inout vec4 shadow_modulate,
400 vec2 screen_uv,
401 vec2 uv,
402 vec4 color, bool is_directional) {
403 vec4 light = vec4(0.0);
404 vec3 light_direction = vec3(0.0);
405
406 if (is_directional) {
407 light_direction = normalize(mix(vec3(light_position.xy, 0.0), vec3(0, 0, 1), light_position.z));
408 light_position = vec3(0.0);
409 } else {
410 light_direction = normalize(light_position - light_vertex);
411 }
412
413 #CODE : LIGHT
414
415 return light;
416 }
417
418 #endif
419
420 vec3 light_normal_compute(vec3 light_vec, vec3 normal, vec3 base_color, vec3 light_color, vec4 specular_shininess, bool specular_shininess_used) {
421 float cNdotL = max(0.0, dot(normal, light_vec));
422
423 if (specular_shininess_used) {
424 //blinn
425 vec3 view = vec3(0.0, 0.0, 1.0); // not great but good enough
426 vec3 half_vec = normalize(view + light_vec);
427
428 float cNdotV = max(dot(normal, view), 0.0);
429 float cNdotH = max(dot(normal, half_vec), 0.0);
430 float cVdotH = max(dot(view, half_vec), 0.0);
431 float cLdotH = max(dot(light_vec, half_vec), 0.0);
432 float shininess = exp2(15.0 * specular_shininess.a + 1.0) * 0.25;
433 float blinn = pow(cNdotH, shininess);
434 blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
435 float s = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
436
437 return specular_shininess.rgb * light_color * s + light_color * base_color * cNdotL;
438 } else {
439 return light_color * base_color * cNdotL;
440 }
441 }
442
443 #ifdef USE_RGBA_SHADOWS
444
445 #define SHADOW_DEPTH(m_uv) (dot(textureLod(shadow_atlas_texture, (m_uv), 0.0), vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0)) * 2.0 - 1.0)
446
447 #else
448
449 #define SHADOW_DEPTH(m_uv) (textureLod(shadow_atlas_texture, (m_uv), 0.0).r)
450
451 #endif
452
453 /* clang-format off */
454 #define SHADOW_TEST(m_uv) { highp float sd = SHADOW_DEPTH(m_uv); shadow += step(sd, shadow_uv.z / shadow_uv.w); }
455 /* clang-format on */
456
457 //float distance = length(shadow_pos);
458 vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv
459 #ifdef LIGHT_CODE_USED
460 ,
461 vec3 shadow_modulate
462 #endif
463 ) {
464 float shadow = 0.0;
465 uint shadow_mode = light_array[light_base].flags & LIGHT_FLAGS_FILTER_MASK;
466
467 if (shadow_mode == LIGHT_FLAGS_SHADOW_NEAREST) {
468 SHADOW_TEST(shadow_uv.xy);
469 } else if (shadow_mode == LIGHT_FLAGS_SHADOW_PCF5) {
470 vec2 shadow_pixel_size = vec2(light_array[light_base].shadow_pixel_size, 0.0);
471 SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 2.0);
472 SHADOW_TEST(shadow_uv.xy - shadow_pixel_size);
473 SHADOW_TEST(shadow_uv.xy);
474 SHADOW_TEST(shadow_uv.xy + shadow_pixel_size);
475 SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 2.0);
476 shadow /= 5.0;
477 } else { //PCF13
478 vec2 shadow_pixel_size = vec2(light_array[light_base].shadow_pixel_size, 0.0);
479 SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 6.0);
480 SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 5.0);
481 SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 4.0);
482 SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 3.0);
483 SHADOW_TEST(shadow_uv.xy - shadow_pixel_size * 2.0);
484 SHADOW_TEST(shadow_uv.xy - shadow_pixel_size);
485 SHADOW_TEST(shadow_uv.xy);
486 SHADOW_TEST(shadow_uv.xy + shadow_pixel_size);
487 SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 2.0);
488 SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 3.0);
489 SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 4.0);
490 SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 5.0);
491 SHADOW_TEST(shadow_uv.xy + shadow_pixel_size * 6.0);
492 shadow /= 13.0;
493 }
494
495 vec4 shadow_color = godot_unpackUnorm4x8(light_array[light_base].shadow_color);
496 #ifdef LIGHT_CODE_USED
497 shadow_color.rgb *= shadow_modulate;
498 #endif
499
500 shadow_color.a *= light_color.a; //respect light alpha
501
502 return mix(light_color, shadow_color, shadow);
503 }
504
505 void light_blend_compute(uint light_base, vec4 light_color, inout vec3 color) {
506 uint blend_mode = light_array[light_base].flags & LIGHT_FLAGS_BLEND_MASK;
507
508 if (blend_mode == LIGHT_FLAGS_BLEND_MODE_ADD) {
509 color.rgb += light_color.rgb * light_color.a;
510 } else if (blend_mode == LIGHT_FLAGS_BLEND_MODE_SUB) {
511 color.rgb -= light_color.rgb * light_color.a;
512 } else if (blend_mode == LIGHT_FLAGS_BLEND_MODE_MIX) {
513 color.rgb = mix(color.rgb, light_color.rgb, light_color.a);
514 }
515 }
516
517 #endif
518
519 #ifdef USE_NINEPATCH
520
521 float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, int np_repeat, inout int draw_center) {
522 float tex_size = 1.0 / tex_pixel_size;
523
524 if (pixel < margin_begin) {
525 return pixel * tex_pixel_size;
526 } else if (pixel >= draw_size - margin_end) {
527 return (tex_size - (draw_size - pixel)) * tex_pixel_size;
528 } else {
529 if (!bool(read_draw_data_flags & INSTANCE_FLAGS_NINEPATCH_DRAW_CENTER)) {
530 draw_center--;
531 }
532
533 // np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum.
534 if (np_repeat == 0) { // Stretch.
535 // Convert to ratio.
536 float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end);
537 // Scale to source texture.
538 return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size;
539 } else if (np_repeat == 1) { // Tile.
540 // Convert to offset.
541 float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end);
542 // Scale to source texture.
543 return (margin_begin + ofs) * tex_pixel_size;
544 } else if (np_repeat == 2) { // Tile Fit.
545 // Calculate scale.
546 float src_area = draw_size - margin_begin - margin_end;
547 float dst_area = tex_size - margin_begin - margin_end;
548 float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5));
549 // Convert to ratio.
550 float ratio = (pixel - margin_begin) / src_area;
551 ratio = mod(ratio * scale, 1.0);
552 // Scale to source texture.
553 return (margin_begin + ratio * dst_area) * tex_pixel_size;
554 } else { // Shouldn't happen, but silences compiler warning.
555 return 0.0;
556 }
557 }
558 }
559
560 #endif
561
562 float msdf_median(float r, float g, float b) {
563 return max(min(r, g), min(max(r, g), b));
564 }
565
566 void main() {
567 vec4 color = color_interp;
568 vec2 uv = uv_interp;
569 vec2 vertex = vertex_interp;
570
571 #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
572 vec4 region_rect = read_draw_data_src_rect;
573 #else
574 vec4 region_rect = vec4(0.0, 0.0, 1.0 / read_draw_data_color_texture_pixel_size);
575 #endif
576
577 #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
578
579 #ifdef USE_NINEPATCH
580
581 int draw_center = 2;
582 uv = vec2(
583 map_ninepatch_axis(pixel_size_interp.x, abs(read_draw_data_dst_rect_z), read_draw_data_color_texture_pixel_size.x, read_draw_data_ninepatch_margins.x, read_draw_data_ninepatch_margins.z, int(read_draw_data_flags >> INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT) & 0x3, draw_center),
584 map_ninepatch_axis(pixel_size_interp.y, abs(read_draw_data_dst_rect_w), read_draw_data_color_texture_pixel_size.y, read_draw_data_ninepatch_margins.y, read_draw_data_ninepatch_margins.w, int(read_draw_data_flags >> INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT) & 0x3, draw_center));
585
586 if (draw_center == 0) {
587 color.a = 0.0;
588 }
589
590 uv = uv * read_draw_data_src_rect.zw + read_draw_data_src_rect.xy; //apply region if needed
591
592 #endif
593 if (bool(read_draw_data_flags & INSTANCE_FLAGS_CLIP_RECT_UV)) {
594 vec2 half_texpixel = read_draw_data_color_texture_pixel_size * 0.5;
595 uv = clamp(uv, read_draw_data_src_rect.xy + half_texpixel, read_draw_data_src_rect.xy + abs(read_draw_data_src_rect.zw) - half_texpixel);
596 }
597
598 #endif
599
600 #ifndef USE_PRIMITIVE
601 if (bool(read_draw_data_flags & INSTANCE_FLAGS_USE_MSDF)) {
602 float px_range = read_draw_data_ninepatch_margins.x;
603 float outline_thickness = read_draw_data_ninepatch_margins.y;
604
605 vec4 msdf_sample = texture(color_texture, uv);
606 vec2 msdf_size = vec2(textureSize(color_texture, 0));
607 vec2 dest_size = vec2(1.0) / fwidth(uv);
608 float px_size = max(0.5 * dot((vec2(px_range) / msdf_size), dest_size), 1.0);
609 float d = msdf_median(msdf_sample.r, msdf_sample.g, msdf_sample.b);
610
611 if (outline_thickness > 0.0) {
612 float cr = clamp(outline_thickness, 0.0, (px_range / 2.0) - 1.0) / px_range;
613 d = min(d, msdf_sample.a);
614 float a = clamp((d - 0.5 + cr) * px_size, 0.0, 1.0);
615 color.a = a * color.a;
616 } else {
617 float a = clamp((d - 0.5) * px_size + 0.5, 0.0, 1.0);
618 color.a = a * color.a;
619 }
620 } else if (bool(read_draw_data_flags & INSTANCE_FLAGS_USE_LCD)) {
621 vec4 lcd_sample = texture(color_texture, uv);
622 if (lcd_sample.a == 1.0) {
623 color.rgb = lcd_sample.rgb * color.a;
624 } else {
625 color = vec4(0.0, 0.0, 0.0, 0.0);
626 }
627 } else {
628 #else
629 {
630 #endif
631 color *= texture(color_texture, uv);
632 }
633
634 uint light_count = read_draw_data_flags & uint(0xF); // Max 16 lights.
635 bool using_light = light_count > 0u || directional_light_count > 0u;
636
637 vec3 normal;
638
639 #if defined(NORMAL_USED)
640 bool normal_used = true;
641 #else
642 bool normal_used = false;
643 #endif
644
645 if (normal_used || (using_light && bool(batch_flags & BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED))) {
646 normal.xy = texture(normal_texture, uv).xy * vec2(2.0, -2.0) - vec2(1.0, -1.0);
647
648 #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
649 if (bool(read_draw_data_flags & INSTANCE_FLAGS_TRANSPOSE_RECT)) {
650 normal.xy = normal.yx;
651 }
652 normal.xy *= sign(read_draw_data_src_rect.zw);
653 #endif
654
655 normal.z = sqrt(max(0.0, 1.0 - dot(normal.xy, normal.xy)));
656 normal_used = true;
657 } else {
658 normal = vec3(0.0, 0.0, 1.0);
659 }
660
661 vec4 specular_shininess;
662
663 #if defined(SPECULAR_SHININESS_USED)
664
665 bool specular_shininess_used = true;
666 #else
667 bool specular_shininess_used = false;
668 #endif
669
670 if (specular_shininess_used || (using_light && normal_used && bool(batch_flags & BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED))) {
671 specular_shininess = texture(specular_texture, uv);
672 specular_shininess *= godot_unpackUnorm4x8(specular_shininess_in);
673 specular_shininess_used = true;
674 } else {
675 specular_shininess = vec4(1.0);
676 }
677
678 #if defined(SCREEN_UV_USED)
679 vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size;
680 #else
681 vec2 screen_uv = vec2(0.0);
682 #endif
683
684 vec2 color_texture_pixel_size = read_draw_data_color_texture_pixel_size.xy;
685
686 vec3 light_vertex = vec3(vertex, 0.0);
687 vec2 shadow_vertex = vertex;
688
689 {
690 float normal_map_depth = 1.0;
691
692 #if defined(NORMAL_MAP_USED)
693 vec3 normal_map = vec3(0.0, 0.0, 1.0);
694 normal_used = true;
695 #endif
696
697 #CODE : FRAGMENT
698
699 #if defined(NORMAL_MAP_USED)
700 normal = mix(vec3(0.0, 0.0, 1.0), normal_map * vec3(2.0, -2.0, 1.0) - vec3(1.0, -1.0, 0.0), normal_map_depth);
701 #endif
702 }
703
704 if (normal_used) {
705 //convert by item transform
706 normal.xy = mat2(normalize(read_draw_data_world_x), normalize(read_draw_data_world_y)) * normal.xy;
707 //convert by canvas transform
708 normal = normalize((canvas_normal_transform * vec4(normal, 0.0)).xyz);
709 }
710
711 vec4 base_color = color;
712
713 #ifdef MODE_LIGHT_ONLY
714 float light_only_alpha = 0.0;
715 #elif !defined(MODE_UNSHADED)
716 color *= canvas_modulation;
717 #endif
718
719 #if !defined(DISABLE_LIGHTING) && !defined(MODE_UNSHADED)
720
721 // Directional Lights
722
723 for (uint i = 0u; i < directional_light_count; i++) {
724 uint light_base = i;
725
726 vec2 direction = light_array[light_base].position;
727 vec4 light_color = light_array[light_base].color;
728
729 #ifdef LIGHT_CODE_USED
730
731 vec4 shadow_modulate = vec4(1.0);
732 light_color = light_compute(light_vertex, vec3(direction, light_array[light_base].height), normal, light_color, light_color.a, specular_shininess, shadow_modulate, screen_uv, uv, base_color, true);
733 #else
734
735 if (normal_used) {
736 vec3 light_vec = normalize(mix(vec3(direction, 0.0), vec3(0, 0, 1), light_array[light_base].height));
737 light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used);
738 } else {
739 light_color.rgb *= base_color.rgb;
740 }
741 #endif
742
743 if (bool(light_array[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) {
744 vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_array[light_base].shadow_matrix[0], light_array[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations.
745
746 vec4 shadow_uv = vec4(shadow_pos.x, light_array[light_base].shadow_y_ofs, shadow_pos.y * light_array[light_base].shadow_zfar_inv, 1.0);
747
748 light_color = light_shadow_compute(light_base, light_color, shadow_uv
749 #ifdef LIGHT_CODE_USED
750 ,
751 shadow_modulate.rgb
752 #endif
753 );
754 }
755
756 light_blend_compute(light_base, light_color, color.rgb);
757 #ifdef MODE_LIGHT_ONLY
758 light_only_alpha += light_color.a;
759 #endif
760 }
761
762 // Positional Lights
763
764 for (uint i = 0u; i < MAX_LIGHTS_PER_ITEM; i++) {
765 if (i >= light_count) {
766 break;
767 }
768 uint light_base;
769 if (i < 8u) {
770 if (i < 4u) {
771 light_base = read_draw_data_lights[0];
772 } else {
773 light_base = read_draw_data_lights[1];
774 }
775 } else {
776 if (i < 12u) {
777 light_base = read_draw_data_lights[2];
778 } else {
779 light_base = read_draw_data_lights[3];
780 }
781 }
782 light_base >>= (i & 3u) * 8u;
783 light_base &= uint(0xFF);
784
785 vec2 tex_uv = (vec4(vertex, 0.0, 1.0) * mat4(light_array[light_base].texture_matrix[0], light_array[light_base].texture_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations.
786 vec2 tex_uv_atlas = tex_uv * light_array[light_base].atlas_rect.zw + light_array[light_base].atlas_rect.xy;
787
788 if (any(lessThan(tex_uv, vec2(0.0, 0.0))) || any(greaterThanEqual(tex_uv, vec2(1.0, 1.0)))) {
789 //if outside the light texture, light color is zero
790 continue;
791 }
792
793 vec4 light_color = textureLod(atlas_texture, tex_uv_atlas, 0.0);
794 vec4 light_base_color = light_array[light_base].color;
795
796 #ifdef LIGHT_CODE_USED
797
798 vec4 shadow_modulate = vec4(1.0);
799 vec3 light_position = vec3(light_array[light_base].position, light_array[light_base].height);
800
801 light_color.rgb *= light_base_color.rgb;
802 light_color = light_compute(light_vertex, light_position, normal, light_color, light_base_color.a, specular_shininess, shadow_modulate, screen_uv, uv, base_color, false);
803 #else
804
805 light_color.rgb *= light_base_color.rgb * light_base_color.a;
806
807 if (normal_used) {
808 vec3 light_pos = vec3(light_array[light_base].position, light_array[light_base].height);
809 vec3 pos = light_vertex;
810 vec3 light_vec = normalize(light_pos - pos);
811
812 light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used);
813 } else {
814 light_color.rgb *= base_color.rgb;
815 }
816 #endif
817
818 if (bool(light_array[light_base].flags & LIGHT_FLAGS_HAS_SHADOW) && bool(read_draw_data_flags & uint(INSTANCE_FLAGS_SHADOW_MASKED << i))) {
819 vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_array[light_base].shadow_matrix[0], light_array[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations.
820
821 vec2 pos_norm = normalize(shadow_pos);
822 vec2 pos_abs = abs(pos_norm);
823 vec2 pos_box = pos_norm / max(pos_abs.x, pos_abs.y);
824 vec2 pos_rot = pos_norm * mat2(vec2(0.7071067811865476, -0.7071067811865476), vec2(0.7071067811865476, 0.7071067811865476)); //is there a faster way to 45 degrees rot?
825 float tex_ofs;
826 float dist;
827 if (pos_rot.y > 0.0) {
828 if (pos_rot.x > 0.0) {
829 tex_ofs = pos_box.y * 0.125 + 0.125;
830 dist = shadow_pos.x;
831 } else {
832 tex_ofs = pos_box.x * -0.125 + (0.25 + 0.125);
833 dist = shadow_pos.y;
834 }
835 } else {
836 if (pos_rot.x < 0.0) {
837 tex_ofs = pos_box.y * -0.125 + (0.5 + 0.125);
838 dist = -shadow_pos.x;
839 } else {
840 tex_ofs = pos_box.x * 0.125 + (0.75 + 0.125);
841 dist = -shadow_pos.y;
842 }
843 }
844
845 dist *= light_array[light_base].shadow_zfar_inv;
846
847 //float distance = length(shadow_pos);
848 vec4 shadow_uv = vec4(tex_ofs, light_array[light_base].shadow_y_ofs, dist, 1.0);
849
850 light_color = light_shadow_compute(light_base, light_color, shadow_uv
851 #ifdef LIGHT_CODE_USED
852 ,
853 shadow_modulate.rgb
854 #endif
855 );
856 }
857
858 light_blend_compute(light_base, light_color, color.rgb);
859 #ifdef MODE_LIGHT_ONLY
860 light_only_alpha += light_color.a;
861 #endif
862 }
863 #endif
864
865 #ifdef MODE_LIGHT_ONLY
866 color.a *= light_only_alpha;
867 #endif
868
869 frag_color = color;
870 }