GLSL.Shader.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLSL.Shader;
  5. (* TGLSLShader is a wrapper for GLS shaders. *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. Winapi.OpenGL,
  10. Winapi.OpenGLext,
  11. System.Classes,
  12. System.SysUtils,
  13. GLS.OpenGLTokens,
  14. GLS.VectorGeometry,
  15. GLS.VectorTypes,
  16. GLS.Texture,
  17. GLS.Context,
  18. GLSL.CustomShader,
  19. GLS.RenderContextInfo,
  20. GLS.TextureFormat,
  21. GLSL.ShaderParameter,
  22. GLS.Material,
  23. GLS.State;
  24. type
  25. TGLSLShaderParameter = class;
  26. TGLCustomGLSLShader = class;
  27. EGLSLShaderException = class(EGLCustomShaderException);
  28. TGLSLShaderEvent = procedure(Shader: TGLCustomGLSLShader) of object;
  29. TGLSLShaderUnApplyEvent = procedure(Shader: TGLCustomGLSLShader;
  30. var ThereAreMorePasses: Boolean) of object;
  31. TGLSLShaderEventEx = procedure(Shader: TGLCustomGLSLShader;
  32. Sender: TObject) of object;
  33. TGLActiveAttrib = record
  34. Name: string;
  35. Size: Integer;
  36. AType: TGLSLDataType;
  37. Location: Integer;
  38. end;
  39. TGLActiveAttribArray = array of TGLActiveAttrib;
  40. TGLCustomGLSLShader = class(TGLCustomShader)
  41. private
  42. FGLSLProg: TGLProgramHandle;
  43. FParam: TGLSLShaderParameter;
  44. FActiveVarying: TStrings;
  45. FTransformFeedBackMode: TGLTransformFeedBackMode;
  46. FOnInitialize: TGLSLShaderEvent;
  47. FOnApply: TGLSLShaderEvent;
  48. FOnUnApply: TGLSLShaderUnApplyEvent;
  49. FOnInitializeEx: TGLSLShaderEventEx;
  50. FOnApplyEx: TGLSLShaderEventEx;
  51. FNextTexIndex : integer; // for auto texture unit indicing
  52. function GetParam(const Index: string): TGLSLShaderParameter;
  53. function GetDirectParam(const Index: Cardinal): TGLSLShaderParameter;
  54. procedure OnChangeActiveVarying(Sender: TObject);
  55. protected
  56. property OnApply: TGLSLShaderEvent read FOnApply write FOnApply;
  57. property OnUnApply: TGLSLShaderUnApplyEvent read FOnUnApply write FOnUnApply;
  58. property OnInitialize: TGLSLShaderEvent read FOnInitialize write FOnInitialize;
  59. property OnInitializeEx: TGLSLShaderEventEx read FOnInitializeEx write FOnInitializeEx;
  60. property OnApplyEx: TGLSLShaderEventEx read FOnApplyEx write FOnApplyEx;
  61. function GetGLSLProg: TGLProgramHandle; virtual;
  62. function GetCurrentParam: TGLSLShaderParameter; virtual;
  63. procedure SetActiveVarying(const Value: TStrings);
  64. procedure SetTransformFeedBackMode(const Value: TGLTransformFeedBackMode);
  65. procedure DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject); override;
  66. procedure DoFinalize; override;
  67. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  68. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  69. public
  70. constructor Create(AOwner: TComponent); override;
  71. destructor Destroy; override;
  72. procedure Assign(Source: TPersistent); override;
  73. function ShaderSupported: Boolean; override;
  74. function GetActiveAttribs: TGLActiveAttribArray;
  75. // SetTex() sets texture with automatic book-keeping of texture unit indices.
  76. // Users can just call SetTex() in the OnApply event without keeping track of texture unit indices.
  77. // Call from OnApply() only.
  78. procedure SetTex(const TexParamName : String; Tex : TGLTexture); overload;
  79. procedure SetTex(const TexParamName : String; Mat : TGLLibMaterial); overload;
  80. procedure SetTex(TexParam : TGLSLShaderParameter; Tex : TGLTexture); overload;
  81. procedure SetTex(TexParam : TGLSLShaderParameter; Mat : TGLLibMaterial); overload;
  82. property Param[const Index: string]: TGLSLShaderParameter read GetParam;
  83. property DirectParam[const Index: Cardinal]: TGLSLShaderParameter read GetDirectParam;
  84. property ActiveVarying: TStrings read FActiveVarying write SetActiveVarying;
  85. property TransformFeedBackMode: TGLTransformFeedBackMode read FTransformFeedBackMode write SetTransformFeedBackMode default tfbmInterleaved;
  86. end;
  87. // Wrapper around a parameter of a GLSL program.
  88. TGLSLShaderParameter = class(TGLCustomShaderParameter)
  89. private
  90. FGLSLProg: TGLProgramHandle;
  91. FParameterID: Integer;
  92. protected
  93. function GetAsVector1f: Single; override;
  94. function GetAsVector2f: TVector2f; override;
  95. function GetAsVector3f: TVector3f; override;
  96. function GetAsVector4f: TGLVector; override;
  97. function GetAsVector1i: Integer; override;
  98. function GetAsVector2i: TVector2i; override;
  99. function GetAsVector3i: TVector3i; override;
  100. function GetAsVector4i: TVector4i; override;
  101. function GetAsVector1ui: Cardinal; override;
  102. function GetAsVector2ui: TVector2ui; override;
  103. function GetAsVector3ui: TVector3ui; override;
  104. function GetAsVector4ui: TVector4ui; override;
  105. procedure SetAsVector1f(const Value: Single); override;
  106. procedure SetAsVector2f(const Value: TVector2f); override;
  107. procedure SetAsVector3f(const Value: TVector3f); override;
  108. procedure SetAsVector4f(const Value: TVector4f); override;
  109. procedure SetAsVector1i(const Value: Integer); override;
  110. procedure SetAsVector2i(const Value: TVector2i); override;
  111. procedure SetAsVector3i(const Value: TVector3i); override;
  112. procedure SetAsVector4i(const Value: TVector4i); override;
  113. procedure SetAsVector1ui(const Value: Cardinal); override;
  114. procedure SetAsVector2ui(const Value: TVector2ui); override;
  115. procedure SetAsVector3ui(const Value: TVector3ui); override;
  116. procedure SetAsVector4ui(const Value: TVector4ui); override;
  117. function GetAsMatrix2f: TMatrix2f; override;
  118. function GetAsMatrix3f: TMatrix3f; override;
  119. function GetAsMatrix4f: TMatrix4f; override;
  120. procedure SetAsMatrix2f(const Value: TMatrix2f); override;
  121. procedure SetAsMatrix3f(const Value: TMatrix3f); override;
  122. procedure SetAsMatrix4f(const Value: TMatrix4f); override;
  123. function GetAsCustomTexture(const TextureIndex: Integer;
  124. TextureTarget: TGLTextureTarget): Cardinal; override;
  125. procedure SetAsCustomTexture(const TextureIndex: Integer;
  126. TextureTarget: TGLTextureTarget; const Value: Cardinal); override;
  127. function GetAsUniformBuffer: Cardinal; override;
  128. procedure SetAsUniformBuffer( UBO: Cardinal); override;
  129. public
  130. // Nothing here ...yet.
  131. end;
  132. TGLSLShader = class(TGLCustomGLSLShader)
  133. published
  134. property FragmentProgram;
  135. property VertexProgram;
  136. property GeometryProgram;
  137. property OnApply;
  138. property OnApplyEx;
  139. property OnUnApply;
  140. property OnInitialize;
  141. property OnInitializeEx;
  142. property ShaderStyle;
  143. property FailedInitAction;
  144. property ActiveVarying;
  145. property TransformFeedBackMode;
  146. end;
  147. //------------------------------------------------------------------------
  148. implementation
  149. //------------------------------------------------------------------------
  150. //----------------------------------
  151. // TGLCustomGLSLShader
  152. //----------------------------------
  153. procedure TGLCustomGLSLShader.DoApply(var rci: TGLRenderContextInfo; Sender: TObject);
  154. begin
  155. FGLSLProg.UseProgramObject;
  156. FNextTexIndex := 0;
  157. if Assigned(FOnApply) then
  158. FOnApply(Self);
  159. if Assigned(FOnApplyEx) then
  160. FOnApplyEx(Self, Sender);
  161. end;
  162. procedure TGLCustomGLSLShader.DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject);
  163. const
  164. cBufferMode: array[tfbmInterleaved..tfbmSeparate] of Cardinal = (
  165. GL_INTERLEAVED_ATTRIBS_EXT, GL_SEPARATE_ATTRIBS_EXT);
  166. var
  167. i, NumVarying: Integer;
  168. sVaryings: array of AnsiString;
  169. pVaryings: array of PAnsiChar;
  170. begin
  171. try
  172. if not ShaderSupported then
  173. HandleFailedInitialization
  174. else
  175. try
  176. FGLSLProg.AllocateHandle;
  177. if FGLSLProg.IsDataNeedUpdate then
  178. begin
  179. if Name <> '' then
  180. FGLSLProg.Name := Name
  181. else
  182. FGLSLProg.Name := ClassName;
  183. FGLSLProg.DetachAllObject;
  184. if VertexProgram.Enabled then
  185. FGLSLProg.AddShader(TGLVertexShaderHandle, VertexProgram.Code.Text, FDebugMode);
  186. if FragmentProgram.Enabled then
  187. FGLSLProg.AddShader(TGLFragmentShaderHandle, FragmentProgram.Code.Text, FDebugMode);
  188. if GeometryProgram.Enabled then
  189. FGLSLProg.AddShader(TGLGeometryShaderHandle, GeometryProgram.Code.Text, FDebugMode);
  190. if VertexProgram.Enabled or FragmentProgram.Enabled or GeometryProgram.Enabled then
  191. begin
  192. if GeometryProgram.Enabled then
  193. begin
  194. gl.ProgramParameteri(FGLSLProg.Handle, GL_GEOMETRY_INPUT_TYPE_EXT,
  195. cGLgsInTypes[GeometryProgram.InputPrimitiveType]);
  196. gl.ProgramParameteri(FGLSLProg.Handle, GL_GEOMETRY_OUTPUT_TYPE_EXT,
  197. cGLgsOutTypes[GeometryProgram.OutputPrimitiveType]);
  198. gl.ProgramParameteri(FGLSLProg.Handle, GL_GEOMETRY_VERTICES_OUT_EXT,
  199. GeometryProgram.VerticesOut);
  200. end;
  201. NumVarying := FActiveVarying.Count;
  202. if NumVarying > 0 then
  203. begin
  204. // Activate varying
  205. SetLength(sVaryings, NumVarying);
  206. SetLength(pVaryings, NumVarying);
  207. for i := 0 to NumVarying - 1 do
  208. begin
  209. sVaryings[i] := AnsiString(FActiveVarying.Strings[i]) + #0;
  210. pVaryings[i] := PAnsiChar( sVaryings[i] );
  211. end;
  212. gl.TransformFeedbackVaryings(
  213. FGLSLProg.Handle, NumVarying, @pVaryings[0],
  214. cBufferMode[FTransformFeedBackMode] );
  215. end;
  216. if (not FGLSLProg.LinkProgram) then
  217. raise EGLSLShaderException.Create(FGLSLProg.InfoLog);
  218. FGLSLProg.DetachAllObject; // Detach shaders after linking.
  219. end;
  220. FGLSLProg.NotifyDataUpdated;
  221. end;
  222. except
  223. on E: Exception do
  224. begin
  225. Enabled := False;
  226. HandleFailedInitialization(E.Message);
  227. end;
  228. end;
  229. finally
  230. if Enabled then
  231. try
  232. if Assigned(FOnInitialize) then
  233. begin
  234. FGLSLProg.UseProgramObject;
  235. FOnInitialize(Self);
  236. FGLSLProg.EndUseProgramObject;
  237. end;
  238. if Assigned(FOnInitializeEx) then
  239. begin
  240. FGLSLProg.UseProgramObject;
  241. FOnInitializeEx(Self, Sender);
  242. FGLSLProg.EndUseProgramObject;
  243. end;
  244. if (not FGLSLProg.ValidateProgram) then
  245. raise EGLSLShaderException.Create(FGLSLProg.InfoLog);
  246. except
  247. on E: Exception do
  248. begin
  249. Enabled := False;
  250. HandleFailedInitialization(E.Message);
  251. end;
  252. end;
  253. end;
  254. end;
  255. function TGLCustomGLSLShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  256. begin
  257. Result := False;
  258. if Assigned(FOnUnApply) then
  259. FOnUnApply(Self, Result);
  260. if not Result then
  261. FGLSLProg.EndUseProgramObject;
  262. end;
  263. function TGLCustomGLSLShader.ShaderSupported: Boolean;
  264. begin
  265. Result := (GL.ARB_shader_objects and GL.ARB_vertex_program and
  266. GL.ARB_vertex_shader and GL.ARB_fragment_shader);
  267. end;
  268. function TGLCustomGLSLShader.GetActiveAttribs: TGLActiveAttribArray;
  269. var
  270. LRci: TGLRenderContextInfo;
  271. i, j: Integer;
  272. buff: array[0..127] of AnsiChar;
  273. len: Integer;
  274. max: Integer;
  275. glType: Cardinal;
  276. begin
  277. DoInitialize(LRci, Self);
  278. SetLength(Result, 16);
  279. j := 0;
  280. if FGLSLProg.Handle<>0 then
  281. begin
  282. gl.GetProgramiv(FGLSLProg.Handle, GL_ACTIVE_ATTRIBUTES, @max);
  283. for i := 0 to 16 - 1 do
  284. if i<max then
  285. begin
  286. gl.GetActiveAttrib(FGLSLProg.Handle, i, Length(buff), @len, @Result[j].Size,
  287. @glType, @buff[0]);
  288. if glType > 0 then
  289. with Result[j] do
  290. begin
  291. case glType of
  292. GL_FLOAT: AType := GLSLType1F;
  293. GL_FLOAT_VEC2: AType := GLSLType2F;
  294. GL_FLOAT_VEC3: AType := GLSLType3F;
  295. GL_FLOAT_VEC4: AType := GLSLType4F;
  296. GL_INT: AType := GLSLType1I;
  297. GL_INT_VEC2: AType := GLSLType2I;
  298. GL_INT_VEC3: AType := GLSLType3I;
  299. GL_INT_VEC4: AType := GLSLType4I;
  300. GL_UNSIGNED_INT: AType := GLSLType1UI;
  301. GL_UNSIGNED_INT_VEC2: AType := GLSLType2UI;
  302. GL_UNSIGNED_INT_VEC3: AType := GLSLType3UI;
  303. GL_UNSIGNED_INT_VEC4: AType := GLSLType4UI;
  304. GL_BOOL: AType := GLSLType1I;
  305. GL_BOOL_VEC2: AType := GLSLType2I;
  306. GL_BOOL_VEC3: AType := GLSLType3I;
  307. GL_BOOL_VEC4: AType := GLSLType4I;
  308. GL_FLOAT_MAT2: AType := GLSLTypeMat2F;
  309. GL_FLOAT_MAT3: AType := GLSLTypeMat3F;
  310. GL_FLOAT_MAT4: AType := GLSLTypeMat4F;
  311. end;
  312. Name := Copy(string(buff), 0, len);
  313. Location := i;
  314. Inc(j);
  315. end;
  316. end;
  317. end;
  318. SetLength(Result, j);
  319. end;
  320. procedure TGLCustomGLSLShader.Assign(Source: TPersistent);
  321. begin
  322. inherited Assign(Source);
  323. if Source is TGLCustomGLSLShader then
  324. begin
  325. FreeAndNil(FGLSLProg); //just free the handle for it to be recreated on next initialization
  326. end;
  327. end;
  328. procedure TGLCustomGLSLShader.DoFinalize;
  329. begin
  330. inherited;
  331. if Assigned(FGLSLProg) then
  332. FGLSLProg.NotifyChangesOfData;
  333. end;
  334. function TGLCustomGLSLShader.GetGLSLProg: TGLProgramHandle;
  335. begin
  336. Result := FGLSLProg;
  337. end;
  338. function TGLCustomGLSLShader.GetParam(
  339. const Index: string): TGLSLShaderParameter;
  340. begin
  341. FParam.FParameterID := FGLSLProg.GetUniformLocation(Index);
  342. Result := FParam;
  343. end;
  344. function TGLCustomGLSLShader.GetDirectParam(
  345. const Index: Cardinal): TGLSLShaderParameter;
  346. begin
  347. FParam.FParameterID := Index;
  348. Result := FParam;
  349. end;
  350. function TGLCustomGLSLShader.GetCurrentParam: TGLSLShaderParameter;
  351. begin
  352. Result := FParam;
  353. end;
  354. constructor TGLCustomGLSLShader.Create(AOwner: TComponent);
  355. begin
  356. inherited;
  357. FGLSLProg := TGLProgramHandle.Create;
  358. FParam := TGLSLShaderParameter.Create;
  359. FParam.FGLSLProg := FGLSLProg;
  360. FActiveVarying := TStringList.Create;
  361. TStringList(FActiveVarying).OnChange := OnChangeActiveVarying;
  362. FTransformFeedBackMode := tfbmInterleaved;
  363. end;
  364. destructor TGLCustomGLSLShader.Destroy;
  365. begin
  366. FreeAndNil(FGLSLProg);
  367. FreeAndNil(FParam);
  368. FreeAndNil(FActiveVarying);
  369. inherited;
  370. end;
  371. procedure TGLCustomGLSLShader.SetActiveVarying(const Value: TStrings);
  372. begin
  373. FActiveVarying.Assign(Value);
  374. NotifyChange(Self);
  375. end;
  376. procedure TGLCustomGLSLShader.SetTransformFeedBackMode(const Value: TGLTransformFeedBackMode);
  377. begin
  378. if Value <> FTransformFeedBackMode then
  379. begin
  380. FTransformFeedBackMode := Value;
  381. NotifyChange(Self);
  382. end;
  383. end;
  384. procedure TGLCustomGLSLShader.OnChangeActiveVarying(Sender: TObject);
  385. begin
  386. NotifyChange(Self);
  387. end;
  388. procedure TGLCustomGLSLShader.SetTex(TexParam : TGLSLShaderParameter; Tex : TGLTexture);
  389. begin
  390. TexParam.AsTexture[FNextTexIndex] := Tex;
  391. inc(FNextTexIndex);
  392. end;
  393. procedure TGLCustomGLSLShader.SetTex(TexParam : TGLSLShaderParameter; Mat : TGLLibMaterial);
  394. begin
  395. SetTex(TexParam, Mat.Material.Texture);
  396. end;
  397. procedure TGLCustomGLSLShader.SetTex(const TexParamName: String; Tex: TGLTexture);
  398. begin
  399. SetTex(Param[TexParamName], Tex);
  400. end;
  401. procedure TGLCustomGLSLShader.SetTex(const TexParamName: String; Mat: TGLLibMaterial);
  402. begin
  403. SetTex(TexParamName, Mat.Material.Texture);
  404. end;
  405. { TGLSLShaderParameter }
  406. function TGLSLShaderParameter.GetAsCustomTexture(
  407. const TextureIndex: Integer; TextureTarget: TGLTextureTarget): Cardinal;
  408. begin
  409. gl.GetUniformiv(FGLSLProg.Handle, TextureIndex, @Result);
  410. end;
  411. function TGLSLShaderParameter.GetAsMatrix2f: TMatrix2f;
  412. begin
  413. gl.GetUniformfv(FGLSLProg.Handle, FParameterID, @Result);
  414. end;
  415. function TGLSLShaderParameter.GetAsMatrix3f: TMatrix3f;
  416. begin
  417. gl.GetUniformfv(FGLSLProg.Handle, FParameterID, @Result);
  418. end;
  419. function TGLSLShaderParameter.GetAsMatrix4f: TMatrix4f;
  420. begin
  421. gl.GetUniformfv(FGLSLProg.Handle, FParameterID, @Result);
  422. end;
  423. function TGLSLShaderParameter.GetAsVector1f: Single;
  424. begin
  425. gl.GetUniformfv(FGLSLProg.Handle, FParameterID, @Result);
  426. end;
  427. function TGLSLShaderParameter.GetAsVector1i: Integer;
  428. begin
  429. gl.GetUniformiv(FGLSLProg.Handle, FParameterID, @Result);
  430. end;
  431. function TGLSLShaderParameter.GetAsVector2f: TVector2f;
  432. begin
  433. gl.GetUniformfv(FGLSLProg.Handle, FParameterID, @Result);
  434. end;
  435. function TGLSLShaderParameter.GetAsVector2i: TVector2i;
  436. begin
  437. gl.GetUniformiv(FGLSLProg.Handle, FParameterID, @Result);
  438. end;
  439. function TGLSLShaderParameter.GetAsVector3f: TVector3f;
  440. begin
  441. gl.GetUniformfv(FGLSLProg.Handle, FParameterID, @Result);
  442. end;
  443. function TGLSLShaderParameter.GetAsVector3i: TVector3i;
  444. begin
  445. gl.GetUniformiv(FGLSLProg.Handle, FParameterID, @Result);
  446. end;
  447. function TGLSLShaderParameter.GetAsVector4f: TGLVector;
  448. begin
  449. gl.GetUniformfv(FGLSLProg.Handle, FParameterID, @Result);
  450. end;
  451. function TGLSLShaderParameter.GetAsVector4i: TVector4i;
  452. begin
  453. gl.GetUniformiv(FGLSLProg.Handle, FParameterID, @Result);
  454. end;
  455. procedure TGLSLShaderParameter.SetAsCustomTexture(
  456. const TextureIndex: Integer; TextureTarget: TGLTextureTarget;
  457. const Value: Cardinal);
  458. begin
  459. CurrentGLContext.GLStates.TextureBinding[TextureIndex, TextureTarget] := Value;
  460. gl.Uniform1i(FParameterID, TextureIndex);
  461. end;
  462. procedure TGLSLShaderParameter.SetAsMatrix2f(const Value: TMatrix2f);
  463. begin
  464. gl.UniformMatrix2fv(FParameterID, 1, False, @Value);
  465. end;
  466. procedure TGLSLShaderParameter.SetAsMatrix3f(const Value: TMatrix3f);
  467. begin
  468. gl.UniformMatrix3fv(FParameterID, 1, False, @Value);
  469. end;
  470. procedure TGLSLShaderParameter.SetAsMatrix4f(const Value: TMatrix4f);
  471. begin
  472. gl.UniformMatrix4fv(FParameterID, 1, False, @Value);
  473. end;
  474. procedure TGLSLShaderParameter.SetAsVector1f(const Value: Single);
  475. begin
  476. gl.Uniform1f(FParameterID, Value);
  477. end;
  478. procedure TGLSLShaderParameter.SetAsVector1i(const Value: Integer);
  479. begin
  480. gl.Uniform1i(FParameterID, Value);
  481. end;
  482. procedure TGLSLShaderParameter.SetAsVector2f(const Value: TVector2f);
  483. begin
  484. gl.Uniform2f(FParameterID, Value.X, Value.Y);
  485. end;
  486. procedure TGLSLShaderParameter.SetAsVector2i(const Value: TVector2i);
  487. begin
  488. gl.Uniform2i(FParameterID, Value.X, Value.Y);
  489. end;
  490. procedure TGLSLShaderParameter.SetAsVector3f(const Value: TVector3f);
  491. begin
  492. gl.Uniform3f(FParameterID, Value.X, Value.Y, Value.Z);
  493. end;
  494. procedure TGLSLShaderParameter.SetAsVector3i(const Value: TVector3i);
  495. begin
  496. gl.Uniform3i(FParameterID, Value.X, Value.Y, Value.Z);
  497. end;
  498. procedure TGLSLShaderParameter.SetAsVector4f(const Value: TVector4f);
  499. begin
  500. gl.Uniform4f(FParameterID, Value.X, Value.Y, Value.Z, Value.W);
  501. end;
  502. procedure TGLSLShaderParameter.SetAsVector4i(const Value: TVector4i);
  503. begin
  504. gl.Uniform4i(FParameterID, Value.X, Value.Y, Value.Z, Value.W);
  505. end;
  506. function TGLSLShaderParameter.GetAsUniformBuffer: Cardinal;
  507. begin
  508. gl.GetUniformiv(FGLSLProg.Handle, FParameterID, @Result);
  509. end;
  510. function TGLSLShaderParameter.GetAsVector1ui: Cardinal;
  511. begin
  512. gl.GetUniformuiv(FGLSLProg.Handle, FParameterID, @Result);
  513. end;
  514. procedure TGLSLShaderParameter.SetAsVector1ui(const Value: Cardinal);
  515. begin
  516. gl.Uniform1ui(FParameterID, Value);
  517. end;
  518. function TGLSLShaderParameter.GetAsVector2ui: TVector2ui;
  519. begin
  520. gl.GetUniformiv(FGLSLProg.Handle, FParameterID, @Result);
  521. end;
  522. procedure TGLSLShaderParameter.SetAsVector2ui(const Value: TVector2ui);
  523. begin
  524. gl.Uniform2ui(FParameterID, Value.X, Value.Y);
  525. end;
  526. function TGLSLShaderParameter.GetAsVector3ui: TVector3ui;
  527. begin
  528. gl.GetUniformiv(FGLSLProg.Handle, FParameterID, @Result);
  529. end;
  530. procedure TGLSLShaderParameter.SetAsVector3ui(const Value: TVector3ui);
  531. begin
  532. gl.Uniform3ui(FParameterID, Value.X, Value.Y, Value.Z);
  533. end;
  534. function TGLSLShaderParameter.GetAsVector4ui: TVector4ui;
  535. begin
  536. gl.GetUniformiv(FGLSLProg.Handle, FParameterID, @Result);
  537. end;
  538. procedure TGLSLShaderParameter.SetAsVector4ui(const Value: TVector4ui);
  539. begin
  540. gl.Uniform4ui(FParameterID, Value.X, Value.Y, Value.Z, Value.W);
  541. end;
  542. procedure TGLSLShaderParameter.SetAsUniformBuffer(UBO: Cardinal);
  543. begin
  544. CurrentGLContext.GLStates.UniformBufferBinding := UBO;
  545. gl.UniformBuffer(FGLSLProg.Handle, FParameterID, UBO);
  546. end;
  547. //--------------------------------------------------
  548. initialization
  549. //--------------------------------------------------
  550. RegisterClasses([TGLCustomGLSLShader, TGLSLShader]);
  551. end.