Direct3D11 improvements.

This commit is contained in:
Amer Koleci
2023-01-17 17:21:14 +01:00
parent 568352435f
commit 7b8eedd6a8
3 changed files with 156 additions and 77 deletions

View File

@@ -14,7 +14,7 @@
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<RestoreConfigFile>$(MSBuildThisFileDirectory)NuGet.config</RestoreConfigFile>
<VersionPrefix>1.9.13</VersionPrefix>
<VersionPrefix>1.9.14</VersionPrefix>
<VersionSuffix Condition="'$(VersionSuffix)' == ''"></VersionSuffix>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

View File

@@ -8,6 +8,7 @@ namespace Win32.Graphics.Direct3D11;
public static unsafe class ID3D11DeviceContextExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RSSetViewport<TD3D11DeviceContext>(
ref this TD3D11DeviceContext self,
float x, float y, float width, float height, float minDepth = 0.0f, float maxDepth = 1.0f)
@@ -17,6 +18,7 @@ public static unsafe class ID3D11DeviceContextExtensions
self.RSSetViewports(1, &viewport);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RSSetViewport<TD3D11DeviceContext>(
ref this TD3D11DeviceContext self,
Viewport viewport)
@@ -25,6 +27,17 @@ public static unsafe class ID3D11DeviceContextExtensions
self.RSSetViewports(1, &viewport);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RSSetScissorRect<TD3D11DeviceContext>(
ref this TD3D11DeviceContext self,
int width, int height)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
Rect rawRect = Rect.Create(0, 0, width, height);
self.RSSetScissorRects(1, &rawRect);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RSSetScissorRect<TD3D11DeviceContext>(
ref this TD3D11DeviceContext self,
int x, int y, int width, int height)
@@ -34,6 +47,7 @@ public static unsafe class ID3D11DeviceContextExtensions
self.RSSetScissorRects(1, &rawRect);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RSSetScissorRect<TD3D11DeviceContext>(
ref this TD3D11DeviceContext self,
Rect rect)
@@ -42,6 +56,7 @@ public static unsafe class ID3D11DeviceContextExtensions
self.RSSetScissorRects(1, &rect);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OMSetBlendState<TD3D11DeviceContext>(
ref this TD3D11DeviceContext self,
ID3D11BlendState* blendState)
@@ -50,6 +65,7 @@ public static unsafe class ID3D11DeviceContextExtensions
self.OMSetBlendState(blendState, null, D3D11_DEFAULT_SAMPLE_MASK);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OMSetBlendState<TD3D11DeviceContext>(
ref this TD3D11DeviceContext self,
ID3D11BlendState* blendState, float* blendFactor)
@@ -57,126 +73,178 @@ public static unsafe class ID3D11DeviceContextExtensions
{
self.OMSetBlendState(blendState, blendFactor, D3D11_DEFAULT_SAMPLE_MASK);
}
}
public unsafe partial struct ID3D11DeviceContext
{
public void IASetVertexBuffer(int slot, ID3D11Buffer* buffer, uint stride, uint offset = 0)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UnsetRenderTargets<TD3D11DeviceContext>(ref this TD3D11DeviceContext self)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
IASetVertexBuffers((uint)slot, 1, buffer == null ? null : &buffer, &stride, &offset);
self.OMSetRenderTargets(0, null, null);
}
public void VSSetShaderResource(uint slot, ID3D11ShaderResourceView* view)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OMSetRenderTargets<TD3D11DeviceContext>(ref this TD3D11DeviceContext self,
ID3D11RenderTargetView* renderTargetView, ID3D11DepthStencilView* depthStencilView = null)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
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 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 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 void UnsetRenderTargets()
{
OMSetRenderTargets(0, null, null);
}
public void OMSetRenderTargets(ID3D11RenderTargetView* renderTargetView, ID3D11DepthStencilView* depthStencilView = null)
{
OMSetRenderTargets(1,
self.OMSetRenderTargets(1,
renderTargetView == null ? null : &renderTargetView,
depthStencilView);
}
public void ClearRenderTargetView(ID3D11RenderTargetView* renderTargetView, Color4 color)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OMSetRenderTargets<TD3D11DeviceContext>(ref this TD3D11DeviceContext self,
ID3D11RenderTargetView*[] renderTargetViews, ID3D11DepthStencilView* depthStencilView = default)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
ClearRenderTargetView(renderTargetView, (float*)&color);
fixed (ID3D11RenderTargetView** ppRenderTargetViews = renderTargetViews)
{
self.OMSetRenderTargets((uint)renderTargetViews.Length, ppRenderTargetViews, depthStencilView);
}
}
public void ClearUnorderedAccessViewFloat(ID3D11UnorderedAccessView* unorderedAccessView, Color4 color)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IASetVertexBuffer<TD3D11DeviceContext>(ref this TD3D11DeviceContext self,
int slot, ID3D11Buffer* buffer, uint stride, uint offset = 0)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
ClearUnorderedAccessViewFloat(unorderedAccessView, (float*)&color);
self.IASetVertexBuffers((uint)slot, 1, buffer == null ? null : &buffer, &stride, &offset);
}
public HResult Map(ID3D11Texture2D* resource,
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ClearRenderTargetView<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, ID3D11RenderTargetView* renderTargetView, Color4 color)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.ClearRenderTargetView(renderTargetView, (float*)&color);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ClearUnorderedAccessViewFloat<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, ID3D11UnorderedAccessView* unorderedAccessView, Color4 color)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.ClearUnorderedAccessViewFloat(unorderedAccessView, (float*)&color);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ComPtr<ID3D11CommandList> FinishCommandList<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, bool restoreDeferredContextState = false)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
using ComPtr<ID3D11CommandList> commandList = default;
ThrowIfFailed(self.FinishCommandList(restoreDeferredContextState, commandList.GetAddressOf()));
return commandList.Move();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HResult Map<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, ID3D11Texture2D* resource,
uint mipSlice, uint arraySlice,
MapMode mode, MapFlags flags,
MappedSubresource* pMappedResource, out uint subresource, out uint mipSize)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
subresource = resource->CalculateSubResourceIndex(mipSlice, arraySlice, out mipSize);
return Map((ID3D11Resource*)resource, subresource, mode, flags, pMappedResource);
return self.Map((ID3D11Resource*)resource, subresource, mode, flags, pMappedResource);
}
public Span<T> Map<T>(ID3D11Texture2D* resource,
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> Map<TD3D11DeviceContext, T>(ref this TD3D11DeviceContext self, ID3D11Texture2D* resource,
uint mipSlice, uint arraySlice,
MapMode mode = MapMode.Read, MapFlags flags = MapFlags.None) where T : unmanaged
MapMode mode = MapMode.Read, MapFlags flags = MapFlags.None)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
where T : unmanaged
{
uint subresource = resource->CalculateSubResourceIndex(mipSlice, arraySlice, out uint mipSize);
MappedSubresource mappedSubresource;
ThrowIfFailed(Map((ID3D11Resource*)resource, subresource, mode, flags, &mappedSubresource));
ThrowIfFailed(self.Map((ID3D11Resource*)resource, subresource, mode, flags, &mappedSubresource));
Span<byte> source = new(mappedSubresource.pData, (int)(mipSize * mappedSubresource.RowPitch));
return global::System.Runtime.InteropServices.MemoryMarshal.Cast<byte, T>(source);
}
public void Unmap(ID3D11Texture1D* resource, uint mipSlice, uint arraySlice)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Unmap<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, ID3D11Texture1D* resource, uint mipSlice, uint arraySlice)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
uint subresource = resource->CalculateSubResourceIndex(mipSlice, arraySlice, out _);
Unmap((ID3D11Resource*)resource, subresource);
self.Unmap((ID3D11Resource*)resource, subresource);
}
public void Unmap(ID3D11Texture2D* resource, uint mipSlice, uint arraySlice)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Unmap<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, ID3D11Texture2D* resource, uint mipSlice, uint arraySlice)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
uint subresource = resource->CalculateSubResourceIndex(mipSlice, arraySlice, out _);
Unmap((ID3D11Resource*)resource, subresource);
self.Unmap((ID3D11Resource*)resource, subresource);
}
public void Unmap(ID3D11Texture3D* resource, uint mipSlice, uint arraySlice)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Unmap<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, ID3D11Texture3D* resource, uint mipSlice, uint arraySlice)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
uint subresource = resource->CalculateSubResourceIndex(mipSlice, arraySlice, out _);
Unmap((ID3D11Resource*)resource, subresource);
self.Unmap((ID3D11Resource*)resource, subresource);
}
public ComPtr<ID3D11CommandList> FinishCommandList(bool RestoreDeferredContextState = false)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void VSSetShaderResource<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11ShaderResourceView* view)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
using ComPtr<ID3D11CommandList> commandList = default;
ThrowIfFailed(FinishCommandList(RestoreDeferredContextState, commandList.GetAddressOf()));
self.VSSetShaderResources(slot, 1, view != null ? &view : null);
}
return commandList.Move();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void VSSetSampler<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11SamplerState* sampler)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.VSSetSamplers(slot, 1, sampler != null ? &sampler : null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void VSSetConstantBuffer<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11Buffer* constantBuffer)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.VSSetConstantBuffers(slot, 1, constantBuffer != null ? &constantBuffer : null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PSSetShaderResource<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11ShaderResourceView* view)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.PSSetShaderResources(slot, 1, view != null ? &view : null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PSSetSampler<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11SamplerState* sampler)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.PSSetSamplers(slot, 1, sampler != null ? &sampler : null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PSSetConstantBuffer<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11Buffer* constantBuffer)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.PSSetConstantBuffers(slot, 1, constantBuffer != null ? &constantBuffer : null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CSSetShaderResource<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11ShaderResourceView* view)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.CSSetShaderResources(slot, 1, view != null ? &view : null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CSSetSampler<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11SamplerState* sampler)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.CSSetSamplers(slot, 1, sampler != null ? &sampler : null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CSSetConstantBuffer<TD3D11DeviceContext>(ref this TD3D11DeviceContext self, uint slot, ID3D11Buffer* constantBuffer)
where TD3D11DeviceContext : unmanaged, ID3D11DeviceContext.Interface
{
self.CSSetConstantBuffers(slot, 1, constantBuffer != null ? &constantBuffer : null);
}
}

View File

@@ -286,6 +286,17 @@ public unsafe struct ComPtr<T> : IDisposable
return GetAddressOf();
}
/// <summary>
/// Releases the current COM object in use and gets the address of the <see cref="ComPtr{T}"/> instance as a void* double pointer.
/// This method is only valid when the current <see cref="ComPtr{T}"/> instance is on the stack or pinned.</summary>
/// <returns>The raw pointer to the current <see cref="ComPtr{T}"/> instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void** ReleaseAndGetVoidAddressOf()
{
_ = InternalRelease();
return GetVoidAddressOf();
}
/// <summary>
/// Moves the current <see cref="ComPtr{T}"/> instance and resets it without releasing the reference.
/// </summary>