1 /*
2 Copyright (c) 2017-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.filters.hdrprepass;
29 
30 import derelict.opengl;
31 import dlib.math.matrix;
32 import dagon.core.ownership;
33 import dagon.graphics.postproc;
34 import dagon.graphics.framebuffer;
35 import dagon.graphics.rc;
36 
37 /*
38  * HDR prepass filter applies HDR effects that should be done before motion blur:
39  * - Glow
40  * - DoF (TODO)
41  */
42 
43 class PostFilterHDRPrepass: PostFilter
44 {
45     private string vs = q{
46         #version 330 core
47         
48         uniform mat4 modelViewMatrix;
49         uniform mat4 projectionMatrix;
50 
51         uniform vec2 viewSize;
52         
53         layout (location = 0) in vec2 va_Vertex;
54         layout (location = 1) in vec2 va_Texcoord;
55 
56         out vec2 texCoord;
57         
58         void main()
59         {
60             texCoord = va_Texcoord;
61             gl_Position = projectionMatrix * modelViewMatrix * vec4(va_Vertex * viewSize, 0.0, 1.0);
62         }
63     };
64 
65     private string fs = q{
66         #version 330 core
67         
68         #define PI 3.14159265359
69         
70         uniform sampler2D fbColor;
71         uniform sampler2D fbBlurred;
72         uniform vec2 viewSize;
73         
74         uniform mat4 perspectiveMatrix;
75         
76         uniform bool useGlow;
77         uniform float glowBrightness;
78         
79         in vec2 texCoord;
80         
81         out vec4 frag_color;
82 
83         void main()
84         {
85             vec3 res = texture(fbColor, texCoord).rgb;
86 
87             if (useGlow)
88             {
89                 vec3 glow = texture(fbBlurred, texCoord).rgb;
90                 float lum = glow.r * 0.2126 + glow.g * 0.7152 + glow.b * 0.0722;
91                 //TODO: make uniform
92                 const float minGlowLuminance = 0.01;
93                 const float maxGlowLuminance = 1.0;
94                 lum = (clamp(lum, minGlowLuminance, maxGlowLuminance) - minGlowLuminance) / (maxGlowLuminance - minGlowLuminance);
95                 res += glow * lum * glowBrightness;
96             }
97 
98             frag_color = vec4(res, 1.0); 
99         }
100 
101     };
102 
103     override string vertexShader()
104     {
105         return vs;
106     }
107 
108     override string fragmentShader()
109     {
110         return fs;
111     }
112     
113     GLint perspectiveMatrixLoc;
114     Matrix4x4f perspectiveMatrix;
115        
116     GLint fbBlurredLoc;
117     GLint useGlowLoc;
118     GLint glowBrightnessLoc;
119     bool glowEnabled = false;
120     float glowBrightness = 1.0;
121     GLuint blurredTexture;
122 
123     this(Framebuffer inputBuffer, Framebuffer outputBuffer, Owner o)
124     {
125         super(inputBuffer, outputBuffer, o);
126         
127         perspectiveMatrixLoc = glGetUniformLocation(shaderProgram, "perspectiveMatrix");
128         fbBlurredLoc = glGetUniformLocation(shaderProgram, "fbBlurred");
129         useGlowLoc = glGetUniformLocation(shaderProgram, "useGlow");
130         glowBrightnessLoc = glGetUniformLocation(shaderProgram, "glowBrightness");
131         
132         perspectiveMatrix = Matrix4x4f.identity;
133     }
134     
135     override void bind(RenderingContext* rc)
136     {
137         super.bind(rc);
138         
139         glUniformMatrix4fv(perspectiveMatrixLoc, 1, GL_FALSE, perspectiveMatrix.arrayof.ptr);
140         
141         glActiveTexture(GL_TEXTURE5);
142         glBindTexture(GL_TEXTURE_2D, blurredTexture);
143         glActiveTexture(GL_TEXTURE0);
144         
145         glUniform1i(fbBlurredLoc, 5);
146         glUniform1i(useGlowLoc, glowEnabled);
147         glUniform1f(glowBrightnessLoc, glowBrightness);
148     }
149     
150     override void unbind(RenderingContext* rc)
151     {        
152         glActiveTexture(GL_TEXTURE5);
153         glBindTexture(GL_TEXTURE_2D, 0);
154         glActiveTexture(GL_TEXTURE0);
155         
156         super.unbind(rc);
157     }
158 }