Use a big triangle instead of 2 to cover full screen.

This allows to rasterize a single primitive instead of two, and avoid
trashing the cache between the 2 triangles drawing.
This commit is contained in:
vlj 2014-06-03 19:56:59 +02:00
parent d4fe539385
commit e5a5c78045
12 changed files with 110 additions and 105 deletions

View File

@ -1,22 +1,16 @@
uniform sampler2D tex;
uniform float low;
#if __VERSION__ >= 130
in vec2 uv;
out vec4 FragColor;
#else
varying vec2 uv;
#define FragColor gl_FragColor
#endif
vec3 getCIEYxy(vec3 rgbColor);
void main()
{
vec3 col = texture(tex, uv).xyz;
float luma = getCIEYxy(col).x;
vec2 uv = gl_FragCoord.xy / 512;
vec3 col = texture(tex, uv).xyz;
float luma = getCIEYxy(col).x;
col *= smoothstep(1., 10., luma);
FragColor = vec4(col, 1.0);
col *= smoothstep(1., 10., luma);
FragColor = vec4(col, 1.0);
}

View File

@ -11,7 +11,6 @@ layout (std140) uniform MatrixesData
vec2 screen;
};
in vec2 uv;
out vec4 FragColor;
float focalDepth = 10.;
@ -20,6 +19,7 @@ float range = 100.;
void main()
{
vec2 uv = gl_FragCoord.xy / screen;
float curdepth = texture(dtex, uv).x;
vec4 FragPos = InverseProjectionMatrix * (2.0f * vec4(uv, curdepth, 1.0f) - 1.0f);
FragPos /= FragPos.w;

View File

@ -4,15 +4,14 @@ uniform sampler2D tex;
uniform vec2 pixel;
uniform float sigma = 5.;
in vec2 uv;
out vec4 FragColor;
void main()
{
vec2 uv = gl_FragCoord.xy * pixel;
float X = uv.x;
float Y = uv.y;
float g0, g1, g2;
g0 = 1.0 / (sqrt(2.0 * 3.14) * sigma);
g1 = exp(-0.5 / (sigma * sigma));

View File

@ -4,15 +4,14 @@ uniform sampler2D tex;
uniform vec2 pixel;
uniform float sigma = 5.;
in vec2 uv;
out vec4 FragColor;
void main()
{
vec2 uv = gl_FragCoord.xy * pixel;
float X = uv.x;
float Y = uv.y;
float g0, g1, g2;
g0 = 1.0 / (sqrt(2.0 * 3.14) * sigma);
g1 = exp(-0.5 / (sigma * sigma));

View File

@ -3,25 +3,20 @@ uniform vec2 pixel;
// Gaussian separated blur with radius 3.
#if __VERSION__ >= 130
in vec2 uv;
out vec4 FragColor;
#else
varying vec2 uv;
#define FragColor gl_FragColor
#endif
void main()
{
vec4 sum = vec4(0.0);
float X = uv.x;
float Y = uv.y;
vec2 uv = gl_FragCoord.xy * pixel;
vec4 sum = vec4(0.0);
float X = uv.x;
float Y = uv.y;
sum += texture(tex, vec2(X - 3.0 * pixel.x, Y)) * 0.03125;
sum += texture(tex, vec2(X - 1.3333 * pixel.x, Y)) * 0.328125;
sum += texture(tex, vec2(X, Y)) * 0.273438;
sum += texture(tex, vec2(X + 1.3333 * pixel.x, Y)) * 0.328125;
sum += texture(tex, vec2(X + 3.0 * pixel.x, Y)) * 0.03125;
sum += texture(tex, vec2(X - 3.0 * pixel.x, Y)) * 0.03125;
sum += texture(tex, vec2(X - 1.3333 * pixel.x, Y)) * 0.328125;
sum += texture(tex, vec2(X, Y)) * 0.273438;
sum += texture(tex, vec2(X + 1.3333 * pixel.x, Y)) * 0.328125;
sum += texture(tex, vec2(X + 3.0 * pixel.x, Y)) * 0.03125;
FragColor = sum;
FragColor = sum;
}

View File

@ -3,25 +3,20 @@ uniform vec2 pixel;
// Gaussian separated blur with radius 3.
#if __VERSION__ >= 130
in vec2 uv;
out vec4 FragColor;
#else
varying vec2 uv;
#define FragColor gl_FragColor
#endif
void main()
{
vec4 sum = vec4(0.0);
float X = uv.x;
float Y = uv.y;
vec2 uv = gl_FragCoord.xy * pixel;
vec4 sum = vec4(0.0);
float X = uv.x;
float Y = uv.y;
sum += texture(tex, vec2(X, Y - 3.0 * pixel.y)) * 0.03125;
sum += texture(tex, vec2(X, Y - 1.3333 * pixel.y)) * 0.328125;
sum += texture(tex, vec2(X, Y)) * 0.273438;
sum += texture(tex, vec2(X, Y + 1.3333 * pixel.y)) * 0.328125;
sum += texture(tex, vec2(X, Y + 3.0 * pixel.y)) * 0.03125;
sum += texture(tex, vec2(X, Y - 3.0 * pixel.y)) * 0.03125;
sum += texture(tex, vec2(X, Y - 1.3333 * pixel.y)) * 0.328125;
sum += texture(tex, vec2(X, Y)) * 0.273438;
sum += texture(tex, vec2(X, Y + 1.3333 * pixel.y)) * 0.328125;
sum += texture(tex, vec2(X, Y + 3.0 * pixel.y)) * 0.03125;
FragColor = sum;
FragColor = sum;
}

View File

@ -3,27 +3,22 @@ uniform vec2 pixel;
// Gaussian separated blur with radius 6.
#if __VERSION__ >= 130
in vec2 uv;
out vec4 FragColor;
#else
varying vec2 uv;
#define FragColor gl_FragColor
#endif
void main()
{
vec4 sum = vec4(0.0);
float X = uv.x;
float Y = uv.y;
vec2 uv = gl_FragCoord.xy * pixel;
vec4 sum = vec4(0.0);
float X = uv.x;
float Y = uv.y;
sum += texture(tex, vec2(X - 5.13333 * pixel.x, Y)) * 0.00640869;
sum += texture(tex, vec2(X - 3.26667 * pixel.x, Y)) * 0.083313;
sum += texture(tex, vec2(X - 1.4 * pixel.x, Y)) * 0.305481;
sum += texture(tex, vec2(X, Y)) * 0.209473;
sum += texture(tex, vec2(X + 1.4 * pixel.x, Y)) * 0.305481;
sum += texture(tex, vec2(X + 3.26667 * pixel.x, Y)) * 0.083313;
sum += texture(tex, vec2(X + 5.13333 * pixel.x, Y)) * 0.00640869;
sum += texture(tex, vec2(X - 5.13333 * pixel.x, Y)) * 0.00640869;
sum += texture(tex, vec2(X - 3.26667 * pixel.x, Y)) * 0.083313;
sum += texture(tex, vec2(X - 1.4 * pixel.x, Y)) * 0.305481;
sum += texture(tex, vec2(X, Y)) * 0.209473;
sum += texture(tex, vec2(X + 1.4 * pixel.x, Y)) * 0.305481;
sum += texture(tex, vec2(X + 3.26667 * pixel.x, Y)) * 0.083313;
sum += texture(tex, vec2(X + 5.13333 * pixel.x, Y)) * 0.00640869;
FragColor = sum;
FragColor = sum;
}

View File

@ -3,27 +3,22 @@ uniform vec2 pixel;
// Gaussian separated blur with radius 6.
#if __VERSION__ >= 130
in vec2 uv;
out vec4 FragColor;
#else
varying vec2 uv;
#define FragColor gl_FragColor
#endif
void main()
{
vec4 sum = vec4(0.0);
float X = uv.x;
float Y = uv.y;
vec2 uv = gl_FragCoord.xy * pixel;
vec4 sum = vec4(0.0);
float X = uv.x;
float Y = uv.y;
sum += texture(tex, vec2(X, Y - 5.13333 * pixel.y)) * 0.00640869;
sum += texture(tex, vec2(X, Y - 3.26667 * pixel.y)) * 0.083313;
sum += texture(tex, vec2(X, Y - 1.4 * pixel.y)) * 0.305481;
sum += texture(tex, vec2(X, Y)) * 0.209473;
sum += texture(tex, vec2(X, Y + 1.4 * pixel.y)) * 0.305481;
sum += texture(tex, vec2(X, Y + 3.26667 * pixel.y)) * 0.083313;
sum += texture(tex, vec2(X, Y + 5.13333 * pixel.y)) * 0.00640869;
sum += texture(tex, vec2(X, Y - 5.13333 * pixel.y)) * 0.00640869;
sum += texture(tex, vec2(X, Y - 3.26667 * pixel.y)) * 0.083313;
sum += texture(tex, vec2(X, Y - 1.4 * pixel.y)) * 0.305481;
sum += texture(tex, vec2(X, Y)) * 0.209473;
sum += texture(tex, vec2(X, Y + 1.4 * pixel.y)) * 0.305481;
sum += texture(tex, vec2(X, Y + 3.26667 * pixel.y)) * 0.083313;
sum += texture(tex, vec2(X, Y + 5.13333 * pixel.y)) * 0.00640869;
FragColor = sum;
FragColor = sum;
}

View File

@ -2,11 +2,21 @@ uniform sampler2D tex;
uniform float zn;
uniform float zf;
in vec2 uv;
layout (std140) uniform MatrixesData
{
mat4 ViewMatrix;
mat4 ProjectionMatrix;
mat4 InverseViewMatrix;
mat4 InverseProjectionMatrix;
mat4 ShadowViewProjMatrixes[4];
vec2 screen;
};
out float Depth;
void main()
{
vec2 uv = gl_FragCoord.xy / screen;
float d = texture(tex, uv).x;
float c0 = zn * zf, c1 = zn - zf, c2 = zf;
Depth = c0 / (d * c1 + c2);

View File

@ -22,7 +22,6 @@ layout (std140) uniform MatrixesData
};
#endif
in vec2 uv;
out float AO;
const float sigma = 1.;
@ -46,6 +45,7 @@ vec3 getXcYcZc(int x, int y, float zC)
void main(void)
{
vec2 uv = gl_FragCoord.xy / screen;
float lineardepth = textureLod(dtex, uv, 0.).x;
int x = int(gl_FragCoord.x), y = int(gl_FragCoord.y);
vec3 FragPos = getXcYcZc(x, y, lineardepth);

View File

@ -214,7 +214,7 @@ void renderBloom(GLuint in)
setTexture(0, in, GL_NEAREST, GL_NEAREST);
FullScreenShader::BloomShader::setUniforms(0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
static
@ -362,7 +362,7 @@ void PostProcessing::renderGaussian3Blur(FrameBuffer &in_fbo, FrameBuffer &auxil
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(FullScreenShader::Gaussian3VBlurShader::uniform_tex, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
{
in_fbo.Bind();
@ -376,7 +376,7 @@ void PostProcessing::renderGaussian3Blur(FrameBuffer &in_fbo, FrameBuffer &auxil
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(FullScreenShader::Gaussian3HBlurShader::uniform_tex, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
}
@ -396,7 +396,7 @@ void PostProcessing::renderGaussian6Blur(FrameBuffer &in_fbo, FrameBuffer &auxil
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(FullScreenShader::Gaussian6VBlurShader::uniform_tex, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
{
in_fbo.Bind();
@ -410,7 +410,7 @@ void PostProcessing::renderGaussian6Blur(FrameBuffer &in_fbo, FrameBuffer &auxil
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(FullScreenShader::Gaussian6HBlurShader::uniform_tex, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
}
@ -430,7 +430,7 @@ void PostProcessing::renderGaussian17TapBlur(FrameBuffer &in_fbo, FrameBuffer &a
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(FullScreenShader::Gaussian17TapHShader::uniform_tex, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
{
in_fbo.Bind();
@ -444,7 +444,7 @@ void PostProcessing::renderGaussian17TapBlur(FrameBuffer &in_fbo, FrameBuffer &a
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(FullScreenShader::Gaussian17TapVShader::uniform_tex, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
}
@ -489,7 +489,7 @@ void PostProcessing::renderSSAO()
glBindVertexArray(FullScreenShader::LinearizeDepthShader::vao);
setTexture(0, irr_driver->getDepthStencilTexture(), GL_LINEAR, GL_LINEAR);
FullScreenShader::LinearizeDepthShader::setUniforms(irr_driver->getSceneManager()->getActiveCamera()->getNearValue(), irr_driver->getSceneManager()->getActiveCamera()->getFarValue(), 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
irr_driver->getFBO(FBO_SSAO).Bind();
if (!noise_tex)
@ -498,15 +498,15 @@ void PostProcessing::renderSSAO()
glUseProgram(FullScreenShader::SSAOShader::Program);
glBindVertexArray(FullScreenShader::SSAOShader::vao);
setTexture(0, irr_driver->getRenderTargetTexture(RTT_LINEAR_DEPTH), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
setTexture(0, irr_driver->getRenderTargetTexture(RTT_LINEAR_DEPTH), GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
setTexture(1, getTextureGLuint(noise_tex), GL_LINEAR, GL_LINEAR);
FullScreenShader::SSAOShader::setUniforms(core::vector2df(float(UserConfigParams::m_width),
FullScreenShader::SSAOShader::setUniforms(core::vector2df(float(UserConfigParams::m_width),
float(UserConfigParams::m_height)),
0, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void PostProcessing::renderFog()
@ -620,7 +620,7 @@ static void renderDoF(FrameBuffer &fbo, GLuint rtt)
setTexture(1, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST);
FullScreenShader::DepthOfFieldShader::setUniforms(0, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
static void averageTexture(GLuint tex)

View File

@ -50,7 +50,7 @@ Shaders::Shaders()
loadShaders();
}
GLuint quad_vbo;
GLuint quad_vbo, tri_vbo;
static void initQuadVBO()
{
@ -64,6 +64,16 @@ static void initQuadVBO()
glBindBuffer(GL_ARRAY_BUFFER, quad_vbo);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad_vertex, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
const float tri_vertex[] = {
-1., -1.,
-1., 3.,
3., -1.,
};
glGenBuffers(1, &tri_vbo);
glBindBuffer(GL_ARRAY_BUFFER, tri_vbo);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), tri_vertex, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// It should be possible to merge it with previous one...
@ -2031,6 +2041,19 @@ namespace ParticleShader
}
}
static GLuint createFullScreenVAO(GLuint Program)
{
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint attrib_position = glGetAttribLocation(Program, "Position");
glBindBuffer(GL_ARRAY_BUFFER, tri_vbo);
glEnableVertexAttribArray(attrib_position);
glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
glBindVertexArray(0);
return vao;
}
static GLuint createVAO(GLuint Program)
{
GLuint vao;
@ -2059,7 +2082,7 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getCIEXYZ.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/bloom.frag").c_str());
uniform_texture = glGetUniformLocation(Program, "tex");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
}
void BloomShader::setUniforms(unsigned TU_tex)
@ -2125,7 +2148,7 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/dof.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_depth = glGetUniformLocation(Program, "dtex");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
}
@ -2430,7 +2453,7 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/gaussian17taph.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_pixel = glGetUniformLocation(Program, "pixel");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
}
GLuint Gaussian6HBlurShader::Program;
@ -2444,7 +2467,7 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/gaussian6h.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_pixel = glGetUniformLocation(Program, "pixel");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
}
GLuint Gaussian3HBlurShader::Program;
@ -2458,7 +2481,7 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/gaussian3h.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_pixel = glGetUniformLocation(Program, "pixel");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
}
GLuint Gaussian17TapVShader::Program;
@ -2472,7 +2495,7 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/gaussian17tapv.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_pixel = glGetUniformLocation(Program, "pixel");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
}
GLuint Gaussian6VBlurShader::Program;
@ -2486,7 +2509,7 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/gaussian6v.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_pixel = glGetUniformLocation(Program, "pixel");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
}
GLuint Gaussian3VBlurShader::Program;
@ -2500,7 +2523,7 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/gaussian3v.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_pixel = glGetUniformLocation(Program, "pixel");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
}
GLuint PassThroughShader::Program;
@ -2528,7 +2551,7 @@ namespace FullScreenShader
uniform_texture = glGetUniformLocation(Program, "texture");
uniform_zf = glGetUniformLocation(Program, "zf");
uniform_zn = glGetUniformLocation(Program, "zn");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
}
void LinearizeDepthShader::setUniforms(float zn, float zf, unsigned TU_tex)
@ -2569,7 +2592,7 @@ namespace FullScreenShader
uniform_dtex = glGetUniformLocation(Program, "dtex");
uniform_noise_texture = glGetUniformLocation(Program, "noise_texture");
uniform_samplePoints = glGetUniformLocation(Program, "samplePoints[0]");
vao = createVAO(Program);
vao = createFullScreenVAO(Program);
if (!UserConfigParams::m_ubo_disabled)
{
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");