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-2006 Torus Knot Software Ltd |
---|
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 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #include "OgreD3D9GpuProgram.h" |
---|
30 | #include "OgreMatrix4.h" |
---|
31 | #include "OgreException.h" |
---|
32 | #include "OgreLogManager.h" |
---|
33 | #include "OgreD3D9Mappings.h" |
---|
34 | #include "OgreResourceGroupManager.h" |
---|
35 | |
---|
36 | namespace Ogre { |
---|
37 | |
---|
38 | //----------------------------------------------------------------------------- |
---|
39 | D3D9GpuProgram::D3D9GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle, |
---|
40 | const String& group, bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev) |
---|
41 | : GpuProgram(creator, name, handle, group, isManual, loader), |
---|
42 | mpDevice(pDev), mpExternalMicrocode(NULL) |
---|
43 | { |
---|
44 | if (createParamDictionary("D3D9GpuProgram")) |
---|
45 | { |
---|
46 | setupBaseParamDictionary(); |
---|
47 | } |
---|
48 | } |
---|
49 | //----------------------------------------------------------------------------- |
---|
50 | void D3D9GpuProgram::loadImpl(void) |
---|
51 | { |
---|
52 | if (mpExternalMicrocode) |
---|
53 | { |
---|
54 | loadFromMicrocode(mpExternalMicrocode); |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | // Normal load-from-source approach |
---|
59 | if (mLoadFromFile) |
---|
60 | { |
---|
61 | // find & load source code |
---|
62 | DataStreamPtr stream = |
---|
63 | ResourceGroupManager::getSingleton().openResource( |
---|
64 | mFilename, mGroup, true, this); |
---|
65 | mSource = stream->getAsString(); |
---|
66 | } |
---|
67 | |
---|
68 | // Call polymorphic load |
---|
69 | loadFromSource(); |
---|
70 | } |
---|
71 | |
---|
72 | } |
---|
73 | //----------------------------------------------------------------------------- |
---|
74 | void D3D9GpuProgram::loadFromSource(void) |
---|
75 | { |
---|
76 | // Create the shader |
---|
77 | // Assemble source into microcode |
---|
78 | LPD3DXBUFFER microcode; |
---|
79 | LPD3DXBUFFER errors; |
---|
80 | HRESULT hr = D3DXAssembleShader( |
---|
81 | mSource.c_str(), |
---|
82 | static_cast<UINT>(mSource.length()), |
---|
83 | NULL, // no #define support |
---|
84 | NULL, // no #include support |
---|
85 | 0, // standard compile options |
---|
86 | µcode, |
---|
87 | &errors); |
---|
88 | |
---|
89 | if (FAILED(hr)) |
---|
90 | { |
---|
91 | String message = "Cannot assemble D3D9 shader " + mName + " Errors:\n" + |
---|
92 | static_cast<const char*>(errors->GetBufferPointer()); |
---|
93 | errors->Release(); |
---|
94 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, message, |
---|
95 | "D3D9GpuProgram::loadFromSource"); |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | loadFromMicrocode(microcode); |
---|
100 | |
---|
101 | SAFE_RELEASE(microcode); |
---|
102 | SAFE_RELEASE(errors); |
---|
103 | } |
---|
104 | //----------------------------------------------------------------------------- |
---|
105 | D3D9GpuVertexProgram::D3D9GpuVertexProgram(ResourceManager* creator, |
---|
106 | const String& name, ResourceHandle handle, const String& group, |
---|
107 | bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev) |
---|
108 | : D3D9GpuProgram(creator, name, handle, group, isManual, loader, pDev) |
---|
109 | , mpVertexShader(NULL) |
---|
110 | { |
---|
111 | mType = GPT_VERTEX_PROGRAM; |
---|
112 | } |
---|
113 | //----------------------------------------------------------------------------- |
---|
114 | D3D9GpuVertexProgram::~D3D9GpuVertexProgram() |
---|
115 | { |
---|
116 | // have to call this here reather than in Resource destructor |
---|
117 | // since calling virtual methods in base destructors causes crash |
---|
118 | unload(); |
---|
119 | } |
---|
120 | //----------------------------------------------------------------------------- |
---|
121 | void D3D9GpuVertexProgram::loadFromMicrocode(LPD3DXBUFFER microcode) |
---|
122 | { |
---|
123 | if (isSupported()) |
---|
124 | { |
---|
125 | // Create the shader |
---|
126 | HRESULT hr = mpDevice->CreateVertexShader( |
---|
127 | static_cast<DWORD*>(microcode->GetBufferPointer()), |
---|
128 | &mpVertexShader); |
---|
129 | |
---|
130 | if (FAILED(hr)) |
---|
131 | { |
---|
132 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, |
---|
133 | "Cannot create D3D9 vertex shader " + mName + " from microcode", |
---|
134 | "D3D9GpuVertexProgram::loadFromMicrocode"); |
---|
135 | |
---|
136 | } |
---|
137 | } |
---|
138 | else |
---|
139 | { |
---|
140 | LogManager::getSingleton().logMessage( |
---|
141 | "Unsupported D3D9 vertex shader '" + mName + "' was not loaded."); |
---|
142 | } |
---|
143 | } |
---|
144 | //----------------------------------------------------------------------------- |
---|
145 | void D3D9GpuVertexProgram::unloadImpl(void) |
---|
146 | { |
---|
147 | SAFE_RELEASE(mpVertexShader); |
---|
148 | } |
---|
149 | //----------------------------------------------------------------------------- |
---|
150 | //----------------------------------------------------------------------------- |
---|
151 | D3D9GpuFragmentProgram::D3D9GpuFragmentProgram(ResourceManager* creator, |
---|
152 | const String& name, ResourceHandle handle, const String& group, |
---|
153 | bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev) |
---|
154 | : D3D9GpuProgram(creator, name, handle, group, isManual, loader, pDev) |
---|
155 | , mpPixelShader(NULL) |
---|
156 | { |
---|
157 | mType = GPT_FRAGMENT_PROGRAM; |
---|
158 | } |
---|
159 | //----------------------------------------------------------------------------- |
---|
160 | D3D9GpuFragmentProgram::~D3D9GpuFragmentProgram() |
---|
161 | { |
---|
162 | // have to call this here reather than in Resource destructor |
---|
163 | // since calling virtual methods in base destructors causes crash |
---|
164 | unload(); |
---|
165 | } |
---|
166 | //----------------------------------------------------------------------------- |
---|
167 | void D3D9GpuFragmentProgram::loadFromMicrocode(LPD3DXBUFFER microcode) |
---|
168 | { |
---|
169 | if (isSupported()) |
---|
170 | { |
---|
171 | // Create the shader |
---|
172 | HRESULT hr = mpDevice->CreatePixelShader( |
---|
173 | static_cast<DWORD*>(microcode->GetBufferPointer()), |
---|
174 | &mpPixelShader); |
---|
175 | |
---|
176 | if (FAILED(hr)) |
---|
177 | { |
---|
178 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, |
---|
179 | "Cannot create D3D9 pixel shader " + mName + " from microcode.", |
---|
180 | "D3D9GpuFragmentProgram::loadFromMicrocode"); |
---|
181 | |
---|
182 | } |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | LogManager::getSingleton().logMessage( |
---|
187 | "Unsupported D3D9 pixel shader '" + mName + "' was not loaded."); |
---|
188 | } |
---|
189 | } |
---|
190 | //----------------------------------------------------------------------------- |
---|
191 | void D3D9GpuFragmentProgram::unloadImpl(void) |
---|
192 | { |
---|
193 | SAFE_RELEASE(mpPixelShader); |
---|
194 | } |
---|
195 | //----------------------------------------------------------------------------- |
---|
196 | |
---|
197 | } |
---|
198 | |
---|