1 /* 2 Copyright (c) 2019 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.motionblur; 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 MotionBlurShader: Shader 47 { 48 String vs, fs; 49 50 bool enabled = true; 51 int samples = 16; 52 float currentFramerate = 60.0; 53 float shutterFramerate = 30.0; 54 float offsetRandomCoefficient = 1.0; 55 56 GBuffer gbuffer; 57 58 this(Owner owner) 59 { 60 vs = Shader.load("data/__internal/shaders/MotionBlur/MotionBlur.vert.glsl"); 61 fs = Shader.load("data/__internal/shaders/MotionBlur/MotionBlur.frag.glsl"); 62 63 auto myProgram = New!ShaderProgram(vs, fs, this); 64 super(myProgram, owner); 65 } 66 67 ~this() 68 { 69 vs.free(); 70 fs.free(); 71 } 72 73 override void bindParameters(GraphicsState* state) 74 { 75 setParameter("viewSize", state.resolution); 76 setParameter("enabled", enabled); 77 setParameter("zNear", state.zNear); 78 setParameter("zFar", state.zFar); 79 80 setParameter("invProjectionMatrix", state.invProjectionMatrix); 81 82 setParameter("blurScale", currentFramerate / shutterFramerate); 83 setParameter("samples", samples); 84 setParameter("offsetRandomCoef", offsetRandomCoefficient); 85 setParameter("time", state.localTime); 86 87 // Texture 0 - color buffer 88 glActiveTexture(GL_TEXTURE0); 89 glBindTexture(GL_TEXTURE_2D, state.colorTexture); 90 setParameter("colorBuffer", 0); 91 92 if (gbuffer) 93 { 94 // Texture 1 - depth buffer 95 glActiveTexture(GL_TEXTURE1); 96 glBindTexture(GL_TEXTURE_2D, gbuffer.depthTexture); 97 setParameter("depthBuffer", 1); 98 99 // Texture 2 - velocity buffer 100 glActiveTexture(GL_TEXTURE2); 101 glBindTexture(GL_TEXTURE_2D, gbuffer.velocityTexture); 102 setParameter("velocityBuffer", 2); 103 } 104 105 glActiveTexture(GL_TEXTURE0); 106 107 super.bindParameters(state); 108 } 109 110 override void unbindParameters(GraphicsState* state) 111 { 112 super.unbindParameters(state); 113 114 glActiveTexture(GL_TEXTURE0); 115 glBindTexture(GL_TEXTURE_2D, 0); 116 117 glActiveTexture(GL_TEXTURE1); 118 glBindTexture(GL_TEXTURE_2D, 0); 119 120 glActiveTexture(GL_TEXTURE2); 121 glBindTexture(GL_TEXTURE_2D, 0); 122 123 glActiveTexture(GL_TEXTURE0); 124 } 125 }