yum-mirror/slang

Making it easier to work with shaders

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

Yong HeMake `-no-mangle` option work, add `-no-hlsl-binding`. (#3817)a23adc221

master
2.0 KiB85 linesraw
1//TEST:COMPARE_HLSL: -profile ps_4_0 -entry main
2
3// We need to allow the user to add explicit bindings to their parameters,
4// and we can't go and auto-assign anything to use the same locations.
5
6#ifdef __SLANG__
7#define R(X) /**/
8#define BEGIN_CBUFFER(NAME) cbuffer NAME
9#define BEGIN_CBUFFER_R(NAME, REG) cbuffer NAME : REG
10#define END_CBUFFER(NAME, REG) /**/
11#define CBUFFER_REF(NAME, FIELD) FIELD
12#else
13#define R(X) X
14#define BEGIN_CBUFFER(NAME) struct SLANG_ParameterGroup_##NAME
15#define BEGIN_CBUFFER_R(NAME, REG) BEGIN_CBUFFER(NAME)
16#define END_CBUFFER(NAME, REG) ; cbuffer NAME : REG { SLANG_ParameterGroup_##NAME NAME; }
17#define CBUFFER_REF(NAME, FIELD) NAME.FIELD
18
19#define CA CA_0
20#define ca ca_0
21
22#define CB CB_0
23#define cb cb_0
24
25#define CC CC_0
26#define cc cc_0
27
28#define sa sa_0
29#define sb sb_0
30#define sc sc_0
31
32#define ta ta_0
33#define tb tb_0
34#define tc tc_0
35
36#endif
37
38float4 use(float4 val) { return val; };
39float4 use(Texture2D t, SamplerState s) { return t.Sample(s, 0.0); }
40
41// We'll make three textures, but explicit assign the third one
42// to the slot `t0`. We expect the others to shift further along
43// to "make room".
44Texture2D 		ta R(: register(t1));
45Texture2D 		tb R(: register(t2));
46Texture2D 		tc : register(t0);
47
48
49// The explicit binding may "split" the range of register available
50// for automatic placement. We use a "first-fit" approach to pack
51// things in:
52SamplerState 	sa R(: register(s0));
53SamplerState 	sb R(: register(s2));
54SamplerState 	sc : register(s1);
55
56// It's also okay to use a register that *doesn't* conflict,
57// and even to make things non-contiguous. Here we bind
58// the third constnat buffer to register `b9`
59//
60BEGIN_CBUFFER(CA)
61{
62	float ca;
63}
64END_CBUFFER(CA, register(b0))
65
66//
67BEGIN_CBUFFER(CB)
68{
69	float cb;
70}
71END_CBUFFER(CB, register(b1))
72//
73BEGIN_CBUFFER_R(CC, register(b9))
74{
75	float cc;
76}
77END_CBUFFER(CC, register(b9))
78
79float4 main() : SV_TARGET
80{
81	// Go ahead and use everything in this case:
82	return use(ta, sa) + use(CBUFFER_REF(CA,ca))
83		+  use(tb, sb) + use(CBUFFER_REF(CB,cb))
84		+  use(tc, sc) + use(CBUFFER_REF(CC,cc));
85}