d3d11-texture2d.cpp 10 KB

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