- add VkMatrixBufferWriter so that both buffers are used same way in VkRenderState

This commit is contained in:
Magnus Norddahl 2019-07-28 17:04:56 +02:00
parent 4adac3fe59
commit 76675a4d61
6 changed files with 45 additions and 54 deletions

View file

@ -373,11 +373,10 @@ void VkRenderState::ApplyPushConstants()
void VkRenderState::ApplyMatrices()
{
auto fb = GetVulkanFrameBuffer();
if (!fb->MatrixBuffer->Write(mModelMatrix, mModelMatrixEnabled, mTextureMatrix, mTextureMatrixEnabled))
if (!mMatrixBufferWriter.Write(mModelMatrix, mModelMatrixEnabled, mTextureMatrix, mTextureMatrixEnabled))
{
WaitForStreamBuffers();
fb->MatrixBuffer->Write(mModelMatrix, mModelMatrixEnabled, mTextureMatrix, mTextureMatrixEnabled);
mMatrixBufferWriter.Write(mModelMatrix, mModelMatrixEnabled, mTextureMatrix, mTextureMatrixEnabled);
}
}
@ -421,7 +420,7 @@ void VkRenderState::ApplyMaterial()
void VkRenderState::ApplyDynamicSet()
{
auto fb = GetVulkanFrameBuffer();
uint32_t matrixOffset = fb->MatrixBuffer->Offset();
uint32_t matrixOffset = mMatrixBufferWriter.Offset();
uint32_t streamDataOffset = mStreamBufferWriter.StreamDataOffset();
if (mViewpointOffset != mLastViewpointOffset || matrixOffset != mLastMatricesOffset || streamDataOffset != mLastStreamDataOffset)
{
@ -442,7 +441,7 @@ void VkRenderState::WaitForStreamBuffers()
GetVulkanFrameBuffer()->WaitForCommands(false);
mApplyCount = 0;
mStreamBufferWriter.Reset();
GetVulkanFrameBuffer()->MatrixBuffer->Reset();
mMatrixBufferWriter.Reset();
}
void VkRenderState::Bind(int bindingpoint, uint32_t offset)
@ -478,7 +477,7 @@ void VkRenderState::EndRenderPass()
void VkRenderState::EndFrame()
{
GetVulkanFrameBuffer()->MatrixBuffer->Reset();
mMatrixBufferWriter.Reset();
mStreamBufferWriter.Reset();
}

View file

@ -99,6 +99,7 @@ protected:
uint32_t mViewpointOffset = 0;
VkStreamBufferWriter mStreamBufferWriter;
VkMatrixBufferWriter mMatrixBufferWriter;
int mLastVertexOffsets[2] = { 0, 0 };
IVertexBuffer *mLastVertexBuffer = nullptr;

View file

@ -4,14 +4,12 @@
#include "vulkan/system/vk_builders.h"
#include "vulkan/renderer/vk_streambuffer.h"
template<typename T>
int UniformBufferAlignedSize(int count) { return ((sizeof(T) + screen->uniformblockalignment - 1) / screen->uniformblockalignment * screen->uniformblockalignment) * count; }
VkStreamBuffer::VkStreamBuffer()
VkStreamBuffer::VkStreamBuffer(size_t structSize, size_t count)
{
auto fb = GetVulkanFrameBuffer();
UniformBuffer = (VKDataBuffer*)fb->CreateDataBuffer(-1, false, false);
UniformBuffer->SetData(UniformBufferAlignedSize<StreamUBO>(200), nullptr, false);
mBlockSize = static_cast<uint32_t>((structSize + screen->uniformblockalignment - 1) / screen->uniformblockalignment * screen->uniformblockalignment);
UniformBuffer = (VKDataBuffer*)GetVulkanFrameBuffer()->CreateDataBuffer(-1, false, false);
UniformBuffer->SetData(mBlockSize * count, nullptr, false);
}
VkStreamBuffer::~VkStreamBuffer()
@ -21,8 +19,8 @@ VkStreamBuffer::~VkStreamBuffer()
uint32_t VkStreamBuffer::NextStreamDataBlock()
{
mStreamDataOffset += sizeof(StreamUBO);
if (mStreamDataOffset + sizeof(StreamUBO) >= UniformBuffer->Size())
mStreamDataOffset += mBlockSize;
if (mStreamDataOffset + (size_t)mBlockSize >= UniformBuffer->Size())
{
mStreamDataOffset = 0;
return 0xffffffff;
@ -61,18 +59,10 @@ void VkStreamBufferWriter::Reset()
/////////////////////////////////////////////////////////////////////////////
VkMatrixBuffer::VkMatrixBuffer()
VkMatrixBufferWriter::VkMatrixBufferWriter()
{
mBuffer = GetVulkanFrameBuffer()->MatrixBuffer;
mIdentityMatrix.loadIdentity();
auto fb = GetVulkanFrameBuffer();
UniformBuffer = (VKDataBuffer*)fb->CreateDataBuffer(-1, false, false);
UniformBuffer->SetData(UniformBufferAlignedSize<MatricesUBO>(50000), nullptr, false);
}
VkMatrixBuffer::~VkMatrixBuffer()
{
delete UniformBuffer;
}
template<typename T>
@ -92,7 +82,7 @@ static void BufferedSet(bool& modified, VSMatrix& dst, const VSMatrix& src)
modified = true;
}
bool VkMatrixBuffer::Write(const VSMatrix& modelMatrix, bool modelMatrixEnabled, const VSMatrix& textureMatrix, bool textureMatrixEnabled)
bool VkMatrixBufferWriter::Write(const VSMatrix& modelMatrix, bool modelMatrixEnabled, const VSMatrix& textureMatrix, bool textureMatrixEnabled)
{
bool modified = (mOffset == 0); // always modified first call
@ -119,17 +109,19 @@ bool VkMatrixBuffer::Write(const VSMatrix& modelMatrix, bool modelMatrixEnabled,
if (modified)
{
if (mOffset + (size_t)UniformBufferAlignedSize<MatricesUBO>(2) >= UniformBuffer->Size())
mOffset = mBuffer->NextStreamDataBlock();
if (mOffset == 0xffffffff)
return false;
mOffset += UniformBufferAlignedSize<MatricesUBO>(1);
memcpy(static_cast<uint8_t*>(UniformBuffer->Memory()) + mOffset, &mMatrices, sizeof(MatricesUBO));
uint8_t* ptr = (uint8_t*)mBuffer->UniformBuffer->Memory();
memcpy(ptr + mOffset, &mMatrices, sizeof(MatricesUBO));
}
return true;
}
void VkMatrixBuffer::Reset()
void VkMatrixBufferWriter::Reset()
{
mOffset = 0;
mBuffer->Reset();
}

View file

@ -5,6 +5,7 @@
#include "vulkan/shaders/vk_shader.h"
class VkStreamBuffer;
class VkMatrixBuffer;
class VkStreamBufferWriter
{
@ -23,10 +24,27 @@ private:
uint32_t mStreamDataOffset = 0;
};
class VkMatrixBufferWriter
{
public:
VkMatrixBufferWriter();
bool Write(const VSMatrix& modelMatrix, bool modelMatrixEnabled, const VSMatrix& textureMatrix, bool textureMatrixEnabled);
void Reset();
uint32_t Offset() const { return mOffset; }
private:
VkStreamBuffer* mBuffer;
MatricesUBO mMatrices = {};
VSMatrix mIdentityMatrix;
uint32_t mOffset = 0;
};
class VkStreamBuffer
{
public:
VkStreamBuffer();
VkStreamBuffer(size_t structSize, size_t count);
~VkStreamBuffer();
uint32_t NextStreamDataBlock();
@ -35,24 +53,6 @@ public:
VKDataBuffer* UniformBuffer = nullptr;
private:
uint32_t mBlockSize = 0;
uint32_t mStreamDataOffset = 0;
};
class VkMatrixBuffer
{
public:
VkMatrixBuffer();
~VkMatrixBuffer();
bool Write(const VSMatrix &modelMatrix, bool modelMatrixEnabled, const VSMatrix& textureMatrix, bool textureMatrixEnabled);
void Reset();
uint32_t Offset() const { return mOffset; }
VKDataBuffer* UniformBuffer = nullptr;
private:
MatricesUBO mMatrices = {};
VSMatrix mIdentityMatrix;
uint32_t mOffset = 0;
};

View file

@ -159,8 +159,8 @@ void VulkanFrameBuffer::InitializeState()
CreateFanToTrisIndexBuffer();
// To do: move this to HW renderer interface maybe?
MatrixBuffer = new VkMatrixBuffer();
StreamBuffer = new VkStreamBuffer();
MatrixBuffer = new VkStreamBuffer(sizeof(MatricesUBO), 50000);
StreamBuffer = new VkStreamBuffer(sizeof(StreamUBO), 200);
mShaderManager.reset(new VkShaderManager(device));
mSamplerManager.reset(new VkSamplerManager(device));

View file

@ -10,7 +10,6 @@ class VkShaderManager;
class VkRenderPassManager;
class VkRenderState;
class VkStreamBuffer;
class VkMatrixBuffer;
class VKDataBuffer;
class VkHardwareTexture;
class VkRenderBuffers;
@ -42,7 +41,7 @@ public:
VKDataBuffer *ViewpointUBO = nullptr;
VKDataBuffer *LightBufferSSO = nullptr;
VkMatrixBuffer *MatrixBuffer = nullptr;
VkStreamBuffer *MatrixBuffer = nullptr;
VkStreamBuffer *StreamBuffer = nullptr;
VKDataBuffer *LightNodes = nullptr;