Skip to content

Commit

Permalink
render: handle UV adjustments privately
Browse files Browse the repository at this point in the history
This removes the g_LabTextureUVFlag array and shifts responsibility to
the renderer fully for resetting these values.
  • Loading branch information
lahm86 committed Jan 28, 2025
1 parent 6a6878e commit 8b57e7f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 25 deletions.
24 changes: 2 additions & 22 deletions src/tr2/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,27 +312,6 @@ static void M_LoadTextures(VFILE *const file)
const int32_t num_textures = VFile_ReadS32(file);
LOG_INFO("object textures: %d", num_textures);
Level_ReadObjectTextures(0, 0, num_textures, file);

// TODO: handle this post-injection/packing
for (int32_t i = 0; i < num_textures; i++) {
OBJECT_TEXTURE *const texture = Output_GetObjectTexture(i);
uint16_t *const uv = &texture->uv[0].u;
uint8_t byte = 0;
for (int32_t j = 0; j < 8; j++) {
if ((uv[j] & 0x80) != 0) {
uv[j] |= 0xFF;
byte |= 1 << j;
} else {
uv[j] &= 0xFF00;
}
}
g_LabTextureUVFlag[i] = byte;

for (int32_t j = 0; j < 4; j++) {
texture->uv_backup[j] = texture->uv[j];
}
}

Benchmark_End(benchmark, NULL);
}

Expand Down Expand Up @@ -757,7 +736,8 @@ static void M_CompleteSetup(void)
Item_Initialise(i);
}

Render_Reset(RENDER_RESET_PALETTE | RENDER_RESET_TEXTURES);
Render_Reset(
RENDER_RESET_PALETTE | RENDER_RESET_TEXTURES | RENDER_RESET_UVS);

Benchmark_End(benchmark, NULL);
}
Expand Down
3 changes: 3 additions & 0 deletions src/tr2/game/render/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ void Render_Reset(const RENDER_RESET_FLAGS reset_flags)

r->Reset(r, reset_flags);

if (reset_flags & (RENDER_RESET_PARAMS | RENDER_RESET_UVS)) {
Render_ResetTextureUVs();
}
if (reset_flags & (RENDER_RESET_PARAMS | RENDER_RESET_TEXTURES)) {
Render_AdjustTextureUVs(reset_flags & RENDER_RESET_TEXTURES);
M_ReuploadBackground();
Expand Down
1 change: 1 addition & 0 deletions src/tr2/game/render/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef enum {
RENDER_RESET_PALETTE = 1 << 1,
RENDER_RESET_TEXTURES = 1 << 2,
RENDER_RESET_PARAMS = 1 << 3,
RENDER_RESET_UVS = 1 << 4,
RENDER_RESET_ALL = INT32_MAX,
// clang-format on
} RENDER_RESET_FLAGS;
Expand Down
25 changes: 24 additions & 1 deletion src/tr2/game/render/priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <libtrx/utils.h>

bool g_DiscardTransparent = false;
static uint8_t m_LabTextureUVFlag[MAX_OBJECT_TEXTURES] = {};

static void M_QuickSort(int32_t left, int32_t right);
static inline void M_ClipG(
Expand Down Expand Up @@ -114,6 +115,28 @@ int32_t Render_GetUVAdjustment(void)
return g_Config.rendering.nearest_adjustment;
}

void Render_ResetTextureUVs(void)
{
for (int32_t i = 0; i < Output_GetObjectTextureCount(); i++) {
OBJECT_TEXTURE *const texture = Output_GetObjectTexture(i);
uint16_t *const uv = &texture->uv[0].u;
uint8_t byte = 0;
for (int32_t j = 0; j < 8; j++) {
if ((uv[j] & 0x80) != 0) {
uv[j] |= 0xFF;
byte |= 1 << j;
} else {
uv[j] &= 0xFF00;
}
}
m_LabTextureUVFlag[i] = byte;

for (int32_t j = 0; j < 4; j++) {
texture->uv_backup[j] = texture->uv[j];
}
}
}

void Render_AdjustTextureUVs(const bool reset_uv_add)
{
const int32_t num_textures = Output_GetObjectTextureCount();
Expand All @@ -126,7 +149,7 @@ void Render_AdjustTextureUVs(const bool reset_uv_add)
OBJECT_TEXTURE *const texture = Output_GetObjectTexture(i);
TEXTURE_UV *const uv = texture->uv;
const TEXTURE_UV *const uv_backup = texture->uv_backup;
int32_t uv_flags = g_LabTextureUVFlag[i];
int32_t uv_flags = m_LabTextureUVFlag[i];
for (int32_t j = 0; j < 4; j++) {
uv[j].u = uv_backup[j].u + ((uv_flags & 1) ? -offset : offset);
uv[j].v = uv_backup[j].v + ((uv_flags & 2) ? -offset : offset);
Expand Down
1 change: 1 addition & 0 deletions src/tr2/game/render/priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ double Render_CalculatePolyZ(

void Render_SortPolyList(void);
int32_t Render_GetUVAdjustment(void);
void Render_ResetTextureUVs(void);
void Render_AdjustTextureUVs(bool reset_uv_add);

int32_t Render_VisibleZClip(
Expand Down
1 change: 0 additions & 1 deletion src/tr2/global/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ int32_t g_FlipMaps[MAX_FLIP_MAPS];
bool g_CameraUnderwater;
int32_t g_BoxCount;
int32_t g_TexturePageCount;
uint8_t g_LabTextureUVFlag[MAX_OBJECT_TEXTURES];
int32_t g_NumCameras;
uint32_t *g_DemoData = NULL;
char g_LevelFileName[256];
Expand Down
1 change: 0 additions & 1 deletion src/tr2/global/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ extern int32_t g_FlipMaps[MAX_FLIP_MAPS];
extern bool g_CameraUnderwater;
extern int32_t g_BoxCount;
extern int32_t g_TexturePageCount;
extern uint8_t g_LabTextureUVFlag[MAX_OBJECT_TEXTURES];
extern int32_t g_NumCameras;
extern uint32_t *g_DemoData;
extern char g_LevelFileName[256];
Expand Down

0 comments on commit 8b57e7f

Please sign in to comment.