Skip to content

Commit 2666eed

Browse files
committed
review get pixel functions
+ review unorm/float conversion
1 parent d3fe48f commit 2666eed

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/external/rlsw.h

+24-21
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ void swBindTexture(uint32_t id);
337337
#define SW_DEG2RAD (SW_PI/180.0f)
338338
#define SW_RAD2DEG (180.0f/SW_PI)
339339

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+
340346
#define SW_STATE_CHECK(flags) ((RLSW.stateFlags & (flags)) == (flags))
341347

342348
#define SW_STATE_TEXTURE_2D (1 << 0)
@@ -639,7 +645,7 @@ static inline float sw_cvt_hf(sw_half_t y)
639645

640646
static inline void sw_get_pixel_grayscale(float* color, const void* pixels, uint32_t offset)
641647
{
642-
float gray = (float)((uint8_t*)pixels)[offset] / 255;
648+
float gray = SW_UNORM8_TO_FLOAT(((uint8_t*)pixels)[offset]);
643649

644650
color[0] = gray;
645651
color[1] = gray;
@@ -669,13 +675,10 @@ static inline void sw_get_pixel_red_32(float* color, const void* pixels, uint32_
669675

670676
static inline void sw_get_pixel_grayscale_alpha(float* color, const void* pixels, uint32_t offset)
671677
{
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;
674679

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]);
679682
}
680683

681684
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
690693

691694
static inline void sw_get_pixel_rgb_888(float* color, const void* pixels, uint32_t offset)
692695
{
693-
const uint8_t* pixel = (uint8_t*)pixels + 3 * offset;
696+
const uint8_t* pixel = (const uint8_t*)pixels + 3 * offset;
694697

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]);
698701
color[3] = 1.0f;
699702
}
700703

@@ -742,10 +745,10 @@ static inline void sw_get_pixel_rgba_8888(float* color, const void* pixels, uint
742745
{
743746
const uint8_t *pixel = (uint8_t*)pixels + 4 * offset;
744747

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]);
749752
}
750753

751754
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,
12921295
if (ENABLE_DEPTH_TEST) { \
12931296
/* Depth testing with direct access to the depth buffer */ \
12941297
/* TODO: Implement different depth funcs? */ \
1295-
float depth = (float)(*dptr) / UINT16_MAX; \
1298+
float depth = SW_UNORM16_TO_FLOAT(*dptr); \
12961299
if (z > depth) goto discard; \
12971300
} \
12981301
\
12991302
/* Update the depth buffer */ \
1300-
*dptr = (uint16_t)(z * UINT16_MAX); \
1303+
*dptr = SW_FLOAT_TO_UNORM16(z); \
13011304
\
13021305
if (ENABLE_COLOR_BLEND) \
13031306
{ \
@@ -1748,11 +1751,11 @@ static inline void FUNC_NAME(const sw_vertex_t* v0, const sw_vertex_t* v1) \
17481751
\
17491752
uint16_t* dptr = &depthBuffer[pixel_index]; \
17501753
if (ENABLE_DEPTH_TEST) { \
1751-
float depth = (float)(*dptr) / UINT16_MAX; \
1754+
float depth = SW_UNORM16_TO_FLOAT(*dptr); \
17521755
if (z > depth) continue; \
17531756
} \
17541757
\
1755-
*dptr = (uint16_t)(z * UINT16_MAX); \
1758+
*dptr = SW_FLOAT_TO_UNORM16(z); \
17561759
\
17571760
int color_index = 4 * pixel_index; \
17581761
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) \
17901793
\
17911794
uint16_t* dptr = &depthBuffer[pixel_index]; \
17921795
if (ENABLE_DEPTH_TEST) { \
1793-
float depth = (float)(*dptr) / UINT16_MAX; \
1796+
float depth = SW_UNORM16_TO_FLOAT(*dptr); \
17941797
if (z > depth) continue; \
17951798
} \
17961799
\
1797-
*dptr = (uint16_t)(z * UINT16_MAX); \
1800+
*dptr = SW_FLOAT_TO_UNORM16(z); \
17981801
\
17991802
int color_index = 4 * pixel_index; \
18001803
uint8_t* cptr = &colorBuffer[color_index]; \

0 commit comments

Comments
 (0)