OgreD3D9GpuProgram.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 "OgreD3D9GpuProgram.h"
  25. #include "OgreMatrix4.h"
  26. #include "OgreException.h"
  27. #include "OgreD3D9Mappings.h"
  28. #include "OgreD3D9RenderSystem.h"
  29. namespace Ogre {
  30. D3D9GpuProgram::CmdColumnMajorMatrices D3D9GpuProgram::msCmdColumnMajorMatrices;
  31. D3D9GpuProgram::CmdExternalMicrocode D3D9GpuProgram::msCmdExternalMicrocode;
  32. //-----------------------------------------------------------------------
  33. String D3D9GpuProgram::CmdColumnMajorMatrices::doGet(const void *target) const
  34. {
  35. return StringConverter::toString(static_cast<const D3D9GpuProgram*>(target)->getColumnMajorMatrices());
  36. }
  37. void D3D9GpuProgram::CmdColumnMajorMatrices::doSet(void *target, const String& val)
  38. {
  39. static_cast<D3D9GpuProgram*>(target)->setColumnMajorMatrices(StringConverter::parseBool(val));
  40. }
  41. //-----------------------------------------------------------------------
  42. String D3D9GpuProgram::CmdExternalMicrocode::doGet(const void *target) const
  43. {
  44. //D3D9GpuProgram* program=const_cast<D3D9GpuProgram*>(static_cast<const D3D9GpuProgram*>(target));
  45. //LPD3DXBUFFER ptr=program->getExternalMicrocode();
  46. //nothing to do
  47. return String();
  48. }
  49. void D3D9GpuProgram::CmdExternalMicrocode::doSet(void *target, const String& val)
  50. {
  51. D3D9GpuProgram* program = const_cast<D3D9GpuProgram*>(static_cast<const D3D9GpuProgram*>(target));
  52. const void* buffer = val.data();
  53. program->setExternalMicrocode(buffer, val.size());
  54. }
  55. //-----------------------------------------------------------------------------
  56. D3D9GpuProgram::D3D9GpuProgram()
  57. : GpuProgram(), mpExternalMicrocode(NULL), mColumnMajorMatrices(false)
  58. {
  59. // TODO PORT - Not using these param dictionaries but I'm not sure what that breaks
  60. // if (createParamDictionary("D3D9GpuProgram"))
  61. // {
  62. // setupBaseParamDictionary();
  63. // ParamDictionary* dict = getParamDictionary();
  64. // dict->addParameter(ParameterDef("column_major_matrices",
  65. // "Whether matrix packing in column-major order.",
  66. // PT_BOOL),&msCmdColumnMajorMatrices);
  67. //dict->addParameter(ParameterDef("external_micro_code",
  68. // "the cached external micro code data.",
  69. // PT_STRING),&msCmdExternalMicrocode);
  70. // }
  71. }
  72. //-----------------------------------------------------------------------------
  73. D3D9GpuProgram::~D3D9GpuProgram()
  74. {
  75. }
  76. //-----------------------------------------------------------------------------
  77. void D3D9GpuProgram::setExternalMicrocode(const void* pMicrocode, size_t size)
  78. {
  79. LPD3DXBUFFER pBuffer=0;
  80. HRESULT hr=D3DXCreateBuffer(size, &pBuffer);
  81. if(pBuffer)
  82. {
  83. memcpy(pBuffer->GetBufferPointer(), pMicrocode, size);
  84. this->setExternalMicrocode(pBuffer);
  85. SAFE_RELEASE(pBuffer);
  86. }
  87. }
  88. //-----------------------------------------------------------------------------
  89. void D3D9GpuProgram::setExternalMicrocode(ID3DXBuffer* pMicrocode)
  90. {
  91. SAFE_RELEASE(mpExternalMicrocode);
  92. mpExternalMicrocode = pMicrocode;
  93. if(mpExternalMicrocode)
  94. {
  95. mpExternalMicrocode->AddRef();
  96. }
  97. }
  98. //-----------------------------------------------------------------------------
  99. LPD3DXBUFFER D3D9GpuProgram::getExternalMicrocode(void)
  100. {
  101. return mpExternalMicrocode;
  102. }
  103. //-----------------------------------------------------------------------------
  104. void D3D9GpuProgram::loadImpl(void)
  105. {
  106. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  107. for (uint i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  108. {
  109. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  110. loadImpl(d3d9Device);
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. void D3D9GpuProgram::loadImpl(IDirect3DDevice9* d3d9Device)
  115. {
  116. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  117. if (mpExternalMicrocode)
  118. {
  119. loadFromMicrocode(d3d9Device, mpExternalMicrocode);
  120. }
  121. else
  122. {
  123. // Normal load-from-source approach
  124. if (mLoadFromFile)
  125. {
  126. //// find & load source code
  127. //DataStreamPtr stream =
  128. // ResourceGroupManager::getSingleton().openResource(
  129. // mFilename, mGroup, true, this);
  130. //mSource = stream->getAsString();
  131. }
  132. // Call polymorphic load
  133. loadFromSource(d3d9Device);
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. void D3D9GpuProgram::unloadImpl(void)
  138. {
  139. SAFE_RELEASE(mpExternalMicrocode);
  140. }
  141. //-----------------------------------------------------------------------------
  142. void D3D9GpuProgram::loadFromSource(void)
  143. {
  144. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  145. for (uint i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  146. {
  147. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  148. loadFromSource(d3d9Device);
  149. }
  150. }
  151. //-----------------------------------------------------------------------------
  152. void D3D9GpuProgram::loadFromSource(IDirect3DDevice9* d3d9Device)
  153. {
  154. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  155. // Populate compile flags
  156. DWORD compileFlags = 0;
  157. // Create the shader
  158. // Assemble source into microcode
  159. LPD3DXBUFFER microcode;
  160. LPD3DXBUFFER errors;
  161. HRESULT hr = D3DXAssembleShader(
  162. mSource.c_str(),
  163. static_cast<UINT>(mSource.length()),
  164. NULL, // no #define support
  165. NULL, // no #include support
  166. compileFlags, // standard compile options
  167. &microcode,
  168. &errors);
  169. if (FAILED(hr))
  170. {
  171. // TODO PORT - Not printing out errors yet
  172. //String message = "Cannot assemble D3D9 shader. Errors:\n" +
  173. // static_cast<const char*>(errors->GetBufferPointer());
  174. String message = "Cannot assemble D3D9 shader. Errors:\n NoErrorsPort";
  175. errors->Release();
  176. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, message,
  177. "D3D9GpuProgram::loadFromSource");
  178. }
  179. loadFromMicrocode(d3d9Device, microcode);
  180. SAFE_RELEASE(microcode);
  181. SAFE_RELEASE(errors);
  182. }
  183. //-----------------------------------------------------------------------
  184. GpuProgramParametersSharedPtr D3D9GpuProgram::createParameters(void)
  185. {
  186. // Call superclass
  187. GpuProgramParametersSharedPtr params = GpuProgram::createParameters();
  188. // Need to transpose matrices if compiled with column-major matrices
  189. params->setTransposeMatrices(mColumnMajorMatrices);
  190. return params;
  191. }
  192. //-----------------------------------------------------------------------------
  193. D3D9GpuVertexProgram::D3D9GpuVertexProgram()
  194. : D3D9GpuProgram()
  195. {
  196. mType = GPT_VERTEX_PROGRAM;
  197. }
  198. //-----------------------------------------------------------------------------
  199. D3D9GpuVertexProgram::~D3D9GpuVertexProgram()
  200. {
  201. }
  202. //-----------------------------------------------------------------------------
  203. void D3D9GpuVertexProgram::loadFromMicrocode(IDirect3DDevice9* d3d9Device, ID3DXBuffer* microcode)
  204. {
  205. DeviceToVertexShaderIterator it = mMapDeviceToVertexShader.find(d3d9Device);
  206. if (it != mMapDeviceToVertexShader.end())
  207. SAFE_RELEASE(it->second);
  208. if (isSupported())
  209. {
  210. // Create the shader
  211. IDirect3DVertexShader9* pVertexShader;
  212. HRESULT hr;
  213. hr = d3d9Device->CreateVertexShader(
  214. static_cast<DWORD*>(microcode->GetBufferPointer()),
  215. &pVertexShader);
  216. if (FAILED(hr))
  217. {
  218. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  219. "Cannot create D3D9 vertex shader from microcode",
  220. "D3D9GpuVertexProgram::loadFromMicrocode");
  221. }
  222. mMapDeviceToVertexShader[d3d9Device] = pVertexShader;
  223. }
  224. else
  225. {
  226. // TODO PORT - Log this error somewhere
  227. //LogManager::getSingleton().logMessage(
  228. // "Unsupported D3D9 vertex shader '" + mName + "' was not loaded.");
  229. mMapDeviceToVertexShader[d3d9Device] = NULL;
  230. }
  231. }
  232. //-----------------------------------------------------------------------------
  233. void D3D9GpuVertexProgram::unloadImpl(void)
  234. {
  235. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  236. DeviceToVertexShaderIterator it = mMapDeviceToVertexShader.begin();
  237. while (it != mMapDeviceToVertexShader.end())
  238. {
  239. SAFE_RELEASE(it->second);
  240. ++it;
  241. }
  242. mMapDeviceToVertexShader.clear();
  243. D3D9GpuProgram::unloadImpl();
  244. }
  245. //-----------------------------------------------------------------------------
  246. void D3D9GpuVertexProgram::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  247. {
  248. }
  249. //-----------------------------------------------------------------------------
  250. void D3D9GpuVertexProgram::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  251. {
  252. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  253. DeviceToVertexShaderIterator it;
  254. // Find the shader of this device.
  255. it = mMapDeviceToVertexShader.find(d3d9Device);
  256. // Case shader found -> release it and erase from map.
  257. if (it != mMapDeviceToVertexShader.end())
  258. {
  259. SAFE_RELEASE(it->second);
  260. mMapDeviceToVertexShader.erase(it);
  261. }
  262. }
  263. //-----------------------------------------------------------------------------
  264. IDirect3DVertexShader9* D3D9GpuVertexProgram::getVertexShader( void )
  265. {
  266. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  267. DeviceToVertexShaderIterator it;
  268. // Find the shader of this device.
  269. it = mMapDeviceToVertexShader.find(d3d9Device);
  270. // Shader was not found -> load it.
  271. if (it == mMapDeviceToVertexShader.end())
  272. {
  273. loadImpl(d3d9Device);
  274. it = mMapDeviceToVertexShader.find(d3d9Device);
  275. }
  276. return it->second;
  277. }
  278. //-----------------------------------------------------------------------------
  279. //-----------------------------------------------------------------------------
  280. D3D9GpuFragmentProgram::D3D9GpuFragmentProgram()
  281. : D3D9GpuProgram()
  282. {
  283. mType = GPT_FRAGMENT_PROGRAM;
  284. }
  285. //-----------------------------------------------------------------------------
  286. D3D9GpuFragmentProgram::~D3D9GpuFragmentProgram()
  287. {
  288. }
  289. //-----------------------------------------------------------------------------
  290. void D3D9GpuFragmentProgram::loadFromMicrocode(IDirect3DDevice9* d3d9Device, ID3DXBuffer* microcode)
  291. {
  292. DeviceToPixelShaderIterator it = mMapDeviceToPixelShader.find(d3d9Device);
  293. if (it != mMapDeviceToPixelShader.end())
  294. SAFE_RELEASE(it->second);
  295. if (isSupported())
  296. {
  297. // Create the shader
  298. IDirect3DPixelShader9* pPixelShader;
  299. HRESULT hr;
  300. hr = d3d9Device->CreatePixelShader(
  301. static_cast<DWORD*>(microcode->GetBufferPointer()),
  302. &pPixelShader);
  303. if (FAILED(hr))
  304. {
  305. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  306. "Cannot create D3D9 pixel shader from microcode.",
  307. "D3D9GpuFragmentProgram::loadFromMicrocode");
  308. }
  309. mMapDeviceToPixelShader[d3d9Device] = pPixelShader;
  310. }
  311. else
  312. {
  313. // TODO PORT - Log this somewhere
  314. //LogManager::getSingleton().logMessage(
  315. // "Unsupported D3D9 pixel shader was not loaded.");
  316. mMapDeviceToPixelShader[d3d9Device] = NULL;
  317. }
  318. }
  319. //-----------------------------------------------------------------------------
  320. void D3D9GpuFragmentProgram::unloadImpl(void)
  321. {
  322. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  323. DeviceToPixelShaderIterator it = mMapDeviceToPixelShader.begin();
  324. while (it != mMapDeviceToPixelShader.end())
  325. {
  326. SAFE_RELEASE(it->second);
  327. ++it;
  328. }
  329. mMapDeviceToPixelShader.clear();
  330. D3D9GpuProgram::unloadImpl();
  331. }
  332. //-----------------------------------------------------------------------------
  333. void D3D9GpuFragmentProgram::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  334. {
  335. }
  336. //-----------------------------------------------------------------------------
  337. void D3D9GpuFragmentProgram::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  338. {
  339. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  340. DeviceToPixelShaderIterator it;
  341. // Find the shader of this device.
  342. it = mMapDeviceToPixelShader.find(d3d9Device);
  343. // Case shader found -> release it and erase from map.
  344. if (it != mMapDeviceToPixelShader.end())
  345. {
  346. SAFE_RELEASE(it->second);
  347. mMapDeviceToPixelShader.erase(it);
  348. }
  349. }
  350. //-----------------------------------------------------------------------------
  351. IDirect3DPixelShader9* D3D9GpuFragmentProgram::getPixelShader( void )
  352. {
  353. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  354. DeviceToPixelShaderIterator it;
  355. // Find the shader of this device.
  356. it = mMapDeviceToPixelShader.find(d3d9Device);
  357. // Shader was not found -> load it.
  358. if (it == mMapDeviceToPixelShader.end())
  359. {
  360. loadImpl(d3d9Device);
  361. it = mMapDeviceToPixelShader.find(d3d9Device);
  362. }
  363. return it->second;
  364. }
  365. //-----------------------------------------------------------------------------
  366. }