Refactor fetching uniform handles for shaders
Fetch uniform handles only once instead of every draw for ElectricityEffect and SparkEffect.
This commit is contained in:
parent
8164c6633f
commit
a7f25e851e
4 changed files with 49 additions and 27 deletions
|
@ -31,6 +31,12 @@
|
||||||
#include "ems.h"
|
#include "ems.h"
|
||||||
|
|
||||||
std::optional<Shader> ElectricityEffect::shader = std::nullopt;
|
std::optional<Shader> ElectricityEffect::shader = std::nullopt;
|
||||||
|
int ElectricityEffect::uniform_screen_height = 0;
|
||||||
|
int ElectricityEffect::uniform_side_pos_a = 0;
|
||||||
|
int ElectricityEffect::uniform_side_dir_a = 0;
|
||||||
|
int ElectricityEffect::uniform_side_pos_b = 0;
|
||||||
|
int ElectricityEffect::uniform_side_dir_b = 0;
|
||||||
|
int ElectricityEffect::uniform_width = 0;
|
||||||
|
|
||||||
ElectricityEffect::ElectricityEffect(Vector3 center, float radius,
|
ElectricityEffect::ElectricityEffect(Vector3 center, float radius,
|
||||||
int line_count, float lifetime,
|
int line_count, float lifetime,
|
||||||
|
@ -160,10 +166,10 @@ void ElectricityEffect::update_shader_height() {
|
||||||
if (!shader.has_value()) {
|
if (!shader.has_value()) {
|
||||||
init_shader();
|
init_shader();
|
||||||
}
|
}
|
||||||
int uniform_loc = GetShaderLocation(get_shader(), "screen_height");
|
|
||||||
float height = GetScreenHeight();
|
float height = GetScreenHeight();
|
||||||
DEBUG_PRINT_FLOAT(height);
|
// DEBUG_PRINT_FLOAT(height);
|
||||||
SetShaderValue(get_shader(), uniform_loc, &height, SHADER_UNIFORM_FLOAT);
|
SetShaderValue(get_shader(), uniform_screen_height, &height,
|
||||||
|
SHADER_UNIFORM_FLOAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ElectricityEffect::update_shader_sides(Vector2 a, Vector2 adir, Vector2 b,
|
void ElectricityEffect::update_shader_sides(Vector2 a, Vector2 adir, Vector2 b,
|
||||||
|
@ -171,21 +177,16 @@ void ElectricityEffect::update_shader_sides(Vector2 a, Vector2 adir, Vector2 b,
|
||||||
if (!shader.has_value()) {
|
if (!shader.has_value()) {
|
||||||
init_shader();
|
init_shader();
|
||||||
}
|
}
|
||||||
int uniform_loc = GetShaderLocation(get_shader(), "sidePosA");
|
// DEBUG_PRINT_VEC2(a);
|
||||||
DEBUG_PRINT_VEC2(a);
|
SetShaderValue(get_shader(), uniform_side_pos_a, &a, SHADER_UNIFORM_VEC2);
|
||||||
SetShaderValue(get_shader(), uniform_loc, &a, SHADER_UNIFORM_VEC2);
|
// DEBUG_PRINT_VEC2(adir);
|
||||||
uniform_loc = GetShaderLocation(get_shader(), "sideDirA");
|
SetShaderValue(get_shader(), uniform_side_dir_a, &adir, SHADER_UNIFORM_VEC2);
|
||||||
DEBUG_PRINT_VEC2(adir);
|
// DEBUG_PRINT_VEC2(b);
|
||||||
SetShaderValue(get_shader(), uniform_loc, &adir, SHADER_UNIFORM_VEC2);
|
SetShaderValue(get_shader(), uniform_side_pos_b, &b, SHADER_UNIFORM_VEC2);
|
||||||
uniform_loc = GetShaderLocation(get_shader(), "sidePosB");
|
// DEBUG_PRINT_VEC2(bdir);
|
||||||
DEBUG_PRINT_VEC2(b);
|
SetShaderValue(get_shader(), uniform_side_dir_b, &bdir, SHADER_UNIFORM_VEC2);
|
||||||
SetShaderValue(get_shader(), uniform_loc, &b, SHADER_UNIFORM_VEC2);
|
// DEBUG_PRINT_FLOAT(width);
|
||||||
uniform_loc = GetShaderLocation(get_shader(), "sideDirB");
|
SetShaderValue(get_shader(), uniform_width, &width, SHADER_UNIFORM_FLOAT);
|
||||||
DEBUG_PRINT_VEC2(bdir);
|
|
||||||
SetShaderValue(get_shader(), uniform_loc, &bdir, SHADER_UNIFORM_VEC2);
|
|
||||||
uniform_loc = GetShaderLocation(get_shader(), "width");
|
|
||||||
DEBUG_PRINT_FLOAT(width);
|
|
||||||
SetShaderValue(get_shader(), uniform_loc, &width, SHADER_UNIFORM_FLOAT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ElectricityEffect::init_shader() {
|
void ElectricityEffect::init_shader() {
|
||||||
|
@ -257,4 +258,11 @@ void ElectricityEffect::init_shader() {
|
||||||
" gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); \n"
|
" gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); \n"
|
||||||
" } \n"
|
" } \n"
|
||||||
"} \n");
|
"} \n");
|
||||||
|
|
||||||
|
uniform_screen_height = GetShaderLocation(shader.value(), "screen_height");
|
||||||
|
uniform_side_pos_a = GetShaderLocation(shader.value(), "sidePosA");
|
||||||
|
uniform_side_dir_a = GetShaderLocation(shader.value(), "sideDirA");
|
||||||
|
uniform_side_pos_b = GetShaderLocation(shader.value(), "sidePosB");
|
||||||
|
uniform_side_dir_b = GetShaderLocation(shader.value(), "sideDirB");
|
||||||
|
uniform_width = GetShaderLocation(shader.value(), "width");
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,12 @@ class ElectricityEffect {
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::optional<Shader> shader;
|
static std::optional<Shader> shader;
|
||||||
|
static int uniform_screen_height;
|
||||||
|
static int uniform_side_pos_a;
|
||||||
|
static int uniform_side_dir_a;
|
||||||
|
static int uniform_side_pos_b;
|
||||||
|
static int uniform_side_dir_b;
|
||||||
|
static int uniform_width;
|
||||||
std::vector<EndPoint> end_points;
|
std::vector<EndPoint> end_points;
|
||||||
Vector3 center;
|
Vector3 center;
|
||||||
Color color;
|
Color color;
|
||||||
|
|
|
@ -28,6 +28,9 @@
|
||||||
#include "ems.h"
|
#include "ems.h"
|
||||||
|
|
||||||
std::optional<Shader> SparkEffect::shader = std::nullopt;
|
std::optional<Shader> SparkEffect::shader = std::nullopt;
|
||||||
|
int SparkEffect::uniform_screen_height = 0;
|
||||||
|
int SparkEffect::uniform_spark_radius = 0;
|
||||||
|
int SparkEffect::uniform_spark_pos = 0;
|
||||||
|
|
||||||
SparkEffect::SparkEffect(int count, float lifetime, Vector3 pos,
|
SparkEffect::SparkEffect(int count, float lifetime, Vector3 pos,
|
||||||
float pos_xz_variance, float radius, Color color)
|
float pos_xz_variance, float radius, Color color)
|
||||||
|
@ -101,10 +104,10 @@ void SparkEffect::update_shader_height() {
|
||||||
if (!shader.has_value()) {
|
if (!shader.has_value()) {
|
||||||
init_shader();
|
init_shader();
|
||||||
}
|
}
|
||||||
int uniform_loc = GetShaderLocation(get_shader(), "screen_height");
|
|
||||||
float height = GetScreenHeight();
|
float height = GetScreenHeight();
|
||||||
DEBUG_PRINT_FLOAT(height);
|
// DEBUG_PRINT_FLOAT(height);
|
||||||
SetShaderValue(get_shader(), uniform_loc, &height, SHADER_UNIFORM_FLOAT);
|
SetShaderValue(get_shader(), uniform_screen_height, &height,
|
||||||
|
SHADER_UNIFORM_FLOAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SparkEffect::update_shader_uniforms(float radius, Vector2 pos) {
|
void SparkEffect::update_shader_uniforms(float radius, Vector2 pos) {
|
||||||
|
@ -112,12 +115,11 @@ void SparkEffect::update_shader_uniforms(float radius, Vector2 pos) {
|
||||||
init_shader();
|
init_shader();
|
||||||
}
|
}
|
||||||
|
|
||||||
int uniform_loc = GetShaderLocation(get_shader(), "spark_radius");
|
// DEBUG_PRINT_FLOAT(radius);
|
||||||
DEBUG_PRINT_FLOAT(radius);
|
SetShaderValue(get_shader(), uniform_spark_radius, &radius,
|
||||||
SetShaderValue(get_shader(), uniform_loc, &radius, SHADER_UNIFORM_FLOAT);
|
SHADER_UNIFORM_FLOAT);
|
||||||
uniform_loc = GetShaderLocation(get_shader(), "spark_pos");
|
// DEBUG_PRINT_VEC2(pos);
|
||||||
DEBUG_PRINT_VEC2(pos);
|
SetShaderValue(get_shader(), uniform_spark_pos, &pos, SHADER_UNIFORM_VEC2);
|
||||||
SetShaderValue(get_shader(), uniform_loc, &pos, SHADER_UNIFORM_VEC2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SparkEffect::init_shader() {
|
void SparkEffect::init_shader() {
|
||||||
|
@ -170,4 +172,7 @@ void SparkEffect::init_shader() {
|
||||||
"(1.0 - lerpVal); \n"
|
"(1.0 - lerpVal); \n"
|
||||||
" } \n"
|
" } \n"
|
||||||
"} \n");
|
"} \n");
|
||||||
|
uniform_screen_height = GetShaderLocation(shader.value(), "screen_height");
|
||||||
|
uniform_spark_radius = GetShaderLocation(shader.value(), "spark_radius");
|
||||||
|
uniform_spark_pos = GetShaderLocation(shader.value(), "spark_pos");
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,9 @@ class SparkEffect {
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::optional<Shader> shader;
|
static std::optional<Shader> shader;
|
||||||
|
static int uniform_screen_height;
|
||||||
|
static int uniform_spark_radius;
|
||||||
|
static int uniform_spark_pos;
|
||||||
std::vector<Spark> sparks;
|
std::vector<Spark> sparks;
|
||||||
Color color;
|
Color color;
|
||||||
float lifetime;
|
float lifetime;
|
||||||
|
|
Loading…
Reference in a new issue