GLSL.AsmShader.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLSL.AsmShader;
  5. (*
  6. TGLAsmShader is a wrapper for all ARB shaders
  7. This component is only a template and has to be replaced with a
  8. proper version by someone who uses ARB shaders.
  9. *)
  10. interface
  11. {$I GLScene.inc}
  12. uses
  13. System.Classes,
  14. System.SysUtils,
  15. GLS.OpenGLTokens,
  16. GLS.VectorGeometry,
  17. GLS.VectorTypes,
  18. GLS.Texture,
  19. GLS.Context,
  20. GLSL.CustomShader,
  21. GLS.RenderContextInfo;
  22. type
  23. TGLCustomAsmShader = class;
  24. TGLAsmShaderEvent = procedure(Shader: TGLCustomAsmShader) of object;
  25. TGLAsmShaderUnUplyEvent = procedure(Shader: TGLCustomAsmShader; var ThereAreMorePasses: Boolean) of object;
  26. TGLAsmShaderParameter = class(TGLCustomShaderParameter)
  27. private
  28. protected
  29. {
  30. function GetAsVector1f: Single; override;
  31. function GetAsVector1i: Integer; override;
  32. function GetAsVector2f: TVector2f; override;
  33. function GetAsVector2i: TVector2i; override;
  34. function GetAsVector3f: TVector3f; override;
  35. function GetAsVector3i: TVector3i; override;
  36. function GetAsVector4f: TGLVector; override;
  37. function GetAsVector4i: TVector4i; override;
  38. procedure SetAsVector1f(const Value: Single); override;
  39. procedure SetAsVector1i(const Value: Integer); override;
  40. procedure SetAsVector2i(const Value: TVector2i); override;
  41. procedure SetAsVector3i(const Value: TVector3i); override;
  42. procedure SetAsVector4i(const Value: TVector4i); override;
  43. procedure SetAsVector2f(const Value: TVector2f); override;
  44. procedure SetAsVector3f(const Value: TVector3f); override;
  45. procedure SetAsVector4f(const Value: TVector4f); override;
  46. function GetAsMatrix2f: TMatrix2f; override;
  47. function GetAsMatrix3f: TMatrix3f; override;
  48. function GetAsMatrix4f: TMatrix4f; override;
  49. procedure SetAsMatrix2f(const Value: TMatrix2f); override;
  50. procedure SetAsMatrix3f(const Value: TMatrix3f); override;
  51. procedure SetAsMatrix4f(const Value: TMatrix4f); override;
  52. procedure SetAsTexture1D(const TextureIndex: Integer;
  53. const Value: TGLTexture);
  54. procedure SetAsTexture2D(const TextureIndex: Integer;
  55. const Value: TGLTexture);
  56. procedure SetAsTexture3D(const TextureIndex: Integer;
  57. const Value: TGLTexture);
  58. procedure SetAsTextureCube(const TextureIndex: Integer;
  59. const Value: TGLTexture);
  60. procedure SetAsTextureRect(const TextureIndex: Integer;
  61. const Value: TGLTexture);
  62. function GetAsCustomTexture(const TextureIndex: Integer;
  63. const TextureTarget: Word): Cardinal; override;
  64. procedure SetAsCustomTexture(const TextureIndex: Integer;
  65. const TextureTarget: Word; const Value: Cardinal); override;
  66. }
  67. end;
  68. TGLCustomAsmShader = class(TGLCustomShader)
  69. private
  70. FVPHandle: TGLARBVertexProgramHandle;
  71. FFPHandle: TGLARBFragmentProgramHandle;
  72. FGPHandle: TGLARBGeometryProgramHandle;
  73. FOnInitialize: TGLAsmShaderEvent;
  74. FOnApply: TGLAsmShaderEvent;
  75. FOnUnApply: TGLAsmShaderUnUplyEvent;
  76. protected
  77. procedure ApplyShaderPrograms;
  78. procedure UnApplyShaderPrograms;
  79. procedure DestroyARBPrograms; virtual;
  80. property OnApply: TGLAsmShaderEvent read FOnApply write FOnApply;
  81. property OnUnApply: TGLAsmShaderUnUplyEvent read FOnUnApply write FOnUnApply;
  82. property OnInitialize: TGLAsmShaderEvent read FOnInitialize write FOnInitialize;
  83. procedure DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject); override;
  84. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  85. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  86. procedure DoFinalize; override;
  87. public
  88. constructor Create(AOwner: TComponent); override;
  89. destructor Destroy; override;
  90. procedure Assign(Source: TPersistent); override;
  91. function ShaderSupported: Boolean; override;
  92. end;
  93. TGLAsmShader = class(TGLCustomAsmShader)
  94. published
  95. property FragmentProgram;
  96. property VertexProgram;
  97. property GeometryProgram;
  98. property OnApply;
  99. property OnUnApply;
  100. property OnInitialize;
  101. property ShaderStyle;
  102. property FailedInitAction;
  103. end;
  104. //--------------------------------------------------------------
  105. implementation
  106. //--------------------------------------------------------------
  107. { TGLCustomAsmShader }
  108. procedure TGLCustomAsmShader.DoFinalize;
  109. begin
  110. inherited;
  111. DestroyARBPrograms;
  112. end;
  113. procedure TGLCustomAsmShader.Assign(Source: TPersistent);
  114. begin
  115. inherited Assign(Source);
  116. if Source is TGLCustomAsmShader then
  117. begin
  118. // Nothing here ...yet
  119. end;
  120. end;
  121. constructor TGLCustomAsmShader.Create(AOwner: TComponent);
  122. begin
  123. inherited Create(AOwner);
  124. end;
  125. destructor TGLCustomAsmShader.Destroy;
  126. begin
  127. DestroyARBPrograms;
  128. inherited Destroy;
  129. end;
  130. procedure TGLCustomAsmShader.DestroyARBPrograms;
  131. begin
  132. FVPHandle.Free;
  133. FVPHandle := nil;
  134. FFPHandle.Free;
  135. FFPHandle := nil;
  136. FGPHandle.Free;
  137. FGPHandle := nil;
  138. end;
  139. procedure TGLCustomAsmShader.DoApply(var rci: TGLRenderContextInfo; Sender: TObject);
  140. begin
  141. ApplyShaderPrograms();
  142. if Assigned(FOnApply) then
  143. FOnApply(Self);
  144. end;
  145. procedure TGLCustomAsmShader.DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject);
  146. begin
  147. if not ShaderSupported then
  148. begin
  149. Enabled := False;
  150. HandleFailedInitialization;
  151. end
  152. else
  153. begin
  154. if VertexProgram.Enabled then
  155. begin
  156. if not Assigned(FVPHandle) then
  157. FVPHandle := TGLARBVertexProgramHandle.CreateAndAllocate;
  158. FVPHandle.LoadARBProgram(VertexProgram.Code.Text);
  159. VertexProgram.Enabled := FVPHandle.Ready;
  160. end;
  161. if FragmentProgram.Enabled then
  162. begin
  163. if not Assigned(FFPHandle) then
  164. FFPHandle := TGLARBFragmentProgramHandle.CreateAndAllocate;
  165. FFPHandle.LoadARBProgram(FragmentProgram.Code.Text);
  166. FragmentProgram.Enabled := FFPHandle.Ready;
  167. end;
  168. if GeometryProgram.Enabled then
  169. begin
  170. if not Assigned(FGPHandle) then
  171. FGPHandle := TGLARBGeometryProgramHandle.CreateAndAllocate;
  172. FGPHandle.LoadARBProgram(GeometryProgram.Code.Text);
  173. GeometryProgram.Enabled := FGPHandle.Ready;
  174. end;
  175. Enabled := (FragmentProgram.Enabled or VertexProgram.Enabled or GeometryProgram.Enabled);
  176. if Enabled then
  177. begin
  178. if Assigned(FOnInitialize) then
  179. FOnInitialize(Self)
  180. end;
  181. end;
  182. end;
  183. function TGLCustomAsmShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  184. begin
  185. if Assigned(FOnUnApply) then
  186. FOnUnApply(Self, Result)
  187. else
  188. Result := False;
  189. UnApplyShaderPrograms();
  190. end;
  191. function TGLCustomAsmShader.ShaderSupported: Boolean;
  192. begin
  193. Result :=
  194. (VertexProgram.Enabled and TGLARBVertexProgramHandle.IsSupported) or
  195. (FragmentProgram.Enabled and TGLARBFragmentProgramHandle.IsSupported) or
  196. (GeometryProgram.Enabled and TGLARBGeometryProgramHandle.IsSupported);
  197. end;
  198. procedure TGLCustomAsmShader.ApplyShaderPrograms;
  199. begin
  200. if VertexProgram.Enabled then
  201. begin
  202. FVPHandle.Enable;
  203. FVPHandle.Bind;
  204. end;
  205. if FragmentProgram.Enabled then
  206. begin
  207. FFPHandle.Enable;
  208. FFPHandle.Bind;
  209. end;
  210. if GeometryProgram.Enabled then
  211. begin
  212. FGPHandle.Enable;
  213. FGPHandle.Bind;
  214. end;
  215. end;
  216. procedure TGLCustomAsmShader.UnApplyShaderPrograms;
  217. begin
  218. if VertexProgram.Enabled then
  219. FVPHandle.Disable;
  220. if FragmentProgram.Enabled then
  221. FFPHandle.Disable;
  222. if GeometryProgram.Enabled then
  223. FGPHandle.Disable;
  224. end;
  225. //-----------------------------------------
  226. initialization
  227. //-----------------------------------------
  228. RegisterClasses([TGLCustomAsmShader, TGLAsmShader]);
  229. end.