BsD3D11GpuProgram.cpp 11 KB

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