diff --git a/src/ComputeSharp.Graphics/Buffers/Enums/TextureType.cs b/src/ComputeSharp.Graphics/Buffers/Enums/TextureType.cs new file mode 100644 index 00000000..c6a79432 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Enums/TextureType.cs @@ -0,0 +1,20 @@ +namespace ComputeSharp.Graphics.Buffers.Enums +{ + /// + /// An that indicates the type of a given HLSL texture. + /// + internal enum TextureType + { + /// + /// A readonly texture, that can store arbitrary arrays of supported values and can only be read from by the GPU. + /// This is mapped to a in . + /// + ReadOnly, + + /// + /// A read write buffer, that can store arbitrary arrays with both read and write access for the GPU. + /// This is mapped to a in . + /// + ReadWrite + } +} diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 8cddb43d..e98ffde8 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -11,6 +11,7 @@ using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_FLAGS; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; using static TerraFX.Interop.D3D12_FENCE_FLAGS; using static TerraFX.Interop.D3D12_MEMORY_POOL; +using static TerraFX.Interop.D3D12_HEAP_FLAGS; using static TerraFX.Interop.D3D12_HEAP_TYPE; using static TerraFX.Interop.D3D12_RESOURCE_FLAGS; using static TerraFX.Interop.D3D12_RESOURCE_STATES; @@ -88,7 +89,7 @@ namespace ComputeSharp.Graphics.Extensions d3d12device.CreateDescriptorHeap( &d3d12DescriptorHeapDesc, - FX.__uuidof< ID3D12DescriptorHeap>(), + FX.__uuidof(), d3d12DescriptorHeap.GetVoidAddressOf()).Assert(); return d3d12DescriptorHeap.Move(); @@ -130,7 +131,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE, + D3D12_HEAP_FLAG_NONE, &d3D12ResourceDescription, d3D12ResourceStates, null, @@ -140,6 +141,51 @@ namespace ComputeSharp.Graphics.Extensions return d3D12Resource.Move(); } + /// + /// Creates a committed resource for a given 2D texture type. + /// + /// The instance in use. + /// The texture type currently in use. + /// The value to use. + /// The height of the texture resource. + /// The width of the texture resource. + /// An reference for the current texture. + public static ComPtr CreateCommittedResource( + this ref ID3D12Device d3D12Device, + TextureType textureType, + DXGI_FORMAT dxgiFormat, + uint height, + uint width) + { + D3D12_RESOURCE_FLAGS d3D12ResourceFlags = textureType switch + { + TextureType.ReadOnly => D3D12_RESOURCE_FLAG_NONE, + TextureType.ReadWrite => D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + _ => ThrowHelper.ThrowArgumentException() + }; + + using ComPtr d3D12Resource = default; + + D3D12_HEAP_PROPERTIES d3D12HeapProperties; + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + d3D12HeapProperties.CreationNodeMask = 1; + d3D12HeapProperties.VisibleNodeMask = 1; + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(dxgiFormat, width, height, flags: d3D12ResourceFlags); + + d3D12Device.CreateCommittedResource( + &d3D12HeapProperties, + D3D12_HEAP_FLAG_NONE, + &d3D12ResourceDescription, + D3D12_RESOURCE_STATE_COMMON, + null, + FX.__uuidof(), + d3D12Resource.GetVoidAddressOf()).Assert(); + + return d3D12Resource.Move(); + } + /// /// Creates a view for a constant buffer. /// @@ -187,7 +233,28 @@ namespace ComputeSharp.Graphics.Extensions } /// - /// Creates a view for a buffer than be both read and written to. + /// Creates a view for a readonly texture. + /// + /// The target instance in use. + /// The to create a view for. + /// The value to use. + /// The instance for the current resource. + public static void CreateShaderResourceView( + this ref ID3D12Device d3d12Device, + ID3D12Resource* d3d12resource, + DXGI_FORMAT dxgiFormat, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) + { + D3D12_SHADER_RESOURCE_VIEW_DESC d3D12ShaderResourceViewDescription = default; + d3D12ShaderResourceViewDescription.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + d3D12ShaderResourceViewDescription.Format = dxgiFormat; + d3D12ShaderResourceViewDescription.Shader4ComponentMapping = FX.D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + + d3d12Device.CreateShaderResourceView(d3d12resource, &d3D12ShaderResourceViewDescription, d3D12CpuDescriptorHandle); + } + + /// + /// Creates a view for a buffer that can be both read and written to. /// /// The target instance in use. /// The to create a view for. @@ -209,6 +276,26 @@ namespace ComputeSharp.Graphics.Extensions d3d12Device.CreateUnorderedAccessView(d3d12resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle); } + /// + /// Creates a view for a texture that can be both read and written to. + /// + /// The target instance in use. + /// The to create a view for. + /// The value to use. + /// The instance for the current resource. + public static void CreateUnorderedAccessView( + this ref ID3D12Device d3d12Device, + ID3D12Resource* d3d12resource, + DXGI_FORMAT dxgiFormat, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) + { + D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default; + d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; + d3D12UnorderedAccessViewDescription.Format = dxgiFormat; + + d3d12Device.CreateUnorderedAccessView(d3d12resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle); + } + /// /// Creates a new for a given device. ///