| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "RenderAPI/BsGpuParamBlockBuffer.h"
- #include "Managers/BsHardwareBufferManager.h"
- #include "Allocators/BsFrameAlloc.h"
- namespace bs
- {
- GpuParamBlockBuffer::GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage)
- :mUsage(usage), mSize(size), mCachedData(nullptr)
- {
- if (mSize > 0)
- mCachedData = (UINT8*)bs_alloc(mSize);
- memset(mCachedData, 0, mSize);
- }
- GpuParamBlockBuffer::~GpuParamBlockBuffer()
- {
- if (mCachedData != nullptr)
- bs_free(mCachedData);
- }
- void GpuParamBlockBuffer::write(UINT32 offset, const void* data, UINT32 size)
- {
- #if BS_DEBUG_MODE
- if ((offset + size) > mSize)
- {
- BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
- "Available range: 0 .. " + toString(mSize) + ". " \
- "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
- }
- #endif
- memcpy(mCachedData + offset, data, size);
- markCoreDirty();
- }
- void GpuParamBlockBuffer::read(UINT32 offset, void* data, UINT32 size)
- {
- #if BS_DEBUG_MODE
- if ((offset + size) > mSize)
- {
- BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
- "Available range: 0 .. " + toString(mSize) + ". " \
- "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
- }
- #endif
- memcpy(data, mCachedData + offset, size);
- }
- void GpuParamBlockBuffer::zeroOut(UINT32 offset, UINT32 size)
- {
- #if BS_DEBUG_MODE
- if ((offset + size) > mSize)
- {
- BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
- "Available range: 0 .. " + toString(mSize) + ". " \
- "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
- }
- #endif
- memset(mCachedData + offset, 0, size);
- markCoreDirty();
- }
- SPtr<ct::GpuParamBlockBuffer> GpuParamBlockBuffer::getCore() const
- {
- return std::static_pointer_cast<ct::GpuParamBlockBuffer>(mCoreSpecific);
- }
- SPtr<ct::CoreObject> GpuParamBlockBuffer::createCore() const
- {
- return ct::HardwareBufferManager::instance().createGpuParamBlockBufferInternal(mSize, mUsage);
- }
- CoreSyncData GpuParamBlockBuffer::syncToCore(FrameAlloc* allocator)
- {
- UINT8* buffer = allocator->alloc(mSize);
- read(0, buffer, mSize);
- return CoreSyncData(buffer, mSize);
- }
- SPtr<GpuParamBlockBuffer> GpuParamBlockBuffer::create(UINT32 size, GpuParamBlockUsage usage)
- {
- return HardwareBufferManager::instance().createGpuParamBlockBuffer(size, usage);
- }
- namespace ct
- {
- GpuParamBlockBuffer::GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage, GpuDeviceFlags deviceMask)
- :mUsage(usage), mSize(size), mCachedData(nullptr), mGPUBufferDirty(false)
- {
- if (mSize > 0)
- mCachedData = (UINT8*)bs_alloc(mSize);
- memset(mCachedData, 0, mSize);
- }
- GpuParamBlockBuffer::~GpuParamBlockBuffer()
- {
- if (mCachedData != nullptr)
- bs_free(mCachedData);
- }
- void GpuParamBlockBuffer::write(UINT32 offset, const void* data, UINT32 size)
- {
- #if BS_DEBUG_MODE
- if ((offset + size) > mSize)
- {
- BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
- "Available range: 0 .. " + toString(mSize) + ". " \
- "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
- }
- #endif
- memcpy(mCachedData + offset, data, size);
- mGPUBufferDirty = true;
- }
- void GpuParamBlockBuffer::read(UINT32 offset, void* data, UINT32 size)
- {
- #if BS_DEBUG_MODE
- if ((offset + size) > mSize)
- {
- BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
- "Available range: 0 .. " + toString(mSize) + ". " \
- "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
- }
- #endif
- memcpy(data, mCachedData + offset, size);
- }
- void GpuParamBlockBuffer::zeroOut(UINT32 offset, UINT32 size)
- {
- #if BS_DEBUG_MODE
- if ((offset + size) > mSize)
- {
- BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
- "Available range: 0 .. " + toString(mSize) + ". " \
- "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
- }
- #endif
- memset(mCachedData + offset, 0, size);
- mGPUBufferDirty = true;
- }
- void GpuParamBlockBuffer::flushToGPU(UINT32 queueIdx)
- {
- if (mGPUBufferDirty)
- {
- writeToGPU(mCachedData, queueIdx);
- mGPUBufferDirty = false;
- }
- }
- void GpuParamBlockBuffer::syncToCore(const CoreSyncData& data)
- {
- assert(mSize == data.getBufferSize());
- write(0, data.getBuffer(), data.getBufferSize());
- }
- SPtr<GpuParamBlockBuffer> GpuParamBlockBuffer::create(UINT32 size, GpuParamBlockUsage usage,
- GpuDeviceFlags deviceMask)
- {
- return HardwareBufferManager::instance().createGpuParamBlockBuffer(size, usage, deviceMask);
- }
- }
- }
|