mirror of
https://github.com/ZDoom/gzdoom.git
synced 2024-11-11 15:21:51 +00:00
Added a generic memory allocator for memory needed for a frame
This commit is contained in:
parent
601ddb270d
commit
e02aece40a
4 changed files with 78 additions and 22 deletions
|
@ -65,10 +65,7 @@ namespace swrenderer
|
|||
}
|
||||
if (!found)
|
||||
{
|
||||
visplane_light *newlight = R_NewPlaneLight();
|
||||
if (!newlight)
|
||||
return;
|
||||
|
||||
visplane_light *newlight = RenderMemory::NewObject<visplane_light>();
|
||||
newlight->next = lights;
|
||||
newlight->lightsource = node->lightsource;
|
||||
lights = newlight;
|
||||
|
|
|
@ -72,23 +72,43 @@ namespace swrenderer
|
|||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace
|
||||
|
||||
void *RenderMemory::AllocBytes(int size)
|
||||
{
|
||||
enum { max_plane_lights = 32 * 1024 };
|
||||
visplane_light plane_lights[max_plane_lights];
|
||||
int next_plane_light = 0;
|
||||
}
|
||||
size = (size + 15) / 16 * 16; // 16-byte align
|
||||
|
||||
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()
|
||||
{
|
||||
if (next_plane_light == max_plane_lights)
|
||||
return nullptr;
|
||||
return &plane_lights[next_plane_light++];
|
||||
return data;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace swrenderer
|
||||
{
|
||||
struct visplane_light;
|
||||
|
@ -22,7 +24,43 @@ namespace swrenderer
|
|||
ptrdiff_t R_NewOpening(ptrdiff_t len);
|
||||
void R_FreeOpenings();
|
||||
void R_DeinitOpenings();
|
||||
|
||||
visplane_light *R_NewPlaneLight();
|
||||
void R_FreePlaneLights();
|
||||
|
||||
// Memory needed for the duration of a frame rendering
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -117,6 +117,8 @@ namespace swrenderer
|
|||
PlaneCycles.Reset();
|
||||
MaskedCycles.Reset();
|
||||
WallScanCycles.Reset();
|
||||
|
||||
RenderMemory::Clear();
|
||||
|
||||
Clip3DFloors *clip3d = Clip3DFloors::Instance();
|
||||
clip3d->Cleanup();
|
||||
|
@ -132,7 +134,6 @@ namespace swrenderer
|
|||
R_ClearClipSegs(0, viewwidth);
|
||||
R_ClearDrawSegs();
|
||||
VisiblePlaneList::Instance()->Clear(true);
|
||||
R_FreePlaneLights();
|
||||
RenderTranslucentPass::Clear();
|
||||
|
||||
// opening / clipping determination
|
||||
|
|
Loading…
Reference in a new issue