1 /* 2 Copyright (c) 2019-2020 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 */ 27 28 module dagon.game.deferredrenderer; 29 30 import dlib.core.memory; 31 import dlib.core.ownership; 32 33 import dagon.core.event; 34 import dagon.core.time; 35 import dagon.resource.scene; 36 import dagon.render.deferred; 37 import dagon.render.gbuffer; 38 import dagon.render.view; 39 import dagon.render.framebuffer; 40 import dagon.render.shadowpass; 41 import dagon.postproc.filterpass; 42 import dagon.postproc.shaders.denoise; 43 import dagon.game.renderer; 44 45 class DeferredRenderer: Renderer 46 { 47 GBuffer gbuffer; 48 49 DenoiseShader denoiseShader; 50 51 ShadowPass passShadow; 52 DeferredClearPass passClear; 53 DeferredBackgroundPass passBackground; 54 DeferredGeometryPass passStaticGeometry; 55 DeferredDecalPass passDecals; 56 DeferredGeometryPass passDynamicGeometry; 57 DeferredOcclusionPass passOcclusion; 58 FilterPass passOcclusionDenoise; 59 DeferredEnvironmentPass passEnvironment; 60 DeferredLightPass passLight; 61 DeferredParticlesPass passParticles; 62 DeferredForwardPass passForward; 63 DeferredDebugOutputPass passDebug; 64 65 RenderView occlusionView; 66 Framebuffer occlusionNoisyBuffer; 67 Framebuffer occlusionBuffer; 68 69 DebugOutputMode outputMode = DebugOutputMode.Radiance; 70 71 bool _ssaoEnabled = true; 72 73 int ssaoSamples = 10; 74 float ssaoRadius = 0.2f; 75 float ssaoPower = 7.0f; 76 float ssaoDenoise = 1.0f; 77 78 float _occlusionBufferDetail = 1.0; 79 80 void occlusionBufferDetail(float value) @property 81 { 82 _occlusionBufferDetail = value; 83 occlusionView.resize(cast(uint)(view.width * _occlusionBufferDetail), cast(uint)(view.height * _occlusionBufferDetail)); 84 occlusionNoisyBuffer.resize(occlusionView.width, occlusionView.height); 85 occlusionBuffer.resize(occlusionView.width, occlusionView.height); 86 } 87 float occlusionBufferDetail() @property 88 { 89 return _occlusionBufferDetail; 90 } 91 92 this(EventManager eventManager, Owner owner) 93 { 94 super(eventManager, owner); 95 96 occlusionView = New!RenderView(0, 0, cast(uint)(view.width * _occlusionBufferDetail), cast(uint)(view.height * _occlusionBufferDetail), this); 97 occlusionNoisyBuffer = New!Framebuffer(occlusionView.width, occlusionView.height, FrameBufferFormat.R8, false, this); 98 occlusionBuffer = New!Framebuffer(occlusionView.width, occlusionView.height, FrameBufferFormat.R8, false, this); 99 100 // HDR buffer 101 auto radianceBuffer = New!Framebuffer(eventManager.windowWidth, eventManager.windowHeight, FrameBufferFormat.RGBA16F, true, this); 102 outputBuffer = radianceBuffer; 103 104 gbuffer = New!GBuffer(view.width, view.height, radianceBuffer, this); 105 106 passShadow = New!ShadowPass(pipeline); 107 108 //passClear = New!DeferredClearPass(pipeline, gbuffer); 109 110 passBackground = New!DeferredBackgroundPass(pipeline, gbuffer); 111 passBackground.view = view; 112 113 passStaticGeometry = New!DeferredGeometryPass(pipeline, gbuffer); 114 passStaticGeometry.view = view; 115 116 passDecals = New!DeferredDecalPass(pipeline, gbuffer); 117 passDecals.view = view; 118 119 passDynamicGeometry = New!DeferredGeometryPass(pipeline, gbuffer); 120 passDynamicGeometry.view = view; 121 122 passOcclusion = New!DeferredOcclusionPass(pipeline, gbuffer); 123 passOcclusion.view = occlusionView; 124 passOcclusion.outputBuffer = occlusionNoisyBuffer; 125 126 denoiseShader = New!DenoiseShader(this); 127 passOcclusionDenoise = New!FilterPass(pipeline, denoiseShader); 128 passOcclusionDenoise.view = occlusionView; 129 passOcclusionDenoise.inputBuffer = occlusionNoisyBuffer; 130 passOcclusionDenoise.outputBuffer = occlusionBuffer; 131 132 passEnvironment = New!DeferredEnvironmentPass(pipeline, gbuffer); 133 passEnvironment.view = view; 134 passEnvironment.outputBuffer = radianceBuffer; 135 passEnvironment.occlusionBuffer = occlusionBuffer; 136 137 passLight = New!DeferredLightPass(pipeline, gbuffer); 138 passLight.view = view; 139 passLight.outputBuffer = radianceBuffer; 140 passLight.occlusionBuffer = occlusionBuffer; 141 142 passForward = New!DeferredForwardPass(pipeline, gbuffer); 143 passForward.view = view; 144 passForward.outputBuffer = radianceBuffer; 145 146 passParticles = New!DeferredParticlesPass(pipeline, gbuffer); 147 passParticles.view = view; 148 passParticles.outputBuffer = radianceBuffer; 149 passParticles.gbuffer = gbuffer; 150 151 passDebug = New!DeferredDebugOutputPass(pipeline, gbuffer); 152 passDebug.view = view; 153 passDebug.active = false; 154 passDebug.outputBuffer = radianceBuffer; 155 passDebug.occlusionBuffer = occlusionBuffer; 156 } 157 158 void ssaoEnabled(bool mode) @property 159 { 160 _ssaoEnabled = mode; 161 passOcclusion.active = mode; 162 passOcclusionDenoise.active = mode; 163 if (_ssaoEnabled) 164 { 165 passEnvironment.occlusionBuffer = occlusionBuffer; 166 passLight.occlusionBuffer = occlusionBuffer; 167 } 168 else 169 { 170 passEnvironment.occlusionBuffer = null; 171 passLight.occlusionBuffer = null; 172 } 173 } 174 175 bool ssaoEnabled() @property 176 { 177 return _ssaoEnabled; 178 } 179 180 override void scene(Scene s) 181 { 182 passShadow.group = s.spatial; 183 passShadow.lightGroup = s.lights; 184 passBackground.group = s.background; 185 passStaticGeometry.group = s.spatialOpaqueStatic; 186 passDecals.group = s.decals; 187 passDynamicGeometry.group = s.spatialOpaqueDynamic; 188 passLight.groupSunLights = s.sunLights; 189 passLight.groupAreaLights = s.areaLights; 190 passForward.group = s.spatialTransparent; 191 passParticles.group = s.spatial; 192 193 pipeline.environment = s.environment; 194 } 195 196 override void update(Time t) 197 { 198 passShadow.camera = activeCamera; 199 passDebug.active = (outputMode != DebugOutputMode.Radiance); 200 passDebug.outputMode = outputMode; 201 202 passOcclusion.ssaoShader.samples = ssaoSamples; 203 passOcclusion.ssaoShader.radius = ssaoRadius; 204 passOcclusion.ssaoShader.power = ssaoPower; 205 denoiseShader.factor = ssaoDenoise; 206 207 super.update(t); 208 } 209 210 override void setViewport(uint x, uint y, uint w, uint h) 211 { 212 super.setViewport(x, y, w, h); 213 214 outputBuffer.resize(view.width, view.height); 215 gbuffer.resize(view.width, view.height); 216 217 occlusionView.resize(cast(uint)(view.width * _occlusionBufferDetail), cast(uint)(view.height * _occlusionBufferDetail)); 218 occlusionNoisyBuffer.resize(occlusionView.width, occlusionView.height); 219 occlusionBuffer.resize(occlusionView.width, occlusionView.height); 220 221 passForward.resize(w, h); 222 } 223 }