GLSL.AsmShader.pas 7.4 KB

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