Skip to content

Commit

Permalink
tr1/inject: improve texture injection performance
Browse files Browse the repository at this point in the history
This removes texture packing from the injection process. We instead
inject the individual pages that come with the injections (which are
packed individually anyway). Packing will later be re-introduced for
unoptimized page groups only, but this is dependent on a format
refactor.
  • Loading branch information
lahm86 committed Jan 29, 2025
1 parent 69f73ed commit 7564a84
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 59 deletions.
1 change: 1 addition & 0 deletions docs/tr1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- fixed `/demo` command crashing the game if no demos are present (regression from 4.1)
- fixed Lara not being able to jump or stop swimming if the related responsive config options are enabled, but enhanced animations are not present (#2397, regression from 4.6)
- improved pause screen compatibility with PS1 (#2248)
- improved level loading times with respect to injection processing

## [4.7.1](https://github.com/LostArtefacts/TRX/compare/tr1-4.7...tr1-4.7.1) - 2024-12-21
- changed the inventory examine UI to auto-hide if the item description is empty (#2097)
Expand Down
55 changes: 13 additions & 42 deletions src/tr1/game/inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,8 @@ static void M_AlignTextureReferences(
const OBJECT *object, const uint16_t *palette_map, int32_t tex_info_base);

static void M_LoadTexturePages(
INJECTION *injection, LEVEL_INFO *level_info, uint16_t *palette_map,
RGBA_8888 *page_ptr);
static void M_TextureData(
INJECTION *injection, LEVEL_INFO *level_info, int32_t page_base);
const INJECTION *injection, LEVEL_INFO *level_info, uint16_t *palette_map);
static void M_TextureData(const INJECTION *injection, LEVEL_INFO *level_info);
static void M_MeshData(const INJECTION *injection, LEVEL_INFO *level_info);
static void M_AnimData(INJECTION *injection, LEVEL_INFO *level_info);
static void M_AnimRangeEdits(INJECTION *injection);
Expand Down Expand Up @@ -352,8 +350,8 @@ static void M_LoadFromFile(INJECTION *injection, const char *filename)
}

static void M_LoadTexturePages(
INJECTION *injection, LEVEL_INFO *level_info, uint16_t *palette_map,
RGBA_8888 *page_ptr)
const INJECTION *const injection, LEVEL_INFO *const level_info,
uint16_t *const palette_map)
{
BENCHMARK *const benchmark = Benchmark_Start();

Expand Down Expand Up @@ -382,7 +380,9 @@ static void M_LoadTexturePages(
uint8_t *indices = Memory_Alloc(pixel_count);
VFile_Read(fp, indices, pixel_count);
uint8_t *input = indices;
RGBA_8888 *output = page_ptr;
RGBA_8888 *output =
&level_info->textures
.pages_32[TEXTURE_PAGE_SIZE * level_info->textures.page_count];
for (size_t i = 0; i < pixel_count; i++) {
const uint8_t index = *input++;
if (index == 0) {
Expand All @@ -401,14 +401,15 @@ static void M_LoadTexturePages(
}

static void M_TextureData(
INJECTION *injection, LEVEL_INFO *level_info, int32_t page_base)
const INJECTION *const injection, LEVEL_INFO *const level_info)
{
BENCHMARK *const benchmark = Benchmark_Start();

INJECTION_INFO *inj_info = injection->info;
VFILE *const fp = injection->fp;

// Read the tex_infos and align them to the end of the page list.
const int32_t page_base = level_info->textures.page_count;
Level_ReadObjectTextures(
level_info->textures.object_count, page_base, inj_info->texture_count,
fp);
Expand Down Expand Up @@ -1625,23 +1626,16 @@ void Inject_AllInjections(LEVEL_INFO *level_info)
BENCHMARK *const benchmark = Benchmark_Start();

uint16_t palette_map[256];
RGBA_8888 *source_pages = Memory_Alloc(
m_Aggregate->texture_page_count * TEXTURE_PAGE_SIZE
* sizeof(RGBA_8888));
int32_t source_page_count = 0;
int32_t tpage_base = level_info->textures.page_count;

for (int32_t i = 0; i < m_NumInjections; i++) {
INJECTION *injection = &m_Injections[i];
if (!injection->relevant) {
continue;
}

M_LoadTexturePages(
injection, level_info, palette_map,
source_pages + (source_page_count * TEXTURE_PAGE_SIZE));
// TODO: use texture packing for unoptimized pages.
M_LoadTexturePages(injection, level_info, palette_map);

M_TextureData(injection, level_info, tpage_base);
M_TextureData(injection, level_info);
M_MeshData(injection, level_info);
M_AnimData(injection, level_info);
M_ObjectData(injection, level_info, palette_map);
Expand All @@ -1666,30 +1660,7 @@ void Inject_AllInjections(LEVEL_INFO *level_info)
level_info->anims.anim_count += inj_info->anim_count;
level_info->mesh_ptr_count += inj_info->mesh_ptr_count;
level_info->textures.object_count += inj_info->texture_count;
source_page_count += inj_info->texture_page_count;
tpage_base += inj_info->texture_page_count;
}

if (source_page_count != 0) {
PACKER_DATA data = {
.level.page_count = level_info->textures.page_count,
.level.pages_32 = level_info->textures.pages_32,
.level.pages_24 = NULL,
.level.palette_24 = NULL,
.source.page_count = source_page_count,
.source.pages_32 = source_pages,
.source.pages_24 = NULL,
.source.palette_24 = NULL,
.object_count = level_info->textures.object_count,
.sprite_count = level_info->textures.sprite_count,
};

if (Packer_Pack(&data)) {
level_info->textures.page_count += Packer_GetAddedPageCount();
level_info->textures.pages_32 = data.level.pages_32;
}

Memory_FreePointer(&source_pages);
level_info->textures.page_count += inj_info->texture_page_count;
}

Benchmark_End(benchmark, NULL);
Expand Down
35 changes: 18 additions & 17 deletions src/tr1/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ static void M_CompleteSetup(const GF_LEVEL *const level)

// Expand paletted texture data to RGB
m_LevelInfo.textures.pages_32 = Memory_Alloc(
m_LevelInfo.textures.page_count * TEXTURE_PAGE_SIZE
* sizeof(RGBA_8888));
(m_LevelInfo.textures.page_count + m_InjectionInfo->texture_page_count)
* TEXTURE_PAGE_SIZE * sizeof(RGBA_8888));
RGBA_8888 *output = m_LevelInfo.textures.pages_32;
const uint8_t *input = m_LevelInfo.textures.pages_24;
for (int32_t i = 0; i < m_LevelInfo.textures.page_count; i++) {
Expand Down Expand Up @@ -843,22 +843,23 @@ static void M_CompleteSetup(const GF_LEVEL *const level)
LOG_INFO("Maximum vertices: %d", max_vertices);
Output_ReserveVertexBuffer(max_vertices);

// Move the prepared texture pages into g_TexturePagePtrs.
RGBA_8888 *final_texture_data = GameBuf_Alloc(
m_LevelInfo.textures.page_count * TEXTURE_PAGE_SIZE * sizeof(RGBA_8888),
GBUF_TEXTURE_PAGES);
memcpy(
final_texture_data, m_LevelInfo.textures.pages_32,
m_LevelInfo.textures.page_count * TEXTURE_PAGE_SIZE
* sizeof(RGBA_8888));
for (int i = 0; i < m_LevelInfo.textures.page_count; i++) {
g_TexturePagePtrs[i] = &final_texture_data[i * TEXTURE_PAGE_SIZE];
{
// Move the prepared texture pages into g_TexturePagePtrs.
const int32_t num_pages = m_LevelInfo.textures.page_count;
const int32_t page_size = TEXTURE_PAGE_SIZE * sizeof(RGBA_8888);
RGBA_8888 *const pages =
GameBuf_Alloc(num_pages * page_size, GBUF_TEXTURE_PAGES);
memcpy(pages, m_LevelInfo.textures.pages_32, num_pages * page_size);
for (int32_t i = 0; i < num_pages; i++) {
g_TexturePagePtrs[i] = &pages[i * TEXTURE_PAGE_SIZE];
}
Output_DownloadTextures(m_LevelInfo.textures.page_count);
Output_SetPalette(
m_LevelInfo.palette.data_24, m_LevelInfo.palette.size);
Memory_FreePointer(&m_LevelInfo.textures.pages_24);
Memory_FreePointer(&m_LevelInfo.textures.pages_32);
Memory_FreePointer(&m_LevelInfo.palette.data_24);
}
Output_DownloadTextures(m_LevelInfo.textures.page_count);
Output_SetPalette(m_LevelInfo.palette.data_24, m_LevelInfo.palette.size);
Memory_FreePointer(&m_LevelInfo.textures.pages_24);
Memory_FreePointer(&m_LevelInfo.textures.pages_32);
Memory_FreePointer(&m_LevelInfo.palette.data_24);

// Initialise the sound effects.
const int32_t sample_count = m_LevelInfo.samples.offset_count;
Expand Down

0 comments on commit 7564a84

Please sign in to comment.