Skip to content

Commit e7daf51

Browse files
committed
Improve OpenGL performance by only calculating displacement textures when needed
It would appear that atm its setting a 1x1 grey texture which isn't too much of an issue, but I don't believe that works well. Let's try this.
1 parent baafd6b commit e7daf51

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

data/shader/shader100.frag

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ uniform float backbuffer;
1010
uniform float game_time;
1111
uniform vec2 animate;
1212
uniform vec2 displacement_animate;
13+
uniform bool is_displacement;
1314

1415
varying vec2 texcoord_var;
1516
varying vec4 diffuse_var;
1617

1718
void main(void)
1819
{
19-
if (backbuffer == 0.0)
20+
if (backbuffer == 0.0 || !is_displacement)
2021
{
2122
vec4 color = diffuse_var * texture2D(diffuse_texture, texcoord_var.st + (animate * game_time));
2223
gl_FragColor = color;

data/shader/shader330.frag

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ uniform float backbuffer;
88
uniform float game_time;
99
uniform vec2 animate;
1010
uniform vec2 displacement_animate;
11+
uniform bool is_displacement;
1112

1213
in vec4 diffuse_var;
1314
in vec2 texcoord_var;
@@ -16,12 +17,12 @@ out vec4 fragColor;
1617

1718
void main(void)
1819
{
19-
if (backbuffer == 0.0)
20+
if (backbuffer == 0.0 || !is_displacement)
2021
{
2122
vec4 color = diffuse_var * texture(diffuse_texture, texcoord_var.st + (animate * game_time));
2223
fragColor = color;
2324
}
24-
else if (true)
25+
else if (is_displacement)
2526
{
2627
vec4 pixel = texture(displacement_texture, texcoord_var.st + (displacement_animate * game_time));
2728
vec2 displacement = (pixel.rg - vec2(0.5, 0.5)) * 255;
@@ -34,22 +35,6 @@ void main(void)
3435
vec4 color = diffuse_var * texture(diffuse_texture, texcoord_var.st + (animate * game_time));
3536
fragColor = vec4(mix(color.rgb, back_color.rgb, alpha), color.a);
3637
}
37-
else
38-
{
39-
// water reflection
40-
vec4 color = diffuse_var * texture(diffuse_texture, texcoord_var.st);
41-
vec2 uv = (fragcoord2uv * gl_FragCoord.xyw).xy + vec2(0, 0.05);
42-
uv.x = uv.x + 0.005 * sin(game_time + uv.y * 100);
43-
uv = vec2(uv.x, 1.0 - uv.y);
44-
vec4 back_color = texture(framebuffer_texture, uv);
45-
if (backbuffer == 0.0)
46-
fragColor = color;
47-
else
48-
if (uv.y > 0.5)
49-
fragColor = vec4(mix(vec3(0,0,0.75), mix(color.rgb, back_color.rgb, 0.95 * backbuffer), (1.2 - uv.y) * (1.2 - uv.y)), 1.0);
50-
else
51-
fragColor = color;
52-
}
5338
}
5439

5540
/* EOF */

src/video/gl/gl33core_context.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
#include "supertux/globals.hpp"
2020
#include "video/color.hpp"
21+
#include "video/gl.hpp"
2122
#include "video/gl/gl_program.hpp"
2223
#include "video/gl/gl_texture.hpp"
2324
#include "video/gl/gl_texture_renderer.hpp"
2425
#include "video/gl/gl_vertex_arrays.hpp"
2526
#include "video/gl/gl_video_system.hpp"
2627
#include "video/glutil.hpp"
28+
#include <iostream>
2729

2830
GL33CoreContext::GL33CoreContext(GLVideoSystem& video_system) :
2931
m_video_system(video_system),
@@ -185,6 +187,7 @@ GL33CoreContext::bind_texture(const Texture& texture, const Texture* displacemen
185187
{
186188
glActiveTexture(GL_TEXTURE0);
187189
glBindTexture(GL_TEXTURE_2D, m_transparent_texture->get_handle());
190+
glUniform1i(m_program->get_is_displacement_location(), true);
188191
}
189192
else
190193
{
@@ -197,6 +200,7 @@ GL33CoreContext::bind_texture(const Texture& texture, const Texture* displacemen
197200
animate.y /= static_cast<float>(texture.get_image_height());
198201

199202
glUniform2f(m_program->get_animate_location(), animate.x, animate.y);
203+
glUniform1i(m_program->get_is_displacement_location(), false);
200204
}
201205

202206
if (displacement_texture)
@@ -210,11 +214,13 @@ GL33CoreContext::bind_texture(const Texture& texture, const Texture* displacemen
210214
animate.y /= static_cast<float>(displacement_texture->get_image_height());
211215

212216
glUniform2f(m_program->get_displacement_animate_location(), animate.x, animate.y);
217+
glUniform1i(m_program->get_is_displacement_location(), true);
213218
}
214219
else
215220
{
216221
glActiveTexture(GL_TEXTURE1);
217222
glBindTexture(GL_TEXTURE_2D, m_grey_texture->get_handle());
223+
glUniform1i(m_program->get_is_displacement_location(), false);
218224
}
219225

220226
assert_gl();

src/video/gl/gl_program.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "video/gl/gl_program.hpp"
1818

19+
#include <iostream>
1920
#include <sstream>
2021

2122
#include "video/glutil.hpp"
@@ -35,7 +36,8 @@ GLProgram::GLProgram() :
3536
m_displacement_animate_location(-1),
3637
m_position_location(-1),
3738
m_texcoord_location(-1),
38-
m_diffuse_location(-1)
39+
m_diffuse_location(-1),
40+
m_is_displacement_location(-1)
3941
{
4042
assert_gl();
4143

@@ -71,6 +73,7 @@ GLProgram::GLProgram() :
7173
m_modelviewprojection_location = glGetUniformLocation(m_program, "modelviewprojection");
7274
m_animate_location = glGetUniformLocation(m_program, "animate");
7375
m_displacement_animate_location = glGetUniformLocation(m_program, "displacement_animate");
76+
m_is_displacement_location = glGetUniformLocation(m_program, "is_displacement");
7477
m_position_location = glGetAttribLocation(m_program, "position");
7578
m_texcoord_location = glGetAttribLocation(m_program, "texcoord");
7679
m_diffuse_location = glGetAttribLocation(m_program, "diffuse");

src/video/gl/gl_program.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class GLProgram final
4545
inline GLint get_position_location() const { return check_valid(m_position_location, "position"); }
4646
inline GLint get_texcoord_location() const { return check_valid(m_texcoord_location, "texcoord"); }
4747
inline GLint get_diffuse_location() const { return check_valid(m_diffuse_location, "diffuse"); }
48+
inline GLint get_is_displacement_location() const { return check_valid(m_is_displacement_location, "is_displacement"); }
4849

4950
private:
5051
bool get_link_status() const;
@@ -70,6 +71,7 @@ class GLProgram final
7071
GLint m_position_location;
7172
GLint m_texcoord_location;
7273
GLint m_diffuse_location;
74+
GLint m_is_displacement_location;
7375

7476
private:
7577
GLProgram(const GLProgram&) = delete;

0 commit comments

Comments
 (0)