Skip to content

Commit bca5404

Browse files
authored
[rlsw] Review depth formats and fix depth writing (#5317)
* review depth format/writing * adding a note
1 parent b2d4554 commit bca5404

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/external/rlsw.h

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@
114114
#endif
115115

116116
#ifndef SW_COLOR_BUFFER_BITS
117-
#define SW_COLOR_BUFFER_BITS 32 //< 32 (rgba), 16 (rgb packed) or 8 (rgb packed)
117+
#define SW_COLOR_BUFFER_BITS 32 //< 32 (rgba), 16 (rgb packed) or 8 (rgb packed)
118118
#endif
119119

120120
#ifndef SW_DEPTH_BUFFER_BITS
121-
#define SW_DEPTH_BUFFER_BITS 16 //< 24, 16 or 8
121+
#define SW_DEPTH_BUFFER_BITS 16 //< 32, 24 or 16
122122
#endif
123123

124124
#ifndef SW_MAX_PROJECTION_STACK_SIZE
@@ -748,32 +748,32 @@ SWAPI void swBindTexture(uint32_t id);
748748
#define SW_COLOR_PACK_COMP 4
749749
#endif
750750

751-
#if (SW_DEPTH_BUFFER_BITS == 8)
752-
#define SW_DEPTH_TYPE uint8_t
753-
#define SW_DEPTH_IS_PACKED 1
754-
#define SW_DEPTH_PACK_COMP 1
755-
#define SW_DEPTH_MAX UINT8_MAX
756-
#define SW_DEPTH_SCALE (1.0f/UINT8_MAX)
757-
#define SW_PACK_DEPTH(d) ((SW_DEPTH_TYPE)((d)*SW_DEPTH_MAX))
758-
#define SW_UNPACK_DEPTH(p) (p)
759-
#elif (SW_DEPTH_BUFFER_BITS == 16)
760-
#define SW_DEPTH_TYPE uint16_t
761-
#define SW_DEPTH_IS_PACKED 1
762-
#define SW_DEPTH_PACK_COMP 1
763-
#define SW_DEPTH_MAX UINT16_MAX
764-
#define SW_DEPTH_SCALE (1.0f/UINT16_MAX)
765-
#define SW_PACK_DEPTH(d) ((SW_DEPTH_TYPE)((d)*SW_DEPTH_MAX))
766-
#define SW_UNPACK_DEPTH(p) (p)
767-
#else // 24 bits
768-
#define SW_DEPTH_TYPE uint8_t
769-
#define SW_DEPTH_IS_PACKED 0
770-
#define SW_DEPTH_PACK_COMP 3
771-
#define SW_DEPTH_MAX 0xFFFFFF
772-
#define SW_DEPTH_SCALE (1.0f/0xFFFFFF)
773-
#define SW_PACK_DEPTH_0(d) (((uint32_t)((d)*SW_DEPTH_MAX)>>16)&0xFF)
774-
#define SW_PACK_DEPTH_1(d) (((uint32_t)((d)*SW_DEPTH_MAX)>>8)&0xFF)
775-
#define SW_PACK_DEPTH_2(d) ((uint32_t)((d)*SW_DEPTH_MAX)&0xFF)
776-
#define SW_UNPACK_DEPTH(p) (((p)[0]<<16)|((p)[1]<<8)|(p)[2])
751+
#if (SW_DEPTH_BUFFER_BITS == 16)
752+
#define SW_DEPTH_TYPE uint16_t
753+
#define SW_DEPTH_IS_PACKED 1
754+
#define SW_DEPTH_PACK_COMP 1
755+
#define SW_DEPTH_MAX UINT16_MAX
756+
#define SW_DEPTH_SCALE (1.0f/UINT16_MAX)
757+
#define SW_PACK_DEPTH(d) ((SW_DEPTH_TYPE)((d)*SW_DEPTH_MAX))
758+
#define SW_UNPACK_DEPTH(p) (p)
759+
#elif (SW_DEPTH_BUFFER_BITS == 24)
760+
#define SW_DEPTH_TYPE uint8_t
761+
#define SW_DEPTH_IS_PACKED 0
762+
#define SW_DEPTH_PACK_COMP 3
763+
#define SW_DEPTH_MAX 0xFFFFFFU
764+
#define SW_DEPTH_SCALE (1.0f/0xFFFFFFU)
765+
#define SW_PACK_DEPTH_0(d) ((uint8_t)(((uint32_t)((d)*SW_DEPTH_MAX)>>16)&0xFFU))
766+
#define SW_PACK_DEPTH_1(d) ((uint8_t)(((uint32_t)((d)*SW_DEPTH_MAX)>>8)&0xFFU))
767+
#define SW_PACK_DEPTH_2(d) ((uint8_t)((uint32_t)((d)*SW_DEPTH_MAX)&0xFFU))
768+
#define SW_UNPACK_DEPTH(p) ((((uint32_t)(p)[0]<<16)|((uint32_t)(p)[1]<<8)|(uint32_t)(p)[2]))
769+
#else // 32 bits
770+
#define SW_DEPTH_TYPE float
771+
#define SW_DEPTH_IS_PACKED 1
772+
#define SW_DEPTH_PACK_COMP 1
773+
#define SW_DEPTH_MAX 1.0f
774+
#define SW_DEPTH_SCALE 1.0f
775+
#define SW_PACK_DEPTH(d) ((SW_DEPTH_TYPE)(d))
776+
#define SW_UNPACK_DEPTH(p) (p)
777777
#endif
778778

779779
#define SW_STATE_CHECK(flags) (SW_STATE_CHECK_EX(RLSW.stateFlags, (flags)))
@@ -1346,6 +1346,8 @@ static inline void sw_framebuffer_write_color(sw_pixel_t *dst, const float src[4
13461346

13471347
static inline void sw_framebuffer_write_depth(sw_pixel_t *dst, float depth)
13481348
{
1349+
depth = sw_saturate(depth); // REVIEW: An overflow can occur in certain circumstances with clipping, and needs to be reviewed...
1350+
13491351
#if SW_DEPTH_IS_PACKED
13501352
dst->depth[0] = SW_PACK_DEPTH(depth);
13511353
#else

0 commit comments

Comments
 (0)