Store the name of custom shaders in the glTF file
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -101,7 +101,7 @@ struct material
|
||||
size_t offset;
|
||||
|
||||
std::string name;
|
||||
std::string fragment_shader_name = "lit.frag";
|
||||
std::string fragment_shader = "lit.frag";
|
||||
};
|
||||
|
||||
struct primitive
|
||||
|
||||
@@ -45,6 +45,9 @@
|
||||
#include <limits>
|
||||
#include <ranges>
|
||||
#include <simdjson/dom.h>
|
||||
#include <simdjson/dom/element.h>
|
||||
#include <simdjson/dom/object.h>
|
||||
#include <simdjson/error.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string>
|
||||
#include <vulkan/vulkan_format_traits.hpp>
|
||||
@@ -513,7 +516,7 @@ struct : std::error_category
|
||||
} fastgltf_error_category;
|
||||
// END
|
||||
|
||||
fastgltf::Asset load_gltf_asset(fastgltf::GltfDataBuffer & buffer, const std::filesystem::path & directory)
|
||||
fastgltf::Asset load_gltf_asset(fastgltf::GltfDataBuffer & buffer, const std::filesystem::path & directory, std::function<void(simdjson::dom::object &, std::size_t, fastgltf::Category)> extra_callback)
|
||||
{
|
||||
fastgltf::Parser parser(
|
||||
fastgltf::Extensions::KHR_texture_basisu |
|
||||
@@ -524,6 +527,12 @@ fastgltf::Asset load_gltf_asset(fastgltf::GltfDataBuffer & buffer, const std::fi
|
||||
fastgltf::Options::AllowDouble |
|
||||
fastgltf::Options::DecomposeNodeMatrices;
|
||||
|
||||
parser.setUserPointer(&extra_callback);
|
||||
parser.setExtrasParseCallback([](simdjson::dom::object * extras, std::size_t object_index, fastgltf::Category category, void * user_pointer) {
|
||||
auto f = reinterpret_cast<std::function<void(simdjson::dom::object &, std::size_t, fastgltf::Category)> *>(user_pointer);
|
||||
(*f)(*extras, object_index, category);
|
||||
});
|
||||
|
||||
auto expected_asset = parser.loadGltf(buffer, directory, gltf_options);
|
||||
|
||||
if (auto error = expected_asset.error(); error != fastgltf::Error::None)
|
||||
@@ -540,7 +549,8 @@ class loader_context
|
||||
vk::raii::PhysicalDevice physical_device;
|
||||
vk::raii::Device & device;
|
||||
thread_safe<vk::raii::Queue> & queue;
|
||||
image_loader & loader;
|
||||
image_loader loader;
|
||||
const std::map<std::tuple<fastgltf::Category, size_t, std::string>, std::string> & extras;
|
||||
|
||||
std::vector<utils::mapped_file> loaded_assets;
|
||||
utils::mapped_file & load_from_asset(const std::filesystem::path & path)
|
||||
@@ -563,14 +573,16 @@ public:
|
||||
vk::raii::PhysicalDevice physical_device,
|
||||
vk::raii::Device & device,
|
||||
thread_safe<vk::raii::Queue> & queue,
|
||||
image_loader & loader) :
|
||||
uint32_t queue_family_index,
|
||||
std::map<std::tuple<fastgltf::Category, size_t, std::string>, std::string> & extras) :
|
||||
base_directory(std::move(base_directory)),
|
||||
name(std::move(name)),
|
||||
gltf(gltf),
|
||||
physical_device(physical_device),
|
||||
device(device),
|
||||
queue(queue),
|
||||
loader(loader)
|
||||
loader(device, physical_device, queue, queue_family_index),
|
||||
extras(extras)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -793,6 +805,8 @@ public:
|
||||
materials.reserve(gltf.materials.size());
|
||||
for (const fastgltf::Material & gltf_material: gltf.materials)
|
||||
{
|
||||
size_t material_id = materials.size();
|
||||
|
||||
// Copy the default material, without references to its buffer or descriptor set
|
||||
auto & material = *materials.emplace_back(std::make_shared<renderer::material>(default_material));
|
||||
material.name = gltf_material.name;
|
||||
@@ -803,21 +817,24 @@ public:
|
||||
switch (gltf_material.alphaMode)
|
||||
{
|
||||
case fastgltf::AlphaMode::Opaque:
|
||||
material.fragment_shader_name = "lit.frag";
|
||||
material.fragment_shader = "lit.frag";
|
||||
material.blend_enable = false;
|
||||
break;
|
||||
|
||||
case fastgltf::AlphaMode::Mask:
|
||||
material.fragment_shader_name = "lit_mask.frag";
|
||||
material.fragment_shader = "lit_mask.frag";
|
||||
material.blend_enable = false;
|
||||
break;
|
||||
|
||||
case fastgltf::AlphaMode::Blend:
|
||||
material.fragment_shader_name = "lit.frag";
|
||||
material.fragment_shader = "lit.frag";
|
||||
material.blend_enable = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (auto it = extras.find({fastgltf::Category::Materials, material_id, "fragment_shader"}); it != extras.end())
|
||||
material.fragment_shader = it->second;
|
||||
|
||||
renderer::material::gpu_data & material_data = material.staging;
|
||||
|
||||
material_data.base_color_factor = convert(gltf_material.pbrData.baseColorFactor);
|
||||
@@ -870,6 +887,7 @@ public:
|
||||
meshes.reserve(gltf.meshes.size());
|
||||
for (const fastgltf::Mesh & gltf_mesh: gltf.meshes)
|
||||
{
|
||||
size_t mesh_id = meshes.size();
|
||||
std::shared_ptr<renderer::mesh> & mesh_ref = meshes.emplace_back();
|
||||
mesh_ref = std::make_shared<renderer::mesh>();
|
||||
|
||||
@@ -892,7 +910,9 @@ public:
|
||||
|
||||
renderer::vertex_layout & layout = primitive_ref.layout;
|
||||
|
||||
if (gltf_primitive.findAttribute("JOINTS_0") == gltf_primitive.attributes.cend())
|
||||
if (auto it = extras.find({fastgltf::Category::Meshes, mesh_id, "vertex_shader"}); it != extras.end())
|
||||
primitive_ref.vertex_shader = it->second;
|
||||
else if (gltf_primitive.findAttribute("JOINTS_0") == gltf_primitive.attributes.cend())
|
||||
primitive_ref.vertex_shader = "lit.vert";
|
||||
else
|
||||
primitive_ref.vertex_shader = "lit_skinned.vert";
|
||||
@@ -1147,10 +1167,19 @@ std::shared_ptr<entt::registry> scene_loader::operator()(
|
||||
if (auto error = data_buffer.error(); error != fastgltf::Error::None)
|
||||
throw std::system_error((int)error, fastgltf_error_category);
|
||||
|
||||
fastgltf::Asset asset = load_gltf_asset(data_buffer.get(), parent_path);
|
||||
std::map<std::tuple<fastgltf::Category, size_t, std::string>, std::string> gltf_extras;
|
||||
|
||||
image_loader loader{device, physical_device, queue, queue_family_index};
|
||||
loader_context ctx{parent_path, name, asset, physical_device, device, queue, loader};
|
||||
fastgltf::Asset asset = load_gltf_asset(data_buffer.get(), parent_path, [&](simdjson::dom::object & extras, std::size_t object_index, fastgltf::Category category) {
|
||||
std::string_view value;
|
||||
|
||||
for (const auto & kv: extras)
|
||||
{
|
||||
if (kv.value.get_string().get(value) == simdjson::SUCCESS)
|
||||
gltf_extras.emplace(std::tuple{category, object_index, kv.key}, value);
|
||||
}
|
||||
});
|
||||
|
||||
loader_context ctx{parent_path, name, asset, physical_device, device, queue, queue_family_index, gltf_extras};
|
||||
|
||||
#ifndef NDEBUG
|
||||
if (auto error = fastgltf::validate(asset); error != fastgltf::Error::None)
|
||||
|
||||
@@ -676,8 +676,8 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info)
|
||||
{
|
||||
spdlog::debug("Creating pipeline");
|
||||
|
||||
auto vertex_shader = shader_loader{device}(info.vertex_shader_name);
|
||||
auto fragment_shader = shader_loader{device}(info.fragment_shader_name);
|
||||
auto vertex_shader = load_shader(device, info.vertex_shader_name);
|
||||
auto fragment_shader = load_shader(device, info.fragment_shader_name);
|
||||
|
||||
auto specialization = make_specialization_constants(
|
||||
int32_t(info.nb_texcoords),
|
||||
@@ -1144,7 +1144,7 @@ void scene_renderer::render(
|
||||
pipeline_info info{
|
||||
.renderpass = rp_info,
|
||||
.vertex_shader_name = primitive.vertex_shader,
|
||||
.fragment_shader_name = material->fragment_shader_name,
|
||||
.fragment_shader_name = material->fragment_shader,
|
||||
.vertex_layout = primitive.layout,
|
||||
.cull_mode = primitive.cull_mode,
|
||||
.front_face = primitive.front_face,
|
||||
|
||||
@@ -194,17 +194,6 @@ struct ray_extra_shader_data
|
||||
void setup_ray_shader(entt::registry & scene, entt::entity entity, const ray_extra_shader_data & initial_data = {})
|
||||
{
|
||||
auto & node = scene.get<components::node>(entity);
|
||||
|
||||
for (auto & primitive: node.mesh->primitives)
|
||||
{
|
||||
if (primitive.vertex_shader == "lit.vert")
|
||||
primitive.vertex_shader = "ray.vert";
|
||||
else if (primitive.vertex_shader == "lit_skinned.vert")
|
||||
primitive.vertex_shader = "ray_skinned.vert";
|
||||
|
||||
primitive.material_->fragment_shader_name = "ray.frag";
|
||||
}
|
||||
|
||||
node.set_extra_shader_data(initial_data);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -1034,7 +1034,7 @@ void scenes::lobby::gui_debug_node_hierarchy(entt::entity root)
|
||||
primitive.vertex_count,
|
||||
primitive.material_->name,
|
||||
primitive.vertex_shader,
|
||||
primitive.material_->fragment_shader_name);
|
||||
primitive.material_->fragment_shader);
|
||||
|
||||
imgui_ctx->tooltip(tooltip);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
*/
|
||||
|
||||
#include "shader.h"
|
||||
#include "utils/cache.h"
|
||||
#include "wivrn_shaders.h"
|
||||
#include <magic_enum.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -104,8 +103,3 @@ std::shared_ptr<shader> shader_loader::operator()(const std::string & name)
|
||||
|
||||
return (*this)(shaders.at(name));
|
||||
}
|
||||
|
||||
std::shared_ptr<shader> load_shader(vk::raii::Device & device, const std::string & name)
|
||||
{
|
||||
return shader_loader{device}(name);
|
||||
}
|
||||
|
||||
+4
-1
@@ -64,4 +64,7 @@ struct shader_loader
|
||||
std::shared_ptr<shader> operator()(const std::string & name);
|
||||
};
|
||||
|
||||
std::shared_ptr<shader> load_shader(vk::raii::Device & device, const std::string & name);
|
||||
inline std::shared_ptr<shader> load_shader(vk::raii::Device & device, const std::string & name)
|
||||
{
|
||||
return shader_loader{device}(name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user