DX11Texture.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "DX11Texture.h"
  2. #include "Image.h"
  3. #ifdef WIN32
  4. # include <d3d11.h>
  5. #endif
  6. using namespace Framework;
  7. DX11Texture::DX11Texture(ID3D11Device* device,
  8. ID3D11DeviceContext* context,
  9. Critical& deviceLock,
  10. TextureDirection dir)
  11. : Texture(dir),
  12. txt(0),
  13. view(0),
  14. device(device),
  15. context(context),
  16. useMips(dir == RAM_TO_GPU),
  17. deviceLock(deviceLock),
  18. readAccessTexture(0)
  19. {}
  20. DX11Texture::~DX11Texture()
  21. {
  22. #ifdef WIN32
  23. if (txt) txt->Release();
  24. if (view) view->Release();
  25. if (readAccessTexture) readAccessTexture->Release();
  26. #endif
  27. }
  28. // Updates the texture. The pixels of the current image are copied into
  29. // the graphics memory
  30. bool DX11Texture::updateTextur()
  31. {
  32. if (!bild) return 0;
  33. #ifdef WIN32
  34. if (!txt || lastGr != bild->getSize())
  35. {
  36. if (txt) txt->Release();
  37. txt = 0;
  38. D3D11_TEXTURE2D_DESC bufferDesc;
  39. memset(&bufferDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
  40. bufferDesc.ArraySize = 1;
  41. bufferDesc.Width = bild->getWidth();
  42. bufferDesc.Height = bild->getHeight();
  43. bufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  44. bufferDesc.BindFlags = (getDirection() == GPU_TO_RAM || useMips
  45. ? D3D11_BIND_RENDER_TARGET
  46. : 0)
  47. | D3D11_BIND_SHADER_RESOURCE;
  48. bufferDesc.CPUAccessFlags = getDirection() == GPU_TO_RAM || useMips
  49. ? 0
  50. : D3D11_CPU_ACCESS_WRITE;
  51. bufferDesc.SampleDesc.Count = 1;
  52. bufferDesc.MipLevels = useMips ? 0 : 1;
  53. bufferDesc.Usage = getDirection() == GPU_TO_RAM || useMips
  54. ? D3D11_USAGE_DEFAULT
  55. : D3D11_USAGE_DYNAMIC;
  56. bufferDesc.MiscFlags = useMips ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0;
  57. deviceLock.lock();
  58. HRESULT r = device->CreateTexture2D(&bufferDesc, 0, &txt);
  59. deviceLock.unlock();
  60. if (r != S_OK) return 0;
  61. }
  62. if (getDirection() == RAM_TO_GPU && (bild->getNeedRender() || changed))
  63. {
  64. changed = 0;
  65. if (useMips)
  66. {
  67. deviceLock.lock();
  68. context->UpdateSubresource(txt,
  69. 0,
  70. 0,
  71. bild->getBuffer(),
  72. 4 * bild->getWidth(),
  73. 4 * bild->getWidth() * bild->getHeight());
  74. deviceLock.unlock();
  75. }
  76. else
  77. {
  78. D3D11_MAPPED_SUBRESOURCE buffer;
  79. deviceLock.lock();
  80. context->Map(
  81. txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer);
  82. int* bgBuff = bild->getBuffer();
  83. int tmpBr = 4 * bild->getWidth();
  84. for (int y = 0, pitch = 0, bry = 0; y < bild->getHeight();
  85. ++y, pitch += buffer.RowPitch, bry += bild->getWidth())
  86. {
  87. memcpy(&((BYTE*)buffer.pData)[pitch],
  88. (void*)&(bgBuff[bry]),
  89. tmpBr);
  90. }
  91. context->Unmap(txt, 0);
  92. deviceLock.unlock();
  93. }
  94. }
  95. else if (getDirection() == GPU_TO_RAM)
  96. {
  97. if (!readAccessTexture || lastGr != bild->getSize())
  98. {
  99. if (readAccessTexture) readAccessTexture->Release();
  100. readAccessTexture = 0;
  101. D3D11_TEXTURE2D_DESC tempBufferDesc;
  102. memset(&tempBufferDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
  103. tempBufferDesc.ArraySize = 1;
  104. tempBufferDesc.Width = bild->getWidth();
  105. tempBufferDesc.Height = bild->getHeight();
  106. tempBufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  107. tempBufferDesc.BindFlags = 0;
  108. tempBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  109. tempBufferDesc.SampleDesc.Count = 1;
  110. tempBufferDesc.MipLevels = 1;
  111. tempBufferDesc.Usage = D3D11_USAGE_STAGING;
  112. deviceLock.lock();
  113. HRESULT r = device->CreateTexture2D(
  114. &tempBufferDesc, 0, &readAccessTexture);
  115. deviceLock.unlock();
  116. if (r != S_OK) return 0;
  117. }
  118. else
  119. {
  120. deviceLock.lock();
  121. context->CopyResource(readAccessTexture, txt);
  122. deviceLock.unlock();
  123. D3D11_MAPPED_SUBRESOURCE buffer;
  124. deviceLock.lock();
  125. HRESULT r = context->Map(
  126. readAccessTexture, 0, D3D11_MAP::D3D11_MAP_READ, 0, &buffer);
  127. deviceLock.unlock();
  128. if (r != S_OK) return 0;
  129. int* bgBuff = bild->getBuffer();
  130. int tmpBr = 4 * bild->getWidth();
  131. for (int y = 0, pitch = 0, bry = 0; y < bild->getHeight();
  132. ++y, pitch += buffer.RowPitch, bry += bild->getWidth())
  133. {
  134. memcpy((void*)&(bgBuff[bry]),
  135. &((BYTE*)buffer.pData)[pitch],
  136. tmpBr);
  137. }
  138. for (int i = 0; i < bild->getWidth() * bild->getHeight(); i++)
  139. {
  140. if (bgBuff[i]) bgBuff[i] |= 0xFF000000;
  141. }
  142. deviceLock.lock();
  143. context->Unmap(readAccessTexture, 0);
  144. deviceLock.unlock();
  145. }
  146. }
  147. if (!view || lastGr != bild->getSize())
  148. {
  149. if (view) view->Release();
  150. view = 0;
  151. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesk;
  152. memset(&resourceDesk, 0, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
  153. resourceDesk.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  154. resourceDesk.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  155. resourceDesk.Texture2D.MipLevels = useMips ? -1 : 1;
  156. deviceLock.lock();
  157. HRESULT r = device->CreateShaderResourceView(txt, &resourceDesk, &view);
  158. deviceLock.unlock();
  159. if (r != S_OK) return 0;
  160. if (context && useMips)
  161. {
  162. deviceLock.lock();
  163. context->GenerateMips(view);
  164. deviceLock.unlock();
  165. }
  166. }
  167. lastGr = bild->getSize();
  168. #endif
  169. return 1;
  170. }
  171. // Returns true if updateTextur needs to be called
  172. bool DX11Texture::needsUpdate() const
  173. {
  174. return !view;
  175. }
  176. // Returns the used shader resource view
  177. DX11Texture::operator ID3D11ShaderResourceView*() const
  178. {
  179. return view;
  180. }
  181. //! Returns the used texture
  182. DX11Texture::operator ID3D11Texture2D*() const
  183. {
  184. return txt;
  185. }
  186. //! specifies if a mip map should be generated
  187. void DX11Texture::setUseMips(bool useMips)
  188. {
  189. this->useMips = useMips;
  190. }