yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f4d5372d3
master
1//TEST(smoke,render):COMPARE_HLSL_GLSL_RENDER: 2 3// This is a basic test case for cross-compilation behavior. 4// 5// We will define distinct HLSL and GLSL entry points, 6// but the two will share a dependency on a file of 7// pure Slang code that provides the actual shading logic. 8 9#if defined ( __HLSL__ ) 10 11// Pull in Slang code depdendency using extended syntax: 12__import cross_compile0 ; 13 14cbuffer Uniforms 15{ 16float4x4 modelViewProjection ; 17}; 18 19struct AssembledVertex 20{ 21float3 position ; 22float3 color ; 23}; 24 25struct CoarseVertex 26{ 27float3 color ; 28}; 29 30struct Fragment 31{ 32float4 color ; 33}; 34 35// Vertex Shader 36 37struct VertexStageInput 38{ 39AssembledVertex assembledVertex : A ; 40}; 41 42struct VertexStageOutput 43{ 44CoarseVertex coarseVertex : CoarseVertex ; 45float4 sv_position : SV_Position ; 46}; 47 48[ shader ( "vertex" )] 49VertexStageOutput vertexMain ( VertexStageInput input ) 50{ 51VertexStageOutput output ; 52 53float3 position = input . assembledVertex . position ; 54float3 color = input . assembledVertex . color ; 55 56output . coarseVertex . color = color ; 57output . sv_position = mul ( modelViewProjection , float4 ( position , 1.0 )); 58 59return output ; 6061 } 62 63// Fragment Shader 64 65struct FragmentStageInput 66{ 67CoarseVertex coarseVertex : CoarseVertex ; 68}; 69 70struct FragmentStageOutput 71{ 72Fragment fragment : SV_Target ; 73}; 74 75[ shader ( "fragment" )] 76FragmentStageOutput fragmentMain ( FragmentStageInput input ) 77{ 78FragmentStageOutput output ; 79 80float3 color = input . coarseVertex . color ; 81 82color = transformColor ( color ); 83 84output . fragment . color = float4 ( color , 1.0 ); 85 86return output ; 87} 88 89#elif defined ( __GLSL__ ) 90 91#version 420 92 93float saturate ( float x ) 94{ 95return clamp ( x , float ( 0 ), float ( 1 )); 96 } 97 98vec3 transformColor ( vec3 color ) 99{ 100vec3 result ; 101 102result . x = sin ( 20.0 * ( color . x + color . y )); 103result . y = saturate ( cos ( color . z * 30.0 )); 104result . z = sin ( color . x * color . y * color . z * 100.0 ); 105 106result = 0.5 * ( result + 1 ); 107 108return result ; 109} 110 111uniform Uniforms 112{ 113mat4x4 modelViewProjection ; 114}; 115 116#define ASSEMBLED_VERTEX ( QUAL ) \ 117/* */ 118 119#define V2F ( QUAL ) \ 120layout(location = 0) QUAL vec3 coarse_color; \ 121/* */ 122 123// Vertex Shader 124 125#ifdef __GLSL_VERTEX__ 126 127layout ( location = 0 ) 128in vec3 assembled_position ; 129 130layout ( location = 1 ) 131in vec3 assembled_color ; 132 133V2F ( out ) 134 135void main () 136{ 137vec3 position = assembled_position ; 138vec3 color = assembled_color ; 139 140coarse_color = color ; 141// gl_Position = modelViewProjection * vec4(position, 1.0); 142gl_Position = vec4 ( position , 1.0 ) * modelViewProjection ; 143} 144 145#endif 146 147#ifdef __GLSL_FRAGMENT__ 148 149V2F ( in ) 150 151layout ( location = 0 ) 152out vec4 fragment_color ; 153 154void main () 155{ 156vec3 color = coarse_color ; 157 158color = transformColor ( color ); 159 160fragment_color = vec4 ( color , 1.0 ); 161} 162 163 164#endif 165 166#endif