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

View file

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

View file

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

View file

@ -5,6 +5,7 @@
#include "vulkan/shaders/vk_shader.h" #include "vulkan/shaders/vk_shader.h"
class VkStreamBuffer; class VkStreamBuffer;
class VkMatrixBuffer;
class VkStreamBufferWriter class VkStreamBufferWriter
{ {
@ -23,10 +24,27 @@ private:
uint32_t mStreamDataOffset = 0; 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 class VkStreamBuffer
{ {
public: public:
VkStreamBuffer(); VkStreamBuffer(size_t structSize, size_t count);
~VkStreamBuffer(); ~VkStreamBuffer();
uint32_t NextStreamDataBlock(); uint32_t NextStreamDataBlock();
@ -35,24 +53,6 @@ public:
VKDataBuffer* UniformBuffer = nullptr; VKDataBuffer* UniformBuffer = nullptr;
private: private:
uint32_t mBlockSize = 0;
uint32_t mStreamDataOffset = 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(); CreateFanToTrisIndexBuffer();
// To do: move this to HW renderer interface maybe? // To do: move this to HW renderer interface maybe?
MatrixBuffer = new VkMatrixBuffer(); MatrixBuffer = new VkStreamBuffer(sizeof(MatricesUBO), 50000);
StreamBuffer = new VkStreamBuffer(); StreamBuffer = new VkStreamBuffer(sizeof(StreamUBO), 200);
mShaderManager.reset(new VkShaderManager(device)); mShaderManager.reset(new VkShaderManager(device));
mSamplerManager.reset(new VkSamplerManager(device)); mSamplerManager.reset(new VkSamplerManager(device));

View file

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