123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include "DX12Textur.h"
- #include "Bild.h"
- #include "d3dx12.h"
- #include "DX12CommandQueue.h"
- using namespace Framework;
- DX12Textur::DX12Textur(ID3D12Device* device,
- DX12CopyCommandQueue* copy,
- DX12DirectCommandQueue* direct)
- : Textur(),
- buffer(0),
- intermediate(0),
- device(device),
- copy(copy),
- direct(direct),
- shaderResource(0)
- {}
- DX12Textur::~DX12Textur()
- {
- #ifdef WIN32
- if (buffer) buffer->Release();
- if (intermediate) intermediate->Release();
- #endif
- }
- // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den
- // Graphikspeicher kopiert
- bool DX12Textur::updateTextur()
- {
- if (!bild) return 0;
- #ifdef WIN32
- 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_B8G8R8A8_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 && (changed || bild->getRend()))
- {
- changed = 0;
- 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;
- }
- #endif
- 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;
- }
|