BsD3D11GpuProgram.cpp 14 KB

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