CmD3D11GpuProgram.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #include "CmD3D11GpuProgram.h"
  2. #include "CmD3D11Device.h"
  3. #include "CmException.h"
  4. #include "CmDebug.h"
  5. #include "CmGpuParams.h"
  6. #include "CmD3D11RenderSystem.h"
  7. #include "CmGpuProgramManager.h"
  8. #include "CmHardwareBufferManager.h"
  9. #include "CmD3D11HLSLParamParser.h"
  10. #include "CmD3D11GpuProgramRTTI.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(CM_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. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration();
  104. parser.parse(microcode, mParametersDesc, mInputDeclaration);
  105. }
  106. GpuParamsPtr D3D11GpuProgram::createParameters()
  107. {
  108. GpuParamsPtr params = cm_shared_ptr<GpuParams, PoolAlloc>(std::ref(mParametersDesc), mColumnMajorMatrices);
  109. return params;
  110. }
  111. const String& D3D11GpuProgram::getLanguage() const
  112. {
  113. static String name = "hlsl";
  114. return name;
  115. }
  116. /************************************************************************/
  117. /* SERIALIZATION */
  118. /************************************************************************/
  119. RTTITypeBase* D3D11GpuProgram::getRTTIStatic()
  120. {
  121. return D3D11GpuProgramRTTI::instance();
  122. }
  123. RTTITypeBase* D3D11GpuProgram::getRTTI() const
  124. {
  125. return D3D11GpuProgram::getRTTIStatic();
  126. }
  127. D3D11GpuVertexProgram::D3D11GpuVertexProgram(const String& source, const String& entryPoint,
  128. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  129. : D3D11GpuProgram(source, entryPoint, GPT_VERTEX_PROGRAM, profile, includes, false)
  130. , mVertexShader(nullptr)
  131. { }
  132. D3D11GpuVertexProgram::~D3D11GpuVertexProgram()
  133. { }
  134. void D3D11GpuVertexProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  135. {
  136. HRESULT hr = device.getD3D11Device()->CreateVertexShader(
  137. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  138. device.getClassLinkage(), &mVertexShader);
  139. if (FAILED(hr) || device.hasError())
  140. {
  141. String errorDescription = device.getErrorDescription();
  142. CM_EXCEPT(RenderingAPIException,
  143. "Cannot create D3D11 vertex shader from microcode\nError Description:" + errorDescription);
  144. }
  145. }
  146. void D3D11GpuVertexProgram::destroy_internal()
  147. {
  148. SAFE_RELEASE(mVertexShader);
  149. D3D11GpuProgram::destroy_internal();
  150. }
  151. ID3D11VertexShader * D3D11GpuVertexProgram::getVertexShader() const
  152. {
  153. return mVertexShader;
  154. }
  155. /************************************************************************/
  156. /* SERIALIZATION */
  157. /************************************************************************/
  158. RTTITypeBase* D3D11GpuVertexProgram::getRTTIStatic()
  159. {
  160. return D3D11GpuVertexProgramRTTI::instance();
  161. }
  162. RTTITypeBase* D3D11GpuVertexProgram::getRTTI() const
  163. {
  164. return D3D11GpuVertexProgram::getRTTIStatic();
  165. }
  166. D3D11GpuFragmentProgram::D3D11GpuFragmentProgram(const String& source, const String& entryPoint,
  167. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  168. : D3D11GpuProgram(source, entryPoint, GPT_FRAGMENT_PROGRAM, profile, includes, false)
  169. , mPixelShader(nullptr)
  170. { }
  171. D3D11GpuFragmentProgram::~D3D11GpuFragmentProgram()
  172. { }
  173. void D3D11GpuFragmentProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  174. {
  175. HRESULT hr = device.getD3D11Device()->CreatePixelShader(
  176. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  177. device.getClassLinkage(), &mPixelShader);
  178. if (FAILED(hr) || device.hasError())
  179. {
  180. String errorDescription = device.getErrorDescription();
  181. CM_EXCEPT(RenderingAPIException,
  182. "Cannot create D3D11 pixel shader from microcode.\nError Description:" + errorDescription);
  183. }
  184. }
  185. void D3D11GpuFragmentProgram::destroy_internal()
  186. {
  187. SAFE_RELEASE(mPixelShader);
  188. D3D11GpuProgram::destroy_internal();
  189. }
  190. ID3D11PixelShader * D3D11GpuFragmentProgram::getPixelShader() const
  191. {
  192. return mPixelShader;
  193. }
  194. /************************************************************************/
  195. /* SERIALIZATION */
  196. /************************************************************************/
  197. RTTITypeBase* D3D11GpuFragmentProgram::getRTTIStatic()
  198. {
  199. return D3D11GpuFragmentProgramRTTI::instance();
  200. }
  201. RTTITypeBase* D3D11GpuFragmentProgram::getRTTI() const
  202. {
  203. return D3D11GpuFragmentProgram::getRTTIStatic();
  204. }
  205. D3D11GpuGeometryProgram::D3D11GpuGeometryProgram(const String& source, const String& entryPoint,
  206. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  207. : D3D11GpuProgram(source, entryPoint, GPT_GEOMETRY_PROGRAM, profile, includes, isAdjacencyInfoRequired)
  208. , mGeometryShader(nullptr)
  209. { }
  210. D3D11GpuGeometryProgram::~D3D11GpuGeometryProgram()
  211. { }
  212. void D3D11GpuGeometryProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  213. {
  214. HRESULT hr = device.getD3D11Device()->CreateGeometryShader(
  215. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  216. device.getClassLinkage(), &mGeometryShader);
  217. if (FAILED(hr) || device.hasError())
  218. {
  219. String errorDescription = device.getErrorDescription();
  220. CM_EXCEPT(RenderingAPIException,
  221. "Cannot create D3D11 geometry shader from microcode.\nError Description:" + errorDescription);
  222. }
  223. }
  224. void D3D11GpuGeometryProgram::destroy_internal()
  225. {
  226. SAFE_RELEASE(mGeometryShader);
  227. D3D11GpuProgram::destroy_internal();
  228. }
  229. ID3D11GeometryShader * D3D11GpuGeometryProgram::getGeometryShader() const
  230. {
  231. return mGeometryShader;
  232. }
  233. /************************************************************************/
  234. /* SERIALIZATION */
  235. /************************************************************************/
  236. RTTITypeBase* D3D11GpuGeometryProgram::getRTTIStatic()
  237. {
  238. return D3D11GpuGeometryProgramRTTI::instance();
  239. }
  240. RTTITypeBase* D3D11GpuGeometryProgram::getRTTI() const
  241. {
  242. return D3D11GpuGeometryProgram::getRTTIStatic();
  243. }
  244. D3D11GpuDomainProgram::D3D11GpuDomainProgram(const String& source, const String& entryPoint,
  245. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  246. : D3D11GpuProgram(source, entryPoint, GPT_DOMAIN_PROGRAM, profile, includes, false)
  247. , mDomainShader(nullptr)
  248. { }
  249. D3D11GpuDomainProgram::~D3D11GpuDomainProgram()
  250. { }
  251. void D3D11GpuDomainProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  252. {
  253. HRESULT hr = device.getD3D11Device()->CreateDomainShader(
  254. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  255. device.getClassLinkage(), &mDomainShader);
  256. if (FAILED(hr) || device.hasError())
  257. {
  258. String errorDescription = device.getErrorDescription();
  259. CM_EXCEPT(RenderingAPIException,
  260. "Cannot create D3D11 domain shader from microcode.\nError Description:" + errorDescription);
  261. }
  262. }
  263. void D3D11GpuDomainProgram::destroy_internal()
  264. {
  265. SAFE_RELEASE(mDomainShader);
  266. D3D11GpuProgram::destroy_internal();
  267. }
  268. ID3D11DomainShader * D3D11GpuDomainProgram::getDomainShader() const
  269. {
  270. return mDomainShader;
  271. }
  272. /************************************************************************/
  273. /* SERIALIZATION */
  274. /************************************************************************/
  275. RTTITypeBase* D3D11GpuDomainProgram::getRTTIStatic()
  276. {
  277. return D3D11GpuDomainProgramRTTI::instance();
  278. }
  279. RTTITypeBase* D3D11GpuDomainProgram::getRTTI() const
  280. {
  281. return D3D11GpuDomainProgram::getRTTIStatic();
  282. }
  283. D3D11GpuHullProgram::D3D11GpuHullProgram(const String& source, const String& entryPoint,
  284. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  285. : D3D11GpuProgram(source, entryPoint, GPT_HULL_PROGRAM, profile, includes, false)
  286. , mHullShader(nullptr)
  287. { }
  288. D3D11GpuHullProgram::~D3D11GpuHullProgram()
  289. { }
  290. void D3D11GpuHullProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  291. {
  292. // Create the shader
  293. HRESULT hr = device.getD3D11Device()->CreateHullShader(
  294. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  295. device.getClassLinkage(), &mHullShader);
  296. if (FAILED(hr) || device.hasError())
  297. {
  298. String errorDescription = device.getErrorDescription();
  299. CM_EXCEPT(RenderingAPIException,
  300. "Cannot create D3D11 hull shader from microcode.\nError Description:" + errorDescription);
  301. }
  302. }
  303. void D3D11GpuHullProgram::destroy_internal()
  304. {
  305. SAFE_RELEASE(mHullShader);
  306. D3D11GpuProgram::destroy_internal();
  307. }
  308. ID3D11HullShader* D3D11GpuHullProgram::getHullShader() const
  309. {
  310. return mHullShader;
  311. }
  312. /************************************************************************/
  313. /* SERIALIZATION */
  314. /************************************************************************/
  315. RTTITypeBase* D3D11GpuHullProgram::getRTTIStatic()
  316. {
  317. return D3D11GpuHullProgramRTTI::instance();
  318. }
  319. RTTITypeBase* D3D11GpuHullProgram::getRTTI() const
  320. {
  321. return D3D11GpuHullProgram::getRTTIStatic();
  322. }
  323. D3D11GpuComputeProgram::D3D11GpuComputeProgram(const String& source, const String& entryPoint,
  324. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes)
  325. : D3D11GpuProgram(source, entryPoint, GPT_COMPUTE_PROGRAM, profile, includes, false)
  326. , mComputeShader(nullptr)
  327. { }
  328. D3D11GpuComputeProgram::~D3D11GpuComputeProgram()
  329. { }
  330. void D3D11GpuComputeProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  331. {
  332. HRESULT hr = device.getD3D11Device()->CreateComputeShader(
  333. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  334. device.getClassLinkage(), &mComputeShader);
  335. if (FAILED(hr) || device.hasError())
  336. {
  337. String errorDescription = device.getErrorDescription();
  338. CM_EXCEPT(RenderingAPIException,
  339. "Cannot create D3D11 compute shader from microcode.\nError Description:" + errorDescription);
  340. }
  341. }
  342. void D3D11GpuComputeProgram::destroy_internal()
  343. {
  344. SAFE_RELEASE(mComputeShader);
  345. D3D11GpuProgram::destroy_internal();
  346. }
  347. ID3D11ComputeShader* D3D11GpuComputeProgram::getComputeShader() const
  348. {
  349. return mComputeShader;
  350. }
  351. /************************************************************************/
  352. /* SERIALIZATION */
  353. /************************************************************************/
  354. RTTITypeBase* D3D11GpuComputeProgram::getRTTIStatic()
  355. {
  356. return D3D11GpuComputeProgramRTTI::instance();
  357. }
  358. RTTITypeBase* D3D11GpuComputeProgram::getRTTI() const
  359. {
  360. return D3D11GpuComputeProgram::getRTTIStatic();
  361. }
  362. }