1 use super::Error; 2 3 impl crate::ScalarKind { to_hlsl_cast(self) -> &'static str4 pub(super) fn to_hlsl_cast(self) -> &'static str { 5 match self { 6 Self::Float => "asfloat", 7 Self::Sint => "asint", 8 Self::Uint => "asuint", 9 Self::Bool => unreachable!(), 10 } 11 } 12 13 /// Helper function that returns scalar related strings 14 /// 15 /// <https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-scalar> to_hlsl_str(self, width: crate::Bytes) -> Result<&'static str, Error>16 pub(super) fn to_hlsl_str(self, width: crate::Bytes) -> Result<&'static str, Error> { 17 match self { 18 Self::Sint => Ok("int"), 19 Self::Uint => Ok("uint"), 20 Self::Float => match width { 21 2 => Ok("half"), 22 4 => Ok("float"), 23 8 => Ok("double"), 24 _ => Err(Error::UnsupportedScalar(self, width)), 25 }, 26 Self::Bool => Ok("bool"), 27 } 28 } 29 } 30 31 impl crate::TypeInner { is_matrix(&self) -> bool32 pub(super) fn is_matrix(&self) -> bool { 33 match *self { 34 Self::Matrix { .. } => true, 35 _ => false, 36 } 37 } 38 } 39 40 impl crate::StorageFormat { to_hlsl_str(self) -> &'static str41 pub(super) fn to_hlsl_str(self) -> &'static str { 42 match self { 43 Self::R16Float => "float", 44 Self::R8Unorm => "unorm float", 45 Self::R8Snorm => "snorm float", 46 Self::R8Uint | Self::R16Uint => "uint", 47 Self::R8Sint | Self::R16Sint => "int", 48 49 Self::Rg16Float => "float2", 50 Self::Rg8Unorm => "unorm float2", 51 Self::Rg8Snorm => "snorm float2", 52 53 Self::Rg8Sint | Self::Rg16Sint => "int2", 54 Self::Rg8Uint | Self::Rg16Uint => "uint2", 55 56 Self::Rg11b10Float => "float3", 57 58 Self::Rgba16Float | Self::R32Float | Self::Rg32Float | Self::Rgba32Float => "float4", 59 Self::Rgba8Unorm | Self::Rgb10a2Unorm => "unorm float4", 60 Self::Rgba8Snorm => "snorm float4", 61 62 Self::Rgba8Uint 63 | Self::Rgba16Uint 64 | Self::R32Uint 65 | Self::Rg32Uint 66 | Self::Rgba32Uint => "uint4", 67 Self::Rgba8Sint 68 | Self::Rgba16Sint 69 | Self::R32Sint 70 | Self::Rg32Sint 71 | Self::Rgba32Sint => "int4", 72 } 73 } 74 } 75 76 impl crate::BuiltIn { to_hlsl_str(self) -> Result<&'static str, Error>77 pub(super) fn to_hlsl_str(self) -> Result<&'static str, Error> { 78 Ok(match self { 79 Self::Position => "SV_Position", 80 // vertex 81 Self::ClipDistance => "SV_ClipDistance", 82 Self::CullDistance => "SV_CullDistance", 83 Self::InstanceIndex => "SV_InstanceID", 84 // based on this page https://docs.microsoft.com/en-us/windows/uwp/gaming/glsl-to-hlsl-reference#comparing-opengl-es-20-with-direct3d-11 85 // No meaning unless you target Direct3D 9 86 Self::PointSize => "PSIZE", 87 Self::VertexIndex => "SV_VertexID", 88 // fragment 89 Self::FragDepth => "SV_Depth", 90 Self::FrontFacing => "SV_IsFrontFace", 91 Self::PrimitiveIndex => "SV_PrimitiveID", 92 Self::SampleIndex => "SV_SampleIndex", 93 Self::SampleMask => "SV_Coverage", 94 // compute 95 Self::GlobalInvocationId => "SV_DispatchThreadID", 96 Self::LocalInvocationId => "SV_GroupThreadID", 97 Self::LocalInvocationIndex => "SV_GroupIndex", 98 Self::WorkGroupId => "SV_GroupID", 99 // The specific semantic we use here doesn't matter, because references 100 // to this field will get replaced with references to `SPECIAL_CBUF_VAR` 101 // in `Writer::write_expr`. 102 Self::NumWorkGroups => "SV_GroupID", 103 Self::BaseInstance | Self::BaseVertex | Self::WorkGroupSize => { 104 return Err(Error::Unimplemented(format!("builtin {:?}", self))) 105 } 106 Self::ViewIndex => { 107 return Err(Error::Custom(format!("Unsupported builtin {:?}", self))) 108 } 109 }) 110 } 111 } 112 113 impl crate::Interpolation { 114 /// Helper function that returns the string corresponding to the HLSL interpolation qualifier to_hlsl_str(self) -> &'static str115 pub(super) fn to_hlsl_str(self) -> &'static str { 116 match self { 117 Self::Perspective => "linear", 118 Self::Linear => "noperspective", 119 Self::Flat => "nointerpolation", 120 } 121 } 122 } 123 124 impl crate::Sampling { 125 /// Return the HLSL auxiliary qualifier for the given sampling value. to_hlsl_str(self) -> Option<&'static str>126 pub(super) fn to_hlsl_str(self) -> Option<&'static str> { 127 match self { 128 Self::Center => None, 129 Self::Centroid => Some("centroid"), 130 Self::Sample => Some("sample"), 131 } 132 } 133 } 134 135 impl crate::AtomicFunction { 136 /// Return the HLSL suffix for the `InterlockedXxx` method. to_hlsl_suffix(self) -> &'static str137 pub(super) fn to_hlsl_suffix(self) -> &'static str { 138 match self { 139 Self::Add | Self::Subtract => "Add", 140 Self::And => "And", 141 Self::InclusiveOr => "Or", 142 Self::ExclusiveOr => "Xor", 143 Self::Min => "Min", 144 Self::Max => "Max", 145 Self::Exchange { compare: None } => "Exchange", 146 Self::Exchange { .. } => "", //TODO 147 } 148 } 149 } 150