Speed up and fix unigi_ext_texture_draw

This commit is contained in:
Fierelier 2024-09-25 21:12:28 +02:00
parent b1cc74007a
commit d8789b7689
2 changed files with 40 additions and 48 deletions

84
main.c
View File

@ -1,51 +1,43 @@
#include "unigi.ext/main.h" #include "unigi.ext/main.h"
void unigi_ext_texture_draw( void unigi_ext_texture_draw(unigi_ext_type_texture *sprite, unigi_ext_type_texture *fb, unigi_ext_type_rect texrect, unigi_ext_type_rect outrect) {
unigi_ext_type_texture tex, // Precalc the step, as it's always the same even if we clip the rect
unigi_ext_type_texture buffer, unigi_ext_type_2d_coord tex_width, tex_height, out_width, out_height;
unigi_ext_type_rect texbounds, unigi_ext_rect_dimensions(texrect,&tex_width,&tex_height);
unigi_ext_type_rect bufbounds unigi_ext_rect_dimensions(outrect,&out_width,&out_height);
) { int32_t stepx = (1 << unigi_ext_texture_fixedpointdepth) * (float)tex_width / out_width;
unigi_ext_type_2d_coord texwidth; int32_t stepy = (1 << unigi_ext_texture_fixedpointdepth) * (float)tex_height / out_height;
unigi_ext_type_2d_coord texheight; int32_t texx_init = (1 << unigi_ext_texture_fixedpointdepth) * texrect.x1;
unigi_ext_type_2d_coord outwidth; int32_t texy = (1 << unigi_ext_texture_fixedpointdepth) * texrect.y1;
unigi_ext_type_2d_coord outheight; // Clip the rect
unigi_ext_type_2d_coord x; if (outrect.x1 < 0) {
unigi_ext_type_2d_coord y; texx_init += stepx * -outrect.x1;
unigi_type_resolution_1d_coord texi; outrect.x1 = 0;
unigi_type_resolution_1d_coord bufi; }
unigi_ext_type_2d_coord stepx; if (outrect.y1 < 0) {
unigi_ext_type_2d_coord stepy; texy += stepy * -outrect.y1;
unigi_ext_type_2d_coord texx; outrect.y1 = 0;
unigi_ext_type_2d_coord texy; }
if (outrect.x2 >= fb->width) {
unigi_ext_rect_dimensions(texbounds, &texwidth, &texheight); outrect.x2 = fb->width - 1;
unigi_ext_rect_dimensions(bufbounds, &outwidth, &outheight); }
// We precalculate the step through the texture to avoid division and if (outrect.y2 >= fb->height) {
// multiplication per pixel. We also expand the range so we can use outrect.y2 = fb->height - 1;
// integers. By using 256, we are essentially adding 8 bits of "decimal }
// point". If you need more bits (or less), just change this to some other
// power of 2. for (int y = outrect.y1; y < outrect.y2; y++) {
stepx = (1 << unigi_ext_texture_fixedpointdepth) * (float)texwidth / outwidth; int32_t texx = texx_init;
stepy = (1 << unigi_ext_texture_fixedpointdepth) * (float)texheight / outheight; int yiter = y * fb->width;
texx = (1 << unigi_ext_texture_fixedpointdepth) * texbounds.x1; for (int x = outrect.x1; x < outrect.x2; x++) {
texy = (1 << unigi_ext_texture_fixedpointdepth) * texbounds.y1; uint16_t pix = sprite->pixels[((texy >> unigi_ext_texture_fixedpointdepth) * sprite->width) + (texx >> unigi_ext_texture_fixedpointdepth)];
//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, if (pix & 0xF000) {
// it's: 3 * (x + y * width) fb->pixels[yiter + x] = pix;
for(y = bufbounds.y1; y < bufbounds.y2; y++) { }
texx = texbounds.x1; texx += stepx;
for(x = bufbounds.x1; x < bufbounds.x2; x++) { }
// And then with the shift, it is log2(256), or whatever you shifted texy += stepy;
// by up there. So 512 would be 9, because 2^9 = 512 }
texi = (texx >> unigi_ext_texture_fixedpointdepth) + tex.width * (texy >> unigi_ext_texture_fixedpointdepth); // 'texy >> FIXEDPOINTDEPTH' could be calculated per y-iteration
bufi = x + y * buffer.width; // 'y * buffer.width' could be calculated per y-iteration
buffer.pixels[bufi] = tex.pixels[texi];
texx += stepx;
bufi += 1;
}
texy += stepy;
}
} }
unigi_ext_type_texture * unigi_ext_texture_create(unigi_ext_type_2d_coord width, unigi_ext_type_2d_coord height) { unigi_ext_type_texture * unigi_ext_texture_create(unigi_ext_type_2d_coord width, unigi_ext_type_2d_coord height) {

4
main.h
View File

@ -5,8 +5,8 @@
#include "structs.h" #include "structs.h"
void unigi_ext_texture_draw( void unigi_ext_texture_draw(
unigi_ext_type_texture tex, unigi_ext_type_texture * tex,
unigi_ext_type_texture buffer, unigi_ext_type_texture * buffer,
unigi_ext_type_rect texbounds, unigi_ext_type_rect texbounds,
unigi_ext_type_rect bufbounds unigi_ext_type_rect bufbounds
); );