| 419 | } |
| 420 | |
| 421 | WGPUBindGroup GPUBindGroupCacheWebGPU::Get(WGPUDevice device, GPUBindGroupKeyWebGPU& key, const StringAnsiView& debugName, uint64 gcFrames) |
| 422 | { |
| 423 | #if WEBGPU_LOG_BIND_GROUPS |
| 424 | #ifdef WEBGPU_LOG_PSO_NAME |
| 425 | const bool log = debugName.Contains(WEBGPU_LOG_PSO_NAME); |
| 426 | #else |
| 427 | const bool log = true; |
| 428 | #endif |
| 429 | #endif |
| 430 | |
| 431 | // Generate a hash for the key |
| 432 | key.LastFrameUsed = Engine::FrameCount; |
| 433 | key.Hash = Crc::MemCrc32(&key.Entries, key.EntriesCount * sizeof(WGPUBindGroupEntry)); |
| 434 | CombineHash(key.Hash, GetHash(key.EntriesCount)); |
| 435 | CombineHash(key.Hash, GetHash(key.Layout)); |
| 436 | CombineHash(key.Hash, Crc::MemCrc32(&key.Versions, key.EntriesCount * sizeof(uint8))); |
| 437 | |
| 438 | // Lookup for existing bind group |
| 439 | WGPUBindGroup bindGroup; |
| 440 | #if WEBGPU_CACHE_BIND_GROUPS |
| 441 | auto found = _bindGroups.Find(key); |
| 442 | if (found.IsNotEnd()) |
| 443 | { |
| 444 | // Get cached bind group and update the last usage frame |
| 445 | bindGroup = found->Value; |
| 446 | found->Key.LastFrameUsed = key.LastFrameUsed; |
| 447 | |
| 448 | // Periodically remove old bind groups (unused for some time) |
| 449 | if (key.LastFrameUsed - _lastFrameBindGroupsGC > gcFrames * 2) |
| 450 | { |
| 451 | _lastFrameBindGroupsGC = key.LastFrameUsed; |
| 452 | int32 freed = 0; |
| 453 | for (auto it = _bindGroups.Begin(); it.IsNotEnd(); ++it) |
| 454 | { |
| 455 | if (key.LastFrameUsed - it->Key.LastFrameUsed > gcFrames) |
| 456 | { |
| 457 | freed++; |
| 458 | wgpuBindGroupRelease(it->Value); |
| 459 | _bindGroups.Remove(it); |
| 460 | } |
| 461 | } |
| 462 | #if WEBGPU_LOG_BIND_GROUPS |
| 463 | if (freed > 0 && log) |
| 464 | LOG(Info, "[WebGPU] Removed {} old entries from '{}'", freed, String(debugName)); |
| 465 | #endif |
| 466 | } |
| 467 | |
| 468 | return bindGroup; |
| 469 | } |
| 470 | #endif |
| 471 | PROFILE_CPU(); |
| 472 | PROFILE_MEM(GraphicsShaders); |
| 473 | #if GPU_ENABLE_RESOURCE_NAMING |
| 474 | ZoneText(debugName.Get(), debugName.Length()); |
| 475 | #endif |
| 476 | #if WEBGPU_LOG_BIND_GROUPS |
| 477 | if (log) |
| 478 | LOG(Info, "[WebGPU] GetBindGroup: '{}', hash: {}", String(debugName), key.Hash); |
no test coverage detected