[692] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #include "OgreD3D9GpuProgram.h"
|
---|
| 26 | #include "OgreMatrix4.h"
|
---|
| 27 | #include "OgreException.h"
|
---|
| 28 | #include "OgreLogManager.h"
|
---|
| 29 | #include "OgreD3D9Mappings.h"
|
---|
| 30 | #include "OgreResourceGroupManager.h"
|
---|
| 31 |
|
---|
| 32 | namespace Ogre {
|
---|
| 33 |
|
---|
| 34 | //-----------------------------------------------------------------------------
|
---|
| 35 | D3D9GpuProgram::D3D9GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
|
---|
| 36 | const String& group, bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev)
|
---|
| 37 | : GpuProgram(creator, name, handle, group, isManual, loader),
|
---|
| 38 | mpDevice(pDev), mpExternalMicrocode(NULL)
|
---|
| 39 | {
|
---|
| 40 | if (createParamDictionary("D3D9GpuProgram"))
|
---|
| 41 | {
|
---|
| 42 | setupBaseParamDictionary();
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | //-----------------------------------------------------------------------------
|
---|
| 46 | void D3D9GpuProgram::loadImpl(void)
|
---|
| 47 | {
|
---|
| 48 | if (mpExternalMicrocode)
|
---|
| 49 | {
|
---|
| 50 | loadFromMicrocode(mpExternalMicrocode);
|
---|
| 51 | }
|
---|
| 52 | else
|
---|
| 53 | {
|
---|
| 54 | // Normal load-from-source approach
|
---|
| 55 | if (mLoadFromFile)
|
---|
| 56 | {
|
---|
| 57 | // find & load source code
|
---|
| 58 | DataStreamPtr stream =
|
---|
| 59 | ResourceGroupManager::getSingleton().openResource(
|
---|
| 60 | mFilename, mGroup, true, this);
|
---|
| 61 | mSource = stream->getAsString();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | // Call polymorphic load
|
---|
| 65 | loadFromSource();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | }
|
---|
| 69 | //-----------------------------------------------------------------------------
|
---|
| 70 | void D3D9GpuProgram::loadFromSource(void)
|
---|
| 71 | {
|
---|
| 72 | // Create the shader
|
---|
| 73 | // Assemble source into microcode
|
---|
| 74 | LPD3DXBUFFER microcode;
|
---|
| 75 | LPD3DXBUFFER errors;
|
---|
| 76 | HRESULT hr = D3DXAssembleShader(
|
---|
| 77 | mSource.c_str(),
|
---|
| 78 | static_cast<UINT>(mSource.length()),
|
---|
| 79 | NULL, // no #define support
|
---|
| 80 | NULL, // no #include support
|
---|
| 81 | 0, // standard compile options
|
---|
| 82 | µcode,
|
---|
| 83 | &errors);
|
---|
| 84 |
|
---|
| 85 | if (FAILED(hr))
|
---|
| 86 | {
|
---|
| 87 | String message = "Cannot assemble D3D9 shader " + mName + " Errors:\n" +
|
---|
| 88 | static_cast<const char*>(errors->GetBufferPointer());
|
---|
| 89 | errors->Release();
|
---|
| 90 | OGRE_EXCEPT(hr, message,
|
---|
| 91 | "D3D9GpuProgram::loadFromSource");
|
---|
| 92 |
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | loadFromMicrocode(microcode);
|
---|
| 96 |
|
---|
| 97 | SAFE_RELEASE(microcode);
|
---|
| 98 | SAFE_RELEASE(errors);
|
---|
| 99 | }
|
---|
| 100 | //-----------------------------------------------------------------------------
|
---|
| 101 | D3D9GpuVertexProgram::D3D9GpuVertexProgram(ResourceManager* creator,
|
---|
| 102 | const String& name, ResourceHandle handle, const String& group,
|
---|
| 103 | bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev)
|
---|
| 104 | : D3D9GpuProgram(creator, name, handle, group, isManual, loader, pDev)
|
---|
| 105 | , mpVertexShader(NULL)
|
---|
| 106 | {
|
---|
| 107 | mType = GPT_VERTEX_PROGRAM;
|
---|
| 108 | }
|
---|
| 109 | //-----------------------------------------------------------------------------
|
---|
| 110 | D3D9GpuVertexProgram::~D3D9GpuVertexProgram()
|
---|
| 111 | {
|
---|
| 112 | // have to call this here reather than in Resource destructor
|
---|
| 113 | // since calling virtual methods in base destructors causes crash
|
---|
| 114 | unload();
|
---|
| 115 | }
|
---|
| 116 | //-----------------------------------------------------------------------------
|
---|
| 117 | void D3D9GpuVertexProgram::loadFromMicrocode(LPD3DXBUFFER microcode)
|
---|
| 118 | {
|
---|
| 119 | if (isSupported())
|
---|
| 120 | {
|
---|
| 121 | // Create the shader
|
---|
| 122 | HRESULT hr = mpDevice->CreateVertexShader(
|
---|
| 123 | static_cast<DWORD*>(microcode->GetBufferPointer()),
|
---|
| 124 | &mpVertexShader);
|
---|
| 125 |
|
---|
| 126 | if (FAILED(hr))
|
---|
| 127 | {
|
---|
| 128 | OGRE_EXCEPT(hr, "Cannot create D3D9 vertex shader " + mName + " from microcode.",
|
---|
| 129 | "D3D9GpuVertexProgram::loadFromMicrocode");
|
---|
| 130 |
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | else
|
---|
| 134 | {
|
---|
| 135 | LogManager::getSingleton().logMessage(
|
---|
| 136 | "Unsupported D3D9 vertex shader '" + mName + "' was not loaded.");
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | //-----------------------------------------------------------------------------
|
---|
| 140 | void D3D9GpuVertexProgram::unloadImpl(void)
|
---|
| 141 | {
|
---|
| 142 | SAFE_RELEASE(mpVertexShader);
|
---|
| 143 | }
|
---|
| 144 | //-----------------------------------------------------------------------------
|
---|
| 145 | //-----------------------------------------------------------------------------
|
---|
| 146 | D3D9GpuFragmentProgram::D3D9GpuFragmentProgram(ResourceManager* creator,
|
---|
| 147 | const String& name, ResourceHandle handle, const String& group,
|
---|
| 148 | bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev)
|
---|
| 149 | : D3D9GpuProgram(creator, name, handle, group, isManual, loader, pDev)
|
---|
| 150 | , mpPixelShader(NULL)
|
---|
| 151 | {
|
---|
| 152 | mType = GPT_FRAGMENT_PROGRAM;
|
---|
| 153 | }
|
---|
| 154 | //-----------------------------------------------------------------------------
|
---|
| 155 | D3D9GpuFragmentProgram::~D3D9GpuFragmentProgram()
|
---|
| 156 | {
|
---|
| 157 | // have to call this here reather than in Resource destructor
|
---|
| 158 | // since calling virtual methods in base destructors causes crash
|
---|
| 159 | unload();
|
---|
| 160 | }
|
---|
| 161 | //-----------------------------------------------------------------------------
|
---|
| 162 | void D3D9GpuFragmentProgram::loadFromMicrocode(LPD3DXBUFFER microcode)
|
---|
| 163 | {
|
---|
| 164 | if (isSupported())
|
---|
| 165 | {
|
---|
| 166 | // Create the shader
|
---|
| 167 | HRESULT hr = mpDevice->CreatePixelShader(
|
---|
| 168 | static_cast<DWORD*>(microcode->GetBufferPointer()),
|
---|
| 169 | &mpPixelShader);
|
---|
| 170 |
|
---|
| 171 | if (FAILED(hr))
|
---|
| 172 | {
|
---|
| 173 | OGRE_EXCEPT(hr, "Cannot create D3D9 pixel shader " + mName + " from microcode.",
|
---|
| 174 | "D3D9GpuFragmentProgram::loadFromMicrocode");
|
---|
| 175 |
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | else
|
---|
| 179 | {
|
---|
| 180 | LogManager::getSingleton().logMessage(
|
---|
| 181 | "Unsupported D3D9 pixel shader '" + mName + "' was not loaded.");
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | //-----------------------------------------------------------------------------
|
---|
| 185 | void D3D9GpuFragmentProgram::unloadImpl(void)
|
---|
| 186 | {
|
---|
| 187 | SAFE_RELEASE(mpPixelShader);
|
---|
| 188 | }
|
---|
| 189 | //-----------------------------------------------------------------------------
|
---|
| 190 |
|
---|
| 191 | }
|
---|
| 192 |
|
---|