summaryrefslogtreecommitdiff
path: root/src/render/Shader.hpp
diff options
context:
space:
mode:
authorTom Englund <tomenglund26@gmail.com>2025-06-25 12:42:32 +0200
committerGitHub <noreply@github.com>2025-06-25 12:42:32 +0200
commitf4f090e4b2f9f0bba5408cbd135d2fff1990be1d (patch)
tree0d7d63cea74a4198f5d99f43f1eaeeda27e38dbf /src/render/Shader.hpp
parent5a348fb7dfaf398922c119d21acb7d7f831f8688 (diff)
renderer: reduce a lot of glcalls and cache various states (#10757)
* opengl: cache viewport state according to nvidia docs calling glViewPort unnecessarily on the same already set viewport is wasteful and can cause state changes when not needed. cache it in a struct and only call it when the viewport is actually changing. * opengl: cache glenable/gldisable state avoid making multiple glenable/gldisable calls on already set caps, can cause state changes and incur driver overhead. * opengl: cache glscissor box only call glscissor if the box actually has changed, try to avoid state changes. * opengl: cache gluniform calls cache the gluniform calls, the uniform values are cached in driver per program only the drawcalls setting the uniform yet again with the same value on same location is causing more overhead then caching it ourself and just no oping on it if no changes. * shader: rewrite handling of uniforms and state this is way faster as we don't need to mess with maps (hashing, etc) and instead can just use an array * opengl: stuff and 300 shaders * opengl: typo * opengl: get the uniform locations properly now that the legacy shaders are gone get the uniformlocations for SKIP_CM etc, so they can be properly set and used depending on if cm_enabled is set to false or true, before it was falling back to a legacy shader that didnt even have those uniforms. * opengl: check epsilon on float and remove extra glcall seems an extra unset glcall was added, remove it. and check the float epsilon on the glfloat. * opengl: remove instanced shader draw remove the instanced boolean from the vertex shader, might be neglible differences, needs more benchmark/work to see if its even worth it. * texture: cache texture paramaters parameters where occasionally set twice or more on same texture, short version wrap it and cache it. and move gpu churn to cpu churn. add a bind/unbind to texture aswell. * texture: use fast std::array caching cache the texparameter values in fast array lookups and incase we dont want it cached, apply it anyways. * shader: fix typo and hdr typo actually use Matrix4x2fv in the 4x2fv cache function, and send the proper float array for hdr. * texture: make caching not linear lookup make caching of texture params not linear. * minor style changes * opengl: revert drawarrays revert the mostly code style reduce loc change of drawarrays, and focus on the caching. its a if else case going wrong here breaking blur/contrast amongst others drawing. --------- Co-authored-by: Vaxry <vaxry@vaxry.net>
Diffstat (limited to 'src/render/Shader.hpp')
-rw-r--r--src/render/Shader.hpp168
1 files changed, 98 insertions, 70 deletions
diff --git a/src/render/Shader.hpp b/src/render/Shader.hpp
index 7bed9041..76d1235f 100644
--- a/src/render/Shader.hpp
+++ b/src/render/Shader.hpp
@@ -1,85 +1,113 @@
#pragma once
#include "../defines.hpp"
-#include <unordered_map>
+#include <array>
+#include <variant>
-struct SShader {
- ~SShader();
-
- GLuint program = 0;
- GLint proj = -1;
- GLint color = -1;
- GLint alphaMatte = -1;
- GLint texType = -1;
- GLint skipCM = -1;
- GLint sourceTF = -1;
- GLint targetTF = -1;
- GLint srcTFRange = -1;
- GLint dstTFRange = -1;
- GLint targetPrimaries = -1;
- GLint maxLuminance = -1;
- GLint dstMaxLuminance = -1;
- GLint dstRefLuminance = -1;
- GLint sdrSaturation = -1; // sdr -> hdr saturation
- GLint sdrBrightness = -1; // sdr -> hdr brightness multiplier
- GLint convertMatrix = -1;
- GLint tex = -1;
- GLint alpha = -1;
- GLint posAttrib = -1;
- GLint texAttrib = -1;
- GLint matteTexAttrib = -1;
- GLint discardOpaque = -1;
- GLint discardAlpha = -1;
- GLfloat discardAlphaValue = -1;
-
- GLuint shaderVao = 0;
- GLuint shaderVboPos = 0;
- GLuint shaderVboUv = 0;
+enum eShaderUniform : uint8_t {
+ SHADER_PROJ = 0,
+ SHADER_COLOR,
+ SHADER_ALPHA_MATTE,
+ SHADER_TEX_TYPE,
+ SHADER_SKIP_CM,
+ SHADER_SOURCE_TF,
+ SHADER_TARGET_TF,
+ SHADER_SRC_TF_RANGE,
+ SHADER_DST_TF_RANGE,
+ SHADER_TARGET_PRIMARIES,
+ SHADER_MAX_LUMINANCE,
+ SHADER_DST_MAX_LUMINANCE,
+ SHADER_DST_REF_LUMINANCE,
+ SHADER_SDR_SATURATION,
+ SHADER_SDR_BRIGHTNESS,
+ SHADER_CONVERT_MATRIX,
+ SHADER_TEX,
+ SHADER_ALPHA,
+ SHADER_POS_ATTRIB,
+ SHADER_TEX_ATTRIB,
+ SHADER_MATTE_TEX_ATTRIB,
+ SHADER_DISCARD_OPAQUE,
+ SHADER_DISCARD_ALPHA,
+ SHADER_DISCARD_ALPHA_VALUE,
+ SHADER_SHADER_VAO,
+ SHADER_SHADER_VBO_POS,
+ SHADER_SHADER_VBO_UV,
+ SHADER_TOP_LEFT,
+ SHADER_BOTTOM_RIGHT,
+ SHADER_FULL_SIZE,
+ SHADER_FULL_SIZE_UNTRANSFORMED,
+ SHADER_RADIUS,
+ SHADER_RADIUS_OUTER,
+ SHADER_ROUNDING_POWER,
+ SHADER_THICK,
+ SHADER_HALFPIXEL,
+ SHADER_RANGE,
+ SHADER_SHADOW_POWER,
+ SHADER_USE_ALPHA_MATTE,
+ SHADER_APPLY_TINT,
+ SHADER_TINT,
+ SHADER_GRADIENT,
+ SHADER_GRADIENT_LENGTH,
+ SHADER_ANGLE,
+ SHADER_GRADIENT2,
+ SHADER_GRADIENT2_LENGTH,
+ SHADER_ANGLE2,
+ SHADER_GRADIENT_LERP,
+ SHADER_TIME,
+ SHADER_DISTORT,
+ SHADER_WL_OUTPUT,
+ SHADER_CONTRAST,
+ SHADER_PASSES,
+ SHADER_VIBRANCY,
+ SHADER_VIBRANCY_DARKNESS,
+ SHADER_BRIGHTNESS,
+ SHADER_NOISE,
- GLint topLeft = -1;
- GLint bottomRight = -1;
- GLint fullSize = -1;
- GLint fullSizeUntransformed = -1;
- GLint radius = -1;
- GLint radiusOuter = -1;
- GLfloat roundingPower = -1;
-
- GLint thick = -1;
+ SHADER_LAST,
+};
- GLint halfpixel = -1;
+struct SShader {
+ SShader();
+ ~SShader();
- GLint range = -1;
- GLint shadowPower = -1;
- GLint useAlphaMatte = -1; // always inverted
+ GLuint program = 0;
- GLint applyTint = -1;
- GLint tint = -1;
+ std::array<GLint, SHADER_LAST> uniformLocations;
- GLint gradient = -1;
- GLint gradientLength = -1;
- GLint angle = -1;
- GLint gradient2 = -1;
- GLint gradient2Length = -1;
- GLint angle2 = -1;
- GLint gradientLerp = -1;
+ float initialTime = 0;
- float initialTime = 0;
- GLint time = -1;
- GLint distort = -1;
- GLint wl_output = -1;
+ struct SUniformMatrix3Data {
+ GLsizei count = 0;
+ GLboolean transpose = false;
+ std::array<GLfloat, 9> value = {};
+ };
- // Blur prepare
- GLint contrast = -1;
+ struct SUniformMatrix4Data {
+ GLsizei count = 0;
+ GLboolean transpose = false;
+ std::array<GLfloat, 8> value = {};
+ };
- // Blur
- GLint passes = -1; // Used by `vibrancy`
- GLint vibrancy = -1;
- GLint vibrancy_darkness = -1;
+ struct SUniform4Data {
+ GLsizei count = 0;
+ std::vector<float> value;
+ };
- // Blur finish
- GLint brightness = -1;
- GLint noise = -1;
+ //
+ std::array<std::variant<std::monostate, GLint, GLfloat, std::array<GLfloat, 2>, std::array<GLfloat, 3>, std::array<GLfloat, 4>, SUniformMatrix3Data, SUniformMatrix4Data,
+ SUniform4Data>,
+ SHADER_LAST>
+ uniformStatus;
+ //
- void createVao();
- void destroy();
+ void createVao();
+ void setUniformInt(eShaderUniform location, GLint v0);
+ void setUniformFloat(eShaderUniform location, GLfloat v0);
+ void setUniformFloat2(eShaderUniform location, GLfloat v0, GLfloat v1);
+ void setUniformFloat3(eShaderUniform location, GLfloat v0, GLfloat v1, GLfloat v2);
+ void setUniformFloat4(eShaderUniform location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
+ void setUniformMatrix3fv(eShaderUniform location, GLsizei count, GLboolean transpose, std::array<GLfloat, 9> value);
+ void setUniformMatrix4x2fv(eShaderUniform location, GLsizei count, GLboolean transpose, std::array<GLfloat, 8> value);
+ void setUniform4fv(eShaderUniform location, GLsizei count, std::vector<float> value);
+ void destroy();
};