From 4be11ec139052d1e5142f99080ce86610033c0a9 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Mon, 9 Sep 2024 17:19:06 +1200 Subject: [PATCH 01/43] implement device calls --- wgpu-hal/src/dx12/adapter.rs | 22 +++++ wgpu-hal/src/dx12/conv.rs | 65 +++++++++++- wgpu-hal/src/dx12/device.rs | 152 +++++++++++++++++++++++++---- wgpu-hal/src/dx12/mod.rs | 5 +- wgpu-hal/src/dx12/suballocation.rs | 50 ++++++++++ 5 files changed, 274 insertions(+), 20 deletions(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 7d604654bc..e553d2a37b 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -195,6 +195,23 @@ impl super::Adapter { .is_ok() }; + let ray_query = { + let mut features5 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS5::default(); + let has_features5 = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS5, + <*mut _>::cast(&mut features5), + size_of_val(&features5) as u32, + ) + } + .is_ok(); + if has_features5 { + features5.RaytracingTier == Direct3D12::D3D12_RAYTRACING_TIER_1_1 + } else { + false + } + }; + let shader_model = if dxc_container.is_none() { naga::back::hlsl::ShaderModel::V5_1 } else { @@ -385,6 +402,11 @@ impl super::Adapter { && features1.WaveOps.as_bool(), ); + features.set( + wgt::Features::RAY_QUERY, + ray_query, + ); + let atomic_int64_on_typed_resource_supported = { let mut features9 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS9::default(); unsafe { diff --git a/wgpu-hal/src/dx12/conv.rs b/wgpu-hal/src/dx12/conv.rs index 8e60f6e064..7174075481 100644 --- a/wgpu-hal/src/dx12/conv.rs +++ b/wgpu-hal/src/dx12/conv.rs @@ -1,4 +1,4 @@ -use windows::Win32::Graphics::{Direct3D, Direct3D12}; +use windows::Win32::Graphics::{Direct3D, Direct3D12, Dxgi}; pub fn map_buffer_usage_to_resource_flags( usage: crate::BufferUses, @@ -346,3 +346,66 @@ pub fn map_depth_stencil(ds: &wgt::DepthStencilState) -> Direct3D12::D3D12_DEPTH BackFace: map_stencil_face(&ds.stencil.back), } } + +pub(crate) fn map_acceleration_structure_build_flags(flags: wgt::AccelerationStructureFlags) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS { + let mut d3d_flags = Default::default(); + if flags.contains(wgt::AccelerationStructureFlags::ALLOW_COMPACTION) { + d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_COMPACTION; + } + + if flags.contains(wgt::AccelerationStructureFlags::ALLOW_UPDATE) { + d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE; + } + + if flags.contains(wgt::AccelerationStructureFlags::LOW_MEMORY) { + d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_MINIMIZE_MEMORY; + } + + if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_BUILD) { + d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_BUILD; + } + + if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_TRACE) { + d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE; + } + + d3d_flags +} + +pub(crate) fn map_acceleration_structure_geometry_flags(flags: wgt::AccelerationStructureGeometryFlags) -> Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAGS { + let mut d3d_flags = Default::default(); + if flags.contains(wgt::AccelerationStructureGeometryFlags::OPAQUE) { + d3d_flags |= Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE; + } + if flags.contains(wgt::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION) { + d3d_flags |= Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAG_NO_DUPLICATE_ANYHIT_INVOCATION; + } + d3d_flags +} + +pub(crate) fn map_index_format(format: wgt::IndexFormat) -> Dxgi::Common::DXGI_FORMAT { + match format { + wgt::IndexFormat::Uint16 => Dxgi::Common::DXGI_FORMAT_R16_UINT, + wgt::IndexFormat::Uint32 => Dxgi::Common::DXGI_FORMAT_R32_UINT, + } +} + +pub(crate) fn map_acceleration_structure_vertex_format(format: wgt::VertexFormat) -> Dxgi::Common::DXGI_FORMAT { + match format { + wgt::VertexFormat::Unorm8x2 => Dxgi::Common::DXGI_FORMAT_R8G8_UNORM, + wgt::VertexFormat::Unorm8x4 => Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM, + wgt::VertexFormat::Snorm8x2 => Dxgi::Common::DXGI_FORMAT_R8G8_SNORM, + wgt::VertexFormat::Snorm8x4 => Dxgi::Common::DXGI_FORMAT_R8G8B8A8_SNORM, + wgt::VertexFormat::Unorm16x2 => Dxgi::Common::DXGI_FORMAT_R16G16_UNORM, + wgt::VertexFormat::Unorm16x4 => Dxgi::Common::DXGI_FORMAT_R16G16B16A16_UNORM, + wgt::VertexFormat::Snorm16x2 => Dxgi::Common::DXGI_FORMAT_R16G16_SNORM, + wgt::VertexFormat::Snorm16x4 => Dxgi::Common::DXGI_FORMAT_R16G16B16A16_SNORM, + wgt::VertexFormat::Float16x2 => Dxgi::Common::DXGI_FORMAT_R16G16_FLOAT, + wgt::VertexFormat::Float16x4 => Dxgi::Common::DXGI_FORMAT_R16G16B16A16_FLOAT, + wgt::VertexFormat::Float32x2 => Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT, + wgt::VertexFormat::Float32x3 => Dxgi::Common::DXGI_FORMAT_R32G32B32_FLOAT, + wgt::VertexFormat::Unorm10_10_10_2 => Dxgi::Common::DXGI_FORMAT_R10G10B10A2_UNORM, + // no other formats are supported + _ => unimplemented!("disallowed vertex format"), + } +} \ No newline at end of file diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index c5286c12d8..186ec80c33 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -17,11 +17,8 @@ use windows::{ }, }; -use super::{conv, descriptor, D3D12Lib}; -use crate::{ - auxil::{self, dxgi::result::HResult}, - dx12::{borrow_optional_interface_temporarily, shader_compilation, Event}, -}; +use super::{conv, descriptor, D3D12Lib, Buffer}; +use crate::{AccelerationStructureEntries, auxil::{self, dxgi::result::HResult}, dx12::{borrow_optional_interface_temporarily, shader_compilation, Event}}; // this has to match Naga's HLSL backend, and also needs to be null-terminated const NAGA_LOCATION_SEMANTIC: &[u8] = b"LOC\0"; @@ -1781,36 +1778,155 @@ impl crate::Device for super::Device { unsafe fn get_acceleration_structure_build_sizes<'a>( &self, - _desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>, + desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>, ) -> crate::AccelerationStructureBuildSizes { - // Implement using `GetRaytracingAccelerationStructurePrebuildInfo`: - // https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#getraytracingaccelerationstructureprebuildinfo - todo!() + let mut geometry_desc; + let device5 = self.raw.cast::().unwrap(); + let (ty, layout, inputs0, num_desc) = match desc.entries { + AccelerationStructureEntries::Instances(instances) => { + ( + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, + Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + InstanceDescs: instances.buffer.expect("needs buffer to build").resource.GetGPUVirtualAddress() + instances.offset as u64, + }, + instances.count, + ) + }, + AccelerationStructureEntries::Triangles(triangles) => { + geometry_desc = Vec::with_capacity(triangles.len()); + for triangle in triangles { + geometry_desc.push( + Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + Triangles: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { + Transform3x4: triangle.transform.as_ref().map_or(0, |transform| transform.buffer.resource.GetGPUVirtualAddress() + transform.offset as u64), + IndexFormat: triangle.indices.as_ref().map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| conv::map_index_format(indices.format)), + VertexFormat: conv::map_acceleration_structure_vertex_format(triangle.vertex_format), + IndexCount: triangle.indices.as_ref().map_or(0, |indices| indices.count), + VertexCount: triangle.vertex_count, + IndexBuffer: triangle.indices.as_ref().map_or(0, |indices| indices.buffer.expect("needs buffer to build").resource.GetGPUVirtualAddress() + indices.offset), + VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: triangle.vertex_buffer.expect("needs buffer to build").resource.GetGPUVirtualAddress() + (triangle.first_vertex as u64 * triangle.vertex_stride), + StrideInBytes: triangle.vertex_stride, + }, + } + }, + } + ) + } + ( + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, + Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }, + geometry_desc.len() as u32, + ) + }, + AccelerationStructureEntries::AABBs(aabbs) => { + geometry_desc = Vec::with_capacity(aabbs.len()); + for aabb in aabbs { + geometry_desc.push( + Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { + AABBCount: aabb.count as u64, + AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: aabb.buffer.expect("needs buffer to build").resource.GetGPUVirtualAddress() + (aabb.offset as u64 * aabb.stride), + StrideInBytes: aabb.stride, + }, + } + }, + } + ) + } + ( + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, + Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }, + geometry_desc.len() as u32, + ) + }, + }; + let acceleration_structure_inputs = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { + Type: ty, + Flags: conv::map_acceleration_structure_build_flags(desc.flags), + NumDescs: num_desc, + DescsLayout: layout, + Anonymous: inputs0, + }; + let mut info = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO::default(); + device5.GetRaytracingAccelerationStructurePrebuildInfo(&acceleration_structure_inputs, &mut info); + crate::AccelerationStructureBuildSizes { + acceleration_structure_size: info.ResultDataMaxSizeInBytes, + update_scratch_size: info.UpdateScratchDataSizeInBytes, + build_scratch_size: info.ScratchDataSizeInBytes, + } } unsafe fn get_acceleration_structure_device_address( &self, - _acceleration_structure: &super::AccelerationStructure, + acceleration_structure: &super::AccelerationStructure, ) -> wgt::BufferAddress { - // Implement using `GetGPUVirtualAddress`: - // https://docs.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12resource-getgpuvirtualaddress - todo!() + acceleration_structure.resource.GetGPUVirtualAddress() } unsafe fn create_acceleration_structure( &self, - _desc: &crate::AccelerationStructureDescriptor, + desc: &crate::AccelerationStructureDescriptor, ) -> Result { // Create a D3D12 resource as per-usual. - todo!() + let mut size = desc.size; + + let raw_desc = Direct3D12::D3D12_RESOURCE_DESC { + Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER, + Alignment: 0, + Width: size, + Height: 1, + DepthOrArraySize: 1, + MipLevels: 1, + Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN, + SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + Flags: Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE, + }; + + let (resource, allocation) = + super::suballocation::create_acceleration_structure_resource(self, desc, raw_desc)?; + + if let Some(label) = desc.label { + unsafe { resource.SetName(&windows::core::HSTRING::from(label)) } + .into_device_result("SetName")?; + } + + // for some reason there is no counter for acceleration structures + + Ok(super::AccelerationStructure { + resource, + allocation, + }) } unsafe fn destroy_acceleration_structure( &self, - _acceleration_structure: super::AccelerationStructure, + mut acceleration_structure: super::AccelerationStructure, ) { - // Destroy a D3D12 resource as per-usual. - todo!() + if let Some(alloc) = acceleration_structure.allocation.take() { + // Resource should be dropped before free suballocation + drop(acceleration_structure); + + super::suballocation::free_buffer_allocation(self, alloc, &self.mem_allocator); + } } fn get_internal_counters(&self) -> wgt::HalCounters { diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index b871e10112..e0c8137073 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -973,7 +973,10 @@ pub struct PipelineCache; impl crate::DynPipelineCache for PipelineCache {} #[derive(Debug)] -pub struct AccelerationStructure {} +pub struct AccelerationStructure { + resource: Direct3D12::ID3D12Resource, + allocation: Option, +} impl crate::DynAccelerationStructure for AccelerationStructure {} diff --git a/wgpu-hal/src/dx12/suballocation.rs b/wgpu-hal/src/dx12/suballocation.rs index bdb3e85129..1997bbf81b 100644 --- a/wgpu-hal/src/dx12/suballocation.rs +++ b/wgpu-hal/src/dx12/suballocation.rs @@ -151,6 +151,56 @@ pub(crate) fn create_texture_resource( Ok((resource, Some(AllocationWrapper { allocation }))) } +pub(crate) fn create_acceleration_structure_resource( + device: &crate::dx12::Device, + desc: &crate::AccelerationStructureDescriptor, + raw_desc: Direct3D12::D3D12_RESOURCE_DESC, +) -> Result<(Direct3D12::ID3D12Resource, Option), crate::DeviceError> { + + // Workaround for Intel Xe drivers + if !device.private_caps.suballocation_supported { + todo!() + //return create_committed_buffer_resource(device, desc, raw_desc) + // .map(|resource| (resource, None)); + } + + let location = MemoryLocation::GpuOnly; + + let name = desc.label.unwrap_or("Unlabeled acceleration structure"); + + let mut allocator = device.mem_allocator.lock(); + + let allocation_desc = AllocationCreateDesc::from_d3d12_resource_desc( + allocator.allocator.device(), + &raw_desc, + name, + location, + ); + let allocation = allocator.allocator.allocate(&allocation_desc)?; + let mut resource = None; + + unsafe { + device.raw.CreatePlacedResource( + allocation.heap(), + allocation.offset(), + &raw_desc, + Direct3D12::D3D12_RESOURCE_STATE_COMMON, + None, + &mut resource, + ) + } + .into_device_result("Placed acceleration structure creation")?; + + let resource = resource.ok_or(crate::DeviceError::Unexpected)?; + + device + .counters + .buffer_memory + .add(allocation.size() as isize); + + Ok((resource, Some(AllocationWrapper { allocation }))) +} + pub(crate) fn free_buffer_allocation( device: &crate::dx12::Device, allocation: AllocationWrapper, From 647ddd067021a57a7ae5884e06cad776e36f7517 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Mon, 9 Sep 2024 17:23:30 +1200 Subject: [PATCH 02/43] implement todo and fmt --- wgpu-hal/src/dx12/adapter.rs | 5 +- wgpu-hal/src/dx12/conv.rs | 23 +++-- wgpu-hal/src/dx12/device.rs | 147 ++++++++++++++++++----------- wgpu-hal/src/dx12/suballocation.rs | 56 ++++++++++- 4 files changed, 160 insertions(+), 71 deletions(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index e553d2a37b..3ac6935d0b 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -402,10 +402,7 @@ impl super::Adapter { && features1.WaveOps.as_bool(), ); - features.set( - wgt::Features::RAY_QUERY, - ray_query, - ); + features.set(wgt::Features::RAY_QUERY, ray_query); let atomic_int64_on_typed_resource_supported = { let mut features9 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS9::default(); diff --git a/wgpu-hal/src/dx12/conv.rs b/wgpu-hal/src/dx12/conv.rs index 7174075481..7dd701a1fc 100644 --- a/wgpu-hal/src/dx12/conv.rs +++ b/wgpu-hal/src/dx12/conv.rs @@ -347,10 +347,13 @@ pub fn map_depth_stencil(ds: &wgt::DepthStencilState) -> Direct3D12::D3D12_DEPTH } } -pub(crate) fn map_acceleration_structure_build_flags(flags: wgt::AccelerationStructureFlags) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS { +pub(crate) fn map_acceleration_structure_build_flags( + flags: wgt::AccelerationStructureFlags, +) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS { let mut d3d_flags = Default::default(); if flags.contains(wgt::AccelerationStructureFlags::ALLOW_COMPACTION) { - d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_COMPACTION; + d3d_flags |= + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_COMPACTION; } if flags.contains(wgt::AccelerationStructureFlags::ALLOW_UPDATE) { @@ -362,17 +365,21 @@ pub(crate) fn map_acceleration_structure_build_flags(flags: wgt::AccelerationStr } if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_BUILD) { - d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_BUILD; + d3d_flags |= + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_BUILD; } if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_TRACE) { - d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE; + d3d_flags |= + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE; } d3d_flags } -pub(crate) fn map_acceleration_structure_geometry_flags(flags: wgt::AccelerationStructureGeometryFlags) -> Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAGS { +pub(crate) fn map_acceleration_structure_geometry_flags( + flags: wgt::AccelerationStructureGeometryFlags, +) -> Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAGS { let mut d3d_flags = Default::default(); if flags.contains(wgt::AccelerationStructureGeometryFlags::OPAQUE) { d3d_flags |= Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE; @@ -390,7 +397,9 @@ pub(crate) fn map_index_format(format: wgt::IndexFormat) -> Dxgi::Common::DXGI_F } } -pub(crate) fn map_acceleration_structure_vertex_format(format: wgt::VertexFormat) -> Dxgi::Common::DXGI_FORMAT { +pub(crate) fn map_acceleration_structure_vertex_format( + format: wgt::VertexFormat, +) -> Dxgi::Common::DXGI_FORMAT { match format { wgt::VertexFormat::Unorm8x2 => Dxgi::Common::DXGI_FORMAT_R8G8_UNORM, wgt::VertexFormat::Unorm8x4 => Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM, @@ -408,4 +417,4 @@ pub(crate) fn map_acceleration_structure_vertex_format(format: wgt::VertexFormat // no other formats are supported _ => unimplemented!("disallowed vertex format"), } -} \ No newline at end of file +} diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 186ec80c33..62ee0ec258 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -17,8 +17,12 @@ use windows::{ }, }; -use super::{conv, descriptor, D3D12Lib, Buffer}; -use crate::{AccelerationStructureEntries, auxil::{self, dxgi::result::HResult}, dx12::{borrow_optional_interface_temporarily, shader_compilation, Event}}; +use super::{conv, descriptor, Buffer, D3D12Lib}; +use crate::{ + auxil::{self, dxgi::result::HResult}, + dx12::{borrow_optional_interface_temporarily, shader_compilation, Event}, + AccelerationStructureEntries, +}; // this has to match Naga's HLSL backend, and also needs to be null-terminated const NAGA_LOCATION_SEMANTIC: &[u8] = b"LOC\0"; @@ -1783,39 +1787,65 @@ impl crate::Device for super::Device { let mut geometry_desc; let device5 = self.raw.cast::().unwrap(); let (ty, layout, inputs0, num_desc) = match desc.entries { - AccelerationStructureEntries::Instances(instances) => { - ( - Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, - Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), - Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { - InstanceDescs: instances.buffer.expect("needs buffer to build").resource.GetGPUVirtualAddress() + instances.offset as u64, - }, - instances.count, - ) - }, + AccelerationStructureEntries::Instances(instances) => ( + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, + Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + InstanceDescs: instances + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + instances.offset as u64, + }, + instances.count, + ), AccelerationStructureEntries::Triangles(triangles) => { geometry_desc = Vec::with_capacity(triangles.len()); for triangle in triangles { - geometry_desc.push( - Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { - Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, - Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), - Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { - Triangles: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { - Transform3x4: triangle.transform.as_ref().map_or(0, |transform| transform.buffer.resource.GetGPUVirtualAddress() + transform.offset as u64), - IndexFormat: triangle.indices.as_ref().map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| conv::map_index_format(indices.format)), - VertexFormat: conv::map_acceleration_structure_vertex_format(triangle.vertex_format), - IndexCount: triangle.indices.as_ref().map_or(0, |indices| indices.count), - VertexCount: triangle.vertex_count, - IndexBuffer: triangle.indices.as_ref().map_or(0, |indices| indices.buffer.expect("needs buffer to build").resource.GetGPUVirtualAddress() + indices.offset), - VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: triangle.vertex_buffer.expect("needs buffer to build").resource.GetGPUVirtualAddress() + (triangle.first_vertex as u64 * triangle.vertex_stride), - StrideInBytes: triangle.vertex_stride, - }, - } + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + Triangles: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { + Transform3x4: triangle.transform.as_ref().map_or(0, |transform| { + transform.buffer.resource.GetGPUVirtualAddress() + + transform.offset as u64 + }), + IndexFormat: triangle + .indices + .as_ref() + .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { + conv::map_index_format(indices.format) + }), + VertexFormat: conv::map_acceleration_structure_vertex_format( + triangle.vertex_format, + ), + IndexCount: triangle + .indices + .as_ref() + .map_or(0, |indices| indices.count), + VertexCount: triangle.vertex_count, + IndexBuffer: triangle.indices.as_ref().map_or(0, |indices| { + indices + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + indices.offset + }), + VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: triangle + .vertex_buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (triangle.first_vertex as u64 * triangle.vertex_stride), + StrideInBytes: triangle.vertex_stride, + }, }, - } - ) + }, + }) } ( Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, @@ -1825,25 +1855,28 @@ impl crate::Device for super::Device { }, geometry_desc.len() as u32, ) - }, + } AccelerationStructureEntries::AABBs(aabbs) => { geometry_desc = Vec::with_capacity(aabbs.len()); for aabb in aabbs { - geometry_desc.push( - Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { - Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, - Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), - Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { - AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { - AABBCount: aabb.count as u64, - AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: aabb.buffer.expect("needs buffer to build").resource.GetGPUVirtualAddress() + (aabb.offset as u64 * aabb.stride), - StrideInBytes: aabb.stride, - }, - } + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { + AABBCount: aabb.count as u64, + AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: aabb + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (aabb.offset as u64 * aabb.stride), + StrideInBytes: aabb.stride, + }, }, - } - ) + }, + }) } ( Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, @@ -1853,17 +1886,21 @@ impl crate::Device for super::Device { }, geometry_desc.len() as u32, ) - }, - }; - let acceleration_structure_inputs = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { - Type: ty, - Flags: conv::map_acceleration_structure_build_flags(desc.flags), - NumDescs: num_desc, - DescsLayout: layout, - Anonymous: inputs0, + } }; + let acceleration_structure_inputs = + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { + Type: ty, + Flags: conv::map_acceleration_structure_build_flags(desc.flags), + NumDescs: num_desc, + DescsLayout: layout, + Anonymous: inputs0, + }; let mut info = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO::default(); - device5.GetRaytracingAccelerationStructurePrebuildInfo(&acceleration_structure_inputs, &mut info); + device5.GetRaytracingAccelerationStructurePrebuildInfo( + &acceleration_structure_inputs, + &mut info, + ); crate::AccelerationStructureBuildSizes { acceleration_structure_size: info.ResultDataMaxSizeInBytes, update_scratch_size: info.UpdateScratchDataSizeInBytes, diff --git a/wgpu-hal/src/dx12/suballocation.rs b/wgpu-hal/src/dx12/suballocation.rs index 1997bbf81b..450449334a 100644 --- a/wgpu-hal/src/dx12/suballocation.rs +++ b/wgpu-hal/src/dx12/suballocation.rs @@ -156,12 +156,10 @@ pub(crate) fn create_acceleration_structure_resource( desc: &crate::AccelerationStructureDescriptor, raw_desc: Direct3D12::D3D12_RESOURCE_DESC, ) -> Result<(Direct3D12::ID3D12Resource, Option), crate::DeviceError> { - // Workaround for Intel Xe drivers if !device.private_caps.suballocation_supported { - todo!() - //return create_committed_buffer_resource(device, desc, raw_desc) - // .map(|resource| (resource, None)); + return create_committed_acceleration_structure_resource(device, desc, raw_desc) + .map(|resource| (resource, None)); } let location = MemoryLocation::GpuOnly; @@ -189,7 +187,7 @@ pub(crate) fn create_acceleration_structure_resource( &mut resource, ) } - .into_device_result("Placed acceleration structure creation")?; + .into_device_result("Placed acceleration structure creation")?; let resource = resource.ok_or(crate::DeviceError::Unexpected)?; @@ -354,3 +352,51 @@ pub(crate) fn create_committed_texture_resource( resource.ok_or(crate::DeviceError::Unexpected) } + +pub(crate) fn create_committed_acceleration_structure_resource( + device: &crate::dx12::Device, + desc: &crate::AccelerationStructureDescriptor, + raw_desc: Direct3D12::D3D12_RESOURCE_DESC, +) -> Result { + let is_cpu_read = desc.usage.contains(crate::BufferUses::MAP_READ); + let is_cpu_write = desc.usage.contains(crate::BufferUses::MAP_WRITE); + + let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES { + Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM, + CPUPageProperty: if is_cpu_read { + Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_BACK + } else if is_cpu_write { + Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE + } else { + Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE + }, + MemoryPoolPreference: match device.private_caps.memory_architecture { + crate::dx12::MemoryArchitecture::NonUnified if !is_cpu_read && !is_cpu_write => { + Direct3D12::D3D12_MEMORY_POOL_L1 + } + _ => Direct3D12::D3D12_MEMORY_POOL_L0, + }, + CreationNodeMask: 0, + VisibleNodeMask: 0, + }; + + let mut resource = None; + + unsafe { + device.raw.CreateCommittedResource( + &heap_properties, + if device.private_caps.heap_create_not_zeroed { + Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED + } else { + Direct3D12::D3D12_HEAP_FLAG_NONE + }, + &raw_desc, + Direct3D12::D3D12_RESOURCE_STATE_COMMON, + None, + &mut resource, + ) + } + .into_device_result("Committed buffer creation")?; + + resource.ok_or(crate::DeviceError::Unexpected) +} From 0d0b2ab90713308cfde96304a6536517bd9a71e7 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Mon, 9 Sep 2024 17:28:48 +1200 Subject: [PATCH 03/43] properly implement todo and fix other clippy errors --- wgpu-hal/src/dx12/device.rs | 2 +- wgpu-hal/src/dx12/suballocation.rs | 13 ++----------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 62ee0ec258..167950b53c 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1832,7 +1832,7 @@ impl crate::Device for super::Device { .expect("needs buffer to build") .resource .GetGPUVirtualAddress() - + indices.offset + + indices.offset as u64 }), VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { StartAddress: triangle diff --git a/wgpu-hal/src/dx12/suballocation.rs b/wgpu-hal/src/dx12/suballocation.rs index 450449334a..a1325b497b 100644 --- a/wgpu-hal/src/dx12/suballocation.rs +++ b/wgpu-hal/src/dx12/suballocation.rs @@ -358,20 +358,11 @@ pub(crate) fn create_committed_acceleration_structure_resource( desc: &crate::AccelerationStructureDescriptor, raw_desc: Direct3D12::D3D12_RESOURCE_DESC, ) -> Result { - let is_cpu_read = desc.usage.contains(crate::BufferUses::MAP_READ); - let is_cpu_write = desc.usage.contains(crate::BufferUses::MAP_WRITE); - let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES { Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM, - CPUPageProperty: if is_cpu_read { - Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_BACK - } else if is_cpu_write { - Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE - } else { - Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE - }, + CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE, MemoryPoolPreference: match device.private_caps.memory_architecture { - crate::dx12::MemoryArchitecture::NonUnified if !is_cpu_read && !is_cpu_write => { + crate::dx12::MemoryArchitecture::NonUnified => { Direct3D12::D3D12_MEMORY_POOL_L1 } _ => Direct3D12::D3D12_MEMORY_POOL_L0, From f91a482a7405d06e250a9db8c3ca46f189b249f2 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Mon, 9 Sep 2024 18:39:03 +1200 Subject: [PATCH 04/43] don't use buffers in getting build sizes --- wgpu-hal/src/dx12/device.rs | 35 +++++------------------------------ 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 167950b53c..3857089a57 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1791,12 +1791,7 @@ impl crate::Device for super::Device { Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { - InstanceDescs: instances - .buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + instances.offset as u64, + InstanceDescs: 0, }, instances.count, ), @@ -1808,10 +1803,7 @@ impl crate::Device for super::Device { Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { Triangles: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { - Transform3x4: triangle.transform.as_ref().map_or(0, |transform| { - transform.buffer.resource.GetGPUVirtualAddress() - + transform.offset as u64 - }), + Transform3x4: 0, IndexFormat: triangle .indices .as_ref() @@ -1826,21 +1818,9 @@ impl crate::Device for super::Device { .as_ref() .map_or(0, |indices| indices.count), VertexCount: triangle.vertex_count, - IndexBuffer: triangle.indices.as_ref().map_or(0, |indices| { - indices - .buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + indices.offset as u64 - }), + IndexBuffer: 0, VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: triangle - .vertex_buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + (triangle.first_vertex as u64 * triangle.vertex_stride), + StartAddress: 0, StrideInBytes: triangle.vertex_stride, }, }, @@ -1866,12 +1846,7 @@ impl crate::Device for super::Device { AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { AABBCount: aabb.count as u64, AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: aabb - .buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + (aabb.offset as u64 * aabb.stride), + StartAddress: 0, StrideInBytes: aabb.stride, }, }, From 951d4379d5a7c33577c70d508241a5103c46bcd8 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Mon, 9 Sep 2024 18:39:12 +1200 Subject: [PATCH 05/43] fmt --- wgpu-hal/src/dx12/suballocation.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/wgpu-hal/src/dx12/suballocation.rs b/wgpu-hal/src/dx12/suballocation.rs index a1325b497b..f8877e9229 100644 --- a/wgpu-hal/src/dx12/suballocation.rs +++ b/wgpu-hal/src/dx12/suballocation.rs @@ -362,9 +362,7 @@ pub(crate) fn create_committed_acceleration_structure_resource( Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM, CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE, MemoryPoolPreference: match device.private_caps.memory_architecture { - crate::dx12::MemoryArchitecture::NonUnified => { - Direct3D12::D3D12_MEMORY_POOL_L1 - } + crate::dx12::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1, _ => Direct3D12::D3D12_MEMORY_POOL_L0, }, CreationNodeMask: 0, From f01708006167416de7afa166bf606fdd9cfaef81 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Mon, 9 Sep 2024 18:39:26 +1200 Subject: [PATCH 06/43] implement commands --- wgpu-hal/src/dx12/command.rs | 171 +++++++++++++++++++++++++++++++++-- 1 file changed, 164 insertions(+), 7 deletions(-) diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 00f56cdb5f..2b29521e8e 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1,12 +1,14 @@ -use std::{mem, ops::Range}; - -use windows::Win32::{Foundation, Graphics::Direct3D12}; - use super::conv; use crate::{ auxil::{self, dxgi::result::HResult as _}, dx12::borrow_interface_temporarily, + AccelerationStructureEntries, }; +use std::mem::ManuallyDrop; +use std::{mem, ops::Range}; +use windows::Win32::Graphics::Dxgi; +use windows::Win32::{Foundation, Graphics::Direct3D12}; +use windows_core::Interface; fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> Direct3D12::D3D12_BOX { Direct3D12::D3D12_BOX { @@ -1227,7 +1229,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn build_acceleration_structures<'a, T>( &mut self, _descriptor_count: u32, - _descriptors: T, + descriptors: T, ) where super::Api: 'a, T: IntoIterator< @@ -1240,13 +1242,168 @@ impl crate::CommandEncoder for super::CommandEncoder { { // Implement using `BuildRaytracingAccelerationStructure`: // https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#buildraytracingaccelerationstructure - todo!() + let list = self + .list + .as_ref() + .unwrap() + .cast::() + .unwrap(); + for descriptor in descriptors { + // TODO: This is the same as getting build sizes apart from requiring buffers, should this be de-duped? + let mut geometry_desc; + let (ty, layout, inputs0, num_desc) = match desc.entries { + AccelerationStructureEntries::Instances(instances) => ( + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, + Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + InstanceDescs: instances + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + instances.offset as u64, + }, + instances.count, + ), + AccelerationStructureEntries::Triangles(triangles) => { + geometry_desc = Vec::with_capacity(triangles.len()); + for triangle in triangles { + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + Triangles: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { + Transform3x4: triangle.transform.as_ref().map_or( + 0, + |transform| { + transform.buffer.resource.GetGPUVirtualAddress() + + transform.offset as u64 + }, + ), + IndexFormat: triangle + .indices + .as_ref() + .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { + conv::map_index_format(indices.format) + }), + VertexFormat: conv::map_acceleration_structure_vertex_format( + triangle.vertex_format, + ), + IndexCount: triangle + .indices + .as_ref() + .map_or(0, |indices| indices.count), + VertexCount: triangle.vertex_count, + IndexBuffer: triangle.indices.as_ref().map_or(0, |indices| { + indices + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + indices.offset as u64 + }), + VertexBuffer: + Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: triangle + .vertex_buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (triangle.first_vertex as u64 + * triangle.vertex_stride), + StrideInBytes: triangle.vertex_stride, + }, + }, + }, + }) + } + ( + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, + Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }, + geometry_desc.len() as u32, + ) + } + AccelerationStructureEntries::AABBs(aabbs) => { + geometry_desc = Vec::with_capacity(aabbs.len()); + for aabb in aabbs { + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { + AABBCount: aabb.count as u64, + AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: aabb + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (aabb.offset as u64 * aabb.stride), + StrideInBytes: aabb.stride, + }, + }, + }, + }) + } + ( + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, + Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }, + geometry_desc.len() as u32, + ) + } + }; + let acceleration_structure_inputs = + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { + Type: ty, + Flags: conv::map_acceleration_structure_build_flags(descriptor.flags), + NumDescs: num_desc, + DescsLayout: layout, + Anonymous: inputs0, + }; + let desc = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC { + DestAccelerationStructureData: descriptor + .destination_acceleration_structure + .resource + .GetGPUVirtualAddress(), + Inputs: acceleration_structure_inputs, + SourceAccelerationStructureData: descriptor + .source_acceleration_structure + .as_ref() + .map_or(0, |source| source.resource.GetGPUVirtualAddress()), + ScratchAccelerationStructureData: descriptor + .scratch_buffer + .resource + .GetGPUVirtualAddress() + + descriptor.scratch_buffer_offset, + }; + list.BuildRaytracingAccelerationStructure(&desc, None); + } } unsafe fn place_acceleration_structure_barrier( &mut self, _barriers: crate::AccelerationStructureBarrier, ) { - todo!() + let list = self + .list + .as_ref() + .unwrap() + .cast::() + .unwrap(); + list.ResourceBarrier(&[Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + UAV: ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { + pResource: Default::default(), + }), + }, + }]) } } From 23cc3a1f7edfd7106b6008d4a1763e56a3ee8738 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Mon, 9 Sep 2024 18:41:27 +1200 Subject: [PATCH 07/43] fix errors --- wgpu-hal/src/dx12/command.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 2b29521e8e..5229a17fdc 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1251,7 +1251,7 @@ impl crate::CommandEncoder for super::CommandEncoder { for descriptor in descriptors { // TODO: This is the same as getting build sizes apart from requiring buffers, should this be de-duped? let mut geometry_desc; - let (ty, layout, inputs0, num_desc) = match desc.entries { + let (ty, layout, inputs0, num_desc) = match descriptor.entries { AccelerationStructureEntries::Instances(instances) => ( Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), From 6a2f362dc2c89dabc7ed567ee830fec9d92d4cca Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 11 Sep 2024 16:06:25 +1200 Subject: [PATCH 08/43] implement hlsl out --- naga/src/back/hlsl/help.rs | 35 +++++---- naga/src/back/hlsl/keywords.rs | 1 + naga/src/back/hlsl/mod.rs | 2 + naga/src/back/hlsl/ray.rs | 56 ++++++++++++++ naga/src/back/hlsl/writer.rs | 80 ++++++++++++++++---- naga/tests/out/hlsl/ray-query.hlsl | 117 +++++++++++++++++++++++++++++ naga/tests/out/hlsl/ray-query.ron | 12 +++ naga/tests/snapshots.rs | 2 +- 8 files changed, 275 insertions(+), 30 deletions(-) create mode 100644 naga/src/back/hlsl/ray.rs create mode 100644 naga/tests/out/hlsl/ray-query.hlsl create mode 100644 naga/tests/out/hlsl/ray-query.ron diff --git a/naga/src/back/hlsl/help.rs b/naga/src/back/hlsl/help.rs index 80f385d016..9bb50dbb4a 100644 --- a/naga/src/back/hlsl/help.rs +++ b/naga/src/back/hlsl/help.rs @@ -26,11 +26,7 @@ int dim_1d = NagaDimensions1D(image_1d); ``` */ -use super::{ - super::FunctionCtx, - writer::{EXTRACT_BITS_FUNCTION, INSERT_BITS_FUNCTION}, - BackendResult, -}; +use super::{super::FunctionCtx, writer::{EXTRACT_BITS_FUNCTION, INSERT_BITS_FUNCTION}, BackendResult, Error}; use crate::{arena::Handle, proc::NameKey}; use std::fmt::Write; @@ -841,6 +837,9 @@ impl<'a, W: Write> super::Writer<'a, W> { &crate::PredeclaredType::AtomicCompareExchangeWeakResult { .. } => {} } } + if module.special_types.ray_desc.is_some() { + self.write_ray_desc_from_ray_desc_constructor_function(module)?; + } Ok(()) } @@ -852,16 +851,26 @@ impl<'a, W: Write> super::Writer<'a, W> { expressions: &crate::Arena, ) -> BackendResult { for (handle, _) in expressions.iter() { - if let crate::Expression::Compose { ty, .. } = expressions[handle] { - match module.types[ty].inner { - crate::TypeInner::Struct { .. } | crate::TypeInner::Array { .. } => { - let constructor = WrappedConstructor { ty }; - if self.wrapped.constructors.insert(constructor) { - self.write_wrapped_constructor_function(module, constructor)?; + match expressions[handle] { + crate::Expression::Compose { ty, .. } => { + match module.types[ty].inner { + crate::TypeInner::Struct { .. } | crate::TypeInner::Array { .. } => { + let constructor = WrappedConstructor { ty }; + if self.wrapped.constructors.insert(constructor) { + self.write_wrapped_constructor_function(module, constructor)?; + } } + _ => {} + }; + } + crate::Expression::RayQueryGetIntersection { committed, .. } => { + if committed { + self.write_commited_intersection_function(module)?; + } else { + return Err(Error::Unimplemented("candidate intersection".to_string())); } - _ => {} - }; + } + _ => {} } } Ok(()) diff --git a/naga/src/back/hlsl/keywords.rs b/naga/src/back/hlsl/keywords.rs index 2cb715c42c..c15e17636c 100644 --- a/naga/src/back/hlsl/keywords.rs +++ b/naga/src/back/hlsl/keywords.rs @@ -814,6 +814,7 @@ pub const RESERVED: &[&str] = &[ "TextureBuffer", "ConstantBuffer", "RayQuery", + "RayDesc", // Naga utilities super::writer::MODF_FUNCTION, super::writer::FREXP_FUNCTION, diff --git a/naga/src/back/hlsl/mod.rs b/naga/src/back/hlsl/mod.rs index d28b387bf7..36b44eadad 100644 --- a/naga/src/back/hlsl/mod.rs +++ b/naga/src/back/hlsl/mod.rs @@ -103,6 +103,7 @@ mod help; mod keywords; mod storage; mod writer; +mod ray; use std::fmt::Error as FmtError; use thiserror::Error; @@ -327,6 +328,7 @@ pub struct Writer<'a, W> { /// Set of expressions that have associated temporary variables named_expressions: crate::NamedExpressions, wrapped: Wrapped, + written_committed_intersection: bool, continue_ctx: back::continue_forward::ContinueCtx, /// A reference to some part of a global variable, lowered to a series of diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs new file mode 100644 index 0000000000..16374aa098 --- /dev/null +++ b/naga/src/back/hlsl/ray.rs @@ -0,0 +1,56 @@ +use std::fmt::Write; +use crate::back::hlsl::BackendResult; +use crate::TypeInner; + +impl<'a, W: Write> super::Writer<'a, W> { + // constructs hlsl RayDesc from wgsl RayDesc + pub(super) fn write_ray_desc_from_ray_desc_constructor_function( + &mut self, + module: &crate::Module, + ) -> BackendResult { + write!(self.out, "RayDesc RayDescFromRayDesc_(")?; + self.write_type(module, module.special_types.ray_desc.unwrap())?; + writeln!(self.out, " arg0) {{")?; + writeln!(self.out, " RayDesc ret = (RayDesc)0;")?; + writeln!(self.out, " ret.Origin = arg0.origin;")?; + writeln!(self.out, " ret.TMin = arg0.tmin;")?; + writeln!(self.out, " ret.Direction = arg0.dir;")?; + writeln!(self.out, " ret.TMax = arg0.tmax;")?; + writeln!(self.out, "}}")?; + writeln!(self.out)?; + Ok(()) + } + pub(super) fn write_commited_intersection_function( + &mut self, + module: &crate::Module, + ) -> BackendResult { + self.write_type(module, module.special_types.ray_intersection.unwrap())?; + write!(self.out, " GetCommittedIntersection(")?; + self.write_value_type(module, &TypeInner::RayQuery)?; + writeln!(self.out, " rq) {{")?; + write!(self.out, " ")?; + self.write_type(module, module.special_types.ray_intersection.unwrap())?; + write!(self.out, " ret = (")?; + self.write_type(module, module.special_types.ray_intersection.unwrap())?; + writeln!(self.out, ")0;")?; + writeln!(self.out, " ret.kind = rq.CommittedStatus()")?; + writeln!(self.out, " if( rq.CommittedStatus() == COMMITTED_NOTHING) {{}} else {{")?; + writeln!(self.out, " ret.t = rq.CommittedRayT();")?; + writeln!(self.out, " ret.instance_custom_index = rq.CommittedInstanceIndex();")?; + writeln!(self.out, " ret.instance_id = rq.CommittedInstanceID();")?; + writeln!(self.out, " ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex();")?; + writeln!(self.out, " ret.geometry_index = rq.CommittedGeometryIndex();")?; + writeln!(self.out, " ret.primitive_index = .CommittedPrimitiveIndex();")?; + writeln!(self.out, " if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) {{")?; + writeln!(self.out, " ret.barycentrics = rq.CommittedTriangleBarycentrics();")?; + writeln!(self.out, " ret.front_face = rq.CommittedTriangleFrontFace();")?; + writeln!(self.out, " }}")?; + writeln!(self.out, " ret.object_to_world = rq.CommittedObjectToWorld4x3();")?; + writeln!(self.out, " ret.world_to_object = rq.CommittedWorldToObject4x3();")?; + writeln!(self.out, " }}")?; + writeln!(self.out, " return ret;")?; + writeln!(self.out, "}}")?; + writeln!(self.out)?; + Ok(()) + } +} \ No newline at end of file diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index e33fc79f20..dc9e89cd1e 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -6,11 +6,7 @@ use super::{ storage::StoreValue, BackendResult, Error, FragmentEntryPoint, Options, }; -use crate::{ - back::{self, Baked}, - proc::{self, ExpressionKindTracker, NameKey}, - valid, Handle, Module, Scalar, ScalarKind, ShaderStage, TypeInner, -}; +use crate::{back::{self, Baked}, proc::{self, ExpressionKindTracker, NameKey}, valid, Handle, Module, Scalar, ScalarKind, ShaderStage, TypeInner, RayQueryFunction}; use std::{fmt, mem}; const LOCATION_SEMANTIC: &str = "LOC"; @@ -104,6 +100,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { entry_point_io: Vec::new(), named_expressions: crate::NamedExpressions::default(), wrapped: super::Wrapped::default(), + written_committed_intersection: false, continue_ctx: back::continue_forward::ContinueCtx::default(), temp_access_chain: Vec::new(), need_bake_expressions: Default::default(), @@ -123,6 +120,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { self.entry_point_io.clear(); self.named_expressions.clear(); self.wrapped.clear(); + self.written_committed_intersection = false; self.continue_ctx.clear(); self.need_bake_expressions.clear(); } @@ -1217,6 +1215,13 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { TypeInner::Array { base, size, .. } | TypeInner::BindingArray { base, size } => { self.write_array_size(module, base, size)?; } + TypeInner::AccelerationStructure => { + write!(self.out, "RaytracingAccelerationStructure")?; + } + TypeInner::RayQuery => { + // these are constant flags, there are dynamic flags also but constant flags are not supported by naga + write!(self.out, "RayQuery")?; + } _ => return Err(Error::Unimplemented(format!("write_value_type {inner:?}"))), } @@ -1374,15 +1379,20 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { self.write_array_size(module, base, size)?; } - write!(self.out, " = ")?; - // Write the local initializer if needed - if let Some(init) = local.init { - self.write_expr(module, init, func_ctx)?; - } else { - // Zero initialize local variables - self.write_default_init(module, local.ty)?; + match module.types[local.ty].inner { + // from https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#tracerayinline-example-1 it seems that ray queries shouldn't be zeroed + TypeInner::RayQuery => {} + _ => { + write!(self.out, " = ")?; + // Write the local initializer if needed + if let Some(init) = local.init { + self.write_expr(module, init, func_ctx)?; + } else { + // Zero initialize local variables + self.write_default_init(module, local.ty)?; + } + } } - // Finish the local with `;` and add a newline (only for readability) writeln!(self.out, ";")? } @@ -2223,7 +2233,38 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { } => { self.write_switch(module, func_ctx, level, selector, cases)?; } - Statement::RayQuery { .. } => unreachable!(), + Statement::RayQuery { query, ref fun } => { + match *fun { + RayQueryFunction::Initialize { acceleration_structure, descriptor } => { + write!(self.out, "{level}")?; + self.write_expr(module, query, func_ctx)?; + write!(self.out, ".TraceRayInline(")?; + self.write_expr(module, acceleration_structure, func_ctx)?; + write!(self.out, ", ")?; + self.write_expr(module, descriptor, func_ctx)?; + write!(self.out, ".flags, ")?; + self.write_expr(module, descriptor, func_ctx)?; + write!(self.out, ".cull_mask, ")?; + self.write_expr(module, descriptor, func_ctx)?; + write!(self.out, ".flags, ")?; + write!(self.out, "RayDescFromRayDesc_(")?; + self.write_expr(module, descriptor, func_ctx)?; + writeln!(self.out, "))")?; + } + RayQueryFunction::Proceed { result } => { + write!(self.out, "{level}")?; + let name = Baked(result).to_string(); + write!(self.out, "const uint4 {name} = ")?; + self.named_expressions.insert(result, name); + self.write_expr(module, query, func_ctx)?; + writeln!(self.out, ".Proceed()")?; + } + RayQueryFunction::Terminate => { + self.write_expr(module, query, func_ctx)?; + writeln!(self.out, ".Abort()")?; + } + } + } Statement::SubgroupBallot { result, predicate } => { write!(self.out, "{level}")?; let name = Baked(result).to_string(); @@ -3530,8 +3571,15 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { self.write_expr(module, reject, func_ctx)?; write!(self.out, ")")? } - // Not supported yet - Expression::RayQueryGetIntersection { .. } => unreachable!(), + Expression::RayQueryGetIntersection { query, committed } => { + if committed { + write!(self.out, "GetCommittedIntersection(")?; + self.write_expr(module, query, func_ctx)?; + write!(self.out, ")")?; + } else { + return Err(Error::Unimplemented("candidate intersection".to_string())); + } + } // Nothing to do here, since call expression already cached Expression::CallResult(_) | Expression::AtomicResult { .. } diff --git a/naga/tests/out/hlsl/ray-query.hlsl b/naga/tests/out/hlsl/ray-query.hlsl new file mode 100644 index 0000000000..50bdfdd6fc --- /dev/null +++ b/naga/tests/out/hlsl/ray-query.hlsl @@ -0,0 +1,117 @@ +struct RayIntersection { + uint kind; + float t; + uint instance_custom_index; + uint instance_id; + uint sbt_record_offset; + uint geometry_index; + uint primitive_index; + float2 barycentrics; + bool front_face; + int _pad9_0; + int _pad9_1; + row_major float4x3 object_to_world; + int _pad10_0; + row_major float4x3 world_to_object; + int _end_pad_0; +}; + +struct RayDesc_ { + uint flags; + uint cull_mask; + float tmin; + float tmax; + float3 origin; + int _pad5_0; + float3 dir; + int _end_pad_0; +}; + +struct Output { + uint visible; + int _pad1_0; + int _pad1_1; + int _pad1_2; + float3 normal; + int _end_pad_0; +}; + +RayDesc RayDescFromRayDesc_(RayDesc_ arg0) { + RayDesc ret = (RayDesc)0; + ret.Origin = arg0.origin; + ret.TMin = arg0.tmin; + ret.Direction = arg0.dir; + ret.TMax = arg0.tmax; +} + +RaytracingAccelerationStructure acc_struct : register(t0); +RWByteAddressBuffer output : register(u1); + +RayDesc_ ConstructRayDesc_(uint arg0, uint arg1, float arg2, float arg3, float3 arg4, float3 arg5) { + RayDesc_ ret = (RayDesc_)0; + ret.flags = arg0; + ret.cull_mask = arg1; + ret.tmin = arg2; + ret.tmax = arg3; + ret.origin = arg4; + ret.dir = arg5; + return ret; +} + +RayIntersection GetCommittedIntersection(RayQuery rq) { + RayIntersection ret = (RayIntersection)0; + ret.kind = rq.CommittedStatus() + if( rq.CommittedStatus() == COMMITTED_NOTHING) {} else { + ret.t = rq.CommittedRayT(); + ret.instance_custom_index = rq.CommittedInstanceIndex(); + ret.instance_id = rq.CommittedInstanceID(); + ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex(); + ret.geometry_index = rq.CommittedGeometryIndex(); + ret.primitive_index = .CommittedPrimitiveIndex(); + if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) { + ret.barycentrics = rq.CommittedTriangleBarycentrics(); + ret.front_face = rq.CommittedTriangleFrontFace(); + } + ret.object_to_world = rq.CommittedObjectToWorld4x3(); + ret.world_to_object = rq.CommittedWorldToObject4x3(); + } + return ret; +} + +RayIntersection query_loop(float3 pos, float3 dir, RaytracingAccelerationStructure acs) +{ + RayQuery rq; + + rq.TraceRayInline(acs, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).cull_mask, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir))) + while(true) { + const uint4 _e9 = rq.Proceed() + if (_e9) { + } else { + break; + } + { + } + } + const RayIntersection rayintersection = GetCommittedIntersection(rq); + return rayintersection; +} + +float3 get_torus_normal(float3 world_point, RayIntersection intersection) +{ + float3 local_point = mul(float4(world_point, 1.0), intersection.world_to_object); + float2 point_on_guiding_line = (normalize(local_point.xy) * 2.4); + float3 world_point_on_guiding_line = mul(float4(point_on_guiding_line, 0.0, 1.0), intersection.object_to_world); + return normalize((world_point - world_point_on_guiding_line)); +} + +[numthreads(1, 1, 1)] +void main() +{ + float3 pos_1 = (0.0).xxx; + float3 dir_1 = float3(0.0, 1.0, 0.0); + const RayIntersection _e7 = query_loop(pos_1, dir_1, acc_struct); + output.Store(0, asuint(uint((_e7.kind == 0u)))); + const float3 _e18 = get_torus_normal((dir_1 * _e7.t), _e7); + output.Store3(16, asuint(_e18)); + return; +} diff --git a/naga/tests/out/hlsl/ray-query.ron b/naga/tests/out/hlsl/ray-query.ron new file mode 100644 index 0000000000..a07b03300b --- /dev/null +++ b/naga/tests/out/hlsl/ray-query.ron @@ -0,0 +1,12 @@ +( + vertex:[ + ], + fragment:[ + ], + compute:[ + ( + entry_point:"main", + target_profile:"cs_5_1", + ), + ], +) diff --git a/naga/tests/snapshots.rs b/naga/tests/snapshots.rs index 69e5922b39..db809b6b22 100644 --- a/naga/tests/snapshots.rs +++ b/naga/tests/snapshots.rs @@ -857,7 +857,7 @@ fn convert_wgsl() { ("sprite", Targets::SPIRV), ("force_point_size_vertex_shader_webgl", Targets::GLSL), ("invariant", Targets::GLSL), - ("ray-query", Targets::SPIRV | Targets::METAL), + ("ray-query", Targets::SPIRV | Targets::METAL | Targets::HLSL), ("hlsl-keyword", Targets::HLSL), ( "constructors", From 7e2fc3432b96f419d22534d41d8ad80edf1fa572 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 11 Sep 2024 16:43:05 +1200 Subject: [PATCH 09/43] fix clippy warnings --- wgpu-hal/src/dx12/command.rs | 106 ++++++++++++++++------------- wgpu-hal/src/dx12/device.rs | 16 +++-- wgpu-hal/src/dx12/suballocation.rs | 2 +- 3 files changed, 70 insertions(+), 54 deletions(-) diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 5229a17fdc..8e17fbf04c 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1256,12 +1256,14 @@ impl crate::CommandEncoder for super::CommandEncoder { Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { - InstanceDescs: instances - .buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + instances.offset as u64, + InstanceDescs: unsafe { + instances + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + instances.offset as u64 + }, }, instances.count, ), @@ -1275,7 +1277,7 @@ impl crate::CommandEncoder for super::CommandEncoder { Triangles: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { Transform3x4: triangle.transform.as_ref().map_or( 0, - |transform| { + |transform| unsafe { transform.buffer.resource.GetGPUVirtualAddress() + transform.offset as u64 }, @@ -1295,22 +1297,26 @@ impl crate::CommandEncoder for super::CommandEncoder { .map_or(0, |indices| indices.count), VertexCount: triangle.vertex_count, IndexBuffer: triangle.indices.as_ref().map_or(0, |indices| { - indices - .buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + indices.offset as u64 - }), - VertexBuffer: - Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: triangle - .vertex_buffer + unsafe { + indices + .buffer .expect("needs buffer to build") .resource .GetGPUVirtualAddress() - + (triangle.first_vertex as u64 - * triangle.vertex_stride), + + indices.offset as u64 + } + }), + VertexBuffer: + Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: unsafe { + triangle + .vertex_buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (triangle.first_vertex as u64 + * triangle.vertex_stride) + }, StrideInBytes: triangle.vertex_stride, }, }, @@ -1336,12 +1342,14 @@ impl crate::CommandEncoder for super::CommandEncoder { AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { AABBCount: aabb.count as u64, AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: aabb - .buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + (aabb.offset as u64 * aabb.stride), + StartAddress: unsafe { + aabb + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (aabb.offset as u64 * aabb.stride) + }, StrideInBytes: aabb.stride, }, }, @@ -1367,22 +1375,26 @@ impl crate::CommandEncoder for super::CommandEncoder { Anonymous: inputs0, }; let desc = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC { - DestAccelerationStructureData: descriptor - .destination_acceleration_structure - .resource - .GetGPUVirtualAddress(), + DestAccelerationStructureData: unsafe { + descriptor + .destination_acceleration_structure + .resource + .GetGPUVirtualAddress() + }, Inputs: acceleration_structure_inputs, SourceAccelerationStructureData: descriptor .source_acceleration_structure .as_ref() - .map_or(0, |source| source.resource.GetGPUVirtualAddress()), - ScratchAccelerationStructureData: descriptor - .scratch_buffer - .resource - .GetGPUVirtualAddress() - + descriptor.scratch_buffer_offset, + .map_or(0, |source| unsafe { source.resource.GetGPUVirtualAddress() }), + ScratchAccelerationStructureData: unsafe { + descriptor + .scratch_buffer + .resource + .GetGPUVirtualAddress() + + descriptor.scratch_buffer_offset + }, }; - list.BuildRaytracingAccelerationStructure(&desc, None); + unsafe { list.BuildRaytracingAccelerationStructure(&desc, None) }; } } @@ -1396,14 +1408,16 @@ impl crate::CommandEncoder for super::CommandEncoder { .unwrap() .cast::() .unwrap(); - list.ResourceBarrier(&[Direct3D12::D3D12_RESOURCE_BARRIER { - Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, - Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, - Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { - UAV: ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { - pResource: Default::default(), - }), - }, - }]) + unsafe { + list.ResourceBarrier(&[Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + UAV: ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { + pResource: Default::default(), + }), + }, + }]) + } } } diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 3857089a57..10f87222e3 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -17,7 +17,7 @@ use windows::{ }, }; -use super::{conv, descriptor, Buffer, D3D12Lib}; +use super::{conv, descriptor, D3D12Lib}; use crate::{ auxil::{self, dxgi::result::HResult}, dx12::{borrow_optional_interface_temporarily, shader_compilation, Event}, @@ -1872,10 +1872,12 @@ impl crate::Device for super::Device { Anonymous: inputs0, }; let mut info = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO::default(); - device5.GetRaytracingAccelerationStructurePrebuildInfo( - &acceleration_structure_inputs, - &mut info, - ); + unsafe { + device5.GetRaytracingAccelerationStructurePrebuildInfo( + &acceleration_structure_inputs, + &mut info, + ) + }; crate::AccelerationStructureBuildSizes { acceleration_structure_size: info.ResultDataMaxSizeInBytes, update_scratch_size: info.UpdateScratchDataSizeInBytes, @@ -1887,7 +1889,7 @@ impl crate::Device for super::Device { &self, acceleration_structure: &super::AccelerationStructure, ) -> wgt::BufferAddress { - acceleration_structure.resource.GetGPUVirtualAddress() + unsafe { acceleration_structure.resource.GetGPUVirtualAddress() } } unsafe fn create_acceleration_structure( @@ -1895,7 +1897,7 @@ impl crate::Device for super::Device { desc: &crate::AccelerationStructureDescriptor, ) -> Result { // Create a D3D12 resource as per-usual. - let mut size = desc.size; + let size = desc.size; let raw_desc = Direct3D12::D3D12_RESOURCE_DESC { Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER, diff --git a/wgpu-hal/src/dx12/suballocation.rs b/wgpu-hal/src/dx12/suballocation.rs index f8877e9229..864633d187 100644 --- a/wgpu-hal/src/dx12/suballocation.rs +++ b/wgpu-hal/src/dx12/suballocation.rs @@ -355,7 +355,7 @@ pub(crate) fn create_committed_texture_resource( pub(crate) fn create_committed_acceleration_structure_resource( device: &crate::dx12::Device, - desc: &crate::AccelerationStructureDescriptor, + _desc: &crate::AccelerationStructureDescriptor, raw_desc: Direct3D12::D3D12_RESOURCE_DESC, ) -> Result { let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES { From 2350e04ef4ad0e40493dca7b4af955694eff5a47 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 11 Sep 2024 17:08:32 +1200 Subject: [PATCH 10/43] fix CI errors --- naga/src/back/hlsl/help.rs | 6 +++- naga/src/back/hlsl/mod.rs | 2 +- naga/src/back/hlsl/ray.rs | 62 +++++++++++++++++++++++++-------- naga/src/back/hlsl/writer.rs | 67 +++++++++++++++++++----------------- wgpu-hal/src/dx12/command.rs | 23 ++++++------- 5 files changed, 100 insertions(+), 60 deletions(-) diff --git a/naga/src/back/hlsl/help.rs b/naga/src/back/hlsl/help.rs index 9bb50dbb4a..c71f2eb3ef 100644 --- a/naga/src/back/hlsl/help.rs +++ b/naga/src/back/hlsl/help.rs @@ -26,7 +26,11 @@ int dim_1d = NagaDimensions1D(image_1d); ``` */ -use super::{super::FunctionCtx, writer::{EXTRACT_BITS_FUNCTION, INSERT_BITS_FUNCTION}, BackendResult, Error}; +use super::{ + super::FunctionCtx, + writer::{EXTRACT_BITS_FUNCTION, INSERT_BITS_FUNCTION}, + BackendResult, Error, +}; use crate::{arena::Handle, proc::NameKey}; use std::fmt::Write; diff --git a/naga/src/back/hlsl/mod.rs b/naga/src/back/hlsl/mod.rs index 36b44eadad..5b288b6c80 100644 --- a/naga/src/back/hlsl/mod.rs +++ b/naga/src/back/hlsl/mod.rs @@ -101,9 +101,9 @@ accessing individual columns by dynamic index. mod conv; mod help; mod keywords; +mod ray; mod storage; mod writer; -mod ray; use std::fmt::Error as FmtError; use thiserror::Error; diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 16374aa098..4a4f331670 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -1,6 +1,6 @@ -use std::fmt::Write; use crate::back::hlsl::BackendResult; use crate::TypeInner; +use std::fmt::Write; impl<'a, W: Write> super::Writer<'a, W> { // constructs hlsl RayDesc from wgsl RayDesc @@ -16,6 +16,7 @@ impl<'a, W: Write> super::Writer<'a, W> { writeln!(self.out, " ret.TMin = arg0.tmin;")?; writeln!(self.out, " ret.Direction = arg0.dir;")?; writeln!(self.out, " ret.TMax = arg0.tmax;")?; + writeln!(self.out, " return ret;")?; writeln!(self.out, "}}")?; writeln!(self.out)?; Ok(()) @@ -33,24 +34,57 @@ impl<'a, W: Write> super::Writer<'a, W> { write!(self.out, " ret = (")?; self.write_type(module, module.special_types.ray_intersection.unwrap())?; writeln!(self.out, ")0;")?; - writeln!(self.out, " ret.kind = rq.CommittedStatus()")?; - writeln!(self.out, " if( rq.CommittedStatus() == COMMITTED_NOTHING) {{}} else {{")?; + writeln!(self.out, " ret.kind = rq.CommittedStatus();")?; + writeln!( + self.out, + " if( rq.CommittedStatus() == COMMITTED_NOTHING) {{}} else {{" + )?; writeln!(self.out, " ret.t = rq.CommittedRayT();")?; - writeln!(self.out, " ret.instance_custom_index = rq.CommittedInstanceIndex();")?; - writeln!(self.out, " ret.instance_id = rq.CommittedInstanceID();")?; - writeln!(self.out, " ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex();")?; - writeln!(self.out, " ret.geometry_index = rq.CommittedGeometryIndex();")?; - writeln!(self.out, " ret.primitive_index = .CommittedPrimitiveIndex();")?; - writeln!(self.out, " if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) {{")?; - writeln!(self.out, " ret.barycentrics = rq.CommittedTriangleBarycentrics();")?; - writeln!(self.out, " ret.front_face = rq.CommittedTriangleFrontFace();")?; + writeln!( + self.out, + " ret.instance_custom_index = rq.CommittedInstanceIndex();" + )?; + writeln!( + self.out, + " ret.instance_id = rq.CommittedInstanceID();" + )?; + writeln!( + self.out, + " ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex();" + )?; + writeln!( + self.out, + " ret.geometry_index = rq.CommittedGeometryIndex();" + )?; + writeln!( + self.out, + " ret.primitive_index = rq.CommittedPrimitiveIndex();" + )?; + writeln!( + self.out, + " if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) {{" + )?; + writeln!( + self.out, + " ret.barycentrics = rq.CommittedTriangleBarycentrics();" + )?; + writeln!( + self.out, + " ret.front_face = rq.CommittedTriangleFrontFace();" + )?; writeln!(self.out, " }}")?; - writeln!(self.out, " ret.object_to_world = rq.CommittedObjectToWorld4x3();")?; - writeln!(self.out, " ret.world_to_object = rq.CommittedWorldToObject4x3();")?; + writeln!( + self.out, + " ret.object_to_world = rq.CommittedObjectToWorld4x3();" + )?; + writeln!( + self.out, + " ret.world_to_object = rq.CommittedWorldToObject4x3();" + )?; writeln!(self.out, " }}")?; writeln!(self.out, " return ret;")?; writeln!(self.out, "}}")?; writeln!(self.out)?; Ok(()) } -} \ No newline at end of file +} diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index dc9e89cd1e..12b4482eae 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -6,7 +6,11 @@ use super::{ storage::StoreValue, BackendResult, Error, FragmentEntryPoint, Options, }; -use crate::{back::{self, Baked}, proc::{self, ExpressionKindTracker, NameKey}, valid, Handle, Module, Scalar, ScalarKind, ShaderStage, TypeInner, RayQueryFunction}; +use crate::{ + back::{self, Baked}, + proc::{self, ExpressionKindTracker, NameKey}, + valid, Handle, Module, RayQueryFunction, Scalar, ScalarKind, ShaderStage, TypeInner, +}; use std::{fmt, mem}; const LOCATION_SEMANTIC: &str = "LOC"; @@ -2233,38 +2237,37 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { } => { self.write_switch(module, func_ctx, level, selector, cases)?; } - Statement::RayQuery { query, ref fun } => { - match *fun { - RayQueryFunction::Initialize { acceleration_structure, descriptor } => { - write!(self.out, "{level}")?; - self.write_expr(module, query, func_ctx)?; - write!(self.out, ".TraceRayInline(")?; - self.write_expr(module, acceleration_structure, func_ctx)?; - write!(self.out, ", ")?; - self.write_expr(module, descriptor, func_ctx)?; - write!(self.out, ".flags, ")?; - self.write_expr(module, descriptor, func_ctx)?; - write!(self.out, ".cull_mask, ")?; - self.write_expr(module, descriptor, func_ctx)?; - write!(self.out, ".flags, ")?; - write!(self.out, "RayDescFromRayDesc_(")?; - self.write_expr(module, descriptor, func_ctx)?; - writeln!(self.out, "))")?; - } - RayQueryFunction::Proceed { result } => { - write!(self.out, "{level}")?; - let name = Baked(result).to_string(); - write!(self.out, "const uint4 {name} = ")?; - self.named_expressions.insert(result, name); - self.write_expr(module, query, func_ctx)?; - writeln!(self.out, ".Proceed()")?; - } - RayQueryFunction::Terminate => { - self.write_expr(module, query, func_ctx)?; - writeln!(self.out, ".Abort()")?; - } + Statement::RayQuery { query, ref fun } => match *fun { + RayQueryFunction::Initialize { + acceleration_structure, + descriptor, + } => { + write!(self.out, "{level}")?; + self.write_expr(module, query, func_ctx)?; + write!(self.out, ".TraceRayInline(")?; + self.write_expr(module, acceleration_structure, func_ctx)?; + write!(self.out, ", ")?; + self.write_expr(module, descriptor, func_ctx)?; + write!(self.out, ".flags, ")?; + self.write_expr(module, descriptor, func_ctx)?; + write!(self.out, ".cull_mask, ")?; + write!(self.out, "RayDescFromRayDesc_(")?; + self.write_expr(module, descriptor, func_ctx)?; + writeln!(self.out, "))")?; + } + RayQueryFunction::Proceed { result } => { + write!(self.out, "{level}")?; + let name = Baked(result).to_string(); + write!(self.out, "const uint4 {name} = ")?; + self.named_expressions.insert(result, name); + self.write_expr(module, query, func_ctx)?; + writeln!(self.out, ".Proceed()")?; } - } + RayQueryFunction::Terminate => { + self.write_expr(module, query, func_ctx)?; + writeln!(self.out, ".Abort()")?; + } + }, Statement::SubgroupBallot { result, predicate } => { write!(self.out, "{level}")?; let name = Baked(result).to_string(); diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 8e17fbf04c..f35efc74a7 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1296,16 +1296,17 @@ impl crate::CommandEncoder for super::CommandEncoder { .as_ref() .map_or(0, |indices| indices.count), VertexCount: triangle.vertex_count, - IndexBuffer: triangle.indices.as_ref().map_or(0, |indices| { - unsafe { + IndexBuffer: triangle.indices.as_ref().map_or( + 0, + |indices| unsafe { indices .buffer .expect("needs buffer to build") .resource .GetGPUVirtualAddress() + indices.offset as u64 - } - }), + }, + ), VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { StartAddress: unsafe { @@ -1315,7 +1316,7 @@ impl crate::CommandEncoder for super::CommandEncoder { .resource .GetGPUVirtualAddress() + (triangle.first_vertex as u64 - * triangle.vertex_stride) + * triangle.vertex_stride) }, StrideInBytes: triangle.vertex_stride, }, @@ -1343,8 +1344,7 @@ impl crate::CommandEncoder for super::CommandEncoder { AABBCount: aabb.count as u64, AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { StartAddress: unsafe { - aabb - .buffer + aabb.buffer .expect("needs buffer to build") .resource .GetGPUVirtualAddress() @@ -1385,12 +1385,11 @@ impl crate::CommandEncoder for super::CommandEncoder { SourceAccelerationStructureData: descriptor .source_acceleration_structure .as_ref() - .map_or(0, |source| unsafe { source.resource.GetGPUVirtualAddress() }), + .map_or(0, |source| unsafe { + source.resource.GetGPUVirtualAddress() + }), ScratchAccelerationStructureData: unsafe { - descriptor - .scratch_buffer - .resource - .GetGPUVirtualAddress() + descriptor.scratch_buffer.resource.GetGPUVirtualAddress() + descriptor.scratch_buffer_offset }, }; From 4bdf0ed91ffd83a493ba2e3702341ccdb8ee22cf Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 11 Sep 2024 17:15:38 +1200 Subject: [PATCH 11/43] fix CI errors #2 --- naga/src/back/hlsl/help.rs | 2 +- naga/src/back/hlsl/ray.rs | 2 +- naga/tests/out/hlsl/ray-query.hlsl | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/naga/src/back/hlsl/help.rs b/naga/src/back/hlsl/help.rs index c71f2eb3ef..a7dc5a29f8 100644 --- a/naga/src/back/hlsl/help.rs +++ b/naga/src/back/hlsl/help.rs @@ -869,7 +869,7 @@ impl<'a, W: Write> super::Writer<'a, W> { } crate::Expression::RayQueryGetIntersection { committed, .. } => { if committed { - self.write_commited_intersection_function(module)?; + self.write_committed_intersection_function(module)?; } else { return Err(Error::Unimplemented("candidate intersection".to_string())); } diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 4a4f331670..2554a37a91 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -21,7 +21,7 @@ impl<'a, W: Write> super::Writer<'a, W> { writeln!(self.out)?; Ok(()) } - pub(super) fn write_commited_intersection_function( + pub(super) fn write_committed_intersection_function( &mut self, module: &crate::Module, ) -> BackendResult { diff --git a/naga/tests/out/hlsl/ray-query.hlsl b/naga/tests/out/hlsl/ray-query.hlsl index 50bdfdd6fc..2873372786 100644 --- a/naga/tests/out/hlsl/ray-query.hlsl +++ b/naga/tests/out/hlsl/ray-query.hlsl @@ -42,6 +42,7 @@ RayDesc RayDescFromRayDesc_(RayDesc_ arg0) { ret.TMin = arg0.tmin; ret.Direction = arg0.dir; ret.TMax = arg0.tmax; + return ret; } RaytracingAccelerationStructure acc_struct : register(t0); @@ -60,14 +61,14 @@ RayDesc_ ConstructRayDesc_(uint arg0, uint arg1, float arg2, float arg3, float3 RayIntersection GetCommittedIntersection(RayQuery rq) { RayIntersection ret = (RayIntersection)0; - ret.kind = rq.CommittedStatus() + ret.kind = rq.CommittedStatus(); if( rq.CommittedStatus() == COMMITTED_NOTHING) {} else { ret.t = rq.CommittedRayT(); ret.instance_custom_index = rq.CommittedInstanceIndex(); ret.instance_id = rq.CommittedInstanceID(); ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex(); ret.geometry_index = rq.CommittedGeometryIndex(); - ret.primitive_index = .CommittedPrimitiveIndex(); + ret.primitive_index = rq.CommittedPrimitiveIndex(); if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) { ret.barycentrics = rq.CommittedTriangleBarycentrics(); ret.front_face = rq.CommittedTriangleFrontFace(); @@ -82,7 +83,7 @@ RayIntersection query_loop(float3 pos, float3 dir, RaytracingAccelerationStructu { RayQuery rq; - rq.TraceRayInline(acs, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).cull_mask, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir))) + rq.TraceRayInline(acs, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).cull_mask, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir))) while(true) { const uint4 _e9 = rq.Proceed() if (_e9) { From 1750afbb78386786b0838ee07473621ee249a946 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Thu, 12 Sep 2024 06:43:56 +1200 Subject: [PATCH 12/43] fix CI errors #3 --- naga/src/back/hlsl/writer.rs | 6 +++--- naga/tests/in/ray-query.param.ron | 7 +++++++ naga/tests/out/hlsl/ray-query.hlsl | 4 ++-- naga/tests/out/hlsl/ray-query.ron | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index 12b4482eae..c87b79c98f 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -2253,7 +2253,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { write!(self.out, ".cull_mask, ")?; write!(self.out, "RayDescFromRayDesc_(")?; self.write_expr(module, descriptor, func_ctx)?; - writeln!(self.out, "))")?; + writeln!(self.out, "));")?; } RayQueryFunction::Proceed { result } => { write!(self.out, "{level}")?; @@ -2261,11 +2261,11 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { write!(self.out, "const uint4 {name} = ")?; self.named_expressions.insert(result, name); self.write_expr(module, query, func_ctx)?; - writeln!(self.out, ".Proceed()")?; + writeln!(self.out, ".Proceed();")?; } RayQueryFunction::Terminate => { self.write_expr(module, query, func_ctx)?; - writeln!(self.out, ".Abort()")?; + writeln!(self.out, ".Abort();")?; } }, Statement::SubgroupBallot { result, predicate } => { diff --git a/naga/tests/in/ray-query.param.ron b/naga/tests/in/ray-query.param.ron index c400db8c64..c366bf7e90 100644 --- a/naga/tests/in/ray-query.param.ron +++ b/naga/tests/in/ray-query.param.ron @@ -11,4 +11,11 @@ per_entry_point_map: {}, inline_samplers: [], ), + hlsl: ( + shader_model: V6_0, + binding_map: {}, + fake_missing_bindings: true, + special_constants_binding: None, + zero_initialize_workgroup_memory: true, + ) ) diff --git a/naga/tests/out/hlsl/ray-query.hlsl b/naga/tests/out/hlsl/ray-query.hlsl index 2873372786..e3154f4a7b 100644 --- a/naga/tests/out/hlsl/ray-query.hlsl +++ b/naga/tests/out/hlsl/ray-query.hlsl @@ -83,9 +83,9 @@ RayIntersection query_loop(float3 pos, float3 dir, RaytracingAccelerationStructu { RayQuery rq; - rq.TraceRayInline(acs, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).cull_mask, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir))) + rq.TraceRayInline(acs, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).cull_mask, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir))); while(true) { - const uint4 _e9 = rq.Proceed() + const uint4 _e9 = rq.Proceed(); if (_e9) { } else { break; diff --git a/naga/tests/out/hlsl/ray-query.ron b/naga/tests/out/hlsl/ray-query.ron index a07b03300b..b973fe3da1 100644 --- a/naga/tests/out/hlsl/ray-query.ron +++ b/naga/tests/out/hlsl/ray-query.ron @@ -6,7 +6,7 @@ compute:[ ( entry_point:"main", - target_profile:"cs_5_1", + target_profile:"cs_6_0", ), ], ) From 45ec097780873017524ffd68ae60db2c3bf84b26 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Thu, 12 Sep 2024 07:12:05 +1200 Subject: [PATCH 13/43] fix CI errors #4 --- naga/src/back/hlsl/writer.rs | 2 +- naga/tests/out/hlsl/ray-query.hlsl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index c87b79c98f..e090d9b42c 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -2258,7 +2258,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { RayQueryFunction::Proceed { result } => { write!(self.out, "{level}")?; let name = Baked(result).to_string(); - write!(self.out, "const uint4 {name} = ")?; + write!(self.out, "const bool {name} = ")?; self.named_expressions.insert(result, name); self.write_expr(module, query, func_ctx)?; writeln!(self.out, ".Proceed();")?; diff --git a/naga/tests/out/hlsl/ray-query.hlsl b/naga/tests/out/hlsl/ray-query.hlsl index e3154f4a7b..aae2172707 100644 --- a/naga/tests/out/hlsl/ray-query.hlsl +++ b/naga/tests/out/hlsl/ray-query.hlsl @@ -85,7 +85,7 @@ RayIntersection query_loop(float3 pos, float3 dir, RaytracingAccelerationStructu rq.TraceRayInline(acs, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).cull_mask, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir))); while(true) { - const uint4 _e9 = rq.Proceed(); + const bool _e9 = rq.Proceed(); if (_e9) { } else { break; From 275d08240dd607cb947cd0317278e1866a712b7a Mon Sep 17 00:00:00 2001 From: Vecvec Date: Thu, 12 Sep 2024 08:28:55 +1200 Subject: [PATCH 14/43] fix CI errors #5 --- naga/tests/in/ray-query.param.ron | 2 +- naga/tests/out/hlsl/ray-query.ron | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/naga/tests/in/ray-query.param.ron b/naga/tests/in/ray-query.param.ron index c366bf7e90..481d311fa4 100644 --- a/naga/tests/in/ray-query.param.ron +++ b/naga/tests/in/ray-query.param.ron @@ -12,7 +12,7 @@ inline_samplers: [], ), hlsl: ( - shader_model: V6_0, + shader_model: V6_5, binding_map: {}, fake_missing_bindings: true, special_constants_binding: None, diff --git a/naga/tests/out/hlsl/ray-query.ron b/naga/tests/out/hlsl/ray-query.ron index b973fe3da1..4b041674bb 100644 --- a/naga/tests/out/hlsl/ray-query.ron +++ b/naga/tests/out/hlsl/ray-query.ron @@ -6,7 +6,7 @@ compute:[ ( entry_point:"main", - target_profile:"cs_6_0", + target_profile:"cs_6_5", ), ], ) From 3cc98010061c33fb18e90284e6ddd24851821c9e Mon Sep 17 00:00:00 2001 From: Vecvec Date: Thu, 12 Sep 2024 16:14:01 +1200 Subject: [PATCH 15/43] check for sm 6.5 --- wgpu-hal/src/dx12/adapter.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 3ac6935d0b..2946953206 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -195,23 +195,6 @@ impl super::Adapter { .is_ok() }; - let ray_query = { - let mut features5 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS5::default(); - let has_features5 = unsafe { - device.CheckFeatureSupport( - Direct3D12::D3D12_FEATURE_D3D12_OPTIONS5, - <*mut _>::cast(&mut features5), - size_of_val(&features5) as u32, - ) - } - .is_ok(); - if has_features5 { - features5.RaytracingTier == Direct3D12::D3D12_RAYTRACING_TIER_1_1 - } else { - false - } - }; - let shader_model = if dxc_container.is_none() { naga::back::hlsl::ShaderModel::V5_1 } else { @@ -401,8 +384,19 @@ impl super::Adapter { && hr.is_ok() && features1.WaveOps.as_bool(), ); + let mut features5 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS5::default(); + let has_features5 = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS5, + <*mut _>::cast(&mut features5), + size_of_val(&features5) as u32, + ) + } + .is_ok(); + if has_features5 { + features.set(wgt::Features::RAY_QUERY, features5.RaytracingTier == Direct3D12::D3D12_RAYTRACING_TIER_1_1 && shader_model >= naga::back::hlsl::ShaderModel::V6_5); + } - features.set(wgt::Features::RAY_QUERY, ray_query); let atomic_int64_on_typed_resource_supported = { let mut features9 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS9::default(); From dd99a08bd5835b13eece650e74c43a53d3991b8b Mon Sep 17 00:00:00 2001 From: Vecvec Date: Sat, 14 Sep 2024 07:09:21 +1200 Subject: [PATCH 16/43] implement rest of direct x ray queries --- wgpu-hal/src/dx12/adapter.rs | 2 +- wgpu-hal/src/dx12/command.rs | 4 +-- wgpu-hal/src/dx12/conv.rs | 7 +++++- wgpu-hal/src/dx12/device.rs | 40 +++++++++++++++++++++++++----- wgpu-hal/src/dx12/mod.rs | 1 + wgpu-hal/src/dx12/suballocation.rs | 22 +++++++++++++--- wgpu-types/src/counters.rs | 2 ++ 7 files changed, 65 insertions(+), 13 deletions(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 2946953206..e869c249b5 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -242,7 +242,7 @@ impl super::Adapter { _ => unreachable!(), } }; - + println!("{shader_model:?}"); let private_caps = super::PrivateCapabilities { instance_flags, heterogeneous_resource_heaps: options.ResourceHeapTier diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index f35efc74a7..61401dfcb2 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1337,7 +1337,7 @@ impl crate::CommandEncoder for super::CommandEncoder { geometry_desc = Vec::with_capacity(aabbs.len()); for aabb in aabbs { geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { - Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS, Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { @@ -1369,7 +1369,7 @@ impl crate::CommandEncoder for super::CommandEncoder { let acceleration_structure_inputs = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { Type: ty, - Flags: conv::map_acceleration_structure_build_flags(descriptor.flags), + Flags: conv::map_acceleration_structure_build_flags(descriptor.flags, Some(descriptor.mode)), NumDescs: num_desc, DescsLayout: layout, Anonymous: inputs0, diff --git a/wgpu-hal/src/dx12/conv.rs b/wgpu-hal/src/dx12/conv.rs index 7dd701a1fc..af3ccd408a 100644 --- a/wgpu-hal/src/dx12/conv.rs +++ b/wgpu-hal/src/dx12/conv.rs @@ -108,7 +108,7 @@ pub fn map_binding_type(ty: &wgt::BindingType) -> Direct3D12::D3D12_DESCRIPTOR_R .. } | Bt::StorageTexture { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_UAV, - Bt::AccelerationStructure => todo!(), + Bt::AccelerationStructure => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, } } @@ -349,6 +349,7 @@ pub fn map_depth_stencil(ds: &wgt::DepthStencilState) -> Direct3D12::D3D12_DEPTH pub(crate) fn map_acceleration_structure_build_flags( flags: wgt::AccelerationStructureFlags, + mode: Option, ) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS { let mut d3d_flags = Default::default(); if flags.contains(wgt::AccelerationStructureFlags::ALLOW_COMPACTION) { @@ -374,6 +375,10 @@ pub(crate) fn map_acceleration_structure_build_flags( Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE; } + if let Some(crate::AccelerationStructureBuildMode::Update) = mode { + d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE + } + d3d_flags } diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 10f87222e3..67143e7526 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -747,7 +747,7 @@ impl crate::Device for super::Device { &self, desc: &crate::BindGroupLayoutDescriptor, ) -> Result { - let (mut num_buffer_views, mut num_samplers, mut num_texture_views) = (0, 0, 0); + let (mut num_buffer_views, mut num_samplers, mut num_texture_views, mut num_acceleration_structures) = (0, 0, 0, 0); for entry in desc.entries.iter() { let count = entry.count.map_or(1, NonZeroU32::get); match entry.ty { @@ -760,13 +760,13 @@ impl crate::Device for super::Device { num_texture_views += count } wgt::BindingType::Sampler { .. } => num_samplers += count, - wgt::BindingType::AccelerationStructure => todo!(), + wgt::BindingType::AccelerationStructure => num_acceleration_structures += count, } } self.counters.bind_group_layouts.add(1); - let num_views = num_buffer_views + num_texture_views; + let num_views = num_buffer_views + num_texture_views + num_acceleration_structures; Ok(super::BindGroupLayout { entries: desc.entries.to_vec(), cpu_heap_views: if num_views != 0 { @@ -1287,7 +1287,34 @@ impl crate::Device for super::Device { cpu_samplers.as_mut().unwrap().stage.push(data.handle.raw); } } - wgt::BindingType::AccelerationStructure => todo!(), + wgt::BindingType::AccelerationStructure => { + let start = entry.resource_index as usize; + let end = start + entry.count as usize; + for data in &desc.acceleration_structures[start..end] { + let inner = cpu_views.as_mut().unwrap(); + let cpu_index = inner.stage.len() as u32; + let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index); + let raw_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN, + Shader4ComponentMapping: + Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE, + Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + RaytracingAccelerationStructure: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_SRV { + Location: data.resource.GetGPUVirtualAddress(), + }, + }, + }; + unsafe { + self.raw.CreateShaderResourceView( + None, + Some(&raw_desc), + handle, + ) + }; + inner.stage.push(handle); + } + } } } @@ -1866,7 +1893,7 @@ impl crate::Device for super::Device { let acceleration_structure_inputs = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { Type: ty, - Flags: conv::map_acceleration_structure_build_flags(desc.flags), + Flags: conv::map_acceleration_structure_build_flags(desc.flags, None), NumDescs: num_desc, DescsLayout: layout, Anonymous: inputs0, @@ -1912,7 +1939,7 @@ impl crate::Device for super::Device { Quality: 0, }, Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR, - Flags: Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE, + Flags: Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE | Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, }; let (resource, allocation) = @@ -1927,6 +1954,7 @@ impl crate::Device for super::Device { Ok(super::AccelerationStructure { resource, + size, allocation, }) } diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index e0c8137073..0b25433ffc 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -976,6 +976,7 @@ impl crate::DynPipelineCache for PipelineCache {} pub struct AccelerationStructure { resource: Direct3D12::ID3D12Resource, allocation: Option, + size: wgt::BufferAddress, } impl crate::DynAccelerationStructure for AccelerationStructure {} diff --git a/wgpu-hal/src/dx12/suballocation.rs b/wgpu-hal/src/dx12/suballocation.rs index 864633d187..13aab6dcdc 100644 --- a/wgpu-hal/src/dx12/suballocation.rs +++ b/wgpu-hal/src/dx12/suballocation.rs @@ -182,7 +182,7 @@ pub(crate) fn create_acceleration_structure_resource( allocation.heap(), allocation.offset(), &raw_desc, - Direct3D12::D3D12_RESOURCE_STATE_COMMON, + Direct3D12::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, None, &mut resource, ) @@ -193,7 +193,7 @@ pub(crate) fn create_acceleration_structure_resource( device .counters - .buffer_memory + .acceleration_structure_memory .add(allocation.size() as isize); Ok((resource, Some(AllocationWrapper { allocation }))) @@ -231,6 +231,22 @@ pub(crate) fn free_texture_allocation( }; } +pub(crate) fn free_acceleration_structure_allocation( + device: &crate::dx12::Device, + allocation: AllocationWrapper, + allocator: &Mutex, +) { + device + .counters + .acceleration_structure_memory + .sub(allocation.allocation.size() as isize); + match allocator.lock().allocator.free(allocation.allocation) { + Ok(_) => (), + // TODO: Don't panic here + Err(e) => panic!("Failed to destroy dx12 buffer, {e}"), + }; +} + impl From for crate::DeviceError { fn from(result: gpu_allocator::AllocationError) -> Self { match result { @@ -380,7 +396,7 @@ pub(crate) fn create_committed_acceleration_structure_resource( Direct3D12::D3D12_HEAP_FLAG_NONE }, &raw_desc, - Direct3D12::D3D12_RESOURCE_STATE_COMMON, + Direct3D12::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, None, &mut resource, ) diff --git a/wgpu-types/src/counters.rs b/wgpu-types/src/counters.rs index 6b5b87dfb2..8077c0f345 100644 --- a/wgpu-types/src/counters.rs +++ b/wgpu-types/src/counters.rs @@ -127,6 +127,8 @@ pub struct HalCounters { pub texture_memory: InternalCounter, /// Number of gpu memory allocations. pub memory_allocations: InternalCounter, + /// Amount of allocated gpu memory attributed to buffers, in bytes. + pub acceleration_structure_memory: InternalCounter, } /// `wgpu-core`'s internal counters. From 499fa9bb911745d9dc32c797061827bb0b9ee643 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Sat, 14 Sep 2024 07:11:57 +1200 Subject: [PATCH 17/43] fix issues with ray-traced triangle --- wgpu-hal/examples/ray-traced-triangle/main.rs | 13 ++++++++----- wgpu-hal/examples/ray-traced-triangle/shader.wgsl | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/wgpu-hal/examples/ray-traced-triangle/main.rs b/wgpu-hal/examples/ray-traced-triangle/main.rs index 816827b5ae..730dcb8653 100644 --- a/wgpu-hal/examples/ray-traced-triangle/main.rs +++ b/wgpu-hal/examples/ray-traced-triangle/main.rs @@ -13,6 +13,7 @@ use std::{ ptr, time::Instant, }; +use wgt::Features; use winit::window::WindowButtons; const DESIRED_MAX_LATENCY: u32 = 2; @@ -245,6 +246,7 @@ impl Example { } let exposed = adapters.swap_remove(0); dbg!(exposed.features); + assert!(exposed.features.contains(Features::RAY_QUERY)); (exposed.adapter, exposed.features) }; let surface_caps = unsafe { adapter.surface_capabilities(&surface) } @@ -265,9 +267,9 @@ impl Example { dbg!(&surface_caps.formats); let surface_format = if surface_caps .formats - .contains(&wgt::TextureFormat::Rgba8Snorm) + .contains(&wgt::TextureFormat::Bgra8Unorm) { - wgt::TextureFormat::Rgba8Unorm + wgt::TextureFormat::Bgra8Unorm } else { *surface_caps.formats.first().unwrap() }; @@ -449,7 +451,8 @@ impl Example { vertex_buffer: Some(&vertices_buffer), first_vertex: 0, vertex_format: wgt::VertexFormat::Float32x3, - vertex_count: vertices.len() as u32, + // each vertex is 3 floats, and floats are stored raw in the array + vertex_count: vertices.len() as u32 / 3, vertex_stride: 3 * 4, indices: Some(hal::AccelerationStructureTriangleIndices { buffer: Some(&indices_buffer), @@ -553,10 +556,10 @@ impl Example { mip_level_count: 1, sample_count: 1, dimension: wgt::TextureDimension::D2, - format: wgt::TextureFormat::Rgba8Unorm, + format: wgt::TextureFormat::Bgra8Unorm, usage: hal::TextureUses::STORAGE_READ_WRITE | hal::TextureUses::COPY_SRC, memory_flags: hal::MemoryFlags::empty(), - view_formats: vec![wgt::TextureFormat::Rgba8Unorm], + view_formats: vec![wgt::TextureFormat::Bgra8Unorm], }; let texture = unsafe { device.create_texture(&texture_desc).unwrap() }; diff --git a/wgpu-hal/examples/ray-traced-triangle/shader.wgsl b/wgpu-hal/examples/ray-traced-triangle/shader.wgsl index 8d9e475e3e..2cf3d219e0 100644 --- a/wgpu-hal/examples/ray-traced-triangle/shader.wgsl +++ b/wgpu-hal/examples/ray-traced-triangle/shader.wgsl @@ -6,7 +6,7 @@ struct Uniforms { var uniforms: Uniforms; @group(0) @binding(1) -var output: texture_storage_2d; +var output: texture_storage_2d; @group(0) @binding(2) var acc_struct: acceleration_structure; From 4f762e0ee8c963a0162d2a51798ccf09d0746081 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Sat, 14 Sep 2024 07:13:34 +1200 Subject: [PATCH 18/43] clippy & fmt --- wgpu-hal/src/dx12/adapter.rs | 9 ++++++--- wgpu-hal/src/dx12/command.rs | 5 ++++- wgpu-hal/src/dx12/device.rs | 36 ++++++++++++++++++++++-------------- wgpu-hal/src/dx12/mod.rs | 1 - 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index e869c249b5..3bf839c87b 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -392,12 +392,15 @@ impl super::Adapter { size_of_val(&features5) as u32, ) } - .is_ok(); + .is_ok(); if has_features5 { - features.set(wgt::Features::RAY_QUERY, features5.RaytracingTier == Direct3D12::D3D12_RAYTRACING_TIER_1_1 && shader_model >= naga::back::hlsl::ShaderModel::V6_5); + features.set( + wgt::Features::RAY_QUERY, + features5.RaytracingTier == Direct3D12::D3D12_RAYTRACING_TIER_1_1 + && shader_model >= naga::back::hlsl::ShaderModel::V6_5, + ); } - let atomic_int64_on_typed_resource_supported = { let mut features9 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS9::default(); unsafe { diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 61401dfcb2..2d95c9d8a6 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1369,7 +1369,10 @@ impl crate::CommandEncoder for super::CommandEncoder { let acceleration_structure_inputs = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { Type: ty, - Flags: conv::map_acceleration_structure_build_flags(descriptor.flags, Some(descriptor.mode)), + Flags: conv::map_acceleration_structure_build_flags( + descriptor.flags, + Some(descriptor.mode), + ), NumDescs: num_desc, DescsLayout: layout, Anonymous: inputs0, diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 67143e7526..a1427f724c 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -747,7 +747,12 @@ impl crate::Device for super::Device { &self, desc: &crate::BindGroupLayoutDescriptor, ) -> Result { - let (mut num_buffer_views, mut num_samplers, mut num_texture_views, mut num_acceleration_structures) = (0, 0, 0, 0); + let ( + mut num_buffer_views, + mut num_samplers, + mut num_texture_views, + mut num_acceleration_structures, + ) = (0, 0, 0, 0); for entry in desc.entries.iter() { let count = entry.count.map_or(1, NonZeroU32::get); match entry.ty { @@ -1297,20 +1302,19 @@ impl crate::Device for super::Device { let raw_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC { Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN, Shader4ComponentMapping: - Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, - ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE, + Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + ViewDimension: + Direct3D12::D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE, Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 { - RaytracingAccelerationStructure: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_SRV { - Location: data.resource.GetGPUVirtualAddress(), - }, + RaytracingAccelerationStructure: + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_SRV { + Location: unsafe { data.resource.GetGPUVirtualAddress() }, + }, }, }; unsafe { - self.raw.CreateShaderResourceView( - None, - Some(&raw_desc), - handle, - ) + self.raw + .CreateShaderResourceView(None, Some(&raw_desc), handle) }; inner.stage.push(handle); } @@ -1939,7 +1943,8 @@ impl crate::Device for super::Device { Quality: 0, }, Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR, - Flags: Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE | Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + Flags: Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE + | Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, }; let (resource, allocation) = @@ -1954,7 +1959,6 @@ impl crate::Device for super::Device { Ok(super::AccelerationStructure { resource, - size, allocation, }) } @@ -1967,7 +1971,11 @@ impl crate::Device for super::Device { // Resource should be dropped before free suballocation drop(acceleration_structure); - super::suballocation::free_buffer_allocation(self, alloc, &self.mem_allocator); + super::suballocation::free_acceleration_structure_allocation( + self, + alloc, + &self.mem_allocator, + ); } } diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index 0b25433ffc..e0c8137073 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -976,7 +976,6 @@ impl crate::DynPipelineCache for PipelineCache {} pub struct AccelerationStructure { resource: Direct3D12::ID3D12Resource, allocation: Option, - size: wgt::BufferAddress, } impl crate::DynAccelerationStructure for AccelerationStructure {} From 7576bbe4a827f80ec5ed4c255ab6688a3c4cc18d Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 08:16:09 +1300 Subject: [PATCH 19/43] fix merge --- examples/src/ray_cube_compute/mod.rs | 2 +- examples/src/utils.rs | 1 + wgpu-hal/src/dx12/adapter.rs | 7 ++++--- wgpu-hal/src/dx12/device.rs | 23 ++++++++++++++++++----- wgpu/src/backend/wgpu_core.rs | 2 +- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/examples/src/ray_cube_compute/mod.rs b/examples/src/ray_cube_compute/mod.rs index 3f8f31ee0a..a40681f762 100644 --- a/examples/src/ray_cube_compute/mod.rs +++ b/examples/src/ray_cube_compute/mod.rs @@ -137,7 +137,7 @@ struct Example { impl crate::framework::Example for Example { fn required_features() -> wgpu::Features { wgpu::Features::TEXTURE_BINDING_ARRAY - | wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY + //| wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY | wgpu::Features::VERTEX_WRITABLE_STORAGE | wgpu::Features::EXPERIMENTAL_RAY_QUERY | wgpu::Features::EXPERIMENTAL_RAY_TRACING_ACCELERATION_STRUCTURE diff --git a/examples/src/utils.rs b/examples/src/utils.rs index bbc727582b..ab819451db 100644 --- a/examples/src/utils.rs +++ b/examples/src/utils.rs @@ -211,6 +211,7 @@ pub(crate) async fn get_adapter_with_capabilities_or_from_env( let required_features = *required_features; let adapter_features = adapter.features(); if !adapter_features.contains(required_features) { + println!("{:?}", required_features.difference(adapter_features)); continue; } else { chosen_adapter = Some(adapter); diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 82befb561e..a120388b31 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -399,7 +399,8 @@ impl super::Adapter { .is_ok(); if has_features5 { features.set( - wgt::Features::RAY_QUERY, + wgt::Features::EXPERIMENTAL_RAY_QUERY + | wgt::Features::EXPERIMENTAL_RAY_TRACING_ACCELERATION_STRUCTURE, features5.RaytracingTier == Direct3D12::D3D12_RAYTRACING_TIER_1_1 && shader_model >= naga::back::hlsl::ShaderModel::V6_5, ); @@ -539,8 +540,8 @@ impl super::Adapter { // Direct3D correctly bounds-checks all array accesses: // https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#18.6.8.2%20Device%20Memory%20Reads uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(), - raw_tlas_instance_size: 0, - ray_tracing_scratch_buffer_alignment: 0, + raw_tlas_instance_size: 64, + ray_tracing_scratch_buffer_alignment: 256, }, downlevel, }, diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index a12723c205..508ab74dd9 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -2,7 +2,7 @@ use std::{ ffi, mem::{self, size_of, size_of_val}, num::NonZeroU32, - ptr, + ptr, slice, sync::Arc, time::{Duration, Instant}, }; @@ -21,8 +21,7 @@ use super::{conv, descriptor, D3D12Lib}; use crate::{ auxil::{self, dxgi::result::HResult}, dx12::{borrow_optional_interface_temporarily, shader_compilation, Event}, - AccelerationStructureEntries, - TlasInstance, + AccelerationStructureEntries, TlasInstance, }; // this has to match Naga's HLSL backend, and also needs to be null-terminated @@ -2106,7 +2105,21 @@ impl crate::Device for super::Device { }) } - fn tlas_instance_to_bytes(&self, _instance: TlasInstance) -> Vec { - todo!() + fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec { + const MAX_U24: u32 = (1u32 << 24u32) - 1u32; + let temp = Direct3D12::D3D12_RAYTRACING_INSTANCE_DESC { + Transform: instance.transform, + _bitfield1: (instance.custom_index & MAX_U24) | (u32::from(instance.mask) << 24), + _bitfield2: 0, + AccelerationStructure: instance.blas_address, + }; + let temp: *const _ = &temp; + unsafe { + slice::from_raw_parts::( + temp.cast::(), + size_of::(), + ) + .to_vec() + } } } diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 779440f6de..892dbdf006 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -1466,7 +1466,7 @@ impl dispatch::DeviceInterface for CoreDevice { global.device_create_tlas(self.id, &desc.map_label(|l| l.map(Borrowed)), None); if let Some(cause) = error { self.context - .handle_error(&self.error_sink, cause, desc.label, "Device::create_blas"); + .handle_error(&self.error_sink, cause, desc.label, "Device::create_tlas"); } CoreTlas { context: self.context.clone(), From 44f73e6c973807f4d71cb2c52ea7e765caec9add Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 08:29:16 +1300 Subject: [PATCH 20/43] fix build --- wgpu-hal/examples/ray-traced-triangle/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/wgpu-hal/examples/ray-traced-triangle/main.rs b/wgpu-hal/examples/ray-traced-triangle/main.rs index 28b2d096f3..34a6e6a476 100644 --- a/wgpu-hal/examples/ray-traced-triangle/main.rs +++ b/wgpu-hal/examples/ray-traced-triangle/main.rs @@ -265,7 +265,6 @@ impl Example { } let exposed = adapters.swap_remove(0); dbg!(exposed.features); - assert!(exposed.features.contains(Features::RAY_QUERY)); (exposed.adapter, exposed.features) }; let surface_caps = unsafe { adapter.surface_capabilities(&surface) } From 02c8699865a2063c9e0106ed33cbfc96bce3ab75 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 08:37:22 +1300 Subject: [PATCH 21/43] remove unused import --- wgpu-hal/examples/ray-traced-triangle/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/wgpu-hal/examples/ray-traced-triangle/main.rs b/wgpu-hal/examples/ray-traced-triangle/main.rs index 34a6e6a476..4184db5313 100644 --- a/wgpu-hal/examples/ray-traced-triangle/main.rs +++ b/wgpu-hal/examples/ray-traced-triangle/main.rs @@ -13,7 +13,6 @@ use std::{ ptr, time::Instant, }; -use wgt::Features; use winit::window::WindowButtons; const DESIRED_MAX_LATENCY: u32 = 2; From 19caada7581d4f0f814da87daac18de30caad2ab Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 10:38:19 +1300 Subject: [PATCH 22/43] don't use `Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE` as we don't yet support enhanced barriers for acceleration structures --- wgpu-hal/src/dx12/adapter.rs | 20 ++++++++++++-------- wgpu-hal/src/dx12/command.rs | 1 + wgpu-hal/src/dx12/device.rs | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index a120388b31..087f9bd715 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -397,14 +397,18 @@ impl super::Adapter { ) } .is_ok(); - if has_features5 { - features.set( - wgt::Features::EXPERIMENTAL_RAY_QUERY - | wgt::Features::EXPERIMENTAL_RAY_TRACING_ACCELERATION_STRUCTURE, - features5.RaytracingTier == Direct3D12::D3D12_RAYTRACING_TIER_1_1 - && shader_model >= naga::back::hlsl::ShaderModel::V6_5, - ); - } + + // Since all features for raytracing pipeline (geometry index) and ray queries both come + // from here, there is no point in adding an extra call here given that there will be no + // feature using EXPERIMENTAL_RAY_TRACING_ACCELERATION_STRUCTURE if all these are not met. + // Once ray tracing pipelines are supported they also will go here + features.set( + wgt::Features::EXPERIMENTAL_RAY_QUERY + | wgt::Features::EXPERIMENTAL_RAY_TRACING_ACCELERATION_STRUCTURE, + features5.RaytracingTier == Direct3D12::D3D12_RAYTRACING_TIER_1_1 + && shader_model >= naga::back::hlsl::ShaderModel::V6_5 + && has_features5, + ); let atomic_int64_on_typed_resource_supported = { let mut features9 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS9::default(); diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index b9c2d1b26f..f49cc76866 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1424,6 +1424,7 @@ impl crate::CommandEncoder for super::CommandEncoder { &mut self, _barriers: crate::AccelerationStructureBarrier, ) { + // TODO: This is not very optimal, we should be using [enhanced barriers](https://microsoft.github.io/DirectX-Specs/d3d/D3D12EnhancedBarriers.html) if possible let list = self .list .as_ref() diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 508ab74dd9..c82b8494de 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -2035,8 +2035,8 @@ impl crate::Device for super::Device { Quality: 0, }, Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR, - Flags: Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE - | Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + // TODO: when moving to enhanced barriers use Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE + Flags: Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, }; let (resource, allocation) = From 2493f0480188b08ff541e6345532d137e5013e13 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 10:59:43 +1300 Subject: [PATCH 23/43] support candidate intersections --- naga/src/back/hlsl/help.rs | 4 +- naga/src/back/hlsl/ray.rs | 69 ++++++++++++++++++++++++++++++ naga/src/back/hlsl/writer.rs | 4 +- naga/tests/out/hlsl/ray-query.hlsl | 44 +++++++++++++++++-- naga/tests/out/hlsl/ray-query.ron | 4 ++ 5 files changed, 118 insertions(+), 7 deletions(-) diff --git a/naga/src/back/hlsl/help.rs b/naga/src/back/hlsl/help.rs index edb03db277..ff6a8c3338 100644 --- a/naga/src/back/hlsl/help.rs +++ b/naga/src/back/hlsl/help.rs @@ -29,7 +29,7 @@ int dim_1d = NagaDimensions1D(image_1d); use super::{ super::FunctionCtx, writer::{EXTRACT_BITS_FUNCTION, INSERT_BITS_FUNCTION}, - BackendResult, Error, + BackendResult, }; use crate::{arena::Handle, proc::NameKey}; use std::fmt::Write; @@ -871,7 +871,7 @@ impl super::Writer<'_, W> { if committed { self.write_committed_intersection_function(module)?; } else { - return Err(Error::Unimplemented("candidate intersection".to_string())); + self.write_candidate_intersection_function(module)?; } } _ => {} diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 2554a37a91..2ad4e0e5fd 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -87,4 +87,73 @@ impl<'a, W: Write> super::Writer<'a, W> { writeln!(self.out)?; Ok(()) } + pub(super) fn write_candidate_intersection_function( + &mut self, + module: &crate::Module, + ) -> BackendResult { + self.write_type(module, module.special_types.ray_intersection.unwrap())?; + write!(self.out, " GetCandidateIntersection(")?; + self.write_value_type(module, &TypeInner::RayQuery)?; + writeln!(self.out, " rq) {{")?; + write!(self.out, " ")?; + self.write_type(module, module.special_types.ray_intersection.unwrap())?; + write!(self.out, " ret = (")?; + self.write_type(module, module.special_types.ray_intersection.unwrap())?; + writeln!(self.out, ")0;")?; + writeln!(self.out, " CANDIDATE_TYPE kind = rq.CandidateType();")?; + writeln!( + self.out, + " if (kind == CANDIDATE_NON_OPAQUE_TRIANGLE) {{" + )?; + writeln!( + self.out, + " ret.kind = RAY_QUERY_INTERSECTION_TRIANGLE;" + )?; + writeln!(self.out, " }} else {{")?; + writeln!(self.out, " ret.kind = RAY_QUERY_INTERSECTION_AABB;")?; + writeln!(self.out, " }}")?; + writeln!(self.out, " ret.t = rq.CommittedRayT();")?; + writeln!( + self.out, + " ret.instance_custom_index = rq.CommittedInstanceIndex();" + )?; + writeln!(self.out, " ret.instance_id = rq.CommittedInstanceID();")?; + writeln!( + self.out, + " ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex();" + )?; + writeln!( + self.out, + " ret.geometry_index = rq.CommittedGeometryIndex();" + )?; + writeln!( + self.out, + " ret.primitive_index = rq.CommittedPrimitiveIndex();" + )?; + writeln!( + self.out, + " if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) {{" + )?; + writeln!( + self.out, + " ret.barycentrics = rq.CommittedTriangleBarycentrics();" + )?; + writeln!( + self.out, + " ret.front_face = rq.CommittedTriangleFrontFace();" + )?; + writeln!(self.out, " }}")?; + writeln!( + self.out, + " ret.object_to_world = rq.CommittedObjectToWorld4x3();" + )?; + writeln!( + self.out, + " ret.world_to_object = rq.CommittedWorldToObject4x3();" + )?; + writeln!(self.out, " return ret;")?; + writeln!(self.out, "}}")?; + writeln!(self.out)?; + Ok(()) + } } diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index 7b72eb25fd..998e2b76c9 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -3631,7 +3631,9 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { self.write_expr(module, query, func_ctx)?; write!(self.out, ")")?; } else { - return Err(Error::Unimplemented("candidate intersection".to_string())); + write!(self.out, "GetCandidateIntersection(")?; + self.write_expr(module, query, func_ctx)?; + write!(self.out, ")")?; } } // Nothing to do here, since call expression already cached diff --git a/naga/tests/out/hlsl/ray-query.hlsl b/naga/tests/out/hlsl/ray-query.hlsl index aae2172707..594bc9d534 100644 --- a/naga/tests/out/hlsl/ray-query.hlsl +++ b/naga/tests/out/hlsl/ray-query.hlsl @@ -81,11 +81,11 @@ RayIntersection GetCommittedIntersection(RayQuery rq) { RayIntersection query_loop(float3 pos, float3 dir, RaytracingAccelerationStructure acs) { - RayQuery rq; + RayQuery rq_1; - rq.TraceRayInline(acs, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).cull_mask, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir))); + rq_1.TraceRayInline(acs, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir).cull_mask, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos, dir))); while(true) { - const bool _e9 = rq.Proceed(); + const bool _e9 = rq_1.Proceed(); if (_e9) { } else { break; @@ -93,7 +93,7 @@ RayIntersection query_loop(float3 pos, float3 dir, RaytracingAccelerationStructu { } } - const RayIntersection rayintersection = GetCommittedIntersection(rq); + const RayIntersection rayintersection = GetCommittedIntersection(rq_1); return rayintersection; } @@ -116,3 +116,39 @@ void main() output.Store3(16, asuint(_e18)); return; } + +RayIntersection GetCandidateIntersection(RayQuery rq) { + RayIntersection ret = (RayIntersection)0; + CANDIDATE_TYPE kind = rq.CandidateType(); + if (kind == CANDIDATE_NON_OPAQUE_TRIANGLE) { + ret.kind = RAY_QUERY_INTERSECTION_TRIANGLE; + } else { + ret.kind = RAY_QUERY_INTERSECTION_AABB; + } + ret.t = rq.CommittedRayT(); + ret.instance_custom_index = rq.CommittedInstanceIndex(); + ret.instance_id = rq.CommittedInstanceID(); + ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex(); + ret.geometry_index = rq.CommittedGeometryIndex(); + ret.primitive_index = rq.CommittedPrimitiveIndex(); + if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) { + ret.barycentrics = rq.CommittedTriangleBarycentrics(); + ret.front_face = rq.CommittedTriangleFrontFace(); + } + ret.object_to_world = rq.CommittedObjectToWorld4x3(); + ret.world_to_object = rq.CommittedWorldToObject4x3(); + return ret; +} + +[numthreads(1, 1, 1)] +void main_candidate() +{ + RayQuery rq; + + float3 pos_2 = (0.0).xxx; + float3 dir_2 = float3(0.0, 1.0, 0.0); + rq.TraceRayInline(acc_struct, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos_2, dir_2).flags, ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos_2, dir_2).cull_mask, RayDescFromRayDesc_(ConstructRayDesc_(4u, 255u, 0.1, 100.0, pos_2, dir_2))); + RayIntersection intersection_1 = GetCandidateIntersection(rq); + output.Store(0, asuint(uint((intersection_1.kind == 3u)))); + return; +} diff --git a/naga/tests/out/hlsl/ray-query.ron b/naga/tests/out/hlsl/ray-query.ron index 4b041674bb..a31e1db125 100644 --- a/naga/tests/out/hlsl/ray-query.ron +++ b/naga/tests/out/hlsl/ray-query.ron @@ -8,5 +8,9 @@ entry_point:"main", target_profile:"cs_6_5", ), + ( + entry_point:"main_candidate", + target_profile:"cs_6_5", + ), ], ) From 38100517239f819bfb2a7c01aeff444997fd0564 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 11:12:10 +1300 Subject: [PATCH 24/43] write out values of constants, not their wgsl names. --- naga/src/back/hlsl/ray.rs | 7 ++++--- naga/tests/out/hlsl/ray-query.hlsl | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 2ad4e0e5fd..4591bccbb3 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -1,5 +1,5 @@ use crate::back::hlsl::BackendResult; -use crate::TypeInner; +use crate::{RayQueryIntersection, TypeInner}; use std::fmt::Write; impl<'a, W: Write> super::Writer<'a, W> { @@ -107,10 +107,11 @@ impl<'a, W: Write> super::Writer<'a, W> { )?; writeln!( self.out, - " ret.kind = RAY_QUERY_INTERSECTION_TRIANGLE;" + " ret.kind = {};", + RayQueryIntersection::Triangle as u32 )?; writeln!(self.out, " }} else {{")?; - writeln!(self.out, " ret.kind = RAY_QUERY_INTERSECTION_AABB;")?; + writeln!(self.out, " ret.kind = {};", RayQueryIntersection::Aabb as u32)?; writeln!(self.out, " }}")?; writeln!(self.out, " ret.t = rq.CommittedRayT();")?; writeln!( diff --git a/naga/tests/out/hlsl/ray-query.hlsl b/naga/tests/out/hlsl/ray-query.hlsl index 594bc9d534..43c9ee632e 100644 --- a/naga/tests/out/hlsl/ray-query.hlsl +++ b/naga/tests/out/hlsl/ray-query.hlsl @@ -121,9 +121,9 @@ RayIntersection GetCandidateIntersection(RayQuery rq) { RayIntersection ret = (RayIntersection)0; CANDIDATE_TYPE kind = rq.CandidateType(); if (kind == CANDIDATE_NON_OPAQUE_TRIANGLE) { - ret.kind = RAY_QUERY_INTERSECTION_TRIANGLE; + ret.kind = 1; } else { - ret.kind = RAY_QUERY_INTERSECTION_AABB; + ret.kind = 3; } ret.t = rq.CommittedRayT(); ret.instance_custom_index = rq.CommittedInstanceIndex(); From d6a4fee693bf830c7ec60c6e40d8b0993a6c8c9c Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 11:17:21 +1300 Subject: [PATCH 25/43] format --- naga/src/back/hlsl/ray.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 4591bccbb3..81691b1ca2 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -111,7 +111,11 @@ impl<'a, W: Write> super::Writer<'a, W> { RayQueryIntersection::Triangle as u32 )?; writeln!(self.out, " }} else {{")?; - writeln!(self.out, " ret.kind = {};", RayQueryIntersection::Aabb as u32)?; + writeln!( + self.out, + " ret.kind = {};", + RayQueryIntersection::Aabb as u32 + )?; writeln!(self.out, " }}")?; writeln!(self.out, " ret.t = rq.CommittedRayT();")?; writeln!( From 8a88223d72ff6acd9c7e1b4edbd4e361709bfb19 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 11:51:53 +1300 Subject: [PATCH 26/43] fix merge --- naga/src/back/hlsl/ray.rs | 2 +- wgpu-hal/src/dx12/command.rs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 81691b1ca2..3d630c484c 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -2,7 +2,7 @@ use crate::back::hlsl::BackendResult; use crate::{RayQueryIntersection, TypeInner}; use std::fmt::Write; -impl<'a, W: Write> super::Writer<'a, W> { +impl super::Writer<'_, W> { // constructs hlsl RayDesc from wgsl RayDesc pub(super) fn write_ray_desc_from_ray_desc_constructor_function( &mut self, diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index ca86ea3735..5854c1a985 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -4,7 +4,6 @@ use crate::{ dx12::borrow_interface_temporarily, AccelerationStructureEntries, }; -use std::mem::ManuallyDrop; use std::{mem, ops::Range}; use windows::Win32::Graphics::Dxgi; use windows::Win32::{Foundation, Graphics::Direct3D12}; @@ -779,8 +778,8 @@ impl crate::CommandEncoder for super::CommandEncoder { // ) // TODO: Replace with the above in the next breaking windows-rs release, // when https://github.com/microsoft/win32metadata/pull/1971 is in. - (windows_core::Interface::vtable(list).ClearDepthStencilView)( - windows_core::Interface::as_raw(list), + (Interface::vtable(list).ClearDepthStencilView)( + Interface::as_raw(list), ds_view, flags, ds.clear_value.0, @@ -1448,7 +1447,7 @@ impl crate::CommandEncoder for super::CommandEncoder { Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { - UAV: ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { + UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { pResource: Default::default(), }), }, From f74a6da9c4aad5fa85321eac9f40a4f8b5b21702 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 12:36:45 +1300 Subject: [PATCH 27/43] remove println! --- wgpu-hal/src/dx12/adapter.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 381a3c6b30..8be12c5549 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -244,7 +244,6 @@ impl super::Adapter { _ => unreachable!(), } }; - println!("{shader_model:?}"); let private_caps = super::PrivateCapabilities { instance_flags, heterogeneous_resource_heaps: options.ResourceHeapTier From 01e31e425ca7df0e7ccb8e0b533259689605e2b1 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 14:13:03 +1300 Subject: [PATCH 28/43] Remove unnecessary feature. This was preventing DXR from being used. --- examples/src/ray_cube_compute/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/src/ray_cube_compute/mod.rs b/examples/src/ray_cube_compute/mod.rs index a40681f762..0f8d9404d6 100644 --- a/examples/src/ray_cube_compute/mod.rs +++ b/examples/src/ray_cube_compute/mod.rs @@ -137,7 +137,6 @@ struct Example { impl crate::framework::Example for Example { fn required_features() -> wgpu::Features { wgpu::Features::TEXTURE_BINDING_ARRAY - //| wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY | wgpu::Features::VERTEX_WRITABLE_STORAGE | wgpu::Features::EXPERIMENTAL_RAY_QUERY | wgpu::Features::EXPERIMENTAL_RAY_TRACING_ACCELERATION_STRUCTURE From 9a57f861366fba7942ba9f9cef884a449c2524ff Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 14:17:02 +1300 Subject: [PATCH 29/43] Remove println!() --- examples/src/utils.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/src/utils.rs b/examples/src/utils.rs index ab819451db..bbc727582b 100644 --- a/examples/src/utils.rs +++ b/examples/src/utils.rs @@ -211,7 +211,6 @@ pub(crate) async fn get_adapter_with_capabilities_or_from_env( let required_features = *required_features; let adapter_features = adapter.features(); if !adapter_features.contains(required_features) { - println!("{:?}", required_features.difference(adapter_features)); continue; } else { chosen_adapter = Some(adapter); From a66ebcf858c467e8458c3a0d38a54ae2abf5f50b Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 14:51:19 +1300 Subject: [PATCH 30/43] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b89aac4aa7..f35bf37ba5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -149,6 +149,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148] - Return submission index in `map_async` and `on_submitted_work_done` to track down completion of async callbacks. By @eliemichel in [#6360](https://github.com/gfx-rs/wgpu/pull/6360). - Move raytracing alignments into HAL instead of in core. By @Vecvec in [#6563](https://github.com/gfx-rs/wgpu/pull/6563). - Allow for statically linking DXC rather than including separate `.dll` files. By @DouglasDwyer in [#6574](https://github.com/gfx-rs/wgpu/pull/6574). +- Support DXR in wgpu-hal. By @Vecvec in [#6777](https://github.com/gfx-rs/wgpu/pull/6777) ### Changes From f93a97d9045d98cb96ca7ad66f8aa94bff10c5be Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 15:02:12 +1300 Subject: [PATCH 31/43] Correctly use candidate instead of committed int the candidate intersection function --- naga/src/back/hlsl/ray.rs | 34 +++++++++++++----------------- naga/tests/out/hlsl/ray-query.hlsl | 22 +++++++++---------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 3d630c484c..95f18683cc 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -110,51 +110,47 @@ impl super::Writer<'_, W> { " ret.kind = {};", RayQueryIntersection::Triangle as u32 )?; - writeln!(self.out, " }} else {{")?; - writeln!( - self.out, - " ret.kind = {};", - RayQueryIntersection::Aabb as u32 - )?; - writeln!(self.out, " }}")?; - writeln!(self.out, " ret.t = rq.CommittedRayT();")?; + writeln!(self.out, " ret.t = rq.CandidateTriangleRayT();")?; writeln!( self.out, - " ret.instance_custom_index = rq.CommittedInstanceIndex();" + " ret.barycentrics = rq.CandidateTriangleBarycentrics();" )?; - writeln!(self.out, " ret.instance_id = rq.CommittedInstanceID();")?; writeln!( self.out, - " ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex();" + " ret.front_face = rq.CandidateTriangleFrontFace();" )?; + writeln!(self.out, " }} else {{")?; writeln!( self.out, - " ret.geometry_index = rq.CommittedGeometryIndex();" + " ret.kind = {};", + RayQueryIntersection::Aabb as u32 )?; + writeln!(self.out, " }}")?; + writeln!( self.out, - " ret.primitive_index = rq.CommittedPrimitiveIndex();" + " ret.instance_custom_index = rq.CandidateInstanceIndex();" )?; + writeln!(self.out, " ret.instance_id = rq.CandidateInstanceID();")?; writeln!( self.out, - " if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) {{" + " ret.sbt_record_offset = rq.CandidateInstanceContributionToHitGroupIndex();" )?; writeln!( self.out, - " ret.barycentrics = rq.CommittedTriangleBarycentrics();" + " ret.geometry_index = rq.CandidateGeometryIndex();" )?; writeln!( self.out, - " ret.front_face = rq.CommittedTriangleFrontFace();" + " ret.primitive_index = rq.CandidatePrimitiveIndex();" )?; - writeln!(self.out, " }}")?; writeln!( self.out, - " ret.object_to_world = rq.CommittedObjectToWorld4x3();" + " ret.object_to_world = rq.CandidateObjectToWorld4x3();" )?; writeln!( self.out, - " ret.world_to_object = rq.CommittedWorldToObject4x3();" + " ret.world_to_object = rq.CandidateWorldToObject4x3();" )?; writeln!(self.out, " return ret;")?; writeln!(self.out, "}}")?; diff --git a/naga/tests/out/hlsl/ray-query.hlsl b/naga/tests/out/hlsl/ray-query.hlsl index 43c9ee632e..2e1278db9d 100644 --- a/naga/tests/out/hlsl/ray-query.hlsl +++ b/naga/tests/out/hlsl/ray-query.hlsl @@ -122,21 +122,19 @@ RayIntersection GetCandidateIntersection(RayQuery rq) { CANDIDATE_TYPE kind = rq.CandidateType(); if (kind == CANDIDATE_NON_OPAQUE_TRIANGLE) { ret.kind = 1; + ret.t = rq.CandidateTriangleRayT(); + ret.barycentrics = rq.CandidateTriangleBarycentrics(); + ret.front_face = rq.CandidateTriangleFrontFace(); } else { ret.kind = 3; } - ret.t = rq.CommittedRayT(); - ret.instance_custom_index = rq.CommittedInstanceIndex(); - ret.instance_id = rq.CommittedInstanceID(); - ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex(); - ret.geometry_index = rq.CommittedGeometryIndex(); - ret.primitive_index = rq.CommittedPrimitiveIndex(); - if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) { - ret.barycentrics = rq.CommittedTriangleBarycentrics(); - ret.front_face = rq.CommittedTriangleFrontFace(); - } - ret.object_to_world = rq.CommittedObjectToWorld4x3(); - ret.world_to_object = rq.CommittedWorldToObject4x3(); + ret.instance_custom_index = rq.CandidateInstanceIndex(); + ret.instance_id = rq.CandidateInstanceID(); + ret.sbt_record_offset = rq.CandidateInstanceContributionToHitGroupIndex(); + ret.geometry_index = rq.CandidateGeometryIndex(); + ret.primitive_index = rq.CandidatePrimitiveIndex(); + ret.object_to_world = rq.CandidateObjectToWorld4x3(); + ret.world_to_object = rq.CandidateWorldToObject4x3(); return ret; } From def77b5518a95f40c278162097c871117fe9a9d3 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 15:16:30 +1300 Subject: [PATCH 32/43] swap the instance calls because have different names --- naga/src/back/hlsl/ray.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 95f18683cc..5c67f896ec 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -42,11 +42,11 @@ impl super::Writer<'_, W> { writeln!(self.out, " ret.t = rq.CommittedRayT();")?; writeln!( self.out, - " ret.instance_custom_index = rq.CommittedInstanceIndex();" + " ret.instance_custom_index = rq.CommittedInstanceID();" )?; writeln!( self.out, - " ret.instance_id = rq.CommittedInstanceID();" + " ret.instance_id = rq.CommittedInstanceIndex();" )?; writeln!( self.out, @@ -129,9 +129,9 @@ impl super::Writer<'_, W> { writeln!( self.out, - " ret.instance_custom_index = rq.CandidateInstanceIndex();" + " ret.instance_custom_index = rq.CandidateInstanceID();" )?; - writeln!(self.out, " ret.instance_id = rq.CandidateInstanceID();")?; + writeln!(self.out, " ret.instance_id = rq.CandidateInstanceIndex();")?; writeln!( self.out, " ret.sbt_record_offset = rq.CandidateInstanceContributionToHitGroupIndex();" From 93f700b47806cf922d6aadec60c026523d53f465 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 15:17:00 +1300 Subject: [PATCH 33/43] fmt --- naga/src/back/hlsl/ray.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/naga/src/back/hlsl/ray.rs b/naga/src/back/hlsl/ray.rs index 5c67f896ec..ab57f06a6c 100644 --- a/naga/src/back/hlsl/ray.rs +++ b/naga/src/back/hlsl/ray.rs @@ -131,7 +131,10 @@ impl super::Writer<'_, W> { self.out, " ret.instance_custom_index = rq.CandidateInstanceID();" )?; - writeln!(self.out, " ret.instance_id = rq.CandidateInstanceIndex();")?; + writeln!( + self.out, + " ret.instance_id = rq.CandidateInstanceIndex();" + )?; writeln!( self.out, " ret.sbt_record_offset = rq.CandidateInstanceContributionToHitGroupIndex();" From 31715f49f6f4a1d61b801ae2672c15a72dc54ec2 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 18 Dec 2024 15:19:53 +1300 Subject: [PATCH 34/43] regen snapshots --- naga/tests/out/hlsl/ray-query.hlsl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/naga/tests/out/hlsl/ray-query.hlsl b/naga/tests/out/hlsl/ray-query.hlsl index 2e1278db9d..9a0a2da1ce 100644 --- a/naga/tests/out/hlsl/ray-query.hlsl +++ b/naga/tests/out/hlsl/ray-query.hlsl @@ -64,8 +64,8 @@ RayIntersection GetCommittedIntersection(RayQuery rq) { ret.kind = rq.CommittedStatus(); if( rq.CommittedStatus() == COMMITTED_NOTHING) {} else { ret.t = rq.CommittedRayT(); - ret.instance_custom_index = rq.CommittedInstanceIndex(); - ret.instance_id = rq.CommittedInstanceID(); + ret.instance_custom_index = rq.CommittedInstanceID(); + ret.instance_id = rq.CommittedInstanceIndex(); ret.sbt_record_offset = rq.CommittedInstanceContributionToHitGroupIndex(); ret.geometry_index = rq.CommittedGeometryIndex(); ret.primitive_index = rq.CommittedPrimitiveIndex(); @@ -128,8 +128,8 @@ RayIntersection GetCandidateIntersection(RayQuery rq) { } else { ret.kind = 3; } - ret.instance_custom_index = rq.CandidateInstanceIndex(); - ret.instance_id = rq.CandidateInstanceID(); + ret.instance_custom_index = rq.CandidateInstanceID(); + ret.instance_id = rq.CandidateInstanceIndex(); ret.sbt_record_offset = rq.CandidateInstanceContributionToHitGroupIndex(); ret.geometry_index = rq.CandidateGeometryIndex(); ret.primitive_index = rq.CandidatePrimitiveIndex(); From 0aeed6577226952a015df5472346f6c1a6327162 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Sun, 22 Dec 2024 18:08:41 +1300 Subject: [PATCH 35/43] remove layout variable (all paths lead to the same value). Fix incorrect value. --- wgpu-hal/src/dx12/device.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index f3f670079d..ecf81b2666 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1916,10 +1916,9 @@ impl crate::Device for super::Device { ) -> crate::AccelerationStructureBuildSizes { let mut geometry_desc; let device5 = self.raw.cast::().unwrap(); - let (ty, layout, inputs0, num_desc) = match desc.entries { + let (ty, inputs0, num_desc) = match desc.entries { AccelerationStructureEntries::Instances(instances) => ( Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, - Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { InstanceDescs: 0, }, @@ -1959,7 +1958,6 @@ impl crate::Device for super::Device { } ( Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, - Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { pGeometryDescs: geometry_desc.as_ptr(), }, @@ -1970,7 +1968,7 @@ impl crate::Device for super::Device { geometry_desc = Vec::with_capacity(aabbs.len()); for aabb in aabbs { geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { - Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS, Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { @@ -1985,7 +1983,6 @@ impl crate::Device for super::Device { } ( Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, - Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { pGeometryDescs: geometry_desc.as_ptr(), }, @@ -1998,7 +1995,7 @@ impl crate::Device for super::Device { Type: ty, Flags: conv::map_acceleration_structure_build_flags(desc.flags, None), NumDescs: num_desc, - DescsLayout: layout, + DescsLayout: Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, Anonymous: inputs0, }; let mut info = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO::default(); From 9c54f4f7e9226b1191e38d292559233bfab63f50 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Tue, 24 Dec 2024 16:08:52 +1300 Subject: [PATCH 36/43] begin addressing comments --- wgpu-hal/src/dx12/adapter.rs | 4 +-- wgpu-hal/src/dx12/command.rs | 50 ++++++++++++++---------------- wgpu-hal/src/dx12/conv.rs | 29 ----------------- wgpu-hal/src/dx12/device.rs | 4 +-- wgpu-hal/src/dx12/suballocation.rs | 4 +-- wgpu-types/src/counters.rs | 4 +-- 6 files changed, 31 insertions(+), 64 deletions(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 8be12c5549..2a97b3cd45 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -549,8 +549,8 @@ impl super::Adapter { // Direct3D correctly bounds-checks all array accesses: // https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#18.6.8.2%20Device%20Memory%20Reads uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(), - raw_tlas_instance_size: 64, - ray_tracing_scratch_buffer_alignment: 256, + raw_tlas_instance_size: size_of::(), + ray_tracing_scratch_buffer_alignment: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT, }, downlevel, }, diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 5854c1a985..e083b12a0c 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1282,11 +1282,13 @@ impl crate::CommandEncoder for super::CommandEncoder { for descriptor in descriptors { // TODO: This is the same as getting build sizes apart from requiring buffers, should this be de-duped? let mut geometry_desc; - let (ty, layout, inputs0, num_desc) = match descriptor.entries { - AccelerationStructureEntries::Instances(instances) => ( - Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, - Direct3D12::D3D12_ELEMENTS_LAYOUT::default(), - Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + let ty; + let inputs0; + let num_desc; + match descriptor.entries { + AccelerationStructureEntries::Instances(instances) => { + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { InstanceDescs: unsafe { instances .buffer @@ -1295,9 +1297,9 @@ impl crate::CommandEncoder for super::CommandEncoder { .GetGPUVirtualAddress() + instances.offset as u64 }, - }, - instances.count, - ), + }; + num_desc = instances.count; + }, AccelerationStructureEntries::Triangles(triangles) => { geometry_desc = Vec::with_capacity(triangles.len()); for triangle in triangles { @@ -1317,9 +1319,9 @@ impl crate::CommandEncoder for super::CommandEncoder { .indices .as_ref() .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { - conv::map_index_format(indices.format) + auxil::dxgi::conv::map_index_format(indices.format) }), - VertexFormat: conv::map_acceleration_structure_vertex_format( + VertexFormat: auxil::dxgi::conv::map_vertex_format( triangle.vertex_format, ), IndexCount: triangle @@ -1355,14 +1357,11 @@ impl crate::CommandEncoder for super::CommandEncoder { }, }) } - ( - Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, - Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, - Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { - pGeometryDescs: geometry_desc.as_ptr(), - }, - geometry_desc.len() as u32, - ) + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }; + num_desc = geometry_desc.len() as u32; } AccelerationStructureEntries::AABBs(aabbs) => { geometry_desc = Vec::with_capacity(aabbs.len()); @@ -1387,14 +1386,11 @@ impl crate::CommandEncoder for super::CommandEncoder { }, }) } - ( - Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, - Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, - Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { - pGeometryDescs: geometry_desc.as_ptr(), - }, - geometry_desc.len() as u32, - ) + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }; + num_desc = geometry_desc.len() as u32; } }; let acceleration_structure_inputs = @@ -1405,7 +1401,7 @@ impl crate::CommandEncoder for super::CommandEncoder { Some(descriptor.mode), ), NumDescs: num_desc, - DescsLayout: layout, + DescsLayout: Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, Anonymous: inputs0, }; let desc = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC { diff --git a/wgpu-hal/src/dx12/conv.rs b/wgpu-hal/src/dx12/conv.rs index bc3565f4b8..a80045e6b1 100644 --- a/wgpu-hal/src/dx12/conv.rs +++ b/wgpu-hal/src/dx12/conv.rs @@ -398,32 +398,3 @@ pub(crate) fn map_acceleration_structure_geometry_flags( } d3d_flags } - -pub(crate) fn map_index_format(format: wgt::IndexFormat) -> Dxgi::Common::DXGI_FORMAT { - match format { - wgt::IndexFormat::Uint16 => Dxgi::Common::DXGI_FORMAT_R16_UINT, - wgt::IndexFormat::Uint32 => Dxgi::Common::DXGI_FORMAT_R32_UINT, - } -} - -pub(crate) fn map_acceleration_structure_vertex_format( - format: wgt::VertexFormat, -) -> Dxgi::Common::DXGI_FORMAT { - match format { - wgt::VertexFormat::Unorm8x2 => Dxgi::Common::DXGI_FORMAT_R8G8_UNORM, - wgt::VertexFormat::Unorm8x4 => Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM, - wgt::VertexFormat::Snorm8x2 => Dxgi::Common::DXGI_FORMAT_R8G8_SNORM, - wgt::VertexFormat::Snorm8x4 => Dxgi::Common::DXGI_FORMAT_R8G8B8A8_SNORM, - wgt::VertexFormat::Unorm16x2 => Dxgi::Common::DXGI_FORMAT_R16G16_UNORM, - wgt::VertexFormat::Unorm16x4 => Dxgi::Common::DXGI_FORMAT_R16G16B16A16_UNORM, - wgt::VertexFormat::Snorm16x2 => Dxgi::Common::DXGI_FORMAT_R16G16_SNORM, - wgt::VertexFormat::Snorm16x4 => Dxgi::Common::DXGI_FORMAT_R16G16B16A16_SNORM, - wgt::VertexFormat::Float16x2 => Dxgi::Common::DXGI_FORMAT_R16G16_FLOAT, - wgt::VertexFormat::Float16x4 => Dxgi::Common::DXGI_FORMAT_R16G16B16A16_FLOAT, - wgt::VertexFormat::Float32x2 => Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT, - wgt::VertexFormat::Float32x3 => Dxgi::Common::DXGI_FORMAT_R32G32B32_FLOAT, - wgt::VertexFormat::Unorm10_10_10_2 => Dxgi::Common::DXGI_FORMAT_R10G10B10A2_UNORM, - // no other formats are supported - _ => unimplemented!("disallowed vertex format"), - } -} diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index ecf81b2666..533d3631ee 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1937,9 +1937,9 @@ impl crate::Device for super::Device { .indices .as_ref() .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { - conv::map_index_format(indices.format) + auxil::dxgi::conv::map_index_format(indices.format) }), - VertexFormat: conv::map_acceleration_structure_vertex_format( + VertexFormat: auxil::dxgi::conv::map_vertex_format( triangle.vertex_format, ), IndexCount: triangle diff --git a/wgpu-hal/src/dx12/suballocation.rs b/wgpu-hal/src/dx12/suballocation.rs index 13aab6dcdc..2b0cbf8a47 100644 --- a/wgpu-hal/src/dx12/suballocation.rs +++ b/wgpu-hal/src/dx12/suballocation.rs @@ -243,7 +243,7 @@ pub(crate) fn free_acceleration_structure_allocation( match allocator.lock().allocator.free(allocation.allocation) { Ok(_) => (), // TODO: Don't panic here - Err(e) => panic!("Failed to destroy dx12 buffer, {e}"), + Err(e) => panic!("Failed to destroy dx12 acceleration structure, {e}"), }; } @@ -401,7 +401,7 @@ pub(crate) fn create_committed_acceleration_structure_resource( &mut resource, ) } - .into_device_result("Committed buffer creation")?; + .into_device_result("Committed acceleration structure creation")?; resource.ok_or(crate::DeviceError::Unexpected) } diff --git a/wgpu-types/src/counters.rs b/wgpu-types/src/counters.rs index 8077c0f345..49e6219ddd 100644 --- a/wgpu-types/src/counters.rs +++ b/wgpu-types/src/counters.rs @@ -125,10 +125,10 @@ pub struct HalCounters { pub buffer_memory: InternalCounter, /// Amount of allocated gpu memory attributed to textures, in bytes. pub texture_memory: InternalCounter, + /// Amount of allocated gpu memory attributed to acceleration structures, in bytes. + pub acceleration_structure_memory: InternalCounter, /// Number of gpu memory allocations. pub memory_allocations: InternalCounter, - /// Amount of allocated gpu memory attributed to buffers, in bytes. - pub acceleration_structure_memory: InternalCounter, } /// `wgpu-core`'s internal counters. From 0665c938bc1550a00b4e121465ed622185cf045e Mon Sep 17 00:00:00 2001 From: Vecvec Date: Tue, 24 Dec 2024 16:41:38 +1300 Subject: [PATCH 37/43] address comments --- wgpu-hal/src/dx12/adapter.rs | 3 +- wgpu-hal/src/dx12/command.rs | 178 ++++++++++++++++++----------------- wgpu-hal/src/dx12/conv.rs | 2 +- wgpu-hal/src/dx12/device.rs | 61 ++++++------ 4 files changed, 127 insertions(+), 117 deletions(-) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 2a97b3cd45..5edbc5508f 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -550,7 +550,8 @@ impl super::Adapter { // https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#18.6.8.2%20Device%20Memory%20Reads uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(), raw_tlas_instance_size: size_of::(), - ray_tracing_scratch_buffer_alignment: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT, + ray_tracing_scratch_buffer_alignment: + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT, }, downlevel, }, diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index e083b12a0c..4e586acec9 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1287,73 +1287,72 @@ impl crate::CommandEncoder for super::CommandEncoder { let num_desc; match descriptor.entries { AccelerationStructureEntries::Instances(instances) => { + let desc_address = unsafe { + instances + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + } + instances.offset as u64; ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { - InstanceDescs: unsafe { - instances - .buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + instances.offset as u64 - }, + InstanceDescs: desc_address, }; num_desc = instances.count; - }, + } AccelerationStructureEntries::Triangles(triangles) => { geometry_desc = Vec::with_capacity(triangles.len()); for triangle in triangles { + let transform_address = + triangle.transform.as_ref().map_or(0, |transform| unsafe { + transform.buffer.resource.GetGPUVirtualAddress() + + transform.offset as u64 + }); + let index_format = triangle + .indices + .as_ref() + .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { + auxil::dxgi::conv::map_index_format(indices.format) + }); + let vertex_format = + auxil::dxgi::conv::map_vertex_format(triangle.vertex_format); + let index_count = + triangle.indices.as_ref().map_or(0, |indices| indices.count); + let index_address = triangle.indices.as_ref().map_or(0, |indices| unsafe { + indices + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + indices.offset as u64 + }); + let vertex_address = unsafe { + triangle + .vertex_buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (triangle.first_vertex as u64 * triangle.vertex_stride) + }; + + let triangle_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { + Transform3x4: transform_address, + IndexFormat: index_format, + VertexFormat: vertex_format, + IndexCount: index_count, + VertexCount: triangle.vertex_count, + IndexBuffer: index_address, + VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: vertex_address, + StrideInBytes: triangle.vertex_stride, + }, + }; + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { - Triangles: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { - Transform3x4: triangle.transform.as_ref().map_or( - 0, - |transform| unsafe { - transform.buffer.resource.GetGPUVirtualAddress() - + transform.offset as u64 - }, - ), - IndexFormat: triangle - .indices - .as_ref() - .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { - auxil::dxgi::conv::map_index_format(indices.format) - }), - VertexFormat: auxil::dxgi::conv::map_vertex_format( - triangle.vertex_format, - ), - IndexCount: triangle - .indices - .as_ref() - .map_or(0, |indices| indices.count), - VertexCount: triangle.vertex_count, - IndexBuffer: triangle.indices.as_ref().map_or( - 0, - |indices| unsafe { - indices - .buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + indices.offset as u64 - }, - ), - VertexBuffer: - Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: unsafe { - triangle - .vertex_buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + (triangle.first_vertex as u64 - * triangle.vertex_stride) - }, - StrideInBytes: triangle.vertex_stride, - }, - }, + Triangles: triangle_desc, }, }) } @@ -1366,23 +1365,27 @@ impl crate::CommandEncoder for super::CommandEncoder { AccelerationStructureEntries::AABBs(aabbs) => { geometry_desc = Vec::with_capacity(aabbs.len()); for aabb in aabbs { + let aabb_address = unsafe { + aabb.buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (aabb.offset as u64 * aabb.stride) + }; + + let aabb_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { + AABBCount: aabb.count as u64, + AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: aabb_address, + StrideInBytes: aabb.stride, + }, + }; + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS, Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { - AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { - AABBCount: aabb.count as u64, - AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: unsafe { - aabb.buffer - .expect("needs buffer to build") - .resource - .GetGPUVirtualAddress() - + (aabb.offset as u64 * aabb.stride) - }, - StrideInBytes: aabb.stride, - }, - }, + AABBs: aabb_desc, }, }) } @@ -1404,24 +1407,29 @@ impl crate::CommandEncoder for super::CommandEncoder { DescsLayout: Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, Anonymous: inputs0, }; + + let dst_acceleration_structure_address = unsafe { + descriptor + .destination_acceleration_structure + .resource + .GetGPUVirtualAddress() + }; + let src_acceleration_structure_address = descriptor + .source_acceleration_structure + .as_ref() + .map_or(0, |source| unsafe { + source.resource.GetGPUVirtualAddress() + }); + let scratch_address = unsafe { + descriptor.scratch_buffer.resource.GetGPUVirtualAddress() + + descriptor.scratch_buffer_offset + }; + let desc = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC { - DestAccelerationStructureData: unsafe { - descriptor - .destination_acceleration_structure - .resource - .GetGPUVirtualAddress() - }, + DestAccelerationStructureData: dst_acceleration_structure_address, Inputs: acceleration_structure_inputs, - SourceAccelerationStructureData: descriptor - .source_acceleration_structure - .as_ref() - .map_or(0, |source| unsafe { - source.resource.GetGPUVirtualAddress() - }), - ScratchAccelerationStructureData: unsafe { - descriptor.scratch_buffer.resource.GetGPUVirtualAddress() - + descriptor.scratch_buffer_offset - }, + SourceAccelerationStructureData: src_acceleration_structure_address, + ScratchAccelerationStructureData: scratch_address, }; unsafe { list.BuildRaytracingAccelerationStructure(&desc, None) }; } diff --git a/wgpu-hal/src/dx12/conv.rs b/wgpu-hal/src/dx12/conv.rs index a80045e6b1..5117378942 100644 --- a/wgpu-hal/src/dx12/conv.rs +++ b/wgpu-hal/src/dx12/conv.rs @@ -1,4 +1,4 @@ -use windows::Win32::Graphics::{Direct3D, Direct3D12, Dxgi}; +use windows::Win32::Graphics::{Direct3D, Direct3D12}; pub fn map_buffer_usage_to_resource_flags( usage: crate::BufferUses, diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 533d3631ee..0b0bb00152 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1927,32 +1927,32 @@ impl crate::Device for super::Device { AccelerationStructureEntries::Triangles(triangles) => { geometry_desc = Vec::with_capacity(triangles.len()); for triangle in triangles { + let index_format = triangle + .indices + .as_ref() + .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { + auxil::dxgi::conv::map_index_format(indices.format) + }); + let index_count = triangle.indices.as_ref().map_or(0, |indices| indices.count); + + let triangle_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { + Transform3x4: 0, + IndexFormat: index_format, + VertexFormat: auxil::dxgi::conv::map_vertex_format(triangle.vertex_format), + IndexCount: index_count, + VertexCount: triangle.vertex_count, + IndexBuffer: 0, + VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: 0, + StrideInBytes: triangle.vertex_stride, + }, + }; + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { - Triangles: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { - Transform3x4: 0, - IndexFormat: triangle - .indices - .as_ref() - .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { - auxil::dxgi::conv::map_index_format(indices.format) - }), - VertexFormat: auxil::dxgi::conv::map_vertex_format( - triangle.vertex_format, - ), - IndexCount: triangle - .indices - .as_ref() - .map_or(0, |indices| indices.count), - VertexCount: triangle.vertex_count, - IndexBuffer: 0, - VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: 0, - StrideInBytes: triangle.vertex_stride, - }, - }, + Triangles: triangle_desc, }, }) } @@ -1967,17 +1967,18 @@ impl crate::Device for super::Device { AccelerationStructureEntries::AABBs(aabbs) => { geometry_desc = Vec::with_capacity(aabbs.len()); for aabb in aabbs { + let aabb_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { + AABBCount: aabb.count as u64, + AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: 0, + StrideInBytes: aabb.stride, + }, + }; geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS, Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { - AABBs: Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { - AABBCount: aabb.count as u64, - AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { - StartAddress: 0, - StrideInBytes: aabb.stride, - }, - }, + AABBs: aabb_desc, }, }) } @@ -2119,7 +2120,7 @@ impl crate::Device for super::Device { }; let temp: *const _ = &temp; unsafe { - slice::from_raw_parts::( + slice::from_raw_parts( temp.cast::(), size_of::(), ) From c5fdde44b8636177cd4bdf7389a8aa520b9d788b Mon Sep 17 00:00:00 2001 From: Vecvec Date: Fri, 27 Dec 2024 07:36:19 +1300 Subject: [PATCH 38/43] use rgba8 --- wgpu-hal/examples/ray-traced-triangle/main.rs | 8 ++++---- wgpu-hal/examples/ray-traced-triangle/shader.wgsl | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/wgpu-hal/examples/ray-traced-triangle/main.rs b/wgpu-hal/examples/ray-traced-triangle/main.rs index d6aab77efd..8185870ec0 100644 --- a/wgpu-hal/examples/ray-traced-triangle/main.rs +++ b/wgpu-hal/examples/ray-traced-triangle/main.rs @@ -284,9 +284,9 @@ impl Example { dbg!(&surface_caps.formats); let surface_format = if surface_caps .formats - .contains(&wgt::TextureFormat::Bgra8Unorm) + .contains(&wgt::TextureFormat::Rgba8Unorm) { - wgt::TextureFormat::Bgra8Unorm + wgt::TextureFormat::Rgba8Unorm } else { *surface_caps.formats.first().unwrap() }; @@ -581,10 +581,10 @@ impl Example { mip_level_count: 1, sample_count: 1, dimension: wgt::TextureDimension::D2, - format: wgt::TextureFormat::Bgra8Unorm, + format: wgt::TextureFormat::Rgba8Unorm, usage: hal::TextureUses::STORAGE_READ_WRITE | hal::TextureUses::COPY_SRC, memory_flags: hal::MemoryFlags::empty(), - view_formats: vec![wgt::TextureFormat::Bgra8Unorm], + view_formats: vec![wgt::TextureFormat::Rgba8Unorm], }; let texture = unsafe { device.create_texture(&texture_desc).unwrap() }; diff --git a/wgpu-hal/examples/ray-traced-triangle/shader.wgsl b/wgpu-hal/examples/ray-traced-triangle/shader.wgsl index 2cf3d219e0..8d9e475e3e 100644 --- a/wgpu-hal/examples/ray-traced-triangle/shader.wgsl +++ b/wgpu-hal/examples/ray-traced-triangle/shader.wgsl @@ -6,7 +6,7 @@ struct Uniforms { var uniforms: Uniforms; @group(0) @binding(1) -var output: texture_storage_2d; +var output: texture_storage_2d; @group(0) @binding(2) var acc_struct: acceleration_structure; From 06fec21a620e97a5568f602fd36cc9e93606cd7a Mon Sep 17 00:00:00 2001 From: Vecvec Date: Mon, 30 Dec 2024 09:32:28 +0000 Subject: [PATCH 39/43] clarify that DXR means DirectX Ray-tracing --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 179f1f9e89..ab3648b779 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -149,7 +149,10 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148] - Return submission index in `map_async` and `on_submitted_work_done` to track down completion of async callbacks. By @eliemichel in [#6360](https://github.com/gfx-rs/wgpu/pull/6360). - Move raytracing alignments into HAL instead of in core. By @Vecvec in [#6563](https://github.com/gfx-rs/wgpu/pull/6563). - Allow for statically linking DXC rather than including separate `.dll` files. By @DouglasDwyer in [#6574](https://github.com/gfx-rs/wgpu/pull/6574). -- Support DXR in wgpu-hal. By @Vecvec in [#6777](https://github.com/gfx-rs/wgpu/pull/6777) + +#### D3D12 + +- Support DXR (DirectX Ray-tracing) in wgpu-hal. By @Vecvec in [#6777](https://github.com/gfx-rs/wgpu/pull/6777) ### Changes From 261941b1fe8a6e131b024a13a4fe0df1c289fccf Mon Sep 17 00:00:00 2001 From: Vecvec Date: Wed, 8 Jan 2025 08:15:57 +0000 Subject: [PATCH 40/43] Address feedback. --- wgpu-hal/src/dx12/device.rs | 43 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index c0979ce29b..b9a825845a 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1923,14 +1923,17 @@ impl crate::Device for super::Device { ) -> crate::AccelerationStructureBuildSizes { let mut geometry_desc; let device5 = self.raw.cast::().unwrap(); - let (ty, inputs0, num_desc) = match desc.entries { - AccelerationStructureEntries::Instances(instances) => ( - Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL, - Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + let ty; + let inputs0; + let num_desc; + match desc.entries { + AccelerationStructureEntries::Instances(instances) => { + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { InstanceDescs: 0, - }, - instances.count, - ), + }; + num_desc = instances.count; + } AccelerationStructureEntries::Triangles(triangles) => { geometry_desc = Vec::with_capacity(triangles.len()); for triangle in triangles { @@ -1963,13 +1966,11 @@ impl crate::Device for super::Device { }, }) } - ( - Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, - Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { - pGeometryDescs: geometry_desc.as_ptr(), - }, - geometry_desc.len() as u32, - ) + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }; + num_desc = geometry_desc.len() as u32; } AccelerationStructureEntries::AABBs(aabbs) => { geometry_desc = Vec::with_capacity(aabbs.len()); @@ -1989,13 +1990,11 @@ impl crate::Device for super::Device { }, }) } - ( - Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL, - Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { - pGeometryDescs: geometry_desc.as_ptr(), - }, - geometry_desc.len() as u32, - ) + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }; + num_desc = geometry_desc.len() as u32; } }; let acceleration_structure_inputs = @@ -2072,7 +2071,7 @@ impl crate::Device for super::Device { mut acceleration_structure: super::AccelerationStructure, ) { if let Some(alloc) = acceleration_structure.allocation.take() { - // Resource should be dropped before free suballocation + // Resource should be dropped before suballocation is freed drop(acceleration_structure); super::suballocation::free_acceleration_structure_allocation( From eb14b300d52523c6be61271541e8a2327407486c Mon Sep 17 00:00:00 2001 From: Vecvec Date: Tue, 14 Jan 2025 07:39:16 +0000 Subject: [PATCH 41/43] check that it only writes intersection getters once. --- naga/src/back/hlsl/help.rs | 10 ++++++++-- naga/src/back/hlsl/mod.rs | 1 + naga/src/back/hlsl/writer.rs | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/naga/src/back/hlsl/help.rs b/naga/src/back/hlsl/help.rs index 7c0750c2a5..f87f864041 100644 --- a/naga/src/back/hlsl/help.rs +++ b/naga/src/back/hlsl/help.rs @@ -869,9 +869,15 @@ impl super::Writer<'_, W> { } crate::Expression::RayQueryGetIntersection { committed, .. } => { if committed { - self.write_committed_intersection_function(module)?; + if !self.written_committed_intersection { + self.write_committed_intersection_function(module)?; + self.written_committed_intersection = true; + } } else { - self.write_candidate_intersection_function(module)?; + if !self.written_candidate_intersection { + self.write_candidate_intersection_function(module)?; + self.written_candidate_intersection = true; + } } } _ => {} diff --git a/naga/src/back/hlsl/mod.rs b/naga/src/back/hlsl/mod.rs index 9f812523fc..dcce866bac 100644 --- a/naga/src/back/hlsl/mod.rs +++ b/naga/src/back/hlsl/mod.rs @@ -333,6 +333,7 @@ pub struct Writer<'a, W> { named_expressions: crate::NamedExpressions, wrapped: Wrapped, written_committed_intersection: bool, + written_candidate_intersection: bool, continue_ctx: back::continue_forward::ContinueCtx, /// A reference to some part of a global variable, lowered to a series of diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index 78dec5052f..b5df135766 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -105,6 +105,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { named_expressions: crate::NamedExpressions::default(), wrapped: super::Wrapped::default(), written_committed_intersection: false, + written_candidate_intersection: false, continue_ctx: back::continue_forward::ContinueCtx::default(), temp_access_chain: Vec::new(), need_bake_expressions: Default::default(), @@ -125,6 +126,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { self.named_expressions.clear(); self.wrapped.clear(); self.written_committed_intersection = false; + self.written_candidate_intersection = false; self.continue_ctx.clear(); self.need_bake_expressions.clear(); } From db8a6a8aff3e110b728ac6cfca2a72b2a7b0d700 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Tue, 14 Jan 2025 07:52:59 +0000 Subject: [PATCH 42/43] address uses feedback --- wgpu-hal/src/dx12/command.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 4e586acec9..4584ff9e09 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -5,8 +5,7 @@ use crate::{ AccelerationStructureEntries, }; use std::{mem, ops::Range}; -use windows::Win32::Graphics::Dxgi; -use windows::Win32::{Foundation, Graphics::Direct3D12}; +use windows::Win32::{Foundation, Graphics::{Direct3D12, Dxgi}}; use windows_core::Interface; fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> Direct3D12::D3D12_BOX { From 6c83651fa5e1f6147a6b74fb87d481600b33cf24 Mon Sep 17 00:00:00 2001 From: Vecvec Date: Tue, 14 Jan 2025 08:05:40 +0000 Subject: [PATCH 43/43] Clippy & format. --- naga/src/back/hlsl/help.rs | 8 +++----- wgpu-hal/src/dx12/command.rs | 5 ++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/naga/src/back/hlsl/help.rs b/naga/src/back/hlsl/help.rs index f87f864041..f63c9d2cfd 100644 --- a/naga/src/back/hlsl/help.rs +++ b/naga/src/back/hlsl/help.rs @@ -873,11 +873,9 @@ impl super::Writer<'_, W> { self.write_committed_intersection_function(module)?; self.written_committed_intersection = true; } - } else { - if !self.written_candidate_intersection { - self.write_candidate_intersection_function(module)?; - self.written_candidate_intersection = true; - } + } else if !self.written_candidate_intersection { + self.write_candidate_intersection_function(module)?; + self.written_candidate_intersection = true; } } _ => {} diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 4584ff9e09..99cee37373 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -5,7 +5,10 @@ use crate::{ AccelerationStructureEntries, }; use std::{mem, ops::Range}; -use windows::Win32::{Foundation, Graphics::{Direct3D12, Dxgi}}; +use windows::Win32::{ + Foundation, + Graphics::{Direct3D12, Dxgi}, +}; use windows_core::Interface; fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> Direct3D12::D3D12_BOX {