| 1 | use std::{fmt, os::raw::c_uint}; |
| 2 | |
| 3 | use skia_bindings::{ |
| 4 | GrD3DAlloc, GrD3DMemoryAllocator, GrD3DSurfaceInfo, GrD3DTextureResourceInfo, SkRefCntBase, |
| 5 | }; |
| 6 | use windows::Win32::Graphics::{ |
| 7 | Direct3D12::{ID3D12Fence, D3D12_RESOURCE_STATE_COMMON}, |
| 8 | Dxgi::Common::{DXGI_FORMAT_UNKNOWN, DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN}, |
| 9 | }; |
| 10 | |
| 11 | use crate::{ |
| 12 | gpu::{ |
| 13 | self, |
| 14 | d3d::{ID3D12Resource, D3D12_RESOURCE_STATES, DXGI_FORMAT}, |
| 15 | }, |
| 16 | prelude::*, |
| 17 | }; |
| 18 | |
| 19 | // TODO: add remaining cp functions to ComPtr via traits (get, reset, retain). |
| 20 | |
| 21 | pub type Alloc = RCHandle<GrD3DAlloc>; |
| 22 | unsafe_send_sync!(Alloc); |
| 23 | |
| 24 | impl NativeRefCountedBase for GrD3DAlloc { |
| 25 | type Base = SkRefCntBase; |
| 26 | } |
| 27 | |
| 28 | impl fmt::Debug for Alloc { |
| 29 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 30 | f.debug_struct(name:"Alloc" ).finish() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // TODO: support the implementation of custom D3D memory allocator's |
| 35 | // virtual createResource() and createAliasingResource() functions. |
| 36 | pub type MemoryAllocator = RCHandle<GrD3DMemoryAllocator>; |
| 37 | unsafe_send_sync!(MemoryAllocator); |
| 38 | |
| 39 | impl NativeRefCountedBase for GrD3DMemoryAllocator { |
| 40 | type Base = SkRefCntBase; |
| 41 | } |
| 42 | |
| 43 | impl fmt::Debug for MemoryAllocator { |
| 44 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 45 | f.debug_struct(name:"MemoryAllocator" ).finish() |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[repr (C)] |
| 50 | #[derive (Clone, Debug)] |
| 51 | pub struct TextureResourceInfo { |
| 52 | pub resource: ID3D12Resource, |
| 53 | pub alloc: Option<Alloc>, |
| 54 | pub resource_state: D3D12_RESOURCE_STATES, |
| 55 | pub format: DXGI_FORMAT, |
| 56 | pub sample_count: u32, |
| 57 | pub level_count: u32, |
| 58 | pub sample_quality_pattern: std::os::raw::c_uint, |
| 59 | pub protected: gpu::Protected, |
| 60 | } |
| 61 | unsafe_send_sync!(TextureResourceInfo); |
| 62 | |
| 63 | impl TextureResourceInfo { |
| 64 | pub fn from_resource(resource: ID3D12Resource) -> Self { |
| 65 | Self { |
| 66 | resource, |
| 67 | alloc: None, |
| 68 | resource_state: D3D12_RESOURCE_STATE_COMMON, |
| 69 | format: DXGI_FORMAT_UNKNOWN, |
| 70 | sample_count: 1, |
| 71 | level_count: 0, |
| 72 | sample_quality_pattern: DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN, |
| 73 | protected: gpu::Protected::No, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #[must_use ] |
| 78 | pub fn with_state(self, resource_state: D3D12_RESOURCE_STATES) -> Self { |
| 79 | Self { |
| 80 | resource_state, |
| 81 | ..self |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl From<ID3D12Resource> for TextureResourceInfo { |
| 87 | fn from(resource: ID3D12Resource) -> Self { |
| 88 | Self::from_resource(resource) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | native_transmutable!( |
| 93 | GrD3DTextureResourceInfo, |
| 94 | TextureResourceInfo, |
| 95 | texture_resource_info_layout |
| 96 | ); |
| 97 | |
| 98 | #[repr (C)] |
| 99 | #[derive (Clone, Debug)] |
| 100 | pub struct FenceInfo { |
| 101 | pub fence: ID3D12Fence, |
| 102 | pub value: u64, |
| 103 | } |
| 104 | |
| 105 | unsafe_send_sync!(FenceInfo); |
| 106 | |
| 107 | #[derive (Copy, Clone, PartialEq, Eq, Debug)] |
| 108 | #[repr (C)] |
| 109 | pub struct SurfaceInfo { |
| 110 | pub sample_count: u32, |
| 111 | pub level_count: u32, |
| 112 | pub protected: gpu::Protected, |
| 113 | |
| 114 | pub format: DXGI_FORMAT, |
| 115 | pub sample_quality_pattern: c_uint, |
| 116 | } |
| 117 | |
| 118 | native_transmutable!(GrD3DSurfaceInfo, SurfaceInfo, surface_info_layout); |
| 119 | |
| 120 | impl Default for SurfaceInfo { |
| 121 | fn default() -> Self { |
| 122 | Self { |
| 123 | sample_count: 1, |
| 124 | level_count: 0, |
| 125 | protected: gpu::Protected::No, |
| 126 | format: DXGI_FORMAT_UNKNOWN, |
| 127 | sample_quality_pattern: DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN, |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |