From d0af666bc776889cef45a5a5327557dd71c30e8a Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 26 Feb 2019 22:13:00 +0100 Subject: [PATCH] Improve layers --- Sources/arm/LayerSlot.hx | 5 +++ Sources/arm/Layers.hx | 25 ++++++------ Sources/arm/MaterialBuilder.hx | 21 ++++++---- Sources/arm/renderpath/RenderPathDeferred.hx | 41 +++++++++++++++++--- 4 files changed, 65 insertions(+), 27 deletions(-) diff --git a/Sources/arm/LayerSlot.hx b/Sources/arm/LayerSlot.hx index f3aae06c..e55f2ebe 100644 --- a/Sources/arm/LayerSlot.hx +++ b/Sources/arm/LayerSlot.hx @@ -67,6 +67,11 @@ class LayerSlot { } public function unload() { + // Set null depth so paintdb stays alive + if (UITrait.inst.layers.length > 0 && UITrait.inst.layers[0] != this) { + texpaint.setDepthStencilFrom(texpaint_nor); + } + texpaint.unload(); texpaint_nor.unload(); texpaint_pack.unload(); diff --git a/Sources/arm/Layers.hx b/Sources/arm/Layers.hx index b78b9760..07259c8d 100644 --- a/Sources/arm/Layers.hx +++ b/Sources/arm/Layers.hx @@ -133,23 +133,22 @@ class Layers { } static function makePipe() { - var pipe = UITrait.inst.pipe; - pipe = new kha.graphics4.PipelineState(); - pipe.vertexShader = kha.graphics4.VertexShader.fromSource(ConstData.painterVert); - pipe.fragmentShader = kha.graphics4.FragmentShader.fromSource(ConstData.painterFrag); + UITrait.inst.pipe = new kha.graphics4.PipelineState(); + UITrait.inst.pipe.vertexShader = kha.graphics4.VertexShader.fromSource(ConstData.painterVert); + UITrait.inst.pipe.fragmentShader = kha.graphics4.FragmentShader.fromSource(ConstData.painterFrag); var vs = new kha.graphics4.VertexStructure(); vs.add("pos", kha.graphics4.VertexData.Float3); vs.add("tex", kha.graphics4.VertexData.Float2); vs.add("col", kha.graphics4.VertexData.Float4); - pipe.inputLayout = [vs]; - pipe.blendSource = kha.graphics4.BlendingFactor.SourceAlpha; - pipe.blendDestination = kha.graphics4.BlendingFactor.InverseSourceAlpha; - pipe.compile(); + UITrait.inst.pipe.inputLayout = [vs]; + UITrait.inst.pipe.blendSource = kha.graphics4.BlendingFactor.SourceAlpha; + UITrait.inst.pipe.blendDestination = kha.graphics4.BlendingFactor.InverseSourceAlpha; + UITrait.inst.pipe.colorWriteMaskAlpha = false; + UITrait.inst.pipe.compile(); } public static function applySelectedLayer(g:kha.graphics4.Graphics) { - var pipe = UITrait.inst.pipe; - if (pipe == null) makePipe(); + if (UITrait.inst.pipe == null) makePipe(); var l0 = UITrait.inst.layers[0]; var l1 = UITrait.inst.selectedLayer; @@ -157,17 +156,17 @@ class Layers { g.end(); l0.texpaint.g2.begin(false); - l0.texpaint.g2.pipeline = pipe; + l0.texpaint.g2.pipeline = UITrait.inst.pipe; l0.texpaint.g2.drawImage(l1.texpaint, 0, 0); l0.texpaint.g2.end(); l0.texpaint_nor.g2.begin(false); - l0.texpaint_nor.g2.pipeline = pipe; + l0.texpaint_nor.g2.pipeline = UITrait.inst.pipe; l0.texpaint_nor.g2.drawImage(l1.texpaint_nor, 0, 0); l0.texpaint_nor.g2.end(); l0.texpaint_pack.g2.begin(false); - l0.texpaint_pack.g2.pipeline = pipe; + l0.texpaint_pack.g2.pipeline = UITrait.inst.pipe; l0.texpaint_pack.g2.drawImage(l1.texpaint_pack, 0, 0); l0.texpaint_pack.g2.end(); diff --git a/Sources/arm/MaterialBuilder.hx b/Sources/arm/MaterialBuilder.hx index f6d2a6ad..656ac6a4 100644 --- a/Sources/arm/MaterialBuilder.hx +++ b/Sources/arm/MaterialBuilder.hx @@ -564,9 +564,11 @@ class MaterialBuilder { if (UITrait.inst.layers.length > 1) { frag.write('float factor0;'); frag.write('float factorinv0;'); + frag.write('float nfactor0;'); + frag.write('float nfactorinv0;'); frag.write('vec4 col_tex0;'); - // frag.write('vec4 col_nor0;'); - // frag.write('vec3 n0;'); + frag.write('vec4 col_nor0;'); + frag.write('vec3 n0;'); frag.write('vec4 pack0;'); for (i in 1...UITrait.inst.layers.length) { if (!UITrait.inst.layers[i].visible) continue; @@ -577,16 +579,19 @@ class MaterialBuilder { // frag.add_uniform('sampler2D texpaint_opt' + id); frag.write('col_tex0 = texture(texpaint' + id + ', texCoord);'); - // frag.write('col_nor0 = texture(texpaint_nor' + id + ', texCoord);'); + frag.write('col_nor0 = texture(texpaint_nor' + id + ', texCoord);'); frag.write('factor0 = col_tex0.a;'); frag.write('factorinv0 = 1.0 - factor0;'); - frag.write('basecol = basecol * factorinv0 + pow(col_tex0.rgb, vec3(2.2, 2.2, 2.2)) * factor0;'); - - // frag.write('n0 = texture(texpaint_nor' + id + ', texCoord).rgb * 2.0 - 1.0;'); - // frag.write('n0 = normalize(TBN * normalize(n0));'); - // frag.write('n *= n0;'); + + frag.write('nfactor0 = col_nor0.a;'); + frag.write('nfactorinv0 = 1.0 - nfactor0;'); + frag.write('n0 = texture(texpaint_nor' + id + ', texCoord).rgb * 2.0 - 1.0;'); + frag.write('n0.y = -n0.y;'); + frag.write('n0 = normalize(mul(n0, TBN));'); + frag.write('n = normalize(n * nfactorinv0 + n0 * nfactor0);'); + frag.write('pack0 = texture(texpaint_pack' + id + ', texCoord);'); frag.write('occlusion = occlusion * factorinv0 + pack0.r * factor0;'); frag.write('roughness = roughness * factorinv0 + pack0.g * factor0;'); diff --git a/Sources/arm/renderpath/RenderPathDeferred.hx b/Sources/arm/renderpath/RenderPathDeferred.hx index 2be54db7..5ff4cfc3 100644 --- a/Sources/arm/renderpath/RenderPathDeferred.hx +++ b/Sources/arm/renderpath/RenderPathDeferred.hx @@ -253,7 +253,7 @@ class RenderPathDeferred { // #if (!kha_opengl) path.setDepthFrom("bufa", "texpaint" + tid); // Unbind depth so we can read it - path.setDepthFrom("texpaint" + tid, "bufb"); + path.setDepthFrom("texpaint" + tid, "texpaint_nor" + tid); path.depthToRenderTarget.set("paintdb", path.renderTargets.get("bufa")); #end // @@ -282,8 +282,9 @@ class RenderPathDeferred { // #if (!kha_opengl) path.setDepthFrom("texpaint" + tid, "bufa"); // Re-bind depth - path.setDepthFrom("bufa", "bufb"); - path.depthToRenderTarget.set("paintdb", path.renderTargets.get("texpaint" + tid)); + path.setDepthFrom("bufa", "texpaint_nor" + tid); + var tid0 = UITrait.inst.layers[0].id; + path.depthToRenderTarget.set("paintdb", path.renderTargets.get("texpaint" + tid0)); #end // } @@ -346,15 +347,19 @@ class RenderPathDeferred { #if rp_decals { + #if (!kha_opengl) path.setDepthFrom("gbuffer0", "gbuffer1"); // Unbind depth so we can read it path.depthToRenderTarget.set("main", path.renderTargets.get("tex")); + #end + path.setTarget("gbuffer0", ["gbuffer1"]); - path.bindTarget("_main", "gbufferD"); path.drawDecals("decal"); + #if (!kha_opengl) path.setDepthFrom("gbuffer0", "tex"); // Re-bind depth path.depthToRenderTarget.set("main", path.renderTargets.get("gbuffer0")); + #end } #end @@ -476,7 +481,9 @@ class RenderPathDeferred { // --- // Deferred light // --- + #if (!kha_opengl) path.setDepthFrom("tex", "gbuffer1"); // Unbind depth so we can read it + #end path.setTarget("tex"); path.bindTarget("_main", "gbufferD"); path.bindTarget("gbuffer0", "gbuffer0"); @@ -551,7 +558,9 @@ class RenderPathDeferred { } #end + #if (!kha_opengl) path.setDepthFrom("tex", "gbuffer0"); // Re-bind depth + #end // #if rp_volumetriclight // { @@ -864,7 +873,12 @@ class RenderPathDeferred { RenderPathCreator.drawMeshes(); - // Light + // --- + // Deferred light + // --- + #if (!kha_opengl) + path.setDepthFrom("mtex", "mgbuffer1"); // Unbind depth so we can read it + #end path.setTarget("mtex"); path.bindTarget("_mmain", "gbufferD"); path.bindTarget("mgbuffer0", "gbuffer0"); @@ -876,8 +890,13 @@ class RenderPathDeferred { #end path.drawShader("shader_datas/deferred_light/deferred_light"); + #if (!kha_opengl) + path.setDepthFrom("mtex", "mgbuffer0"); // Re-bind depth + #end + #if (rp_background == "World") { + path.setTarget("mtex"); // Re-binds depth path.drawSkydome("shader_datas/world_pass/world_pass"); } #end @@ -972,7 +991,12 @@ class RenderPathDeferred { RenderPathCreator.drawMeshes(); - // Light + // --- + // Deferred light + // --- + #if (!kha_opengl) + path.setDepthFrom("tex", "gbuffer1"); // Unbind depth so we can read it + #end path.setTarget("tex"); path.bindTarget("_main", "gbufferD"); path.bindTarget("gbuffer0", "gbuffer0"); @@ -984,8 +1008,13 @@ class RenderPathDeferred { #end path.drawShader("shader_datas/deferred_light/deferred_light"); + #if (!kha_opengl) + path.setDepthFrom("tex", "gbuffer0"); // Re-bind depth + #end + #if (rp_background == "World") { + path.setTarget("tex"); // Re-binds depth path.drawSkydome("shader_datas/world_pass/world_pass"); } #end