Add optional ambient occlusion and graphics quality presets (#1135)
Thanks @CypherNaught-0x.
This commit is contained in:
@@ -325,6 +325,7 @@ fn frame(scene: &Scene, cam: &Camera, [width, height]: [u32; 2], key: u64, acces
|
||||
access,
|
||||
outline: true,
|
||||
eye: [eye[0], eye[1], eye[2], if cam.fov > 0.0 { 1.0 } else { 0.0 }],
|
||||
ao_radius: 0.0,
|
||||
ssaa: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
+16
-4
@@ -14,14 +14,17 @@ use wgpu::util::DeviceExt;
|
||||
|
||||
use crate::pbr;
|
||||
|
||||
mod ao;
|
||||
|
||||
/// Triangle vertex: position (3 × f32), normal (3 × f32), sRGB colour (4 × u8).
|
||||
pub const TRI_SIZE: usize = 28;
|
||||
pub const PBR_SIZE: usize = pbr::VERTEX_SIZE;
|
||||
/// Line instance: a, b (3 × f32 each), colour (4 × u8), width in pixels (f32).
|
||||
pub const LINE_SIZE: usize = 32;
|
||||
/// The shader's `U`: the view-projection matrix, 9 `vec4`s, the 8 analysis colours, their
|
||||
/// flags and the depth direction (`uniform_bytes`).
|
||||
const UNIFORM_SIZE: u64 = 368;
|
||||
/// The shader's `U`: the view-projection matrix, 10 `vec4`s (the tenth carries the ambient
|
||||
/// occlusion radius), the 8 analysis colours, their flags and the depth direction
|
||||
/// (`uniform_bytes`).
|
||||
const UNIFORM_SIZE: u64 = 384;
|
||||
|
||||
/// A body drawn from its display plan, evaluated on the GPU: the plan, and a colour per face
|
||||
/// (the plan's face numbering; faces past the end take the last colour).
|
||||
@@ -245,6 +248,8 @@ pub struct Frame {
|
||||
/// Eye position and 1 for a perspective camera (0: orthographic), for the section cap's depth
|
||||
/// and the depth bias.
|
||||
pub eye: [f32; 4],
|
||||
/// World-space occlusion radius; zero skips the depth prepass and all AO sampling.
|
||||
pub ao_radius: f32,
|
||||
/// Supersampling (#1288): draw the frame this many times the viewport's pixels in each
|
||||
/// direction and filter it down (1: off). Under 1 the frame is drawn smaller and filtered up
|
||||
/// (the product preview past its pixel budget, #1464).
|
||||
@@ -541,6 +546,8 @@ impl Batches {
|
||||
|
||||
/// The viewport's pipelines and the scenes uploaded to one device, for one kind of target.
|
||||
pub struct Renderer {
|
||||
/// The AO depth prepass resources (a 1 × 1 stand-in while ambient occlusion is off).
|
||||
ao: ao::Resources,
|
||||
tri: wgpu::RenderPipeline,
|
||||
/// `tri` and `tri_cut` for planned bodies (positions and normals from the GPU evaluation,
|
||||
/// colours per vertex, indexed).
|
||||
@@ -959,9 +966,10 @@ impl Renderer {
|
||||
count: None,
|
||||
}],
|
||||
});
|
||||
let ao = ao::Resources::new(device, &bgl, &module, reversed);
|
||||
let solid_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("sc_solid"),
|
||||
bind_group_layouts: &[Some(&bgl), Some(&access_bgl)],
|
||||
bind_group_layouts: &[Some(&bgl), Some(&access_bgl), Some(&ao.layout)],
|
||||
immediate_size: 0,
|
||||
});
|
||||
let built = [Some(tri_layout())];
|
||||
@@ -1108,6 +1116,7 @@ impl Renderer {
|
||||
})
|
||||
});
|
||||
Renderer {
|
||||
ao,
|
||||
depth_reset,
|
||||
target: t,
|
||||
max_texture: device.limits().max_texture_dimension_2d,
|
||||
@@ -1305,6 +1314,7 @@ impl Renderer {
|
||||
res.access_bind = access_texture(device, Some(queue), &res.access_bgl, a.size, &a.depth);
|
||||
res.access_version = a.version;
|
||||
}
|
||||
res.ao.prepare(device, encoder, &res.bind, cb.ao_radius, cb.size_px, res.target.reversed_depth(), [&res.model, &res.preview]);
|
||||
// Images: upload new or changed textures, drop removed ones, rebuild the quads.
|
||||
res.textures.retain(|id, _| cb.images.iter().any(|i| i.id == *id));
|
||||
res.quads.clear();
|
||||
@@ -1414,6 +1424,7 @@ impl Renderer {
|
||||
lines(pass, &res.overlay, &m.under);
|
||||
lines(pass, &res.overlay, &sk.under);
|
||||
pass.set_bind_group(1, &res.access_bind, &[]);
|
||||
pass.set_bind_group(2, &res.ao.bind, &[]);
|
||||
let planned = |pass: &mut wgpu::RenderPass<'static>, p: &wgpu::RenderPipeline| {
|
||||
for d in &m.planned {
|
||||
pass.set_pipeline(p);
|
||||
@@ -1570,6 +1581,7 @@ pub fn uniform_bytes(cb: &Frame, w: f32, h: f32, linear: bool, reversed: bool) -
|
||||
None => v.extend([0.0; 8]),
|
||||
}
|
||||
v.extend(cb.eye);
|
||||
v.extend([cb.ao_radius, 0.0, 0.0, 0.0]);
|
||||
v.extend(cb.analysis_colors);
|
||||
v.extend([if reversed { 1.0 } else { 0.0 }, FACE_STEPS as f32, FACE_SLOPE, 0.0]);
|
||||
v.iter().flat_map(|x| x.to_le_bytes()).collect()
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
//! The optional AO depth prepass. Only opaque model and preview triangles participate: their
|
||||
//! depth is drawn once into a single-sample depth map the lit fragment samples for occlusion.
|
||||
//! The map is independent of the window's MSAA, and the depth runs the same way as the
|
||||
//! viewport's own buffer (see [`super::depth_compare`] and [`super::far_depth`]).
|
||||
|
||||
use super::{Batches, color_layout, depth_compare, far_depth, planned_layout, tri_layout};
|
||||
|
||||
pub(super) struct Resources {
|
||||
pub layout: wgpu::BindGroupLayout,
|
||||
pub bind: wgpu::BindGroup,
|
||||
depth: wgpu::TextureView,
|
||||
size: [u32; 2],
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
planned_pipeline: wgpu::RenderPipeline,
|
||||
}
|
||||
|
||||
fn texture(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, size: [u32; 2]) -> (wgpu::TextureView, wgpu::BindGroup) {
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("sc_ao_depth"),
|
||||
size: wgpu::Extent3d { width: size[0], height: size[1], depth_or_array_layers: 1 },
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Depth32Float,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
|
||||
view_formats: &[],
|
||||
});
|
||||
let depth = texture.create_view(&Default::default());
|
||||
let bind = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("sc_ao_depth"),
|
||||
layout,
|
||||
entries: &[wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&depth) }],
|
||||
});
|
||||
(depth, bind)
|
||||
}
|
||||
|
||||
impl Resources {
|
||||
pub fn new(device: &wgpu::Device, uniform: &wgpu::BindGroupLayout, module: &wgpu::ShaderModule, reversed: bool) -> Self {
|
||||
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("sc_ao_depth"),
|
||||
entries: &[wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture {
|
||||
sample_type: wgpu::TextureSampleType::Depth,
|
||||
view_dimension: wgpu::TextureViewDimension::D2,
|
||||
multisampled: false,
|
||||
},
|
||||
count: None,
|
||||
}],
|
||||
});
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("sc_ao_prepass"),
|
||||
bind_group_layouts: &[Some(uniform)],
|
||||
immediate_size: 0,
|
||||
});
|
||||
let built = [Some(tri_layout())];
|
||||
let evaluated = [Some(planned_layout()), Some(color_layout())];
|
||||
let pipeline = |planned: bool| {
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("sc_ao_prepass"),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module,
|
||||
entry_point: Some("vs_tri"),
|
||||
buffers: if planned { &evaluated } else { &built },
|
||||
compilation_options: Default::default(),
|
||||
},
|
||||
primitive: wgpu::PrimitiveState::default(),
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: wgpu::TextureFormat::Depth32Float,
|
||||
depth_write_enabled: Some(true),
|
||||
depth_compare: Some(depth_compare(reversed, wgpu::CompareFunction::Less)),
|
||||
stencil: Default::default(),
|
||||
bias: Default::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module,
|
||||
entry_point: Some("fs_ao_depth"),
|
||||
targets: &[],
|
||||
compilation_options: Default::default(),
|
||||
}),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
})
|
||||
};
|
||||
let planned_pipeline = pipeline(true);
|
||||
let pipeline = pipeline(false);
|
||||
let (depth, bind) = texture(device, &layout, [1, 1]);
|
||||
Self { layout, bind, depth, size: [1, 1], pipeline, planned_pipeline }
|
||||
}
|
||||
|
||||
pub fn prepare(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
uniform: &wgpu::BindGroup,
|
||||
radius: f32,
|
||||
size_px: [f32; 2],
|
||||
reversed: bool,
|
||||
batches: [&Batches; 2],
|
||||
) {
|
||||
let enabled = radius.is_finite() && radius > 0.0;
|
||||
// Bound memory at large window sizes; normalized coordinates also handle downsampling.
|
||||
let size = target_size(enabled, size_px, device.limits().max_texture_dimension_2d);
|
||||
if size != self.size {
|
||||
(self.depth, self.bind) = texture(device, &self.layout, size);
|
||||
self.size = size;
|
||||
}
|
||||
if !enabled {
|
||||
return;
|
||||
}
|
||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("sc_ao_prepass"),
|
||||
color_attachments: &[],
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &self.depth,
|
||||
depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(far_depth(reversed)), store: wgpu::StoreOp::Store }),
|
||||
stencil_ops: None,
|
||||
}),
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
multiview_mask: None,
|
||||
});
|
||||
pass.set_bind_group(0, uniform, &[]);
|
||||
for batch in batches {
|
||||
pass.set_pipeline(&self.pipeline);
|
||||
if let Some(tris) = &batch.tris {
|
||||
for (buffer, count) in &tris.parts {
|
||||
pass.set_vertex_buffer(0, buffer.slice(..));
|
||||
pass.draw(0..*count, 0..1);
|
||||
}
|
||||
}
|
||||
pass.set_pipeline(&self.planned_pipeline);
|
||||
for draw in &batch.planned {
|
||||
pass.set_vertex_buffer(0, draw.mesh.vertices.slice(..));
|
||||
pass.set_vertex_buffer(1, draw.colors.slice(..));
|
||||
pass.set_index_buffer(draw.mesh.indices.slice(..), wgpu::IndexFormat::Uint32);
|
||||
pass.draw_indexed(0..draw.mesh.index_count, 0, 0..1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn target_size(enabled: bool, pixels: [f32; 2], device_limit: u32) -> [u32; 2] {
|
||||
if !enabled {
|
||||
return [1, 1];
|
||||
}
|
||||
let limit = device_limit.clamp(1, 2048);
|
||||
let pixels = pixels.map(|s| if s.is_finite() { s.max(1.0) } else { 1.0 });
|
||||
let scale = (limit as f32 / pixels[0].max(pixels[1])).min(1.0);
|
||||
pixels.map(|s| (s * scale).ceil().clamp(1.0, limit as f32) as u32)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::target_size;
|
||||
#[test]
|
||||
fn depth_map_is_bounded_and_released_when_disabled() {
|
||||
assert_eq!(target_size(true, [6000.0, 3000.0], 8192), [2048, 1024]);
|
||||
assert_eq!(target_size(true, [6000.0, 3000.0], 1024), [1024, 512]);
|
||||
assert_eq!(target_size(false, [6000.0, 3000.0], 8192), [1, 1]);
|
||||
assert_eq!(target_size(true, [f32::NAN, f32::INFINITY], 8192), [1, 1]);
|
||||
assert_eq!(target_size(true, [0.0, -1.0], 8192), [1, 1]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user