Unify the different ray models, allow overriding shaders used in gltf

This commit is contained in:
Guillaume Meunier
2026-07-25 18:35:41 +02:00
parent ed8316f320
commit eb94356092
4 changed files with 68 additions and 41 deletions
Binary file not shown.
+60 -41
View File
@@ -31,6 +31,8 @@
#include <limits> #include <limits>
#include <optional> #include <optional>
#include <string> #include <string>
#include <type_traits>
#include <unordered_map>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
@@ -55,7 +57,11 @@ static std::optional<std::string> get_property(const char * property)
} }
#endif #endif
static std::optional<std::string> env_string(std::string_view name) template <typename T>
std::optional<T> env(std::string_view name);
template <>
std::optional<std::string> env(std::string_view name)
{ {
#ifdef __ANDROID__ #ifdef __ANDROID__
auto android_sysprop_name = std::format("debug.wivrn.{}", name); auto android_sysprop_name = std::format("debug.wivrn.{}", name);
@@ -72,9 +78,10 @@ static std::optional<std::string> env_string(std::string_view name)
#endif #endif
} }
static std::optional<uint32_t> env_u32(std::string_view name) template <>
std::optional<uint32_t> env(std::string_view name)
{ {
const auto value = env_string(name); const auto value = env<std::string>(name);
if (not value) if (not value)
return std::nullopt; return std::nullopt;
try try
@@ -91,9 +98,10 @@ static std::optional<uint32_t> env_u32(std::string_view name)
} }
} }
static std::optional<bool> env_bool(std::string_view name) template <>
std::optional<bool> env(std::string_view name)
{ {
const auto value = env_string(name); const auto value = env<std::string>(name);
if (not value) if (not value)
return std::nullopt; return std::nullopt;
if (strcmp(value->c_str(), "1") == 0 || strcasecmp(value->c_str(), "true") == 0 || strcasecmp(value->c_str(), "yes") == 0 || strcasecmp(value->c_str(), "on") == 0) if (strcmp(value->c_str(), "1") == 0 || strcasecmp(value->c_str(), "true") == 0 || strcasecmp(value->c_str(), "yes") == 0 || strcasecmp(value->c_str(), "on") == 0)
@@ -103,6 +111,23 @@ static std::optional<bool> env_bool(std::string_view name)
return std::nullopt; return std::nullopt;
} }
template <>
std::optional<std::unordered_map<std::string, std::string>> env(std::string_view name)
{
const auto values = env<std::string>(name);
if (not values)
return std::nullopt;
std::unordered_map<std::string, std::string> map;
for (auto value: utils::split(*values, ","))
{
if (auto pos = value.find('='); pos != std::string::npos)
map.insert(std::make_pair(value.substr(0, pos), value.substr(pos + 1)));
}
return map;
}
hmd_traits::hmd_traits() = default; hmd_traits::hmd_traits() = default;
void hmd_traits::init() void hmd_traits::init()
@@ -225,7 +250,11 @@ void hmd_traits::init()
} }
} }
if (need_htc_ray) if (need_htc_ray)
controller_ray_model = "assets://ray-htc.glb"; override_shader = {
{"ray.frag", "ray_htc.frag"},
{"ray_skinned.vert", "ray_skinned_htc.vert"},
{"ray.vert", "ray_htc.vert"},
};
controller_profile = "htc-vive-focus-3"; controller_profile = "htc-vive-focus-3";
@@ -252,30 +281,8 @@ void hmd_traits::init()
#endif #endif
spdlog::info("HMD traits initialized"); spdlog::info("HMD traits initialized");
auto log_string = [](std::string_view name, std::string & field) { auto log = [](std::string_view name, auto & field) {
if (auto val = env_string(name)) if (auto val = env<std::remove_cvref_t<decltype(field)>>(name))
{
spdlog::info("\t{} override: {} -> {}", name, field, *val);
field = *val;
}
else
{
spdlog::info("\t{}: {}", name, field);
}
};
auto log_u32 = [](std::string_view name, uint32_t & field) {
if (auto val = env_u32(name))
{
spdlog::info("\t{} override: {} -> {}", name, field, *val);
field = *val;
}
else
{
spdlog::info("\t{}: {}", name, field);
}
};
auto log_bool = [](std::string_view name, bool & field) {
if (auto val = env_bool(name))
{ {
spdlog::info("\t{} override: {} -> {}", name, field, *val); spdlog::info("\t{} override: {} -> {}", name, field, *val);
field = *val; field = *val;
@@ -286,9 +293,9 @@ void hmd_traits::init()
} }
}; };
log_string("controller_profile", controller_profile); log("controller_profile", controller_profile);
log_string("controller_ray_model", controller_ray_model); log("controller_ray_model", controller_ray_model);
if (auto val = env_string("max_openxr_api_version")) if (auto val = env<std::string>("max_openxr_api_version"))
{ {
XrVersion v; XrVersion v;
if (val == "1.1") if (val == "1.1")
@@ -307,14 +314,26 @@ void hmd_traits::init()
{ {
spdlog::info("\t{}: {}", "max_openxr_api_version", xr::to_string(max_openxr_api_version)); spdlog::info("\t{}: {}", "max_openxr_api_version", xr::to_string(max_openxr_api_version));
} }
log_u32("panel_width_override", panel_width_override); log("panel_width_override", panel_width_override);
log_bool("needs_srgb_conversion", needs_srgb_conversion); log("needs_srgb_conversion", needs_srgb_conversion);
log_bool("view_locate", view_locate); log("view_locate", view_locate);
log_bool("vk_debug_ext_allowed", vk_debug_ext_allowed); log("vk_debug_ext_allowed", vk_debug_ext_allowed);
log_bool("bind_simple_controller", bind_simple_controller); log("bind_simple_controller", bind_simple_controller);
log_bool("hand_interaction_grip_surface", hand_interaction_grip_surface); log("hand_interaction_grip_surface", hand_interaction_grip_surface);
log_bool("pico_face_tracker", pico_face_tracker); log("pico_face_tracker", pico_face_tracker);
log_bool("discard_frame", discard_frame); log("discard_frame", discard_frame);
if (auto val = env<std::unordered_map<std::string, std::string>>("override_shader"))
{
for (const auto & [original, overridden]: *val)
override_shader.insert_or_assign(original, overridden);
}
std::erase_if(override_shader, [](const std::pair<std::string, std::string> & kv) { return kv.second == ""; });
for (const auto & [original, overridden]: override_shader)
{
spdlog::info("\tshader: {} -> {}", original, overridden);
}
} }
static XrViewConfigurationView scale_view(XrViewConfigurationView view, uint32_t width) static XrViewConfigurationView scale_view(XrViewConfigurationView view, uint32_t width)
+1
View File
@@ -54,6 +54,7 @@ public:
bool hand_interaction_grip_surface = true; bool hand_interaction_grip_surface = true;
bool pico_face_tracker = false; bool pico_face_tracker = false;
bool discard_frame = true; // can do xrBeginFrame twice to discard the first one bool discard_frame = true; // can do xrBeginFrame twice to discard the first one
std::unordered_map<std::string, std::string> override_shader;
#ifndef NDEBUG #ifndef NDEBUG
private: private:
bool initialized_ = false; bool initialized_ = false;
+7
View File
@@ -19,6 +19,7 @@
#include "scene_loader.h" #include "scene_loader.h"
#include "gpu_buffer.h" #include "gpu_buffer.h"
#include "hmd_traits.h"
#include "image_loader.h" #include "image_loader.h"
#include "render/scene_components.h" #include "render/scene_components.h"
#include "render/vertex_layout.h" #include "render/vertex_layout.h"
@@ -835,6 +836,9 @@ public:
if (auto it = extras.find({fastgltf::Category::Materials, material_id, "fragment_shader"}); it != extras.end()) if (auto it = extras.find({fastgltf::Category::Materials, material_id, "fragment_shader"}); it != extras.end())
material.fragment_shader = it->second; material.fragment_shader = it->second;
if (auto it = application::get_hmd_traits().override_shader.find(material.fragment_shader); it != application::get_hmd_traits().override_shader.end())
material.fragment_shader = it->second;
renderer::material::gpu_data & material_data = material.staging; renderer::material::gpu_data & material_data = material.staging;
material_data.base_color_factor = convert(gltf_material.pbrData.baseColorFactor); material_data.base_color_factor = convert(gltf_material.pbrData.baseColorFactor);
@@ -917,6 +921,9 @@ public:
else else
primitive_ref.vertex_shader = "lit_skinned.vert"; primitive_ref.vertex_shader = "lit_skinned.vert";
if (auto it = application::get_hmd_traits().override_shader.find(primitive_ref.vertex_shader); it != application::get_hmd_traits().override_shader.end())
primitive_ref.vertex_shader = it->second;
// For attributes types, see https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview (§ 3.7.2.1) // For attributes types, see https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview (§ 3.7.2.1)
auto shader = shader_loader{device}(primitive_ref.vertex_shader); auto shader = shader_loader{device}(primitive_ref.vertex_shader);
for (const auto & input: shader->inputs) for (const auto & input: shader->inputs)