BsD3D11GpuProgram.cpp 11 KB

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