Skip to content

Commit

Permalink
fix(renderer): Use atomics for tracking postprocess pingpong state
Browse files Browse the repository at this point in the history
  • Loading branch information
cohaereo committed Oct 27, 2024
1 parent b4553c3 commit a154be3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
21 changes: 13 additions & 8 deletions crates/alkahest-renderer/src/renderer/gbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::mem::size_of;

use alkahest_data::dxgi::DxgiFormat;
use anyhow::Context;
use crossbeam::atomic::AtomicCell;
use glam::Vec3;
use windows::Win32::Graphics::{
Direct3D::{D3D11_SRV_DIMENSION_TEXTURE2D, D3D11_SRV_DIMENSION_TEXTURE2DARRAY},
Expand Down Expand Up @@ -38,20 +39,20 @@ pub struct GBuffer {

pub postprocess_ping: RenderTarget,
pub postprocess_pong: RenderTarget,
postprocess_pingpong: PingPong,
postprocess_pingpong: AtomicCell<PingPong>,

current_size: (u32, u32),
}

#[derive(PartialEq)]
#[derive(PartialEq, Clone, Copy)]
enum PingPong {
Ping,
Pong,
}

impl PingPong {
pub fn flip(&mut self) {
*self = match self {
pub fn next(&self) -> Self {
match self {
PingPong::Ping => PingPong::Pong,
PingPong::Pong => PingPong::Ping,
}
Expand Down Expand Up @@ -164,7 +165,7 @@ impl GBuffer {
"postprocess_ping",
)
.context("postprocess_ping")?,
postprocess_pingpong: PingPong::Ping,
postprocess_pingpong: AtomicCell::new(PingPong::Ping),

current_size: size,
})
Expand Down Expand Up @@ -240,20 +241,24 @@ impl GBuffer {

/// Returns (source, target). If `swap_after_use` is enabled, the order of the buffers will be reversed on the next call
pub fn get_postprocess_rt(&self, swap_after_use: bool) -> (&RenderTarget, &RenderTarget) {
let r = match self.postprocess_pingpong {
let current_pingpong = self.postprocess_pingpong.load();
let r = match current_pingpong {
PingPong::Ping => (&self.postprocess_ping, &self.postprocess_pong),
PingPong::Pong => (&self.postprocess_pong, &self.postprocess_ping),
};

assert_ne!(&r.0.texture, &r.1.texture);

if swap_after_use {
self.postprocess_pingpong.pocus().flip();
self.postprocess_pingpong.store(current_pingpong.next());
}

r
}

/// Get the last rendertarget that was written to in the postprocess pass
pub fn get_postprocess_output(&self) -> &RenderTarget {
match self.postprocess_pingpong {
match self.postprocess_pingpong.load() {
PingPong::Ping => &self.postprocess_ping,
PingPong::Pong => &self.postprocess_pong,
}
Expand Down
4 changes: 4 additions & 0 deletions crates/alkahest-renderer/src/renderer/postprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ impl Renderer {

{
let data = &mut self.data.lock();
// Ping, Pong
// Blits shading_result to pong
let (_source, target) = data.gbuffers.get_postprocess_rt(true);
self.gpu.blit_texture_alphaluminance(
&data.gbuffers.shading_result.view,
Expand All @@ -22,6 +24,8 @@ impl Renderer {
if self.settings.feature_fxaa {
unsafe {
let data = &mut self.data.lock();
// (pong, ping)
// renders to ping
let (source, target) = data.gbuffers.get_postprocess_rt(true);
let rt = target.render_target.clone();
data.externs.fxaa = Some(externs::Fxaa {
Expand Down
12 changes: 9 additions & 3 deletions crates/alkahest-renderer/src/renderer/shadows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,32 @@ impl Renderer {
#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, strum::EnumIter, strum::Display)]
pub enum ShadowQuality {
Off,
Lowest,
Low,
Medium,
High,
Highest,
}

impl ShadowQuality {
pub fn pcf_samples(&self) -> ShadowPcfSamples {
match self {
ShadowQuality::Off => ShadowPcfSamples::Samples13,
ShadowQuality::Low => ShadowPcfSamples::Samples13,
ShadowQuality::Lowest => ShadowPcfSamples::Samples13,
ShadowQuality::Medium => ShadowPcfSamples::Samples17,
ShadowQuality::High => ShadowPcfSamples::Samples21,
ShadowQuality::Highest => ShadowPcfSamples::Samples21,
}
}

pub fn resolution(&self) -> u32 {
match self {
ShadowQuality::Off | ShadowQuality::Low => 256,
ShadowQuality::Medium => 512,
ShadowQuality::High => 1024,
ShadowQuality::Off | ShadowQuality::Lowest => 256,
ShadowQuality::Low => 512,
ShadowQuality::Medium => 1024,
ShadowQuality::High => 2048,
ShadowQuality::Highest => 4096,
}
}
}
Expand Down

0 comments on commit a154be3

Please sign in to comment.