Textur.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "Textur.h"
  2. #include "Bild.h"
  3. #ifdef WIN32
  4. # include <d3d11.h>
  5. # include <d3d12.h>
  6. # include "d3dx12.h"
  7. #endif
  8. using namespace Framework;
  9. // Inhalt der Textur Klasse
  10. // Constructor
  11. Textur::Textur()
  12. : ReferenceCounter()
  13. {
  14. bild = 0;
  15. lastGr = Punkt(0, 0);
  16. id = -1;
  17. changed = 0;
  18. }
  19. // Destructor
  20. Textur::~Textur()
  21. {
  22. if (bild) bild->release();
  23. }
  24. // Setzt einen Zeiger auf das Bild, welches die Textur enthaelt
  25. // b: Der Zeiger auf das Bild
  26. void Textur::setBildZ(Bild* b)
  27. {
  28. if (bild != b) changed = 1;
  29. if (bild) bild->release();
  30. bild = b;
  31. }
  32. // Setzt das Bild welches die Textur enthaelt, indem es kopiert wird
  33. // b: Das Bild, was kopiert werden soll
  34. void Textur::setBild(Bild* b)
  35. {
  36. if (!b) return;
  37. if (bild != b) changed = 1;
  38. if (!bild || bild->getBreite() != b->getBreite()
  39. || bild->getHeight() != b->getHeight())
  40. {
  41. if (!bild) bild = new Bild();
  42. bild->neuBild(b->getBreite(), b->getHeight(), 0);
  43. }
  44. bild->drawBild(0, 0, bild->getBreite(), bild->getHeight(), *b);
  45. b->release();
  46. }
  47. // Gibt einen Zeiger auf das Bild zurueck
  48. Bild* Textur::getBild() const
  49. {
  50. return bild ? dynamic_cast<Bild*>(bild->getThis()) : 0;
  51. }
  52. // Gibt einen Zeiger auf das Bild ohne erhoehten Reference Counter zurueck
  53. Bild* Textur::zBild() const
  54. {
  55. return bild;
  56. }
  57. // Gibt die Id der Textur zurueck, wenn sie in einer TexturList registriert
  58. // wurde. (siehe Framework::zTexturRegister())
  59. int Textur::getId() const
  60. {
  61. return id;
  62. }
  63. // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den
  64. // Graphikspeicher kopiert
  65. bool DX9Textur::updateTextur()
  66. {
  67. return 1;
  68. }
  69. // Gibt true zuruek, wenn updateTextur aufgerufen werden muss
  70. bool DX9Textur::brauchtUpdate() const
  71. {
  72. return 0;
  73. }
  74. DX11Textur::DX11Textur(ID3D11Device* device, ID3D11DeviceContext* context)
  75. : Textur(),
  76. txt(0),
  77. view(0),
  78. device(device),
  79. context(context),
  80. renderTarget(0),
  81. useMips(1)
  82. {}
  83. DX11Textur::~DX11Textur()
  84. {
  85. #ifdef WIN32
  86. if (txt) txt->Release();
  87. if (view) view->Release();
  88. #endif
  89. }
  90. // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den
  91. // Graphikspeicher kopiert
  92. bool DX11Textur::updateTextur()
  93. {
  94. if (!bild) return 0;
  95. #ifdef WIN32
  96. if (!txt || lastGr != bild->getSize())
  97. {
  98. if (txt) txt->Release();
  99. txt = 0;
  100. D3D11_TEXTURE2D_DESC bufferDesc;
  101. memset(&bufferDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
  102. bufferDesc.ArraySize = 1;
  103. bufferDesc.Width = bild->getBreite();
  104. bufferDesc.Height = bild->getHeight();
  105. bufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  106. bufferDesc.BindFlags
  107. = (renderTarget || useMips ? D3D11_BIND_RENDER_TARGET : 0)
  108. | D3D11_BIND_SHADER_RESOURCE;
  109. bufferDesc.CPUAccessFlags
  110. = renderTarget || useMips ? 0 : D3D11_CPU_ACCESS_WRITE;
  111. bufferDesc.SampleDesc.Count = 1;
  112. bufferDesc.MipLevels = useMips ? 0 : 1;
  113. bufferDesc.Usage = renderTarget || useMips ? D3D11_USAGE_DEFAULT
  114. : D3D11_USAGE_DYNAMIC;
  115. bufferDesc.MiscFlags = useMips ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0;
  116. HRESULT r = device->CreateTexture2D(&bufferDesc, 0, &txt);
  117. if (r != S_OK) return 0;
  118. }
  119. if (!renderTarget && (bild->getRend() || changed))
  120. {
  121. changed = 0;
  122. if (useMips)
  123. {
  124. context->UpdateSubresource(txt,
  125. 0,
  126. 0,
  127. bild->getBuffer(),
  128. 4 * bild->getBreite(),
  129. 4 * bild->getBreite() * bild->getHeight());
  130. }
  131. else
  132. {
  133. D3D11_MAPPED_SUBRESOURCE buffer;
  134. context->Map(
  135. txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer);
  136. int* bgBuff = bild->getBuffer();
  137. int tmpBr = 4 * bild->getBreite();
  138. for (int y = 0, pitch = 0, bry = 0; y < bild->getHeight();
  139. ++y, pitch += buffer.RowPitch, bry += bild->getBreite())
  140. memcpy(&((BYTE*)buffer.pData)[pitch],
  141. (void*)&(bgBuff[bry]),
  142. tmpBr);
  143. context->Unmap(txt, 0);
  144. }
  145. }
  146. if (!view || lastGr != bild->getSize())
  147. {
  148. if (view) view->Release();
  149. view = 0;
  150. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesk;
  151. memset(&resourceDesk, 0, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
  152. resourceDesk.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  153. resourceDesk.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  154. resourceDesk.Texture2D.MipLevels = useMips ? -1 : 1;
  155. HRESULT r = device->CreateShaderResourceView(txt, &resourceDesk, &view);
  156. if (r != S_OK) return 0;
  157. if (context && useMips) context->GenerateMips(view);
  158. }
  159. lastGr = bild->getSize();
  160. #endif
  161. return 1;
  162. }
  163. // Gibt true zuruek, wenn updateTextur aufgerufen werden muss
  164. bool DX11Textur::brauchtUpdate() const
  165. {
  166. return !view;
  167. }
  168. // Gibt die verwendtete Shader Resource View zurueck
  169. DX11Textur::operator ID3D11ShaderResourceView*() const
  170. {
  171. return view;
  172. }
  173. //! Gibt die verwendete Textur zurueck
  174. DX11Textur::operator ID3D11Texture2D*() const
  175. {
  176. return txt;
  177. }
  178. //! specifies that this texture is used as a render target
  179. void DX11Textur::setRenderTarget(bool rt)
  180. {
  181. if (rt) useMips = 0;
  182. renderTarget = rt;
  183. }
  184. //! specifies if a mip map should be generated
  185. void DX11Textur::setUseMips(bool useMips)
  186. {
  187. this->useMips = useMips;
  188. }
  189. //! copy the texture to an image
  190. void DX11Textur::copyToImage(Bild* zB)
  191. {
  192. #ifdef WIN32
  193. D3D11_TEXTURE2D_DESC tempBufferDesc;
  194. memset(&tempBufferDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
  195. tempBufferDesc.ArraySize = 1;
  196. tempBufferDesc.Width = bild->getBreite();
  197. tempBufferDesc.Height = bild->getHeight();
  198. tempBufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  199. tempBufferDesc.BindFlags = 0;
  200. tempBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  201. tempBufferDesc.SampleDesc.Count = 1;
  202. tempBufferDesc.MipLevels = 1;
  203. tempBufferDesc.Usage = D3D11_USAGE_STAGING;
  204. ID3D11Texture2D* tmpTxt;
  205. HRESULT r = device->CreateTexture2D(&tempBufferDesc, 0, &tmpTxt);
  206. if (r != S_OK) throw "could not create resource copy with cpu read access";
  207. context->CopyResource(tmpTxt, txt);
  208. zB->neuBild(bild->getBreite(), bild->getHeight(), 0);
  209. D3D11_MAPPED_SUBRESOURCE buffer;
  210. r = context->Map(tmpTxt, 0, D3D11_MAP::D3D11_MAP_READ, 0, &buffer);
  211. if (r != S_OK) throw "could not access recource copy";
  212. int* bgBuff = zB->getBuffer();
  213. int tmpBr = 4 * zB->getBreite();
  214. for (int y = 0, pitch = 0, bry = 0; y < zB->getHeight();
  215. ++y, pitch += buffer.RowPitch, bry += zB->getBreite())
  216. memcpy((void*)&(bgBuff[bry]), &((BYTE*)buffer.pData)[pitch], tmpBr);
  217. for (int i = 0; i < zB->getBreite() * zB->getHeight(); i++)
  218. {
  219. if (bgBuff[i]) bgBuff[i] |= 0xFF000000;
  220. }
  221. context->Unmap(tmpTxt, 0);
  222. tmpTxt->Release();
  223. #endif
  224. }