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-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #include "OgreD3D11GpuProgram.h"
29 #include "OgreD3D11Mappings.h"
30 #include "OgreD3D11Device.h"
31 #include "OgreException.h"
32 
33 namespace Ogre {
34 	//-----------------------------------------------------------------------------
D3D11GpuProgram(ResourceManager * creator,const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader,D3D11Device & device)35 	D3D11GpuProgram::D3D11GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
36 		const String& group, bool isManual, ManualResourceLoader* loader, D3D11Device & device)
37 		: GpuProgram(creator, name, handle, group, isManual, loader),
38 		mDevice(device)
39 	{
40 		if (createParamDictionary("D3D11GpuProgram"))
41 		{
42 			setupBaseParamDictionary();
43 		}
44 	}
45 	//-----------------------------------------------------------------------------
loadImpl(void)46 	void D3D11GpuProgram::loadImpl(void)
47 	{
48 		// Normal load-from-source approach
49 		if (mLoadFromFile)
50 		{
51 			// find & load source code
52 			DataStreamPtr stream =
53 				ResourceGroupManager::getSingleton().openResource(
54 				mFilename, mGroup, true, this);
55 			mSource = stream->getAsString();
56 		}
57 
58 		// Call polymorphic load
59 		loadFromSource();
60 	}
61 	//-----------------------------------------------------------------------------
loadFromSource(void)62 	void D3D11GpuProgram::loadFromSource(void)
63 	{
64 		String message = "AIZ:D3D11 dosn't support assembly shaders. Shader name:" + mName + "\n" ;
65 		OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, message,
66 			"D3D11GpuProgram::loadFromSource");
67 	}
68 	//-----------------------------------------------------------------------------
D3D11GpuVertexProgram(ResourceManager * creator,const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader,D3D11Device & device)69 	D3D11GpuVertexProgram::D3D11GpuVertexProgram(ResourceManager* creator,
70 		const String& name, ResourceHandle handle, const String& group,
71 		bool isManual, ManualResourceLoader* loader, D3D11Device & device)
72 		: D3D11GpuProgram(creator, name, handle, group, isManual, loader, device)
73 		, mVertexShader(NULL)
74 	{
75 		mType = GPT_VERTEX_PROGRAM;
76 	}
77 	//-----------------------------------------------------------------------------
~D3D11GpuVertexProgram()78 	D3D11GpuVertexProgram::~D3D11GpuVertexProgram()
79 	{
80 		// have to call this here reather than in Resource destructor
81 		// since calling virtual methods in base destructors causes crash
82 		unload();
83 	}
84 	//-----------------------------------------------------------------------------
loadFromMicrocode(ID3D10Blob * microcode)85 	void D3D11GpuVertexProgram::loadFromMicrocode(ID3D10Blob *  microcode)
86 	{
87 		if (isSupported())
88 		{
89 			// Create the shader
90 			HRESULT hr = mDevice->CreateVertexShader(
91 				static_cast<DWORD*>(microcode->GetBufferPointer()),
92 				microcode->GetBufferSize(),
93 				NULL,
94 				&mVertexShader);
95 
96 			if (FAILED(hr) || mDevice.isError())
97 			{
98 				String errorDescription = mDevice.getErrorDescription(hr);
99 				OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
100 					"Cannot create D3D11 vertex shader " + mName + " from microcode\nError Description:" + errorDescription,
101 					"D3D11GpuVertexProgram::loadFromMicrocode");
102 
103 			}
104 		}
105 		else
106 		{
107 			LogManager::getSingleton().logMessage(
108 				"Unsupported D3D11 vertex shader '" + mName + "' was not loaded.");
109 		}
110 	}
111 	//-----------------------------------------------------------------------------
unloadImpl(void)112 	void D3D11GpuVertexProgram::unloadImpl(void)
113 	{
114 		SAFE_RELEASE(mVertexShader);
115 	}
116 	//-----------------------------------------------------------------------------
getVertexShader(void) const117 	ID3D11VertexShader * D3D11GpuVertexProgram::getVertexShader( void ) const
118 	{
119 		return mVertexShader;
120 	}
121 	//-----------------------------------------------------------------------------
D3D11GpuFragmentProgram(ResourceManager * creator,const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader,D3D11Device & device)122 	D3D11GpuFragmentProgram::D3D11GpuFragmentProgram(ResourceManager* creator,
123 		const String& name, ResourceHandle handle, const String& group,
124 		bool isManual, ManualResourceLoader* loader, D3D11Device & device)
125 		: D3D11GpuProgram(creator, name, handle, group, isManual, loader, device)
126 		, mPixelShader(NULL)
127 	{
128 		mType = GPT_FRAGMENT_PROGRAM;
129 	}
130 	//-----------------------------------------------------------------------------
~D3D11GpuFragmentProgram()131 	D3D11GpuFragmentProgram::~D3D11GpuFragmentProgram()
132 	{
133 		// have to call this here reather than in Resource destructor
134 		// since calling virtual methods in base destructors causes crash
135 		unload();
136 	}
137 	//-----------------------------------------------------------------------------
loadFromMicrocode(ID3D10Blob * microcode)138 	void D3D11GpuFragmentProgram::loadFromMicrocode(ID3D10Blob *  microcode)
139 	{
140 		if (isSupported())
141 		{
142 			// Create the shader
143 			HRESULT hr = mDevice->CreatePixelShader(
144 				static_cast<DWORD*>(microcode->GetBufferPointer()),
145 				microcode->GetBufferSize(),
146 				NULL,
147 				&mPixelShader);
148 
149 			if (FAILED(hr) || mDevice.isError())
150 			{
151 				String errorDescription = mDevice.getErrorDescription(hr);
152 				OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
153 					"Cannot create D3D11 pixel shader " + mName + " from microcode.\nError Description:" + errorDescription,
154 					"D3D11GpuFragmentProgram::loadFromMicrocode");
155 			}
156 		}
157 		else
158 		{
159 			LogManager::getSingleton().logMessage(
160 				"Unsupported D3D11 pixel shader '" + mName + "' was not loaded.");
161 		}
162 	}
163 	//-----------------------------------------------------------------------------
unloadImpl(void)164 	void D3D11GpuFragmentProgram::unloadImpl(void)
165 	{
166 		SAFE_RELEASE(mPixelShader);
167 	}
168 	//-----------------------------------------------------------------------------
getPixelShader(void) const169 	ID3D11PixelShader * D3D11GpuFragmentProgram::getPixelShader( void ) const
170 	{
171 		return mPixelShader;
172 	}
173 	//-----------------------------------------------------------------------------
D3D11GpuGeometryProgram(ResourceManager * creator,const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader,D3D11Device & device)174 	D3D11GpuGeometryProgram::D3D11GpuGeometryProgram(ResourceManager* creator,
175 		const String& name, ResourceHandle handle, const String& group,
176 		bool isManual, ManualResourceLoader* loader, D3D11Device & device)
177 		: D3D11GpuProgram(creator, name, handle, group, isManual, loader, device)
178 		, mGeometryShader(NULL)
179 	{
180 		mType = GPT_GEOMETRY_PROGRAM;
181 	}
182 	//-----------------------------------------------------------------------------
~D3D11GpuGeometryProgram()183 	D3D11GpuGeometryProgram::~D3D11GpuGeometryProgram()
184 	{
185 		// have to call this here reather than in Resource destructor
186 		// since calling virtual methods in base destructors causes crash
187 		unload();
188 	}
189 	//-----------------------------------------------------------------------------
loadFromMicrocode(ID3D10Blob * microcode)190 	void D3D11GpuGeometryProgram::loadFromMicrocode(ID3D10Blob *  microcode)
191 	{
192 		if (isSupported())
193 		{
194 			// Create the shader
195 			HRESULT hr = mDevice->CreateGeometryShader(
196 				static_cast<DWORD*>(microcode->GetBufferPointer()),
197 				microcode->GetBufferSize(),
198 				NULL,
199 				&mGeometryShader);
200 
201 			if (FAILED(hr) || mDevice.isError())
202 			{
203 				String errorDescription = mDevice.getErrorDescription(hr);
204 				OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
205 					"Cannot create D3D11 geometry shader " + mName + " from microcode.\nError Description:" + errorDescription,
206 					"D3D11GpuGeometryProgram::loadFromMicrocode");
207 			}
208 		}
209 		else
210 		{
211 			LogManager::getSingleton().logMessage(
212 				"Unsupported D3D11 geometry shader '" + mName + "' was not loaded.");
213 		}
214 	}
215 	//-----------------------------------------------------------------------------
unloadImpl(void)216 	void D3D11GpuGeometryProgram::unloadImpl(void)
217 	{
218 		SAFE_RELEASE(mGeometryShader);
219 	}
220 	//-----------------------------------------------------------------------------
getGeometryShader(void) const221 	ID3D11GeometryShader * D3D11GpuGeometryProgram::getGeometryShader( void ) const
222 	{
223 		return mGeometryShader;
224 	}
225 	//-----------------------------------------------------------------------------
D3D11GpuDomainProgram(ResourceManager * creator,const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader,D3D11Device & device)226 	D3D11GpuDomainProgram::D3D11GpuDomainProgram(ResourceManager* creator,
227 		const String& name, ResourceHandle handle, const String& group,
228 		bool isManual, ManualResourceLoader* loader, D3D11Device & device)
229 		: D3D11GpuProgram(creator, name, handle, group, isManual, loader, device)
230 		, mDomainShader(NULL)
231 	{
232 		mType = GPT_DOMAIN_PROGRAM;
233 	}
234 	//-----------------------------------------------------------------------------
~D3D11GpuDomainProgram()235 	D3D11GpuDomainProgram::~D3D11GpuDomainProgram()
236 	{
237 		// have to call this here reather than in Resource destructor
238 		// since calling virtual methods in base destructors causes crash
239 		unload();
240 	}
241 	//-----------------------------------------------------------------------------
loadFromMicrocode(ID3D10Blob * microcode)242 	void D3D11GpuDomainProgram::loadFromMicrocode(ID3D10Blob *  microcode)
243 	{
244 		if (isSupported())
245 		{
246 			// Create the shader
247 			HRESULT hr = mDevice->CreateDomainShader(
248 				static_cast<DWORD*>(microcode->GetBufferPointer()),
249 				microcode->GetBufferSize(),
250 				NULL,
251 				&mDomainShader);
252 
253 			if (FAILED(hr) || mDevice.isError())
254 			{
255 				String errorDescription = mDevice.getErrorDescription(hr);
256 				OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
257 					"Cannot create D3D11 domain shader " + mName + " from microcode.\nError Description:" + errorDescription,
258 					"D3D11GpuFragmentProgram::loadFromMicrocode");
259 			}
260 		}
261 		else
262 		{
263 			LogManager::getSingleton().logMessage(
264 				"Unsupported D3D11 domain shader '" + mName + "' was not loaded.");
265 		}
266 	}
267 	//-----------------------------------------------------------------------------
unloadImpl(void)268 	void D3D11GpuDomainProgram::unloadImpl(void)
269 	{
270 		SAFE_RELEASE(mDomainShader);
271 	}
272 	//-----------------------------------------------------------------------------
getDomainShader(void) const273 	ID3D11DomainShader * D3D11GpuDomainProgram::getDomainShader( void ) const
274 	{
275 		return mDomainShader;
276 	}
277 	//-----------------------------------------------------------------------------
D3D11GpuHullProgram(ResourceManager * creator,const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader,D3D11Device & device)278 	D3D11GpuHullProgram::D3D11GpuHullProgram(ResourceManager* creator,
279 		const String& name, ResourceHandle handle, const String& group,
280 		bool isManual, ManualResourceLoader* loader, D3D11Device & device)
281 		: D3D11GpuProgram(creator, name, handle, group, isManual, loader, device)
282 		, mHullShader(NULL)
283 	{
284 		mType = GPT_HULL_PROGRAM;
285 	}
286 	//-----------------------------------------------------------------------------
~D3D11GpuHullProgram()287 	D3D11GpuHullProgram::~D3D11GpuHullProgram()
288 	{
289 		// have to call this here reather than in Resource destructor
290 		// since calling virtual methods in base destructors causes crash
291 		unload();
292 	}
293 	//-----------------------------------------------------------------------------
loadFromMicrocode(ID3D10Blob * microcode)294 	void D3D11GpuHullProgram::loadFromMicrocode(ID3D10Blob *  microcode)
295 	{
296 		if (isSupported())
297 		{
298 			// Create the shader
299 			HRESULT hr = mDevice->CreateHullShader(
300 				static_cast<DWORD*>(microcode->GetBufferPointer()),
301 				microcode->GetBufferSize(),
302 				NULL,
303 				&mHullShader);
304 
305 			if (FAILED(hr) || mDevice.isError())
306 			{
307 				String errorDescription = mDevice.getErrorDescription(hr);
308 				OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
309 					"Cannot create D3D11 hull shader " + mName + " from microcode.\nError Description:" + errorDescription,
310 					"D3D11GpuFragmentProgram::loadFromMicrocode");
311 			}
312 		}
313 		else
314 		{
315 			LogManager::getSingleton().logMessage(
316 				"Unsupported D3D11 hull shader '" + mName + "' was not loaded.");
317 		}
318 	}
319 	//-----------------------------------------------------------------------------
unloadImpl(void)320 	void D3D11GpuHullProgram::unloadImpl(void)
321 	{
322 		SAFE_RELEASE(mHullShader);
323 	}
324 	//-----------------------------------------------------------------------------
getHullShader(void) const325 	ID3D11HullShader * D3D11GpuHullProgram::getHullShader( void ) const
326 	{
327 		return mHullShader;
328 	}
329 	//-----------------------------------------------------------------------------
330 }
331 
332