1 /*
2  Copyright (c) 2013 yvt
3 
4  This file is part of OpenSpades.
5 
6  OpenSpades is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OpenSpades is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OpenSpades.  If not, see <http://www.gnu.org/licenses/>.
18 
19  */
20 
21 #include <vector>
22 
23 #include <Core/Debug.h>
24 #include <Core/Math.h>
25 #include "GLFXAAFilter.h"
26 #include "GLProgram.h"
27 #include "GLProgramAttribute.h"
28 #include "GLProgramUniform.h"
29 #include "GLQuadRenderer.h"
30 #include "GLRenderer.h"
31 #include "IGLDevice.h"
32 
33 namespace spades {
34 	namespace draw {
GLFXAAFilter(GLRenderer * renderer)35 		GLFXAAFilter::GLFXAAFilter(GLRenderer *renderer) : renderer(renderer) {
36 			lens = renderer->RegisterProgram("Shaders/PostFilters/FXAA.program");
37 		}
Filter(GLColorBuffer input)38 		GLColorBuffer GLFXAAFilter::Filter(GLColorBuffer input) {
39 			SPADES_MARK_FUNCTION();
40 
41 			IGLDevice *dev = renderer->GetGLDevice();
42 			GLQuadRenderer qr(dev);
43 
44 			static GLProgramAttribute lensPosition("positionAttribute");
45 			static GLProgramUniform lensTexture("mainTexture");
46 			static GLProgramUniform inverseVP("inverseVP");
47 
48 			dev->Enable(IGLDevice::Blend, false);
49 
50 			lensPosition(lens);
51 			lensTexture(lens);
52 			inverseVP(lens);
53 
54 			lens->Use();
55 
56 			inverseVP.SetValue(1.f / dev->ScreenWidth(), 1.f / dev->ScreenHeight());
57 			lensTexture.SetValue(0);
58 
59 			// composite to the final image
60 			GLColorBuffer output = input.GetManager()->CreateBufferHandle();
61 
62 			qr.SetCoordAttributeIndex(lensPosition());
63 			dev->BindTexture(IGLDevice::Texture2D, input.GetTexture());
64 			dev->BindFramebuffer(IGLDevice::Framebuffer, output.GetFramebuffer());
65 			dev->Viewport(0, 0, output.GetWidth(), output.GetHeight());
66 			qr.Draw();
67 			dev->BindTexture(IGLDevice::Texture2D, 0);
68 
69 			return output;
70 		}
71 	}
72 }