From 328e6004738338e1d3524ad3285dff9d4e2ccf72 Mon Sep 17 00:00:00 2001 From: Amer Koleci Date: Mon, 26 Sep 2022 16:43:44 +0200 Subject: [PATCH] More D3D11 goodies --- .../Graphics/Direct3D11.Manual.cs | 50 ------------- src/Vortice.Win32/Graphics/Direct3D11/Apis.cs | 75 +++++++++++++++++++ .../Direct3D11/ID3D11DeviceContext.cs | 71 ++++++++++++++++++ .../Graphics/Direct3D11/ID3D11Texture1D.cs | 18 +++++ .../Graphics/Direct3D11/ID3D11Texture2D.cs | 18 +++++ .../Graphics/Direct3D11/ID3D11Texture3D.cs | 18 +++++ .../Graphics/Direct3D11/MappedSubresource.cs | 49 ++++++++++++ src/Vortice.Win32/Vortice.Win32.csproj | 2 +- 8 files changed, 250 insertions(+), 51 deletions(-) create mode 100644 src/Vortice.Win32/Graphics/Direct3D11/Apis.cs create mode 100644 src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture1D.cs create mode 100644 src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture2D.cs create mode 100644 src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture3D.cs create mode 100644 src/Vortice.Win32/Graphics/Direct3D11/MappedSubresource.cs diff --git a/src/Vortice.Win32/Graphics/Direct3D11.Manual.cs b/src/Vortice.Win32/Graphics/Direct3D11.Manual.cs index f6fdcde..974c4ee 100644 --- a/src/Vortice.Win32/Graphics/Direct3D11.Manual.cs +++ b/src/Vortice.Win32/Graphics/Direct3D11.Manual.cs @@ -55,53 +55,3 @@ public partial struct AuthenticatedProtectionFlags } } } - - -public static unsafe partial class Apis -{ - public static HResult D3D11CreateDevice( - IDXGIAdapter* adapter, - DriverType driverType, - CreateDeviceFlags flags, - ID3D11Device** ppDevice, - FeatureLevel* pFeatureLevel, - ID3D11DeviceContext** ppImmediateContext) - { - return D3D11CreateDevice( - adapter, - driverType, - IntPtr.Zero, - flags, - null, - 0u, - D3D11_SDK_VERSION, - ppDevice, - pFeatureLevel, - ppImmediateContext); - } - - public static HResult D3D11CreateDevice( - IDXGIAdapter* pAdapter, - DriverType driverType, - CreateDeviceFlags flags, - ReadOnlySpan featureLevels, - ID3D11Device** ppDevice, - FeatureLevel* pFeatureLevel, - ID3D11DeviceContext** ppImmediateContext) - { - fixed (FeatureLevel* pfeatureLevels = featureLevels) - { - return D3D11CreateDevice( - pAdapter, - driverType, - IntPtr.Zero, - flags, - pfeatureLevels, - (uint)featureLevels.Length, - D3D11_SDK_VERSION, - ppDevice, - pFeatureLevel, - ppImmediateContext); - } - } -} diff --git a/src/Vortice.Win32/Graphics/Direct3D11/Apis.cs b/src/Vortice.Win32/Graphics/Direct3D11/Apis.cs new file mode 100644 index 0000000..160f3ac --- /dev/null +++ b/src/Vortice.Win32/Graphics/Direct3D11/Apis.cs @@ -0,0 +1,75 @@ +// Copyright © Amer Koleci and Contributors. +// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. + +using Win32.Graphics.Direct3D; +using Win32.Graphics.Dxgi; + +namespace Win32.Graphics.Direct3D11; + +public static unsafe partial class Apis +{ + public static uint D3D11CalcSubresource(uint MipSlice, uint ArraySlice, uint MipLevels) + { + return MipSlice + ArraySlice * MipLevels; + } + + /// + /// Calculates the resulting size at a single level for an original size. + /// + /// The mip level to get the size. + /// Size of the base. + /// + /// Size of the mipLevel + /// + public static uint D3D11CalculateMipSize(uint mipLevel, uint baseSize) + { + baseSize = baseSize >> (int)mipLevel; + return baseSize > 0 ? baseSize : 1; + } + + public static HResult D3D11CreateDevice( + IDXGIAdapter* adapter, + DriverType driverType, + CreateDeviceFlags flags, + ID3D11Device** ppDevice, + FeatureLevel* pFeatureLevel, + ID3D11DeviceContext** ppImmediateContext) + { + return D3D11CreateDevice( + adapter, + driverType, + IntPtr.Zero, + flags, + null, + 0u, + D3D11_SDK_VERSION, + ppDevice, + pFeatureLevel, + ppImmediateContext); + } + + public static HResult D3D11CreateDevice( + IDXGIAdapter* pAdapter, + DriverType driverType, + CreateDeviceFlags flags, + ReadOnlySpan featureLevels, + ID3D11Device** ppDevice, + FeatureLevel* pFeatureLevel, + ID3D11DeviceContext** ppImmediateContext) + { + fixed (FeatureLevel* pfeatureLevels = featureLevels) + { + return D3D11CreateDevice( + pAdapter, + driverType, + IntPtr.Zero, + flags, + pfeatureLevels, + (uint)featureLevels.Length, + D3D11_SDK_VERSION, + ppDevice, + pFeatureLevel, + ppImmediateContext); + } + } +} diff --git a/src/Vortice.Win32/Graphics/Direct3D11/ID3D11DeviceContext.cs b/src/Vortice.Win32/Graphics/Direct3D11/ID3D11DeviceContext.cs index e46ccda..30f4ab5 100644 --- a/src/Vortice.Win32/Graphics/Direct3D11/ID3D11DeviceContext.cs +++ b/src/Vortice.Win32/Graphics/Direct3D11/ID3D11DeviceContext.cs @@ -1,6 +1,8 @@ // Copyright © Amer Koleci and Contributors. // Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. +using static Win32.Graphics.Direct3D11.Apis; + namespace Win32.Graphics.Direct3D11; public unsafe partial struct ID3D11DeviceContext @@ -27,6 +29,75 @@ public unsafe partial struct ID3D11DeviceContext RSSetScissorRects(1, &rect); } + public void OMSetBlendState(ID3D11BlendState* blendState, float* blendFactor) + { + OMSetBlendState(blendState, blendFactor, D3D11_DEFAULT_SAMPLE_MASK); + } + + public void OMSetBlendState(ID3D11BlendState* blendState) + { + OMSetBlendState(blendState, null, D3D11_DEFAULT_SAMPLE_MASK); + } + + public void VSSetShader(ID3D11VertexShader* shader) + { + VSSetShader(shader, null, 0); + } + + public void VSSetShaderResource(uint slot, ID3D11ShaderResourceView* view) + { + VSSetShaderResources(slot, 1, view != null ? &view : null); + } + + public void VSSetSampler(uint slot, ID3D11SamplerState* sampler) + { + VSSetSamplers(slot, 1, sampler != null ? &sampler : null); + } + + public void VSSetConstantBuffer(uint slot, ID3D11Buffer* constantBuffer) + { + VSSetConstantBuffers(slot, 1, constantBuffer != null ? &constantBuffer : null); + } + + public void PSSetShader(ID3D11PixelShader* shader) + { + PSSetShader(shader, null, 0); + } + + public void PSSetShaderResource(uint slot, ID3D11ShaderResourceView* view) + { + PSSetShaderResources(slot, 1, view != null ? &view : null); + } + + public void PSSetSampler(uint slot, ID3D11SamplerState* sampler) + { + PSSetSamplers(slot, 1, sampler != null ? &sampler : null); + } + + public void PSSetConstantBuffer(uint slot, ID3D11Buffer* constantBuffer) + { + PSSetConstantBuffers(slot, 1, constantBuffer != null ? &constantBuffer : null); + } + + public void CSSetShader(ID3D11ComputeShader* shader) + { + CSSetShader(shader, null, 0); + } + + public void CSSetShaderResource(uint slot, ID3D11ShaderResourceView* view) + { + CSSetShaderResources(slot, 1, view != null ? &view : null); + } + + public void CSSetSampler(uint slot, ID3D11SamplerState* sampler) + { + CSSetSamplers(slot, 1, sampler != null ? &sampler : null); + } + + public void CSSetConstantBuffer(uint slot, ID3D11Buffer* constantBuffer) + { + CSSetConstantBuffers(slot, 1, constantBuffer != null ? &constantBuffer : null); + } public ComPtr FinishCommandList(bool RestoreDeferredContextState = false) { diff --git a/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture1D.cs b/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture1D.cs new file mode 100644 index 0000000..df6f3c5 --- /dev/null +++ b/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture1D.cs @@ -0,0 +1,18 @@ +// Copyright © Amer Koleci and Contributors. +// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. + +using static Win32.Graphics.Direct3D11.Apis; + +namespace Win32.Graphics.Direct3D11; + +public unsafe partial struct ID3D11Texture1D +{ + public uint CalculateSubResourceIndex(uint mipSlice, uint arraySlice, out uint mipSize) + { + Texture1DDescription desc; + GetDesc(&desc); + + mipSize = D3D11CalculateMipSize(mipSlice, desc.Width); + return D3D11CalcSubresource(mipSlice, arraySlice, desc.MipLevels); + } +} diff --git a/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture2D.cs b/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture2D.cs new file mode 100644 index 0000000..42c76a7 --- /dev/null +++ b/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture2D.cs @@ -0,0 +1,18 @@ +// Copyright © Amer Koleci and Contributors. +// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. + +using static Win32.Graphics.Direct3D11.Apis; + +namespace Win32.Graphics.Direct3D11; + +public unsafe partial struct ID3D11Texture2D +{ + public uint CalculateSubResourceIndex(uint mipSlice, uint arraySlice, out uint mipSize) + { + Texture2DDescription desc; + GetDesc(&desc); + + mipSize = D3D11CalculateMipSize(mipSlice, desc.Height); + return D3D11CalcSubresource(mipSlice, arraySlice, desc.MipLevels); + } +} diff --git a/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture3D.cs b/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture3D.cs new file mode 100644 index 0000000..506879e --- /dev/null +++ b/src/Vortice.Win32/Graphics/Direct3D11/ID3D11Texture3D.cs @@ -0,0 +1,18 @@ +// Copyright © Amer Koleci and Contributors. +// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. + +using static Win32.Graphics.Direct3D11.Apis; + +namespace Win32.Graphics.Direct3D11; + +public unsafe partial struct ID3D11Texture3D +{ + public uint CalculateSubResourceIndex(uint mipSlice, uint arraySlice, out uint mipSize) + { + Texture3DDescription desc; + GetDesc(&desc); + + mipSize = D3D11CalculateMipSize(mipSlice, desc.Depth); + return D3D11CalcSubresource(mipSlice, arraySlice, desc.MipLevels); + } +} diff --git a/src/Vortice.Win32/Graphics/Direct3D11/MappedSubresource.cs b/src/Vortice.Win32/Graphics/Direct3D11/MappedSubresource.cs new file mode 100644 index 0000000..0da57bb --- /dev/null +++ b/src/Vortice.Win32/Graphics/Direct3D11/MappedSubresource.cs @@ -0,0 +1,49 @@ +// Copyright © Amer Koleci and Contributors. +// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. + +using static Win32.Graphics.Direct3D11.Apis; + +namespace Win32.Graphics.Direct3D11; + +public unsafe partial struct MappedSubresource +{ + public Span AsSpan(int length) => new(pData, length); + + public Span AsSpan(int length) where T : unmanaged + { + return new Span(pData, length); + } + + public Span AsSpan(ID3D11Buffer* buffer) where T : unmanaged + { + BufferDescription desc; + buffer->GetDesc(&desc); + + Span source = new(pData, (int)desc.ByteWidth); + return global::System.Runtime.InteropServices.MemoryMarshal.Cast(source); + } + + public Span AsSpan(ID3D11Texture1D* resource, uint mipSlice, uint arraySlice) where T : unmanaged + { + resource->CalculateSubResourceIndex(mipSlice, arraySlice, out uint mipSize); + + Span source = new(pData, (int)(mipSize * RowPitch)); + return global::System.Runtime.InteropServices.MemoryMarshal.Cast(source); + } + + public Span AsSpan(ID3D11Texture2D* resource, uint mipSlice, uint arraySlice) where T : unmanaged + { + resource->CalculateSubResourceIndex(mipSlice, arraySlice, out uint mipSize); + + Span source = new Span(pData, (int)(mipSize * RowPitch)); + return global::System.Runtime.InteropServices.MemoryMarshal.Cast(source); + } + + public Span AsSpan(ID3D11Texture3D* resource, uint mipSlice, uint arraySlice) where T : unmanaged + { + resource->CalculateSubResourceIndex(mipSlice, arraySlice, out uint mipSize); + + Span source = new(pData, (int)(mipSize * DepthPitch)); + return global::System.Runtime.InteropServices.MemoryMarshal.Cast(source); + } +} diff --git a/src/Vortice.Win32/Vortice.Win32.csproj b/src/Vortice.Win32/Vortice.Win32.csproj index 64b4261..1e89fc9 100644 --- a/src/Vortice.Win32/Vortice.Win32.csproj +++ b/src/Vortice.Win32/Vortice.Win32.csproj @@ -3,7 +3,7 @@ netstandard2.0;net6.0;net7.0 Windows API low level bindings. - 1.6.4 + 1.6.5 true True