Files
DarkflameServer/dUgcServer/Render/UgcHsr.h
Aaron Kimbrell 07dbf30dc8 feat(ugc): ray_backend=hiprt traces on the GPU with HIPRT and Orochi (optional build)
HIPRT (MIT) behind the CMake option DLU_HIPRT (off): its headers come from its
SDK (HIPRT_ROOT, else ROCm's /opt/rocm) and are copied next to the servers; its
library is loaded when first used (hiprtew), as HIP or CUDA are by Orochi
(MIT, fetched pinned by hash; CUDA when its toolkit is found). The trace
kernels (nearest hit skipping the triangle a ray leaves, any hit) are compiled
the first time and kept in cache/hiprt. One GPU context for the process
(hiprt_device picks the GPU); the workers take turns on it. When HIPRT, the
GPU or a scene's upload fails, Embree is used instead, and the UGC server logs
why at start.

For a GPU the rays go in batches (UgcRays::Scene gets batch queries; the CPU
backends answer them a ray at a time):
- hidden faces: with a batch backend the paths are traced side by side, a
  bounce at a time (the path code split into Start, Scatter and Bounce, the one
  by one tracing unchanged); the same paths with the same random numbers, so
  the same triangles are decided (tested with builtin side by side)
- the occlusion bake and the denoised icons' traced occlusion always ask in
  batches (the same rays, the same results)

Its symbols are hidden: the servers export theirs (-rdynamic), and HIPRT's
library, which has an Orochi of its own, would otherwise call ours.

Check: configure with -DDLU_HIPRT=ON on a machine with ROCm (or HIPRT's SDK)
and an AMD RDNA or NVIDIA GPU; UgcServer --make-model x.lxfml out hiprt; the
UGC tests (hits, hidden faces and occlusion against builtin); a build without
it leaves everything as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 12:29:08 -05:00

71 lines
3.7 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string_view>
#include <vector>
#include <glm/glm.hpp>
#include "UgcModel.h"
#include "UgcRays.h"
/**
* Hidden surface removal as LU Toolbox's Remove Hidden Faces decides it, without its texture: paths are traced from
* points on each opaque triangle under a sky of overwhelming brightness, bouncing off the model; a triangle none of
* whose paths reaches the sky is removed. So faces seen only through openings, or lit only by light bounced in
* (interiors, recesses), stay; faces sealed inside go. docs/UgcServer.md ("Hidden faces") has the details.
* The fast method (hsr_method=fast) is the test the UGC server used before: renders from 42 directions around the
* model (UgcRender::VisibleFromAround), which removes faces seen only by bounced light too.
*/
namespace UgcHsr {
// How hidden faces are found (hsr_method): LU Toolbox's paths, or the renders from around the model
enum class eMethod : uint8_t { TOOLBOX = 0, FAST };
// The setting's name of a method (toolbox, fast)
std::string_view Name(eMethod method);
// A method by its name; nullopt for anything else
std::optional<eMethod> Parse(std::string_view name);
struct Options {
bool enabled{ true }; // remove_hidden_faces
eMethod method{}; // hsr_method
bool groundPlane{ false }; // hsr_ground_plane: LU Toolbox's black box under the model (y 0 down to -100)
int samples{ 8 }; // hsr_samples: paths traced from each point (LU Toolbox's Samples)
int bounces{ 8 }; // hsr_bounces: bounces a path may take (the Cycles bake's Max Bounces)
float spacing{ 0.1143f }; // hsr_sample_spacing: LDD units between points (a stud is 0.8: 7 points along it)
int minPoints{ 28 }; // hsr_min_points: points on a triangle at least (LU Toolbox bakes 28 texels a triangle)
uint64_t seed{}; // of the paths' random numbers (the same seed gives the same result)
UgcRays::eBackend rays{}; // what traces the paths' rays (ray_backend)
int fastResolution{ 1024 }; // hsr_fast_resolution: pixels square of each of the fast method's renders
bool sideBySide{}; // trace the paths side by side as for a GPU whatever the backend (to compare the two)
};
struct Result {
size_t trianglesBefore{}; // opaque and transparent
size_t trianglesRemoved{};
std::vector<bool> kept; // per opaque triangle before: whether it stayed
uint64_t points{}; // sample points traced from (toolbox)
uint64_t paths{}; // paths traced (toolbox)
};
/**
* Which triangles of `mesh` a path from them reaches the sky from (triangles without area: none). `mesh` is
* everything that occludes (the opaque bricks; transparent ones hide nothing, as in LU Toolbox).
*/
std::vector<bool> Visible(const UgcModel::Mesh& mesh, const Options& options, uint64_t* points = nullptr, uint64_t* paths = nullptr);
// Removes the opaque triangles that aren't Visible, or with the fast method don't show in its renders (nothing when
// options.enabled is off); transparent ones stay
Result RemoveHiddenFaces(UgcModel::Model& model, const Options& options);
/**
* The points on triangle a, b, c paths start from, as barycentric weights (of a, b, c): rows parallel to its
* longest side, `spacing` apart, with points `spacing` apart along each row (so about area / spacing^2 of them).
* When that gives fewer than `minimum` the rows and points are closer together until there are that many; at
* least 4 (its centre and one towards each corner), at most 4096 (big triangles get them further apart).
*/
std::vector<glm::vec3> SamplePoints(const glm::vec3& a, const glm::vec3& b, const glm::vec3& c, float spacing, size_t minimum = 4);
}