| 4836 | } |
| 4837 | |
| 4838 | bool MemoryLruVK::find(uint32_t _size, int32_t _memoryTypeIndex, DeviceMemoryAllocationVK *_alloc) |
| 4839 | { |
| 4840 | BGFX_PROFILER_SCOPE("MemoryLruVK::find", kColorResource); |
| 4841 | // Find best fit. |
| 4842 | uint16_t slot; |
| 4843 | |
| 4844 | { |
| 4845 | int16_t bestIdx = MAX_ENTRIES; |
| 4846 | uint32_t bestWaste = 0xffff'ffff; |
| 4847 | |
| 4848 | slot = lru.getFront(); |
| 4849 | |
| 4850 | while (UINT16_MAX != slot) |
| 4851 | { |
| 4852 | DeviceMemoryAllocationVK& alloc = entries[slot]; |
| 4853 | |
| 4854 | if (alloc.memoryTypeIndex == _memoryTypeIndex) |
| 4855 | { |
| 4856 | // 50% waste allowed, otherwise we'll just allocate a new one. |
| 4857 | // This is to prevent we trash this cache of useful allocations |
| 4858 | // with a handful of tiny allocations. |
| 4859 | |
| 4860 | if (alloc.size >= _size |
| 4861 | && alloc.size <= _size * 2) |
| 4862 | { |
| 4863 | const uint32_t waste = bx::narrowCast<uint32_t>(alloc.size - _size); |
| 4864 | |
| 4865 | if (waste < bestWaste) |
| 4866 | { |
| 4867 | bestIdx = slot; |
| 4868 | bestWaste = waste; |
| 4869 | |
| 4870 | if (waste == 0) |
| 4871 | { |
| 4872 | break; |
| 4873 | } |
| 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | |
| 4878 | slot = lru.getNext(slot); |
| 4879 | } |
| 4880 | |
| 4881 | slot = bestIdx; |
| 4882 | } |
| 4883 | |
| 4884 | if (MAX_ENTRIES != slot) |
| 4885 | { |
| 4886 | *_alloc = entries[slot]; |
| 4887 | lru.free(slot); |
| 4888 | totalSizeCached -= _alloc->size; |
| 4889 | |
| 4890 | return true; |
| 4891 | } |
| 4892 | |
| 4893 | return false; |
| 4894 | } |
| 4895 |
no test coverage detected