ShellRenderInterfaceExtensionsDirectX10_Win32.cpp 7.6 KB

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