using System.Collections.Generic; using System.Globalization; namespace UnityEditor.TerrainTools { /// /// Enum for determining the type of a given HLSL value during shader generation /// internal enum HlslValueType { Float = 0, Float2, Float3, Float4, } /// /// Representation for an HLSL float /// internal struct HlslFloat { /// /// The value for the HLSL float /// public float val; /// /// The constructor for an HlslFloat /// /// The GPU value for this HlslFloat public HlslFloat(float val) { this.val = val; } } /// // Representation for an HLSL float2 /// internal struct HlslFloat2 { /// /// The x-compenent for the HLSL float2 /// public float x; /// /// The y-compenent for the HLSL float2 /// public float y; /// /// The constructor for an HlslFloat2 /// /// The GPU value of the x-component to be used for this HlslFloat2 /// The GPU value of the y-component to be used for this HlslFloat2 public HlslFloat2(float x, float y) { this.x = x; this.y = y; } } /// /// Representation for an HLSL float3 /// internal struct HlslFloat3 { /// /// The x-compenent for the HLSL float3 /// public float x; /// /// The y-compenent for the HLSL float3 /// public float y; /// /// The z-compenent for the HLSL float3 /// public float z; /// /// The constructor for an HlslFloat3 /// /// The GPU value of the x-component to be used for this HlslFloat3 /// The GPU value of the y-component to be used for this HlslFloat3 /// The GPU value of the z-component to be used for this HlslFloat3 public HlslFloat3(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } } /// /// Representation for an HLSL float4 /// internal struct HlslFloat4 { /// /// The x-compenent for the HLSL float4 /// public float x; /// /// The y-compenent for the HLSL float4 /// public float y; /// /// The z-compenent for the HLSL float4 /// public float z; /// /// The w-compenent for the HLSL float4 /// public float w; /// /// The constructor for an HlslFloat4 /// /// The GPU value of the x-component to be used for this HlslFloat4 /// The GPU value of the y-component to be used for this HlslFloat4 /// The GPU value of the z-component to be used for this HlslFloat4 /// The GPU value of the w-component to be used for this HlslFloat4 public HlslFloat4(float x, float y, float z, float w) { this.x = x; this.y = y; this.z = z; this.w = w; } } /// /// Representation for an HLSL function input parameter /// internal struct HlslInput { /// /// The name of this HLSL function input parameter /// public string name; /// /// Returns the value type for the variable this HlslInput represents /// public HlslValueType valueType { get; private set; } private HlslFloat m_floatValue; /// /// Returns the HLSL float value. Sets the HLSL float value and value /// type to HlslValueType.Float /// public HlslFloat floatValue { get { return m_floatValue; } set { valueType = HlslValueType.Float; m_floatValue = value; } } private HlslFloat2 m_float2Value; /// /// Returns the HLSL float2 value. Sets the HLSL float value and value /// type to HlslValueType.Float2 /// public HlslFloat2 float2Value { get { return m_float2Value; } set { valueType = HlslValueType.Float2; m_float2Value = value; } } private HlslFloat3 m_float3Value; /// /// Returns the HLSL float3 value. Sets the HLSL float value and value /// type to HlslValueType.Float3 /// public HlslFloat3 float3Value { get { return m_float3Value; } set { valueType = HlslValueType.Float3; m_float3Value = value; } } private HlslFloat4 m_float4Value; /// /// Returns the HLSL float4 value. Sets the HLSL float value and value /// type to HlslValueType.Float4 /// public HlslFloat4 float4Value { get { return m_float4Value; } set { valueType = HlslValueType.Float4; m_float4Value = value; } } /// /// Returns the string representation of the HlslValueType for this struct /// public string GetHlslValueTypeString() { switch(valueType) { case HlslValueType.Float: return "float"; case HlslValueType.Float2: return "float2"; case HlslValueType.Float3: return "float3"; case HlslValueType.Float4: return "float4"; } return "unsupported_type"; } /// /// Returns the formatted HLSL string for the default value declaration for this struct's HlslValueType /// public string GetDefaultValueString() { string valueTypeString = GetHlslValueTypeString(); string constructedValueString = null; switch(valueType) { case HlslValueType.Float: constructedValueString = floatValue.val.ToString( CultureInfo.InvariantCulture ); break; case HlslValueType.Float2: constructedValueString = string.Format( CultureInfo.InvariantCulture, "{0}({1})", valueTypeString, float2Value.x.ToString( CultureInfo.InvariantCulture ) + ", " + float2Value.y.ToString( CultureInfo.InvariantCulture ) ); break; case HlslValueType.Float3: constructedValueString = string.Format( CultureInfo.InvariantCulture, "{0}({1})", valueTypeString, float3Value.x.ToString( CultureInfo.InvariantCulture ) + ", " + float3Value.y.ToString( CultureInfo.InvariantCulture ) + ", " + float3Value.z.ToString( CultureInfo.InvariantCulture ) ); break; case HlslValueType.Float4: constructedValueString = string.Format( CultureInfo.InvariantCulture, "{0}({1})", valueTypeString, float4Value.x.ToString( CultureInfo.InvariantCulture ) + ", " + float4Value.y.ToString( CultureInfo.InvariantCulture ) + ", " + float4Value.z.ToString( CultureInfo.InvariantCulture ) + ", " + float4Value.w.ToString( CultureInfo.InvariantCulture ) ); break; default: return "unsupported_type()"; } return constructedValueString; } } }