#![expect(
missing_debug_implementations,
reason = "TODO: someone developing on Windows add Debug impls where possible"
)]
mod adapter;
mod command;
mod conv;
mod descriptor;
mod device;
mod instance;
mod pipeline_desc;
mod sampler;
mod shader_compilation;
mod suballocation;
mod view;
use alloc::{borrow::ToOwned as _, string::String, sync::Arc, vec::Vec};
use core::{ffi, fmt, ops::Deref};
use arrayvec::ArrayVec;
use hashbrown::HashMap;
use suballocation::Allocator;
use wgpu_sync::atomic::AtomicU64;
use wgpu_sync::{Mutex, RwLock};
use windows::{
core::{Free as _, Interface},
Win32::{
Foundation,
Graphics::{
Direct3D,
Direct3D12::{self, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT},
DirectComposition, Dxgi,
},
System::Threading,
},
};
use crate::auxil::{
self,
dxgi::{
d3d12_lib::D3D12Lib,
dcomp::DCompLib,
device_factory::DeviceFactory,
dxgi_lib::DxgiLib,
factory::{DxgiAdapter, DxgiFactory},
result::HResult,
swapchain::SurfaceTarget,
types::ISwapChainPanelNative,
},
};
pub use crate::auxil::dxgi::d3d12_lib::CreateDeviceError;
struct D3DBlob(Direct3D::ID3DBlob);
impl Deref for D3DBlob {
type Target = Direct3D::ID3DBlob;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl D3DBlob {
unsafe fn as_slice(&self) -> &[u8] {
unsafe { core::slice::from_raw_parts(self.GetBufferPointer().cast(), self.GetBufferSize()) }
}
}
#[derive(Clone, Debug)]
pub struct Api;
impl crate::Api for Api {
const VARIANT: wgt::Backend = wgt::Backend::Dx12;
type Instance = Instance;
type Surface = Surface;
type Adapter = Adapter;
type Device = Device;
type Queue = Queue;
type CommandEncoder = CommandEncoder;
type CommandBuffer = CommandBuffer;
type Buffer = Buffer;
type Texture = Texture;
type SurfaceTexture = Texture;
type TextureView = TextureView;
type Sampler = Sampler;
type QuerySet = QuerySet;
type Fence = Fence;
type BindGroupLayout = BindGroupLayout;
type BindGroup = BindGroup;
type PipelineLayout = PipelineLayout;
type ShaderModule = ShaderModule;
type RenderPipeline = RenderPipeline;
type ComputePipeline = ComputePipeline;
type RayTracingPipeline = RayTracingPipeline;
type PipelineCache = PipelineCache;
type AccelerationStructure = AccelerationStructure;
}
crate::impl_dyn_resource!(
Adapter,
AccelerationStructure,
BindGroup,
BindGroupLayout,
Buffer,
CommandBuffer,
CommandEncoder,
ComputePipeline,
Device,
Fence,
Instance,
PipelineCache,
PipelineLayout,
QuerySet,
Queue,
RayTracingPipeline,
RenderPipeline,
Sampler,
ShaderModule,
Surface,
Texture,
TextureView
);
const MAX_ROOT_ELEMENTS: usize = 64;
const MAX_IMMEDIATE_SIZE: u32 = 128;
const MAX_IMMEDIATES: usize = MAX_IMMEDIATE_SIZE as usize / 4;
const ZERO_BUFFER_SIZE: wgt::BufferAddress = 256 << 10;
pub struct Instance {
factory: DxgiFactory,
factory_media: Option<Dxgi::IDXGIFactoryMedia>,
device_factory: Arc<DeviceFactory>,
library: Arc<D3D12Lib>,
dcomp_lib: Arc<DCompLib>,
supports_allow_tearing: bool,
presentation_system: wgt::Dx12SwapchainKind,
_lib_dxgi: DxgiLib,
flags: wgt::InstanceFlags,
memory_budget_thresholds: wgt::MemoryBudgetThresholds,
compiler_container: Arc<shader_compilation::CompilerContainer>,
options: wgt::Dx12BackendOptions,
telemetry: Option<crate::Telemetry>,
}
impl Instance {
pub unsafe fn raw_factory4(&self) -> &Dxgi::IDXGIFactory4 {
self.factory.deref()
}
pub unsafe fn create_surface_from_visual(&self, visual: *mut ffi::c_void) -> Surface {
let visual = unsafe { DirectComposition::IDCompositionVisual::from_raw_borrowed(&visual) }
.expect("COM pointer should not be NULL");
Surface {
factory: self.factory.clone(),
factory_media: self.factory_media.clone(),
target: SurfaceTarget::Visual(visual.to_owned()),
supports_allow_tearing: self.supports_allow_tearing,
swap_chain: RwLock::new(None),
options: self.options.clone(),
hdr_source: None,
}
}
pub unsafe fn create_surface_from_surface_handle(
&self,
surface_handle: *mut ffi::c_void,
) -> Surface {
let surface_handle = Foundation::HANDLE(surface_handle);
Surface {
factory: self.factory.clone(),
factory_media: self.factory_media.clone(),
target: SurfaceTarget::SurfaceHandle(surface_handle),
supports_allow_tearing: self.supports_allow_tearing,
swap_chain: RwLock::new(None),
options: self.options.clone(),
hdr_source: None,
}
}
pub unsafe fn create_surface_from_swap_chain_panel(
&self,
swap_chain_panel: *mut ffi::c_void,
) -> Surface {
let swap_chain_panel =
unsafe { ISwapChainPanelNative::from_raw_borrowed(&swap_chain_panel) }
.expect("COM pointer should not be NULL");
Surface {
factory: self.factory.clone(),
factory_media: self.factory_media.clone(),
target: SurfaceTarget::SwapChainPanel(swap_chain_panel.to_owned()),
supports_allow_tearing: self.supports_allow_tearing,
swap_chain: RwLock::new(None),
options: self.options.clone(),
hdr_source: None,
}
}
}
unsafe impl Send for Instance {}
unsafe impl Sync for Instance {}
struct SwapChain {
raw: Dxgi::IDXGISwapChain3,
resources: Vec<Direct3D12::ID3D12Resource>,
waitable: Option<Foundation::HANDLE>,
acquired_count: usize,
pending_present_count: usize,
present_mode: wgt::PresentMode,
format: wgt::TextureFormat,
size: wgt::Extent3d,
restart_pending: bool,
}
pub struct Surface {
factory: DxgiFactory,
factory_media: Option<Dxgi::IDXGIFactoryMedia>,
target: SurfaceTarget,
supports_allow_tearing: bool,
swap_chain: RwLock<Option<SwapChain>>,
options: wgt::Dx12BackendOptions,
hdr_source: Option<auxil::dxgi::hdr::DxgiHdrSource>,
}
unsafe impl Send for Surface {}
unsafe impl Sync for Surface {}
impl Surface {
pub fn swap_chain(&self) -> Option<Dxgi::IDXGISwapChain3> {
Some(self.swap_chain.read().as_ref()?.raw.clone())
}
pub unsafe fn waitable_handle(&self) -> Option<Foundation::HANDLE> {
self.swap_chain.read().as_ref()?.waitable
}
}
#[derive(Debug, Clone, Copy)]
enum MemoryArchitecture {
Unified {
#[allow(unused)]
cache_coherent: bool,
},
NonUnified,
}
#[derive(Debug, Clone, Copy)]
struct PrivateCapabilities {
instance_flags: wgt::InstanceFlags,
workarounds: Workarounds,
#[allow(unused)]
heterogeneous_resource_heaps: bool,
memory_architecture: MemoryArchitecture,
heap_create_not_zeroed: bool,
casting_fully_typed_format_supported: bool,
suballocation_supported: bool,
shader_model: naga::back::hlsl::ShaderModel,
max_sampler_descriptor_heap_size: u32,
unrestricted_buffer_texture_copy_pitch_supported: bool,
}
impl PrivateCapabilities {
fn texture_data_placement_alignment(&self) -> u64 {
if self.unrestricted_buffer_texture_copy_pitch_supported {
4
} else {
D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT.into()
}
}
fn texture_data_pitch_alignment(&self) -> u32 {
if self.unrestricted_buffer_texture_copy_pitch_supported {
1
} else {
Direct3D12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT
}
}
}
#[derive(Default, Debug, Copy, Clone)]
struct Workarounds {
avoid_shader_debug_info: bool,
}
pub struct Adapter {
raw: DxgiAdapter,
device: Direct3D12::ID3D12Device,
library: Arc<D3D12Lib>,
dcomp_lib: Arc<DCompLib>,
private_caps: PrivateCapabilities,
presentation_timer: auxil::dxgi::time::PresentationTimer,
memory_budget_thresholds: wgt::MemoryBudgetThresholds,
compiler_container: Arc<shader_compilation::CompilerContainer>,
options: wgt::Dx12BackendOptions,
}
unsafe impl Send for Adapter {}
unsafe impl Sync for Adapter {}
impl Adapter {
pub fn as_raw(&self) -> &Dxgi::IDXGIAdapter3 {
&self.raw
}
}
struct Event(pub Foundation::HANDLE);
impl Event {
pub fn create(manual_reset: bool, initial_state: bool) -> Result<Self, crate::DeviceError> {
Ok(Self(
unsafe { Threading::CreateEventA(None, manual_reset, initial_state, None) }
.into_device_result("CreateEventA")?,
))
}
}
impl Drop for Event {
fn drop(&mut self) {
unsafe { Foundation::HANDLE::free(&mut self.0) }
}
}
struct Idler {
fence: Direct3D12::ID3D12Fence,
}
#[derive(Debug, Clone)]
struct CommandSignatures {
draw: Direct3D12::ID3D12CommandSignature,
draw_indexed: Direct3D12::ID3D12CommandSignature,
draw_mesh: Option<Direct3D12::ID3D12CommandSignature>,
dispatch: Direct3D12::ID3D12CommandSignature,
}
struct DeviceShared {
adapter: DxgiAdapter,
zero_buffer: Direct3D12::ID3D12Resource,
cmd_signatures: CommandSignatures,
heap_views: descriptor::GeneralHeap,
sampler_heap: sampler::SamplerHeap,
private_caps: PrivateCapabilities,
}
unsafe impl Send for DeviceShared {}
unsafe impl Sync for DeviceShared {}
pub struct Device {
raw: Direct3D12::ID3D12Device,
present_queue: Direct3D12::ID3D12CommandQueue,
idler: Idler,
features: wgt::Features,
shared: Arc<DeviceShared>,
options: wgt::Dx12BackendOptions,
rtv_pool: Arc<Mutex<descriptor::CpuPool>>,
dsv_pool: Mutex<descriptor::CpuPool>,
srv_uav_pool: Mutex<descriptor::CpuPool>,
library: Arc<D3D12Lib>,
dcomp_lib: Arc<DCompLib>,
#[cfg(feature = "renderdoc")]
render_doc: auxil::renderdoc::RenderDoc,
null_rtv_handle: descriptor::Handle,
mem_allocator: Allocator,
compiler_container: Arc<shader_compilation::CompilerContainer>,
shader_cache: Mutex<ShaderCache>,
counters: Arc<wgt::HalCounters>,
limits: wgt::Limits,
}
impl Drop for Device {
fn drop(&mut self) {
self.rtv_pool.lock().free_handle(self.null_rtv_handle);
if self
.shared
.private_caps
.instance_flags
.contains(wgt::InstanceFlags::VALIDATION)
{
auxil::dxgi::exception::unregister_exception_handler();
}
}
}
unsafe impl Send for Device {}
unsafe impl Sync for Device {}
pub struct Queue {
raw: Direct3D12::ID3D12CommandQueue,
temp_lists: Mutex<Vec<Option<Direct3D12::ID3D12CommandList>>>,
idle_fence: Direct3D12::ID3D12Fence,
idle_event: Event,
idle_fence_value: AtomicU64,
pending_waits: Mutex<Vec<(Direct3D12::ID3D12Fence, u64)>>,
pending_signals: Mutex<Vec<(Direct3D12::ID3D12Fence, u64)>>,
}
impl Queue {
pub fn as_raw(&self) -> &Direct3D12::ID3D12CommandQueue {
&self.raw
}
pub fn add_wait_fence(&self, fence: Direct3D12::ID3D12Fence, value: u64) {
self.pending_waits.lock().push((fence, value));
}
pub fn remove_wait_fence(&self, fence: &Direct3D12::ID3D12Fence) -> bool {
let target = fence.as_raw();
let mut waits = self.pending_waits.lock();
let before = waits.len();
waits.retain(|(f, _)| f.as_raw() != target);
waits.len() != before
}
pub fn add_signal_fence(&self, fence: Direct3D12::ID3D12Fence, value: u64) {
self.pending_signals.lock().push((fence, value));
}
pub fn remove_signal_fence(&self, fence: &Direct3D12::ID3D12Fence) -> bool {
let target = fence.as_raw();
let mut signals = self.pending_signals.lock();
let before = signals.len();
signals.retain(|(f, _)| f.as_raw() != target);
signals.len() != before
}
}
unsafe impl Send for Queue {}
unsafe impl Sync for Queue {}
#[derive(Default)]
struct Temp {
marker: Vec<u16>,
barriers: Vec<Direct3D12::D3D12_RESOURCE_BARRIER>,
}
impl Temp {
fn clear(&mut self) {
self.marker.clear();
self.barriers.clear();
}
}
struct PassResolve {
src: (Direct3D12::ID3D12Resource, u32),
dst: (Direct3D12::ID3D12Resource, u32),
format: Dxgi::Common::DXGI_FORMAT,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct SpecialConstants {
first_vertex_or_x: i32,
first_instance_or_y: u32,
unused_or_z: u32,
}
impl SpecialConstants {
fn from_indirect_draw_call_params(first_vertex: i32, first_instance: u32) -> Self {
Self {
first_vertex_or_x: first_vertex,
first_instance_or_y: first_instance,
unused_or_z: 0,
}
}
fn from_compute_dispatch_params(workgroup_count: [u32; 3]) -> Self {
Self {
first_vertex_or_x: workgroup_count[0] as i32,
first_instance_or_y: workgroup_count[1],
unused_or_z: workgroup_count[2],
}
}
}
#[derive(Clone, Copy, Debug)]
enum RootElement {
Empty,
Immediates,
SpecialConstants(SpecialConstants),
DescriptorTable(Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE),
SamplerHeapDescriptorTable,
DynamicUniformBuffer {
address: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE,
},
DynamicStorageBufferOffsets {
start: usize,
end: usize,
},
}
#[derive(Clone, Copy)]
enum PassKind {
Render,
Compute,
Transfer,
}
struct PassState {
has_label: bool,
resolves: ArrayVec<PassResolve, { crate::MAX_COLOR_ATTACHMENTS }>,
layout: PipelineLayoutShared,
root_elements: [RootElement; MAX_ROOT_ELEMENTS],
immediates: [u32; MAX_IMMEDIATES],
dynamic_storage_buffer_offsets: Vec<u32>,
dirty_root_elements: u64,
vertex_buffers: [Direct3D12::D3D12_VERTEX_BUFFER_VIEW; crate::MAX_VERTEX_BUFFERS],
dirty_vertex_buffers: usize,
kind: PassKind,
}
const _: () = assert!(MAX_ROOT_ELEMENTS == u64::BITS as usize);
impl PassState {
fn new() -> Self {
PassState {
has_label: false,
resolves: ArrayVec::new(),
layout: PipelineLayoutShared {
signature: None,
total_root_elements: 0,
special_constants: None,
immediates_info: None,
sampler_heap_root_index: None,
},
root_elements: [RootElement::Empty; MAX_ROOT_ELEMENTS],
immediates: [0; MAX_IMMEDIATES],
dynamic_storage_buffer_offsets: Vec::new(),
dirty_root_elements: 0,
vertex_buffers: [Default::default(); crate::MAX_VERTEX_BUFFERS],
dirty_vertex_buffers: 0,
kind: PassKind::Transfer,
}
}
fn clear(&mut self) {
*self = Self::new();
}
}
pub struct CommandEncoder {
allocator: Direct3D12::ID3D12CommandAllocator,
device: Direct3D12::ID3D12Device,
shared: Arc<DeviceShared>,
mem_allocator: Allocator,
rtv_pool: Arc<Mutex<descriptor::CpuPool>>,
temp_rtv_handles: Vec<descriptor::Handle>,
intermediate_copy_bufs: Vec<Buffer>,
null_rtv_handle: descriptor::Handle,
list: Option<Direct3D12::ID3D12GraphicsCommandList>,
free_lists: Vec<Direct3D12::ID3D12GraphicsCommandList>,
pass: PassState,
temp: Temp,
end_of_pass_timer_query: Option<(Direct3D12::ID3D12QueryHeap, u32)>,
counters: Arc<wgt::HalCounters>,
}
unsafe impl Send for CommandEncoder {}
unsafe impl Sync for CommandEncoder {}
impl fmt::Debug for CommandEncoder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CommandEncoder")
.field("allocator", &self.allocator)
.field("device", &self.allocator)
.finish()
}
}
#[derive(Debug)]
pub struct CommandBuffer {
raw: Direct3D12::ID3D12GraphicsCommandList,
}
impl crate::DynCommandBuffer for CommandBuffer {}
unsafe impl Send for CommandBuffer {}
unsafe impl Sync for CommandBuffer {}
#[derive(Debug)]
pub struct Buffer {
resource: Direct3D12::ID3D12Resource,
allocated_size: wgt::BufferAddress,
allocation: suballocation::Allocation,
_drop_guard: Option<crate::DropGuard>,
}
impl Buffer {
pub unsafe fn raw_resource(&self) -> &Direct3D12::ID3D12Resource {
&self.resource
}
}
unsafe impl Send for Buffer {}
unsafe impl Sync for Buffer {}
impl crate::DynBuffer for Buffer {}
impl<S> crate::BufferBinding<'_, Buffer, S> {
fn resolve_address(&self) -> wgt::BufferAddress {
(unsafe { self.buffer.resource.GetGPUVirtualAddress() }) + self.offset
}
}
#[derive(Debug)]
pub struct Texture {
resource: Direct3D12::ID3D12Resource,
format: wgt::TextureFormat,
dimension: wgt::TextureDimension,
size: wgt::Extent3d,
mip_level_count: u32,
sample_count: u32,
allocation: suballocation::Allocation,
plane_slice_override: Option<u32>,
_drop_guard: Option<crate::DropGuard>,
}
impl Texture {
pub unsafe fn raw_resource(&self) -> &Direct3D12::ID3D12Resource {
&self.resource
}
pub fn with_plane_slice(mut self, plane_slice: u32) -> Self {
debug_assert!(
!self.format.is_multi_planar_format(),
"`with_plane_slice` expects a single-plane format wrapping a \
multi-plane DXGI resource; got planar format `{:?}`",
self.format,
);
self.plane_slice_override = Some(plane_slice);
self
}
pub(super) fn plane_slice_override(&self) -> Option<u32> {
self.plane_slice_override
}
}
impl crate::DynTexture for Texture {}
impl crate::DynSurfaceTexture for Texture {}
impl core::borrow::Borrow<dyn crate::DynTexture> for Texture {
fn borrow(&self) -> &dyn crate::DynTexture {
self
}
}
unsafe impl Send for Texture {}
unsafe impl Sync for Texture {}
impl Texture {
fn array_layer_count(&self) -> u32 {
match self.dimension {
wgt::TextureDimension::D1 | wgt::TextureDimension::D3 => 1,
wgt::TextureDimension::D2 => self.size.depth_or_array_layers,
}
}
fn calc_subresource(&self, mip_level: u32, array_layer: u32, plane: u32) -> u32 {
mip_level + (array_layer + plane * self.array_layer_count()) * self.mip_level_count
}
fn calc_subresource_for_copy(&self, base: &crate::TextureCopyBase) -> u32 {
let plane = self
.plane_slice_override
.unwrap_or_else(|| match base.aspect {
crate::FormatAspects::COLOR
| crate::FormatAspects::DEPTH
| crate::FormatAspects::PLANE_0 => 0,
crate::FormatAspects::STENCIL | crate::FormatAspects::PLANE_1 => 1,
crate::FormatAspects::PLANE_2 => 2,
_ => unreachable!(),
});
self.calc_subresource(base.mip_level, base.array_layer, plane)
}
}
#[derive(Debug)]
pub struct TextureView {
raw_format: Dxgi::Common::DXGI_FORMAT,
aspects: crate::FormatAspects,
dimension: wgt::TextureViewDimension,
texture: Direct3D12::ID3D12Resource,
subresource_index: u32,
mip_slice: u32,
handle_srv: Option<descriptor::Handle>,
handle_uav: Option<descriptor::Handle>,
handle_rtv: Option<descriptor::Handle>,
handle_dsv_wr: Option<descriptor::Handle>,
handle_dsv_rw: Option<descriptor::Handle>,
handle_dsv_rr: Option<descriptor::Handle>,
handle_dsv_ww: Option<descriptor::Handle>,
}
impl crate::DynTextureView for TextureView {}
unsafe impl Send for TextureView {}
unsafe impl Sync for TextureView {}
#[derive(Debug)]
pub struct Sampler {
index: sampler::SamplerIndex,
desc: Direct3D12::D3D12_SAMPLER_DESC,
}
impl crate::DynSampler for Sampler {}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(Sampler: Send, Sync);
#[derive(Debug)]
pub struct QuerySet {
raw: Direct3D12::ID3D12QueryHeap,
raw_ty: Direct3D12::D3D12_QUERY_TYPE,
}
impl crate::DynQuerySet for QuerySet {}
unsafe impl Send for QuerySet {}
unsafe impl Sync for QuerySet {}
#[derive(Debug)]
pub struct Fence {
raw: Direct3D12::ID3D12Fence,
}
impl crate::DynFence for Fence {}
unsafe impl Send for Fence {}
unsafe impl Sync for Fence {}
impl Fence {
pub fn raw_fence(&self) -> &Direct3D12::ID3D12Fence {
&self.raw
}
}
#[derive(Debug)]
pub struct BindGroupLayout {
entries: Vec<wgt::BindGroupLayoutEntry>,
cpu_heap_views: Option<descriptor::CpuHeap>,
copy_counts: Vec<u32>, }
impl crate::DynBindGroupLayout for BindGroupLayout {}
#[derive(Debug, Clone, Copy)]
enum DynamicBuffer {
Uniform(Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE),
Storage,
}
#[derive(Debug)]
struct SamplerIndexBuffer {
buffer: Direct3D12::ID3D12Resource,
allocation: suballocation::Allocation,
}
#[derive(Debug)]
pub struct BindGroup {
handle_views: Option<descriptor::DualHandle>,
sampler_index_buffer: Option<SamplerIndexBuffer>,
dynamic_buffers: Vec<DynamicBuffer>,
}
impl crate::DynBindGroup for BindGroup {}
bitflags::bitflags! {
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
struct TableTypes: u8 {
const SRV_CBV_UAV = 1 << 0;
const SAMPLERS = 1 << 1;
}
}
type RootIndex = u32;
#[derive(Debug)]
struct BindGroupInfo {
base_root_index: RootIndex,
tables: TableTypes,
dynamic_storage_buffer_offsets: Option<DynamicStorageBufferOffsets>,
}
#[derive(Debug, Clone)]
struct ImmediatesInfo {
root_index: RootIndex,
size: u32,
}
#[derive(Debug, Clone)]
struct DynamicStorageBufferOffsets {
root_index: RootIndex,
range: core::ops::Range<usize>,
}
#[derive(Debug, Clone)]
struct PipelineLayoutShared {
signature: Option<Direct3D12::ID3D12RootSignature>,
total_root_elements: RootIndex,
special_constants: Option<PipelineLayoutSpecialConstants>,
immediates_info: Option<ImmediatesInfo>,
sampler_heap_root_index: Option<RootIndex>,
}
unsafe impl Send for PipelineLayoutShared {}
unsafe impl Sync for PipelineLayoutShared {}
#[derive(Debug, Clone)]
struct PipelineLayoutSpecialConstants {
root_index: RootIndex,
indirect_cmd_signatures: Option<CommandSignatures>,
}
unsafe impl Send for PipelineLayoutSpecialConstants {}
unsafe impl Sync for PipelineLayoutSpecialConstants {}
#[derive(Debug)]
pub struct PipelineLayout {
shared: PipelineLayoutShared,
bind_group_infos: [Option<BindGroupInfo>; crate::MAX_BIND_GROUPS],
naga_options: naga::back::hlsl::Options,
}
impl crate::DynPipelineLayout for PipelineLayout {}
#[derive(Debug)]
pub struct ShaderModule {
source: ShaderModuleSource,
raw_name: Option<alloc::ffi::CString>,
runtime_checks: wgt::ShaderRuntimeChecks,
}
impl crate::DynShaderModule for ShaderModule {}
#[derive(Default)]
pub struct ShaderCache {
nr_of_shaders_compiled: u32,
entries: HashMap<ShaderCacheKey, ShaderCacheValue>,
}
#[derive(PartialEq, Eq, Hash)]
pub(super) struct ShaderCacheKey {
source: String,
entry_point: String,
stage: naga::ShaderStage,
shader_model: naga::back::hlsl::ShaderModel,
}
pub(super) struct ShaderCacheValue {
last_used: u32,
shader: CompiledShader,
}
#[derive(Clone)]
pub(super) struct CompiledShader(Arc<[u8]>);
impl From<&[u8]> for CompiledShader {
fn from(bytecode: &[u8]) -> Self {
Self(Arc::from(bytecode))
}
}
impl CompiledShader {
fn create_native_shader(&self) -> Direct3D12::D3D12_SHADER_BYTECODE {
Direct3D12::D3D12_SHADER_BYTECODE {
pShaderBytecode: self.0.as_ptr().cast(),
BytecodeLength: self.0.len(),
}
}
}
#[derive(Debug)]
pub struct RenderPipeline {
raw: Direct3D12::ID3D12PipelineState,
layout: PipelineLayoutShared,
topology: Direct3D::D3D_PRIMITIVE_TOPOLOGY,
vertex_strides: [Option<u32>; crate::MAX_VERTEX_BUFFERS],
}
impl crate::DynRenderPipeline for RenderPipeline {}
unsafe impl Send for RenderPipeline {}
unsafe impl Sync for RenderPipeline {}
#[derive(Debug)]
pub struct ComputePipeline {
raw: Direct3D12::ID3D12PipelineState,
layout: PipelineLayoutShared,
}
impl crate::ComputePipeline for ComputePipeline {
fn get_subgroup_size(&self) -> Option<u32> {
None
}
}
unsafe impl Send for ComputePipeline {}
unsafe impl Sync for ComputePipeline {}
#[derive(Debug)]
pub struct RayTracingPipeline {}
impl crate::DynRayTracingPipeline for RayTracingPipeline {}
#[derive(Debug)]
pub struct PipelineCache;
impl crate::DynPipelineCache for PipelineCache {}
#[derive(Debug)]
pub struct AccelerationStructure {
resource: Direct3D12::ID3D12Resource,
allocation: suballocation::Allocation,
}
impl crate::DynAccelerationStructure for AccelerationStructure {}
impl SwapChain {
unsafe fn release_resources(mut self) -> Dxgi::IDXGISwapChain3 {
if let Some(mut waitable) = self.waitable.take() {
unsafe { Foundation::HANDLE::free(&mut waitable) };
}
self.raw
}
}
impl crate::Surface for Surface {
type A = Api;
unsafe fn configure(
&self,
device: &Device,
config: &crate::SurfaceConfiguration,
) -> Result<(), crate::SurfaceError> {
let mut flags = Dxgi::DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
if self.supports_allow_tearing {
flags |= Dxgi::DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
}
unsafe { device.wait_for_present_queue_idle() }?;
let non_srgb_format = auxil::dxgi::conv::map_texture_format_nosrgb(config.format);
debug_assert!(config.maximum_frame_latency <= 15);
let swap_chain_buffer = (config.maximum_frame_latency + 1).min(16);
let pending_present_count = config.maximum_frame_latency as usize;
let swap_chain = match self.swap_chain.write().take() {
Some(sc) => {
let raw = unsafe { sc.release_resources() };
let result = unsafe {
raw.ResizeBuffers(
swap_chain_buffer,
config.extent.width,
config.extent.height,
non_srgb_format,
flags,
)
};
if let Err(err) = result {
log::error!("ResizeBuffers failed: {err}");
return Err(crate::SurfaceError::Other("window is in use"));
}
raw
}
None => {
let desc = auxil::dxgi::swapchain::swap_chain_descriptor(
non_srgb_format,
config.extent,
swap_chain_buffer,
auxil::dxgi::conv::map_acomposite_alpha_mode(config.composite_alpha_mode),
flags,
);
let swap_chain1 = match self.target {
SurfaceTarget::Visual(_)
| SurfaceTarget::VisualFromWndHandle { .. }
| SurfaceTarget::SwapChainPanel(_) => {
profiling::scope!("IDXGIFactory2::CreateSwapChainForComposition");
unsafe {
self.factory.CreateSwapChainForComposition(
&device.present_queue,
&desc,
None,
)
}
}
SurfaceTarget::SurfaceHandle(handle) => {
profiling::scope!(
"IDXGIFactoryMedia::CreateSwapChainForCompositionSurfaceHandle"
);
unsafe {
self.factory_media
.as_ref()
.ok_or(crate::SurfaceError::Other("IDXGIFactoryMedia not found"))?
.CreateSwapChainForCompositionSurfaceHandle(
&device.present_queue,
Some(handle),
&desc,
None,
)
}
}
SurfaceTarget::WndHandle(hwnd) => {
profiling::scope!("IDXGIFactory2::CreateSwapChainForHwnd");
unsafe {
self.factory.CreateSwapChainForHwnd(
&device.present_queue,
hwnd,
&desc,
None,
None,
)
}
}
};
let swap_chain1 = swap_chain1.map_err(|err| {
log::error!("SwapChain creation error: {err}");
crate::SurfaceError::Other("swapchain creation")
})?;
match &self.target {
SurfaceTarget::WndHandle(_) | SurfaceTarget::SurfaceHandle(_) => {}
SurfaceTarget::VisualFromWndHandle {
handle,
dcomp_state,
} => {
let mut dcomp_state = dcomp_state.lock();
let dcomp_state =
unsafe { dcomp_state.get_or_init(&device.dcomp_lib, handle) }?;
{
profiling::scope!("IDCompositionVisual::SetContent");
unsafe { dcomp_state.visual.SetContent(&swap_chain1) }.map_err(
|err| {
log::error!("IDCompositionVisual::SetContent failed: {err}");
crate::SurfaceError::Other("IDCompositionVisual::SetContent")
},
)?;
}
{
profiling::scope!("IDCompositionDevice::Commit");
unsafe { dcomp_state.device.Commit() }.map_err(|err| {
log::error!("IDCompositionDevice::Commit failed: {err}");
crate::SurfaceError::Other("IDCompositionDevice::Commit")
})?;
}
}
SurfaceTarget::Visual(visual) => {
if let Err(err) = unsafe { visual.SetContent(&swap_chain1) } {
log::error!("Unable to SetContent: {err}");
return Err(crate::SurfaceError::Other(
"IDCompositionVisual::SetContent",
));
}
}
SurfaceTarget::SwapChainPanel(swap_chain_panel) => {
if let Err(err) = unsafe { swap_chain_panel.SetSwapChain(&swap_chain1) } {
log::error!("Unable to SetSwapChain: {err}");
return Err(crate::SurfaceError::Other(
"ISwapChainPanelNative::SetSwapChain",
));
}
}
}
swap_chain1.cast::<Dxgi::IDXGISwapChain3>().map_err(|err| {
log::error!("Unable to cast swapchain: {err}");
crate::SurfaceError::Other("swapchain cast to version 3")
})?
}
};
let color_space = auxil::dxgi::swapchain::map_surface_color_space(config.color_space);
unsafe { swap_chain.SetColorSpace1(color_space) }.map_err(|err| {
log::error!("SetColorSpace1 failed: {err}");
crate::SurfaceError::Other("IDXGISwapChain3::SetColorSpace1")
})?;
match self.target {
SurfaceTarget::WndHandle(wnd_handle) => {
unsafe {
self.factory.MakeWindowAssociation(
wnd_handle,
Dxgi::DXGI_MWA_NO_WINDOW_CHANGES | Dxgi::DXGI_MWA_NO_ALT_ENTER,
)
}
.into_device_result("MakeWindowAssociation")?;
}
SurfaceTarget::Visual(_)
| SurfaceTarget::VisualFromWndHandle { .. }
| SurfaceTarget::SurfaceHandle(_)
| SurfaceTarget::SwapChainPanel(_) => {}
}
unsafe { swap_chain.SetMaximumFrameLatency(config.maximum_frame_latency) }
.into_device_result("SetMaximumFrameLatency")?;
let waitable = match device.options.latency_waitable_object {
wgt::Dx12UseFrameLatencyWaitableObject::None => None,
wgt::Dx12UseFrameLatencyWaitableObject::Wait
| wgt::Dx12UseFrameLatencyWaitableObject::DontWait => {
Some(unsafe { swap_chain.GetFrameLatencyWaitableObject() })
}
};
let mut resources = Vec::with_capacity(swap_chain_buffer as usize);
for i in 0..swap_chain_buffer {
let resource = unsafe { swap_chain.GetBuffer(i) }
.into_device_result("Failed to get swapchain buffer")?;
resources.push(resource);
}
let mut swapchain = self.swap_chain.write();
*swapchain = Some(SwapChain {
raw: swap_chain,
resources,
waitable,
acquired_count: 0,
pending_present_count,
present_mode: config.present_mode,
format: config.format,
size: config.extent,
restart_pending: true,
});
Ok(())
}
unsafe fn unconfigure(&self, device: &Device) {
if let Some(sc) = self.swap_chain.write().take() {
unsafe {
let _ = device.wait_for_present_queue_idle();
let _raw = sc.release_resources();
}
}
}
unsafe fn acquire_texture(
&self,
timeout: Option<core::time::Duration>,
_fence: &Fence,
) -> Result<crate::AcquiredSurfaceTexture<Api>, crate::SurfaceError> {
let mut swapchain = self.swap_chain.write();
let sc = swapchain.as_mut().unwrap();
match self.options.latency_waitable_object {
wgt::Dx12UseFrameLatencyWaitableObject::None
| wgt::Dx12UseFrameLatencyWaitableObject::DontWait => {}
wgt::Dx12UseFrameLatencyWaitableObject::Wait => {
if let Some(remaining) = sc.pending_present_count.checked_sub(1) {
auxil::dxgi::swapchain::wait_for_waitable(sc.waitable, timeout)?;
sc.pending_present_count = remaining;
}
}
}
let base_index = unsafe { sc.raw.GetCurrentBackBufferIndex() } as usize;
let index = (base_index + sc.acquired_count) % sc.resources.len();
sc.acquired_count += 1;
let texture = Texture {
resource: sc.resources[index].clone(),
format: sc.format,
dimension: wgt::TextureDimension::D2,
size: sc.size,
mip_level_count: 1,
sample_count: 1,
allocation: suballocation::Allocation::none(
suballocation::AllocationType::Texture,
sc.format.theoretical_memory_footprint(sc.size),
),
plane_slice_override: None,
_drop_guard: None,
};
Ok(crate::AcquiredSurfaceTexture {
texture,
suboptimal: false,
})
}
unsafe fn discard_texture(&self, _texture: Texture) {
let mut swapchain = self.swap_chain.write();
let sc = swapchain.as_mut().unwrap();
sc.acquired_count -= 1;
}
}
impl crate::Queue for Queue {
type A = Api;
unsafe fn submit(
&self,
command_buffers: &[&CommandBuffer],
_surface_textures: &[&Texture],
(signal_fence, signal_value): (&Fence, crate::FenceValue),
) -> Result<(), crate::DeviceError> {
let mut temp_lists = self.temp_lists.lock();
temp_lists.clear();
for cmd_buf in command_buffers {
temp_lists.push(Some(cmd_buf.raw.clone().into()));
}
{
let mut waits = self.pending_waits.lock();
for (fence, value) in waits.drain(..) {
unsafe { self.raw.Wait(&fence, value) }.into_device_result("Wait pending fence")?;
}
}
{
profiling::scope!("ID3D12CommandQueue::ExecuteCommandLists");
unsafe { self.raw.ExecuteCommandLists(&temp_lists) }
}
unsafe { self.raw.Signal(&signal_fence.raw, signal_value) }
.into_device_result("Signal fence")?;
{
let mut signals = self.pending_signals.lock();
for (fence, value) in signals.drain(..) {
unsafe { self.raw.Signal(&fence, value) }
.into_device_result("Signal pending fence")?;
}
}
Ok(())
}
unsafe fn present(
&self,
surface: &Surface,
_texture: Texture,
_damage_rects: &[wgt::Rect<u32>],
) -> Result<(), crate::SurfaceError> {
let mut swapchain = surface.swap_chain.write();
let sc = swapchain.as_mut().unwrap();
sc.acquired_count -= 1;
let (interval, flags) = auxil::dxgi::swapchain::present_flags(sc.present_mode);
if sc.restart_pending {
sc.restart_pending = false;
profiling::scope!("IDXGISwapchain3::Present(RESTART)");
unsafe { sc.raw.Present(1, Dxgi::DXGI_PRESENT_RESTART) }
.ok()
.into_device_result("Present(RESTART)")?;
} else {
profiling::scope!("IDXGISwapchain3::Present");
unsafe { sc.raw.Present(interval, flags) }
.ok()
.into_device_result("Present")?;
}
sc.pending_present_count += 1;
Ok(())
}
unsafe fn get_timestamp_period(&self) -> f32 {
let frequency = unsafe { self.raw.GetTimestampFrequency() }.expect("GetTimestampFrequency");
(1_000_000_000.0 / frequency as f64) as f32
}
unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> {
let value = self
.idle_fence_value
.fetch_add(1, core::sync::atomic::Ordering::Relaxed)
+ 1;
unsafe { self.raw.Signal(&self.idle_fence, value) }
.into_device_result("Signal idle fence")?;
unsafe {
self.idle_fence
.SetEventOnCompletion(value, self.idle_event.0)
}
.into_device_result("SetEventOnCompletion")?;
unsafe { Threading::WaitForSingleObject(self.idle_event.0, Threading::INFINITE) };
Ok(())
}
}
#[derive(Debug)]
pub struct DxilPassthroughShader {
pub shader: Vec<u8>,
}
#[derive(Debug)]
pub struct HlslPassthroughShader {
pub shader: String,
}
#[derive(Debug)]
pub enum ShaderModuleSource {
Naga(crate::NagaShader),
DxilPassthrough(DxilPassthroughShader),
HlslPassthrough(HlslPassthroughShader),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum FeatureLevel {
V11_0,
V11_1,
V12_0,
V12_1,
V12_2,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ShaderModel {
V5_1,
V6_0,
V6_1,
V6_2,
V6_3,
V6_4,
V6_5,
V6_6,
V6_7,
V6_8,
V6_9,
}