From a589a76250023f71c1b0cbbc640a36e6a636c7e2 Mon Sep 17 00:00:00 2001 From: Lubos Lenco Date: Fri, 13 May 2016 00:08:11 +0200 Subject: [PATCH] Bindable depth. --- blender/nodes_pipeline.py | 38 +++++++++++++++++-- raw/blur_pass/blur_pass_edges.frag.glsl | 1 + raw/compositor_pass/compositor_pass.frag.glsl | 1 + raw/deferred_light/deferred_light.frag.glsl | 8 +++- .../motion_blur_pass.frag.glsl | 7 +++- raw/ssao_pass/ssao_pass.frag.glsl | 10 +++-- raw/ssdo_pass/ssdo_pass.frag.glsl | 10 +++-- raw/ssr_pass/ssr_pass.frag.glsl | 9 +++-- raw/sss_pass/sss_pass.frag.glsl | 6 ++- raw/water_pass/water_pass.frag.glsl | 11 ++++-- 10 files changed, 79 insertions(+), 22 deletions(-) diff --git a/blender/nodes_pipeline.py b/blender/nodes_pipeline.py index 763fb518..8e4a6a83 100755 --- a/blender/nodes_pipeline.py +++ b/blender/nodes_pipeline.py @@ -130,6 +130,23 @@ class ColorBufferNode(Node, CGPipelineTreeNode): def free(self): print("Removing node ", self, ", Goodbye!") + +class DepthBufferNode(Node, CGPipelineTreeNode): + '''A custom node''' + bl_idname = 'DepthBufferNodeType' + bl_label = 'Depth Buffer' + bl_icon = 'SOUND' + + def init(self, context): + self.inputs.new('NodeSocketShader', "Target") + + self.outputs.new('NodeSocketShader', "Target") + + def copy(self, node): + print("Copying from node ", node) + + def free(self): + print("Removing node ", self, ", Goodbye!") class FramebufferNode(Node, CGPipelineTreeNode): '''A custom node''' @@ -356,6 +373,7 @@ node_categories = [ NodeItem("DrawWorldNodeType"), NodeItem("TargetNodeType"), NodeItem("ColorBufferNodeType"), + NodeItem("DepthBufferNodeType"), NodeItem("FramebufferNodeType"), ]), MyPassNodeCategory("PASSNODES", "Pass", items=[ @@ -457,7 +475,7 @@ def buildNodeTree(node_group, shader_references, asset_references): with open(path + node_group_name + '.json', 'w') as f: f.write(output.to_JSON()) -def make_set_target(stage, node_group, node, target_index=1, color_buffer_index=-1): +def make_set_target(stage, node_group, node, target_index=1, color_buffer_index=-1, is_depth_attachment=False): stage.command = 'set_target' targetNode = findNodeByLink(node_group, node, node.inputs[target_index]) @@ -466,12 +484,19 @@ def make_set_target(stage, node_group, node, target_index=1, color_buffer_index= if color_buffer_index >= 0 and targetNode.inputs[3].default_value > 1: # MRT target, bind only specified target cb_postfix = str(color_buffer_index) + elif is_depth_attachment == True: # Bind texture depth + cb_postfix = 'D' + targetId = targetNode.inputs[0].default_value + cb_postfix elif targetNode.bl_idname == 'ColorBufferNodeType': cb_index = targetNode.inputs[1].default_value make_set_target(stage, node_group, targetNode, target_index=0, color_buffer_index=cb_index) return + + elif targetNode.bl_idname == 'DepthBufferNodeType': + make_set_target(stage, node_group, targetNode, target_index=0, is_depth_attachment=True) + return elif targetNode.bl_idname == 'NodeReroute': make_set_target(stage, node_group, targetNode, target_index=0, color_buffer_index=color_buffer_index) @@ -495,7 +520,7 @@ def make_draw_geometry(stage, node_group, node): stage.command = 'draw_geometry' stage.params.append(node.inputs[1].default_value) # Context -def make_bind_target(stage, node_group, node, target_index=1, constant_index=2, color_buffer_index=-1): +def make_bind_target(stage, node_group, node, target_index=1, constant_index=2, color_buffer_index=-1, is_depth_attachment=False): stage.command = 'bind_target' targetNode = findNodeByLink(node_group, node, node.inputs[target_index]) @@ -505,12 +530,19 @@ def make_bind_target(stage, node_group, node, target_index=1, constant_index=2, if targetNode.bl_idname == 'ColorBufferNodeType': # Make recursive color_buffer_index = targetNode.inputs[1].default_value targetNode = findNodeByLink(node_group, targetNode, targetNode.inputs[0]) + + if targetNode.bl_idname == 'DepthBufferNodeType': # Make recursive + is_depth_attachment = True + targetNode = findNodeByLink(node_group, targetNode, targetNode.inputs[0]) if targetNode.bl_idname == 'TargetNodeType': cb_postfix = '' if color_buffer_index >= 0 and targetNode.inputs[3].default_value > 1: # MRT target, bind only specified target cb_postfix = str(color_buffer_index) - + + elif is_depth_attachment == True: # Bind texture depth + cb_postfix = 'D' + targetId = targetNode.inputs[0].default_value + cb_postfix stage.params.append(targetId) diff --git a/raw/blur_pass/blur_pass_edges.frag.glsl b/raw/blur_pass/blur_pass_edges.frag.glsl index 7a1ab3d8..62eb6579 100644 --- a/raw/blur_pass/blur_pass_edges.frag.glsl +++ b/raw/blur_pass/blur_pass_edges.frag.glsl @@ -5,6 +5,7 @@ precision mediump float; #endif uniform sampler2D tex; +// uniform sampler2D gbufferD; uniform sampler2D gbuffer0; uniform vec2 dir; diff --git a/raw/compositor_pass/compositor_pass.frag.glsl b/raw/compositor_pass/compositor_pass.frag.glsl index b807f650..fdbfc415 100644 --- a/raw/compositor_pass/compositor_pass.frag.glsl +++ b/raw/compositor_pass/compositor_pass.frag.glsl @@ -5,6 +5,7 @@ precision highp float; #endif uniform sampler2D tex; +// uniform sampler2D gbufferD; uniform sampler2D gbuffer0; uniform sampler2D gbuffer1; uniform sampler2D noise256; diff --git a/raw/deferred_light/deferred_light.frag.glsl b/raw/deferred_light/deferred_light.frag.glsl index f8cab4e7..8013580d 100644 --- a/raw/deferred_light/deferred_light.frag.glsl +++ b/raw/deferred_light/deferred_light.frag.glsl @@ -7,6 +7,7 @@ precision mediump float; #define PI 3.1415926535 #define TwoPI (2.0 * PI) +uniform sampler2D gbufferD; uniform sampler2D gbuffer0; uniform sampler2D gbuffer1; uniform sampler2D gbuffer2; @@ -385,9 +386,12 @@ vec3 LTC_Evaluate(vec3 N, vec3 V, vec3 P, mat3 Minv, vec3 points0, vec3 points1, void main() { + float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0; + // float depth = 1.0 - g0.a; + // if (depth == 0.0) discard; + if (depth == 1.0) discard; + vec4 g0 = texture(gbuffer0, texCoord); // Normal.xy, mask, depth - float depth = 1.0 - g0.a; - if (depth == 0.0) discard; vec4 g1 = texture(gbuffer1, texCoord); // Base color.rgb, roughn/met float ao = texture(ssaotex, texCoord).r; diff --git a/raw/motion_blur_pass/motion_blur_pass.frag.glsl b/raw/motion_blur_pass/motion_blur_pass.frag.glsl index e7124991..f7bfcbe5 100644 --- a/raw/motion_blur_pass/motion_blur_pass.frag.glsl +++ b/raw/motion_blur_pass/motion_blur_pass.frag.glsl @@ -6,6 +6,7 @@ precision mediump float; #endif +uniform sampler2D gbufferD; uniform sampler2D gbuffer0; uniform sampler2D gbuffer1; @@ -50,8 +51,10 @@ void main() { return; } - float depth = 1.0 - texture(gbuffer0, texCoord).a; - if (depth == 0.0) { + // float depth = 1.0 - texture(gbuffer0, texCoord).a; + float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0; + // if (depth == 0.0) { + if (depth == 1.0) { gl_FragColor = color; return; } diff --git a/raw/ssao_pass/ssao_pass.frag.glsl b/raw/ssao_pass/ssao_pass.frag.glsl index d8c9a39f..dbf9a308 100644 --- a/raw/ssao_pass/ssao_pass.frag.glsl +++ b/raw/ssao_pass/ssao_pass.frag.glsl @@ -17,6 +17,7 @@ precision mediump float; #endif +uniform sampler2D gbufferD; uniform sampler2D gbuffer0; uniform sampler2D gbuffer1; uniform sampler2D snoise; @@ -52,7 +53,8 @@ float doAO(vec2 kernelVec, vec2 randomVec, mat2 rotMat, vec3 currentPos, vec3 cu float radius = aoSize * randomVec.y; kernelVec.xy = ((rotMat * kernelVec.xy) / currentDistance) * radius; vec2 coord = texCoord + kernelVec.xy; - float depth = 1.0 - texture(gbuffer0, coord).a; + // float depth = 1.0 - texture(gbuffer0, coord).a; + float depth = texture(gbufferD, coord).r * 2.0 - 1.0; vec3 pos = getPos(depth, coord) - currentPos; float angle = dot(pos, currentNormal); @@ -64,8 +66,10 @@ float doAO(vec2 kernelVec, vec2 randomVec, mat2 rotMat, vec3 currentPos, vec3 cu } void main() { - float depth = 1.0 - texture(gbuffer0, texCoord).a; - if (depth == 0.0) { + // float depth = 1.0 - texture(gbuffer0, texCoord).a; + float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0; + // if (depth == 0.0) { + if (depth == 1.0) { gl_FragColor = vec4(1.0); return; } diff --git a/raw/ssdo_pass/ssdo_pass.frag.glsl b/raw/ssdo_pass/ssdo_pass.frag.glsl index 7c0e3cda..f82fc33e 100644 --- a/raw/ssdo_pass/ssdo_pass.frag.glsl +++ b/raw/ssdo_pass/ssdo_pass.frag.glsl @@ -6,6 +6,7 @@ precision mediump float; #endif +uniform sampler2D gbufferD; uniform sampler2D gbuffer0; uniform sampler2D gbuffer1; uniform sampler2D snoise; @@ -42,7 +43,8 @@ vec4 doDO(vec3 point, vec3 noise, float radius, vec3 center_pos, float max_dista vec2 textureOffset = reflect( point.xy, noise.xy ).xy * radius; vec2 sample_tex = texCoord + textureOffset; - float depth = 1.0 - texture(gbuffer0, sample_tex).a; + // float depth = 1.0 - texture(gbuffer0, sample_tex).a; + float depth = texture(gbufferD, sample_tex).r * 2.0 - 1.0; vec3 sample_pos = getPos(depth, sample_tex); vec3 center_to_sample = sample_pos - center_pos; @@ -58,8 +60,10 @@ vec4 doDO(vec3 point, vec3 noise, float radius, vec3 center_pos, float max_dista } void main() { - float depth = 1.0 - texture(gbuffer0, texCoord).a; - if (depth == 0.0) { + // float depth = 1.0 - texture(gbuffer0, texCoord).a; + float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0; + // if (depth == 0.0) { + if (depth == 1.0) { gl_FragColor = vec4(1.0); return; } diff --git a/raw/ssr_pass/ssr_pass.frag.glsl b/raw/ssr_pass/ssr_pass.frag.glsl index 027db5db..5e6f233b 100644 --- a/raw/ssr_pass/ssr_pass.frag.glsl +++ b/raw/ssr_pass/ssr_pass.frag.glsl @@ -5,7 +5,8 @@ precision mediump float; #endif uniform sampler2D tex; -uniform sampler2D gbuffer0; // Normal, depth +uniform sampler2D gbufferD; +uniform sampler2D gbuffer0; // Normal uniform sampler2D gbuffer1; // Color, roughness uniform mat4 P; uniform mat4 tiV; @@ -45,7 +46,8 @@ vec3 getPos(float depth) { } float getDeltaDepth(vec3 hitCoord) { - depth = 1.0 - texture(gbuffer0, getProjectedCoord(hitCoord).xy).a; + // depth = 1.0 - texture(gbuffer0, getProjectedCoord(hitCoord).xy).a; + depth = texture(gbufferD, getProjectedCoord(hitCoord).xy).r * 2.0 - 1.0; vec3 viewPos = getPos(depth); return viewPos.z - hitCoord.z; } @@ -189,7 +191,8 @@ void main() { if (viewNormal.z <= 0.9) discard; // Only up facing surfaces for now viewNormal = tiV * normalize(viewNormal); - float d = 1.0 - g0.a; + // float d = 1.0 - g0.a; + float d = texture(gbufferD, texCoord).r * 2.0 - 1.0; vec3 viewPos = getPos(d); vec3 reflected = normalize(reflect((viewPos.xyz), normalize(viewNormal.xyz))); diff --git a/raw/sss_pass/sss_pass.frag.glsl b/raw/sss_pass/sss_pass.frag.glsl index b3d52271..4348d29c 100644 --- a/raw/sss_pass/sss_pass.frag.glsl +++ b/raw/sss_pass/sss_pass.frag.glsl @@ -41,6 +41,7 @@ precision mediump float; #endif +uniform sampler2D gbufferD; uniform sampler2D gbuffer0; uniform sampler2D tex; @@ -74,8 +75,9 @@ vec4 SSSSBlur(float sssWidth) { // if (SSSS_STREGTH_SOURCE == 0.0) discard; // Fetch linear depth of current pixel - vec4 g0 = texture(gbuffer0, texCoord); - float depth = 1.0 - g0.a; + // vec4 g0 = texture(gbuffer0, texCoord); + // float depth = 1.0 - g0.a; + float depth = texture(gbufferD, texCoord) * 2.0 - 1.0; const float znear = 0.1; const float zfar = 1000.0; const float projectionA = zfar / (zfar - znear); diff --git a/raw/water_pass/water_pass.frag.glsl b/raw/water_pass/water_pass.frag.glsl index baa6836a..6d8716ec 100644 --- a/raw/water_pass/water_pass.frag.glsl +++ b/raw/water_pass/water_pass.frag.glsl @@ -12,6 +12,7 @@ precision mediump float; const float PI = 3.1415926535; const float TwoPI = (2.0 * PI); +uniform sampler2D gbufferD; uniform sampler2D gbuffer0; uniform sampler2D tex; uniform sampler2D senvmapRadiance; @@ -354,11 +355,13 @@ float godRays(vec2 uv) { return light; } -void main() { - vec4 g0 = texture(gbuffer0, texCoord); // Normal.xy, mask, depth - float gdepth = 1.0 - g0.a; +void main() { + // vec4 g0 = texture(gbuffer0, texCoord); // Normal.xy, mask + // float gdepth = 1.0 - g0.a; + float gdepth = texture(gbufferD, texCoord) * 2.0 - 1.0; vec4 colorOriginal = texture(tex, texCoord); - if (gdepth == 0.0) { + // if (gdepth == 0.0) { + if (gdepth == 1.0) { gl_FragColor = colorOriginal; return; }