Example of pulling math out of loop using more increments

This commit is contained in:
Carlos Sanchez 2024-07-22 20:58:35 -04:00
parent 4db50f3f73
commit 32c37b8a7d

19
main.c
View File

@ -58,22 +58,25 @@ void drawTexInto(struct texture tex, struct texture buffer, struct rect texbound
i32 stepy = 256 * (float)texheight / outheight;
i32 texx = 256 * texbounds.x1;
i32 texy = 256 * texbounds.y1;
i32 bufy = 3 * (bufbounds.x1 + bufbounds.y1 * buffer.width);
i32 bufyinc = 3 * buffer.width;
i32 bufi = bufy;
//log("%f,%f step %f,%f", texx, texy, stepx, stepy);
// Buffer is R G B for each pixel, then across then down. If you want X, Y,
// it's: 3 * (x + y * width)
for(int y = bufbounds.y1; y < bufbounds.y2; y++) {
texx = texbounds.x1;
bufi = bufy;
for(int x = bufbounds.x1; x < bufbounds.x2; x++) {
i32 texi = 3 * ((texx >> 8) + tex.width * (texy >> 8));
i32 bufi = 3 * (x + buffer.width * y);
// You can get rid of this for loop for more performance, but probably
// not a big deal with -O3, it'll be unrolled
buffer.data[bufi] = tex.data[texi];
buffer.data[bufi + 1] = tex.data[texi + 1];
buffer.data[bufi + 2] = tex.data[texi + 2];
memcpy(buffer.data + bufi, tex.data + texi, 3);
texx += stepx;
bufi += 3;
}
texy += stepy;
bufy += bufyinc;
}
}
@ -98,10 +101,10 @@ int main() {
// We actually stretch a texture here
struct rect texrect = { 0, 0, 16, 16 };
struct rect bufrect = { 10, 10, 26, 26 };
struct rect bufrect = { 10, 10, 509, 499 };
clock_t start = clock();
for(int i = 0; i < 50000; i++) { //overdraw factor
for(int i = 0; i < 50; i++) { //overdraw factor
drawTexInto(tex, fb, texrect, bufrect);
}
clock_t end = clock();