#include "DX11Buffer.h" #include "Logging.h" #ifdef WIN32 # include # include # include "d3dx12.h" // Contents of the DX11Buffer class // Constructor // eSize: The length of an element in bytes Framework::DX11Buffer::DX11Buffer(int eSize, ID3D11Device* device, ID3D11DeviceContext* context, int bindFlags, Critical& deviceLock) : DXBuffer(eSize), deviceLock(deviceLock) { buffer = 0; description = new D3D11_BUFFER_DESC(); memset(description, 0, sizeof(description)); description->Usage = D3D11_USAGE_DYNAMIC; description->CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; description->BindFlags = bindFlags; this->device = device; this->context = context; } // Destructor Framework::DX11Buffer::~DX11Buffer() { if (buffer) buffer->Release(); delete description; } // Copies the data into the buffer if it has changed // zRObj: The object used to communicate with the graphics card void Framework::DX11Buffer::copyToGPU(__int64 byteCount) { if (!len) return; if (byteCount < 0) byteCount = len; if (description->ByteWidth < (unsigned)len) { if (buffer) buffer->Release(); buffer = 0; } if (!buffer) { description->ByteWidth = (unsigned)len; deviceLock.lock(); device->CreateBuffer(description, 0, &buffer); deviceLock.unlock(); if (data) changed = 1; } if (changed) { HRESULT res; D3D11_MAPPED_SUBRESOURCE map; deviceLock.lock(); if ((description->Usage | D3D11_USAGE_DYNAMIC) == description->Usage) res = context->Map( buffer, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &map); else res = context->Map(buffer, 0, D3D11_MAP::D3D11_MAP_WRITE, 0, &map); deviceLock.unlock(); if (res == S_OK) { memcpy(map.pData, data, byteCount); deviceLock.lock(); context->Unmap(buffer, 0); deviceLock.unlock(); changed = 0; } else { Logging::error() << "Could not update buffer: " << std::hex << res << std::endl; } } } // Returns the buffer ID3D11Buffer* Framework::DX11Buffer::zBuffer() const { return buffer; } // Contents of the DXStructuredBuffer class // Constructor // eSize: The length of an element in bytes Framework::DX11StructuredBuffer::DX11StructuredBuffer(int eSize, ID3D11Device* device, ID3D11DeviceContext* context, Critical& deviceLock) : DX11Buffer(eSize, device, context, D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE, deviceLock) { description->MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; description->StructureByteStride = eSize; description->Usage = D3D11_USAGE_DEFAULT; view = 0; } // Destructor Framework::DX11StructuredBuffer::~DX11StructuredBuffer() { if (view) view->Release(); } // Copies the data into the buffer if it has changed // zRObj: The object used to communicate with the graphics card void Framework::DX11StructuredBuffer::copyToGPU(__int64 byteCount) { ID3D11Buffer* old = buffer; DX11Buffer::copyToGPU(byteCount); if (buffer != old) { if (view) view->Release(); D3D11_SHADER_RESOURCE_VIEW_DESC desc = {}; desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX; desc.BufferEx.FirstElement = 0; desc.Format = DXGI_FORMAT_UNKNOWN; desc.BufferEx.NumElements = description->ByteWidth / description->StructureByteStride; deviceLock.lock(); device->CreateShaderResourceView(buffer, &desc, &view); deviceLock.unlock(); } } // Returns the used shader resource view Framework::DX11StructuredBuffer::operator ID3D11ShaderResourceView*() const { return view; } #endif