Texture.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "Texture.h"
  2. #include "Image.h"
  3. #ifdef WIN32
  4. # include <d3d11.h>
  5. # include <d3d12.h>
  6. # include "d3dx12.h"
  7. #endif
  8. using namespace Framework;
  9. // Contents of the Texture class
  10. // Constructor
  11. Texture::Texture()
  12. : ReferenceCounter()
  13. {
  14. bild = 0;
  15. lastGr = Point(0, 0);
  16. id = -1;
  17. changed = 0;
  18. }
  19. // Destructor
  20. Texture::~Texture()
  21. {
  22. if (bild) bild->release();
  23. }
  24. // Sets a pointer to the image that contains the texture
  25. // b: The pointer to the image
  26. void Texture::setImageZ(Image* b)
  27. {
  28. if (bild != b) changed = 1;
  29. if (bild) bild->release();
  30. bild = b;
  31. }
  32. // Sets the image that contains the texture by copying it
  33. // b: The image to be copied
  34. void Texture::setImage(Image* b)
  35. {
  36. if (!b) return;
  37. if (bild != b) changed = 1;
  38. if (!bild || bild->getWidth() != b->getWidth()
  39. || bild->getHeight() != b->getHeight())
  40. {
  41. if (!bild) bild = new Image();
  42. bild->newImage(b->getWidth(), b->getHeight(), 0);
  43. }
  44. bild->drawImage(0, 0, bild->getWidth(), bild->getHeight(), *b);
  45. b->release();
  46. }
  47. // Returns a pointer to the image
  48. Image* Texture::getImage() const
  49. {
  50. return bild ? dynamic_cast<Image*>(bild->getThis()) : 0;
  51. }
  52. // Returns a pointer to the image without increased reference counter
  53. Image* Texture::zImage() const
  54. {
  55. return bild;
  56. }
  57. // Returns the id of the texture if it was registered in a TextureList.
  58. // (see Framework::zTextureRegister())
  59. int Texture::getId() const
  60. {
  61. return id;
  62. }
  63. // Updates the texture. The pixels of the current image are copied into
  64. // the graphics memory
  65. bool DX9Texture::updateTextur()
  66. {
  67. return 1;
  68. }
  69. // Returns true if updateTextur needs to be called
  70. bool DX9Texture::needsUpdate() const
  71. {
  72. return 0;
  73. }
  74. DX11Texture::DX11Texture(
  75. ID3D11Device* device, ID3D11DeviceContext* context, Critical& deviceLock)
  76. : Texture(),
  77. txt(0),
  78. view(0),
  79. device(device),
  80. context(context),
  81. renderTarget(0),
  82. useMips(1),
  83. deviceLock(deviceLock)
  84. {}
  85. DX11Texture::~DX11Texture()
  86. {
  87. #ifdef WIN32
  88. if (txt) txt->Release();
  89. if (view) view->Release();
  90. #endif
  91. }
  92. // Updates the texture. The pixels of the current image are copied into
  93. // the graphics memory
  94. bool DX11Texture::updateTextur()
  95. {
  96. if (!bild) return 0;
  97. #ifdef WIN32
  98. if (!txt || lastGr != bild->getSize())
  99. {
  100. if (txt) txt->Release();
  101. txt = 0;
  102. D3D11_TEXTURE2D_DESC bufferDesc;
  103. memset(&bufferDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
  104. bufferDesc.ArraySize = 1;
  105. bufferDesc.Width = bild->getWidth();
  106. bufferDesc.Height = bild->getHeight();
  107. bufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  108. bufferDesc.BindFlags
  109. = (renderTarget || useMips ? D3D11_BIND_RENDER_TARGET : 0)
  110. | D3D11_BIND_SHADER_RESOURCE;
  111. bufferDesc.CPUAccessFlags
  112. = renderTarget || useMips ? 0 : D3D11_CPU_ACCESS_WRITE;
  113. bufferDesc.SampleDesc.Count = 1;
  114. bufferDesc.MipLevels = useMips ? 0 : 1;
  115. bufferDesc.Usage = renderTarget || useMips ? D3D11_USAGE_DEFAULT
  116. : D3D11_USAGE_DYNAMIC;
  117. bufferDesc.MiscFlags = useMips ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0;
  118. deviceLock.lock();
  119. HRESULT r = device->CreateTexture2D(&bufferDesc, 0, &txt);
  120. deviceLock.unlock();
  121. if (r != S_OK) return 0;
  122. }
  123. if (!renderTarget && (bild->getNeedRender() || changed))
  124. {
  125. changed = 0;
  126. if (useMips)
  127. {
  128. context->UpdateSubresource(txt,
  129. 0,
  130. 0,
  131. bild->getBuffer(),
  132. 4 * bild->getWidth(),
  133. 4 * bild->getWidth() * bild->getHeight());
  134. }
  135. else
  136. {
  137. D3D11_MAPPED_SUBRESOURCE buffer;
  138. context->Map(
  139. txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer);
  140. int* bgBuff = bild->getBuffer();
  141. int tmpBr = 4 * bild->getWidth();
  142. for (int y = 0, pitch = 0, bry = 0; y < bild->getHeight();
  143. ++y, pitch += buffer.RowPitch, bry += bild->getWidth())
  144. memcpy(&((BYTE*)buffer.pData)[pitch],
  145. (void*)&(bgBuff[bry]),
  146. tmpBr);
  147. context->Unmap(txt, 0);
  148. }
  149. }
  150. if (!view || lastGr != bild->getSize())
  151. {
  152. if (view) view->Release();
  153. view = 0;
  154. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesk;
  155. memset(&resourceDesk, 0, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
  156. resourceDesk.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  157. resourceDesk.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  158. resourceDesk.Texture2D.MipLevels = useMips ? -1 : 1;
  159. deviceLock.lock();
  160. HRESULT r = device->CreateShaderResourceView(txt, &resourceDesk, &view);
  161. deviceLock.unlock();
  162. if (r != S_OK) return 0;
  163. if (context && useMips) context->GenerateMips(view);
  164. }
  165. lastGr = bild->getSize();
  166. #endif
  167. return 1;
  168. }
  169. // Returns true if updateTextur needs to be called
  170. bool DX11Texture::needsUpdate() const
  171. {
  172. return !view;
  173. }
  174. // Returns the used shader resource view
  175. DX11Texture::operator ID3D11ShaderResourceView*() const
  176. {
  177. return view;
  178. }
  179. //! Returns the used texture
  180. DX11Texture::operator ID3D11Texture2D*() const
  181. {
  182. return txt;
  183. }
  184. //! specifies that this texture is used as a render target
  185. void DX11Texture::setRenderTarget(bool rt)
  186. {
  187. if (rt) useMips = 0;
  188. renderTarget = rt;
  189. }
  190. //! specifies if a mip map should be generated
  191. void DX11Texture::setUseMips(bool useMips)
  192. {
  193. this->useMips = useMips;
  194. }
  195. //! copy the texture to an image
  196. void DX11Texture::copyToImage(Image* zB)
  197. {
  198. #ifdef WIN32
  199. D3D11_TEXTURE2D_DESC tempBufferDesc;
  200. memset(&tempBufferDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
  201. tempBufferDesc.ArraySize = 1;
  202. tempBufferDesc.Width = bild->getWidth();
  203. tempBufferDesc.Height = bild->getHeight();
  204. tempBufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  205. tempBufferDesc.BindFlags = 0;
  206. tempBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  207. tempBufferDesc.SampleDesc.Count = 1;
  208. tempBufferDesc.MipLevels = 1;
  209. tempBufferDesc.Usage = D3D11_USAGE_STAGING;
  210. ID3D11Texture2D* tmpTxt;
  211. deviceLock.lock();
  212. HRESULT r = device->CreateTexture2D(&tempBufferDesc, 0, &tmpTxt);
  213. deviceLock.unlock();
  214. if (r != S_OK) throw "could not create resource copy with cpu read access";
  215. context->CopyResource(tmpTxt, txt);
  216. zB->newImage(bild->getWidth(), bild->getHeight(), 0);
  217. D3D11_MAPPED_SUBRESOURCE buffer;
  218. r = context->Map(tmpTxt, 0, D3D11_MAP::D3D11_MAP_READ, 0, &buffer);
  219. if (r != S_OK) throw "could not access recource copy";
  220. int* bgBuff = zB->getBuffer();
  221. int tmpBr = 4 * zB->getWidth();
  222. for (int y = 0, pitch = 0, bry = 0; y < zB->getHeight();
  223. ++y, pitch += buffer.RowPitch, bry += zB->getWidth())
  224. memcpy((void*)&(bgBuff[bry]), &((BYTE*)buffer.pData)[pitch], tmpBr);
  225. for (int i = 0; i < zB->getWidth() * zB->getHeight(); i++)
  226. {
  227. if (bgBuff[i]) bgBuff[i] |= 0xFF000000;
  228. }
  229. context->Unmap(tmpTxt, 0);
  230. tmpTxt->Release();
  231. #endif
  232. }