Refactor fetching uniform handles for shaders
Build and Publish WASM version of demo / Build-And-Deploy-main (push) Successful in 25s Details
Build and Publish WASM version of demo / Build-And-Deploy-devel (push) Has been skipped Details

Fetch uniform handles only once instead of every draw for
ElectricityEffect and SparkEffect.
This commit is contained in:
Stephen Seo 2023-09-04 12:07:15 +09:00
parent 8164c6633f
commit a7f25e851e
4 changed files with 49 additions and 27 deletions

View File

@ -31,6 +31,12 @@
#include "ems.h"
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,
int line_count, float lifetime,
@ -160,10 +166,10 @@ void ElectricityEffect::update_shader_height() {
if (!shader.has_value()) {
init_shader();
}
int uniform_loc = GetShaderLocation(get_shader(), "screen_height");
float height = GetScreenHeight();
DEBUG_PRINT_FLOAT(height);
SetShaderValue(get_shader(), uniform_loc, &height, SHADER_UNIFORM_FLOAT);
// DEBUG_PRINT_FLOAT(height);
SetShaderValue(get_shader(), uniform_screen_height, &height,
SHADER_UNIFORM_FLOAT);
}
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()) {
init_shader();
}
int uniform_loc = GetShaderLocation(get_shader(), "sidePosA");
DEBUG_PRINT_VEC2(a);
SetShaderValue(get_shader(), uniform_loc, &a, SHADER_UNIFORM_VEC2);
uniform_loc = GetShaderLocation(get_shader(), "sideDirA");
DEBUG_PRINT_VEC2(adir);
SetShaderValue(get_shader(), uniform_loc, &adir, SHADER_UNIFORM_VEC2);
uniform_loc = GetShaderLocation(get_shader(), "sidePosB");
DEBUG_PRINT_VEC2(b);
SetShaderValue(get_shader(), uniform_loc, &b, SHADER_UNIFORM_VEC2);
uniform_loc = GetShaderLocation(get_shader(), "sideDirB");
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);
// DEBUG_PRINT_VEC2(a);
SetShaderValue(get_shader(), uniform_side_pos_a, &a, SHADER_UNIFORM_VEC2);
// DEBUG_PRINT_VEC2(adir);
SetShaderValue(get_shader(), uniform_side_dir_a, &adir, SHADER_UNIFORM_VEC2);
// DEBUG_PRINT_VEC2(b);
SetShaderValue(get_shader(), uniform_side_pos_b, &b, SHADER_UNIFORM_VEC2);
// DEBUG_PRINT_VEC2(bdir);
SetShaderValue(get_shader(), uniform_side_dir_b, &bdir, SHADER_UNIFORM_VEC2);
// DEBUG_PRINT_FLOAT(width);
SetShaderValue(get_shader(), uniform_width, &width, SHADER_UNIFORM_FLOAT);
}
void ElectricityEffect::init_shader() {
@ -257,4 +258,11 @@ void ElectricityEffect::init_shader() {
" gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); \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");
}

View File

@ -35,6 +35,12 @@ class ElectricityEffect {
};
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;
Vector3 center;
Color color;

View File

@ -28,6 +28,9 @@
#include "ems.h"
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,
float pos_xz_variance, float radius, Color color)
@ -101,10 +104,10 @@ void SparkEffect::update_shader_height() {
if (!shader.has_value()) {
init_shader();
}
int uniform_loc = GetShaderLocation(get_shader(), "screen_height");
float height = GetScreenHeight();
DEBUG_PRINT_FLOAT(height);
SetShaderValue(get_shader(), uniform_loc, &height, SHADER_UNIFORM_FLOAT);
// DEBUG_PRINT_FLOAT(height);
SetShaderValue(get_shader(), uniform_screen_height, &height,
SHADER_UNIFORM_FLOAT);
}
void SparkEffect::update_shader_uniforms(float radius, Vector2 pos) {
@ -112,12 +115,11 @@ void SparkEffect::update_shader_uniforms(float radius, Vector2 pos) {
init_shader();
}
int uniform_loc = GetShaderLocation(get_shader(), "spark_radius");
DEBUG_PRINT_FLOAT(radius);
SetShaderValue(get_shader(), uniform_loc, &radius, SHADER_UNIFORM_FLOAT);
uniform_loc = GetShaderLocation(get_shader(), "spark_pos");
DEBUG_PRINT_VEC2(pos);
SetShaderValue(get_shader(), uniform_loc, &pos, SHADER_UNIFORM_VEC2);
// DEBUG_PRINT_FLOAT(radius);
SetShaderValue(get_shader(), uniform_spark_radius, &radius,
SHADER_UNIFORM_FLOAT);
// DEBUG_PRINT_VEC2(pos);
SetShaderValue(get_shader(), uniform_spark_pos, &pos, SHADER_UNIFORM_VEC2);
}
void SparkEffect::init_shader() {
@ -170,4 +172,7 @@ void SparkEffect::init_shader() {
"(1.0 - lerpVal); \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");
}

View File

@ -34,6 +34,9 @@ class SparkEffect {
};
static std::optional<Shader> shader;
static int uniform_screen_height;
static int uniform_spark_radius;
static int uniform_spark_pos;
std::vector<Spark> sparks;
Color color;
float lifetime;