d3d11-rebuild.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /******************************************************************************
  2. Copyright (C) 2016 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "d3d11-subsystem.hpp"
  15. inline void gs_vertex_buffer::Rebuild()
  16. {
  17. uvBuffers.clear();
  18. uvSizes.clear();
  19. BuildBuffers();
  20. }
  21. inline void gs_index_buffer::Rebuild(ID3D11Device *dev)
  22. {
  23. HRESULT hr = dev->CreateBuffer(&bd, &srd, &indexBuffer);
  24. if (FAILED(hr))
  25. throw HRError("Failed to create buffer", hr);
  26. }
  27. void gs_texture_2d::RebuildSharedTextureFallback()
  28. {
  29. td = {};
  30. td.Width = 2;
  31. td.Height = 2;
  32. td.MipLevels = 1;
  33. td.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  34. td.ArraySize = 1;
  35. td.SampleDesc.Count = 1;
  36. width = td.Width;
  37. height = td.Height;
  38. dxgiFormat = td.Format;
  39. levels = 1;
  40. resourceDesc = {};
  41. resourceDesc.Format = td.Format;
  42. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  43. resourceDesc.Texture2D.MipLevels = 1;
  44. isShared = false;
  45. }
  46. inline void gs_texture_2d::Rebuild(ID3D11Device *dev)
  47. {
  48. HRESULT hr;
  49. if (isShared) {
  50. hr = dev->OpenSharedResource((HANDLE)(uintptr_t)sharedHandle,
  51. __uuidof(ID3D11Texture2D), (void**)&texture);
  52. if (FAILED(hr)) {
  53. blog(LOG_WARNING, "Failed to rebuild shared texture: ",
  54. "0x%08lX", hr);
  55. RebuildSharedTextureFallback();
  56. }
  57. }
  58. if (!isShared) {
  59. hr = dev->CreateTexture2D(&td,
  60. data.size() ? srd.data() : nullptr,
  61. &texture);
  62. if (FAILED(hr))
  63. throw HRError("Failed to create 2D texture", hr);
  64. }
  65. hr = dev->CreateShaderResourceView(texture, &resourceDesc, &shaderRes);
  66. if (FAILED(hr))
  67. throw HRError("Failed to create resource view", hr);
  68. if (isRenderTarget)
  69. InitRenderTargets();
  70. if (isGDICompatible) {
  71. hr = texture->QueryInterface(__uuidof(IDXGISurface1),
  72. (void**)&gdiSurface);
  73. if (FAILED(hr))
  74. throw HRError("Failed to create GDI surface", hr);
  75. }
  76. }
  77. inline void gs_zstencil_buffer::Rebuild(ID3D11Device *dev)
  78. {
  79. HRESULT hr;
  80. hr = dev->CreateTexture2D(&td, nullptr, &texture);
  81. if (FAILED(hr))
  82. throw HRError("Failed to create depth stencil texture", hr);
  83. hr = dev->CreateDepthStencilView(texture, &dsvd, &view);
  84. if (FAILED(hr))
  85. throw HRError("Failed to create depth stencil view", hr);
  86. }
  87. inline void gs_stage_surface::Rebuild(ID3D11Device *dev)
  88. {
  89. HRESULT hr = dev->CreateTexture2D(&td, nullptr, &texture);
  90. if (FAILED(hr))
  91. throw HRError("Failed to create staging surface", hr);
  92. }
  93. inline void gs_sampler_state::Rebuild(ID3D11Device *dev)
  94. {
  95. HRESULT hr = dev->CreateSamplerState(&sd, state.Assign());
  96. if (FAILED(hr))
  97. throw HRError("Failed to create sampler state", hr);
  98. }
  99. inline void gs_vertex_shader::Rebuild(ID3D11Device *dev)
  100. {
  101. HRESULT hr;
  102. hr = dev->CreateVertexShader(data.data(), data.size(), nullptr, &shader);
  103. if (FAILED(hr))
  104. throw HRError("Failed to create vertex shader", hr);
  105. hr = dev->CreateInputLayout(layoutData.data(), (UINT)layoutData.size(),
  106. data.data(), data.size(), &layout);
  107. if (FAILED(hr))
  108. throw HRError("Failed to create input layout", hr);
  109. if (constantSize) {
  110. hr = dev->CreateBuffer(&bd, NULL, &constants);
  111. if (FAILED(hr))
  112. throw HRError("Failed to create constant buffer", hr);
  113. }
  114. for (gs_shader_param &param : params) {
  115. param.nextSampler = nullptr;
  116. param.curValue.clear();
  117. gs_shader_set_default(&param);
  118. }
  119. }
  120. inline void gs_pixel_shader::Rebuild(ID3D11Device *dev)
  121. {
  122. HRESULT hr;
  123. hr = dev->CreatePixelShader(data.data(), data.size(), nullptr,
  124. &shader);
  125. if (FAILED(hr))
  126. throw HRError("Failed to create pixel shader", hr);
  127. if (constantSize) {
  128. hr = dev->CreateBuffer(&bd, NULL, &constants);
  129. if (FAILED(hr))
  130. throw HRError("Failed to create constant buffer", hr);
  131. }
  132. for (gs_shader_param &param : params) {
  133. param.nextSampler = nullptr;
  134. param.curValue.clear();
  135. gs_shader_set_default(&param);
  136. }
  137. }
  138. inline void gs_swap_chain::Rebuild(ID3D11Device *dev)
  139. {
  140. HRESULT hr = device->factory->CreateSwapChain(dev, &swapDesc, &swap);
  141. if (FAILED(hr))
  142. throw HRError("Failed to create swap chain", hr);
  143. Init();
  144. }
  145. inline void SavedBlendState::Rebuild(ID3D11Device *dev)
  146. {
  147. HRESULT hr = dev->CreateBlendState(&bd, &state);
  148. if (FAILED(hr))
  149. throw HRError("Failed to create blend state", hr);
  150. }
  151. inline void SavedZStencilState::Rebuild(ID3D11Device *dev)
  152. {
  153. HRESULT hr = dev->CreateDepthStencilState(&dsd, &state);
  154. if (FAILED(hr))
  155. throw HRError("Failed to create depth stencil state", hr);
  156. }
  157. inline void SavedRasterState::Rebuild(ID3D11Device *dev)
  158. {
  159. HRESULT hr = dev->CreateRasterizerState(&rd, &state);
  160. if (FAILED(hr))
  161. throw HRError("Failed to create rasterizer state", hr);
  162. }
  163. const static D3D_FEATURE_LEVEL featureLevels[] =
  164. {
  165. D3D_FEATURE_LEVEL_11_0,
  166. D3D_FEATURE_LEVEL_10_1,
  167. D3D_FEATURE_LEVEL_10_0,
  168. D3D_FEATURE_LEVEL_9_3,
  169. };
  170. void gs_device::RebuildDevice()
  171. try {
  172. ID3D11Device *dev = nullptr;
  173. HRESULT hr;
  174. blog(LOG_WARNING, "Device Remove/Reset! Rebuilding all assets...");
  175. /* ----------------------------------------------------------------- */
  176. gs_obj *obj = first_obj;
  177. while (obj) {
  178. switch (obj->obj_type) {
  179. case gs_type::gs_vertex_buffer:
  180. ((gs_vertex_buffer*)obj)->Release();
  181. break;
  182. case gs_type::gs_index_buffer:
  183. ((gs_index_buffer*)obj)->Release();
  184. break;
  185. case gs_type::gs_texture_2d:
  186. ((gs_texture_2d*)obj)->Release();
  187. break;
  188. case gs_type::gs_zstencil_buffer:
  189. ((gs_zstencil_buffer*)obj)->Release();
  190. break;
  191. case gs_type::gs_stage_surface:
  192. ((gs_stage_surface*)obj)->Release();
  193. break;
  194. case gs_type::gs_sampler_state:
  195. ((gs_sampler_state*)obj)->Release();
  196. break;
  197. case gs_type::gs_vertex_shader:
  198. ((gs_vertex_shader*)obj)->Release();
  199. break;
  200. case gs_type::gs_pixel_shader:
  201. ((gs_pixel_shader*)obj)->Release();
  202. break;
  203. case gs_type::gs_duplicator:
  204. ((gs_duplicator*)obj)->Release();
  205. break;
  206. case gs_type::gs_swap_chain:
  207. ((gs_swap_chain*)obj)->Release();
  208. break;
  209. }
  210. obj = obj->next;
  211. }
  212. for (auto &state : zstencilStates)
  213. state.Release();
  214. for (auto &state : rasterStates)
  215. state.Release();
  216. for (auto &state : blendStates)
  217. state.Release();
  218. context->ClearState();
  219. context.Release();
  220. device.Release();
  221. adapter.Release();
  222. factory.Release();
  223. /* ----------------------------------------------------------------- */
  224. InitFactory(adpIdx);
  225. uint32_t createFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  226. hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN,
  227. nullptr, createFlags, featureLevels,
  228. sizeof(featureLevels) / sizeof(D3D_FEATURE_LEVEL),
  229. D3D11_SDK_VERSION, &device, nullptr, &context);
  230. if (FAILED(hr))
  231. throw HRError("Failed to create device", hr);
  232. dev = device;
  233. obj = first_obj;
  234. while (obj) {
  235. switch (obj->obj_type) {
  236. case gs_type::gs_vertex_buffer:
  237. ((gs_vertex_buffer*)obj)->Rebuild();
  238. break;
  239. case gs_type::gs_index_buffer:
  240. ((gs_index_buffer*)obj)->Rebuild(dev);
  241. break;
  242. case gs_type::gs_texture_2d:
  243. ((gs_texture_2d*)obj)->Rebuild(dev);
  244. break;
  245. case gs_type::gs_zstencil_buffer:
  246. ((gs_zstencil_buffer*)obj)->Rebuild(dev);
  247. break;
  248. case gs_type::gs_stage_surface:
  249. ((gs_stage_surface*)obj)->Rebuild(dev);
  250. break;
  251. case gs_type::gs_sampler_state:
  252. ((gs_sampler_state*)obj)->Rebuild(dev);
  253. break;
  254. case gs_type::gs_vertex_shader:
  255. ((gs_vertex_shader*)obj)->Rebuild(dev);
  256. break;
  257. case gs_type::gs_pixel_shader:
  258. ((gs_pixel_shader*)obj)->Rebuild(dev);
  259. break;
  260. case gs_type::gs_duplicator:
  261. ((gs_duplicator*)obj)->Start();
  262. break;
  263. case gs_type::gs_swap_chain:
  264. ((gs_swap_chain*)obj)->Rebuild(dev);
  265. break;
  266. }
  267. obj = obj->next;
  268. }
  269. curRenderTarget = nullptr;
  270. curZStencilBuffer = nullptr;
  271. curRenderSide = 0;
  272. memset(&curTextures, 0, sizeof(curTextures));
  273. memset(&curSamplers, 0, sizeof(curSamplers));
  274. curVertexBuffer = nullptr;
  275. curIndexBuffer = nullptr;
  276. curVertexShader = nullptr;
  277. curPixelShader = nullptr;
  278. curSwapChain = nullptr;
  279. zstencilStateChanged = true;
  280. rasterStateChanged = true;
  281. blendStateChanged = true;
  282. curDepthStencilState = nullptr;
  283. curRasterState = nullptr;
  284. curBlendState = nullptr;
  285. curToplogy = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
  286. for (auto &state : zstencilStates)
  287. state.Rebuild(dev);
  288. for (auto &state : rasterStates)
  289. state.Rebuild(dev);
  290. for (auto &state : blendStates)
  291. state.Rebuild(dev);
  292. } catch (const char *error) {
  293. bcrash("Failed to recreate D3D11: %s", error);
  294. } catch (HRError error) {
  295. bcrash("Failed to recreate D3D11: %s (%08lX)",
  296. error.str, error.hr);
  297. }