| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include "DX12Texture.h"
- #include "d3dx12.h"
- #include "DX12CommandQueue.h"
- #include "Image.h"
- using namespace Framework;
- DX12Texture::DX12Texture(
- ID3D12Device* device, DX12DirectCommandQueue* direct, TextureDirection dir)
- : Texture(dir),
- buffer(0),
- device(device),
- direct(direct),
- upload(0),
- wcName(0)
- {}
- DX12Texture::~DX12Texture()
- {
- #ifdef WIN32
- if (buffer) buffer->Release();
- if (upload) upload->Release();
- #endif
- delete[] wcName;
- }
- // Updates the texture. The pixels of the current image are copied into the
- // graphics memory
- bool DX12Texture::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->getWidth();
- 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_ALLOW_UNORDERED_ACCESS;
- 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,
- getDirection() == RAM_TO_GPU ? D3D12_RESOURCE_STATE_COPY_DEST
- : D3D12_RESOURCE_STATE_COPY_SOURCE,
- 0,
- __uuidof(ID3D12Resource),
- (void**)&buffer);
- if (!wcName)
- {
- wcName = new wchar_t[name.getLength() + 1];
- mbstowcs_s(0,
- wcName,
- name.getLength() + 1,
- name.getText(),
- name.getLength() + 1);
- }
- buffer->SetName(wcName);
- bufferChanged = 1;
- if (getDirection() == RAM_TO_GPU)
- {
- 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**)&upload);
- }
- }
- if (getDirection() == RAM_TO_GPU)
- {
- if (bild && (changed || bild->getNeedRender()))
- {
- changed = 0;
- }
- D3D12_SUBRESOURCE_DATA textureData = {};
- textureData.pData = bild->getBuffer();
- textureData.RowPitch = bild->getWidth() * sizeof(int);
- textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
- UpdateSubresources(
- direct->zCommandList(), buffer, upload, 0, 0, 1, &textureData);
- }
- // TODO: Implement GPU_TO_RAM direction
- #endif
- return 1;
- }
- // Returns true if updateTextur needs to be called
- bool DX12Texture::needsUpdate() const
- {
- return bild && !buffer;
- }
- // Returns the DX12 resource
- ID3D12Resource* DX12Texture::zResource()
- {
- return buffer;
- }
|