Add GlContext base class for MapContext

This commit is contained in:
Dan Paulat 2022-10-02 23:44:09 -05:00
parent a45e996872
commit d84a618d3d
7 changed files with 115 additions and 57 deletions

View file

@ -41,9 +41,11 @@ set(UI_MAIN source/scwx/qt/main/main_window.ui)
set(HDR_CONFIG source/scwx/qt/config/radar_site.hpp) set(HDR_CONFIG source/scwx/qt/config/radar_site.hpp)
set(SRC_CONFIG source/scwx/qt/config/radar_site.cpp) set(SRC_CONFIG source/scwx/qt/config/radar_site.cpp)
set(HDR_GL source/scwx/qt/gl/gl.hpp set(HDR_GL source/scwx/qt/gl/gl.hpp
source/scwx/qt/gl/gl_context.hpp
source/scwx/qt/gl/shader_program.hpp source/scwx/qt/gl/shader_program.hpp
source/scwx/qt/gl/text_shader.hpp) source/scwx/qt/gl/text_shader.hpp)
set(SRC_GL source/scwx/qt/gl/shader_program.cpp set(SRC_GL source/scwx/qt/gl/gl_context.cpp
source/scwx/qt/gl/shader_program.cpp
source/scwx/qt/gl/text_shader.cpp) source/scwx/qt/gl/text_shader.cpp)
set(HDR_GL_DRAW source/scwx/qt/gl/draw/draw_item.hpp set(HDR_GL_DRAW source/scwx/qt/gl/draw/draw_item.hpp
source/scwx/qt/gl/draw/geo_line.hpp source/scwx/qt/gl/draw/geo_line.hpp

View file

@ -0,0 +1,64 @@
#include <scwx/qt/gl/gl_context.hpp>
#include <scwx/util/hash.hpp>
namespace scwx
{
namespace qt
{
namespace gl
{
class GlContext::Impl
{
public:
explicit Impl() : gl_ {}, shaderProgramMap_ {}, shaderProgramMutex_ {} {}
~Impl() {}
gl::OpenGLFunctions gl_;
std::unordered_map<std::pair<std::string, std::string>,
std::shared_ptr<gl::ShaderProgram>,
util::hash<std::pair<std::string, std::string>>>
shaderProgramMap_;
std::mutex shaderProgramMutex_;
};
GlContext::GlContext() : p(std::make_unique<Impl>()) {}
GlContext::~GlContext() = default;
GlContext::GlContext(GlContext&&) noexcept = default;
GlContext& GlContext::operator=(GlContext&&) noexcept = default;
gl::OpenGLFunctions& GlContext::gl()
{
return p->gl_;
}
std::shared_ptr<gl::ShaderProgram>
GlContext::GetShaderProgram(const std::string& vertexPath,
const std::string& fragmentPath)
{
const std::pair<std::string, std::string> key {vertexPath, fragmentPath};
std::shared_ptr<gl::ShaderProgram> shaderProgram;
std::unique_lock lock(p->shaderProgramMutex_);
auto it = p->shaderProgramMap_.find(key);
if (it == p->shaderProgramMap_.end())
{
shaderProgram = std::make_shared<gl::ShaderProgram>(p->gl_);
shaderProgram->Load(vertexPath, fragmentPath);
p->shaderProgramMap_[key] = shaderProgram;
}
else
{
shaderProgram = it->second;
}
return shaderProgram;
}
} // namespace gl
} // namespace qt
} // namespace scwx

View file

@ -0,0 +1,39 @@
#pragma once
#include <scwx/qt/gl/gl.hpp>
#include <scwx/qt/gl/shader_program.hpp>
namespace scwx
{
namespace qt
{
namespace gl
{
class GlContext
{
public:
explicit GlContext();
virtual ~GlContext();
GlContext(const GlContext&) = delete;
GlContext& operator=(const GlContext&) = delete;
GlContext(GlContext&&) noexcept;
GlContext& operator=(GlContext&&) noexcept;
gl::OpenGLFunctions& gl();
std::shared_ptr<gl::ShaderProgram>
GetShaderProgram(const std::string& vertexPath,
const std::string& fragmentPath);
private:
class Impl;
std::unique_ptr<Impl> p;
};
} // namespace gl
} // namespace qt
} // namespace scwx

View file

@ -19,7 +19,7 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class TextShaderImpl class TextShaderImpl
{ {
public: public:
explicit TextShaderImpl(std::shared_ptr<map::MapContext> context) : explicit TextShaderImpl(std::shared_ptr<GlContext> context) :
context_ {context}, context_ {context},
shaderProgram_ {nullptr}, shaderProgram_ {nullptr},
projectionLocation_(GL_INVALID_INDEX) projectionLocation_(GL_INVALID_INDEX)
@ -28,13 +28,13 @@ public:
~TextShaderImpl() {} ~TextShaderImpl() {}
std::shared_ptr<map::MapContext> context_; std::shared_ptr<GlContext> context_;
std::shared_ptr<ShaderProgram> shaderProgram_; std::shared_ptr<ShaderProgram> shaderProgram_;
GLint projectionLocation_; GLint projectionLocation_;
}; };
TextShader::TextShader(std::shared_ptr<map::MapContext> context) : TextShader::TextShader(std::shared_ptr<GlContext> context) :
p(std::make_unique<TextShaderImpl>(context)) p(std::make_unique<TextShaderImpl>(context))
{ {
} }

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <scwx/qt/map/map_context.hpp> #include <scwx/qt/gl/gl_context.hpp>
#include <scwx/qt/util/font.hpp> #include <scwx/qt/util/font.hpp>
#include <memory> #include <memory>
@ -27,7 +27,7 @@ class TextShaderImpl;
class TextShader class TextShader
{ {
public: public:
explicit TextShader(std::shared_ptr<map::MapContext> context); explicit TextShader(std::shared_ptr<GlContext> context);
~TextShader(); ~TextShader();
TextShader(const TextShader&) = delete; TextShader(const TextShader&) = delete;

View file

@ -1,5 +1,4 @@
#include <scwx/qt/map/map_context.hpp> #include <scwx/qt/map/map_context.hpp>
#include <scwx/util/hash.hpp>
namespace scwx namespace scwx
{ {
@ -12,31 +11,21 @@ class MapContext::Impl
{ {
public: public:
explicit Impl(std::shared_ptr<view::RadarProductView> radarProductView) : explicit Impl(std::shared_ptr<view::RadarProductView> radarProductView) :
gl_ {},
settings_ {}, settings_ {},
radarProductView_ {radarProductView}, radarProductView_ {radarProductView},
radarProductGroup_ {common::RadarProductGroup::Unknown}, radarProductGroup_ {common::RadarProductGroup::Unknown},
radarProduct_ {"???"}, radarProduct_ {"???"},
radarProductCode_ {0}, radarProductCode_ {0}
shaderProgramMap_ {},
shaderProgramMutex_ {}
{ {
} }
~Impl() {} ~Impl() {}
gl::OpenGLFunctions gl_;
MapSettings settings_; MapSettings settings_;
std::shared_ptr<view::RadarProductView> radarProductView_; std::shared_ptr<view::RadarProductView> radarProductView_;
common::RadarProductGroup radarProductGroup_; common::RadarProductGroup radarProductGroup_;
std::string radarProduct_; std::string radarProduct_;
int16_t radarProductCode_; int16_t radarProductCode_;
std::unordered_map<std::pair<std::string, std::string>,
std::shared_ptr<gl::ShaderProgram>,
util::hash<std::pair<std::string, std::string>>>
shaderProgramMap_;
std::mutex shaderProgramMutex_;
}; };
MapContext::MapContext( MapContext::MapContext(
@ -49,11 +38,6 @@ MapContext::~MapContext() = default;
MapContext::MapContext(MapContext&&) noexcept = default; MapContext::MapContext(MapContext&&) noexcept = default;
MapContext& MapContext::operator=(MapContext&&) noexcept = default; MapContext& MapContext::operator=(MapContext&&) noexcept = default;
gl::OpenGLFunctions& MapContext::gl()
{
return p->gl_;
}
MapSettings& MapContext::settings() MapSettings& MapContext::settings()
{ {
return p->settings_; return p->settings_;
@ -101,31 +85,6 @@ void MapContext::set_radar_product_code(int16_t radarProductCode)
p->radarProductCode_ = radarProductCode; p->radarProductCode_ = radarProductCode;
} }
std::shared_ptr<gl::ShaderProgram>
MapContext::GetShaderProgram(const std::string& vertexPath,
const std::string& fragmentPath)
{
const std::pair<std::string, std::string> key {vertexPath, fragmentPath};
std::shared_ptr<gl::ShaderProgram> shaderProgram;
std::unique_lock lock(p->shaderProgramMutex_);
auto it = p->shaderProgramMap_.find(key);
if (it == p->shaderProgramMap_.end())
{
shaderProgram = std::make_shared<gl::ShaderProgram>(p->gl_);
shaderProgram->Load(vertexPath, fragmentPath);
p->shaderProgramMap_[key] = shaderProgram;
}
else
{
shaderProgram = it->second;
}
return shaderProgram;
}
} // namespace map } // namespace map
} // namespace qt } // namespace qt
} // namespace scwx } // namespace scwx

View file

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <scwx/qt/gl/gl.hpp> #include <scwx/qt/gl/gl_context.hpp>
#include <scwx/qt/gl/shader_program.hpp>
#include <scwx/qt/map/map_settings.hpp> #include <scwx/qt/map/map_settings.hpp>
#include <scwx/qt/view/radar_product_view.hpp> #include <scwx/qt/view/radar_product_view.hpp>
@ -12,7 +11,7 @@ namespace qt
namespace map namespace map
{ {
class MapContext class MapContext : public gl::GlContext
{ {
public: public:
explicit MapContext( explicit MapContext(
@ -25,7 +24,6 @@ public:
MapContext(MapContext&&) noexcept; MapContext(MapContext&&) noexcept;
MapContext& operator=(MapContext&&) noexcept; MapContext& operator=(MapContext&&) noexcept;
gl::OpenGLFunctions& gl();
MapSettings& settings(); MapSettings& settings();
std::shared_ptr<view::RadarProductView> radar_product_view() const; std::shared_ptr<view::RadarProductView> radar_product_view() const;
common::RadarProductGroup radar_product_group() const; common::RadarProductGroup radar_product_group() const;
@ -38,10 +36,6 @@ public:
void set_radar_product(const std::string& radarProduct); void set_radar_product(const std::string& radarProduct);
void set_radar_product_code(int16_t radarProductCode); void set_radar_product_code(int16_t radarProductCode);
std::shared_ptr<gl::ShaderProgram>
GetShaderProgram(const std::string& vertexPath,
const std::string& fragmentPath);
private: private:
class Impl; class Impl;