Skip to content

Commit fe3f4a6

Browse files
committed
Fix linear mipmapping crash in tinydisplay
1 parent 09741ec commit fe3f4a6

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

panda/src/tinydisplay/zbuffer.cxx

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
33
* Z buffer: 16 bits Z / 32 bits color
4-
*
4+
*
55
*/
66
#include <stdlib.h>
77
#include <stdio.h>
@@ -159,7 +159,7 @@ ZB_copyBufferNoAlpha(const ZBuffer * zb, void *buf, int linesize) {
159159
PIXEL *p2 = p1 + xsize;
160160
while (p1 < p2) {
161161
// Make sure the alpha bits are set to 0xff.
162-
#ifdef WORDS_BIGENDIAN
162+
#ifdef WORDS_BIGENDIAN
163163
*p1 = *q1 | 0x000000ff;
164164
#else
165165
*p1 = *q1 | 0xff000000;
@@ -176,8 +176,8 @@ ZB_copyBufferNoAlpha(const ZBuffer * zb, void *buf, int linesize) {
176176
(((v >> 8) & 0xf800) | (((v) >> 5) & 0x07e0) | (((v) & 0xff) >> 3))
177177

178178
/* XXX: not optimized */
179-
static void ZB_copyFrameBuffer5R6G5B(const ZBuffer * zb,
180-
void *buf, int linesize)
179+
static void ZB_copyFrameBuffer5R6G5B(const ZBuffer * zb,
180+
void *buf, int linesize)
181181
{
182182
PIXEL *q;
183183
unsigned short *p, *p1;
@@ -202,8 +202,8 @@ static void ZB_copyFrameBuffer5R6G5B(const ZBuffer * zb,
202202
}
203203

204204
/* XXX: not optimized */
205-
static void ZB_copyFrameBufferRGB24(const ZBuffer * zb,
206-
void *buf, int linesize)
205+
static void ZB_copyFrameBufferRGB24(const ZBuffer * zb,
206+
void *buf, int linesize)
207207
{
208208
PIXEL *q;
209209
unsigned char *p, *p1;
@@ -276,7 +276,7 @@ ZB_copyFrameBufferNoAlpha(const ZBuffer * zb, void *buf,
276276
}
277277
}
278278

279-
// Copy from (source_xmin,source_ymin)+(source_xsize,source_ysize) to
279+
// Copy from (source_xmin,source_ymin)+(source_xsize,source_ysize) to
280280
// (dest_xmin,dest_ymin)+(dest_xsize,dest_ysize).
281281
void ZB_zoomFrameBuffer(ZBuffer *dest, int dest_xmin, int dest_ymin, int dest_xsize, int dest_ysize,
282282
const ZBuffer *source, int source_xmin, int source_ymin, int source_xsize, int source_ysize) {
@@ -483,14 +483,15 @@ lookup_texture_mipmap_linear(ZTextureDef *texture_def, int s, int t, unsigned in
483483
PIXEL p1, p2;
484484
int r, g, b, a;
485485

486-
p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level - 1);
486+
p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
487+
level = max((int)level - 1, 0);
487488
p2 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
488489

489-
unsigned int bitsize = (level - 1) + ZB_POINT_ST_FRAC_BITS;
490-
r = LINEAR_FILTER_BITSIZE(PIXEL_R(p1), PIXEL_R(p2), level_dx, bitsize);
491-
g = LINEAR_FILTER_BITSIZE(PIXEL_G(p1), PIXEL_G(p2), level_dx, bitsize);
492-
b = LINEAR_FILTER_BITSIZE(PIXEL_B(p1), PIXEL_B(p2), level_dx, bitsize);
493-
a = LINEAR_FILTER_BITSIZE(PIXEL_A(p1), PIXEL_A(p2), level_dx, bitsize);
490+
unsigned int bitsize = level + ZB_POINT_ST_FRAC_BITS;
491+
r = LINEAR_FILTER_BITSIZE(PIXEL_R(p2), PIXEL_R(p1), level_dx, bitsize);
492+
g = LINEAR_FILTER_BITSIZE(PIXEL_G(p2), PIXEL_G(p1), level_dx, bitsize);
493+
b = LINEAR_FILTER_BITSIZE(PIXEL_B(p2), PIXEL_B(p1), level_dx, bitsize);
494+
a = LINEAR_FILTER_BITSIZE(PIXEL_A(p2), PIXEL_A(p1), level_dx, bitsize);
494495

495496
return RGBA_TO_PIXEL(r, g, b, a);
496497
}
@@ -529,21 +530,23 @@ lookup_texture_mipmap_trilinear(ZTextureDef *texture_def, int s, int t, unsigned
529530
int sf, tf;
530531
int r, g, b, a;
531532

532-
p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t - ZB_ST_FRAC_HIGH, level - 1);
533-
p2 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t - ZB_ST_FRAC_HIGH, level - 1);
534-
sf = (s >> (level - 1)) & ZB_ST_FRAC_MASK;
535-
536-
p3 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t, level - 1);
537-
p4 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level - 1);
538-
tf = (t >> (level - 1)) & ZB_ST_FRAC_MASK;
539-
533+
p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t - ZB_ST_FRAC_HIGH, level);
534+
p2 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t - ZB_ST_FRAC_HIGH, level);
535+
sf = (s >> level) & ZB_ST_FRAC_MASK;
536+
537+
p3 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t, level);
538+
p4 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
539+
tf = (t >> level) & ZB_ST_FRAC_MASK;
540+
540541
r = BILINEAR_FILTER(PIXEL_R(p1), PIXEL_R(p2), PIXEL_R(p3), PIXEL_R(p4), sf, tf);
541542
g = BILINEAR_FILTER(PIXEL_G(p1), PIXEL_G(p2), PIXEL_G(p3), PIXEL_G(p4), sf, tf);
542543
b = BILINEAR_FILTER(PIXEL_B(p1), PIXEL_B(p2), PIXEL_B(p3), PIXEL_B(p4), sf, tf);
543544
a = BILINEAR_FILTER(PIXEL_A(p1), PIXEL_A(p2), PIXEL_A(p3), PIXEL_A(p4), sf, tf);
544545
p1a = RGBA_TO_PIXEL(r, g, b, a);
545546
}
546547

548+
level = max((int)level - 1, 0);
549+
547550
{
548551
PIXEL p1, p2, p3, p4;
549552
int sf, tf;
@@ -552,11 +555,11 @@ lookup_texture_mipmap_trilinear(ZTextureDef *texture_def, int s, int t, unsigned
552555
p1 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t - ZB_ST_FRAC_HIGH, level);
553556
p2 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t - ZB_ST_FRAC_HIGH, level);
554557
sf = (s >> level) & ZB_ST_FRAC_MASK;
555-
558+
556559
p3 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s - ZB_ST_FRAC_HIGH, t, level);
557560
p4 = ZB_LOOKUP_TEXTURE_MIPMAP_NEAREST(texture_def, s, t, level);
558561
tf = (t >> level) & ZB_ST_FRAC_MASK;
559-
562+
560563
r = BILINEAR_FILTER(PIXEL_R(p1), PIXEL_R(p2), PIXEL_R(p3), PIXEL_R(p4), sf, tf);
561564
g = BILINEAR_FILTER(PIXEL_G(p1), PIXEL_G(p2), PIXEL_G(p3), PIXEL_G(p4), sf, tf);
562565
b = BILINEAR_FILTER(PIXEL_B(p1), PIXEL_B(p2), PIXEL_B(p3), PIXEL_B(p4), sf, tf);
@@ -565,11 +568,11 @@ lookup_texture_mipmap_trilinear(ZTextureDef *texture_def, int s, int t, unsigned
565568
}
566569

567570
int r, g, b, a;
568-
unsigned int bitsize = (level - 1) + ZB_POINT_ST_FRAC_BITS;
569-
r = LINEAR_FILTER_BITSIZE(PIXEL_R(p1a), PIXEL_R(p2a), level_dx, bitsize);
570-
g = LINEAR_FILTER_BITSIZE(PIXEL_G(p1a), PIXEL_G(p2a), level_dx, bitsize);
571-
b = LINEAR_FILTER_BITSIZE(PIXEL_B(p1a), PIXEL_B(p2a), level_dx, bitsize);
572-
a = LINEAR_FILTER_BITSIZE(PIXEL_A(p1a), PIXEL_A(p2a), level_dx, bitsize);
571+
unsigned int bitsize = level + ZB_POINT_ST_FRAC_BITS;
572+
r = LINEAR_FILTER_BITSIZE(PIXEL_R(p2a), PIXEL_R(p1a), level_dx, bitsize);
573+
g = LINEAR_FILTER_BITSIZE(PIXEL_G(p2a), PIXEL_G(p1a), level_dx, bitsize);
574+
b = LINEAR_FILTER_BITSIZE(PIXEL_B(p2a), PIXEL_B(p1a), level_dx, bitsize);
575+
a = LINEAR_FILTER_BITSIZE(PIXEL_A(p2a), PIXEL_A(p1a), level_dx, bitsize);
573576

574577
return RGBA_TO_PIXEL(r, g, b, a);
575578
}

0 commit comments

Comments
 (0)