From 0e5b7b532c46103851c5706773e7d1b193cc1ff3 Mon Sep 17 00:00:00 2001 From: Benau Date: Wed, 7 Feb 2018 12:49:45 +0800 Subject: [PATCH] Further boost stk by writing 1 less framebuffer attachment Now we can get determine the background color by depth, so the alpha tricks in diffuse color fbo is not necessary And using rgba8 to pack normal doesn't seem to cause visual glitches. --- data/shaders/combine_diffuse_color.frag | 15 ++++++------ data/shaders/sp_alpha_test.frag | 14 ++++++----- data/shaders/sp_decal.frag | 14 ++++++----- data/shaders/sp_grass.frag | 13 ++++++---- data/shaders/sp_normal_map.frag | 28 +++++++++++++++------- data/shaders/sp_solid.frag | 14 ++++++----- data/shaders/sp_unlit.frag | 13 ++++++---- data/shaders/utils/sp_normalMapOutput.frag | 21 ---------------- src/graphics/rtts.cpp | 4 +--- src/graphics/rtts.hpp | 1 - src/graphics/shader_based_renderer.cpp | 10 ++++---- 11 files changed, 73 insertions(+), 74 deletions(-) delete mode 100644 data/shaders/utils/sp_normalMapOutput.frag diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index 6c4815b20..3afbec781 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -1,7 +1,7 @@ uniform sampler2D diffuse_map; uniform sampler2D specular_map; uniform sampler2D ssao_tex; -uniform sampler2D gloss_map; +uniform sampler2D normal_color; uniform sampler2D diffuse_color; uniform sampler2D depth_stencil; uniform sampler2D light_scatter; @@ -17,10 +17,11 @@ void main() vec2 tc = gl_FragCoord.xy / u_screen; vec4 diffuseMatColor = texture(diffuse_color, tc); - // Gloss map here is stored in red and green for metallic and emit map - // Real gloss channel is stored in normal and depth framebuffer .z - float metallicMapValue = texture(gloss_map, tc).x; - float emitMapValue = texture(gloss_map, tc).y; + // Polish map is stored in normal color framebuffer .z + // Metallic map is stored in normal color framebuffer .w + // Emit map is stored in diffuse color framebuffer.w + float metallicMapValue = texture(normal_color, tc).w; + float emitMapValue = diffuseMatColor.w; float ao = texture(ssao_tex, tc).x; vec3 DiffuseComponent = texture(diffuse_map, tc).xyz; @@ -30,8 +31,8 @@ void main() vec3 metallicMatColor = mix(vec3(0.04), diffuse_color_for_mix, metallicMapValue); vec3 tmp = DiffuseComponent * mix(diffuseMatColor.xyz, vec3(0.0), metallicMapValue) + (metallicMatColor * SpecularComponent); - vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * pow(emitMapValue, 2.) * 10.); - vec4 color_1 = vec4(tmp * ao + (emitMapValue * emitCol), diffuseMatColor.a); + vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * emitMapValue * emitMapValue * 10.0); + vec4 color_1 = vec4(tmp * ao + (emitMapValue * emitCol), 1.0); // Fog float depth = texture(depth_stencil, tc).x; diff --git a/data/shaders/sp_alpha_test.frag b/data/shaders/sp_alpha_test.frag index f344c95c3..374adc072 100644 --- a/data/shaders/sp_alpha_test.frag +++ b/data/shaders/sp_alpha_test.frag @@ -5,8 +5,7 @@ in vec3 normal; in vec2 uv; layout(location = 0) out vec4 o_diffuse_color; -layout(location = 1) out vec3 o_normal_depth; -layout(location = 2) out vec2 o_gloss_map; +layout(location = 1) out vec4 o_normal_color; #stk_include "utils/encode_normal.frag" #stk_include "utils/rgb_conversion.frag" @@ -28,12 +27,15 @@ void main(void) vec3 new_color = hsvToRgb(vec3(new_xy.x, new_xy.y, old_hsv.z)); col = vec4(new_color.r, new_color.g, new_color.b, col.a); } - o_diffuse_color = vec4(col.xyz, 1.0); #if defined(Advanced_Lighting_Enabled) vec4 layer_2 = sampleTextureLayer2(uv); - o_normal_depth.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; - o_normal_depth.z = layer_2.x; - o_gloss_map = layer_2.yz; + o_diffuse_color = vec4(col.xyz, layer_2.z); + + o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; + o_normal_color.zw = layer_2.xy; +#else + o_diffuse_color = vec4(col.xyz, 1.0); #endif + } diff --git a/data/shaders/sp_decal.frag b/data/shaders/sp_decal.frag index 3886e93cf..a0e2636de 100644 --- a/data/shaders/sp_decal.frag +++ b/data/shaders/sp_decal.frag @@ -3,8 +3,7 @@ in vec2 uv; in vec2 uv_two; layout(location = 0) out vec4 o_diffuse_color; -layout(location = 1) out vec3 o_normal_depth; -layout(location = 2) out vec2 o_gloss_map; +layout(location = 1) out vec4 o_normal_color; #stk_include "utils/encode_normal.frag" #stk_include "utils/sp_texture_sampling.frag" @@ -16,12 +15,15 @@ void main(void) layer_two_tex.rgb = layer_two_tex.a * layer_two_tex.rgb; vec3 final_color = layer_two_tex.rgb + color.rgb * (1.0 - layer_two_tex.a); - o_diffuse_color = vec4(final_color, 1.0); #if defined(Advanced_Lighting_Enabled) vec4 layer_2 = sampleTextureLayer2(uv); - o_normal_depth.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; - o_normal_depth.z = layer_2.x; - o_gloss_map = layer_2.yz; + o_diffuse_color = vec4(final_color, layer_2.z); + + o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; + o_normal_color.zw = layer_2.xy; +#else + o_diffuse_color = vec4(final_color, 1.0); #endif + } diff --git a/data/shaders/sp_grass.frag b/data/shaders/sp_grass.frag index 412e7a7a9..6605979fd 100644 --- a/data/shaders/sp_grass.frag +++ b/data/shaders/sp_grass.frag @@ -4,8 +4,7 @@ in vec3 normal; in vec2 uv; layout(location = 0) out vec4 o_diffuse_color; -layout(location = 1) out vec3 o_normal_depth; -layout(location = 2) out vec2 o_gloss_map; +layout(location = 1) out vec4 o_normal_color; #stk_include "utils/encode_normal.frag" #stk_include "utils/rgb_conversion.frag" @@ -30,8 +29,12 @@ void main(void) #if defined(Advanced_Lighting_Enabled) vec4 layer_2 = sampleTextureLayer2(uv); - o_normal_depth.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; - o_normal_depth.z = layer_2.x; - o_gloss_map = 0.1 * layer_2.yz; + o_diffuse_color = vec4(col.xyz, layer_2.z); + + o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; + o_normal_color.zw = layer_2.xy; +#else + o_diffuse_color = vec4(col.xyz, 1.0); #endif + } diff --git a/data/shaders/sp_normal_map.frag b/data/shaders/sp_normal_map.frag index c98497748..60a7ddfaa 100644 --- a/data/shaders/sp_normal_map.frag +++ b/data/shaders/sp_normal_map.frag @@ -1,16 +1,16 @@ -in float hue_change; - +in vec3 bitangent; in vec4 color; +in float hue_change; +in vec3 normal; +in vec3 tangent; in vec2 uv; layout(location = 0) out vec4 o_diffuse_color; -layout(location = 1) out vec3 o_normal_depth; -layout(location = 2) out vec2 o_gloss_map; +layout(location = 1) out vec4 o_normal_color; #stk_include "utils/encode_normal.frag" #stk_include "utils/rgb_conversion.frag" #stk_include "utils/sp_texture_sampling.frag" -#stk_include "utils/sp_normalMapOutput.frag" void main() { @@ -33,11 +33,23 @@ void main() } vec3 final_color = col.xyz * color.xyz; - o_diffuse_color = vec4(final_color, 1.0); #if defined(Advanced_Lighting_Enabled) - vec4 layer_3 = sampleTextureLayer3(uv); vec4 layer_2 = sampleTextureLayer2(uv); - outputNormalMapPbrData(layer_3.rgb, layer_2.rgb); + vec4 layer_3 = sampleTextureLayer3(uv); + o_diffuse_color = vec4(final_color, layer_2.z); + + vec3 tangent_space_normal = 2.0 * layer_3.xyz - 1.0; + vec3 frag_tangent = normalize(tangent); + vec3 frag_bitangent = normalize(bitangent); + vec3 frag_normal = normalize(normal); + mat3 t_b_n = mat3(frag_tangent, frag_bitangent, frag_normal); + + vec3 world_normal = t_b_n * tangent_space_normal; + + o_normal_color.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5; + o_normal_color.zw = layer_2.xy; +#else + o_diffuse_color = vec4(final_color, 1.0); #endif } diff --git a/data/shaders/sp_solid.frag b/data/shaders/sp_solid.frag index b9860f786..9a68416ed 100644 --- a/data/shaders/sp_solid.frag +++ b/data/shaders/sp_solid.frag @@ -5,8 +5,7 @@ in vec3 normal; in vec2 uv; layout(location = 0) out vec4 o_diffuse_color; -layout(location = 1) out vec3 o_normal_depth; -layout(location = 2) out vec2 o_gloss_map; +layout(location = 1) out vec4 o_normal_color; #stk_include "utils/encode_normal.frag" #stk_include "utils/rgb_conversion.frag" @@ -33,12 +32,15 @@ void main(void) } vec3 final_color = col.xyz * color.xyz; - o_diffuse_color = vec4(final_color, 1.0); #if defined(Advanced_Lighting_Enabled) vec4 layer_2 = sampleTextureLayer2(uv); - o_normal_depth.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; - o_normal_depth.z = layer_2.x; - o_gloss_map = layer_2.yz; + o_diffuse_color = vec4(final_color, layer_2.z); + + o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; + o_normal_color.zw = layer_2.xy; +#else + o_diffuse_color = vec4(final_color, 1.0); #endif + } diff --git a/data/shaders/sp_unlit.frag b/data/shaders/sp_unlit.frag index eb82a661b..5a82f58f7 100644 --- a/data/shaders/sp_unlit.frag +++ b/data/shaders/sp_unlit.frag @@ -3,8 +3,7 @@ in vec3 normal; in vec2 uv; layout(location = 0) out vec4 o_diffuse_color; -layout(location = 1) out vec3 o_normal_depth; -layout(location = 2) out vec2 o_gloss_map; +layout(location = 1) out vec4 o_normal_color; #stk_include "utils/encode_normal.frag" #stk_include "utils/sp_texture_sampling.frag" @@ -21,8 +20,12 @@ void main(void) o_diffuse_color = vec4(final_color, 1.0); #if defined(Advanced_Lighting_Enabled) - o_normal_depth.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; - o_normal_depth.z = 0.0; - o_gloss_map = vec2(0.0, 0.1); + o_diffuse_color = vec4(final_color, 0.4); + + o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; + o_normal_color.zw = vec2(0.0); +#else + o_diffuse_color = vec4(final_color, 1.0); #endif + } diff --git a/data/shaders/utils/sp_normalMapOutput.frag b/data/shaders/utils/sp_normalMapOutput.frag deleted file mode 100644 index 92c752b84..000000000 --- a/data/shaders/utils/sp_normalMapOutput.frag +++ /dev/null @@ -1,21 +0,0 @@ -// This function encapsulate the computation of normal maps -in vec3 tangent; -in vec3 bitangent; -in vec3 normal; - -void outputNormalMapPbrData(vec3 layer_3, vec3 layer_2) -{ - vec3 tangent_space_normal = 2.0 * layer_3.xyz - 1.0; - vec3 frag_tangent = normalize(tangent); - vec3 frag_bitangent = normalize(bitangent); - vec3 frag_normal = normalize(normal); - mat3 t_b_n = mat3(frag_tangent, frag_bitangent, frag_normal); - - vec3 world_normal = t_b_n * tangent_space_normal; - - o_normal_depth.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5; - o_normal_depth.z = layer_2.x; - o_gloss_map = layer_2.yz; -} - - diff --git a/src/graphics/rtts.cpp b/src/graphics/rtts.cpp index 78890cf0c..162e004b1 100644 --- a/src/graphics/rtts.cpp +++ b/src/graphics/rtts.cpp @@ -118,8 +118,7 @@ RTT::RTT(unsigned int width, unsigned int height, float rtt_scale, } if (CVS->isDeferredEnabled()) { - m_render_target_textures[RTT_NORMAL_AND_DEPTH] = generateRTT(res, GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV); - m_render_target_textures[RTT_SP_GLOSS] = generateRTT(res, GL_RG8, GL_RG, GL_UNSIGNED_BYTE); + m_render_target_textures[RTT_NORMAL_AND_DEPTH] = generateRTT(res, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE); m_render_target_textures[RTT_SP_DIFF_COLOR] = generateRTT(res, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE); m_render_target_textures[RTT_RGBA_2] = generateRTT(res, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE); m_render_target_textures[RTT_DIFFUSE] = generateRTT(res, diffuse_specular_internal_format, rgb_format, type); @@ -181,7 +180,6 @@ RTT::RTT(unsigned int width, unsigned int height, float rtt_scale, somevector.clear(); somevector.push_back(m_render_target_textures[RTT_SP_DIFF_COLOR]); somevector.push_back(m_render_target_textures[RTT_NORMAL_AND_DEPTH]); - somevector.push_back(m_render_target_textures[RTT_SP_GLOSS]); m_frame_buffers[FBO_SP] = new FrameBuffer(somevector, m_depth_stencil_tex, res.Width, res.Height); somevector.clear(); diff --git a/src/graphics/rtts.hpp b/src/graphics/rtts.hpp index 87adef8a4..4a456ef3f 100644 --- a/src/graphics/rtts.hpp +++ b/src/graphics/rtts.hpp @@ -65,7 +65,6 @@ enum TypeRTT : unsigned int { RTT_COLOR = 0, RTT_NORMAL_AND_DEPTH, - RTT_SP_GLOSS, RTT_SP_DIFF_COLOR, // RGBA RTT_RGBA_2, RTT_DIFFUSE, diff --git a/src/graphics/shader_based_renderer.cpp b/src/graphics/shader_based_renderer.cpp index 147e86dcf..e0f7e5e6b 100644 --- a/src/graphics/shader_based_renderer.cpp +++ b/src/graphics/shader_based_renderer.cpp @@ -207,16 +207,16 @@ public: assignSamplerNames(0, "diffuse_map", ST_NEAREST_FILTERED, 1, "specular_map", ST_NEAREST_FILTERED, 2, "ssao_tex", ST_NEAREST_FILTERED, - 3, "gloss_map", ST_NEAREST_FILTERED, + 3, "normal_color", ST_NEAREST_FILTERED, 4, "diffuse_color", ST_NEAREST_FILTERED, 5, "depth_stencil", ST_NEAREST_FILTERED, 6, "light_scatter", ST_NEAREST_FILTERED); } // CombineDiffuseColor // ------------------------------------------------------------------------ - void render(GLuint dm, GLuint sm, GLuint st, GLuint gm, GLuint dc, + void render(GLuint dm, GLuint sm, GLuint st, GLuint nt, GLuint dc, GLuint ds, GLuint lt, const std::array & bg_color) { - setTextureUnits(dm, sm, st, gm, dc, ds, lt); + setTextureUnits(dm, sm, st, nt, dc, ds, lt); drawFullScreenEffect(bg_color); } // render }; // CombineDiffuseColor @@ -259,10 +259,8 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca { m_rtts->getFBO(FBO_SP).bind(); float clear_color_empty[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; - float clear_color_gloss[4] = { 0.1f, 0.1f, 0.0f, 0.0f }; glClearBufferfv(GL_COLOR, 0, clear_color_empty); glClearBufferfv(GL_COLOR, 1, clear_color_empty); - glClearBufferfv(GL_COLOR, 2, clear_color_gloss); glClearBufferfi(GL_DEPTH_STENCIL, 0, 1.0f, 0); ScopedGPUTimer Timer(irr_driver->getGPUTimer(Q_SOLID_PASS)); SP::draw(SP::RP_1ST, SP::DCT_NORMAL); @@ -352,7 +350,7 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca m_rtts->getRenderTarget(RTT_DIFFUSE), m_rtts->getRenderTarget(RTT_SPECULAR), m_rtts->getRenderTarget(RTT_HALF1_R), - m_rtts->getRenderTarget(RTT_SP_GLOSS), + m_rtts->getRenderTarget(RTT_NORMAL_AND_DEPTH), m_rtts->getRenderTarget(RTT_SP_DIFF_COLOR), m_rtts->getDepthStencilTexture(), m_rtts->getRenderTarget(RTT_HALF1), !m_skybox ?