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.hdr;
29 
30 import dagon.core.libs;
31 import dagon.core.ownership;
32 import dagon.graphics.postproc;
33 import dagon.graphics.framebuffer;
34 import dagon.graphics.texture;
35 import dagon.graphics.rc;
36 
37 enum Tonemapper
38 {
39     Reinhard = 0,
40     Hable = 1,
41     ACES = 2,
42     Parametric = 3
43 }
44 
45 class PostFilterHDR: PostFilter
46 {
47     private string vs = import("HDR.vs");
48     private string fs = import("HDR.fs");
49 
50     override string vertexShader()
51     {
52         return vs;
53     }
54 
55     override string fragmentShader()
56     {
57         return fs;
58     }
59 
60     GLint fbPositionLoc;
61     GLint colorTableLoc;
62     GLint exposureLoc;
63     GLint tonemapFunctionLoc;
64     GLint useLUTLoc;
65     GLint vignetteLoc;
66     GLint useVignetteLoc;
67     GLint fbVelocityLoc;
68     GLint useMotionBlurLoc;
69     GLint motionBlurSamplesLoc;
70     GLint shutterFpsLoc;
71     GLint timeStepLoc;
72     GLint parametricCurveKLoc;
73 
74     bool autoExposure = false;
75 
76     float minLuminance = 0.1f;
77     float maxLuminance = 100000.0f;
78     float keyValue = 0.5f;
79     float adaptationSpeed = 4.0f;
80 
81     float exposure = 0.5f;
82     Tonemapper tonemapFunction = Tonemapper.ACES;
83     float parametricTonemapperLinearity = 0.2;
84 
85     GLuint velocityTexture;
86     bool mblurEnabled = false;
87     int motionBlurSamples = 20;
88     float shutterFps = 60.0;
89     float shutterSpeed = 1.0 / 60.0;
90 
91     Texture colorTable;
92     Texture vignette;
93 
94     this(Framebuffer inputBuffer, Framebuffer outputBuffer, Owner o)
95     {
96         super(inputBuffer, outputBuffer, o);
97 
98         fbPositionLoc = glGetUniformLocation(shaderProgram, "fbPosition");
99         colorTableLoc = glGetUniformLocation(shaderProgram, "colorTable");
100         exposureLoc = glGetUniformLocation(shaderProgram, "exposure");
101         tonemapFunctionLoc = glGetUniformLocation(shaderProgram, "tonemapFunction");
102         useLUTLoc = glGetUniformLocation(shaderProgram, "useLUT");
103         vignetteLoc = glGetUniformLocation(shaderProgram, "vignette");
104         useVignetteLoc = glGetUniformLocation(shaderProgram, "useVignette");
105         fbVelocityLoc = glGetUniformLocation(shaderProgram, "fbVelocity");
106         useMotionBlurLoc = glGetUniformLocation(shaderProgram, "useMotionBlur");
107         motionBlurSamplesLoc = glGetUniformLocation(shaderProgram, "motionBlurSamples");
108         shutterFpsLoc = glGetUniformLocation(shaderProgram, "shutterFps");
109         timeStepLoc = glGetUniformLocation(shaderProgram, "timeStep");
110         parametricCurveKLoc = glGetUniformLocation(shaderProgram, "parametricCurveK");
111     }
112 
113     override void bind(RenderingContext* rc)
114     {
115         super.bind(rc);
116 
117         glActiveTexture(GL_TEXTURE2);
118         glBindTexture(GL_TEXTURE_2D, velocityTexture);
119 
120         glActiveTexture(GL_TEXTURE3);
121         if (colorTable)
122             colorTable.bind();
123         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
124         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125         glActiveTexture(GL_TEXTURE0);
126 
127         glActiveTexture(GL_TEXTURE4);
128         if (vignette)
129             vignette.bind();
130 
131         glActiveTexture(GL_TEXTURE5);
132         glBindTexture(GL_TEXTURE_2D, inputBuffer.gbuffer.positionTexture);
133 
134         glActiveTexture(GL_TEXTURE0);
135 
136         glUniform1i(fbPositionLoc, 5);
137         glUniform1i(fbVelocityLoc, 2);
138         glUniform1i(colorTableLoc, 3);
139         glUniform1f(exposureLoc, exposure);
140         glUniform1i(tonemapFunctionLoc, tonemapFunction);
141         glUniform1i(useLUTLoc, (colorTable !is null));
142         glUniform1i(vignetteLoc, 4);
143         glUniform1i(useVignetteLoc, (vignette !is null));
144         glUniform1i(useMotionBlurLoc, mblurEnabled);
145         glUniform1i(motionBlurSamplesLoc, motionBlurSamples);
146         glUniform1f(shutterFpsLoc, shutterFps);
147         glUniform1f(timeStepLoc, rc.eventManager.deltaTime);
148         glUniform1f(parametricCurveKLoc, 1.0 - parametricTonemapperLinearity);
149     }
150 
151     override void unbind(RenderingContext* rc)
152     {
153         glActiveTexture(GL_TEXTURE2);
154         glBindTexture(GL_TEXTURE_2D, 0);
155 
156         glActiveTexture(GL_TEXTURE3);
157         if (colorTable)
158             colorTable.unbind();
159 
160         glActiveTexture(GL_TEXTURE4);
161         if (vignette)
162             vignette.unbind();
163 
164         glActiveTexture(GL_TEXTURE5);
165         glBindTexture(GL_TEXTURE_2D, 0);
166 
167         glActiveTexture(GL_TEXTURE0);
168     }
169 }