FMxShaderUniformEditor.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // The graphics engine GXScene https://github.com/glscene
  3. //
  4. unit FMxShaderUniformEditor;
  5. interface
  6. uses
  7. System.SysUtils,
  8. System.Types,
  9. System.UITypes,
  10. System.Classes,
  11. System.Variants,
  12. FMX.Types,
  13. FMX.Controls,
  14. FMX.Forms,
  15. FMX.Graphics,
  16. FMX.Dialogs,
  17. FMX.Layouts,
  18. FMX.ListBox,
  19. FMX.StdCtrls,
  20. FMX.Controls.Presentation,
  21. GXSL.Parameter,
  22. Stage.TextureFormat,
  23. Stage.VectorGeometry,
  24. Stage.Strings;
  25. type
  26. TShaderUniformEditorForm = class(TForm)
  27. Label1: TLabel;
  28. LBUniforms: TListBox;
  29. Label2: TLabel;
  30. AutoSetBox: TComboBox;
  31. SamplerBox: TComboBox;
  32. RedGroup: TGroupBox;
  33. GreenGroup: TGroupBox;
  34. BlueGroup: TGroupBox;
  35. AlphaGroup: TGroupBox;
  36. Label3: TLabel;
  37. Label4: TLabel;
  38. TextureBox: TComboBox;
  39. Label5: TLabel;
  40. RadioButton1: TRadioButton;
  41. RadioButton2: TRadioButton;
  42. RadioButton3: TRadioButton;
  43. RadioButton4: TRadioButton;
  44. RadioButton5: TRadioButton;
  45. RadioButton6: TRadioButton;
  46. RadioButton7: TRadioButton;
  47. RadioButton8: TRadioButton;
  48. RadioButton9: TRadioButton;
  49. RadioButton10: TRadioButton;
  50. RadioButton11: TRadioButton;
  51. RadioButton12: TRadioButton;
  52. RadioButton13: TRadioButton;
  53. RadioButton14: TRadioButton;
  54. RadioButton15: TRadioButton;
  55. RadioButton16: TRadioButton;
  56. RadioButton17: TRadioButton;
  57. RadioButton18: TRadioButton;
  58. RadioButton19: TRadioButton;
  59. RadioButton20: TRadioButton;
  60. RadioButton21: TRadioButton;
  61. RadioButton22: TRadioButton;
  62. RadioButton23: TRadioButton;
  63. RadioButton24: TRadioButton;
  64. procedure FormDestroy(Sender: TObject);
  65. procedure LBUniformsClick(Sender: TObject);
  66. procedure ColorGroupClick(Sender: TObject);
  67. procedure AutoSetBoxChange(Sender: TObject);
  68. procedure TextureBoxChange(Sender: TObject);
  69. procedure SamplerBoxChange(Sender: TObject);
  70. procedure LBUniformsKeyDown(Sender: TObject; var Key: Word;
  71. var KeyChar: Char; Shift: TShiftState);
  72. private
  73. FUniformList: array of IgxShaderParameter;
  74. public
  75. procedure Clear;
  76. procedure AddTextureName(const S: string);
  77. procedure AddSamplerName(const S: string);
  78. procedure AddUniform(AValue: IgxShaderParameter);
  79. procedure Execute;
  80. end;
  81. function ShaderUniformEditorForm: TShaderUniformEditorForm;
  82. procedure ReleaseShaderUniformEditor;
  83. //==================================================================
  84. implementation
  85. //==================================================================
  86. {$R *.fmx}
  87. var
  88. vShaderUniformEditor: TShaderUniformEditorForm;
  89. function ShaderUniformEditorForm: TShaderUniformEditorForm;
  90. begin
  91. if not Assigned(vShaderUniformEditor) then
  92. vShaderUniformEditor := TShaderUniformEditorForm.Create(nil);
  93. Result := vShaderUniformEditor;
  94. end;
  95. procedure ReleaseShaderUniformEditor;
  96. begin
  97. if Assigned(vShaderUniformEditor) then
  98. begin
  99. vShaderUniformEditor.Free;
  100. vShaderUniformEditor := nil;
  101. end;
  102. end;
  103. //-----------------------------------------------------------
  104. // TShaderUniformEditor
  105. //-----------------------------------------------------------
  106. procedure TShaderUniformEditorForm.AddUniform(AValue: IgxShaderParameter);
  107. begin
  108. if AValue <> nil then
  109. begin
  110. SetLength(FUniformList, Length(FUniformList)+1);
  111. FUniformList[High(FUniformList)] := AValue;
  112. end;
  113. end;
  114. procedure TShaderUniformEditorForm.AutoSetBoxChange(Sender: TObject);
  115. begin
  116. if LBUniforms.ItemIndex >= 0 then
  117. begin
  118. FUniformList[LBUniforms.ItemIndex].AutoSetMethod := AutoSetBox.Items[AutoSetBox.ItemIndex];
  119. end;
  120. end;
  121. procedure TShaderUniformEditorForm.Clear;
  122. var
  123. I: Integer;
  124. begin
  125. for I := 0 to High(FUniformList) do
  126. FUniformList[I] := nil;
  127. SetLength(FUniformList, 0);
  128. LBUniforms.Items.Clear;
  129. LBUniforms.ItemIndex := -1;
  130. AutoSetBox.Items.Clear;
  131. TextureBox.Items.Clear;
  132. SamplerBox.Items.Clear;
  133. AutoSetBox.Items.Add(strNothing);
  134. TextureBox.Items.Add(strNothing);
  135. SamplerBox.Items.Add(strNothing);
  136. AutoSetBox.ItemIndex := 0;
  137. TextureBox.ItemIndex := 0;
  138. SamplerBox.ItemIndex := 0;
  139. RedGroup.Index := -1;
  140. GreenGroup.Index := -1;
  141. BlueGroup.Index := -1;
  142. AlphaGroup.Index := -1;
  143. end;
  144. procedure TShaderUniformEditorForm.Execute;
  145. var
  146. I: Integer;
  147. str: AnsiString;
  148. begin
  149. for I := 0 to High(FUniformList) do
  150. begin
  151. if FUniformList[I].GLSLType <> GLSLTypeUndefined then
  152. str := cGLXLTypeString[FUniformList[I].GLSLType];
  153. if FUniformList[I].GLSLSamplerType <> GLSLSamplerUndefined then
  154. str := cGLXLSamplerString[FUniformList[I].GLSLSamplerType];
  155. LBUniforms.Items.Add(FUniformList[I].Name+': '+string(str));
  156. end;
  157. ShowModal;
  158. end;
  159. procedure TShaderUniformEditorForm.FormDestroy(Sender: TObject);
  160. begin
  161. FUniformList := nil;
  162. end;
  163. procedure TShaderUniformEditorForm.LBUniformsClick(Sender: TObject);
  164. var
  165. SV: TglSwizzleVector;
  166. IParam: IgxShaderParameter;
  167. begin
  168. if LBUniforms.ItemIndex >= 0 then
  169. begin
  170. AutoSetBox.Items.Clear;
  171. AutoSetBox.Items.Add(strNothing);
  172. IParam := FUniformList[LBUniforms.ItemIndex];
  173. if IParam.GLSLSamplerType <> GLSLSamplerUndefined then
  174. begin
  175. FillUniformAutoSetMethodList(AutoSetBox.Items, IParam.GLSLSamplerType);
  176. AutoSetBox.ItemIndex :=
  177. MaxInteger(AutoSetBox.Items.IndexOf(IParam.AutoSetMethod), 0);
  178. TextureBox.Enabled := True;
  179. SamplerBox.Enabled := True;
  180. TextureBox.ItemIndex :=
  181. MaxInteger(TextureBox.Items.IndexOf(IParam.TextureName), 0);
  182. SamplerBox.ItemIndex :=
  183. MaxInteger(SamplerBox.Items.IndexOf(IParam.SamplerName), 0);
  184. SV := IParam.GetTextureSwizzle;
  185. RedGroup.Index := Ord(SV[0]);
  186. GreenGroup.Index := Ord(SV[1]);
  187. BlueGroup.Index := Ord(SV[2]);
  188. AlphaGroup.Index := Ord(SV[3]);
  189. end
  190. else
  191. begin
  192. TextureBox.Enabled := False;
  193. SamplerBox.Enabled := False;
  194. FillUniformAutoSetMethodList(AutoSetBox.Items, IParam.GLSLType);
  195. AutoSetBox.ItemIndex :=
  196. MaxInteger(AutoSetBox.Items.IndexOf(IParam.AutoSetMethod), 0);
  197. RedGroup.Index := -1;
  198. GreenGroup.Index := -1;
  199. BlueGroup.Index := -1;
  200. AlphaGroup.Index := -1;
  201. end;
  202. end;
  203. end;
  204. procedure TShaderUniformEditorForm.LBUniformsKeyDown(Sender: TObject; var Key: Word;
  205. var KeyChar: Char; Shift: TShiftState);
  206. begin
  207. LBUniformsClick(Self);
  208. end;
  209. procedure TShaderUniformEditorForm.SamplerBoxChange(Sender: TObject);
  210. begin
  211. if LBUniforms.ItemIndex >= 0 then
  212. begin
  213. FUniformList[LBUniforms.ItemIndex].SamplerName :=
  214. SamplerBox.Items[SamplerBox.ItemIndex];
  215. end;
  216. end;
  217. procedure TShaderUniformEditorForm.TextureBoxChange(Sender: TObject);
  218. begin
  219. if LBUniforms.ItemIndex >= 0 then
  220. begin
  221. FUniformList[LBUniforms.ItemIndex].TextureName :=
  222. TextureBox.Items[TextureBox.ItemIndex];
  223. end;
  224. end;
  225. procedure TShaderUniformEditorForm.ColorGroupClick(Sender: TObject);
  226. var
  227. SV: TglSwizzleVector;
  228. begin
  229. if LBUniforms.ItemIndex >= 0 then
  230. begin
  231. if FUniformList[LBUniforms.ItemIndex].GLSLSamplerType = GLSLSamplerUndefined then
  232. exit;
  233. SV := FUniformList[LBUniforms.ItemIndex].GetTextureSwizzle;
  234. SV[TGroupBox(Sender).Tag] := TglTextureSwizzle(TGroupBox(Sender).Index);
  235. FUniformList[LBUniforms.ItemIndex].SetTextureSwizzle(SV);
  236. end;
  237. end;
  238. procedure TShaderUniformEditorForm.AddTextureName(const S: string);
  239. begin
  240. TextureBox.Items.Add(S);
  241. end;
  242. procedure TShaderUniformEditorForm.AddSamplerName(const S: string);
  243. begin
  244. SamplerBox.Items.Add(S);
  245. end;
  246. initialization //-----------------------------------------------------------
  247. finalization
  248. ReleaseShaderUniformEditor;
  249. end.