/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
-
- #include "stdafx.h"
- #include <stdlib.h>
- #include "Performers.h"
-
- using namespace System::Collections::Generic;
- using namespace CBucksDeviceGpuOpenGLWrapper;
-
- //----------------------
- //--- KernelCompiler ---
- //----------------------
-
- KernelCompiler::KernelCompiler()
- {
- this->varconverters = gcnew GpuVarConverters();
- this->commandconverters = gcnew GpuCommandConverters();
- this->commandconverters->setVarConverters(varconverters);
- }
-
- GpuCommandConverter ^KernelCompiler::getCommandConverter(GpuOp op)
- {
- return commandconverters->getConverter(op);
- }
-
- void KernelCompiler::setCommandConverter(int op, GpuCommandConverter ^converter)
- {
- commandconverters->setConverter(op, converter);
- }
-
- void KernelCompiler::setVarConverter(int var, GpuVarConverter ^converter)
- {
- varconverters->setConverter(var, converter);
- }
-
- String ^KernelCompiler::compile(GpuShaderCode ^code)
- {
- String ^shader;
- this->clearError();
- try
- {
- for each (GpuVar ^var in code->variables)
- shader += varconverters->getConverter(var)->getDeclaration(var);
- //<fix me>
- shader += "void main(void){";
- for each (GpuCommand ^command in code->commandList)
- shader += commandconverters->getConverter(command)->convert(command);
- shader += "}";
- //</fix me>
- }
- catch (Exception ^ex)
- {
- this->setError(Error::BAD_PARAMS, ex->ToString());
- return nullptr;
- }
- return shader;
- }
-
- List<ParamFormat ^> ^KernelCompiler::compileParams(String ^params)
- {
- this->clearError();
- cli::array<String ^, 1> ^tokens =
- params->Split(gcnew cli::array<String ^> {" ", ",", "\n"},
- System::StringSplitOptions::RemoveEmptyEntries);
- int i = 0;
- List<ParamFormat ^> ^kparams = gcnew List<ParamFormat ^>();
- bool isOutParam;
- GpuDataType datatype;
- while (i < tokens->Length)
- {
- if (tokens[i] == "out")
- {
- isOutParam = true;
- i++;
- }
- else
- isOutParam = false;
-
- switch (tokens[i][0])
- {
- case 'f': datatype = GpuDataType::float1 + (GpuDataType)(tokens[i][5] - '1');
- break;
- case 'i': datatype = GpuDataType::int1 + (GpuDataType)(tokens[i][3] - '1');
- break;
- case 's': datatype = GpuDataType::samplerFloat1 + (GpuDataType)(tokens[i][12] == '1' ? 0 : 1);
- break;
- }
- i++;
- kparams->Add(gcnew ParamFormat(tokens[i],
- datatype,
- isOutParam));
- i++;
- }
- return kparams;
- }
-
- //-----------------------
- //--- KernelPerformer ---
- //-----------------------
-
- bool KernelPerformer::getOutputSize(array<Object ^> ^args,
- List<ParamFormat ^> ^vars,
- float &sizex, float &sizey,
- float &maxcoordx, float &maxcoordy)
- {
- int i = 0;
- for each (ParamFormat ^f in vars)
- {
- if (f->isOutParam())
- {
- if (i >= args->Length ||
- args[i]->GetType() != BufferContext::typeid)
- {
- this->setError(Error::BAD_PARAMS, "Real params doesn't correspond to their definition");
- return false;
- }
- BufferFormat ^bformat = ((BufferContext ^)args[i])->getFormat();
- sizex = bformat->getSizeX();
- sizey = bformat->getSizeY();
- maxcoordx = bformat->getMaxCoordX();
- maxcoordy = bformat->getMaxCoordY();
- return true;
- }
- i++;
- }
- this->setError(Error::BAD_PARAMS, "There aren't any 'out' parameters");
- return false;
- }
-
- bool KernelPerformer::setDevice(float sizex, float sizey)
- {
- this->clearError();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, sizex, 0.0, sizey, -1.0, 1.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glViewport(0, 0, sizex, sizey);
- return true;
- }
-
- KernelPerformer::KernelPerformer(FBOContainer ^container)
- {
- this->container = container;
- this->varbinders = gcnew GpuVarBinders();
- }
-
- bool KernelPerformer::bindParams(array<Object ^> ^args, List<ParamFormat ^> ^vars)
- {
- this->clearError();
- GLenum err;
- IEnumerator<ParamFormat ^> ^en = vars->GetEnumerator();
- try
- {
- for each (Object ^o in args)
- {
- if (!en->MoveNext())
- {
- this->setError(Error::BAD_PARAMS, "There aren't enough var for binding");
- return false;
- }
- varbinders->getBinder((int)en->Current->getDataType())
- ->bind(en->Current, o);
- }
- if (en->MoveNext())
- {
- this->setError(Error::BAD_PARAMS, "There aren't enough objects for binding");
- return false;
- }
- }
- catch (Exception ^ex)
- {
- this->setError(Error::BAD_PARAMS, ex->ToString());
- return false;
- }
- if (!container->bind())
- {
- this->setError(container);
- return false;
- }
- if ((err = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)) !=
- GL_FRAMEBUFFER_COMPLETE_EXT)
- {
- this->setError(Error::OPENGL_ERROR, "Framebuffer incomplete");
- return false;
- }
- return true;
- }
-
- bool KernelPerformer::unbindParams(array<Object ^> ^args, List<ParamFormat ^> ^vars)
- {
- this->clearError();
- IEnumerator<ParamFormat ^> ^en = vars->GetEnumerator();
- try
- {
- for each (Object ^o in args)
- {
- if (!en->MoveNext())
- {
- this->setError(Error::BAD_PARAMS, "There aren't enough var for binding");
- return false;
- }
- varbinders->getBinder((int)en->Current->getDataType())
- ->unbind(en->Current, o);
- }
- if (en->MoveNext())
- {
- this->setError(Error::BAD_PARAMS, "There aren't enough objects for binding");
- return false;
- }
- }
- catch (Exception ^ex)
- {
- this->setError(Error::BAD_PARAMS, ex->ToString());
- return false;
- }
- return true;
- }