Skip to content

Commit ec68b14

Browse files
committed
Change priority of internal logging from kInfo to kTrace. Make julia the default shader for shadertui.
1 parent d8d618a commit ec68b14

File tree

2 files changed

+32
-54
lines changed

2 files changed

+32
-54
lines changed

examples/shadertui/shader.wgsl

+22-43
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,39 @@
22
@group(0) @binding(1) var<uniform> params: Params;
33

44
struct Params {
5-
time: f32,
6-
screenwidth: u32,
7-
screenheight: u32,
5+
time: f32,
6+
screenwidth: u32,
7+
screenheight: u32,
88
};
99

10-
struct Particle {
11-
position: vec2<f32>,
12-
velocity: vec2<f32>,
13-
life: f32,
14-
}
15-
16-
const NUM_PARTICLES: u32 = 1000u;
17-
const PARTICLE_LIFE: f32 = 9.0;
18-
const EMISSION_RATE: f32 = 300.0;
10+
const MAX_ITERATIONS: u32 = 100;
1911

20-
fn rand(n: f32) -> f32 {
21-
return fract(sin(n) * 43758.5453123);
22-
}
23-
24-
fn initialize_particle(id: f32, time: f32) -> Particle {
25-
let random1 = rand(id * 0.01 + time * 0.1);
26-
let random2 = rand(id * 0.02 + time * 0.1);
27-
let angle = random1 * 2.0 * 3.14159;
28-
let speed = 0.05 + random2 * 0.05;
29-
30-
return Particle(
31-
vec2<f32>(0.5, 0.5),
32-
vec2<f32>(cos(angle) * speed, sin(angle) * speed),
33-
PARTICLE_LIFE
34-
);
12+
fn complex_mul(a: vec2<f32>, b: vec2<f32>) -> vec2<f32> {
13+
return vec2<f32>(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
3514
}
3615

3716
@compute @workgroup_size(16, 16, 1)
3817
fn main(@builtin(global_invocation_id) globalID : vec3<u32>) {
3918
let resolution = vec2<f32>(f32(params.screenwidth), f32(params.screenheight));
40-
let uv = vec2<f32>(f32(globalID.x) / resolution.x, f32(globalID.y) / resolution.y);
41-
let idx = globalID.y * params.screenwidth + globalID.x;
19+
let uv = (vec2<f32>(globalID.xy) - 0.5 * resolution) / min(resolution.x, resolution.y);
20+
21+
// Animate the Julia set parameters
22+
let t = params.time * 0.3;
23+
let c = 0.7885 * vec2<f32>(cos(t), sin(t));
4224

43-
var color: f32 = 0.0;
25+
var z = uv * 3.0;
26+
var i: u32 = 0u;
4427

45-
for (var i: f32 = 0.0; i < f32(NUM_PARTICLES); i += 1.0) {
46-
let spawn_time = i / EMISSION_RATE;
47-
let particle_age = fract((params.time - spawn_time) / PARTICLE_LIFE) * PARTICLE_LIFE;
48-
49-
if (particle_age < PARTICLE_LIFE) {
50-
var particle = initialize_particle(i, spawn_time);
51-
particle.position += particle.velocity * particle_age;
52-
53-
let distance = length(uv - particle.position);
54-
if (distance < 0.005) {
55-
color += 0.5 * (1.0 - particle_age / PARTICLE_LIFE);
56-
}
28+
for (; i < MAX_ITERATIONS; i = i + 1u) {
29+
z = complex_mul(z, z) + c;
30+
if (dot(z, z) > 4.0) {
31+
break;
5732
}
5833
}
5934

60-
out[idx] = min(color, 1.0);
35+
let smooth_i = f32(i) + 1.0 - log2(log2(dot(z, z)));
36+
let color = 0.5 + 0.5 * cos(3.0 + smooth_i * 0.15 + vec3<f32>(0.0, 0.6, 1.0));
37+
38+
let idx = globalID.y * params.screenwidth + globalID.x;
39+
out[idx] = (color.r + color.g + color.b) / 3.0;
6140
}

gpu.hpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ struct Context {
530530
// Default constructor
531531
Context() = default;
532532

533-
// Move constructor: steals GPU handles so the source destructor won't free them.
534533
Context(Context&& other) noexcept
535534
: instance(other.instance),
536535
adapter(other.adapter),
@@ -542,6 +541,7 @@ struct Context {
542541
adapterStatus(other.adapterStatus),
543542
deviceStatus(other.deviceStatus)
544543
{
544+
LOG(kDefLog, kTrace, "Moving Context ownership");
545545
// Move over the resources in the pools:
546546
pool.data = std::move(other.pool.data);
547547
kernelPool.data = std::move(other.kernelPool.data);
@@ -555,7 +555,6 @@ struct Context {
555555
// other.deviceStatus = 0;
556556
}
557557

558-
// Optional move‐assignment operator, similarly stealing resources:
559558
Context& operator=(Context&& other) noexcept {
560559
if (this != &other) {
561560
// Free any existing resources. In most cases, this should be a no-op
@@ -573,26 +572,26 @@ struct Context {
573572
if (queue) {
574573
wgpuQueueRelease(queue);
575574
} else {
576-
LOG(kDefLog, kWarn, "Queue is null");
575+
LOG(kDefLog, kTrace, "Queue is null");
577576
}
578577
if (device) {
579578
wgpuDeviceRelease(device);
580579
processEvents(instance);
581580
} else {
582-
LOG(kDefLog, kWarn, "Device is null");
581+
LOG(kDefLog, kTrace, "Device is null");
583582
}
584583
if (adapter) {
585584
wgpuAdapterRelease(adapter);
586585
processEvents(instance);
587586
} else {
588-
LOG(kDefLog, kWarn, "Adapter is null");
587+
LOG(kDefLog, kTrace, "Adapter is null");
589588
}
590589
if (instance) {
591590
wgpuInstanceRelease(instance);
592591
} else {
593-
LOG(kDefLog, kWarn, "Instance is null");
592+
LOG(kDefLog, kTrace, "Instance is null");
594593
}
595-
LOG(kDefLog, kInfo, "Context destroyed");
594+
LOG(kDefLog, kTrace, "Context destroyed");
596595
}
597596
};
598597

@@ -827,7 +826,7 @@ inline Context createContext(
827826
#endif
828827
check(ctx.instance, "Initialize WebGPU", __FILE__, __LINE__);
829828

830-
LOG(kDefLog, kInfo, "Requesting adapter");
829+
LOG(kDefLog, kTrace, "Requesting adapter");
831830
{
832831
struct AdapterData {
833832
WGPUAdapter adapter = nullptr;
@@ -869,7 +868,7 @@ inline Context createContext(
869868
ctx.adapterStatus = adapterData.status;
870869
}
871870

872-
LOG(kDefLog, kInfo, "Requesting device");
871+
LOG(kDefLog, kTrace, "Requesting device");
873872
{
874873
struct DeviceData {
875874
WGPUDevice device = nullptr;
@@ -900,11 +899,11 @@ inline Context createContext(
900899
};
901900
wgpuAdapterRequestDevice(ctx.adapter, &devDescriptor, deviceCallbackInfo);
902901

903-
LOG(kDefLog, kInfo, "Waiting for device request to end");
902+
LOG(kDefLog, kTrace, "Waiting for device request to end");
904903
while (!devData.requestEnded) {
905904
processEvents(ctx.instance);
906905
}
907-
LOG(kDefLog, kInfo, "Device request ended");
906+
LOG(kDefLog, kTrace, "Device request ended");
908907

909908
ctx.device = devData.device;
910909
ctx.deviceStatus = devData.status;

0 commit comments

Comments
 (0)