mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-25 08:13:27 +00:00
Merge pull request #45 from jrmuizel/ownership2
This commit is contained in:
commit
661dcee3f9
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "d3d12"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
authors = [
|
||||
"gfx-rs developers",
|
||||
]
|
||||
|
82
src/com.rs
82
src/com.rs
@ -8,15 +8,18 @@ use std::{
|
||||
use winapi::{ctypes::c_void, um::unknwnbase::IUnknown, Interface};
|
||||
|
||||
#[repr(transparent)]
|
||||
pub struct WeakPtr<T>(*mut T);
|
||||
pub struct ComPtr<T: Interface>(*mut T);
|
||||
|
||||
impl<T> WeakPtr<T> {
|
||||
impl<T: Interface> ComPtr<T> {
|
||||
pub fn null() -> Self {
|
||||
WeakPtr(ptr::null_mut())
|
||||
ComPtr(ptr::null_mut())
|
||||
}
|
||||
|
||||
pub unsafe fn from_raw(raw: *mut T) -> Self {
|
||||
WeakPtr(raw)
|
||||
if !raw.is_null() {
|
||||
(&*(raw as *mut IUnknown)).AddRef();
|
||||
}
|
||||
ComPtr(raw)
|
||||
}
|
||||
|
||||
pub fn is_null(&self) -> bool {
|
||||
@ -40,40 +43,42 @@ impl<T> WeakPtr<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Interface> WeakPtr<T> {
|
||||
impl<T: Interface> ComPtr<T> {
|
||||
pub unsafe fn as_unknown(&self) -> &IUnknown {
|
||||
debug_assert!(!self.is_null());
|
||||
&*(self.0 as *mut IUnknown)
|
||||
}
|
||||
|
||||
// Cast creates a new WeakPtr requiring explicit destroy call.
|
||||
pub unsafe fn cast<U>(&self) -> D3DResult<WeakPtr<U>>
|
||||
pub unsafe fn cast<U>(&self) -> D3DResult<ComPtr<U>>
|
||||
where
|
||||
U: Interface,
|
||||
{
|
||||
let mut obj = WeakPtr::<U>::null();
|
||||
debug_assert!(!self.is_null());
|
||||
let mut obj = ComPtr::<U>::null();
|
||||
let hr = self
|
||||
.as_unknown()
|
||||
.QueryInterface(&U::uuidof(), obj.mut_void());
|
||||
(obj, hr)
|
||||
}
|
||||
|
||||
// Destroying one instance of the WeakPtr will invalidate all
|
||||
// copies and clones.
|
||||
pub unsafe fn destroy(&self) {
|
||||
self.as_unknown().Release();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for WeakPtr<T> {
|
||||
impl<T: Interface> Clone for ComPtr<T> {
|
||||
fn clone(&self) -> Self {
|
||||
WeakPtr(self.0)
|
||||
debug_assert!(!self.is_null());
|
||||
unsafe { self.as_unknown().AddRef(); }
|
||||
ComPtr(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Copy for WeakPtr<T> {}
|
||||
impl<T: Interface> Drop for ComPtr<T> {
|
||||
fn drop(&mut self) {
|
||||
if !self.0.is_null() {
|
||||
unsafe { self.as_unknown().Release(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for WeakPtr<T> {
|
||||
impl<T: Interface> Deref for ComPtr<T> {
|
||||
type Target = T;
|
||||
fn deref(&self) -> &T {
|
||||
debug_assert!(!self.is_null());
|
||||
@ -81,25 +86,25 @@ impl<T> Deref for WeakPtr<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for WeakPtr<T> {
|
||||
impl<T: Interface> fmt::Debug for ComPtr<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "WeakPtr( ptr: {:?} )", self.0)
|
||||
write!(f, "ComPtr( ptr: {:?} )", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PartialEq<*mut T> for WeakPtr<T> {
|
||||
impl<T: Interface> PartialEq<*mut T> for ComPtr<T> {
|
||||
fn eq(&self, other: &*mut T) -> bool {
|
||||
self.0 == *other
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PartialEq for WeakPtr<T> {
|
||||
impl<T: Interface> PartialEq for ComPtr<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Hash for WeakPtr<T> {
|
||||
impl<T: Interface> Hash for ComPtr<T> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.0.hash(state);
|
||||
}
|
||||
@ -110,9 +115,9 @@ impl<T> Hash for WeakPtr<T> {
|
||||
/// Give the variants so that parents come before children. This often manifests as going up in order (1 -> 2 -> 3). This is vital for safety.
|
||||
///
|
||||
/// Three function names need to be attached to each variant. The examples are given for the MyComObject1 variant below:
|
||||
/// - the from function (`WeakPtr<actual::ComObject1> -> Self`)
|
||||
/// - the as function (`&self -> Option<WeakPtr<actual::ComObject1>>`)
|
||||
/// - the unwrap function (`&self -> WeakPtr<actual::ComObject1>` panicing on failure to cast)
|
||||
/// - the from function (`ComPtr<actual::ComObject1> -> Self`)
|
||||
/// - the as function (`&self -> Option<ComPtr<actual::ComObject1>>`)
|
||||
/// - the unwrap function (`&self -> ComPtr<actual::ComObject1>` panicing on failure to cast)
|
||||
///
|
||||
/// ```rust
|
||||
/// # pub use d3d12::weak_com_inheritance_chain;
|
||||
@ -145,21 +150,12 @@ macro_rules! weak_com_inheritance_chain {
|
||||
) => {
|
||||
$(#[$meta])*
|
||||
$vis enum $name {
|
||||
$first_variant($crate::WeakPtr<$first_type>),
|
||||
$first_variant($crate::ComPtr<$first_type>),
|
||||
$(
|
||||
$variant($crate::WeakPtr<$type>)
|
||||
$variant($crate::ComPtr<$type>)
|
||||
),+
|
||||
}
|
||||
impl $name {
|
||||
$vis unsafe fn destroy(&self) {
|
||||
match *self {
|
||||
Self::$first_variant(v) => v.destroy(),
|
||||
$(
|
||||
Self::$variant(v) => v.destroy(),
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
$crate::weak_com_inheritance_chain! {
|
||||
@recursion_logic,
|
||||
$vis,
|
||||
@ -170,7 +166,7 @@ macro_rules! weak_com_inheritance_chain {
|
||||
}
|
||||
|
||||
impl std::ops::Deref for $name {
|
||||
type Target = $crate::WeakPtr<$first_type>;
|
||||
type Target = $crate::ComPtr<$first_type>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.$first_unwrap_name()
|
||||
}
|
||||
@ -223,12 +219,12 @@ macro_rules! weak_com_inheritance_chain {
|
||||
$($next_variant:ident),*;
|
||||
) => {
|
||||
// Construct this enum from weak pointer to this interface. For best usability, always use the highest constructor you can. This doesn't try to upcast.
|
||||
$vis unsafe fn $from_name(value: $crate::WeakPtr<$type>) -> Self {
|
||||
$vis unsafe fn $from_name(value: $crate::ComPtr<$type>) -> Self {
|
||||
Self::$variant(value)
|
||||
}
|
||||
|
||||
// Returns Some if the value implements the interface otherwise returns None.
|
||||
$vis fn $as_name(&self) -> Option<&$crate::WeakPtr<$type>> {
|
||||
$vis fn $as_name(&self) -> Option<&$crate::ComPtr<$type>> {
|
||||
match *self {
|
||||
$(
|
||||
Self::$prev_variant(_) => None,
|
||||
@ -236,7 +232,7 @@ macro_rules! weak_com_inheritance_chain {
|
||||
Self::$variant(ref v) => Some(v),
|
||||
$(
|
||||
Self::$next_variant(ref v) => {
|
||||
// v is &WeakPtr<NextType> and se cast to &WeakPtr<Type>
|
||||
// v is &ComPtr<NextType> and se cast to &ComPtr<Type>
|
||||
Some(unsafe { std::mem::transmute(v) })
|
||||
}
|
||||
)*
|
||||
@ -245,7 +241,7 @@ macro_rules! weak_com_inheritance_chain {
|
||||
|
||||
// Returns the interface if the value implements it, otherwise panics.
|
||||
#[track_caller]
|
||||
$vis fn $unwrap_name(&self) -> &$crate::WeakPtr<$type> {
|
||||
$vis fn $unwrap_name(&self) -> &$crate::ComPtr<$type> {
|
||||
match *self {
|
||||
$(
|
||||
Self::$prev_variant(_) => panic!(concat!("Tried to unwrap a ", stringify!($prev_variant), " as a ", stringify!($variant))),
|
||||
@ -253,7 +249,7 @@ macro_rules! weak_com_inheritance_chain {
|
||||
Self::$variant(ref v) => &*v,
|
||||
$(
|
||||
Self::$next_variant(ref v) => {
|
||||
// v is &WeakPtr<NextType> and se cast to &WeakPtr<Type>
|
||||
// v is &ComPtr<NextType> and se cast to &ComPtr<Type>
|
||||
unsafe { std::mem::transmute(v) }
|
||||
}
|
||||
)*
|
||||
|
@ -1,9 +1,9 @@
|
||||
//! Command Allocator
|
||||
|
||||
use crate::com::WeakPtr;
|
||||
use crate::com::ComPtr;
|
||||
use winapi::um::d3d12;
|
||||
|
||||
pub type CommandAllocator = WeakPtr<d3d12::ID3D12CommandAllocator>;
|
||||
pub type CommandAllocator = ComPtr<d3d12::ID3D12CommandAllocator>;
|
||||
|
||||
impl CommandAllocator {
|
||||
pub fn reset(&self) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Graphics command list
|
||||
|
||||
use crate::{
|
||||
com::WeakPtr, resource::DiscardRegion, CommandAllocator, CpuDescriptor, DescriptorHeap, Format,
|
||||
com::ComPtr, resource::DiscardRegion, CommandAllocator, CpuDescriptor, DescriptorHeap, Format,
|
||||
GpuAddress, GpuDescriptor, IndexCount, InstanceCount, PipelineState, Rect, Resource, RootIndex,
|
||||
RootSignature, Subresource, VertexCount, VertexOffset, WorkGroupCount, HRESULT,
|
||||
};
|
||||
@ -140,9 +140,9 @@ impl ResourceBarrier {
|
||||
}
|
||||
}
|
||||
|
||||
pub type CommandSignature = WeakPtr<d3d12::ID3D12CommandSignature>;
|
||||
pub type CommandList = WeakPtr<d3d12::ID3D12CommandList>;
|
||||
pub type GraphicsCommandList = WeakPtr<d3d12::ID3D12GraphicsCommandList>;
|
||||
pub type CommandSignature = ComPtr<d3d12::ID3D12CommandSignature>;
|
||||
pub type CommandList = ComPtr<d3d12::ID3D12CommandList>;
|
||||
pub type GraphicsCommandList = ComPtr<d3d12::ID3D12GraphicsCommandList>;
|
||||
|
||||
impl GraphicsCommandList {
|
||||
pub fn as_list(&self) -> CommandList {
|
||||
@ -153,7 +153,7 @@ impl GraphicsCommandList {
|
||||
unsafe { self.Close() }
|
||||
}
|
||||
|
||||
pub fn reset(&self, allocator: CommandAllocator, initial_pso: PipelineState) -> HRESULT {
|
||||
pub fn reset(&self, allocator: &CommandAllocator, initial_pso: PipelineState) -> HRESULT {
|
||||
unsafe { self.Reset(allocator.as_mut_ptr(), initial_pso.as_mut_ptr()) }
|
||||
}
|
||||
|
||||
@ -263,7 +263,7 @@ impl GraphicsCommandList {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_pipeline_state(&self, pso: PipelineState) {
|
||||
pub fn set_pipeline_state(&self, pso:&PipelineState) {
|
||||
unsafe {
|
||||
self.SetPipelineState(pso.as_mut_ptr());
|
||||
}
|
||||
@ -284,13 +284,13 @@ impl GraphicsCommandList {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_compute_root_signature(&self, signature: RootSignature) {
|
||||
pub fn set_compute_root_signature(&self, signature: &RootSignature) {
|
||||
unsafe {
|
||||
self.SetComputeRootSignature(signature.as_mut_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_graphics_root_signature(&self, signature: RootSignature) {
|
||||
pub fn set_graphics_root_signature(&self, signature: &RootSignature) {
|
||||
unsafe {
|
||||
self.SetGraphicsRootSignature(signature.as_mut_ptr());
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
use crate::com::WeakPtr;
|
||||
use crate::com::ComPtr;
|
||||
use winapi::um::d3d12sdklayers;
|
||||
#[cfg(any(feature = "libloading", feature = "implicit-link"))]
|
||||
use winapi::Interface as _;
|
||||
|
||||
pub type Debug = WeakPtr<d3d12sdklayers::ID3D12Debug>;
|
||||
pub type Debug = ComPtr<d3d12sdklayers::ID3D12Debug>;
|
||||
|
||||
#[cfg(feature = "libloading")]
|
||||
impl crate::D3D12Lib {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{com::WeakPtr, Blob, D3DResult, Error, TextureAddressMode};
|
||||
use crate::{com::ComPtr, Blob, D3DResult, Error, TextureAddressMode};
|
||||
use std::{fmt, mem, ops::Range};
|
||||
use winapi::{shared::dxgiformat, um::d3d12};
|
||||
|
||||
@ -27,7 +27,7 @@ bitflags! {
|
||||
}
|
||||
}
|
||||
|
||||
pub type DescriptorHeap = WeakPtr<d3d12::ID3D12DescriptorHeap>;
|
||||
pub type DescriptorHeap = ComPtr<d3d12::ID3D12DescriptorHeap>;
|
||||
|
||||
impl DescriptorHeap {
|
||||
pub fn start_cpu_descriptor(&self) -> CpuDescriptor {
|
||||
@ -265,7 +265,7 @@ bitflags! {
|
||||
}
|
||||
}
|
||||
|
||||
pub type RootSignature = WeakPtr<d3d12::ID3D12RootSignature>;
|
||||
pub type RootSignature = ComPtr<d3d12::ID3D12RootSignature>;
|
||||
pub type BlobResult = D3DResult<(Blob, Error)>;
|
||||
|
||||
#[cfg(feature = "libloading")]
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Device
|
||||
|
||||
use crate::{
|
||||
com::WeakPtr,
|
||||
com::ComPtr,
|
||||
command_list::{CmdListType, CommandSignature, IndirectArgument},
|
||||
descriptor::{CpuDescriptor, DescriptorHeapFlags, DescriptorHeapType, RenderTargetViewDesc},
|
||||
heap::{Heap, HeapFlags, HeapProperties},
|
||||
@ -12,13 +12,13 @@ use crate::{
|
||||
use std::ops::Range;
|
||||
use winapi::{um::d3d12, Interface};
|
||||
|
||||
pub type Device = WeakPtr<d3d12::ID3D12Device>;
|
||||
pub type Device = ComPtr<d3d12::ID3D12Device>;
|
||||
|
||||
#[cfg(feature = "libloading")]
|
||||
impl crate::D3D12Lib {
|
||||
pub fn create_device<I: Interface>(
|
||||
pub fn create_device<I: Interface>(
|
||||
&self,
|
||||
adapter: WeakPtr<I>,
|
||||
adapter: &ComPtr<I>,
|
||||
feature_level: crate::FeatureLevel,
|
||||
) -> Result<D3DResult<Device>, libloading::Error> {
|
||||
type Fun = extern "system" fn(
|
||||
@ -46,7 +46,7 @@ impl crate::D3D12Lib {
|
||||
impl Device {
|
||||
#[cfg(feature = "implicit-link")]
|
||||
pub fn create<I: Interface>(
|
||||
adapter: WeakPtr<I>,
|
||||
adapter: ComPtr<I>,
|
||||
feature_level: crate::FeatureLevel,
|
||||
) -> D3DResult<Self> {
|
||||
let mut device = Device::null();
|
||||
@ -155,7 +155,7 @@ impl Device {
|
||||
pub fn create_graphics_command_list(
|
||||
&self,
|
||||
list_type: CmdListType,
|
||||
allocator: CommandAllocator,
|
||||
allocator: &CommandAllocator,
|
||||
initial: PipelineState,
|
||||
node_mask: NodeMask,
|
||||
) -> D3DResult<GraphicsCommandList> {
|
||||
@ -215,7 +215,7 @@ impl Device {
|
||||
|
||||
pub fn create_compute_pipeline_state(
|
||||
&self,
|
||||
root_signature: RootSignature,
|
||||
root_signature: &RootSignature,
|
||||
cs: Shader,
|
||||
node_mask: NodeMask,
|
||||
cached_pso: CachedPSO,
|
||||
|
40
src/dxgi.rs
40
src/dxgi.rs
@ -1,4 +1,4 @@
|
||||
use crate::{com::WeakPtr, D3DResult, Resource, SampleDesc, HRESULT};
|
||||
use crate::{com::ComPtr, D3DResult, Resource, SampleDesc, HRESULT};
|
||||
use std::ptr;
|
||||
use winapi::{
|
||||
shared::{
|
||||
@ -43,14 +43,14 @@ pub enum AlphaMode {
|
||||
ForceDword = dxgi1_2::DXGI_ALPHA_MODE_FORCE_DWORD,
|
||||
}
|
||||
|
||||
pub type InfoQueue = WeakPtr<dxgidebug::IDXGIInfoQueue>;
|
||||
pub type InfoQueue = ComPtr<dxgidebug::IDXGIInfoQueue>;
|
||||
|
||||
pub type Adapter1 = WeakPtr<dxgi::IDXGIAdapter1>;
|
||||
pub type Adapter2 = WeakPtr<dxgi1_2::IDXGIAdapter2>;
|
||||
pub type Adapter3 = WeakPtr<dxgi1_4::IDXGIAdapter3>;
|
||||
pub type Adapter4 = WeakPtr<dxgi1_6::IDXGIAdapter4>;
|
||||
pub type Adapter1 = ComPtr<dxgi::IDXGIAdapter1>;
|
||||
pub type Adapter2 = ComPtr<dxgi1_2::IDXGIAdapter2>;
|
||||
pub type Adapter3 = ComPtr<dxgi1_4::IDXGIAdapter3>;
|
||||
pub type Adapter4 = ComPtr<dxgi1_6::IDXGIAdapter4>;
|
||||
crate::weak_com_inheritance_chain! {
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub enum DxgiAdapter {
|
||||
Adapter1(dxgi::IDXGIAdapter1), from_adapter1, as_adapter1, adapter1;
|
||||
Adapter2(dxgi1_2::IDXGIAdapter2), from_adapter2, as_adapter2, unwrap_adapter2;
|
||||
@ -59,14 +59,14 @@ crate::weak_com_inheritance_chain! {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Factory1 = WeakPtr<dxgi::IDXGIFactory1>;
|
||||
pub type Factory2 = WeakPtr<dxgi1_2::IDXGIFactory2>;
|
||||
pub type Factory3 = WeakPtr<dxgi1_3::IDXGIFactory3>;
|
||||
pub type Factory4 = WeakPtr<dxgi1_4::IDXGIFactory4>;
|
||||
pub type Factory5 = WeakPtr<dxgi1_5::IDXGIFactory5>;
|
||||
pub type Factory6 = WeakPtr<dxgi1_6::IDXGIFactory6>;
|
||||
pub type Factory1 = ComPtr<dxgi::IDXGIFactory1>;
|
||||
pub type Factory2 = ComPtr<dxgi1_2::IDXGIFactory2>;
|
||||
pub type Factory3 = ComPtr<dxgi1_3::IDXGIFactory3>;
|
||||
pub type Factory4 = ComPtr<dxgi1_4::IDXGIFactory4>;
|
||||
pub type Factory5 = ComPtr<dxgi1_5::IDXGIFactory5>;
|
||||
pub type Factory6 = ComPtr<dxgi1_6::IDXGIFactory6>;
|
||||
crate::weak_com_inheritance_chain! {
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub enum DxgiFactory {
|
||||
Factory1(dxgi::IDXGIFactory1), from_factory1, as_factory1, factory1;
|
||||
Factory2(dxgi1_2::IDXGIFactory2), from_factory2, as_factory2, unwrap_factory2;
|
||||
@ -77,14 +77,14 @@ crate::weak_com_inheritance_chain! {
|
||||
}
|
||||
}
|
||||
|
||||
pub type FactoryMedia = WeakPtr<dxgi1_3::IDXGIFactoryMedia>;
|
||||
pub type FactoryMedia = ComPtr<dxgi1_3::IDXGIFactoryMedia>;
|
||||
|
||||
pub type SwapChain = WeakPtr<dxgi::IDXGISwapChain>;
|
||||
pub type SwapChain1 = WeakPtr<dxgi1_2::IDXGISwapChain1>;
|
||||
pub type SwapChain2 = WeakPtr<dxgi1_3::IDXGISwapChain2>;
|
||||
pub type SwapChain3 = WeakPtr<dxgi1_4::IDXGISwapChain3>;
|
||||
pub type SwapChain = ComPtr<dxgi::IDXGISwapChain>;
|
||||
pub type SwapChain1 = ComPtr<dxgi1_2::IDXGISwapChain1>;
|
||||
pub type SwapChain2 = ComPtr<dxgi1_3::IDXGISwapChain2>;
|
||||
pub type SwapChain3 = ComPtr<dxgi1_4::IDXGISwapChain3>;
|
||||
crate::weak_com_inheritance_chain! {
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub enum DxgiSwapchain {
|
||||
SwapChain(dxgi::IDXGISwapChain), from_swap_chain, as_swap_chain, swap_chain;
|
||||
SwapChain1(dxgi1_2::IDXGISwapChain1), from_swap_chain1, as_swap_chain1, unwrap_swap_chain1;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::com::WeakPtr;
|
||||
use crate::com::ComPtr;
|
||||
use winapi::um::d3d12;
|
||||
|
||||
pub type Heap = WeakPtr<d3d12::ID3D12Heap>;
|
||||
pub type Heap = ComPtr<d3d12::ID3D12Heap>;
|
||||
|
||||
#[repr(u32)]
|
||||
#[derive(Clone, Copy)]
|
||||
|
@ -96,9 +96,9 @@ impl TryFrom<u32> for FeatureLevel {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Blob = WeakPtr<d3dcommon::ID3DBlob>;
|
||||
pub type Blob = ComPtr<d3dcommon::ID3DBlob>;
|
||||
|
||||
pub type Error = WeakPtr<d3dcommon::ID3DBlob>;
|
||||
pub type Error = ComPtr<d3dcommon::ID3DBlob>;
|
||||
impl Error {
|
||||
pub unsafe fn as_c_str(&self) -> &CStr {
|
||||
debug_assert!(!self.is_null());
|
||||
|
43
src/pso.rs
43
src/pso.rs
@ -1,7 +1,7 @@
|
||||
//! Pipeline state
|
||||
|
||||
use crate::{com::WeakPtr, Blob, D3DResult, Error};
|
||||
use std::{ffi, ops::Deref, ptr};
|
||||
use crate::{com::ComPtr, Blob, D3DResult, Error};
|
||||
use std::{ffi::{self, c_void}, ops::Deref, ptr, marker::PhantomData};
|
||||
use winapi::um::{d3d12, d3dcompiler};
|
||||
|
||||
bitflags! {
|
||||
@ -25,28 +25,28 @@ bitflags! {
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Shader(d3d12::D3D12_SHADER_BYTECODE);
|
||||
impl Shader {
|
||||
pub struct Shader<'a>(d3d12::D3D12_SHADER_BYTECODE, PhantomData<&'a c_void>);
|
||||
impl<'a> Shader<'a> {
|
||||
pub fn null() -> Self {
|
||||
Shader(d3d12::D3D12_SHADER_BYTECODE {
|
||||
BytecodeLength: 0,
|
||||
pShaderBytecode: ptr::null(),
|
||||
})
|
||||
}, PhantomData)
|
||||
}
|
||||
|
||||
pub fn from_raw(data: &[u8]) -> Self {
|
||||
pub fn from_raw(data: &'a [u8]) -> Self {
|
||||
Shader(d3d12::D3D12_SHADER_BYTECODE {
|
||||
BytecodeLength: data.len() as _,
|
||||
pShaderBytecode: data.as_ptr() as _,
|
||||
})
|
||||
}, PhantomData)
|
||||
}
|
||||
|
||||
// `blob` may not be null.
|
||||
pub fn from_blob(blob: Blob) -> Self {
|
||||
pub fn from_blob(blob: &'a Blob) -> Self {
|
||||
Shader(d3d12::D3D12_SHADER_BYTECODE {
|
||||
BytecodeLength: unsafe { blob.GetBufferSize() },
|
||||
pShaderBytecode: unsafe { blob.GetBufferPointer() },
|
||||
})
|
||||
}, PhantomData)
|
||||
}
|
||||
|
||||
/// Compile a shader from raw HLSL.
|
||||
@ -81,49 +81,40 @@ impl Shader {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Shader {
|
||||
impl<'a> Deref for Shader<'a> {
|
||||
type Target = d3d12::D3D12_SHADER_BYTECODE;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<Blob>> for Shader {
|
||||
fn from(blob: Option<Blob>) -> Self {
|
||||
match blob {
|
||||
Some(b) => Shader::from_blob(b),
|
||||
None => Shader::null(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct CachedPSO(d3d12::D3D12_CACHED_PIPELINE_STATE);
|
||||
impl CachedPSO {
|
||||
pub struct CachedPSO<'a>(d3d12::D3D12_CACHED_PIPELINE_STATE, PhantomData<&'a c_void>);
|
||||
impl<'a> CachedPSO<'a> {
|
||||
pub fn null() -> Self {
|
||||
CachedPSO(d3d12::D3D12_CACHED_PIPELINE_STATE {
|
||||
CachedBlobSizeInBytes: 0,
|
||||
pCachedBlob: ptr::null(),
|
||||
})
|
||||
}, PhantomData)
|
||||
}
|
||||
|
||||
// `blob` may not be null.
|
||||
pub fn from_blob(blob: Blob) -> Self {
|
||||
pub fn from_blob(blob: &'a Blob) -> Self {
|
||||
CachedPSO(d3d12::D3D12_CACHED_PIPELINE_STATE {
|
||||
CachedBlobSizeInBytes: unsafe { blob.GetBufferSize() },
|
||||
pCachedBlob: unsafe { blob.GetBufferPointer() },
|
||||
})
|
||||
}, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for CachedPSO {
|
||||
impl<'a> Deref for CachedPSO<'a> {
|
||||
type Target = d3d12::D3D12_CACHED_PIPELINE_STATE;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub type PipelineState = WeakPtr<d3d12::ID3D12PipelineState>;
|
||||
pub type PipelineState = ComPtr<d3d12::ID3D12PipelineState>;
|
||||
|
||||
#[repr(u32)]
|
||||
pub enum Subobject {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::com::WeakPtr;
|
||||
use crate::com::ComPtr;
|
||||
use winapi::um::d3d12;
|
||||
|
||||
#[repr(u32)]
|
||||
@ -12,4 +12,4 @@ pub enum QueryHeapType {
|
||||
// CopyQueueTimestamp = d3d12::D3D12_QUERY_HEAP_TYPE_COPY_QUEUE_TIMESTAMP,
|
||||
}
|
||||
|
||||
pub type QueryHeap = WeakPtr<d3d12::ID3D12QueryHeap>;
|
||||
pub type QueryHeap = ComPtr<d3d12::ID3D12QueryHeap>;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{com::WeakPtr, sync::Fence, CommandList, HRESULT};
|
||||
use crate::{com::ComPtr, sync::Fence, CommandList, HRESULT};
|
||||
use winapi::um::d3d12;
|
||||
|
||||
#[repr(u32)]
|
||||
@ -15,7 +15,7 @@ bitflags! {
|
||||
}
|
||||
}
|
||||
|
||||
pub type CommandQueue = WeakPtr<d3d12::ID3D12CommandQueue>;
|
||||
pub type CommandQueue = ComPtr<d3d12::ID3D12CommandQueue>;
|
||||
|
||||
impl CommandQueue {
|
||||
pub fn execute_command_lists(&self, command_lists: &[CommandList]) {
|
||||
@ -26,7 +26,7 @@ impl CommandQueue {
|
||||
unsafe { self.ExecuteCommandLists(command_lists.len() as _, command_lists.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn signal(&self, fence: Fence, value: u64) -> HRESULT {
|
||||
pub fn signal(&self, fence: &Fence, value: u64) -> HRESULT {
|
||||
unsafe { self.Signal(fence.as_mut_ptr(), value) }
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! GPU Resource
|
||||
|
||||
use crate::{com::WeakPtr, D3DResult, Rect};
|
||||
use crate::{com::ComPtr, D3DResult, Rect};
|
||||
use std::{ops::Range, ptr};
|
||||
use winapi::um::d3d12;
|
||||
|
||||
@ -11,7 +11,7 @@ pub struct DiscardRegion<'a> {
|
||||
pub subregions: Range<Subresource>,
|
||||
}
|
||||
|
||||
pub type Resource = WeakPtr<d3d12::ID3D12Resource>;
|
||||
pub type Resource = ComPtr<d3d12::ID3D12Resource>;
|
||||
|
||||
impl Resource {
|
||||
///
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{com::WeakPtr, HRESULT};
|
||||
use crate::{com::ComPtr, HRESULT};
|
||||
use std::ptr;
|
||||
use winapi::um::{d3d12, synchapi, winnt};
|
||||
|
||||
@ -23,7 +23,7 @@ impl Event {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Fence = WeakPtr<d3d12::ID3D12Fence>;
|
||||
pub type Fence = ComPtr<d3d12::ID3D12Fence>;
|
||||
impl Fence {
|
||||
pub fn set_event_on_completion(&self, event: Event, value: u64) -> HRESULT {
|
||||
unsafe { self.SetEventOnCompletion(value, event.0) }
|
||||
|
Loading…
Reference in New Issue
Block a user