/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
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
-
- namespace dpvmWrapper
- {
- /// <summary>
- /// Handle for sampler constant in shader
- /// </summary>
- public class sampler
- {
- }
-
- /// <summary>
- /// The class providing interface to the constants in the DPVM program
- /// </summary>
- public class AmuABI
- {
- /// <summary>
- /// Maximum number of user constants
- /// </summary>
- const int MaxUserConsts = 256;
-
- /// <summary>
- /// Maximum number of literal constants
- /// </summary>
- const int MaxLiteralConsts = 256;
-
- /// <summary>
- /// Maximum number of user samplers
- /// </summary>
- const int MaxSamplerNames = 16;
-
- /// <summary>
- /// Maximum length of constant or sampler names
- /// </summary>
- const int MaxFixedStringLength = 256;
-
- /// <summary>
- /// ABI datatypes for constants
- /// </summary>
- internal enum DataType
- {
- /// <summary>
- /// Undefined
- /// </summary>
- UNDEF,
-
- /// <summary>
- /// 32bit boolean (stored as unsigned int)
- /// </summary>
- BOOL32,
-
- /// <summary>
- /// 32bit integer
- /// </summary>
- INT32,
-
- /// <summary>
- /// 32bit floating point
- /// </summary>
- FLOAT32,
-
- /// <summary>
- /// Sampler
- /// </summary>
- SAMPLER,
-
- /// <summary>
- /// Number of datatypes
- /// </summary>
- MAXTYPES = 255
- }
-
- /// <summary>
- /// Static dictionary that converts ABI datatypes to standart types
- /// </summary>
- static Dictionary<AmuABI.DataType, Type> m_typeAmu;
-
- static AmuABI()
- {
- m_typeAmu = new Dictionary<AmuABI.DataType, Type>();
- m_typeAmu.Add(AmuABI.DataType.BOOL32, typeof(bool));
- m_typeAmu.Add(AmuABI.DataType.INT32, typeof(int));
- m_typeAmu.Add(AmuABI.DataType.FLOAT32, typeof(float));
- m_typeAmu.Add(AmuABI.DataType.SAMPLER, typeof(sampler));
- }
-
- /// <summary>
- /// ABI Program Info
- /// </summary>
- struct ProgInfo
- {
- uint RESERVED_00;
- uint FG_DEPTH_SRC;
- uint US_CONFIG_FUDO;
- uint US_PIXSIZE_FUDO;
- uint US_FC_CTRL;
- uint US_CODE_ADDR;
- uint US_CODE_RANGE;
- uint RESERVED_07;
- uint RESERVED_08;
- uint RESERVED_09;
- uint RESERVED_10;
- uint RESERVED_11;
- uint RESERVED_12;
- uint RESERVED_13;
- uint RESERVED_14;
- uint RESERVED_15;
- uint RESERVED_16;
- uint RESERVED_17;
- uint RESERVED_18;
- uint RESERVED_19;
- uint RESERVED_20;
- uint RESERVED_21;
- uint RESERVED_22;
- uint RESERVED_23;
- uint RESERVED_24;
- }
-
- /// <summary>
- /// ABI Program Info Note Section
- /// </summary>
- struct ProgInfoNote
- {
- uint namesz;
- uint descsz;
- uint type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- byte[] name;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
- ProgInfo[] progInfo;
- }
-
- /// <summary>
- /// ABI Inputs Note Section
- /// </summary>
- struct InputsNote
- {
- uint namesz;
- uint descsz;
- uint type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- byte[] name;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
- uint[] inputs;
- }
-
- /// <summary>
- /// ABI Outputs Note Section
- /// </summary>
- struct OutputsNote
- {
- uint namesz;
- uint descsz;
- uint type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- byte[] name;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
- uint[] outputs;
- }
-
- /// <summary>
- /// ABI Conditional Outputs Note Section
- /// </summary>
- struct CondOutNote
- {
- uint namesz;
- uint descsz;
- uint type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- byte[] name;
- uint condOut;
- }
-
- /// <summary>
- /// ABI Float32 Constants Note Section
- /// </summary>
- struct Float32ConstsNote
- {
- uint namesz;
- uint descsz;
- uint type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- byte[] name;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
- uint[] float32Consts;
- }
-
- /// <summary>
- /// ABI Int32 Contants Note Section
- /// </summary>
- struct Int32ConstsNote
- {
- uint namesz;
- uint descsz;
- uint type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- byte[] name;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
- uint[] int32Consts;
- }
-
- /// <summary>
- /// ABI Bool32 Contants Note Section
- /// </summary>
- struct Bool32ConstsNote
- {
- uint namesz;
- uint descsz;
- uint type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- byte[] name;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
- uint[] bool32Consts;
- }
-
- /// <summary>
- /// ABI Constants Data Section
- /// </summary>
- struct ConstsData
- {
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256 * 4)]
- float[] float32;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32 * 4)]
- uint[] int32;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
- uint[] bool32;
- }
-
- /// <summary>
- /// ABI Early Exit Note Section
- /// </summary>
- struct EarlyExitNote
- {
- uint namesz;
- uint descsz;
- uint type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- byte[] name;
- uint earlyExit;
- }
-
- /// <summary>
- /// ABI Symbol Table Entry
- /// </summary>
- struct Symbol
- {
- uint name;
- uint value;
- uint size;
- byte info;
- byte other;
- byte section;
- }
-
- /// <summary>
- /// ABI Fixed Length String
- /// </summary>
- struct FixedString
- {
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxFixedStringLength)]
- internal byte[] str; ///< fixed length NULL-terminated string
- }
-
- /// <summary>
- /// ABI Constant Value Storage
- /// </summary>
- struct DataValue
- {
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
- float[] float32;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
- uint[] int32;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
- uint[] bool32;
- }
-
- /// <summary>
- /// ABI Literal Constant
- /// </summary>
- struct LiteralConst
- {
- uint addr;
- DataType type;
- DataValue value;
- }
-
- /// <summary>
- /// ABI User Constant
- /// </summary>
- struct UserConst
- {
- internal uint addr;
- internal DataType type;
- internal FixedString name;
- }
-
- /// <summary>
- /// ABI Constants Info Struct for Extraction Interface
- /// </summary>
- struct ConstsInfo
- {
- /// <summary>
- /// Number of literal constants
- /// </summary>
- internal uint litConstsCount;
-
- /// <summary>
- /// Literal constants
- /// </summary>
- internal LiteralConst[] litConsts;
-
- /// <summary>
- /// Number of user constants
- /// </summary>
- internal uint userConstsCount;
-
- /// <summary>
- /// User constants
- /// </summary>
- internal UserConst[] userConsts;
- }
-
- /// <summary>
- /// ABI ELF Info Struct for Extraction Interface
- /// </summary>
- [StructLayout(LayoutKind.Sequential)]
- struct ElfInfo
- {
- IntPtr progInfoNote; ///< program info note
- IntPtr inputsNote; ///< inputs note
- IntPtr outputsNote; ///< outputs note
- IntPtr condOutNote; ///< conditional output note
- IntPtr earlyExitNote; ///< early exit note
- IntPtr float32ConstsNote; ///< float32 constants note
- IntPtr int32ConstsNote; ///< int32 constants note
- IntPtr bool32ConstsNote; ///< bool32 constants note
- string text; ///< text section (program instructions)
- uint textSize; ///< size of text section (in bytes)
- IntPtr constsData; ///< constants data section
- IntPtr symtab; ///< symbol table section
- uint symtabSize; ///< size of symbol table section (in bytes)
- string strtab; ///< string table section
- uint strtabSize; ///< size of string table section (in bytes)
- }
-
- [DllImport("_amuABI.dll")]
- static extern uint amuABICreateExecutable(
- uint inputCount, IntPtr inputs,
- uint outputCount, IntPtr outputs,
- uint condOut, uint earlyExit,
- uint userConstsCount, ref UserConst userConsts,
- uint litConstsCount, ref LiteralConst litConsts,
- ProgInfo progInfo, uint textSize, IntPtr textData,
- IntPtr binarySize, IntPtr binaryData);
-
- [DllImport("_amuABI.dll")]
- static extern ElfInfo _amuABIExtractReferences(IntPtr binary);
-
- [DllImport("_amuABI.dll")]
- static extern void _amuABISetLiteralConstants(ConstsInfo compOutput,
- IntPtr floatConstAddressCPU,
- IntPtr intConstAddressCPU,
- IntPtr boolConstAddressCPU);
-
- [DllImport("_amuABI.dll")]
- static extern void _amuABISetUserConstant(ConstsInfo compOutput,
- IntPtr addressCPU,
- string name,
- IntPtr value,
- DataType type);
-
- [DllImport("_amuABI.dll")]
- static extern void _amuABIExtractConstants(
- ref uint litConstsCount,
- out IntPtr plitConsts,
- out uint litOffset,
- ref uint userConstsCount,
- out IntPtr puserConsts,
- out uint userOffser,
- IntPtr binary);
-
- /// <summary>
- /// Class that represents constants as pair (name, type)
- /// </summary>
- struct stringType
- {
- string m_str;
- Type m_type;
-
- /// <summary>
- /// Creates const pair by passing name and type
- /// </summary>
- /// <param name="str"> Name of constant </param>
- /// <param name="type"> Type of constant </param>
- public stringType(string str, Type type)
- {
- m_str = str;
- m_type = type;
- }
- }
-
- /// <summary>
- /// List of constants (user and literal)
- /// </summary>
- ConstsInfo m_consts = new AmuABI.ConstsInfo();
-
- /// <summary>
- /// Dictionary for getting const index in list by pair (name, type)
- /// </summary>
- Dictionary<stringType,int> m_constIndexByName = new Dictionary<stringType, int>();
-
- /// <summary>
- /// Creates string from array of bytes (ANSI chars)
- /// </summary>
- /// <param name="str"> Array of bytes </param>
- /// <returns> String that represent this array </returns>
- string toString(byte[] str)
- {
- string cur = "";
- int i = 0;
- while (str[i] != '\0')
- {
- cur += (char)str[i];
- i++;
- }
- return cur;
- }
-
- /// <summary>
- /// Set user constant of specified type in the constant buffer by passing name of constant and value
- /// </summary>
- /// <typeparam name="T"> Standard type of constant </typeparam>
- /// <param name="constbuf"> Constant buffer that contain this constant </param>
- /// <param name="name"> Name of constant </param>
- /// <param name="value"> Vector of values of specified type represents constant value </param>
- public void setUserConst<T>(ConstBuffer<T> constbuf, string name, T[] value)
- {
- stringType st = new stringType(name, typeof(T));
- if (!m_constIndexByName.ContainsKey(st))
- throw new NotFoundConstException("not found const '" + name + "' of type " + typeof(T));
-
- int u = m_constIndexByName[st];
- if (typeof(T) == typeof(float))
- {
- unsafe
- {
- float* c = &((float*)constbuf.addrCPU)[4 * m_consts.userConsts[u].addr];
- c[0] = (float)(object)value[0];
- c[1] = (float)(object)value[1];
- c[2] = (float)(object)value[2];
- c[3] = (float)(object)value[3];
- }
- }
- else if (typeof(T) == typeof(int))
- {
- unsafe
- {
-
- byte* c = &((byte*)constbuf.addrCPU)[4 * m_consts.userConsts[u].addr];
- c[0] = (byte)(int)(object)value[0];
- c[1] = (byte)(int)(object)value[1];
- c[2] = (byte)(int)(object)value[2];
- c[3] = (byte)(int)(object)value[3];
- }
- }
- else if (typeof(T) == typeof(bool))
- {
- throw new InvalidConstException("unsupported type bool");
- }
- }
-
- /// <summary>
- /// Get constant value of specified type by passing constant buffer and name of constant
- /// </summary>
- /// <typeparam name="T"> Standard type of constant </typeparam>
- /// <param name="constbuf"> Constant buffer that contain this constant </param>
- /// <param name="name"> Name of constant </param>
- /// <returns> Vector of values of specified type represents constant value </returns>
- public T[] getUserConst<T>(ConstBuffer<T> constbuf, string name)
- {
- stringType st = new stringType(name, typeof(T));
- if (!m_constIndexByName.ContainsKey(st))
- throw new InvalidConstException("not found const '" + name + "' of type " + typeof(T));
-
- int u = m_constIndexByName[st];
- if (typeof(T) == typeof(float))
- {
- unsafe
- {
- float* c = &((float*)constbuf.addrCPU)[4 * m_consts.userConsts[u].addr];
- T[] value = new T[4];
- value[0] = (T)(object)c[0];
- value[1] = (T)(object)c[1];
- value[2] = (T)(object)c[2];
- value[3] = (T)(object)c[3];
- return value;
- }
- }
- else if (typeof(T) == typeof(int))
- {
- throw new InvalidConstException("unsupported type int");
- }
- else if (typeof(T) == typeof(bool))
- {
- throw new InvalidConstException("unsupported type bool");
- }
-
- return null;
- }
-
- /// <summary>
- /// Extract constants info from binary
- /// </summary>
- /// <param name="comp"> Binary DPVM program that created by AMU compiler </param>
- public void extractConstants(IntPtr data)
- {
- IntPtr plitConsts, puserConsts;
- uint litOffset, userOffset;
-
- _amuABIExtractConstants(ref m_consts.litConstsCount, out plitConsts, out litOffset,
- ref m_consts.userConstsCount, out puserConsts, out userOffset, data);
-
- m_consts.litConsts = new AmuABI.LiteralConst[m_consts.litConstsCount];
- for (int i = 0; i < m_consts.litConstsCount; i++)
- {
- m_consts.litConsts[i] = (AmuABI.LiteralConst)Marshal.PtrToStructure(plitConsts, typeof(AmuABI.LiteralConst));
- plitConsts = (IntPtr)((uint)plitConsts + litOffset);
- }
- m_consts.userConsts = new AmuABI.UserConst[m_consts.userConstsCount];
- for (int i = 0; i < m_consts.userConstsCount; i++)
- {
- m_consts.userConsts[i] = (AmuABI.UserConst)Marshal.PtrToStructure(puserConsts, typeof(AmuABI.UserConst));
- puserConsts = (IntPtr)((uint)puserConsts + userOffset);
- }
-
- // fill dictionary (name-type,index)
- for (int u = 0; u < m_consts.userConstsCount; u++)
- {
- string s = toString(m_consts.userConsts[u].name.str);
- Type t = m_typeAmu[m_consts.userConsts[u].type];
- m_constIndexByName.Add(new stringType(s,t), u);
- }
- }
- }
- }