forked from QianMo/GPU-Gems-Book-Source-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompCosines.fx
428 lines (356 loc) · 8.98 KB
/
CompCosines.fx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
texture tCosineLUT;
texture tBiasNoise;
float4 cUTrans0;
float4 cUTrans1;
float4 cUTrans2;
float4 cUTrans3;
float4 cUTrans4;
float4 cUTrans5;
float4 cUTrans6;
float4 cUTrans7;
float4 cUTrans8;
float4 cUTrans9;
float4 cUTrans10;
float4 cUTrans11;
float4 cUTrans12;
float4 cUTrans13;
float4 cUTrans14;
float4 cUTrans15;
float4 cCoef0;
float4 cCoef1;
float4 cCoef2;
float4 cCoef3;
float4 cCoef4;
float4 cCoef5;
float4 cCoef6;
float4 cCoef7;
float4 cCoef8;
float4 cCoef9;
float4 cCoef10;
float4 cCoef11;
float4 cCoef12;
float4 cCoef13;
float4 cCoef14;
float4 cCoef15;
float4 cReScale;
float4 cNoiseXForm0_00;
float4 cNoiseXForm0_10;
float4 cNoiseXForm1_00;
float4 cNoiseXForm1_10;
float4 cScaleBias;
sampler CosineLUT = sampler_state
{
Texture = <tCosineLUT>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
};
sampler BiasNoise = sampler_state
{
Texture = <tBiasNoise>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
};
struct VertIn
{
float4 Position : POSITION;
float4 Uv : TEXCOORD0;
};
struct VertOut
{
float4 Position : POSITION;
float4 Uv0 : TEXCOORD0; // Ripple texture coords
float4 Uv1 : TEXCOORD1; // Ripple texture coords
float4 Uv2 : TEXCOORD2; // Ripple texture coords
float4 Uv3 : TEXCOORD3; // Ripple texture coords
};
VertOut vs_main(VertIn In,
uniform float4 rot0,
uniform float4 rot1,
uniform float4 rot2,
uniform float4 rot3)
{
VertOut Out;
Out.Position = In.Position;
float4 uv = float4(0.f, 0.f, 0.f, 1.f);
uv.x = dot(In.Uv, rot0);
Out.Uv0 = uv;
uv.x = dot(In.Uv, rot1);
Out.Uv1 = uv;
uv.x = dot(In.Uv, rot2);
Out.Uv2 = uv;
uv.x = dot(In.Uv, rot3);
Out.Uv3 = uv;
return Out;
}
technique T0
{
pass P0
{
// Vertex shader
VertexShader = compile vs_1_1 vs_main(cUTrans0, cUTrans1, cUTrans2, cUTrans3);
// Pixel shader
PixelShader =
asm
{
// Composite the cosines together.
// Input map is cosine(pix) for each of
// the 4 waves.
//
// The constants are set up so:
// Nx = -freq * amp * dirX * cos(pix);
// Ny = -freq * amp * dirY * cos(pix);
// So c[i].x = -freq[i] * amp[i] * dirX[i]
// etc.
// All textures are:
// (r,g,b,a) = (cos(), cos(), 1, 1)
//
ps_1_1
tex t0;
tex t1;
tex t2;
tex t3;
mul r0, t0_bx2, c0;
mad r0, t1_bx2, c1, r0;
mad r0, t2_bx2, c2, r0;
mad r0, t3_bx2, c3, r0;
// Now scale and bias it back into range for output.
mul r0, r0, c4;
add r0, r0, c4;
};
PixelShaderConstant1[0] = <cCoef0>;
PixelShaderConstant1[1] = <cCoef1>;
PixelShaderConstant1[2] = <cCoef2>;
PixelShaderConstant1[3] = <cCoef3>;
PixelShaderConstant1[4] = <cReScale>;
Sampler[0] = (CosineLUT);
Sampler[1] = (CosineLUT);
Sampler[2] = (CosineLUT);
Sampler[3] = (CosineLUT);
// Clip/Raster state
SrcBlend = One;
DestBlend = Zero;
SpecularEnable = FALSE;
AlphaBlendEnable = TRUE;
FogEnable = FALSE;
CullMode = NONE;
ZEnable = FALSE;
}
pass P1
{
// Vertex shader
VertexShader = compile vs_1_1 vs_main(cUTrans4, cUTrans5, cUTrans6, cUTrans7);
// Pixel shader
PixelShader =
asm
{
// Composite the cosines together.
// Input map is cosine(pix) for each of
// the 4 waves.
//
// The constants are set up so:
// Nx = -freq * amp * dirX * cos(pix);
// Ny = -freq * amp * dirY * cos(pix);
// So c[i].x = -freq[i] * amp[i] * dirX[i]
// etc.
// All textures are:
// (r,g,b,a) = (cos(), cos(), 1, 1)
//
// So c[0].z = 1, but all other c[i].z = 0
// Note also the c4 used for biasing back at the end.
ps_1_1
tex t0;
tex t1;
tex t2;
tex t3;
mul r0, t0_bx2, c0;
mad r0, t1_bx2, c1, r0;
mad r0, t2_bx2, c2, r0;
mad r0, t3_bx2, c3, r0;
// Now bias it back into range [0..1] for output.
mul r0, r0, c4; // c4 = (0.5, 0.5, 0.5, 1)
add r0, r0, c4;
};
PixelShaderConstant1[0] = <cCoef4>;
PixelShaderConstant1[1] = <cCoef5>;
PixelShaderConstant1[2] = <cCoef6>;
PixelShaderConstant1[3] = <cCoef7>;
PixelShaderConstant1[4] = <cReScale>;
Sampler[0] = (CosineLUT);
Sampler[1] = (CosineLUT);
Sampler[2] = (CosineLUT);
Sampler[3] = (CosineLUT);
// Clip/Raster state
SrcBlend = One;
DestBlend = One;
SpecularEnable = FALSE;
AlphaBlendEnable = TRUE;
FogEnable = FALSE;
CullMode = NONE;
ZEnable = FALSE;
}
pass P2
{
// Vertex shader
VertexShader = compile vs_1_1 vs_main(cUTrans8, cUTrans9, cUTrans10, cUTrans11);
// Pixel shader
PixelShader =
asm
{
// Composite the cosines together.
// Input map is cosine(pix) for each of
// the 4 waves.
//
// The constants are set up so:
// Nx = -freq * amp * dirX * cos(pix);
// Ny = -freq * amp * dirY * cos(pix);
// So c[i].x = -freq[i] * amp[i] * dirX[i]
// etc.
// All textures are:
// (r,g,b,a) = (cos(), cos(), 1, 1)
//
// So c[0].z = 1, but all other c[i].z = 0
// Note also the c4 used for biasing back at the end.
ps_1_1
tex t0;
tex t1;
tex t2;
tex t3;
mul r0, t0_bx2, c0;
mad r0, t1_bx2, c1, r0;
mad r0, t2_bx2, c2, r0;
mad r0, t3_bx2, c3, r0;
// Now bias it back into range [0..1] for output.
mul r0, r0, c4; // c4 = (0.5, 0.5, 0.5, 1)
add r0, r0, c4;
};
PixelShaderConstant1[0] = <cCoef8>;
PixelShaderConstant1[1] = <cCoef9>;
PixelShaderConstant1[2] = <cCoef10>;
PixelShaderConstant1[3] = <cCoef11>;
PixelShaderConstant1[4] = <cReScale>;
Sampler[0] = (CosineLUT);
Sampler[1] = (CosineLUT);
Sampler[2] = (CosineLUT);
Sampler[3] = (CosineLUT);
// Clip/Raster state
SrcBlend = One;
DestBlend = One;
SpecularEnable = FALSE;
AlphaBlendEnable = TRUE;
FogEnable = FALSE;
CullMode = NONE;
ZEnable = FALSE;
}
pass P3
{
// Vertex shader
VertexShader = compile vs_1_1 vs_main(cUTrans12, cUTrans13, cUTrans14, cUTrans15);
// Pixel shader
PixelShader =
asm
{
// Composite the cosines together.
// Input map is cosine(pix) for each of
// the 4 waves.
//
// The constants are set up so:
// Nx = -freq * amp * dirX * cos(pix);
// Ny = -freq * amp * dirY * cos(pix);
// So c[i].x = -freq[i] * amp[i] * dirX[i]
// etc.
// All textures are:
// (r,g,b,a) = (cos(), cos(), 1, 1)
//
// So c[0].z = 1, but all other c[i].z = 0
// Note also the c4 used for biasing back at the end.
ps_1_1
tex t0;
tex t1;
tex t2;
tex t3;
mul r0, t0_bx2, c0;
mad r0, t1_bx2, c1, r0;
mad r0, t2_bx2, c2, r0;
mad r0, t3_bx2, c3, r0;
// Now bias it back into range [0..1] for output.
mul r0, r0, c4; // c4 = (0.5, 0.5, 0.5, 1)
add r0, r0, c4;
};
PixelShaderConstant1[0] = <cCoef12>;
PixelShaderConstant1[1] = <cCoef13>;
PixelShaderConstant1[2] = <cCoef14>;
PixelShaderConstant1[3] = <cCoef15>;
PixelShaderConstant1[4] = <cReScale>;
Sampler[0] = (CosineLUT);
Sampler[1] = (CosineLUT);
Sampler[2] = (CosineLUT);
Sampler[3] = (CosineLUT);
// Clip/Raster state
SrcBlend = One;
DestBlend = One;
SpecularEnable = FALSE;
AlphaBlendEnable = TRUE;
FogEnable = FALSE;
CullMode = NONE;
ZEnable = FALSE;
}
pass p4
{
Sampler[0] = (BiasNoise);
Sampler[1] = (BiasNoise);
VertexShader =
asm
{
vs_1_1
dcl_position v0
dcl_texcoord v7
// Take in a screen space position,
// transform the UVW,
// c0 = uvXform0[0]
// c1 = uvXform0[1]
// c2 = uvXform1[0]
// c3 = uvXform1[1]
// c4 = (0,0.5,1.0,2.0)
// c5 = (noiseScale, bias, 0, 1)
mov oPos, v0;
mov r0.zw, c4.xxxz; // yzw will stay constant (0,0,1);
dp4 r0.x, v7, c0;
dp4 r0.y, v7, c1;
mov oT0, r0;
dp4 r0.x, v7, c2;
dp4 r0.y, v7, c3;
mov oT1, r0;
mov oD0, c5.xxzz;
mov oD1, c5.yyzz;
};
VertexShaderConstant[0] = <cNoiseXForm0_00>;
VertexShaderConstant[1] = <cNoiseXForm0_10>;
VertexShaderConstant[2] = <cNoiseXForm1_00>;
VertexShaderConstant[3] = <cNoiseXForm1_10>;
VertexShaderConstant[4] = float4(0.f, 0.5f, 1.f, 2.f);
VertexShaderConstant[5] = <cScaleBias>;
PixelShader =
asm
{
ps_1_1
// Grab noise texture,
// modulate biased version by vtx color 0,
// add to vtx color 1
tex t0;
tex t1;
add r0.rgb, t0_bias, t1_bias;
+add r0.a, t0, t1;
mad r0.rgb, r0, v0, v1;
};
// Clip/Raster state
SrcBlend = One;
DestBlend = One;
SpecularEnable = FALSE;
AlphaBlendEnable = TRUE;
FogEnable = FALSE;
CullMode = NONE;
ZEnable = FALSE;
}
}