]> git.seodisparate.com - jumpartifact.com_demo_0/commitdiff
Refactor fetching uniform handles for shaders
authorStephen Seo <seo.disparate@gmail.com>
Mon, 4 Sep 2023 03:07:15 +0000 (12:07 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 4 Sep 2023 03:07:15 +0000 (12:07 +0900)
Fetch uniform handles only once instead of every draw for
ElectricityEffect and SparkEffect.

src/electricity_effect.cc
src/electricity_effect.h
src/spark_effect.cc
src/spark_effect.h

index 5b0ed59dc6bb0ba0a7179ca5c233cf93c167bebb..3bdb466364740a39d3c5d690905b58c89126cd0a 100644 (file)
 #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");
 }
index 4bd816ab794a49baa7ba8f424b3fbbc2188450f1..c6cd7d3cfb7990f2a301eca4d1c61bbd0ed8758d 100644 (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;
index 5e8672d2fd81f8150c8dcaa383595172a7e3dfff..678b4caa838c8d9baef3244aec8f0321e497ea01 100644 (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");
 }
index 0614f3f424d9bdd1c1c076aee3a7f319891475a7..4f87b28dd13362fb16016d0ee7c507e593c80a29 100644 (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;