CmD3D11GpuProgram.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "CmD3D11GpuProgram.h"
  2. #include "CmD3D11Device.h"
  3. #include "CmException.h"
  4. #include "CmDebug.h"
  5. #include "CmRenderSystem.h"
  6. #include "CmGpuProgramManager.h"
  7. namespace BansheeEngine
  8. {
  9. D3D11GpuProgram::D3D11GpuProgram(GpuProgramType type, GpuProgramProfile profile)
  10. : GpuProgram("", "", type, profile, nullptr)
  11. {
  12. }
  13. D3D11GpuProgram::~D3D11GpuProgram()
  14. {
  15. }
  16. bool D3D11GpuProgram::isSupported() const
  17. {
  18. if (!isRequiredCapabilitiesSupported())
  19. return false;
  20. RenderSystem* rs = BansheeEngine::RenderSystem::instancePtr();
  21. String hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  22. return rs->getCapabilities()->isShaderProfileSupported(hlslProfile);
  23. }
  24. D3D11GpuVertexProgram::D3D11GpuVertexProgram(GpuProgramProfile profile)
  25. : D3D11GpuProgram(GPT_VERTEX_PROGRAM, profile)
  26. , mVertexShader(nullptr)
  27. { }
  28. D3D11GpuVertexProgram::~D3D11GpuVertexProgram()
  29. { }
  30. void D3D11GpuVertexProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  31. {
  32. if (isSupported())
  33. {
  34. // Create the shader
  35. HRESULT hr = device.getD3D11Device()->CreateVertexShader(
  36. static_cast<DWORD*>(microcode->GetBufferPointer()),
  37. microcode->GetBufferSize(),
  38. device.getClassLinkage(),
  39. &mVertexShader);
  40. if (FAILED(hr) || device.hasError())
  41. {
  42. String errorDescription = device.getErrorDescription();
  43. CM_EXCEPT(RenderingAPIException,
  44. "Cannot create D3D11 vertex shader from microcode\nError Description:" + errorDescription);
  45. }
  46. }
  47. else
  48. {
  49. LOGWRN("Unsupported D3D11 vertex shader was not loaded.");
  50. }
  51. }
  52. void D3D11GpuVertexProgram::destroy_internal()
  53. {
  54. SAFE_RELEASE(mVertexShader);
  55. D3D11GpuProgram::destroy_internal();
  56. }
  57. ID3D11VertexShader * D3D11GpuVertexProgram::getVertexShader( void ) const
  58. {
  59. return mVertexShader;
  60. }
  61. D3D11GpuFragmentProgram::D3D11GpuFragmentProgram(GpuProgramProfile profile)
  62. : D3D11GpuProgram(GPT_FRAGMENT_PROGRAM, profile)
  63. , mPixelShader(nullptr)
  64. { }
  65. D3D11GpuFragmentProgram::~D3D11GpuFragmentProgram()
  66. { }
  67. void D3D11GpuFragmentProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  68. {
  69. if (isSupported())
  70. {
  71. // Create the shader
  72. HRESULT hr = device.getD3D11Device()->CreatePixelShader(
  73. static_cast<DWORD*>(microcode->GetBufferPointer()),
  74. microcode->GetBufferSize(),
  75. device.getClassLinkage(),
  76. &mPixelShader);
  77. if (FAILED(hr) || device.hasError())
  78. {
  79. String errorDescription = device.getErrorDescription();
  80. CM_EXCEPT(RenderingAPIException,
  81. "Cannot create D3D11 pixel shader from microcode.\nError Description:" + errorDescription);
  82. }
  83. }
  84. else
  85. {
  86. LOGWRN("Unsupported D3D11 pixel shader was not loaded.");
  87. }
  88. }
  89. void D3D11GpuFragmentProgram::destroy_internal()
  90. {
  91. SAFE_RELEASE(mPixelShader);
  92. D3D11GpuProgram::destroy_internal();
  93. }
  94. ID3D11PixelShader * D3D11GpuFragmentProgram::getPixelShader( void ) const
  95. {
  96. return mPixelShader;
  97. }
  98. D3D11GpuGeometryProgram::D3D11GpuGeometryProgram(GpuProgramProfile profile)
  99. : D3D11GpuProgram(GPT_GEOMETRY_PROGRAM, profile)
  100. , mGeometryShader(nullptr)
  101. { }
  102. D3D11GpuGeometryProgram::~D3D11GpuGeometryProgram()
  103. { }
  104. void D3D11GpuGeometryProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  105. {
  106. if (isSupported())
  107. {
  108. // Create the shader
  109. HRESULT hr = device.getD3D11Device()->CreateGeometryShader(
  110. static_cast<DWORD*>(microcode->GetBufferPointer()),
  111. microcode->GetBufferSize(),
  112. device.getClassLinkage(),
  113. &mGeometryShader);
  114. if (FAILED(hr) || device.hasError())
  115. {
  116. String errorDescription = device.getErrorDescription();
  117. CM_EXCEPT(RenderingAPIException,
  118. "Cannot create D3D11 geometry shader from microcode.\nError Description:" + errorDescription);
  119. }
  120. }
  121. else
  122. {
  123. LOGWRN("Unsupported D3D11 geometry shader was not loaded.");
  124. }
  125. }
  126. void D3D11GpuGeometryProgram::destroy_internal()
  127. {
  128. SAFE_RELEASE(mGeometryShader);
  129. D3D11GpuProgram::destroy_internal();
  130. }
  131. ID3D11GeometryShader * D3D11GpuGeometryProgram::getGeometryShader(void) const
  132. {
  133. return mGeometryShader;
  134. }
  135. D3D11GpuDomainProgram::D3D11GpuDomainProgram(GpuProgramProfile profile)
  136. : D3D11GpuProgram(GPT_DOMAIN_PROGRAM, profile)
  137. , mDomainShader(nullptr)
  138. { }
  139. D3D11GpuDomainProgram::~D3D11GpuDomainProgram()
  140. { }
  141. void D3D11GpuDomainProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  142. {
  143. if (isSupported())
  144. {
  145. // Create the shader
  146. HRESULT hr = device.getD3D11Device()->CreateDomainShader(
  147. static_cast<DWORD*>(microcode->GetBufferPointer()),
  148. microcode->GetBufferSize(),
  149. device.getClassLinkage(),
  150. &mDomainShader);
  151. if (FAILED(hr) || device.hasError())
  152. {
  153. String errorDescription = device.getErrorDescription();
  154. CM_EXCEPT(RenderingAPIException,
  155. "Cannot create D3D11 domain shader from microcode.\nError Description:" + errorDescription);
  156. }
  157. }
  158. else
  159. {
  160. LOGWRN("Unsupported D3D11 domain shader was not loaded.");
  161. }
  162. }
  163. void D3D11GpuDomainProgram::destroy_internal()
  164. {
  165. SAFE_RELEASE(mDomainShader);
  166. D3D11GpuProgram::destroy_internal();
  167. }
  168. ID3D11DomainShader * D3D11GpuDomainProgram::getDomainShader() const
  169. {
  170. return mDomainShader;
  171. }
  172. D3D11GpuHullProgram::D3D11GpuHullProgram(GpuProgramProfile profile)
  173. : D3D11GpuProgram(GPT_HULL_PROGRAM, profile)
  174. , mHullShader(nullptr)
  175. { }
  176. D3D11GpuHullProgram::~D3D11GpuHullProgram()
  177. { }
  178. void D3D11GpuHullProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  179. {
  180. if (isSupported())
  181. {
  182. // Create the shader
  183. HRESULT hr = device.getD3D11Device()->CreateHullShader(
  184. static_cast<DWORD*>(microcode->GetBufferPointer()),
  185. microcode->GetBufferSize(),
  186. device.getClassLinkage(),
  187. &mHullShader);
  188. if (FAILED(hr) || device.hasError())
  189. {
  190. String errorDescription = device.getErrorDescription();
  191. CM_EXCEPT(RenderingAPIException,
  192. "Cannot create D3D11 hull shader from microcode.\nError Description:" + errorDescription);
  193. }
  194. }
  195. else
  196. {
  197. LOGWRN("Unsupported D3D11 hull shader was not loaded.");
  198. }
  199. }
  200. void D3D11GpuHullProgram::destroy_internal()
  201. {
  202. SAFE_RELEASE(mHullShader);
  203. D3D11GpuProgram::destroy_internal();
  204. }
  205. ID3D11HullShader* D3D11GpuHullProgram::getHullShader(void) const
  206. {
  207. return mHullShader;
  208. }
  209. D3D11GpuComputeProgram::D3D11GpuComputeProgram(GpuProgramProfile profile)
  210. : D3D11GpuProgram(GPT_COMPUTE_PROGRAM, profile)
  211. , mComputeShader(nullptr)
  212. { }
  213. D3D11GpuComputeProgram::~D3D11GpuComputeProgram()
  214. { }
  215. void D3D11GpuComputeProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  216. {
  217. if (isSupported())
  218. {
  219. // Create the shader
  220. HRESULT hr = device.getD3D11Device()->CreateComputeShader(
  221. static_cast<DWORD*>(microcode->GetBufferPointer()),
  222. microcode->GetBufferSize(),
  223. device.getClassLinkage(),
  224. &mComputeShader);
  225. if (FAILED(hr) || device.hasError())
  226. {
  227. String errorDescription = device.getErrorDescription();
  228. CM_EXCEPT(RenderingAPIException,
  229. "Cannot create D3D11 compute shader from microcode.\nError Description:" + errorDescription);
  230. }
  231. }
  232. else
  233. {
  234. LOGWRN("Unsupported D3D11 compute shader was not loaded.");
  235. }
  236. }
  237. void D3D11GpuComputeProgram::destroy_internal()
  238. {
  239. SAFE_RELEASE(mComputeShader);
  240. D3D11GpuProgram::destroy_internal();
  241. }
  242. ID3D11ComputeShader* D3D11GpuComputeProgram::getComputeShader(void) const
  243. {
  244. return mComputeShader;
  245. }
  246. }