mirror of
https://github.com/ZDoom/gzdoom.git
synced 2024-11-14 08:31:23 +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)
|
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;
|
||||||
|
|
|
@ -73,22 +73,42 @@ 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>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
visplane_light *R_NewPlaneLight()
|
auto &block = UsedBlocks.back();
|
||||||
{
|
void *data = block->Data + block->Position;
|
||||||
if (next_plane_light == max_plane_lights)
|
block->Position += size;
|
||||||
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
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace swrenderer
|
namespace swrenderer
|
||||||
{
|
{
|
||||||
struct visplane_light;
|
struct visplane_light;
|
||||||
|
@ -23,6 +25,42 @@ namespace swrenderer
|
||||||
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;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,6 +118,8 @@ namespace swrenderer
|
||||||
MaskedCycles.Reset();
|
MaskedCycles.Reset();
|
||||||
WallScanCycles.Reset();
|
WallScanCycles.Reset();
|
||||||
|
|
||||||
|
RenderMemory::Clear();
|
||||||
|
|
||||||
Clip3DFloors *clip3d = Clip3DFloors::Instance();
|
Clip3DFloors *clip3d = Clip3DFloors::Instance();
|
||||||
clip3d->Cleanup();
|
clip3d->Cleanup();
|
||||||
clip3d->ResetClip(); // reset clips (floor/ceiling)
|
clip3d->ResetClip(); // reset clips (floor/ceiling)
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue