BsD3D11GpuProgram.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "BsRenderStats.h"
  11. namespace BansheeEngine
  12. {
  13. UINT32 D3D11GpuProgramCore::GlobalProgramId = 0;
  14. D3D11GpuProgramCore::D3D11GpuProgramCore(const String& source, const String& entryPoint, GpuProgramType gptype,
  15. GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  16. : GpuProgramCore(source, entryPoint, gptype, profile, isAdjacencyInfoRequired),
  17. mColumnMajorMatrices(false), mEnableBackwardsCompatibility(false), mProgramId(0)
  18. {
  19. }
  20. D3D11GpuProgramCore::~D3D11GpuProgramCore()
  21. {
  22. mMicrocode.clear();
  23. mInputDeclaration = nullptr;
  24. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuProgram);
  25. }
  26. void D3D11GpuProgramCore::initialize()
  27. {
  28. if (!isSupported())
  29. {
  30. mIsCompiled = false;
  31. mCompileError = "Specified program is not supported by the current render system.";
  32. GpuProgramCore::initialize();
  33. return;
  34. }
  35. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  36. String hlslProfile = rs->getCapabilities()->gpuProgProfileToRSSpecificProfile(mProperties.getProfile());
  37. ID3DBlob* microcode = compileMicrocode(hlslProfile);
  38. if (microcode != nullptr)
  39. {
  40. mMicrocode.resize(microcode->GetBufferSize());
  41. memcpy(&mMicrocode[0], microcode->GetBufferPointer(), microcode->GetBufferSize());
  42. populateParametersAndConstants(microcode);
  43. loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  44. SAFE_RELEASE(microcode);
  45. }
  46. mProgramId = GlobalProgramId++;
  47. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuProgram);
  48. GpuProgramCore::initialize();
  49. }
  50. ID3DBlob* D3D11GpuProgramCore::compileMicrocode(const String& profile)
  51. {
  52. // TODO - Preprocessor defines aren't supported
  53. UINT compileFlags = 0;
  54. #if defined(BS_DEBUG_MODE)
  55. compileFlags |= D3DCOMPILE_DEBUG;
  56. compileFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  57. #endif
  58. if (mColumnMajorMatrices)
  59. compileFlags |= D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
  60. else
  61. compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
  62. if (mEnableBackwardsCompatibility)
  63. compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  64. ID3DBlob* microCode = nullptr;
  65. ID3DBlob* errors = nullptr;
  66. const String& source = mProperties.getSource();
  67. const String& entryPoint = mProperties.getEntryPoint();
  68. HRESULT hr = D3DCompile(
  69. source.c_str(), // [in] Pointer to the shader in memory.
  70. source.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. entryPoint.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 D3D11GpuProgramCore::populateParametersAndConstants(ID3DBlob* microcode)
  99. {
  100. assert(microcode != nullptr);
  101. D3D11HLSLParamParser parser;
  102. if (mProperties.getType() == GPT_VERTEX_PROGRAM)
  103. {
  104. List<VertexElement> inputParams;
  105. parser.parse(microcode, *mParametersDesc, &inputParams);
  106. mInputDeclaration = HardwareBufferCoreManager::instance().createVertexDeclaration(inputParams);
  107. }
  108. else
  109. {
  110. parser.parse(microcode, *mParametersDesc, nullptr);
  111. }
  112. }
  113. D3D11GpuVertexProgramCore::D3D11GpuVertexProgramCore(const String& source, const String& entryPoint,
  114. GpuProgramProfile profile)
  115. : D3D11GpuProgramCore(source, entryPoint, GPT_VERTEX_PROGRAM, profile, false)
  116. , mVertexShader(nullptr)
  117. { }
  118. D3D11GpuVertexProgramCore::~D3D11GpuVertexProgramCore()
  119. {
  120. SAFE_RELEASE(mVertexShader);
  121. }
  122. void D3D11GpuVertexProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  123. {
  124. HRESULT hr = device.getD3D11Device()->CreateVertexShader(
  125. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  126. device.getClassLinkage(), &mVertexShader);
  127. if (FAILED(hr) || device.hasError())
  128. {
  129. String errorDescription = device.getErrorDescription();
  130. BS_EXCEPT(RenderingAPIException,
  131. "Cannot create D3D11 vertex shader from microcode\nError Description:" + errorDescription);
  132. }
  133. }
  134. ID3D11VertexShader * D3D11GpuVertexProgramCore::getVertexShader() const
  135. {
  136. return mVertexShader;
  137. }
  138. D3D11GpuFragmentProgramCore::D3D11GpuFragmentProgramCore(const String& source, const String& entryPoint,
  139. GpuProgramProfile profile)
  140. : D3D11GpuProgramCore(source, entryPoint, GPT_FRAGMENT_PROGRAM, profile, false)
  141. , mPixelShader(nullptr)
  142. { }
  143. D3D11GpuFragmentProgramCore::~D3D11GpuFragmentProgramCore()
  144. {
  145. SAFE_RELEASE(mPixelShader);
  146. }
  147. void D3D11GpuFragmentProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  148. {
  149. HRESULT hr = device.getD3D11Device()->CreatePixelShader(
  150. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  151. device.getClassLinkage(), &mPixelShader);
  152. if (FAILED(hr) || device.hasError())
  153. {
  154. String errorDescription = device.getErrorDescription();
  155. BS_EXCEPT(RenderingAPIException,
  156. "Cannot create D3D11 pixel shader from microcode.\nError Description:" + errorDescription);
  157. }
  158. }
  159. ID3D11PixelShader * D3D11GpuFragmentProgramCore::getPixelShader() const
  160. {
  161. return mPixelShader;
  162. }
  163. D3D11GpuGeometryProgramCore::D3D11GpuGeometryProgramCore(const String& source, const String& entryPoint,
  164. GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  165. : D3D11GpuProgramCore(source, entryPoint, GPT_GEOMETRY_PROGRAM, profile, isAdjacencyInfoRequired)
  166. , mGeometryShader(nullptr)
  167. { }
  168. D3D11GpuGeometryProgramCore::~D3D11GpuGeometryProgramCore()
  169. {
  170. SAFE_RELEASE(mGeometryShader);
  171. }
  172. void D3D11GpuGeometryProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  173. {
  174. HRESULT hr = device.getD3D11Device()->CreateGeometryShader(
  175. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  176. device.getClassLinkage(), &mGeometryShader);
  177. if (FAILED(hr) || device.hasError())
  178. {
  179. String errorDescription = device.getErrorDescription();
  180. BS_EXCEPT(RenderingAPIException,
  181. "Cannot create D3D11 geometry shader from microcode.\nError Description:" + errorDescription);
  182. }
  183. }
  184. ID3D11GeometryShader * D3D11GpuGeometryProgramCore::getGeometryShader() const
  185. {
  186. return mGeometryShader;
  187. }
  188. D3D11GpuDomainProgramCore::D3D11GpuDomainProgramCore(const String& source, const String& entryPoint,
  189. GpuProgramProfile profile)
  190. : D3D11GpuProgramCore(source, entryPoint, GPT_DOMAIN_PROGRAM, profile, false)
  191. , mDomainShader(nullptr)
  192. { }
  193. D3D11GpuDomainProgramCore::~D3D11GpuDomainProgramCore()
  194. {
  195. SAFE_RELEASE(mDomainShader);
  196. }
  197. void D3D11GpuDomainProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  198. {
  199. HRESULT hr = device.getD3D11Device()->CreateDomainShader(
  200. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  201. device.getClassLinkage(), &mDomainShader);
  202. if (FAILED(hr) || device.hasError())
  203. {
  204. String errorDescription = device.getErrorDescription();
  205. BS_EXCEPT(RenderingAPIException,
  206. "Cannot create D3D11 domain shader from microcode.\nError Description:" + errorDescription);
  207. }
  208. }
  209. ID3D11DomainShader * D3D11GpuDomainProgramCore::getDomainShader() const
  210. {
  211. return mDomainShader;
  212. }
  213. D3D11GpuHullProgramCore::D3D11GpuHullProgramCore(const String& source, const String& entryPoint,
  214. GpuProgramProfile profile)
  215. : D3D11GpuProgramCore(source, entryPoint, GPT_HULL_PROGRAM, profile, false)
  216. , mHullShader(nullptr)
  217. { }
  218. D3D11GpuHullProgramCore::~D3D11GpuHullProgramCore()
  219. {
  220. SAFE_RELEASE(mHullShader);
  221. }
  222. void D3D11GpuHullProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  223. {
  224. // Create the shader
  225. HRESULT hr = device.getD3D11Device()->CreateHullShader(
  226. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  227. device.getClassLinkage(), &mHullShader);
  228. if (FAILED(hr) || device.hasError())
  229. {
  230. String errorDescription = device.getErrorDescription();
  231. BS_EXCEPT(RenderingAPIException,
  232. "Cannot create D3D11 hull shader from microcode.\nError Description:" + errorDescription);
  233. }
  234. }
  235. ID3D11HullShader* D3D11GpuHullProgramCore::getHullShader() const
  236. {
  237. return mHullShader;
  238. }
  239. D3D11GpuComputeProgramCore::D3D11GpuComputeProgramCore(const String& source, const String& entryPoint,
  240. GpuProgramProfile profile)
  241. : D3D11GpuProgramCore(source, entryPoint, GPT_COMPUTE_PROGRAM, profile, false)
  242. , mComputeShader(nullptr)
  243. { }
  244. D3D11GpuComputeProgramCore::~D3D11GpuComputeProgramCore()
  245. {
  246. SAFE_RELEASE(mComputeShader);
  247. }
  248. void D3D11GpuComputeProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  249. {
  250. HRESULT hr = device.getD3D11Device()->CreateComputeShader(
  251. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  252. device.getClassLinkage(), &mComputeShader);
  253. if (FAILED(hr) || device.hasError())
  254. {
  255. String errorDescription = device.getErrorDescription();
  256. BS_EXCEPT(RenderingAPIException,
  257. "Cannot create D3D11 compute shader from microcode.\nError Description:" + errorDescription);
  258. }
  259. }
  260. ID3D11ComputeShader* D3D11GpuComputeProgramCore::getComputeShader() const
  261. {
  262. return mComputeShader;
  263. }
  264. }