#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) {} DX12Texture::~DX12Texture() { #ifdef WIN32 if (buffer) buffer->Release(); if (upload) upload->Release(); #endif } // 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); 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; }