GXSL.ShaderParameter.pas 9.7 KB

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