#pragma once #include "DX12Buffer.h" #include "d3dx12.h" #include "DX12CommandQueue.h" using namespace Framework; // Constructor // eSize: length of an element in bytes DX12Buffer::DX12Buffer(int eSize, ID3D12Device5* device, int flags) : DXBuffer(eSize), buffer(0), device(device) { description = new D3D12_RESOURCE_DESC(); ZeroMemory(description, sizeof(D3D12_RESOURCE_DESC)); description->Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; description->Alignment = 0; description->Height = 1; description->DepthOrArraySize = 1; description->MipLevels = 1; description->Format = DXGI_FORMAT_UNKNOWN; description->SampleDesc.Count = 1; description->Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; description->Flags = (D3D12_RESOURCE_FLAGS)flags; } // Destructor DX12Buffer::~DX12Buffer() { if (buffer) buffer->Release(); delete description; } // Copies the data into the buffer if it has changed void DX12Buffer::copyToGPU(__int64 byteCount) { if (!len) return; if (byteCount < 0) byteCount = len; if (description->Width != (unsigned)len) { if (buffer) buffer->Release(); buffer = 0; description->Width = (unsigned)len; } if (!buffer) { D3D12_HEAP_PROPERTIES hprop; hprop.Type = D3D12_HEAP_TYPE_UPLOAD; 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_GENERIC_READ, 0, __uuidof(ID3D12Resource), (void**)&buffer); } if (changed && data) { BYTE* pData; D3D12_RANGE r; r.Begin = 0; r.End = 0; buffer->Map(0, &r, (void**)&pData); memcpy(pData, data, byteCount); r.End = byteCount; buffer->Unmap(0, &r); changed = 0; } } void Framework::DX12Buffer::createBufferWithoutData( D3D12_RESOURCE_STATES states, D3D12_HEAP_TYPE heapType) { if (!len) return; if (description->Width != len) { if (buffer) buffer->Release(); buffer = 0; description->Width = len; } if (!buffer) { D3D12_HEAP_PROPERTIES hprop; hprop.Type = heapType; hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; hprop.CreationNodeMask = 0; hprop.VisibleNodeMask = 0; device->CreateCommittedResource(&hprop, D3D12_HEAP_FLAG_NONE, description, states, 0, __uuidof(ID3D12Resource), (void**)&buffer); } } // Returns the buffer ID3D12Resource* DX12Buffer::zBuffer() const { return buffer; }