Skip to content

Commit

Permalink
Merge pull request #7 from Lehm2000/ja-dds-support
Browse files Browse the repository at this point in the history
Add DDS Compressed File Support
  • Loading branch information
Contingencyy authored Feb 15, 2024
2 parents 7d2de08 + 7c5fe29 commit ed14950
Show file tree
Hide file tree
Showing 12 changed files with 1,319 additions and 33 deletions.
101 changes: 69 additions & 32 deletions RT/RTmaterials.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,43 +160,79 @@ static int RT_LoadMaterialTexturesFromPaths(uint16_t bm_index, RT_Material *mate
{
if (load_mask & RT_BITN(i))
{

// first try loading the compressed dds version of the file if it exists
bool ddsLoaded = false;
RT_ArenaMemoryScope(&g_thread_arena)
{
char *file = RT_ArenaPrintF(&g_thread_arena, "assets/textures/%s", paths->textures[i]);

RT_TextureFormat format = g_rt_material_texture_slot_formats[i];
char* ddsFile = RT_ArenaPrintF(&g_thread_arena, "assets/textures/%s.dds", paths->textures[i]);

uint8_t* ddsData = NULL; // this is pointer to whole file including header
const struct DDS_HEADER* header = NULL; // pointer to the header (in same buffer as ddsData)
const uint8_t* bitData = NULL; // pointer to the compressed pixel data (in same buffer as ddsData)
size_t bitSize = 0; // size of the compressed pixel data

bool fileLoaded = RT_LoadDDSImageFromDisk(&g_thread_arena, ddsFile, &ddsData, &header, &bitData, &bitSize);

if (fileLoaded && ddsData)
{
ddsLoaded = true;

material->textures[i] = RT_UploadTextureDDS(&(RT_UploadTextureParamsDDS) {
.header = header,
.ddsData = ddsData,
.bitData = bitData,
.bitSize = bitSize,
.sRGB = i == 0 || i == 4, // set sRGB to true if basecolor or emissive
.name = RT_ArenaPrintF(&g_thread_arena, "Game Texture %hu:%s (source: %s)",
bm_index, g_rt_texture_slot_names[i], ddsFile)
});

int bpp = g_rt_texture_format_bpp[format];
textures_reloaded += 1;
}

int w = 0, h = 0, c = 0;
unsigned char *pixels = RT_LoadImageFromDisk(&g_thread_arena, file, &w, &h, &c, bpp);
}

if (i == RT_MaterialTextureSlot_Emissive)
if (!ddsLoaded) // dds file not loaded try png
{
RT_ArenaMemoryScope(&g_thread_arena)
{
// Premultiply emissive by alpha to avoid white transparent backgrounds from showing...
char* file = RT_ArenaPrintF(&g_thread_arena, "assets/textures/%s.png", paths->textures[i]);

RT_TextureFormat format = g_rt_material_texture_slot_formats[i];

int bpp = g_rt_texture_format_bpp[format];

uint32_t *at = (uint32_t *)pixels;
for (int i = 0; i < w*h; i++)
int w = 0, h = 0, c = 0;
unsigned char* pixels = RT_LoadImageFromDisk(&g_thread_arena, file, &w, &h, &c, bpp);

if (i == RT_MaterialTextureSlot_Emissive)
{
RT_Vec4 color = RT_UnpackRGBA(*at);
color.xyz = RT_Vec3Muls(color.xyz, color.w);
*at++ = RT_PackRGBA(color);
// Premultiply emissive by alpha to avoid white transparent backgrounds from showing...

uint32_t* at = (uint32_t*)pixels;
for (size_t i = 0; i < w * h; i++)
{
RT_Vec4 color = RT_UnpackRGBA(*at);
color.xyz = RT_Vec3Muls(color.xyz, color.w);
*at++ = RT_PackRGBA(color);
}
}
}

if (pixels)
{
material->textures[i] = RT_UploadTexture(&(RT_UploadTextureParams){
.format = format,
.width = w,
.height = h,
.pixels = pixels,
.pitch = bpp*w,
.name = RT_ArenaPrintF(&g_thread_arena, "Game Texture %hu:%s (source: %s)",
bm_index, g_rt_texture_slot_names[i], file),
});

textures_reloaded += 1;
if (pixels)
{
material->textures[i] = RT_UploadTexture(&(RT_UploadTextureParams) {
.format = format,
.width = w,
.height = h,
.pixels = pixels,
.pitch = bpp * w,
.name = RT_ArenaPrintF(&g_thread_arena, "Game Texture %hu:%s (source: %s)",
bm_index, g_rt_texture_slot_names[i], file),
});

textures_reloaded += 1;
}
}
}
}
Expand Down Expand Up @@ -261,11 +297,11 @@ void RT_InitAllBitmaps(void)
material->roughness = 0.5f;
}

snprintf(paths->albedo_texture, sizeof(paths->albedo_texture), "%s_basecolor.png", bitmap_name);
snprintf(paths->normal_texture, sizeof(paths->normal_texture), "%s_normal.png", bitmap_name);
snprintf(paths->metalness_texture, sizeof(paths->metalness_texture), "%s_metallic.png", bitmap_name);
snprintf(paths->roughness_texture, sizeof(paths->roughness_texture), "%s_roughness.png", bitmap_name);
snprintf(paths->emissive_texture, sizeof(paths->emissive_texture), "%s_emissive.png", bitmap_name);
snprintf(paths->albedo_texture, sizeof(paths->albedo_texture), "%s_basecolor", bitmap_name);
snprintf(paths->normal_texture, sizeof(paths->normal_texture), "%s_normal", bitmap_name);
snprintf(paths->metalness_texture, sizeof(paths->metalness_texture), "%s_metallic", bitmap_name);
snprintf(paths->roughness_texture, sizeof(paths->roughness_texture), "%s_roughness", bitmap_name);
snprintf(paths->emissive_texture, sizeof(paths->emissive_texture), "%s_emissive", bitmap_name);

// ------------------------------------------------------------------

Expand All @@ -274,6 +310,7 @@ void RT_InitAllBitmaps(void)

bool default_metalness, default_roughness;
RT_ParseMaterialDefinitionFile(bm_index, material, paths, &default_metalness, &default_roughness);

RT_LoadMaterialTexturesFromPaths(bm_index, material, paths, ~0u);

if (default_metalness && RT_RESOURCE_HANDLE_VALID(material->metalness_texture))
Expand Down
2 changes: 2 additions & 0 deletions RT/Renderer/Backend/Common/include/ImageReadWrite.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "ApiTypes.h"
#include "Renderer/Backend/DX12/DirectXTK12/src/DDSc.h"

#pragma pack(push, 8)

Expand All @@ -9,5 +10,6 @@ typedef struct RT_Arena RT_Arena;
RT_API unsigned char *RT_LoadImageFromDisk(RT_Arena *arena, const char *path, int *w, int *h, int *channel_count, int required_channel_count);
RT_API unsigned char *RT_LoadImageFromMemory(RT_Arena *arena, const void *memory, size_t memory_size, int *w, int *h, int *channel_count, int required_channel_count);
RT_API void RT_WritePNGToDisk(const char *path, int w, int h, int channel_count, const void *pixels, int stride_in_bytes);
RT_API bool RT_LoadDDSImageFromDisk(RT_Arena* arena, const char* path, const uint8_t** ddsData, const struct DDS_HEADER** header,const uint8_t** bitData, size_t* bitSize);

#pragma pack(pop)
11 changes: 11 additions & 0 deletions RT/Renderer/Backend/Common/include/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ typedef struct RT_UploadTextureParams
const char *name; // Optional, used for enhanced graphics debugging
} RT_UploadTextureParams;

typedef struct RT_UploadTextureParamsDDS
{
struct DDS_HEADER* header;
uint8_t* ddsData;
const uint8_t* bitData;
size_t bitSize;
bool sRGB; // force the format to a srgb format
const char* name; // Optional, used for enhanced graphics debugging
} RT_UploadTextureParamsDDS;

typedef struct RT_Triangle
{
// NOTE(daniel): I go through the effort of making these unions mostly to indicate
Expand Down Expand Up @@ -312,6 +322,7 @@ RT_API uint16_t *RT_GetMaterialIndicesArray(void);
RT_API void RT_DoRendererDebugMenus(const RT_DoRendererDebugMenuParams *params);

RT_API RT_ResourceHandle RT_UploadTexture(const RT_UploadTextureParams* params);
RT_API RT_ResourceHandle RT_UploadTextureDDS(const RT_UploadTextureParamsDDS* params);
// Updates the material on the GPU at the given index, so long as it is less than RT_MAX_MATERIALS.
// Returns the material_index you passed in, or UINT16_MAX if it was out of bounds.
RT_API uint16_t RT_UpdateMaterial(uint16_t material_index, const RT_Material *material);
Expand Down
3 changes: 3 additions & 0 deletions RT/Renderer/Backend/DX12/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set (Renderer_HEADER
"CGLTF/cgltf.h"
"src/DescriptorArena.hpp"
"src/mikktspace.h"
"DirectXTK12/src/DDSc.h"
"src/FSR2.h")

#I hate cmake for not allowing me to link interface libs. Ah well, now we do this indirection!
Expand Down Expand Up @@ -104,11 +105,13 @@ target_include_directories(Renderer PUBLIC

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/cimgui
${CMAKE_CURRENT_SOURCE_DIR}/DirectXTK12/src
${CMAKE_CURRENT_SOURCE_DIR}/FSR2/include
)
target_include_directories(
Renderer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/cimgui/imgui
Renderer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/implot
Renderer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/DirectXTK12/src
)

target_link_libraries(Renderer
Expand Down
Loading

0 comments on commit ed14950

Please sign in to comment.