Commit 11eb6032 authored by Christian Schulte zu Berge's avatar Christian Schulte zu Berge
Browse files

cleaned up GlReduction interface

parent 18b72370
...@@ -45,13 +45,5 @@ void main() { ...@@ -45,13 +45,5 @@ void main() {
vec4 c = texelFetch(_texture, texel + ivec2(0, 1), 0); vec4 c = texelFetch(_texture, texel + ivec2(0, 1), 0);
vec4 d = texelFetch(_texture, texel + ivec2(1, 1), 0); vec4 d = texelFetch(_texture, texel + ivec2(1, 1), 0);
//vec4 a = texture(_texture, tmp); out_Color = REDUCTION_OP(a, b, c, d);
//vec4 b = texture(_texture, texel + vec2(_textureParams._sizeRCP.x, 0));
//vec4 c = texture(_texture, texel + vec2(0, _textureParams._sizeRCP.y));
//vec4 d = texture(_texture, texel + vec2(_textureParams._sizeRCP.x, _textureParams._sizeRCP.y));
//out_Color = max(a, max(b, max(c, d)));
out_Color = a+b+c+d;
//out_Color = vec4(texel, 0.0, 1.0);
//out_Color = vec4(ex_TexCoord, 1.0);
} }
...@@ -48,12 +48,12 @@ namespace campvis { ...@@ -48,12 +48,12 @@ namespace campvis {
const std::string GlReduction::loggerCat_ = "CAMPVis.modules.registration.GlReduction"; const std::string GlReduction::loggerCat_ = "CAMPVis.modules.registration.GlReduction";
GlReduction::GlReduction() GlReduction::GlReduction(ReductionOperator reductionOperator)
: _shader(0) : _reductionOperator(reductionOperator)
, _shader(0)
, _fbo(0) , _fbo(0)
, _renderQuad(0)
{ {
_shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "core/glsl/tools/glreduction.frag", "", false); _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "core/glsl/tools/glreduction.frag", generateGlslHeader(_reductionOperator), false);
if (_shader == 0) { if (_shader == 0) {
LERROR("Could not load Shader for OpenGL reduction. Reduction will not work!"); LERROR("Could not load Shader for OpenGL reduction. Reduction will not work!");
return; return;
...@@ -100,7 +100,6 @@ namespace campvis { ...@@ -100,7 +100,6 @@ namespace campvis {
return toReturn; return toReturn;
} }
//tgtAssert(texture->getNumChannels() == 1, "Reduction of images with more than one channel currently not implemented! Somebody was too lazy (or stressed - deadline was close) to do that...");
tgtAssert(texture->getDimensions().z == 1, "Reduction of 3D images not yet implemented! Somebody was too lazy (or stressed - deadline was close) to do that..."); tgtAssert(texture->getDimensions().z == 1, "Reduction of 3D images not yet implemented! Somebody was too lazy (or stressed - deadline was close) to do that...");
std::vector<float> readBackBuffer; std::vector<float> readBackBuffer;
...@@ -116,14 +115,14 @@ namespace campvis { ...@@ -116,14 +115,14 @@ namespace campvis {
tgt::TextureUnit inputUnit; tgt::TextureUnit inputUnit;
// create temporary textures // create temporary textures
tgt::Texture* tempTextures[2];
for (size_t i = 0; i < 2; ++i) { for (size_t i = 0; i < 2; ++i) {
//_tempTextures[i] = new tgt::Texture(0, tgt::ivec3(currentSize, 1), GL_RED, GL_R32F, GL_FLOAT, tgt::Texture::NEAREST); tempTextures[i] = new tgt::Texture(0, tgt::ivec3(currentSize, 1), GL_RGBA, GL_RGBA32F, GL_FLOAT, tgt::Texture::NEAREST);
_tempTextures[i] = new tgt::Texture(0, tgt::ivec3(currentSize, 1), GL_RGBA, GL_RGBA32F, GL_FLOAT, tgt::Texture::NEAREST); tempTextures[i]->uploadTexture();
_tempTextures[i]->uploadTexture(); tempTextures[i]->setWrapping(tgt::Texture::CLAMP);
_tempTextures[i]->setWrapping(tgt::Texture::CLAMP);
} }
_readTex = 0; size_t readTex = 0;
_writeTex = 1; size_t writeTex = 1;
// create and initialize FBO // create and initialize FBO
_fbo = new tgt::FramebufferObject(); _fbo = new tgt::FramebufferObject();
...@@ -132,7 +131,7 @@ namespace campvis { ...@@ -132,7 +131,7 @@ namespace campvis {
// perform first reduction step outside: // perform first reduction step outside:
_shader->activate(); _shader->activate();
_fbo->attachTexture(_tempTextures[_readTex]); _fbo->attachTexture(tempTextures[readTex]);
_shader->setIgnoreUniformLocationError(true); _shader->setIgnoreUniformLocationError(true);
inputUnit.activate(); inputUnit.activate();
...@@ -152,8 +151,8 @@ namespace campvis { ...@@ -152,8 +151,8 @@ namespace campvis {
while (currentSize.x > 1 || currentSize.y > 1) { while (currentSize.x > 1 || currentSize.y > 1) {
reduceSizes(currentSize, texCoordMultiplier); reduceSizes(currentSize, texCoordMultiplier);
_fbo->attachTexture(_tempTextures[_writeTex]); _fbo->attachTexture(tempTextures[writeTex]);
_tempTextures[_readTex]->bind(); tempTextures[readTex]->bind();
glViewport(0, 0, currentSize.x, currentSize.y); glViewport(0, 0, currentSize.x, currentSize.y);
_shader->setUniform("_texCoordsMultiplier", texCoordMultiplier); _shader->setUniform("_texCoordsMultiplier", texCoordMultiplier);
...@@ -161,15 +160,15 @@ namespace campvis { ...@@ -161,15 +160,15 @@ namespace campvis {
LGL_ERROR; LGL_ERROR;
//_fbo->detachTexture(GL_COLOR_ATTACHMENT0); //_fbo->detachTexture(GL_COLOR_ATTACHMENT0);
std::swap(_writeTex, _readTex); std::swap(writeTex, readTex);
} }
_shader->deactivate(); _shader->deactivate();
// read back stuff // read back stuff
GLenum readBackFormat = _tempTextures[_readTex]->getFormat(); GLenum readBackFormat = tempTextures[readTex]->getFormat();
size_t channels = _tempTextures[_readTex]->getNumChannels(); size_t channels = tempTextures[readTex]->getNumChannels();
toReturn.resize(currentSize.x * currentSize.y * channels); toReturn.resize(currentSize.x * currentSize.y * channels);
glReadBuffer(GL_COLOR_ATTACHMENT0); glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(0, 0, currentSize.x, currentSize.y, readBackFormat, GL_FLOAT, &toReturn.front()); glReadPixels(0, 0, currentSize.x, currentSize.y, readBackFormat, GL_FLOAT, &toReturn.front());
...@@ -181,8 +180,8 @@ namespace campvis { ...@@ -181,8 +180,8 @@ namespace campvis {
delete _fbo; delete _fbo;
_fbo = 0; _fbo = 0;
delete _tempTextures[0]; delete tempTextures[0];
delete _tempTextures[1]; delete tempTextures[1];
LGL_ERROR; LGL_ERROR;
return toReturn; return toReturn;
...@@ -200,4 +199,25 @@ namespace campvis { ...@@ -200,4 +199,25 @@ namespace campvis {
} }
std::string GlReduction::generateGlslHeader(ReductionOperator reductionOperator) {
switch (reductionOperator) {
case MIN:
return "#define REDUCTION_OP(a, b, c, d) min(a, min(b, min(c, d)))";
break;
case MAX:
return "#define REDUCTION_OP(a, b, c, d) max(a, max(b, max(c, d)))";
break;
case PLUS:
return "#define REDUCTION_OP(a, b, c, d) a+b+c+d";
break;
case MULTIPLICATION:
return "#define REDUCTION_OP(a, b, c, d) a*b*c*d";
break;
default:
tgtAssert(false, "Should not reach this, wrong enum value?");
return "";
break;
}
}
} }
\ No newline at end of file
...@@ -49,18 +49,45 @@ namespace campvis { ...@@ -49,18 +49,45 @@ namespace campvis {
/** /**
* Class performing a reduction of 2D image data using OpenGL. * Class performing a reduction of 2D image data using OpenGL.
* TODO: implement 3D reduction - shouldn't be that difficult * \note All methods need to be called from a valid OpenGL context.
*/ */
class GlReduction { class GlReduction {
public: public:
GlReduction(); /// Operation to be performed by reduction
enum ReductionOperator {
~GlReduction(); MIN, ///< Minimum
MAX, ///< Maximum
PLUS, ///< Sum (plus)
MULTIPLICATION ///< Product (multiplication)
};
/**
* Constructor
* \param reductionOperator Operation to be performed by reduction
*/
GlReduction(ReductionOperator reductionOperator);
/**
* Destructor
*/
~GlReduction();
/**
* Performs the reduction on the given image.
* \note Image must be two-dimensional!
* \note Reduction will be performed on a copy, \a image will not change.
* \param image Pointer to ImageData to perform reduction on, must be two-dimensional.
* \return A vector of floats containing the reduction results for each channel of the input image.
*/
std::vector<float> reduce(const ImageData* image); std::vector<float> reduce(const ImageData* image);
/**
* Performs the reduction on the given OpenGL texture.
* \note Image must be two-dimensional!
* \note Reduction will be performed on a copy, \a texture will not change.
* \param image Pointer to a texture to perform reduction on, must be two-dimensional.
* \return A vector of floats containing the reduction results for each channel of the input texture.
*/
std::vector<float> reduce(const tgt::Texture* texture); std::vector<float> reduce(const tgt::Texture* texture);
...@@ -72,13 +99,17 @@ namespace campvis { ...@@ -72,13 +99,17 @@ namespace campvis {
*/ */
static void reduceSizes(tgt::ivec2& currentSize, tgt::vec2& texCoordMultiplier); static void reduceSizes(tgt::ivec2& currentSize, tgt::vec2& texCoordMultiplier);
size_t _readTex; /**
size_t _writeTex; * Generates the GLSL header corresponding to the given reduction operator.
* \param reductionOperator Operation to be performed by reduction
* \return A std::string with the corresponding GLSL header.
*/
static std::string generateGlslHeader(ReductionOperator reductionOperator);
tgt::Shader* _shader; ReductionOperator _reductionOperator; ///< Operation to be performed by reduction
tgt::FramebufferObject* _fbo; tgt::Shader* _shader; ///< OpenGL shader performing the reduction
tgt::Texture* _tempTextures[2]; tgt::FramebufferObject* _fbo; ///< FBO performing the reduction
FaceGeometry* _renderQuad;
const static std::string loggerCat_; const static std::string loggerCat_;
}; };
......
...@@ -128,7 +128,7 @@ namespace campvis { ...@@ -128,7 +128,7 @@ namespace campvis {
_differenceShader->setAttributeLocation(0, "in_Position"); _differenceShader->setAttributeLocation(0, "in_Position");
_differenceShader->setAttributeLocation(1, "in_TexCoord"); _differenceShader->setAttributeLocation(1, "in_TexCoord");
_glr = new GlReduction(); _glr = new GlReduction(GlReduction::PLUS);
} }
void SimilarityMeasure::deinit() { void SimilarityMeasure::deinit() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment