BsD3D11GpuProgram.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D11GpuProgram.h"
  4. #include "BsD3D11Device.h"
  5. #include "Error/BsException.h"
  6. #include "Debug/BsDebug.h"
  7. #include "RenderAPI/BsGpuParams.h"
  8. #include "BsD3D11RenderAPI.h"
  9. #include "Managers/BsGpuProgramManager.h"
  10. #include "Managers/BsHardwareBufferManager.h"
  11. #include "BsD3D11HLSLParamParser.h"
  12. #include "Profiling/BsRenderStats.h"
  13. #include <regex>
  14. namespace bs { namespace ct
  15. {
  16. UINT32 D3D11GpuProgram::GlobalProgramId = 0;
  17. D3D11GpuProgram::D3D11GpuProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  18. : GpuProgram(desc, deviceMask),
  19. mEnableBackwardsCompatibility(false), mProgramId(0)
  20. {
  21. assert((deviceMask == GDF_DEFAULT || deviceMask == GDF_PRIMARY) && "Multiple GPUs not supported natively on DirectX 11.");
  22. }
  23. D3D11GpuProgram::~D3D11GpuProgram()
  24. {
  25. mMicrocode.clear();
  26. mInputDeclaration = nullptr;
  27. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuProgram);
  28. }
  29. void D3D11GpuProgram::initialize()
  30. {
  31. if (!isSupported())
  32. {
  33. mIsCompiled = false;
  34. mCompileError = "Specified program is not supported by the current render system.";
  35. GpuProgram::initialize();
  36. return;
  37. }
  38. String hlslProfile;
  39. switch(mProperties.getType())
  40. {
  41. case GPT_FRAGMENT_PROGRAM:
  42. hlslProfile = "ps_5_0";
  43. break;
  44. case GPT_VERTEX_PROGRAM:
  45. hlslProfile = "vs_5_0";
  46. break;
  47. case GPT_GEOMETRY_PROGRAM:
  48. hlslProfile = "gs_5_0";
  49. break;
  50. case GPT_COMPUTE_PROGRAM:
  51. hlslProfile = "cs_5_0";
  52. break;
  53. case GPT_HULL_PROGRAM:
  54. hlslProfile = "hs_5_0";
  55. break;
  56. case GPT_DOMAIN_PROGRAM:
  57. hlslProfile = "ds_5_0";
  58. break;
  59. }
  60. D3D11RenderAPI* rapi = static_cast<D3D11RenderAPI*>(RenderAPI::instancePtr());
  61. ID3DBlob* microcode = compileMicrocode(hlslProfile);
  62. if (microcode != nullptr)
  63. {
  64. mMicrocode.resize(microcode->GetBufferSize());
  65. memcpy(&mMicrocode[0], microcode->GetBufferPointer(), microcode->GetBufferSize());
  66. populateParametersAndConstants(microcode);
  67. loadFromMicrocode(rapi->getPrimaryDevice(), microcode);
  68. SAFE_RELEASE(microcode);
  69. }
  70. mProgramId = GlobalProgramId++;
  71. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuProgram);
  72. GpuProgram::initialize();
  73. }
  74. UINT32 D3D11GpuProgram::parseErrorMessage(const char* message)
  75. {
  76. if (message == nullptr)
  77. return 0;
  78. String pattern = R"(\(([0-9]*),.*\))";
  79. std::regex regex(pattern);
  80. std::cmatch results;
  81. if (std::regex_search(message, results, regex))
  82. {
  83. std::string result = results[1].str();
  84. return strtol(result.c_str(), nullptr, 10) - 1;
  85. }
  86. return 0;
  87. }
  88. ID3DBlob* D3D11GpuProgram::compileMicrocode(const String& profile)
  89. {
  90. UINT compileFlags = 0;
  91. #if defined(BS_DEBUG_MODE)
  92. compileFlags |= D3DCOMPILE_DEBUG;
  93. compileFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  94. #endif
  95. compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
  96. if (mEnableBackwardsCompatibility)
  97. compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  98. ID3DBlob* microCode = nullptr;
  99. ID3DBlob* messages = nullptr;
  100. const String& source = mProperties.getSource();
  101. const String& entryPoint = mProperties.getEntryPoint();
  102. const D3D_SHADER_MACRO defines[] =
  103. {
  104. { "HLSL", "1" },
  105. { nullptr, nullptr }
  106. };
  107. HRESULT hr = D3DCompile(
  108. source.c_str(), // [in] Pointer to the shader in memory.
  109. source.size(), // [in] Size of the shader in memory.
  110. nullptr, // [in] The name of the file that contains the shader code.
  111. defines, // [in] Optional. Pointer to a NULL-terminated array of macro definitions.
  112. // See D3D_SHADER_MACRO. If not used, set this to NULL.
  113. nullptr, // [in] Optional. Pointer to an ID3DInclude Interface interface for handling include files.
  114. // Setting this to NULL will cause a compile error if a shader contains a #include.
  115. entryPoint.c_str(), // [in] Name of the shader-entrypoint function where shader execution begins.
  116. profile.c_str(), // [in] A string that specifies the shader model; can be any profile in shader model 4 or higher.
  117. compileFlags, // [in] Effect compile flags - no D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY at the first try...
  118. 0, // [in] Effect compile flags
  119. &microCode, // [out] A pointer to an ID3DBlob Interface which contains the compiled shader, as well as
  120. // any embedded debug and symbol-table information.
  121. &messages // [out] A pointer to an ID3DBlob Interface which contains a listing of errors and warnings
  122. // that occurred during compilation. These errors and warnings are identical to the
  123. // debug output from a debugger.
  124. );
  125. if (messages != nullptr)
  126. {
  127. const char* message = static_cast<const char*>(messages->GetBufferPointer());
  128. UINT32 lineIdx = parseErrorMessage(message);
  129. Vector<String> sourceLines = StringUtil::split(source, "\n");
  130. String sourceLine;
  131. if (lineIdx < sourceLines.size())
  132. sourceLine = sourceLines[lineIdx];
  133. mCompileError =
  134. String(message) + "\n" +
  135. "\n" +
  136. "Line " + toString(lineIdx) + ": " + sourceLine;
  137. SAFE_RELEASE(messages);
  138. }
  139. else
  140. mCompileError = "";
  141. if (FAILED(hr))
  142. {
  143. mIsCompiled = false;
  144. mCompileError = "Cannot compile D3D11 high-level shader. Errors:\n" + mCompileError;
  145. SAFE_RELEASE(microCode);
  146. return nullptr;
  147. }
  148. else
  149. {
  150. mIsCompiled = true;
  151. return microCode;
  152. }
  153. }
  154. void D3D11GpuProgram::populateParametersAndConstants(ID3DBlob* microcode)
  155. {
  156. assert(microcode != nullptr);
  157. D3D11HLSLParamParser parser;
  158. if (mProperties.getType() == GPT_VERTEX_PROGRAM)
  159. {
  160. List<VertexElement> inputParams;
  161. parser.parse(microcode, mProperties.getType(), *mParametersDesc, &inputParams);
  162. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration(inputParams);
  163. }
  164. else
  165. {
  166. parser.parse(microcode, mProperties.getType(), *mParametersDesc, nullptr);
  167. }
  168. }
  169. D3D11GpuVertexProgram::D3D11GpuVertexProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  170. : D3D11GpuProgram(desc, deviceMask), mVertexShader(nullptr)
  171. { }
  172. D3D11GpuVertexProgram::~D3D11GpuVertexProgram()
  173. {
  174. SAFE_RELEASE(mVertexShader);
  175. }
  176. void D3D11GpuVertexProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  177. {
  178. HRESULT hr = device.getD3D11Device()->CreateVertexShader(
  179. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  180. device.getClassLinkage(), &mVertexShader);
  181. if (FAILED(hr) || device.hasError())
  182. {
  183. String errorDescription = device.getErrorDescription();
  184. BS_EXCEPT(RenderingAPIException,
  185. "Cannot create D3D11 vertex shader from microcode\nError Description:" + errorDescription);
  186. }
  187. }
  188. ID3D11VertexShader * D3D11GpuVertexProgram::getVertexShader() const
  189. {
  190. return mVertexShader;
  191. }
  192. D3D11GpuFragmentProgram::D3D11GpuFragmentProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  193. : D3D11GpuProgram(desc, deviceMask), mPixelShader(nullptr)
  194. { }
  195. D3D11GpuFragmentProgram::~D3D11GpuFragmentProgram()
  196. {
  197. SAFE_RELEASE(mPixelShader);
  198. }
  199. void D3D11GpuFragmentProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  200. {
  201. HRESULT hr = device.getD3D11Device()->CreatePixelShader(
  202. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  203. device.getClassLinkage(), &mPixelShader);
  204. if (FAILED(hr) || device.hasError())
  205. {
  206. String errorDescription = device.getErrorDescription();
  207. BS_EXCEPT(RenderingAPIException,
  208. "Cannot create D3D11 pixel shader from microcode.\nError Description:" + errorDescription);
  209. }
  210. }
  211. ID3D11PixelShader * D3D11GpuFragmentProgram::getPixelShader() const
  212. {
  213. return mPixelShader;
  214. }
  215. D3D11GpuGeometryProgram::D3D11GpuGeometryProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  216. : D3D11GpuProgram(desc, deviceMask), mGeometryShader(nullptr)
  217. { }
  218. D3D11GpuGeometryProgram::~D3D11GpuGeometryProgram()
  219. {
  220. SAFE_RELEASE(mGeometryShader);
  221. }
  222. void D3D11GpuGeometryProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  223. {
  224. HRESULT hr = device.getD3D11Device()->CreateGeometryShader(
  225. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  226. device.getClassLinkage(), &mGeometryShader);
  227. if (FAILED(hr) || device.hasError())
  228. {
  229. String errorDescription = device.getErrorDescription();
  230. BS_EXCEPT(RenderingAPIException,
  231. "Cannot create D3D11 geometry shader from microcode.\nError Description:" + errorDescription);
  232. }
  233. }
  234. ID3D11GeometryShader * D3D11GpuGeometryProgram::getGeometryShader() const
  235. {
  236. return mGeometryShader;
  237. }
  238. D3D11GpuDomainProgram::D3D11GpuDomainProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  239. : D3D11GpuProgram(desc, deviceMask), mDomainShader(nullptr)
  240. { }
  241. D3D11GpuDomainProgram::~D3D11GpuDomainProgram()
  242. {
  243. SAFE_RELEASE(mDomainShader);
  244. }
  245. void D3D11GpuDomainProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  246. {
  247. HRESULT hr = device.getD3D11Device()->CreateDomainShader(
  248. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  249. device.getClassLinkage(), &mDomainShader);
  250. if (FAILED(hr) || device.hasError())
  251. {
  252. String errorDescription = device.getErrorDescription();
  253. BS_EXCEPT(RenderingAPIException,
  254. "Cannot create D3D11 domain shader from microcode.\nError Description:" + errorDescription);
  255. }
  256. }
  257. ID3D11DomainShader * D3D11GpuDomainProgram::getDomainShader() const
  258. {
  259. return mDomainShader;
  260. }
  261. D3D11GpuHullProgram::D3D11GpuHullProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  262. : D3D11GpuProgram(desc, deviceMask), mHullShader(nullptr)
  263. { }
  264. D3D11GpuHullProgram::~D3D11GpuHullProgram()
  265. {
  266. SAFE_RELEASE(mHullShader);
  267. }
  268. void D3D11GpuHullProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  269. {
  270. // Create the shader
  271. HRESULT hr = device.getD3D11Device()->CreateHullShader(
  272. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  273. device.getClassLinkage(), &mHullShader);
  274. if (FAILED(hr) || device.hasError())
  275. {
  276. String errorDescription = device.getErrorDescription();
  277. BS_EXCEPT(RenderingAPIException,
  278. "Cannot create D3D11 hull shader from microcode.\nError Description:" + errorDescription);
  279. }
  280. }
  281. ID3D11HullShader* D3D11GpuHullProgram::getHullShader() const
  282. {
  283. return mHullShader;
  284. }
  285. D3D11GpuComputeProgram::D3D11GpuComputeProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask)
  286. : D3D11GpuProgram(desc, deviceMask), mComputeShader(nullptr)
  287. { }
  288. D3D11GpuComputeProgram::~D3D11GpuComputeProgram()
  289. {
  290. SAFE_RELEASE(mComputeShader);
  291. }
  292. void D3D11GpuComputeProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  293. {
  294. HRESULT hr = device.getD3D11Device()->CreateComputeShader(
  295. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  296. device.getClassLinkage(), &mComputeShader);
  297. if (FAILED(hr) || device.hasError())
  298. {
  299. String errorDescription = device.getErrorDescription();
  300. BS_EXCEPT(RenderingAPIException,
  301. "Cannot create D3D11 compute shader from microcode.\nError Description:" + errorDescription);
  302. }
  303. }
  304. ID3D11ComputeShader* D3D11GpuComputeProgram::getComputeShader() const
  305. {
  306. return mComputeShader;
  307. }
  308. }}