CmD3D11GpuProgram.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "CmD3D11GpuProgram.h"
  2. #include "CmD3D11Device.h"
  3. #include "CmException.h"
  4. #include "CmDebug.h"
  5. namespace CamelotEngine
  6. {
  7. D3D11GpuProgram::D3D11GpuProgram(GpuProgramType type, GpuProgramProfile profile)
  8. : GpuProgram("", "", "", type, profile)
  9. {
  10. }
  11. void D3D11GpuProgram::loadImpl(void)
  12. {
  13. // Call polymorphic load
  14. loadFromSource();
  15. }
  16. void D3D11GpuProgram::loadFromSource(void)
  17. {
  18. CM_EXCEPT(RenderingAPIException, "DirectX 11 doesn't support assembly shaders.");
  19. }
  20. D3D11GpuVertexProgram::D3D11GpuVertexProgram(GpuProgramProfile profile)
  21. : D3D11GpuProgram(GPT_VERTEX_PROGRAM, profile)
  22. , mVertexShader(nullptr)
  23. { }
  24. D3D11GpuVertexProgram::~D3D11GpuVertexProgram()
  25. {
  26. // have to call this here reather than in Resource destructor
  27. // since calling virtual methods in base destructors causes crash
  28. unload();
  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. NULL,
  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::unloadImpl(void)
  53. {
  54. SAFE_RELEASE(mVertexShader);
  55. }
  56. ID3D11VertexShader * D3D11GpuVertexProgram::getVertexShader( void ) const
  57. {
  58. return mVertexShader;
  59. }
  60. D3D11GpuFragmentProgram::D3D11GpuFragmentProgram(GpuProgramProfile profile)
  61. : D3D11GpuProgram(GPT_FRAGMENT_PROGRAM, profile)
  62. , mPixelShader(nullptr)
  63. { }
  64. D3D11GpuFragmentProgram::~D3D11GpuFragmentProgram()
  65. {
  66. unload();
  67. }
  68. void D3D11GpuFragmentProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  69. {
  70. if (isSupported())
  71. {
  72. // Create the shader
  73. HRESULT hr = device.getD3D11Device()->CreatePixelShader(
  74. static_cast<DWORD*>(microcode->GetBufferPointer()),
  75. microcode->GetBufferSize(),
  76. NULL,
  77. &mPixelShader);
  78. if (FAILED(hr) || device.hasError())
  79. {
  80. String errorDescription = device.getErrorDescription();
  81. CM_EXCEPT(RenderingAPIException,
  82. "Cannot create D3D11 pixel shader from microcode.\nError Description:" + errorDescription);
  83. }
  84. }
  85. else
  86. {
  87. LOGWRN("Unsupported D3D11 pixel shader was not loaded.");
  88. }
  89. }
  90. void D3D11GpuFragmentProgram::unloadImpl(void)
  91. {
  92. SAFE_RELEASE(mPixelShader);
  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. unload();
  105. }
  106. void D3D11GpuGeometryProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  107. {
  108. if (isSupported())
  109. {
  110. // Create the shader
  111. HRESULT hr = device.getD3D11Device()->CreateGeometryShader(
  112. static_cast<DWORD*>(microcode->GetBufferPointer()),
  113. microcode->GetBufferSize(),
  114. NULL,
  115. &mGeometryShader);
  116. if (FAILED(hr) || device.hasError())
  117. {
  118. String errorDescription = device.getErrorDescription();
  119. CM_EXCEPT(RenderingAPIException,
  120. "Cannot create D3D11 geometry shader from microcode.\nError Description:" + errorDescription);
  121. }
  122. }
  123. else
  124. {
  125. LOGWRN("Unsupported D3D11 geometry shader was not loaded.");
  126. }
  127. }
  128. void D3D11GpuGeometryProgram::unloadImpl(void)
  129. {
  130. SAFE_RELEASE(mGeometryShader);
  131. }
  132. ID3D11GeometryShader * D3D11GpuGeometryProgram::getGeometryShader(void) const
  133. {
  134. return mGeometryShader;
  135. }
  136. D3D11GpuDomainProgram::D3D11GpuDomainProgram(GpuProgramProfile profile)
  137. : D3D11GpuProgram(GPT_DOMAIN_PROGRAM, profile)
  138. , mDomainShader(nullptr)
  139. { }
  140. D3D11GpuDomainProgram::~D3D11GpuDomainProgram()
  141. {
  142. unload();
  143. }
  144. void D3D11GpuDomainProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  145. {
  146. if (isSupported())
  147. {
  148. // Create the shader
  149. HRESULT hr = device.getD3D11Device()->CreateDomainShader(
  150. static_cast<DWORD*>(microcode->GetBufferPointer()),
  151. microcode->GetBufferSize(),
  152. NULL,
  153. &mDomainShader);
  154. if (FAILED(hr) || device.hasError())
  155. {
  156. String errorDescription = device.getErrorDescription();
  157. CM_EXCEPT(RenderingAPIException,
  158. "Cannot create D3D11 domain shader from microcode.\nError Description:" + errorDescription);
  159. }
  160. }
  161. else
  162. {
  163. LOGWRN("Unsupported D3D11 domain shader was not loaded.");
  164. }
  165. }
  166. void D3D11GpuDomainProgram::unloadImpl(void)
  167. {
  168. SAFE_RELEASE(mDomainShader);
  169. }
  170. ID3D11DomainShader * D3D11GpuDomainProgram::getDomainShader(void) const
  171. {
  172. return mDomainShader;
  173. }
  174. D3D11GpuHullProgram::D3D11GpuHullProgram(GpuProgramProfile profile)
  175. : D3D11GpuProgram(GPT_HULL_PROGRAM, profile)
  176. , mHullShader(nullptr)
  177. { }
  178. D3D11GpuHullProgram::~D3D11GpuHullProgram()
  179. {
  180. unload();
  181. }
  182. void D3D11GpuHullProgram::loadFromMicrocode(D3D11Device& device, ID3D10Blob* microcode)
  183. {
  184. if (isSupported())
  185. {
  186. // Create the shader
  187. HRESULT hr = device.getD3D11Device()->CreateHullShader(
  188. static_cast<DWORD*>(microcode->GetBufferPointer()),
  189. microcode->GetBufferSize(),
  190. NULL,
  191. &mHullShader);
  192. if (FAILED(hr) || device.hasError())
  193. {
  194. String errorDescription = device.getErrorDescription();
  195. CM_EXCEPT(RenderingAPIException,
  196. "Cannot create D3D11 hull shader from microcode.\nError Description:" + errorDescription);
  197. }
  198. }
  199. else
  200. {
  201. LOGWRN("Unsupported D3D11 hull shader was not loaded.");
  202. }
  203. }
  204. void D3D11GpuHullProgram::unloadImpl(void)
  205. {
  206. SAFE_RELEASE(mHullShader);
  207. }
  208. ID3D11HullShader* D3D11GpuHullProgram::getHullShader(void) const
  209. {
  210. return mHullShader;
  211. }
  212. }