Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreD3D9GpuProgram.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2003 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreD3D9GpuProgram.h"
00026 #include "OgreMatrix4.h"
00027 #include "OgreException.h"
00028 #include "OgreLogManager.h"
00029 #include "OgreD3D9Mappings.h"
00030 #include "OgreGpuProgramManager.h"
00031 #include "OgreSDDataChunk.h"
00032 
00033 namespace Ogre {
00034 
00035     //-----------------------------------------------------------------------------
00036     D3D9GpuProgram::D3D9GpuProgram(const String& name, GpuProgramType gptype, 
00037         const String& syntaxCode, LPDIRECT3DDEVICE9 pDev) 
00038         : GpuProgram(name, gptype, syntaxCode), mpDevice(pDev), mpExternalMicrocode(NULL)
00039     {
00040     }
00041     //-----------------------------------------------------------------------------
00042     void D3D9GpuProgram::load(void)
00043     {
00044         if (mIsLoaded)
00045         {
00046             unload();
00047         }
00048 
00049         if (mpExternalMicrocode)
00050         {
00051             loadFromMicrocode(mpExternalMicrocode);
00052         }
00053         else
00054         {
00055             // Normal load-from-source approach
00056             if (mLoadFromFile)
00057             {
00058                 // find & load source code
00059                 SDDataChunk chunk;
00060                 GpuProgramManager::getSingleton()._findResourceData(mFilename, chunk);
00061                 mSource = chunk.getAsString();
00062             }
00063 
00064             // Call polymorphic load
00065             loadFromSource();
00066         }
00067 
00068         mIsLoaded = true;
00069 
00070     }
00071     //-----------------------------------------------------------------------------
00072     void D3D9GpuProgram::loadFromSource(void)
00073     {
00074         // Create the shader
00075         // Assemble source into microcode
00076         LPD3DXBUFFER microcode;
00077         LPD3DXBUFFER errors;
00078         HRESULT hr = D3DXAssembleShader(
00079             mSource.c_str(),
00080             static_cast<UINT>(mSource.length()),
00081             NULL,               // no #define support
00082             NULL,               // no #include support
00083             0,                  // standard compile options
00084             &microcode,
00085             &errors);
00086 
00087         if (FAILED(hr))
00088         {
00089             Except(hr, "Cannot assemble D3D9 shader " + mName,
00090                 "D3D9GpuProgram::loadFromSource");
00091             
00092         }
00093 
00094         loadFromMicrocode(microcode);
00095 
00096         SAFE_RELEASE(microcode);
00097         SAFE_RELEASE(errors);
00098     }
00099     //-----------------------------------------------------------------------------
00100     D3D9GpuVertexProgram::D3D9GpuVertexProgram(const String& name, const String& syntaxCode, LPDIRECT3DDEVICE9 pDev) 
00101         : D3D9GpuProgram(name, GPT_VERTEX_PROGRAM, syntaxCode, pDev), mpVertexShader(NULL)
00102     {
00103         // do nothing here, all is done in load()
00104     }
00105     //-----------------------------------------------------------------------------
00106     void D3D9GpuVertexProgram::loadFromMicrocode(LPD3DXBUFFER microcode)
00107     {
00108         // Create the shader
00109         HRESULT hr = mpDevice->CreateVertexShader( 
00110             static_cast<DWORD*>(microcode->GetBufferPointer()), 
00111             &mpVertexShader);
00112 
00113         if (FAILED(hr))
00114         {
00115             Except(hr, "Cannot create D3D9 vertex shader " + mName + " from microcode.",
00116                 "D3D9GpuVertexProgram::loadFromMicrocode");
00117             
00118         }
00119     }
00120     //-----------------------------------------------------------------------------
00121     void D3D9GpuVertexProgram::unload(void)
00122     {
00123         SAFE_RELEASE(mpVertexShader);
00124     }
00125     //-----------------------------------------------------------------------------
00126     //-----------------------------------------------------------------------------
00127     D3D9GpuFragmentProgram::D3D9GpuFragmentProgram(const String& name, const String& syntaxCode, LPDIRECT3DDEVICE9 pDev) 
00128         : D3D9GpuProgram(name, GPT_FRAGMENT_PROGRAM, syntaxCode, pDev), mpPixelShader(NULL)
00129     {
00130         // do nothing here, all is done in load()
00131     }
00132     //-----------------------------------------------------------------------------
00133     void D3D9GpuFragmentProgram::loadFromMicrocode(LPD3DXBUFFER microcode)
00134     {
00135         // Create the shader
00136         HRESULT hr = mpDevice->CreatePixelShader(
00137             static_cast<DWORD*>(microcode->GetBufferPointer()), 
00138             &mpPixelShader);
00139 
00140         if (FAILED(hr))
00141         {
00142             Except(hr, "Cannot create D3D9 pixel shader " + mName + " from microcode.",
00143                 "D3D9GpuFragmentProgram::loadFromMicrocode");
00144             
00145         }
00146     }
00147     //-----------------------------------------------------------------------------
00148     void D3D9GpuFragmentProgram::unload(void)
00149     {
00150         SAFE_RELEASE(mpPixelShader);
00151     }
00152     //-----------------------------------------------------------------------------
00153 
00154 }
00155 

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:02 2004