From 48e5a8a1491636fe6eb532ef364fc2e232d87775 Mon Sep 17 00:00:00 2001 From: chyyran Date: Fri, 27 Jan 2023 02:53:24 -0500 Subject: [PATCH] d3d12: build mipmapper shader --- librashader-runtime-d3d12/src/filter_chain.rs | 6 +++- .../src/hello_triangle.rs | 22 +++++++------- librashader-runtime-d3d12/src/mipmap.rs | 29 +++++++++++++++++-- librashader-runtime-d3d12/src/util.rs | 6 ++-- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/librashader-runtime-d3d12/src/filter_chain.rs b/librashader-runtime-d3d12/src/filter_chain.rs index 78c8a5f..4b7e986 100644 --- a/librashader-runtime-d3d12/src/filter_chain.rs +++ b/librashader-runtime-d3d12/src/filter_chain.rs @@ -18,6 +18,7 @@ use windows::Win32::Graphics::Direct3D12::{ }; use windows::Win32::System::Threading::{CreateEventA, WaitForSingleObject}; use windows::Win32::System::WindowsProgramming::INFINITE; +use crate::mipmap::D3D12MipmapGen; pub struct FilterChainD3D12 { pub(crate) common: FilterCommon, @@ -39,6 +40,7 @@ pub(crate) struct FilterCommon { // pub disable_mipmaps: bool, lut_heap: D3D12DescriptorHeap, luts: FxHashMap, + mipmap_gen: D3D12MipmapGen, } impl FilterChainD3D12 { @@ -124,8 +126,9 @@ impl FilterChainD3D12 { )?; let samplers = SamplerSet::new(device)?; - let mut lut_heap = D3D12DescriptorHeap::new(device, preset.textures.len())?; + let mipmap_gen = D3D12MipmapGen::new(device)?; + let mut lut_heap = D3D12DescriptorHeap::new(device, preset.textures.len())?; let luts = FilterChainD3D12::load_luts(device, &mut lut_heap, &preset.textures)?; Ok(FilterChainD3D12 { @@ -134,6 +137,7 @@ impl FilterChainD3D12 { samplers, lut_heap, luts, + mipmap_gen, }, }) } diff --git a/librashader-runtime-d3d12/src/hello_triangle.rs b/librashader-runtime-d3d12/src/hello_triangle.rs index 6e571ac..079bd03 100644 --- a/librashader-runtime-d3d12/src/hello_triangle.rs +++ b/librashader-runtime-d3d12/src/hello_triangle.rs @@ -279,17 +279,19 @@ pub mod d3d12_hello_triangle { fn new(filter: impl AsRef, command_line: &SampleCommandLine) -> Result { let (dxgi_factory, device) = create_device(command_line)?; // - let queue = device.cast::()?; - unsafe { - queue - .RegisterMessageCallback( - Some(debug_log), - D3D12_MESSAGE_CALLBACK_FLAG_NONE, - std::ptr::null_mut(), - &mut 0, - ) - .expect("could not register message callback"); + if let Ok(queue) = device.cast::() { + unsafe { + queue + .RegisterMessageCallback( + Some(debug_log), + D3D12_MESSAGE_CALLBACK_FLAG_NONE, + std::ptr::null_mut(), + &mut 0, + ) + .expect("could not register message callback"); + } } + let filter = FilterChainD3D12::load_from_path(&device, filter, None).unwrap(); Ok(Sample { diff --git a/librashader-runtime-d3d12/src/mipmap.rs b/librashader-runtime-d3d12/src/mipmap.rs index cc90ba7..98f604b 100644 --- a/librashader-runtime-d3d12/src/mipmap.rs +++ b/librashader-runtime-d3d12/src/mipmap.rs @@ -1,6 +1,7 @@ -use windows::Win32::Graphics::Direct3D12::{D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE, ID3D12Device, ID3D12PipelineState, ID3D12RootSignature}; +use windows::Win32::Graphics::Direct3D12::{D3D12_COMPUTE_PIPELINE_STATE_DESC, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_DESCRIPTOR_RANGE, D3D12_DESCRIPTOR_RANGE_TYPE_SRV, D3D12_DESCRIPTOR_RANGE_TYPE_UAV, D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT, D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_ROOT_CONSTANTS, D3D12_ROOT_DESCRIPTOR_TABLE, D3D12_ROOT_PARAMETER, D3D12_ROOT_PARAMETER_0, D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, D3D12_ROOT_SIGNATURE_DESC, D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS, D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS, D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS, D3D12_ROOT_SIGNATURE_FLAG_DENY_PIXEL_SHADER_ROOT_ACCESS, D3D12_ROOT_SIGNATURE_FLAG_DENY_VERTEX_SHADER_ROOT_ACCESS, D3D12_SHADER_BYTECODE, D3D12_SHADER_VISIBILITY_ALL, D3D12_STATIC_SAMPLER_DESC, D3D12_TEXTURE_ADDRESS_MODE_CLAMP, ID3D12Device, ID3D12PipelineState, ID3D12RootSignature}; use librashader_common::Size; use crate::error; +use crate::util::d3d_compile_shader; static GENERATE_MIPS_SRC: &[u8] = b" // Copyright (c) Microsoft Corporation. @@ -54,7 +55,31 @@ pub struct D3D12MipmapGen { impl D3D12MipmapGen { pub fn new(device: &ID3D12Device) -> error::Result { - todo!() + unsafe { + let blob = d3d_compile_shader(GENERATE_MIPS_SRC, b"main\0", b"cs_5_1\0")?; + let blob = std::slice::from_raw_parts(blob.GetBufferPointer().cast(), blob.GetBufferSize()); + let root_signature: ID3D12RootSignature = device.CreateRootSignature(0, blob)?; + + let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: windows::core::ManuallyDrop::new(&root_signature), + CS: D3D12_SHADER_BYTECODE { + pShaderBytecode: blob.as_ptr().cast(), + BytecodeLength: blob.len() + }, + NodeMask: 0, + ..Default::default() + }; + + let pipeline = device.CreateComputePipelineState(&desc)?; + + Ok(D3D12MipmapGen { + device: device.clone(), + root_signature, + pipeline, + }) + } + + } pub fn generate_mipmaps(miplevels: u16, diff --git a/librashader-runtime-d3d12/src/util.rs b/librashader-runtime-d3d12/src/util.rs index 2c58fb0..3507b80 100644 --- a/librashader-runtime-d3d12/src/util.rs +++ b/librashader-runtime-d3d12/src/util.rs @@ -2,9 +2,7 @@ use std::mem::ManuallyDrop; use std::u64; use crate::error; use windows::core::PCSTR; -use windows::Win32::Graphics::Direct3D::Fxc::{ - D3DCompile, D3DCOMPILE_DEBUG, D3DCOMPILE_SKIP_OPTIMIZATION, -}; +use windows::Win32::Graphics::Direct3D::Fxc::{D3DCompile, D3DCOMPILE_DEBUG, D3DCOMPILE_OPTIMIZATION_LEVEL3, D3DCOMPILE_SKIP_OPTIMIZATION}; use windows::Win32::Graphics::Direct3D::ID3DBlob; use windows::Win32::Graphics::Direct3D12::{ID3D12Device, D3D12_FEATURE_DATA_FORMAT_SUPPORT, D3D12_FEATURE_FORMAT_SUPPORT, ID3D12CommandList, ID3D12GraphicsCommandList, ID3D12Resource, D3D12_RESOURCE_DESC, D3D12_RESOURCE_BARRIER, D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, D3D12_RESOURCE_BARRIER_FLAG_NONE, D3D12_RESOURCE_DESC1, D3D12_RESOURCE_BARRIER_0, D3D12_RESOURCE_TRANSITION_BARRIER, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, D3D12_RESOURCE_STATES, D3D12_PLACED_SUBRESOURCE_FOOTPRINT, D3D12_MEMCPY_DEST, D3D12_SUBRESOURCE_DATA, D3D12_RESOURCE_DIMENSION_BUFFER, D3D12_TEXTURE_COPY_LOCATION, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, D3D12_TEXTURE_COPY_LOCATION_0, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; use windows::Win32::Graphics::Dxgi::Common::*; @@ -131,7 +129,7 @@ pub fn d3d_compile_shader(source: &[u8], entry: &[u8], version: &[u8]) -> error: if cfg!(feature = "debug-shader") { D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION } else { - 0 + D3DCOMPILE_OPTIMIZATION_LEVEL3 }, 0, &mut blob,