GXSL.Parameter.pas 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXSL.Parameter;
  5. interface
  6. {$I Stage.Defines.inc}
  7. {$M-}
  8. uses
  9. Winapi.OpenGL,
  10. Winapi.OpenGLext,
  11. System.Classes,
  12. Stage.VectorTypes,
  13. Stage.TextureFormat,
  14. GXS.RenderContextInfo;
  15. type
  16. TgxSLDataType = (
  17. GLSLTypeUndefined,
  18. GLSLType1F,
  19. GLSLType2F,
  20. GLSLType3F,
  21. GLSLType4F,
  22. GLSLType1I,
  23. GLSLType2I,
  24. GLSLType3I,
  25. GLSLType4I,
  26. GLSLType1UI,
  27. GLSLType2UI,
  28. GLSLType3UI,
  29. GLSLType4UI,
  30. GLSLTypeMat2F,
  31. GLSLTypeMat3F,
  32. GLSLTypeMat4F,
  33. GLSLTypeVoid);
  34. TgxSLSamplerType = (
  35. GLSLSamplerUndefined,
  36. GLSLSampler1D,
  37. GLSLSampler2D,
  38. GLSLSampler3D,
  39. GLSLSamplerCube,
  40. GLSLSampler1DShadow,
  41. GLSLSampler2DShadow,
  42. GLSLSampler1DArray,
  43. GLSLSampler2DArray,
  44. GLSLSampler1DArrayShadow,
  45. GLSLSampler2DArrayShadow,
  46. GLSLSamplerCubeShadow,
  47. GLSLIntSampler1D,
  48. GLSLIntSampler2D,
  49. GLSLIntSampler3D,
  50. GLSLIntSamplerCube,
  51. GLSLIntSampler1DArray,
  52. GLSLIntSampler2DArray,
  53. GLSLUIntSampler1D,
  54. GLSLUIntSampler2D,
  55. GLSLUIntSampler3D,
  56. GLSLUIntSamplerCube,
  57. GLSLUIntSampler1DArray,
  58. GLSLUIntSampler2DArray,
  59. GLSLSamplerRect,
  60. GLSLSamplerRectShadow,
  61. GLSLSamplerBuffer,
  62. GLSLIntSamplerRect,
  63. GLSLIntSamplerBuffer,
  64. GLSLUIntSamplerRect,
  65. GLSLUIntSamplerBuffer,
  66. GLSLSamplerMS,
  67. GLSLIntSamplerMS,
  68. GLSLUIntSamplerMS,
  69. GLSLSamplerMSArray,
  70. GLSLIntSamplerMSArray,
  71. GLSLUIntSamplerMSArray
  72. );
  73. TgxgsInTypes = (
  74. gsInPoints,
  75. gsInLines,
  76. gsInAdjLines,
  77. gsInTriangles,
  78. gsInAdjTriangles
  79. );
  80. TgxgsOutTypes = (
  81. gsOutPoints,
  82. gsOutLineStrip,
  83. sOutTriangleStrip
  84. );
  85. IgxShaderParameter = interface(IInterface)
  86. function GetName: string;
  87. function GetGLSLType: TgxSLDataType;
  88. function GetGLSLSamplerType: TgxSLSamplerType;
  89. function GetAutoSetMethod: string;
  90. function GetTextureName: string;
  91. function GetSamplerName: string;
  92. function GetTextureSwizzle: TglSwizzleVector;
  93. procedure SetTextureName(const AValue: string);
  94. procedure SetSamplerName(const AValue: string);
  95. procedure SetAutoSetMethod(const AValue: string);
  96. procedure SetTextureSwizzle(const AValue: TglSwizzleVector);
  97. function GetFloat: Single;
  98. function GetVec2: TVector2f;
  99. function GetVec3: TVector3f;
  100. function GetVec4: TVector4f;
  101. function GetInt: Integer;
  102. function GetIVec2: TVector2i;
  103. function GetIVec3: TVector3i;
  104. function GetIVec4: TVector4i;
  105. function GetUInt: Cardinal;
  106. function GetUVec2: TVector2ui;
  107. function GetUVec3: TVector3ui;
  108. function GetUVec4: TVector4ui;
  109. procedure SetFloat(const Value: Single);
  110. procedure SetVec2(const Value: TVector2f);
  111. procedure SetVec3(const Value: TVector3f);
  112. procedure SetVec4(const Value: TVector4f);
  113. procedure SetInt(const Value: Integer);
  114. procedure SetIVec2(const Value: TVector2i);
  115. procedure SetIVec3(const Value: TVector3i);
  116. procedure SetIVec4(const Value: TVector4i);
  117. procedure SetUInt(const Value: Cardinal);
  118. procedure SetUVec2(const Value: TVector2ui);
  119. procedure SetUVec3(const Value: TVector3ui);
  120. procedure SetUVec4(const Value: TVector4ui);
  121. function GetMat2: TMatrix2f;
  122. function GetMat3: TMatrix3f;
  123. function GetMat4: TMatrix4f;
  124. procedure SetMat2(const Value: TMatrix2f);
  125. procedure SetMat3(const Value: TMatrix3f);
  126. procedure SetMat4(const Value: TMatrix4f);
  127. procedure SetFloatArray(const Values: PGLFloat; Count: Integer);
  128. procedure SetIntArray(const Values: PGLInt; Count: Integer);
  129. procedure SetUIntArray(const Values: PGLUInt; Count: Integer);
  130. property Name: string read GetName;
  131. property GLSLType: TgxSLDataType read GetGLSLType;
  132. property GLSLSamplerType: TgxSLSamplerType read GetGLSLSamplerType;
  133. // Scalar types.
  134. property float: Single read GetFloat write SetFloat;
  135. property int: GLint read GetInt write SetInt;
  136. property uint: GLuint read GetUInt write SetUInt;
  137. // Float vector types.
  138. property vec2: TVector2f read GetVec2 write SetVec2;
  139. property vec3: TVector3f read GetVec3 write SetVec3;
  140. property vec4: TVector4f read GetVec4 write SetVec4;
  141. // Integer vector types.
  142. property ivec2: TVector2i read GetIVec2 write SetIVec2;
  143. property ivec3: TVector3i read GetIVec3 write SetIVec3;
  144. property ivec4: TVector4i read GetIVec4 write SetIVec4;
  145. // Unsigned integer vector types.
  146. property uvec2: TVector2ui read GetUVec2 write SetUVec2;
  147. property uvec3: TVector3ui read GetUVec3 write SetUVec3;
  148. property uvec4: TVector4ui read GetUVec4 write SetUVec4;
  149. // Matrix Types.
  150. property mat2: TMatrix2f read GetMat2 write SetMat2;
  151. property mat3: TMatrix3f read GetMat3 write SetMat3;
  152. property mat4: TMatrix4f read GetMat4 write SetMat4;
  153. // Bindings.
  154. property AutoSetMethod: string read GetAutoSetMethod write SetAutoSetMethod;
  155. property TextureName: string read GetTextureName write SetTextureName;
  156. property SamplerName: string read GetSamplerName write SetSamplerName;
  157. property TextureSwizzle: TglSwizzleVector read GetTextureSwizzle write SetTextureSwizzle;
  158. end;
  159. const
  160. cGLXLTypeString: array[TgxSLDataType] of AnsiString = (
  161. 'undefined',
  162. 'float',
  163. 'vec2',
  164. 'vec3',
  165. 'vec4',
  166. 'int',
  167. 'ivec2',
  168. 'ivec3',
  169. 'ivec4',
  170. 'uint',
  171. 'uivec2',
  172. 'uivec3',
  173. 'uivec4',
  174. 'mat2',
  175. 'mat3',
  176. 'mat4',
  177. 'void');
  178. cGLXLSamplerString: array[TgxSLSamplerType] of AnsiString = (
  179. 'undefined',
  180. 'sampler1D',
  181. 'sampler2D',
  182. 'sampler3D',
  183. 'samplerCube',
  184. 'sampler1DShadow',
  185. 'sampler2DShadow',
  186. 'sampler1DArray',
  187. 'sampler2DArray',
  188. 'sampler1DArrayShadow',
  189. 'sampler2DArrayShadow',
  190. 'samplerCubeShadow',
  191. 'isampler1D',
  192. 'isampler2D',
  193. 'isampler3D',
  194. 'isamplerCube',
  195. 'isampler1DArray',
  196. 'isampler2DArray',
  197. 'usampler1D',
  198. 'usampler2D',
  199. 'usampler3D',
  200. 'usamplerCube',
  201. 'usampler1DArray',
  202. 'usampler2DArray',
  203. 'samplerRect',
  204. 'samplerRectShadow',
  205. 'samplerBuffer',
  206. 'isamplerRect',
  207. 'isamplerBuffer',
  208. 'usamplerRect',
  209. 'usamplerBuffer',
  210. 'samplerMS',
  211. 'isamplerMS',
  212. 'usamplerMS',
  213. 'samplerMSArray',
  214. 'isamplerMSArray',
  215. 'usamplerMSArray');
  216. const
  217. cGXgsInTypes : array[TgxgsInTypes] of GLenum =
  218. (GL_POINTS, GL_LINES, GL_LINES_ADJACENCY_EXT, GL_TRIANGLES,
  219. GL_TRIANGLES_ADJACENCY_EXT);
  220. cGXgsOutTypes: array[TgxgsOutTypes] of GLenum =
  221. (GL_POINTS, GL_LINE_STRIP, GL_TRIANGLE_STRIP);
  222. type
  223. TUniformAutoSetMethod = procedure(Sender: IgxShaderParameter; var ARci: TgxRenderContextInfo) of object;
  224. function GLXLTypeEnum(AType: TgxSLDataType): GLEnum;
  225. function GLXLTypeComponentCount(AType: TgxSLDataType): Integer;
  226. procedure RegisterUniformAutoSetMethod(AMethodName: string;
  227. AType: TgxSLDataType; AMethod: TUniformAutoSetMethod);
  228. procedure FillUniformAutoSetMethodList(AList: TStrings;
  229. TypeFilter: TgxSLDataType); overload;
  230. procedure FillUniformAutoSetMethodList(AList: TStrings;
  231. TypeFilter: TgxSLSamplerType); overload;
  232. function GetUniformAutoSetMethod(AMethodName: string): TUniformAutoSetMethod;
  233. function GetUniformAutoSetMethodName(AMethod: TUniformAutoSetMethod): string;
  234. //---------------------------------------------------------------------------
  235. implementation
  236. //---------------------------------------------------------------------------
  237. const
  238. cGLXLTypeComponents: array[TgxSLDataType] of Integer =
  239. (
  240. 0,
  241. 1,
  242. 2,
  243. 3,
  244. 4,
  245. 1,
  246. 2,
  247. 3,
  248. 4,
  249. 1,
  250. 2,
  251. 3,
  252. 4,
  253. 4,
  254. 9,
  255. 16,
  256. 0
  257. );
  258. cGLXLTypeEnum: array[TgxSLDataType] of Integer =
  259. (
  260. 0,
  261. GL_FLOAT,
  262. GL_FLOAT,
  263. GL_FLOAT,
  264. GL_FLOAT,
  265. GL_INT,
  266. GL_INT,
  267. GL_INT,
  268. GL_INT,
  269. GL_UNSIGNED_INT,
  270. GL_UNSIGNED_INT,
  271. GL_UNSIGNED_INT,
  272. GL_UNSIGNED_INT,
  273. GL_FLOAT,
  274. GL_FLOAT,
  275. GL_FLOAT,
  276. 0
  277. );
  278. type
  279. TAutoSetMethodRec = record
  280. Name: string;
  281. UniformType: TgxSLDataType;
  282. SamplerType: TgxSLSamplerType;
  283. Method: TUniformAutoSetMethod;
  284. end;
  285. var
  286. vMethods: array of TAutoSetMethodRec;
  287. function GLXLTypeEnum(AType: TgxSLDataType): GLEnum;
  288. begin
  289. Result := cGLXLTypeEnum[AType];
  290. end;
  291. function GLXLTypeComponentCount(AType: TgxSLDataType): Integer;
  292. begin
  293. Result := cGLXLTypeComponents[AType];
  294. end;
  295. procedure RegisterUniformAutoSetMethod(AMethodName: string;
  296. AType: TgxSLDataType; AMethod: TUniformAutoSetMethod);
  297. var
  298. I: Integer;
  299. begin
  300. for I := 0 to High(vMethods) do
  301. if vMethods[I].Name = AMethodName then
  302. begin
  303. vMethods[I].UniformType := AType;
  304. vMethods[I].Method := AMethod;
  305. exit;
  306. end;
  307. I := Length(vMethods);
  308. SetLength(vMethods, I+1);
  309. vMethods[I].Name := AMethodName;
  310. vMethods[I].UniformType := AType;
  311. vMethods[I].SamplerType := GLSLSamplerUndefined;
  312. vMethods[I].Method := AMethod;
  313. end;
  314. procedure FillUniformAutoSetMethodList(AList: TStrings; TypeFilter: TgxSLDataType);
  315. var
  316. I: Integer;
  317. begin
  318. for I := 0 to High(vMethods) do
  319. if vMethods[I].UniformType = TypeFilter then
  320. AList.Add(vMethods[I].Name);
  321. end;
  322. procedure FillUniformAutoSetMethodList(AList: TStrings; TypeFilter: TgxSLSamplerType);
  323. var
  324. I: Integer;
  325. begin
  326. for I := 0 to High(vMethods) do
  327. if vMethods[I].SamplerType = TypeFilter then
  328. AList.Add(vMethods[I].Name);
  329. end;
  330. function GetUniformAutoSetMethod(AMethodName: string): TUniformAutoSetMethod;
  331. var
  332. I: Integer;
  333. begin
  334. for I := 0 to High(vMethods) do
  335. if vMethods[I].Name = AMethodName then
  336. begin
  337. Result := vMethods[I].Method;
  338. exit;
  339. end;
  340. Result := nil;
  341. end;
  342. function GetUniformAutoSetMethodName(AMethod: TUniformAutoSetMethod): string;
  343. var
  344. I: Integer;
  345. begin
  346. for I := 0 to High(vMethods) do
  347. if @vMethods[I].Method = @AMethod then
  348. begin
  349. Result := vMethods[I].Name;
  350. exit;
  351. end;
  352. Result := '';
  353. end;
  354. end.