BsD3D9GpuProgram.cpp 8.8 KB

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