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.render.deferred.debugoutputpass;
29 
30 import std.stdio;
31 
32 import dlib.core.memory;
33 import dlib.core.ownership;
34 import dlib.image.color;
35 
36 import dagon.core.bindings;
37 import dagon.graphics.screensurface;
38 import dagon.render.pipeline;
39 import dagon.render.pass;
40 import dagon.render.framebuffer;
41 import dagon.render.gbuffer;
42 import dagon.render.shaders.debugoutput;
43 
44 enum DebugOutputMode: int
45 {
46     Radiance = 0,
47     Albedo = 1,
48     Normal = 2,
49     Position = 3,
50     Roughness = 4,
51     Metallic = 5,
52     Occlusion = 6
53 }
54 
55 class DeferredDebugOutputPass: RenderPass
56 {
57     GBuffer gbuffer;
58     Framebuffer occlusionBuffer;
59     ScreenSurface screenSurface;
60     DebugOutputShader debugOutputShader;
61     DebugOutputMode outputMode = DebugOutputMode.Radiance;
62     Framebuffer outputBuffer;
63 
64     this(RenderPipeline pipeline, GBuffer gbuffer)
65     {
66         super(pipeline);
67         this.gbuffer = gbuffer;
68         screenSurface = New!ScreenSurface(this);
69         debugOutputShader = New!DebugOutputShader(this);
70     }
71 
72     override void render()
73     {
74         if (view && gbuffer)
75         {
76             if (outputBuffer)
77                 outputBuffer.bind();
78 
79             state.colorTexture = gbuffer.colorTexture;
80             state.depthTexture = gbuffer.depthTexture;
81             state.normalTexture = gbuffer.normalTexture;
82             state.pbrTexture = gbuffer.pbrTexture;
83             if (occlusionBuffer)
84                 state.occlusionTexture = occlusionBuffer.colorTexture;
85             else
86                 state.occlusionTexture = 0;
87 
88             Color4f backgroundColor = Color4f(0.0f, 0.0f, 0.0f, 1.0f);
89             if (state.environment)
90                 backgroundColor = state.environment.backgroundColor;
91 
92             glScissor(view.x, view.y, view.width, view.height);
93             glViewport(view.x, view.y, view.width, view.height);
94 
95             glClearColor(
96                 backgroundColor.r,
97                 backgroundColor.g,
98                 backgroundColor.b,
99                 backgroundColor.a);
100             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
101 
102             debugOutputShader.outputMode = outputMode;
103             debugOutputShader.bind();
104             debugOutputShader.bindParameters(&state);
105             screenSurface.render(&state);
106             debugOutputShader.unbindParameters(&state);
107             debugOutputShader.unbind();
108 
109             if (outputBuffer)
110                 outputBuffer.unbind();
111         }
112     }
113 }