2
0

CmD3D9GpuProgram.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmD3D9GpuProgram.h"
  25. #include "CmMatrix4.h"
  26. #include "CmException.h"
  27. #include "CmD3D9Mappings.h"
  28. #include "CmD3D9RenderSystem.h"
  29. namespace CamelotEngine {
  30. //-----------------------------------------------------------------------------
  31. D3D9GpuProgram::D3D9GpuProgram()
  32. : GpuProgram(), mpExternalMicrocode(NULL), mColumnMajorMatrices(false)
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. D3D9GpuProgram::~D3D9GpuProgram()
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. void D3D9GpuProgram::setExternalMicrocode(const void* pMicrocode, size_t size)
  41. {
  42. LPD3DXBUFFER pBuffer=0;
  43. HRESULT hr=D3DXCreateBuffer(size, &pBuffer);
  44. if(pBuffer)
  45. {
  46. memcpy(pBuffer->GetBufferPointer(), pMicrocode, size);
  47. this->setExternalMicrocode(pBuffer);
  48. SAFE_RELEASE(pBuffer);
  49. }
  50. }
  51. //-----------------------------------------------------------------------------
  52. void D3D9GpuProgram::setExternalMicrocode(ID3DXBuffer* pMicrocode)
  53. {
  54. SAFE_RELEASE(mpExternalMicrocode);
  55. mpExternalMicrocode = pMicrocode;
  56. if(mpExternalMicrocode)
  57. {
  58. mpExternalMicrocode->AddRef();
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. LPD3DXBUFFER D3D9GpuProgram::getExternalMicrocode(void)
  63. {
  64. return mpExternalMicrocode;
  65. }
  66. //-----------------------------------------------------------------------------
  67. void D3D9GpuProgram::load(void)
  68. {
  69. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  70. for (UINT32 i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  71. {
  72. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  73. load(d3d9Device);
  74. }
  75. }
  76. //-----------------------------------------------------------------------------
  77. void D3D9GpuProgram::load(IDirect3DDevice9* d3d9Device)
  78. {
  79. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  80. if (mpExternalMicrocode)
  81. {
  82. loadFromMicrocode(d3d9Device, mpExternalMicrocode);
  83. }
  84. else
  85. {
  86. // Call polymorphic load
  87. loadFromSource(d3d9Device);
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. void D3D9GpuProgram::unload(void)
  92. {
  93. SAFE_RELEASE(mpExternalMicrocode);
  94. }
  95. //-----------------------------------------------------------------------------
  96. void D3D9GpuProgram::loadFromSource(void)
  97. {
  98. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  99. for (UINT32 i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  100. {
  101. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  102. loadFromSource(d3d9Device);
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. void D3D9GpuProgram::loadFromSource(IDirect3DDevice9* d3d9Device)
  107. {
  108. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  109. // Populate compile flags
  110. DWORD compileFlags = 0;
  111. // Create the shader
  112. // Assemble source into microcode
  113. LPD3DXBUFFER microcode;
  114. LPD3DXBUFFER errors;
  115. HRESULT hr = D3DXAssembleShader(
  116. mSource.c_str(),
  117. static_cast<UINT>(mSource.length()),
  118. NULL, // no #define support
  119. NULL, // no #include support
  120. compileFlags, // standard compile options
  121. &microcode,
  122. &errors);
  123. if (FAILED(hr))
  124. {
  125. String message = "Cannot assemble D3D9 shader. Errors:\n";
  126. message += static_cast<const char*>(errors->GetBufferPointer());
  127. errors->Release();
  128. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, message,
  129. "D3D9GpuProgram::loadFromSource");
  130. }
  131. loadFromMicrocode(d3d9Device, microcode);
  132. SAFE_RELEASE(microcode);
  133. SAFE_RELEASE(errors);
  134. }
  135. //-----------------------------------------------------------------------
  136. GpuProgramParametersSharedPtr D3D9GpuProgram::createParameters(void)
  137. {
  138. // Call superclass
  139. GpuProgramParametersSharedPtr params = GpuProgram::createParameters();
  140. // Need to transpose matrices if compiled with column-major matrices
  141. params->setTransposeMatrices(mColumnMajorMatrices);
  142. return params;
  143. }
  144. //-----------------------------------------------------------------------------
  145. D3D9GpuVertexProgram::D3D9GpuVertexProgram()
  146. : D3D9GpuProgram()
  147. {
  148. mType = GPT_VERTEX_PROGRAM;
  149. }
  150. //-----------------------------------------------------------------------------
  151. D3D9GpuVertexProgram::~D3D9GpuVertexProgram()
  152. {
  153. }
  154. //-----------------------------------------------------------------------------
  155. void D3D9GpuVertexProgram::loadFromMicrocode(IDirect3DDevice9* d3d9Device, ID3DXBuffer* microcode)
  156. {
  157. DeviceToVertexShaderIterator it = mMapDeviceToVertexShader.find(d3d9Device);
  158. if (it != mMapDeviceToVertexShader.end())
  159. SAFE_RELEASE(it->second);
  160. if (isSupported())
  161. {
  162. // Create the shader
  163. IDirect3DVertexShader9* pVertexShader;
  164. HRESULT hr;
  165. hr = d3d9Device->CreateVertexShader(
  166. static_cast<DWORD*>(microcode->GetBufferPointer()),
  167. &pVertexShader);
  168. if (FAILED(hr))
  169. {
  170. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  171. "Cannot create D3D9 vertex shader from microcode",
  172. "D3D9GpuVertexProgram::loadFromMicrocode");
  173. }
  174. mMapDeviceToVertexShader[d3d9Device] = pVertexShader;
  175. }
  176. else
  177. {
  178. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  179. "Specified D3D9 vertex shader is not supported!",
  180. "D3D9GpuVertexProgram::loadFromMicrocode");
  181. // TODO LOG PORT - Log this error somewhere
  182. //LogManager::getSingleton().logMessage(
  183. // "Unsupported D3D9 vertex shader '" + mName + "' was not loaded.");
  184. mMapDeviceToVertexShader[d3d9Device] = NULL;
  185. }
  186. }
  187. //-----------------------------------------------------------------------------
  188. void D3D9GpuVertexProgram::unload(void)
  189. {
  190. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  191. DeviceToVertexShaderIterator it = mMapDeviceToVertexShader.begin();
  192. while (it != mMapDeviceToVertexShader.end())
  193. {
  194. SAFE_RELEASE(it->second);
  195. ++it;
  196. }
  197. mMapDeviceToVertexShader.clear();
  198. D3D9GpuProgram::unload();
  199. }
  200. //-----------------------------------------------------------------------------
  201. void D3D9GpuVertexProgram::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  202. {
  203. }
  204. //-----------------------------------------------------------------------------
  205. void D3D9GpuVertexProgram::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  206. {
  207. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  208. DeviceToVertexShaderIterator it;
  209. // Find the shader of this device.
  210. it = mMapDeviceToVertexShader.find(d3d9Device);
  211. // Case shader found -> release it and erase from map.
  212. if (it != mMapDeviceToVertexShader.end())
  213. {
  214. SAFE_RELEASE(it->second);
  215. mMapDeviceToVertexShader.erase(it);
  216. }
  217. }
  218. //-----------------------------------------------------------------------------
  219. IDirect3DVertexShader9* D3D9GpuVertexProgram::getVertexShader( void )
  220. {
  221. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  222. DeviceToVertexShaderIterator it;
  223. // Find the shader of this device.
  224. it = mMapDeviceToVertexShader.find(d3d9Device);
  225. // Shader was not found -> load it.
  226. if (it == mMapDeviceToVertexShader.end())
  227. {
  228. load(d3d9Device);
  229. it = mMapDeviceToVertexShader.find(d3d9Device);
  230. }
  231. return it->second;
  232. }
  233. //-----------------------------------------------------------------------------
  234. //-----------------------------------------------------------------------------
  235. D3D9GpuFragmentProgram::D3D9GpuFragmentProgram()
  236. : D3D9GpuProgram()
  237. {
  238. mType = GPT_FRAGMENT_PROGRAM;
  239. }
  240. //-----------------------------------------------------------------------------
  241. D3D9GpuFragmentProgram::~D3D9GpuFragmentProgram()
  242. {
  243. }
  244. //-----------------------------------------------------------------------------
  245. void D3D9GpuFragmentProgram::loadFromMicrocode(IDirect3DDevice9* d3d9Device, ID3DXBuffer* microcode)
  246. {
  247. DeviceToPixelShaderIterator it = mMapDeviceToPixelShader.find(d3d9Device);
  248. if (it != mMapDeviceToPixelShader.end())
  249. SAFE_RELEASE(it->second);
  250. if (isSupported())
  251. {
  252. // Create the shader
  253. IDirect3DPixelShader9* pPixelShader;
  254. HRESULT hr;
  255. hr = d3d9Device->CreatePixelShader(
  256. static_cast<DWORD*>(microcode->GetBufferPointer()),
  257. &pPixelShader);
  258. if (FAILED(hr))
  259. {
  260. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  261. "Cannot create D3D9 pixel shader from microcode.",
  262. "D3D9GpuFragmentProgram::loadFromMicrocode");
  263. }
  264. mMapDeviceToPixelShader[d3d9Device] = pPixelShader;
  265. }
  266. else
  267. {
  268. // TODO LOG PORT - Log this somewhere
  269. //LogManager::getSingleton().logMessage(
  270. // "Unsupported D3D9 pixel shader was not loaded.");
  271. mMapDeviceToPixelShader[d3d9Device] = NULL;
  272. }
  273. }
  274. //-----------------------------------------------------------------------------
  275. void D3D9GpuFragmentProgram::unload(void)
  276. {
  277. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  278. DeviceToPixelShaderIterator it = mMapDeviceToPixelShader.begin();
  279. while (it != mMapDeviceToPixelShader.end())
  280. {
  281. SAFE_RELEASE(it->second);
  282. ++it;
  283. }
  284. mMapDeviceToPixelShader.clear();
  285. D3D9GpuProgram::unload();
  286. }
  287. //-----------------------------------------------------------------------------
  288. void D3D9GpuFragmentProgram::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  289. {
  290. }
  291. //-----------------------------------------------------------------------------
  292. void D3D9GpuFragmentProgram::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  293. {
  294. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  295. DeviceToPixelShaderIterator it;
  296. // Find the shader of this device.
  297. it = mMapDeviceToPixelShader.find(d3d9Device);
  298. // Case shader found -> release it and erase from map.
  299. if (it != mMapDeviceToPixelShader.end())
  300. {
  301. SAFE_RELEASE(it->second);
  302. mMapDeviceToPixelShader.erase(it);
  303. }
  304. }
  305. //-----------------------------------------------------------------------------
  306. IDirect3DPixelShader9* D3D9GpuFragmentProgram::getPixelShader( void )
  307. {
  308. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  309. DeviceToPixelShaderIterator it;
  310. // Find the shader of this device.
  311. it = mMapDeviceToPixelShader.find(d3d9Device);
  312. // Shader was not found -> load it.
  313. if (it == mMapDeviceToPixelShader.end())
  314. {
  315. load(d3d9Device);
  316. it = mMapDeviceToPixelShader.find(d3d9Device);
  317. }
  318. return it->second;
  319. }
  320. //-----------------------------------------------------------------------------
  321. }