123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- #include "Textur.h"
- #include "Bild.h"
- #include "DXCommandQueue.h"
- #ifdef WIN32
- #include <d3d11.h>
- #include <d3d12.h>
- #include <d3dx12.h>
- #endif
- using namespace Framework;
- // Inhalt der Textur Klasse
- // Konstruktor
- Textur::Textur()
- {
- bild = 0;
- lastGr = Punkt( 0, 0 );
- id = -1;
- ref = 1;
- }
- // Destruktor
- Textur::~Textur()
- {
- if( bild )
- bild->release();
- }
- // Setzt einen Zeiger auf das Bild, welches die Textur enthält
- // b: Der Zeiger auf das Bild
- void Textur::setBildZ( Bild *b )
- {
- if( bild )
- bild->release();
- bild = b;
- }
- // Setzt das Bild welches die Textur enthält, indem es kopiert wird
- // b: Das Bild, was kopiert werden soll
- void Textur::setBild( Bild *b )
- {
- if( !b )
- return;
- if( !bild || bild->getBreite() != b->getBreite() || bild->getHeight() != b->getHeight() )
- {
- if( !bild )
- bild = new Bild();
- bild->neuBild( b->getBreite(), b->getHeight(), 0 );
- }
- bild->drawBild( 0, 0, bild->getBreite(), bild->getHeight(), *b );
- b->release();
- }
- // Gibt einen Zeiger auf das Bild zurück
- Bild *Textur::getBild() const
- {
- return bild ? bild->getThis() : 0;
- }
- // Gibt einen Zeiger auf das Bild ohne erhöhten Reference Counter zurück
- Bild *Textur::zBild() const
- {
- return bild;
- }
- // Gibt die Id der Textur zurück, wenn sie in einer TexturList registriert wurde. (siehe Framework::zTexturRegister())
- int Textur::getId() const
- {
- return id;
- }
- // Erhöht den Reference Counting Zähler.
- // return: this.
- Textur *Textur::getThis()
- {
- ref++;
- return this;
- }
- // Verringert den Reference Counting Zähler. Wenn der Zähler 0 erreicht, wird das Zeichnung automatisch gelöscht.
- // return: 0.
- Textur *Textur::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
- DX11Textur::DX11Textur( ID3D11Device *device, ID3D11DeviceContext *context )
- : Textur(),
- txt( 0 ),
- view( 0 ),
- device( device ),
- context( context )
- {}
- DX11Textur::~DX11Textur()
- {
- if( txt )
- txt->Release();
- if( view )
- view->Release();
- }
- // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den Graphikspeicher kopiert
- bool DX11Textur::updateTextur()
- {
- if( !bild )
- return 0;
- #ifdef WIN32
- if( !txt || lastGr != bild->getSize() )
- {
- if( txt )
- txt->Release();
- txt = 0;
- D3D11_TEXTURE2D_DESC bufferDesc;
- memset( &bufferDesc, 0, sizeof( D3D11_TEXTURE2D_DESC ) );
- bufferDesc.ArraySize = 1;
- bufferDesc.Width = bild->getBreite();
- bufferDesc.Height = bild->getHeight();
- bufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
- bufferDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
- bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
- bufferDesc.SampleDesc.Count = 1;
- bufferDesc.MipLevels = 1;
- bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
- HRESULT r = device->CreateTexture2D( &bufferDesc, 0, &txt );
- if( r != S_OK )
- return 0;
- }
- D3D11_MAPPED_SUBRESOURCE buffer;
- context->Map( txt, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &buffer );
- int *bgBuff = bild->getBuffer();
- int tmpBr = 4 * bild->getBreite();
- for( int y = 0, pitch = 0, bry = 0; y < bild->getHeight(); ++y, pitch += buffer.RowPitch, bry += bild->getBreite() )
- memcpy( &( (BYTE *)buffer.pData )[ pitch ], ( void * ) & ( bgBuff[ bry ] ), tmpBr );
- context->Unmap( txt, 0 );
- if( !view || lastGr != bild->getSize() )
- {
- if( view )
- view->Release();
- view = 0;
- D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesk;
- memset( &resourceDesk, 0, sizeof( D3D11_SHADER_RESOURCE_VIEW_DESC ) );
- resourceDesk.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
- resourceDesk.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
- resourceDesk.Texture2D.MipLevels = 1;
- HRESULT r = device->CreateShaderResourceView( txt, &resourceDesk, &view );
- if( r != S_OK )
- return 0;
- }
- lastGr = bild->getSize();
- #endif
- return 1;
- }
- // Gibt true zurük, wenn updateTextur aufgerufen werden muss
- bool DX11Textur::brauchtUpdate() const
- {
- return !view;
- }
- // Gibt die verwendtete Shader Resource View zurück
- DX11Textur::operator ID3D11ShaderResourceView *( ) const
- {
- return view;
- }
- DX12Textur::DX12Textur( ID3D12Device2 *device, DX12CopyCommandQueue *copy, DX12DirectCommandQueue *direct )
- : Textur(),
- buffer( 0 ),
- intermediate( 0 ),
- device( device ),
- copy( copy ),
- direct( direct ),
- shaderResource( 0 )
- {}
- DX12Textur::~DX12Textur()
- {
- if( buffer )
- buffer->Release();
- if( intermediate )
- intermediate->Release();
- }
- // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den Graphikspeicher kopiert
- bool DX12Textur::updateTextur()
- {
- if( !bild )
- return 0;
- if( !buffer )
- {
- D3D12_RESOURCE_DESC description;
- ZeroMemory( &description, sizeof( D3D12_RESOURCE_DESC ) );
- description.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
- description.Height = bild->getHeight();
- description.Width = bild->getBreite();
- description.DepthOrArraySize = 1;
- description.MipLevels = 1;
- description.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- description.SampleDesc.Count = 1;
- description.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
- description.Flags = D3D12_RESOURCE_FLAG_NONE;
- D3D12_HEAP_PROPERTIES hprop;
- hprop.Type = D3D12_HEAP_TYPE_DEFAULT;
- hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
- hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
- hprop.CreationNodeMask = 1;
- hprop.VisibleNodeMask = 1;
- device->CreateCommittedResource( &hprop, D3D12_HEAP_FLAG_NONE, &description, D3D12_RESOURCE_STATE_COPY_DEST, 0, __uuidof( ID3D12Resource ), (void **)& buffer );
- const UINT64 uploadBufferSize = GetRequiredIntermediateSize( buffer, 0, 1 );
- D3D12_RESOURCE_DESC iDescription;
- iDescription.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
- iDescription.Alignment = 0;
- iDescription.Width = uploadBufferSize;
- iDescription.Height = 1;
- iDescription.DepthOrArraySize = 1;
- iDescription.MipLevels = 1;
- iDescription.Format = DXGI_FORMAT_UNKNOWN;
- iDescription.SampleDesc.Count = 1;
- iDescription.SampleDesc.Quality = 0;
- iDescription.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
- iDescription.Flags = D3D12_RESOURCE_FLAG_NONE;
- hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
- device->CreateCommittedResource( &hprop, D3D12_HEAP_FLAG_NONE, &iDescription, D3D12_RESOURCE_STATE_GENERIC_READ, 0, __uuidof( ID3D12Resource ), (void **)& intermediate );
- shaderResource = 0;
- }
- if( bild )
- {
- if( shaderResource )
- {
- D3D12_RESOURCE_BARRIER barrier;
- barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
- barrier.Transition.pResource = buffer;
- barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
- barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
- barrier.Transition.Subresource = 0;
- barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
- direct->getCommandList()->ResourceBarrier( 1, &barrier );
- shaderResource = 0;
- }
- D3D12_SUBRESOURCE_DATA textureData = {};
- textureData.pData = bild->getBuffer();
- textureData.RowPitch = bild->getBreite() * sizeof( int );
- textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
- UpdateSubresources( direct->getCommandList(), buffer, intermediate, 0, 0, 1, &textureData );
- D3D12_RESOURCE_BARRIER barrier;
- barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
- barrier.Transition.pResource = buffer;
- barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
- barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
- barrier.Transition.Subresource = 0;
- barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
- direct->getCommandList()->ResourceBarrier( 1, &barrier );
- shaderResource = 1;
- }
- return 1;
- }
- // Gibt true zurük, wenn updateTextur aufgerufen werden muss
- bool DX12Textur::brauchtUpdate() const
- {
- return bild && !buffer;
- }
- // Gibt die DX12 Resource zurück
- ID3D12Resource *DX12Textur::getResource()
- {
- return buffer;
- }
|