d3d11-texture3d.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_3d::InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd)
  17. {
  18. uint32_t rowSizeBits = width * gs_get_format_bpp(format);
  19. uint32_t sliceSizeBytes = height * rowSizeBits / 8;
  20. uint32_t actual_levels = levels;
  21. if (!actual_levels)
  22. actual_levels = gs_get_total_levels(width, height, depth);
  23. uint32_t newRowSize = rowSizeBits / 8;
  24. uint32_t newSlizeSize = sliceSizeBytes;
  25. for (uint32_t level = 0; level < actual_levels; ++level) {
  26. D3D11_SUBRESOURCE_DATA newSRD;
  27. newSRD.pSysMem = data[level].data();
  28. newSRD.SysMemPitch = newRowSize;
  29. newSRD.SysMemSlicePitch = newSlizeSize;
  30. srd.push_back(newSRD);
  31. newRowSize /= 2;
  32. newSlizeSize /= 4;
  33. }
  34. }
  35. void gs_texture_3d::BackupTexture(const uint8_t *const *data)
  36. {
  37. this->data.resize(levels);
  38. uint32_t w = width;
  39. uint32_t h = height;
  40. uint32_t d = depth;
  41. const uint32_t bbp = gs_get_format_bpp(format);
  42. for (uint32_t i = 0; i < levels; i++) {
  43. if (!data[i])
  44. break;
  45. const uint32_t texSize = bbp * w * h * d / 8;
  46. this->data[i].resize(texSize);
  47. vector<uint8_t> &subData = this->data[i];
  48. memcpy(&subData[0], data[i], texSize);
  49. if (w > 1)
  50. w /= 2;
  51. if (h > 1)
  52. h /= 2;
  53. if (d > 1)
  54. d /= 2;
  55. }
  56. }
  57. void gs_texture_3d::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,
  64. "GetSharedHandle: Failed to "
  65. "get shared handle: %08lX",
  66. hr);
  67. } else {
  68. sharedHandle = (uint32_t)(uintptr_t)handle;
  69. }
  70. }
  71. void gs_texture_3d::InitTexture(const uint8_t *const *data)
  72. {
  73. HRESULT hr;
  74. memset(&td, 0, sizeof(td));
  75. td.Width = width;
  76. td.Height = height;
  77. td.Depth = depth;
  78. td.MipLevels = genMipmaps ? 0 : levels;
  79. td.Format = dxgiFormat;
  80. td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  81. td.CPUAccessFlags = isDynamic ? D3D11_CPU_ACCESS_WRITE : 0;
  82. td.Usage = isDynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  83. if (type == GS_TEXTURE_CUBE)
  84. td.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
  85. if (isRenderTarget)
  86. td.BindFlags |= D3D11_BIND_RENDER_TARGET;
  87. if ((flags & GS_SHARED_KM_TEX) != 0)
  88. td.MiscFlags |= D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
  89. else if ((flags & GS_SHARED_TEX) != 0)
  90. td.MiscFlags |= D3D11_RESOURCE_MISC_SHARED;
  91. if (data) {
  92. BackupTexture(data);
  93. InitSRD(srd);
  94. }
  95. hr = device->device->CreateTexture3D(&td, data ? srd.data() : NULL,
  96. texture.Assign());
  97. if (FAILED(hr))
  98. throw HRError("Failed to create 3D texture", hr);
  99. if (isShared) {
  100. ComPtr<IDXGIResource> dxgi_res;
  101. texture->SetEvictionPriority(DXGI_RESOURCE_PRIORITY_MAXIMUM);
  102. hr = texture->QueryInterface(__uuidof(IDXGIResource),
  103. (void **)&dxgi_res);
  104. if (FAILED(hr)) {
  105. blog(LOG_WARNING,
  106. "InitTexture: Failed to query "
  107. "interface: %08lX",
  108. hr);
  109. } else {
  110. GetSharedHandle(dxgi_res);
  111. if (flags & GS_SHARED_KM_TEX) {
  112. ComPtr<IDXGIKeyedMutex> km;
  113. hr = texture->QueryInterface(
  114. __uuidof(IDXGIKeyedMutex),
  115. (void **)&km);
  116. if (FAILED(hr)) {
  117. throw HRError("Failed to query "
  118. "IDXGIKeyedMutex",
  119. hr);
  120. }
  121. km->AcquireSync(0, INFINITE);
  122. acquired = true;
  123. }
  124. }
  125. }
  126. }
  127. void gs_texture_3d::InitResourceView()
  128. {
  129. HRESULT hr;
  130. memset(&resourceDesc, 0, sizeof(resourceDesc));
  131. resourceDesc.Format = dxgiFormat;
  132. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  133. resourceDesc.Texture3D.MostDetailedMip = 0;
  134. resourceDesc.Texture3D.MipLevels = genMipmaps || !levels ? -1 : levels;
  135. hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
  136. shaderRes.Assign());
  137. if (FAILED(hr))
  138. throw HRError("Failed to create resource view", hr);
  139. }
  140. void gs_texture_3d::InitRenderTargets()
  141. {
  142. D3D11_RENDER_TARGET_VIEW_DESC rtv;
  143. rtv.Format = dxgiFormat;
  144. rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D;
  145. rtv.Texture3D.MipSlice = 0;
  146. rtv.Texture3D.WSize = 1;
  147. for (UINT i = 0; i < 6; i++) {
  148. rtv.Texture3D.FirstWSlice = i;
  149. const HRESULT hr = device->device->CreateRenderTargetView(
  150. texture, &rtv, renderTarget[i].Assign());
  151. if (FAILED(hr))
  152. throw HRError("Failed to create volume render "
  153. "target view",
  154. hr);
  155. }
  156. }
  157. #define SHARED_FLAGS (GS_SHARED_TEX | GS_SHARED_KM_TEX)
  158. gs_texture_3d::gs_texture_3d(gs_device_t *device, uint32_t width,
  159. uint32_t height, uint32_t depth,
  160. gs_color_format colorFormat, uint32_t levels,
  161. const uint8_t *const *data, uint32_t flags_)
  162. : gs_texture(device, gs_type::gs_texture_3d, GS_TEXTURE_3D, levels,
  163. colorFormat),
  164. width(width),
  165. height(height),
  166. depth(depth),
  167. flags(flags_),
  168. dxgiFormat(ConvertGSTextureFormat(format)),
  169. isRenderTarget((flags_ & GS_RENDER_TARGET) != 0),
  170. isDynamic((flags_ & GS_DYNAMIC) != 0),
  171. isShared((flags_ & SHARED_FLAGS) != 0),
  172. genMipmaps((flags_ & GS_BUILD_MIPMAPS) != 0),
  173. sharedHandle(GS_INVALID_HANDLE)
  174. {
  175. InitTexture(data);
  176. InitResourceView();
  177. if (isRenderTarget)
  178. InitRenderTargets();
  179. }
  180. gs_texture_3d::gs_texture_3d(gs_device_t *device, uint32_t handle)
  181. : gs_texture(device, gs_type::gs_texture_3d, GS_TEXTURE_3D),
  182. isShared(true),
  183. sharedHandle(handle)
  184. {
  185. HRESULT hr;
  186. hr = device->device->OpenSharedResource((HANDLE)(uintptr_t)handle,
  187. IID_PPV_ARGS(texture.Assign()));
  188. if (FAILED(hr))
  189. throw HRError("Failed to open shared 3D texture", hr);
  190. texture->GetDesc(&td);
  191. this->type = GS_TEXTURE_3D;
  192. this->format = ConvertDXGITextureFormat(td.Format);
  193. this->levels = 1;
  194. this->device = device;
  195. this->width = td.Width;
  196. this->height = td.Height;
  197. this->depth = td.Depth;
  198. this->dxgiFormat = td.Format;
  199. memset(&resourceDesc, 0, sizeof(resourceDesc));
  200. resourceDesc.Format = td.Format;
  201. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  202. resourceDesc.Texture3D.MostDetailedMip = 0;
  203. resourceDesc.Texture3D.MipLevels = 1;
  204. hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
  205. shaderRes.Assign());
  206. if (FAILED(hr))
  207. throw HRError("Failed to create shader resource view", hr);
  208. }