BsD3D11GpuProgram.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsD3D11GpuProgram.h"
  5. #include "BsD3D11Device.h"
  6. #include "BsException.h"
  7. #include "BsDebug.h"
  8. #include "BsGpuParams.h"
  9. #include "BsD3D11RenderSystem.h"
  10. #include "BsGpuProgramManager.h"
  11. #include "BsHardwareBufferManager.h"
  12. #include "BsD3D11HLSLParamParser.h"
  13. #include "BsD3D11GpuProgramRTTI.h"
  14. #include "BsRenderStats.h"
  15. namespace BansheeEngine
  16. {
  17. UINT32 D3D11GpuProgram::GlobalProgramId = 0;
  18. D3D11GpuProgram::D3D11GpuProgram(const String& source, const String& entryPoint, GpuProgramType gptype,
  19. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  20. : GpuProgram(source, entryPoint, gptype, profile, includes, isAdjacencyInfoRequired),
  21. mColumnMajorMatrices(true), mEnableBackwardsCompatibility(false), mProgramId(0)
  22. {
  23. }
  24. D3D11GpuProgram::~D3D11GpuProgram()
  25. {
  26. }
  27. void D3D11GpuProgram::initialize_internal()
  28. {
  29. if (!isSupported())
  30. {
  31. mIsCompiled = false;
  32. mCompileError = "Specified program is not supported by the current render system.";
  33. GpuProgram::initialize_internal();
  34. return;
  35. }
  36. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  37. String hlslProfile = rs->getCapabilities()->gpuProgProfileToRSSpecificProfile(mProfile);
  38. ID3DBlob* microcode = compileMicrocode(hlslProfile);
  39. if (microcode != nullptr)
  40. {
  41. mMicrocode.resize(microcode->GetBufferSize());
  42. memcpy(&mMicrocode[0], microcode->GetBufferPointer(), microcode->GetBufferSize());
  43. populateParametersAndConstants(microcode);
  44. loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  45. SAFE_RELEASE(microcode);
  46. }
  47. mProgramId = GlobalProgramId++;
  48. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuProgram);
  49. GpuProgram::initialize_internal();
  50. }
  51. void D3D11GpuProgram::destroy_internal()
  52. {
  53. mMicrocode.clear();
  54. mInputDeclaration = nullptr;
  55. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuProgram);
  56. GpuProgram::destroy_internal();
  57. }
  58. ID3DBlob* D3D11GpuProgram::compileMicrocode(const String& profile)
  59. {
  60. // TODO - Preprocessor defines aren't supported
  61. UINT compileFlags = 0;
  62. #if defined(BS_DEBUG_MODE)
  63. compileFlags |= D3DCOMPILE_DEBUG;
  64. compileFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  65. #endif
  66. if (mColumnMajorMatrices)
  67. compileFlags |= D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
  68. else
  69. compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
  70. if (mEnableBackwardsCompatibility)
  71. compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  72. ID3DBlob* microCode = nullptr;
  73. ID3DBlob* errors = nullptr;
  74. HRESULT hr = D3DCompile(
  75. mSource.c_str(), // [in] Pointer to the shader in memory.
  76. mSource.size(), // [in] Size of the shader in memory.
  77. nullptr, // [in] The name of the file that contains the shader code.
  78. nullptr, // [in] Optional. Pointer to a NULL-terminated array of macro definitions. See D3D_SHADER_MACRO. If not used, set this to NULL.
  79. 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.
  80. mEntryPoint.c_str(),// [in] Name of the shader-entrypoint function where shader execution begins.
  81. profile.c_str(),// [in] A string that specifies the shader model; can be any profile in shader model 4 or higher.
  82. compileFlags, // [in] Effect compile flags - no D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY at the first try...
  83. 0, // [in] Effect compile flags
  84. &microCode, // [out] A pointer to an ID3DBlob Interface which contains the compiled shader, as well as any embedded debug and symbol-table information.
  85. &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.
  86. );
  87. if (FAILED(hr))
  88. {
  89. mIsCompiled = false;
  90. mCompileError = "Cannot compile D3D11 high-level shader. Errors:\n" +
  91. String(static_cast<const char*>(errors->GetBufferPointer()));
  92. SAFE_RELEASE(microCode);
  93. SAFE_RELEASE(errors);
  94. return nullptr;
  95. }
  96. else
  97. {
  98. mIsCompiled = true;
  99. mCompileError = "";
  100. SAFE_RELEASE(errors);
  101. return microCode;
  102. }
  103. }
  104. void D3D11GpuProgram::populateParametersAndConstants(ID3DBlob* microcode)
  105. {
  106. assert(microcode != nullptr);
  107. D3D11HLSLParamParser parser;
  108. if (mType == GPT_VERTEX_PROGRAM)
  109. {
  110. VertexDeclaration::VertexElementList inputParams;
  111. parser.parse(microcode, *mParametersDesc, &inputParams);
  112. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration(inputParams);
  113. }
  114. else
  115. {
  116. parser.parse(microcode, *mParametersDesc, nullptr);
  117. }
  118. }
  119. GpuParamsPtr D3D11GpuProgram::createParameters()
  120. {
  121. GpuParamsPtr params = bs_shared_ptr<GpuParams, PoolAlloc>(mParametersDesc, mColumnMajorMatrices);
  122. return params;
  123. }
  124. const String& D3D11GpuProgram::getLanguage() const
  125. {
  126. static String name = "hlsl";
  127. return name;
  128. }
  129. /************************************************************************/
  130. /* SERIALIZATION */
  131. /************************************************************************/
  132. RTTITypeBase* D3D11GpuProgram::getRTTIStatic()
  133. {
  134. return D3D11GpuProgramRTTI::instance();
  135. }
  136. RTTITypeBase* D3D11GpuProgram::getRTTI() const
  137. {
  138. return D3D11GpuProgram::getRTTIStatic();
  139. }
  140. D3D11GpuVertexProgram::D3D11GpuVertexProgram(const String& source, const String& entryPoint,
  141. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  142. : D3D11GpuProgram(source, entryPoint, GPT_VERTEX_PROGRAM, profile, includes, false)
  143. , mVertexShader(nullptr)
  144. { }
  145. D3D11GpuVertexProgram::~D3D11GpuVertexProgram()
  146. { }
  147. void D3D11GpuVertexProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  148. {
  149. HRESULT hr = device.getD3D11Device()->CreateVertexShader(
  150. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  151. device.getClassLinkage(), &mVertexShader);
  152. if (FAILED(hr) || device.hasError())
  153. {
  154. String errorDescription = device.getErrorDescription();
  155. BS_EXCEPT(RenderingAPIException,
  156. "Cannot create D3D11 vertex shader from microcode\nError Description:" + errorDescription);
  157. }
  158. }
  159. void D3D11GpuVertexProgram::destroy_internal()
  160. {
  161. SAFE_RELEASE(mVertexShader);
  162. D3D11GpuProgram::destroy_internal();
  163. }
  164. ID3D11VertexShader * D3D11GpuVertexProgram::getVertexShader() const
  165. {
  166. return mVertexShader;
  167. }
  168. /************************************************************************/
  169. /* SERIALIZATION */
  170. /************************************************************************/
  171. RTTITypeBase* D3D11GpuVertexProgram::getRTTIStatic()
  172. {
  173. return D3D11GpuVertexProgramRTTI::instance();
  174. }
  175. RTTITypeBase* D3D11GpuVertexProgram::getRTTI() const
  176. {
  177. return D3D11GpuVertexProgram::getRTTIStatic();
  178. }
  179. D3D11GpuFragmentProgram::D3D11GpuFragmentProgram(const String& source, const String& entryPoint,
  180. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  181. : D3D11GpuProgram(source, entryPoint, GPT_FRAGMENT_PROGRAM, profile, includes, false)
  182. , mPixelShader(nullptr)
  183. { }
  184. D3D11GpuFragmentProgram::~D3D11GpuFragmentProgram()
  185. { }
  186. void D3D11GpuFragmentProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  187. {
  188. HRESULT hr = device.getD3D11Device()->CreatePixelShader(
  189. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  190. device.getClassLinkage(), &mPixelShader);
  191. if (FAILED(hr) || device.hasError())
  192. {
  193. String errorDescription = device.getErrorDescription();
  194. BS_EXCEPT(RenderingAPIException,
  195. "Cannot create D3D11 pixel shader from microcode.\nError Description:" + errorDescription);
  196. }
  197. }
  198. void D3D11GpuFragmentProgram::destroy_internal()
  199. {
  200. SAFE_RELEASE(mPixelShader);
  201. D3D11GpuProgram::destroy_internal();
  202. }
  203. ID3D11PixelShader * D3D11GpuFragmentProgram::getPixelShader() const
  204. {
  205. return mPixelShader;
  206. }
  207. /************************************************************************/
  208. /* SERIALIZATION */
  209. /************************************************************************/
  210. RTTITypeBase* D3D11GpuFragmentProgram::getRTTIStatic()
  211. {
  212. return D3D11GpuFragmentProgramRTTI::instance();
  213. }
  214. RTTITypeBase* D3D11GpuFragmentProgram::getRTTI() const
  215. {
  216. return D3D11GpuFragmentProgram::getRTTIStatic();
  217. }
  218. D3D11GpuGeometryProgram::D3D11GpuGeometryProgram(const String& source, const String& entryPoint,
  219. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  220. : D3D11GpuProgram(source, entryPoint, GPT_GEOMETRY_PROGRAM, profile, includes, isAdjacencyInfoRequired)
  221. , mGeometryShader(nullptr)
  222. { }
  223. D3D11GpuGeometryProgram::~D3D11GpuGeometryProgram()
  224. { }
  225. void D3D11GpuGeometryProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  226. {
  227. HRESULT hr = device.getD3D11Device()->CreateGeometryShader(
  228. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  229. device.getClassLinkage(), &mGeometryShader);
  230. if (FAILED(hr) || device.hasError())
  231. {
  232. String errorDescription = device.getErrorDescription();
  233. BS_EXCEPT(RenderingAPIException,
  234. "Cannot create D3D11 geometry shader from microcode.\nError Description:" + errorDescription);
  235. }
  236. }
  237. void D3D11GpuGeometryProgram::destroy_internal()
  238. {
  239. SAFE_RELEASE(mGeometryShader);
  240. D3D11GpuProgram::destroy_internal();
  241. }
  242. ID3D11GeometryShader * D3D11GpuGeometryProgram::getGeometryShader() const
  243. {
  244. return mGeometryShader;
  245. }
  246. /************************************************************************/
  247. /* SERIALIZATION */
  248. /************************************************************************/
  249. RTTITypeBase* D3D11GpuGeometryProgram::getRTTIStatic()
  250. {
  251. return D3D11GpuGeometryProgramRTTI::instance();
  252. }
  253. RTTITypeBase* D3D11GpuGeometryProgram::getRTTI() const
  254. {
  255. return D3D11GpuGeometryProgram::getRTTIStatic();
  256. }
  257. D3D11GpuDomainProgram::D3D11GpuDomainProgram(const String& source, const String& entryPoint,
  258. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  259. : D3D11GpuProgram(source, entryPoint, GPT_DOMAIN_PROGRAM, profile, includes, false)
  260. , mDomainShader(nullptr)
  261. { }
  262. D3D11GpuDomainProgram::~D3D11GpuDomainProgram()
  263. { }
  264. void D3D11GpuDomainProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  265. {
  266. HRESULT hr = device.getD3D11Device()->CreateDomainShader(
  267. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  268. device.getClassLinkage(), &mDomainShader);
  269. if (FAILED(hr) || device.hasError())
  270. {
  271. String errorDescription = device.getErrorDescription();
  272. BS_EXCEPT(RenderingAPIException,
  273. "Cannot create D3D11 domain shader from microcode.\nError Description:" + errorDescription);
  274. }
  275. }
  276. void D3D11GpuDomainProgram::destroy_internal()
  277. {
  278. SAFE_RELEASE(mDomainShader);
  279. D3D11GpuProgram::destroy_internal();
  280. }
  281. ID3D11DomainShader * D3D11GpuDomainProgram::getDomainShader() const
  282. {
  283. return mDomainShader;
  284. }
  285. /************************************************************************/
  286. /* SERIALIZATION */
  287. /************************************************************************/
  288. RTTITypeBase* D3D11GpuDomainProgram::getRTTIStatic()
  289. {
  290. return D3D11GpuDomainProgramRTTI::instance();
  291. }
  292. RTTITypeBase* D3D11GpuDomainProgram::getRTTI() const
  293. {
  294. return D3D11GpuDomainProgram::getRTTIStatic();
  295. }
  296. D3D11GpuHullProgram::D3D11GpuHullProgram(const String& source, const String& entryPoint,
  297. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  298. : D3D11GpuProgram(source, entryPoint, GPT_HULL_PROGRAM, profile, includes, false)
  299. , mHullShader(nullptr)
  300. { }
  301. D3D11GpuHullProgram::~D3D11GpuHullProgram()
  302. { }
  303. void D3D11GpuHullProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  304. {
  305. // Create the shader
  306. HRESULT hr = device.getD3D11Device()->CreateHullShader(
  307. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  308. device.getClassLinkage(), &mHullShader);
  309. if (FAILED(hr) || device.hasError())
  310. {
  311. String errorDescription = device.getErrorDescription();
  312. BS_EXCEPT(RenderingAPIException,
  313. "Cannot create D3D11 hull shader from microcode.\nError Description:" + errorDescription);
  314. }
  315. }
  316. void D3D11GpuHullProgram::destroy_internal()
  317. {
  318. SAFE_RELEASE(mHullShader);
  319. D3D11GpuProgram::destroy_internal();
  320. }
  321. ID3D11HullShader* D3D11GpuHullProgram::getHullShader() const
  322. {
  323. return mHullShader;
  324. }
  325. /************************************************************************/
  326. /* SERIALIZATION */
  327. /************************************************************************/
  328. RTTITypeBase* D3D11GpuHullProgram::getRTTIStatic()
  329. {
  330. return D3D11GpuHullProgramRTTI::instance();
  331. }
  332. RTTITypeBase* D3D11GpuHullProgram::getRTTI() const
  333. {
  334. return D3D11GpuHullProgram::getRTTIStatic();
  335. }
  336. D3D11GpuComputeProgram::D3D11GpuComputeProgram(const String& source, const String& entryPoint,
  337. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  338. : D3D11GpuProgram(source, entryPoint, GPT_COMPUTE_PROGRAM, profile, includes, false)
  339. , mComputeShader(nullptr)
  340. { }
  341. D3D11GpuComputeProgram::~D3D11GpuComputeProgram()
  342. { }
  343. void D3D11GpuComputeProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  344. {
  345. HRESULT hr = device.getD3D11Device()->CreateComputeShader(
  346. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  347. device.getClassLinkage(), &mComputeShader);
  348. if (FAILED(hr) || device.hasError())
  349. {
  350. String errorDescription = device.getErrorDescription();
  351. BS_EXCEPT(RenderingAPIException,
  352. "Cannot create D3D11 compute shader from microcode.\nError Description:" + errorDescription);
  353. }
  354. }
  355. void D3D11GpuComputeProgram::destroy_internal()
  356. {
  357. SAFE_RELEASE(mComputeShader);
  358. D3D11GpuProgram::destroy_internal();
  359. }
  360. ID3D11ComputeShader* D3D11GpuComputeProgram::getComputeShader() const
  361. {
  362. return mComputeShader;
  363. }
  364. /************************************************************************/
  365. /* SERIALIZATION */
  366. /************************************************************************/
  367. RTTITypeBase* D3D11GpuComputeProgram::getRTTIStatic()
  368. {
  369. return D3D11GpuComputeProgramRTTI::instance();
  370. }
  371. RTTITypeBase* D3D11GpuComputeProgram::getRTTI() const
  372. {
  373. return D3D11GpuComputeProgram::getRTTIStatic();
  374. }
  375. }