Skip to content

Commit e40ce70

Browse files
committed
replace while loops with for
1 parent de0c62a commit e40ce70

27 files changed

+94
-256
lines changed

shaders/src/a_lot_of_spheres.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ impl Inputs {
154154
let mut dis: Vec3 = (pos - ro + 0.5 * Vec3::splat(GRIDSIZE) + rs * 0.5) * ri;
155155
let mut mm: Vec3;
156156

157-
let mut i = 0;
158-
while i < RAYCASTSTEPS {
157+
for _ in 0..RAYCASTSTEPS {
159158
if *material > 1 || ro.xz().distance(pos.xz()) > *dist + GRIDSIZE {
160159
break;
161160
}
@@ -184,7 +183,6 @@ impl Inputs {
184183
mm = dis.step(dis.zyx());
185184
dis += mm * rs * ri;
186185
pos += mm * rs;
187-
i += 1;
188186
}
189187

190188
let mut color: Vec3 = Vec3::ZERO;
@@ -195,10 +193,8 @@ impl Inputs {
195193
if *material == 1 || *material == 3 {
196194
// lightning
197195
let c: Vec3 = vec3(-GRIDSIZE, 0.0, GRIDSIZE);
198-
let mut x = 0;
199-
while x < 3 {
200-
let mut y = 0;
201-
while y < 3 {
196+
for x in 0..3 {
197+
for y in 0..3 {
202198
let mapoffset: Vec2 = map + vec2([c.x, c.y, c.z][x], [c.x, c.y, c.z][y]);
203199
let mut offset: Vec2 = Vec2::ZERO;
204200
get_sphere_offset(mapoffset, &mut offset);
@@ -209,12 +205,9 @@ impl Inputs {
209205
let mut shadow: f32 = 1.0;
210206

211207
if SHADOW && *material == 1 {
212-
let mut sx = 0;
213-
while sx < 3 {
214-
let mut sy = 0;
215-
while sy < 3 {
208+
for _ in 0..3 {
209+
for _ in 0..3 {
216210
if shadow < 1.0 {
217-
sy += 1;
218211
continue;
219212
}
220213

@@ -235,19 +228,15 @@ impl Inputs {
235228
) {
236229
shadow = 0.0;
237230
}
238-
sy += 1;
239231
}
240-
sx += 1;
241232
}
242233
}
243234
color += col
244235
* lcolor
245236
* (shadow
246237
* ((lpos - *intersection).normalize().dot(*normal)).max(0.0)
247238
* (1. - (lpos.distance(*intersection) / GRIDSIZE).clamp(0.0, 1.)));
248-
y += 1;
249239
}
250-
x += 1;
251240
}
252241
} else {
253242
// emitter

shaders/src/a_question_of_time.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,30 @@ fn apollonian(uv: Vec2) -> Vec3 {
9393
dec[0] = vec3(0.0, 0.0, -1.0 / ra);
9494
let radius: f32 = 0.5 * (ra - rb);
9595
let bend: f32 = 1.0 / radius;
96-
let mut i = 1;
97-
while i < 4 {
96+
for i in 1..4 {
9897
dec[i] = vec3((i as f32 * a).cos(), (i as f32 * a).sin(), bend);
9998
// if the point is in one of the starting circles we have already found our solution
10099
if (uv - dec[i].xy()).length() < radius {
101100
return (uv - dec[i].xy()).extend(radius);
102101
}
103-
i += 1;
104102
}
105103

106104
// Now that we have a starting DEC we are going to try to
107105
// find the solution for the current point
108-
let mut i = 0;
109-
while i < 7 {
106+
for _ in 0..7 {
110107
// find the circle that is further away from the point uv, using euclidean distance
111108
let mut fi: usize = 0;
112109
let mut d: f32 = uv.distance(dec[0].xy()) - (1.0 / dec[0].z).abs();
113110
// for some reason, the euclidean distance doesn't work for the circle with negative bend
114111
// can anyone with proper math skills, explain me why?
115112
d *= if dec[0].z < 0.0 { -0.5 } else { 1.0 }; // just scale it to make it work...
116-
let mut j = 1;
117-
while j < 4 {
113+
for j in 1..4 {
118114
let mut fd: f32 = uv.distance(dec[j].xy()) - (1. / dec[j].z).abs();
119115
fd *= if dec[j].z < 0.0 { -0.5 } else { 1.0 };
120116
if fd > d {
121117
fi = j;
122118
d = fd;
123119
}
124-
j += 1;
125120
}
126121
// put the cicle found in the last slot, to generate a solution
127122
// in the "direction" of the point
@@ -147,7 +142,6 @@ fn apollonian(uv: Vec2) -> Vec3 {
147142
// else update the descartes configuration,
148143
dec[3] = solution;
149144
// and repeat...
150-
i += 1;
151145
}
152146
// if nothing is found we return by default the inner circle of the Steiner chain
153147
uv.extend(rb)

shaders/src/apollonian.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ impl State {
5151
fn map(&mut self, mut p: Vec3, s: f32) -> f32 {
5252
let mut scale: f32 = 1.0;
5353
self.orb = Vec4::splat(1000.0);
54-
let mut i = 0;
55-
while i < 8 {
54+
for _ in 0..8 {
5655
p = Vec3::splat(-1.0) + 2.0 * (0.5 * p + Vec3::splat(0.5)).gl_fract();
5756

5857
let r2: f32 = p.dot(p);
@@ -62,7 +61,6 @@ impl State {
6261
let k: f32 = s / r2;
6362
p *= k;
6463
scale *= k;
65-
i += 1;
6664
}
6765
0.25 * p.y.abs() / scale
6866
}
@@ -71,16 +69,14 @@ impl State {
7169
let maxd = 30.0;
7270
let mut t: f32 = 0.01;
7371

74-
let mut i = 0;
75-
while i < 512 {
72+
for _ in 0..512 {
7673
let precis = 0.001 * t;
7774

7875
let h: f32 = self.map(ro + rd * t, s);
7976
if h < precis || t > maxd {
8077
break;
8178
}
8279
t += h;
83-
i += 1;
8480
}
8581
if t > maxd {
8682
t = -1.0;
@@ -141,10 +137,8 @@ impl State {
141137
let anim: f32 = 1.1 + 0.5 * smoothstep(-0.3, 0.3, (0.1 * self.inputs.time).cos());
142138
let mut tot: Vec3 = Vec3::ZERO;
143139

144-
let mut jj = 0;
145-
while jj < AA {
146-
let mut ii = 0;
147-
while ii < AA {
140+
for jj in 0..AA {
141+
for ii in 0..AA {
148142
let q: Vec2 = frag_coord + vec2(ii as f32, jj as f32) / AA as f32;
149143
let p: Vec2 = (2.0 * q - self.inputs.resolution.xy()) / self.inputs.resolution.y;
150144

@@ -167,9 +161,7 @@ impl State {
167161
let rd: Vec3 = (p.x * cu + p.y * cv + 2.0 * cw).normalize();
168162

169163
tot += self.render(ro, rd, anim);
170-
ii += 1;
171164
}
172-
jj += 1;
173165
}
174166

175167
tot = tot / (AA * AA) as f32;

shaders/src/atmosphere_system_test.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ fn get_sun_light(ray: Ray, optical_depth_r: &mut f32, optical_depth_m: &mut f32)
149149
let mut march_pos: f32 = 0.0;
150150
let march_step: f32 = t1 / NUM_SAMPLES_LIGHT as f32;
151151

152-
let mut i = 0;
153-
while i < NUM_SAMPLES_LIGHT {
152+
for _ in 0..NUM_SAMPLES_LIGHT {
154153
let s: Vec3 = ray.origin + ray.direction * (march_pos + 0.5 * march_step);
155154
let height: f32 = s.length() - EARTH_RADIUS;
156155
if height < 0.0 {
@@ -161,7 +160,6 @@ fn get_sun_light(ray: Ray, optical_depth_r: &mut f32, optical_depth_m: &mut f32)
161160
*optical_depth_m += (-height / H_M).exp() * march_step;
162161

163162
march_pos += march_step;
164-
i += 1;
165163
}
166164
true
167165
}
@@ -203,8 +201,7 @@ impl State {
203201
let mut sum_m: Vec3 = Vec3::ZERO;
204202
let mut march_pos: f32 = 0.0;
205203

206-
let mut i = 0;
207-
while i < NUM_SAMPLES {
204+
for _ in 0..NUM_SAMPLES {
208205
let s: Vec3 = ray.origin + ray.direction * (march_pos + 0.5 * march_step);
209206
let height: f32 = s.length() - EARTH_RADIUS;
210207

@@ -237,7 +234,6 @@ impl State {
237234
}
238235

239236
march_pos += march_step;
240-
i += 1;
241237
}
242238

243239
SUN_POWER * (sum_r * phase_r * BETA_R + sum_m * phase_m * BETA_M)

shaders/src/bubble_buckey_balls.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ impl<C0, C1> State<C0, C1> {
243243
let mut dist: f32 = 10.0 * epsilon;
244244
let mut t: f32 = 0.0;
245245
let mut material: f32 = 0.0;
246-
let mut i = 0;
247-
while i < DISTMARCH_STEPS {
246+
for _ in 0..DISTMARCH_STEPS {
248247
if dist.abs() < epsilon || t > maxd {
249248
break;
250249
}
@@ -253,7 +252,6 @@ impl<C0, C1> State<C0, C1> {
253252
let dfresult: Vec2 = self.scenedf(ro + t * rd);
254253
dist = dfresult.x;
255254
material = dfresult.y;
256-
i += 1;
257255
}
258256

259257
if t > maxd {
@@ -274,14 +272,12 @@ impl<C0, C1> State<C0, C1> {
274272
let mut shadow: f32 = 1.0;
275273
let mut t: f32 = mint;
276274

277-
let mut i = 0;
278-
while i < SOFTSHADOW_STEPS {
275+
for _ in 0..SOFTSHADOW_STEPS {
279276
if t < maxt {
280277
let h: f32 = self.scenedf(ro + rd * t).x;
281278
shadow = shadow.min(k * h / t);
282279
t += SOFTSHADOW_STEPSIZE;
283280
}
284-
i += 1;
285281
}
286282
shadow.clamp(0.0, 1.0)
287283
}
@@ -296,15 +292,13 @@ impl<C0, C1> State<C0, C1> {
296292
let mut ao: f32 = 0.0;
297293
let mut aoscale: f32 = 1.0;
298294

299-
let mut aoi = 0;
300-
while aoi < AO_NUMSAMPLES {
295+
for aoi in 0..AO_NUMSAMPLES {
301296
let step: f32 = 0.01 + AO_STEPSIZE * aoi as f32;
302297
let aop: Vec3 = n * step + p;
303298

304299
let d: f32 = self.scenedf(aop).x;
305300
ao += -(d - step) * aoscale;
306301
aoscale *= AO_STEPSCALE;
307-
aoi += 1;
308302
}
309303

310304
ao.clamp(0.0, 1.0)

shaders/src/clouds.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
use shared::*;
44
use spirv_std::glam::{mat2, vec2, vec3, Mat2, Vec2, Vec3, Vec3Swizzles, Vec4};
55

6-
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
7-
// we tie #[no_std] above to the same condition, so it's fine.
8-
#[cfg(target_arch = "spirv")]
9-
use spirv_std::num_traits::Float;
10-
116
pub struct Inputs {
127
pub resolution: Vec3,
138
pub time: f32,
@@ -55,13 +50,11 @@ fn noise(p: Vec2) -> f32 {
5550
fn fbm(mut n: Vec2) -> f32 {
5651
let mut total: f32 = 0.0;
5752
let mut amplitude: f32 = 0.1;
58-
let mut i = 0;
59-
while i < 7 {
53+
for _ in 0..7 {
6054
total += noise(n) * amplitude;
6155
let m = M;
6256
n = m.transpose() * n;
6357
amplitude *= 0.4;
64-
i += 1;
6558
}
6659
total
6760
}
@@ -80,13 +73,11 @@ impl Inputs {
8073
uv *= CLOUD_SCALE;
8174
uv -= Vec2::splat(q - time);
8275
let mut weight: f32 = 0.8;
83-
let mut i = 0;
84-
while i < 8 {
76+
for _ in 0..8 {
8577
r += (weight * noise(uv)).abs();
8678
let m = M;
8779
uv = m.transpose() * uv + Vec2::splat(time);
8880
weight *= 0.7;
89-
i += 1;
9081
}
9182

9283
//noise shape
@@ -95,13 +86,11 @@ impl Inputs {
9586
uv *= CLOUD_SCALE;
9687
uv -= Vec2::splat(q - time);
9788
weight = 0.7;
98-
let mut i = 0;
99-
while i < 8 {
89+
for _ in 0..8 {
10090
f += weight * noise(uv);
10191
let m = M;
10292
uv = m.transpose() * uv + Vec2::splat(time);
10393
weight *= 0.6;
104-
i += 1;
10594
}
10695

10796
f *= r + f;
@@ -113,13 +102,11 @@ impl Inputs {
113102
uv *= CLOUD_SCALE * 2.0;
114103
uv -= Vec2::splat(q - time);
115104
weight = 0.4;
116-
let mut i = 0;
117-
while i < 7 {
105+
for _ in 0..7 {
118106
c += weight * noise(uv);
119107
let m = M;
120108
uv = m.transpose() * uv + Vec2::splat(time);
121109
weight *= 0.6;
122-
i += 1;
123110
}
124111

125112
//noise ridge colour
@@ -129,13 +116,11 @@ impl Inputs {
129116
uv *= CLOUD_SCALE * 3.0;
130117
uv -= Vec2::splat(q - time);
131118
weight = 0.4;
132-
let mut i = 0;
133-
while i < 7 {
119+
for _ in 0..7 {
134120
c1 += (weight * noise(uv)).abs();
135121
let m = M;
136122
uv = m.transpose() * uv + Vec2::splat(time);
137123
weight *= 0.6;
138-
i += 1;
139124
}
140125

141126
c += c1;

shaders/src/filtering_procedurals.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,8 @@ fn sample_texture_with_filter(
304304
let mut no: Vec3 = Vec3::ZERO;
305305

306306
if true {
307-
let mut j = 0;
308-
while j < MAX_SAMPLES {
309-
let mut i = 0;
310-
while i < MAX_SAMPLES {
307+
for j in 0..MAX_SAMPLES {
308+
for i in 0..MAX_SAMPLES {
311309
if j < sy && i < sx {
312310
let st: Vec2 = vec2(i as f32, j as f32) / vec2(sx as f32, sy as f32);
313311
no += mytexture(
@@ -316,24 +314,18 @@ fn sample_texture_with_filter(
316314
mid,
317315
);
318316
}
319-
i += 1;
320317
}
321-
j += 1;
322318
}
323319
} else {
324-
let mut j = 0;
325-
while j < sy {
326-
let mut i = 0;
327-
while i < sx {
320+
for j in 0..sy {
321+
for i in 0..sx {
328322
let st: Vec2 = vec2(i as f32, j as f32) / vec2(sx as f32, sy as f32);
329323
no += mytexture(
330324
uvw + st.x * (ddx_uvw - uvw) + st.y * (ddy_uvw - uvw),
331325
nor,
332326
mid,
333327
);
334-
i += 1;
335328
}
336-
j += 1;
337329
}
338330
}
339331

0 commit comments

Comments
 (0)