CmD3D11GpuProgram.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. return;
  30. }
  31. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  32. String hlslProfile = rs->getCapabilities()->gpuProgProfileToRSSpecificProfile(mProfile);
  33. ID3DBlob* microcode = compileMicrocode(hlslProfile);
  34. if (microcode != nullptr)
  35. {
  36. mMicrocode.resize(microcode->GetBufferSize());
  37. memcpy(&mMicrocode[0], microcode->GetBufferPointer(), microcode->GetBufferSize());
  38. populateParametersAndConstants(microcode);
  39. loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  40. SAFE_RELEASE(microcode);
  41. }
  42. mProgramId = GlobalProgramId++;
  43. GpuProgram::initialize_internal();
  44. }
  45. void D3D11GpuProgram::destroy_internal()
  46. {
  47. mMicrocode.clear();
  48. mInputDeclaration = nullptr;
  49. GpuProgram::destroy_internal();
  50. }
  51. ID3DBlob* D3D11GpuProgram::compileMicrocode(const String& profile)
  52. {
  53. // TODO - Preprocessor defines aren't supported
  54. UINT compileFlags = 0;
  55. #if defined(CM_DEBUG_MODE)
  56. compileFlags |= D3DCOMPILE_DEBUG;
  57. compileFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  58. #endif
  59. if (mColumnMajorMatrices)
  60. compileFlags |= D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
  61. else
  62. compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
  63. if (mEnableBackwardsCompatibility)
  64. compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  65. ID3DBlob* microCode = nullptr;
  66. ID3DBlob* errors = nullptr;
  67. HRESULT hr = D3DCompile(
  68. mSource.c_str(), // [in] Pointer to the shader in memory.
  69. mSource.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. mEntryPoint.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 D3D11GpuProgram::populateParametersAndConstants(ID3DBlob* microcode)
  98. {
  99. assert(microcode != nullptr);
  100. D3D11HLSLParamParser parser;
  101. if (mType == GPT_VERTEX_PROGRAM)
  102. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration();
  103. parser.parse(microcode, mParametersDesc, mInputDeclaration);
  104. }
  105. GpuParamsPtr D3D11GpuProgram::createParameters()
  106. {
  107. GpuParamsPtr params = cm_shared_ptr<GpuParams, PoolAlloc>(std::ref(mParametersDesc), mColumnMajorMatrices);
  108. return params;
  109. }
  110. const String& D3D11GpuProgram::getLanguage() const
  111. {
  112. static String name = "hlsl";
  113. return name;
  114. }
  115. /************************************************************************/
  116. /* SERIALIZATION */
  117. /************************************************************************/
  118. RTTITypeBase* D3D11GpuProgram::getRTTIStatic()
  119. {
  120. return D3D11GpuProgramRTTI::instance();
  121. }
  122. RTTITypeBase* D3D11GpuProgram::getRTTI() const
  123. {
  124. return D3D11GpuProgram::getRTTIStatic();
  125. }
  126. D3D11GpuVertexProgram::D3D11GpuVertexProgram(const String& source, const String& entryPoint,
  127. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  128. : D3D11GpuProgram(source, entryPoint, GPT_VERTEX_PROGRAM, profile, includes, isAdjacencyInfoRequired)
  129. , mVertexShader(nullptr)
  130. { }
  131. D3D11GpuVertexProgram::~D3D11GpuVertexProgram()
  132. { }
  133. void D3D11GpuVertexProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  134. {
  135. HRESULT hr = device.getD3D11Device()->CreateVertexShader(
  136. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  137. device.getClassLinkage(), &mVertexShader);
  138. if (FAILED(hr) || device.hasError())
  139. {
  140. String errorDescription = device.getErrorDescription();
  141. CM_EXCEPT(RenderingAPIException,
  142. "Cannot create D3D11 vertex shader from microcode\nError Description:" + errorDescription);
  143. }
  144. }
  145. void D3D11GpuVertexProgram::destroy_internal()
  146. {
  147. SAFE_RELEASE(mVertexShader);
  148. D3D11GpuProgram::destroy_internal();
  149. }
  150. ID3D11VertexShader * D3D11GpuVertexProgram::getVertexShader() const
  151. {
  152. return mVertexShader;
  153. }
  154. D3D11GpuFragmentProgram::D3D11GpuFragmentProgram(const String& source, const String& entryPoint,
  155. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  156. : D3D11GpuProgram(source, entryPoint, GPT_FRAGMENT_PROGRAM, profile, includes, isAdjacencyInfoRequired)
  157. , mPixelShader(nullptr)
  158. { }
  159. D3D11GpuFragmentProgram::~D3D11GpuFragmentProgram()
  160. { }
  161. void D3D11GpuFragmentProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  162. {
  163. HRESULT hr = device.getD3D11Device()->CreatePixelShader(
  164. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  165. device.getClassLinkage(), &mPixelShader);
  166. if (FAILED(hr) || device.hasError())
  167. {
  168. String errorDescription = device.getErrorDescription();
  169. CM_EXCEPT(RenderingAPIException,
  170. "Cannot create D3D11 pixel shader from microcode.\nError Description:" + errorDescription);
  171. }
  172. }
  173. void D3D11GpuFragmentProgram::destroy_internal()
  174. {
  175. SAFE_RELEASE(mPixelShader);
  176. D3D11GpuProgram::destroy_internal();
  177. }
  178. ID3D11PixelShader * D3D11GpuFragmentProgram::getPixelShader() const
  179. {
  180. return mPixelShader;
  181. }
  182. D3D11GpuGeometryProgram::D3D11GpuGeometryProgram(const String& source, const String& entryPoint,
  183. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  184. : D3D11GpuProgram(source, entryPoint, GPT_GEOMETRY_PROGRAM, profile, includes, isAdjacencyInfoRequired)
  185. , mGeometryShader(nullptr)
  186. { }
  187. D3D11GpuGeometryProgram::~D3D11GpuGeometryProgram()
  188. { }
  189. void D3D11GpuGeometryProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  190. {
  191. HRESULT hr = device.getD3D11Device()->CreateGeometryShader(
  192. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  193. device.getClassLinkage(), &mGeometryShader);
  194. if (FAILED(hr) || device.hasError())
  195. {
  196. String errorDescription = device.getErrorDescription();
  197. CM_EXCEPT(RenderingAPIException,
  198. "Cannot create D3D11 geometry shader from microcode.\nError Description:" + errorDescription);
  199. }
  200. }
  201. void D3D11GpuGeometryProgram::destroy_internal()
  202. {
  203. SAFE_RELEASE(mGeometryShader);
  204. D3D11GpuProgram::destroy_internal();
  205. }
  206. ID3D11GeometryShader * D3D11GpuGeometryProgram::getGeometryShader() const
  207. {
  208. return mGeometryShader;
  209. }
  210. D3D11GpuDomainProgram::D3D11GpuDomainProgram(const String& source, const String& entryPoint,
  211. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  212. : D3D11GpuProgram(source, entryPoint, GPT_DOMAIN_PROGRAM, profile, includes, isAdjacencyInfoRequired)
  213. , mDomainShader(nullptr)
  214. { }
  215. D3D11GpuDomainProgram::~D3D11GpuDomainProgram()
  216. { }
  217. void D3D11GpuDomainProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  218. {
  219. HRESULT hr = device.getD3D11Device()->CreateDomainShader(
  220. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  221. device.getClassLinkage(), &mDomainShader);
  222. if (FAILED(hr) || device.hasError())
  223. {
  224. String errorDescription = device.getErrorDescription();
  225. CM_EXCEPT(RenderingAPIException,
  226. "Cannot create D3D11 domain shader from microcode.\nError Description:" + errorDescription);
  227. }
  228. }
  229. void D3D11GpuDomainProgram::destroy_internal()
  230. {
  231. SAFE_RELEASE(mDomainShader);
  232. D3D11GpuProgram::destroy_internal();
  233. }
  234. ID3D11DomainShader * D3D11GpuDomainProgram::getDomainShader() const
  235. {
  236. return mDomainShader;
  237. }
  238. D3D11GpuHullProgram::D3D11GpuHullProgram(const String& source, const String& entryPoint,
  239. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  240. : D3D11GpuProgram(source, entryPoint, GPT_HULL_PROGRAM, profile, includes, isAdjacencyInfoRequired)
  241. , mHullShader(nullptr)
  242. { }
  243. D3D11GpuHullProgram::~D3D11GpuHullProgram()
  244. { }
  245. void D3D11GpuHullProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  246. {
  247. // Create the shader
  248. HRESULT hr = device.getD3D11Device()->CreateHullShader(
  249. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  250. device.getClassLinkage(), &mHullShader);
  251. if (FAILED(hr) || device.hasError())
  252. {
  253. String errorDescription = device.getErrorDescription();
  254. CM_EXCEPT(RenderingAPIException,
  255. "Cannot create D3D11 hull shader from microcode.\nError Description:" + errorDescription);
  256. }
  257. }
  258. void D3D11GpuHullProgram::destroy_internal()
  259. {
  260. SAFE_RELEASE(mHullShader);
  261. D3D11GpuProgram::destroy_internal();
  262. }
  263. ID3D11HullShader* D3D11GpuHullProgram::getHullShader() const
  264. {
  265. return mHullShader;
  266. }
  267. D3D11GpuComputeProgram::D3D11GpuComputeProgram(const String& source, const String& entryPoint,
  268. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes, bool isAdjacencyInfoRequired)
  269. : D3D11GpuProgram(source, entryPoint, GPT_COMPUTE_PROGRAM, profile, includes, isAdjacencyInfoRequired)
  270. , mComputeShader(nullptr)
  271. { }
  272. D3D11GpuComputeProgram::~D3D11GpuComputeProgram()
  273. { }
  274. void D3D11GpuComputeProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  275. {
  276. HRESULT hr = device.getD3D11Device()->CreateComputeShader(
  277. static_cast<DWORD*>(microcode->GetBufferPointer()), microcode->GetBufferSize(),
  278. device.getClassLinkage(), &mComputeShader);
  279. if (FAILED(hr) || device.hasError())
  280. {
  281. String errorDescription = device.getErrorDescription();
  282. CM_EXCEPT(RenderingAPIException,
  283. "Cannot create D3D11 compute shader from microcode.\nError Description:" + errorDescription);
  284. }
  285. }
  286. void D3D11GpuComputeProgram::destroy_internal()
  287. {
  288. SAFE_RELEASE(mComputeShader);
  289. D3D11GpuProgram::destroy_internal();
  290. }
  291. ID3D11ComputeShader* D3D11GpuComputeProgram::getComputeShader() const
  292. {
  293. return mComputeShader;
  294. }
  295. }