yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Anders LeinoMigrate render-test away from deprecated compile request API (#6514)f4d5372d3

master
2.6 KiB166 linesraw
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{
16	float4x4 modelViewProjection;
17};
18
19struct AssembledVertex
20{
21	float3	position;
22	float3	color;
23};
24
25struct CoarseVertex
26{
27	float3	color;
28};
29
30struct Fragment
31{
32	float4 color;
33};
34
35// Vertex  Shader
36
37struct VertexStageInput
38{
39	AssembledVertex assembledVertex	: A;
40};
41
42struct VertexStageOutput
43{
44	CoarseVertex	coarseVertex	: CoarseVertex;
45	float4			sv_position		: SV_Position;
46};
47
48[shader("vertex")]
49VertexStageOutput vertexMain(VertexStageInput input)
50{
51	VertexStageOutput output;
52
53	float3 position = input.assembledVertex.position;
54	float3 color	= input.assembledVertex.color;
55
56	output.coarseVertex.color = color;
57	output.sv_position = mul(modelViewProjection, float4(position, 1.0));
58
59	return output;
60	
61}
62
63// Fragment Shader
64
65struct FragmentStageInput
66{
67	CoarseVertex	coarseVertex	: CoarseVertex;
68};
69
70struct FragmentStageOutput
71{
72	Fragment fragment	: SV_Target;
73};
74
75[shader("fragment")]
76FragmentStageOutput fragmentMain(FragmentStageInput input)
77{
78	FragmentStageOutput output;
79
80	float3 color = input.coarseVertex.color;
81
82	color = transformColor(color);
83
84	output.fragment.color = float4(color, 1.0);
85
86	return output;
87}
88
89#elif defined(__GLSL__)
90
91#version 420
92
93float saturate(float x)
94{
95    return clamp(x, float(0), float(1));	
96}
97
98vec3 transformColor(vec3 color)
99{
100	vec3 result;
101
102	result.x = sin(20.0 * (color.x + color.y));
103	result.y = saturate(cos(color.z * 30.0));
104	result.z = sin(color.x * color.y * color.z * 100.0);
105
106	result = 0.5 * (result + 1);
107
108	return result;
109}
110
111uniform Uniforms
112{
113	mat4x4 modelViewProjection;
114};
115
116#define ASSEMBLED_VERTEX(QUAL)		\
117	/* */
118
119#define V2F(QUAL)									\
120	layout(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{
137	vec3 position = assembled_position;
138	vec3 color	= assembled_color;
139
140	coarse_color = color;
141//	gl_Position = modelViewProjection * vec4(position, 1.0);
142	gl_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{
156	vec3 color = coarse_color;
157
158	color = transformColor(color);
159
160	fragment_color = vec4(color, 1.0);
161}
162
163
164#endif
165
166#endif