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 dlib.math.matrix;
31 import dagon.core.libs;
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 = import("HDRPrepass.vs");
46     private string fs = import("HDRPrepass.fs");
47 
48     override string vertexShader()
49     {
50         return vs;
51     }
52 
53     override string fragmentShader()
54     {
55         return fs;
56     }
57 
58     GLint perspectiveMatrixLoc;
59     Matrix4x4f perspectiveMatrix;
60 
61     GLint fbBlurredLoc;
62     GLint useGlowLoc;
63     GLint glowBrightnessLoc;
64     bool glowEnabled = false;
65     float glowBrightness = 1.0;
66     GLuint blurredTexture;
67 
68     this(Framebuffer inputBuffer, Framebuffer outputBuffer, Owner o)
69     {
70         super(inputBuffer, outputBuffer, o);
71 
72         perspectiveMatrixLoc = glGetUniformLocation(shaderProgram, "perspectiveMatrix");
73         fbBlurredLoc = glGetUniformLocation(shaderProgram, "fbBlurred");
74         useGlowLoc = glGetUniformLocation(shaderProgram, "useGlow");
75         glowBrightnessLoc = glGetUniformLocation(shaderProgram, "glowBrightness");
76 
77         perspectiveMatrix = Matrix4x4f.identity;
78     }
79 
80     override void bind(RenderingContext* rc)
81     {
82         super.bind(rc);
83 
84         glUniformMatrix4fv(perspectiveMatrixLoc, 1, GL_FALSE, perspectiveMatrix.arrayof.ptr);
85 
86         glActiveTexture(GL_TEXTURE5);
87         glBindTexture(GL_TEXTURE_2D, blurredTexture);
88         glActiveTexture(GL_TEXTURE0);
89 
90         glUniform1i(fbBlurredLoc, 5);
91         glUniform1i(useGlowLoc, glowEnabled);
92         glUniform1f(glowBrightnessLoc, glowBrightness);
93     }
94 
95     override void unbind(RenderingContext* rc)
96     {
97         glActiveTexture(GL_TEXTURE5);
98         glBindTexture(GL_TEXTURE_2D, 0);
99         glActiveTexture(GL_TEXTURE0);
100 
101         super.unbind(rc);
102     }
103 }