2
0

CmD3D11RenderSystem.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "CmD3D11RenderSystem.h"
  2. #include "CmD3D11DriverList.h"
  3. #include "CmD3D11Driver.h"
  4. #include "CmD3D11Device.h"
  5. #include "CmRenderSystem.h"
  6. #include "CmDebug.h"
  7. #include "CmException.h"
  8. namespace CamelotEngine
  9. {
  10. D3D11RenderSystem::D3D11RenderSystem()
  11. : mDXGIFactory(nullptr), mDevice(nullptr), mDriverList(nullptr)
  12. , mActiveD3DDriver(nullptr), mFeatureLevel(D3D_FEATURE_LEVEL_9_1)
  13. {
  14. }
  15. D3D11RenderSystem::~D3D11RenderSystem()
  16. {
  17. destroy_internal();
  18. }
  19. const String& D3D11RenderSystem::getName() const
  20. {
  21. static String strName("D3D11RenderSystem");
  22. return strName;
  23. }
  24. void D3D11RenderSystem::initialize_internal()
  25. {
  26. HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&mDXGIFactory);
  27. if(FAILED(hr))
  28. CM_EXCEPT(RenderingAPIException, "Failed to create Direct3D11 DXGIFactory");
  29. mDriverList = new D3D11DriverList(mDXGIFactory);
  30. mActiveD3DDriver = mDriverList->item(0); // TODO: Always get first driver, for now
  31. IDXGIAdapter* selectedAdapter = mActiveD3DDriver->getDeviceAdapter();
  32. D3D_FEATURE_LEVEL requestedLevels[] = {
  33. D3D_FEATURE_LEVEL_11_0,
  34. D3D_FEATURE_LEVEL_10_1,
  35. D3D_FEATURE_LEVEL_10_0,
  36. D3D_FEATURE_LEVEL_9_3,
  37. D3D_FEATURE_LEVEL_9_2,
  38. D3D_FEATURE_LEVEL_9_1
  39. };
  40. UINT32 numRequestedLevel = sizeof(requestedLevels) / sizeof(requestedLevels[0]);
  41. ID3D11Device* device;
  42. hr = D3D11CreateDevice(selectedAdapter, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0,
  43. requestedLevels, numRequestedLevel, D3D11_SDK_VERSION, &device, &mFeatureLevel, 0);
  44. if(FAILED(hr))
  45. CM_EXCEPT(RenderingAPIException, "Failed to create Direct3D11 object. D3D11CreateDeviceN returned this error code: " + toString(hr));
  46. mDevice = new D3D11Device(device);
  47. LARGE_INTEGER driverVersion;
  48. if(SUCCEEDED(selectedAdapter->CheckInterfaceSupport(IID_ID3D10Device /* intentionally D3D10, not D3D11 */, &driverVersion)))
  49. {
  50. mDriverVersion.major = HIWORD(driverVersion.HighPart);
  51. mDriverVersion.minor = LOWORD(driverVersion.HighPart);
  52. mDriverVersion.release = HIWORD(driverVersion.LowPart);
  53. mDriverVersion.build = LOWORD(driverVersion.LowPart);
  54. }
  55. RenderSystem::initialize_internal();
  56. }
  57. void D3D11RenderSystem::destroy_internal()
  58. {
  59. SAFE_RELEASE(mDXGIFactory);
  60. SAFE_DELETE(mDevice);
  61. SAFE_DELETE(mDriverList);
  62. mActiveD3DDriver = nullptr;
  63. RenderSystem::destroy_internal();
  64. }
  65. void D3D11RenderSystem::setSamplerState(UINT16 texUnit, const SamplerState& samplerState)
  66. {
  67. throw std::exception("The method or operation is not implemented.");
  68. }
  69. void D3D11RenderSystem::setBlendState(const BlendState& blendState)
  70. {
  71. throw std::exception("The method or operation is not implemented.");
  72. }
  73. void D3D11RenderSystem::setRasterizerState(const RasterizerState& rasterizerState)
  74. {
  75. throw std::exception("The method or operation is not implemented.");
  76. }
  77. void D3D11RenderSystem::setDepthStencilState(const DepthStencilState& depthStencilState)
  78. {
  79. throw std::exception("The method or operation is not implemented.");
  80. }
  81. void D3D11RenderSystem::setStencilRefValue(UINT32 refValue)
  82. {
  83. throw std::exception("The method or operation is not implemented.");
  84. }
  85. void D3D11RenderSystem::setTexture(UINT16 unit, bool enabled, const TexturePtr &texPtr)
  86. {
  87. throw std::exception("The method or operation is not implemented.");
  88. }
  89. void D3D11RenderSystem::disableTextureUnit(UINT16 texUnit)
  90. {
  91. throw std::exception("The method or operation is not implemented.");
  92. }
  93. void D3D11RenderSystem::beginFrame()
  94. {
  95. throw std::exception("The method or operation is not implemented.");
  96. }
  97. void D3D11RenderSystem::endFrame()
  98. {
  99. throw std::exception("The method or operation is not implemented.");
  100. }
  101. void D3D11RenderSystem::setViewport(const Viewport& vp)
  102. {
  103. throw std::exception("The method or operation is not implemented.");
  104. }
  105. void D3D11RenderSystem::setVertexDeclaration(VertexDeclarationPtr decl)
  106. {
  107. throw std::exception("The method or operation is not implemented.");
  108. }
  109. void D3D11RenderSystem::setVertexBufferBinding(VertexBufferBinding* binding)
  110. {
  111. throw std::exception("The method or operation is not implemented.");
  112. }
  113. void D3D11RenderSystem::bindGpuProgram(GpuProgramHandle prg)
  114. {
  115. throw std::exception("The method or operation is not implemented.");
  116. }
  117. void D3D11RenderSystem::unbindGpuProgram(GpuProgramType gptype)
  118. {
  119. throw std::exception("The method or operation is not implemented.");
  120. }
  121. void D3D11RenderSystem::bindGpuProgramParameters(GpuProgramType gptype, GpuProgramParametersSharedPtr params, UINT16 variabilityMask)
  122. {
  123. throw std::exception("The method or operation is not implemented.");
  124. }
  125. void D3D11RenderSystem::setScissorRect(UINT32 left /*= 0*/, UINT32 top /*= 0*/, UINT32 right /*= 800*/, UINT32 bottom /*= 600 */)
  126. {
  127. throw std::exception("The method or operation is not implemented.");
  128. }
  129. void D3D11RenderSystem::clearFrameBuffer(unsigned int buffers, const Color& color /*= Color::Black*/, float depth /*= 1.0f*/, unsigned short stencil /*= 0 */)
  130. {
  131. throw std::exception("The method or operation is not implemented.");
  132. }
  133. void D3D11RenderSystem::setRenderTarget(RenderTarget* target)
  134. {
  135. throw std::exception("The method or operation is not implemented.");
  136. }
  137. void D3D11RenderSystem::setClipPlanesImpl(const PlaneList& clipPlanes)
  138. {
  139. LOGWRN("This call will be ignored. DX11 uses shaders for setting clip planes.");
  140. }
  141. RenderSystemCapabilities* D3D11RenderSystem::createRenderSystemCapabilities() const
  142. {
  143. throw std::exception("The method or operation is not implemented.");
  144. }
  145. void D3D11RenderSystem::initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps)
  146. {
  147. throw std::exception("The method or operation is not implemented.");
  148. }
  149. D3D11Device& D3D11RenderSystem::getPrimaryDevice()
  150. {
  151. CM_EXCEPT(NotImplementedException, "Not implemented");
  152. }
  153. CamelotEngine::String D3D11RenderSystem::getErrorDescription(long errorNumber) const
  154. {
  155. throw std::exception("The method or operation is not implemented.");
  156. }
  157. void D3D11RenderSystem::determineFSAASettings(UINT32 fsaa, const String& fsaaHint, DXGI_FORMAT format, DXGI_SAMPLE_DESC* outFSAASettings)
  158. {
  159. CM_EXCEPT(NotImplementedException, "Not implemented");
  160. /*
  161. bool ok = false;
  162. bool qualityHint = fsaaHint.find("Quality") != String::npos;
  163. size_t origFSAA = fsaa;
  164. bool tryCSAA = false;
  165. // NVIDIA, prefer CSAA if available for 8+
  166. // it would be tempting to use getCapabilities()->getVendor() == GPU_NVIDIA but
  167. // if this is the first window, caps will not be initialised yet
  168. if (mActiveD3DDriver->getAdapterIdentifier().VendorId == 0x10DE &&
  169. fsaa >= 8)
  170. {
  171. tryCSAA = true;
  172. }
  173. while (!ok)
  174. {
  175. // Deal with special cases
  176. if (tryCSAA)
  177. {
  178. // see http://developer.nvidia.com/object/coverage-sampled-aa.html
  179. switch(fsaa)
  180. {
  181. case 8:
  182. if (qualityHint)
  183. {
  184. outFSAASettings->Count = 8;
  185. outFSAASettings->Quality = 8;
  186. }
  187. else
  188. {
  189. outFSAASettings->Count = 4;
  190. outFSAASettings->Quality = 8;
  191. }
  192. break;
  193. case 16:
  194. if (qualityHint)
  195. {
  196. outFSAASettings->Count = 8;
  197. outFSAASettings->Quality = 16;
  198. }
  199. else
  200. {
  201. outFSAASettings->Count = 4;
  202. outFSAASettings->Quality = 16;
  203. }
  204. break;
  205. }
  206. }
  207. else // !CSAA
  208. {
  209. outFSAASettings->Count = fsaa == 0 ? 1 : fsaa;
  210. outFSAASettings->Quality = 0;
  211. }
  212. HRESULT hr;
  213. UINT outQuality;
  214. hr = mDevice->CheckMultisampleQualityLevels(
  215. format,
  216. outFSAASettings->Count,
  217. &outQuality);
  218. if (SUCCEEDED(hr) && (!tryCSAA || outQuality > outFSAASettings->Quality))
  219. {
  220. ok = true;
  221. }
  222. else
  223. {
  224. // downgrade
  225. if (tryCSAA && fsaa == 8)
  226. {
  227. // for CSAA, we'll try downgrading with quality mode at all samples.
  228. // then try without quality, then drop CSAA
  229. if (qualityHint)
  230. {
  231. // drop quality first
  232. qualityHint = false;
  233. }
  234. else
  235. {
  236. // drop CSAA entirely
  237. tryCSAA = false;
  238. }
  239. // return to original requested samples
  240. fsaa = static_cast<UINT32>(origFSAA);
  241. }
  242. else
  243. {
  244. // drop samples
  245. --fsaa;
  246. if (fsaa == 1)
  247. {
  248. // ran out of options, no FSAA
  249. fsaa = 0;
  250. ok = true;
  251. }
  252. }
  253. }
  254. } // while !ok
  255. */
  256. }
  257. bool D3D11RenderSystem::checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage)
  258. {
  259. return true;
  260. }
  261. CamelotEngine::VertexElementType D3D11RenderSystem::getColorVertexElementType() const
  262. {
  263. return VET_COLOR_ABGR;
  264. }
  265. void D3D11RenderSystem::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest, bool forGpuProgram /*= false */)
  266. {
  267. dest = matrix;
  268. // Convert depth range from [-1,+1] to [0,1]
  269. dest[2][0] = (dest[2][0] + dest[3][0]) / 2;
  270. dest[2][1] = (dest[2][1] + dest[3][1]) / 2;
  271. dest[2][2] = (dest[2][2] + dest[3][2]) / 2;
  272. dest[2][3] = (dest[2][3] + dest[3][3]) / 2;
  273. if (!forGpuProgram)
  274. {
  275. // Convert right-handed to left-handed
  276. dest[0][2] = -dest[0][2];
  277. dest[1][2] = -dest[1][2];
  278. dest[2][2] = -dest[2][2];
  279. dest[3][2] = -dest[3][2];
  280. }
  281. }
  282. float D3D11RenderSystem::getHorizontalTexelOffset()
  283. {
  284. return 0.0f;
  285. }
  286. float D3D11RenderSystem::getVerticalTexelOffset()
  287. {
  288. return 0.0f;
  289. }
  290. float D3D11RenderSystem::getMinimumDepthInputValue()
  291. {
  292. return 0.0f;
  293. }
  294. float D3D11RenderSystem::getMaximumDepthInputValue()
  295. {
  296. return -1.0f;
  297. }
  298. }