23constexpr const char* kSkinKernel = R
"WGSL(
25 count : u32, stride : u32, position : i32, normal : i32,
26 tangent : i32, joints : i32, jointBytes : u32, weights : i32,
27 weightKind : u32, chunked : u32, pad0 : u32, pad1 : u32,
29@group(0) @binding(0) var<uniform> P : Params;
30@group(0) @binding(1) var<storage, read> src : array<u32>;
31@group(0) @binding(2) var<storage, read_write> dst : array<u32>;
32@group(0) @binding(3) var<storage, read> pose : array<mat4x4f>;
33@group(0) @binding(4) var<storage, read> base : array<u32>;
34@group(0) @binding(5) var<storage, read> bones : array<u32>;
36fn byteAt(b : u32) -> u32 { return (src[b >> 2u] >> ((b & 3u) * 8u)) & 0xffu; }
37fn halfAt(b : u32) -> u32 { return byteAt(b) | (byteAt(b + 1u) << 8u); }
38fn word(w : u32) -> f32 { return bitcast<f32>(src[w]); }
40fn jointOf(v : u32, k : u32) -> u32 {
41 let at = v * P.stride * 4u + u32(P.joints) + k * P.jointBytes;
43 if (P.jointBytes == 1u) { j = byteAt(at); }
44 if (P.chunked != 0u) { j = bones[base[v] + j]; }
48fn weightOf(v : u32, k : u32) -> f32 {
49 let at = v * P.stride * 4u + u32(P.weights);
50 if (P.weightKind == 2u) { return bitcast<f32>(src[(at >> 2u) + k]); }
51 if (P.weightKind == 1u) { return f32(halfAt(at + k * 2u)) / 65535.0; }
52 return f32(byteAt(at + k)) / 255.0;
55fn put(w : u32, value : vec3f) {
56 dst[w] = bitcast<u32>(value.x);
57 dst[w + 1u] = bitcast<u32>(value.y);
58 dst[w + 2u] = bitcast<u32>(value.z);
61@compute @workgroup_size(64)
62fn main(@builtin(global_invocation_id) id : vec3u) {
64 if (v >= P.count) { return; }
65 var m = mat4x4f(vec4f(0.0), vec4f(0.0), vec4f(0.0), vec4f(0.0));
66 for (var k = 0u; k < 4u; k = k + 1u) {
67 let w = weightOf(v, k);
68 if (w > 0.0) { m = m + pose[jointOf(v, k)] * w; }
70 let row = v * P.stride;
71 let r = mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz);
72 if (P.position >= 0) {
73 let o = row + u32(P.position) / 4u;
74 put(o, (m * vec4f(word(o), word(o + 1u), word(o + 2u), 1.0)).xyz);
77 let o = row + u32(P.normal) / 4u;
78 put(o, r * vec3f(word(o), word(o + 1u), word(o + 2u)));
81 let o = row + u32(P.tangent) / 4u;
82 put(o, r * vec3f(word(o), word(o + 1u), word(o + 2u)));
87constexpr u32 kWorkgroup = 64;
89constexpr u64 kIdleFrames = 120;
90constexpr u32 kParamsBytes = 48;
97 for (
auto& [
id, entry] : entries_) release(entry);
102 return settings_.enabled && device_.supportsCompute() && !kernelFailed_;
111 if (frame == frame_)
return;
116 for (
auto it = entries_.begin(); it != entries_.end();) {
117 it->second.scheduled =
false;
118 if (it->second.lastFrame + kIdleFrames < frame) {
120 it = entries_.erase(it);
126 const u64 budget =
static_cast<u64>(std::max(settings_.maxMegabytes, 0.0f) * 1024.0f * 1024.0f);
127 while (bytes_ > budget && !entries_.empty()) {
128 auto oldest = std::min_element(entries_.begin(), entries_.end(),
129 [](
const auto& a,
const auto& b) { return a.second.lastFrame < b.second.lastFrame; });
130 release(oldest->second);
131 entries_.erase(oldest);
135void SkinCache::release(Entry& entry) {
136 for (
BufferHandle* b : {&entry.sourceBuffer, &entry.output, &entry.boneBase, &entry.bones, &entry.pose, &entry.params}) {
140 bytes_ -= std::min(bytes_, entry.bytes);
144bool SkinCache::build(Entry& entry,
const Mesh& mesh) {
145 const MeshSkinLayout& layout = mesh.skinLayout;
146 const auto& bytes = *mesh.skinSource;
147 const u32 size =
static_cast<u32>(bytes.size());
148 entry.vertexCount = mesh.vertexCount;
149 entry.source = mesh.skinSource;
150 entry.meshRevision = mesh.revision;
156 std::vector<u32> base(std::max<u32>(mesh.vertexCount, 1u), 0u);
157 std::vector<u32> bones(1, 0u);
158 if (mesh.isChunked() && mesh.skinIndices) {
159 const auto& indices = *mesh.skinIndices;
160 for (
const MeshSkinChunk& chunk : mesh.skinChunks) {
161 for (
u32 i = chunk.firstIndex; i < chunk.firstIndex + chunk.indexCount && i < indices.size(); ++i) {
162 if (indices[i] < base.size()) base[indices[i]] = chunk.boneOffset;
165 bones.assign(mesh.skinChunkBones.begin(), mesh.skinChunkBones.end());
171 const i32 params[12] = {
172 static_cast<i32>(mesh.vertexCount),
static_cast<i32>(layout.stride / 4), layout.position, layout.normal,
173 layout.tangent, layout.joints,
static_cast<i32>(layout.jointBytes), layout.weights,
174 static_cast<i32>(layout.weightKind), mesh.isChunked() ? 1 : 0, 0, 0,
177 entry.bytes =
static_cast<u64>(size) * 2 + base.size() * 4 + bones.size() * 4 + kParamsBytes;
178 bytes_ += entry.bytes;
188 auto found = entries_.find(entity.
raw);
189 if (found != entries_.end() && found->second.meshRevision != mesh.
revision) {
191 release(found->second);
192 entries_.erase(found);
193 found = entries_.end();
195 if (found == entries_.end()) {
196 const u64 budget =
static_cast<u64>(std::max(settings_.maxMegabytes, 0.0f) * 1024.0f * 1024.0f);
198 if (bytes_ + needs > budget) {
203 if (!build(entry, mesh)) {
207 found = entries_.emplace(entity.
raw, std::move(entry)).first;
209 Entry& entry = found->second;
210 if (entry.scheduled)
return entry.output;
211 entry.pendingPose.assign(pose, pose + joints);
212 entry.scheduled =
true;
213 entry.lastFrame = frame_;
219 const auto found = entries_.find(entity.
raw);
220 return found != entries_.end() && found->second.scheduled ? found->second.output :
BufferHandle::Invalid;
224 if (scheduled_ == 0)
return;
229 kernelFailed_ =
true;
230 ES_LOG_ERROR(
"SkinCache: the skinning kernel did not compile, skins are posed in the vertex stage: {}", log);
234 for (
auto& [
id, entry] : entries_) {
235 if (!entry.scheduled || entry.pendingPose.empty())
continue;
236 const u32 joints =
static_cast<u32>(entry.pendingPose.size());
239 device_.deleteBuffer(entry.pose);
240 entry.bytes -=
static_cast<u64>(entry.poseCapacity) *
sizeof(glm::mat4);
241 bytes_ -=
static_cast<u64>(entry.poseCapacity) *
sizeof(glm::mat4);
243 entry.poseCapacity = joints;
246 entry.bytes +=
static_cast<u64>(joints) *
sizeof(glm::mat4);
247 bytes_ +=
static_cast<u64>(joints) *
sizeof(glm::mat4);
250 device_.updateBuffer(entry.pose, 0, entry.pendingPose.data(), joints *
static_cast<u32>(
sizeof(glm::mat4)));
259 device_.dispatchCompute(kernel_, bindings, 6, (entry.vertexCount + kWorkgroup - 1) / kWorkgroup);
Abstract graphics device interface.
Logging utilities for ESEngine.
#define ES_LOG_ERROR(...)
Logs an error message.
Geometry that lives on the GPU rather than in a frame.
Skinned meshes posed once a frame by a compute shader, into vertices every pass then draws as they ar...
static constexpr GfxContent transient()
Abstract graphics device interface.
void deleteBuffer(BufferHandle buffer)
Deletes a buffer.
Buffers, the layout that describes them, and the bounds culling reads.
MeshSkinLayout skinLayout
std::shared_ptr< const std::vector< u8 > > skinSource
bool wants(SkinCacheUsage usage) const
BufferHandle bufferFor(Entity entity) const
void beginFrame(u64 frame)
SkinCache(GfxDevice &device)
BufferHandle schedule(Entity entity, const Mesh &mesh, const glm::mat4 *pose, u32 joints)
Schedules entity's skin for this frame, posed by pose (one matrix a joint: joint world times inverse ...
std::int32_t i32
Signed 32-bit integer.
std::uint64_t u64
Unsigned 64-bit integer.
@ Storage
Read and written by compute shaders (var<storage, read_write>), and readable back....
std::uint32_t u32
Unsigned 32-bit integer.
SkinCacheUsage
A mesh's own word on the cache (MeshSkin::skinCache).
Entity identifier with packed index + generation.
One resource a compute dispatch binds, at @group(0) @binding(binding).
static GfxComputeBinding uniform(u32 binding, BufferHandle buffer, u32 offset=0, u32 size=0)
static GfxComputeBinding storageRead(u32 binding, BufferHandle buffer, u32 offset=0, u32 size=0)
static GfxComputeBinding storage(u32 binding, BufferHandle buffer, u32 offset=0, u32 size=0)
A compute program's source. Compute is WGSL only: GLSL ES 3.00 has no compute stage.