BsD3D11GpuProgram.cpp 11 KB

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