-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.Pixel32.Fragments.pas
365 lines (287 loc) · 9.5 KB
/
GS.Pixel32.Fragments.pas
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
{$I GSCore.Inc}
unit GS.Pixel32.Fragments;
interface
uses sysutils, classes, Windows, Math,
GS.Geometry,
GS.Pixel,
GS.Pixel32.types,
GS.Pixel32;
Type
TPixel32FragmentShaderVerticeColor = Class(TPixel32FragmentShader)
public
procedure process; override;
End;
TPixel32FragmentShaderTextureColor = class(TPixel32FragmentShader)
protected
fTexture: TPixel32;
public
property Texture : TPixel32 read fTexture write fTexture;
procedure process; override;
end;
TPixel32ShaderSquaredMotif = class(TPixel32FragmentShaderFlatColor)
private
protected
ff, fm : TP32;
fl : Integer;
procedure setSquareLen(const Value: integer);
public
constructor create; reintroduce;
procedure process; override;
property colorFill : TP32 read ff write ff;
property colorMotif : TP32 read fm write fm;
property Squarelen : integer read fl write setSquareLen;
end;
TPixel32ShaderWithGlobalAlphaBlending = Class(TPixel32FragmentShaderFlatColor)
private
protected
fAlpha: byte;
public
Constructor Create(const alphavalue : byte = 255); Reintroduce; virtual;
property AlphaChannel : byte read fAlpha write fAlpha;
End;
TPixel32FragmentShaderCheckerBoard = class(TPixel32ShaderWithGlobalAlphaBlending)
protected
public
procedure process; override;
end;
TPixel32ShaderRandomizer = class(TPixel32ShaderWithGlobalAlphaBlending)
protected
public
procedure process; override;
end;
TPixel32ShaderColorTest = Class(TPixel32ShaderWithGlobalAlphaBlending)
private
protected
public
procedure process; override;
End;
TPixel32ShaderPlasma = class(TPixel32ShaderWithGlobalAlphaBlending)
public
procedure process; override;
end;
TPixel32ShaderStaticTexture = class(TPixel32ShaderWithGlobalAlphaBlending)
private
protected
FTileTexture: TPixel32;
public
Constructor Create(TileTexture : TPixel32; const alphavalue : byte = 255); Reintroduce;
property Texture : TPixel32 read FTileTexture Write FTileTexture;
end;
TPixel32ShaderStaticTextureStretchOnSurface = class(TPixel32ShaderStaticTexture)
public
procedure process; override;
end;
TPixel32ShaderStaticTextureTiledOnSurface = class(TPixel32ShaderStaticTexture)
public
procedure process; override;
end;
implementation
{ TPixel32FragmentShaderTextureColor }
procedure TPixel32FragmentShaderTextureColor.process;
var sSurfaceColor : TP32Rec; //Current surface's target color.
fcolorAsRec : TP32Rec;
tb : pTP32;
u,v : single;
ui,vi : integer;
begin
assert(assigned(fTexture));
u := interpolationVerticeA * VerticeA^.u + interpolationVerticeB * VerticeB^.u + interpolationVerticeC * VerticeC^.u;
v := interpolationVerticeA * VerticeA^.v + interpolationVerticeB * VerticeB^.v + interpolationVerticeC * VerticeC^.v;
if zProc <>0 then
begin
// u := u * zProc; //TODO Perspective correction.
// v := v * zProc;
end;
ui := Trunc(u * fTexture.width);
vi := Trunc(v * fTexture.height);
tb := fTexture.getSurfacePtr;
inc(tb,vi*fTexture.width+ui);
if TP32rec(tb^).AlphaChannel<255 then
begin
fcolorAsRec.Color := tb^;
sSurfaceColor.Color := sourceSurfaceBits^;
_processedColor.red:=(fcolorAsRec.AlphaChannel * (fcolorAsRec.Red - sSurfaceColor.Red) shr 8) + (sSurfaceColor.Red);
_processedColor.Blue:=(fcolorAsRec.AlphaChannel * (fcolorAsRec.Blue - sSurfaceColor.Blue) shr 8) + (sSurfaceColor.Blue);
_processedColor.Green:=(fcolorAsRec.AlphaChannel * (fcolorAsRec.Green - sSurfaceColor.Green) shr 8) + (sSurfaceColor.Green);
end
else
_processedColor.Color := tb^;
end;
{ TPixel32FragmentShaderVerticeColor }
procedure TPixel32FragmentShaderVerticeColor.process;
var sSurfaceColor : TP32Rec; //Current surface's target color.
fcolorAsRec : TP32Rec;
r,g,b,a : single;
begin
r := interpolationVerticeA * VerticeA^.rgba.r + interpolationVerticeB * VerticeB^.rgba.r + interpolationVerticeC * VerticeC^.rgba.r;
g := interpolationVerticeA * VerticeA^.rgba.g + interpolationVerticeB * VerticeB^.rgba.g + interpolationVerticeC * VerticeC^.rgba.g;
b := interpolationVerticeA * VerticeA^.rgba.b + interpolationVerticeB * VerticeB^.rgba.b + interpolationVerticeC * VerticeC^.rgba.b;
a := interpolationVerticeA * VerticeA^.rgba.a + interpolationVerticeB * VerticeB^.rgba.a + interpolationVerticeC * VerticeC^.rgba.a;
//Perspective correctoi
if zProc > 0 then
begin
r := r*_z;
g := g*_z;
b := b*_z;
a := a*_z;
end;
fcolorAsRec.Color := TPixel32(sourceSurface).colorP32Rec(trunc(r*255),trunc(g*255),trunc(b*255),trunc(a*255)).Color;
sSurfaceColor.Color := sourceSurfaceBits^;
_processedColor.red:=(fcolorAsRec.AlphaChannel * (fcolorAsRec.Red - sSurfaceColor.Red) shr 8) + (sSurfaceColor.Red);
_processedColor.Blue:=(fcolorAsRec.AlphaChannel * (fcolorAsRec.Blue - sSurfaceColor.Blue) shr 8) + (sSurfaceColor.Blue);
_processedColor.Green:=(fcolorAsRec.AlphaChannel * (fcolorAsRec.Green - sSurfaceColor.Green) shr 8) + (sSurfaceColor.Green);
end;
{ TPixel32FragmentShaderCheckerBoard }
procedure TPixel32FragmentShaderCheckerBoard.process;
const M : Integer = 10;
var s,t,p : single;
st0, st1, st2 : Vec2;
begin
st2.create(0,0);
st1.create(1,0);
st0.create(1,1);
s := interpolationVerticeA * st0.x + interpolationVerticeB * st1.x + interpolationVerticeC * st2.x;
t := interpolationVerticeA * st0.y + interpolationVerticeB * st1.y + interpolationVerticeC * st2.y;
//Perspective Correction.
if zProc > 0 then
begin
s := s*_z;
t := t*_z;
end;
p := _pow(integer(_fmods(s*M,1.0)>0.5),integer(_fmods(t*M,1.0)<0.5));
fColor := TPixel32(sourceSurface).colorP32Rec(round(p*255),round(p*255),round(p*255),100).Color;
inherited;
end;
{ TPixel32ShaderSquaredMotif }
constructor TPixel32ShaderSquaredMotif.create;
begin
inherited create;
fl := 5;
ff := gspWhite;
fm := gspBlue;
end;
procedure TPixel32ShaderSquaredMotif.process;
begin
_processedColor.Color := ff;
if x mod fl = 1 then
_processedColor.Color := fm
else
if y mod fl = 1 then
_processedColor.Color := fm;
fColor := _processedColor.Color; //for inherited call.
inherited;
end;
procedure TPixel32ShaderSquaredMotif.setSquareLen(const Value: integer);
begin
fl := Value;
if fl<3 then
fl := 3;
end;
{ TPixel32ShaderWithGlobalAlphaBlending }
constructor TPixel32ShaderWithGlobalAlphaBlending.Create(
const alphavalue: byte);
begin
inherited create;
fAlpha := alphavalue;
end;
{ TPixel32ShaderRandomizer }
procedure TPixel32ShaderRandomizer.process;
begin
fColor := (sourceSurface as TPixel32).colorP32Rec(10+Random(245),10+Random(245),10+Random(245),fAlpha).Color;
inherited;
end;
{ TPixel32ShaderColorTest }
//Inspired from fantastic delphi port of opencl shader :
//https://github.com/WouterVanNifterick/delphi-shader
procedure TPixel32ShaderColorTest.process;
var
uv :Vec2;
col:TP32Rec;
begin
uv.x := x / sourcesurface.width;
uv.y := y / sourcesurface.height;
col.Red := trunc( (_sqrts(_mix(-0.20,2,_dot(uv.x*2,uv.y*0.5)))+0.2) * 255);
col.green := trunc( (_sqrts(_mix(-0.20,2,_dot(1-uv.y,1-uv.x)))+0.2) * 255);
col.blue := trunc( (_sqrts(_mix(-0.20,2,_dot(1-uv.x,uv.y)))+0.2) * 255);
col.alphachannel := fAlpha;
fColor:= col.Color;
inherited;
end;
{ TPixel32ShaderPlasma }
procedure TPixel32ShaderPlasma.process;
function sinLarge(const x: single): single;
begin
{$IFDEF CPUX64}
Result := System.sin(_Mod(x,2*pi));
{$ELSE}
Result := System.sin(x);
{$ENDIF}
end;
function cosLarge(const x: single): Single;
begin
{$IFDEF CPUX64}
Result := System.cos(_Mod(x,2*pi));
{$ELSE}
Result := System.cos(x);
{$ENDIF}
end;
var
mov0, mov1, mov2, c1, c2, c3: Single;
iGlobalTime : double;
col : TP32Rec;
begin
iGlobalTime := GetTickcount/1000;
mov0 := x + y + System.cos(sinLarge(iGlobalTime) * 2) * 100. + System.sin(x / 100) * 1000;
mov1 := y / sourcesurface.height / 0.2 + iGlobalTime;
mov2 := x / sourcesurface.width / 0.2;
c1 := System.abs(sinLarge(mov1 + iGlobalTime) / 2. + mov2 / 2. - mov1 - mov2 + iGlobalTime);
c2 := System.abs(System.sin(c1 + sinLarge(mov0 / 1000 + iGlobalTime) + System.sin(y / 40 + iGlobalTime) + System.sin((x + y) / 100) * 3));
c3 := System.abs(System.sin(c2 + cosLarge(mov1 + mov2 + c2) + System.cos(mov2) + System.sin(x / 1000)));
col.Red := trunc(c1*255);
col.Green := trunc(c2*255);
col.Blue := trunc(c3*255);
col.AlphaChannel := fAlpha;
fColor := col.Color;
inherited;
end;
{ TPixel32ShaderStaticTexture }
constructor TPixel32ShaderStaticTexture.Create(TileTexture: TPixel32;
const alphavalue: byte);
begin
assert(assigned(TileTexture));
assert((TileTexture.width>1) and (TileTexture.height>1));
inherited Create(alphavalue);
FTileTexture := TileTexture;
end;
{ TPixel32ShaderStaticTextureStretchOnSurface }
procedure TPixel32ShaderStaticTextureStretchOnSurface.process;
var b : pTP32;
tx,ty : integer;
xscale,yscale : single;
begin
b := FTileTexture.getSurfacePtr;
//Texture is like stretching in surface size.
xscale := FTileTexture.width/sourcesurface.width;
yscale := FTileTexture.height/sourcesurface.height;
tx := trunc(x*xscale);
ty := trunc(y*yscale);
inc(b,ty*FTileTexture.width+tx);
fColor := b^;
TP32Rec(fColor).AlphaChannel := fAlpha;
inherited;
end;
{ TPixel32ShaderStaticTextureTiledOnSurface }
procedure TPixel32ShaderStaticTextureTiledOnSurface.process;
var b : pTP32;
tx,ty : integer;
begin
b := FTileTexture.getSurfacePtr;
//Texture is untouched in size, and display tiled on surface size.
tx := x mod FTileTexture.width;
ty := y mod FTileTexture.height;
inc(b,ty*FTileTexture.width+tx);
fColor := b^;
TP32Rec(fColor).AlphaChannel := fAlpha;
inherited;
end;
end.