Textur.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "Textur.h"
  2. #include "Bild.h"
  3. #ifdef WIN32
  4. #include "DXCommandQueue.h"
  5. #include <d3d11.h>
  6. #include <d3d12.h>
  7. #include <d3dx12.h>
  8. #endif
  9. using namespace Framework;
  10. // Inhalt der Textur Klasse
  11. // Konstruktor
  12. Textur::Textur()
  13. : ReferenceCounter()
  14. {
  15. bild = 0;
  16. lastGr = Punkt( 0, 0 );
  17. id = -1;
  18. }
  19. // Destruktor
  20. Textur::~Textur()
  21. {
  22. if( bild )
  23. bild->release();
  24. }
  25. // Setzt einen Zeiger auf das Bild, welches die Textur enthält
  26. // b: Der Zeiger auf das Bild
  27. void Textur::setBildZ( Bild *b )
  28. {
  29. if( bild )
  30. bild->release();
  31. bild = b;
  32. }
  33. // Setzt das Bild welches die Textur enthält, indem es kopiert wird
  34. // b: Das Bild, was kopiert werden soll
  35. void Textur::setBild( Bild *b )
  36. {
  37. if( !b )
  38. return;
  39. if( !bild || bild->getBreite() != b->getBreite() || bild->getHeight() != b->getHeight() )
  40. {
  41. if( !bild )
  42. bild = new Bild();
  43. bild->neuBild( b->getBreite(), b->getHeight(), 0 );
  44. }
  45. bild->drawBild( 0, 0, bild->getBreite(), bild->getHeight(), *b );
  46. b->release();
  47. }
  48. // Gibt einen Zeiger auf das Bild zurück
  49. Bild *Textur::getBild() const
  50. {
  51. return bild ? (Bild *)bild->getThis() : 0;
  52. }
  53. // Gibt einen Zeiger auf das Bild ohne erhöhten Reference Counter zurück
  54. Bild *Textur::zBild() const
  55. {
  56. return bild;
  57. }
  58. // Gibt die Id der Textur zurück, wenn sie in einer TexturList registriert 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 Graphikspeicher kopiert
  64. bool DX9Textur::updateTextur()
  65. {
  66. return 1;
  67. }
  68. // Gibt true zurük, wenn updateTextur aufgerufen werden muss
  69. bool DX9Textur::brauchtUpdate() const
  70. {
  71. return 0;
  72. }
  73. DX11Textur::DX11Textur( ID3D11Device *device, ID3D11DeviceContext *context )
  74. : Textur(),
  75. txt( 0 ),
  76. view( 0 ),
  77. device( device ),
  78. context( context )
  79. {}
  80. DX11Textur::~DX11Textur()
  81. {
  82. #ifdef WIN32
  83. if( txt )
  84. txt->Release();
  85. if( view )
  86. view->Release();
  87. #endif
  88. }
  89. // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den Graphikspeicher kopiert
  90. bool DX11Textur::updateTextur()
  91. {
  92. if( !bild )
  93. return 0;
  94. #ifdef WIN32
  95. if( !txt || lastGr != bild->getSize() )
  96. {
  97. if( txt )
  98. 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 = D3D11_BIND_SHADER_RESOURCE;
  107. bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  108. bufferDesc.SampleDesc.Count = 1;
  109. bufferDesc.MipLevels = 1;
  110. bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
  111. HRESULT r = device->CreateTexture2D( &bufferDesc, 0, &txt );
  112. if( r != S_OK )
  113. return 0;
  114. }
  115. D3D11_MAPPED_SUBRESOURCE buffer;
  116. context->Map( txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer );
  117. int *bgBuff = bild->getBuffer();
  118. int tmpBr = 4 * bild->getBreite();
  119. for( int y = 0, pitch = 0, bry = 0; y < bild->getHeight(); ++y, pitch += buffer.RowPitch, bry += bild->getBreite() )
  120. memcpy( &( (BYTE *)buffer.pData )[ pitch ], (void *)&( bgBuff[ bry ] ), tmpBr );
  121. context->Unmap( txt, 0 );
  122. if( !view || lastGr != bild->getSize() )
  123. {
  124. if( view )
  125. view->Release();
  126. view = 0;
  127. D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesk;
  128. memset( &resourceDesk, 0, sizeof( D3D11_SHADER_RESOURCE_VIEW_DESC ) );
  129. resourceDesk.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  130. resourceDesk.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  131. resourceDesk.Texture2D.MipLevels = 1;
  132. HRESULT r = device->CreateShaderResourceView( txt, &resourceDesk, &view );
  133. if( r != S_OK )
  134. return 0;
  135. }
  136. lastGr = bild->getSize();
  137. #endif
  138. return 1;
  139. }
  140. // Gibt true zurük, wenn updateTextur aufgerufen werden muss
  141. bool DX11Textur::brauchtUpdate() const
  142. {
  143. return !view;
  144. }
  145. // Gibt die verwendtete Shader Resource View zurück
  146. DX11Textur::operator ID3D11ShaderResourceView *( ) const
  147. {
  148. return view;
  149. }
  150. DX12Textur::DX12Textur( ID3D12Device2 *device, DX12CopyCommandQueue *copy, DX12DirectCommandQueue *direct )
  151. : Textur(),
  152. buffer( 0 ),
  153. intermediate( 0 ),
  154. device( device ),
  155. copy( copy ),
  156. direct( direct ),
  157. shaderResource( 0 )
  158. {}
  159. DX12Textur::~DX12Textur()
  160. {
  161. #ifdef WIN32
  162. if( buffer )
  163. buffer->Release();
  164. if( intermediate )
  165. intermediate->Release();
  166. #endif
  167. }
  168. // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den Graphikspeicher kopiert
  169. bool DX12Textur::updateTextur()
  170. {
  171. if( !bild )
  172. return 0;
  173. #ifdef WIN32
  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_B8G8R8A8_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 | D3D12_RESOURCE_STATE_NON_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->getCommandList()->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( direct->getCommandList(), 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 | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  235. barrier.Transition.Subresource = 0;
  236. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  237. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  238. shaderResource = 1;
  239. }
  240. #endif
  241. return 1;
  242. }
  243. // Gibt true zurük, wenn updateTextur aufgerufen werden muss
  244. bool DX12Textur::brauchtUpdate() const
  245. {
  246. return bild && !buffer;
  247. }
  248. // Gibt die DX12 Resource zurück
  249. ID3D12Resource *DX12Textur::getResource()
  250. {
  251. return buffer;
  252. }