Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- move streaming uniform buffers out of VkRenderState
- Loading branch information
Showing
8 changed files
with
227 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
|
||
#include "vk_renderstate.h" | ||
#include "vulkan/system/vk_framebuffer.h" | ||
#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() | ||
{ | ||
auto fb = GetVulkanFrameBuffer(); | ||
UniformBuffer = (VKDataBuffer*)fb->CreateDataBuffer(-1, false, false); | ||
UniformBuffer->SetData(UniformBufferAlignedSize<StreamUBO>(200), nullptr, false); | ||
} | ||
|
||
VkStreamBuffer::~VkStreamBuffer() | ||
{ | ||
delete UniformBuffer; | ||
} | ||
|
||
uint32_t VkStreamBuffer::NextStreamDataBlock() | ||
{ | ||
mStreamDataOffset += sizeof(StreamUBO); | ||
if (mStreamDataOffset + sizeof(StreamUBO) >= UniformBuffer->Size()) | ||
{ | ||
mStreamDataOffset = 0; | ||
return 0xffffffff; | ||
} | ||
return mStreamDataOffset; | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
|
||
VkStreamBufferWriter::VkStreamBufferWriter() | ||
{ | ||
mBuffer = GetVulkanFrameBuffer()->StreamBuffer; | ||
} | ||
|
||
bool VkStreamBufferWriter::Write(const StreamData& data) | ||
{ | ||
mDataIndex++; | ||
if (mDataIndex == MAX_STREAM_DATA) | ||
{ | ||
mDataIndex = 0; | ||
mStreamDataOffset = mBuffer->NextStreamDataBlock(); | ||
if (mStreamDataOffset == 0xffffffff) | ||
return false; | ||
} | ||
uint8_t* ptr = (uint8_t*)mBuffer->UniformBuffer->Memory(); | ||
memcpy(ptr + mStreamDataOffset + sizeof(StreamData) * mDataIndex, &data, sizeof(StreamData)); | ||
return true; | ||
} | ||
|
||
void VkStreamBufferWriter::Reset() | ||
{ | ||
mDataIndex = MAX_STREAM_DATA - 1; | ||
mStreamDataOffset = 0; | ||
mBuffer->Reset(); | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
|
||
VkMatrixBuffer::VkMatrixBuffer() | ||
{ | ||
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> | ||
static void BufferedSet(bool& modified, T& dst, const T& src) | ||
{ | ||
if (dst == src) | ||
return; | ||
dst = src; | ||
modified = true; | ||
} | ||
|
||
static void BufferedSet(bool& modified, VSMatrix& dst, const VSMatrix& src) | ||
{ | ||
if (memcmp(dst.get(), src.get(), sizeof(FLOATTYPE) * 16) == 0) | ||
return; | ||
dst = src; | ||
modified = true; | ||
} | ||
|
||
bool VkMatrixBuffer::Write(const VSMatrix& modelMatrix, bool modelMatrixEnabled, const VSMatrix& textureMatrix, bool textureMatrixEnabled) | ||
{ | ||
bool modified = (mOffset == 0); // always modified first call | ||
|
||
if (modelMatrixEnabled) | ||
{ | ||
BufferedSet(modified, mMatrices.ModelMatrix, modelMatrix); | ||
if (modified) | ||
mMatrices.NormalModelMatrix.computeNormalMatrix(modelMatrix); | ||
} | ||
else | ||
{ | ||
BufferedSet(modified, mMatrices.ModelMatrix, mIdentityMatrix); | ||
BufferedSet(modified, mMatrices.NormalModelMatrix, mIdentityMatrix); | ||
} | ||
|
||
if (textureMatrixEnabled) | ||
{ | ||
BufferedSet(modified, mMatrices.TextureMatrix, textureMatrixEnabled); | ||
} | ||
else | ||
{ | ||
BufferedSet(modified, mMatrices.TextureMatrix, mIdentityMatrix); | ||
} | ||
|
||
if (modified) | ||
{ | ||
if (mOffset + (size_t)UniformBufferAlignedSize<MatricesUBO>(2) >= UniformBuffer->Size()) | ||
return false; | ||
|
||
mOffset += UniformBufferAlignedSize<MatricesUBO>(1); | ||
memcpy(static_cast<uint8_t*>(UniformBuffer->Memory()) + mOffset, &mMatrices, sizeof(MatricesUBO)); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void VkMatrixBuffer::Reset() | ||
{ | ||
mOffset = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
#pragma once | ||
|
||
#include "vulkan/system/vk_buffers.h" | ||
#include "vulkan/shaders/vk_shader.h" | ||
|
||
class VkStreamBuffer; | ||
|
||
class VkStreamBufferWriter | ||
{ | ||
public: | ||
VkStreamBufferWriter(); | ||
|
||
bool Write(const StreamData& data); | ||
void Reset(); | ||
|
||
uint32_t DataIndex() const { return mDataIndex; } | ||
uint32_t StreamDataOffset() const { return mStreamDataOffset; } | ||
|
||
private: | ||
VkStreamBuffer* mBuffer; | ||
uint32_t mDataIndex = MAX_STREAM_DATA - 1; | ||
uint32_t mStreamDataOffset = 0; | ||
}; | ||
|
||
class VkStreamBuffer | ||
{ | ||
public: | ||
VkStreamBuffer(); | ||
~VkStreamBuffer(); | ||
|
||
uint32_t NextStreamDataBlock(); | ||
void Reset() { mStreamDataOffset = 0; } | ||
|
||
VKDataBuffer* UniformBuffer = nullptr; | ||
|
||
private: | ||
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; | ||
}; |
Oops, something went wrong.