Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color mixing is done with floats, and is a bottleneck #6

Open
smcameron opened this issue Oct 24, 2021 · 0 comments
Open

Color mixing is done with floats, and is a bottleneck #6

smcameron opened this issue Oct 24, 2021 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@smcameron
Copy link
Owner

smcameron commented Oct 24, 2021

Color blending is done by converting bytes to floats, mixing, then converting floats back to bytes. Additionally, color blending is a bottleneck in the program.

I have a strong suspicion this could all be speeded up a little bit by using fixed point arithmetic to do the blending, perhaps something like this: https://www.reddit.com/r/GraphicsProgramming/comments/qczmbi/can_someone_walk_me_through_why_this_blender_works/

uint32 blend(uint32 color1, uint32 color2, uint8 alpha) {
    uint32 rb = color1 & 0xff00ff;
    uint32 g  = color1 & 0x00ff00;
    rb += ((color2 & 0xff00ff) - rb) * alpha >> 8;
    g  += ((color2 & 0x00ff00) -  g) * alpha >> 8;
    return (rb & 0xff00ff) | (g & 0xff00);
}

I have not tried the above code, but I have tried this, below:

static struct color combine_color(struct color *oc, struct color *c)
{
        unsigned int alpha = oc->a + 1;
        unsigned int inv_alpha = 256 - alpha;

        struct color nc;

        nc.a = (c->a + oc->a * (256 - c->a)) >> 8;
        nc.r = (unsigned char) (alpha * oc->r + inv_alpha * c->r) >> 8;
        nc.g = (unsigned char) (alpha * oc->g + inv_alpha * c->g) >> 8;
        nc.b = (unsigned char) (alpha * oc->b + inv_alpha * c->b) >> 8;
        return nc;
}

but so far have not got anything working. In theory, using fixed point math for the color mixing rather than floating point should be an easy win.

@smcameron smcameron added enhancement New feature or request help wanted Extra attention is needed labels Oct 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant