Skip to content

Commit

Permalink
Make gltf textures srgb back
Browse files Browse the repository at this point in the history
  • Loading branch information
krupitskas committed Oct 17, 2024
1 parent 07fc847 commit 5f68336
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 50 deletions.
75 changes: 31 additions & 44 deletions Yasno/System/GltfLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "GltfLoader.hpp"

#include <memory>
#include <unordered_set>

#include <d3d12.h>
#include <wrl.h>
Expand Down Expand Up @@ -178,16 +179,44 @@ static inline uint32_t ComputeNumMips(uint32_t Width, uint32_t Height)
return HighBit + 1;
}

void FindAllSrgbTextures(std::unordered_set<int>& srgb_textures, const tinygltf::Model& gltf_model)
{
for (const tinygltf::Material& gltf_material : gltf_model.materials)
{
const tinygltf::PbrMetallicRoughness& pbr_material = gltf_material.pbrMetallicRoughness;

const tinygltf::TextureInfo& base_color = pbr_material.baseColorTexture;

if (base_color.index >= 0)
{
const tinygltf::Texture& texture = gltf_model.textures[base_color.index];

if (srgb_textures.contains(texture.source))
{
LogError << "Base color texture sRGB search collision\n";
}

srgb_textures.emplace(texture.source);
}
}
}

static bool BuildImages(ysn::Model& model, LoadGltfContext& build_context, const tinygltf::Model& gltf_model)
{
auto dx_renderer = ysn::Application::Get().GetRenderer();

HRESULT hr = S_OK;

for (const tinygltf::Image& image : gltf_model.images)
// Mark all albedo and emissive images as srgb
std::unordered_set<int> srgb_textures;
FindAllSrgbTextures(srgb_textures, gltf_model);

for (int i = 0; i < gltf_model.images.size(); i++)
{
const tinygltf::Image& image = gltf_model.images[i];

const uint32_t num_mips = ComputeNumMips(image.width, image.height);
bool is_srgb = false; // TODO: add it back
bool is_srgb = srgb_textures.contains(i);

wil::com_ptr<ID3D12Resource> dst_texture;
{
Expand Down Expand Up @@ -307,46 +336,6 @@ static bool BuildImages(ysn::Model& model, LoadGltfContext& build_context, const
return true;
}

//void FindAllSrgbTextures(ysn::ModelRenderContext* model_renderer_context, tinygltf::Model* gltf_model)
//{
// for (const tinygltf::Material& gltf_material : gltf_model->materials)
// {
// const tinygltf::PbrMetallicRoughness& pbr_material = gltf_material.pbrMetallicRoughness;
//
// {
// const tinygltf::TextureInfo& base_color = pbr_material.baseColorTexture;
//
// if (base_color.index >= 0)
// {
// const tinygltf::Texture& texture = gltf_model->textures[base_color.index];
//
// if (model_renderer_context->srgb_textures.contains(texture.source))
// {
// LogError << "Base color texture sRGB search collision\n";
// }
//
// model_renderer_context->srgb_textures.emplace(texture.source);
// }
// }
//
// {
// const tinygltf::TextureInfo& emissive_texture = gltf_material.emissiveTexture;
//
// if (emissive_texture.index >= 0)
// {
// const tinygltf::Texture& texture = gltf_model->textures[emissive_texture.index];
//
// if (model_renderer_context->srgb_textures.contains(texture.source))
// {
// LogError << "Emissive texture sRGB search collision\n";
// }
//
// model_renderer_context->srgb_textures.emplace(texture.source);
// }
// }
// }
//}

static void BuildSamplerDescs(ysn::Model& model, const tinygltf::Model& gltf_model)
{
model.sampler_descs.reserve(gltf_model.samplers.size());
Expand Down Expand Up @@ -594,7 +583,6 @@ static void BuildMeshes(ysn::Model& model, const tinygltf::Model& gltf_model)
}
}


static bool BuildNodes(ysn::Model& model, const tinygltf::Model& gltf_model, const ysn::LoadingParameters& loading_parameters)
{
auto dx_renderer = ysn::Application::Get().GetRenderer();
Expand Down Expand Up @@ -974,7 +962,6 @@ namespace ysn
LogError << "GLTF loader can't build materials\n";
return false;
}
//FindAllSrgbTextures(ModelRenderContext, pModel);
if(!BuildMaterials(model, load_gltf_context, gltf_model))
{
LogError << "GLTF loader can't build materials\n";
Expand Down
12 changes: 6 additions & 6 deletions Yasno/Yasno/Yasno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,15 @@ namespace ysn
// TODO(last): Another command list?
bool load_result = false;
//LoadGLTFModel(&m_gltf_draw_context, GetVirtualFilesystemPath(L"Assets/DamagedHelmet/DamagedHelmet.gltf"), Application::Get().GetRenderer(), command_list);
//{
// LoadingParameters loading_parameters;
// loading_parameters.model_modifier = XMMatrixScaling(0.01f, 0.01f, 0.01f);
// load_result = LoadGltfFromFile(m_render_scene, GetVirtualFilesystemPath(L"Assets/Sponza/Sponza.gltf"), loading_parameters);
//}
{
LoadingParameters loading_parameters;
load_result = LoadGltfFromFile(m_render_scene, GetVirtualFilesystemPath(L"Assets/DamagedHelmet/DamagedHelmet.gltf"), loading_parameters);
loading_parameters.model_modifier = XMMatrixScaling(0.01f, 0.01f, 0.01f);
load_result = LoadGltfFromFile(m_render_scene, GetVirtualFilesystemPath(L"Assets/Sponza/Sponza.gltf"), loading_parameters);
}
//{
// LoadingParameters loading_parameters;
// load_result = LoadGltfFromFile(m_render_scene, GetVirtualFilesystemPath(L"Assets/DamagedHelmet/DamagedHelmet.gltf"), loading_parameters);
//}
//LoadGLTFModel(&m_gltf_draw_context, GetVirtualFilesystemPath(L"Assets/BoomBoxWithAxes/glTF/BoomBoxWithAxes.gltf"), Application::Get().GetRenderer(), command_list);

//{
Expand Down

0 comments on commit 5f68336

Please sign in to comment.