Skip to content

Commit

Permalink
Samples: Lot of new D3D12 samples landed, WIP Texture Cube example
Browse files Browse the repository at this point in the history
  • Loading branch information
amerkoleci committed Jun 25, 2024
1 parent 3365228 commit 8e0bcd9
Show file tree
Hide file tree
Showing 29 changed files with 1,462 additions and 601 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- https://learn.microsoft.com/nuget/consume-packages/central-package-management -->
<!-- Package versions for package references across all projects -->
<PropertyGroup>
<VorticeWindowsVersion>3.5.4-beta</VorticeWindowsVersion>
<VorticeWindowsVersion>3.5.7-beta</VorticeWindowsVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
361 changes: 209 additions & 152 deletions Vortice.Windows.Samples.sln

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/Direct3D11/06_CubeAlphaBlend/CubeAlphaBlendApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,25 @@ protected unsafe override void OnRender()
List<VertexPositionColor> vertices = new();
Span<ushort> indices = stackalloc ushort[36];

Vector3[] faceNormals = new Vector3[CubeFaceCount]
{
Vector3[] faceNormals =
[
Vector3.UnitZ,
new Vector3(0.0f, 0.0f, -1.0f),
Vector3.UnitX,
new Vector3(-1.0f, 0.0f, 0.0f),
Vector3.UnitY,
new Vector3(0.0f, -1.0f, 0.0f),
};
];

Color4[] faceColors = new Color4[CubeFaceCount]
{
Color4[] faceColors =
[
new(1.0f, 0.0f, 0.0f, 0.4f),
new(0.0f, 1.0f, 0.0f, 0.4f),
new(0.0f, 0.0f, 1.0f, 0.4f),
new(1.0f, 1.0f, 0.0f, 0.4f),
new(1.0f, 0.0f, 1.0f, 0.4f),
new(0.0f, 1.0f, 1.0f, 0.4f),
};
];

Vector3 tsize = size / 2.0f;

Expand Down
File renamed without changes.
100 changes: 100 additions & 0 deletions src/Direct3D12/02_DrawTriangle/DrawTriangleApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) Amer Koleci and contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.

using System.Numerics;
using Vortice.Direct3D;
using Vortice.Direct3D12;
using Vortice.Dxc;
using Vortice.DXGI;
using Vortice.Framework;
using Vortice.Mathematics;

#nullable disable

class DrawTriangleApp : D3D12Application
{
private ID3D12Resource _vertexBuffer;
private ID3D12RootSignature _rootSignature;
private ID3D12PipelineState _pipelineState;

protected override void Initialize()
{
Span<VertexPositionColor> triangleVertices =
[
new VertexPositionColor(new Vector3(0f, 0.5f, 0.0f), new Color4(1.0f, 0.0f, 0.0f, 1.0f)),
new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.0f), new Color4(0.0f, 1.0f, 0.0f, 1.0f)),
new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.0f), new Color4(0.0f, 0.0f, 1.0f, 1.0f))
];

UploadBatch.Begin(CommandListType.Direct);
_vertexBuffer = CreateStaticBuffer(triangleVertices, ResourceStates.VertexAndConstantBuffer);
UploadBatch.End(DirectQueue);

// Create empty root signature first
RootSignatureFlags rootSignatureFlags =
RootSignatureFlags.AllowInputAssemblerInputLayout |
RootSignatureFlags.DenyHullShaderRootAccess |
RootSignatureFlags.DenyDomainShaderRootAccess |
RootSignatureFlags.DenyGeometryShaderRootAccess |
RootSignatureFlags.DenyPixelShaderRootAccess;

_rootSignature = Device.CreateRootSignature(new RootSignatureDescription1(rootSignatureFlags));

// Create pipeline
ReadOnlyMemory<byte> vertexShaderByteCode = CompileBytecode(DxcShaderStage.Vertex, "HelloTriangle.hlsl", "VSMain");
ReadOnlyMemory<byte> pixelShaderByteCode = CompileBytecode(DxcShaderStage.Pixel, "HelloTriangle.hlsl", "PSMain");

GraphicsPipelineStateDescription psoDesc = new()
{
RootSignature = _rootSignature,
VertexShader = vertexShaderByteCode,
PixelShader = pixelShaderByteCode,
InputLayout = new InputLayoutDescription(VertexPositionColor.InputElementsD3D12),
PrimitiveTopologyType = PrimitiveTopologyType.Triangle,
RasterizerState = RasterizerDescription.CullCounterClockwise,
BlendState = BlendDescription.Opaque,
DepthStencilState = DepthStencilDescription.Default,
RenderTargetFormats = new[] { ColorFormat },
DepthStencilFormat = DepthStencilFormat,
SampleDescription = SampleDescription.Default
};
_pipelineState = Device.CreateGraphicsPipelineState<ID3D12PipelineState>(psoDesc);
}

protected override void OnDestroy()
{
_vertexBuffer.Dispose();
_rootSignature.Dispose();
_pipelineState.Dispose();
}

protected override void OnRender()
{
Color4 clearColor = new(0.0f, 0.2f, 0.4f, 1.0f);
CommandList.ClearRenderTargetView(ColorTextureView, clearColor);

if (DepthStencilView.HasValue)
{
CommandList.ClearDepthStencilView(DepthStencilView.Value, ClearFlags.Depth, 1.0f, 0);
}

// Set necessary state.
CommandList.SetGraphicsRootSignature(_rootSignature);
CommandList.SetPipelineState(_pipelineState);

int stride = VertexPositionColor.SizeInBytes;
int vertexBufferSize = 3 * stride;

CommandList.IASetPrimitiveTopology(PrimitiveTopology.TriangleList);


CommandList.IASetVertexBuffers(0, new VertexBufferView(_vertexBuffer.GPUVirtualAddress, vertexBufferSize, stride));
CommandList.DrawInstanced(3, 1, 0, 0);
}

static void Main()
{
DrawTriangleApp app = new();
app.Run();
}
}
107 changes: 107 additions & 0 deletions src/Direct3D12/03_DrawQuad/DrawQuadApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) Amer Koleci and contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.

using System.Numerics;
using Vortice.Direct3D;
using Vortice.Direct3D12;
using Vortice.Dxc;
using Vortice.DXGI;
using Vortice.Framework;
using Vortice.Mathematics;

#nullable disable

internal class DrawQuadApp : D3D12Application
{
private ID3D12Resource _vertexBuffer;
private ID3D12Resource _indexBuffer;
private ID3D12RootSignature _rootSignature;
private ID3D12PipelineState _pipelineState;

protected override void Initialize()
{
Span<VertexPositionColor> quadVertices =
[
new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.0f), new Color4(1.0f, 0.0f, 0.0f, 1.0f)),
new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f), new Color4(0.0f, 1.0f, 0.0f, 1.0f)),
new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.0f), new Color4(0.0f, 0.0f, 1.0f, 1.0f)),
new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.0f), new Color4(0.0f, 0.0f, 1.0f, 1.0f))
];

UploadBatch.Begin(CommandListType.Direct);
_vertexBuffer = CreateStaticBuffer(quadVertices, ResourceStates.VertexAndConstantBuffer);

Span<ushort> quadIndices = [0, 1, 2, 0, 2, 3];
_indexBuffer = CreateStaticBuffer(quadIndices, ResourceStates.IndexBuffer);
UploadBatch.End(DirectQueue);

// Create empty root signature first
RootSignatureFlags rootSignatureFlags =
RootSignatureFlags.AllowInputAssemblerInputLayout |
RootSignatureFlags.DenyHullShaderRootAccess |
RootSignatureFlags.DenyDomainShaderRootAccess |
RootSignatureFlags.DenyGeometryShaderRootAccess |
RootSignatureFlags.DenyPixelShaderRootAccess;

_rootSignature = Device.CreateRootSignature(new RootSignatureDescription1(rootSignatureFlags));

// Create pipeline
ReadOnlyMemory<byte> vertexShaderByteCode = CompileBytecode(DxcShaderStage.Vertex, "HelloTriangle.hlsl", "VSMain");
ReadOnlyMemory<byte> pixelShaderByteCode = CompileBytecode(DxcShaderStage.Pixel, "HelloTriangle.hlsl", "PSMain");

GraphicsPipelineStateDescription psoDesc = new()
{
RootSignature = _rootSignature,
VertexShader = vertexShaderByteCode,
PixelShader = pixelShaderByteCode,
InputLayout = new InputLayoutDescription(VertexPositionColor.InputElementsD3D12),
PrimitiveTopologyType = PrimitiveTopologyType.Triangle,
RasterizerState = RasterizerDescription.CullCounterClockwise,
BlendState = BlendDescription.Opaque,
DepthStencilState = DepthStencilDescription.Default,
RenderTargetFormats = [ColorFormat],
DepthStencilFormat = DepthStencilFormat,
SampleDescription = SampleDescription.Default,
};
_pipelineState = Device.CreateGraphicsPipelineState<ID3D12PipelineState>(psoDesc);
}

protected override void OnDestroy()
{
_vertexBuffer.Dispose();
_indexBuffer.Dispose();
_rootSignature.Dispose();
_pipelineState.Dispose();
}

protected override void OnRender()
{
Color4 clearColor = new(0.0f, 0.2f, 0.4f, 1.0f);
CommandList.ClearRenderTargetView(ColorTextureView, clearColor);

if (DepthStencilView.HasValue)
{
CommandList.ClearDepthStencilView(DepthStencilView.Value, ClearFlags.Depth, 1.0f, 0);
}

// Set necessary state.
CommandList.SetGraphicsRootSignature(_rootSignature);
CommandList.SetPipelineState(_pipelineState);
CommandList.IASetPrimitiveTopology(PrimitiveTopology.TriangleList);

// Vertex Buffer
int stride = VertexPositionColor.SizeInBytes;
int vertexBufferSize = 3 * stride;
CommandList.IASetVertexBuffers(0, new VertexBufferView(_vertexBuffer.GPUVirtualAddress, vertexBufferSize, stride));

// Index Buffer
int indexBufferSize = 6 * sizeof(ushort);
CommandList.IASetIndexBuffer(new IndexBufferView(_indexBuffer.GPUVirtualAddress, indexBufferSize, false));
CommandList.DrawIndexedInstanced(6, 1, 0, 0, 0);
}
static void Main()
{
DrawQuadApp app = new();
app.Run();
}
}
20 changes: 20 additions & 0 deletions src/Direct3D12/03_DrawQuad/Shaders/HelloTriangle.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
struct VSInput {
float3 Position : POSITION;
float4 Color : COLOR;
};

struct PSInput {
float4 Position : SV_POSITION;
float4 Color : COLOR;
};

PSInput VSMain(in VSInput input) {
PSInput result;
result.Position = float4(input.Position, 1.0f);
result.Color = input.Color;
return result;
}

float4 PSMain(in PSInput input) : SV_TARGET{
return input.Color;
}
Loading

0 comments on commit 8e0bcd9

Please sign in to comment.