@@ -337,6 +337,12 @@ void swBindTexture(uint32_t id);
337
337
#define SW_DEG2RAD (SW_PI/180.0f)
338
338
#define SW_RAD2DEG (180.0f/SW_PI)
339
339
340
+ #define SW_FLOAT_TO_UNORM8 (x ) ((uint8_t)((x) * UINT8_MAX))
341
+ #define SW_FLOAT_TO_UNORM16 (x ) ((uint16_t)((x) * UINT16_MAX))
342
+
343
+ #define SW_UNORM8_TO_FLOAT (x ) ((float)(x) * (1.0f / UINT8_MAX))
344
+ #define SW_UNORM16_TO_FLOAT (x ) ((float)(x) * (1.0f / UINT16_MAX))
345
+
340
346
#define SW_STATE_CHECK (flags ) ((RLSW.stateFlags & (flags)) == (flags))
341
347
342
348
#define SW_STATE_TEXTURE_2D (1 << 0)
@@ -639,7 +645,7 @@ static inline float sw_cvt_hf(sw_half_t y)
639
645
640
646
static inline void sw_get_pixel_grayscale (float * color , const void * pixels , uint32_t offset )
641
647
{
642
- float gray = ( float ) ((uint8_t * )pixels )[offset ] / 255 ;
648
+ float gray = SW_UNORM8_TO_FLOAT ( ((uint8_t * )pixels )[offset ]) ;
643
649
644
650
color [0 ] = gray ;
645
651
color [1 ] = gray ;
@@ -669,13 +675,10 @@ static inline void sw_get_pixel_red_32(float* color, const void* pixels, uint32_
669
675
670
676
static inline void sw_get_pixel_grayscale_alpha (float * color , const void * pixels , uint32_t offset )
671
677
{
672
- float gray = (float )((uint8_t * )pixels )[2 * offset ] / 255 ;
673
- float alpha = (float )((uint8_t * )pixels )[2 * offset + 1 ] / 255 ;
678
+ const uint8_t * pixelData = (const uint8_t * )pixels + 2 * offset ;
674
679
675
- color [0 ] = gray ;
676
- color [1 ] = gray ;
677
- color [2 ] = gray ;
678
- color [3 ] = alpha ;
680
+ color [0 ] = color [1 ] = color [2 ] = SW_UNORM8_TO_FLOAT (pixelData [0 ]);
681
+ color [3 ] = SW_UNORM8_TO_FLOAT (pixelData [1 ]);
679
682
}
680
683
681
684
static inline void sw_get_pixel_rgb_565 (float * color , const void * pixels , uint32_t offset )
@@ -690,11 +693,11 @@ static inline void sw_get_pixel_rgb_565(float* color, const void* pixels, uint32
690
693
691
694
static inline void sw_get_pixel_rgb_888 (float * color , const void * pixels , uint32_t offset )
692
695
{
693
- const uint8_t * pixel = (uint8_t * )pixels + 3 * offset ;
696
+ const uint8_t * pixel = (const uint8_t * )pixels + 3 * offset ;
694
697
695
- color [0 ] = ( float ) pixel [0 ] / 255 ;
696
- color [1 ] = ( float ) pixel [1 ] / 255 ;
697
- color [2 ] = ( float ) pixel [2 ] / 255 ;
698
+ color [0 ] = SW_UNORM8_TO_FLOAT ( pixel [0 ]) ;
699
+ color [1 ] = SW_UNORM8_TO_FLOAT ( pixel [1 ]) ;
700
+ color [2 ] = SW_UNORM8_TO_FLOAT ( pixel [2 ]) ;
698
701
color [3 ] = 1.0f ;
699
702
}
700
703
@@ -742,10 +745,10 @@ static inline void sw_get_pixel_rgba_8888(float* color, const void* pixels, uint
742
745
{
743
746
const uint8_t * pixel = (uint8_t * )pixels + 4 * offset ;
744
747
745
- color [0 ] = ( float ) pixel [0 ] / 255 ;
746
- color [1 ] = ( float ) pixel [1 ] / 255 ;
747
- color [2 ] = ( float ) pixel [2 ] / 255 ;
748
- color [3 ] = ( float ) pixel [3 ] / 255 ;
748
+ color [0 ] = SW_UNORM8_TO_FLOAT ( pixel [0 ]) ;
749
+ color [1 ] = SW_UNORM8_TO_FLOAT ( pixel [1 ]) ;
750
+ color [2 ] = SW_UNORM8_TO_FLOAT ( pixel [2 ]) ;
751
+ color [3 ] = SW_UNORM8_TO_FLOAT ( pixel [3 ]) ;
749
752
}
750
753
751
754
static inline void sw_get_pixel_rgba_16161616 (float * color , const void * pixels , uint32_t offset )
@@ -1292,12 +1295,12 @@ static inline void FUNC_NAME(const sw_texture_t* tex, const sw_vertex_t* start,
1292
1295
if (ENABLE_DEPTH_TEST ) { \
1293
1296
/* Depth testing with direct access to the depth buffer */ \
1294
1297
/* TODO: Implement different depth funcs? */ \
1295
- float depth = ( float )( * dptr ) / UINT16_MAX ; \
1298
+ float depth = SW_UNORM16_TO_FLOAT ( * dptr ); \
1296
1299
if (z > depth ) goto discard ; \
1297
1300
} \
1298
1301
\
1299
1302
/* Update the depth buffer */ \
1300
- * dptr = ( uint16_t )( z * UINT16_MAX ); \
1303
+ * dptr = SW_FLOAT_TO_UNORM16 ( z ); \
1301
1304
\
1302
1305
if (ENABLE_COLOR_BLEND ) \
1303
1306
{ \
@@ -1748,11 +1751,11 @@ static inline void FUNC_NAME(const sw_vertex_t* v0, const sw_vertex_t* v1) \
1748
1751
\
1749
1752
uint16_t* dptr = &depthBuffer[pixel_index]; \
1750
1753
if (ENABLE_DEPTH_TEST) { \
1751
- float depth = (float)( *dptr) / UINT16_MAX; \
1754
+ float depth = SW_UNORM16_TO_FLOAT( *dptr); \
1752
1755
if (z > depth) continue; \
1753
1756
} \
1754
1757
\
1755
- *dptr = (uint16_t)(z * UINT16_MAX); \
1758
+ *dptr = SW_FLOAT_TO_UNORM16(z); \
1756
1759
\
1757
1760
int color_index = 4 * pixel_index; \
1758
1761
uint8_t* cptr = &colorBuffer[color_index]; \
@@ -1790,11 +1793,11 @@ static inline void FUNC_NAME(const sw_vertex_t* v0, const sw_vertex_t* v1) \
1790
1793
\
1791
1794
uint16_t* dptr = &depthBuffer[pixel_index]; \
1792
1795
if (ENABLE_DEPTH_TEST) { \
1793
- float depth = (float)( *dptr) / UINT16_MAX; \
1796
+ float depth = SW_UNORM16_TO_FLOAT( *dptr); \
1794
1797
if (z > depth) continue; \
1795
1798
} \
1796
1799
\
1797
- *dptr = (uint16_t)(z * UINT16_MAX); \
1800
+ *dptr = SW_FLOAT_TO_UNORM16(z); \
1798
1801
\
1799
1802
int color_index = 4 * pixel_index; \
1800
1803
uint8_t* cptr = &colorBuffer[color_index]; \
0 commit comments