Skip to content
Permalink
Browse files

- encapsulate the stream data buffer in its own object.

  • Loading branch information...
coelckers committed Jun 9, 2019
1 parent 4a9a7ae commit b7f4a610b34d60faee012353ac079dfe88d54587
@@ -174,7 +174,7 @@ void VkRenderPassManager::UpdateDynamicSet()
update.addBuffer(DynamicSet.get(), 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, fb->ViewpointUBO->mBuffer.get(), 0, sizeof(HWViewpointUniforms));
update.addBuffer(DynamicSet.get(), 1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->LightBufferSSO->mBuffer.get());
update.addBuffer(DynamicSet.get(), 2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, fb->MatricesUBO->mBuffer.get(), 0, sizeof(MatricesUBO));
update.addBuffer(DynamicSet.get(), 3, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, fb->StreamUBO->mBuffer.get(), 0, sizeof(StreamUBO));
update.addBuffer(DynamicSet.get(), 3, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, fb->StreamData.Buffer()->mBuffer.get(), 0, sizeof(StreamUBO));
update.addCombinedImageSampler(DynamicSet.get(), 4, fb->GetBuffers()->Shadowmap.View.get(), fb->GetBuffers()->ShadowmapSampler.get(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
update.updateSets(fb->device);
}
@@ -323,14 +323,9 @@ void VkRenderState::ApplyStreamData()
else
mStreamData.timer = 0.0f;

mDataIndex++;
if (mDataIndex == MAX_STREAM_DATA)
{
mDataIndex = 0;
mStreamDataOffset += sizeof(StreamUBO);
}
uint8_t *ptr = (uint8_t*)fb->StreamUBO->Memory();
memcpy(ptr + mStreamDataOffset + sizeof(StreamData) * mDataIndex, &mStreamData, sizeof(StreamData));
auto index = fb->StreamData.AllocateEntry(mStreamData);
mDataIndex = index.first;
mStreamDataOffset = index.second;
}

void VkRenderState::ApplyPushConstants()
@@ -423,7 +418,7 @@ void VkRenderState::ApplyMatrices()

if (mMatricesOffset + (fb->UniformBufferAlignedSize<MatricesUBO>() << 1) < fb->MatricesUBO->Size())
{
mMatricesOffset += fb->UniformBufferAlignedSize<MatricesUBO>();
mMatricesOffset += (uint32_t)fb->UniformBufferAlignedSize<MatricesUBO>();
memcpy(static_cast<uint8_t*>(fb->MatricesUBO->Memory()) + mMatricesOffset, &mMatrices, sizeof(MatricesUBO));
}
}
@@ -516,8 +511,6 @@ void VkRenderState::EndRenderPass()
void VkRenderState::EndFrame()
{
mMatricesOffset = 0;
mStreamDataOffset = 0;
mDataIndex = -1;
}

void VkRenderState::EnableDrawBuffers(int count)
@@ -94,9 +94,10 @@ class VkRenderState : public FRenderState

uint32_t mLastViewpointOffset = 0xffffffff;
uint32_t mLastMatricesOffset = 0xffffffff;
uint32_t mLastStreamDataOffset = 0xffffffff;
uint32_t mViewpointOffset = 0;
uint32_t mMatricesOffset = 0;

uint32_t mLastStreamDataOffset = 0xffffffff;
uint32_t mDataIndex = -1;
uint32_t mStreamDataOffset = 0;

@@ -18,7 +18,11 @@ struct MatricesUBO
VSMatrix TextureMatrix;
};

#define MAX_STREAM_DATA 256
enum
{
MAX_STREAM_DATA = 256
};


struct StreamUBO
{
@@ -76,6 +76,33 @@ extern bool gpuStatActive;
extern bool keepGpuStatActive;
extern FString gpuStatOutput;


void VulkanStreamUBO::Delete()
{
if (mBuffer) delete mBuffer;
}

void VulkanStreamUBO::Create(VulkanFrameBuffer* owner)
{
mBuffer = (VKDataBuffer*)owner->CreateDataBuffer(-1, false, false);
mBuffer->SetData(owner->UniformBufferAlignedSize<::StreamUBO>() * 200, nullptr, false);
mOuterIndex = mInnerIndex = 0;
}

std::pair<uint32_t, uint32_t> VulkanStreamUBO::AllocateEntry(const StreamData& data)
{
mInnerIndex++;
if (mInnerIndex == MAX_STREAM_DATA)
{
mInnerIndex = 0;
mOuterIndex += sizeof(StreamUBO);
}
uint8_t* ptr = (uint8_t*)mBuffer->Memory();
memcpy(ptr + mInnerIndex + sizeof(StreamData) * mOuterIndex, &data, sizeof(StreamData));
return std::make_pair(mInnerIndex, mOuterIndex);
}


VulkanFrameBuffer::VulkanFrameBuffer(void *hMonitor, bool fullscreen, VulkanDevice *dev) :
Super(hMonitor, fullscreen)
{
@@ -107,7 +134,7 @@ VulkanFrameBuffer::~VulkanFrameBuffer()
PPResource::ResetAll();

delete MatricesUBO;
delete StreamUBO;
StreamData.Delete();
delete mVertexData;
delete mSkyData;
delete mViewpoints;
@@ -159,9 +186,9 @@ void VulkanFrameBuffer::InitializeState()

// To do: move this to HW renderer interface maybe?
MatricesUBO = (VKDataBuffer*)CreateDataBuffer(-1, false, false);
StreamUBO = (VKDataBuffer*)CreateDataBuffer(-1, false, false);
MatricesUBO->SetData(UniformBufferAlignedSize<::MatricesUBO>() * 50000, nullptr, false);
StreamUBO->SetData(UniformBufferAlignedSize<::StreamUBO>() * 200, nullptr, false);
StreamData.Create(this);


mShaderManager.reset(new VkShaderManager(device));
mSamplerManager.reset(new VkSamplerManager(device));
@@ -193,6 +220,7 @@ void VulkanFrameBuffer::Update()

mRenderState->EndRenderPass();
mRenderState->EndFrame();
StreamData.Reset();

Flush3D.Unclock();

@@ -15,6 +15,27 @@ class VkRenderBuffers;
class VkPostprocess;
class SWSceneDrawer;

// Todo: move these buffer definitions to a separate header later.
class VulkanFrameBuffer;
struct StreamData;

class VulkanStreamUBO
{
VKDataBuffer* mBuffer = nullptr;
uint32_t mOuterIndex = 0;
uint32_t mInnerIndex = 0;

public:
void Reset()
{
mInnerIndex = mOuterIndex = 0;
}
void Delete();
void Create(VulkanFrameBuffer* owner);
std::pair<uint32_t, uint32_t> AllocateEntry(const StreamData& data);
VKDataBuffer* Buffer() const { return mBuffer; }
};

class VulkanFrameBuffer : public SystemBaseFrameBuffer
{
typedef SystemBaseFrameBuffer Super;
@@ -39,12 +60,13 @@ class VulkanFrameBuffer : public SystemBaseFrameBuffer
unsigned int GetLightBufferBlockSize() const;

template<typename T>
int UniformBufferAlignedSize() const { return (sizeof(T) + uniformblockalignment - 1) / uniformblockalignment * uniformblockalignment; }
size_t UniformBufferAlignedSize() const { return (sizeof(T) + uniformblockalignment - 1) / uniformblockalignment * uniformblockalignment; }

VKDataBuffer *ViewpointUBO = nullptr;
VKDataBuffer *LightBufferSSO = nullptr;
VKDataBuffer *MatricesUBO = nullptr;
VKDataBuffer *StreamUBO = nullptr;

VulkanStreamUBO StreamData;

VKDataBuffer *LightNodes = nullptr;
VKDataBuffer *LightLines = nullptr;

0 comments on commit b7f4a61

Please sign in to comment.
You can’t perform that action at this time.