Fixed bug in Model Loading.

Model Loader was loading indexes into image instead of going via texture.
TODO: Textures also have samplers.
This commit is contained in:
Anish Bhobe 2025-04-10 23:50:21 +02:00
parent b8b620a723
commit 668189acb5
75 changed files with 8816 additions and 22 deletions

View File

@ -367,14 +367,21 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, const Ref<Texture> &texture, vk
#endif
}
std::tuple<systems::ResId<TextureView>, Ref<Buffer>>
AssetLoader::LoadImageToGpu(tinygltf::Image *image, bool isSrgb) const
std::tuple<systems::ResId<TextureView>, Ref<StagingBuffer>>
AssetLoader::LoadImageToGpu(tinygltf::Image *image, bool isSrgb, cstr name) const
{
// TODO(Something not loading properly).
assert(image->component == 4);
assert(image->height > 0 && image->width > 0);
#if !defined(ASTER_NDEBUG)
auto assignedName = name ? name : image->name.empty() ? image->uri.c_str() : image->name.c_str();
#else
auto assignedName = nullptr;
#endif
u32 height = Cast<u32>(image->height);
u32 width = Cast<u32>(image->width);
@ -384,7 +391,7 @@ AssetLoader::LoadImageToGpu(tinygltf::Image *image, bool isSrgb) const
auto texture = m_ResourceManager->Images().CreateTexture2D<Texture>({
.m_Format = imageFormat,
.m_Extent = {width, height},
.m_Name = image->name.c_str(),
.m_Name = assignedName,
.m_IsSampled = true,
.m_IsMipMapped = true,
.m_IsStorage = false,
@ -395,7 +402,7 @@ AssetLoader::LoadImageToGpu(tinygltf::Image *image, bool isSrgb) const
#if !defined(ASTER_NDEBUG)
StackString<128> loadActionName = "Load: ";
loadActionName += image->name.empty() ? "<texture>" : image->name.c_str();
loadActionName += assignedName;
vk::DebugUtilsLabelEXT debugLabel = {
.pLabelName = loadActionName.c_str(),
.color = std::array{1.0f, 1.0f, 1.0f, 1.0f},
@ -541,7 +548,7 @@ AssetLoader::LoadModelToGpu(cstr path, cstr name)
m_CommandBuffer.beginDebugUtilsLabelEXT(&debugLabel);
#endif
eastl::vector<Ref<Buffer>> stagingBuffers;
eastl::vector<Ref<StagingBuffer>> stagingBuffers;
eastl::hash_map<i32, systems::ResId<TextureView>> textureHandleMap;
@ -562,8 +569,9 @@ AssetLoader::LoadModelToGpu(cstr path, cstr name)
return iter->second;
}
auto *image = &model.images[index];
auto [handle, staging] = LoadImageToGpu(image, isSrgb);
const auto &texture = model.textures[index];
auto *image = &model.images[texture.source];
auto [handle, staging] = LoadImageToGpu(image, isSrgb, texture.name.empty() ? nullptr : texture.name.c_str());
textureHandleMap.emplace(index, handle);
stagingBuffers.emplace_back(std::move(staging));
return handle;

View File

@ -112,7 +112,6 @@ struct AssetLoader
u32 m_GraphicsQueueIndex;
Ref<TextureView> LoadHdrImage(cstr path, cstr name = nullptr) const;
std::tuple<systems::ResId<TextureView>, Ref<Buffer>> LoadImageToGpu(tinygltf::Image *image, bool isSrgb) const;
Model LoadModelToGpu(cstr path, cstr name = nullptr);
constexpr static auto ANormal = "NORMAL";
@ -132,6 +131,10 @@ struct AssetLoader
AssetLoader &operator=(AssetLoader &&other) noexcept;
DISALLOW_COPY_AND_ASSIGN(AssetLoader);
private:
std::tuple<systems::ResId<TextureView>, Ref<StagingBuffer>>
LoadImageToGpu(tinygltf::Image *image, bool isSrgb, cstr name = nullptr) const;
};
void

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/1219024358953944284.jpg (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/2051777328469649772.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/2185409758123873465.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/2299742237651021498.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/2374361008830720677.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/2411100444841994089.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/2775690330959970771.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/2969916736137545357.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/332936164838540657.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/3371964815757888145.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/3455394979645218238.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/3628158980083700836.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/3827035219084910048.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/4477655471536070370.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/4601176305987539675.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/466164707995436622.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/4675343432951571524.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/4871783166746854860.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/4910669866631290573.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/4975155472559461469.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/5061699253647017043.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/5792855332885324923.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/5823059166183034438.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/6047387724914829168.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/6151467286084645207.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/6593109234861095314.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/6667038893015345571.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/6772804448157695701.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/7056944414013900257.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/715093869573992647.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/7268504077753552595.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/7441062115984513793.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/755318871556304029.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/759203620573749278.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/7645212358685992005.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/7815564343179553343.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8006627369776289000.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8051790464816141987.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8114461559286000061.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8481240838833932244.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8503262930880235456.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8747919177698443163.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8750083169368950601.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8773302468495022225.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/8783994986360286082.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/9288698199695299068.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/9916269861720640319.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
samples/03_model_render/model/Sponza/Sponza.bin (Stored with Git LFS) Normal file

Binary file not shown.

File diff suppressed because it is too large Load Diff

BIN
samples/03_model_render/model/Sponza/white.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -33,7 +33,7 @@
constexpr u32 MAX_FRAMES_IN_FLIGHT = 3;
constexpr auto PIPELINE_CACHE_FILE = "PipelineCacheData.bin";
constexpr auto MODEL_FILE = "model/DamagedHelmet.glb";
constexpr auto MODEL_FILE = "model/Sponza/Sponza.gltf";
constexpr auto BACKDROP_FILE = "image/photo_studio_loft_hall_4k.hdr";
constexpr u32 INIT_WIDTH = 1280;
constexpr u32 INIT_HEIGHT = 720;

View File

@ -15,38 +15,38 @@ struct FS_Output
float4 GetAlbedo(float2 UV, float4 InColor)
{
float4 AlbedoFactor = (float4) MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].AlbedoFactor;
uint AlbedoTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].AlbedoTex;
float4 AlbedoFactor = (float4) MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].AlbedoFactor;
uint AlbedoTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].AlbedoTex;
return AlbedoFactor * InColor * (AlbedoTexId != INVALID_HANDLE ? Textures[AlbedoTexId].Sample(ImmutableSamplers[AlbedoTexId], UV) : 1.0f.xxxx);
return AlbedoFactor * InColor * (AlbedoTexId != INVALID_HANDLE ? Textures[NonUniformResourceIndex(AlbedoTexId)].Sample(ImmutableSamplers[NonUniformResourceIndex(AlbedoTexId)], UV) : 1.0f.xxxx);
}
float GetOcclusion(float2 UV)
{
uint OcclusionTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].OcclusionTex;
uint OcclusionTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].OcclusionTex;
return OcclusionTexId != INVALID_HANDLE ? Textures[OcclusionTexId].Sample(ImmutableSamplers[OcclusionTexId], UV).r : 1.0f;
return OcclusionTexId != INVALID_HANDLE ? Textures[NonUniformResourceIndex(OcclusionTexId)].Sample(ImmutableSamplers[NonUniformResourceIndex(OcclusionTexId)], UV).r : 1.0f;
}
float3 GetEmissive(float2 UV)
{
float3 EmissionFactor = (float3) MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].EmissionFactor;
uint EmissionTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].EmissionTex;
float3 EmissionFactor = (float3) MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].EmissionFactor;
uint EmissionTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].EmissionTex;
return EmissionFactor * (EmissionTexId != INVALID_HANDLE ? Textures[EmissionTexId].Sample(ImmutableSamplers[EmissionTexId], UV).rgb : 1.0f.xxx);
return EmissionFactor * (EmissionTexId != INVALID_HANDLE ? Textures[NonUniformResourceIndex(EmissionTexId)].Sample(ImmutableSamplers[NonUniformResourceIndex(EmissionTexId)], UV).rgb : 1.0f.xxx);
}
float3 GetNormal(float3 Position, float3 Normal, float2 UV)
{
float3 N = normalize(Normal);
uint NormalTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].NormalTex;
uint NormalTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].NormalTex;
if (NormalTexId == INVALID_HANDLE)
{
return N;
}
float3 TangentSpaceNormal = Textures[NormalTexId].Sample(ImmutableSamplers[NormalTexId], UV).xyz * 2.0f - 1.0f;
float3 TangentSpaceNormal = Textures[NonUniformResourceIndex(NormalTexId)].Sample(ImmutableSamplers[NonUniformResourceIndex(NormalTexId)], UV).xyz * 2.0f - 1.0f;
float3 q1 = ddx(Position);
float3 q2 = ddy(Position);
@ -62,10 +62,10 @@ float3 GetNormal(float3 Position, float3 Normal, float2 UV)
float2 GetMetalRough(float2 UV)
{
float2 MetalRoughFactors = float2(MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].MetalFactor, MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].RoughFactor);
uint MetalRoughTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][PushConstant.MaterialIdx].MetalRoughTex;
float2 MetalRoughFactors = float2(MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].MetalFactor, MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].RoughFactor);
uint MetalRoughTexId = MaterialsBuffer[PushConstant.MaterialBufferHandle][NonUniformResourceIndex(PushConstant.MaterialIdx)].MetalRoughTex;
return MetalRoughFactors * (MetalRoughTexId != INVALID_HANDLE ? Textures[MetalRoughTexId].Sample(ImmutableSamplers[MetalRoughTexId], UV).bg : 1.0f.xx); // Metal is B, Rough is G.
return MetalRoughFactors * (MetalRoughTexId != INVALID_HANDLE ? Textures[NonUniformResourceIndex(MetalRoughTexId)].Sample(ImmutableSamplers[NonUniformResourceIndex(MetalRoughTexId)], UV).bg : 1.0f.xx); // Metal is B, Rough is G.
}
float3 SampleIrradiance(float3 Direction)