From 92c9836985f3f451c4c8c9d3333aaf1ef8fbba57 Mon Sep 17 00:00:00 2001 From: chyyran Date: Sat, 21 Jan 2023 15:34:13 -0500 Subject: [PATCH] d3d12: try to do desc heaps --- .../src/d3d12_primitives.rs | 52 ++++++++++++++++++ librashader-runtime-d3d12/src/error.rs | 3 + librashader-runtime-d3d12/src/filter_chain.rs | 55 +++++++++++++++++++ librashader-runtime-d3d12/src/samplers.rs | 8 +++ 4 files changed, 118 insertions(+) create mode 100644 librashader-runtime-d3d12/src/d3d12_primitives.rs create mode 100644 librashader-runtime-d3d12/src/error.rs create mode 100644 librashader-runtime-d3d12/src/filter_chain.rs create mode 100644 librashader-runtime-d3d12/src/samplers.rs diff --git a/librashader-runtime-d3d12/src/d3d12_primitives.rs b/librashader-runtime-d3d12/src/d3d12_primitives.rs new file mode 100644 index 0000000..0fee970 --- /dev/null +++ b/librashader-runtime-d3d12/src/d3d12_primitives.rs @@ -0,0 +1,52 @@ +use std::ptr::NonNull; +use windows::Win32::Graphics::Direct3D12::{D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_DESCRIPTOR_HEAP_DESC, D3D12_GPU_DESCRIPTOR_HANDLE, ID3D12DescriptorHeap, ID3D12Device}; +use crate::error; + +pub struct D3D12DescriptorHeap { + heap: ID3D12DescriptorHeap, + desc: D3D12_DESCRIPTOR_HEAP_DESC, + cpu_handle: D3D12_CPU_DESCRIPTOR_HANDLE, + gpu_handle: D3D12_GPU_DESCRIPTOR_HANDLE, + alignment: u32, + map: Box<[bool]>, + start: usize +} + +impl D3D12DescriptorHeap { + pub fn new(device: &ID3D12Device, desc: D3D12_DESCRIPTOR_HEAP_DESC) -> error::Result { + unsafe { + let heap: ID3D12DescriptorHeap = device.CreateDescriptorHeap(&desc)?; + let cpu_handle = heap.GetCPUDescriptorHandleForHeapStart(); + let gpu_handle = heap.GetGPUDescriptorHandleForHeapStart(); + let alignment = device.GetDescriptorHandleIncrementSize(desc.Type); + let mut map = Vec::new(); + map.resize(desc.NumDescriptors as usize, false); + + Ok(D3D12DescriptorHeap { + heap, + desc, + cpu_handle, + gpu_handle, + alignment, + map: Box::new([]), + start: 0, + }) + } + } + + pub fn allocate_slot(&mut self) -> error::Result { + let mut handle = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }; + + for i in self.start..self.desc.NumDescriptors as usize { + if !self.map[i] { + self.map[i] = true; + handle.ptr = self.cpu_handle.ptr + (i * self.alignment) as u64; + self.start = i + 1; + return Ok(handle); + } + } + + todo!("error need to fail"); + } + +} \ No newline at end of file diff --git a/librashader-runtime-d3d12/src/error.rs b/librashader-runtime-d3d12/src/error.rs new file mode 100644 index 0000000..fa138cb --- /dev/null +++ b/librashader-runtime-d3d12/src/error.rs @@ -0,0 +1,3 @@ +use std::error::Error; + +pub type Result = std::result::Result>; diff --git a/librashader-runtime-d3d12/src/filter_chain.rs b/librashader-runtime-d3d12/src/filter_chain.rs new file mode 100644 index 0000000..0dcacb9 --- /dev/null +++ b/librashader-runtime-d3d12/src/filter_chain.rs @@ -0,0 +1,55 @@ +use std::error::Error; +use std::path::Path; +use rustc_hash::FxHashMap; +use windows::Win32::Graphics::Direct3D12::ID3D12Device; +use librashader_presets::ShaderPreset; +use librashader_reflect::back::targets::HLSL; +use librashader_reflect::front::GlslangCompilation; +use librashader_reflect::reflect::presets::CompilePresetTarget; +use crate::error; + +pub struct FilterChainD3D12 { + pub(crate) common: FilterCommon, + // pub(crate) passes: Vec, + // pub(crate) output_framebuffers: Box<[OwnedFramebuffer]>, + // pub(crate) feedback_framebuffers: Box<[OwnedFramebuffer]>, + // pub(crate) history_framebuffers: VecDeque, + // pub(crate) draw_quad: DrawQuad, +} + +pub(crate) struct FilterCommon { + pub(crate) d3d12: ID3D12Device, + // pub(crate) luts: FxHashMap, + // pub samplers: SamplerSet, + // pub output_textures: Box<[Option]>, + // pub feedback_textures: Box<[Option]>, + // pub history_textures: Box<[Option]>, + // pub config: FilterMutable, + // pub disable_mipmaps: bool, +} + +impl FilterChainD3D12 { + /// Load the shader preset at the given path into a filter chain. + pub fn load_from_path( + path: impl AsRef, + options: Option<&()>, + ) -> error::Result { + // load passes from preset + let preset = ShaderPreset::try_parse(path)?; + Self::load_from_preset(preset, options) + } + + /// Load a filter chain from a pre-parsed `ShaderPreset`. + pub fn load_from_preset( + preset: ShaderPreset, + options: Option<&()>, + ) -> error::Result { + let (passes, semantics) = HLSL::compile_preset_passes::< + GlslangCompilation, + Box, + >(preset.shaders, &preset.textures)?; + + + todo!() + } +} \ No newline at end of file diff --git a/librashader-runtime-d3d12/src/samplers.rs b/librashader-runtime-d3d12/src/samplers.rs new file mode 100644 index 0000000..6d387af --- /dev/null +++ b/librashader-runtime-d3d12/src/samplers.rs @@ -0,0 +1,8 @@ +use rustc_hash::FxHashMap; +use windows::Win32::Graphics::Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE; +use librashader_common::{FilterMode, WrapMode}; + +pub struct SamplerSet { + samplers: FxHashMap<(WrapMode, FilterMode), D3D12_GPU_DESCRIPTOR_HANDLE>, + heap: D3D12Descriptor_heap +}