[BROKEN] This compiles if we use fastgltf 0.9.0, but it says Error::UnsupportedVersion for the file. Earlier versions can't compile because either simdjson has code errors, or the .a/.so is missing important functions.

This commit is contained in:
Zed A. Shaw 2025-12-20 00:36:55 -05:00
parent 85d792e257
commit dbda70e3a0
7 changed files with 185 additions and 7 deletions

View file

@ -29,9 +29,12 @@ shaders:
%.glb: %.glb.gz %.glb: %.glb.gz
gunzip -k $< gunzip -k $<
build: basicmesh.glb build:
meson compile -j 10 -C $(ROOT_DIR)/builddir meson compile -j 10 -C $(ROOT_DIR)/builddir
meshes: basicmesh.glb
@echo "Meshes made"
release_build: release_build:
meson --wipe builddir -Db_ndebug=true --buildtype release meson --wipe builddir -Db_ndebug=true --buildtype release
meson compile -j 10 -C builddir meson compile -j 10 -C builddir

View file

@ -78,7 +78,14 @@ imgui = subproject('imgui',
).get_variable('imgui_dep') ).get_variable('imgui_dep')
sdl2 = subproject('sdl2').get_variable('sdl2_dep') sdl2 = subproject('sdl2').get_variable('sdl2_dep')
fastgltf = subproject('fastgltf').get_variable('fastgltf_dep')
fastgltf_opts = cmake.subproject_options()
fastgltf_opts.add_cmake_defines({
'FASTGLTF_DOWNLOAD_SIMDJSON': false,
})
fastgltf_proj = cmake.subproject('fastgltf', options: fastgltf_opts)
fastgltf = fastgltf_proj.get_variable('fastgltf_dep')
glm_opts = cmake.subproject_options() glm_opts = cmake.subproject_options()
glm_opts.add_cmake_defines({ glm_opts.add_cmake_defines({
@ -112,6 +119,7 @@ dependencies += [
glm, glm,
imgui, imgui,
sdl2, sdl2,
fastgltf,
] ]
sources = [ sources = [

View file

@ -14,7 +14,7 @@
#define VMA_IMPLEMENTATION #define VMA_IMPLEMENTATION
#include "vk_mem_alloc.h" #include "vk_mem_alloc.h"
constexpr bool bUseValidationLayers = true; constexpr bool bUseValidationLayers = false;
VulkanEngine* loadedEngine = nullptr; VulkanEngine* loadedEngine = nullptr;
@ -202,6 +202,7 @@ void VulkanEngine::draw_geometry(VkCommandBuffer cmd)
vkCmdDraw(cmd, 3, 1, 0, 0); vkCmdDraw(cmd, 3, 1, 0, 0);
// draw rectangle
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, _meshPipeline); vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, _meshPipeline);
GPUDrawPushConstants push_constants{ GPUDrawPushConstants push_constants{
@ -215,6 +216,30 @@ void VulkanEngine::draw_geometry(VkCommandBuffer cmd)
vkCmdDrawIndexed(cmd, 6, 1, 0, 0, 0); vkCmdDrawIndexed(cmd, 6, 1, 0, 0, 0);
// draw monkey
size_t model_idx = 2; // 0 cube; 1 sphere; 2 monkey
push_constants.vertexBuffer = testMeshes[model_idx]->meshBuffers.vertexBufferAddress;
vkCmdPushConstants(cmd,
_meshPipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT,
0,
sizeof(GPUDrawPushConstants),
&push_constants);
vkCmdBindIndexBuffer(cmd,
testMeshes[model_idx]->meshBuffers.indexBuffer.buffer,
0,
VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(cmd,
testMeshes[model_idx]->surfaces[0].count,
1,
testMeshes[model_idx]->surfaces[0].startIndex,
0, 0);
vkCmdEndRendering(cmd); vkCmdEndRendering(cmd);
} }
@ -711,7 +736,6 @@ void VulkanEngine::destroy_buffer(const AllocatedBuffer& buffer)
vmaDestroyBuffer(_allocator, buffer.buffer, buffer.allocation); vmaDestroyBuffer(_allocator, buffer.buffer, buffer.allocation);
} }
GPUMeshBuffers VulkanEngine::uploadMesh(std::span<uint32_t> indices, std::span<Vertex> vertices) GPUMeshBuffers VulkanEngine::uploadMesh(std::span<uint32_t> indices, std::span<Vertex> vertices)
{ {
const size_t vertexBufferSize = vertices.size() * sizeof(Vertex); const size_t vertexBufferSize = vertices.size() * sizeof(Vertex);
@ -796,6 +820,11 @@ void VulkanEngine::init_default_data() {
rectangle = uploadMesh(rect_indices, rect_vertices); rectangle = uploadMesh(rect_indices, rect_vertices);
auto basicmesh = loadGltfMeshes(this, "basicmesh.glb");
assert(basicmesh != std::nullopt && "Failed to load basicmesh.glb");
testMeshes = *basicmesh;
_mainDeletionQueue.push_function([&](){ _mainDeletionQueue.push_function([&](){
destroy_buffer(rectangle.indexBuffer); destroy_buffer(rectangle.indexBuffer);
destroy_buffer(rectangle.vertexBuffer); destroy_buffer(rectangle.vertexBuffer);

View file

@ -6,6 +6,7 @@
#include <vk_types.h> #include <vk_types.h>
#include <vk_descriptors.h> #include <vk_descriptors.h>
#include "vk_gui.h" #include "vk_gui.h"
#include "vk_loader.h"
struct Transaction { struct Transaction {
VkCommandBuffer cmd; VkCommandBuffer cmd;
@ -100,6 +101,7 @@ public:
VkPipeline _meshPipeline; VkPipeline _meshPipeline;
GPUMeshBuffers rectangle; GPUMeshBuffers rectangle;
std::vector<std::shared_ptr<MeshAsset>> testMeshes;
// ZED's REFACTOR // ZED's REFACTOR
VkGUI _gui; VkGUI _gui;
@ -123,6 +125,8 @@ public:
Transaction begin_transaction(); Transaction begin_transaction();
void commit_transaction(Transaction& cmd); void commit_transaction(Transaction& cmd);
GPUMeshBuffers uploadMesh(std::span<uint32_t> indices, std::span<Vertex> vertices);
private: private:
void init_vulkan(); void init_vulkan();
void init_swapchain(); void init_swapchain();
@ -146,5 +150,4 @@ private:
void draw_background(VkCommandBuffer cmd); void draw_background(VkCommandBuffer cmd);
void draw_geometry(VkCommandBuffer cmd); void draw_geometry(VkCommandBuffer cmd);
GPUMeshBuffers uploadMesh(std::span<uint32_t> indices, std::span<Vertex> vertices);
}; };

View file

@ -11,9 +11,131 @@
#include <fastgltf/core.hpp> #include <fastgltf/core.hpp>
#include <fastgltf/tools.hpp> #include <fastgltf/tools.hpp>
constexpr bool OverrideColors = true;
std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath) std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath)
{ {
std::println("\nLoading GLTF: {}", filePath.string());
auto data = fastgltf::GltfDataBuffer::FromPath(filePath);
return std::nullopt; if(data.error() != fastgltf::Error::None) {
std::println("Failed to load glTF: {}={}\n",
typeid(data.error()).name(),
fastgltf::to_underlying(data.error()));
return std::nullopt;
}
constexpr auto gltfOptions = fastgltf::Options::LoadExternalBuffers;
fastgltf::Asset gltf{};
fastgltf::Parser parser{};
auto load = parser.loadGltfBinary(data.get(), filePath.parent_path(), gltfOptions);
switch(load.error()) {
case fastgltf::Error::None:
gltf = std::move(load.get());
break;
case fastgltf::Error::InvalidGLB:
std::println("fastgltf says Error::InvalidGLB {}", filePath.string());
return std::nullopt;
break;
case fastgltf::Error::UnsupportedVersion:
std::println("fastgltf says Error::UnsupportedVersion {}", filePath.string());
return std::nullopt;
break;
case fastgltf::Error::InvalidPath:
std::println("fastgltf says Error::UnsupportedVersion {}",
filePath.string());
return std::nullopt;
break;
default:
std::println("Unknown fastgltf error loading {}", filePath.string());
break;
}
std::vector<std::shared_ptr<MeshAsset>> meshes;
// use the same vectors for all meshes
std::vector<uint32_t> indices;
std::vector<Vertex> vertices;
for(auto& mesh : gltf.meshes) {
MeshAsset newmesh;
newmesh.name = mesh.name;
indices.clear();
indices.clear();
for(auto&& p : mesh.primitives) {
auto& indexAccessor = gltf.accessors[p.indicesAccessor.value()];
GeoSurface newSurface {
.startIndex = (uint32_t)indices.size(),
.count = (uint32_t)indexAccessor.count,
};
size_t initial_vtx = vertices.size();
// load indices
{
indices.reserve(indices.size() + indexAccessor.count);
fastgltf::iterateAccessor<std::uint32_t>(
gltf, indexAccessor, [&](std::uint32_t idx) {
indices.push_back(idx + initial_vtx);
});
}
// load vertex positions
{
fastgltf::Accessor& posAccessor = gltf.accessors[
p.findAttribute("POSITIONS")->accessorIndex];
vertices.resize(vertices.size() + posAccessor.count);
fastgltf::iterateAccessorWithIndex<glm::vec3>(
gltf, posAccessor, [&](glm::vec3 v, size_t index)
{
vertices[initial_vtx + index] = {
.position = v,
.uv_x = 0,
.normal = {1, 0, 0},
.uv_y = 0,
.color = glm::vec4{1.0f},
};
});
}
// load vertex normals
auto normals = p.findAttribute("NORMA:");
if(normals != p.attributes.end()) {
fastgltf::iterateAccessorWithIndex<glm::vec3>(
gltf, gltf.accessors[(*normals).accessorIndex],
[&](glm::vec3 v, size_t index)
{
vertices[initial_vtx + index].normal = v;
});
}
auto uv = p.findAttribute("TEXCOORD_0");
if(uv != p.attributes.end()) {
fastgltf::iterateAccessorWithIndex<glm::vec2>(
gltf, gltf.accessors[(*uv).accessorIndex],
[&](glm::vec2 v, size_t index) {
vertices[initial_vtx + index].uv_x = v.x;
vertices[initial_vtx + index].uv_y = v.y;
});
}
if(OverrideColors) {
for(Vertex& vtx : vertices) {
vtx.color = glm::vec4(vtx.normal, 1.0f);
}
}
newmesh.meshBuffers = engine->uploadMesh(indices, vertices);
meshes.emplace_back(std::make_shared<MeshAsset>(std::move(newmesh)));
}
}
return meshes;
} }

View file

@ -1,7 +1,7 @@
[wrap-git] [wrap-git]
directory=fastgltf directory=fastgltf
url=https://github.com/spnda/fastgltf.git url=https://github.com/spnda/fastgltf.git
revision=v0.9.x revision=v0.9.0
depth=1 depth=1
method=cmake method=cmake

13
wraps/simdjson.wrap Normal file
View file

@ -0,0 +1,13 @@
[wrap-file]
directory = simdjson-3.3.0
source_url = https://github.com/simdjson/simdjson/archive/refs/tags/v3.3.0.tar.gz
source_filename = simdjson-3.3.0.tar.gz
source_hash = a8c9feff2f19c3ff281d42f0b6b4b18f02236513b99229756fa9a1b14787a58a
patch_filename = simdjson_3.3.0-2_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/simdjson_3.3.0-2/get_patch
patch_hash = 3c39f8a5abac17732b9599a416e6edb09f5f987bcf4e8b46808dbe88040eb40f
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/simdjson_3.3.0-2/simdjson-3.3.0.tar.gz
wrapdb_version = 3.3.0-2
[provide]
simdjson = simdjson_dep