Proper move, reset & null-construct for gfx::program

This commit is contained in:
Nikita Lisitsa 2021-07-04 23:17:33 +03:00
parent 4139c9b5c0
commit 25153001c0
2 changed files with 43 additions and 1 deletions

View file

@ -19,12 +19,23 @@ namespace psemek::gfx
program(std::string_view vertex_source, std::string_view fragment_source);
program(std::string_view vertex_source, std::string_view geometry_source, std::string_view fragment_source);
program(program && other);
program & operator = (program && other);
~program();
static program null()
{
return program(nullptr);
}
GLuint id() const;
void bind() const;
void reset();
GLint location(char const * name) const;
struct uniform_proxy
@ -104,6 +115,10 @@ namespace psemek::gfx
private:
GLuint program_;
mutable std::unordered_map<std::string, GLint> uniforms_;
program(std::nullptr_t)
: program_{0}
{}
};
template <typename T, std::size_t D>

View file

@ -322,9 +322,30 @@ namespace psemek::gfx
program_ = create_program({{gl::VERTEX_SHADER, vertex_source}, {gl::GEOMETRY_SHADER, geometry_source}, {gl::FRAGMENT_SHADER, fragment_source}});
}
program::program(program && other)
: program_(other.program_)
, uniforms_(std::move(other.uniforms_))
{
other.program_ = 0;
}
program & program::operator = (program && other)
{
if (this == &other)
return *this;
reset();
program_ = other.program_;
uniforms_ = std::move(other.uniforms_);
other.program_ = 0;
return *this;
}
program::~program()
{
gl::DeleteProgram(program_);
reset();
}
GLuint program::id() const
@ -337,6 +358,12 @@ namespace psemek::gfx
gl::UseProgram(program_);
}
void program::reset()
{
gl::DeleteProgram(program_);
program_ = 0;
}
GLint program::location(char const * name) const
{
auto it = uniforms_.find(name);