PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/adinetz01/runtime/CBucks.Device.Gpu.OpenGL.Wrapper/Performers.cpp

#
C++ | 217 lines | 193 code | 16 blank | 8 comment | 20 complexity | c9aca910a77e47bc14379488bb6aecbb 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 CBucksDeviceGpuOpenGLWrapper;
  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. bool KernelPerformer::getOutputSize(array<Object ^> ^args,
  89. List<ParamFormat ^> ^vars,
  90. float &sizex, float &sizey,
  91. float &maxcoordx, float &maxcoordy)
  92. {
  93. int i = 0;
  94. for each (ParamFormat ^f in vars)
  95. {
  96. if (f->isOutParam())
  97. {
  98. if (i >= args->Length ||
  99. args[i]->GetType() != BufferContext::typeid)
  100. {
  101. this->setError(Error::BAD_PARAMS, "Real params doesn't correspond to their definition");
  102. return false;
  103. }
  104. BufferFormat ^bformat = ((BufferContext ^)args[i])->getFormat();
  105. sizex = bformat->getSizeX();
  106. sizey = bformat->getSizeY();
  107. maxcoordx = bformat->getMaxCoordX();
  108. maxcoordy = bformat->getMaxCoordY();
  109. return true;
  110. }
  111. i++;
  112. }
  113. this->setError(Error::BAD_PARAMS, "There aren't any 'out' parameters");
  114. return false;
  115. }
  116. bool KernelPerformer::setDevice(float sizex, float sizey)
  117. {
  118. this->clearError();
  119. glMatrixMode(GL_PROJECTION);
  120. glLoadIdentity();
  121. glOrtho(0.0, sizex, 0.0, sizey, -1.0, 1.0);
  122. glMatrixMode(GL_MODELVIEW);
  123. glLoadIdentity();
  124. glViewport(0, 0, sizex, sizey);
  125. return true;
  126. }
  127. KernelPerformer::KernelPerformer(FBOContainer ^container)
  128. {
  129. this->container = container;
  130. this->varbinders = gcnew GpuVarBinders();
  131. }
  132. bool KernelPerformer::bindParams(array<Object ^> ^args, List<ParamFormat ^> ^vars)
  133. {
  134. this->clearError();
  135. GLenum err;
  136. IEnumerator<ParamFormat ^> ^en = vars->GetEnumerator();
  137. try
  138. {
  139. for each (Object ^o in args)
  140. {
  141. if (!en->MoveNext())
  142. {
  143. this->setError(Error::BAD_PARAMS, "There aren't enough var for binding");
  144. return false;
  145. }
  146. varbinders->getBinder((int)en->Current->getDataType())
  147. ->bind(en->Current, o);
  148. }
  149. if (en->MoveNext())
  150. {
  151. this->setError(Error::BAD_PARAMS, "There aren't enough objects for binding");
  152. return false;
  153. }
  154. }
  155. catch (Exception ^ex)
  156. {
  157. this->setError(Error::BAD_PARAMS, ex->ToString());
  158. return false;
  159. }
  160. if (!container->bind())
  161. {
  162. this->setError(container);
  163. return false;
  164. }
  165. if ((err = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)) !=
  166. GL_FRAMEBUFFER_COMPLETE_EXT)
  167. {
  168. this->setError(Error::OPENGL_ERROR, "Framebuffer incomplete");
  169. return false;
  170. }
  171. return true;
  172. }
  173. bool KernelPerformer::unbindParams(array<Object ^> ^args, List<ParamFormat ^> ^vars)
  174. {
  175. this->clearError();
  176. IEnumerator<ParamFormat ^> ^en = vars->GetEnumerator();
  177. try
  178. {
  179. for each (Object ^o in args)
  180. {
  181. if (!en->MoveNext())
  182. {
  183. this->setError(Error::BAD_PARAMS, "There aren't enough var for binding");
  184. return false;
  185. }
  186. varbinders->getBinder((int)en->Current->getDataType())
  187. ->unbind(en->Current, o);
  188. }
  189. if (en->MoveNext())
  190. {
  191. this->setError(Error::BAD_PARAMS, "There aren't enough objects for binding");
  192. return false;
  193. }
  194. }
  195. catch (Exception ^ex)
  196. {
  197. this->setError(Error::BAD_PARAMS, ex->ToString());
  198. return false;
  199. }
  200. return true;
  201. }