1 /*
2 Copyright (c) 2021-2022 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.postproc.shaders.dof;
29 
30 import std.stdio;
31 
32 import dlib.core.memory;
33 import dlib.core.ownership;
34 import dlib.math.vector;
35 import dlib.math.matrix;
36 import dlib.math.transformation;
37 import dlib.math.interpolation;
38 import dlib.image.color;
39 import dlib.text.str;
40 
41 import dagon.core.bindings;
42 import dagon.graphics.shader;
43 import dagon.graphics.state;
44 import dagon.render.gbuffer;
45 
46 class DepthOfFieldShader: Shader
47 {
48     String vs, fs;
49 
50     bool enabled = true;
51     
52     bool autofocus = true; // Focus to screen center
53     float focalDepth = 1.5; // Focal distance value in meters when autofocus is false
54     float focalLength = 5.0; // Focal length in mm
55     float fStop = 2.0; // F-stop value
56     
57     bool manual = false; // Manual DoF calculation
58     float nearStart = 1.0; // Near DoF blur start
59     float nearDistance = 2.0; // Near DoF blur falloff distance
60     float farStart = 1.0; // Far DoF blur start
61     float farDistance = 3.0; // Far DoF blur falloff distance
62 
63     GBuffer gbuffer;
64 
65     this(Owner owner)
66     {
67         vs = Shader.load("data/__internal/shaders/DoF/DoF.vert.glsl");
68         fs = Shader.load("data/__internal/shaders/DoF/DoF.frag.glsl");
69 
70         auto myProgram = New!ShaderProgram(vs, fs, this);
71         super(myProgram, owner);
72     }
73 
74     ~this()
75     {
76         vs.free();
77         fs.free();
78     }
79 
80     override void bindParameters(GraphicsState* state)
81     {
82         setParameter("viewSize", state.resolution);
83         setParameter("enabled", enabled);
84         setParameter("zNear", state.zNear);
85         setParameter("zFar", state.zFar);
86 
87         setParameter("invProjectionMatrix", state.invProjectionMatrix);
88         
89         setParameter("autofocus", autofocus);
90         setParameter("focalDepth", focalDepth);
91         setParameter("focalLength", focalLength);
92         setParameter("fstop", fStop);
93         
94         setParameter("manual", manual);
95         setParameter("nearStart", nearStart);
96         setParameter("nearDistance", nearDistance);
97         setParameter("farStart", farStart);
98         setParameter("farDistance", farDistance);
99 
100         // Texture 0 - color buffer
101         glActiveTexture(GL_TEXTURE0);
102         glBindTexture(GL_TEXTURE_2D, state.colorTexture);
103         setParameter("colorBuffer", 0);
104 
105         if (gbuffer)
106         {
107             // Texture 1 - depth buffer
108             glActiveTexture(GL_TEXTURE1);
109             glBindTexture(GL_TEXTURE_2D, gbuffer.depthTexture);
110             setParameter("depthBuffer", 1);
111         }
112 
113         glActiveTexture(GL_TEXTURE0);
114 
115         super.bindParameters(state);
116     }
117 
118     override void unbindParameters(GraphicsState* state)
119     {
120         super.unbindParameters(state);
121 
122         glActiveTexture(GL_TEXTURE0);
123         glBindTexture(GL_TEXTURE_2D, 0);
124 
125         glActiveTexture(GL_TEXTURE1);
126         glBindTexture(GL_TEXTURE_2D, 0);
127 
128         glActiveTexture(GL_TEXTURE2);
129         glBindTexture(GL_TEXTURE_2D, 0);
130 
131         glActiveTexture(GL_TEXTURE0);
132     }
133 }