1
// This unit is part of the GLScene Engine https://github.com/glscene
4
A collection of pure abstract classes - descendants of TGLShader, which are
5
used for purpose of not having to write the same stuff all over and over
6
again in your own shader classes.
7
It also contains a procedures and function that can be used in all shaders.
10
23/08/10 - Yar - Added OpenGLTokens to uses, replaced OpenGL1x functions to OpenGLAdapter
11
15/16/10 - Yar - Rewrited static procedures (InitTexture, etc.)
12
04/06/10 - Yar - Added unsigned integer uniforms
13
22/04/10 - Yar - Fixes after GLState revision
14
22/01/10 - Yar - Added to TGLCustomShaderParameter property AsTexture
15
25/10/09 - DaStr - Updated TGLGeometryProgram (thanks YarUnderoaker)
16
24/08/09 - DaStr - Separated TGLShaderProgram into TGLVertexProgram,
17
TGLFragmentProgram and TGLGeometryProgram
18
Added TGLCustomShaderParameter.AsUniformBuffer
19
(thanks YarUnderoaker)
20
28/07/09 - DaStr - Added GeometryShader support (thanks YarUnderoaker)
21
Fixed TGLCustomShader.[...]Program serialization
22
24/07/09 - DaStr - Added TGLCustomShader.DebugMode
23
Fixed spelling mistake in TGLShaderUnAplyEvent
24
Added TGLShaderFogSupport, IsFogEnabled()
25
03/04/07 - DaStr - Added TGLCustomShaderParameter.AsFloat and AsInteger
26
25/03/07 - DaStr - Added TGLCustomShaderParameter.SetToTextureOf
27
20/03/07 - DaStr - Added DrawTexturedScreenQuad[4/5/6]
28
"TextureType" parameter renamed to "TextureTarget"
29
Finished working on TGLCustomShaderParameter
30
04/03/07 - DaStr - Added IGLPostShader
31
03/03/07 - DaStr - Added TGLCustomShaderParameter (beta state)
32
22/02/07 - DaStr - Initial version (contributed to GLScene)
35
What different shader prefixes might mean:
37
ML - Multi Light - Shader supports up to 8 lights.
38
Attributes such as [Ambient/Diffuse/Specular]
39
Colors are taken from the current OpenGL
40
state (that means from TGLLightSource too)
41
In all other cases shader supports only
42
one light, position of which is taken
43
from the first registered OpenGL light.
47
What different shader suffixes might mean:
49
MP - Manual Parameters - [Ambient/Diffuse/Specular] Colors have
50
to be set manualy as shader's properties.
51
In all other cases they are taken from
52
the current OpenGL state
53
(that means from TGLLightSource too)
55
MT - Manual Main Texture - Main Texture is taken not from the
56
current texture, that is applied to the
57
rendered object, but has to be set manualy
59
In all other cases it is taken from
60
the current texture, that is applied to
63
AM - All Manual - MP + MMT
65
AST - Auto Secondary Textures - All other textures are taken from the
66
textures, that are applied to the
67
rendered object after the main one
68
(like the one in TGLLIbMaterial.Texture2Name,
69
or any other textures that are applied to the
70
object manualy using TGLMaterial.Apply
72
In all other cases they are taken from
73
the shader's properties
76
Previous version history:
77
v1.0 11 March '2006 Creation, separated from GLSLShader
78
v1.1 06 August '2006 TGLCustomShader.HandleShaderNotSupportedException added
79
TGLCustomShader.ShaderNotSupportedExceptionMessage added
80
v1.2 14 August '2006 IGLShaderSupported separated
81
TGLShaderTextureSource added
82
v1.2.2 19 August '2006 IMultiShaderCompatible added
83
v1.2.4 24 August '2006 TGLCustomShader.ParameterTexture[1-3]D added
84
v1.2.6 04 September '2006 Minor fixes
85
v1.3 04 November '2006 TGLShaderUnUplyEvent added
86
OnApply, OnUnApply, OnInitialize moved to
88
(Un)ApplyBlendingMode added
89
(Get/Set)ParameterTexture[1/2/3]DHandle added
90
InitTexture(), DrawTexturedScreenQuad() added
91
(Get/Set)ParameterCustomTextureHandle support added
92
v1.3.2 16 December '2006 Added shader Naming convention in the comments
93
STR_SHADER_NEEDS_AT_LEAST_ONE_LIGHT_SOURCE
94
moved here from StrangeGLSLBumpShader
95
vStrangeShaderClassList and all shader
96
registration utility functions added
97
v1.3.4 18 February '2007 StrangeTextureUtilities dependancy removed
98
Updated to the latest CVS version of GLScene
112
GLVectorGeometry, GLVectorTypes, GLTexture, GLCadencer, OpenGLTokens, GLScene,
113
GLStrings, GLCrossPlatform, GLContext, GLRenderContextInfo, GLMaterial,
114
GLVectorLists, GLTextureFormat, GLSLParameter;
117
glsShaderMaxLightSources = 8;
120
TGLShaderFogSupport = (sfsEnabled, sfsDisabled, sfsAuto);
121
TGLTransformFeedBackMode = (tfbmInterleaved, tfbmSeparate);
123
EGLCustomShaderException = class(EGLShaderException);
125
TGLCustomShader = class;
126
TGLVertexProgram = class;
127
TGLFragmentProgram = class;
128
TGLGeometryProgram = class;
130
TGLShaderEvent = procedure(Shader: TGLCustomShader) of object;
131
TGLShaderUnAplyEvent = procedure(Shader: TGLCustomShader; var ThereAreMorePasses: Boolean) of object;
133
TGLLightSourceEnum = 1..glsShaderMaxLightSources;
134
TGLLightSourceSet = set of TGLLightSourceEnum;
136
{ This interface describes user shaders, in order to be able to access them
137
via a unified interface. If user shader does not support some option, don't
138
raise an axception, just ignore it.
140
IGLShaderDescription = interface
141
['{04089C64-60C2-43F5-AC9C-38ED46264812}']
142
procedure SetShaderTextures(const Textures: array of TGLTexture);
143
procedure GetShaderTextures(var Textures: array of TGLTexture);
145
procedure SetShaderColorParams(const AAmbientColor, ADiffuseColor, ASpecularcolor: TVector4f);
146
procedure GetShaderColorParams(var AAmbientColor, ADiffuseColor, ASpecularcolor: TVector4f);
148
procedure SetShaderMiscParameters(const ACadencer: TGLCadencer; const AMatLib: TGLMaterialLibrary; const ALightSources: TGLLightSourceSet);
149
procedure GetShaderMiscParameters(var ACadencer: TGLCadencer; var AMatLib: TGLMaterialLibrary; var ALightSources: TGLLightSourceSet);
151
function GetShaderAlpha: Single;
152
procedure SetShaderAlpha(const Value: Single);
154
function GetShaderDescription: string;
157
{ Used in the TGLPostShaderHolder component. }
158
IGLPostShader = interface
159
['{68A62362-AF0A-4CE8-A9E1-714FE02AFA4A}']
160
{ Called on every pass. }
161
procedure DoUseTempTexture(const TempTexture: TGLTextureHandle;
162
TextureTarget: TGLTextureTarget);
163
{ Called to determine if it is compatible. }
164
function GetTextureTarget: TGLTextureTarget;
167
{ A pure abstract class, must be overriden. }
168
TGLCustomShader = class(TGLShader)
170
FFragmentProgram: TGLFragmentProgram;
171
FVertexProgram: TGLVertexProgram;
172
FGeometryProgram: TGLGeometryProgram;
175
procedure SetFragmentProgram(const Value: TGLFragmentProgram);
176
procedure SetGeometryProgram(const Value: TGLGeometryProgram);
177
procedure SetVertexProgram(const Value: TGLVertexProgram);
178
function StoreFragmentProgram: Boolean;
179
function StoreGeometryProgram: Boolean;
180
function StoreVertexProgram: Boolean;
183
procedure SetDebugMode(const Value: Boolean); virtual;
185
property FragmentProgram: TGLFragmentProgram read FFragmentProgram write SetFragmentProgram stored StoreFragmentProgram;
186
property VertexProgram: TGLVertexProgram read FVertexProgram write SetVertexProgram stored StoreVertexProgram;
187
property GeometryProgram: TGLGeometryProgram read FGeometryProgram write SetGeometryProgram stored StoreGeometryProgram;
189
{ Treats warnings as errors and displays this error,
190
instead of a general shader-not-supported message. }
191
property DebugMode: Boolean read FDebugMode write SetDebugMode default False;
192
property TagObject: TObject read FTagObject write FTagObject default nil;
194
constructor Create(AOwner: TComponent); override;
195
destructor Destroy; override;
196
procedure Assign(Source: TPersistent); override;
198
procedure LoadShaderPrograms(const VPFilename, FPFilename: string; GPFilename: string = '');
201
{ A custom shader program. }
202
TGLShaderProgram = class(TPersistent)
204
FParent: TGLCustomShader;
207
procedure SetCode(const Value: TStrings);
208
procedure SetEnabled(const Value: Boolean);
209
procedure OnChangeCode(Sender: TObject);
211
function GetOwner: TPersistent; override;
213
procedure LoadFromFile(const AFileName: string);
214
procedure Apply; virtual;
215
constructor Create(const AParent: TGLCustomShader); virtual;
216
destructor Destroy; override;
217
procedure Assign(Source: TPersistent); override;
219
property Code: TStrings read FCode write SetCode;
220
property Enabled: Boolean read FEnabled write SetEnabled default False;
223
TGLVertexProgram = class(TGLShaderProgram)
229
TGLFragmentProgram = class(TGLShaderProgram)
235
TGLGeometryProgram = class(TGLShaderProgram)
237
FInputPrimitiveType: TGLgsInTypes;
238
FOutputPrimitiveType: TGLgsOutTypes;
239
FVerticesOut: TGLint;
240
procedure SetInputPrimitiveType(const Value: TGLgsInTypes);
241
procedure SetOutputPrimitiveType(const Value: TGLgsOutTypes);
242
procedure SetVerticesOut(const Value: TGLint);
244
constructor Create(const AParent: TGLCustomShader); override;
249
property InputPrimitiveType: TGLgsInTypes read FInputPrimitiveType write SetInputPrimitiveType default gsInPoints;
250
property OutputPrimitiveType: TGLgsOutTypes read FOutputPrimitiveType write SetOutputPrimitiveType default gsOutPoints;
251
property VerticesOut: TGLint read FVerticesOut write SetVerticesOut default 0;
254
{ Wrapper around a parameter of the main program. }
255
TGLCustomShaderParameter = class(TObject)
260
function GetAsVector1f: Single; virtual; abstract;
261
function GetAsVector2f: TVector2f; virtual; abstract;
262
function GetAsVector3f: TVector3f; virtual; abstract;
263
function GetAsVector4f: TVector; virtual; abstract;
265
function GetAsVector1i: Integer; virtual; abstract;
266
function GetAsVector2i: TVector2i; virtual; abstract;
267
function GetAsVector3i: TVector3i; virtual; abstract;
268
function GetAsVector4i: TVector4i; virtual; abstract;
270
function GetAsVector1ui: GLuint; virtual; abstract;
271
function GetAsVector2ui: TVector2ui; virtual; abstract;
272
function GetAsVector3ui: TVector3ui; virtual; abstract;
273
function GetAsVector4ui: TVector4ui; virtual; abstract;
275
procedure SetAsVector1f(const Value: Single); virtual; abstract;
276
procedure SetAsVector2f(const Value: TVector2f); virtual; abstract;
277
procedure SetAsVector3f(const Value: TVector3f); virtual; abstract;
278
procedure SetAsVector4f(const Value: TVector4f); virtual; abstract;
280
procedure SetAsVector1i(const Value: Integer); virtual; abstract;
281
procedure SetAsVector2i(const Value: TVector2i); virtual; abstract;
282
procedure SetAsVector3i(const Value: TVector3i); virtual; abstract;
283
procedure SetAsVector4i(const Value: TVector4i); virtual; abstract;
285
procedure SetAsVector1ui(const Value: GLuint); virtual; abstract;
286
procedure SetAsVector2ui(const Value: TVector2ui); virtual; abstract;
287
procedure SetAsVector3ui(const Value: TVector3ui); virtual; abstract;
288
procedure SetAsVector4ui(const Value: TVector4ui); virtual; abstract;
290
function GetAsMatrix2f: TMatrix2f; virtual; abstract;
291
function GetAsMatrix3f: TMatrix3f; virtual; abstract;
292
function GetAsMatrix4f: TMatrix4f; virtual; abstract;
293
procedure SetAsMatrix2f(const Value: TMatrix2f); virtual; abstract;
294
procedure SetAsMatrix3f(const Value: TMatrix3f); virtual; abstract;
295
procedure SetAsMatrix4f(const Value: TMatrix4f); virtual; abstract;
297
procedure SetAsTexture(const TextureIndex: Integer;
298
const Value: TGLTexture);
299
procedure SetAsTexture1D(const TextureIndex: Integer;
300
const Value: TGLTexture);
301
procedure SetAsTexture2D(const TextureIndex: Integer;
302
const Value: TGLTexture);
303
procedure SetAsTexture3D(const TextureIndex: Integer;
304
const Value: TGLTexture);
305
procedure SetAsTextureCube(const TextureIndex: Integer;
306
const Value: TGLTexture);
307
procedure SetAsTextureRect(const TextureIndex: Integer;
308
const Value: TGLTexture);
310
function GetAsCustomTexture(const TextureIndex: Integer;
311
TextureTarget: TGLTextureTarget): Cardinal; virtual; abstract;
312
procedure SetAsCustomTexture(const TextureIndex: Integer;
313
TextureTarget: TGLTextureTarget; const Value: Cardinal); virtual; abstract;
315
function GetAsUniformBuffer: GLenum; virtual; abstract;
316
procedure SetAsUniformBuffer(UBO: GLenum); virtual; abstract;
320
{ This overloaded SetAsVector accepts open array as input. e.g.
321
SetAsVectorF([0.1, 0.2]). Array length must between 1-4. }
322
procedure SetAsVectorF(const Values: array of Single); overload;
323
procedure SetAsVectorI(const Values: array of Integer); overload;
325
{ SetToTextureOf determines texture type on-the-fly.}
326
procedure SetToTextureOf(const LibMaterial: TGLLibMaterial; const TextureIndex: Integer); overload;
327
procedure SetToTextureOf(const Texture: TGLTexture; const TextureIndex: Integer); overload;
329
// GLScene-friendly properties.
330
property AsVector: TVector read GetAsVector4f write SetAsVector4f;
331
property AsAffineVector: TAffineVector read GetAsVector3f write SetAsVector3f;
334
property AsFloat: Single read GetAsVector1f write SetAsVector1f;
335
property AsInteger: Integer read GetAsVector1i write SetAsVector1i;
337
// Float vector types.
338
property AsVector1f: Single read GetAsVector1f write SetAsVector1f;
339
property AsVector2f: TVector2f read GetAsVector2f write SetAsVector2f;
340
property AsVector3f: TVector3f read GetAsVector3f write SetAsVector3f;
341
property AsVector4f: TVector4f read GetAsVector4f write SetAsVector4f;
343
// Integer vector types.
344
property AsVector1i: Integer read GetAsVector1i write SetAsVector1i;
345
property AsVector2i: TVector2i read GetAsVector2i write SetAsVector2i;
346
property AsVector3i: TVector3i read GetAsVector3i write SetAsVector3i;
347
property AsVector4i: TVector4i read GetAsVector4i write SetAsVector4i;
349
// Unsigned integer vector types.
350
property AsVector1ui: GLuint read GetAsVector1ui write SetAsVector1ui;
351
property AsVector2ui: TVector2ui read GetAsVector2ui write SetAsVector2ui;
352
property AsVector3ui: TVector3ui read GetAsVector3ui write SetAsVector3ui;
353
property AsVector4ui: TVector4ui read GetAsVector4ui write SetAsVector4ui;
356
property AsMatrix2f: TMatrix2f read GetAsMatrix2f write SetAsMatrix2f;
357
property AsMatrix3f: TMatrix3f read GetAsMatrix3f write SetAsMatrix3f;
358
property AsMatrix4f: TMatrix4f read GetAsMatrix4f write SetAsMatrix4f;
361
property AsTexture [const TextureIndex: Integer]: TGLTexture write SetAsTexture;
362
property AsTexture1D [const TextureIndex: Integer]: TGLTexture write SetAsTexture1D;
363
property AsTexture2D [const TextureIndex: Integer]: TGLTexture write SetAsTexture2D;
364
property AsTexture3D [const TextureIndex: Integer]: TGLTexture write SetAsTexture3D;
365
property AsTextureRect[const TextureIndex: Integer]: TGLTexture write SetAsTextureRect;
366
property AsTextureCube[const TextureIndex: Integer]: TGLTexture write SetAsTextureCube;
368
property AsCustomTexture[const TextureIndex: Integer; TextureTarget: TGLTextureTarget]: Cardinal read GetAsCustomTexture write SetAsCustomTexture;
370
property AsUniformBuffer: GLenum read GetAsUniformBuffer write SetAsUniformBuffer;
374
{ Adds two more blending modes to standard ones.
375
Not sure how to name them or if they should be included in TBlending mode,
376
so I created a new type here. }
377
TGLBlendingModeEx = (bmxOpaque, bmxTransparency, bmxAdditive,
378
bmxAlphaTest50, bmxAlphaTest100, bmxModulate,
379
bmxDestColorOne, bmxDestAlphaOne);
381
// Exported procedures.
382
procedure ApplyBlendingModeEx(const BlendingMode: TGLBlendingModeEx);
383
procedure UnApplyBlendingModeEx;
384
procedure InitTexture(
385
const TextureHandle: Cardinal;
386
const TextureSize: TGLSize;
387
const TextureTarget: TGLTextureTarget = ttTexture2D);
388
// Probably need to give them proper names, instead of numbers...
389
procedure DrawTexturedScreenQuad;
390
procedure DrawTexturedScreenQuad2(const ViewPortSize: TGLSize);
391
procedure DrawTexturedScreenQuad3;
392
procedure DrawTexturedScreenQuad4(const ViewPortSize: TGLSize);
393
procedure DrawTexturedScreenQuad5(const ViewPortSize: TGLSize);
394
procedure DrawTexturedScreenQuad6(const ViewPortSize: TGLSize);
396
procedure CopyScreentoTexture(const ViewPortSize: TGLSize; const TextureTarget: Word = GL_TEXTURE_2D);
397
procedure CopyScreentoTexture2(const ViewPortSize: TGLSize; const TextureTarget: Word = GL_TEXTURE_2D);
399
function IsFogEnabled(const AFogSupportMode: TGLShaderFogSupport; var rci: TGLRenderContextInfo): Boolean;
400
procedure GetActiveLightsList(const ALightIDs: TIntegerList);
407
procedure GetActiveLightsList(const ALightIDs: TIntegerList);
412
with CurrentGLContext.GLStates do
414
for I := 0 to MaxLights - 1 do
416
if LightEnabling[I] then
422
function IsFogEnabled(const AFogSupportMode: TGLShaderFogSupport; var rci: TGLRenderContextInfo): Boolean;
424
case AFogSupportMode of
425
sfsEnabled: Result := True;
426
sfsDisabled: Result := False;
427
sfsAuto: Result := TGLSceneBuffer(rci.buffer).FogEnable;
430
Assert(False, glsUnknownType);
434
procedure CopyScreentoTexture(const ViewPortSize: TGLSize; const TextureTarget: Word = GL_TEXTURE_2D);
436
GL.CopyTexSubImage2D(TextureTarget, 0, 0, 0, 0, 0, ViewPortSize.cx, ViewPortSize.cy);
439
procedure CopyScreentoTexture2(const ViewPortSize: TGLSize; const TextureTarget: Word = GL_TEXTURE_2D);
441
GL.CopyTexImage2D(TextureTarget, 0, GL_RGB, 0, 0, ViewPortSize.cx, ViewPortSize.cy, 0);
444
procedure ApplyBlendingModeEx(const BlendingMode: TGLBlendingModeEx);
446
with CurrentGLContext.GLStates do
451
bmxOpaque: SetBlendFunc(bfSRCALPHA, bfONE);
452
bmxTransparency: SetBlendFunc(bfSRCALPHA, bfONEMINUSSRCALPHA);
453
bmxAdditive: SetBlendFunc(bfSRCALPHA, bfONE);
454
bmxAlphaTest50: SetGLAlphaFunction(cfGEQUAL, 0.5);
455
bmxAlphaTest100: SetGLAlphaFunction(cfGEQUAL, 1.0);
456
bmxModulate: SetBlendFunc(bfDSTCOLOR, bfZERO);
457
bmxDestColorOne: SetBlendFunc(bfDSTCOLOR, bfONE);
458
bmxDestAlphaOne: SetBlendFunc(bfDSTALPHA, bfONE);
460
Assert(False, glsErrorEx + glsUnknownType);
465
procedure UnApplyBlendingModeEx;
469
procedure DrawTexturedScreenQuad;
471
GL.MatrixMode(GL_MODELVIEW);
474
GL.MatrixMode(GL_PROJECTION);
478
// drawing rectangle over screen
479
GL.Disable(GL_DEPTH_TEST);
480
DrawTexturedScreenQuad3;
481
GL.Enable(GL_DEPTH_TEST);
484
GL.MatrixMode(GL_MODELVIEW);
488
procedure DrawTexturedScreenQuad2(const ViewPortSize: TGLSize);
491
GL.MatrixMode(GL_PROJECTION);
494
GL.Ortho(0, ViewPortSize.cx, ViewPortSize.cy, 0, 0, 1);
495
GL.Disable(GL_DEPTH_TEST);
498
GL.TexCoord2f(0.0, ViewPortSize.cy); GL.Vertex2f(0, 0);
499
GL.TexCoord2f(0.0, 0.0); GL.Vertex2f(0, ViewPortSize.cy);
500
GL.TexCoord2f(ViewPortSize.cx, 0.0); GL.Vertex2f(ViewPortSize.cx, ViewPortSize.cy);
501
GL.TexCoord2f(ViewPortSize.cx, ViewPortSize.cy); GL.Vertex2f(ViewPortSize.cx, 0);
504
GL.Enable(GL_DEPTH_TEST);
505
GL.MatrixMode(GL_PROJECTION);
507
GL.MatrixMode(GL_MODELVIEW);
511
procedure DrawTexturedScreenQuad4(const ViewPortSize: TGLSize);
514
GL.TexCoord2f(0, 0); GL.Vertex2f(-1, -1);
515
GL.TexCoord2f(ViewPortSize.cx, 0); GL.Vertex2f( 1, -1);
516
GL.TexCoord2f(ViewPortSize.cx, ViewPortSize.cy); GL.Vertex2f( 1, 1);
517
GL.TexCoord2f(0, ViewPortSize.cy); GL.Vertex2f(-1, 1);
521
procedure DrawTexturedScreenQuad5(const ViewPortSize: TGLSize);
523
GL.MatrixMode( GL_PROJECTION );
526
GL.Ortho( 0, ViewPortSize.cx, ViewPortSize.cy, 0, 0, 1 );
527
GL.MatrixMode(GL_MODELVIEW);
530
GL.Disable(GL_DEPTH_TEST);
531
GL.DepthMask( FALSE );
532
DrawTexturedScreenQuad3;
533
GL.DepthMask( TRUE );
534
GL.Enable(GL_DEPTH_TEST);
536
GL.MatrixMode( GL_PROJECTION );
538
GL.MatrixMode( GL_MODELVIEW );
541
procedure DrawTexturedScreenQuad6(const ViewPortSize: TGLSize);
543
GL.MatrixMode( GL_PROJECTION );
546
GL.Ortho( 0, ViewPortSize.cx, ViewPortSize.cy, 0, 0, 1 );
547
GL.MatrixMode(GL_MODELVIEW);
550
GL.Disable(GL_DEPTH_TEST);
551
GL.DepthMask( FALSE );
552
DrawTexturedScreenQuad4(ViewPortSize);;
553
GL.DepthMask( TRUE );
554
GL.Enable(GL_DEPTH_TEST);
556
GL.MatrixMode( GL_PROJECTION );
558
GL.MatrixMode( GL_MODELVIEW );
561
procedure DrawTexturedScreenQuad3;
564
GL.TexCoord2f(0, 0); GL.Vertex2f(-1, -1);
565
GL.TexCoord2f(1, 0); GL.Vertex2f(1, -1);
566
GL.TexCoord2f(1, 1); GL.Vertex2f(1, 1);
567
GL.TexCoord2f(0, 1); GL.Vertex2f(-1, 1);
571
procedure InitTexture(
572
const TextureHandle: Cardinal;
573
const TextureSize: TGLSize;
574
const TextureTarget: TGLTextureTarget = ttTexture2D);
578
with CurrentGLContext.GLStates do
580
TextureBinding[ActiveTexture, TextureTarget] := TextureHandle;
582
glTarget := DecodeGLTextureTarget(TextureTarget);
583
GL.TexParameteri(glTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
584
GL.TexParameteri(glTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
585
GL.TexParameteri(glTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
586
GL.TexParameteri(glTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
587
GL.CopyTexImage2D(glTarget, 0, GL_RGBA8, 0, 0, TextureSize.cx, TextureSize.cy, 0);
592
procedure TGLShaderProgram.Apply;
594
FParent.FinalizeShader;
598
procedure TGLShaderProgram.Assign(Source: TPersistent);
603
if (Source is TGLShaderProgram) then
605
FEnabled := TGLShaderProgram(Source).FEnabled;
606
FCode.Assign(TGLShaderProgram(Source).FCode);
609
inherited; //die, die, die!!!
613
constructor TGLShaderProgram.Create(const AParent: TGLCustomShader);
616
FCode := TStringList.Create;
617
TStringList(FCode).OnChange := OnChangeCode;
622
destructor TGLShaderProgram.Destroy;
628
function TGLShaderProgram.GetOwner: TPersistent;
633
procedure TGLShaderProgram.LoadFromFile(const AFileName: string);
635
FCode.LoadFromFile(AFileName);
640
procedure TGLShaderProgram.OnChangeCode(Sender: TObject);
643
FParent.NotifyChange(self);
647
procedure TGLShaderProgram.SetCode(const Value: TStrings);
650
FParent.NotifyChange(self);
654
procedure TGLShaderProgram.SetEnabled(const Value: Boolean);
656
if Value = FEnabled then
660
FParent.FinalizeShader;
666
procedure TGLCustomShader.Assign(Source: TPersistent);
668
if Source is TGLCustomShader then
670
FFragmentProgram.Assign(TGLCustomShader(Source).FFragmentProgram);
671
FVertexProgram.Assign(TGLCustomShader(Source).FVertexProgram);
672
FGeometryProgram.Assign(TGLCustomShader(Source).FGeometryProgram);
673
FTagObject := TGLCustomShader(Source).FTagObject;
679
constructor TGLCustomShader.Create(AOwner: TComponent);
681
inherited Create(AOwner);
684
FFragmentProgram := TGLFragmentProgram.Create(Self);
685
FVertexProgram := TGLVertexProgram.Create(Self);
686
FGeometryProgram := TGLGeometryProgram.Create(Self);
690
destructor TGLCustomShader.Destroy;
692
FFragmentProgram.Destroy;
693
FVertexProgram.Destroy;
694
FGeometryProgram.Destroy;
699
procedure TGLCustomShader.LoadShaderPrograms(const VPFilename, FPFilename: string; GPFilename: string = '');
701
If VPFilename <> '' then VertexProgram.LoadFromFile(VPFilename);
702
If FPFilename <> '' then FragmentProgram.LoadFromFile(FPFilename);
703
If GPFilename <> '' then GeometryProgram.LoadFromFile(GPFilename);
706
procedure TGLCustomShader.SetDebugMode(const Value: Boolean);
708
if FDebugMode <> Value then
713
FailedInitAction := fiaReRaiseException
715
FailedInitAction := fiaRaiseStandardException;
719
procedure TGLCustomShader.SetFragmentProgram(const Value: TGLFragmentProgram);
721
FFragmentProgram.Assign(Value);
724
procedure TGLCustomShader.SetGeometryProgram(const Value: TGLGeometryProgram);
726
FGeometryProgram.Assign(Value);
729
procedure TGLCustomShader.SetVertexProgram(const Value: TGLVertexProgram);
731
FVertexProgram.Assign(Value);
734
function TGLCustomShader.StoreFragmentProgram: Boolean;
736
Result := FFragmentProgram.Enabled or (FFragmentProgram.Code.Text <> '')
739
function TGLCustomShader.StoreGeometryProgram: Boolean;
741
Result := FGeometryProgram.Enabled or (FGeometryProgram.Code.Text <> '')
744
function TGLCustomShader.StoreVertexProgram: Boolean;
746
Result := FVertexProgram.Enabled or (FVertexProgram.Code.Text <> '')
749
{ TGLCustomShaderParameter }
751
procedure TGLCustomShaderParameter.SetAsTexture(
752
const TextureIndex: Integer; const Value: TGLTexture);
754
SetAsCustomTexture(TextureIndex, Value.TextureHandle.Target, Value.Handle);
757
procedure TGLCustomShaderParameter.SetAsTexture1D(
758
const TextureIndex: Integer; const Value: TGLTexture);
760
SetAsCustomTexture(TextureIndex, ttTexture1D, Value.Handle);
763
procedure TGLCustomShaderParameter.SetAsTexture2D(
764
const TextureIndex: Integer; const Value: TGLTexture);
766
SetAsCustomTexture(TextureIndex, ttTexture2D, Value.Handle);
769
procedure TGLCustomShaderParameter.SetAsTexture3D(
770
const TextureIndex: Integer; const Value: TGLTexture);
772
SetAsCustomTexture(TextureIndex, ttTexture3D, Value.Handle);
775
procedure TGLCustomShaderParameter.SetAsTextureCube(
776
const TextureIndex: Integer; const Value: TGLTexture);
778
SetAsCustomTexture(TextureIndex, ttTextureCube, Value.Handle);
781
procedure TGLCustomShaderParameter.SetAsTextureRect(
782
const TextureIndex: Integer; const Value: TGLTexture);
784
SetAsCustomTexture(TextureIndex, ttTextureRect, Value.Handle);
787
procedure TGLCustomShaderParameter.SetAsVectorF(const Values: array of Single);
789
case Length(Values) of
790
1: SetAsVector1f(Values[0]);
791
2: SetAsVector2f(Vector2fMake(Values[0], Values[1]));
792
3: SetAsVector3f(Vector3fMake(Values[0], Values[1], Values[2]));
793
4: SetAsVector4f(Vector4fMake(Values[0], Values[1], Values[2], Values[3]));
795
Assert(False, 'Vector length must be between 1 to 4');
799
procedure TGLCustomShaderParameter.SetAsVectorI(const Values: array of Integer);
801
case Length(Values) of
802
1: SetAsVector1i(Values[0]);
803
2: SetAsVector2i(Vector2iMake(Values[0], Values[1]));
804
3: SetAsVector3i(Vector3iMake(Values[0], Values[1], Values[2]));
805
4: SetAsVector4i(Vector4iMake(Values[0], Values[1], Values[2], Values[3]));
807
Assert(False, 'Vector length must be between 1 to 4');
811
procedure TGLCustomShaderParameter.SetToTextureOf(
812
const LibMaterial: TGLLibMaterial; const TextureIndex: Integer);
814
SetToTextureOf(LibMaterial.Material.Texture, TextureIndex);
817
procedure TGLCustomShaderParameter.SetToTextureOf(
818
const Texture: TGLTexture; const TextureIndex: Integer);
820
SetAsCustomTexture(TextureIndex, Texture.Image.NativeTextureTarget, Texture.Handle);
823
constructor TGLGeometryProgram.Create(const AParent: TGLCustomShader);
825
inherited Create(AParent);
826
FInputPrimitiveType := gsInPoints;
827
FOutputPrimitiveType := gsOutPoints;
831
procedure TGLGeometryProgram.SetInputPrimitiveType(const Value: TGLgsInTypes);
833
if Value <> FInputPrimitiveType then
835
FInputPrimitiveType := Value;
836
FParent.NotifyChange(Self);
840
procedure TGLGeometryProgram.SetOutputPrimitiveType(const Value: TGLgsOutTypes);
842
if Value<>FOutputPrimitiveType then
844
FOutputPrimitiveType := Value;
845
FParent.NotifyChange(Self);
849
procedure TGLGeometryProgram.SetVerticesOut(const Value: TGLint);
851
if Value<>FVerticesOut then
853
FVerticesOut := Value;
854
FParent.NotifyChange(Self);
859
RegisterClasses([TGLCustomShader, TGLShaderProgram,
860
TGLVertexProgram, TGLFragmentProgram, TGLGeometryProgram]);