Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions native/shared/shaders/visibility_buffer/geometry.wgsl
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// Bloom shared-geometry storage ABI for visibility shading — version 1.
// Bloom shared-geometry storage ABI for visibility shading — version 2.
//
// Vertex3D is a tightly packed 96-byte Rust/vertex-buffer record. WGSL
// storage structs give vec3 values 16-byte alignment, so spelling the record
// with native vec3 fields would silently read the wrong offsets. Six vec4<u32>
// lanes preserve the exact 24-word byte layout and are decoded explicitly.
// The shared static-geometry arena holds unskinned meshes only, as the
// tightly packed 64-byte StaticVertex3D (renderer/vertex_formats.rs):
// position, normal, color, uv, tangent. WGSL storage structs give vec3 values
// 16-byte alignment, so spelling the record with native vec3 fields would
// silently read the wrong offsets. Four vec4<u32> lanes preserve the exact
// 16-word byte layout and are decoded explicitly. Joints and weights are not
// stored: they are zero for every arena vertex, as the 96-byte record held.

const BLOOM_VERTEX3D_WORDS: u32 = 24u;
const BLOOM_VERTEX3D_WORDS: u32 = 16u;

struct BloomPackedVertex3D {
words_0: vec4<u32>,
words_1: vec4<u32>,
words_2: vec4<u32>,
words_3: vec4<u32>,
words_4: vec4<u32>,
words_5: vec4<u32>,
};

struct BloomVertex3D {
Expand All @@ -36,9 +37,9 @@ fn bloom_decode_vertex3d(packed: BloomPackedVertex3D) -> BloomVertex3D {
)),
bitcast<vec4<f32>>(vec4<u32>(packed.words_1.zw, packed.words_2.xy)),
bitcast<vec2<f32>>(packed.words_2.zw),
vec4<f32>(0.0),
vec4<f32>(0.0),
bitcast<vec4<f32>>(packed.words_3),
bitcast<vec4<f32>>(packed.words_4),
bitcast<vec4<f32>>(packed.words_5),
);
}

Expand Down
4 changes: 4 additions & 0 deletions native/shared/src/models_vertex_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ impl VertexStore {
self.weights.get(i)
}

pub fn joint(&self, i: usize) -> [f32; 4] {
self.joints.get(i)
}

/// The sum of every normal, added in vertex order (as a loop over the
/// whole vertices would, so the result has the same bits).
pub fn normal_sum(&self) -> [f32; 3] {
Expand Down
15 changes: 13 additions & 2 deletions native/shared/src/renderer/card_capture_pass.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! The render pass and atlas copies of one frame's mesh-card captures.

use super::{Renderer, CARD_SLOTS_PER_ROW, CARD_SLOT_SIZE};
use super::{vertex_formats, Renderer, VertexLayout, CARD_SLOTS_PER_ROW, CARD_SLOT_SIZE};

/// One axis of one mesh: what to draw, the scratch-grid cell it draws into
/// and the atlas slot that cell is copied to.
pub(super) struct CardCaptureDraw {
pub uniform_offset: u32,
pub texture_bg: wgpu::BindGroup,
pub vertex_buffer: wgpu::Buffer,
pub vertex_layout: VertexLayout,
pub index_buffer: wgpu::Buffer,
pub index_count: u32,
pub cell_x: u32,
Expand Down Expand Up @@ -60,8 +61,12 @@ impl Renderer {
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(&self.card_capture_pipeline);
let mut current_layout = None;
for draw in draws {
if current_layout != Some(draw.vertex_layout) {
pass.set_pipeline(self.card_capture_pipeline.get(draw.vertex_layout));
current_layout = Some(draw.vertex_layout);
}
// The viewport maps the capture's clip space onto its cell;
// the scissor keeps geometry outside the card bounds from
// spilling into a neighbouring cell.
Expand All @@ -77,6 +82,12 @@ impl Renderer {
pass.set_bind_group(0, uniform_bg, &[draw.uniform_offset]);
pass.set_bind_group(1, &draw.texture_bg, &[]);
pass.set_vertex_buffer(0, draw.vertex_buffer.slice(..));
vertex_formats::bind_skin_zero(
&mut pass,
draw.vertex_layout,
1,
self.gpu_driven.skin_zero(),
);
pass.set_index_buffer(draw.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
pass.draw_indexed(0..draw.index_count, 0, 0..1);
}
Expand Down
31 changes: 21 additions & 10 deletions native/shared/src/renderer/geometry_arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
//! index buffer for every static mesh, with a free list. It grows by doubling
//! up to the device's buffer limit; past that an upload is refused and the
//! caller keeps the mesh on buffers of its own.
//!
//! It holds only unskinned meshes, so its vertices are the 64-byte
//! `StaticVertex3D` (`VertexLayout::arena()`; 96 bytes on OpenGL).

use super::super::{material_indirection::GpuCompletionTracker, Vertex3D};
use super::super::{material_indirection::GpuCompletionTracker, Vertex3D, VertexLayout};
use super::GeometrySlice;

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
Expand Down Expand Up @@ -34,17 +37,22 @@ pub(super) struct GeometryArena {
/// so the storage-binding limit applies as well as the buffer limit.
max_capacity: u64,
full_warned: bool,
/// The format of every vertex in the arena.
pub(super) layout: VertexLayout,
}

impl GeometryArena {
const INITIAL_CAPACITY: u64 = 64 * 1024;

pub(super) fn new(device: &wgpu::Device) -> Self {
let limits = device.limits();
let layout = VertexLayout::arena();
Self {
layout,
max_capacity: arena_max_capacity(
limits.max_buffer_size,
limits.max_storage_buffer_binding_size as u64,
layout.stride(),
),
full_warned: false,
vertex: create_vertex_arena(device, Self::INITIAL_CAPACITY),
Expand All @@ -69,7 +77,8 @@ impl GeometryArena {
indices: &[u32],
) -> Option<GeometrySlice> {
self.collect();
let vertex_size = std::mem::size_of_val(vertices) as u64;
let stride = self.layout.stride();
let vertex_size = vertices.len() as u64 * stride;
let index_size = std::mem::size_of_val(indices) as u64;
let previous_vertex_end = self.vertex_end;
let previous_index_end = self.index_end;
Expand Down Expand Up @@ -119,7 +128,7 @@ impl GeometryArena {
previous_index_end,
);
if vertex_size > 0 {
queue.write_buffer(&self.vertex, vertex_offset, bytemuck::cast_slice(vertices));
queue.write_buffer(&self.vertex, vertex_offset, &self.layout.encode(vertices));
}
if index_size > 0 {
queue.write_buffer(&self.index, index_offset, bytemuck::cast_slice(indices));
Expand All @@ -130,7 +139,7 @@ impl GeometryArena {
index_offset,
index_size,
first_index: (index_offset / 4) as u32,
base_vertex: (vertex_offset / std::mem::size_of::<Vertex3D>() as u64) as i32,
base_vertex: (vertex_offset / stride) as i32,
})
}

Expand Down Expand Up @@ -264,10 +273,10 @@ fn grown_capacity(current: u64, required: u64, max: u64) -> u64 {
}
}

/// Both limits, rounded down to whole vertices and indices.
fn arena_max_capacity(max_buffer_size: u64, max_storage_binding_size: u64) -> u64 {
/// Both limits, rounded down to whole vertices (of `stride` bytes) and indices.
fn arena_max_capacity(max_buffer_size: u64, max_storage_binding_size: u64, stride: u64) -> u64 {
let limit = max_buffer_size.min(max_storage_binding_size);
let unit = std::mem::size_of::<Vertex3D>() as u64 * 4;
let unit = stride * 4;
limit / unit * unit
}

Expand Down Expand Up @@ -360,9 +369,11 @@ mod tests {
assert_eq!(grown_capacity(32 * mb, 40 * mb, 1024 * mb), 64 * mb);
// 390 MB would round up to 448 MB; a 400 MB device gets 400 MB.
assert_eq!(grown_capacity(256 * mb, 390 * mb, 400 * mb), 400 * mb);
let unit = std::mem::size_of::<Vertex3D>() as u64 * 4;
assert_eq!(arena_max_capacity(1000 * mb, 300 * mb) % unit, 0);
assert!(arena_max_capacity(1000 * mb, 300 * mb) <= 300 * mb);
for stride in [64, 96] {
let unit = stride * 4;
assert_eq!(arena_max_capacity(1000 * mb, 300 * mb, stride) % unit, 0);
assert!(arena_max_capacity(1000 * mb, 300 * mb, stride) <= 300 * mb);
}
}

#[test]
Expand Down
46 changes: 41 additions & 5 deletions native/shared/src/renderer/gpu_driven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ pub struct GeometrySlice {
}

pub enum MeshGeometry {
/// A slice of the arena, in the arena's format (`arena_layout()`).
Shared(GeometrySlice),
Dedicated {
vertex: wgpu::Buffer,
index: wgpu::Buffer,
/// The format `vertex` holds.
layout: super::VertexLayout,
},
}

Expand Down Expand Up @@ -122,6 +125,8 @@ pub struct GpuDrivenRenderer {
main_visibility_compat_pipeline: Option<LazyRenderPipeline>,
main_visibility_compat_prepassed_pipeline: Option<LazyRenderPipeline>,
visibility: super::visibility_buffer::VisibilityBufferRuntime,
/// Zero joints/weights for 64-byte static draws (`vertex_formats.rs`).
skin_zero: wgpu::Buffer,
pub(super) draw_scratch: Vec<GpuDrawRecord>,
pub stats: SubmissionStats,
}
Expand Down Expand Up @@ -329,6 +334,7 @@ impl GpuDrivenRenderer {

let renderer = Self {
arena: GeometryArena::new(device),
skin_zero: super::vertex_formats::create_skin_zero_buffer(device),
enabled,
count_supported,
draw_capacity,
Expand Down Expand Up @@ -387,6 +393,16 @@ impl GpuDrivenRenderer {
(&self.arena.vertex, &self.arena.index)
}

/// The format of every vertex in the shared arena.
pub(crate) fn arena_layout(&self) -> super::VertexLayout {
self.arena.layout
}

/// The shared zero joints/weights that static draws bind.
pub(crate) fn skin_zero(&self) -> &wgpu::Buffer {
&self.skin_zero
}

pub fn upload_static(
&mut self,
device: &wgpu::Device,
Expand All @@ -410,9 +426,10 @@ impl GpuDrivenRenderer {
match self.arena.upload(device, queue, vertices, indices) {
Some(slice) => MeshGeometry::Shared(slice),
None => MeshGeometry::Dedicated {
layout: self.arena.layout,
vertex: device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("cached_static_model_vb"),
contents: bytemuck::cast_slice(vertices),
contents: &self.arena.layout.encode(vertices),
usage: wgpu::BufferUsages::VERTEX,
}),
index: device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
Expand Down Expand Up @@ -444,13 +461,19 @@ impl GpuDrivenRenderer {
first_index: slice.first_index,
index_count,
base_vertex: slice.base_vertex,
layout: self.arena.layout,
},
MeshGeometry::Dedicated { vertex, index } => MeshDrawRef {
MeshGeometry::Dedicated {
vertex,
index,
layout,
} => MeshDrawRef {
vertex,
index,
first_index: 0,
index_count,
base_vertex: 0,
layout: *layout,
},
}
}
Expand All @@ -472,17 +495,23 @@ impl GpuDrivenRenderer {
first_index: 0,
index_count,
base_vertex: 0,
layout: self.arena.layout,
},
slice.vertex_offset,
slice.index_offset,
),
MeshGeometry::Dedicated { vertex, index } => (
MeshGeometry::Dedicated {
vertex,
index,
layout,
} => (
MeshDrawRef {
vertex,
index,
first_index: 0,
index_count,
base_vertex: 0,
layout: *layout,
},
0,
0,
Expand Down Expand Up @@ -845,6 +874,7 @@ impl GpuDrivenRenderer {
pass.set_bind_group(2, global_materials, &[]);
pass.set_bind_group(3, joints, &[]);
pass.set_vertex_buffer(0, self.arena.vertex.slice(..));
super::bind_skin_zero(pass, self.arena.layout, 1, &self.skin_zero);
pass.set_index_buffer(self.arena.index.slice(..), wgpu::IndexFormat::Uint32);
let indirect = match route {
IndirectRoute::All => &self.indirect_buffer,
Expand Down Expand Up @@ -1183,6 +1213,12 @@ fn create_cull_bind_group(
})
}

/// The vertex streams of a pipeline drawing the arena: its format, plus the
/// zero joints/weights at slot 1 when that is the 64-byte one.
pub(super) fn arena_buffers() -> Vec<wgpu::VertexBufferLayout<'static>> {
super::vertex_formats::layout_buffers(super::VertexLayout::arena(), &[Vertex3D::desc()])
}

fn create_depth_pipeline(
device: &wgpu::Device,
layout: &wgpu::PipelineLayout,
Expand All @@ -1204,7 +1240,7 @@ fn create_depth_pipeline(
vertex: wgpu::VertexState {
module: shader,
entry_point: Some("vs_main_scene"),
buffers: &[Vertex3D::desc()],
buffers: &arena_buffers(),
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
Expand Down Expand Up @@ -1290,7 +1326,7 @@ fn create_main_pipeline(
vertex: wgpu::VertexState {
module: vertex_shader,
entry_point: Some("vs_main_scene"),
buffers: &[Vertex3D::desc()],
buffers: &arena_buffers(),
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
Expand Down
Loading
Loading