Skip to content

Commit dad2d23

Browse files
author
GBDixonAlex
committed
- flip rt pipeline output, tweak colours and positions for demo
1 parent 76b672a commit dad2d23

File tree

4 files changed

+49
-39
lines changed

4 files changed

+49
-39
lines changed

plugins/ecs_examples/src/raytraced_shadows.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ pub fn setup_raytraced_shadows_scene(
6666
mut commands: Commands) -> Result<(), hotline_rs::Error> {
6767

6868
let cube_mesh = hotline_rs::primitives::create_cube_mesh(&mut device.0);
69-
let tourus_mesh = hotline_rs::primitives::create_tourus_mesh(&mut device.0, 32);
69+
70+
let dodeca_mesh = hotline_rs::primitives::create_dodecahedron_mesh(&mut device.0);
7071
let teapot_mesh = hotline_rs::primitives::create_teapot_mesh(&mut device.0, 32);
71-
let tube_mesh = hotline_rs::primitives::create_tube_prism_mesh(&mut device.0, 32, 0, 32, true, true, 1.0, 0.66, 1.0);
72-
let triangle_mesh = hotline_rs::primitives::create_tube_prism_mesh(&mut device.0, 3, 0, 3, false, true, 0.33, 0.66, 1.0);
72+
let tube_mesh = hotline_rs::primitives::create_tube_prism_mesh(&mut device.0, 5, 0, 4, false, true, 0.33, 0.33, 1.0);
73+
let triangle_mesh = hotline_rs::primitives::create_pyramid_mesh(&mut device.0, 4, false, true);
7374

7475
let bounds = 100.0;
7576

@@ -80,7 +81,7 @@ pub fn setup_raytraced_shadows_scene(
8081
commands.spawn((
8182
Position(light_pos),
8283
Velocity(Vec3f::unit_z()),
83-
Colour(vec4f(0.125, 0.5, 0.25, 1.0)),
84+
Colour(vec4f(0.5, 0.125, 0.25, 1.0)),
8485
LightComponent {
8586
light_type: LightType::Point,
8687
radius: light_radius,
@@ -100,22 +101,22 @@ pub fn setup_raytraced_shadows_scene(
100101
});
101102

102103
let shape_bounds = bounds * 0.6;
104+
let shape_size = bounds * 0.1;
103105

104-
// tourus
105-
let tourus_size = bounds * 0.1;
106-
let tourus_blas = commands.spawn((
106+
// dodeca
107+
let dodeca_blas = commands.spawn((
107108
Position(vec3f(shape_bounds * -0.75, shape_bounds * 0.7, -shape_bounds * 0.1)),
108-
Scale(splat3f(tourus_size)),
109+
Scale(splat3f(shape_size * 2.0)),
109110
Rotation(Quatf::identity()),
110-
MeshComponent(tourus_mesh.clone()),
111+
MeshComponent(dodeca_mesh.clone()),
111112
WorldMatrix(Mat34f::identity()),
112-
blas_from_mesh(&mut device, &tourus_mesh)?
113+
blas_from_mesh(&mut device, &dodeca_mesh)?
113114
)).id();
114115

115116
// helix
116117
commands.spawn((
117118
Position(vec3f(shape_bounds * -0.3, shape_bounds * -0.6, shape_bounds * 0.8)),
118-
Scale(splat3f(tourus_size * 2.0)),
119+
Scale(splat3f(shape_size * 2.0)),
119120
Rotation(Quatf::identity()),
120121
MeshComponent(teapot_mesh.clone()),
121122
WorldMatrix(Mat34f::identity()),
@@ -125,7 +126,7 @@ pub fn setup_raytraced_shadows_scene(
125126
// tube
126127
commands.spawn((
127128
Position(vec3f(shape_bounds * 1.0, shape_bounds * 0.1, shape_bounds * -1.0)),
128-
Scale(splat3f(tourus_size)),
129+
Scale(splat3f(shape_size)),
129130
Rotation(Quatf::identity()),
130131
MeshComponent(tube_mesh.clone()),
131132
WorldMatrix(Mat34f::identity()),
@@ -135,7 +136,7 @@ pub fn setup_raytraced_shadows_scene(
135136
// tri prsim
136137
commands.spawn((
137138
Position(vec3f(shape_bounds * 0.123, shape_bounds * -0.6, shape_bounds * -0.8)),
138-
Scale(splat3f(tourus_size * 2.0)),
139+
Scale(splat3f(shape_size * 2.0)),
139140
Rotation(Quatf::identity()),
140141
MeshComponent(triangle_mesh.clone()),
141142
WorldMatrix(Mat34f::identity()),
@@ -146,9 +147,11 @@ pub fn setup_raytraced_shadows_scene(
146147
let thickness = bounds * 0.1;
147148
let face_size = bounds * 2.0;
148149

150+
let wall_offset = (bounds * 2.0) - thickness;
151+
149152
// -y
150153
commands.spawn((
151-
Position(vec3f(0.0, -bounds, 0.0)),
154+
Position(vec3f(0.0, -wall_offset, 0.0)),
152155
Scale(vec3f(face_size, thickness, face_size)),
153156
Rotation(Quatf::identity()),
154157
MeshComponent(cube_mesh.clone()),
@@ -159,7 +162,7 @@ pub fn setup_raytraced_shadows_scene(
159162

160163
// + y
161164
commands.spawn((
162-
Position(vec3f(0.0, bounds, 0.0)),
165+
Position(vec3f(0.0, wall_offset, 0.0)),
163166
Scale(vec3f(face_size, thickness, face_size)),
164167
Rotation(Quatf::identity()),
165168
MeshComponent(cube_mesh.clone()),
@@ -170,7 +173,7 @@ pub fn setup_raytraced_shadows_scene(
170173

171174
// -z
172175
commands.spawn((
173-
Position(vec3f(0.0, 0.0, -bounds)),
176+
Position(vec3f(0.0, 0.0, -wall_offset)),
174177
Scale(vec3f(face_size, face_size, thickness)),
175178
Rotation(Quatf::identity()),
176179
MeshComponent(cube_mesh.clone()),
@@ -181,7 +184,7 @@ pub fn setup_raytraced_shadows_scene(
181184

182185
// -x
183186
commands.spawn((
184-
Position(vec3f(-bounds, 0.0, 0.0)),
187+
Position(vec3f(-wall_offset, 0.0, 0.0)),
185188
Scale(vec3f(thickness, face_size, face_size)),
186189
Rotation(Quatf::identity()),
187190
MeshComponent(cube_mesh.clone()),
@@ -215,9 +218,10 @@ pub fn setup_tlas(
215218
let translate = Mat34f::from_translation(position.0);
216219
let rotate = Mat34f::from(rotation.0);
217220
let scale = Mat34f::from_scale(scale.0);
221+
let flip = Mat34f::from_scale(vec3f(1.0, 1.0, 1.0));
218222
instances.push(
219223
gfx::RaytracingInstanceInfo::<gfx_platform::Device> {
220-
transform: (translate * rotate * scale).m,
224+
transform: (flip * translate * rotate * scale).m,
221225
instance_id: index as u32,
222226
instance_mask: 0xff,
223227
hit_group_index: 0,
@@ -236,7 +240,6 @@ pub fn setup_tlas(
236240
)?;
237241
let tlas_srv = tlas.get_srv_index().expect("expect tlas to have an srv");
238242
pmfx.push_constant_user_data[0] = tlas_srv as u32;
239-
println!("tlas_srv = {tlas_srv}");
240243
t.tlas = Some(tlas);
241244
}
242245
}
@@ -276,9 +279,10 @@ pub fn update_tlas(
276279
let translate = Mat34f::from_translation(position.0);
277280
let rotate = Mat34f::from(rotation.0);
278281
let scale = Mat34f::from_scale(scale.0);
282+
let flip = Mat34f::from_scale(vec3f(1.0, 1.0, 1.0));
279283
instances.push(
280284
gfx::RaytracingInstanceInfo::<gfx_platform::Device> {
281-
transform: (translate * rotate * scale).m,
285+
transform: (flip * translate * rotate * scale).m,
282286
instance_id: index as u32,
283287
instance_mask: 0xff,
284288
hit_group_index: 0,
@@ -298,11 +302,7 @@ pub fn update_tlas(
298302
Ok(())
299303
}
300304

301-
// TODO_RT
302-
// mesh vertex count
303-
// crash when switching demo
304-
305-
// TODO clean
305+
// TODO cleanup
306306
// fix: Ignoring InitialState D3D12_RESOURCE_STATE_COPY_DEST. Buffers are effectively created in state D3D12_RESOURCE_STATE_COMMON.
307307

308308
#[export_compute_fn]

shaders/material.hlsl

+5-2
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ void scene_raygen_shader()
312312
float2 uv = (float2)DispatchRaysIndex() / (float2)DispatchRaysDimensions();
313313
float2 ndc = uv * 2.0 - 1.0;
314314

315+
float2 output_location = DispatchRaysIndex();
316+
output_location.y = DispatchRaysDimensions().y - output_location.y;
317+
315318
// unproject ray
316319
float4 near = float4(ndc.x, ndc.y, 0.0, 1.0);
317320
float4 far = float4(ndc.x, ndc.y, 1.0, 1.0);
@@ -384,11 +387,11 @@ void scene_raygen_shader()
384387
}
385388
}
386389

387-
rw_textures[resource_indices.x][DispatchRaysIndex().xy] = payload.col;
390+
rw_textures[resource_indices.x][output_location] = payload.col;
388391
}
389392
else
390393
{
391-
rw_textures[resource_indices.x][DispatchRaysIndex().xy] = float4(0.0, 1.0, 0.0, 1.0);
394+
rw_textures[resource_indices.x][output_location] = float4(0.0, 1.0, 0.0, 1.0);
392395
}
393396
}
394397

shaders/material.jsn

+6-10
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,19 @@
167167
}
168168
}
169169
mesh_lit_rt_shadow: {
170-
debug: {
171-
view: "main_view"
172-
pipelines: ["imdraw_3d"]
173-
function: "render_debug"
174-
}
175170
update_tlas: {
176171
pipelines: []
177172
function: "update_tlas"
178-
depends_on: ["debug"]
179173
}
180-
meshes: {
174+
render_meshes_raytraced: {
181175
pipelines: ["mesh_lit_rt_shadow"]
182176
function: "render_meshes_raytraced"
183-
depends_on: ["update_tlas"]
184-
uses: [
177+
uses: [
185178
["staging_output", "Write"]
186179
]
180+
depends_on: [
181+
"update_tlas"
182+
]
187183
}
188184
blit_main_colour: {
189185
view: "main_view"
@@ -193,7 +189,7 @@
193189
]
194190
target_dimension: "staging_output"
195191
function: "blit"
196-
depends_on: ["meshes"]
192+
depends_on: ["render_meshes_raytraced"]
197193
}
198194
}
199195
mesh_pbr: {

src/gfx/d3d12.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -3355,7 +3355,7 @@ impl super::Device for Device {
33553355
IndexFormat: to_dxgi_format(tris.index_format),
33563356
VertexFormat: to_dxgi_format(tris.vertex_format),
33573357
IndexCount: tris.index_count as u32,
3358-
VertexCount: tris.index_count as u32,
3358+
VertexCount: tris.vertex_count as u32,
33593359
IndexBuffer: tris.index_buffer.d3d_virtual_address(),
33603360
VertexBuffer: D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE {
33613361
StartAddress: tris.vertex_buffer.d3d_virtual_address(),
@@ -4403,7 +4403,6 @@ impl super::CmdBuf<Device> for CmdBuf {
44034403
}
44044404

44054405
fn update_raytracing_tlas(&self, tlas: &RaytracingTLAS, instance_buffer: &Buffer, instance_count: usize, mode: AccelerationStructureRebuildMode) {
4406-
44074406
// create acceleration structure inputs
44084407
let inputs = D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS {
44094408
Type: D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL,
@@ -4432,6 +4431,18 @@ impl super::CmdBuf<Device> for CmdBuf {
44324431
let cmd = self.command_list[bb].cast::<ID3D12GraphicsCommandList4>()
44334432
.expect("hotline_rs::gfx::d3d12: expected ID3D12GraphicsCommandList4 availability to create raytracing blas");
44344433
cmd.BuildRaytracingAccelerationStructure(&blas_desc, None);
4434+
4435+
// resource barrier
4436+
let barrier = D3D12_RESOURCE_BARRIER {
4437+
Type: D3D12_RESOURCE_BARRIER_TYPE_UAV,
4438+
Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE,
4439+
Anonymous: D3D12_RESOURCE_BARRIER_0 {
4440+
UAV: std::mem::ManuallyDrop::new(D3D12_RESOURCE_UAV_BARRIER {
4441+
pResource: std::mem::ManuallyDrop::new(tlas.tlas_buffer.resource.clone())
4442+
})
4443+
}
4444+
};
4445+
cmd.ResourceBarrier(&[barrier]);
44354446
}
44364447
}
44374448

0 commit comments

Comments
 (0)