From 5e181111216ba05c89b245d9b239b47a914fd714 Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Mon, 12 Jan 2026 18:27:16 +0100 Subject: renderer: shader code refactor (#12926) * shader: begin the shader refactor make SShader a class and rename it to CShader, move createprogram, compileshader, logshadererror to CShader. * shader: move uniform creation to CShader move uniform creation to CShader, reduces tons of duplicated effort, however forcing uniform names to be same in all shaders. * shader: move to array based frag handling use an array with an enum so it gets easier dealing with multiple shaders, move creating program to a for loop and array, reduces line of code a lot. * shader: use shared ptr for frags with smart pointers we can now rename useProgram to useShader and return the shader directly, means only place we have to decide the shader frag is when calling useShader. easier for future shader splitting to reduce branching. * shader: move unneded public members to private move structs and uniforms to private add a get/set for initialtime and add a getUniformLocation to make the code tell what its doing, instead of direct array getting when all we wanted to get was its value, also limits the setting of uniformLocations to the createProgram as it should be. * shader: fix style nits set first enum member to 0 , remove extra {} * shader: dont show a failed notif on success the logic got inverted in the refactor here. * shader: split CM shader to rgba/rgbx variants split shader to rgba/rgbx variants, use bool, and reduce branching. * shader: split up blurprepare CM and non CM split up blurprepare, remove skipcm, move gain to gain.glsl. remove ternary operator and reduce branching by using step() and mix() use vec3 for gain, make brightness a cheap mulitplication with max. * shader: split up border to CM/noncm variants splitup border shader to CM/noncm variant, move common used things to border.glsl , there is room for optimisations here but its a complex shader im putting it for future PR. * shader: touchup blurfinish make brightness a cheap multiplication instead of branching. mod is redundant, fract in hash already returns a value in [0.0, 1.0] --- src/render/OpenGL.hpp | 63 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 23 deletions(-) (limited to 'src/render/OpenGL.hpp') diff --git a/src/render/OpenGL.hpp b/src/render/OpenGL.hpp index e71429b7..857cb891 100644 --- a/src/render/OpenGL.hpp +++ b/src/render/OpenGL.hpp @@ -81,23 +81,43 @@ enum eMonitorExtraRenderFBs : uint8_t { FB_MONITOR_RENDER_EXTRA_BLUR, }; +enum ePreparedFragmentShader : uint8_t { + SH_FRAG_QUAD = 0, + SH_FRAG_RGBA, + SH_FRAG_PASSTHRURGBA, + SH_FRAG_MATTE, + SH_FRAG_RGBX, + SH_FRAG_EXT, + SH_FRAG_BLUR1, + SH_FRAG_BLUR2, + SH_FRAG_CM_BLURPREPARE, + SH_FRAG_BLURPREPARE, + SH_FRAG_BLURFINISH, + SH_FRAG_SHADOW, + SH_FRAG_CM_BORDER1, + SH_FRAG_BORDER1, + SH_FRAG_GLITCH, + SH_FRAG_CM_RGBA, + SH_FRAG_CM_RGBX, + + SH_FRAG_LAST, +}; + +struct SFragShaderDesc { + ePreparedFragmentShader id; + const char* file; +}; + struct SPreparedShaders { - std::string TEXVERTSRC; - std::string TEXVERTSRC320; - SShader m_shQUAD; - SShader m_shRGBA; - SShader m_shPASSTHRURGBA; - SShader m_shMATTE; - SShader m_shRGBX; - SShader m_shEXT; - SShader m_shBLUR1; - SShader m_shBLUR2; - SShader m_shBLURPREPARE; - SShader m_shBLURFINISH; - SShader m_shSHADOW; - SShader m_shBORDER1; - SShader m_shGLITCH; - SShader m_shCM; + SPreparedShaders() { + for (auto& f : frag) { + f = makeShared(); + } + } + + std::string TEXVERTSRC; + std::string TEXVERTSRC320; + std::array, SH_FRAG_LAST> frag; }; struct SMonitorRenderData { @@ -274,9 +294,7 @@ class CHyprOpenGLImpl { bool initShaders(); - GLuint createProgram(const std::string&, const std::string&, bool dynamic = false, bool silent = false); - GLuint compileShader(const GLuint&, std::string, bool dynamic = false, bool silent = false); - void useProgram(GLuint prog); + WP useShader(WP prog); void ensureLockTexturesRendered(bool load); @@ -375,13 +393,12 @@ class CHyprOpenGLImpl { SP m_lockDeadTexture; SP m_lockDead2Texture; SP m_lockTtyTextTexture; - SShader m_finalScreenShader; + SP m_finalScreenShader; CTimer m_globalTimer; GLuint m_currentProgram; ASP m_backgroundResource; bool m_backgroundResourceFailed = false; - void logShaderError(const GLuint&, bool program = false, bool silent = false); void createBGTextureForMonitor(PHLMONITOR); void initDRMFormats(); void initEGL(bool gbm); @@ -403,9 +420,9 @@ class CHyprOpenGLImpl { CFramebuffer* blurMainFramebufferWithDamage(float a, CRegion* damage); CFramebuffer* blurFramebufferWithDamage(float a, CRegion* damage, CFramebuffer& source); - void passCMUniforms(SShader&, const NColorManagement::PImageDescription imageDescription, const NColorManagement::PImageDescription targetImageDescription, + void passCMUniforms(WP, const NColorManagement::PImageDescription imageDescription, const NColorManagement::PImageDescription targetImageDescription, bool modifySDR = false, float sdrMinLuminance = -1.0f, int sdrMaxLuminance = -1); - void passCMUniforms(SShader&, const NColorManagement::PImageDescription imageDescription); + void passCMUniforms(WP, const NColorManagement::PImageDescription imageDescription); void renderTexturePrimitive(SP tex, const CBox& box); void renderSplash(cairo_t* const, cairo_surface_t* const, double offset, const Vector2D& size); void renderRectInternal(const CBox&, const CHyprColor&, const SRectRenderData& data); -- cgit v1.2.3