Optimize uniforms usage

optimize-gl-calls-part-3
Hleb Valoshka 2020-07-08 18:57:14 +03:00
parent 18dd48db0e
commit 5d5e8783f5
13 changed files with 70 additions and 121 deletions

View File

@ -414,7 +414,7 @@ void Galaxy::renderGalaxyPointSprites(const Vector3f& offset,
GLushort j = 0;
prog->use();
prog->mat4Param("MVPMatrix") = (*ms.projection) * mv;
prog->MVPMatrix = (*ms.projection) * mv;
prog->samplerParam("galaxyTex") = 0;
prog->samplerParam("colorTex") = 1;

View File

@ -363,7 +363,7 @@ void Renderer::renderSelectionPointer(const Observer& observer,
prog->use();
const Vector3f &center = cameraMatrix.col(2);
prog->mat4Param("MVPMatrix") = getProjectionMatrix() * getModelViewMatrix() * vecgl::translate(Vector3f(-center));
prog->MVPMatrix = getProjectionMatrix() * getModelViewMatrix() * vecgl::translate(Vector3f(-center));
prog->vec4Param("color") = Color(SelectionCursorColor, 0.6f).toVector4();
prog->floatParam("pixelSize") = pixelSize;
prog->floatParam("s") = s;
@ -401,7 +401,7 @@ void Renderer::renderEclipticLine()
initVO(markerVO);
prog->use();
prog->mat4Param("MVPMatrix") = getProjectionMatrix() * getModelViewMatrix();
prog->MVPMatrix = getProjectionMatrix() * getModelViewMatrix();
prog->vec4Param("color") = EclipticColor.toVector4();
markerVO.draw(GL_LINE_LOOP, EclipticCount, EclipticOffset);
@ -437,7 +437,7 @@ void Renderer::renderCrosshair(float selectionSizeInPixels,
float cursorGrow = max(1.0f, min(2.5f, (selectionSizeInPixels - 10.0f) / 100.0f));
prog->use();
prog->mat4Param("MVPMatrix") = (*m.projection) * (*m.modelview);
prog->MVPMatrix = (*m.projection) * (*m.modelview);
prog->vec4Param("color") = color.toVector4();
prog->floatParam("radius") = cursorRadius;
prog->floatParam("width") = minCursorWidth * cursorGrow;

View File

@ -468,7 +468,7 @@ void Globular::renderGlobularPointSprites(
centerTex[ic]->bind();
Matrix4f mvp = (*m.projection) * (*m.modelview);
tidalProg->mat4Param("MVPMatrix") = mvp;
tidalProg->MVPMatrix = mvp;
Matrix3f viewMat = viewerOrientation.conjugate().toRotationMatrix();
tidalProg->vec4Param("color") = Vector4f(Rr, Gg, Bb, min(2 * brightness * pixelWeight, 1.0f));
@ -492,8 +492,8 @@ void Globular::renderGlobularPointSprites(
globProg->use();
globularTex->bind();
globProg->mat4Param("MVPMatrix") = mvp;
globProg->mat4Param("ModelViewMatrix") = vecgl::translate(*m.modelview, offset);
globProg->MVPMatrix = mvp;
globProg->ModelViewMatrix = vecgl::translate(*m.modelview, offset);
Matrix3f mx = Scaling(form->scale) * getOrientation().toRotationMatrix() * Scaling(tidalSize);
globProg->mat3Param("m") = mx;
globProg->vec3Param("offset") = offset;

View File

@ -68,16 +68,6 @@ GLShader::~GLShader()
//************* GLxxxProperty **********
FloatShaderParameter::FloatShaderParameter() :
slot(-1)
{
}
FloatShaderParameter::FloatShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
}
FloatShaderParameter&
FloatShaderParameter::operator=(float f)
{
@ -86,17 +76,6 @@ FloatShaderParameter::operator=(float f)
return *this;
}
Vec3ShaderParameter::Vec3ShaderParameter() :
slot(-1)
{
}
Vec3ShaderParameter::Vec3ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
}
Vec3ShaderParameter&
Vec3ShaderParameter::operator=(const Eigen::Vector3f& v)
{
@ -105,16 +84,6 @@ Vec3ShaderParameter::operator=(const Eigen::Vector3f& v)
return *this;
}
Vec4ShaderParameter::Vec4ShaderParameter() :
slot(-1)
{
}
Vec4ShaderParameter::Vec4ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
}
Vec4ShaderParameter&
Vec4ShaderParameter::operator=(const Eigen::Vector4f& v)
{
@ -123,17 +92,6 @@ Vec4ShaderParameter::operator=(const Eigen::Vector4f& v)
return *this;
}
IntegerShaderParameter::IntegerShaderParameter() :
slot(-1)
{
}
IntegerShaderParameter::IntegerShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
}
IntegerShaderParameter&
IntegerShaderParameter::operator=(int i)
{
@ -142,17 +100,6 @@ IntegerShaderParameter::operator=(int i)
return *this;
}
Mat3ShaderParameter::Mat3ShaderParameter() :
slot(-1)
{
}
Mat3ShaderParameter::Mat3ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
}
Mat3ShaderParameter&
Mat3ShaderParameter::operator=(const Eigen::Matrix3f& v)
{
@ -161,17 +108,6 @@ Mat3ShaderParameter::operator=(const Eigen::Matrix3f& v)
return *this;
}
Mat4ShaderParameter::Mat4ShaderParameter() :
slot(-1)
{
}
Mat4ShaderParameter::Mat4ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
}
Mat4ShaderParameter&
Mat4ShaderParameter::operator=(const Eigen::Matrix4f& v)
{

View File

@ -84,82 +84,79 @@ class GLProgram
friend class GLShaderLoader;
};
class FloatShaderParameter
class ShaderParameter
{
public:
FloatShaderParameter();
FloatShaderParameter(GLuint obj, const char* name);
ShaderParameter() = default;
~ShaderParameter() = default;
ShaderParameter(const ShaderParameter&) = default;
ShaderParameter(ShaderParameter&&) = default;
ShaderParameter& operator=(const ShaderParameter&) = default;
ShaderParameter& operator=(ShaderParameter&&) = default;
ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
}
bool valid() const noexcept
{
return slot != -1;
};
operator bool() const noexcept
{
return valid();
}
protected:
int slot { -1 };
};
class FloatShaderParameter : public ShaderParameter
{
public:
using ShaderParameter::ShaderParameter;
FloatShaderParameter& operator=(float);
private:
int slot;
};
class Vec3ShaderParameter
class Vec3ShaderParameter : public ShaderParameter
{
public:
Vec3ShaderParameter();
Vec3ShaderParameter(GLuint obj, const char* name);
using ShaderParameter::ShaderParameter;
Vec3ShaderParameter& operator=(const Eigen::Vector3f&);
private:
int slot;
};
class Vec4ShaderParameter
class Vec4ShaderParameter : public ShaderParameter
{
public:
Vec4ShaderParameter();
Vec4ShaderParameter(GLuint obj, const char* name);
using ShaderParameter::ShaderParameter;
Vec4ShaderParameter& operator=(const Eigen::Vector4f&);
private:
int slot;
};
class IntegerShaderParameter
class IntegerShaderParameter : public ShaderParameter
{
public:
IntegerShaderParameter();
IntegerShaderParameter(GLuint obj, const char* name);
using ShaderParameter::ShaderParameter;
IntegerShaderParameter& operator=(int);
private:
int slot;
};
class Mat3ShaderParameter
class Mat3ShaderParameter : public ShaderParameter
{
public:
Mat3ShaderParameter();
Mat3ShaderParameter(GLuint obj, const char* name);
using ShaderParameter::ShaderParameter;
Mat3ShaderParameter& operator=(const Eigen::Matrix3f&);
private:
int slot;
};
class Mat4ShaderParameter
class Mat4ShaderParameter : public ShaderParameter
{
public:
Mat4ShaderParameter();
Mat4ShaderParameter(GLuint obj, const char* name);
using ShaderParameter::ShaderParameter;
Mat4ShaderParameter& operator=(const Eigen::Matrix4f&);
private:
int slot;
};

View File

@ -36,8 +36,10 @@ void PointStarVertexBuffer::startSprites()
if (prog == nullptr)
return;
prog->use();
prog->mat4Param("MVPMatrix") = renderer.getProjectionMatrix() * renderer.getModelViewMatrix();
prog->samplerParam("starTex") = 0;
prog->MVPMatrix = renderer.getProjectionMatrix() * renderer.getModelViewMatrix();
if (!starTexParam)
starTexParam = prog->samplerParam("starTex");
starTexParam = 0;
unsigned int stride = sizeof(StarVertex);
glEnableVertexAttribArray(CelestiaGLProgram::VertexCoordAttributeIndex);
@ -106,7 +108,7 @@ void PointStarVertexBuffer::render()
glPointSize(1.0f);
}
#endif
glVertexAttribPointer(CelestiaGLProgram::VertexCoordAttributeIndex,
glVertexAttribPointer(CelestiaGLProgram::VertexCoordAttributeIndex,
3, GL_FLOAT, GL_FALSE,
stride, &vertices[0].position);
glVertexAttribPointer(CelestiaGLProgram::ColorAttributeIndex,

View File

@ -11,6 +11,7 @@
#pragma once
#include <Eigen/Core>
#include "glshader.h"
class Color;
class Renderer;
@ -53,6 +54,7 @@ private:
StarVertex* vertices { nullptr };
Texture* texture { nullptr };
bool useSprites { false };
IntegerShaderParameter starTexParam;
};
inline void PointStarVertexBuffer::addStar(const Eigen::Vector3f& pos,

View File

@ -577,7 +577,9 @@ GLSL_RenderContext::makeCurrent(const Material& m)
shadowBias.diagonal() = Vector4f(0.5f, 0.5f, 0.5f, 1.0f);
shadowBias.col(3) = Vector4f(0.5f, 0.5f, 0.5f, 1.0f);
prog->ShadowMatrix0 = shadowBias * (*lightMatrix);
prog->floatParam("shadowMapSize") = static_cast<float>(shadowMapWidth);
if (!shadowMapSizeParam)
shadowMapSizeParam = prog->floatParam("shadowMapSize");
shadowMapSizeParam = static_cast<float>(shadowMapWidth);
}
// setLightParameters() expects opacity in the alpha channel of the diffuse color

View File

@ -120,6 +120,7 @@ class GLSL_RenderContext : public RenderContext
const Eigen::Matrix4f *lightMatrix { nullptr };
GLuint shadowMap { 0 };
GLuint shadowMapWidth { 0 };
FloatShaderParameter shadowMapSizeParam;
};

View File

@ -1799,7 +1799,7 @@ void renderPoint(const Renderer &renderer,
prog->use();
prog->samplerParam("starTex") = 0;
prog->mat4Param("MVPMatrix") = (*m.projection) * (*m.modelview);
prog->MVPMatrix = (*m.projection) * (*m.modelview);
#ifndef GL_ES
glEnable(GL_POINT_SPRITE);
@ -3687,7 +3687,7 @@ void Renderer::renderCometTail(const Body& body,
setBlendingFactors(GL_SRC_ALPHA, GL_ONE);
prog->use();
prog->mat4Param("MVPMatrix") = (*m.projection) * (*m.modelview) * vecgl::translate(pos);
prog->MVPMatrix = (*m.projection) * (*m.modelview) * vecgl::translate(pos);
glEnableVertexAttribArray(CelestiaGLProgram::VertexCoordAttributeIndex);
glEnableVertexAttribArray(CelestiaGLProgram::NormalAttributeIndex);

View File

@ -943,7 +943,7 @@ void renderGeometryShadow_GLSL(Geometry* geometry,
Matrix4f projMat = Ortho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f);
Matrix4f modelViewMat = directionalLightMatrix(ls.lights[lightIndex].direction_obj);
*lightMatrix = projMat * modelViewMat;
prog->mat4Param("MVPMatrix") = *lightMatrix;
prog->MVPMatrix = *lightMatrix;
geometry->render(rc, tsec);

View File

@ -3352,6 +3352,7 @@ CelestiaGLProgram::CelestiaGLProgram(GLProgram& _program,
CelestiaGLProgram::CelestiaGLProgram(GLProgram& _program) :
program(&_program)
{
initCommonParameters();
}
CelestiaGLProgram::~CelestiaGLProgram()
@ -3417,10 +3418,17 @@ CelestiaGLProgram::attribIndex(const std::string& paramName) const
void
CelestiaGLProgram::initParameters()
CelestiaGLProgram::initCommonParameters()
{
ModelViewMatrix = mat4Param("ModelViewMatrix");
MVPMatrix = mat4Param("MVPMatrix");
}
void
CelestiaGLProgram::initParameters()
{
initCommonParameters();
for (unsigned int i = 0; i < props.nLights; i++)
{

View File

@ -270,6 +270,7 @@ class CelestiaGLProgram
int attribIndex(const std::string&) const;
private:
void initCommonParameters();
void initParameters();
void initSamplers();