ambient_gpu 0.2.1

Ambient GPU functionality. Host-only.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use std::{
    borrow::Cow,
    collections::{btree_map, BTreeMap},
    sync::Arc,
};

use aho_corasick::AhoCorasick;
use ambient_std::{asset_cache::*, CowStr};
use anyhow::Context;
use itertools::Itertools;
use wgpu::{BindGroupLayout, BindGroupLayoutEntry, ComputePipelineDescriptor, DepthBiasState, TextureFormat};

use super::gpu::{Gpu, GpuKey, DEFAULT_SAMPLE_COUNT};

#[derive(Debug, Clone, PartialEq)]
pub enum WgslValue {
    String(CowStr),
    Raw(CowStr),
    Float(f32),
    Int32(u32),
    Int64(u64),
}

impl WgslValue {
    pub fn as_integer(&self) -> Option<u32> {
        match self {
            WgslValue::Int32(v) => Some(*v),
            _ => None,
        }
    }

    fn to_wgsl(&self) -> String {
        match self {
            WgslValue::String(v) => format!("{v:?}"),
            WgslValue::Raw(v) => v.to_string(),
            WgslValue::Float(v) => v.to_string(),
            WgslValue::Int32(v) => v.to_string(),
            WgslValue::Int64(v) => v.to_string(),
        }
    }
}

impl From<&'static str> for WgslValue {
    fn from(v: &'static str) -> Self {
        Self::String(v.into())
    }
}
impl From<String> for WgslValue {
    fn from(v: String) -> Self {
        Self::String(v.into())
    }
}

impl From<f32> for WgslValue {
    fn from(v: f32) -> Self {
        Self::Float(v)
    }
}

impl From<u32> for WgslValue {
    fn from(v: u32) -> Self {
        Self::Int32(v)
    }
}

impl From<u64> for WgslValue {
    fn from(v: u64) -> Self {
        Self::Int64(v)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ShaderIdent {
    name: CowStr,
    value: WgslValue,
}

impl ShaderIdent {
    /// Shortcut for unescaped text replacement
    pub fn raw(name: impl Into<CowStr>, value: impl Into<CowStr>) -> Self {
        Self { name: name.into(), value: WgslValue::Raw(value.into()) }
    }

    /// Replaces any occurence of `name` with the wgsl representation of `value`
    pub fn constant(name: impl Into<CowStr>, value: impl Into<WgslValue>) -> Self {
        Self { name: name.into(), value: value.into() }
    }
}

type BindingEntry = (CowStr, BindGroupLayoutEntry);

/// Defines a part of a shader, with preprocessing.
///
/// Each shadermodule contains:
/// - Source code
/// - Dependencies
/// - Identifier, used for preprocessing and replacing, such as constants
/// - A list of binding entries for generating the complete pipeline layout when the shader is assembled.
///     The bindings *do not* describe complete binding groups, as they may be spread out over several shader modules.
///
///     As such, it is not possible to get the bind group layout from a single shader module. Prefer to split out and reuse the entries in a separate function
#[derive(Debug, Default)]
pub struct ShaderModule {
    /// The unique name of the shadermodule.
    pub name: CowStr,
    /// The wgsl source for the module, *without* dependencies
    pub source: CowStr,

    /// Dependencies for the module
    pub dependencies: Vec<Arc<ShaderModule>>,

    // Use the label to preprocess constants
    pub idents: Vec<ShaderIdent>,
    bindings: Vec<BindingEntry>,
}

impl ShaderModule {
    pub fn new(name: impl Into<CowStr>, source: impl Into<CowStr>) -> Self {
        Self {
            name: name.into(),
            source: source.into(),
            idents: Default::default(),
            bindings: Default::default(),
            dependencies: Default::default(),
        }
    }

    pub fn with_ident(mut self, ident: ShaderIdent) -> Self {
        self.idents.push(ident);
        self
    }

    pub fn with_binding(mut self, group: impl Into<CowStr>, entry: BindGroupLayoutEntry) -> Self {
        self.bindings.push((group.into(), entry));
        self
    }

    pub fn with_bindings(mut self, bindings: impl IntoIterator<Item = (CowStr, BindGroupLayoutEntry)>) -> Self {
        self.bindings.extend(bindings.into_iter());
        self
    }

    pub fn with_binding_desc(mut self, desc: BindGroupDesc<'static>) -> Self {
        let group = desc.label.clone();
        self.bindings.extend(desc.entries.iter().map(|&entry| (group.clone(), entry)));
        self
    }

    pub fn with_dependency(mut self, module: Arc<ShaderModule>) -> Self {
        self.dependencies.push(module);
        self
    }

    pub fn with_dependencies(mut self, modules: impl IntoIterator<Item = Arc<ShaderModule>>) -> Self {
        self.dependencies.extend(modules);
        self
    }

    fn sanitized_label(&self) -> String {
        self.name.replace(|v: char| !v.is_ascii_alphanumeric() && !"_-.".contains(v), "?")
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct BindGroupDesc<'a> {
    pub entries: Vec<wgpu::BindGroupLayoutEntry>,
    // Name for group preprocessor
    pub label: Cow<'a, str>,
}

impl<'a> SyncAssetKey<Arc<wgpu::BindGroupLayout>> for BindGroupDesc<'a> {
    fn load(&self, assets: AssetCache) -> Arc<wgpu::BindGroupLayout> {
        let gpu = GpuKey.get(&assets);

        let layout =
            gpu.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some(&*self.label), entries: &self.entries });

        Arc::new(layout)
    }
}

/// Returns all shader modules in the dependency graph in topological order
///
/// # Panics
///
/// If the dependency graph contains a cycle
fn resolve_module_graph<'a>(roots: impl IntoIterator<Item = &'a ShaderModule>) -> Vec<&'a ShaderModule> {
    enum VisitedState {
        Pending,
        Visited,
    }

    let mut visited = BTreeMap::new();

    fn visit<'a>(
        visited: &mut BTreeMap<&'a str, VisitedState>,
        result: &mut Vec<&'a ShaderModule>,
        module: &'a ShaderModule,
        backtrace: &[&str],
    ) {
        match visited.entry(&module.name) {
            btree_map::Entry::Vacant(slot) => {
                slot.insert(VisitedState::Pending);
            }
            btree_map::Entry::Occupied(slot) => match slot.get() {
                VisitedState::Pending => panic!("Circular dependency for module: {:?} in {:?}", module.name, backtrace),
                VisitedState::Visited => return,
            },
        }

        let backtrace = backtrace.iter().copied().chain([&*module.name]).collect_vec();

        // Ensure dependencies are satisfied first
        for module in &module.dependencies {
            visit(visited, result, module, &backtrace)
        }

        visited.insert(&module.name, VisitedState::Visited);

        result.push(module);
    }

    let mut result = Vec::new();
    for root in roots {
        visit(&mut visited, &mut result, root, &[]);
    }

    result
}

/// Represents a shader and its layout
pub struct Shader {
    module: wgpu::ShaderModule,
    // Ordered sets
    bind_group_layouts: Vec<Arc<wgpu::BindGroupLayout>>,
    label: CowStr,
}

impl std::ops::Deref for Shader {
    type Target = wgpu::ShaderModule;

    fn deref(&self) -> &Self::Target {
        &self.module
    }
}

impl Shader {
    pub fn new(
        assets: &AssetCache,
        label: impl Into<CowStr>,
        bind_group_names: &[&str],
        module: &ShaderModule,
    ) -> anyhow::Result<Arc<Self>> {
        let label = label.into();
        let gpu = GpuKey.get(assets);

        let _span = tracing::info_span!("Shader::from_modules", ?label).entered();

        // The complete dependency graph, in the correct order
        let modules = resolve_module_graph([module]);

        // Resolve all bind groups, resolving the names to an index
        let bind_group_index: BTreeMap<_, _> = bind_group_names.iter().enumerate().map(|(a, &b)| (b, a)).collect();
        let mut bind_groups =
            bind_group_names.iter().map(|group| BindGroupDesc { label: Cow::Borrowed(*group), entries: Default::default() }).collect_vec();

        for module in &modules {
            for (group, binding) in &module.bindings {
                let index =
                    *bind_group_index.get(&**group).with_context(|| format!("Failed to resolve bind group: {group} in {}", module.name))?;

                let desc = &mut bind_groups[index];
                desc.entries.push(*binding);
            }
        }

        // Now for the fun part: constructing the binding group layout descriptors
        let bind_group_layouts = bind_groups.iter().map(|desc| desc.get(assets)).collect_vec();
        if bind_group_layouts.len() > 4 {
            anyhow::bail!(
                "Maximum bind group layout count exceeded. Expected a maximum of 4, found {}: {bind_group_names:?}",
                bind_group_layouts.len()
            );
        }

        // Efficiently replace all identifiers
        let (patterns, replace_with): (Vec<_>, Vec<_>) = modules
            .iter()
            .flat_map(|v| v.idents.iter().map(|ShaderIdent { name, value }| (format!("{name}"), value.to_wgsl())))
            .chain(bind_group_index.iter().map(|(name, &index)| (name.to_string(), (index as u32).to_string())))
            .unzip();

        tracing::debug!(
            "Preprocessing shader using {}",
            patterns.iter().zip_eq(&replace_with).map(|(a, b)| { format!("{a} => {b}") }).format("\n")
        );

        // Collect the raw source code
        let source = {
            let source = modules
                .iter()
                .map(|module| {
                    let div = "--------------------------------";
                    let label = module.sanitized_label();
                    let source = &module.source;
                    format!("// {div}\n// @module: {label}\n// {div}\n{source}")
                })
                .join("\n\n");

            AhoCorasick::new(patterns).replace_all(&source, &replace_with)
        };

        #[cfg(all(not(target_os = "unknown"), debug_assertions))]
        {
            let path = format!("tmp/{label}.wgsl");
            std::fs::create_dir_all("tmp/").unwrap();
            std::fs::write(path, source.as_bytes()).unwrap();
        }

        let module = gpu
            .device
            .create_shader_module(wgpu::ShaderModuleDescriptor { label: Some(&label), source: wgpu::ShaderSource::Wgsl(source.into()) });

        Ok(Arc::new(Self { module, bind_group_layouts, label }))
    }

    #[inline]
    pub fn layouts(&self) -> &[Arc<BindGroupLayout>] {
        &self.bind_group_layouts
    }

    /// The wgpu shader module
    #[inline]
    pub fn module(&self) -> &wgpu::ShaderModule {
        &self.module
    }

    pub fn to_pipeline(self: &Arc<Self>, gpu: &Gpu, info: GraphicsPipelineInfo) -> GraphicsPipeline {
        let layout = gpu.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some(&self.label),
            bind_group_layouts: &self.layouts().iter().map(|v| &**v).collect_vec(),
            push_constant_ranges: &[],
        });

        let pipeline = gpu.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some(&self.label),
            layout: Some(&layout),
            vertex: wgpu::VertexState { module: self.module(), entry_point: info.vs_main, buffers: &[] },
            primitive: wgpu::PrimitiveState {
                front_face: info.front_face,
                cull_mode: info.cull_mode,
                topology: info.topology,
                ..Default::default()
            },
            fragment: Some(wgpu::FragmentState { module: self.module(), entry_point: info.fs_main, targets: info.targets }),
            depth_stencil: info.depth,
            multisample: wgpu::MultisampleState { count: DEFAULT_SAMPLE_COUNT, mask: !0, alpha_to_coverage_enabled: false },
            multiview: None,
        });

        GraphicsPipeline { pipeline, shader: self.clone() }
    }

    pub fn to_compute_pipeline(self: &Arc<Self>, gpu: &Gpu, entry_point: &str) -> ComputePipeline {
        let layout = gpu.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some(&self.label),
            bind_group_layouts: &self.layouts().iter().map(|v| &**v).collect_vec(),
            push_constant_ranges: &[],
        });

        let pipeline = gpu.device.create_compute_pipeline(&ComputePipelineDescriptor {
            label: Some(&self.label),
            layout: Some(&layout),
            module: self.module(),
            entry_point,
        });

        ComputePipeline { pipeline, shader: self.clone() }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct GraphicsPipelineInfo<'a> {
    pub vs_main: &'a str,
    pub fs_main: &'a str,
    pub depth: Option<wgpu::DepthStencilState>,
    pub targets: &'a [Option<wgpu::ColorTargetState>],
    pub front_face: wgpu::FrontFace,
    pub cull_mode: Option<wgpu::Face>,
    pub topology: wgpu::PrimitiveTopology,
}

impl<'a> Default for GraphicsPipelineInfo<'a> {
    fn default() -> Self {
        Self {
            vs_main: "vs_main",
            fs_main: "fs_main",
            depth: None,
            targets: &[],
            front_face: wgpu::FrontFace::Cw,
            cull_mode: None,
            topology: wgpu::PrimitiveTopology::TriangleList,
        }
    }
}

pub type GraphicsPipeline = Pipeline<wgpu::RenderPipeline>;
pub type ComputePipeline = Pipeline<wgpu::ComputePipeline>;

pub struct Pipeline<P> {
    pipeline: P,
    shader: Arc<Shader>,
}

impl<P> Pipeline<P> {
    /// Get a reference to the graphics pipeline's pipeline.
    pub fn pipeline(&self) -> &P {
        &self.pipeline
    }

    /// Get a reference to the pipeline's shader.
    #[must_use]
    pub fn shader(&self) -> &Shader {
        self.shader.as_ref()
    }
}

impl<P> std::ops::Deref for Pipeline<P> {
    type Target = Shader;

    fn deref(&self) -> &Self::Target {
        &self.shader
    }
}

#[cfg(not(target_os = "unknown"))]
pub const DEPTH_FORMAT: TextureFormat = TextureFormat::Depth32Float;
#[cfg(target_os = "unknown")]
// HACK: float depth are broken on wgpu:
// stencilLoadOp is (LoadOp::Load) and stencilStoreOp is (StoreOp::Store) when stencilReadOnly (0) or the attachment ([TextureView "Renderer.shadow_target_views" of Texture "Renderer.shadow_texture"]) has no stencil aspect.
// - While validating depthStencilAttachment.
// - While encoding [CommandEncoder].BeginRenderPass([RenderPassDescriptor "Shadow cascade 0"]).

// Adding a stencil part crashes the gpu
pub const DEPTH_FORMAT: TextureFormat = TextureFormat::Depth24PlusStencil8;

impl<'a> GraphicsPipelineInfo<'a> {
    pub fn with_depth(self) -> GraphicsPipelineInfo<'a> {
        Self {
            depth: Some(wgpu::DepthStencilState {
                format: DEPTH_FORMAT,
                depth_write_enabled: true,
                // This is Greater because we're using reverse-z NDC
                depth_compare: wgpu::CompareFunction::Greater,
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState::default(),
            }),
            ..self
        }
    }

    pub fn with_depth_bias(mut self, state: DepthBiasState) -> GraphicsPipelineInfo<'a> {
        self.depth.as_mut().expect("Attempt to set depth bias without a depth buffer").bias = state;
        self
    }
}