8
System.Generics.Collections,
25
TShaderVar<_TValue_> = class;
26
TShaderVarPrim<_TValue_> = class;
27
TShaderVarSingle = class;
28
TShaderVarPointF = class;
29
TShaderVarPoint3D = class;
30
TShaderVarVector3D = class;
31
TShaderVarColor = class;
32
TShaderVarColorF = class;
33
TShaderVarMatrix3D = class;
34
TShaderVarTexture = class;
35
TShaderVarTexture3D<_TValue_: TTexture3D> = class;
36
TShaderVarLight = class;
38
TShaderSource = class;
39
TShaderSourceV = class;
40
TShaderSourceP = class;
44
TShaderVars = array of TShaderVar;
52
function GetSize: Integer; virtual; abstract;
54
constructor Create(const Name_: String);
55
property Name: String read _Name write _Name;
56
property Size: Integer read GetSize;
57
function GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables;
59
procedure SendVar(const Context_: TContext3D); virtual; abstract;
60
function GetSource(var C_: Integer; var T_: Integer): String; virtual; abstract;
63
// TShaderVar<_TValue_>
65
TShaderVar<_TValue_> = class(TShaderVar)
69
procedure SetValue(const Value_: _TValue_); virtual;
71
property Value: _TValue_ read _Value write SetValue;
76
TShaderVarPrim<_TValue_> = class(TShaderVar<_TValue_>)
79
function GetKind: TContextShaderVariableKind; virtual; abstract;
81
property Kind: TContextShaderVariableKind read GetKind;
82
function GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables; override;
87
TShaderVarSingle = class(TShaderVarPrim<Single>)
91
function GetKind: TContextShaderVariableKind; override;
92
function GetSize: Integer; override;
95
procedure SendVar(const Context_: TContext3D); override;
96
function GetSource(var C_: Integer; var T_: Integer): String; override;
101
TShaderVarPointF = class(TShaderVarPrim<TPointF>)
105
function GetKind: TContextShaderVariableKind; override;
106
function GetSize: Integer; override;
109
procedure SendVar(const Context_: TContext3D); override;
110
function GetSource(var C_: Integer; var T_: Integer): String; override;
115
TShaderVarPoint3D = class(TShaderVarPrim<TPoint3D>)
119
function GetKind: TContextShaderVariableKind; override;
120
function GetSize: Integer; override;
123
procedure SendVar(const Context_: TContext3D); override;
124
function GetSource(var C_: Integer; var T_: Integer): String; override;
127
// TShaderVarVector3D
129
TShaderVarVector3D = class(TShaderVarPrim<TVector3D>)
133
function GetKind: TContextShaderVariableKind; override;
134
function GetSize: Integer; override;
137
procedure SendVar(const Context_: TContext3D); override;
138
function GetSource(var C_: Integer; var T_: Integer): String; override;
143
TShaderVarColor = class(TShaderVarPrim<TAlphaColor>)
147
function GetKind: TContextShaderVariableKind; override;
148
function GetSize: Integer; override;
151
procedure SendVar(const Context_: TContext3D); override;
152
function GetSource(var C_: Integer; var T_: Integer): String; override;
157
TShaderVarColorF = class(TShaderVarPrim<TAlphaColorF>)
161
function GetKind: TContextShaderVariableKind; override;
162
function GetSize: Integer; override;
165
procedure SendVar(const Context_: TContext3D); override;
166
function GetSource(var C_: Integer; var T_: Integer): String; override;
169
// TShaderVarMatrix3D
171
TShaderVarMatrix3D = class(TShaderVarPrim<TMatrix3D>)
175
function GetKind: TContextShaderVariableKind; override;
176
function GetSize: Integer; override;
179
function GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables; override;
180
procedure SendVar(const Context_: TContext3D); override;
181
function GetSource(var C_: Integer; var T_: Integer): String; override;
186
TShaderVarTexture = class(TShaderVarPrim<TTexture>)
190
function GetKind: TContextShaderVariableKind; override;
191
function GetSize: Integer; override;
194
procedure SendVar(const Context_: TContext3D); override;
195
function GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables; override;
196
function GetSource(var C_: Integer; var T_: Integer): String; override;
199
// TShaderVarTexture3D<_TValue_>
201
TShaderVarTexture3D<_TValue_: TTexture3D> = class(TShaderVarPrim<_TValue_>)
205
function GetKind: TContextShaderVariableKind; override;
206
function GetSize: Integer; override;
208
constructor Create(const Name_: String);
209
destructor Destroy; override;
211
procedure SendVar(const Context_: TContext3D); override;
212
function GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables; override;
213
function GetSource(var C_: Integer; var T_: Integer): String; override;
218
TShaderVarLight = class(TShaderVar<TLightDescription>)
220
_Opt: TShaderVarPoint3D;
221
_Pos: TShaderVarPoint3D;
222
_Dir: TShaderVarPoint3D;
223
_Col: TShaderVarColor;
226
function GetSize: Integer; override;
227
procedure SetValue(const Value_: TLightDescription); override;
229
constructor Create(const Name_: String);
230
destructor Destroy; override;
232
function GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables; override;
233
procedure SendVar(const Context_: TContext3D); override;
234
function GetSource(var C_: Integer; var T_: Integer): String; override;
239
TShaderSource = class
243
_Shader: TContextShader;
246
_Source: TStringList;
247
_Targets: TDictionary<TContextShaderArch, AnsiString>;
248
_Errors: TDictionary<String, String>;
250
function GetKind: TContextShaderKind; virtual; abstract;
251
procedure SetSource(Sender_: TObject);
254
destructor Destroy; override;
256
property Name: String read _Name write _Name;
257
property Shader: TContextShader read _Shader write _Shader;
258
property Kind: TContextShaderKind read GetKind;
259
property Vars: TShaderVars read _Vars write _Vars;
260
property Entry: AnsiString read _Entry write _Entry;
261
property Source: TStringList read _Source;
262
property Errors: TDictionary<String, String> read _Errors;
264
procedure LoadFromFile(const Name_: String);
265
procedure LoadFromStream(const Stream_: TStream);
266
procedure LoadFromResource(const Name_: String);
267
function GetVars(const A_: TContextShaderArch): TContextShaderVariables;
269
procedure SendVars(const Context_: TContext3D);
270
function GetSources: String;
275
TShaderSourceV = class(TShaderSource)
279
function GetKind: TContextShaderKind; override;
286
TShaderSourceP = class(TShaderSource)
290
function GetKind: TContextShaderKind; override;
297
TLuxMaterial = class(TMaterial)
300
_ShaderV: TShaderSourceV;
301
_ShaderP: TShaderSourceP;
303
procedure DoInitialize; override;
305
constructor Create; override;
306
destructor Destroy; override;
308
property ShaderV: TShaderSourceV read _ShaderV;
309
property ShaderP: TShaderSourceP read _ShaderP;
312
// TMaterialSource<_TMaterial_>
314
TLuxMaterialSource<_TMaterial_: TLuxMaterial> = class(TMaterialSource)
317
function GetMaterial: _TMaterial_;
320
function GetShaderV: TShaderSourceV;
321
function GetShaderP: TShaderSourceP;
323
property _Material: _TMaterial_ read GetMaterial;
325
function CreateMaterial: TMaterial; override;
328
property ShaderV: TShaderSourceV read GetShaderV;
329
property ShaderP: TShaderSourceP read GetShaderP;
334
VARUNIT: array [TContextShaderArch] of Byte =
347
function PixelFormatToColorN(const PF_: TPixelFormat): Byte;
360
constructor TShaderVar.Create(const Name_: String);
367
procedure TShaderVar<_TValue_>.SetValue(const Value_: _TValue_);
373
// TShaderVarPrim<_TValue_>
375
function TShaderVarPrim<_TValue_>.GetVars(var I_, T_: Integer; const U_: Byte)
376
: TContextShaderVariables;
378
Result := [TContextShaderVariable.Create(Name, Kind, I_, U_ * Size)];
385
function TShaderVarSingle.GetKind: TContextShaderVariableKind;
387
Result := TContextShaderVariableKind.Float;
390
function TShaderVarSingle.GetSize: Integer;
395
procedure TShaderVarSingle.SendVar(const Context_: TContext3D);
397
Context_.SetShaderVariable(_Name, [TVector3D.Create(_Value, 0, 0, 0)]);
400
function TShaderVarSingle.GetSource(var C_: Integer; var T_: Integer): String;
402
Result := 'float ' + _Name + ' : register( c' + C_.ToString + ' );' + CRLF;
409
function TShaderVarPointF.GetKind: TContextShaderVariableKind;
411
Result := TContextShaderVariableKind.Float2;
414
function TShaderVarPointF.GetSize: Integer;
419
procedure TShaderVarPointF.SendVar(const Context_: TContext3D);
421
Context_.SetShaderVariable(_Name, [TVector3D.Create(_Value.X, _Value.Y, 0, 0)]);
424
function TShaderVarPointF.GetSource(var C_: Integer; var T_: Integer): String;
426
Result := 'float2 ' + _Name + ' : register( c' + C_.ToString + ' );' + CRLF;
433
function TShaderVarPoint3D.GetKind: TContextShaderVariableKind;
435
Result := TContextShaderVariableKind.Float3;
438
function TShaderVarPoint3D.GetSize: Integer;
443
procedure TShaderVarPoint3D.SendVar(const Context_: TContext3D);
445
Context_.SetShaderVariable(_Name, [TVector3D.Create(_Value.X, _Value.Y, _Value.Z, 0)]);
448
function TShaderVarPoint3D.GetSource(var C_: Integer; var T_: Integer): String;
450
Result := 'float3 ' + _Name + ' : register( c' + C_.ToString + ' );' + CRLF;
457
function TShaderVarVector3D.GetKind: TContextShaderVariableKind;
459
Result := TContextShaderVariableKind.Vector;
462
function TShaderVarVector3D.GetSize: Integer;
467
procedure TShaderVarVector3D.SendVar(const Context_: TContext3D);
469
Context_.SetShaderVariable(_Name, _Value);
472
function TShaderVarVector3D.GetSource(var C_: Integer; var T_: Integer): String;
474
Result := 'float4 ' + _Name + ' : register( c' + C_.ToString + ' );' + CRLF;
481
function TShaderVarColor.GetKind: TContextShaderVariableKind;
483
Result := TContextShaderVariableKind.Vector;
486
function TShaderVarColor.GetSize: Integer;
491
procedure TShaderVarColor.SendVar(const Context_: TContext3D);
493
Context_.SetShaderVariable(_Name, _Value);
496
function TShaderVarColor.GetSource(var C_: Integer; var T_: Integer): String;
498
Result := 'float4 ' + _Name + ' : register( c' + C_.ToString + ' );' + CRLF;
505
function TShaderVarColorF.GetKind: TContextShaderVariableKind;
507
Result := TContextShaderVariableKind.Vector;
510
function TShaderVarColorF.GetSize: Integer;
515
procedure TShaderVarColorF.SendVar(const Context_: TContext3D);
518
Context_.SetShaderVariable(_Name, TVector3D.Create(R, G, B, A));
521
function TShaderVarColorF.GetSource(var C_: Integer; var T_: Integer): String;
523
Result := 'float4 ' + _Name + ' : register( c' + C_.ToString + ' );' + CRLF;
530
function TShaderVarMatrix3D.GetKind: TContextShaderVariableKind;
532
Result := TContextShaderVariableKind.Matrix;
535
function TShaderVarMatrix3D.GetSize: Integer;
540
function TShaderVarMatrix3D.GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables;
542
Result := [TContextShaderVariable.Create(Name, Kind, I_, U_ * Size)];
547
procedure TShaderVarMatrix3D.SendVar(const Context_: TContext3D);
549
Context_.SetShaderVariable(_Name, _Value);
552
function TShaderVarMatrix3D.GetSource(var C_: Integer; var T_: Integer): String;
554
Result := 'row_major float4x4 ' + _Name + ' : register( c' + C_.ToString + ' );' + CRLF;
561
function TShaderVarTexture.GetKind: TContextShaderVariableKind;
563
Result := TContextShaderVariableKind.Texture;
566
function TShaderVarTexture.GetSize: Integer;
571
function TShaderVarTexture.GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables;
573
Result := [TContextShaderVariable.Create(Name, Kind, T_, U_ * Size)];
578
procedure TShaderVarTexture.SendVar(const Context_: TContext3D);
580
Context_.SetShaderVariable(_Name, _Value);
583
function TShaderVarTexture.GetSource(var C_: Integer; var T_: Integer): String;
585
Result := 'Texture2D<float4> ' + _Name + ' : register( t' + T_.ToString + ' );' + CRLF;
590
// TShaderVarTexture3D
592
constructor TShaderVarTexture3D<_TValue_>.Create(const Name_: String);
596
_Value := _TValue_.Create;
599
destructor TShaderVarTexture3D<_TValue_>.Destroy;
606
function TShaderVarTexture3D<_TValue_>.GetKind: TContextShaderVariableKind;
608
Result := TContextShaderVariableKind.Texture;
611
function TShaderVarTexture3D<_TValue_>.GetSize: Integer;
616
function TShaderVarTexture3D<_TValue_>.GetVars(var I_, T_: Integer; const U_: Byte)
617
: TContextShaderVariables;
619
Result := [TContextShaderVariable.Create(Name, Kind, T_, U_ * Size)];
624
procedure TShaderVarTexture3D<_TValue_>.SendVar(const Context_: TContext3D);
626
Context_.SetShaderVariable(_Name, _Value);
629
function TShaderVarTexture3D<_TValue_>.GetSource(var C_: Integer; var T_: Integer): String;
631
Result := 'Texture3D<float' + PixelFormatToColorN(_Value.PixelFormat).ToString + '> ' + _Name +
632
' : register( t' + T_.ToString + ' );' + CRLF;
639
function TShaderVarLight.GetSize: Integer;
641
Result := _Opt.Size + _Pos.Size + _Dir.Size + _Col.Size;
644
procedure TShaderVarLight.SetValue(const Value_: TLightDescription);
650
_Opt.Value := TPoint3D.Create(Integer(LightType) + 1, Cos(DegToRad(SpotCutoff)), SpotExponent);
651
_Pos.Value := Position;
652
_Dir.Value := Direction;
657
constructor TShaderVarLight.Create(const Name_: String);
661
_Opt := TShaderVarPoint3D.Create(_Name + '.Opt');
662
_Pos := TShaderVarPoint3D.Create(_Name + '.Pos');
663
_Dir := TShaderVarPoint3D.Create(_Name + '.Dir');
664
_Col := TShaderVarColor.Create(_Name + '.Col');
667
destructor TShaderVarLight.Destroy;
675
function TShaderVarLight.GetVars(var I_, T_: Integer; const U_: Byte): TContextShaderVariables;
677
Result := _Opt.GetVars(I_, T_, U_) + _Pos.GetVars(I_, T_, U_) + _Dir.GetVars(I_, T_, U_) +
678
_Col.GetVars(I_, T_, U_);
681
procedure TShaderVarLight.SendVar(const Context_: TContext3D);
683
_Opt.SendVar(Context_);
684
_Pos.SendVar(Context_);
685
_Dir.SendVar(Context_);
686
_Col.SendVar(Context_);
689
function TShaderVarLight.GetSource(var C_: Integer; var T_: Integer): String;
691
Result := 'struct TLight' + CRLF + '{' + CRLF + ' float3 Opt;' + CRLF + ' float3 Pos;' +
692
CRLF + ' float3 Dir;' + CRLF + ' float4 Col;' + CRLF + '};' + CRLF + 'TLight ' + _Name +
693
' : register( c' + C_.ToString + ' );' + CRLF;
700
procedure TShaderSource.SetSource(Sender_: TObject);
705
constructor TShaderSource.Create;
709
_Source := TStringList.Create;
710
_Source.OnChange := SetSource;
712
_Targets := TDictionary<TContextShaderArch, AnsiString>.Create;
713
_Errors := TDictionary<String, String>.Create;
716
destructor TShaderSource.Destroy;
726
procedure TShaderSource.LoadFromFile(const Name_: String);
728
_Source.LoadFromFile(Name_);
730
_Name := TPath.GetFileName(Name_);
735
procedure TShaderSource.LoadFromStream(const Stream_: TStream);
737
_Source.LoadFromStream(Stream_);
744
procedure TShaderSource.LoadFromResource(const Name_: String);
748
RS := TResourceStream.Create(HInstance, Name_, RT_RCDATA);
757
function TShaderSource.GetVars(const A_: TContextShaderArch): TContextShaderVariables;
767
Result := Result + V.GetVars(C, T, VARUNIT[A_]);
770
procedure TShaderSource.Compile;
773
CSSs: array of TContextShaderSource;
774
A: TContextShaderArch;
780
TShaderManager.UnregisterShader(_Shader);
784
S := AnsiString(GetSources + _Source.Text);
785
N := AnsiString(_Name);
789
for A in _Targets.Keys do
793
H := D3DCompile(PAnsiChar(S), Length(S), PAnsiChar(N), nil, nil, PAnsiChar(_Entry),
794
PAnsiChar(T), D3DCOMPILE_OPTIMIZATION_LEVEL3, 0, B, E);
798
SetLength(Bs, B.GetBufferSize);
800
Move(B.GetBufferPointer^, Bs[0], B.GetBufferSize);
802
CSSs := CSSs + [TContextShaderSource.Create(A, Bs, GetVars(A))];
806
SetString(M, PAnsiChar(E.GetBufferPointer), E.GetBufferSize);
808
_Errors.Add(String(T), M);
812
if Assigned(CSSs) then
813
_Shader := TShaderManager.RegisterShaderFromData(_Name, GetKind, '', CSSs);
816
procedure TShaderSource.SendVars(const Context_: TContext3D);
824
function TShaderSource.GetSources: String;
834
Result := Result + V.GetSource(C, T);
839
function TShaderSourceV.GetKind: TContextShaderKind;
841
Result := TContextShaderKind.VertexShader;
844
constructor TShaderSourceV.Create;
850
_Targets.Add(TContextShaderArch.DX9, 'vs_3_0');
851
_Targets.Add(TContextShaderArch.DX11_level_9, 'vs_4_0_level_9_3');
852
_Targets.Add(TContextShaderArch.DX11, 'vs_5_0');
857
function TShaderSourceP.GetKind: TContextShaderKind;
859
Result := TContextShaderKind.PixelShader;
862
constructor TShaderSourceP.Create;
868
_Targets.Add(TContextShaderArch.DX9, 'ps_3_0');
869
_Targets.Add(TContextShaderArch.DX11_level_9, 'ps_4_0_level_9_3');
870
_Targets.Add(TContextShaderArch.DX11, 'ps_5_0');
875
procedure TLuxMaterial.DoInitialize;
881
constructor TLuxMaterial.Create;
885
_ShaderV := TShaderSourceV.Create;
886
_ShaderP := TShaderSourceP.Create;
889
destructor TLuxMaterial.Destroy;
897
// TLuxMaterialSource<_TMaterial_>
899
function TLuxMaterialSource<_TMaterial_>.GetMaterial: _TMaterial_;
901
Result := _TMaterial_(Material);
904
function TLuxMaterialSource<_TMaterial_>.GetShaderV: TShaderSourceV;
906
Result := _Material.ShaderV;
909
function TLuxMaterialSource<_TMaterial_>.GetShaderP: TShaderSourceP;
911
Result := _Material.ShaderP;
914
function TLuxMaterialSource<_TMaterial_>.CreateMaterial: TMaterial;
916
Result := _TMaterial_.Create;
919
function PixelFormatToColorN(const PF_: TPixelFormat): Byte;
932
TPixelFormat.BGR_565:
938
TPixelFormat.BGR5_A1:
942
TPixelFormat.BGR10_A2:
944
TPixelFormat.RGB10_A2:
960
TPixelFormat.RGBA16F:
966
TPixelFormat.RGBA32F: