GXS.ParallelRegister.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. //
  2. // The graphics GaLaXy Engine. The unit of GXScene
  3. //
  4. unit GXS.ParallelRegister;
  5. (* Registration unit for GPU Computing package *)
  6. interface
  7. uses
  8. System.Classes,
  9. System.SysUtils,
  10. DesignIntf,
  11. DesignEditors,
  12. {
  13. ToolsAPI,
  14. STREDIT,
  15. }
  16. Stage.Strings;
  17. procedure Register;
  18. type
  19. TCUDAEditor = 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. TCUDACompilerEditor = 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. TCUDACompilerSourceProperty = 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. TCUDADeviceProperty = 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. //======================================
  55. implementation
  56. //======================================
  57. uses
  58. FxCUDAEditor,
  59. CUDAx.RunTime,
  60. CUDAx.Parser,
  61. CUDAx.Context,
  62. CUDAx.API,
  63. CUDAx.Compiler,
  64. CUDAx.FFTPlan,
  65. CUDAx.Graphics;
  66. function FindCuFile(var AModuleName: string): Boolean;
  67. var
  68. proj: IOTAProject;
  69. I: Integer;
  70. LModule: IOTAModuleInfo;
  71. LName: string;
  72. begin
  73. proj := GetActiveProject;
  74. if proj <> nil then
  75. begin
  76. for I := 0 to proj.GetModuleCount - 1 do
  77. begin
  78. LModule := proj.GetModule(I);
  79. LName := ExtractFileName(LModule.FileName);
  80. if LName = AModuleName then
  81. begin
  82. AModuleName := LModule.FileName;
  83. exit(True);
  84. end;
  85. end;
  86. end;
  87. Result := False;
  88. end;
  89. // ------------------
  90. // ------------------ TCUDAEditor ------------------
  91. // ------------------
  92. procedure TCUDAEditor.Edit;
  93. begin
  94. with CUDAEditorForm do
  95. begin
  96. SetCUDAEditorClient(TCUDA(Self.Component), Self.Designer);
  97. Show;
  98. end;
  99. end;
  100. procedure TCUDAEditor.ExecuteVerb(Index: Integer);
  101. begin
  102. case Index of
  103. 0: Edit;
  104. end;
  105. end;
  106. function TCUDAEditor.GetVerb(Index: Integer): string;
  107. begin
  108. case Index of
  109. 0: Result := 'Show CUDA Items Editor';
  110. end;
  111. end;
  112. function TCUDAEditor.GetVerbCount: Integer;
  113. begin
  114. Result := 1;
  115. end;
  116. // ------------------
  117. // ------------------ TCUDACompilerEditor ------------------
  118. // ------------------
  119. procedure TCUDACompilerEditor.Edit;
  120. var
  121. CUDACompiler: TCUDACompiler;
  122. I, J: Integer;
  123. func: TCUDAFunction;
  124. tex: TCUDATexture;
  125. cnst: TCUDAConstant;
  126. param: TCUDAFuncParam;
  127. parent: TCUDAModule;
  128. info: TCUDAModuleInfo;
  129. bUseless: Boolean;
  130. useless: array of TCUDAComponent;
  131. CTN: TChannelTypeAndNum;
  132. procedure CreateFuncParams;
  133. var
  134. K: Integer;
  135. begin
  136. for K := 0 to High(info.Func[I].Args) do
  137. begin
  138. param := TCUDAFuncParam(Designer.CreateComponent(TCUDAFuncParam,
  139. func, 0, 0, 0, 0));
  140. param.Master := TCUDAComponent(func);
  141. param.KernelName := info.Func[I].Args[K].Name;
  142. param.Name := func.KernelName+'_'+param.KernelName;
  143. param.DataType := info.Func[I].Args[K].DataType;
  144. param.CustomType := info.Func[I].Args[K].CustomType;
  145. param.Reference := info.Func[I].Args[K].Ref;
  146. end;
  147. end;
  148. begin
  149. CUDACompiler := TCUDACompiler(Self.Component);
  150. if CUDACompiler.Compile then
  151. begin
  152. info := CUDACompiler.ModuleInfo;
  153. parent := TCUDAModule(info.Owner);
  154. // Create kernel's functions
  155. for I := 0 to High(info.Func) do
  156. begin
  157. func := parent.KernelFunction[info.Func[I].KernelName];
  158. if not Assigned(func) then
  159. begin
  160. func := TCUDAFunction(Designer.CreateComponent(TCUDAFunction,
  161. info.Owner, 0, 0, 0, 0));
  162. func.Master := TCUDAComponent(info.Owner);
  163. func.KernelName := info.Func[I].KernelName;
  164. func.Name := TCUDAComponent(info.Owner).MakeUniqueName(info.Func[I].Name);
  165. end
  166. else
  167. begin
  168. // destroy old parameters
  169. while func.ItemsCount > 0 do
  170. func.Items[0].Destroy;
  171. end;
  172. try
  173. bUseless := func.Handle = nil;
  174. except
  175. bUseless := True;
  176. end;
  177. if bUseless then
  178. begin
  179. Designer.SelectComponent(func);
  180. Designer.DeleteSelection(True);
  181. func := nil;
  182. end
  183. else
  184. CreateFuncParams;
  185. end;
  186. // Create kernel's textures
  187. for I := 0 to High(info.TexRef) do
  188. begin
  189. tex := parent.KernelTexture[info.TexRef[I].Name];
  190. if not Assigned(tex) then
  191. begin
  192. tex := TCUDATexture(Designer.CreateComponent(TCUDATexture,
  193. info.Owner, 0, 0, 0, 0));
  194. tex.Master := TCUDAComponent(info.Owner);
  195. tex.KernelName := info.TexRef[I].Name;
  196. tex.Name := tex.KernelName;
  197. tex.ReadAsInteger := (info.TexRef[I].ReadMode = cudaReadModeElementType);
  198. CTN := GetChannelTypeAndNum(info.TexRef[I].DataType);
  199. tex.Format := CTN.F;
  200. end;
  201. tex.ChannelNum := CTN.C;
  202. try
  203. bUseless := tex.Handle = nil;
  204. except
  205. bUseless := True;
  206. end;
  207. if bUseless then
  208. begin
  209. Designer.SelectComponent(tex);
  210. Designer.DeleteSelection(True);
  211. end;
  212. end;
  213. // Create kernel's constants
  214. for I := 0 to High(info.Constant) do
  215. begin
  216. cnst := parent.KernelConstant[info.Constant[I].Name];
  217. if not Assigned(cnst) then
  218. begin
  219. cnst := TCUDAConstant(Designer.CreateComponent(TCUDAConstant,
  220. info.Owner, 0, 0, 0, 0));
  221. cnst.Master := TCUDAComponent(info.Owner);
  222. cnst.KernelName := info.Constant[I].Name;
  223. cnst.Name := cnst.KernelName;
  224. cnst.DataType := info.Constant[I].DataType;
  225. cnst.CustomType := info.Constant[I].CustomType;
  226. cnst.IsValueDefined := info.Constant[I].DefValue;
  227. end;
  228. try
  229. bUseless := cnst.DeviceAddress = nil;
  230. except
  231. bUseless := True;
  232. end;
  233. if bUseless then
  234. begin
  235. Designer.SelectComponent(cnst);
  236. Designer.DeleteSelection(True);
  237. end;
  238. end;
  239. // Delete useless components
  240. SetLength(useless, parent.ItemsCount);
  241. j := 0;
  242. for i := 0 to parent.ItemsCount - 1 do
  243. begin
  244. if not TCUDAComponent(parent.Items[i]).IsAllocated then
  245. begin
  246. useless[j] := parent.Items[i];
  247. inc(j);
  248. end;
  249. end;
  250. for i := 0 to j - 1 do
  251. useless[i].Destroy;
  252. end;
  253. Designer.Modified;
  254. end;
  255. procedure TCUDACompilerEditor.ExecuteVerb(Index: Integer);
  256. begin
  257. case Index of
  258. 0: Edit;
  259. end;
  260. end;
  261. function TCUDACompilerEditor.GetVerb(Index: Integer): string;
  262. begin
  263. case Index of
  264. 0: Result := 'Compile Module';
  265. end;
  266. end;
  267. function TCUDACompilerEditor.GetVerbCount: Integer;
  268. begin
  269. Result := 1;
  270. end;
  271. // ------------------
  272. // ------------------ TCUDACompilerSourceProperty ------------------
  273. // ------------------
  274. constructor TCUDACompilerSourceProperty.Create(
  275. const ADesigner: IDesigner; APropCount: Integer);
  276. begin
  277. inherited;
  278. FModuleList := TStringList.Create;
  279. end;
  280. destructor TCUDACompilerSourceProperty.Destroy;
  281. begin
  282. FModuleList.Destroy;
  283. inherited;
  284. end;
  285. procedure TCUDACompilerSourceProperty.RefreshModuleList;
  286. var
  287. proj: IOTAProject;
  288. I: Integer;
  289. LModule: IOTAModuleInfo;
  290. LName: string;
  291. begin
  292. FModuleList.Clear;
  293. FModuleList.Add('none');
  294. proj := GetActiveProject;
  295. if proj <> nil then
  296. begin
  297. for I := 0 to proj.GetModuleCount - 1 do
  298. begin
  299. LModule := proj.GetModule(I);
  300. LName := UpperCase(ExtractFileExt(LModule.FileName));
  301. if LName = '.CU' then
  302. FModuleList.Add(LModule.FileName);
  303. end;
  304. end;
  305. end;
  306. function TCUDACompilerSourceProperty.GetAttributes;
  307. begin
  308. Result := [paValueList];
  309. end;
  310. procedure TCUDACompilerSourceProperty.GetValues(Proc: TGetStrProc);
  311. var
  312. I : Integer;
  313. begin
  314. RefreshModuleList;
  315. for I := 0 to FModuleList.Count - 1 do
  316. Proc(ExtractFileName(FModuleList[I]));
  317. end;
  318. procedure TCUDACompilerSourceProperty.SetValue(const Value: String);
  319. var
  320. I, J: Integer;
  321. begin
  322. RefreshModuleList;
  323. J := -1;
  324. for I := 1 to FModuleList.Count - 1 do
  325. if Value = ExtractFileName(FModuleList[I]) then
  326. begin
  327. J := I;
  328. Break;
  329. end;
  330. if J > 0 then
  331. begin
  332. TCUDACompiler(GetComponent(0)).SetSourceCodeFile(FModuleList[J]);
  333. SetStrValue(ExtractFileName(Value));
  334. end
  335. else
  336. begin
  337. SetStrValue('none');
  338. end;
  339. Modified;
  340. end;
  341. // ------------------
  342. // ------------------ TCUDADeviceProperty ------------------
  343. // ------------------
  344. constructor TCUDADeviceProperty.Create(const ADesigner: IDesigner; APropCount: Integer);
  345. begin
  346. inherited;
  347. FDeviceList := TStringList.Create;
  348. end;
  349. destructor TCUDADeviceProperty.Destroy;
  350. begin
  351. FDeviceList.Destroy;
  352. inherited;
  353. end;
  354. function TCUDADeviceProperty.GetAttributes: TPropertyAttributes;
  355. begin
  356. Result := [paValueList];
  357. end;
  358. procedure TCUDADeviceProperty.GetValues(Proc: TGetStrProc);
  359. begin
  360. CUDAContextManager.FillUnusedDeviceList(FDeviceList);
  361. end;
  362. procedure TCUDADeviceProperty.SetValue(const Value: String);
  363. var
  364. I: Integer;
  365. begin
  366. for I := 0 to FDeviceList.Count - 1 do
  367. if Value = FDeviceList[I] then
  368. begin
  369. SetStrValue(Value);
  370. Break;
  371. end;
  372. Modified;
  373. end;
  374. procedure Register;
  375. begin
  376. RegisterComponents('GPU Computing', [TCUDA, TCUDADevice, TCUDACompiler]);
  377. RegisterComponentEditor(TCUDA, TCUDAEditor);
  378. RegisterComponentEditor(TCUDACompiler, TCUDACompilerEditor);
  379. RegisterPropertyEditor(TypeInfo(string), TCUDACompiler, 'ProjectModule',
  380. TCUDACompilerSourceProperty);
  381. RegisterPropertyEditor(TypeInfo(string), TCUDADevice, 'SelectDevice',
  382. TCUDADeviceProperty);
  383. RegisterNoIcon([TCUDAModule, TCUDAMemData, TCUDAFunction, TCUDATexture,
  384. TCUDAFFTPlan, TCUDAImageResource, TCUDAGeometryResource, TCUDAConstant, TCUDAFuncParam]);
  385. ObjectManager.RegisterSceneObject(TgxFeedBackMesh, 'GPU generated mesh', 'GPU Computing', HInstance);
  386. end;
  387. //-------------------------------------
  388. initialization
  389. //-------------------------------------
  390. vFindCuFileFunc := FindCuFile;
  391. end.