Files
o3de/Gems/Atom/Feature/Common/Code/Source/Utils/GpuBufferHandler.cpp
T
Ken Pruiksma 2f1346c719 Terrain Feature Processor separated into several classes. Macro materials abstracted from meshes. (#6350)
* Breaking up terrain FP wip - macro materials decoupled from meshes. Mesh creation and rendering pulled out from MeshFeatureProcessor into TerrainMeshManager. Stubs added for macro and detail material managers.

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Separated macro material management from the terrain feature processor. Also separated bindless image array handling from terrain feature processor.

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Detail materials separated from terrain feature processor. Also pulled out Aabb2i and Vector2i into their own simple classes

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Changed some classes so that when the SRG changes the classes don't need to be completely reinitialized and can instead just update their indices and push data to the new srg. Fixed an issue where MacroMaterialData wasn't being exposed to ScriptCanvas. Terrain shader reloads should now work correctly. Also added some debug logging to help catch an issue where sometimes detail materials don't seem to load.

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Terrain PR reveiw updates

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Some small PR review fixes. More comments in TerrainDetailMaterialManager.h

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Fixing unused variable causing clang failure

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Fixing unused variable in release.

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Fixing a forward declare that oddly didn't work on linux.

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Fixing linux missing include... not sure why only linux failed on this.

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>

* Adding missing include

Signed-off-by: Ken Pruiksma <pruiksma@amazon.com>
2021-12-29 16:38:28 -06:00

103 lines
3.6 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <Atom/Feature/Utils/GpuBufferHandler.h>
#include <Atom/RHI/Buffer.h>
#include <Atom/RHI/Factory.h>
#include <Atom/RPI.Public/Buffer/BufferSystemInterface.h>
#include <Atom/Utils/Utils.h>
#include <cinttypes>
namespace AZ
{
namespace Render
{
[[maybe_unused]] static const char* ClassName = "GpuBufferHandler";
static const uint32_t BufferMinSize = 1 << 16; // Min 64Kb.
GpuBufferHandler::GpuBufferHandler(const Descriptor& descriptor)
{
m_elementSize = descriptor.m_elementSize;
m_elementCount = 0;
m_bufferIndex = descriptor.m_srgLayout->FindShaderInputBufferIndex(Name(descriptor.m_bufferSrgName));
AZ_Error(ClassName, m_bufferIndex.IsValid(), "Unable to find %s in %s shader resource group.", descriptor.m_bufferSrgName.c_str(), descriptor.m_srgLayout->GetName().GetCStr());
if (!descriptor.m_elementCountSrgName.empty())
{
m_elementCountIndex = descriptor.m_srgLayout->FindShaderInputConstantIndex(Name(descriptor.m_elementCountSrgName));
AZ_Error(ClassName, m_elementCountIndex.IsValid(), "Unable to find %s in %s shader resource group.", descriptor.m_elementCountSrgName.c_str(), descriptor.m_srgLayout->GetName().GetCStr());
}
if (m_bufferIndex.IsValid())
{
uint32_t byteCount = RHI::NextPowerOfTwo(GetMax<uint32_t>(BufferMinSize, m_elementCount * m_elementSize));
RPI::CommonBufferDescriptor desc;
desc.m_poolType = RPI::CommonBufferPoolType::ReadOnly;
desc.m_bufferName = descriptor.m_bufferName;
desc.m_byteCount = byteCount;
desc.m_elementSize = descriptor.m_elementSize;
m_buffer = RPI::BufferSystemInterface::Get()->CreateBufferFromCommonPool(desc);
}
}
bool GpuBufferHandler::IsValid() const
{
// Consider this is valid if the buffer is valid.
return m_buffer;
}
void GpuBufferHandler::Release()
{
m_buffer = nullptr;
m_bufferIndex.Reset();
m_elementCountIndex.Reset();
}
bool GpuBufferHandler::UpdateBuffer(uint32_t elementCount, const void* data)
{
if (!IsValid())
{
return false;
}
m_elementCount = elementCount;
AZ::u64 currentByteCount = m_buffer->GetBufferSize();
uint32_t dataSize = elementCount * m_elementSize;
if (dataSize > currentByteCount)
{
uint32_t byteCount = RHI::NextPowerOfTwo(GetMax<uint32_t>(BufferMinSize, dataSize));
m_buffer->Resize(byteCount);
}
if (dataSize > 0)
{
return m_buffer->UpdateData(data, dataSize, 0);
}
return true;
}
void GpuBufferHandler::UpdateSrg(RPI::ShaderResourceGroup* srg) const
{
if (m_bufferIndex.IsValid())
{
srg->SetBufferView(m_bufferIndex, m_buffer->GetBufferView());
}
if (m_elementCountIndex.IsValid())
{
srg->SetConstant<uint32_t>(m_elementCountIndex, m_elementCount);
}
}
} // namespace Render
} // namespace AZ