Skip to content

Commit 89ff964

Browse files
author
GBDixonAlex
committed
- add resize to (rw texture output) to raytraced triangle example
1 parent fbe10bf commit 89ff964

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

examples/raytraced_triangle/main.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ use os::Window;
1919
use os::win32 as os_platform;
2020
use gfx::d3d12 as gfx_platform;
2121

22+
/// Create an rw texture output for raytracing to write into
23+
fn create_raytracing_output(device: &mut gfx_platform::Device, window_rect: &os::Rect<i32>) -> gfx_platform::Texture {
24+
let rw_info = gfx::TextureInfo {
25+
format: gfx::Format::RGBA8n,
26+
tex_type: gfx::TextureType::Texture2D,
27+
width: window_rect.width as u64,
28+
height: window_rect.height as u64,
29+
depth: 1,
30+
array_layers: 1,
31+
mip_levels: 1,
32+
samples: 1,
33+
usage: gfx::TextureUsage::SHADER_RESOURCE | gfx::TextureUsage::UNORDERED_ACCESS,
34+
initial_state: gfx::ResourceState::UnorderedAccess,
35+
};
36+
device.create_texture::<u8>(&rw_info, None).unwrap()
37+
}
38+
2239
fn main() -> Result<(), hotline_rs::Error> {
2340
// setup app
2441
let mut app = os_platform::App::create(os::AppInfo {
@@ -32,7 +49,7 @@ fn main() -> Result<(), hotline_rs::Error> {
3249
let num_buffers : u32 = 2;
3350
let mut device = gfx_platform::Device::create(&gfx::DeviceInfo {
3451
render_target_heap_size: num_buffers as usize,
35-
shader_heap_size: 4,
52+
shader_heap_size: 32,
3653
..Default::default()
3754
});
3855
println!("{}", device.get_adapter_info());
@@ -123,29 +140,23 @@ fn main() -> Result<(), hotline_rs::Error> {
123140
})?;
124141

125142
// unordered access rw texture
126-
let window_rect = window.get_viewport_rect();
127-
let rw_info = gfx::TextureInfo {
128-
format: gfx::Format::RGBA8n,
129-
tex_type: gfx::TextureType::Texture2D,
130-
width: window_rect.width as u64,
131-
height: window_rect.height as u64,
132-
depth: 1,
133-
array_layers: 1,
134-
mip_levels: 1,
135-
samples: 1,
136-
usage: gfx::TextureUsage::SHADER_RESOURCE | gfx::TextureUsage::UNORDERED_ACCESS,
137-
initial_state: gfx::ResourceState::UnorderedAccess,
138-
};
139-
let raytracing_output = device.create_texture::<u8>(&rw_info, None).unwrap();
143+
let mut raytracing_output = create_raytracing_output(&mut device, &window.get_viewport_rect());
140144

141145
while app.run() {
142146
// update window and swap chain
143147
window.update(&mut app);
144-
swap_chain.update::<os_platform::App>(&mut device, &window, &mut cmd);
145148

146149
// update viewport from window size
147150
let window_rect = window.get_viewport_rect();
148151

152+
// update and or resize swap chain
153+
let reized = swap_chain.update::<os_platform::App>(&mut device, &window, &mut cmd);
154+
155+
// resize the rw texture output
156+
if reized {
157+
raytracing_output = create_raytracing_output(&mut device, &window.get_viewport_rect());
158+
}
159+
149160
// build command buffer and make draw calls
150161
cmd.reset(&swap_chain);
151162

src/gfx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1308,8 +1308,8 @@ pub trait Device: 'static + Send + Sync + Sized + Any + Clone {
13081308
pub trait SwapChain<D: Device>: 'static + Sized + Any + Send + Sync + Clone {
13091309
/// Call to begin a new frame, to synconise with v-sync and internally swap buffers
13101310
fn new_frame(&mut self);
1311-
/// Update to syncornise with the window, this may require the backbuffer to resize
1312-
fn update<A: os::App>(&mut self, device: &mut D, window: &A::Window, cmd: &mut D::CmdBuf);
1311+
/// Update to syncornise with the window, this may require the backbuffer to resize, returns true if a resize occured
1312+
fn update<A: os::App>(&mut self, device: &mut D, window: &A::Window, cmd: &mut D::CmdBuf) -> bool;
13131313
/// Waits on the CPU for the last frame that was submitted with `swap` to be completed by the GPU
13141314
fn wait_for_last_frame(&self);
13151315
/// Returns the fence value for the current frame, you can use this to syncronise reads

src/gfx/d3d12.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3811,7 +3811,7 @@ impl super::SwapChain<Device> for SwapChain {
38113811
self.frame_fence_value[self.bb_index]
38123812
}
38133813

3814-
fn update<A: os::App>(&mut self, device: &mut Device, window: &A::Window, cmd: &mut CmdBuf) {
3814+
fn update<A: os::App>(&mut self, device: &mut Device, window: &A::Window, cmd: &mut CmdBuf) -> bool {
38153815
let size = window.get_size();
38163816
if (size.x != self.width || size.y != self.height) && size.x > 0 && size.y > 0 {
38173817
unsafe {
@@ -3861,9 +3861,11 @@ impl super::SwapChain<Device> for SwapChain {
38613861
self.width = size.x;
38623862
self.height = size.y;
38633863
self.bb_index = 0;
3864+
true
38643865
}
38653866
} else {
38663867
self.new_frame();
3868+
false
38673869
}
38683870
}
38693871

0 commit comments

Comments
 (0)