d3d11-texture2d.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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);
  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 **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. w /= 2;
  54. h /= 2;
  55. }
  56. }
  57. void gs_texture_2d::GetSharedHandle(IDXGIResource *dxgi_res)
  58. {
  59. HANDLE handle;
  60. HRESULT hr;
  61. hr = dxgi_res->GetSharedHandle(&handle);
  62. if (FAILED(hr)) {
  63. blog(LOG_WARNING, "GetSharedHandle: Failed to "
  64. "get shared handle: %08lX", hr);
  65. } else {
  66. sharedHandle = (uint32_t)(uintptr_t)handle;
  67. }
  68. }
  69. void gs_texture_2d::InitTexture(const uint8_t **data)
  70. {
  71. HRESULT hr;
  72. memset(&td, 0, sizeof(td));
  73. td.Width = width;
  74. td.Height = height;
  75. td.MipLevels = genMipmaps ? 0 : levels;
  76. td.ArraySize = type == GS_TEXTURE_CUBE ? 6 : 1;
  77. td.Format = nv12 ? DXGI_FORMAT_NV12 : dxgiFormat;
  78. td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  79. td.SampleDesc.Count = 1;
  80. td.CPUAccessFlags = isDynamic ? D3D11_CPU_ACCESS_WRITE : 0;
  81. td.Usage = isDynamic ? D3D11_USAGE_DYNAMIC :
  82. D3D11_USAGE_DEFAULT;
  83. if (type == GS_TEXTURE_CUBE)
  84. td.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
  85. if (isRenderTarget || isGDICompatible)
  86. td.BindFlags |= D3D11_BIND_RENDER_TARGET;
  87. if (isGDICompatible)
  88. td.MiscFlags |= D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
  89. if ((flags & GS_SHARED_KM_TEX) != 0)
  90. td.MiscFlags |= D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
  91. else if ((flags & GS_SHARED_TEX) != 0)
  92. td.MiscFlags |= D3D11_RESOURCE_MISC_SHARED;
  93. if (data) {
  94. BackupTexture(data);
  95. InitSRD(srd);
  96. }
  97. hr = device->device->CreateTexture2D(&td, data ? srd.data() : NULL,
  98. texture.Assign());
  99. if (FAILED(hr))
  100. throw HRError("Failed to create 2D texture", hr);
  101. if (isGDICompatible) {
  102. hr = texture->QueryInterface(__uuidof(IDXGISurface1),
  103. (void**)gdiSurface.Assign());
  104. if (FAILED(hr))
  105. throw HRError("Failed to create GDI surface", hr);
  106. }
  107. if (isShared) {
  108. ComPtr<IDXGIResource> dxgi_res;
  109. texture->SetEvictionPriority(DXGI_RESOURCE_PRIORITY_MAXIMUM);
  110. hr = texture->QueryInterface(__uuidof(IDXGIResource),
  111. (void**)&dxgi_res);
  112. if (FAILED(hr)) {
  113. blog(LOG_WARNING, "InitTexture: Failed to query "
  114. "interface: %08lX", hr);
  115. } else {
  116. GetSharedHandle(dxgi_res);
  117. if (flags & GS_SHARED_KM_TEX) {
  118. ComPtr<IDXGIKeyedMutex> km;
  119. hr = texture->QueryInterface(
  120. __uuidof(IDXGIKeyedMutex),
  121. (void**)&km);
  122. if (FAILED(hr)) {
  123. throw HRError("Failed to query "
  124. "IDXGIKeyedMutex",
  125. hr);
  126. }
  127. km->AcquireSync(0, INFINITE);
  128. acquired = true;
  129. }
  130. }
  131. }
  132. }
  133. void gs_texture_2d::InitResourceView()
  134. {
  135. HRESULT hr;
  136. memset(&resourceDesc, 0, sizeof(resourceDesc));
  137. resourceDesc.Format = dxgiFormat;
  138. if (type == GS_TEXTURE_CUBE) {
  139. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  140. resourceDesc.TextureCube.MipLevels =
  141. genMipmaps || !levels ? -1 : levels;
  142. } else {
  143. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  144. resourceDesc.Texture2D.MipLevels =
  145. genMipmaps || !levels ? -1 : levels;
  146. }
  147. hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
  148. shaderRes.Assign());
  149. if (FAILED(hr))
  150. throw HRError("Failed to create resource view", hr);
  151. }
  152. void gs_texture_2d::InitRenderTargets()
  153. {
  154. HRESULT hr;
  155. if (type == GS_TEXTURE_2D) {
  156. D3D11_RENDER_TARGET_VIEW_DESC rtv;
  157. rtv.Format = dxgiFormat;
  158. rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  159. rtv.Texture2D.MipSlice = 0;
  160. hr = device->device->CreateRenderTargetView(texture, &rtv,
  161. renderTarget[0].Assign());
  162. if (FAILED(hr))
  163. throw HRError("Failed to create render target view",
  164. hr);
  165. } else {
  166. D3D11_RENDER_TARGET_VIEW_DESC rtv;
  167. rtv.Format = dxgiFormat;
  168. rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
  169. rtv.Texture2DArray.MipSlice = 0;
  170. rtv.Texture2DArray.ArraySize = 1;
  171. for (UINT i = 0; i < 6; i++) {
  172. rtv.Texture2DArray.FirstArraySlice = i;
  173. hr = device->device->CreateRenderTargetView(texture,
  174. &rtv, renderTarget[i].Assign());
  175. if (FAILED(hr))
  176. throw HRError("Failed to create cube render "
  177. "target view", hr);
  178. }
  179. }
  180. }
  181. #define SHARED_FLAGS (GS_SHARED_TEX | GS_SHARED_KM_TEX)
  182. gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t width,
  183. uint32_t height, gs_color_format colorFormat, uint32_t levels,
  184. const uint8_t **data, uint32_t flags_, gs_texture_type type,
  185. bool gdiCompatible, bool nv12_)
  186. : gs_texture (device, gs_type::gs_texture_2d, type, levels,
  187. colorFormat),
  188. width (width),
  189. height (height),
  190. flags (flags_),
  191. dxgiFormat (ConvertGSTextureFormat(format)),
  192. isRenderTarget ((flags_ & GS_RENDER_TARGET) != 0),
  193. isGDICompatible (gdiCompatible),
  194. isDynamic ((flags_ & GS_DYNAMIC) != 0),
  195. isShared ((flags_ & SHARED_FLAGS) != 0),
  196. genMipmaps ((flags_ & GS_BUILD_MIPMAPS) != 0),
  197. sharedHandle (GS_INVALID_HANDLE),
  198. nv12 (nv12_)
  199. {
  200. InitTexture(data);
  201. InitResourceView();
  202. if (isRenderTarget)
  203. InitRenderTargets();
  204. }
  205. gs_texture_2d::gs_texture_2d(gs_device_t *device, ID3D11Texture2D *nv12tex,
  206. uint32_t flags_)
  207. : gs_texture (device, gs_type::gs_texture_2d, GS_TEXTURE_2D),
  208. isRenderTarget ((flags_ & GS_RENDER_TARGET) != 0),
  209. isDynamic ((flags_ & GS_DYNAMIC) != 0),
  210. isShared ((flags_ & SHARED_FLAGS) != 0),
  211. genMipmaps ((flags_ & GS_BUILD_MIPMAPS) != 0),
  212. nv12 (true)
  213. {
  214. texture = nv12tex;
  215. texture->GetDesc(&td);
  216. this->type = GS_TEXTURE_2D;
  217. this->format = GS_R8G8;
  218. this->flags = flags_;
  219. this->levels = 1;
  220. this->device = device;
  221. this->chroma = true;
  222. this->width = td.Width / 2;
  223. this->height = td.Height / 2;
  224. this->dxgiFormat = DXGI_FORMAT_R8G8_UNORM;
  225. InitResourceView();
  226. if (isRenderTarget)
  227. InitRenderTargets();
  228. }
  229. gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t handle)
  230. : gs_texture (device, gs_type::gs_texture_2d,
  231. GS_TEXTURE_2D),
  232. isShared (true),
  233. sharedHandle (handle)
  234. {
  235. HRESULT hr;
  236. hr = device->device->OpenSharedResource((HANDLE)(uintptr_t)handle,
  237. __uuidof(ID3D11Texture2D), (void**)texture.Assign());
  238. if (FAILED(hr))
  239. throw HRError("Failed to open shared 2D texture", hr);
  240. texture->GetDesc(&td);
  241. this->type = GS_TEXTURE_2D;
  242. this->format = ConvertDXGITextureFormat(td.Format);
  243. this->levels = 1;
  244. this->device = device;
  245. this->width = td.Width;
  246. this->height = td.Height;
  247. this->dxgiFormat = td.Format;
  248. memset(&resourceDesc, 0, sizeof(resourceDesc));
  249. resourceDesc.Format = td.Format;
  250. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  251. resourceDesc.Texture2D.MipLevels = 1;
  252. hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
  253. shaderRes.Assign());
  254. if (FAILED(hr))
  255. throw HRError("Failed to create shader resource view", hr);
  256. }