// Copyright (c) Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Graphics/GPUDevice.h"
#include "ResourceOwnerDX12.h"
#if GRAPHICS_API_DIRECTX12
class GPUDeviceDX12;
class UploadBufferPageDX12;
// Upload buffer page size
#define DX12_DEFAULT_UPLOAD_PAGE_SIZE (4 * 1014 * 1024) // 4 MB
// Upload buffer generations timeout to dispose
#define DX12_UPLOAD_PAGE_GEN_TIMEOUT DX12_BACK_BUFFER_COUNT
// Upload buffer pages that are not used for a few frames are disposed
#define DX12_UPLOAD_PAGE_NOT_USED_FRAME_TIMEOUT 60
///
/// Uploading data to GPU buffer utility
///
class UploadBufferDX12
{
public:
///
/// Upload buffer allocation
///
struct Allocation
{
///
/// CPU memory address of the allocation start.
///
void* CPUAddress;
///
/// Allocation offset in bytes (from the start of the heap buffer).
///
uint64 Offset;
///
/// Allocation size in bytes
///
uint64 Size;
///
/// GPU virtual memory address of the allocation start.
///
D3D12_GPU_VIRTUAL_ADDRESS GPUAddress;
///
/// Upload buffer page resource that owns that allocation
///
ID3D12Resource* Resource;
///
/// Generation number of that allocation (generally allocation is invalid after one or two generations)
///
uint64 Generation;
};
private:
GPUDeviceDX12* _device;
UploadBufferPageDX12* _currentPage;
uint64 _currentOffset;
uint64 _currentGeneration;
Array> _freePages;
Array> _usedPages;
public:
UploadBufferDX12(GPUDeviceDX12* device);
public:
///
/// Allocates memory for custom data in the buffer.
///
/// Size of the data in bytes
/// Data alignment in buffer in bytes
/// Dynamic location
Allocation Allocate(uint64 size, uint64 align);
///
/// Uploads data to the buffer.
///
/// GPU command list to record upload command to it
/// Destination buffer
/// Destination buffer offset in bytes.
/// Data to allocate
/// Size of the data in bytes
void UploadBuffer(ID3D12GraphicsCommandList* commandList, ID3D12Resource* buffer, uint32 bufferOffset, const void* data, uint64 size);
///
/// Uploads data to the texture.
///
/// GPU command list to record upload command to it
/// Destination texture
/// Data to allocate
/// Source data row pitch value to upload.
/// Source data slice pitch value to upload.
/// Mip map to stream index
/// Texture array index
void UploadTexture(ID3D12GraphicsCommandList* commandList, ID3D12Resource* texture, const void* srcData, uint32 srcRowPitch, uint32 srcSlicePitch, int32 mipIndex, int32 arrayIndex);
public:
void BeginGeneration(uint64 generation);
void ReleaseGPU();
};
#endif