Skip to content

Commit

Permalink
[rendering] fixed an issue where after packing certain materials, sea…
Browse files Browse the repository at this point in the history
…ms/glitches would be visible on some surfaces
  • Loading branch information
PanosK92 committed Dec 28, 2024
1 parent 7731a0f commit af8fca1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 deletions.
4 changes: 2 additions & 2 deletions runtime/RHI/Vulkan/Vulkan_FidelityFX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ namespace Spartan
const uint32_t cascade_resolution = 64;
const bool sdf_center_around_camera = true;
const float sdf_ray_normal_offset = 0.5f; // distance from a surface along the normal vector to offset the ray origin - below 0.5 I see artifacts
const float sdf_ray_epsilon = 0.1f; // epsilon value for ray marching to be used with brixelizer for rays
const float sdf_ray_epsilon = 0.5f; // epsilon value for ray marching to be used with brixelizer for rays
const uint32_t bricks_per_update_max = 1 << 14; // maximum number of bricks to be updated
const uint32_t triangle_references_max = 32 * (1 << 20); // maximum number of triangle voxel references to be stored in the update
const uint32_t triangle_swap_size = 300 * (1 << 20); // size of the swap space available to be used for storing triangles in the update
Expand Down Expand Up @@ -553,7 +553,7 @@ namespace Spartan
Irradiance, // brixelizer gi
Max
};
DebugMode debug_mode = DebugMode::Max;
DebugMode debug_mode = DebugMode::Max; // overwrites light_diffuse_gi render target
bool debug_mode_arrow_switch = false;
bool debug_mode_aabbs_and_stats = false;
bool debug_mode_log_instances = false;
Expand Down
86 changes: 45 additions & 41 deletions runtime/Rendering/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace Spartan
"The dimensions must be equal"
);

// gltf stores occlusion, roughness and metalness in the same texture, as r, g, b channels respectively
// just like gltf: occlusion, roughness and metalness as r, g, b channels respectively
for (size_t i = 0; i < occlusion.size(); i += 4)
{
output[i + 0] = occlusion[i];
Expand Down Expand Up @@ -335,6 +335,18 @@ namespace Spartan
RHI_Texture* texture_metalness = GetTexture(MaterialTextureType::Metalness, slot);
RHI_Texture* texture_height = GetTexture(MaterialTextureType::Height, slot);

RHI_Texture* reference_texture = texture_color ? texture_color :
texture_alpha_mask ? texture_alpha_mask :
texture_occlusion ? texture_occlusion :
texture_roughness ? texture_roughness :
texture_metalness ? texture_metalness :
texture_height;

uint32_t reference_width = reference_texture ? reference_texture->GetWidth() : 1;
uint32_t reference_height = reference_texture ? reference_texture->GetHeight() : 1;
uint32_t reference_depth = reference_texture ? reference_texture->GetDepth() : 1;
uint32_t reference_mip_count = reference_texture ? reference_texture->GetMipCount() : 1;

// pack textures
{
// step 1: pack alpha mask into color alpha
Expand All @@ -356,43 +368,35 @@ namespace Spartan
// step 2: pack occlusion, roughness, metalness, and height into a single texture
{
// generate unique name by hashing texture IDs
size_t hash_value = 0;
if (texture_occlusion) hash_value ^= texture_occlusion->GetObjectId();
if (texture_roughness) hash_value ^= texture_roughness->GetObjectId();
if (texture_metalness) hash_value ^= texture_metalness->GetObjectId();
if (texture_height) hash_value ^= texture_height->GetObjectId();

// fetch the packed texture that corresponds to the hash (in case it was already packed)
const string tex_name = "texture_packed_" + to_string(hash_value);
shared_ptr<RHI_Texture> texture_packed = ResourceCache::GetByName<RHI_Texture>(tex_name);
string tex_name;
{
uint64_t hash = 0;
hash = rhi_hash_combine(hash, static_cast<uint64_t>(texture_occlusion ? texture_occlusion->GetObjectId() : 0));
hash = rhi_hash_combine(hash, static_cast<uint64_t>(texture_roughness ? texture_roughness->GetObjectId() : 0));
hash = rhi_hash_combine(hash, static_cast<uint64_t>(texture_metalness ? texture_metalness->GetObjectId() : 0));
hash = rhi_hash_combine(hash, static_cast<uint64_t>(texture_height ? texture_height->GetObjectId() : 0));
hash = rhi_hash_combine(hash, static_cast<uint64_t>(reference_width));
hash = rhi_hash_combine(hash, static_cast<uint64_t>(reference_height));

tex_name = "texture_packed_" + to_string(hash);
}

shared_ptr<RHI_Texture> texture_packed = ResourceCache::GetByName<RHI_Texture>(tex_name);
if (!texture_packed)
{
bool textures_are_compressed = (texture_occlusion && texture_occlusion->IsCompressedFormat()) ||
(texture_roughness && texture_roughness->IsCompressedFormat()) ||
(texture_metalness && texture_metalness->IsCompressedFormat()) ||
(texture_height && texture_height->IsCompressedFormat());

RHI_Texture* texture_reference = texture_color ? texture_color :
texture_alpha_mask ? texture_alpha_mask :
texture_occlusion ? texture_occlusion :
texture_roughness ? texture_roughness :
texture_metalness ? texture_metalness :
texture_height;

uint32_t width = texture_reference ? texture_reference->GetWidth() : 1;
uint32_t height = texture_reference ? texture_reference->GetHeight() : 1;
uint32_t depth = texture_reference ? texture_reference->GetDepth() : 1;
uint32_t mip_count = texture_reference ? texture_reference->GetMipCount() : 1;


// create packed texture
texture_packed = make_shared<RHI_Texture>
(
RHI_Texture_Type::Type2D,
width,
height,
depth,
mip_count,
reference_width,
reference_height,
reference_depth,
reference_mip_count,
RHI_Format::R8G8B8A8_Unorm,
RHI_Texture_Srv | RHI_Texture_Compress | RHI_Texture_DontPrepareForGpu,
tex_name.c_str()
Expand All @@ -401,7 +405,7 @@ namespace Spartan
texture_packed->AllocateMip();

// create some default data to replace missing textures
const size_t texture_size = width * height * 4;
const size_t texture_size = reference_width * reference_height * 4;
vector<byte> texture_one(texture_size, static_cast<byte>(255));
vector<byte> texture_zero(texture_size, static_cast<byte>(0));
vector<byte> texture_half(texture_size, static_cast<byte>(127));
Expand Down Expand Up @@ -452,19 +456,19 @@ namespace Spartan
}
}

// determine if the material is optimized
bool is_optimized = GetTexture(MaterialTextureType::Packed) != nullptr;
for (RHI_Texture* texture : m_textures)
{
if (texture && texture->IsCompressedFormat())
{
is_optimized = true;
break;
}
}
SetProperty(MaterialProperty::Optimized, is_optimized ? 1.0f : 0.0f);

m_resource_state = ResourceState::PreparedForGpu;
// determine if the material is optimized
bool is_optimized = GetTexture(MaterialTextureType::Packed) != nullptr;
for (RHI_Texture* texture : m_textures)
{
if (texture && texture->IsCompressedFormat())
{
is_optimized = true;
break;
}
}
SetProperty(MaterialProperty::Optimized, is_optimized ? 1.0f : 0.0f);
m_resource_state = ResourceState::PreparedForGpu;
});
}

Expand Down

0 comments on commit af8fca1

Please sign in to comment.