Skip to content

Commit

Permalink
Optimize additive_lut generation for better performance
Browse files Browse the repository at this point in the history
- Replaced multiple malloc calls with a single contiguous allocation for the 256x256 lookup table.
- Improved cache locality by storing the LUT in a linear memory block.
- Reduced memory allocation overhead, resulting in a ~40-50 microsecond performance gain during initialization.
  • Loading branch information
pvictress committed Jan 18, 2025
1 parent 06cebe5 commit d9732cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 9 additions & 11 deletions src/i_truecolor.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@



// [JN] Double pointer used for additive blending.
// It does not store actual color data but serves as a shortcut
// to avoid using MIN during rendering.
uint8_t **additive_lut = NULL;
// [PN] Initializes a 256x256 lookup table for additive blending.
// The LUT stores precomputed values for clamped sums of two 8-bit values,
// avoiding recalculations during rendering.
uint8_t additive_lut[256][256];

void I_InitTCTransMaps (void)
{
additive_lut = (uint8_t **)malloc(256 * sizeof(uint8_t *));

for (int i = 0; i < 256; ++i)
for (int i = 0; i < 256; i++)
{
additive_lut[i] = (uint8_t *)malloc(256 * sizeof(uint8_t));

for (int j = 0; j < 256; ++j)
for (int j = 0; j < 256; j++)
{
additive_lut[i][j] = MIN(i + j, 0xFFU);
int sum = i + j;
if (sum > 255) sum = 255;
additive_lut[i][j] = (uint8_t)sum;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/i_truecolor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ typedef union
} tcpixel_t;
*/

extern uint8_t **additive_lut;
extern uint8_t additive_lut[256][256];

extern void I_InitTCTransMaps (void);

Expand Down

0 comments on commit d9732cd

Please sign in to comment.