Skip to content

Commit

Permalink
Merge pull request pioneerspacesim#5970 from Web-eWorks/microfixes
Browse files Browse the repository at this point in the history
Allocate light data UBO from per-frame allocator, misc. fixes and cleanup
  • Loading branch information
Webster Sheets authored Nov 17, 2024
2 parents a841814 + 827d42f commit e03e743
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ if (USE_TIME_TRACE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftime-trace")
endif()

option(USE_ASAN "Use -fsanitize=asan when compiling (requires Clang)" OFF)
option(USE_ASAN "Use -fsanitize=address when compiling (requires Clang)" OFF)
if (USE_ASAN)
add_compile_options(
-g
Expand Down
18 changes: 13 additions & 5 deletions contrib/profiler/Profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,11 @@ namespace Profiler {
template <typename Value>
struct HashTable {
public:
HashTable() {
mNumChildren = 0;
HashTable() :
mBucketCount(0),
mNumChildren(0),
mBuckets(nullptr)
{
Resize(2);
}

Expand Down Expand Up @@ -1050,6 +1053,8 @@ namespace Profiler {
};

void Init(const char *dir) {
frameTable = new HashTable<Entry>();

firstThreadDump = true;
time_t now;
time( &now );
Expand All @@ -1067,7 +1072,7 @@ namespace Profiler {

void PrintThread( Caller *r ) {
root = r;
SharedPrinter printer(&frameTable, f, r);
SharedPrinter printer(frameTable, f, r);
}

void PrintAccumulated( Caller * ) {
Expand All @@ -1076,7 +1081,7 @@ namespace Profiler {

void PrintZone( const Zone *z, f64 cyclesToTime, bool isLast ) {
u64 at = z->time * cyclesToTime;
Entry *ent = frameTable.Find(z->str());
Entry *ent = frameTable->Find(z->str());
fprintf( f, "{\"type\":\"%c\",\"frame\":%d,\"at\":%lld}%c", (z->type == ZoneType::ZoneEnter ? 'O' : 'C'),
(ent ? ent->index : 0), at, (isLast ? ' ' : ','));
}
Expand Down Expand Up @@ -1112,11 +1117,14 @@ namespace Profiler {
fprintf( f, "]}\n");
fflush( f );
fclose( f );

delete frameTable;
frameTable = 0;
}

protected:
FILE *f;
HashTable<Entry> frameTable;
HashTable<Entry> *frameTable;
char timeFormat[256], fileFormat[4096];
bool firstThreadDump;
};
Expand Down
2 changes: 1 addition & 1 deletion data/pigui/modules/station-view/04-shipMarket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ function FormatAndCompareShips:draw_accel_cell(desc, thrustKey, massKey )
end

function FormatAndCompareShips:draw_deltav_cell(desc, massNumeratorKey, massDenominatorKey)
local deltavA = self.def.effectiveExhaustVelocity * math.log( self:get_value(massNumeratorKey) / self.b:get_value(massDenominatorKey) )
local deltavA = self.def.effectiveExhaustVelocity * math.log( self:get_value(massNumeratorKey) / self:get_value(massDenominatorKey) )
local deltavB = self.b.def.effectiveExhaustVelocity * math.log( self.b:get_value(massNumeratorKey) / self.b:get_value(massDenominatorKey) )

local function fmt( v )
Expand Down
8 changes: 4 additions & 4 deletions src/graphics/opengl/MaterialGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ namespace Graphics {
PROFILE_SCOPED()

if (m_descriptor.lighting) {
UniformBuffer *lightBuffer = m_renderer->GetLightUniformBuffer();
SetBuffer(s_lightDataName, { lightBuffer, 0, lightBuffer->GetSize() });
assert(m_renderer->GetLightUniformBuffer().buffer != nullptr);
SetBuffer(s_lightDataName, m_renderer->GetLightUniformBuffer());

float intensity[4] = { 0.f, 0.f, 0.f, 0.f };
for (uint32_t i = 0; i < m_renderer->GetNumLights(); i++)
Expand All @@ -111,7 +111,7 @@ namespace Graphics {
// this should always be present, but just in case...
if (m_perDrawBinding != Shader::InvalidBinding) {
auto buffer = m_renderer->GetDrawUniformBuffer(sizeof(DrawDataBlock));
BufferBinding<UniformBuffer> binding;
BufferBinding<Graphics::UniformBuffer> binding;

auto dataBlock = buffer->Allocate<DrawDataBlock>(binding);
dataBlock->diffuse = this->diffuse.ToColor4f();
Expand All @@ -127,7 +127,7 @@ namespace Graphics {
dataBlock->uViewMatrixInverse = mv.Inverse();
dataBlock->uViewProjectionMatrix = proj * mv;

SetBuffer(s_drawDataName, { binding.buffer, binding.offset, binding.size });
SetBuffer(s_drawDataName, binding);
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/graphics/opengl/RendererGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ namespace Graphics {
m_drawUniformBuffers.reserve(8);
GetDrawUniformBuffer(0);

m_lightUniformBuffer.Reset(new OGL::UniformBuffer(sizeof(LightData) * TOTAL_NUM_LIGHTS, BUFFER_USAGE_DYNAMIC));
m_lightUniformBuffer = {};
}

RendererOGL::~RendererOGL()
Expand All @@ -296,7 +296,7 @@ namespace Graphics {
buffer.reset();
}

m_lightUniformBuffer.Reset();
m_lightUniformBuffer = {};

s_DynamicDrawBufferMap.clear();

Expand Down Expand Up @@ -560,6 +560,8 @@ namespace Graphics {
stat.SetStatCount(Stats::STAT_NUM_RENDER_STATES, m_renderStateCache->m_stateDescCache.size());
stat.SetStatCount(Stats::STAT_NUM_SHADER_PROGRAMS, numShaderPrograms);

m_lightUniformBuffer = {};

return true;
}

Expand Down Expand Up @@ -769,8 +771,11 @@ namespace Graphics {

m_numLights = numlights;
m_numDirLights = 0;

using LightUBO = LightData[TOTAL_NUM_LIGHTS];

// ScopedMap will be released at the end of the function
auto lightData = m_lightUniformBuffer->Map<LightData>(BufferMapMode::BUFFER_MAP_WRITE);
auto lightData = GetDrawUniformBuffer(sizeof(LightUBO))->Allocate<LightUBO>(m_lightUniformBuffer);
assert(lightData.isValid());

for (Uint32 i = 0; i < numlights; i++) {
Expand All @@ -785,7 +790,7 @@ namespace Graphics {
assert(m_numDirLights <= TOTAL_NUM_LIGHTS);

// Update the GPU-side light data buffer
LightData &gpuLight = lightData.data()[i];
LightData &gpuLight = (*lightData.data())[i];
gpuLight.diffuse = l.GetDiffuse().ToColor4f();
gpuLight.specular = l.GetSpecular().ToColor4f();
gpuLight.position = l.GetPosition();
Expand Down Expand Up @@ -1162,9 +1167,9 @@ namespace Graphics {
return CreateMeshObject(vertexBuffer, indexBuffer);
}

OGL::UniformBuffer *RendererOGL::GetLightUniformBuffer()
const BufferBinding<UniformBuffer> &RendererOGL::GetLightUniformBuffer()
{
return m_lightUniformBuffer.Get();
return m_lightUniformBuffer;
}

OGL::UniformLinearBuffer *RendererOGL::GetDrawUniformBuffer(Uint32 size)
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/opengl/RendererGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace Graphics {

virtual const RenderStateDesc &GetMaterialRenderState(const Graphics::Material *m) override final;

OGL::UniformBuffer *GetLightUniformBuffer();
const BufferBinding<UniformBuffer> &GetLightUniformBuffer();
OGL::UniformLinearBuffer *GetDrawUniformBuffer(Uint32 size);
OGL::RenderStateCache *GetStateCache() { return m_renderStateCache.get(); }

Expand Down Expand Up @@ -141,7 +141,7 @@ namespace Graphics {
std::vector<std::pair<std::string, OGL::Shader *>> m_shaders;
std::vector<std::unique_ptr<OGL::UniformLinearBuffer>> m_drawUniformBuffers;
std::unique_ptr<OGL::RenderStateCache> m_renderStateCache;
RefCountedPtr<OGL::UniformBuffer> m_lightUniformBuffer;
BufferBinding<UniformBuffer> m_lightUniformBuffer;
bool m_useNVDepthRanged;
OGL::RenderTarget *m_activeRenderTarget = nullptr;
std::unique_ptr<OGL::CommandList> m_drawCommandList;
Expand Down
5 changes: 3 additions & 2 deletions src/graphics/opengl/UniformBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void UniformBuffer::Unmap()

void UniformBuffer::BufferData(const size_t size, void *data)
{
PROFILE_SCOPED()
assert(m_mapMode == BUFFER_MAP_NONE);
glBindBuffer(GL_UNIFORM_BUFFER, m_buffer);
glBufferSubData(GL_UNIFORM_BUFFER, 0, size, data);
Expand All @@ -55,6 +56,7 @@ void UniformBuffer::Release()

void *UniformBuffer::MapInternal(BufferMapMode mode)
{
PROFILE_SCOPED()
assert(m_mapMode == BUFFER_MAP_NONE);
glBindBuffer(GL_UNIFORM_BUFFER, m_buffer);
void *data = glMapBuffer(GL_UNIFORM_BUFFER, (mode == BUFFER_MAP_READ) ? GL_READ_ONLY : GL_WRITE_ONLY);
Expand Down Expand Up @@ -140,9 +142,8 @@ BufferBinding<UniformBuffer> UniformLinearBuffer::Allocate(void *data, size_t si
return { this, offset, uint32_t(size) };
}

void *UniformLinearBuffer::AllocInternal(size_t size, BufferBinding<UniformBuffer> &outBinding)
void *UniformLinearBuffer::AllocInternal(size_t size, BufferBinding<Graphics::UniformBuffer> &outBinding)
{
PROFILE_SCOPED()
assert(m_mapMode == BUFFER_MAP_NONE);
m_mapMode = BUFFER_MAP_WRITE;

Expand Down
4 changes: 2 additions & 2 deletions src/graphics/opengl/UniformBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace Graphics {
uint32_t NumAllocs() const { return m_numAllocs; }

template <typename T>
ScopedMapping<T> Allocate(BufferBinding<UniformBuffer> &outBinding)
ScopedMapping<T> Allocate(BufferBinding<Graphics::UniformBuffer> &outBinding)
{
assert(m_mapMode == BUFFER_MAP_NONE);
return ScopedMapping<T>(AllocInternal(sizeof(T), outBinding), this);
Expand All @@ -74,7 +74,7 @@ namespace Graphics {
using UniformBuffer::BufferData;
using UniformBuffer::Map;

void *AllocInternal(size_t size, BufferBinding<UniformBuffer> &outBinding);
void *AllocInternal(size_t size, BufferBinding<Graphics::UniformBuffer> &outBinding);

// cache individual allocations into a single buffer and upload to
// the GPU in one large chunk.
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/opengl/VertexBufferGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ namespace Graphics {

matrix4x4f *InstanceBuffer::Map(BufferMapMode mode)
{
PROFILE_SCOPED()
assert(mode != BUFFER_MAP_NONE); //makes no sense
assert(m_mapMode == BUFFER_MAP_NONE); //must not be currently mapped
m_mapMode = mode;
Expand All @@ -556,6 +557,7 @@ namespace Graphics {

void InstanceBuffer::Unmap()
{
PROFILE_SCOPED()
assert(m_mapMode != BUFFER_MAP_NONE); //not currently mapped

if (GetUsage() == BUFFER_USAGE_STATIC) {
Expand Down
2 changes: 2 additions & 0 deletions src/scenegraph/StaticGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ namespace SceneGraph {
Graphics::InstanceBuffer *ib = m_instBuffer.Get();
matrix4x4f *pBuffer = ib->Map(Graphics::BUFFER_MAP_WRITE);
if (pBuffer) {
PROFILE_SCOPED_DESC("Copy Instance Data")

// Copy the transforms into the buffer
for (const matrix4x4f &mt : trans) {
(*pBuffer) = mt;
Expand Down

0 comments on commit e03e743

Please sign in to comment.