Texture.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. deviceLock.lock();
  129. context->UpdateSubresource(txt,
  130. 0,
  131. 0,
  132. bild->getBuffer(),
  133. 4 * bild->getWidth(),
  134. 4 * bild->getWidth() * bild->getHeight());
  135. deviceLock.unlock();
  136. }
  137. else
  138. {
  139. D3D11_MAPPED_SUBRESOURCE buffer;
  140. deviceLock.lock();
  141. context->Map(
  142. txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer);
  143. int* bgBuff = bild->getBuffer();
  144. int tmpBr = 4 * bild->getWidth();
  145. for (int y = 0, pitch = 0, bry = 0; y < bild->getHeight();
  146. ++y, pitch += buffer.RowPitch, bry += bild->getWidth())
  147. {
  148. memcpy(&((BYTE*)buffer.pData)[pitch],
  149. (void*)&(bgBuff[bry]),
  150. tmpBr);
  151. }
  152. context->Unmap(txt, 0);
  153. deviceLock.unlock();
  154. }
  155. }
  156. if (!view || lastGr != bild->getSize())
  157. {
  158. if (view) view->Release();
  159. view = 0;
  160. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesk;
  161. memset(&resourceDesk, 0, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
  162. resourceDesk.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  163. resourceDesk.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  164. resourceDesk.Texture2D.MipLevels = useMips ? -1 : 1;
  165. deviceLock.lock();
  166. HRESULT r = device->CreateShaderResourceView(txt, &resourceDesk, &view);
  167. deviceLock.unlock();
  168. if (r != S_OK) return 0;
  169. if (context && useMips)
  170. {
  171. deviceLock.lock();
  172. context->GenerateMips(view);
  173. deviceLock.unlock();
  174. }
  175. }
  176. lastGr = bild->getSize();
  177. #endif
  178. return 1;
  179. }
  180. // Returns true if updateTextur needs to be called
  181. bool DX11Texture::needsUpdate() const
  182. {
  183. return !view;
  184. }
  185. // Returns the used shader resource view
  186. DX11Texture::operator ID3D11ShaderResourceView*() const
  187. {
  188. return view;
  189. }
  190. //! Returns the used texture
  191. DX11Texture::operator ID3D11Texture2D*() const
  192. {
  193. return txt;
  194. }
  195. //! specifies that this texture is used as a render target
  196. void DX11Texture::setRenderTarget(bool rt)
  197. {
  198. if (rt) useMips = 0;
  199. renderTarget = rt;
  200. }
  201. //! specifies if a mip map should be generated
  202. void DX11Texture::setUseMips(bool useMips)
  203. {
  204. this->useMips = useMips;
  205. }
  206. //! copy the texture to an image
  207. void DX11Texture::copyToImage(Image* zB)
  208. {
  209. #ifdef WIN32
  210. D3D11_TEXTURE2D_DESC tempBufferDesc;
  211. memset(&tempBufferDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
  212. tempBufferDesc.ArraySize = 1;
  213. tempBufferDesc.Width = bild->getWidth();
  214. tempBufferDesc.Height = bild->getHeight();
  215. tempBufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  216. tempBufferDesc.BindFlags = 0;
  217. tempBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  218. tempBufferDesc.SampleDesc.Count = 1;
  219. tempBufferDesc.MipLevels = 1;
  220. tempBufferDesc.Usage = D3D11_USAGE_STAGING;
  221. ID3D11Texture2D* tmpTxt;
  222. deviceLock.lock();
  223. HRESULT r = device->CreateTexture2D(&tempBufferDesc, 0, &tmpTxt);
  224. deviceLock.unlock();
  225. if (r != S_OK)
  226. {
  227. throw "could not create resource copy with cpu read access";
  228. }
  229. deviceLock.lock();
  230. context->CopyResource(tmpTxt, txt);
  231. deviceLock.unlock();
  232. zB->newImage(bild->getWidth(), bild->getHeight(), 0);
  233. D3D11_MAPPED_SUBRESOURCE buffer;
  234. deviceLock.lock();
  235. r = context->Map(tmpTxt, 0, D3D11_MAP::D3D11_MAP_READ, 0, &buffer);
  236. deviceLock.unlock();
  237. if (r != S_OK)
  238. {
  239. throw "could not access recource copy";
  240. }
  241. int* bgBuff = zB->getBuffer();
  242. int tmpBr = 4 * zB->getWidth();
  243. for (int y = 0, pitch = 0, bry = 0; y < zB->getHeight();
  244. ++y, pitch += buffer.RowPitch, bry += zB->getWidth())
  245. {
  246. memcpy((void*)&(bgBuff[bry]), &((BYTE*)buffer.pData)[pitch], tmpBr);
  247. }
  248. for (int i = 0; i < zB->getWidth() * zB->getHeight(); i++)
  249. {
  250. if (bgBuff[i]) bgBuff[i] |= 0xFF000000;
  251. }
  252. deviceLock.lock();
  253. context->Unmap(tmpTxt, 0);
  254. deviceLock.unlock();
  255. tmpTxt->Release();
  256. #endif
  257. }