Textur.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // Konstruktor
  11. Textur::Textur()
  12. {
  13. bild = 0;
  14. lastGr = Punkt( 0, 0 );
  15. id = -1;
  16. ref = 1;
  17. }
  18. // Destruktor
  19. Textur::~Textur()
  20. {
  21. if( bild )
  22. bild->release();
  23. }
  24. // Setzt einen Zeiger auf das Bild, welches die Textur enthält
  25. // b: Der Zeiger auf das Bild
  26. void Textur::setBildZ( Bild *b )
  27. {
  28. if( bild )
  29. bild->release();
  30. bild = b;
  31. }
  32. // Setzt das Bild welches die Textur enthält, indem es kopiert wird
  33. // b: Das Bild, was kopiert werden soll
  34. void Textur::setBild( Bild *b )
  35. {
  36. if( !b )
  37. return;
  38. if( !bild || bild->getBreite() != b->getBreite() || bild->getHeight() != b->getHeight() )
  39. {
  40. if( !bild )
  41. 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 zurück
  48. Bild *Textur::getBild() const
  49. {
  50. return bild ? bild->getThis() : 0;
  51. }
  52. // Gibt einen Zeiger auf das Bild ohne erhöhten Reference Counter zurück
  53. Bild *Textur::zBild() const
  54. {
  55. return bild;
  56. }
  57. // Gibt die Id der Textur zurück, wenn sie in einer TexturList registriert wurde. (siehe Framework::zTexturRegister())
  58. int Textur::getId() const
  59. {
  60. return id;
  61. }
  62. // Erhöht den Reference Counting Zähler.
  63. // return: this.
  64. Textur *Textur::getThis()
  65. {
  66. ref++;
  67. return this;
  68. }
  69. // Verringert den Reference Counting Zähler. Wenn der Zähler 0 erreicht, wird das Zeichnung automatisch gelöscht.
  70. // return: 0.
  71. Textur *Textur::release()
  72. {
  73. ref--;
  74. if( !ref )
  75. delete this;
  76. return 0;
  77. }
  78. DX11Textur::DX11Textur( ID3D11Device *device, ID3D11DeviceContext *context )
  79. : Textur(),
  80. txt( 0 ),
  81. view( 0 ),
  82. device( device ),
  83. context( context )
  84. {}
  85. DX11Textur::~DX11Textur()
  86. {
  87. if( txt )
  88. txt->Release();
  89. if( view )
  90. view->Release();
  91. }
  92. // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den Graphikspeicher kopiert
  93. bool DX11Textur::updateTextur()
  94. {
  95. if( !bild )
  96. return 0;
  97. #ifdef WIN32
  98. if( !txt || lastGr != bild->getSize() )
  99. {
  100. if( txt )
  101. txt->Release();
  102. txt = 0;
  103. D3D11_TEXTURE2D_DESC bufferDesc;
  104. memset( &bufferDesc, 0, sizeof( D3D11_TEXTURE2D_DESC ) );
  105. bufferDesc.ArraySize = 1;
  106. bufferDesc.Width = bild->getBreite();
  107. bufferDesc.Height = bild->getHeight();
  108. bufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  109. bufferDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  110. bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  111. bufferDesc.SampleDesc.Count = 1;
  112. bufferDesc.MipLevels = 1;
  113. bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
  114. HRESULT r = device->CreateTexture2D( &bufferDesc, 0, &txt );
  115. if( r != S_OK )
  116. return 0;
  117. }
  118. D3D11_MAPPED_SUBRESOURCE buffer;
  119. context->Map( txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer );
  120. int *bgBuff = bild->getBuffer();
  121. int tmpBr = 4 * bild->getBreite();
  122. for( int y = 0, pitch = 0, bry = 0; y < bild->getHeight(); ++y, pitch += buffer.RowPitch, bry += bild->getBreite() )
  123. memcpy( &( (BYTE *)buffer.pData )[ pitch ], ( void * ) & ( bgBuff[ bry ] ), tmpBr );
  124. context->Unmap( txt, 0 );
  125. if( !view || lastGr != bild->getSize() )
  126. {
  127. if( view )
  128. view->Release();
  129. view = 0;
  130. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesk;
  131. memset( &resourceDesk, 0, sizeof( D3D11_SHADER_RESOURCE_VIEW_DESC ) );
  132. resourceDesk.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  133. resourceDesk.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  134. resourceDesk.Texture2D.MipLevels = 1;
  135. HRESULT r = device->CreateShaderResourceView( txt, &resourceDesk, &view );
  136. if( r != S_OK )
  137. return 0;
  138. }
  139. lastGr = bild->getSize();
  140. #endif
  141. return 1;
  142. }
  143. // Gibt true zurük, wenn updateTextur aufgerufen werden muss
  144. bool DX11Textur::brauchtUpdate() const
  145. {
  146. return !view;
  147. }
  148. // Gibt die verwendtete Shader Resource View zurück
  149. DX11Textur::operator ID3D11ShaderResourceView *( ) const
  150. {
  151. return view;
  152. }
  153. DX12Textur::DX12Textur( ID3D12Device2 *device, ID3D12GraphicsCommandList2 *copy, ID3D12GraphicsCommandList2 *direct )
  154. : Textur(),
  155. buffer( 0 ),
  156. intermediate( 0 ),
  157. device( device ),
  158. copy( copy ),
  159. direct( direct ),
  160. shaderResource( 0 )
  161. {}
  162. DX12Textur::~DX12Textur()
  163. {
  164. if( buffer )
  165. buffer->Release();
  166. if( intermediate )
  167. intermediate->Release();
  168. }
  169. // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den Graphikspeicher kopiert
  170. bool DX12Textur::updateTextur()
  171. {
  172. if( !bild )
  173. return 0;
  174. if( !buffer )
  175. {
  176. D3D12_RESOURCE_DESC description;
  177. ZeroMemory( &description, sizeof( D3D12_RESOURCE_DESC ) );
  178. description.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  179. description.Height = bild->getHeight();
  180. description.Width = bild->getBreite();
  181. description.DepthOrArraySize = 1;
  182. description.MipLevels = 1;
  183. description.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  184. description.SampleDesc.Count = 1;
  185. description.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  186. description.Flags = D3D12_RESOURCE_FLAG_NONE;
  187. D3D12_HEAP_PROPERTIES hprop;
  188. hprop.Type = D3D12_HEAP_TYPE_DEFAULT;
  189. hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  190. hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  191. hprop.CreationNodeMask = 1;
  192. hprop.VisibleNodeMask = 1;
  193. device->CreateCommittedResource( &hprop, D3D12_HEAP_FLAG_NONE, &description, D3D12_RESOURCE_STATE_COPY_DEST, 0, __uuidof( ID3D12Resource ), (void **)& buffer );
  194. const UINT64 uploadBufferSize = GetRequiredIntermediateSize( buffer, 0, 1 );
  195. D3D12_RESOURCE_DESC iDescription;
  196. iDescription.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  197. iDescription.Alignment = 0;
  198. iDescription.Width = uploadBufferSize;
  199. iDescription.Height = 1;
  200. iDescription.DepthOrArraySize = 1;
  201. iDescription.MipLevels = 1;
  202. iDescription.Format = DXGI_FORMAT_UNKNOWN;
  203. iDescription.SampleDesc.Count = 1;
  204. iDescription.SampleDesc.Quality = 0;
  205. iDescription.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  206. iDescription.Flags = D3D12_RESOURCE_FLAG_NONE;
  207. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  208. device->CreateCommittedResource( &hprop, D3D12_HEAP_FLAG_NONE, &iDescription, D3D12_RESOURCE_STATE_GENERIC_READ, 0, __uuidof( ID3D12Resource ), (void **)& intermediate );
  209. shaderResource = 0;
  210. }
  211. if( bild )
  212. {
  213. if( shaderResource )
  214. {
  215. D3D12_RESOURCE_BARRIER barrier;
  216. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  217. barrier.Transition.pResource = buffer;
  218. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  219. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  220. barrier.Transition.Subresource = 0;
  221. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  222. direct->ResourceBarrier( 1, &barrier );
  223. shaderResource = 0;
  224. }
  225. D3D12_SUBRESOURCE_DATA textureData = {};
  226. textureData.pData = bild->getBuffer();
  227. textureData.RowPitch = bild->getBreite() * sizeof( int );
  228. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  229. UpdateSubresources( copy, buffer, intermediate, 0, 0, 1, &textureData );
  230. D3D12_RESOURCE_BARRIER barrier;
  231. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  232. barrier.Transition.pResource = buffer;
  233. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  234. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
  235. barrier.Transition.Subresource = 0;
  236. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  237. direct->ResourceBarrier( 1, &barrier );
  238. shaderResource = 1;
  239. }
  240. return 1;
  241. }
  242. // Gibt true zurük, wenn updateTextur aufgerufen werden muss
  243. bool DX12Textur::brauchtUpdate() const
  244. {
  245. return bild && !buffer;
  246. }
  247. // Gibt die DX12 Resource zurück
  248. ID3D12Resource *DX12Textur::getResource()
  249. {
  250. return buffer;
  251. }