From af2cbe43c83a1ad656b3ba0687946dd02c2d4ae4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 18 Aug 2019 11:14:11 +0200 Subject: [PATCH] Fields loading code moved to separate method --- .../Translation/ShaderLoader.cs | 98 ++++++++++--------- 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index c1b683f8..cf5d819c 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -144,53 +144,63 @@ namespace ComputeSharp.Shaders.Translation // Inspect the captured fields foreach (FieldInfo fieldInfo in shaderFields) { - Type fieldType = fieldInfo.FieldType; - string fieldName = fieldInfo.Name; - - // Constant buffer - if (HlslKnownTypes.IsConstantBufferType(fieldType)) - { - DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, _ConstantBuffersCount)); - - // Track the buffer field - _BufferFields.Add(fieldInfo); - - string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); - _BuffersList.Add(new ConstantBufferFieldInfo(fieldType, typeName, fieldName, _ConstantBuffersCount++)); - } - else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) - { - // Root parameter for a readonly buffer - DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, _ReadOnlyBuffersCount)); - - // Track the buffer field - _BufferFields.Add(fieldInfo); - - string typeName = HlslKnownTypes.GetMappedName(fieldType); - _BuffersList.Add(new ReadOnlyBufferFieldInfo(fieldType, typeName, fieldName, _ReadOnlyBuffersCount++)); - } - else if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) - { - // Root parameter for a read write buffer - DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, _ReadWriteBuffersCount)); - - // Track the buffer field - _BufferFields.Add(fieldInfo); - - string typeName = HlslKnownTypes.GetMappedName(fieldType); - _BuffersList.Add(new ReadWriteBufferFieldInfo(fieldType, typeName, fieldName, _ReadWriteBuffersCount++)); - } - else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) - { - // Register the captured field - _VariableFields.Add(fieldInfo); - string typeName = HlslKnownTypes.GetMappedName(fieldType); - _FieldsList.Add(new CapturedFieldInfo(fieldType, typeName, fieldName)); - } - else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}"); + LoadFieldInfo(fieldInfo); } } + /// + /// Loads a specified and adds it to the shader model + /// + /// The target to load + /// The optional explicit name to use for the field + private void LoadFieldInfo(FieldInfo fieldInfo, string? name = null) + { + Type fieldType = fieldInfo.FieldType; + string fieldName = name ?? fieldInfo.Name; + + // Constant buffer + if (HlslKnownTypes.IsConstantBufferType(fieldType)) + { + DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, _ConstantBuffersCount)); + + // Track the buffer field + _BufferFields.Add(fieldInfo); + + string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); + _BuffersList.Add(new ConstantBufferFieldInfo(fieldType, typeName, fieldName, _ConstantBuffersCount++)); + } + else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) + { + // Root parameter for a readonly buffer + DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, _ReadOnlyBuffersCount)); + + // Track the buffer field + _BufferFields.Add(fieldInfo); + + string typeName = HlslKnownTypes.GetMappedName(fieldType); + _BuffersList.Add(new ReadOnlyBufferFieldInfo(fieldType, typeName, fieldName, _ReadOnlyBuffersCount++)); + } + else if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) + { + // Root parameter for a read write buffer + DescriptorRanges.Add(new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, _ReadWriteBuffersCount)); + + // Track the buffer field + _BufferFields.Add(fieldInfo); + + string typeName = HlslKnownTypes.GetMappedName(fieldType); + _BuffersList.Add(new ReadWriteBufferFieldInfo(fieldType, typeName, fieldName, _ReadWriteBuffersCount++)); + } + else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) + { + // Register the captured field + _VariableFields.Add(fieldInfo); + string typeName = HlslKnownTypes.GetMappedName(fieldType); + _FieldsList.Add(new CapturedFieldInfo(fieldType, typeName, fieldName)); + } + else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}"); + } + /// /// Loads the entry method for the current shader being loaded ///