Skip to content

Commit

Permalink
Switch to root signature version 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Aminator committed Aug 18, 2019
1 parent 76d6ab7 commit 87d879f
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 114 deletions.
23 changes: 14 additions & 9 deletions DirectX12ComputeShaderSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using DirectX12GameEngine.Shaders;
using DirectX12GameEngine.Shaders.Numerics;
using Vortice.DirectX.Direct3D12;

using Buffer = DirectX12GameEngine.Graphics.Buffer;
using CommandList = DirectX12GameEngine.Graphics.CommandList;
using CommandListType = DirectX12GameEngine.Graphics.CommandListType;
using PipelineState = DirectX12GameEngine.Graphics.PipelineState;
using DescriptorHeapType = DirectX12GameEngine.Graphics.DescriptorHeapType;
using ShaderModel = DirectX12GameEngine.Shaders.ShaderModel;

namespace DirectX12ComputeShaderSample
Expand All @@ -34,7 +34,7 @@ private static async Task Main()
{
// Create graphics device

using GraphicsDevice device = new GraphicsDevice(FeatureLevel.Level_12_1);
using GraphicsDevice device = new GraphicsDevice(FeatureLevel.Level12_1);

// Create graphics buffer

Expand All @@ -53,6 +53,10 @@ private static async Task Main()
using Buffer<float> sourceBuffer = Buffer.ShaderResource.New(device, array.AsSpan());
using Buffer<float> destinationBuffer = Buffer.UnorderedAccess.New<float>(device, array.Length);

DescriptorSet descriptorSet = new DescriptorSet(device, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, 2);
descriptorSet.AddDescriptor(sourceBuffer);
descriptorSet.AddDescriptor(destinationBuffer);

// Generate computer shader

//Action<UInt3> action = id =>
Expand All @@ -69,13 +73,15 @@ private static async Task Main()

byte[] shaderBytecode = ShaderCompiler.CompileShader(result.ShaderSource, ShaderProfile.ComputeShader, ShaderModel.Model6_1, result.ComputeShader);

RootParameter[] rootParameters = new RootParameter[]
DescriptorRange1[] descriptorRanges = new DescriptorRange1[]
{
new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, 0)) },
new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, 0)) }
new DescriptorRange1(DescriptorRangeType.ShaderResourceView, 1, 0),
new DescriptorRange1(DescriptorRangeType.UnorderedAccessView, 1, 0)
};

var rootSignatureDescription = new VersionedRootSignatureDescription(new RootSignatureDescription(RootSignatureFlags.None, rootParameters));
RootParameter1 rootParameter = new RootParameter1 { DescriptorTable = new RootDescriptorTable1(descriptorRanges) };

var rootSignatureDescription = new VersionedRootSignatureDescription(new RootSignatureDescription1(RootSignatureFlags.None, new[] { rootParameter }));
var rootSignature = device.CreateRootSignature(rootSignatureDescription);

PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode);
Expand All @@ -86,8 +92,7 @@ private static async Task Main()
{
commandList.SetPipelineState(pipelineState);

commandList.SetComputeRootDescriptorTable(0, sourceBuffer);
commandList.SetComputeRootDescriptorTable(1, destinationBuffer);
commandList.SetComputeRootDescriptorTable(0, descriptorSet);

commandList.Dispatch(1, 1, 1);
await commandList.FlushAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0-preview7.19362.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0-preview8.19405.4" />
<PackageReference Include="Nito.AsyncEx" Version="5.0.0" />
</ItemGroup>

Expand Down
9 changes: 2 additions & 7 deletions DirectX12GameEngine.Engine/EntitySystems/RenderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,16 @@ private void RecordCommandList(Model model, CommandList commandList, Buffer[] wo

commandList.SetGraphicsRootDescriptorTable(rootParameterIndex++, DirectionalLightGroupBuffer);

if (materialPass.ConstantBufferDescriptorSet != null)
if (materialPass.ShaderResourceDescriptorSet != null)
{
commandList.SetGraphicsRootDescriptorTable(rootParameterIndex++, materialPass.ConstantBufferDescriptorSet);
commandList.SetGraphicsRootDescriptorTable(rootParameterIndex++, materialPass.ShaderResourceDescriptorSet);
}

if (materialPass.SamplerDescriptorSet != null)
{
commandList.SetGraphicsRootDescriptorTable(rootParameterIndex++, materialPass.SamplerDescriptorSet);
}

if (materialPass.TextureDescriptorSet != null)
{
commandList.SetGraphicsRootDescriptorTable(rootParameterIndex++, materialPass.TextureDescriptorSet);
}

if (mesh.MeshDraw.IndexBufferView != null)
{
commandList.DrawIndexedInstanced(mesh.MeshDraw.IndexBufferView.SizeInBytes / mesh.MeshDraw.IndexBufferView.StructuredByteStride, instanceCount);
Expand Down
14 changes: 4 additions & 10 deletions DirectX12GameEngine.Graphics/FeatureLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
{
public enum FeatureLevel
{
Level_1_0_Core = 4096,
Level_9_1 = 37120,
Level_9_2 = 37376,
Level_9_3 = 37632,
Level_10_0 = 40960,
Level_10_1 = 41216,
Level_11_0 = 45056,
Level_11_1 = 45312,
Level_12_0 = 49152,
Level_12_1 = 49408
Level11_0 = 45056,
Level11_1 = 45312,
Level12_0 = 49152,
Level12_1 = 49408
}
}
6 changes: 2 additions & 4 deletions DirectX12GameEngine.Graphics/GraphicsDevice.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DirectX12GameEngine.Core;
using Nito.AsyncEx.Interop;
using SharpGen.Runtime;
using Vortice.DirectX.Direct3D;
using Vortice.DirectX.Direct3D12;

namespace DirectX12GameEngine.Graphics
Expand All @@ -16,14 +14,14 @@ public sealed class GraphicsDevice : IDisposable, ICollector
private readonly AutoResetEvent fenceEvent = new AutoResetEvent(false);
private Vortice.DirectX.Direct3D11.ID3D11Device? nativeDirect3D11Device;

public GraphicsDevice(FeatureLevel minFeatureLevel = FeatureLevel.Level_11_0, bool enableDebugLayer = false)
public GraphicsDevice(FeatureLevel minFeatureLevel = FeatureLevel.Level11_0, bool enableDebugLayer = false)
{
#if DEBUG
if (enableDebugLayer)
{
}
#endif
FeatureLevel = minFeatureLevel < FeatureLevel.Level_11_0 ? FeatureLevel.Level_11_0 : minFeatureLevel;
FeatureLevel = minFeatureLevel < FeatureLevel.Level11_0 ? FeatureLevel.Level11_0 : minFeatureLevel;

Result result = D3D12.D3D12CreateDevice(null, (Vortice.DirectX.Direct3D.FeatureLevel)FeatureLevel, out ID3D12Device device);
NativeDevice = device;
Expand Down
2 changes: 1 addition & 1 deletion DirectX12GameEngine.Rendering/Lights/DirectLightGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace DirectX12GameEngine.Rendering.Lights
{
[ShaderContract]
[ConstantBuffer]
[ConstantBufferView]
public abstract class DirectLightGroup
{
[ShaderMember] public int LightCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DirectX12GameEngine.Rendering.Lights
{
[ShaderContract]
[ConstantBuffer]
[ConstantBufferView]
public class DirectionalLightGroup : DirectLightGroup
{
#nullable disable
Expand Down
5 changes: 1 addition & 4 deletions DirectX12GameEngine.Rendering/MaterialPass.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DirectX12GameEngine.Graphics;
using PipelineState = DirectX12GameEngine.Graphics.PipelineState;

namespace DirectX12GameEngine.Rendering
{
Expand All @@ -9,10 +8,8 @@ public class MaterialPass

public PipelineState? PipelineState { get; set; }

public DescriptorSet? ConstantBufferDescriptorSet { get; set; }
public DescriptorSet? ShaderResourceDescriptorSet { get; set; }

public DescriptorSet? SamplerDescriptorSet { get; set; }

public DescriptorSet? TextureDescriptorSet { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class MaterialCelShadingLightDefault : IMaterialCelShadingLightFunction
public void Visit(MaterialGeneratorContext context)
{
isBlackAndWhiteBuffer ??= Buffer.Constant.New(context.GraphicsDevice, IsBlackAndWhite).DisposeBy(context.GraphicsDevice);
context.ConstantBuffers.Add(isBlackAndWhiteBuffer);
context.ConstantBufferViews.Add(isBlackAndWhiteBuffer);
}

[ConstantBuffer] public bool IsBlackAndWhite { get; set; }
[ConstantBufferView] public bool IsBlackAndWhite { get; set; }

[ShaderMember]
public Vector3 Compute(float LightIn)
Expand Down
4 changes: 2 additions & 2 deletions DirectX12GameEngine.Rendering/Materials/ComputeColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace DirectX12GameEngine.Rendering.Materials
{
[ShaderContract]
[ConstantBuffer]
[ConstantBufferView]
public class ComputeColor : IComputeColor
{
private Vector4 color;
Expand All @@ -24,7 +24,7 @@ public ComputeColor(in Vector4 color)
public void Visit(MaterialGeneratorContext context)
{
colorBuffer ??= Buffer.Constant.New(context.GraphicsDevice, Color).DisposeBy(context.GraphicsDevice);
context.ConstantBuffers.Add(colorBuffer);
context.ConstantBufferViews.Add(colorBuffer);
}

#region Shader
Expand Down
4 changes: 2 additions & 2 deletions DirectX12GameEngine.Rendering/Materials/ComputeScalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace DirectX12GameEngine.Rendering.Materials
{
[ShaderContract]
[ConstantBuffer]
[ConstantBufferView]
public class ComputeScalar : IComputeScalar
{
private float scalarValue;
Expand All @@ -23,7 +23,7 @@ public ComputeScalar(float value)
public void Visit(MaterialGeneratorContext context)
{
valueBuffer ??= Buffer.Constant.New(context.GraphicsDevice, Value).DisposeBy(context.GraphicsDevice);
context.ConstantBuffers.Add(valueBuffer);
context.ConstantBufferViews.Add(valueBuffer);
}

#region Shader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Visit(MaterialGeneratorContext context)
{
if (Texture != null)
{
context.Textures.Add(Texture);
context.ShaderResourceViews.Add(Texture);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public void Visit(MaterialGeneratorContext context)
{
if (Texture != null)
{
context.Textures.Add(Texture);
context.ShaderResourceViews.Add(Texture);
}

colorChannelBuffer ??= Buffer.Constant.New(context.GraphicsDevice, Channel).DisposeBy(context.GraphicsDevice);
context.ConstantBuffers.Add(colorChannelBuffer);
context.ConstantBufferViews.Add(colorChannelBuffer);
}

#region Shader
Expand All @@ -46,7 +46,7 @@ public void Visit(MaterialGeneratorContext context)
[ShaderMember] public Texture2DResource ScalarTexture;
#nullable enable

[ConstantBuffer] public ColorChannel Channel
[ConstantBufferView] public ColorChannel Channel
{
get => channel;
set
Expand Down
12 changes: 5 additions & 7 deletions DirectX12GameEngine.Rendering/Materials/MaterialGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using DirectX12GameEngine.Graphics;
using Nito.AsyncEx;
Expand All @@ -24,21 +25,18 @@ public static async Task<Material> GenerateAsync(MaterialDescriptor descriptor,

materialPass.PipelineState = await context.CreateGraphicsPipelineStateAsync();

if (context.ConstantBuffers.Count > 0)
var shaderResources = context.ConstantBufferViews.Concat(context.ShaderResourceViews).Concat(context.UnorderedAccessViews);

if (shaderResources.Count() > 0)
{
materialPass.ConstantBufferDescriptorSet = new DescriptorSet(context.GraphicsDevice, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, context.ConstantBuffers);
materialPass.SamplerDescriptorSet = new DescriptorSet(context.GraphicsDevice, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, shaderResources);
}

if (context.Samplers.Count > 0)
{
materialPass.SamplerDescriptorSet = new DescriptorSet(context.GraphicsDevice, DescriptorHeapType.Sampler, context.Samplers);
}

if (context.Textures.Count > 0)
{
materialPass.TextureDescriptorSet = new DescriptorSet(context.GraphicsDevice, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, context.Textures);
}

context.PopPass();
}

Expand Down
52 changes: 35 additions & 17 deletions DirectX12GameEngine.Rendering/Materials/MaterialGeneratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public MaterialGeneratorContext(GraphicsDevice device, Material material, Shader

public ShaderContentManager Content { get; }

public IList<Graphics.Buffer> ConstantBuffers { get; } = new List<Graphics.Buffer>();
public IList<GraphicsResource> ConstantBufferViews { get; } = new List<GraphicsResource>();

public IList<GraphicsResource> Samplers { get; } = new List<GraphicsResource>();
public IList<GraphicsResource> ShaderResourceViews { get; } = new List<GraphicsResource>();

public IList<GraphicsResource> UnorderedAccessViews { get; } = new List<GraphicsResource>();

public IList<Texture> Textures { get; } = new List<Texture>();
public IList<GraphicsResource> Samplers { get; } = new List<GraphicsResource>();

public IMaterialDescriptor? MaterialDescriptor => materialDescriptorStack.Count > 0 ? materialDescriptorStack.Peek() : null;

Expand Down Expand Up @@ -67,7 +69,9 @@ public MaterialPass PushPass()
MaterialPass? materialPass = MaterialPass;
MaterialPass = null;

Textures.Clear();
ConstantBufferViews.Clear();
ShaderResourceViews.Clear();
UnorderedAccessViews.Clear();

return materialPass;
}
Expand Down Expand Up @@ -131,36 +135,50 @@ public async Task<PipelineState> CreateGraphicsPipelineStateAsync()

public ID3D12RootSignature CreateRootSignature()
{
List<RootParameter> rootParameters = new List<RootParameter>
int cbvShaderRegister = 0;

List<RootParameter1> rootParameters = new List<RootParameter1>
{
new RootParameter { ParameterType = RootParameterType.Constant32Bits, Constants = new RootConstants(0, 0, 1) },
new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, 1)) },
new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, 2)) },
new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, 3)) },
new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, 4)) }
new RootParameter1 { ParameterType = RootParameterType.Constant32Bits, Constants = new RootConstants(cbvShaderRegister++, 0, 1) },
new RootParameter1 { DescriptorTable = new RootDescriptorTable1(new DescriptorRange1(DescriptorRangeType.ConstantBufferView, 1, cbvShaderRegister++)) },
new RootParameter1 { DescriptorTable = new RootDescriptorTable1(new DescriptorRange1(DescriptorRangeType.ConstantBufferView, 1, cbvShaderRegister++)) },
new RootParameter1 { DescriptorTable = new RootDescriptorTable1(new DescriptorRange1(DescriptorRangeType.ConstantBufferView, 1, cbvShaderRegister++)) },
new RootParameter1 { DescriptorTable = new RootDescriptorTable1(new DescriptorRange1(DescriptorRangeType.ConstantBufferView, 1, cbvShaderRegister++)) }
};

if (ConstantBuffers.Count > 0)
List<DescriptorRange1> shaderResourceRootDescriptorRanges = new List<DescriptorRange1>();

if (ConstantBufferViews.Count > 0)
{
rootParameters.Add(new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.ConstantBufferView, ConstantBuffers.Count, 5)) });
shaderResourceRootDescriptorRanges.Add(new DescriptorRange1(DescriptorRangeType.ConstantBufferView, ConstantBufferViews.Count, cbvShaderRegister));
}

if (Samplers.Count > 0)
if (ShaderResourceViews.Count > 0)
{
rootParameters.Add(new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.Sampler, Samplers.Count, 1)) });
shaderResourceRootDescriptorRanges.Add(new DescriptorRange1(DescriptorRangeType.ShaderResourceView, ShaderResourceViews.Count, 0));
}

if (Textures.Count > 0)
if (UnorderedAccessViews.Count > 0)
{
shaderResourceRootDescriptorRanges.Add(new DescriptorRange1(DescriptorRangeType.UnorderedAccessView, UnorderedAccessViews.Count, 1));
}

if (shaderResourceRootDescriptorRanges.Count > 0)
{
rootParameters.Add(new RootParameter1 { DescriptorTable = new RootDescriptorTable1(shaderResourceRootDescriptorRanges.ToArray()) });
}

if (Samplers.Count > 0)
{
rootParameters.Add(new RootParameter { DescriptorTable = new RootDescriptorTable(new DescriptorRange(DescriptorRangeType.ShaderResourceView, Textures.Count, 0)) });
rootParameters.Add(new RootParameter1 { DescriptorTable = new RootDescriptorTable1(new DescriptorRange1(DescriptorRangeType.Sampler, Samplers.Count, 0)) });
}

StaticSamplerDescription[] staticSamplers = new StaticSamplerDescription[]
{
new StaticSamplerDescription(ShaderVisibility.All, 0, 0)
};

RootSignatureDescription rootSignatureDescription = new RootSignatureDescription(
RootSignatureDescription1 rootSignatureDescription = new RootSignatureDescription1(
RootSignatureFlags.AllowInputAssemblerInputLayout, rootParameters.ToArray(), staticSamplers);

return GraphicsDevice.CreateRootSignature(new VersionedRootSignatureDescription(rootSignatureDescription));
Expand Down
Loading

0 comments on commit 87d879f

Please sign in to comment.