d3d11-texture2d.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. const uint8_t **data)
  18. {
  19. uint32_t rowSizeBytes = width * gs_get_format_bpp(format);
  20. uint32_t texSizeBytes = height * rowSizeBytes / 8;
  21. size_t textures = type == GS_TEXTURE_2D ? 1 : 6;
  22. uint32_t actual_levels = levels;
  23. if (!actual_levels)
  24. actual_levels = gs_num_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;
  32. newSRD.SysMemPitch = newRowSize;
  33. newSRD.SysMemSlicePitch = newTexSize;
  34. srd.push_back(newSRD);
  35. newRowSize /= 2;
  36. newTexSize /= 4;
  37. data++;
  38. }
  39. }
  40. }
  41. void gs_texture_2d::InitTexture(const uint8_t **data)
  42. {
  43. vector<D3D11_SUBRESOURCE_DATA> srd;
  44. D3D11_TEXTURE2D_DESC td;
  45. HRESULT hr;
  46. memset(&td, 0, sizeof(td));
  47. td.Width = width;
  48. td.Height = height;
  49. td.MipLevels = genMipmaps ? 0 : levels;
  50. td.ArraySize = type == GS_TEXTURE_CUBE ? 6 : 1;
  51. td.Format = dxgiFormat;
  52. td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  53. td.SampleDesc.Count = 1;
  54. td.CPUAccessFlags = isDynamic ? D3D11_CPU_ACCESS_WRITE : 0;
  55. td.Usage = isDynamic ? D3D11_USAGE_DYNAMIC :
  56. D3D11_USAGE_DEFAULT;
  57. if (type == GS_TEXTURE_CUBE)
  58. td.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
  59. if (isRenderTarget || isGDICompatible)
  60. td.BindFlags |= D3D11_BIND_RENDER_TARGET;
  61. if (isGDICompatible)
  62. td.MiscFlags |= D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
  63. if (data)
  64. InitSRD(srd, data);
  65. hr = device->device->CreateTexture2D(&td, data ? srd.data() : NULL,
  66. texture.Assign());
  67. if (FAILED(hr))
  68. throw HRError("Failed to create 2D texture", hr);
  69. if (isGDICompatible) {
  70. hr = texture->QueryInterface(__uuidof(IDXGISurface1),
  71. (void**)gdiSurface.Assign());
  72. if (FAILED(hr))
  73. throw HRError("Failed to create GDI surface", hr);
  74. }
  75. }
  76. void gs_texture_2d::InitResourceView()
  77. {
  78. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesc;
  79. HRESULT hr;
  80. memset(&resourceDesc, 0, sizeof(resourceDesc));
  81. resourceDesc.Format = dxgiFormat;
  82. if (type == GS_TEXTURE_CUBE) {
  83. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  84. resourceDesc.TextureCube.MipLevels = genMipmaps ? -1 : 1;
  85. } else {
  86. resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  87. resourceDesc.Texture2D.MipLevels = genMipmaps ? -1 : 1;
  88. }
  89. hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
  90. shaderRes.Assign());
  91. if (FAILED(hr))
  92. throw HRError("Failed to create resource view", hr);
  93. }
  94. void gs_texture_2d::InitRenderTargets()
  95. {
  96. HRESULT hr;
  97. if (type == GS_TEXTURE_2D) {
  98. hr = device->device->CreateRenderTargetView(texture, NULL,
  99. renderTarget[0].Assign());
  100. if (FAILED(hr))
  101. throw HRError("Failed to create render target view",
  102. hr);
  103. } else {
  104. D3D11_RENDER_TARGET_VIEW_DESC rtv;
  105. rtv.Format = dxgiFormat;
  106. rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
  107. rtv.Texture2DArray.MipSlice = 0;
  108. rtv.Texture2DArray.ArraySize = 1;
  109. for (UINT i = 0; i < 6; i++) {
  110. rtv.Texture2DArray.FirstArraySlice = i;
  111. hr = device->device->CreateRenderTargetView(texture,
  112. &rtv, renderTarget[i].Assign());
  113. if (FAILED(hr))
  114. throw HRError("Failed to create cube render "
  115. "target view", hr);
  116. }
  117. }
  118. }
  119. gs_texture_2d::gs_texture_2d(device_t device, uint32_t width, uint32_t height,
  120. gs_color_format colorFormat, uint32_t levels,
  121. const uint8_t **data, uint32_t flags, gs_texture_type type,
  122. bool gdiCompatible, bool shared)
  123. : gs_texture (device, type, levels, colorFormat),
  124. width (width),
  125. height (height),
  126. dxgiFormat (ConvertGSTextureFormat(format)),
  127. isGDICompatible (gdiCompatible),
  128. isShared (shared),
  129. isDynamic ((flags & GS_DYNAMIC) != 0),
  130. isRenderTarget ((flags & GS_RENDERTARGET) != 0),
  131. genMipmaps ((flags & GS_BUILDMIPMAPS) != 0)
  132. {
  133. InitTexture(data);
  134. InitResourceView();
  135. if (isRenderTarget)
  136. InitRenderTargets();
  137. }