GLS.CUDA.PropEditors.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //
  2. // The graphics engine GLXEngine. The unit of GLScene for Delphi
  3. //
  4. unit GLS.CUDA.PropEditors;
  5. interface
  6. {$I Stage.Defines.inc}
  7. uses
  8. System.Classes,
  9. System.SysUtils,
  10. ToolsAPI,
  11. StrEdit,
  12. DesignEditors,
  13. DesignIntf,
  14. GLS.CUDA.APIComps,
  15. GLS.CUDA.Compiler,
  16. GLS.CUDA.Context,
  17. FmCUDAEditor;
  18. type
  19. TGLCUDAEditor = class(TComponentEditor)
  20. public
  21. procedure Edit; override;
  22. procedure ExecuteVerb(Index: Integer); override;
  23. function GetVerb(Index: Integer): string; override;
  24. function GetVerbCount: Integer; override;
  25. end;
  26. TGLCUDACompilerEditor = class(TComponentEditor)
  27. public
  28. procedure Edit; override;
  29. procedure ExecuteVerb(Index: Integer); override;
  30. function GetVerb(Index: Integer): string; override;
  31. function GetVerbCount: Integer; override;
  32. end;
  33. TGLCUDACompilerSourceProperty = class(TStringProperty)
  34. private
  35. FModuleList: TStringList;
  36. procedure RefreshModuleList;
  37. public
  38. constructor Create(const ADesigner: IDesigner; APropCount: Integer); override;
  39. destructor Destroy; override;
  40. function GetAttributes: TPropertyAttributes; override;
  41. procedure GetValues(Proc: TGetStrProc); override;
  42. procedure SetValue(const Value: String); override;
  43. end;
  44. TGLCUDADeviceProperty = class(TStringProperty)
  45. private
  46. FDeviceList: TStringList;
  47. public
  48. constructor Create(const ADesigner: IDesigner; APropCount: Integer); override;
  49. destructor Destroy; override;
  50. function GetAttributes: TPropertyAttributes; override;
  51. procedure GetValues(Proc: TGetStrProc); override;
  52. procedure SetValue(const Value: String); override;
  53. end;
  54. function FindCuFile(var AModuleName: string): Boolean;
  55. implementation //-------------------------------------------------------------
  56. uses
  57. CUDA.Parser,
  58. CUDA.RunTime;
  59. function FindCuFile(var AModuleName: string): Boolean;
  60. var
  61. proj: IOTAProject;
  62. I: Integer;
  63. LModule: IOTAModuleInfo;
  64. LName: string;
  65. begin
  66. proj := GetActiveProject;
  67. if proj <> nil then
  68. begin
  69. for I := 0 to proj.GetModuleCount - 1 do
  70. begin
  71. LModule := proj.GetModule(I);
  72. LName := ExtractFileName(LModule.FileName);
  73. if LName = AModuleName then
  74. begin
  75. AModuleName := LModule.FileName;
  76. exit(True);
  77. end;
  78. end;
  79. end;
  80. Result := False;
  81. end;
  82. // ------------------
  83. // ------------------ TGLCUDAEditor ------------------
  84. // ------------------
  85. procedure TGLCUDAEditor.Edit;
  86. begin
  87. with GLCUDAEditorForm do
  88. begin
  89. SetCUDAEditorClient(TGLCUDA(Self.Component), Self.Designer);
  90. Show;
  91. end;
  92. end;
  93. procedure TGLCUDAEditor.ExecuteVerb(Index: Integer);
  94. begin
  95. case Index of
  96. 0: Edit;
  97. end;
  98. end;
  99. function TGLCUDAEditor.GetVerb(Index: Integer): string;
  100. begin
  101. case Index of
  102. 0: Result := 'Show CUDA Items Editor';
  103. end;
  104. end;
  105. function TGLCUDAEditor.GetVerbCount: Integer;
  106. begin
  107. Result := 1;
  108. end;
  109. // ------------------
  110. // ------------------ TGLCUDACompilerEditor ------------------
  111. // ------------------
  112. procedure TGLCUDACompilerEditor.Edit;
  113. var
  114. CUDACompiler: TGLCUDACompiler;
  115. I, J: Integer;
  116. func: TCUDAFunction;
  117. tex: TCUDATexture;
  118. cnst: TCUDAConstant;
  119. param: TCUDAFuncParam;
  120. parent: TCUDAModule;
  121. info: TCUDAModuleInfo;
  122. bUseless: Boolean;
  123. useless: array of TCUDAComponent;
  124. CTN: TChannelTypeAndNum;
  125. procedure CreateFuncParams;
  126. var
  127. K: Integer;
  128. begin
  129. for K := 0 to High(info.Func[I].Args) do
  130. begin
  131. param := TCUDAFuncParam(Designer.CreateComponent(TCUDAFuncParam,
  132. func, 0, 0, 0, 0));
  133. param.Master := TCUDAComponent(func);
  134. param.KernelName := info.Func[I].Args[K].Name;
  135. param.Name := func.KernelName+'_'+param.KernelName;
  136. param.DataType := info.Func[I].Args[K].DataType;
  137. param.CustomType := info.Func[I].Args[K].CustomType;
  138. param.Reference := info.Func[I].Args[K].Ref;
  139. end;
  140. end;
  141. begin
  142. CUDACompiler := TGLCUDACompiler(Self.Component);
  143. if CUDACompiler.Compile then
  144. begin
  145. info := CUDACompiler.ModuleInfo;
  146. parent := TCUDAModule(info.Owner);
  147. // Creates kernel's functions
  148. for I := 0 to High(info.Func) do
  149. begin
  150. func := parent.KernelFunction[info.Func[I].KernelName];
  151. if not Assigned(func) then
  152. begin
  153. func := TCUDAFunction(Designer.CreateComponent(TCUDAFunction,
  154. info.Owner, 0, 0, 0, 0));
  155. func.Master := TCUDAComponent(info.Owner);
  156. func.KernelName := info.Func[I].KernelName;
  157. func.Name := TCUDAComponent(info.Owner).MakeUniqueName(info.Func[I].Name);
  158. end
  159. else
  160. begin
  161. // for old parameters
  162. while func.ItemsCount > 0 do
  163. func.Items[0].Destroy;
  164. end;
  165. try
  166. bUseless := func.Handle = nil;
  167. except
  168. bUseless := True;
  169. end;
  170. if bUseless then
  171. begin
  172. Designer.SelectComponent(func);
  173. Designer.DeleteSelection(True);
  174. func := nil;
  175. end
  176. else
  177. CreateFuncParams;
  178. end;
  179. // Creates kernel's textures
  180. for I := 0 to High(info.TexRef) do
  181. begin
  182. tex := parent.KernelTexture[info.TexRef[I].Name];
  183. if not Assigned(tex) then
  184. begin
  185. tex := TCUDATexture(Designer.CreateComponent(TCUDATexture,
  186. info.Owner, 0, 0, 0, 0));
  187. tex.Master := TCUDAComponent(info.Owner);
  188. tex.KernelName := info.TexRef[I].Name;
  189. tex.Name := tex.KernelName;
  190. tex.ReadAsInteger := (info.TexRef[I].ReadMode = cudaReadModeElementType);
  191. CTN := GetChannelTypeAndNum(info.TexRef[I].DataType);
  192. tex.Format := CTN.F;
  193. end;
  194. tex.ChannelNum := CTN.C;
  195. try
  196. bUseless := tex.Handle = nil;
  197. except
  198. bUseless := True;
  199. end;
  200. if bUseless then
  201. begin
  202. Designer.SelectComponent(tex);
  203. Designer.DeleteSelection(True);
  204. end;
  205. end;
  206. // Creates kernel's constants
  207. for I := 0 to High(info.Constant) do
  208. begin
  209. cnst := parent.KernelConstant[info.Constant[I].Name];
  210. if not Assigned(cnst) then
  211. begin
  212. cnst := TCUDAConstant(Designer.CreateComponent(TCUDAConstant,
  213. info.Owner, 0, 0, 0, 0));
  214. cnst.Master := TCUDAComponent(info.Owner);
  215. cnst.KernelName := info.Constant[I].Name;
  216. cnst.Name := cnst.KernelName;
  217. cnst.DataType := info.Constant[I].DataType;
  218. cnst.CustomType := info.Constant[I].CustomType;
  219. cnst.IsValueDefined := info.Constant[I].DefValue;
  220. end;
  221. try
  222. bUseless := cnst.DeviceAddress = nil;
  223. except
  224. bUseless := True;
  225. end;
  226. if bUseless then
  227. begin
  228. Designer.SelectComponent(cnst);
  229. Designer.DeleteSelection(True);
  230. end;
  231. end;
  232. // Delete useless components
  233. SetLength(useless, parent.ItemsCount);
  234. j := 0;
  235. for i := 0 to parent.ItemsCount - 1 do
  236. begin
  237. if not TCUDAComponent(parent.Items[i]).IsAllocated then
  238. begin
  239. useless[j] := parent.Items[i];
  240. inc(j);
  241. end;
  242. end;
  243. for i := 0 to j - 1 do
  244. useless[i].Destroy;
  245. end;
  246. Designer.Modified;
  247. end;
  248. procedure TGLCUDACompilerEditor.ExecuteVerb(Index: Integer);
  249. begin
  250. case Index of
  251. 0: Edit;
  252. end;
  253. end;
  254. function TGLCUDACompilerEditor.GetVerb(Index: Integer): string;
  255. begin
  256. case Index of
  257. 0: Result := 'Compile Module';
  258. end;
  259. end;
  260. function TGLCUDACompilerEditor.GetVerbCount: Integer;
  261. begin
  262. Result := 1;
  263. end;
  264. // ------------------
  265. // ------------------ TGLCUDACompilerSourceProperty ------------------
  266. // ------------------
  267. constructor TGLCUDACompilerSourceProperty.Create(
  268. const ADesigner: IDesigner; APropCount: Integer);
  269. begin
  270. inherited;
  271. FModuleList := TStringList.Create;
  272. end;
  273. destructor TGLCUDACompilerSourceProperty.Destroy;
  274. begin
  275. FModuleList.Destroy;
  276. inherited;
  277. end;
  278. procedure TGLCUDACompilerSourceProperty.RefreshModuleList;
  279. var
  280. proj: IOTAProject;
  281. I: Integer;
  282. LModule: IOTAModuleInfo;
  283. LName: string;
  284. begin
  285. FModuleList.Clear;
  286. FModuleList.Add('none');
  287. proj := GetActiveProject;
  288. if proj <> nil then
  289. begin
  290. for I := 0 to proj.GetModuleCount - 1 do
  291. begin
  292. LModule := proj.GetModule(I);
  293. LName := UpperCase(ExtractFileExt(LModule.FileName));
  294. if LName = '.CU' then
  295. FModuleList.Add(LModule.FileName);
  296. end;
  297. end;
  298. end;
  299. function TGLCUDACompilerSourceProperty.GetAttributes;
  300. begin
  301. Result := [paValueList];
  302. end;
  303. procedure TGLCUDACompilerSourceProperty.GetValues(Proc: TGetStrProc);
  304. var
  305. I : Integer;
  306. begin
  307. RefreshModuleList;
  308. for I := 0 to FModuleList.Count - 1 do
  309. Proc(ExtractFileName(FModuleList[I]));
  310. end;
  311. procedure TGLCUDACompilerSourceProperty.SetValue(const Value: String);
  312. var
  313. I, J: Integer;
  314. begin
  315. RefreshModuleList;
  316. J := -1;
  317. for I := 1 to FModuleList.Count - 1 do
  318. if Value = ExtractFileName(FModuleList[I]) then
  319. begin
  320. J := I;
  321. Break;
  322. end;
  323. if J > 0 then
  324. begin
  325. TGLCUDACompiler(GetComponent(0)).SetSourceCodeFile(FModuleList[J]);
  326. SetStrValue(ExtractFileName(Value));
  327. end
  328. else
  329. begin
  330. SetStrValue('none');
  331. end;
  332. Modified;
  333. end;
  334. // ------------------
  335. // ------------------ TGLCUDADeviceProperty ------------------
  336. // ------------------
  337. constructor TGLCUDADeviceProperty.Create(const ADesigner: IDesigner; APropCount: Integer);
  338. begin
  339. inherited;
  340. FDeviceList := TStringList.Create;
  341. end;
  342. destructor TGLCUDADeviceProperty.Destroy;
  343. begin
  344. FDeviceList.Destroy;
  345. inherited;
  346. end;
  347. function TGLCUDADeviceProperty.GetAttributes: TPropertyAttributes;
  348. begin
  349. Result := [paValueList];
  350. end;
  351. procedure TGLCUDADeviceProperty.GetValues(Proc: TGetStrProc);
  352. begin
  353. CUDAContextManager.FillUnusedDeviceList(FDeviceList);
  354. end;
  355. procedure TGLCUDADeviceProperty.SetValue(const Value: String);
  356. var
  357. I: Integer;
  358. begin
  359. for I := 0 to FDeviceList.Count - 1 do
  360. if Value = FDeviceList[I] then
  361. begin
  362. SetStrValue(Value);
  363. Break;
  364. end;
  365. Modified;
  366. end;
  367. end.