d3d11-rebuild.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  37. width = td.Width;
  38. height = td.Height;
  39. dxgiFormat = td.Format;
  40. levels = 1;
  41. resourceDesc = {};
  42. resourceDesc.Format = td.Format;
  43. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  44. resourceDesc.Texture2D.MipLevels = 1;
  45. isShared = false;
  46. }
  47. inline void gs_texture_2d::Rebuild(ID3D11Device *dev)
  48. {
  49. HRESULT hr;
  50. if (isShared) {
  51. hr = dev->OpenSharedResource((HANDLE)(uintptr_t)sharedHandle,
  52. __uuidof(ID3D11Texture2D), (void**)&texture);
  53. if (FAILED(hr)) {
  54. blog(LOG_WARNING, "Failed to rebuild shared texture: ",
  55. "0x%08lX", hr);
  56. RebuildSharedTextureFallback();
  57. }
  58. }
  59. if (!isShared) {
  60. hr = dev->CreateTexture2D(&td,
  61. data.size() ? srd.data() : nullptr,
  62. &texture);
  63. if (FAILED(hr))
  64. throw HRError("Failed to create 2D texture", hr);
  65. }
  66. hr = dev->CreateShaderResourceView(texture, &resourceDesc, &shaderRes);
  67. if (FAILED(hr))
  68. throw HRError("Failed to create resource view", hr);
  69. if (isRenderTarget)
  70. InitRenderTargets();
  71. if (isGDICompatible) {
  72. hr = texture->QueryInterface(__uuidof(IDXGISurface1),
  73. (void**)&gdiSurface);
  74. if (FAILED(hr))
  75. throw HRError("Failed to create GDI surface", hr);
  76. }
  77. }
  78. inline void gs_zstencil_buffer::Rebuild(ID3D11Device *dev)
  79. {
  80. HRESULT hr;
  81. hr = dev->CreateTexture2D(&td, nullptr, &texture);
  82. if (FAILED(hr))
  83. throw HRError("Failed to create depth stencil texture", hr);
  84. hr = dev->CreateDepthStencilView(texture, &dsvd, &view);
  85. if (FAILED(hr))
  86. throw HRError("Failed to create depth stencil view", hr);
  87. }
  88. inline void gs_stage_surface::Rebuild(ID3D11Device *dev)
  89. {
  90. HRESULT hr = dev->CreateTexture2D(&td, nullptr, &texture);
  91. if (FAILED(hr))
  92. throw HRError("Failed to create staging surface", hr);
  93. }
  94. inline void gs_sampler_state::Rebuild(ID3D11Device *dev)
  95. {
  96. HRESULT hr = dev->CreateSamplerState(&sd, state.Assign());
  97. if (FAILED(hr))
  98. throw HRError("Failed to create sampler state", hr);
  99. }
  100. inline void gs_vertex_shader::Rebuild(ID3D11Device *dev)
  101. {
  102. HRESULT hr;
  103. hr = dev->CreateVertexShader(data.data(), data.size(), nullptr, &shader);
  104. if (FAILED(hr))
  105. throw HRError("Failed to create vertex shader", hr);
  106. hr = dev->CreateInputLayout(layoutData.data(), (UINT)layoutData.size(),
  107. data.data(), data.size(), &layout);
  108. if (FAILED(hr))
  109. throw HRError("Failed to create input layout", hr);
  110. if (constantSize) {
  111. hr = dev->CreateBuffer(&bd, NULL, &constants);
  112. if (FAILED(hr))
  113. throw HRError("Failed to create constant buffer", hr);
  114. }
  115. for (gs_shader_param &param : params) {
  116. param.nextSampler = nullptr;
  117. param.curValue.clear();
  118. gs_shader_set_default(&param);
  119. }
  120. }
  121. inline void gs_pixel_shader::Rebuild(ID3D11Device *dev)
  122. {
  123. HRESULT hr;
  124. hr = dev->CreatePixelShader(data.data(), data.size(), nullptr,
  125. &shader);
  126. if (FAILED(hr))
  127. throw HRError("Failed to create pixel shader", hr);
  128. if (constantSize) {
  129. hr = dev->CreateBuffer(&bd, NULL, &constants);
  130. if (FAILED(hr))
  131. throw HRError("Failed to create constant buffer", hr);
  132. }
  133. for (gs_shader_param &param : params) {
  134. param.nextSampler = nullptr;
  135. param.curValue.clear();
  136. gs_shader_set_default(&param);
  137. }
  138. }
  139. inline void gs_swap_chain::Rebuild(ID3D11Device *dev)
  140. {
  141. HRESULT hr = device->factory->CreateSwapChain(dev, &swapDesc, &swap);
  142. if (FAILED(hr))
  143. throw HRError("Failed to create swap chain", hr);
  144. Init();
  145. }
  146. inline void SavedBlendState::Rebuild(ID3D11Device *dev)
  147. {
  148. HRESULT hr = dev->CreateBlendState(&bd, &state);
  149. if (FAILED(hr))
  150. throw HRError("Failed to create blend state", hr);
  151. }
  152. inline void SavedZStencilState::Rebuild(ID3D11Device *dev)
  153. {
  154. HRESULT hr = dev->CreateDepthStencilState(&dsd, &state);
  155. if (FAILED(hr))
  156. throw HRError("Failed to create depth stencil state", hr);
  157. }
  158. inline void SavedRasterState::Rebuild(ID3D11Device *dev)
  159. {
  160. HRESULT hr = dev->CreateRasterizerState(&rd, &state);
  161. if (FAILED(hr))
  162. throw HRError("Failed to create rasterizer state", hr);
  163. }
  164. const static D3D_FEATURE_LEVEL featureLevels[] =
  165. {
  166. D3D_FEATURE_LEVEL_11_0,
  167. D3D_FEATURE_LEVEL_10_1,
  168. D3D_FEATURE_LEVEL_10_0,
  169. D3D_FEATURE_LEVEL_9_3,
  170. };
  171. void gs_device::RebuildDevice()
  172. try {
  173. ID3D11Device *dev = nullptr;
  174. HRESULT hr;
  175. blog(LOG_WARNING, "Device Remove/Reset! Rebuilding all assets...");
  176. /* ----------------------------------------------------------------- */
  177. gs_obj *obj = first_obj;
  178. while (obj) {
  179. switch (obj->obj_type) {
  180. case gs_type::gs_vertex_buffer:
  181. ((gs_vertex_buffer*)obj)->Release();
  182. break;
  183. case gs_type::gs_index_buffer:
  184. ((gs_index_buffer*)obj)->Release();
  185. break;
  186. case gs_type::gs_texture_2d:
  187. ((gs_texture_2d*)obj)->Release();
  188. break;
  189. case gs_type::gs_zstencil_buffer:
  190. ((gs_zstencil_buffer*)obj)->Release();
  191. break;
  192. case gs_type::gs_stage_surface:
  193. ((gs_stage_surface*)obj)->Release();
  194. break;
  195. case gs_type::gs_sampler_state:
  196. ((gs_sampler_state*)obj)->Release();
  197. break;
  198. case gs_type::gs_vertex_shader:
  199. ((gs_vertex_shader*)obj)->Release();
  200. break;
  201. case gs_type::gs_pixel_shader:
  202. ((gs_pixel_shader*)obj)->Release();
  203. break;
  204. case gs_type::gs_duplicator:
  205. ((gs_duplicator*)obj)->Release();
  206. break;
  207. case gs_type::gs_swap_chain:
  208. ((gs_swap_chain*)obj)->Release();
  209. break;
  210. }
  211. obj = obj->next;
  212. }
  213. for (auto &state : zstencilStates)
  214. state.Release();
  215. for (auto &state : rasterStates)
  216. state.Release();
  217. for (auto &state : blendStates)
  218. state.Release();
  219. context->ClearState();
  220. context.Release();
  221. device.Release();
  222. adapter.Release();
  223. factory.Release();
  224. /* ----------------------------------------------------------------- */
  225. InitFactory(adpIdx);
  226. uint32_t createFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  227. hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN,
  228. nullptr, createFlags, featureLevels,
  229. sizeof(featureLevels) / sizeof(D3D_FEATURE_LEVEL),
  230. D3D11_SDK_VERSION, &device, nullptr, &context);
  231. if (FAILED(hr))
  232. throw HRError("Failed to create device", hr);
  233. dev = device;
  234. obj = first_obj;
  235. while (obj) {
  236. switch (obj->obj_type) {
  237. case gs_type::gs_vertex_buffer:
  238. ((gs_vertex_buffer*)obj)->Rebuild();
  239. break;
  240. case gs_type::gs_index_buffer:
  241. ((gs_index_buffer*)obj)->Rebuild(dev);
  242. break;
  243. case gs_type::gs_texture_2d:
  244. ((gs_texture_2d*)obj)->Rebuild(dev);
  245. break;
  246. case gs_type::gs_zstencil_buffer:
  247. ((gs_zstencil_buffer*)obj)->Rebuild(dev);
  248. break;
  249. case gs_type::gs_stage_surface:
  250. ((gs_stage_surface*)obj)->Rebuild(dev);
  251. break;
  252. case gs_type::gs_sampler_state:
  253. ((gs_sampler_state*)obj)->Rebuild(dev);
  254. break;
  255. case gs_type::gs_vertex_shader:
  256. ((gs_vertex_shader*)obj)->Rebuild(dev);
  257. break;
  258. case gs_type::gs_pixel_shader:
  259. ((gs_pixel_shader*)obj)->Rebuild(dev);
  260. break;
  261. case gs_type::gs_duplicator:
  262. try {
  263. ((gs_duplicator*)obj)->Start();
  264. } catch (...) {
  265. ((gs_duplicator*)obj)->Release();
  266. }
  267. break;
  268. case gs_type::gs_swap_chain:
  269. ((gs_swap_chain*)obj)->Rebuild(dev);
  270. break;
  271. }
  272. obj = obj->next;
  273. }
  274. curRenderTarget = nullptr;
  275. curZStencilBuffer = nullptr;
  276. curRenderSide = 0;
  277. memset(&curTextures, 0, sizeof(curTextures));
  278. memset(&curSamplers, 0, sizeof(curSamplers));
  279. curVertexBuffer = nullptr;
  280. curIndexBuffer = nullptr;
  281. curVertexShader = nullptr;
  282. curPixelShader = nullptr;
  283. curSwapChain = nullptr;
  284. zstencilStateChanged = true;
  285. rasterStateChanged = true;
  286. blendStateChanged = true;
  287. curDepthStencilState = nullptr;
  288. curRasterState = nullptr;
  289. curBlendState = nullptr;
  290. curToplogy = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
  291. for (auto &state : zstencilStates)
  292. state.Rebuild(dev);
  293. for (auto &state : rasterStates)
  294. state.Rebuild(dev);
  295. for (auto &state : blendStates)
  296. state.Rebuild(dev);
  297. } catch (const char *error) {
  298. bcrash("Failed to recreate D3D11: %s", error);
  299. } catch (HRError error) {
  300. bcrash("Failed to recreate D3D11: %s (%08lX)",
  301. error.str, error.hr);
  302. }