diff --git a/DirectX12ComputeShaderSample/Program.cs b/DirectX12ComputeShaderSample/Program.cs index 71aee33..50d64e9 100644 --- a/DirectX12ComputeShaderSample/Program.cs +++ b/DirectX12ComputeShaderSample/Program.cs @@ -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 @@ -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 @@ -53,6 +53,10 @@ private static async Task Main() using Buffer sourceBuffer = Buffer.ShaderResource.New(device, array.AsSpan()); using Buffer destinationBuffer = Buffer.UnorderedAccess.New(device, array.Length); + DescriptorSet descriptorSet = new DescriptorSet(device, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, 2); + descriptorSet.AddDescriptor(sourceBuffer); + descriptorSet.AddDescriptor(destinationBuffer); + // Generate computer shader //Action action = id => @@ -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); @@ -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(); diff --git a/DirectX12GameEngine.Core.Assets/DirectX12GameEngine.Core.Assets.csproj b/DirectX12GameEngine.Core.Assets/DirectX12GameEngine.Core.Assets.csproj index b59d5b1..07ca8ea 100644 --- a/DirectX12GameEngine.Core.Assets/DirectX12GameEngine.Core.Assets.csproj +++ b/DirectX12GameEngine.Core.Assets/DirectX12GameEngine.Core.Assets.csproj @@ -11,7 +11,7 @@ - + diff --git a/DirectX12GameEngine.Engine/EntitySystems/RenderSystem.cs b/DirectX12GameEngine.Engine/EntitySystems/RenderSystem.cs index e4fd866..9ccdd3a 100644 --- a/DirectX12GameEngine.Engine/EntitySystems/RenderSystem.cs +++ b/DirectX12GameEngine.Engine/EntitySystems/RenderSystem.cs @@ -260,9 +260,9 @@ 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) @@ -270,11 +270,6 @@ private void RecordCommandList(Model model, CommandList commandList, Buffer[] wo 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); diff --git a/DirectX12GameEngine.Graphics/FeatureLevel.cs b/DirectX12GameEngine.Graphics/FeatureLevel.cs index 75c9af6..3f19d22 100644 --- a/DirectX12GameEngine.Graphics/FeatureLevel.cs +++ b/DirectX12GameEngine.Graphics/FeatureLevel.cs @@ -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 } } diff --git a/DirectX12GameEngine.Graphics/GraphicsDevice.cs b/DirectX12GameEngine.Graphics/GraphicsDevice.cs index 69d7ac4..dd513bb 100644 --- a/DirectX12GameEngine.Graphics/GraphicsDevice.cs +++ b/DirectX12GameEngine.Graphics/GraphicsDevice.cs @@ -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 @@ -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; diff --git a/DirectX12GameEngine.Rendering/Lights/DirectLightGroup.cs b/DirectX12GameEngine.Rendering/Lights/DirectLightGroup.cs index 9b81c30..62f6256 100644 --- a/DirectX12GameEngine.Rendering/Lights/DirectLightGroup.cs +++ b/DirectX12GameEngine.Rendering/Lights/DirectLightGroup.cs @@ -6,7 +6,7 @@ namespace DirectX12GameEngine.Rendering.Lights { [ShaderContract] - [ConstantBuffer] + [ConstantBufferView] public abstract class DirectLightGroup { [ShaderMember] public int LightCount; diff --git a/DirectX12GameEngine.Rendering/Lights/DirectionalLightGroup.cs b/DirectX12GameEngine.Rendering/Lights/DirectionalLightGroup.cs index eb09e67..e8f5417 100644 --- a/DirectX12GameEngine.Rendering/Lights/DirectionalLightGroup.cs +++ b/DirectX12GameEngine.Rendering/Lights/DirectionalLightGroup.cs @@ -3,7 +3,7 @@ namespace DirectX12GameEngine.Rendering.Lights { [ShaderContract] - [ConstantBuffer] + [ConstantBufferView] public class DirectionalLightGroup : DirectLightGroup { #nullable disable diff --git a/DirectX12GameEngine.Rendering/MaterialPass.cs b/DirectX12GameEngine.Rendering/MaterialPass.cs index d928b6b..471c847 100644 --- a/DirectX12GameEngine.Rendering/MaterialPass.cs +++ b/DirectX12GameEngine.Rendering/MaterialPass.cs @@ -1,5 +1,4 @@ using DirectX12GameEngine.Graphics; -using PipelineState = DirectX12GameEngine.Graphics.PipelineState; namespace DirectX12GameEngine.Rendering { @@ -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; } } } diff --git a/DirectX12GameEngine.Rendering/Materials/CelShading/MaterialCelShadingLightDefault.cs b/DirectX12GameEngine.Rendering/Materials/CelShading/MaterialCelShadingLightDefault.cs index fa8cc04..02dce62 100644 --- a/DirectX12GameEngine.Rendering/Materials/CelShading/MaterialCelShadingLightDefault.cs +++ b/DirectX12GameEngine.Rendering/Materials/CelShading/MaterialCelShadingLightDefault.cs @@ -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) diff --git a/DirectX12GameEngine.Rendering/Materials/ComputeColor.cs b/DirectX12GameEngine.Rendering/Materials/ComputeColor.cs index 6daa1e9..5c64065 100644 --- a/DirectX12GameEngine.Rendering/Materials/ComputeColor.cs +++ b/DirectX12GameEngine.Rendering/Materials/ComputeColor.cs @@ -6,7 +6,7 @@ namespace DirectX12GameEngine.Rendering.Materials { [ShaderContract] - [ConstantBuffer] + [ConstantBufferView] public class ComputeColor : IComputeColor { private Vector4 color; @@ -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 diff --git a/DirectX12GameEngine.Rendering/Materials/ComputeScalar.cs b/DirectX12GameEngine.Rendering/Materials/ComputeScalar.cs index 2e84ba9..b76502a 100644 --- a/DirectX12GameEngine.Rendering/Materials/ComputeScalar.cs +++ b/DirectX12GameEngine.Rendering/Materials/ComputeScalar.cs @@ -5,7 +5,7 @@ namespace DirectX12GameEngine.Rendering.Materials { [ShaderContract] - [ConstantBuffer] + [ConstantBufferView] public class ComputeScalar : IComputeScalar { private float scalarValue; @@ -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 diff --git a/DirectX12GameEngine.Rendering/Materials/ComputeTextureColor.cs b/DirectX12GameEngine.Rendering/Materials/ComputeTextureColor.cs index 785949a..aa1771d 100644 --- a/DirectX12GameEngine.Rendering/Materials/ComputeTextureColor.cs +++ b/DirectX12GameEngine.Rendering/Materials/ComputeTextureColor.cs @@ -24,7 +24,7 @@ public void Visit(MaterialGeneratorContext context) { if (Texture != null) { - context.Textures.Add(Texture); + context.ShaderResourceViews.Add(Texture); } } diff --git a/DirectX12GameEngine.Rendering/Materials/ComputeTextureScalar.cs b/DirectX12GameEngine.Rendering/Materials/ComputeTextureScalar.cs index 7d48c2e..9b34d07 100644 --- a/DirectX12GameEngine.Rendering/Materials/ComputeTextureScalar.cs +++ b/DirectX12GameEngine.Rendering/Materials/ComputeTextureScalar.cs @@ -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 @@ -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 diff --git a/DirectX12GameEngine.Rendering/Materials/MaterialGenerator.cs b/DirectX12GameEngine.Rendering/Materials/MaterialGenerator.cs index cd85cf2..1c0b63f 100644 --- a/DirectX12GameEngine.Rendering/Materials/MaterialGenerator.cs +++ b/DirectX12GameEngine.Rendering/Materials/MaterialGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Linq; using System.Threading.Tasks; using DirectX12GameEngine.Graphics; using Nito.AsyncEx; @@ -24,9 +25,11 @@ public static async Task 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) @@ -34,11 +37,6 @@ public static async Task GenerateAsync(MaterialDescriptor descriptor, 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(); } diff --git a/DirectX12GameEngine.Rendering/Materials/MaterialGeneratorContext.cs b/DirectX12GameEngine.Rendering/Materials/MaterialGeneratorContext.cs index edae8bc..fb42687 100644 --- a/DirectX12GameEngine.Rendering/Materials/MaterialGeneratorContext.cs +++ b/DirectX12GameEngine.Rendering/Materials/MaterialGeneratorContext.cs @@ -26,11 +26,13 @@ public MaterialGeneratorContext(GraphicsDevice device, Material material, Shader public ShaderContentManager Content { get; } - public IList ConstantBuffers { get; } = new List(); + public IList ConstantBufferViews { get; } = new List(); - public IList Samplers { get; } = new List(); + public IList ShaderResourceViews { get; } = new List(); + + public IList UnorderedAccessViews { get; } = new List(); - public IList Textures { get; } = new List(); + public IList Samplers { get; } = new List(); public IMaterialDescriptor? MaterialDescriptor => materialDescriptorStack.Count > 0 ? materialDescriptorStack.Peek() : null; @@ -67,7 +69,9 @@ public MaterialPass PushPass() MaterialPass? materialPass = MaterialPass; MaterialPass = null; - Textures.Clear(); + ConstantBufferViews.Clear(); + ShaderResourceViews.Clear(); + UnorderedAccessViews.Clear(); return materialPass; } @@ -131,28 +135,42 @@ public async Task CreateGraphicsPipelineStateAsync() public ID3D12RootSignature CreateRootSignature() { - List rootParameters = new List + int cbvShaderRegister = 0; + + List rootParameters = new List { - 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 shaderResourceRootDescriptorRanges = new List(); + + 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[] @@ -160,7 +178,7 @@ public ID3D12RootSignature CreateRootSignature() 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)); diff --git a/DirectX12GameEngine.Rendering/Materials/MaterialRoughnessMapFeature.cs b/DirectX12GameEngine.Rendering/Materials/MaterialRoughnessMapFeature.cs index 944e28a..3525c2e 100644 --- a/DirectX12GameEngine.Rendering/Materials/MaterialRoughnessMapFeature.cs +++ b/DirectX12GameEngine.Rendering/Materials/MaterialRoughnessMapFeature.cs @@ -25,14 +25,14 @@ public void Visit(MaterialGeneratorContext context) invertBuffer ??= Buffer.Constant.New(context.GraphicsDevice, Invert).DisposeBy(context.GraphicsDevice); - context.ConstantBuffers.Add(invertBuffer); + context.ConstantBufferViews.Add(invertBuffer); } #region Shader [ShaderMember] public IComputeScalar RoughnessMap { get; set; } = new ComputeScalar(); - [ConstantBuffer] public bool Invert + [ConstantBufferView] public bool Invert { get => invert; set diff --git a/DirectX12GameEngine.Rendering/Materials/MaterialShader.cs b/DirectX12GameEngine.Rendering/Materials/MaterialShader.cs index 0d688a8..be414b3 100644 --- a/DirectX12GameEngine.Rendering/Materials/MaterialShader.cs +++ b/DirectX12GameEngine.Rendering/Materials/MaterialShader.cs @@ -8,10 +8,10 @@ namespace DirectX12GameEngine.Rendering.Materials public class MaterialShader : ShaderBase { #nullable disable - [ConstantBuffer] public readonly uint RenderTargetCount; - [ConstantBuffer] public readonly GlobalBuffer Globals; - [ConstantBuffer] public readonly ViewProjectionTransform[] ViewProjectionTransforms; - [ConstantBuffer] public Matrix4x4[] WorldMatrices; + [ConstantBufferView] public readonly uint RenderTargetCount; + [ConstantBufferView] public readonly GlobalBuffer Globals; + [ConstantBufferView] public readonly ViewProjectionTransform[] ViewProjectionTransforms; + [ConstantBufferView] public Matrix4x4[] WorldMatrices; [ShaderMember] public readonly DirectionalLightGroup DirectionalLights; diff --git a/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj b/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj index 399634c..45249dc 100644 --- a/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj +++ b/DirectX12GameEngine.Shaders/DirectX12GameEngine.Shaders.csproj @@ -11,8 +11,8 @@ native - - + + diff --git a/DirectX12GameEngine.Shaders/ShaderGenerator.cs b/DirectX12GameEngine.Shaders/ShaderGenerator.cs index 819882e..f59fef2 100644 --- a/DirectX12GameEngine.Shaders/ShaderGenerator.cs +++ b/DirectX12GameEngine.Shaders/ShaderGenerator.cs @@ -167,7 +167,7 @@ private void CollectStructure(Type type, object? obj) Type parentType = type.BaseType; - while (parentType != null) + while (parentType != null && parentType != typeof(object) && parentType != typeof(ValueType)) { CollectStructure(parentType, obj); parentType = parentType.BaseType; @@ -312,8 +312,7 @@ private void WriteStructureField(MemberInfo memberInfo, Type memberType) writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}"); - int arrayCount = memberType.IsArray ? 2 : 0; - writer.Write(GetArrayString(arrayCount)); + if (memberType.IsArray) writer.Write("[2]"); writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute())); writer.WriteLine(";"); @@ -331,11 +330,8 @@ private void WriteResource(MemberInfo memberInfo, Type memberType, ShaderMemberA { switch (resourceType) { - case ConstantBufferAttribute _: - WriteConstantBuffer(memberInfo, memberType, bindingTracker.ConstantBuffer++); - break; - case SamplerAttribute _: - WriteSampler(memberInfo, memberType, bindingTracker.Sampler++); + case ConstantBufferViewAttribute _: + WriteConstantBufferView(memberInfo, memberType, bindingTracker.ConstantBuffer++); break; case ShaderResourceViewAttribute _: WriteShaderResourceView(memberInfo, memberType, bindingTracker.ShaderResourceView++); @@ -343,6 +339,9 @@ private void WriteResource(MemberInfo memberInfo, Type memberType, ShaderMemberA case UnorderedAccessViewAttribute _: WriteUnorderedAccessView(memberInfo, memberType, bindingTracker.UnorderedAccessView++); break; + case SamplerAttribute _: + WriteSampler(memberInfo, memberType, bindingTracker.Sampler++); + break; case StaticResourceAttribute _: WriteStaticResource(memberInfo, memberType); break; @@ -351,44 +350,44 @@ private void WriteResource(MemberInfo memberInfo, Type memberType, ShaderMemberA } } - private void WriteConstantBuffer(MemberInfo memberInfo, Type memberType, int binding) + private void WriteConstantBufferView(MemberInfo memberInfo, Type memberType, int binding) { - int arrayCount = memberType.IsArray ? 2 : 0; - writer.Write($"cbuffer {memberInfo.Name}Buffer"); writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute())); writer.WriteLine($" : register(b{binding})"); writer.WriteLine("{"); writer.Indent++; - writer.WriteLine($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}{GetArrayString(arrayCount)};"); + writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}"); + if (memberType.IsArray) writer.Write("[2]"); + writer.WriteLine(";"); writer.Indent--; writer.WriteLine("}"); writer.WriteLine(); } - private void WriteSampler(MemberInfo memberInfo, Type memberType, int binding) + private void WriteShaderResourceView(MemberInfo memberInfo, Type memberType, int binding) { writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}"); writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute())); - writer.Write($" : register(s{binding})"); + writer.Write($" : register(t{binding})"); writer.WriteLine(";"); writer.WriteLine(); } - private void WriteShaderResourceView(MemberInfo memberInfo, Type memberType, int binding) + private void WriteUnorderedAccessView(MemberInfo memberInfo, Type memberType, int binding) { writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}"); writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute())); - writer.Write($" : register(t{binding})"); + writer.Write($" : register(u{binding})"); writer.WriteLine(";"); writer.WriteLine(); } - private void WriteUnorderedAccessView(MemberInfo memberInfo, Type memberType, int binding) + private void WriteSampler(MemberInfo memberInfo, Type memberType, int binding) { writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}"); writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute())); - writer.Write($" : register(u{binding})"); + writer.Write($" : register(s{binding})"); writer.WriteLine(";"); writer.WriteLine(); } @@ -420,15 +419,13 @@ private void WriteStaticResource(MemberInfo memberInfo, Type memberType) stringWriter.GetStringBuilder().Length -= 2; - writer.Write("}"); + writer.Write(" }"); } writer.WriteLine(";"); writer.WriteLine(); } - private static string GetArrayString(int arrayCount) => arrayCount > 0 ? $"[{arrayCount}]" : ""; - private static string GetHlslSemantic(ShaderSemanticAttribute? semanticAttribute) { if (semanticAttribute is null) return ""; diff --git a/DirectX12GameEngine.Shaders/ShaderMemberAttribute.cs b/DirectX12GameEngine.Shaders/ShaderMemberAttribute.cs index c92448a..331e8f9 100644 --- a/DirectX12GameEngine.Shaders/ShaderMemberAttribute.cs +++ b/DirectX12GameEngine.Shaders/ShaderMemberAttribute.cs @@ -26,30 +26,30 @@ public ShaderMemberAttribute([CallerLineNumber] int order = 0) public bool Override { get; set; } } - public class ConstantBufferAttribute : ShaderMemberAttribute + public class ConstantBufferViewAttribute : ShaderMemberAttribute { - public ConstantBufferAttribute([CallerLineNumber] int order = 0) : base(order) + public ConstantBufferViewAttribute([CallerLineNumber] int order = 0) : base(order) { } } - public class SamplerAttribute : ShaderMemberAttribute + public class ShaderResourceViewAttribute : ShaderMemberAttribute { - public SamplerAttribute([CallerLineNumber] int order = 0) : base(order) + public ShaderResourceViewAttribute([CallerLineNumber] int order = 0) : base(order) { } } - public class ShaderResourceViewAttribute : ShaderMemberAttribute + public class UnorderedAccessViewAttribute : ShaderMemberAttribute { - public ShaderResourceViewAttribute([CallerLineNumber] int order = 0) : base(order) + public UnorderedAccessViewAttribute([CallerLineNumber] int order = 0) : base(order) { } } - public class UnorderedAccessViewAttribute : ShaderMemberAttribute + public class SamplerAttribute : ShaderMemberAttribute { - public UnorderedAccessViewAttribute([CallerLineNumber] int order = 0) : base(order) + public SamplerAttribute([CallerLineNumber] int order = 0) : base(order) { } } diff --git a/DirectX12GameEngine.Shaders/ShaderResource.cs b/DirectX12GameEngine.Shaders/ShaderResource.cs index 1ef023d..80caf09 100644 --- a/DirectX12GameEngine.Shaders/ShaderResource.cs +++ b/DirectX12GameEngine.Shaders/ShaderResource.cs @@ -29,6 +29,12 @@ public class Texture2DResource : ShaderResource where T : unmanaged public T Sample(SamplerResource sampler, Vector2 texCoord) => throw new NotImplementedException(); } + [UnorderedAccessView] + public class RWTexture2DResource : ShaderResource where T : unmanaged + { + public T this[Numerics.UInt2 index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + } + [ShaderResourceView] public class Texture2DArrayResource : ShaderResource { @@ -62,10 +68,4 @@ public class RWStructuredBufferResource : ShaderResource { public T this[uint index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } } - - [UnorderedAccessView] - public class RWTexture2DResource : ShaderResource where T : unmanaged - { - public T this[Numerics.UInt2 index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - } }