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>
This commit is contained in:
Tom Englund 2025-06-25 12:42:32 +02:00 committed by GitHub
parent 5a348fb7df
commit f4f090e4b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 871 additions and 892 deletions

View file

@ -1,85 +1,113 @@
#pragma once
#include "../defines.hpp"
#include <unordered_map>
#include <array>
#include <variant>
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,
SHADER_LAST,
};
struct SShader {
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 program = 0;
GLuint shaderVao = 0;
GLuint shaderVboPos = 0;
GLuint shaderVboUv = 0;
std::array<GLint, SHADER_LAST> uniformLocations;
GLint topLeft = -1;
GLint bottomRight = -1;
GLint fullSize = -1;
GLint fullSizeUntransformed = -1;
GLint radius = -1;
GLint radiusOuter = -1;
GLfloat roundingPower = -1;
float initialTime = 0;
GLint thick = -1;
struct SUniformMatrix3Data {
GLsizei count = 0;
GLboolean transpose = false;
std::array<GLfloat, 9> value = {};
};
GLint halfpixel = -1;
struct SUniformMatrix4Data {
GLsizei count = 0;
GLboolean transpose = false;
std::array<GLfloat, 8> value = {};
};
GLint range = -1;
GLint shadowPower = -1;
GLint useAlphaMatte = -1; // always inverted
struct SUniform4Data {
GLsizei count = 0;
std::vector<float> value;
};
GLint applyTint = -1;
GLint tint = -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;
//
GLint gradient = -1;
GLint gradientLength = -1;
GLint angle = -1;
GLint gradient2 = -1;
GLint gradient2Length = -1;
GLint angle2 = -1;
GLint gradientLerp = -1;
float initialTime = 0;
GLint time = -1;
GLint distort = -1;
GLint wl_output = -1;
// Blur prepare
GLint contrast = -1;
// Blur
GLint passes = -1; // Used by `vibrancy`
GLint vibrancy = -1;
GLint vibrancy_darkness = -1;
// Blur finish
GLint brightness = -1;
GLint noise = -1;
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();
};