-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUI2DUtils.pas
471 lines (412 loc) · 15.2 KB
/
UI2DUtils.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
unit UI2DUtils;
interface
uses
System.Types, System.SysUtils, System.Generics.Collections, OpenGL, OpenGLUtils,
RecordUtils;
type
TTexturesParameters = record
Left, Top, Right, Bottom: Single;
AttributeIndex: Integer;
constructor Create(Index: Integer; ALeft: Single = 0; ATop: Single = 0; ARight: Single = 1; ABottom: Single = 1);
end;
TBilboard = record
ShaderBlock: TShaderBlock;
Textures: TTexturesInfo;
end;
PBilboard = ^TBilboard;
TMatrixUniformExtension = record
Transpose: Boolean;
Count: GLsizei;
end;
TUniformBlockInfo = record
UniformType: TUniformType;
Size: Integer;
Matrix: TMatrixUniformExtension;
Name: AnsiString;
constructor Create(const AName: AnsiString; AUniformType: TUniformType;
ASize: Integer);
end;
TAttributeInfo = record
Index: GLuint;
Name: AnsiString;
constructor Create(const AName: AnsiString; AIndex: Integer);
end;
TUniformStorage = record
Info: TUniformInfo;
UniformType: TUniformType;
end;
TProgramBlockInformation = record
UniformInfo: TArray<TUniformBlockInfo>;
Attribs: TArray<TAttributeInfo>;
constructor Create(const AUniformInfo: array of TUniformBlockInfo;
const AAttribs: array of TAttributeInfo);
end;
TUniformInformation = procedure (out Info: TProgramBlockInformation);
TShaderPattern = record
Pattern: AnsiString;
BlockInfo: TProgramBlockInformation;
constructor Create(const APattern: AnsiString; const ABlockInfo: TProgramBlockInformation);
end;
TShaderStorage = record
Shader: TProgramInfo;
Uniform: TArray<TUniformStorage>;
MatrixExtension: TArray<TMatrixUniformExtension>;
function GenerateShaderBlock: TShaderBlock;
end;
TProgramGenerator = class
private
FKnownPattern: TPrefixStringTree<TShaderPattern>;//TDictionary<Char, TShaderPattern>;
FGenerated: TDictionary<string, TShaderStorage>;
procedure FreeShader(Sender: TObject; const Item: TShaderStorage; Action: TCollectionNotification); overload;
procedure FreeShader(const Item: TShaderStorage); overload;
protected
function GenerateProgramCode(const Shaders: array of string;
const Types: array of {$IFDEF OGL_USE_ENUMS}TShaderType{$ELSE}GLenum{$ENDIF}): string;
public
procedure SetPattern(const C: string; const Shader: AnsiString; const BlockInfo: TProgramBlockInformation); overload;
procedure SetPattern(const C: string; const Shader: AnsiString); overload;
function Generate(Vertex, Fragment: string): TShaderStorage; overload;
function Generate(const Shaders: array of string;
const Types: array of {$IFDEF OGL_USE_ENUMS}TShaderType{$ELSE}GLenum{$ENDIF}): TShaderStorage; overload;
procedure FreeContext;
constructor Create;
destructor Destroy; override;
end;
const
AttributeVertex = 0;
AttributeTextureCoord = 1;
procedure DrawBilboard(const R: TRect; var ObjectInfo: TBilboard); overload;
procedure DrawBilboard(const R: TRect; var ObjectInfo: TBilboard; const TextureParams: array of TTexturesParameters); overload;
procedure DrawBilboard(const R: TRect; const TextureParams: array of TTexturesParameters); overload;
function ProgramGenerator: TProgramGenerator;
implementation
var
GlobalProgramGenerator: TProgramGenerator = nil;
function ProgramGenerator: TProgramGenerator;
begin
if GlobalProgramGenerator = nil then
GlobalProgramGenerator:= TProgramGenerator.Create;
Result:= GlobalProgramGenerator;
end;
procedure DrawBilboard(const R: TRect; const TextureParams: array of TTexturesParameters);
var i: Integer;
begin
glBegin({$IFDEF OGL_USE_ENUMS}TPrimitiveType.{$ENDIF}GL_TRIANGLE_STRIP);
for i := 0 to High(TextureParams) do with TextureParams[i] do
glVertexAttrib2f(AttributeIndex, Left, Top);
glVertexAttrib2f(AttributeVertex, R.Left, R.Top);
for i := 0 to High(TextureParams) do with TextureParams[i] do
glVertexAttrib2f(AttributeIndex, Left, Bottom);
glVertexAttrib2f(AttributeVertex, R.Left, R.Bottom);
for i := 0 to High(TextureParams) do with TextureParams[i] do
glVertexAttrib2f(AttributeIndex, Right, Top);
glVertexAttrib2f(AttributeVertex, R.Right, R.Top);
for i := 0 to High(TextureParams) do with TextureParams[i] do
glVertexAttrib2f(AttributeIndex, Right, Bottom);
glVertexAttrib2f(AttributeVertex, R.Right, R.Bottom);
glEnd;
end;
procedure DrawBilboard(const R: TRect; var ObjectInfo: TBilboard; const TextureParams: array of TTexturesParameters);
begin
GetViewPortSize(ObjectInfo.ShaderBlock._FloatUniforms[0].Value);
ObjectInfo.ShaderBlock.PrepareToDraw;
ObjectInfo.Textures.Activate;
DrawBilboard(R, TextureParams);
end;
procedure DrawBilboard(const R: TRect; var ObjectInfo: TBilboard);
var tp: TTexturesParameters;
begin
if ObjectInfo.Textures._Textures[0].Texture.Target <> {$IFDEF OGL_USE_ENUMS}TTextureTarget.{$ENDIF}GL_TEXTURE_2D then
tp:= TTexturesParameters.Create(AttributeTextureCoord, 0, 0, R.Width, R.Height)
else
tp:= TTexturesParameters.Create(AttributeTextureCoord);
DrawBilboard(R, ObjectInfo, [tp]);
end;
{ TTexturesParameters }
constructor TTexturesParameters.Create(Index: Integer; ALeft, ATop, ARight,
ABottom: Single);
begin
AttributeIndex:= Index;
Left:= ALeft;
Top:= ATop;
Right:= ARight;
Bottom:= ABottom;
end;
{ TShaderStorage }
function TShaderStorage.GenerateShaderBlock: TShaderBlock;
var FloatUniforms: TArray<TUniformValue<GLfloat>>;
IntegerUniforms: TArray<TUniformValue<GLint>>;
MatrixUniforms: TArray<TUniformMatrixValue>;
fCount, iCount, mCount: Integer;
i: Integer;
begin
fCount:= 0;
iCount:= 0;
mCount:= 0;
for i := 0 to High(Uniform) do
case Uniform[i].UniformType of
utInteger: Inc(iCount);
utFloat: Inc(fCount);
utMatrix: Inc(mCount);
end;
SetLength(FloatUniforms, fCount);
SetLength(IntegerUniforms, iCount);
SetLength(MatrixUniforms, mCount);
fCount:= 0;
iCount:= 0;
mCount:= 0;
for i := 0 to High(Uniform) do
case Uniform[i].UniformType of
utInteger: begin
IntegerUniforms[iCount].Info:= Uniform[i].Info;
SetLength(IntegerUniforms[iCount].Value, Uniform[i].Info.Size);
Inc(iCount);
end;
utFloat: begin
FloatUniforms[fCount].Info:= Uniform[i].Info;
SetLength(FloatUniforms[fCount].Value, Uniform[i].Info.Size);
Inc(fCount);
end;
utMatrix: begin
MatrixUniforms[mCount].MatrixInfo.Info:= Uniform[i].Info;
MatrixUniforms[mCount].MatrixInfo.Transpose:= MatrixExtension[mCount].Transpose;
MatrixUniforms[mCount].MatrixInfo.Count:= MatrixExtension[mCount].Count;
SetLength(MatrixUniforms[mCount].Value, Uniform[i].Info.Size * MatrixUniforms[mCount].MatrixInfo.Count);
Inc(mCount);
end;
end;
Result.Create(Shader._Program, IntegerUniforms, FloatUniforms, MatrixUniforms);
end;
{ TProgramGenerator }
constructor TProgramGenerator.Create;
begin
//FKnownPattern:= TDictionary<Char, TShaderPattern>.Create;
FGenerated:= TDictionary<string, TShaderStorage>.Create;
SetPattern('#120', '#version 120');
SetPattern('{', 'void main(){');
SetPattern('}', '}');
SetPattern('!', 'precision highp float;');
SetPattern('Upal', 'uniform sampler1D Palette;',
TProgramBlockInformation.Create([TUniformBlockInfo.Create('Palette', utInteger, 1)], []));
SetPattern('It2', 'varying vec2 TexCoord;');
SetPattern('Um2', 'uniform sampler2D Map;',
TProgramBlockInformation.Create([TUniformBlockInfo.Create('Map', utInteger, 1)], []));
SetPattern('fPal2', ' float TexColor = texture2D(Map, TexCoord).x;');
SetPattern('PalDiscard', ' if (TexColor < 1.0 / 255.0) discard;');
SetPattern('Fpal', ' gl_FragColor = texture1D(Palette, TexColor);');
SetPattern('Ftex2', ' gl_FragColor = texture2D(Map, TexCoord);');
SetPattern('IOt2', 'attribute vec2 aTexCoord;'#13#10'varying vec2 TexCoord;',
TProgramBlockInformation.Create([], [TAttributeInfo.Create('aTexCoord', 1)]));
SetPattern('Screen', 'uniform vec4 Screen;',
TProgramBlockInformation.Create([TUniformBlockInfo.Create('Screen', utFloat, 4)], []));
SetPattern('Iv2', 'attribute vec2 aVertex;',
TProgramBlockInformation.Create([], [TAttributeInfo.Create('aVertex', 0)]));
SetPattern('Vex', ' gl_Position = vec4(aVertex * 2.0 / Screen.xy - Screen.zw, 0.0, 1.0);');
SetPattern('Tex', ' TexCoord = aTexCoord;');
end;
destructor TProgramGenerator.Destroy;
begin
//FKnownPattern.Free;
FGenerated.Free;
inherited;
end;
procedure TProgramGenerator.FreeContext;
begin
FGenerated.OnValueNotify:= FreeShader;
FGenerated.Clear;
FGenerated.OnValueNotify:= nil;
end;
procedure TProgramGenerator.FreeShader(const Item: TShaderStorage);
var
i: Integer;
begin
glDeleteProgram(Item.Shader._Program);
for i := 0 to High(Item.Shader._Shaders) do
glDeleteShader(Item.Shader._Shaders[i]);
end;
procedure TProgramGenerator.FreeShader(Sender: TObject;
const Item: TShaderStorage; Action: TCollectionNotification);
begin
if Action = cnRemoved then
FreeShader(Item);
end;
function TProgramGenerator.Generate(const Shaders: array of string;
const Types: array of {$IFDEF OGL_USE_ENUMS}TShaderType{$ELSE}GLenum{$ENDIF}): TShaderStorage;
var Shader: TListRecord<PAnsiChar>;
i, uCount, mCount: Integer;
Attribs: TArray<TAttributeInfo>;
Uniforms: TArray<TUniformBlockInfo>;
CompiledShaders: array of GLuint;
Code, Err, s: string;
p: GLuint;
link: Integer;
j, k, l: Integer;
sorted: array of GLenum;
tmp: GLenum;
resultUniforms: TLocalListRecord<TUniformBlockInfo>;
begin
SetLength(sorted, Length(Types));
for i := 0 to High(Types) do
sorted[i]:= GLenum(Types[i]);
for i := 0 to High(Types) - 1 do
for j := i + 1 to High(Types) do
if sorted[i] < sorted[j] then begin
tmp:= sorted[i];
sorted[i]:= sorted[j];
sorted[j]:= tmp;
end;
Code:= '';
for i := 0 to High(Types) do
for j := 0 to High(Types) do
if sorted[i] = Types[j] then begin
Code:= Code + Shaders[j];
Break;
end;
if not FGenerated.TryGetValue(Code, Result) then begin
mCount:= 0;
SetLength(CompiledShaders, Length(Shaders));
for l := 0 to High(Shaders) do
for k := 0 to High(Types) do
if sorted[l] = Types[k] then begin
Shader.Count:= 0;
FKnownPattern.Reset(Shaders[k]);
while not FKnownPattern.IsEnd do
with FKnownPattern.NextToken do begin
Shader.Add(Pointer(Pattern));
Uniforms:= Concat(Uniforms, BlockInfo.UniformInfo);
for j := 0 to High(BlockInfo.UniformInfo) do
if BlockInfo.UniformInfo[j].UniformType = utMatrix then
Inc(mCount);
Attribs:= Concat(Attribs, BlockInfo.Attribs);
end;
Shader.TrimExcess;
CompiledShaders[k]:= CreateShader(Types[k], Shader.List);
end;
try
p:= glCreateProgram;
if p = 0 then
RaiseOpenGLError;
Result.Shader.Create(p, CompiledShaders);
Result.Shader.AttachAll;
for i := 0 to High(Attribs) do
glBindAttribLocation(Result.Shader._Program, Attribs[i].Index, Pointer(Attribs[i].Name));
Err:= '';
for i := 0 to High(Types) do begin
s:= GetShaderLog(Result.Shader._Shaders[i]);
LogOutput(TProgramGenerator, Format('InitializeProgram %s [%x]', [Code, sorted[i]]), s);
Err:= Err + #13#10 + s;
end;
glLinkProgram(Result.Shader._Program);
glGetProgramiv(Result.Shader._Program, {$IFDEF OGL_USE_ENUMS}TProgramPropertyARB.{$ENDIF}GL_LINK_STATUS, @link);
if link = 0 then begin
s:= GetProgramLog(Result.Shader._Program);
LogOutput(TProgramGenerator, Format('InitializeProgram %s link', [Code]), s);
RaiseOpenGLError(0, s + #13#10 + Err);
end;
resultUniforms.Initialize(Uniforms[0], Length(Uniforms), Length(Uniforms));
for i := High(Uniforms) downto 0 do begin
for j := 0 to i - 1 do
if Uniforms[j].Name = Uniforms[i].Name then begin
resultUniforms.Delete(i);
Break;
end;
end;
SetLength(Uniforms, resultUniforms.Count);
SetLength(Result.Uniform, Length(Uniforms));
SetLength(Result.MatrixExtension, mCount);
mCount:= 0;
for i := 0 to High(Result.Uniform) do begin
Result.Uniform[i].Info.Index:= glGetUniformLocation(Result.Shader._Program, Pointer(Uniforms[i].Name));
Result.Uniform[i].Info.Size:= Uniforms[i].Size;
Result.Uniform[i].UniformType:= Uniforms[i].UniformType;
if Uniforms[i].UniformType = utMatrix then begin
Result.MatrixExtension[mCount]:= Uniforms[i].Matrix;
Inc(mCount);
end;
end;
LogOutput(TProgramGenerator, Format('InitializeProgram %s Program', [Code]), GetProgramLog(Result.Shader._Program));
except
FreeShader(Result);
raise;
end;
FGenerated.Add(Code, Result);
end;
end;
function TProgramGenerator.Generate(Vertex, Fragment: string): TShaderStorage;
begin
Result:= Generate([Vertex, Fragment], [{$IFDEF OGL_USE_ENUMS}TShaderType.{$ENDIF}GL_VERTEX_SHADER,
{$IFDEF OGL_USE_ENUMS}TShaderType.{$ENDIF}GL_FRAGMENT_SHADER]);
end;
function TProgramGenerator.GenerateProgramCode(const Shaders: array of string;
const Types: array of {$IFDEF OGL_USE_ENUMS}TShaderType{$ELSE}GLenum{$ENDIF}): string;
var i: Integer;
sorted: array of GLenum;
tmp: GLenum;
j: Integer;
begin
SetLength(sorted, Length(Types));
for i := 0 to High(Types) do
sorted[i]:= GLenum(Types[i]);
for i := 0 to High(Types) - 1 do
for j := i + 1 to High(Types) do
if sorted[i] < sorted[j] then begin
tmp:= sorted[i];
sorted[i]:= sorted[j];
sorted[j]:= tmp;
end;
Result:= '';
for i := 0 to High(Types) do
for j := 0 to High(Types) do
if sorted[i] = Types[j] then begin
Result:= Result + Shaders[j];
Break;
end;
end;
procedure TProgramGenerator.SetPattern(const C: string; const Shader: AnsiString);
var t: TProgramBlockInformation;
begin
FKnownPattern.AddOrSetValue(C, TShaderPattern.Create(Shader + #13#10, t));
end;
procedure TProgramGenerator.SetPattern(const C: string; const Shader: AnsiString;
const BlockInfo: TProgramBlockInformation);
begin
FKnownPattern.AddOrSetValue(C, TShaderPattern.Create(Shader + #13#10, BlockInfo));
end;
{ TShaderPattern }
constructor TShaderPattern.Create(const APattern: AnsiString;
const ABlockInfo: TProgramBlockInformation);
begin
Pattern:= APattern;
BlockInfo:= ABlockInfo;
end;
{ TProgramBlockInformation }
constructor TProgramBlockInformation.Create(
const AUniformInfo: array of TUniformBlockInfo;
const AAttribs: array of TAttributeInfo);
var i: Integer;
begin
SetLength(UniformInfo, Length(AUniformInfo));
for i := 0 to High(AUniformInfo) do
UniformInfo[i]:= AUniformInfo[i];
SetLength(Attribs, Length(AAttribs));
for i := 0 to High(AAttribs) do
Attribs[i]:= AAttribs[i];
end;
{ TUniformBlockInfo }
constructor TUniformBlockInfo.Create(const AName: AnsiString;
AUniformType: TUniformType; ASize: Integer);
begin
Name:= AName;
UniformType:= AUniformType;
Size:= ASize
end;
{ TAttributeInfo }
constructor TAttributeInfo.Create(const AName: AnsiString; AIndex: Integer);
begin
Name:= AName;
Index:= AIndex;
end;
initialization
finalization
FreeAndNil(GlobalProgramGenerator);
end.