d3d11-texture2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 <util/base.h>
  15. #include "d3d11-subsystem.hpp"
  16. void gs_texture_2d::InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd)
  17. {
  18. uint32_t rowSizeBytes = width * gs_get_format_bpp(format);
  19. uint32_t texSizeBytes = height * rowSizeBytes / 8;
  20. size_t textures = type == GS_TEXTURE_2D ? 1 : 6;
  21. uint32_t actual_levels = levels;
  22. size_t curTex = 0;
  23. if (!actual_levels)
  24. actual_levels = gs_get_total_levels(width, height, 1);
  25. rowSizeBytes /= 8;
  26. for (size_t i = 0; i < textures; i++) {
  27. uint32_t newRowSize = rowSizeBytes;
  28. uint32_t newTexSize = texSizeBytes;
  29. for (uint32_t j = 0; j < actual_levels; j++) {
  30. D3D11_SUBRESOURCE_DATA newSRD;
  31. newSRD.pSysMem = data[curTex++].data();
  32. newSRD.SysMemPitch = newRowSize;
  33. newSRD.SysMemSlicePitch = newTexSize;
  34. srd.push_back(newSRD);
  35. newRowSize /= 2;
  36. newTexSize /= 4;
  37. }
  38. }
  39. }
  40. void gs_texture_2d::BackupTexture(const uint8_t *const *data)
  41. {
  42. uint32_t textures = type == GS_TEXTURE_CUBE ? 6 : 1;
  43. uint32_t bbp = gs_get_format_bpp(format);
  44. this->data.resize(levels * textures);
  45. for (uint32_t t = 0; t < textures; t++) {
  46. uint32_t w = width;
  47. uint32_t h = height;
  48. for (uint32_t lv = 0; lv < levels; lv++) {
  49. uint32_t i = levels * t + lv;
  50. if (!data[i])
  51. break;
  52. uint32_t texSize = bbp * w * h / 8;
  53. vector<uint8_t> &subData = this->data[i];
  54. subData.resize(texSize);
  55. memcpy(&subData[0], data[i], texSize);
  56. if (w > 1)
  57. w /= 2;
  58. if (h > 1)
  59. h /= 2;
  60. }
  61. }
  62. }
  63. void gs_texture_2d::GetSharedHandle(IDXGIResource *dxgi_res)
  64. {
  65. HANDLE handle;
  66. HRESULT hr;
  67. hr = dxgi_res->GetSharedHandle(&handle);
  68. if (FAILED(hr)) {
  69. blog(LOG_WARNING,
  70. "GetSharedHandle: Failed to "
  71. "get shared handle: %08lX",
  72. hr);
  73. } else {
  74. sharedHandle = (uint32_t)(uintptr_t)handle;
  75. }
  76. }
  77. void gs_texture_2d::InitTexture(const uint8_t *const *data)
  78. {
  79. HRESULT hr;
  80. memset(&td, 0, sizeof(td));
  81. td.Width = width;
  82. td.Height = height;
  83. td.MipLevels = genMipmaps ? 0 : levels;
  84. td.ArraySize = type == GS_TEXTURE_CUBE ? 6 : 1;
  85. td.Format = twoPlane ? ((format == GS_R16) ? DXGI_FORMAT_P010 : DXGI_FORMAT_NV12) : dxgiFormatResource;
  86. td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  87. td.SampleDesc.Count = 1;
  88. td.CPUAccessFlags = isDynamic ? D3D11_CPU_ACCESS_WRITE : 0;
  89. td.Usage = isDynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  90. if (type == GS_TEXTURE_CUBE)
  91. td.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
  92. if (isRenderTarget || isGDICompatible)
  93. td.BindFlags |= D3D11_BIND_RENDER_TARGET;
  94. if (isGDICompatible)
  95. td.MiscFlags |= D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
  96. if ((flags & GS_SHARED_KM_TEX) != 0)
  97. td.MiscFlags |= D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
  98. else if ((flags & GS_SHARED_TEX) != 0)
  99. td.MiscFlags |= D3D11_RESOURCE_MISC_SHARED;
  100. if (data) {
  101. BackupTexture(data);
  102. InitSRD(srd);
  103. }
  104. hr = device->device->CreateTexture2D(&td, data ? srd.data() : NULL, texture.Assign());
  105. if (FAILED(hr))
  106. throw HRError("Failed to create 2D texture", hr);
  107. if (isGDICompatible) {
  108. hr = texture->QueryInterface(__uuidof(IDXGISurface1), (void **)gdiSurface.Assign());
  109. if (FAILED(hr))
  110. throw HRError("Failed to create GDI surface", hr);
  111. }
  112. if (isShared) {
  113. ComPtr<IDXGIResource> dxgi_res;
  114. texture->SetEvictionPriority(DXGI_RESOURCE_PRIORITY_MAXIMUM);
  115. hr = texture->QueryInterface(__uuidof(IDXGIResource), (void **)&dxgi_res);
  116. if (FAILED(hr)) {
  117. blog(LOG_WARNING,
  118. "InitTexture: Failed to query "
  119. "interface: %08lX",
  120. hr);
  121. } else {
  122. GetSharedHandle(dxgi_res);
  123. if (flags & GS_SHARED_KM_TEX) {
  124. ComPtr<IDXGIKeyedMutex> km;
  125. hr = texture->QueryInterface(__uuidof(IDXGIKeyedMutex), (void **)&km);
  126. if (FAILED(hr)) {
  127. throw HRError("Failed to query "
  128. "IDXGIKeyedMutex",
  129. hr);
  130. }
  131. km->AcquireSync(0, INFINITE);
  132. acquired = true;
  133. }
  134. }
  135. }
  136. }
  137. void gs_texture_2d::InitResourceView()
  138. {
  139. HRESULT hr;
  140. memset(&viewDesc, 0, sizeof(viewDesc));
  141. viewDesc.Format = dxgiFormatView;
  142. if (type == GS_TEXTURE_CUBE) {
  143. viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  144. viewDesc.TextureCube.MipLevels = genMipmaps || !levels ? -1 : levels;
  145. } else {
  146. viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  147. viewDesc.Texture2D.MipLevels = genMipmaps || !levels ? -1 : levels;
  148. }
  149. hr = device->device->CreateShaderResourceView(texture, &viewDesc, shaderRes.Assign());
  150. if (FAILED(hr))
  151. throw HRError("Failed to create SRV", hr);
  152. viewDescLinear = viewDesc;
  153. viewDescLinear.Format = dxgiFormatViewLinear;
  154. if (dxgiFormatView == dxgiFormatViewLinear) {
  155. shaderResLinear = shaderRes;
  156. } else {
  157. hr = device->device->CreateShaderResourceView(texture, &viewDescLinear, shaderResLinear.Assign());
  158. if (FAILED(hr))
  159. throw HRError("Failed to create linear SRV", hr);
  160. }
  161. }
  162. void gs_texture_2d::InitRenderTargets()
  163. {
  164. HRESULT hr;
  165. if (type == GS_TEXTURE_2D) {
  166. D3D11_RENDER_TARGET_VIEW_DESC rtv;
  167. rtv.Format = dxgiFormatView;
  168. rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  169. rtv.Texture2D.MipSlice = 0;
  170. hr = device->device->CreateRenderTargetView(texture, &rtv, renderTarget[0].Assign());
  171. if (FAILED(hr))
  172. throw HRError("Failed to create RTV", hr);
  173. if (dxgiFormatView == dxgiFormatViewLinear) {
  174. renderTargetLinear[0] = renderTarget[0];
  175. } else {
  176. rtv.Format = dxgiFormatViewLinear;
  177. hr = device->device->CreateRenderTargetView(texture, &rtv, renderTargetLinear[0].Assign());
  178. if (FAILED(hr))
  179. throw HRError("Failed to create linear RTV", hr);
  180. }
  181. } else {
  182. D3D11_RENDER_TARGET_VIEW_DESC rtv;
  183. rtv.Format = dxgiFormatView;
  184. rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
  185. rtv.Texture2DArray.MipSlice = 0;
  186. rtv.Texture2DArray.ArraySize = 1;
  187. for (UINT i = 0; i < 6; i++) {
  188. rtv.Texture2DArray.FirstArraySlice = i;
  189. hr = device->device->CreateRenderTargetView(texture, &rtv, renderTarget[i].Assign());
  190. if (FAILED(hr))
  191. throw HRError("Failed to create cube RTV", hr);
  192. if (dxgiFormatView == dxgiFormatViewLinear) {
  193. renderTargetLinear[i] = renderTarget[i];
  194. } else {
  195. rtv.Format = dxgiFormatViewLinear;
  196. hr = device->device->CreateRenderTargetView(texture, &rtv,
  197. renderTargetLinear[i].Assign());
  198. if (FAILED(hr))
  199. throw HRError("Failed to create linear cube RTV", hr);
  200. }
  201. }
  202. }
  203. }
  204. #define SHARED_FLAGS (GS_SHARED_TEX | GS_SHARED_KM_TEX)
  205. gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t width, uint32_t height, gs_color_format colorFormat,
  206. uint32_t levels, const uint8_t *const *data, uint32_t flags_, gs_texture_type type,
  207. bool gdiCompatible, bool twoPlane_)
  208. : gs_texture(device, gs_type::gs_texture_2d, type, levels, colorFormat),
  209. width(width),
  210. height(height),
  211. flags(flags_),
  212. dxgiFormatResource(ConvertGSTextureFormatResource(format)),
  213. dxgiFormatView(ConvertGSTextureFormatView(format)),
  214. dxgiFormatViewLinear(ConvertGSTextureFormatViewLinear(format)),
  215. isRenderTarget((flags_ & GS_RENDER_TARGET) != 0),
  216. isGDICompatible(gdiCompatible),
  217. isDynamic((flags_ & GS_DYNAMIC) != 0),
  218. isShared((flags_ & SHARED_FLAGS) != 0),
  219. genMipmaps((flags_ & GS_BUILD_MIPMAPS) != 0),
  220. sharedHandle(GS_INVALID_HANDLE),
  221. twoPlane(twoPlane_)
  222. {
  223. InitTexture(data);
  224. InitResourceView();
  225. if (isRenderTarget)
  226. InitRenderTargets();
  227. }
  228. gs_texture_2d::gs_texture_2d(gs_device_t *device, ID3D11Texture2D *nv12tex, uint32_t flags_)
  229. : gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D),
  230. isRenderTarget((flags_ & GS_RENDER_TARGET) != 0),
  231. isDynamic((flags_ & GS_DYNAMIC) != 0),
  232. isShared((flags_ & SHARED_FLAGS) != 0),
  233. genMipmaps((flags_ & GS_BUILD_MIPMAPS) != 0),
  234. twoPlane(true),
  235. texture(nv12tex)
  236. {
  237. texture->GetDesc(&td);
  238. const bool p010 = td.Format == DXGI_FORMAT_P010;
  239. const DXGI_FORMAT dxgi_format = p010 ? DXGI_FORMAT_R16G16_UNORM : DXGI_FORMAT_R8G8_UNORM;
  240. this->type = GS_TEXTURE_2D;
  241. this->format = p010 ? GS_RG16 : GS_R8G8;
  242. this->flags = flags_;
  243. this->levels = 1;
  244. this->device = device;
  245. this->chroma = true;
  246. this->width = td.Width / 2;
  247. this->height = td.Height / 2;
  248. this->dxgiFormatResource = dxgi_format;
  249. this->dxgiFormatView = dxgi_format;
  250. this->dxgiFormatViewLinear = dxgi_format;
  251. InitResourceView();
  252. if (isRenderTarget)
  253. InitRenderTargets();
  254. }
  255. gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t handle, bool ntHandle)
  256. : gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D),
  257. isShared(true),
  258. sharedHandle(handle)
  259. {
  260. HRESULT hr;
  261. if (ntHandle) {
  262. ComQIPtr<ID3D11Device1> dev = device->device;
  263. hr = dev->OpenSharedResource1((HANDLE)(uintptr_t)handle, __uuidof(ID3D11Texture2D),
  264. (void **)texture.Assign());
  265. } else {
  266. hr = device->device->OpenSharedResource((HANDLE)(uintptr_t)handle, __uuidof(ID3D11Texture2D),
  267. (void **)texture.Assign());
  268. }
  269. if (FAILED(hr))
  270. throw HRError("Failed to open shared 2D texture", hr);
  271. texture->GetDesc(&td);
  272. const gs_color_format format = ConvertDXGITextureFormat(td.Format);
  273. this->type = GS_TEXTURE_2D;
  274. this->format = format;
  275. this->levels = 1;
  276. this->device = device;
  277. this->width = td.Width;
  278. this->height = td.Height;
  279. this->dxgiFormatResource = ConvertGSTextureFormatResource(format);
  280. this->dxgiFormatView = ConvertGSTextureFormatView(format);
  281. this->dxgiFormatViewLinear = ConvertGSTextureFormatViewLinear(format);
  282. InitResourceView();
  283. }
  284. gs_texture_2d::gs_texture_2d(gs_device_t *device, ID3D11Texture2D *obj)
  285. : gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D)
  286. {
  287. texture = obj;
  288. texture->GetDesc(&td);
  289. const gs_color_format format = ConvertDXGITextureFormat(td.Format);
  290. this->type = GS_TEXTURE_2D;
  291. this->format = format;
  292. this->levels = 1;
  293. this->device = device;
  294. this->width = td.Width;
  295. this->height = td.Height;
  296. this->dxgiFormatResource = ConvertGSTextureFormatResource(format);
  297. this->dxgiFormatView = ConvertGSTextureFormatView(format);
  298. this->dxgiFormatViewLinear = ConvertGSTextureFormatViewLinear(format);
  299. InitResourceView();
  300. }