Added a generic memory allocator for memory needed for a frame

This commit is contained in:
Magnus Norddahl 2017-01-15 21:45:21 +01:00
parent 601ddb270d
commit e02aece40a
4 changed files with 78 additions and 22 deletions

View file

@ -65,10 +65,7 @@ namespace swrenderer
} }
if (!found) if (!found)
{ {
visplane_light *newlight = R_NewPlaneLight(); visplane_light *newlight = RenderMemory::NewObject<visplane_light>();
if (!newlight)
return;
newlight->next = lights; newlight->next = lights;
newlight->lightsource = node->lightsource; newlight->lightsource = node->lightsource;
lights = newlight; lights = newlight;

View file

@ -72,23 +72,43 @@ namespace swrenderer
} }
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
namespace void *RenderMemory::AllocBytes(int size)
{ {
enum { max_plane_lights = 32 * 1024 }; size = (size + 15) / 16 * 16; // 16-byte align
visplane_light plane_lights[max_plane_lights];
int next_plane_light = 0; if (UsedBlocks.empty() || UsedBlocks.back()->Position + size > BlockSize)
} {
if (!FreeBlocks.empty())
{
auto block = std::move(FreeBlocks.back());
block->Position = 0;
FreeBlocks.pop_back();
UsedBlocks.push_back(std::move(block));
}
else
{
UsedBlocks.push_back(std::make_unique<MemoryBlock>());
}
}
auto &block = UsedBlocks.back();
void *data = block->Data + block->Position;
block->Position += size;
visplane_light *R_NewPlaneLight() return data;
{
if (next_plane_light == max_plane_lights)
return nullptr;
return &plane_lights[next_plane_light++];
} }
void R_FreePlaneLights() void RenderMemory::Clear()
{ {
next_plane_light = 0; while (!UsedBlocks.empty())
{
auto block = std::move(UsedBlocks.back());
UsedBlocks.pop_back();
FreeBlocks.push_back(std::move(block));
}
} }
std::vector<std::unique_ptr<RenderMemory::MemoryBlock>> RenderMemory::UsedBlocks;
std::vector<std::unique_ptr<RenderMemory::MemoryBlock>> RenderMemory::FreeBlocks;
} }

View file

@ -13,6 +13,8 @@
#pragma once #pragma once
#include <memory>
namespace swrenderer namespace swrenderer
{ {
struct visplane_light; struct visplane_light;
@ -22,7 +24,43 @@ namespace swrenderer
ptrdiff_t R_NewOpening(ptrdiff_t len); ptrdiff_t R_NewOpening(ptrdiff_t len);
void R_FreeOpenings(); void R_FreeOpenings();
void R_DeinitOpenings(); void R_DeinitOpenings();
visplane_light *R_NewPlaneLight(); // Memory needed for the duration of a frame rendering
void R_FreePlaneLights(); class RenderMemory
{
public:
static void Clear();
template<typename T>
static T *AllocMemory(int size = 1)
{
return (T*)AllocBytes(sizeof(T) * size);
}
template<typename T, typename... Types>
static T *NewObject(Types &&... args)
{
void *ptr = AllocBytes(sizeof(T));
return new (ptr)T(std::forward<Types>(args)...);
}
private:
static void *AllocBytes(int size);
enum { BlockSize = 1024 * 1024 };
struct MemoryBlock
{
MemoryBlock() : Data(new uint8_t[BlockSize]), Position(0) { }
~MemoryBlock() { delete[] Data; }
MemoryBlock(const MemoryBlock &) = delete;
MemoryBlock &operator=(const MemoryBlock &) = delete;
uint8_t *Data;
uint32_t Position;
};
static std::vector<std::unique_ptr<MemoryBlock>> UsedBlocks;
static std::vector<std::unique_ptr<MemoryBlock>> FreeBlocks;
};
} }

View file

@ -117,6 +117,8 @@ namespace swrenderer
PlaneCycles.Reset(); PlaneCycles.Reset();
MaskedCycles.Reset(); MaskedCycles.Reset();
WallScanCycles.Reset(); WallScanCycles.Reset();
RenderMemory::Clear();
Clip3DFloors *clip3d = Clip3DFloors::Instance(); Clip3DFloors *clip3d = Clip3DFloors::Instance();
clip3d->Cleanup(); clip3d->Cleanup();
@ -132,7 +134,6 @@ namespace swrenderer
R_ClearClipSegs(0, viewwidth); R_ClearClipSegs(0, viewwidth);
R_ClearDrawSegs(); R_ClearDrawSegs();
VisiblePlaneList::Instance()->Clear(true); VisiblePlaneList::Instance()->Clear(true);
R_FreePlaneLights();
RenderTranslucentPass::Clear(); RenderTranslucentPass::Clear();
// opening / clipping determination // opening / clipping determination