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.shaders.debugoutput;
29 
30 import std.stdio;
31 import std.math;
32 
33 import dlib.core.memory;
34 import dlib.core.ownership;
35 import dlib.math.vector;
36 import dlib.math.matrix;
37 import dlib.math.transformation;
38 import dlib.math.interpolation;
39 import dlib.image.color;
40 import dlib.text.str;
41 
42 import dagon.core.bindings;
43 import dagon.graphics.shader;
44 import dagon.graphics.state;
45 import dagon.render.deferred;
46 
47 class DebugOutputShader: Shader
48 {
49     String vs, fs;
50 
51     DebugOutputMode outputMode = DebugOutputMode.Radiance;
52 
53     this(Owner owner)
54     {
55         vs = Shader.load("data/__internal/shaders/DebugOutput/DebugOutput.vert.glsl");
56         fs = Shader.load("data/__internal/shaders/DebugOutput/DebugOutput.frag.glsl");
57 
58         auto myProgram = New!ShaderProgram(vs, fs, this);
59         super(myProgram, owner);
60     }
61 
62     ~this()
63     {
64         vs.free();
65         fs.free();
66     }
67 
68     override void bindParameters(GraphicsState* state)
69     {
70         setParameter("projectionMatrix", state.projectionMatrix);
71 
72         setParameter("viewMatrix", state.viewMatrix);
73         setParameter("invViewMatrix", state.invViewMatrix);
74         setParameter("invProjectionMatrix", state.invProjectionMatrix);
75         setParameter("resolution", state.resolution);
76         setParameter("zNear", state.zNear);
77         setParameter("zFar", state.zFar);
78 
79         setParameter("outputMode", cast(int)outputMode);
80 
81         // Texture 0 - color buffer
82         glActiveTexture(GL_TEXTURE0);
83         glBindTexture(GL_TEXTURE_2D, state.colorTexture);
84         setParameter("colorBuffer", 0);
85 
86         // Texture 1 - depth buffer
87         glActiveTexture(GL_TEXTURE1);
88         glBindTexture(GL_TEXTURE_2D, state.depthTexture);
89         setParameter("depthBuffer", 1);
90 
91         // Texture 2 - normal buffer
92         glActiveTexture(GL_TEXTURE2);
93         glBindTexture(GL_TEXTURE_2D, state.normalTexture);
94         setParameter("normalBuffer", 2);
95 
96         // Texture 3 - pbr buffer
97         glActiveTexture(GL_TEXTURE3);
98         glBindTexture(GL_TEXTURE_2D, state.pbrTexture);
99         setParameter("pbrBuffer", 3);
100 
101         // Texture 4 - occlusion buffer
102         if (glIsTexture(state.occlusionTexture))
103         {
104             glActiveTexture(GL_TEXTURE4);
105             glBindTexture(GL_TEXTURE_2D, state.occlusionTexture);
106             setParameter("occlusionBuffer", 4);
107             setParameter("haveOcclusionBuffer", true);
108         }
109         else
110         {
111             setParameter("haveOcclusionBuffer", false);
112         }
113 
114         glActiveTexture(GL_TEXTURE0);
115 
116         super.bindParameters(state);
117     }
118 
119     override void unbindParameters(GraphicsState* state)
120     {
121         super.unbindParameters(state);
122 
123         glActiveTexture(GL_TEXTURE0);
124         glBindTexture(GL_TEXTURE_2D, 0);
125 
126         glActiveTexture(GL_TEXTURE1);
127         glBindTexture(GL_TEXTURE_2D, 0);
128 
129         glActiveTexture(GL_TEXTURE2);
130         glBindTexture(GL_TEXTURE_2D, 0);
131 
132         glActiveTexture(GL_TEXTURE3);
133         glBindTexture(GL_TEXTURE_2D, 0);
134 
135         glActiveTexture(GL_TEXTURE4);
136         glBindTexture(GL_TEXTURE_2D, 0);
137 
138         glActiveTexture(GL_TEXTURE0);
139     }
140 }