CmD3D9GpuProgram.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #include "CmD3D9ResourceManager.h"
  30. #include "CmGpuParams.h"
  31. #include "CmAsyncOp.h"
  32. namespace CamelotFramework {
  33. //-----------------------------------------------------------------------------
  34. D3D9GpuProgram::D3D9GpuProgram(const String& source, const String& entryPoint, const String& language,
  35. GpuProgramType gptype, GpuProgramProfile profile)
  36. : GpuProgram(source, entryPoint, language, gptype, profile, nullptr), mpExternalMicrocode(NULL), mColumnMajorMatrices(false)
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. D3D9GpuProgram::~D3D9GpuProgram()
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. void D3D9GpuProgram::initialize_internal()
  45. {
  46. for (UINT32 i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  47. {
  48. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  49. createInternalResources(d3d9Device);
  50. }
  51. GpuProgram::initialize_internal();
  52. }
  53. //----------------------------------------------------------------------------
  54. void D3D9GpuProgram::destroy_internal()
  55. {
  56. SAFE_RELEASE(mpExternalMicrocode);
  57. GpuProgram::destroy_internal();
  58. }
  59. //-----------------------------------------------------------------------------
  60. void D3D9GpuProgram::setExternalMicrocode(const void* pMicrocode, UINT32 size)
  61. {
  62. LPD3DXBUFFER pBuffer=0;
  63. HRESULT hr = D3DXCreateBuffer(size, &pBuffer);
  64. if(pBuffer)
  65. {
  66. memcpy(pBuffer->GetBufferPointer(), pMicrocode, size);
  67. this->setExternalMicrocode(pBuffer);
  68. SAFE_RELEASE(pBuffer);
  69. }
  70. }
  71. //-----------------------------------------------------------------------------
  72. void D3D9GpuProgram::setExternalMicrocode(ID3DXBuffer* pMicrocode)
  73. {
  74. SAFE_RELEASE(mpExternalMicrocode);
  75. mpExternalMicrocode = pMicrocode;
  76. if(mpExternalMicrocode)
  77. {
  78. mpExternalMicrocode->AddRef();
  79. }
  80. }
  81. //-----------------------------------------------------------------------------
  82. LPD3DXBUFFER D3D9GpuProgram::getExternalMicrocode(void)
  83. {
  84. return mpExternalMicrocode;
  85. }
  86. //-----------------------------------------------------------------------------
  87. void D3D9GpuProgram::createInternalResources(IDirect3DDevice9* d3d9Device)
  88. {
  89. if (mpExternalMicrocode)
  90. {
  91. loadFromMicrocode(d3d9Device, mpExternalMicrocode);
  92. }
  93. else
  94. {
  95. // Populate compile flags
  96. DWORD compileFlags = 0;
  97. // Create the shader
  98. // Assemble source into microcode
  99. LPD3DXBUFFER microcode;
  100. LPD3DXBUFFER errors;
  101. HRESULT hr = D3DXAssembleShader(
  102. mSource.c_str(),
  103. static_cast<UINT>(mSource.length()),
  104. NULL, // no #define support
  105. NULL, // no #include support
  106. compileFlags, // standard compile options
  107. &microcode,
  108. &errors);
  109. if (FAILED(hr))
  110. {
  111. String message = "Cannot assemble D3D9 shader. Errors:\n";
  112. message += static_cast<const char*>(errors->GetBufferPointer());
  113. errors->Release();
  114. CM_EXCEPT(RenderingAPIException, message);
  115. }
  116. loadFromMicrocode(d3d9Device, microcode);
  117. SAFE_RELEASE(microcode);
  118. SAFE_RELEASE(errors);
  119. }
  120. }
  121. //-----------------------------------------------------------------------
  122. GpuParamsPtr D3D9GpuProgram::createParameters()
  123. {
  124. GpuParamsPtr params = cm_shared_ptr<GpuParams, PoolAlloc>(std::ref(mParametersDesc), mColumnMajorMatrices);
  125. return params;
  126. }
  127. //-----------------------------------------------------------------------------
  128. D3D9GpuVertexProgram::D3D9GpuVertexProgram(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype, GpuProgramProfile profile)
  129. : D3D9GpuProgram(source, entryPoint, language, gptype, profile)
  130. {
  131. mType = GPT_VERTEX_PROGRAM;
  132. }
  133. //-----------------------------------------------------------------------------
  134. D3D9GpuVertexProgram::~D3D9GpuVertexProgram()
  135. {
  136. }
  137. //-----------------------------------------------------------------------------
  138. void D3D9GpuVertexProgram::destroy_internal(void)
  139. {
  140. DeviceToVertexShaderIterator it = mMapDeviceToVertexShader.begin();
  141. while (it != mMapDeviceToVertexShader.end())
  142. {
  143. SAFE_RELEASE(it->second);
  144. ++it;
  145. }
  146. mMapDeviceToVertexShader.clear();
  147. D3D9GpuProgram::destroy_internal();
  148. }
  149. //-----------------------------------------------------------------------------
  150. void D3D9GpuVertexProgram::loadFromMicrocode(IDirect3DDevice9* d3d9Device, ID3DXBuffer* microcode)
  151. {
  152. DeviceToVertexShaderIterator it = mMapDeviceToVertexShader.find(d3d9Device);
  153. if (it != mMapDeviceToVertexShader.end())
  154. SAFE_RELEASE(it->second);
  155. if (isSupported())
  156. {
  157. // Create the shader
  158. IDirect3DVertexShader9* pVertexShader;
  159. HRESULT hr;
  160. hr = d3d9Device->CreateVertexShader(
  161. static_cast<DWORD*>(microcode->GetBufferPointer()),
  162. &pVertexShader);
  163. if (FAILED(hr))
  164. {
  165. CM_EXCEPT(RenderingAPIException, "Cannot create D3D9 vertex shader from microcode");
  166. }
  167. mMapDeviceToVertexShader[d3d9Device] = pVertexShader;
  168. }
  169. else
  170. {
  171. CM_EXCEPT(RenderingAPIException, "Specified D3D9 vertex shader is not supported!");
  172. mMapDeviceToVertexShader[d3d9Device] = NULL;
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. void D3D9GpuVertexProgram::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  177. {
  178. }
  179. //-----------------------------------------------------------------------------
  180. void D3D9GpuVertexProgram::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  181. {
  182. DeviceToVertexShaderIterator it;
  183. // Find the shader of this device.
  184. it = mMapDeviceToVertexShader.find(d3d9Device);
  185. // Case shader found -> release it and erase from map.
  186. if (it != mMapDeviceToVertexShader.end())
  187. {
  188. SAFE_RELEASE(it->second);
  189. mMapDeviceToVertexShader.erase(it);
  190. }
  191. }
  192. //-----------------------------------------------------------------------------
  193. IDirect3DVertexShader9* D3D9GpuVertexProgram::getVertexShader( void )
  194. {
  195. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  196. DeviceToVertexShaderIterator it;
  197. // Find the shader of this device.
  198. it = mMapDeviceToVertexShader.find(d3d9Device);
  199. // Shader was not found -> load it.
  200. if (it == mMapDeviceToVertexShader.end())
  201. {
  202. createInternalResources(d3d9Device);
  203. it = mMapDeviceToVertexShader.find(d3d9Device);
  204. }
  205. return it->second;
  206. }
  207. //-----------------------------------------------------------------------------
  208. D3D9GpuFragmentProgram::D3D9GpuFragmentProgram(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype, GpuProgramProfile profile)
  209. : D3D9GpuProgram(source, entryPoint, language, gptype, profile)
  210. {
  211. mType = GPT_FRAGMENT_PROGRAM;
  212. }
  213. //-----------------------------------------------------------------------------
  214. D3D9GpuFragmentProgram::~D3D9GpuFragmentProgram()
  215. {
  216. }
  217. //-----------------------------------------------------------------------------
  218. void D3D9GpuFragmentProgram::destroy_internal()
  219. {
  220. DeviceToPixelShaderIterator it = mMapDeviceToPixelShader.begin();
  221. while (it != mMapDeviceToPixelShader.end())
  222. {
  223. SAFE_RELEASE(it->second);
  224. ++it;
  225. }
  226. mMapDeviceToPixelShader.clear();
  227. D3D9GpuProgram::destroy_internal();
  228. }
  229. //-----------------------------------------------------------------------------
  230. void D3D9GpuFragmentProgram::loadFromMicrocode(IDirect3DDevice9* d3d9Device, ID3DXBuffer* microcode)
  231. {
  232. DeviceToPixelShaderIterator it = mMapDeviceToPixelShader.find(d3d9Device);
  233. if (it != mMapDeviceToPixelShader.end())
  234. SAFE_RELEASE(it->second);
  235. if (isSupported())
  236. {
  237. // Create the shader
  238. IDirect3DPixelShader9* pPixelShader;
  239. HRESULT hr;
  240. hr = d3d9Device->CreatePixelShader(
  241. static_cast<DWORD*>(microcode->GetBufferPointer()),
  242. &pPixelShader);
  243. if (FAILED(hr))
  244. {
  245. CM_EXCEPT(RenderingAPIException, "Cannot create D3D9 pixel shader from microcode.");
  246. }
  247. mMapDeviceToPixelShader[d3d9Device] = pPixelShader;
  248. }
  249. else
  250. {
  251. CM_EXCEPT(RenderingAPIException, "Specified D3D9 pixel shader is not supported!");
  252. mMapDeviceToPixelShader[d3d9Device] = NULL;
  253. }
  254. }
  255. //-----------------------------------------------------------------------------
  256. void D3D9GpuFragmentProgram::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  257. {
  258. }
  259. //-----------------------------------------------------------------------------
  260. void D3D9GpuFragmentProgram::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  261. {
  262. DeviceToPixelShaderIterator it;
  263. // Find the shader of this device.
  264. it = mMapDeviceToPixelShader.find(d3d9Device);
  265. // Case shader found -> release it and erase from map.
  266. if (it != mMapDeviceToPixelShader.end())
  267. {
  268. SAFE_RELEASE(it->second);
  269. mMapDeviceToPixelShader.erase(it);
  270. }
  271. }
  272. //-----------------------------------------------------------------------------
  273. IDirect3DPixelShader9* D3D9GpuFragmentProgram::getPixelShader( void )
  274. {
  275. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  276. DeviceToPixelShaderIterator it;
  277. // Find the shader of this device.
  278. it = mMapDeviceToPixelShader.find(d3d9Device);
  279. // Shader was not found -> load it.
  280. if (it == mMapDeviceToPixelShader.end())
  281. {
  282. createInternalResources(d3d9Device);
  283. it = mMapDeviceToPixelShader.find(d3d9Device);
  284. }
  285. return it->second;
  286. }
  287. //-----------------------------------------------------------------------------
  288. }