simple_task.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00033 #include "gpucalc/simple_task.h"
00034 
00035 #include "gpucalc/application.h"
00036 #include "gpucalc/graphic_core.h"
00037 #include "gpucalc/shader_system.h"
00038 #include "gpucalc/texture.h"
00039 #include "gpucalc/log_manager.h"
00040 #include "gpucalc/framebuffer.h"
00041 #include "gpucalc/shader.h"
00042 
00043 #include "gpucalc/uniform.h"
00044 
00045 
00046 
00047 namespace gpucalc
00048 {
00049  SimpleTask::SimpleTask(const std::string & TaskName): Task(_SimpleTaskClassName, TaskName),
00050   mActiveKernel(0), mActiveFrameBuffer(0), mResultData(0), mRenderTarget(0), mDefaultGraphicCore(Application::getSingleton().getDefaultGraphicCore()),
00051   mDefaultShaderSystem(Application::getSingleton().getCompatibleShaderSystem()), mCompilerParams()
00052  {
00053   mActiveKernel = new Kernel(TaskName + "_kernel", mDefaultGraphicCore);
00054   mActiveFrameBuffer = mDefaultGraphicCore->create(TaskName + "_framebuffer");
00055   mActiveKernel->setFrameBuffer(mActiveFrameBuffer);
00056   Shader * shader = mDefaultShaderSystem->getShaderByName(TaskName + "_shader");
00057   mRenderTarget = mDefaultGraphicCore->create(TaskName + "_render_target", TU_Render);
00058   
00059   mActiveKernel->setShader(shader);
00060   LogManager::getSingleton().logMessage(this, "Successfully created.", LML_Trivial);
00061  }
00062 
00063 
00064  SimpleTask::~SimpleTask()
00065  {
00066   mActiveKernel->removeShader();
00067   mDefaultGraphicCore->destroy(mActiveKernel->removeFrameBuffer());
00068   mDefaultGraphicCore->destroy(mRenderTarget);
00069   delete mActiveKernel;
00070   LogManager::getSingleton().logMessage(this, "Successfully destroyed.", LML_Trivial);
00071  }
00072 
00073 
00074  void SimpleTask::addDataToActiveKernel(const Data & data)
00075  {
00076   Uniform * uniform = 0;
00077   if (!data.isArray())
00078   {
00079    uniform = mDefaultShaderSystem->create(data);
00080   }
00081   else
00082   {
00083    Texture * texture = 0;
00084    if (mDefaultGraphicCore->isTextureCreated(data.getObjectName()))
00085    {
00086     texture = mDefaultGraphicCore->getTextureByName(data.getObjectName());
00087    }
00088    else
00089    {
00090     texture = mDefaultGraphicCore->create(data.getObjectName(), TU_Static);
00091    }
00092    texture->reInitialize(data);
00093    texture->uploadToGPU();
00094 
00095    uniform = mDefaultShaderSystem->create(texture);
00096   }
00097 
00098   uniform->initialize();
00099   uniform->uploadToGPU();
00100   mActiveKernel->getShader()->addUniform(uniform);
00101  }
00102 
00103 
00104  void SimpleTask::addResultToActiveKernel(Data & result)
00105  {
00106   mResultData = &result;
00107  }
00108 
00109 
00110  void SimpleTask::removeResultFromActiveKernel(const Data & /*result*/)
00111  {
00112   mResultData = NULL;
00113  }
00114 
00115 
00116  void SimpleTask::addCompilerParamsToActiveKernel(const std::string & Parameters)
00117  {
00118   mCompilerParams = Parameters;
00119  }
00120 
00121 
00122  Kernel * SimpleTask::getActiveKernel() const
00123  {
00124   return mActiveKernel;
00125  }
00126 
00127 
00128  void SimpleTask::removeKernel(Kernel * /*kernel*/)
00129  {
00130   Warning<ERR_NOT_IMPLEMENTED>(this, "This type of tasks cannot remove kernels manually.", "SimpleTask::removeKernel()", __FILE__, __LINE__);
00131  }
00132 
00133 
00134  void SimpleTask::compile()
00135  {
00136   mActiveKernel->compile(mCompilerParams);
00137   LogManager::getSingleton().logMessage(this, "Task compiled.", LML_Trivial);
00138  }
00139 
00140 
00141  void SimpleTask::compute()
00142  {
00143   LogManager::getSingleton().logMessage(this, "Starting computing...", LML_Trivial);
00144   mRenderTarget->reInitialize(*mResultData);
00145   computeOneKernel(mRenderTarget);
00146   mActiveKernel->getFrameBuffer()->getData(*mResultData, mRenderTarget);
00147   LogManager::getSingleton().logMessage(this, "Computing finished.", LML_Trivial);
00148  }
00149 
00150 
00151  void SimpleTask::computeOneKernel(Texture * RenderTarget)
00152  {
00153   mActiveKernel->preCompute();
00154   mActiveKernel->compute(RenderTarget);
00155   mActiveKernel->postCompute();
00156  }
00157 }

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6