molgfx-render 0.2.1

The render graph, passes and the engine that drives a frame.
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
467
468
469
470
//! Building one shared surface field and the resources it owns.
//!
//! A field key resolves to these resources; the cache that holds them lives in
//! [`super::surface_cache`]. Splitting them keeps the identity question (what
//! makes two surfaces the same field) apart from the allocation question (what
//! one field owns and how its bind groups are described).

use super::record_cache::RecordGeometry;
use super::surface_components::{SurfaceComponentSync, SurfaceComponents};
use super::uniforms::RepresentationUniforms;
use crate::error::RenderError;
use molgfx_core::{Representation, SurfaceComponentPolicy, SurfaceComponentThreshold, SurfaceKind};
use molgfx_gpu::{
    BindGroupDesc, BindGroupEntry, BufferDesc, BufferUsage, Device, Queue as _, TextureDesc,
    TextureDimension, TextureFormat, TextureUsage, TextureViewDesc,
};

/// The sampling policy a field is generated under.
///
/// These are the scalar inputs the generation and erosion dispatches read, in
/// the same order and bit form the key stores them, so a field and its key can
/// never disagree about what was sampled.
#[derive(Clone, Copy, Debug)]
pub(crate) struct FieldSampling {
    pub(crate) probe: f32,
    pub(crate) sigma: f32,
    pub(crate) radius_scale: f32,
    pub(crate) isolevel: f32,
    pub(crate) grid_min: [f32; 3],
    pub(crate) grid_cell: f32,
    pub(crate) grid_size: [u32; 4],
}

/// Everything that forces a field to be generated, and nothing else.
///
/// The record key stands in for the sampled geometry: it carries the asset
/// identity, which fingerprints the coordinates, element, residue and radius
/// columns, so a coordinate or topology change is a different field while a
/// colour or opacity change is not.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub(crate) struct SurfaceFieldKey {
    pub(crate) geometry: RecordGeometry,
    /// Which physically distinct boundary this field is.
    pub(crate) kind: u32,
    /// Probe radius, Gaussian support, radius scale and iso-level, as bits.
    pub(crate) probe: u32,
    pub(crate) sigma: u32,
    pub(crate) radius_scale: u32,
    pub(crate) isolevel: u32,
    /// Local-space grid origin and spacing.
    pub(crate) grid_min: [u32; 3],
    pub(crate) grid_cell: u32,
    /// Sampled grid dimensions.
    pub(crate) grid_size: [u32; 4],
    /// Connected-component policy: threshold tag, payload and ceiling.
    pub(crate) components: [u64; 3],
}

impl SurfaceFieldKey {
    /// Builds the key for one representation's field.
    #[must_use]
    pub(crate) fn new(
        representation: &Representation,
        geometry: RecordGeometry,
        sampling: FieldSampling,
    ) -> Self {
        let FieldSampling {
            probe,
            sigma,
            radius_scale,
            isolevel,
            grid_min,
            grid_cell,
            grid_size,
        } = sampling;
        Self {
            geometry,
            kind: representation.params.surface_kind as u32,
            probe: probe.to_bits(),
            sigma: sigma.to_bits(),
            radius_scale: radius_scale.to_bits(),
            isolevel: isolevel.to_bits(),
            grid_min: grid_min.map(f32::to_bits),
            grid_cell: grid_cell.to_bits(),
            grid_size,
            components: component_key(representation.params.surface_components),
        }
    }

    /// Whether this boundary needs the reentrant rolling-probe erosion pass.
    ///
    /// Only the solvent-excluded surface has reentrant probe-contact patches;
    /// eroding any other field would report a boundary nobody asked for.
    #[must_use]
    pub(crate) const fn erodes(self) -> bool {
        self.kind == SurfaceKind::SolventExcluded as u32
    }

    /// Whether this field is generated from a Gaussian density rather than a
    /// union of atom spheres.
    #[must_use]
    pub(crate) const fn gaussian(self) -> bool {
        self.kind == SurfaceKind::Gaussian as u32
    }

    /// Whether the component filter runs over this field.
    #[must_use]
    pub(crate) const fn filters(self) -> bool {
        self.components[0] != 0
    }
}

/// The connected-component policy as comparable words.
fn component_key(policy: SurfaceComponentPolicy) -> [u64; 3] {
    let (tag, payload) = match policy.threshold() {
        SurfaceComponentThreshold::Disabled => (0, 0),
        SurfaceComponentThreshold::Area(area) => (1, area.to_bits()),
        SurfaceComponentThreshold::Volume(volume) => (2, volume.to_bits()),
        SurfaceComponentThreshold::Voxels(voxels) => (3, voxels),
    };
    [
        tag,
        payload,
        policy.maximum_components().map_or(0, u64::from),
    ]
}

/// One 3-D field texture and its bindable view.
#[derive(Debug)]
pub(super) struct FieldTexture<D: Device> {
    _texture: D::Texture,
    pub(super) view: D::TextureView,
}

impl<D: Device> FieldTexture<D> {
    pub(super) fn create(
        device: &D,
        dimensions: [u32; 3],
        label: &'static str,
        format: TextureFormat,
    ) -> Result<Self, RenderError> {
        let texture = device.create_texture(&TextureDesc {
            label,
            width: dimensions[0],
            height: dimensions[1],
            depth: dimensions[2],
            dimension: TextureDimension::D3,
            format,
            usage: TextureUsage::STORAGE_BINDING.union(TextureUsage::TEXTURE_BINDING),
        })?;
        let view = device.create_texture_view(&texture, &TextureViewDesc {});
        Ok(Self {
            _texture: texture,
            view,
        })
    }
}

/// Every GPU resource one field key owns, and the bind groups over them.
///
/// Held here rather than in the slot so that the field, its normals and its
/// component working set are generated once and read by every surface that
/// shares the key.
#[derive(Debug)]
pub(super) struct SharedField<D: Device> {
    /// Probe-inflated distance field: the generation input, and the shading
    /// field for boundaries that neither erode nor filter.
    pub(super) inflated: FieldTexture<D>,
    /// Eroded solvent-excluded field, for boundaries that erode.
    pub(super) field: Option<FieldTexture<D>>,
    /// Compact normals of whichever field shading reads.
    pub(super) normals: FieldTexture<D>,
    /// Connected-component working set, for policies that filter.
    pub(super) components: Option<SurfaceComponents<D>>,
    /// Generation output, erosion pair, normals pair and generation input.
    pub(super) output: D::BindGroup,
    pub(super) erosion: Option<D::BindGroup>,
    pub(super) normal_output: D::BindGroup,
    pub(super) input: D::BindGroup,
    /// The field's own copy of the representation uniforms.
    ///
    /// The field pass reads the boundary kind, probe, grid and radius scale out
    /// of this block. Those are exactly the values the key covers, so every
    /// surface sharing the key would write identical contents; owning the block
    /// keeps the field independent of any one slot's buffer.
    pub(super) _uniforms: D::Buffer,
    /// Rolling-probe offsets, sized and filled for this key's probe radius.
    pub(super) _erosion_offsets: Option<D::Buffer>,
    pub(super) erosion_offsets_size: u64,
    pub(super) dimensions: [u32; 3],
    /// Whether this field is generated from a Gaussian density.
    pub(super) gaussian: bool,
    /// Whether the field still owes its generation dispatch.
    ///
    /// Set once when the key is created and cleared by the record pass, so a
    /// field shared by four surfaces is generated once, not four times.
    pub(super) pending: bool,
}

impl<D: Device> SharedField<D> {
    /// The field shading reads: filtered, eroded or inflated, in that order.
    #[must_use]
    pub(super) fn shading_field(&self) -> &D::TextureView {
        if let Some(filtered) = self.components.as_ref().and_then(SurfaceComponents::field) {
            return filtered;
        }
        match &self.field {
            Some(field) => &field.view,
            None => &self.inflated.view,
        }
    }

    /// Device bytes this field holds.
    #[must_use]
    pub(super) fn resident_bytes(&self) -> u64 {
        let cells = self
            .dimensions
            .iter()
            .copied()
            .fold(1u64, |each, axis| each.saturating_mul(u64::from(axis)));
        // One R32Float field always, a second when the boundary erodes, one
        // Rgba8Snorm for the normals, and one more when the component filter
        // retains a working set.
        let fields = 1 + u64::from(self.field.is_some());
        let filtered = u64::from(self.components.is_some());
        cells
            .saturating_mul(4)
            .saturating_mul(fields + 1 + filtered)
            .saturating_add(self.erosion_offsets_size)
            .saturating_add(std::mem::size_of::<RepresentationUniforms>() as u64)
    }
}

/// Inputs one field needs from the scene.
pub(super) struct FieldSync<'a, D: Device> {
    pub(super) device: &'a D,
    pub(super) queue: &'a D::Queue,
    pub(super) output_layout: &'a D::BindGroupLayout,
    pub(super) input_layout: &'a D::BindGroupLayout,
    pub(super) erosion_layout: &'a D::BindGroupLayout,
    pub(super) normal_layout: &'a D::BindGroupLayout,
    pub(super) component_layout: &'a D::BindGroupLayout,
    pub(super) key: SurfaceFieldKey,
    /// The policy the key encodes, for the component uniforms.
    pub(super) policy: SurfaceComponentPolicy,
    pub(super) dimensions: [u32; 3],
    pub(super) atoms: &'a D::Buffer,
    /// The placement whose coordinate and BVH slices the field samples.
    pub(super) structure: &'a super::structure::GpuStructure<D>,
    pub(super) asset_arena: &'a super::asset_arena::AssetArena<D>,
    pub(super) compaction: &'a D::Buffer,
    pub(super) uniforms: &'a RepresentationUniforms,
}

/// Allocates one field's textures, bind groups and component working set.
pub(super) fn build_field<D: Device>(
    sync: &FieldSync<'_, D>,
) -> Result<SharedField<D>, RenderError> {
    let dimensions = sync.dimensions;
    let inflated = FieldTexture::create(
        sync.device,
        dimensions,
        "probe-inflated surface field",
        TextureFormat::R32Float,
    )?;
    let field = if sync.key.erodes() {
        Some(FieldTexture::create(
            sync.device,
            dimensions,
            "solvent-excluded surface field",
            TextureFormat::R32Float,
        )?)
    } else {
        None
    };
    let normals = FieldTexture::create(
        sync.device,
        dimensions,
        "continuous surface normals",
        TextureFormat::Rgba8Snorm,
    )?;
    let (erosion_offsets, erosion_offsets_size) = match &field {
        Some(_) => {
            let (buffer, size) = probe_buffer(sync)?;
            (Some(buffer), size)
        }
        None => (None, 0),
    };
    let uniforms = sync.device.create_buffer(&BufferDesc {
        label: "surface field parameters",
        size: std::mem::size_of::<RepresentationUniforms>() as u64,
        usage: BufferUsage::UNIFORM.union(BufferUsage::COPY_DST),
    })?;
    sync.queue
        .write_buffer(&uniforms, 0, bytemuck::bytes_of(sync.uniforms));
    let components = if sync.key.filters() {
        let mut components = SurfaceComponents::new();
        components.sync(&SurfaceComponentSync {
            device: sync.device,
            queue: sync.queue,
            layout: sync.component_layout,
            source: &shading_field(field.as_ref(), &inflated).view,
            dimensions,
            cell: f32::from_bits(sync.key.grid_cell),
            isolevel: f32::from_bits(sync.key.isolevel),
            gaussian: sync.key.gaussian(),
            policy: sync.policy,
        })?;
        Some(components)
    } else {
        None
    };
    let groups = FieldGroups::create(
        sync,
        &inflated,
        field.as_ref(),
        &normals,
        &uniforms,
        erosion_offsets.as_ref(),
    );
    Ok(SharedField {
        inflated,
        field,
        normals,
        components,
        output: groups.output,
        erosion: groups.erosion,
        normal_output: groups.normal_output,
        input: groups.input,
        _uniforms: uniforms,
        _erosion_offsets: erosion_offsets,
        erosion_offsets_size,
        dimensions,
        gaussian: sync.key.gaussian(),
        pending: true,
    })
}

/// Every bind group one field needs, built together.
struct FieldGroups<D: Device> {
    output: D::BindGroup,
    erosion: Option<D::BindGroup>,
    normal_output: D::BindGroup,
    input: D::BindGroup,
}

impl<D: Device> FieldGroups<D> {
    /// Builds the groups over one field's textures and buffers.
    ///
    /// Kept apart from the allocation so a field's textures and the groups that
    /// read them are each described in one place, and so the generation input
    /// and the erosion pair can be read without the allocation around them.
    fn create(
        sync: &FieldSync<'_, D>,
        inflated: &FieldTexture<D>,
        field: Option<&FieldTexture<D>>,
        normals: &FieldTexture<D>,
        uniforms: &D::Buffer,
        erosion_offsets: Option<&D::Buffer>,
    ) -> Self {
        let shading = match field {
            Some(field) => field,
            None => inflated,
        };
        let output = sync.device.create_bind_group(&BindGroupDesc {
            label: "surface field output",
            layout: sync.output_layout,
            entries: &[BindGroupEntry::Texture {
                binding: 0,
                view: &inflated.view,
            }],
        });
        let input = sync.device.create_bind_group(&BindGroupDesc {
            label: "surface field input",
            layout: sync.input_layout,
            entries: &[
                BindGroupEntry::Buffer {
                    binding: 0,
                    buffer: sync.atoms,
                },
                sync.structure.coords_entry(sync.asset_arena, 1),
                sync.structure.bvh_nodes_entry(sync.asset_arena, 6),
                sync.structure.bvh_indices_entry(sync.asset_arena, 7),
                BindGroupEntry::Buffer {
                    binding: 8,
                    buffer: sync.compaction,
                },
                BindGroupEntry::Buffer {
                    binding: 9,
                    buffer: uniforms,
                },
            ],
        });
        let erosion = erosion_offsets.map(|offsets| {
            sync.device.create_bind_group(&BindGroupDesc {
                label: "surface field erosion",
                layout: sync.erosion_layout,
                entries: &[
                    BindGroupEntry::Texture {
                        binding: 0,
                        view: &inflated.view,
                    },
                    BindGroupEntry::Texture {
                        binding: 1,
                        view: &shading.view,
                    },
                    BindGroupEntry::Buffer {
                        binding: 2,
                        buffer: offsets,
                    },
                ],
            })
        });
        let normal_output = sync.device.create_bind_group(&BindGroupDesc {
            label: "surface field normals",
            layout: sync.normal_layout,
            entries: &[
                BindGroupEntry::Texture {
                    binding: 0,
                    view: &shading.view,
                },
                BindGroupEntry::Texture {
                    binding: 1,
                    view: &normals.view,
                },
            ],
        });
        Self {
            output,
            erosion,
            normal_output,
            input,
        }
    }
}

/// The field a shading pass reads, given an optional eroded field.
fn shading_field<'a, D: Device>(
    field: Option<&'a FieldTexture<D>>,
    inflated: &'a FieldTexture<D>,
) -> &'a FieldTexture<D> {
    match field {
        Some(field) => field,
        None => inflated,
    }
}

/// The rolling-probe offsets buffer for one field, filled on creation.
///
/// The offsets are the probe sample directions scaled by the probe radius, so
/// they belong to the key: a different probe is a different buffer rather than
/// an in-place rewrite a resident field would not see.
fn probe_buffer<D: Device>(sync: &FieldSync<'_, D>) -> Result<(D::Buffer, u64), RenderError> {
    let offsets = super::probe_offsets::probe_offsets(f32::from_bits(sync.key.probe));
    let bytes = bytemuck::cast_slice(&offsets);
    let size = u64::try_from(bytes.len())
        .map_err(|_| molgfx_gpu::GpuError::LimitExceeded {
            resource: "surface erosion probe table",
            limit: u64::from(u32::MAX),
        })?
        .next_power_of_two()
        .max(256);
    let buffer = sync.device.create_buffer(&BufferDesc {
        label: "surface erosion probe offsets",
        size,
        usage: BufferUsage::STORAGE.union(BufferUsage::COPY_DST),
    })?;
    sync.queue.write_buffer(&buffer, 0, bytes);
    Ok((buffer, size))
}