Improvements in Direct3D11 bindings and add helper structs + methods.

This commit is contained in:
Amer Koleci
2022-09-07 11:35:15 +02:00
parent b1f807dcfb
commit 05b829f36d
14 changed files with 9580 additions and 3794 deletions

View File

@@ -228,6 +228,7 @@ public static class Program
{ "D3D11_MAP_FLAG", "MapFlags" }, { "D3D11_MAP_FLAG", "MapFlags" },
{ "D3D11_FORMAT_SUPPORT", "FormatSupport" }, { "D3D11_FORMAT_SUPPORT", "FormatSupport" },
{ "D3D11_FORMAT_SUPPORT2", "FormatSupport2" }, { "D3D11_FORMAT_SUPPORT2", "FormatSupport2" },
{ "D3D11_DSV_FLAG", "DsvFlags" },
}; };
private static readonly Dictionary<string, string> s_structFieldTypeRemap = new() private static readonly Dictionary<string, string> s_structFieldTypeRemap = new()
@@ -258,6 +259,8 @@ public static class Program
{ "D3D11_FEATURE_DATA_FORMAT_SUPPORT::OutFormatSupport", "D3D11_FORMAT_SUPPORT" }, { "D3D11_FEATURE_DATA_FORMAT_SUPPORT::OutFormatSupport", "D3D11_FORMAT_SUPPORT" },
{ "D3D11_FEATURE_DATA_FORMAT_SUPPORT2::OutFormatSupport2", "D3D11_FORMAT_SUPPORT2" }, { "D3D11_FEATURE_DATA_FORMAT_SUPPORT2::OutFormatSupport2", "D3D11_FORMAT_SUPPORT2" },
{ "D3D11_DEPTH_STENCIL_VIEW_DESC::Flags", "D3D11_DSV_FLAG" },
}; };
private static readonly HashSet<string> s_visitedEnums = new(); private static readonly HashSet<string> s_visitedEnums = new();

View File

@@ -262,6 +262,20 @@ public unsafe struct ComPtr<T> : IDisposable
return GetAddressOf(); return GetAddressOf();
} }
/// <summary>
/// Moves the current <see cref="ComPtr{T}"/> instance and resets it without releasing the reference.
/// </summary>
/// <returns>The moved <see cref="ComPtr{T}"/> instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ComPtr<T> Move()
{
ComPtr<T> copy = default;
Unsafe.AsRef(in this).Swap(ref copy);
return copy;
}
/// <summary>Resets the current instance by decrementing the reference count for the target COM object and setting the internal raw pointer to <see langword="null"/>.</summary> /// <summary>Resets the current instance by decrementing the reference count for the target COM object and setting the internal raw pointer to <see langword="null"/>.</summary>
/// <returns>The updated reference count for the COM object that was in use, if any.</returns> /// <returns>The updated reference count for the COM object that was in use, if any.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

File diff suppressed because it is too large Load Diff

View File

@@ -1051,7 +1051,7 @@ public enum BufferExtendedSrvFlag : int
/// <include file='../Direct3D11.xml' path='doc/member[@name="D3D11_DSV_FLAG"]/*' /> /// <include file='../Direct3D11.xml' path='doc/member[@name="D3D11_DSV_FLAG"]/*' />
/// <unmanaged>D3D11_DSV_FLAG</unmanaged> /// <unmanaged>D3D11_DSV_FLAG</unmanaged>
[Flags] [Flags]
public enum DsvFlag : int public enum DsvFlags : int
{ {
None = 0, None = 0,
/// <include file='../Direct3D11.xml' path='doc/member[@name="D3D11_DSV_FLAG::D3D11_DSV_READ_ONLY_DEPTH"]/*' /> /// <include file='../Direct3D11.xml' path='doc/member[@name="D3D11_DSV_FLAG::D3D11_DSV_READ_ONLY_DEPTH"]/*' />
@@ -8352,7 +8352,7 @@ public partial struct DepthStencilViewDescription
public DsvDimension ViewDimension; public DsvDimension ViewDimension;
/// <include file='../Direct3D11.xml' path='doc/member[@name="D3D11_DEPTH_STENCIL_VIEW_DESC::Flags"]/*' /> /// <include file='../Direct3D11.xml' path='doc/member[@name="D3D11_DEPTH_STENCIL_VIEW_DESC::Flags"]/*' />
public uint Flags; public DsvFlags Flags;
/// <include file='../Direct3D11.xml' path='doc/member[@name="D3D11_DEPTH_STENCIL_VIEW_DESC::Anonymous"]/*' /> /// <include file='../Direct3D11.xml' path='doc/member[@name="D3D11_DEPTH_STENCIL_VIEW_DESC::Anonymous"]/*' />
public _Anonymous_e__Union Anonymous; public _Anonymous_e__Union Anonymous;

View File

@@ -4,7 +4,8 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Win32.Graphics.Direct3D; using Win32.Graphics.Direct3D;
using Win32.Graphics.Dxgi; using Win32.Graphics.Dxgi;
using static Win32.Graphics.Dxgi.Apis; using Win32.Graphics.Dxgi.Common;
using static Win32.Graphics.Direct3D11.Apis;
using static Win32.StringUtilities; using static Win32.StringUtilities;
namespace Win32.Graphics.Direct3D11; namespace Win32.Graphics.Direct3D11;
@@ -58,6 +59,7 @@ public partial struct AuthenticatedProtectionFlags
} }
} }
public static unsafe partial class Apis public static unsafe partial class Apis
{ {
public static HResult D3D11CreateDevice( public static HResult D3D11CreateDevice(

View File

@@ -0,0 +1,168 @@
// Copyright © Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
using Win32.Graphics.Dxgi.Common;
namespace Win32.Graphics.Direct3D11;
public unsafe partial struct DepthStencilViewDescription
{
/// <summary>
/// Initializes a new instance of the <see cref="DepthStencilViewDescription"/> struct.
/// </summary>
/// <param name="viewDimension">The <see cref="DsvDimension"/></param>
/// <param name="format">The <see cref="Format"/> to use or <see cref="Format.Unknown"/>.</param>
/// <param name="mipSlice">The index of the mipmap level to use mip slice.</param>
/// <param name="firstArraySlice">The index of the first texture to use in an array of textures.</param>
/// <param name="arraySize">Number of textures in the array.</param>
/// <param name="flags"></param>
public DepthStencilViewDescription(
DsvDimension viewDimension,
Format format = Format.Unknown,
uint mipSlice = 0,
uint firstArraySlice = 0,
uint arraySize = unchecked((uint)-1),
DsvFlags flags = DsvFlags.None)
{
Format = format;
ViewDimension = viewDimension;
Flags = flags;
Anonymous = default;
switch (viewDimension)
{
case DsvDimension.Texture1D:
Anonymous.Texture1D.MipSlice = mipSlice;
break;
case DsvDimension.Texture1DArray:
Anonymous.Texture1DArray.MipSlice = mipSlice;
Anonymous.Texture1DArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture1DArray.ArraySize = arraySize;
break;
case DsvDimension.Texture2D:
Anonymous.Texture2D.MipSlice = mipSlice;
break;
case DsvDimension.Texture2DArray:
Anonymous.Texture2DArray.MipSlice = mipSlice;
Anonymous.Texture2DArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture2DArray.ArraySize = arraySize;
break;
case DsvDimension.Texture2DMs:
break;
case DsvDimension.Texture2DMsArray:
Anonymous.Texture2DMSArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture2DMSArray.ArraySize = arraySize;
break;
default:
break;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DepthStencilViewDescription"/> struct.
/// </summary>
/// <param name="texture"></param>
/// <param name="isArray"></param>
/// <param name="format"></param>
/// <param name="mipSlice"></param>
/// <param name="firstArraySlice"></param>
/// <param name="arraySize"></param>
/// <param name="flags"></param>
public DepthStencilViewDescription(
ID3D11Texture1D* texture,
bool isArray,
Format format = Format.Unknown,
uint mipSlice = 0,
uint firstArraySlice = 0,
uint arraySize = unchecked((uint)-1),
DsvFlags flags = DsvFlags.None)
{
ViewDimension = isArray ? DsvDimension.Texture1DArray : DsvDimension.Texture1D;
Flags = flags;
Anonymous = default;
if (format == Format.Unknown
|| (arraySize == unchecked((uint)-1) && DsvDimension.Texture1DArray == ViewDimension))
{
Texture1DDescription textureDesc;
texture->GetDesc(&textureDesc);
if (format == Format.Unknown)
format = textureDesc.Format;
if (arraySize == unchecked((uint)-1))
arraySize = textureDesc.ArraySize - firstArraySlice;
}
Format = format;
switch (ViewDimension)
{
case DsvDimension.Texture1D:
Anonymous.Texture1D.MipSlice = mipSlice;
break;
case DsvDimension.Texture1DArray:
Anonymous.Texture1DArray.MipSlice = mipSlice;
Anonymous.Texture1DArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture1DArray.ArraySize = arraySize;
break;
default:
break;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DepthStencilViewDescription"/> struct.
/// </summary>
/// <param name="texture"></param>
/// <param name="viewDimension"></param>
/// <param name="format"></param>
/// <param name="mipSlice"></param>
/// <param name="firstArraySlice"></param>
/// <param name="arraySize"></param>
/// <param name="flags"></param>
public DepthStencilViewDescription(
ID3D11Texture2D* texture,
DsvDimension viewDimension,
Format format = Format.Unknown,
uint mipSlice = 0,
uint firstArraySlice = 0,
uint arraySize = unchecked((uint)-1),
DsvFlags flags = DsvFlags.None)
{
ViewDimension = viewDimension;
Flags = flags;
Anonymous = default;
if (format == Format.Unknown
|| (arraySize == unchecked((uint)-1) && (DsvDimension.Texture2DArray == viewDimension || DsvDimension.Texture2DMsArray == viewDimension)))
{
Texture2DDescription textureDesc;
texture->GetDesc(&textureDesc);
if (format == Format.Unknown)
format = textureDesc.Format;
if (arraySize == unchecked((uint)-1))
arraySize = textureDesc.ArraySize - firstArraySlice;
}
Format = format;
switch (viewDimension)
{
case DsvDimension.Texture2D:
Anonymous.Texture2D.MipSlice = mipSlice;
break;
case DsvDimension.Texture2DArray:
Anonymous.Texture2DArray.MipSlice = mipSlice;
Anonymous.Texture2DArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture2DArray.ArraySize = arraySize;
break;
case DsvDimension.Texture2DMs:
break;
case DsvDimension.Texture2DMsArray:
Anonymous.Texture2DMSArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture2DMSArray.ArraySize = arraySize;
break;
default:
break;
}
}
}

View File

@@ -0,0 +1,95 @@
// Copyright © Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
namespace Win32.Graphics.Direct3D11;
/// <summary>
/// A <see langword="class"/> with extensions for the <see cref="ID3D11Device"/> type.
/// </summary>
public static unsafe class ID3D11DeviceExtensions
{
public static ComPtr<ID3D11Buffer> CreateBuffer<T>(
this ref ID3D11Device device,
in T data,
BufferDescription description) where T : unmanaged
{
if (description.ByteWidth == 0)
description.ByteWidth = (uint)sizeof(T);
fixed (T* dataPtr = &data)
{
SubresourceData subresourceData = new()
{
pSysMem = dataPtr
};
using ComPtr<ID3D11Buffer> buffer = default;
device.CreateBuffer(&description, &subresourceData, buffer.GetAddressOf()).ThrowIfFailed();
return buffer.Move();
}
}
public static ComPtr<ID3D11Buffer> CreateBuffer<T>(this ref ID3D11Device device, ReadOnlySpan<T> data, BufferDescription description) where T : unmanaged
{
if (description.ByteWidth == 0)
description.ByteWidth = (uint)(sizeof(T) * data.Length);
fixed (T* dataPtr = data)
{
SubresourceData subresourceData = new()
{
pSysMem = dataPtr
};
using ComPtr<ID3D11Buffer> buffer = default;
device.CreateBuffer(&description, &subresourceData, buffer.GetAddressOf()).ThrowIfFailed();
return buffer.Move();
}
}
/// <summary>
/// Creates a new instance of the <see cref="ID3D11Buffer"/> class.
/// </summary>
/// <typeparam name="T">Type of the data to upload</typeparam>
/// <param name="device">The <see cref="ID3D11Device"/> instance.</param>
/// <param name="bindFlags">Flags specifying how the buffer will be bound to the pipeline.</param>
/// <param name="data">Initial data used to initialize the buffer.</param>
/// <param name="sizeInBytes">The size, in bytes, of the buffer. If 0 is specified, sizeof(T) * data.Length is used.</param>
/// <param name="usage">The usage pattern for the buffer.</param>
/// <param name="accessFlags">Flags specifying how the buffer will be accessible from the CPU.</param>
/// <param name="miscFlags">Miscellaneous resource options.</param>
/// <param name="structureByteStride">The size (in bytes) of the structure element for structured buffers.</param>
/// <returns>An initialized buffer</returns>
public static ComPtr<ID3D11Buffer> CreateBuffer<T>(
this ref ID3D11Device device,
ReadOnlySpan<T> data,
BindFlags bindFlags,
Usage usage = Usage.Default,
CpuAccessFlags accessFlags = CpuAccessFlags.None,
ResourceMiscFlags miscFlags = ResourceMiscFlags.None,
uint sizeInBytes = 0,
uint structureByteStride = 0) where T : unmanaged
{
BufferDescription description = new()
{
ByteWidth = sizeInBytes == 0 ? (uint)(sizeof(T) * data.Length) : sizeInBytes,
BindFlags = bindFlags,
CPUAccessFlags = accessFlags,
MiscFlags = miscFlags,
Usage = usage,
StructureByteStride = structureByteStride == 0 ? (uint)sizeof(T) : structureByteStride,
};
fixed (T* dataPtr = data)
{
SubresourceData subresourceData = new()
{
pSysMem = dataPtr
};
using ComPtr<ID3D11Buffer> buffer = default;
device.CreateBuffer(&description, &subresourceData, buffer.GetAddressOf()).ThrowIfFailed();
return buffer.Move();
}
}
}

View File

@@ -0,0 +1,217 @@
// Copyright © Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
using System.Xml.Serialization;
using Win32.Graphics.Dxgi.Common;
namespace Win32.Graphics.Direct3D11;
public unsafe partial struct RenderTargetViewDescription
{
/// <summary>
/// Initializes a new instance of the <see cref="RenderTargetViewDescription"/> struct.
/// </summary>
/// <param name="viewDimension">The <see cref="RtvDimension"/></param>
/// <param name="format">The <see cref="Format"/> to use or <see cref="Format.Unknown"/>.</param>
/// <param name="mipSlice">The index of the mipmap level to use mip slice. or first element for <see cref="RtvDimension.Buffer"/>.</param>
/// <param name="firstArraySlice">The index of the first texture to use in an array of textures or NumElements for <see cref="RtvDimension.Buffer"/>, FirstWSlice for <see cref="RtvDimension.Texture3D"/>.</param>
/// <param name="arraySize">Number of textures in the array or WSize for <see cref="RtvDimension.Texture3D"/>. </param>
public RenderTargetViewDescription(
RtvDimension viewDimension,
Format format = Format.Unknown,
uint mipSlice = 0,
uint firstArraySlice = 0,
uint arraySize = unchecked((uint)-1))
{
Format = format;
ViewDimension = viewDimension;
Anonymous = default;
switch (viewDimension)
{
case RtvDimension.Buffer:
Buffer.FirstElement = mipSlice;
Buffer.NumElements = firstArraySlice;
break;
case RtvDimension.Texture1D:
Texture1D.MipSlice = mipSlice;
break;
case RtvDimension.Texture1DArray:
Texture1DArray.MipSlice = mipSlice;
Texture1DArray.FirstArraySlice = firstArraySlice;
Texture1DArray.ArraySize = arraySize;
break;
case RtvDimension.Texture2D:
Texture2D.MipSlice = mipSlice;
break;
case RtvDimension.Texture2DArray:
Texture2DArray.MipSlice = mipSlice;
Texture2DArray.FirstArraySlice = firstArraySlice;
Texture2DArray.ArraySize = arraySize;
break;
case RtvDimension.Texture2DMs:
break;
case RtvDimension.Texture2DMsArray:
Texture2DMSArray.FirstArraySlice = firstArraySlice;
Texture2DMSArray.ArraySize = arraySize;
break;
case RtvDimension.Texture3D:
Texture3D.MipSlice = mipSlice;
Texture3D.FirstWSlice = firstArraySlice;
Texture3D.WSize = arraySize;
break;
default:
break;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="RenderTargetViewDescription"/> struct.
/// </summary>
/// <param name="buffer">Unused <see cref="ID3D11Buffer"/> </param>
/// <param name="format"></param>
/// <param name="firstElement"></param>
/// <param name="numElements"></param>
public RenderTargetViewDescription(
ID3D11Buffer* buffer,
Format format,
uint firstElement,
uint numElements)
{
Format = format;
ViewDimension = RtvDimension.Buffer;
Anonymous = default;
Anonymous.Buffer.FirstElement = firstElement;
Anonymous.Buffer.NumElements = numElements;
}
public RenderTargetViewDescription(
ID3D11Texture1D* texture,
bool isArray,
Format format = Format.Unknown,
uint mipSlice = 0,
uint firstArraySlice = 0,
uint arraySize = unchecked((uint)-1))
{
ViewDimension = isArray ? RtvDimension.Texture1DArray : RtvDimension.Texture1D;
if (format == Format.Unknown
|| (arraySize == unchecked((uint)-1) && RtvDimension.Texture1DArray == ViewDimension))
{
Texture1DDescription textureDesc;
texture->GetDesc(&textureDesc);
if (format == Format.Unknown)
format = textureDesc.Format;
if (arraySize == unchecked((uint)-1))
arraySize = textureDesc.ArraySize - firstArraySlice;
}
Format = format;
Anonymous = default;
switch (ViewDimension)
{
case RtvDimension.Texture1D:
Anonymous.Texture1D.MipSlice = mipSlice;
break;
case RtvDimension.Texture1DArray:
Anonymous.Texture1DArray.MipSlice = mipSlice;
Anonymous.Texture1DArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture1DArray.ArraySize = arraySize;
break;
default:
break;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="RenderTargetViewDescription"/> struct.
/// </summary>
/// <param name="texture"></param>
/// <param name="viewDimension"></param>
/// <param name="format"></param>
/// <param name="mipSlice"></param>
/// <param name="firstArraySlice"></param>
/// <param name="arraySize"></param>
public RenderTargetViewDescription(
ID3D11Texture2D* texture,
RtvDimension viewDimension,
Format format = Format.Unknown,
uint mipSlice = 0,
uint firstArraySlice = 0,
uint arraySize = unchecked((uint)-1))
{
ViewDimension = viewDimension;
if (format == Format.Unknown
|| (arraySize == unchecked((uint)-1) && (RtvDimension.Texture2DArray == viewDimension || RtvDimension.Texture2DMsArray == viewDimension)))
{
Texture2DDescription textureDesc;
texture->GetDesc(&textureDesc);
if (format == Format.Unknown)
format = textureDesc.Format;
if (arraySize == unchecked((uint)-1))
{
arraySize = textureDesc.ArraySize - firstArraySlice;
}
}
Format = format;
Anonymous = default;
switch (viewDimension)
{
case RtvDimension.Texture2D:
Anonymous.Texture2D.MipSlice = mipSlice;
break;
case RtvDimension.Texture2DArray:
Anonymous.Texture2DArray.MipSlice = mipSlice;
Anonymous.Texture2DArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture2DArray.ArraySize = arraySize;
break;
case RtvDimension.Texture2DMs:
break;
case RtvDimension.Texture2DMsArray:
Anonymous.Texture2DMSArray.FirstArraySlice = firstArraySlice;
Anonymous.Texture2DMSArray.ArraySize = arraySize;
break;
default:
break;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="RenderTargetViewDescription"/> struct.
/// </summary>
/// <param name="texture"></param>
/// <param name="format"></param>
/// <param name="mipSlice"></param>
/// <param name="firstWSlice"></param>
/// <param name="wSize"></param>
public RenderTargetViewDescription(
ID3D11Texture3D* texture,
Format format = Format.Unknown,
uint mipSlice = 0,
uint firstWSlice = 0,
uint wSize = unchecked((uint)-1))
{
ViewDimension = RtvDimension.Texture3D;
if (format == Format.Unknown || wSize == unchecked((uint)-1))
{
Texture3DDescription textureDesc;
texture->GetDesc(&textureDesc);
if (format == Format.Unknown)
format = textureDesc.Format;
if (wSize == unchecked((uint)-1))
wSize = textureDesc.Depth - firstWSlice;
}
Format = format;
Anonymous = default;
Anonymous.Texture3D.MipSlice = mipSlice;
Anonymous.Texture3D.FirstWSlice = firstWSlice;
Anonymous.Texture3D.WSize = wSize;
}
}

View File

@@ -0,0 +1,50 @@
// Copyright © Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
using Win32.Graphics.Dxgi.Common;
using static Win32.Graphics.Direct3D11.Apis;
namespace Win32.Graphics.Direct3D11;
public partial struct Texture1DDescription
{
/// <summary>
/// Initializes a new instance of the <see cref="Texture1DDescription"/> struct.
/// </summary>
/// <param name="format">Texture format.</param>
/// <param name="width">Texture width (in texels).</param>
/// <param name="arraySize">Number of textures in the array.</param>
/// <param name="mipLevels">The maximum number of mipmap levels in the texture.</param>
/// <param name="bindFlags">The <see cref="Direct3D11.BindFlags"/> for binding to pipeline stages.</param>
/// <param name="usage">Value that identifies how the texture is to be read from and written to.</param>
/// <param name="cpuAccessFlags">The <see cref="Direct3D11.CpuAccessFlags"/> to specify the types of CPU access allowed.</param>
/// <param name="miscFlags">The <see cref="ResourceMiscFlags"/> that identify other, less common resource options. </param>
public Texture1DDescription(
Format format,
uint width,
uint arraySize = 1,
uint mipLevels = 0,
BindFlags bindFlags = BindFlags.ShaderResource,
Usage usage = Usage.Default,
CpuAccessFlags cpuAccessFlags = CpuAccessFlags.None,
ResourceMiscFlags miscFlags = ResourceMiscFlags.None)
{
if (format == Format.Unknown)
throw new ArgumentException($"format need to be valid", nameof(format));
if (width < 1 || width > D3D11_REQ_TEXTURE1D_U_DIMENSION)
throw new ArgumentException($"Width need to be in range 1-{D3D11_REQ_TEXTURE1D_U_DIMENSION}", nameof(width));
if (arraySize < 1 || arraySize > D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION)
throw new ArgumentException($"Array size need to be in range 1-{D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION}", nameof(arraySize));
Width = width;
MipLevels = mipLevels;
ArraySize = arraySize;
Format = format;
Usage = usage;
BindFlags = bindFlags;
CPUAccessFlags = cpuAccessFlags;
MiscFlags = miscFlags;
}
}

View File

@@ -0,0 +1,61 @@
// Copyright © Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
using Win32.Graphics.Dxgi.Common;
using static Win32.Graphics.Direct3D11.Apis;
namespace Win32.Graphics.Direct3D11;
public partial struct Texture2DDescription
{
/// <summary>
/// Initializes a new instance of the <see cref="Texture2DDescription"/> struct.
/// </summary>
/// <param name="format">Texture format.</param>
/// <param name="width">Texture width (in texels).</param>
/// <param name="height">Texture height (in texels).</param>
/// <param name="arraySize">Number of textures in the array.</param>
/// <param name="mipLevels">The maximum number of mipmap levels in the texture.</param>
/// <param name="bindFlags">The <see cref="Direct3D11.BindFlags"/> for binding to pipeline stages.</param>
/// <param name="usage">Value that identifies how the texture is to be read from and written to.</param>
/// <param name="cpuAccessFlags">The <see cref="CpuAccessFlags"/> to specify the types of CPU access allowed.</param>
/// <param name="sampleCount">Specifies multisampling parameters for the texture.</param>
/// <param name="sampleQuality">Specifies multisampling parameters for the texture.</param>
/// <param name="miscFlags">The <see cref="ResourceMiscFlags"/> that identify other, less common resource options. </param>
public Texture2DDescription(
Format format,
uint width,
uint height,
uint arraySize = 1,
uint mipLevels = 0,
BindFlags bindFlags = BindFlags.ShaderResource,
Usage usage = Usage.Default,
CpuAccessFlags cpuAccessFlags = CpuAccessFlags.None,
uint sampleCount = 1,
uint sampleQuality = 0,
ResourceMiscFlags miscFlags = ResourceMiscFlags.None)
{
if (format == Format.Unknown)
throw new ArgumentException($"format need to be valid", nameof(format));
if (width < 1 || width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)
throw new ArgumentException($"Width need to be in range 1-{D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION}", nameof(width));
if (height < 1 || height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)
throw new ArgumentException($"Height need to be in range 1-{D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION}", nameof(height));
if (arraySize < 1 || arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION)
throw new ArgumentException($"Array size need to be in range 1-{D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION}", nameof(arraySize));
Width = width;
Height = height;
MipLevels = mipLevels;
ArraySize = arraySize;
Format = format;
SampleDesc = new(sampleCount, sampleQuality);
Usage = usage;
BindFlags = bindFlags;
CPUAccessFlags = cpuAccessFlags;
MiscFlags = miscFlags;
}
}

View File

@@ -0,0 +1,56 @@
// Copyright © Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
using Win32.Graphics.Dxgi.Common;
using static Win32.Graphics.Direct3D11.Apis;
namespace Win32.Graphics.Direct3D11;
public partial struct Texture3DDescription
{
/// <summary>
/// Initializes a new instance of the <see cref="Texture3DDescription"/> struct.
/// </summary>
/// <param name="width">Texture width (in texels).</param>
/// <param name="height">Texture height (in texels).</param>
/// <param name="depth">Texture depth (in texels).</param>
/// <param name="format">Texture format.</param>
/// <param name="mipLevels">The maximum number of mipmap levels in the texture.</param>
/// <param name="bindFlags">The <see cref="Direct3D11.BindFlags"/> for binding to pipeline stages.</param>
/// <param name="usage">Value that identifies how the texture is to be read from and written to.</param>
/// <param name="cpuAccessFlags">The <see cref="CpuAccessFlags"/> to specify the types of CPU access allowed.</param>
/// <param name="miscFlags">The <see cref="ResourceMiscFlags"/> that identify other, less common resource options. </param>
public Texture3DDescription(
Format format,
uint width,
uint height,
uint depth,
uint mipLevels = 0,
BindFlags bindFlags = BindFlags.ShaderResource,
Usage usage = Usage.Default,
CpuAccessFlags cpuAccessFlags = CpuAccessFlags.None,
ResourceMiscFlags miscFlags = ResourceMiscFlags.None)
{
if (format == Format.Unknown)
throw new ArgumentException($"format need to be valid", nameof(format));
if (width < 1 || width > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION)
throw new ArgumentException($"Width need to be in range 1-{D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION}", nameof(width));
if (height < 1 || height > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION)
throw new ArgumentException($"Height need to be in range 1-{D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION}", nameof(height));
if (depth < 1 || depth > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION)
throw new ArgumentException($"Depth need to be in range 1-{D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION}", nameof(depth));
Width = width;
Height = height;
Depth = depth;
MipLevels = mipLevels;
Format = format;
Usage = usage;
BindFlags = bindFlags;
CPUAccessFlags = cpuAccessFlags;
MiscFlags = miscFlags;
}
}

View File

@@ -51,7 +51,7 @@ public partial struct SampleDescription
public static readonly SampleDescription Default = new(1, 0); public static readonly SampleDescription Default = new(1, 0);
/// <summary> /// <summary>
/// Create new instance of <see cref="SampleDescription"/> struct. /// Initializes a new instance of the <see cref="SampleDescription"/> struct.
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
/// <param name="quality"></param> /// <param name="quality"></param>

View File

@@ -10,6 +10,8 @@ using static Win32.Graphics.Direct3D11.Apis;
using static Win32.Graphics.Dxgi.Apis; using static Win32.Graphics.Dxgi.Apis;
using MessageId = Win32.Graphics.Direct3D11.MessageId; using MessageId = Win32.Graphics.Direct3D11.MessageId;
using InfoQueueFilter = Win32.Graphics.Direct3D11.InfoQueueFilter; using InfoQueueFilter = Win32.Graphics.Direct3D11.InfoQueueFilter;
using Win32.Graphics.Dxgi.Common;
using System.Numerics;
namespace ClearScreen; namespace ClearScreen;
@@ -39,6 +41,7 @@ public static unsafe class Program
{ {
using ComPtr<IDXGIFactory2> factory = default; using ComPtr<IDXGIFactory2> factory = default;
uint factoryFlags = 0; uint factoryFlags = 0;
#if DEBUG #if DEBUG
{ {
using ComPtr<IDXGIInfoQueue> dxgiInfoQueue = default; using ComPtr<IDXGIInfoQueue> dxgiInfoQueue = default;
@@ -119,7 +122,7 @@ public static unsafe class Program
using ComPtr<ID3D11Device> tempDevice = default; using ComPtr<ID3D11Device> tempDevice = default;
FeatureLevel featureLevel; FeatureLevel featureLevel;
using ComPtr<ID3D11DeviceContext> immediateContext = default; using ComPtr<ID3D11DeviceContext> tempImmediateContext = default;
D3D11CreateDevice( D3D11CreateDevice(
(IDXGIAdapter*)adapter.Get(), (IDXGIAdapter*)adapter.Get(),
@@ -128,7 +131,7 @@ public static unsafe class Program
featureLevels, featureLevels,
tempDevice.GetAddressOf(), tempDevice.GetAddressOf(),
&featureLevel, &featureLevel,
immediateContext.GetAddressOf()).ThrowIfFailed(); tempImmediateContext.GetAddressOf()).ThrowIfFailed();
#if DEBUG #if DEBUG
using ComPtr<ID3D11Debug> d3dDebug = default; using ComPtr<ID3D11Debug> d3dDebug = default;
@@ -152,5 +155,29 @@ public static unsafe class Program
} }
} }
#endif #endif
using ComPtr<ID3D11Device1> d3dDevice = default;
using ComPtr<ID3D11DeviceContext1> immediateContext = default;
tempDevice.CopyTo(&d3dDevice).ThrowIfFailed();
tempImmediateContext.CopyTo(&immediateContext).ThrowIfFailed();
ReadOnlySpan<VertexPositionColor> triangleVertices = stackalloc VertexPositionColor[]
{
new VertexPositionColor(new Vector3(0f, 0.5f, 0.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f)),
new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f)),
new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f))
};
using ComPtr<ID3D11Buffer> vertexBuffer = ((ID3D11Device*)d3dDevice.Get())->CreateBuffer(triangleVertices, BindFlags.VertexBuffer);
using ComPtr<ID3D11Texture2D> depthStencilTexture = default;
using ComPtr<ID3D11DepthStencilView> depthStencilTextureView = default;
Texture2DDescription texture2DDesc = new(Format.D32Float, 256, 256, 1, 1, BindFlags.DepthStencil);
tempDevice.Get()->CreateTexture2D(&texture2DDesc, null, depthStencilTexture.GetAddressOf()).ThrowIfFailed();
depthStencilTexture.Get()->GetDesc(&texture2DDesc);
tempDevice.Get()->CreateDepthStencilView((ID3D11Resource*)depthStencilTexture.Get(), null, depthStencilTextureView.GetAddressOf()).ThrowIfFailed();
} }
} }

View File

@@ -0,0 +1,31 @@
// Copyright © Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
using Win32;
using Win32.Graphics.Direct3D;
using Win32.Graphics.Direct3D11;
using Win32.Graphics.Dxgi;
using static Win32.Apis;
using static Win32.Graphics.Direct3D11.Apis;
using static Win32.Graphics.Dxgi.Apis;
using MessageId = Win32.Graphics.Direct3D11.MessageId;
using InfoQueueFilter = Win32.Graphics.Direct3D11.InfoQueueFilter;
using Win32.Graphics.Dxgi.Common;
using System.Drawing;
using System.Numerics;
namespace ClearScreen;
public readonly struct VertexPositionColor
{
public static readonly unsafe int SizeInBytes = sizeof(VertexPositionColor);
public VertexPositionColor(in Vector3 position, in Vector4 color)
{
Position = position;
Color = color;
}
public readonly Vector3 Position;
public readonly Vector4 Color;
}