dx12: cleanup and micro-opts

This commit is contained in:
chyyran 2023-02-05 21:49:55 -05:00
parent 621d1da3c2
commit f077f86bad
7 changed files with 67 additions and 43 deletions

48
Cargo.lock generated
View file

@ -101,6 +101,18 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]] [[package]]
name = "block" name = "block"
version = "0.1.6" version = "0.1.6"
@ -554,6 +566,12 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]] [[package]]
name = "fxhash" name = "fxhash"
version = "0.2.1" version = "0.2.1"
@ -885,7 +903,7 @@ name = "librashader-runtime-d3d12"
version = "0.1.0-beta.8" version = "0.1.0-beta.8"
dependencies = [ dependencies = [
"array-init", "array-init",
"bit-set", "bitvec",
"bytemuck", "bytemuck",
"gfx-maths", "gfx-maths",
"librashader-common", "librashader-common",
@ -897,6 +915,7 @@ dependencies = [
"rayon", "rayon",
"rustc-hash", "rustc-hash",
"thiserror", "thiserror",
"widestring",
"windows", "windows",
] ]
@ -1397,6 +1416,12 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]] [[package]]
name = "raw-window-handle" name = "raw-window-handle"
version = "0.4.3" version = "0.4.3"
@ -1678,6 +1703,12 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.38" version = "1.0.38"
@ -1935,6 +1966,12 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "widestring"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "653f141f39ec16bba3c5abe400a0c60da7468261cc2cbf36805022876bc721a8"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@ -2132,6 +2169,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "wyz"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
dependencies = [
"tap",
]
[[package]] [[package]]
name = "x11-dl" name = "x11-dl"
version = "2.21.0" version = "2.21.0"

View file

@ -23,7 +23,8 @@ spirv_cross = { package = "librashader-spirv-cross", version = "0.23" }
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
bytemuck = { version = "1.12.3", features = ["derive"] } bytemuck = { version = "1.12.3", features = ["derive"] }
array-init = "2.1.0" array-init = "2.1.0"
bit-set = "0.5.3" bitvec = "1.0.1"
widestring = "1.0.2"
rayon = "1.6.1" rayon = "1.6.1"

View file

@ -3,6 +3,8 @@ use std::cell::RefCell;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ops::Deref; use std::ops::Deref;
use std::rc::Rc; use std::rc::Rc;
use bitvec::{bits, bitvec};
use bitvec::boxed::BitBox;
use crate::error::FilterChainError; use crate::error::FilterChainError;
use windows::Win32::Graphics::Direct3D12::{ use windows::Win32::Graphics::Direct3D12::{
@ -154,13 +156,7 @@ struct D3D12DescriptorHeapInner {
handle_size: usize, handle_size: usize,
start: usize, start: usize,
num_descriptors: usize, num_descriptors: usize,
// Bit flag representation of available handles in the heap. map: BitBox,
//
// 0 - Occupied
// 1 - free
// todo: actually use a bitset here.
map: Box<[bool]>,
} }
pub struct D3D12DescriptorHeap<T>(Rc<RefCell<D3D12DescriptorHeapInner>>, PhantomData<T>); pub struct D3D12DescriptorHeap<T>(Rc<RefCell<D3D12DescriptorHeapInner>>, PhantomData<T>);
@ -197,7 +193,7 @@ impl<T> D3D12DescriptorHeap<T> {
handle_size: device.GetDescriptorHandleIncrementSize(desc.Type) as usize, handle_size: device.GetDescriptorHandleIncrementSize(desc.Type) as usize,
start: 0, start: 0,
num_descriptors: desc.NumDescriptors as usize, num_descriptors: desc.NumDescriptors as usize,
map: vec![false; desc.NumDescriptors as usize].into_boxed_slice(), map: bitvec![0; desc.NumDescriptors as usize].into_boxed_bitslice()
})), })),
PhantomData::default(), PhantomData::default(),
)) ))
@ -257,7 +253,7 @@ impl<T> D3D12DescriptorHeap<T> {
handle_size: inner.handle_size, handle_size: inner.handle_size,
start: 0, start: 0,
num_descriptors: size, num_descriptors: size,
map: vec![false; size].into_boxed_slice(), map: bitvec![0; size].into_boxed_bitslice()
}); });
start += size; start += size;
@ -280,7 +276,7 @@ impl<T> D3D12DescriptorHeap<T> {
let mut inner = self.0.borrow_mut(); let mut inner = self.0.borrow_mut();
for i in inner.start..inner.num_descriptors { for i in inner.start..inner.num_descriptors {
if !inner.map[i] { if !inner.map[i] {
inner.map[i] = true; inner.map.set(i, true);
handle.ptr = inner.cpu_start.ptr + (i * inner.handle_size); handle.ptr = inner.cpu_start.ptr + (i * inner.handle_size);
inner.start = i + 1; inner.start = i + 1;
@ -314,7 +310,7 @@ impl<T> D3D12DescriptorHeap<T> {
impl<T> Drop for D3D12DescriptorHeapSlotInner<T> { impl<T> Drop for D3D12DescriptorHeapSlotInner<T> {
fn drop(&mut self) { fn drop(&mut self) {
let mut inner = self.heap.borrow_mut(); let mut inner = self.heap.borrow_mut();
inner.map[self.slot] = false; inner.map.set(self.slot, false);
if inner.start > self.slot { if inner.start > self.slot {
inner.start = self.slot inner.start = self.slot
} }

View file

@ -25,15 +25,12 @@ use librashader_reflect::reflect::ReflectShader;
use librashader_runtime::binding::{BindingUtil, TextureInput}; use librashader_runtime::binding::{BindingUtil, TextureInput};
use librashader_runtime::image::{Image, UVDirection}; use librashader_runtime::image::{Image, UVDirection};
use librashader_runtime::quad::{QuadType, DEFAULT_MVP}; use librashader_runtime::quad::{QuadType, DEFAULT_MVP};
use librashader_runtime::scaling::MipmapSize;
use librashader_runtime::uniforms::UniformStorage; use librashader_runtime::uniforms::UniformStorage;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use spirv_cross::hlsl::ShaderModel; use spirv_cross::hlsl::ShaderModel;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::error::Error;
use std::path::Path; use std::path::Path;
use windows::core::Interface; use windows::core::Interface;
use windows::w;
use windows::Win32::Foundation::CloseHandle; use windows::Win32::Foundation::CloseHandle;
use windows::Win32::Graphics::Direct3D::Dxc::{ use windows::Win32::Graphics::Direct3D::Dxc::{
CLSID_DxcCompiler, CLSID_DxcLibrary, CLSID_DxcValidator, DxcCreateInstance, IDxcCompiler, CLSID_DxcCompiler, CLSID_DxcLibrary, CLSID_DxcValidator, DxcCreateInstance, IDxcCompiler,
@ -273,8 +270,6 @@ impl FilterChainD3D12 {
NodeMask: 0, NodeMask: 0,
})?; })?;
queue.SetName(w!("LutQueue"))?;
let fence_event = unsafe { CreateEventA(None, false, false, None)? }; let fence_event = unsafe { CreateEventA(None, false, false, None)? };
let fence: ID3D12Fence = device.CreateFence(0, D3D12_FENCE_FLAG_NONE)?; let fence: ID3D12Fence = device.CreateFence(0, D3D12_FENCE_FLAG_NONE)?;
let mut residuals = Vec::new(); let mut residuals = Vec::new();

View file

@ -39,7 +39,6 @@ impl LutTexture {
wrap_mode: WrapMode, wrap_mode: WrapMode,
mipmap: bool, mipmap: bool,
) -> error::Result<(LutTexture, ID3D12Resource)> { ) -> error::Result<(LutTexture, ID3D12Resource)> {
// todo: d3d12:800
let miplevels = source.size.calculate_miplevels() as u16; let miplevels = source.size.calculate_miplevels() as u16;
let mut desc = D3D12_RESOURCE_DESC { let mut desc = D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,

View file

@ -3,7 +3,6 @@ use crate::error;
use bytemuck::{offset_of, Pod, Zeroable}; use bytemuck::{offset_of, Pod, Zeroable};
use librashader_runtime::quad::QuadType; use librashader_runtime::quad::QuadType;
use windows::core::PCSTR; use windows::core::PCSTR;
use windows::w;
use windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; use windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
use windows::Win32::Graphics::Direct3D12::{ use windows::Win32::Graphics::Direct3D12::{
ID3D12Device, ID3D12GraphicsCommandList, ID3D12Resource, ID3D12Device, ID3D12GraphicsCommandList, ID3D12Resource,
@ -66,9 +65,6 @@ impl DrawQuad {
}; };
let buffer = buffer.into_raw(); let buffer = buffer.into_raw();
unsafe {
buffer.SetName(w!("drawquad"))?;
}
Ok(DrawQuad { buffer, view }) Ok(DrawQuad { buffer, view })
} }

View file

@ -4,14 +4,13 @@ use librashader_reflect::reflect::semantics::BindingStage;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::u64; use std::u64;
use widestring::u16cstr;
use windows::core::{Interface, PCSTR, PCWSTR}; use windows::core::{Interface, PCSTR, PCWSTR};
use windows::Win32::Graphics::Direct3D::Dxc::{ use windows::Win32::Graphics::Direct3D::Dxc::{
DxcValidatorFlags_InPlaceEdit, IDxcBlob, IDxcBlobUtf8, IDxcCompiler, IDxcUtils, IDxcValidator, DxcValidatorFlags_InPlaceEdit, IDxcBlob, IDxcBlobUtf8, IDxcCompiler, IDxcUtils, IDxcValidator,
DXC_CP, DXC_CP_UTF8, DXC_CP, DXC_CP_UTF8,
}; };
use windows::Win32::Graphics::Direct3D::Fxc::{ use windows::Win32::Graphics::Direct3D::Fxc::{D3DCompile, D3DCOMPILE_DEBUG, D3DCOMPILE_OPTIMIZATION_LEVEL3, D3DCOMPILE_SKIP_OPTIMIZATION};
D3DCompile, D3DCOMPILE_DEBUG, D3DCOMPILE_SKIP_OPTIMIZATION,
};
use windows::Win32::Graphics::Direct3D::ID3DBlob; use windows::Win32::Graphics::Direct3D::ID3DBlob;
use windows::Win32::Graphics::Direct3D12::{ use windows::Win32::Graphics::Direct3D12::{
ID3D12Device, ID3D12GraphicsCommandList, ID3D12Resource, D3D12_FEATURE_DATA_FORMAT_SUPPORT, ID3D12Device, ID3D12GraphicsCommandList, ID3D12Resource, D3D12_FEATURE_DATA_FORMAT_SUPPORT,
@ -132,7 +131,6 @@ pub fn d3d12_get_closest_format(
} }
pub fn fxc_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error::Result<ID3DBlob> { pub fn fxc_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error::Result<ID3DBlob> {
// todo: compile with dxc
unsafe { unsafe {
let mut blob = None; let mut blob = None;
D3DCompile( D3DCompile(
@ -143,12 +141,11 @@ pub fn fxc_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error:
None, None,
PCSTR(entry.as_ptr()), PCSTR(entry.as_ptr()),
PCSTR(version.as_ptr()), PCSTR(version.as_ptr()),
D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, if cfg!(feature = "debug-shader") {
// if cfg!(feature = "debug-shader") { D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION
// D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION } else {
// } else { D3DCOMPILE_OPTIMIZATION_LEVEL3
// D3DCOMPILE_OPTIMIZATION_LEVEL3 },
// },
0, 0,
&mut blob, &mut blob,
None, None,
@ -165,9 +162,6 @@ pub fn dxc_compile_shader(
source: &String, source: &String,
profile: BindingStage, profile: BindingStage,
) -> error::Result<IDxcBlob> { ) -> error::Result<IDxcBlob> {
// todo: compile with dxc
// let mut source = source.to_vec();
let include = unsafe { library.CreateDefaultIncludeHandler()? }; let include = unsafe { library.CreateDefaultIncludeHandler()? };
let blob = unsafe { let blob = unsafe {
@ -179,17 +173,17 @@ pub fn dxc_compile_shader(
}; };
let profile = if profile == BindingStage::FRAGMENT { let profile = if profile == BindingStage::FRAGMENT {
windows::w!("ps_6_0") u16cstr!("ps_6_0")
} else { } else {
windows::w!("vs_6_0") u16cstr!("vs_6_0")
}; };
unsafe { unsafe {
let result = compiler.Compile( let result = compiler.Compile(
&blob, &blob,
PCWSTR::null(), PCWSTR::null(),
windows::w!("main"), PCWSTR(u16cstr!("main").as_ptr()),
profile, PCWSTR(profile.as_ptr()),
None, None,
&[], &[],
&include, &include,
@ -205,9 +199,6 @@ pub fn dxc_validate_shader(
validator: &IDxcValidator, validator: &IDxcValidator,
source: &[u8], source: &[u8],
) -> error::Result<IDxcBlob> { ) -> error::Result<IDxcBlob> {
// todo: compile with dxc
// let mut source = source.to_vec();
let blob = let blob =
unsafe { library.CreateBlob(source.as_ptr().cast(), source.len() as u32, DXC_CP(0))? }; unsafe { library.CreateBlob(source.as_ptr().cast(), source.len() as u32, DXC_CP(0))? };