ShellRenderInterfaceExtensionsDirectX10_Win32.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2014 David Wimsey
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "RenderInterfaceDirectX10.h"
  29. #include <RmlUi/Core.h>
  30. #include <d3d10.h>
  31. #include <d3dx10.h>
  32. // For _T unicode/mbcs macro
  33. #include <tchar.h>
  34. void RenderInterfaceDirectX10::SetContext(void *context)
  35. {
  36. m_rmlui_context = context;
  37. }
  38. void RenderInterfaceDirectX10::SetViewport(int width, int height)
  39. {
  40. if(this->m_pD3D10Device != nullptr)
  41. {
  42. if(width == 0 || height == 0)
  43. {
  44. // Windows with no client area cause crashes
  45. return;
  46. }
  47. if(this->m_pRenderTargetView)
  48. {
  49. // Release the existing render target
  50. this->m_pRenderTargetView->Release();
  51. this->m_pRenderTargetView = nullptr;
  52. }
  53. // Resize the swap chain's buffer to the given dimensions
  54. m_pSwapChain->ResizeBuffers(2, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
  55. // Recreate Render Target
  56. ID3D10Texture2D *pBackBuffer;
  57. if(FAILED(this->m_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &pBackBuffer)))
  58. {
  59. MessageBox(nullptr, _T("SwapChain->GetBuffer failed."), _T("Could not resize DirectX 10 surface"), MB_OK|MB_ICONERROR);
  60. return;
  61. }
  62. if(FAILED(this->m_pD3D10Device->CreateRenderTargetView(pBackBuffer, nullptr, &this->m_pRenderTargetView)))
  63. {
  64. pBackBuffer->Release();
  65. MessageBox(nullptr, _T("D3D10Device->CreateRenderTargetView failed."), _T("Could not resize DirectX 10 surface"), MB_OK|MB_ICONERROR);
  66. return;
  67. }
  68. pBackBuffer->Release();
  69. this->m_pD3D10Device->OMSetRenderTargets(1, &this->m_pRenderTargetView, nullptr);
  70. D3D10_VIEWPORT vp;
  71. vp.Width = width;
  72. vp.Height = height;
  73. vp.MinDepth = 0.0f;
  74. vp.MaxDepth = 1.0f;
  75. vp.TopLeftX = 0;
  76. vp.TopLeftY = 0;
  77. this->m_pD3D10Device->RSSetViewports(1, &vp);
  78. // Recreate our view and projection matrix
  79. D3DXMatrixOrthoOffCenterLH(&this->m_matProjection, 0, width, height, 0, -1, 1);
  80. m_pProjectionMatrixVariable->SetMatrix((float*)this->m_matProjection);
  81. if(m_rmlui_context != nullptr)
  82. {
  83. ((Rml::Core::Context*)m_rmlui_context)->SetDimensions(Rml::Core::Vector2i(width, height));
  84. }
  85. }
  86. }
  87. bool RenderInterfaceDirectX10::AttachToNative(void *nativeWindow)
  88. {
  89. RECT clientRect;
  90. if(!GetClientRect((HWND) nativeWindow, &clientRect))
  91. {
  92. // if we can't lookup the client rect, abort, something is seriously wrong
  93. return false;
  94. }
  95. int width = clientRect.right - clientRect.left;
  96. int height = clientRect.bottom - clientRect.top;
  97. //put the device into debug if we are in a debug build
  98. UINT createDeviceFlags=0;
  99. #ifdef _DEBUG
  100. createDeviceFlags|=D3D10_CREATE_DEVICE_DEBUG;
  101. #endif
  102. //Setup swap chain
  103. DXGI_SWAP_CHAIN_DESC sd;
  104. ZeroMemory(&sd, sizeof(sd));
  105. sd.BufferCount=1;
  106. sd.OutputWindow = (HWND) nativeWindow;
  107. sd.Windowed = TRUE;
  108. sd.SampleDesc.Count = 1;
  109. sd.SampleDesc.Quality = 0;
  110. sd.BufferDesc.Width = width;
  111. sd.BufferDesc.Height = height;
  112. sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  113. sd.BufferDesc.RefreshRate.Numerator = 60;
  114. sd.BufferDesc.RefreshRate.Denominator = 1;
  115. sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  116. //Create device and swapchain
  117. if(FAILED(D3D10CreateDeviceAndSwapChain(nullptr, D3D10_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, D3D10_SDK_VERSION, &sd, &this->m_pSwapChain, &this->m_pD3D10Device)))
  118. {
  119. if(MessageBox(nullptr, _T("D3D10CreateDeviceAndSwapChain failed for D3D10_DRIVER_TYPE_HARDWARE.\r\n\r\nWould you like to try the reference renderer, this will be very slow!"), _T("Could not intialized DirectX 10"), MB_OKCANCEL|MB_ICONERROR) == IDOK)
  120. {
  121. if(FAILED(D3D10CreateDeviceAndSwapChain(nullptr, D3D10_DRIVER_TYPE_REFERENCE, nullptr, createDeviceFlags, D3D10_SDK_VERSION, &sd, &this->m_pSwapChain, &this->m_pD3D10Device)))
  122. {
  123. MessageBox(nullptr, _T("D3D10CreateDeviceAndSwapChain failed for D3D10_DRIVER_TYPE_REFERENCE, giving up."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
  124. return false;
  125. }
  126. }
  127. else
  128. {
  129. return false;
  130. }
  131. }
  132. //Create Render Target
  133. ID3D10Texture2D *pBackBuffer;
  134. if(FAILED (this->m_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D),(void**)&pBackBuffer)))
  135. {
  136. MessageBox(nullptr, _T("SwapChain->GetBuffer failed."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
  137. return false;
  138. }
  139. if(FAILED(this->m_pD3D10Device->CreateRenderTargetView(pBackBuffer, nullptr, &this->m_pRenderTargetView)))
  140. {
  141. pBackBuffer->Release();
  142. MessageBox(nullptr, _T("D3D10Device->CreateRenderTargetView failed."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
  143. return false;
  144. }
  145. pBackBuffer->Release();
  146. this->m_pD3D10Device->OMSetRenderTargets(1, &this->m_pRenderTargetView, nullptr);
  147. D3D10_VIEWPORT vp;
  148. vp.Width = width;
  149. vp.Height = height;
  150. vp.MinDepth = 0.0f;
  151. vp.MaxDepth = 1.0f;
  152. vp.TopLeftX = 0;
  153. vp.TopLeftY = 0;
  154. this->m_pD3D10Device->RSSetViewports(1, &vp);
  155. setupEffect();
  156. //Create our view and projection matrix
  157. D3DXMatrixOrthoOffCenterLH(&this->m_matProjection, 0, width, height, 0, -1, 1);
  158. m_pProjectionMatrixVariable->SetMatrix((float*)this->m_matProjection);
  159. //Create scissor raster states
  160. D3D10_RASTERIZER_DESC rasterDesc;
  161. rasterDesc.FillMode=D3D10_FILL_SOLID;
  162. rasterDesc.CullMode=D3D10_CULL_NONE;
  163. rasterDesc.ScissorEnable=TRUE;
  164. rasterDesc.FrontCounterClockwise=TRUE;
  165. if(FAILED(this->m_pD3D10Device->CreateRasterizerState(&rasterDesc, &this->m_pScissorTestEnable)))
  166. {
  167. Rml::Core::Log::Message(Rml::Core::Log::LT_ERROR, "Can't create Raster State - ScissorEnable");
  168. }
  169. rasterDesc.ScissorEnable=FALSE;
  170. if(FAILED(this->m_pD3D10Device->CreateRasterizerState(&rasterDesc, &this->m_pScissorTestDisable)))
  171. {
  172. Rml::Core::Log::Message(Rml::Core::Log::LT_ERROR, "Can't create Raster State - ScissorDisable");
  173. }
  174. return true;
  175. }
  176. void RenderInterfaceDirectX10::DetachFromNative()
  177. {
  178. if(this->m_pD3D10Device != nullptr)
  179. {
  180. this->m_pD3D10Device->ClearState();
  181. this->m_pD3D10Device = nullptr;
  182. }
  183. if(this->m_pRenderTargetView != nullptr)
  184. {
  185. this->m_pRenderTargetView->Release();
  186. this->m_pRenderTargetView = nullptr;
  187. }
  188. if(this->m_pSwapChain != nullptr)
  189. {
  190. this->m_pSwapChain->Release();
  191. this->m_pSwapChain = nullptr;
  192. }
  193. if(this->m_pD3D10Device != nullptr)
  194. {
  195. this->m_pD3D10Device->Release();
  196. this->m_pD3D10Device = nullptr;
  197. }
  198. }
  199. void RenderInterfaceDirectX10::PrepareRenderBuffer()
  200. {
  201. if(this->m_pD3D10Device == nullptr)
  202. {
  203. return;
  204. }
  205. float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f };
  206. this->m_pD3D10Device->ClearRenderTargetView(this->m_pRenderTargetView, ClearColor);
  207. }
  208. void RenderInterfaceDirectX10::PresentRenderBuffer()
  209. {
  210. if(this->m_pSwapChain == nullptr)
  211. {
  212. return;
  213. }
  214. this->m_pSwapChain->Present(0, 0);
  215. }