GS_D3D11Texture2D.cpp 4.7 KB

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