gl: Strengthen RAII guarantees for some more objects

This commit is contained in:
kd-11
2026-04-08 02:25:07 +03:00
committed by kd-11
parent b3ac8127ea
commit 59f54caf08
5 changed files with 33 additions and 15 deletions
+2 -2
View File
@@ -220,7 +220,7 @@ namespace gl
return (found->second >= 0); return (found->second >= 0);
} }
auto result = glGetUniformLocation(m_program.id(), name.c_str()); auto result = glGetUniformLocation(m_program->id(), name.c_str());
locations[name] = result; locations[name] = result;
if (location) if (location)
@@ -247,7 +247,7 @@ namespace gl
} }
} }
auto result = glGetUniformLocation(m_program.id(), name.c_str()); auto result = glGetUniformLocation(m_program->id(), name.c_str());
if (result < 0) if (result < 0)
{ {
+17 -5
View File
@@ -55,7 +55,7 @@ namespace gl
const std::string& get_source() const { return source; } const std::string& get_source() const { return source; }
fence get_compile_fence_sync() const { return m_compiled_fence; } const fence& get_compile_fence_sync() const { return m_compiled_fence; }
bool created() const { return m_id != GL_NONE; } bool created() const { return m_id != GL_NONE; }
@@ -106,29 +106,34 @@ namespace gl
class uniforms_t class uniforms_t
{ {
program& m_program; program* m_program = nullptr;
std::unordered_map<std::string, GLint> locations; std::unordered_map<std::string, GLint> locations;
public: public:
uniforms_t(program* program) uniforms_t(program* program)
: m_program(*program) : m_program(program)
{} {}
uniforms_t(uniforms_t&&) noexcept = default;
uniforms_t& operator = (uniforms_t&&) noexcept = default;
void clear() { locations.clear(); } void clear() { locations.clear(); }
bool has_location(const std::string& name, int* location = nullptr); bool has_location(const std::string& name, int* location = nullptr);
GLint location(const std::string& name); GLint location(const std::string& name);
uniform_t operator[](GLint location) { return{ m_program, location }; } uniform_t operator[](GLint location) { return{ *m_program, location }; }
uniform_t operator[](const std::string& name) { return{ m_program, location(name) }; } uniform_t operator[](const std::string& name) { return{ *m_program, location(name) }; }
} uniforms{ this }; } uniforms{ this };
public: public:
program() = default; program() = default;
program(const program&) = delete; program(const program&) = delete;
program& operator = (const program&) = delete;
~program() ~program()
{ {
@@ -138,6 +143,13 @@ namespace gl
} }
} }
void swap(program&& other) noexcept
{
std::swap(m_id, other.m_id);
std::swap(m_fence, other.m_fence);
std::swap(uniforms, other.uniforms);
}
program& recreate() program& recreate()
{ {
remove(); remove();
+7 -7
View File
@@ -283,10 +283,10 @@ namespace gl
const auto range = utils::address_range32::start_length(start, length); const auto range = utils::address_range32::start_length(start, length);
m_barriers.erase(std::remove_if(m_barriers.begin(), m_barriers.end(), [&range](auto& barrier_) m_barriers.erase(std::remove_if(m_barriers.begin(), m_barriers.end(), [&range](auto& barrier_)
{ {
if (barrier_.range.overlaps(range)) if (barrier_->range.overlaps(range))
{ {
barrier_.signal.server_wait_sync(); barrier_->signal.server_wait_sync();
barrier_.signal.destroy(); barrier_->signal.destroy();
return true; return true;
} }
@@ -301,9 +301,9 @@ namespace gl
return; return;
} }
barrier barrier_; auto barrier_ = std::make_unique<barrier>();
barrier_.range = utils::address_range32::start_length(start, length); barrier_->range = utils::address_range32::start_length(start, length);
barrier_.signal.create(); barrier_->signal.create();
m_barriers.emplace_back(barrier_); m_barriers.emplace_back(std::move(barrier_));
} }
} }
+1 -1
View File
@@ -92,7 +92,7 @@ namespace gl
}; };
buffer m_storage; buffer m_storage;
std::vector<barrier> m_barriers; std::vector<std::unique_ptr<barrier>> m_barriers;
u64 m_alloc_pointer = 0; u64 m_alloc_pointer = 0;
void pop_barrier(u32 start, u32 length); void pop_barrier(u32 start, u32 length);
+6
View File
@@ -15,6 +15,12 @@ namespace gl
fence() = default; fence() = default;
~fence() = default; ~fence() = default;
fence(const fence&) = delete;
fence& operator = (const fence&) = delete;
fence(fence&&) noexcept = default;
fence& operator = (fence&&) noexcept = default;
void create() void create()
{ {
m_value = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); m_value = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);