PageRenderTime 30ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/adinetz/dpvm-x/dpvmWrapper/AmuABI.cs

#
C# | 547 lines | 351 code | 53 blank | 143 comment | 23 complexity | ab26c411808df90a7146b90524d5c63c MD5 | raw file
Possible License(s): AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. namespace dpvmWrapper
  5. {
  6. /// <summary>
  7. /// Handle for sampler constant in shader
  8. /// </summary>
  9. public class sampler
  10. {
  11. }
  12. /// <summary>
  13. /// The class providing interface to the constants in the DPVM program
  14. /// </summary>
  15. public class AmuABI
  16. {
  17. /// <summary>
  18. /// Maximum number of user constants
  19. /// </summary>
  20. const int MaxUserConsts = 256;
  21. /// <summary>
  22. /// Maximum number of literal constants
  23. /// </summary>
  24. const int MaxLiteralConsts = 256;
  25. /// <summary>
  26. /// Maximum number of user samplers
  27. /// </summary>
  28. const int MaxSamplerNames = 16;
  29. /// <summary>
  30. /// Maximum length of constant or sampler names
  31. /// </summary>
  32. const int MaxFixedStringLength = 256;
  33. /// <summary>
  34. /// ABI datatypes for constants
  35. /// </summary>
  36. internal enum DataType
  37. {
  38. /// <summary>
  39. /// Undefined
  40. /// </summary>
  41. UNDEF,
  42. /// <summary>
  43. /// 32bit boolean (stored as unsigned int)
  44. /// </summary>
  45. BOOL32,
  46. /// <summary>
  47. /// 32bit integer
  48. /// </summary>
  49. INT32,
  50. /// <summary>
  51. /// 32bit floating point
  52. /// </summary>
  53. FLOAT32,
  54. /// <summary>
  55. /// Sampler
  56. /// </summary>
  57. SAMPLER,
  58. /// <summary>
  59. /// Number of datatypes
  60. /// </summary>
  61. MAXTYPES = 255
  62. }
  63. /// <summary>
  64. /// Static dictionary that converts ABI datatypes to standart types
  65. /// </summary>
  66. static Dictionary<AmuABI.DataType, Type> m_typeAmu;
  67. static AmuABI()
  68. {
  69. m_typeAmu = new Dictionary<AmuABI.DataType, Type>();
  70. m_typeAmu.Add(AmuABI.DataType.BOOL32, typeof(bool));
  71. m_typeAmu.Add(AmuABI.DataType.INT32, typeof(int));
  72. m_typeAmu.Add(AmuABI.DataType.FLOAT32, typeof(float));
  73. m_typeAmu.Add(AmuABI.DataType.SAMPLER, typeof(sampler));
  74. }
  75. /// <summary>
  76. /// ABI Program Info
  77. /// </summary>
  78. struct ProgInfo
  79. {
  80. uint RESERVED_00;
  81. uint FG_DEPTH_SRC;
  82. uint US_CONFIG_FUDO;
  83. uint US_PIXSIZE_FUDO;
  84. uint US_FC_CTRL;
  85. uint US_CODE_ADDR;
  86. uint US_CODE_RANGE;
  87. uint RESERVED_07;
  88. uint RESERVED_08;
  89. uint RESERVED_09;
  90. uint RESERVED_10;
  91. uint RESERVED_11;
  92. uint RESERVED_12;
  93. uint RESERVED_13;
  94. uint RESERVED_14;
  95. uint RESERVED_15;
  96. uint RESERVED_16;
  97. uint RESERVED_17;
  98. uint RESERVED_18;
  99. uint RESERVED_19;
  100. uint RESERVED_20;
  101. uint RESERVED_21;
  102. uint RESERVED_22;
  103. uint RESERVED_23;
  104. uint RESERVED_24;
  105. }
  106. /// <summary>
  107. /// ABI Program Info Note Section
  108. /// </summary>
  109. struct ProgInfoNote
  110. {
  111. uint namesz;
  112. uint descsz;
  113. uint type;
  114. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  115. byte[] name;
  116. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  117. ProgInfo[] progInfo;
  118. }
  119. /// <summary>
  120. /// ABI Inputs Note Section
  121. /// </summary>
  122. struct InputsNote
  123. {
  124. uint namesz;
  125. uint descsz;
  126. uint type;
  127. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  128. byte[] name;
  129. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  130. uint[] inputs;
  131. }
  132. /// <summary>
  133. /// ABI Outputs Note Section
  134. /// </summary>
  135. struct OutputsNote
  136. {
  137. uint namesz;
  138. uint descsz;
  139. uint type;
  140. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  141. byte[] name;
  142. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  143. uint[] outputs;
  144. }
  145. /// <summary>
  146. /// ABI Conditional Outputs Note Section
  147. /// </summary>
  148. struct CondOutNote
  149. {
  150. uint namesz;
  151. uint descsz;
  152. uint type;
  153. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  154. byte[] name;
  155. uint condOut;
  156. }
  157. /// <summary>
  158. /// ABI Float32 Constants Note Section
  159. /// </summary>
  160. struct Float32ConstsNote
  161. {
  162. uint namesz;
  163. uint descsz;
  164. uint type;
  165. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  166. byte[] name;
  167. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  168. uint[] float32Consts;
  169. }
  170. /// <summary>
  171. /// ABI Int32 Contants Note Section
  172. /// </summary>
  173. struct Int32ConstsNote
  174. {
  175. uint namesz;
  176. uint descsz;
  177. uint type;
  178. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  179. byte[] name;
  180. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  181. uint[] int32Consts;
  182. }
  183. /// <summary>
  184. /// ABI Bool32 Contants Note Section
  185. /// </summary>
  186. struct Bool32ConstsNote
  187. {
  188. uint namesz;
  189. uint descsz;
  190. uint type;
  191. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  192. byte[] name;
  193. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  194. uint[] bool32Consts;
  195. }
  196. /// <summary>
  197. /// ABI Constants Data Section
  198. /// </summary>
  199. struct ConstsData
  200. {
  201. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256 * 4)]
  202. float[] float32;
  203. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32 * 4)]
  204. uint[] int32;
  205. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
  206. uint[] bool32;
  207. }
  208. /// <summary>
  209. /// ABI Early Exit Note Section
  210. /// </summary>
  211. struct EarlyExitNote
  212. {
  213. uint namesz;
  214. uint descsz;
  215. uint type;
  216. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  217. byte[] name;
  218. uint earlyExit;
  219. }
  220. /// <summary>
  221. /// ABI Symbol Table Entry
  222. /// </summary>
  223. struct Symbol
  224. {
  225. uint name;
  226. uint value;
  227. uint size;
  228. byte info;
  229. byte other;
  230. byte section;
  231. }
  232. /// <summary>
  233. /// ABI Fixed Length String
  234. /// </summary>
  235. struct FixedString
  236. {
  237. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxFixedStringLength)]
  238. internal byte[] str; ///< fixed length NULL-terminated string
  239. }
  240. /// <summary>
  241. /// ABI Constant Value Storage
  242. /// </summary>
  243. struct DataValue
  244. {
  245. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  246. float[] float32;
  247. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  248. uint[] int32;
  249. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  250. uint[] bool32;
  251. }
  252. /// <summary>
  253. /// ABI Literal Constant
  254. /// </summary>
  255. struct LiteralConst
  256. {
  257. uint addr;
  258. DataType type;
  259. DataValue value;
  260. }
  261. /// <summary>
  262. /// ABI User Constant
  263. /// </summary>
  264. struct UserConst
  265. {
  266. internal uint addr;
  267. internal DataType type;
  268. internal FixedString name;
  269. }
  270. /// <summary>
  271. /// ABI Constants Info Struct for Extraction Interface
  272. /// </summary>
  273. struct ConstsInfo
  274. {
  275. /// <summary>
  276. /// Number of literal constants
  277. /// </summary>
  278. internal uint litConstsCount;
  279. /// <summary>
  280. /// Literal constants
  281. /// </summary>
  282. internal LiteralConst[] litConsts;
  283. /// <summary>
  284. /// Number of user constants
  285. /// </summary>
  286. internal uint userConstsCount;
  287. /// <summary>
  288. /// User constants
  289. /// </summary>
  290. internal UserConst[] userConsts;
  291. }
  292. /// <summary>
  293. /// ABI ELF Info Struct for Extraction Interface
  294. /// </summary>
  295. [StructLayout(LayoutKind.Sequential)]
  296. struct ElfInfo
  297. {
  298. IntPtr progInfoNote; ///< program info note
  299. IntPtr inputsNote; ///< inputs note
  300. IntPtr outputsNote; ///< outputs note
  301. IntPtr condOutNote; ///< conditional output note
  302. IntPtr earlyExitNote; ///< early exit note
  303. IntPtr float32ConstsNote; ///< float32 constants note
  304. IntPtr int32ConstsNote; ///< int32 constants note
  305. IntPtr bool32ConstsNote; ///< bool32 constants note
  306. string text; ///< text section (program instructions)
  307. uint textSize; ///< size of text section (in bytes)
  308. IntPtr constsData; ///< constants data section
  309. IntPtr symtab; ///< symbol table section
  310. uint symtabSize; ///< size of symbol table section (in bytes)
  311. string strtab; ///< string table section
  312. uint strtabSize; ///< size of string table section (in bytes)
  313. }
  314. [DllImport("_amuABI.dll")]
  315. static extern uint amuABICreateExecutable(
  316. uint inputCount, IntPtr inputs,
  317. uint outputCount, IntPtr outputs,
  318. uint condOut, uint earlyExit,
  319. uint userConstsCount, ref UserConst userConsts,
  320. uint litConstsCount, ref LiteralConst litConsts,
  321. ProgInfo progInfo, uint textSize, IntPtr textData,
  322. IntPtr binarySize, IntPtr binaryData);
  323. [DllImport("_amuABI.dll")]
  324. static extern ElfInfo _amuABIExtractReferences(IntPtr binary);
  325. [DllImport("_amuABI.dll")]
  326. static extern void _amuABISetLiteralConstants(ConstsInfo compOutput,
  327. IntPtr floatConstAddressCPU,
  328. IntPtr intConstAddressCPU,
  329. IntPtr boolConstAddressCPU);
  330. [DllImport("_amuABI.dll")]
  331. static extern void _amuABISetUserConstant(ConstsInfo compOutput,
  332. IntPtr addressCPU,
  333. string name,
  334. IntPtr value,
  335. DataType type);
  336. [DllImport("_amuABI.dll")]
  337. static extern void _amuABIExtractConstants(
  338. ref uint litConstsCount,
  339. out IntPtr plitConsts,
  340. out uint litOffset,
  341. ref uint userConstsCount,
  342. out IntPtr puserConsts,
  343. out uint userOffser,
  344. IntPtr binary);
  345. /// <summary>
  346. /// Class that represents constants as pair (name, type)
  347. /// </summary>
  348. struct stringType
  349. {
  350. string m_str;
  351. Type m_type;
  352. /// <summary>
  353. /// Creates const pair by passing name and type
  354. /// </summary>
  355. /// <param name="str"> Name of constant </param>
  356. /// <param name="type"> Type of constant </param>
  357. public stringType(string str, Type type)
  358. {
  359. m_str = str;
  360. m_type = type;
  361. }
  362. }
  363. /// <summary>
  364. /// List of constants (user and literal)
  365. /// </summary>
  366. ConstsInfo m_consts = new AmuABI.ConstsInfo();
  367. /// <summary>
  368. /// Dictionary for getting const index in list by pair (name, type)
  369. /// </summary>
  370. Dictionary<stringType,int> m_constIndexByName = new Dictionary<stringType, int>();
  371. /// <summary>
  372. /// Creates string from array of bytes (ANSI chars)
  373. /// </summary>
  374. /// <param name="str"> Array of bytes </param>
  375. /// <returns> String that represent this array </returns>
  376. string toString(byte[] str)
  377. {
  378. string cur = "";
  379. int i = 0;
  380. while (str[i] != '\0')
  381. {
  382. cur += (char)str[i];
  383. i++;
  384. }
  385. return cur;
  386. }
  387. /// <summary>
  388. /// Set user constant of specified type in the constant buffer by passing name of constant and value
  389. /// </summary>
  390. /// <typeparam name="T"> Standard type of constant </typeparam>
  391. /// <param name="constbuf"> Constant buffer that contain this constant </param>
  392. /// <param name="name"> Name of constant </param>
  393. /// <param name="value"> Vector of values of specified type represents constant value </param>
  394. public void setUserConst<T>(ConstBuffer<T> constbuf, string name, T[] value)
  395. {
  396. stringType st = new stringType(name, typeof(T));
  397. if (!m_constIndexByName.ContainsKey(st))
  398. throw new NotFoundConstException("not found const '" + name + "' of type " + typeof(T));
  399. int u = m_constIndexByName[st];
  400. if (typeof(T) == typeof(float))
  401. {
  402. unsafe
  403. {
  404. float* c = &((float*)constbuf.addrCPU)[4 * m_consts.userConsts[u].addr];
  405. c[0] = (float)(object)value[0];
  406. c[1] = (float)(object)value[1];
  407. c[2] = (float)(object)value[2];
  408. c[3] = (float)(object)value[3];
  409. }
  410. }
  411. else if (typeof(T) == typeof(int))
  412. {
  413. unsafe
  414. {
  415. byte* c = &((byte*)constbuf.addrCPU)[4 * m_consts.userConsts[u].addr];
  416. c[0] = (byte)(int)(object)value[0];
  417. c[1] = (byte)(int)(object)value[1];
  418. c[2] = (byte)(int)(object)value[2];
  419. c[3] = (byte)(int)(object)value[3];
  420. }
  421. }
  422. else if (typeof(T) == typeof(bool))
  423. {
  424. throw new InvalidConstException("unsupported type bool");
  425. }
  426. }
  427. /// <summary>
  428. /// Get constant value of specified type by passing constant buffer and name of constant
  429. /// </summary>
  430. /// <typeparam name="T"> Standard type of constant </typeparam>
  431. /// <param name="constbuf"> Constant buffer that contain this constant </param>
  432. /// <param name="name"> Name of constant </param>
  433. /// <returns> Vector of values of specified type represents constant value </returns>
  434. public T[] getUserConst<T>(ConstBuffer<T> constbuf, string name)
  435. {
  436. stringType st = new stringType(name, typeof(T));
  437. if (!m_constIndexByName.ContainsKey(st))
  438. throw new InvalidConstException("not found const '" + name + "' of type " + typeof(T));
  439. int u = m_constIndexByName[st];
  440. if (typeof(T) == typeof(float))
  441. {
  442. unsafe
  443. {
  444. float* c = &((float*)constbuf.addrCPU)[4 * m_consts.userConsts[u].addr];
  445. T[] value = new T[4];
  446. value[0] = (T)(object)c[0];
  447. value[1] = (T)(object)c[1];
  448. value[2] = (T)(object)c[2];
  449. value[3] = (T)(object)c[3];
  450. return value;
  451. }
  452. }
  453. else if (typeof(T) == typeof(int))
  454. {
  455. throw new InvalidConstException("unsupported type int");
  456. }
  457. else if (typeof(T) == typeof(bool))
  458. {
  459. throw new InvalidConstException("unsupported type bool");
  460. }
  461. return null;
  462. }
  463. /// <summary>
  464. /// Extract constants info from binary
  465. /// </summary>
  466. /// <param name="comp"> Binary DPVM program that created by AMU compiler </param>
  467. public void extractConstants(IntPtr data)
  468. {
  469. IntPtr plitConsts, puserConsts;
  470. uint litOffset, userOffset;
  471. _amuABIExtractConstants(ref m_consts.litConstsCount, out plitConsts, out litOffset,
  472. ref m_consts.userConstsCount, out puserConsts, out userOffset, data);
  473. m_consts.litConsts = new AmuABI.LiteralConst[m_consts.litConstsCount];
  474. for (int i = 0; i < m_consts.litConstsCount; i++)
  475. {
  476. m_consts.litConsts[i] = (AmuABI.LiteralConst)Marshal.PtrToStructure(plitConsts, typeof(AmuABI.LiteralConst));
  477. plitConsts = (IntPtr)((uint)plitConsts + litOffset);
  478. }
  479. m_consts.userConsts = new AmuABI.UserConst[m_consts.userConstsCount];
  480. for (int i = 0; i < m_consts.userConstsCount; i++)
  481. {
  482. m_consts.userConsts[i] = (AmuABI.UserConst)Marshal.PtrToStructure(puserConsts, typeof(AmuABI.UserConst));
  483. puserConsts = (IntPtr)((uint)puserConsts + userOffset);
  484. }
  485. // fill dictionary (name-type,index)
  486. for (int u = 0; u < m_consts.userConstsCount; u++)
  487. {
  488. string s = toString(m_consts.userConsts[u].name.str);
  489. Type t = m_typeAmu[m_consts.userConsts[u].type];
  490. m_constIndexByName.Add(new stringType(s,t), u);
  491. }
  492. }
  493. }
  494. }