Repository: godot
Issue: CurveTexture throwing error in particles shader generation
Labels: bug,topic:core,topic:rendering

<!-- Please search existing issues for potential duplicates before filing yours:
https://github.com/godotengine/godot/issues?q=is%3Aissue
-->

**Godot version:**
<!-- Specify commit hash if non-official. -->
3.0.6 stable

**OS/device including version:**
<!-- Specify GPU model and drivers if graphics-related. -->


**Issue description:**
<!-- What happened, and what was expected. -->
I noticed a really rare error thrown in a project I am working on.
It seems to be related to the shader generation in 'particles.cpp'.
Pinging @akien-mga as you had some more and better info on this issue, thanks!
```
0 shader_type particles;
1 uniform float spread;
2 uniform float flatness;
3 uniform float initial_linear_velocity;
4 uniform float initial_angle;
5 uniform float angular_velocity;
6 uniform float orbit_velocity;
7 uniform float linear_accel;
8 uniform float radial_accel;
9 uniform float tangent_accel;
10 uniform float damping;
11 uniform float scale;
12 uniform float hue_variation;
13 uniform float anim_speed;
14 uniform float anim_offset;
15 uniform float initial_linear_velocity_random;
16 uniform float initial_angle_random;
17 uniform float angular_velocity_random;
18 uniform float orbit_velocity_random;
19 uniform float linear_accel_random;
20 uniform float radial_accel_random;
21 uniform float tangent_accel_random;
22 uniform float damping_random;
23 uniform float scale_random;
24 uniform float hue_variation_random;
25 uniform float anim_speed_random;
26 uniform float anim_offset_random;
27 uniform vec3 emission_box_extents;
28 uniform vec4 color_value : hint_color;
29 uniform int trail_divisor;
30 uniform vec3 gravity;
31 uniform sampler2D damping_texture;
32
33
34 float rand_from_seed(inout uint seed) {
35  int k;
36  int s = int(seed);
37  if (s == 0)
38  s = 305420679;
39  k = s / 127773;
40  s = 16807 * (s - k * 127773) - 2836 * k;
41  if (s < 0)
42      s += 2147483647;
43  seed = uint(s);
44  return float(seed % uint(65536))/65535.0;
45 }
46
47 float rand_from_seed_m1_p1(inout uint seed) {
48  return rand_from_seed(seed)*2.0-1.0;
49 }
50
51 uint hash(uint x) {
52  x = ((x >> uint(16)) ^ x) * uint(73244475);
53  x = ((x >> uint(16)) ^ x) * uint(73244475);
54  x = (x >> uint(16)) ^ x;
55  return x;
56 }
57
58 void vertex() {
59  uint base_number = NUMBER/uint(trail_divisor);
60  uint alt_seed = hash(base_number+uint(1)+RANDOM_SEED);
61  float angle_rand = rand_from_seed(alt_seed);
62  float scale_rand = rand_from_seed(alt_seed);
63  float hue_rot_rand = rand_from_seed(alt_seed);
64  float anim_offset_rand = rand_from_seed(alt_seed);
65  float pi = 3.14159;
66  float degree_to_rad = pi / 180.0;
67
68  if (RESTART) {
69      float tex_linear_velocity = 0.0;
70      float tex_angle = 0.0;
71      float tex_anim_offset = 0.0;
72      float spread_rad = spread*degree_to_rad;
73      float angle1_rad = rand_from_seed_m1_p1(alt_seed)*spread_rad;
74      vec3 rot = vec3( cos(angle1_rad), sin(angle1_rad),0.0 );
75      VELOCITY = rot*initial_linear_velocity*mix(1.0, rand_from_seed(alt_seed), initial_linear_velocity_random);
76      float base_angle = (initial_angle+tex_angle)*mix(1.0,angle_rand,initial_angle_random);
77      CUSTOM.x = base_angle*degree_to_rad;
78      CUSTOM.y = 0.0;
79      CUSTOM.z = (anim_offset+tex_anim_offset)*mix(1.0,anim_offset_rand,anim_offset_random);
80      TRANSFORM[3].xyz = vec3(rand_from_seed(alt_seed) * 2.0 - 1.0, rand_from_seed(alt_seed) * 2.0-1.0, rand_from_seed(alt_seed) * 2.0-1.0)*emission_box_extents;
81      VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY,0.0)).xyz;
82      TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;
83      VELOCITY.z = 0.0;
84      TRANSFORM[3].z = 0.0;
85  } else {
86      CUSTOM.y += DELTA/LIFETIME;
87      float tex_linear_velocity = 0.0;
88      float tex_orbit_velocity = 0.0;
89      float tex_angular_velocity = 0.0;
90      float tex_linear_accel = 0.0;
91      float tex_radial_accel = 0.0;
92      float tex_tangent_accel = 0.0;
93      float tex_damping = textureLod(damping_texture,vec2(CUSTOM.y,0.0),0.0).r;
94      float tex_angle = 0.0;
95      float tex_anim_speed = 0.0;
96      float tex_anim_offset = 0.0;
97      vec3 force = gravity;
98      vec3 pos = TRANSFORM[3].xyz;
99      pos.z = 0.0;
100         //apply linear acceleration
101         force += length(VELOCITY) > 0.0 ? normalize(VELOCITY) * (linear_accel+tex_linear_accel)*mix(1.0,rand_from_seed(alt_seed),linear_accel_random) : vec3(0.0);
102         //apply radial acceleration
103         vec3 org = EMISSION_TRANSFORM[3].xyz;
104         vec3 diff = pos-org;
105         force += length(diff) > 0.0 ? normalize(diff) * (radial_accel+tex_radial_accel)*mix(1.0,rand_from_seed(alt_seed),radial_accel_random) : vec3(0.0);
106         //apply tangential acceleration;
107         force += length(diff.yx) > 0.0 ? vec3(normalize(diff.yx * vec2(-1.0,1.0)),0.0) * ((tangent_accel+tex_tangent_accel)*mix(1.0,rand_from_seed(alt_seed),tangent_accel_random)) : vec3(0.0);
108         //apply attractor forces
109         VELOCITY += force * DELTA;
110         //orbit velocity
111         float orbit_amount = (orbit_velocity+tex_orbit_velocity)*mix(1.0,rand_from_seed(alt_seed),orbit_velocity_random);
112         if (orbit_amount!=0.0) {
113              float ang = orbit_amount * DELTA * pi * 2.0;
114              mat2 rot = mat2(vec2(cos(ang),-sin(ang)),vec2(sin(ang),cos(ang)));
115              TRANSFORM[3].xy-=diff.xy;
116              TRANSFORM[3].xy+=rot * diff.xy;
117         }
118         if (damping + tex_damping > 0.0) {
119        
120             float v = length(VELOCITY);
121             float damp = (damping+tex_damping)*mix(1.0,rand_from_seed(alt_seed),damping_random);
122             v -= damp * DELTA;
123             if (v < 0.0) {
124                 VELOCITY = vec3(0.0);
125             } else {
126                 VELOCITY = normalize(VELOCITY) * v;
127             }
128         }
129         float base_angle = (initial_angle+tex_angle)*mix(1.0,angle_rand,initial_angle_random);
130         base_angle += CUSTOM.y*LIFETIME*(angular_velocity+tex_angular_velocity)*mix(1.0,rand_from_seed(alt_seed)*2.0-1.0,angular_velocity_random);
131         CUSTOM.x = base_angle*degree_to_rad;
132         CUSTOM.z = (anim_offset+tex_anim_offset)*mix(1.0,anim_offset_rand,anim_offset_random)+CUSTOM.y*(anim_speed+tex_anim_speed)*mix(1.0,rand_from_seed(alt_seed),anim_speed_random);
133         CUSTOM.z = clamp(CUSTOM.z,0.0,1.0);
134     }
135     float tex_scale = textureLod(scale_texture,vec2(CUSTOM.y,0.0),0.0).r;
136     float tex_hue_variation = 0.0;
137     float hue_rot_angle = (hue_variation+tex_hue_variation)*pi*2.0*mix(1.0,hue_rot_rand*2.0-1.0,hue_variation_random);
138     float hue_rot_c = cos(hue_rot_angle);
139     float hue_rot_s = sin(hue_rot_angle);
140     mat4 hue_rot_mat = mat4( vec4(0.299,  0.587,  0.114, 0.0),
141             vec4(0.299,  0.587,  0.114, 0.0),
142             vec4(0.299,  0.587,  0.114, 0.0),
143             vec4(0.000,  0.000,  0.000, 1.0)) +
144         mat4( vec4(0.701, -0.587, -0.114, 0.0),
145             vec4(-0.299,  0.413, -0.114, 0.0),
146             vec4(-0.300, -0.588,  0.886, 0.0),
147             vec4(0.000,  0.000,  0.000, 0.0)) * hue_rot_c +
148         mat4( vec4(0.168,  0.330, -0.497, 0.0),
149             vec4(-0.328,  0.035,  0.292, 0.0),
150             vec4(1.250, -1.050, -0.203, 0.0),
151             vec4(0.000,  0.000,  0.000, 0.0)) * hue_rot_s;
152     COLOR = color_value * hue_rot_mat;
153
154     TRANSFORM[0] = vec4(cos(CUSTOM.x),-sin(CUSTOM.x),0.0,0.0);
155     TRANSFORM[1] = vec4(sin(CUSTOM.x),cos(CUSTOM.x),0.0,0.0);
156     TRANSFORM[2] = vec4(0.0,0.0,1.0,0.0);
157     float base_scale = mix(scale*tex_scale,1.0,scale_random*scale_rand);
158     if (base_scale==0.0) base_scale=0.000001;
159     TRANSFORM[0].xyz *= base_scale;
160     TRANSFORM[1].xyz *= base_scale;
161     TRANSFORM[2].xyz *= base_scale;
162     VELOCITY.z = 0.0;
163     TRANSFORM[3].z = 0.0;
164 }
165
166
SHADER ERROR: (null): Unknown identifier in expression: scale_texture
   At: :136.
ERROR: _update_shader: Condition ' err != OK ' is true.
   At: drivers/gles3/rasterizer_storage_gles3.cpp:1697.
```

**Steps to reproduce:**
Unfortunately I can't reproduce this easily right now, but I will try to make a minimal project that can.

**Minimal reproduction project:**
<!-- Recommended as it greatly speeds up debugging. Drag and drop a zip archive to upload it. -->