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