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.postproc; 29 30 import std.stdio; 31 import std.conv; 32 33 import dlib.math.vector; 34 35 import dagon.core.libs; 36 import dagon.core.ownership; 37 import dagon.graphics.rc; 38 import dagon.graphics.framebuffer; 39 import dagon.graphics.shaderloader; 40 41 class PostFilter: Owner 42 { 43 bool enabled = true; 44 Framebuffer inputBuffer; 45 Framebuffer outputBuffer; 46 47 immutable GLenum shaderProgram; 48 49 GLint modelViewMatrixLoc; 50 GLint prevModelViewProjMatrixLoc; 51 GLint projectionMatrixLoc; 52 GLint fbColorLoc; 53 GLint viewportSizeLoc; 54 GLint enabledLoc; 55 56 private string vsText = import("DefaultFilter.vs"); 57 private string fsText = import("DefaultFilter.vs"); 58 59 string vertexShader() {return vsText;} 60 string fragmentShader() {return fsText;} 61 62 this(Framebuffer inputBuffer, Framebuffer outputBuffer, Owner o) 63 { 64 super(o); 65 66 this.inputBuffer = inputBuffer; 67 this.outputBuffer = outputBuffer; 68 69 GLuint vert = compileShader(vertexShader, ShaderStage.vertex); 70 GLuint frag = compileShader(fragmentShader, ShaderStage.fragment); 71 if (vert != 0 && frag != 0) 72 shaderProgram = linkShaders(vert, frag); 73 74 if (shaderProgram != 0) 75 { 76 modelViewMatrixLoc = glGetUniformLocation(shaderProgram, "modelViewMatrix"); 77 prevModelViewProjMatrixLoc = glGetUniformLocation(shaderProgram, "prevModelViewProjMatrix"); 78 projectionMatrixLoc = glGetUniformLocation(shaderProgram, "projectionMatrix"); 79 80 viewportSizeLoc = glGetUniformLocation(shaderProgram, "viewSize"); 81 fbColorLoc = glGetUniformLocation(shaderProgram, "fbColor"); 82 enabledLoc = glGetUniformLocation(shaderProgram, "enabled"); 83 } 84 } 85 86 void bind(RenderingContext* rc) 87 { 88 glUseProgram(shaderProgram); 89 90 glUniformMatrix4fv(modelViewMatrixLoc, 1, 0, rc.viewMatrix.arrayof.ptr); 91 glUniformMatrix4fv(projectionMatrixLoc, 1, 0, rc.projectionMatrix.arrayof.ptr); 92 93 glUniformMatrix4fv(prevModelViewProjMatrixLoc, 1, 0, rc.prevModelViewProjMatrix.arrayof.ptr); 94 95 Vector2f viewportSize; 96 97 auto colorTexture = inputBuffer.currentColorTexture; 98 99 if (outputBuffer) 100 viewportSize = Vector2f(outputBuffer.width, outputBuffer.height); 101 else 102 viewportSize = Vector2f(rc.eventManager.windowWidth, rc.eventManager.windowHeight); 103 glUniform2fv(viewportSizeLoc, 1, viewportSize.arrayof.ptr); 104 105 glActiveTexture(GL_TEXTURE0); 106 glBindTexture(GL_TEXTURE_2D, colorTexture); 107 108 glUniform1i(fbColorLoc, 0); 109 110 glUniform1i(enabledLoc, enabled); 111 } 112 113 void unbind(RenderingContext* rc) 114 { 115 glActiveTexture(GL_TEXTURE0); 116 glBindTexture(GL_TEXTURE_2D, 0); 117 118 glActiveTexture(GL_TEXTURE0); 119 120 glUseProgram(0); 121 } 122 123 void render(RenderingContext* rc) 124 { 125 bind(rc); 126 inputBuffer.render(); 127 unbind(rc); 128 } 129 }