1 /*
2 Copyright (c) 2018 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.graphics.shaders.environmentpass;
29 
30 import std.stdio;
31 import std.math;
32 
33 import dlib.core.memory;
34 import dlib.math.vector;
35 import dlib.math.matrix;
36 import dlib.image.color;
37 
38 import dagon.core.libs;
39 import dagon.core.ownership;
40 import dagon.graphics.rc;
41 import dagon.graphics.gbuffer;
42 import dagon.graphics.shadow;
43 import dagon.graphics.shader;
44 import dagon.graphics.framebuffer;
45 import dagon.graphics.cubemap;
46 
47 class EnvironmentPassShader: Shader
48 {
49     string vs = import("EnvironmentPass.vs");
50     string fs = import("EnvironmentPass.fs");
51 
52     GBuffer gbuffer;
53 
54     bool enableSSAO = false;
55     int ssaoSamples = 16;
56     float ssaoRadius = 0.2f;
57     float ssaoPower = 4.0f;
58 
59     this(GBuffer gbuffer, Owner o)
60     {
61         auto myProgram = New!ShaderProgram(vs, fs, this);
62         super(myProgram, o);
63         this.gbuffer = gbuffer;
64     }
65 
66     void bind(RenderingContext* rc2d, RenderingContext* rc3d)
67     {
68         setParameter("modelViewMatrix", rc2d.modelViewMatrix);
69         setParameter("projectionMatrix", rc2d.projectionMatrix);
70 
71         setParameter("camProjectionMatrix", rc3d.projectionMatrix);
72         setParameter("camViewMatrix", rc3d.viewMatrix);
73         setParameter("camInvViewMatrix", rc3d.invViewMatrix);
74 
75         setParameter("viewSize", Vector2f(gbuffer.width, gbuffer.height));
76 
77         setParameter("sunDirection", rc3d.environment.sunDirectionEye(rc3d.viewMatrix));
78 
79         // Texture 0 - color buffer
80         glActiveTexture(GL_TEXTURE0);
81         glBindTexture(GL_TEXTURE_2D, gbuffer.colorTexture);
82         setParameter("colorBuffer", 0);
83 
84         // Texture 1 - roughness-metallic-specularity buffer
85         glActiveTexture(GL_TEXTURE1);
86         glBindTexture(GL_TEXTURE_2D, gbuffer.rmsTexture);
87         setParameter("rmsBuffer", 1);
88 
89         // Texture 2 - position buffer
90         glActiveTexture(GL_TEXTURE2);
91         glBindTexture(GL_TEXTURE_2D, gbuffer.positionTexture);
92         setParameter("positionBuffer", 2);
93 
94         // Texture 3 - normal buffer
95         glActiveTexture(GL_TEXTURE3);
96         glBindTexture(GL_TEXTURE_2D, gbuffer.normalTexture);
97         setParameter("normalBuffer", 3);
98 
99         // Texture 4 - environment
100         if (rc3d.environment.environmentMap)
101         {
102             glActiveTexture(GL_TEXTURE4);
103             rc3d.environment.environmentMap.bind();
104 
105             if (cast(Cubemap)rc3d.environment.environmentMap)
106             {
107                 setParameter("envTextureCube", 4);
108                 setParameterSubroutine("environment", ShaderType.Fragment, "environmentCubemap");
109             }
110             else
111             {
112                 setParameter("envTexture", 4);
113                 setParameterSubroutine("environment", ShaderType.Fragment, "environmentTexture");
114             }
115         }
116         else
117         {
118             setParameter("skyZenithColor", rc3d.environment.skyZenithColor);
119             setParameter("skyHorizonColor", rc3d.environment.skyHorizonColor);
120             setParameter("groundColor", rc3d.environment.groundColor);
121             setParameter("skyEnergy", rc3d.environment.skyEnergy);
122             setParameter("groundEnergy", rc3d.environment.groundEnergy);
123             setParameterSubroutine("environment", ShaderType.Fragment, "environmentSky");
124         }
125         
126         setParameter("environmentBrightness", rc3d.environment.environmentBrightness);
127 
128         // Texture 5 - emission buffer
129         glActiveTexture(GL_TEXTURE5);
130         glBindTexture(GL_TEXTURE_2D, gbuffer.emissionTexture);
131         setParameter("emissionBuffer", 5);
132 
133         // SSAO
134         setParameter("enableSSAO", enableSSAO);
135         setParameter("ssaoSamples", ssaoSamples);
136         setParameter("ssaoRadius", ssaoRadius);
137         setParameter("ssaoPower", ssaoPower);
138 
139         // Fog
140         setParameter("fogColor", rc3d.environment.fogColor);
141         setParameter("fogStart", rc3d.environment.fogStart);
142         setParameter("fogEnd", rc3d.environment.fogEnd);
143 
144         glActiveTexture(GL_TEXTURE0);
145 
146         super.bind(rc2d);
147     }
148 
149     void unbind(RenderingContext* rc2d, RenderingContext* rc3d)
150     {
151         super.unbind(rc2d);
152 
153         glActiveTexture(GL_TEXTURE0);
154         glBindTexture(GL_TEXTURE_2D, 0);
155 
156         glActiveTexture(GL_TEXTURE1);
157         glBindTexture(GL_TEXTURE_2D, 0);
158 
159         glActiveTexture(GL_TEXTURE2);
160         glBindTexture(GL_TEXTURE_2D, 0);
161 
162         glActiveTexture(GL_TEXTURE3);
163         glBindTexture(GL_TEXTURE_2D, 0);
164 
165         glActiveTexture(GL_TEXTURE4);
166         glBindTexture(GL_TEXTURE_2D, 0);
167         glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
168 
169         glActiveTexture(GL_TEXTURE5);
170         glBindTexture(GL_TEXTURE_2D, 0);
171 
172         glActiveTexture(GL_TEXTURE6);
173         glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
174 
175         glActiveTexture(GL_TEXTURE7);
176         glBindTexture(GL_TEXTURE_2D, 0);
177 
178         glActiveTexture(GL_TEXTURE0);
179     }
180 }