PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/adinetz02/runtime/CBucks.Device.Gpu.Wrapper/Performers.cpp

#
C++ | 203 lines | 179 code | 16 blank | 8 comment | 17 complexity | 0418021bcc2be1408f9fad18926c6bf5 MD5 | raw file
Possible License(s): AGPL-3.0
  1. #include "stdafx.h"
  2. #include <stdlib.h>
  3. #include "Performers.h"
  4. using namespace System::Collections::Generic;
  5. using namespace CBucksDeviceGpuWrapper;
  6. //----------------------
  7. //--- KernelCompiler ---
  8. //----------------------
  9. KernelCompiler::KernelCompiler()
  10. {
  11. this->varconverters = gcnew GpuVarConverters();
  12. this->commandconverters = gcnew GpuCommandConverters();
  13. this->commandconverters->setVarConverters(varconverters);
  14. }
  15. GpuCommandConverter ^KernelCompiler::getCommandConverter(GpuOp op)
  16. {
  17. return commandconverters->getConverter(op);
  18. }
  19. void KernelCompiler::setCommandConverter(int op, GpuCommandConverter ^converter)
  20. {
  21. commandconverters->setConverter(op, converter);
  22. }
  23. void KernelCompiler::setVarConverter(int var, GpuVarConverter ^converter)
  24. {
  25. varconverters->setConverter(var, converter);
  26. }
  27. String ^KernelCompiler::compile(GpuShaderCode ^code)
  28. {
  29. String ^shader;
  30. this->clearError();
  31. try
  32. {
  33. for each (GpuVar ^var in code->variables)
  34. shader += varconverters->getConverter(var)->getDeclaration(var);
  35. //<fix me>
  36. shader += "void main(void){";
  37. for each (GpuCommand ^command in code->commandList) {
  38. try {
  39. shader += commandconverters->getConverter(command)->convert(command);
  40. } catch(Exception^ ex) {
  41. throw gcnew Exception(command->ToString(), ex);
  42. }
  43. }
  44. shader += "}";
  45. //</fix me>
  46. }
  47. catch (Exception ^ex)
  48. {
  49. this->setError(Error::BAD_PARAMS, ex->ToString());
  50. return nullptr;
  51. }
  52. return shader;
  53. }
  54. List<ParamFormat ^> ^KernelCompiler::compileParams(String ^params)
  55. {
  56. this->clearError();
  57. cli::array<String ^, 1> ^tokens =
  58. params->Split(gcnew cli::array<String ^> {" ", ",", "\n"},
  59. System::StringSplitOptions::RemoveEmptyEntries);
  60. int i = 0;
  61. List<ParamFormat ^> ^kparams = gcnew List<ParamFormat ^>();
  62. bool isOutParam;
  63. GpuDataType datatype;
  64. while (i < tokens->Length)
  65. {
  66. if (tokens[i] == "out")
  67. {
  68. isOutParam = true;
  69. i++;
  70. }
  71. else
  72. isOutParam = false;
  73. switch (tokens[i][0])
  74. {
  75. case 'f': datatype = GpuDataType::float1 + (GpuDataType)(tokens[i][5] - '1');
  76. break;
  77. case 'i': datatype = GpuDataType::int1 + (GpuDataType)(tokens[i][3] - '1');
  78. break;
  79. case 's': datatype = GpuDataType::samplerFloat1 + (GpuDataType)(tokens[i][12] == '1' ? 0 : 1);
  80. break;
  81. }
  82. i++;
  83. kparams->Add(gcnew ParamFormat(tokens[i],
  84. datatype,
  85. isOutParam));
  86. i++;
  87. }
  88. return kparams;
  89. }
  90. //-----------------------
  91. //--- KernelPerformer ---
  92. //-----------------------
  93. void KernelPerformer::setBinder(int var, GpuVarBinder ^binder)
  94. {
  95. varbinders->setBinder(var, binder);
  96. }
  97. bool KernelPerformer::getOutputSize(array<Object ^> ^args,
  98. List<ParamFormat ^> ^vars,
  99. float &sizex, float &sizey,
  100. float &maxcoordx, float &maxcoordy)
  101. {
  102. int i = 0;
  103. BufferContext ^bcontext;
  104. for each (ParamFormat ^f in vars)
  105. {
  106. if (f->isOutParam())
  107. {
  108. if (i >= args->Length ||
  109. (bcontext = dynamic_cast<BufferContext ^>(args[i])) == nullptr)
  110. {
  111. this->setError(Error::BAD_PARAMS, "Real params doesn't correspond to their definition");
  112. return false;
  113. }
  114. BufferFormat ^bformat = bcontext->getFormat();
  115. sizex = bformat->getSizeX();
  116. sizey = bformat->getSizeY();
  117. maxcoordx = bformat->getMaxCoordX();
  118. maxcoordy = bformat->getMaxCoordY();
  119. return true;
  120. }
  121. i++;
  122. }
  123. this->setError(Error::BAD_PARAMS, "There aren't any 'out' parameters");
  124. return false;
  125. }
  126. KernelPerformer::KernelPerformer()
  127. {
  128. this->varbinders = gcnew GpuVarBinders();
  129. }
  130. bool KernelPerformer::bindParams(array<Object ^> ^args, List<ParamFormat ^> ^vars)
  131. {
  132. this->clearError();
  133. IEnumerator<ParamFormat ^> ^en = vars->GetEnumerator();
  134. try
  135. {
  136. for each (Object ^o in args)
  137. {
  138. if (!en->MoveNext())
  139. {
  140. this->setError(Error::BAD_PARAMS, "There aren't enough var for binding");
  141. return false;
  142. }
  143. varbinders->getBinder((int)en->Current->getDataType())
  144. ->bind(en->Current, o);
  145. }
  146. if (en->MoveNext())
  147. {
  148. this->setError(Error::BAD_PARAMS, "There aren't enough objects for binding");
  149. return false;
  150. }
  151. }
  152. catch (Exception ^ex)
  153. {
  154. this->setError(Error::BAD_PARAMS, ex->ToString());
  155. return false;
  156. }
  157. return true;
  158. }
  159. bool KernelPerformer::unbindParams(array<Object ^> ^args, List<ParamFormat ^> ^vars)
  160. {
  161. this->clearError();
  162. IEnumerator<ParamFormat ^> ^en = vars->GetEnumerator();
  163. try
  164. {
  165. for each (Object ^o in args)
  166. {
  167. if (!en->MoveNext())
  168. {
  169. this->setError(Error::BAD_PARAMS, "There aren't enough var for binding");
  170. return false;
  171. }
  172. varbinders->getBinder((int)en->Current->getDataType())
  173. ->unbind(en->Current, o);
  174. }
  175. if (en->MoveNext())
  176. {
  177. this->setError(Error::BAD_PARAMS, "There aren't enough objects for binding");
  178. return false;
  179. }
  180. }
  181. catch (Exception ^ex)
  182. {
  183. this->setError(Error::BAD_PARAMS, ex->ToString());
  184. return false;
  185. }
  186. return true;
  187. }