Add proper support for 8 bit indices
This commit is contained in:
@@ -18,7 +18,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "application.h"
|
||||
#include "utils/alignment.h"
|
||||
#include "utils/contains.h"
|
||||
#include "vk/allocation.h"
|
||||
#include <fastgltf/tools.hpp>
|
||||
#include <fastgltf/types.hpp>
|
||||
@@ -29,6 +31,8 @@ class gpu_buffer
|
||||
{
|
||||
std::vector<std::byte> bytes;
|
||||
vk::PhysicalDeviceLimits limits;
|
||||
vk::PhysicalDeviceIndexTypeUint8FeaturesEXT feat_uint8;
|
||||
|
||||
fastgltf::Asset & asset;
|
||||
vk::BufferUsageFlags usage;
|
||||
|
||||
@@ -58,8 +62,15 @@ class gpu_buffer
|
||||
}
|
||||
|
||||
public:
|
||||
gpu_buffer(vk::PhysicalDeviceProperties properties, fastgltf::Asset & asset) :
|
||||
limits(properties.limits), asset(asset), usage(0) {}
|
||||
gpu_buffer(vk::raii::PhysicalDevice physical_device, fastgltf::Asset & asset) :
|
||||
asset(asset),
|
||||
usage(0)
|
||||
{
|
||||
limits = physical_device.getProperties().limits;
|
||||
|
||||
if (utils::contains(application::instance().get_vk_device_extensions(), VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME))
|
||||
std::tie(std::ignore, feat_uint8) = physical_device.getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceIndexTypeUint8FeaturesEXT>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
size_t add_uniform(const T & data_to_add)
|
||||
@@ -79,7 +90,7 @@ public:
|
||||
return add(alignment, data_to_add);
|
||||
}
|
||||
|
||||
size_t add_indices(const fastgltf::Accessor & accessor)
|
||||
std::pair<size_t, vk::IndexType> add_indices(const fastgltf::Accessor & accessor)
|
||||
{
|
||||
usage |= vk::BufferUsageFlagBits::eIndexBuffer;
|
||||
|
||||
@@ -90,7 +101,11 @@ public:
|
||||
std::vector<uint8_t> indices(accessor.count);
|
||||
using index_t = std::remove_cvref_t<decltype(*indices.data())>;
|
||||
fastgltf::copyFromAccessor<index_t>(asset, accessor, indices.data());
|
||||
return add(4, indices);
|
||||
|
||||
if (feat_uint8.indexTypeUint8)
|
||||
return {add(4, indices), vk::IndexType::eUint8EXT};
|
||||
else
|
||||
return {add(4, std::vector<uint16_t>{indices.begin(), indices.end()}), vk::IndexType::eUint16};
|
||||
}
|
||||
|
||||
case fastgltf::ComponentType::Short:
|
||||
@@ -98,7 +113,7 @@ public:
|
||||
std::vector<uint16_t> indices(accessor.count);
|
||||
using index_t = std::remove_cvref_t<decltype(*indices.data())>;
|
||||
fastgltf::copyFromAccessor<index_t>(asset, accessor, indices.data());
|
||||
return add(4, indices);
|
||||
return {add(4, indices), vk::IndexType::eUint16};
|
||||
}
|
||||
|
||||
case fastgltf::ComponentType::Int:
|
||||
@@ -106,7 +121,7 @@ public:
|
||||
std::vector<uint32_t> indices(accessor.count);
|
||||
using index_t = std::remove_cvref_t<decltype(*indices.data())>;
|
||||
fastgltf::copyFromAccessor<index_t>(asset, accessor, indices.data());
|
||||
return add(4, indices);
|
||||
return {add(4, indices), vk::IndexType::eUint32};
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
@@ -18,10 +18,12 @@
|
||||
|
||||
#include "scene_loader.h"
|
||||
|
||||
#include "application.h"
|
||||
#include "gpu_buffer.h"
|
||||
#include "image_loader.h"
|
||||
#include "render/scene_components.h"
|
||||
#include "render/vertex_layout.h"
|
||||
#include "utils/contains.h"
|
||||
#include "utils/files.h"
|
||||
#include "utils/json_string.h"
|
||||
#include "utils/mapped_file.h"
|
||||
@@ -808,30 +810,8 @@ public:
|
||||
fastgltf::Accessor & indices_accessor = gltf.accessors.at(*gltf_primitive.indicesAccessor);
|
||||
|
||||
primitive_ref.indexed = true;
|
||||
primitive_ref.index_offset = staging_buffer.add_indices(indices_accessor);
|
||||
std::tie(primitive_ref.index_offset, primitive_ref.index_type) = staging_buffer.add_indices(indices_accessor);
|
||||
primitive_ref.index_count = indices_accessor.count;
|
||||
|
||||
switch (indices_accessor.componentType)
|
||||
{
|
||||
case fastgltf::ComponentType::Byte:
|
||||
case fastgltf::ComponentType::UnsignedByte:
|
||||
primitive_ref.index_type = vk::IndexType::eUint8EXT;
|
||||
break;
|
||||
|
||||
case fastgltf::ComponentType::Short:
|
||||
case fastgltf::ComponentType::UnsignedShort:
|
||||
primitive_ref.index_type = vk::IndexType::eUint16;
|
||||
break;
|
||||
|
||||
case fastgltf::ComponentType::Int:
|
||||
case fastgltf::ComponentType::UnsignedInt:
|
||||
primitive_ref.index_type = vk::IndexType::eUint32;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw std::runtime_error("Invalid index type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
primitive_ref.indexed = false;
|
||||
@@ -1089,8 +1069,6 @@ std::shared_ptr<entt::registry> scene_loader::operator()(
|
||||
const std::filesystem::path & gltf_texture_cache,
|
||||
std::function<void(float)> progress_cb)
|
||||
{
|
||||
vk::PhysicalDeviceProperties physical_device_properties = physical_device.getProperties();
|
||||
|
||||
auto data_buffer = fastgltf::GltfDataBuffer::FromBytes(data.data(), data.size());
|
||||
if (auto error = data_buffer.error(); error != fastgltf::Error::None)
|
||||
throw std::system_error((int)error, fastgltf_error_category);
|
||||
@@ -1108,7 +1086,7 @@ std::shared_ptr<entt::registry> scene_loader::operator()(
|
||||
// Load all buffers from URIs
|
||||
ctx.load_all_buffers();
|
||||
|
||||
gpu_buffer staging_buffer(physical_device_properties, asset);
|
||||
gpu_buffer staging_buffer(physical_device, asset);
|
||||
|
||||
// Load all textures
|
||||
auto textures = ctx.load_all_textures(gltf_texture_cache, progress_cb);
|
||||
|
||||
Reference in New Issue
Block a user