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 = "
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 = "
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     override string vertexShader()
103     {
104         return vs;
105     }
106 
107     override string fragmentShader()
108     {
109         return fs;
110     }
111     
112     GLint perspectiveMatrixLoc;
113     Matrix4x4f perspectiveMatrix;
114        
115     GLint fbBlurredLoc;
116     GLint useGlowLoc;
117     GLint glowBrightnessLoc;
118     bool glowEnabled = false;
119     float glowBrightness = 1.0;
120     GLuint blurredTexture;
121 
122     this(Framebuffer inputBuffer, Framebuffer outputBuffer, Owner o)
123     {
124         super(inputBuffer, outputBuffer, o);
125         
126         perspectiveMatrixLoc = glGetUniformLocation(shaderProgram, "perspectiveMatrix");
127         fbBlurredLoc = glGetUniformLocation(shaderProgram, "fbBlurred");
128         useGlowLoc = glGetUniformLocation(shaderProgram, "useGlow");
129         glowBrightnessLoc = glGetUniformLocation(shaderProgram, "glowBrightness");
130         
131         perspectiveMatrix = Matrix4x4f.identity;
132     }
133     
134     override void bind(RenderingContext* rc)
135     {
136         super.bind(rc);
137         
138         glUniformMatrix4fv(perspectiveMatrixLoc, 1, GL_FALSE, perspectiveMatrix.arrayof.ptr);
139         
140         glActiveTexture(GL_TEXTURE5);
141         glBindTexture(GL_TEXTURE_2D, blurredTexture);
142         glActiveTexture(GL_TEXTURE0);
143         
144         glUniform1i(fbBlurredLoc, 5);
145         glUniform1i(useGlowLoc, glowEnabled);
146         glUniform1f(glowBrightnessLoc, glowBrightness);
147     }
148     
149     override void unbind(RenderingContext* rc)
150     {        
151         glActiveTexture(GL_TEXTURE5);
152         glBindTexture(GL_TEXTURE_2D, 0);
153         glActiveTexture(GL_TEXTURE0);
154         
155         super.unbind(rc);
156     }
157 }