GLSL.ShaderParameter.pas 9.7 KB

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