0

Is there a anyway to optimize the next algorithm to be any faster, even if is just a small speed increase?

const mat3 factor = mat3(1.0, 1.0, 1.0, 2.112, 1.4, 0.0, 0.0, 2.18, -2.21);

vec3 calculate(in vec2 coord)
{
    vec3 sample = texture2D(texture_a, coord).rgb;
    return (factor / sample) * 2.15;
} 

2 Answers 2

2

The only significant optimization I can think of is to pack texture_a and texture_b into a single three-channel texture, if you can. That saves you one of the two texture lookups, which are most likely to be the bottleneck here.

0

@Thomas answer is the most helpfull, since texture lookups are most expensive, if his solution is possible in your application. If you already use those textures somewhere else better pass the values as parameters to avoid duplicate lookups.

Else I don't know if it can be optimized that much but some straight forward things that come to my mind.

Compiler optimizations:

Assign const keyword to coord parameter, if possible to sample too.
Assign f literal in each float element.

Maybe manually assign mat

I don't know if its faster because I don't know how the matrix multiplication is implemented but since the constant factor matrix contains many ones and zeros it maybe can be manually assigned.

vec3 calculate(const in vec2 coord)
{
    //not 100% sure if that init is possible
    const vec3 sample = vec3(texture2D(texture_a, coord).r
        texture2D(texture_b, coord).ra - 0.5f);

    vec3 result = vec3(sample.y);
    result.x += sample.x + sample.z;
    result.y += 2.112f * sample.x;
    result.z *= 2.18f;
    result.z -= 2.21f * sample.z;

    return result;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.