BsD3D11GpuProgram.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. VertexDeclaration::VertexElementList inputParams;
  109. parser.parse(microcode, *mParametersDesc, &inputParams);
  110. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration(inputParams);
  111. }
  112. else
  113. {
  114. parser.parse(microcode, *mParametersDesc, nullptr);
  115. }
  116. }
  117. GpuParamsPtr D3D11GpuProgramCore::createParameters()
  118. {
  119. GpuParamsPtr params = bs_shared_ptr<GpuParams, PoolAlloc>(mParametersDesc, mColumnMajorMatrices);
  120. return params;
  121. }
  122. D3D11GpuVertexProgramCore::D3D11GpuVertexProgramCore(const String& source, const String& entryPoint,
  123. GpuProgramProfile profile)
  124. : D3D11GpuProgramCore(source, entryPoint, GPT_VERTEX_PROGRAM, profile, false)
  125. , mVertexShader(nullptr)
  126. { }
  127. D3D11GpuVertexProgramCore::~D3D11GpuVertexProgramCore()
  128. { }
  129. void D3D11GpuVertexProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  130. {
  131. HRESULT hr = device.getD3D11Device()->CreateVertexShader(
  132. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  133. device.getClassLinkage(), &mVertexShader);
  134. if (FAILED(hr) || device.hasError())
  135. {
  136. String errorDescription = device.getErrorDescription();
  137. BS_EXCEPT(RenderingAPIException,
  138. "Cannot create D3D11 vertex shader from microcode\nError Description:" + errorDescription);
  139. }
  140. }
  141. void D3D11GpuVertexProgramCore::destroy()
  142. {
  143. SAFE_RELEASE(mVertexShader);
  144. D3D11GpuProgramCore::destroy();
  145. }
  146. ID3D11VertexShader * D3D11GpuVertexProgramCore::getVertexShader() const
  147. {
  148. return mVertexShader;
  149. }
  150. D3D11GpuFragmentProgramCore::D3D11GpuFragmentProgramCore(const String& source, const String& entryPoint,
  151. GpuProgramProfile profile)
  152. : D3D11GpuProgramCore(source, entryPoint, GPT_FRAGMENT_PROGRAM, profile, false)
  153. , mPixelShader(nullptr)
  154. { }
  155. D3D11GpuFragmentProgramCore::~D3D11GpuFragmentProgramCore()
  156. { }
  157. void D3D11GpuFragmentProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  158. {
  159. HRESULT hr = device.getD3D11Device()->CreatePixelShader(
  160. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  161. device.getClassLinkage(), &mPixelShader);
  162. if (FAILED(hr) || device.hasError())
  163. {
  164. String errorDescription = device.getErrorDescription();
  165. BS_EXCEPT(RenderingAPIException,
  166. "Cannot create D3D11 pixel shader from microcode.\nError Description:" + errorDescription);
  167. }
  168. }
  169. void D3D11GpuFragmentProgramCore::destroy()
  170. {
  171. SAFE_RELEASE(mPixelShader);
  172. D3D11GpuProgramCore::destroy();
  173. }
  174. ID3D11PixelShader * D3D11GpuFragmentProgramCore::getPixelShader() const
  175. {
  176. return mPixelShader;
  177. }
  178. D3D11GpuGeometryProgramCore::D3D11GpuGeometryProgramCore(const String& source, const String& entryPoint,
  179. GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  180. : D3D11GpuProgramCore(source, entryPoint, GPT_GEOMETRY_PROGRAM, profile, isAdjacencyInfoRequired)
  181. , mGeometryShader(nullptr)
  182. { }
  183. D3D11GpuGeometryProgramCore::~D3D11GpuGeometryProgramCore()
  184. { }
  185. void D3D11GpuGeometryProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  186. {
  187. HRESULT hr = device.getD3D11Device()->CreateGeometryShader(
  188. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  189. device.getClassLinkage(), &mGeometryShader);
  190. if (FAILED(hr) || device.hasError())
  191. {
  192. String errorDescription = device.getErrorDescription();
  193. BS_EXCEPT(RenderingAPIException,
  194. "Cannot create D3D11 geometry shader from microcode.\nError Description:" + errorDescription);
  195. }
  196. }
  197. void D3D11GpuGeometryProgramCore::destroy()
  198. {
  199. SAFE_RELEASE(mGeometryShader);
  200. D3D11GpuProgramCore::destroy();
  201. }
  202. ID3D11GeometryShader * D3D11GpuGeometryProgramCore::getGeometryShader() const
  203. {
  204. return mGeometryShader;
  205. }
  206. D3D11GpuDomainProgramCore::D3D11GpuDomainProgramCore(const String& source, const String& entryPoint,
  207. GpuProgramProfile profile)
  208. : D3D11GpuProgramCore(source, entryPoint, GPT_DOMAIN_PROGRAM, profile, false)
  209. , mDomainShader(nullptr)
  210. { }
  211. D3D11GpuDomainProgramCore::~D3D11GpuDomainProgramCore()
  212. { }
  213. void D3D11GpuDomainProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  214. {
  215. HRESULT hr = device.getD3D11Device()->CreateDomainShader(
  216. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  217. device.getClassLinkage(), &mDomainShader);
  218. if (FAILED(hr) || device.hasError())
  219. {
  220. String errorDescription = device.getErrorDescription();
  221. BS_EXCEPT(RenderingAPIException,
  222. "Cannot create D3D11 domain shader from microcode.\nError Description:" + errorDescription);
  223. }
  224. }
  225. void D3D11GpuDomainProgramCore::destroy()
  226. {
  227. SAFE_RELEASE(mDomainShader);
  228. D3D11GpuProgramCore::destroy();
  229. }
  230. ID3D11DomainShader * D3D11GpuDomainProgramCore::getDomainShader() const
  231. {
  232. return mDomainShader;
  233. }
  234. D3D11GpuHullProgramCore::D3D11GpuHullProgramCore(const String& source, const String& entryPoint,
  235. GpuProgramProfile profile)
  236. : D3D11GpuProgramCore(source, entryPoint, GPT_HULL_PROGRAM, profile, false)
  237. , mHullShader(nullptr)
  238. { }
  239. D3D11GpuHullProgramCore::~D3D11GpuHullProgramCore()
  240. { }
  241. void D3D11GpuHullProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  242. {
  243. // Create the shader
  244. HRESULT hr = device.getD3D11Device()->CreateHullShader(
  245. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  246. device.getClassLinkage(), &mHullShader);
  247. if (FAILED(hr) || device.hasError())
  248. {
  249. String errorDescription = device.getErrorDescription();
  250. BS_EXCEPT(RenderingAPIException,
  251. "Cannot create D3D11 hull shader from microcode.\nError Description:" + errorDescription);
  252. }
  253. }
  254. void D3D11GpuHullProgramCore::destroy()
  255. {
  256. SAFE_RELEASE(mHullShader);
  257. D3D11GpuProgramCore::destroy();
  258. }
  259. ID3D11HullShader* D3D11GpuHullProgramCore::getHullShader() const
  260. {
  261. return mHullShader;
  262. }
  263. D3D11GpuComputeProgramCore::D3D11GpuComputeProgramCore(const String& source, const String& entryPoint,
  264. GpuProgramProfile profile)
  265. : D3D11GpuProgramCore(source, entryPoint, GPT_COMPUTE_PROGRAM, profile, false)
  266. , mComputeShader(nullptr)
  267. { }
  268. D3D11GpuComputeProgramCore::~D3D11GpuComputeProgramCore()
  269. { }
  270. void D3D11GpuComputeProgramCore::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  271. {
  272. HRESULT hr = device.getD3D11Device()->CreateComputeShader(
  273. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  274. device.getClassLinkage(), &mComputeShader);
  275. if (FAILED(hr) || device.hasError())
  276. {
  277. String errorDescription = device.getErrorDescription();
  278. BS_EXCEPT(RenderingAPIException,
  279. "Cannot create D3D11 compute shader from microcode.\nError Description:" + errorDescription);
  280. }
  281. }
  282. void D3D11GpuComputeProgramCore::destroy()
  283. {
  284. SAFE_RELEASE(mComputeShader);
  285. D3D11GpuProgramCore::destroy();
  286. }
  287. ID3D11ComputeShader* D3D11GpuComputeProgramCore::getComputeShader() const
  288. {
  289. return mComputeShader;
  290. }
  291. }