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