mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-12 02:01:29 -10:00
One of the main points of contention when adding the NanoVG based TrueType text rendering was that it required an external Opengl renderer which may interfere with our rendering engine by changing OpenGL state without telling the rest of the engine. Since the rendering interface for NanoVG is abstract and can be easily replaced, I did just that and replaced it with an implementation that uses only generic graphics functions without relying on the specific graphics API. This will improve the interoperability of NanoVG with our engine and should make later changes to the rendering engine easier since there is no external rendering code which may interfere with our own code.
84 lines
2.0 KiB
C++
84 lines
2.0 KiB
C++
|
|
|
|
#include "UniformBufferManager.h"
|
|
#include "uniform_structs.h"
|
|
|
|
namespace {
|
|
|
|
size_t getElementSize(uniform_block_type type) {
|
|
switch (type) {
|
|
case uniform_block_type::Lights:
|
|
return sizeof(graphics::deferred_light_data);
|
|
case uniform_block_type::ModelData:
|
|
return sizeof(graphics::model_uniform_data);
|
|
case uniform_block_type::NanoVGData:
|
|
return sizeof(graphics::nanovg_draw_data);
|
|
case uniform_block_type::NUM_BLOCK_TYPES:
|
|
default:
|
|
Assertion(false, "Invalid block type encountered!");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
size_t getHeaderSize(uniform_block_type type) {
|
|
switch (type) {
|
|
case uniform_block_type::Lights:
|
|
return 0;
|
|
case uniform_block_type::ModelData:
|
|
case uniform_block_type::NanoVGData:
|
|
return 0;
|
|
case uniform_block_type::NUM_BLOCK_TYPES:
|
|
default:
|
|
Assertion(false, "Invalid block type encountered!");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
namespace graphics {
|
|
namespace util {
|
|
|
|
|
|
UniformBufferManager::UniformBufferManager(uniform_block_type uniform_type) : _type(uniform_type) {
|
|
}
|
|
UniformBuffer* UniformBufferManager::getBuffer() {
|
|
if (!_retiredBuffers.empty()) {
|
|
// Reuse one of the free buffers
|
|
auto buffer = _retiredBuffers.back();
|
|
|
|
// Sanity check
|
|
Assertion(!buffer->isInUse(), "Retired buffer is in use!");
|
|
|
|
_retiredBuffers.pop_back();
|
|
_usedBuffers.push_back(buffer);
|
|
return buffer;
|
|
}
|
|
|
|
// No free buffers available, create a new one
|
|
std::unique_ptr<UniformBuffer> buffer(new UniformBuffer(getElementSize(_type), getHeaderSize(_type)));
|
|
auto ptr = buffer.get();
|
|
_buffers.push_back(std::move(buffer));
|
|
_usedBuffers.push_back(ptr);
|
|
|
|
return ptr;
|
|
}
|
|
void UniformBufferManager::retireBuffers() {
|
|
GR_DEBUG_SCOPE("Retiring buffers of buffer manager");
|
|
|
|
for (auto& buffer : _usedBuffers) {
|
|
if (!buffer->isInUse()) {
|
|
// This buffer will be retired soon so add it to the list before removing it from the used list
|
|
_retiredBuffers.push_back(buffer);
|
|
}
|
|
}
|
|
|
|
// Remove the buffers from the list
|
|
_usedBuffers.erase(std::remove_if(_usedBuffers.begin(), _usedBuffers.end(), [](UniformBuffer* buffer) {
|
|
return !buffer->isInUse();
|
|
}), _usedBuffers.end());
|
|
}
|
|
|
|
}
|
|
}
|