From a04e58c7cae08ce80a295d6138c9e869b434f03a Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 28 Apr 2017 20:37:44 +0000 Subject: [PATCH] Attempt to implement faster comparison algorithm --- compare.glsl | 40 ++++++++++++++++++++++++++++-- src/Fitness.cpp | 66 +++++++++++++++++++++++++++++++++++++++++-------- src/Fitness.hpp | 5 +++- 3 files changed, 98 insertions(+), 13 deletions(-) diff --git a/compare.glsl b/compare.glsl index 233fcf0..e367810 100644 --- a/compare.glsl +++ b/compare.glsl @@ -1,13 +1,48 @@ -// Compute the pixelwise diff between two textures. -// Computes the diff for each color chanel separately (r, g, b). +// Compute the pixelwise diff between two textures, one row at a time. // Assumes both images are not transparent. +// Warning: Only works on rows <= ((2**8)**4)/255 = 16843009 // try isampler2D, ivec4 etc.? Probably not uniform vec2 size; +uniform bool horizontal; // uniform vec2 offs; uniform sampler2D base; uniform sampler2D curr; +void main(void) +{ + // calculate total diff of row in either direction + float total = 0.0; + + if (horizontal) { + float posy = gl_FragCoord.y/size.y; + for (float x=0.0; xtarget = target; sf::Vector2u targetSize = this->target.getSize(); + this->tex.create(targetSize.x, targetSize.y); - this->sprite.setTexture(this->tex.getTexture()); - this->comp.create(targetSize.x*scale, targetSize.y*scale); + +// this->view.reset(sf::FloatRect(0, 0, targetSize.x, targetSize.y)); +// this->view.setViewport(sf::FloatRect(0, 0, 1, 1)); + + this->horizontal = targetSize.x > targetSize.y; + if (this->horizontal) { + targetSize.x = 1; + } else { + targetSize.y = 1; +// this->comp.create(targetSize.x*scale, 2); + } + + this->comp.create(targetSize.x, targetSize.y); // this->comp.setSmooth(true); - this->view.reset(sf::FloatRect(0, 0, targetSize.x, targetSize.y)); - this->view.setViewport(sf::FloatRect(0, 0, 1, 1)); + this->dummy[0] = sf::Vertex(sf::Vector2f(0, 0), sf::Color::Red ); + this->dummy[1] = sf::Vertex(sf::Vector2f(targetSize.x, 0), sf::Color::Yellow); + this->dummy[2] = sf::Vertex(sf::Vector2f(targetSize), sf::Color::Green ); + this->dummy[3] = sf::Vertex(sf::Vector2f(0, targetSize.y), sf::Color::Blue ); } @@ -26,6 +41,7 @@ bool Fitness::loadShader(std::string filename) this->compshdr.setUniform("base", this->target); this->compshdr.setUniform("curr", this->tex.getTexture()); this->compshdr.setUniform("size", sf::Vector2f(this->comp.getSize())); + this->compshdr.setUniform("horizontal", this->horizontal); return true; } else { return false; @@ -41,8 +57,9 @@ unsigned long long Fitness::of(Chromosome chr) this->tex.display(); this->comp.clear(sf::Color::Blue); - this->comp.setView(this->view); - this->comp.draw(this->sprite, &this->compshdr); +// this->comp.setView(this->view); + this->comp.draw(this->dummy, &this->compshdr); +// this->comp.draw(this->sprite, &this->compshdr); // this->comp.draw(this->sprite); this->comp.display(); @@ -50,11 +67,40 @@ unsigned long long Fitness::of(Chromosome chr) sf::Image image = this->comp.getTexture().copyToImage(); sf::Vector2u size = image.getSize(); unsigned long long fitness = 0; - for (unsigned int x=0; xhorizontal) { for (unsigned int y=0; y