PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C++ | 198 lines | 174 code | 16 blank | 8 comment | 17 complexity | ad940e081ebaddf9b43528763a3db2c9 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. shader += commandconverters->getConverter(command)->convert(command);
  39. shader += "}";
  40. //</fix me>
  41. }
  42. catch (Exception ^ex)
  43. {
  44. this->setError(Error::BAD_PARAMS, ex->ToString());
  45. return nullptr;
  46. }
  47. return shader;
  48. }
  49. List<ParamFormat ^> ^KernelCompiler::compileParams(String ^params)
  50. {
  51. this->clearError();
  52. cli::array<String ^, 1> ^tokens =
  53. params->Split(gcnew cli::array<String ^> {" ", ",", "\n"},
  54. System::StringSplitOptions::RemoveEmptyEntries);
  55. int i = 0;
  56. List<ParamFormat ^> ^kparams = gcnew List<ParamFormat ^>();
  57. bool isOutParam;
  58. GpuDataType datatype;
  59. while (i < tokens->Length)
  60. {
  61. if (tokens[i] == "out")
  62. {
  63. isOutParam = true;
  64. i++;
  65. }
  66. else
  67. isOutParam = false;
  68. switch (tokens[i][0])
  69. {
  70. case 'f': datatype = GpuDataType::float1 + (GpuDataType)(tokens[i][5] - '1');
  71. break;
  72. case 'i': datatype = GpuDataType::int1 + (GpuDataType)(tokens[i][3] - '1');
  73. break;
  74. case 's': datatype = GpuDataType::samplerFloat1 + (GpuDataType)(tokens[i][12] == '1' ? 0 : 1);
  75. break;
  76. }
  77. i++;
  78. kparams->Add(gcnew ParamFormat(tokens[i],
  79. datatype,
  80. isOutParam));
  81. i++;
  82. }
  83. return kparams;
  84. }
  85. //-----------------------
  86. //--- KernelPerformer ---
  87. //-----------------------
  88. void KernelPerformer::setBinder(int var, GpuVarBinder ^binder)
  89. {
  90. varbinders->setBinder(var, binder);
  91. }
  92. bool KernelPerformer::getOutputSize(array<Object ^> ^args,
  93. List<ParamFormat ^> ^vars,
  94. float &sizex, float &sizey,
  95. float &maxcoordx, float &maxcoordy)
  96. {
  97. int i = 0;
  98. BufferContext ^bcontext;
  99. for each (ParamFormat ^f in vars)
  100. {
  101. if (f->isOutParam())
  102. {
  103. if (i >= args->Length ||
  104. (bcontext = dynamic_cast<BufferContext ^>(args[i])) == nullptr)
  105. {
  106. this->setError(Error::BAD_PARAMS, "Real params doesn't correspond to their definition");
  107. return false;
  108. }
  109. BufferFormat ^bformat = bcontext->getFormat();
  110. sizex = bformat->getSizeX();
  111. sizey = bformat->getSizeY();
  112. maxcoordx = bformat->getMaxCoordX();
  113. maxcoordy = bformat->getMaxCoordY();
  114. return true;
  115. }
  116. i++;
  117. }
  118. this->setError(Error::BAD_PARAMS, "There aren't any 'out' parameters");
  119. return false;
  120. }
  121. KernelPerformer::KernelPerformer()
  122. {
  123. this->varbinders = gcnew GpuVarBinders();
  124. }
  125. bool KernelPerformer::bindParams(array<Object ^> ^args, List<ParamFormat ^> ^vars)
  126. {
  127. this->clearError();
  128. IEnumerator<ParamFormat ^> ^en = vars->GetEnumerator();
  129. try
  130. {
  131. for each (Object ^o in args)
  132. {
  133. if (!en->MoveNext())
  134. {
  135. this->setError(Error::BAD_PARAMS, "There aren't enough var for binding");
  136. return false;
  137. }
  138. varbinders->getBinder((int)en->Current->getDataType())
  139. ->bind(en->Current, o);
  140. }
  141. if (en->MoveNext())
  142. {
  143. this->setError(Error::BAD_PARAMS, "There aren't enough objects for binding");
  144. return false;
  145. }
  146. }
  147. catch (Exception ^ex)
  148. {
  149. this->setError(Error::BAD_PARAMS, ex->ToString());
  150. return false;
  151. }
  152. return true;
  153. }
  154. bool KernelPerformer::unbindParams(array<Object ^> ^args, List<ParamFormat ^> ^vars)
  155. {
  156. this->clearError();
  157. IEnumerator<ParamFormat ^> ^en = vars->GetEnumerator();
  158. try
  159. {
  160. for each (Object ^o in args)
  161. {
  162. if (!en->MoveNext())
  163. {
  164. this->setError(Error::BAD_PARAMS, "There aren't enough var for binding");
  165. return false;
  166. }
  167. varbinders->getBinder((int)en->Current->getDataType())
  168. ->unbind(en->Current, o);
  169. }
  170. if (en->MoveNext())
  171. {
  172. this->setError(Error::BAD_PARAMS, "There aren't enough objects for binding");
  173. return false;
  174. }
  175. }
  176. catch (Exception ^ex)
  177. {
  178. this->setError(Error::BAD_PARAMS, ex->ToString());
  179. return false;
  180. }
  181. return true;
  182. }