52#include <ACG/GL/AntiAliasing.hh>
53#include <ACG/GL/acg_glew.hh>
54#include <ACG/ShaderUtils/GLSLShader.hh>
55#include <ACG/GL/ScreenQuad.hh>
57#include <ACG/GL/ShaderCache.hh>
62#define MSAA_SCREENQUAD_SHADER "ScreenQuad/screenquad.glsl"
63#define MSAA_NEAREST_SHADER "MSAA/sample_nearest.glsl"
64#define MSAA_NEAREST_DEPTH_SHADER "MSAA/sample_nearest_and_depth.glsl"
65#define MSAA_LINEAR_SHADER "MSAA/sample_linear.glsl"
79MSFilterWeights::MSFilterWeights(
int _numSamples) : numSamples_(_numSamples) {
81 weights_.resize(_numSamples);
82 float sumWeights = 0.0f;
86 Vec2f texelCenter(0.5f, 0.5f);
90 glGetIntegerv(GL_SAMPLES, &fboSamples);
93 for (
int i = 0; i < _numSamples; ++i) {
97 glGetMultisamplefv(GL_SAMPLE_POSITION, i, val);
99 val[0] = val[1] = 0.5f;
101 Vec2f samplePosition(val[0], val[1]);
104 float sampleDist = (samplePosition - texelCenter).norm();
107 weights_[i] = 1.0f - sampleDist;
109 sumWeights += weights_[i];
114 for (
int i = 0; i < _numSamples; ++i)
115 weights_[i] /= sumWeights;
119void MSFilterWeights::asTextureBuffer( TextureBuffer& out ) {
121 out.setBufferData(numSamples_ * 4, &weights_[0], GL_R32F, GL_STATIC_DRAW);
130#ifdef MSTEXTURESAMPLER
131MSTextureSampler::MSTextureSampler() : shaderNearest_(0), shaderNearestDepth_(0), shaderLinear_(0) {
135MSTextureSampler::~MSTextureSampler() {
136 delete shaderNearest_;
137 delete shaderLinear_;
138 delete shaderNearestDepth_;
141MSTextureSampler& MSTextureSampler::instance() {
142 static MSTextureSampler singleton;
148void MSTextureSampler::init() {
152 if (!shaderNearestDepth_)
153 shaderNearestDepth_ =
GLSL::loadProgram(MSAA_SCREENQUAD_SHADER, MSAA_NEAREST_DEPTH_SHADER);
161void MSTextureSampler::filterMSAATexture_Nearest( GLuint _texture,
int _samples,
const float* _weights ) {
163 MSTextureSampler& sampler = instance();
166 if (!sampler.shaderNearest_)
186 glActiveTexture(GL_TEXTURE0);
187 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _texture);
190 ScreenQuad::draw(shader);
197void MSTextureSampler::filterMSAATexture_Nearest( GLuint _texture, GLuint _depthTexture,
int _samples,
const float* _weights ) {
199 MSTextureSampler& sampler = instance();
202 if (!sampler.shaderNearestDepth_)
223 glActiveTexture(GL_TEXTURE1);
224 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _depthTexture);
228 glActiveTexture(GL_TEXTURE0);
229 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _texture);
232 ScreenQuad::draw(shader);
239void MSTextureSampler::filterMSAATexture_Linear( GLuint _texture,
int _samples,
const float* _weights ) {
241 MSTextureSampler& sampler = instance();
244 if (!sampler.shaderLinear_)
264 glActiveTexture(GL_TEXTURE0);
265 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _texture);
268 ScreenQuad::draw(shader);
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
void disable()
Resets to standard rendering pipeline.
void use()
Enables the program object for using.
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
Namespace providing different geometric functions concerning angles.
GLSL::PtrProgram loadProgram(const char *vertexShaderFile, const char *tessControlShaderFile, const char *tessEvaluationShaderFile, const char *geometryShaderFile, const char *fragmentShaderFile, const GLSL::StringList *macros, bool verbose)