BsD3D9GpuProgram.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "BsD3D9GpuProgram.h"
  2. #include "BsMatrix4.h"
  3. #include "BsException.h"
  4. #include "BsD3D9Mappings.h"
  5. #include "BsD3D9RenderSystem.h"
  6. #include "BsD3D9ResourceManager.h"
  7. #include "BsGpuParams.h"
  8. #include "BsAsyncOp.h"
  9. #include "BsGpuProgramManager.h"
  10. #include "BsD3D9HLSLParamParser.h"
  11. #include "BsRenderStats.h"
  12. namespace BansheeEngine
  13. {
  14. D3D9GpuProgramCore::D3D9GpuProgramCore(const String& source, const String& entryPoint,
  15. GpuProgramType gptype, GpuProgramProfile profile)
  16. : GpuProgramCore(source, entryPoint, gptype, profile, false),
  17. mMicrocode(nullptr), mColumnMajorMatrices(false), mOptimisationLevel(OPT_DEFAULT)
  18. { }
  19. D3D9GpuProgramCore::~D3D9GpuProgramCore()
  20. {
  21. SAFE_RELEASE(mMicrocode);
  22. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuProgram);
  23. }
  24. void D3D9GpuProgramCore::initialize()
  25. {
  26. if (!isSupported())
  27. {
  28. mIsCompiled = false;
  29. mCompileError = "Specified program is not supported by the current render system.";
  30. GpuProgramCore::initialize();
  31. return;
  32. }
  33. // Populate preprocessor defines
  34. String stringBuffer;
  35. Vector<D3DXMACRO> defines;
  36. const D3DXMACRO* pDefines = 0;
  37. if (!mPreprocessorDefines.empty())
  38. {
  39. stringBuffer = mPreprocessorDefines;
  40. // Split preprocessor defines and build up macro array
  41. D3DXMACRO macro;
  42. String::size_type pos = 0;
  43. while (pos != String::npos)
  44. {
  45. macro.Name = &stringBuffer[pos];
  46. macro.Definition = 0;
  47. String::size_type start_pos = pos;
  48. // Find delims
  49. pos = stringBuffer.find_first_of(";,=", pos);
  50. if (start_pos == pos)
  51. {
  52. if (pos == stringBuffer.length())
  53. {
  54. break;
  55. }
  56. pos++;
  57. continue;
  58. }
  59. if (pos != String::npos)
  60. {
  61. // Check definition part
  62. if (stringBuffer[pos] == '=')
  63. {
  64. // Setup null character for macro name
  65. stringBuffer[pos++] = '\0';
  66. macro.Definition = &stringBuffer[pos];
  67. pos = stringBuffer.find_first_of(";,", pos);
  68. }
  69. else
  70. {
  71. // No definition part, define as "1"
  72. macro.Definition = "1";
  73. }
  74. if (pos != String::npos)
  75. {
  76. // Setup null character for macro name or definition
  77. stringBuffer[pos++] = '\0';
  78. }
  79. }
  80. else
  81. {
  82. macro.Definition = "1";
  83. }
  84. if (strlen(macro.Name) > 0)
  85. {
  86. defines.push_back(macro);
  87. }
  88. else
  89. {
  90. break;
  91. }
  92. }
  93. // Add NULL terminator
  94. macro.Name = 0;
  95. macro.Definition = 0;
  96. defines.push_back(macro);
  97. pDefines = &defines[0];
  98. }
  99. // Populate compile flags
  100. DWORD compileFlags = 0;
  101. if (mColumnMajorMatrices)
  102. compileFlags |= D3DXSHADER_PACKMATRIX_COLUMNMAJOR;
  103. else
  104. compileFlags |= D3DXSHADER_PACKMATRIX_ROWMAJOR;
  105. #if BS_DEBUG_MODE
  106. compileFlags |= D3DXSHADER_DEBUG;
  107. #endif
  108. switch (mOptimisationLevel)
  109. {
  110. case OPT_DEFAULT:
  111. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  112. break;
  113. case OPT_NONE:
  114. compileFlags |= D3DXSHADER_SKIPOPTIMIZATION;
  115. break;
  116. case OPT_0:
  117. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL0;
  118. break;
  119. case OPT_1:
  120. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  121. break;
  122. case OPT_2:
  123. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL2;
  124. break;
  125. case OPT_3:
  126. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL3;
  127. break;
  128. }
  129. LPD3DXBUFFER errors = 0;
  130. // include handler
  131. LPD3DXCONSTANTTABLE constTable;
  132. String hlslProfile = D3D9RenderSystem::instance().getCapabilities()->gpuProgProfileToRSSpecificProfile(getProperties().getProfile());
  133. String parsedSource = D3D9EmulatedParamBlockParser::parse(getProperties().getSource(), mBlocks);
  134. // Compile & assemble into microcode
  135. HRESULT hr = D3DXCompileShader(
  136. parsedSource.c_str(),
  137. static_cast<UINT>(parsedSource.length()),
  138. pDefines,
  139. nullptr,
  140. getProperties().getEntryPoint().c_str(),
  141. hlslProfile.c_str(),
  142. compileFlags,
  143. &mMicrocode,
  144. &errors,
  145. &constTable);
  146. if (FAILED(hr))
  147. {
  148. String message = "Cannot compile D3D9 high-level shader ";
  149. if (errors)
  150. {
  151. message += String(" Errors:\n") + static_cast<const char*>(errors->GetBufferPointer());
  152. errors->Release();
  153. }
  154. mIsCompiled = false;
  155. mCompileError = message;
  156. SAFE_RELEASE(mMicrocode);
  157. mMicrocode = nullptr;
  158. }
  159. else
  160. {
  161. for (UINT32 i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  162. {
  163. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  164. loadFromMicrocode(d3d9Device, mMicrocode);
  165. }
  166. D3D9HLSLParamParser paramParser(constTable, mBlocks);
  167. mParametersDesc = paramParser.buildParameterDescriptions();
  168. mIsCompiled = true;
  169. mCompileError = "";
  170. SAFE_RELEASE(constTable);
  171. }
  172. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuProgram);
  173. GpuProgramCore::initialize();
  174. }
  175. D3D9GpuVertexProgramCore::D3D9GpuVertexProgramCore(const String& source, const String& entryPoint,
  176. GpuProgramProfile profile)
  177. : D3D9GpuProgramCore(source, entryPoint, GPT_VERTEX_PROGRAM, profile)
  178. {
  179. }
  180. D3D9GpuVertexProgramCore::~D3D9GpuVertexProgramCore()
  181. {
  182. auto it = mMapDeviceToVertexShader.begin();
  183. while (it != mMapDeviceToVertexShader.end())
  184. {
  185. SAFE_RELEASE(it->second);
  186. ++it;
  187. }
  188. mMapDeviceToVertexShader.clear();
  189. }
  190. void D3D9GpuVertexProgramCore::loadFromMicrocode(IDirect3DDevice9* d3d9Device, ID3DXBuffer* microcode)
  191. {
  192. auto it = mMapDeviceToVertexShader.find(d3d9Device);
  193. if (it != mMapDeviceToVertexShader.end())
  194. SAFE_RELEASE(it->second);
  195. // Create the shader
  196. IDirect3DVertexShader9* pVertexShader;
  197. HRESULT hr;
  198. hr = d3d9Device->CreateVertexShader(
  199. static_cast<DWORD*>(microcode->GetBufferPointer()),
  200. &pVertexShader);
  201. if (FAILED(hr))
  202. {
  203. BS_EXCEPT(RenderingAPIException, "Cannot create D3D9 vertex shader from microcode");
  204. }
  205. mMapDeviceToVertexShader[d3d9Device] = pVertexShader;
  206. }
  207. void D3D9GpuVertexProgramCore::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  208. {
  209. }
  210. void D3D9GpuVertexProgramCore::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  211. {
  212. auto it = mMapDeviceToVertexShader.find(d3d9Device);
  213. // Case shader found -> release it and erase from map.
  214. if (it != mMapDeviceToVertexShader.end())
  215. {
  216. SAFE_RELEASE(it->second);
  217. mMapDeviceToVertexShader.erase(it);
  218. }
  219. }
  220. IDirect3DVertexShader9* D3D9GpuVertexProgramCore::getVertexShader()
  221. {
  222. if (!isCompiled())
  223. return nullptr;
  224. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  225. auto it = mMapDeviceToVertexShader.find(d3d9Device);
  226. // Shader was not found -> load it.
  227. if (it == mMapDeviceToVertexShader.end())
  228. {
  229. loadFromMicrocode(d3d9Device, mMicrocode);
  230. it = mMapDeviceToVertexShader.find(d3d9Device);
  231. }
  232. return it->second;
  233. }
  234. D3D9GpuFragmentProgramCore::D3D9GpuFragmentProgramCore(const String& source, const String& entryPoint,
  235. GpuProgramProfile profile)
  236. : D3D9GpuProgramCore(source, entryPoint, GPT_FRAGMENT_PROGRAM, profile)
  237. {
  238. }
  239. D3D9GpuFragmentProgramCore::~D3D9GpuFragmentProgramCore()
  240. {
  241. auto it = mMapDeviceToPixelShader.begin();
  242. while (it != mMapDeviceToPixelShader.end())
  243. {
  244. SAFE_RELEASE(it->second);
  245. ++it;
  246. }
  247. mMapDeviceToPixelShader.clear();
  248. }
  249. void D3D9GpuFragmentProgramCore::loadFromMicrocode(IDirect3DDevice9* d3d9Device, ID3DXBuffer* microcode)
  250. {
  251. auto it = mMapDeviceToPixelShader.find(d3d9Device);
  252. if (it != mMapDeviceToPixelShader.end())
  253. SAFE_RELEASE(it->second);
  254. // Create the shader
  255. IDirect3DPixelShader9* pPixelShader;
  256. HRESULT hr;
  257. hr = d3d9Device->CreatePixelShader(static_cast<DWORD*>(microcode->GetBufferPointer()), &pPixelShader);
  258. if (FAILED(hr))
  259. BS_EXCEPT(RenderingAPIException, "Cannot create D3D9 pixel shader from microcode.");
  260. mMapDeviceToPixelShader[d3d9Device] = pPixelShader;
  261. }
  262. void D3D9GpuFragmentProgramCore::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  263. {
  264. }
  265. void D3D9GpuFragmentProgramCore::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  266. {
  267. auto it = mMapDeviceToPixelShader.find(d3d9Device);
  268. // Case shader found -> release it and erase from map.
  269. if (it != mMapDeviceToPixelShader.end())
  270. {
  271. SAFE_RELEASE(it->second);
  272. mMapDeviceToPixelShader.erase(it);
  273. }
  274. }
  275. IDirect3DPixelShader9* D3D9GpuFragmentProgramCore::getPixelShader()
  276. {
  277. if (!isCompiled())
  278. return nullptr;
  279. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  280. auto it = mMapDeviceToPixelShader.find(d3d9Device);
  281. // Shader was not found -> load it.
  282. if (it == mMapDeviceToPixelShader.end())
  283. {
  284. loadFromMicrocode(d3d9Device, mMicrocode);
  285. it = mMapDeviceToPixelShader.find(d3d9Device);
  286. }
  287. return it->second;
  288. }
  289. }