Manage the light buffer in the backend

This commit is contained in:
Magnus Norddahl 2023-04-30 03:43:38 +02:00
commit f2585a3b17
23 changed files with 97 additions and 267 deletions

View file

@ -32,7 +32,7 @@
#include "hw_skydome.h"
#include "hw_viewpointuniforms.h"
#include "hw_lightbuffer.h"
#include "hw_dynlightdata.h"
#include "hw_cvars.h"
#include "hw_clock.h"
#include "flatvertices.h"
@ -503,10 +503,63 @@ void VkRenderState::SetViewpoint(int index)
mNeedApply = true;
}
int VkRenderState::UploadLights(const FDynLightData& data)
{
auto buffers = fb->GetBufferManager();
// All meaasurements here are in vec4's.
int size0 = data.arrays[0].Size() / 4;
int size1 = data.arrays[1].Size() / 4;
int size2 = data.arrays[2].Size() / 4;
int totalsize = size0 + size1 + size2 + 1;
if (totalsize > buffers->Lightbuffer.Count)
{
int diff = totalsize - buffers->Lightbuffer.Count;
size2 -= diff;
if (size2 < 0)
{
size1 += size2;
size2 = 0;
}
if (size1 < 0)
{
size0 += size1;
size1 = 0;
}
totalsize = size0 + size1 + size2 + 1;
}
if (totalsize <= 1) return -1; // there are no lights
int thisindex = buffers->Lightbuffer.UploadIndex;
buffers->Lightbuffer.UploadIndex += totalsize;
float parmcnt[] = { 0, float(size0), float(size0 + size1), float(size0 + size1 + size2) };
if (thisindex + totalsize <= buffers->Lightbuffer.Count)
{
float* copyptr = (float*)buffers->Lightbuffer.SSO->Memory() + thisindex * 4;
memcpy(&copyptr[0], parmcnt, sizeof(FVector4));
memcpy(&copyptr[4], &data.arrays[0][0], size0 * sizeof(FVector4));
memcpy(&copyptr[4 + 4 * size0], &data.arrays[1][0], size1 * sizeof(FVector4));
memcpy(&copyptr[4 + 4 * (size0 + size1)], &data.arrays[2][0], size2 * sizeof(FVector4));
return thisindex;
}
else
{
return -1; // Buffer is full. Since it is being used live at the point of the upload we cannot do much here but to abort.
}
}
void VkRenderState::BeginFrame()
{
mMaterial.Reset();
mApplyCount = 0;
fb->GetBufferManager()->Viewpoint.UploadIndex = 0;
fb->GetBufferManager()->Lightbuffer.UploadIndex = 0;
}
void VkRenderState::EndRenderPass()