More D3D11 goodies

This commit is contained in:
Amer Koleci
2022-09-26 16:43:44 +02:00
parent 5b46d60c9a
commit 328e600473
8 changed files with 250 additions and 51 deletions

View File

@@ -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<FeatureLevel> 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);
}
}
}

View File

@@ -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;
}
/// <summary>
/// Calculates the resulting size at a single level for an original size.
/// </summary>
/// <param name="mipLevel">The mip level to get the size.</param>
/// <param name="baseSize">Size of the base.</param>
/// <returns>
/// Size of the mipLevel
/// </returns>
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<FeatureLevel> 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);
}
}
}

View File

@@ -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<ID3D11CommandList> FinishCommandList(bool RestoreDeferredContextState = false)
{

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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<byte> AsSpan(int length) => new(pData, length);
public Span<T> AsSpan<T>(int length) where T : unmanaged
{
return new Span<T>(pData, length);
}
public Span<T> AsSpan<T>(ID3D11Buffer* buffer) where T : unmanaged
{
BufferDescription desc;
buffer->GetDesc(&desc);
Span<byte> source = new(pData, (int)desc.ByteWidth);
return global::System.Runtime.InteropServices.MemoryMarshal.Cast<byte, T>(source);
}
public Span<T> AsSpan<T>(ID3D11Texture1D* resource, uint mipSlice, uint arraySlice) where T : unmanaged
{
resource->CalculateSubResourceIndex(mipSlice, arraySlice, out uint mipSize);
Span<byte> source = new(pData, (int)(mipSize * RowPitch));
return global::System.Runtime.InteropServices.MemoryMarshal.Cast<byte, T>(source);
}
public Span<T> AsSpan<T>(ID3D11Texture2D* resource, uint mipSlice, uint arraySlice) where T : unmanaged
{
resource->CalculateSubResourceIndex(mipSlice, arraySlice, out uint mipSize);
Span<byte> source = new Span<byte>(pData, (int)(mipSize * RowPitch));
return global::System.Runtime.InteropServices.MemoryMarshal.Cast<byte, T>(source);
}
public Span<T> AsSpan<T>(ID3D11Texture3D* resource, uint mipSlice, uint arraySlice) where T : unmanaged
{
resource->CalculateSubResourceIndex(mipSlice, arraySlice, out uint mipSize);
Span<byte> source = new(pData, (int)(mipSize * DepthPitch));
return global::System.Runtime.InteropServices.MemoryMarshal.Cast<byte, T>(source);
}
}

View File

@@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<Description>Windows API low level bindings.</Description>
<VersionPrefix>1.6.4</VersionPrefix>
<VersionPrefix>1.6.5</VersionPrefix>
<VersionSuffix Condition="'$(VersionSuffix)' == ''"></VersionSuffix>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<EnablePreviewFeatures>True</EnablePreviewFeatures>