#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)
    {
        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;
}