1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 11:54:57 +00:00

LibAccelGfx: Add cache for programs

Having programs cache shared between painters would allow us to create
more than one painter without worrying about shaders recompilation.
This commit is contained in:
Aliaksandr Kalenik 2023-11-23 15:09:40 +01:00 committed by Andreas Kling
parent d5630fedf1
commit cb90daadc7
3 changed files with 21 additions and 13 deletions

View file

@ -144,10 +144,10 @@ OwnPtr<Painter> Painter::create()
Painter::Painter(Context& context) Painter::Painter(Context& context)
: m_context(context) : m_context(context)
, m_rectangle_program(Program::create(vertex_shader_source, solid_color_fragment_shader_source)) , m_rectangle_program(Program::create(Program::Name::RectangleProgram, vertex_shader_source, solid_color_fragment_shader_source))
, m_rounded_rectangle_program(Program::create(vertex_shader_source, rect_with_rounded_corners_fragment_shader_source)) , m_rounded_rectangle_program(Program::create(Program::Name::RoundedRectangleProgram, vertex_shader_source, rect_with_rounded_corners_fragment_shader_source))
, m_blit_program(Program::create(blit_vertex_shader_source, blit_fragment_shader_source)) , m_blit_program(Program::create(Program::Name::BlitProgram, blit_vertex_shader_source, blit_fragment_shader_source))
, m_linear_gradient_program(Program::create(linear_gradient_vertex_shader_source, linear_gradient_fragment_shader_source)) , m_linear_gradient_program(Program::create(Program::Name::LinearGradientProgram, linear_gradient_vertex_shader_source, linear_gradient_fragment_shader_source))
, m_glyphs_texture(GL::create_texture()) , m_glyphs_texture(GL::create_texture())
{ {
m_state_stack.empend(State()); m_state_stack.empend(State());

View file

@ -11,12 +11,19 @@
namespace AccelGfx { namespace AccelGfx {
Program Program::create(char const* vertex_shader_source, char const* fragment_shader_source) Optional<GL::Program> programs_cache[to_underlying(Program::Name::ProgramCount)];
Program Program::create(Name name, char const* vertex_shader_source, char const* fragment_shader_source)
{ {
if (programs_cache[to_underlying(name)].has_value()) {
return Program { *programs_cache[to_underlying(name)] };
}
auto vertex_shader = GL::create_shader(GL::ShaderType::Vertex, vertex_shader_source); auto vertex_shader = GL::create_shader(GL::ShaderType::Vertex, vertex_shader_source);
auto fragment_shader = GL::create_shader(GL::ShaderType::Fragment, fragment_shader_source); auto fragment_shader = GL::create_shader(GL::ShaderType::Fragment, fragment_shader_source);
auto program = GL::create_program(vertex_shader, fragment_shader); auto program = GL::create_program(vertex_shader, fragment_shader);
programs_cache[to_underlying(name)] = program;
return Program { program }; return Program { program };
} }
@ -36,9 +43,4 @@ GL::Uniform Program::get_uniform_location(char const* name)
return GL::get_uniform_location(m_program, name); return GL::get_uniform_location(m_program, name);
} }
Program::~Program()
{
GL::delete_program(m_program);
}
} }

View file

@ -15,14 +15,20 @@ class Program {
AK_MAKE_NONCOPYABLE(Program); AK_MAKE_NONCOPYABLE(Program);
public: public:
static Program create(char const* vertex_shader_source, char const* fragment_shader_source); enum class Name {
RectangleProgram,
RoundedRectangleProgram,
BlitProgram,
LinearGradientProgram,
ProgramCount,
};
static Program create(Name name, char const* vertex_shader_source, char const* fragment_shader_source);
void use(); void use();
GL::VertexAttribute get_attribute_location(char const* name); GL::VertexAttribute get_attribute_location(char const* name);
GL::Uniform get_uniform_location(char const* name); GL::Uniform get_uniform_location(char const* name);
~Program();
private: private:
Program(GL::Program program) Program(GL::Program program)
: m_program(program) : m_program(program)