mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-12 22:28:32 +09:00
Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37111ae992 | ||
|
|
6b0c2a15ab | ||
|
|
e9ffd99313 | ||
|
|
7c01ddea0c | ||
|
|
2d4d6e9cfb | ||
|
|
76b8957b99 | ||
|
|
ec685b9fa7 | ||
|
|
a12068df52 | ||
|
|
0b344792cc |
@@ -18,6 +18,7 @@
|
|||||||
#include "MG_Util/Converters/MGToVk/TextureEnumConverter.h"
|
#include "MG_Util/Converters/MGToVk/TextureEnumConverter.h"
|
||||||
#include "MG_Util/Metrics/TextureMetrics.h"
|
#include "MG_Util/Metrics/TextureMetrics.h"
|
||||||
#include <Config.h>
|
#include <Config.h>
|
||||||
|
#include <algorithm>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@@ -204,6 +205,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
// The frame's descriptor sets are recycled above, so last frame's reuse target
|
// The frame's descriptor sets are recycled above, so last frame's reuse target
|
||||||
// is gone: start the per-draw descriptor-reuse cache fresh this frame.
|
// is gone: start the per-draw descriptor-reuse cache fresh this frame.
|
||||||
m_hasLastDescriptor = false;
|
m_hasLastDescriptor = false;
|
||||||
|
m_lastBindValid = false;
|
||||||
// Re-fingerprint the bound sampler set fresh this frame so any GL object address
|
// Re-fingerprint the bound sampler set fresh this frame so any GL object address
|
||||||
// reuse cannot outlive a single frame (see SamplerResolveMemo).
|
// reuse cannot outlive a single frame (see SamplerResolveMemo).
|
||||||
for (auto& memo : m_samplerResolveMemo) {
|
for (auto& memo : m_samplerResolveMemo) {
|
||||||
@@ -1189,16 +1191,47 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
bufferInfo.range = ubo.range;
|
bufferInfo.range = ubo.range;
|
||||||
dynOffset = static_cast<Uint32>(ubo.dynamicOffset);
|
dynOffset = static_cast<Uint32>(ubo.dynamicOffset);
|
||||||
} else {
|
} else {
|
||||||
BufferSlice slice{};
|
// Global-UBO slice reuse (see GlobalUboSliceMemo): unchanged
|
||||||
if (!m_bufferManager->UploadTransient(BufferKind::Uniform, frameIndex, ubo.payload,
|
// uniform bytes re-use the slice already uploaded this frame.
|
||||||
ubo.payloadSize, m_minDynamicOffsetAlignment, slice)) {
|
const Bool isGlobalUbo =
|
||||||
MOBILEGL_ASSERT(false, "UniformDescriptorBinder::BindProgramUniformBuffers failed: UBO upload failed on binding %u element %u",
|
programObj.globalUboBinding == static_cast<Int>(binding) && element == 0;
|
||||||
binding, element);
|
const Uint64 uboFrameSerial = m_bufferManager->GetFrameSerial();
|
||||||
return false;
|
const Uint64 uboProgramLifetimeId = program.GetLifetimeId();
|
||||||
|
const Uint32 uboContentVersion = program.GetUBOContentVersion();
|
||||||
|
Bool reusedSlice = false;
|
||||||
|
if (isGlobalUbo) {
|
||||||
|
for (const auto& memo : m_globalUboMemo) {
|
||||||
|
if (memo.buffer != VK_NULL_HANDLE &&
|
||||||
|
memo.programLifetimeId == uboProgramLifetimeId &&
|
||||||
|
memo.frameSerial == uboFrameSerial &&
|
||||||
|
memo.uboContentVersion == uboContentVersion &&
|
||||||
|
memo.range == static_cast<VkDeviceSize>(ubo.payloadSize)) {
|
||||||
|
bufferInfo.buffer = memo.buffer;
|
||||||
|
bufferInfo.range = memo.range;
|
||||||
|
dynOffset = static_cast<Uint32>(memo.offset);
|
||||||
|
reusedSlice = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!reusedSlice) {
|
||||||
|
BufferSlice slice{};
|
||||||
|
if (!m_bufferManager->UploadTransient(BufferKind::Uniform, frameIndex, ubo.payload,
|
||||||
|
ubo.payloadSize, m_minDynamicOffsetAlignment, slice)) {
|
||||||
|
MOBILEGL_ASSERT(false, "UniformDescriptorBinder::BindProgramUniformBuffers failed: UBO upload failed on binding %u element %u",
|
||||||
|
binding, element);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bufferInfo.buffer = slice.buffer;
|
||||||
|
bufferInfo.range = ubo.payloadSize;
|
||||||
|
dynOffset = static_cast<Uint32>(slice.offset);
|
||||||
|
if (isGlobalUbo) {
|
||||||
|
m_globalUboMemo[m_globalUboMemoNext] = GlobalUboSliceMemo{
|
||||||
|
uboProgramLifetimeId, uboFrameSerial, uboContentVersion,
|
||||||
|
slice.buffer, slice.offset, static_cast<VkDeviceSize>(ubo.payloadSize)};
|
||||||
|
m_globalUboMemoNext = (m_globalUboMemoNext + 1) % kGlobalUboMemoSize;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bufferInfo.buffer = slice.buffer;
|
|
||||||
bufferInfo.range = ubo.payloadSize;
|
|
||||||
dynOffset = static_cast<Uint32>(slice.offset);
|
|
||||||
}
|
}
|
||||||
bufferInfos.push_back(bufferInfo);
|
bufferInfos.push_back(bufferInfo);
|
||||||
// Dynamic offsets are consumed in binding order, then array element order,
|
// Dynamic offsets are consumed in binding order, then array element order,
|
||||||
@@ -1337,8 +1370,34 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
m_hasLastDescriptor = cacheable;
|
m_hasLastDescriptor = cacheable;
|
||||||
}
|
}
|
||||||
|
|
||||||
vkCmdBindDescriptorSets(commandBuffer, bindPoint, programObj.pipelineLayout, 0, 1,
|
// Skip the driver call when this exact binding is already live on the
|
||||||
&descriptorSet, static_cast<Uint32>(dynamicOffsets.size()), dynamicOffsets.data());
|
// command buffer (see the bind-dedup shadow in the header).
|
||||||
|
const Uint32 offsetCount = static_cast<Uint32>(dynamicOffsets.size());
|
||||||
|
Bool identicalBind = m_lastBindValid && m_lastBindSet == descriptorSet &&
|
||||||
|
m_lastBindLayout == programObj.pipelineLayout && m_lastBindPoint == bindPoint &&
|
||||||
|
m_lastBindOffsetCount == offsetCount && offsetCount <= kMaxShadowedDynamicOffsets;
|
||||||
|
if (identicalBind) {
|
||||||
|
for (Uint32 i = 0; i < offsetCount; ++i) {
|
||||||
|
if (m_lastBindOffsets[i] != dynamicOffsets[i]) {
|
||||||
|
identicalBind = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!identicalBind) {
|
||||||
|
vkCmdBindDescriptorSets(commandBuffer, bindPoint, programObj.pipelineLayout, 0, 1,
|
||||||
|
&descriptorSet, offsetCount, dynamicOffsets.data());
|
||||||
|
if (offsetCount <= kMaxShadowedDynamicOffsets) {
|
||||||
|
m_lastBindValid = true;
|
||||||
|
m_lastBindSet = descriptorSet;
|
||||||
|
m_lastBindLayout = programObj.pipelineLayout;
|
||||||
|
m_lastBindPoint = bindPoint;
|
||||||
|
m_lastBindOffsetCount = offsetCount;
|
||||||
|
std::copy_n(dynamicOffsets.data(), offsetCount, m_lastBindOffsets);
|
||||||
|
} else {
|
||||||
|
m_lastBindValid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|
||||||
void BeginFrame(Uint32 frameIndex);
|
void BeginFrame(Uint32 frameIndex);
|
||||||
|
// A command buffer (re)began recording: descriptor bindings recorded into
|
||||||
|
// the previous buffer do not carry over, so drop the bind-dedup shadow.
|
||||||
|
void OnCommandBufferBoundary() { m_lastBindValid = false; }
|
||||||
// A ProgramFactory eviction just destroyed this layout: purge every frame
|
// A ProgramFactory eviction just destroyed this layout: purge every frame
|
||||||
// slot's cached descriptor sets for it, so a recycled handle value can never
|
// slot's cached descriptor sets for it, so a recycled handle value can never
|
||||||
// stale-hit sets written for the dead layout's bindings. The sets are
|
// stale-hit sets written for the dead layout's bindings. The sets are
|
||||||
@@ -185,6 +188,35 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
Uint64 m_lastDescriptorSignature = 0;
|
Uint64 m_lastDescriptorSignature = 0;
|
||||||
Bool m_hasLastDescriptor = false;
|
Bool m_hasLastDescriptor = false;
|
||||||
|
|
||||||
|
// vkCmdBindDescriptorSets dedup: consecutive draws with a static uniform
|
||||||
|
// block resolve to the same set AND the same dynamic offsets, so the
|
||||||
|
// driver call can be skipped outright. Command-buffer-scope state; reset
|
||||||
|
// via OnCommandBufferBoundary whenever a recording (re)begins. Keyed on
|
||||||
|
// layout+bind point, so a pipeline-layout switch always rebinds.
|
||||||
|
static constexpr Uint32 kMaxShadowedDynamicOffsets = 8;
|
||||||
|
Bool m_lastBindValid = false;
|
||||||
|
VkDescriptorSet m_lastBindSet = VK_NULL_HANDLE;
|
||||||
|
VkPipelineLayout m_lastBindLayout = VK_NULL_HANDLE;
|
||||||
|
VkPipelineBindPoint m_lastBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||||
|
Uint32 m_lastBindOffsetCount = 0;
|
||||||
|
Uint32 m_lastBindOffsets[kMaxShadowedDynamicOffsets] = {};
|
||||||
|
|
||||||
|
// Global-UBO transient-slice reuse: MC leaves the default uniform block
|
||||||
|
// untouched across long GUI/terrain runs, so the per-draw re-upload of
|
||||||
|
// the same bytes can reuse the slice uploaded earlier THIS frame (frame
|
||||||
|
// serial guards arena recycling; the content version guards writes).
|
||||||
|
struct GlobalUboSliceMemo {
|
||||||
|
Uint64 programLifetimeId = 0;
|
||||||
|
Uint64 frameSerial = 0;
|
||||||
|
Uint32 uboContentVersion = 0;
|
||||||
|
VkBuffer buffer = VK_NULL_HANDLE;
|
||||||
|
VkDeviceSize offset = 0;
|
||||||
|
VkDeviceSize range = 0;
|
||||||
|
};
|
||||||
|
static constexpr Uint32 kGlobalUboMemoSize = 4;
|
||||||
|
GlobalUboSliceMemo m_globalUboMemo[kGlobalUboMemoSize];
|
||||||
|
Uint32 m_globalUboMemoNext = 0;
|
||||||
|
|
||||||
// Per-binding fast path over VkSamplerManager's content-hashed sampler cache, which
|
// Per-binding fast path over VkSamplerManager's content-hashed sampler cache, which
|
||||||
// stays the source of truth: its key hashes all sampler+texture state, so two distinct
|
// stays the source of truth: its key hashes all sampler+texture state, so two distinct
|
||||||
// sampler objects with identical state still resolve to one VkSampler. This memo only
|
// sampler objects with identical state still resolve to one VkSampler. This memo only
|
||||||
|
|||||||
@@ -58,15 +58,27 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
|
|
||||||
const VertexInputStateFactory::BackendVertexInputState& VertexInputStateFactory::GetOrCreateVertexInputState(
|
const VertexInputStateFactory::BackendVertexInputState& VertexInputStateFactory::GetOrCreateVertexInputState(
|
||||||
const MG_State::GLState::VertexArrayObject& vao) {
|
const MG_State::GLState::VertexArrayObject& vao) {
|
||||||
return GetOrCreateVertexInputState(vao, GetOrComputeHash(vao));
|
// Per-draw fast path: the VAO carries a pointer to its resolved entry,
|
||||||
|
// valid while its config version and the cache's eviction epoch both
|
||||||
|
// match - no re-hash, no map lookup.
|
||||||
|
const void* memoState = nullptr;
|
||||||
|
Uint64 memoEpoch = 0;
|
||||||
|
if (vao.GetBackendStateMemo(memoState, memoEpoch) && memoEpoch == m_evictionEpoch) {
|
||||||
|
const auto* entry = static_cast<const BackendVertexInputState*>(memoState);
|
||||||
|
entry->lastUsedFrameBoundary = m_frameBoundaryCounter;
|
||||||
|
return *entry;
|
||||||
|
}
|
||||||
|
const BackendVertexInputState& entry = GetOrCreateVertexInputState(vao, GetOrComputeHash(vao));
|
||||||
|
vao.SetBackendStateMemo(&entry, m_evictionEpoch);
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
const VertexInputStateFactory::BackendVertexInputState& VertexInputStateFactory::GetOrCreateVertexInputState(
|
const VertexInputStateFactory::BackendVertexInputState& VertexInputStateFactory::GetOrCreateVertexInputState(
|
||||||
const MG_State::GLState::VertexArrayObject& vao, HashType hash) {
|
const MG_State::GLState::VertexArrayObject& vao, HashType hash) {
|
||||||
auto it = m_cache.find(hash);
|
auto it = m_cache.find(hash);
|
||||||
if (it != m_cache.end()) {
|
if (it != m_cache.end()) {
|
||||||
it->second.lastUsedFrameBoundary = m_frameBoundaryCounter;
|
it->second->lastUsedFrameBoundary = m_frameBoundaryCounter;
|
||||||
return it->second;
|
return *it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexInputStateBuilder builder;
|
VertexInputStateBuilder builder;
|
||||||
@@ -172,11 +184,37 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
|
|
||||||
const auto& state = builder.Build();
|
const auto& state = builder.Build();
|
||||||
|
|
||||||
auto& entry = m_cache[hash];
|
auto& slot = m_cache[hash];
|
||||||
|
if (!slot) {
|
||||||
|
slot = MakeUnique<BackendVertexInputState>();
|
||||||
|
}
|
||||||
|
BackendVertexInputState& entry = *slot;
|
||||||
entry.hash = hash;
|
entry.hash = hash;
|
||||||
entry.lastUsedFrameBoundary = m_frameBoundaryCounter;
|
entry.lastUsedFrameBoundary = m_frameBoundaryCounter;
|
||||||
entry.bindings = builder.GetBindings();
|
entry.bindings = builder.GetBindings();
|
||||||
entry.attributes = builder.GetAttributes();
|
entry.attributes = builder.GetAttributes();
|
||||||
|
// See the layoutHash declaration: hash only the resolved layout, never
|
||||||
|
// buffer identities, so identical layouts across VAOs/buffers agree.
|
||||||
|
XXHASH_VERIFY(XXH64_reset(m_hashState, 0));
|
||||||
|
for (const auto& binding : entry.bindings) {
|
||||||
|
XXHASH_VERIFY(XXH64_update(m_hashState, &binding.binding, sizeof(binding.binding)));
|
||||||
|
XXHASH_VERIFY(XXH64_update(m_hashState, &binding.stride, sizeof(binding.stride)));
|
||||||
|
XXHASH_VERIFY(XXH64_update(m_hashState, &binding.inputRate, sizeof(binding.inputRate)));
|
||||||
|
}
|
||||||
|
for (const auto& attribute : entry.attributes) {
|
||||||
|
XXHASH_VERIFY(XXH64_update(m_hashState, &attribute.location, sizeof(attribute.location)));
|
||||||
|
XXHASH_VERIFY(XXH64_update(m_hashState, &attribute.binding, sizeof(attribute.binding)));
|
||||||
|
XXHASH_VERIFY(XXH64_update(m_hashState, &attribute.format, sizeof(attribute.format)));
|
||||||
|
XXHASH_VERIFY(XXH64_update(m_hashState, &attribute.offset, sizeof(attribute.offset)));
|
||||||
|
}
|
||||||
|
XXHASH_VERIFY(XXH64_update(m_hashState, &unsupportedAttribMask, sizeof(unsupportedAttribMask)));
|
||||||
|
entry.layoutHash = XXH64_digest(m_hashState);
|
||||||
|
entry.attributeLocationMask = 0;
|
||||||
|
for (const auto& attribute : entry.attributes) {
|
||||||
|
if (attribute.location < 32u) {
|
||||||
|
entry.attributeLocationMask |= (1u << attribute.location);
|
||||||
|
}
|
||||||
|
}
|
||||||
entry.bindingBufferKeys = std::move(bindingBufferKeys);
|
entry.bindingBufferKeys = std::move(bindingBufferKeys);
|
||||||
entry.bindingBaseOffsets = std::move(bindingBaseOffsets);
|
entry.bindingBaseOffsets = std::move(bindingBaseOffsets);
|
||||||
entry.bindingAttributeLocations = std::move(bindingAttributeLocations);
|
entry.bindingAttributeLocations = std::move(bindingAttributeLocations);
|
||||||
@@ -205,8 +243,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (auto it = m_cache.begin(); it != m_cache.end();) {
|
for (auto it = m_cache.begin(); it != m_cache.end();) {
|
||||||
if (m_frameBoundaryCounter - it->second.lastUsedFrameBoundary > kRetireAgeBoundaries) {
|
if (m_frameBoundaryCounter - it->second->lastUsedFrameBoundary > kRetireAgeBoundaries) {
|
||||||
it = m_cache.erase(it);
|
it = m_cache.erase(it);
|
||||||
|
// Invalidate every VAO's state-pointer memo: the erased node's
|
||||||
|
// address may be reused by a future insert.
|
||||||
|
++m_evictionEpoch;
|
||||||
} else {
|
} else {
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,18 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
|
|
||||||
struct BackendVertexInputState {
|
struct BackendVertexInputState {
|
||||||
HashType hash = 0;
|
HashType hash = 0;
|
||||||
|
// Hash of the resolved Vulkan vertex layout only (bindings, attributes,
|
||||||
|
// unsupported mask) - NO buffer identities. `hash` mixes buffer heap
|
||||||
|
// addresses so per-chunk VBOs mint a fresh identity per buffer; keying
|
||||||
|
// pipelines on that minted one VkPipeline per chunk section for an
|
||||||
|
// identical layout, defeating pipeline reuse and the per-draw memo.
|
||||||
|
// Pipelines depend only on the layout, so they key on this instead.
|
||||||
|
HashType layoutHash = 0;
|
||||||
// Frame boundary of the last cache hit; entries idle past the
|
// Frame boundary of the last cache hit; entries idle past the
|
||||||
// OnFrameBoundary retirement age are evicted (CPU heap only).
|
// OnFrameBoundary retirement age are evicted (CPU heap only).
|
||||||
Uint64 lastUsedFrameBoundary = 0;
|
// Mutable: the VAO's state-pointer memo fast path stamps it through
|
||||||
|
// a const entry reference.
|
||||||
|
mutable Uint64 lastUsedFrameBoundary = 0;
|
||||||
Vector<VkVertexInputBindingDescription> bindings;
|
Vector<VkVertexInputBindingDescription> bindings;
|
||||||
Vector<VkVertexInputAttributeDescription> attributes;
|
Vector<VkVertexInputAttributeDescription> attributes;
|
||||||
Vector<SizeT> bindingBufferKeys;
|
Vector<SizeT> bindingBufferKeys;
|
||||||
@@ -41,6 +50,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
// absent from `attributes`, so without this mask the draw path cannot tell them apart from
|
// absent from `attributes`, so without this mask the draw path cannot tell them apart from
|
||||||
// a genuinely disabled array and would silently feed the shader the current attribute value.
|
// a genuinely disabled array and would silently feed the shader the current attribute value.
|
||||||
Uint32 unsupportedAttribMask = 0;
|
Uint32 unsupportedAttribMask = 0;
|
||||||
|
// Bitmask of `attributes[i].location` - the draw path needs it up to
|
||||||
|
// three times per draw, so it is baked once at build time.
|
||||||
|
Uint32 attributeLocationMask = 0;
|
||||||
VkPipelineVertexInputStateCreateInfo state{
|
VkPipelineVertexInputStateCreateInfo state{
|
||||||
VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
|
VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
|
||||||
};
|
};
|
||||||
@@ -80,9 +92,19 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
|
|
||||||
const VulkanRendererConfig& m_config;
|
const VulkanRendererConfig& m_config;
|
||||||
VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
|
VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
|
||||||
UnorderedMap<HashType, BackendVertexInputState> m_cache;
|
// Values are heap-allocated: FastSTL::unordered_map is open-addressing,
|
||||||
|
// so INSERT invalidates references to stored values. The draw path (and
|
||||||
|
// the VAOs' state-pointer memos) hold entry pointers across inserts;
|
||||||
|
// only the unique_ptr cell moves, never the pointee.
|
||||||
|
UnorderedMap<HashType, UniquePtr<BackendVertexInputState>> m_cache;
|
||||||
// Monotonic frame-boundary counter (bumped in OnFrameBoundary) for cache aging.
|
// Monotonic frame-boundary counter (bumped in OnFrameBoundary) for cache aging.
|
||||||
Uint64 m_frameBoundaryCounter = 0;
|
Uint64 m_frameBoundaryCounter = 0;
|
||||||
|
// Bumped whenever any cache entry is erased. VAOs memo a raw pointer to
|
||||||
|
// their heap-allocated entry (stable across map insert/rehash by
|
||||||
|
// construction); a memo is honored only while its recorded epoch
|
||||||
|
// matches, so an evicted entry can never be dereferenced through a
|
||||||
|
// stale memo.
|
||||||
|
Uint64 m_evictionEpoch = 1;
|
||||||
static inline XXH64_state_t* m_hashState = XXH64_createState();
|
static inline XXH64_state_t* m_hashState = XXH64_createState();
|
||||||
};
|
};
|
||||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
const std::lock_guard<std::mutex> lock(m_mutex);
|
const std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
m_pendingClears.clear();
|
m_pendingClears.clear();
|
||||||
m_aliveObjects.clear();
|
m_aliveObjects.clear();
|
||||||
|
m_pendingCount.store(static_cast<Uint32>(m_pendingClears.size()), std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
TextureIdentity VkClearManager::MakeTextureIdentity(MG_State::GLState::ITextureObject* texture) {
|
TextureIdentity VkClearManager::MakeTextureIdentity(MG_State::GLState::ITextureObject* texture) {
|
||||||
@@ -127,6 +128,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
m_pendingClears.erase(key);
|
m_pendingClears.erase(key);
|
||||||
}
|
}
|
||||||
m_aliveObjects.erase(identity);
|
m_aliveObjects.erase(identity);
|
||||||
|
m_pendingCount.store(static_cast<Uint32>(m_pendingClears.size()), std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
Bool VkClearManager::LockTextureIdentityLocked(const TextureIdentity& identity,
|
Bool VkClearManager::LockTextureIdentityLocked(const TextureIdentity& identity,
|
||||||
@@ -221,6 +223,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
m_aliveObjects[MakeTextureIdentity(texture.get())] = texture;
|
m_aliveObjects[MakeTextureIdentity(texture.get())] = texture;
|
||||||
auto& pending = m_pendingClears[key];
|
auto& pending = m_pendingClears[key];
|
||||||
MergeClearPayload(pending, clearPayload);
|
MergeClearPayload(pending, clearPayload);
|
||||||
|
m_pendingCount.store(static_cast<Uint32>(m_pendingClears.size()), std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VkClearManager::QueueClear(const ClearAttachmentPayload& clearPayload,
|
void VkClearManager::QueueClear(const ClearAttachmentPayload& clearPayload,
|
||||||
@@ -238,6 +241,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
m_aliveObjects[MakeTextureIdentity(texture.get())] = texture;
|
m_aliveObjects[MakeTextureIdentity(texture.get())] = texture;
|
||||||
auto& pending = m_pendingClears[key];
|
auto& pending = m_pendingClears[key];
|
||||||
MergeClearPayload(pending, clearPayload);
|
MergeClearPayload(pending, clearPayload);
|
||||||
|
m_pendingCount.store(static_cast<Uint32>(m_pendingClears.size()), std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
Bool VkClearManager::HasPendingClear(MG_State::GLState::ITextureObject* texture) {
|
Bool VkClearManager::HasPendingClear(MG_State::GLState::ITextureObject* texture) {
|
||||||
@@ -245,6 +249,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_pendingCount.load(std::memory_order_relaxed) == 0) {
|
||||||
|
return false; // per-draw hot path: nothing pending anywhere
|
||||||
|
}
|
||||||
|
|
||||||
const Uint64 lifetimeId = texture->GetLifetimeId();
|
const Uint64 lifetimeId = texture->GetLifetimeId();
|
||||||
const std::lock_guard<std::mutex> lock(m_mutex);
|
const std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
for (auto it = m_pendingClears.begin(); it != m_pendingClears.end(); ++it) {
|
for (auto it = m_pendingClears.begin(); it != m_pendingClears.end(); ++it) {
|
||||||
@@ -260,6 +268,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
if (key.texture == nullptr) {
|
if (key.texture == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (m_pendingCount.load(std::memory_order_relaxed) == 0) {
|
||||||
|
return false; // per-draw hot path: nothing pending anywhere
|
||||||
|
}
|
||||||
|
|
||||||
const std::lock_guard<std::mutex> lock(m_mutex);
|
const std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
if (m_pendingClears.find(key) == m_pendingClears.end()) {
|
if (m_pendingClears.find(key) == m_pendingClears.end()) {
|
||||||
@@ -287,6 +298,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
if (key.texture == nullptr) {
|
if (key.texture == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (m_pendingCount.load(std::memory_order_relaxed) == 0) {
|
||||||
|
return false; // per-draw hot path: nothing pending anywhere
|
||||||
|
}
|
||||||
|
|
||||||
const std::lock_guard<std::mutex> lock(m_mutex);
|
const std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
if (!LockTextureLocked(key, outTexture)) {
|
if (!LockTextureLocked(key, outTexture)) {
|
||||||
@@ -325,6 +339,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
if (texture == nullptr) {
|
if (texture == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (m_pendingCount.load(std::memory_order_relaxed) == 0) {
|
||||||
|
return false; // per-draw hot path: nothing pending anywhere
|
||||||
|
}
|
||||||
|
|
||||||
const Uint64 lifetimeId = texture->GetLifetimeId();
|
const Uint64 lifetimeId = texture->GetLifetimeId();
|
||||||
const std::lock_guard<std::mutex> lock(m_mutex);
|
const std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
@@ -345,6 +362,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_pendingCount.load(std::memory_order_relaxed) == 0) {
|
||||||
|
return; // per-draw hot path: nothing pending anywhere
|
||||||
|
}
|
||||||
const TextureIdentity identity = MakeTextureIdentity(texture);
|
const TextureIdentity identity = MakeTextureIdentity(texture);
|
||||||
MGLOG_D("%s: Pop all pending clears for texture %d", __func__, texture->GetExternalIndex());
|
MGLOG_D("%s: Pop all pending clears for texture %d", __func__, texture->GetExternalIndex());
|
||||||
const std::lock_guard<std::mutex> lock(m_mutex);
|
const std::lock_guard<std::mutex> lock(m_mutex);
|
||||||
@@ -361,6 +381,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
auto it = m_pendingClears.find(key);
|
auto it = m_pendingClears.find(key);
|
||||||
if (it != m_pendingClears.end()) {
|
if (it != m_pendingClears.end()) {
|
||||||
m_pendingClears.erase(it);
|
m_pendingClears.erase(it);
|
||||||
|
m_pendingCount.store(static_cast<Uint32>(m_pendingClears.size()), std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include "MG_Util/Math/VectorTypes.h"
|
#include "MG_Util/Math/VectorTypes.h"
|
||||||
|
|
||||||
#include <Includes.h>
|
#include <Includes.h>
|
||||||
|
#include <atomic>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace MobileGL::MG_Backend::DirectVulkan {
|
namespace MobileGL::MG_Backend::DirectVulkan {
|
||||||
@@ -120,7 +121,19 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
SharedPtr<MG_State::GLState::ITextureObject>& outTexture);
|
SharedPtr<MG_State::GLState::ITextureObject>& outTexture);
|
||||||
|
|
||||||
Uint8 m_gcCounter = 0;
|
Uint8 m_gcCounter = 0;
|
||||||
|
public:
|
||||||
|
// Lock-free probe for the consecutive-draw fast path: any pending clear
|
||||||
|
// forces the full SetupDraw path (which materializes/consumes it).
|
||||||
|
Bool HasAnyPendingClears() const { return m_pendingCount.load(std::memory_order_relaxed) != 0; }
|
||||||
|
|
||||||
|
private:
|
||||||
mutable std::mutex m_mutex;
|
mutable std::mutex m_mutex;
|
||||||
|
// Lock-free mirror of m_pendingClears.size(), maintained under m_mutex
|
||||||
|
// by every mutation. The per-draw probes (HasPendingClear/GetPending*)
|
||||||
|
// read it before taking the lock: during draw batches the pending set
|
||||||
|
// is almost always empty, so this turns several locked map probes per
|
||||||
|
// draw into one relaxed load.
|
||||||
|
std::atomic<Uint32> m_pendingCount{0};
|
||||||
std::unordered_map<PendingClearKey, ClearAttachmentPayload, PendingClearKeyHash> m_pendingClears;
|
std::unordered_map<PendingClearKey, ClearAttachmentPayload, PendingClearKeyHash> m_pendingClears;
|
||||||
std::unordered_map<TextureIdentity, WeakPtr<MG_State::GLState::ITextureObject>, TextureIdentityHash> m_aliveObjects;
|
std::unordered_map<TextureIdentity, WeakPtr<MG_State::GLState::ITextureObject>, TextureIdentityHash> m_aliveObjects;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -966,6 +966,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
trackedAttachmentLayouts.emplace_back(TrackedAttachmentLayoutInfo {
|
trackedAttachmentLayouts.emplace_back(TrackedAttachmentLayoutInfo {
|
||||||
.target = TrackedAttachmentTarget::Texture,
|
.target = TrackedAttachmentTarget::Texture,
|
||||||
.texture = att.GetTexture(),
|
.texture = att.GetTexture(),
|
||||||
|
.textureRaw = att.GetTexture().get(),
|
||||||
.textureMipLevel = attachmentMipLevel,
|
.textureMipLevel = attachmentMipLevel,
|
||||||
.finalLayout = desc.finalLayout,
|
.finalLayout = desc.finalLayout,
|
||||||
});
|
});
|
||||||
@@ -1142,6 +1143,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
trackedAttachmentLayouts.emplace_back(TrackedAttachmentLayoutInfo {
|
trackedAttachmentLayouts.emplace_back(TrackedAttachmentLayoutInfo {
|
||||||
.target = TrackedAttachmentTarget::Texture,
|
.target = TrackedAttachmentTarget::Texture,
|
||||||
.texture = selectedDepthStencilAttachment->GetTexture(),
|
.texture = selectedDepthStencilAttachment->GetTexture(),
|
||||||
|
.textureRaw = selectedDepthStencilAttachment->GetTexture().get(),
|
||||||
.textureMipLevel = attachmentMipLevel,
|
.textureMipLevel = attachmentMipLevel,
|
||||||
.finalLayout = depthAttachmentDescription.finalLayout,
|
.finalLayout = depthAttachmentDescription.finalLayout,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -42,6 +42,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
struct TrackedAttachmentLayoutInfo {
|
struct TrackedAttachmentLayoutInfo {
|
||||||
TrackedAttachmentTarget target = TrackedAttachmentTarget::Texture;
|
TrackedAttachmentTarget target = TrackedAttachmentTarget::Texture;
|
||||||
WeakPtr<MG_State::GLState::ITextureObject> texture;
|
WeakPtr<MG_State::GLState::ITextureObject> texture;
|
||||||
|
// Identity-compare shortcut for the per-draw "does the active pass use
|
||||||
|
// this sampled texture" probe: comparing this against a LIVE texture's
|
||||||
|
// address needs no weak_ptr::lock (two refcount atomics per probe).
|
||||||
|
// May dangle once the texture dies - compare only, never dereference.
|
||||||
|
MG_State::GLState::ITextureObject* textureRaw = nullptr;
|
||||||
WeakPtr<MG_State::GLState::RenderbufferObject> renderbuffer;
|
WeakPtr<MG_State::GLState::RenderbufferObject> renderbuffer;
|
||||||
Uint32 textureMipLevel = 0;
|
Uint32 textureMipLevel = 0;
|
||||||
Uint32 swapchainImageIndex = 0;
|
Uint32 swapchainImageIndex = 0;
|
||||||
@@ -233,6 +238,13 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
// image recreation.
|
// image recreation.
|
||||||
Uint64 m_renderbufferImageEpoch = 1;
|
Uint64 m_renderbufferImageEpoch = 1;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Bumped whenever a renderbuffer backing is (re)created; consecutive-draw
|
||||||
|
// snapshots include it so an attachment respecify forces a re-resolve.
|
||||||
|
Uint64 GetRenderbufferImageEpoch() const { return m_renderbufferImageEpoch; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
// Per-draw fast-path memo for GetOrCreateRenderPass (dirty-flag state tracking): when the
|
// Per-draw fast-path memo for GetOrCreateRenderPass (dirty-flag state tracking): when the
|
||||||
// framebuffer state is provably unchanged since the last resolution, the active render pass
|
// framebuffer state is provably unchanged since the last resolution, the active render pass
|
||||||
// is reused WITHOUT recomputing the expensive per-draw hash. Invalidated by FBO switch /
|
// is reused WITHOUT recomputing the expensive per-draw hash. Invalidated by FBO switch /
|
||||||
|
|||||||
@@ -607,7 +607,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VkTextureManager::Shutdown() {
|
void VkTextureManager::Shutdown() {
|
||||||
|
if (m_device != VK_NULL_HANDLE) {
|
||||||
|
ReclaimCompletedUploads(/*waitAll=*/true);
|
||||||
|
}
|
||||||
DestroyDeferredReleases();
|
DestroyDeferredReleases();
|
||||||
|
++m_resourceEraseEpoch; // every memoized resource pointer dies with the map
|
||||||
m_textureResources.clear();
|
m_textureResources.clear();
|
||||||
m_aliveObjects.clear();
|
m_aliveObjects.clear();
|
||||||
m_storageImageTextures.clear();
|
m_storageImageTextures.clear();
|
||||||
@@ -629,6 +633,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
frameIndex, m_deferredViewReleases.size());
|
frameIndex, m_deferredViewReleases.size());
|
||||||
m_currentFrameIndex = frameIndex;
|
m_currentFrameIndex = frameIndex;
|
||||||
CollectDeferredReleases(frameIndex);
|
CollectDeferredReleases(frameIndex);
|
||||||
|
ReclaimCompletedUploads();
|
||||||
|
|
||||||
// Frame-boundary GC: every 64 frame boundaries (~1 s at 60 fps) bounds the reclaim
|
// Frame-boundary GC: every 64 frame boundaries (~1 s at 60 fps) bounds the reclaim
|
||||||
// latency for dead textures regardless of draw traffic — workloads that churn
|
// latency for dead textures regardless of draw traffic — workloads that churn
|
||||||
@@ -659,6 +664,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
}
|
}
|
||||||
m_aliveObjects.erase(identity);
|
m_aliveObjects.erase(identity);
|
||||||
m_storageImageTextures.erase(identity);
|
m_storageImageTextures.erase(identity);
|
||||||
|
// Invalidate every cross-draw sampled-texture memo: the erased
|
||||||
|
// resource's address may be reused by a future emplace.
|
||||||
|
++m_resourceEraseEpoch;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VkTextureManager::PruneStaleTextureAliases(MG_State::GLState::ITextureObject* texture) {
|
void VkTextureManager::PruneStaleTextureAliases(MG_State::GLState::ITextureObject* texture) {
|
||||||
@@ -714,45 +722,63 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto aliveIt = m_aliveObjects.find(identity);
|
// Cross-draw memo probe (see SyncedTextureMemoEntry): skips both map
|
||||||
if (aliveIt != m_aliveObjects.end() && aliveIt->second.expired()) {
|
// lookups and the (re)registration path for repeat-bound textures.
|
||||||
EraseTrackedTexture(aliveIt->first);
|
TextureResource* resourcePtr = nullptr;
|
||||||
aliveIt = m_aliveObjects.end();
|
for (Uint32 i = 0; i < kSyncedTextureMemoSize; ++i) {
|
||||||
}
|
const SyncedTextureMemoEntry& memo = m_syncedTextureMemo[i];
|
||||||
|
if (memo.texture == &texture && memo.lifetimeId == identity.lifetimeId &&
|
||||||
// Only (re)register and prune when this (texture, lifetime) pair is new: stale
|
memo.eraseEpoch == m_resourceEraseEpoch) {
|
||||||
// aliases can only come into existence through an address reuse, which by
|
resourcePtr = memo.resource;
|
||||||
// construction introduces a new identity. Doing this unconditionally made every
|
break;
|
||||||
// sampled-texture sync scan the entire alive-texture map per draw.
|
|
||||||
if (aliveIt == m_aliveObjects.end()) {
|
|
||||||
WeakPtr<MG_State::GLState::ITextureObject> aliveTexture;
|
|
||||||
const auto& liveTexture = MG_State::pGLContext->GetTextureObject(texture.GetExternalIndex());
|
|
||||||
if (liveTexture && liveTexture.get() == &texture) {
|
|
||||||
aliveTexture = liveTexture;
|
|
||||||
} else {
|
|
||||||
// The name lookup legally fails while the object is alive: the name was
|
|
||||||
// deleted with the texture still attached to an FBO (the attachment's
|
|
||||||
// SharedPtr keeps it alive), or the name was reused by a new texture, or
|
|
||||||
// this is a default texture object (name 0 lives outside the name map).
|
|
||||||
// Register through the object's own control block so the resource created
|
|
||||||
// below still participates in weak-expiry GC instead of becoming an
|
|
||||||
// orphan no reclamation path can reach until Shutdown.
|
|
||||||
aliveTexture = texture.weak_from_this();
|
|
||||||
}
|
|
||||||
if (!aliveTexture.expired()) {
|
|
||||||
m_aliveObjects[identity] = Move(aliveTexture);
|
|
||||||
PruneStaleTextureAliases(&texture);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto it = m_textureResources.find(identity);
|
if (resourcePtr == nullptr) {
|
||||||
if (it == m_textureResources.end()) {
|
auto aliveIt = m_aliveObjects.find(identity);
|
||||||
TextureResource initial{};
|
if (aliveIt != m_aliveObjects.end() && aliveIt->second.expired()) {
|
||||||
auto [insertIt, _] = m_textureResources.emplace(identity, Move(initial));
|
EraseTrackedTexture(aliveIt->first);
|
||||||
it = insertIt;
|
aliveIt = m_aliveObjects.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only (re)register and prune when this (texture, lifetime) pair is new: stale
|
||||||
|
// aliases can only come into existence through an address reuse, which by
|
||||||
|
// construction introduces a new identity. Doing this unconditionally made every
|
||||||
|
// sampled-texture sync scan the entire alive-texture map per draw.
|
||||||
|
if (aliveIt == m_aliveObjects.end()) {
|
||||||
|
WeakPtr<MG_State::GLState::ITextureObject> aliveTexture;
|
||||||
|
const auto& liveTexture = MG_State::pGLContext->GetTextureObject(texture.GetExternalIndex());
|
||||||
|
if (liveTexture && liveTexture.get() == &texture) {
|
||||||
|
aliveTexture = liveTexture;
|
||||||
|
} else {
|
||||||
|
// The name lookup legally fails while the object is alive: the name was
|
||||||
|
// deleted with the texture still attached to an FBO (the attachment's
|
||||||
|
// SharedPtr keeps it alive), or the name was reused by a new texture, or
|
||||||
|
// this is a default texture object (name 0 lives outside the name map).
|
||||||
|
// Register through the object's own control block so the resource created
|
||||||
|
// below still participates in weak-expiry GC instead of becoming an
|
||||||
|
// orphan no reclamation path can reach until Shutdown.
|
||||||
|
aliveTexture = texture.weak_from_this();
|
||||||
|
}
|
||||||
|
if (!aliveTexture.expired()) {
|
||||||
|
m_aliveObjects[identity] = Move(aliveTexture);
|
||||||
|
PruneStaleTextureAliases(&texture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto it = m_textureResources.find(identity);
|
||||||
|
if (it == m_textureResources.end()) {
|
||||||
|
TextureResource initial{};
|
||||||
|
auto [insertIt, _] = m_textureResources.emplace(identity, Move(initial));
|
||||||
|
it = insertIt;
|
||||||
|
}
|
||||||
|
resourcePtr = &(it->second);
|
||||||
|
m_syncedTextureMemo[m_syncedTextureMemoNext] =
|
||||||
|
SyncedTextureMemoEntry{&texture, identity.lifetimeId, m_resourceEraseEpoch, resourcePtr};
|
||||||
|
m_syncedTextureMemoNext = (m_syncedTextureMemoNext + 1) % kSyncedTextureMemoSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!SyncTexture(texture, it->second)) {
|
if (!SyncTexture(texture, *resourcePtr)) {
|
||||||
MGLOG_D("%s: Syncing texture %d failed", __func__, texture.GetExternalIndex());
|
MGLOG_D("%s: Syncing texture %d failed", __func__, texture.GetExternalIndex());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -766,11 +792,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!recorded) {
|
if (!recorded) {
|
||||||
m_drawSyncedThisDraw.push_back({identity, &(it->second)});
|
m_drawSyncedThisDraw.push_back({identity, resourcePtr});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &(it->second);
|
return resourcePtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
VkImageView VkTextureManager::GetOrCreateViewAtMipLevel(MG_State::GLState::ITextureObject& texture, Uint32 mipLevel) {
|
VkImageView VkTextureManager::GetOrCreateViewAtMipLevel(MG_State::GLState::ITextureObject& texture, Uint32 mipLevel) {
|
||||||
@@ -1708,6 +1734,28 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
m_deferredViewReleases[frameIndex].clear();
|
m_deferredViewReleases[frameIndex].clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VkTextureManager::ReclaimCompletedUploads(Bool waitAll) {
|
||||||
|
if (m_pendingUploadReclaims.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SizeT completed = 0;
|
||||||
|
for (; completed < m_pendingUploadReclaims.size(); ++completed) {
|
||||||
|
PendingUploadReclaim& entry = m_pendingUploadReclaims[completed];
|
||||||
|
if (waitAll) {
|
||||||
|
VK_VERIFY(vkWaitForFences(m_device, 1, &entry.fence, VK_TRUE, UINT64_MAX),
|
||||||
|
"vkWaitForFences(texture upload reclaim)");
|
||||||
|
} else if (vkGetFenceStatus(m_device, entry.fence) != VK_SUCCESS) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
vkDestroyFence(m_device, entry.fence, nullptr);
|
||||||
|
vkFreeCommandBuffers(m_device, m_commandPool, 1, &entry.commandBuffer);
|
||||||
|
vmaDestroyBuffer(m_allocator, entry.stagingBuffer, entry.stagingAllocation);
|
||||||
|
}
|
||||||
|
m_pendingUploadReclaims.erase(m_pendingUploadReclaims.begin(),
|
||||||
|
m_pendingUploadReclaims.begin() + static_cast<std::ptrdiff_t>(completed));
|
||||||
|
}
|
||||||
|
|
||||||
void VkTextureManager::DestroyDeferredReleases() {
|
void VkTextureManager::DestroyDeferredReleases() {
|
||||||
for (auto& deferredReleases : m_deferredReleases) {
|
for (auto& deferredReleases : m_deferredReleases) {
|
||||||
deferredReleases.clear();
|
deferredReleases.clear();
|
||||||
@@ -2014,11 +2062,23 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
VK_VERIFY(vkCreateFence(m_device, &fenceInfo, nullptr, &uploadFence), "vkCreateFence(texture upload)");
|
VK_VERIFY(vkCreateFence(m_device, &fenceInfo, nullptr, &uploadFence), "vkCreateFence(texture upload)");
|
||||||
|
|
||||||
VK_VERIFY(vkQueueSubmit(m_graphicsQueue, 1, &submitInfo, uploadFence), "vkQueueSubmit(texture)");
|
VK_VERIFY(vkQueueSubmit(m_graphicsQueue, 1, &submitInfo, uploadFence), "vkQueueSubmit(texture)");
|
||||||
VK_VERIFY(vkWaitForFences(m_device, 1, &uploadFence, VK_TRUE, UINT64_MAX), "vkWaitForFences(texture upload)");
|
// Do NOT wait the fence here: this submit sits behind the previous
|
||||||
vkDestroyFence(m_device, uploadFence, nullptr);
|
// frame's rendering on the queue, so a synchronous wait stalls the CPU
|
||||||
vkFreeCommandBuffers(m_device, m_commandPool, 1, &commandBuffer);
|
// until the GPU drains - a per-frame vkQueueWaitIdle for any workload
|
||||||
|
// with animated textures. Ordering against the current frame's draws is
|
||||||
vmaDestroyBuffer(m_allocator, stagingBuffer, stagingAllocation);
|
// already guaranteed (its command buffer is submitted later, at
|
||||||
|
// present), so only the transient objects need to survive execution;
|
||||||
|
// park them until the fence signals.
|
||||||
|
m_pendingUploadReclaims.push_back({uploadFence, commandBuffer, stagingBuffer, stagingAllocation});
|
||||||
|
ReclaimCompletedUploads();
|
||||||
|
// Backstop for pathological upload storms: bound in-flight staging
|
||||||
|
// memory by blocking on the oldest upload only once the list is deep.
|
||||||
|
constexpr SizeT kMaxPendingTextureUploads = 16;
|
||||||
|
if (m_pendingUploadReclaims.size() > kMaxPendingTextureUploads) {
|
||||||
|
VK_VERIFY(vkWaitForFences(m_device, 1, &m_pendingUploadReclaims.front().fence, VK_TRUE, UINT64_MAX),
|
||||||
|
"vkWaitForFences(texture upload backstop)");
|
||||||
|
ReclaimCompletedUploads();
|
||||||
|
}
|
||||||
|
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
MGLOG_D("%s: texture upload cmd failed", __func__);
|
MGLOG_D("%s: texture upload cmd failed", __func__);
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ public:
|
|||||||
// manager keys its per-draw fast path on this so an attachment's image recreation
|
// manager keys its per-draw fast path on this so an attachment's image recreation
|
||||||
// invalidates the cached render pass (dirty-flag tracking; portable to Vulkan 1.1).
|
// invalidates the cached render pass (dirty-flag tracking; portable to Vulkan 1.1).
|
||||||
Uint64 GetTextureImageEpoch() const { return m_textureImageEpoch; }
|
Uint64 GetTextureImageEpoch() const { return m_textureImageEpoch; }
|
||||||
|
// Bumped whenever any tracked texture resource is erased; cached
|
||||||
|
// TextureResource pointers are valid only while this is unchanged.
|
||||||
|
Uint64 GetResourceEraseEpoch() const { return m_resourceEraseEpoch; }
|
||||||
|
|
||||||
struct TextureIdentity {
|
struct TextureIdentity {
|
||||||
MG_State::GLState::ITextureObject* texture = nullptr;
|
MG_State::GLState::ITextureObject* texture = nullptr;
|
||||||
@@ -420,6 +423,11 @@ private:
|
|||||||
void DeferViewRelease(VkImageView view);
|
void DeferViewRelease(VkImageView view);
|
||||||
void CollectDeferredReleases(Uint32 frameIndex);
|
void CollectDeferredReleases(Uint32 frameIndex);
|
||||||
void DestroyDeferredReleases();
|
void DestroyDeferredReleases();
|
||||||
|
// Frees the fence/command buffer/staging buffer of every in-flight texture
|
||||||
|
// upload whose fence has signaled (submission order = completion order on
|
||||||
|
// the single queue, so the scan stops at the first still-pending entry).
|
||||||
|
// waitAll blocks on every entry - Shutdown's drain.
|
||||||
|
void ReclaimCompletedUploads(Bool waitAll = false);
|
||||||
static TextureIdentity MakeTextureIdentity(MG_State::GLState::ITextureObject* texture);
|
static TextureIdentity MakeTextureIdentity(MG_State::GLState::ITextureObject* texture);
|
||||||
void EraseTrackedTexture(const TextureIdentity& identity);
|
void EraseTrackedTexture(const TextureIdentity& identity);
|
||||||
void PruneStaleTextureAliases(MG_State::GLState::ITextureObject* texture);
|
void PruneStaleTextureAliases(MG_State::GLState::ITextureObject* texture);
|
||||||
@@ -449,6 +457,23 @@ private:
|
|||||||
TextureResource* resource = nullptr;
|
TextureResource* resource = nullptr;
|
||||||
};
|
};
|
||||||
Vector<DrawSyncedTexture> m_drawSyncedThisDraw;
|
Vector<DrawSyncedTexture> m_drawSyncedThisDraw;
|
||||||
|
// Cross-draw sampled-texture memo: the same few textures (atlas, lightmap)
|
||||||
|
// are resolved on every draw, so cache their resource pointers and skip the
|
||||||
|
// alive/resource map lookups. Node-based std::unordered_map keeps the
|
||||||
|
// pointees stable across inserts; erases bump m_resourceEraseEpoch, which
|
||||||
|
// every memo entry must match. SyncTexture still runs on memo hits, so
|
||||||
|
// content/param freshness is unaffected. A dead-then-reused texture address
|
||||||
|
// cannot false-hit: the new object carries a new lifetime id.
|
||||||
|
struct SyncedTextureMemoEntry {
|
||||||
|
const MG_State::GLState::ITextureObject* texture = nullptr;
|
||||||
|
Uint64 lifetimeId = 0;
|
||||||
|
Uint64 eraseEpoch = 0;
|
||||||
|
TextureResource* resource = nullptr;
|
||||||
|
};
|
||||||
|
static constexpr Uint32 kSyncedTextureMemoSize = 8;
|
||||||
|
SyncedTextureMemoEntry m_syncedTextureMemo[kSyncedTextureMemoSize];
|
||||||
|
Uint32 m_syncedTextureMemoNext = 0;
|
||||||
|
Uint64 m_resourceEraseEpoch = 1;
|
||||||
// Formats whose mutable-image probe failed on this device; their images are created
|
// Formats whose mutable-image probe failed on this device; their images are created
|
||||||
// without MUTABLE_FORMAT_BIT so repeat syncs neither re-probe nor flag-mismatch.
|
// without MUTABLE_FORMAT_BIT so repeat syncs neither re-probe nor flag-mismatch.
|
||||||
std::unordered_set<VkFormat> m_mutableFormatUnsupported;
|
std::unordered_set<VkFormat> m_mutableFormatUnsupported;
|
||||||
@@ -458,5 +483,16 @@ private:
|
|||||||
std::unordered_set<TextureIdentity, TextureIdentityHash> m_storageImageTextures;
|
std::unordered_set<TextureIdentity, TextureIdentityHash> m_storageImageTextures;
|
||||||
Vector<Vector<TextureResource>> m_deferredReleases;
|
Vector<Vector<TextureResource>> m_deferredReleases;
|
||||||
Vector<Vector<VkImageView>> m_deferredViewReleases;
|
Vector<Vector<VkImageView>> m_deferredViewReleases;
|
||||||
|
// Texture uploads are submitted out-of-band but NOT waited on (waiting
|
||||||
|
// behind the queue serialized the CPU against the previous frame's GPU
|
||||||
|
// work every time an animated atlas re-uploaded). Their transient objects
|
||||||
|
// are parked here and reclaimed once the upload fence signals.
|
||||||
|
struct PendingUploadReclaim {
|
||||||
|
VkFence fence = VK_NULL_HANDLE;
|
||||||
|
VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
|
||||||
|
VkBuffer stagingBuffer = VK_NULL_HANDLE;
|
||||||
|
VmaAllocation stagingAllocation = nullptr;
|
||||||
|
};
|
||||||
|
Vector<PendingUploadReclaim> m_pendingUploadReclaims;
|
||||||
};
|
};
|
||||||
} // namespace MobileGL::MG_Backend::DirectVulkan
|
} // namespace MobileGL::MG_Backend::DirectVulkan
|
||||||
|
|||||||
@@ -226,6 +226,26 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
// set (blit, depth-mipmap) binds - their static state makes the
|
// set (blit, depth-mipmap) binds - their static state makes the
|
||||||
// corresponding dynamic values undefined per the spec.
|
// corresponding dynamic values undefined per the spec.
|
||||||
struct DynamicStateShadow {
|
struct DynamicStateShadow {
|
||||||
|
// Last graphics pipeline bound on the frame command buffer. Pipeline
|
||||||
|
// binds are command-buffer state (they survive render-pass boundaries),
|
||||||
|
// so the same reset points that invalidate dynamic state - recording
|
||||||
|
// (re)begin and the aux blit pipelines' raw binds - are exactly the
|
||||||
|
// points where this becomes unknown.
|
||||||
|
Bool graphicsPipelineValid = false;
|
||||||
|
VkPipeline graphicsPipeline = VK_NULL_HANDLE;
|
||||||
|
// Index/vertex buffer binds are command-buffer state too. Terrain
|
||||||
|
// sections and GUI quads share one sequential index buffer, and GUI
|
||||||
|
// batches often reuse a vertex arena buffer, so skipping identical
|
||||||
|
// rebinds removes a large share of per-draw driver calls.
|
||||||
|
Bool indexBindValid = false;
|
||||||
|
VkBuffer indexBuffer = VK_NULL_HANDLE;
|
||||||
|
VkDeviceSize indexOffset = 0;
|
||||||
|
VkIndexType indexType = VK_INDEX_TYPE_MAX_ENUM;
|
||||||
|
static constexpr Uint32 kMaxShadowedVertexBindings = 8;
|
||||||
|
Bool vertexBindValid = false;
|
||||||
|
Uint32 vertexBindingCount = 0;
|
||||||
|
VkBuffer vertexBuffers[kMaxShadowedVertexBindings] = {};
|
||||||
|
VkDeviceSize vertexOffsets[kMaxShadowedVertexBindings] = {};
|
||||||
Bool viewportValid = false;
|
Bool viewportValid = false;
|
||||||
VkViewport viewport{};
|
VkViewport viewport{};
|
||||||
Bool scissorValid = false;
|
Bool scissorValid = false;
|
||||||
@@ -815,16 +835,6 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
static_assert(kMaxVertexAttribs <= ProgramFactory::VkProgramObject::kMaxVertexInputLocations,
|
static_assert(kMaxVertexAttribs <= ProgramFactory::VkProgramObject::kMaxVertexInputLocations,
|
||||||
"vertexInputTypes is indexed by vertex attribute location");
|
"vertexInputTypes is indexed by vertex attribute location");
|
||||||
|
|
||||||
static Uint32 BuildVertexInputAttributeMask(const Vector<VkVertexInputAttributeDescription>& attributes) {
|
|
||||||
Uint32 attributeMask = 0;
|
|
||||||
for (const auto& attribute : attributes) {
|
|
||||||
if (attribute.location < kMaxVertexAttribs) {
|
|
||||||
attributeMask |= (1u << attribute.location);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return attributeMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Bool TryGetCurrentVertexAttributeFormat(GLenum glType, VkFormat& outFormat) {
|
static Bool TryGetCurrentVertexAttributeFormat(GLenum glType, VkFormat& outFormat) {
|
||||||
switch (glType) {
|
switch (glType) {
|
||||||
case GL_FLOAT:
|
case GL_FLOAT:
|
||||||
@@ -975,8 +985,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
if (trackedAttachment.target != TrackedAttachmentTarget::Texture) {
|
if (trackedAttachment.target != TrackedAttachmentTarget::Texture) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const auto trackedTexture = trackedAttachment.texture.lock();
|
// Raw identity compare (see textureRaw): the caller's texture is
|
||||||
if (trackedTexture && trackedTexture.get() == &texture) {
|
// live, so a dangling tracked pointer can never equal its address
|
||||||
|
// unless the allocator reused it - and that false positive merely
|
||||||
|
// ends the render pass early, never misses a genuine use.
|
||||||
|
if (trackedAttachment.textureRaw == &texture) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2911,7 +2924,7 @@ void main() {
|
|||||||
// the GetCurrentProgram + GetOrCreateProgram hash lookup every draw.
|
// the GetCurrentProgram + GetOrCreateProgram hash lookup every draw.
|
||||||
auto& vertexInputState = m_vertexInputStateFactory->GetOrCreateVertexInputState(vao);
|
auto& vertexInputState = m_vertexInputStateFactory->GetOrCreateVertexInputState(vao);
|
||||||
const Uint32 activeAttribMask = programObj.activeVertexInputLocationMask;
|
const Uint32 activeAttribMask = programObj.activeVertexInputLocationMask;
|
||||||
const Uint32 vertexInputAttribMask = BuildVertexInputAttributeMask(vertexInputState.attributes);
|
const Uint32 vertexInputAttribMask = vertexInputState.attributeLocationMask;
|
||||||
const Uint32 missingAttribMask = activeAttribMask & ~vertexInputAttribMask;
|
const Uint32 missingAttribMask = activeAttribMask & ~vertexInputAttribMask;
|
||||||
|
|
||||||
const auto bindingCount = vertexInputState.bindings.size() + static_cast<SizeT>(std::popcount(missingAttribMask));
|
const auto bindingCount = vertexInputState.bindings.size() + static_cast<SizeT>(std::popcount(missingAttribMask));
|
||||||
@@ -3176,8 +3189,29 @@ void main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (bindingCount > 0) {
|
if (bindingCount > 0) {
|
||||||
vkCmdBindVertexBuffers(commandBuffer, 0, static_cast<Uint32>(bindingCount), vkBuffers.data(),
|
auto& shadow = g_dynamicStateShadow;
|
||||||
vkOffsets.data());
|
const Uint32 count = static_cast<Uint32>(bindingCount);
|
||||||
|
Bool identical = shadow.vertexBindValid && shadow.vertexBindingCount == count &&
|
||||||
|
count <= DynamicStateShadow::kMaxShadowedVertexBindings;
|
||||||
|
if (identical) {
|
||||||
|
for (Uint32 i = 0; i < count; ++i) {
|
||||||
|
if (shadow.vertexBuffers[i] != vkBuffers[i] || shadow.vertexOffsets[i] != vkOffsets[i]) {
|
||||||
|
identical = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!identical) {
|
||||||
|
vkCmdBindVertexBuffers(commandBuffer, 0, count, vkBuffers.data(), vkOffsets.data());
|
||||||
|
if (count <= DynamicStateShadow::kMaxShadowedVertexBindings) {
|
||||||
|
shadow.vertexBindValid = true;
|
||||||
|
shadow.vertexBindingCount = count;
|
||||||
|
std::copy_n(vkBuffers.data(), count, shadow.vertexBuffers);
|
||||||
|
std::copy_n(vkOffsets.data(), count, shadow.vertexOffsets);
|
||||||
|
} else {
|
||||||
|
shadow.vertexBindValid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -3247,8 +3281,17 @@ void main() {
|
|||||||
MGLOG_E("DrawElements skipped: failed to sync resident index buffer");
|
MGLOG_E("DrawElements skipped: failed to sync resident index buffer");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
vkCmdBindIndexBuffer(frame.commandBuffer, slice.buffer,
|
const VkDeviceSize indexBindOffset =
|
||||||
slice.offset + static_cast<VkDeviceSize>(pIndexBufferView->indexByteOffset), vkIndexType);
|
slice.offset + static_cast<VkDeviceSize>(pIndexBufferView->indexByteOffset);
|
||||||
|
auto& shadow = g_dynamicStateShadow;
|
||||||
|
if (!shadow.indexBindValid || shadow.indexBuffer != slice.buffer ||
|
||||||
|
shadow.indexOffset != indexBindOffset || shadow.indexType != vkIndexType) {
|
||||||
|
vkCmdBindIndexBuffer(frame.commandBuffer, slice.buffer, indexBindOffset, vkIndexType);
|
||||||
|
shadow.indexBindValid = true;
|
||||||
|
shadow.indexBuffer = slice.buffer;
|
||||||
|
shadow.indexOffset = indexBindOffset;
|
||||||
|
shadow.indexType = vkIndexType;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3815,16 +3858,24 @@ void main() {
|
|||||||
// content hash (folds program identity + link version + transform flags + shader stages),
|
// content hash (folds program identity + link version + transform flags + shader stages),
|
||||||
// vertex-input hash (VAO layout), render-pass hash (render targets + the draw-buffer/format
|
// vertex-input hash (VAO layout), render-pass hash (render targets + the draw-buffer/format
|
||||||
// driven blend & write-mask gating), and the render-state version (all fixed-function state).
|
// driven blend & write-mask gating), and the render-state version (all fixed-function state).
|
||||||
// Reset per-frame and on pipeline destruction so m_lastPipelineResult can never dangle.
|
// Reset per-frame and on pipeline destruction so a memoized handle can never dangle.
|
||||||
const Uint64 vertexInputHash = m_vertexInputStateFactory->GetOrComputeHash(vao);
|
// The identity hash mixes buffer heap addresses (per-chunk VBOs mint a new
|
||||||
|
// one per buffer); the memo and the pipeline payload key on the resolved
|
||||||
|
// LAYOUT hash instead, so draws over identical layouts share one pipeline.
|
||||||
|
// The one-arg fetch rides the VAO's state-pointer memo (no hash, no map).
|
||||||
|
auto& vis = m_vertexInputStateFactory->GetOrCreateVertexInputState(vao);
|
||||||
|
const Uint64 vertexLayoutHash = vis.layoutHash;
|
||||||
const Uint64 renderPassHash = renderPassEntry.hash;
|
const Uint64 renderPassHash = renderPassEntry.hash;
|
||||||
const Uint renderStateVersion = MG_State::pGLContext->GetRenderStateParametersVersion();
|
const Uint renderStateVersion = MG_State::pGLContext->GetRenderStateParametersVersion();
|
||||||
if (m_lastPipelineValid && m_lastPipelineResult != VK_NULL_HANDLE && m_lastPipelineMode == mode &&
|
for (Uint32 i = 0; i < m_pipelineMemoCount; ++i) {
|
||||||
m_lastPipelineProgramHash == programObj.hash && m_lastPipelineVertexInputHash == vertexInputHash &&
|
const PipelineMemoEntry& entry = m_pipelineMemo[i];
|
||||||
m_lastPipelineRenderPassHash == renderPassHash &&
|
if (entry.pipeline != VK_NULL_HANDLE && entry.mode == mode &&
|
||||||
m_lastPipelineRenderStateVersion == renderStateVersion &&
|
entry.programHash == programObj.hash && entry.vertexInputHash == vertexLayoutHash &&
|
||||||
m_lastPipelineTransformFlags == transformFlags) {
|
entry.renderPassHash == renderPassHash &&
|
||||||
return m_lastPipelineResult;
|
entry.renderStateVersion == renderStateVersion &&
|
||||||
|
entry.transformFlags == transformFlags) {
|
||||||
|
return entry.pipeline;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MOBILEGL_LOG_ACTIVE_LEVEL <= MOBILEGL_LOG_LEVEL_DEBUG
|
#if MOBILEGL_LOG_ACTIVE_LEVEL <= MOBILEGL_LOG_LEVEL_DEBUG
|
||||||
@@ -3865,9 +3916,7 @@ void main() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// vertexInputHash was computed above for the fast-path key; reuse it here.
|
const Uint32 vertexInputAttribMask = vis.attributeLocationMask;
|
||||||
auto& vis = m_vertexInputStateFactory->GetOrCreateVertexInputState(vao, vertexInputHash);
|
|
||||||
const Uint32 vertexInputAttribMask = BuildVertexInputAttributeMask(vis.attributes);
|
|
||||||
const Uint32 activeAttribMask = programObj.activeVertexInputLocationMask;
|
const Uint32 activeAttribMask = programObj.activeVertexInputLocationMask;
|
||||||
const Uint32 missingAttribMask = activeAttribMask & ~vertexInputAttribMask;
|
const Uint32 missingAttribMask = activeAttribMask & ~vertexInputAttribMask;
|
||||||
auto& patchedAttributes = m_patchedAttributesScratch;
|
auto& patchedAttributes = m_patchedAttributesScratch;
|
||||||
@@ -3977,7 +4026,7 @@ void main() {
|
|||||||
|
|
||||||
PipelineFactory::PipelineCreatePayload payload {
|
PipelineFactory::PipelineCreatePayload payload {
|
||||||
.programHash = programObj.hash,
|
.programHash = programObj.hash,
|
||||||
.vertexInputHash = vertexInputHash,
|
.vertexInputHash = vertexLayoutHash,
|
||||||
.pipelineLayout = programObj.pipelineLayout,
|
.pipelineLayout = programObj.pipelineLayout,
|
||||||
.renderPass = renderPassEntry.renderPass,
|
.renderPass = renderPassEntry.renderPass,
|
||||||
.colorAttachmentCount = renderPassEntry.colorAttachmentCount,
|
.colorAttachmentCount = renderPassEntry.colorAttachmentCount,
|
||||||
@@ -4284,14 +4333,16 @@ void main() {
|
|||||||
}
|
}
|
||||||
VkPipeline pipeline = m_pipelineFactory->GetOrCreatePipeline(payload);
|
VkPipeline pipeline = m_pipelineFactory->GetOrCreatePipeline(payload);
|
||||||
if (pipeline != VK_NULL_HANDLE) {
|
if (pipeline != VK_NULL_HANDLE) {
|
||||||
m_lastPipelineValid = true;
|
PipelineMemoEntry& entry = m_pipelineMemo[m_pipelineMemoNext];
|
||||||
m_lastPipelineMode = mode;
|
entry.mode = mode;
|
||||||
m_lastPipelineProgramHash = programObj.hash;
|
entry.programHash = programObj.hash;
|
||||||
m_lastPipelineVertexInputHash = vertexInputHash;
|
entry.vertexInputHash = vertexLayoutHash;
|
||||||
m_lastPipelineRenderPassHash = renderPassHash;
|
entry.renderPassHash = renderPassHash;
|
||||||
m_lastPipelineRenderStateVersion = renderStateVersion;
|
entry.renderStateVersion = renderStateVersion;
|
||||||
m_lastPipelineTransformFlags = transformFlags;
|
entry.transformFlags = transformFlags;
|
||||||
m_lastPipelineResult = pipeline;
|
entry.pipeline = pipeline;
|
||||||
|
m_pipelineMemoNext = (m_pipelineMemoNext + 1) % kPipelineMemoSize;
|
||||||
|
m_pipelineMemoCount = std::min(m_pipelineMemoCount + 1, kPipelineMemoSize);
|
||||||
}
|
}
|
||||||
return pipeline;
|
return pipeline;
|
||||||
}
|
}
|
||||||
@@ -4393,6 +4444,129 @@ void main() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Bool VulkanRenderer::TrySetupDrawFastPath(FrameContext::FrameData& frame, GLenum mode,
|
||||||
|
Flags<DrawSetupAspect> aspects, const DrawCmdParam& drawParams,
|
||||||
|
const IndexBufferView* pIndexBufferView) {
|
||||||
|
const SetupDrawSnapshot& snap = m_setupDrawSnapshot;
|
||||||
|
if (!snap.valid || !frame.isCommandRecording) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (snap.aspects != aspects.GetRaw() || snap.mode != mode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (m_clearManager->HasAnyPendingClears()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto* activeRenderPass = VkRenderPassManager::GetActiveRenderPass();
|
||||||
|
if (activeRenderPass == nullptr || activeRenderPass->hash != snap.renderPassHash ||
|
||||||
|
snap.imageIndex != m_imageIndexAcquired) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto& program = *MG_State::pGLContext->GetCurrentProgram();
|
||||||
|
if (program.GetLifetimeId() != snap.programLifetimeId ||
|
||||||
|
program.GetBackendStateVersion() != snap.programVersion) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto& vao = *MG_State::pGLContext->GetBoundVertexArray();
|
||||||
|
if (static_cast<const void*>(&vao) != snap.vao || vao.GetConfigVersion() != snap.vaoConfigVersion) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto& drawFbo =
|
||||||
|
MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).GetBoundObject();
|
||||||
|
if (static_cast<const void*>(drawFbo.get()) != snap.drawFbo ||
|
||||||
|
drawFbo->GetObjectVersion() != snap.fboVersion) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (MG_State::pGLContext->GetRenderStateParametersVersion() != snap.renderStateVersion ||
|
||||||
|
MG_State::pGLContext->GetTextureBindGeneration() != snap.bindGeneration) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (GetShaderTransformFlags(m_swapchainObject.GetPreTransform()).GetRaw() != snap.baseTransformFlags) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (m_textureManager->GetResourceEraseEpoch() != snap.textureEraseEpoch ||
|
||||||
|
m_textureManager->GetTextureImageEpoch() != snap.textureImageEpoch ||
|
||||||
|
m_renderPassManager->GetRenderbufferImageEpoch() != snap.renderbufferImageEpoch) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Same sampled set as the snapshotting draw (program/bind keys above);
|
||||||
|
// verify content and params are untouched and every layout is still
|
||||||
|
// sampleable, then stamp recording use exactly as the full path would.
|
||||||
|
// A feedback case (sampled texture written by the active pass) fails the
|
||||||
|
// layout check and falls back to the full path's end-pass handling.
|
||||||
|
const auto& sampledTextures = m_sampledTexturesScratch;
|
||||||
|
const auto& sampledResources = m_sampledResourcesScratch;
|
||||||
|
if (sampledResources.size() != sampledTextures.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Uint64 contentSum = 0;
|
||||||
|
Uint64 paramsSum = 0;
|
||||||
|
for (SizeT i = 0; i < sampledTextures.size(); ++i) {
|
||||||
|
const auto* sampledTexture = sampledTextures[i];
|
||||||
|
if (sampledTexture == nullptr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const auto* resource = sampledResources[i];
|
||||||
|
if (resource == nullptr || !IsValidSampledImageLayout(resource->layout)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
contentSum += sampledTexture->GetContentVersion();
|
||||||
|
paramsSum += sampledTexture->GetTextureParamsVersion();
|
||||||
|
}
|
||||||
|
if (contentSum != snap.sampledContentSum || paramsSum != snap.sampledParamsSum) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (SizeT i = 0; i < sampledTextures.size(); ++i) {
|
||||||
|
if (sampledTextures[i] != nullptr && sampledResources[i] != nullptr) {
|
||||||
|
m_textureManager->StampResourceRecordingUse(*sampledResources[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything the full path would re-resolve is provably unchanged; run
|
||||||
|
// only the per-draw tail.
|
||||||
|
if (!g_dynamicStateShadow.graphicsPipelineValid ||
|
||||||
|
g_dynamicStateShadow.graphicsPipeline != snap.pipeline) {
|
||||||
|
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, snap.pipeline);
|
||||||
|
g_dynamicStateShadow.graphicsPipelineValid = true;
|
||||||
|
g_dynamicStateShadow.graphicsPipeline = snap.pipeline;
|
||||||
|
}
|
||||||
|
const auto& programObj = m_programFactory->GetOrCreateProgram(
|
||||||
|
program, ProgramFactory::CompileOptionFlags(snap.resolvedTransformFlags));
|
||||||
|
if (!m_uniformManager->BindProgramUniformBuffers(frame.commandBuffer, program, programObj,
|
||||||
|
m_frameContext.GetCurrentFrameIndex())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!UploadAndBindVertexBuffers(frame.commandBuffer, vao, programObj, drawParams, pIndexBufferView)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (aspects & DrawSetupAspect::IndexBuffer) {
|
||||||
|
const Bool idxUploadOk = UploadAndBindIndexBuffer(frame, vao, pIndexBufferView);
|
||||||
|
MOBILEGL_ASSERT(idxUploadOk, "SetupDraw fast path: failed to upload index buffer");
|
||||||
|
}
|
||||||
|
ApplyGLViewportState(frame.commandBuffer, snap.renderPassExtent, m_swapchainObject.GetPreTransform(),
|
||||||
|
snap.drawFboIsDefault);
|
||||||
|
ApplyBlendConstants(frame.commandBuffer);
|
||||||
|
ApplyPolygonOffsetState(frame.commandBuffer);
|
||||||
|
ApplyLineWidthState(frame.commandBuffer);
|
||||||
|
ApplyStencilState(frame.commandBuffer);
|
||||||
|
const Bool scissorEnabled = MG_State::pGLContext->IsCapabilityEnabled(CapabilityInput::ScissorTest);
|
||||||
|
VkRect2D scissor{};
|
||||||
|
if (scissorEnabled) {
|
||||||
|
const auto& scissorBox = MG_State::pGLContext->GetScissorBox();
|
||||||
|
scissor = snap.drawFboIsDefault
|
||||||
|
? MakeDefaultFramebufferScissorRect(scissorBox, snap.renderPassExtent,
|
||||||
|
m_swapchainObject.GetPreTransform())
|
||||||
|
: MakeClampedScissorRect(scissorBox, snap.renderPassExtent);
|
||||||
|
} else {
|
||||||
|
scissor.offset = {0, 0};
|
||||||
|
scissor.extent = { (Uint)snap.renderPassExtent.x(), (Uint)snap.renderPassExtent.y() };
|
||||||
|
}
|
||||||
|
ShadowedSetScissor(frame.commandBuffer, scissor);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
Bool VulkanRenderer::SetupDraw(FrameContext::FrameData& frame, GLenum mode, Flags<DrawSetupAspect> aspects,
|
Bool VulkanRenderer::SetupDraw(FrameContext::FrameData& frame, GLenum mode, Flags<DrawSetupAspect> aspects,
|
||||||
const DrawCmdParam& drawParams,
|
const DrawCmdParam& drawParams,
|
||||||
const IndexBufferView* pIndexBufferView) {
|
const IndexBufferView* pIndexBufferView) {
|
||||||
@@ -4401,6 +4575,12 @@ void main() {
|
|||||||
// otherwise each re-run the full SyncTexture path on the same textures.
|
// otherwise each re-run the full SyncTexture path on the same textures.
|
||||||
VkTextureManager::DrawSyncScope drawSyncScope(*m_textureManager);
|
VkTextureManager::DrawSyncScope drawSyncScope(*m_textureManager);
|
||||||
m_textureManager->CollectGarbage();
|
m_textureManager->CollectGarbage();
|
||||||
|
if (TrySetupDrawFastPath(frame, mode, aspects, drawParams, pIndexBufferView)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// The fast path declined: whatever it saw may be stale. The full path
|
||||||
|
// below re-resolves everything and refreshes the snapshot on success.
|
||||||
|
m_setupDrawSnapshot.valid = false;
|
||||||
const auto& drawFbo =
|
const auto& drawFbo =
|
||||||
MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).GetBoundObject();
|
MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).GetBoundObject();
|
||||||
if (drawFbo != nullptr && IsUnsupportedFramebufferForDirectVulkan(*drawFbo)) {
|
if (drawFbo != nullptr && IsUnsupportedFramebufferForDirectVulkan(*drawFbo)) {
|
||||||
@@ -4410,16 +4590,52 @@ void main() {
|
|||||||
const auto& vao = *MG_State::pGLContext->GetBoundVertexArray();
|
const auto& vao = *MG_State::pGLContext->GetBoundVertexArray();
|
||||||
const auto& program = *MG_State::pGLContext->GetCurrentProgram();
|
const auto& program = *MG_State::pGLContext->GetCurrentProgram();
|
||||||
ProgramFactory::CompileOptionFlags transformFlags = GetShaderTransformFlags(m_swapchainObject.GetPreTransform());
|
ProgramFactory::CompileOptionFlags transformFlags = GetShaderTransformFlags(m_swapchainObject.GetPreTransform());
|
||||||
const auto* programObjPtr = &m_programFactory->GetOrCreateProgram(program, transformFlags);
|
|
||||||
// Sampling a colour render target through the driver's implicit-LOD path faults the GPU on
|
// Sampling a colour render target through the driver's implicit-LOD path faults the GPU on
|
||||||
// Adreno 650 (see ForceExplicitLod0SamplePass); ask for the explicit-LOD variant when doing
|
// Adreno 650 (see ForceExplicitLod0SamplePass); ask for the explicit-LOD variant when doing
|
||||||
// so cannot change a texel, i.e. when every sampler this program reads is pinned to a
|
// so cannot change a texel, i.e. when every sampler this program reads is pinned to a
|
||||||
// single mip level.
|
// single mip level. The probe walks every sampler binding, so its verdict is memoized
|
||||||
if (UniformManager::ProgramSamplesOnlySingleLevelTextures(program, *programObjPtr)) {
|
// under the sampled-set memo's key plus the sampled textures' params-version sum (level
|
||||||
transformFlags |= ProgramFactory::CompileOptionBit::ExplicitLod0Sampling;
|
// range and filter changes live there); the previous draw's texture list is valid for the
|
||||||
programObjPtr = &m_programFactory->GetOrCreateProgram(program, transformFlags);
|
// sum exactly when that key matches (same program, same binds).
|
||||||
|
{
|
||||||
|
const Uint64 lodProgramLifetimeId = program.GetLifetimeId();
|
||||||
|
const Uint32 lodProgramVersion = program.GetBackendStateVersion();
|
||||||
|
const Uint64 lodBindGeneration = MG_State::pGLContext->GetTextureBindGeneration();
|
||||||
|
Bool lodMemoHit = false;
|
||||||
|
if (m_lastLodDecisionValid && m_lastSampledSetValid &&
|
||||||
|
m_lastLodProgramLifetimeId == lodProgramLifetimeId &&
|
||||||
|
m_lastLodProgramVersion == lodProgramVersion &&
|
||||||
|
m_lastLodBindGeneration == lodBindGeneration && m_lastLodBaseFlags == transformFlags &&
|
||||||
|
m_lastSampledSetProgramLifetimeId == lodProgramLifetimeId &&
|
||||||
|
m_lastSampledSetProgramVersion == lodProgramVersion &&
|
||||||
|
m_lastSampledSetBindGeneration == lodBindGeneration) {
|
||||||
|
Uint64 paramsSum = 0;
|
||||||
|
for (const auto* sampledTexture : m_sampledTexturesScratch) {
|
||||||
|
if (sampledTexture != nullptr) {
|
||||||
|
paramsSum += sampledTexture->GetTextureParamsVersion();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (paramsSum == m_lastLodParamsSum) {
|
||||||
|
transformFlags = m_lastLodResultFlags;
|
||||||
|
lodMemoHit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!lodMemoHit) {
|
||||||
|
const ProgramFactory::CompileOptionFlags baseFlags = transformFlags;
|
||||||
|
const auto& baseProgramObj = m_programFactory->GetOrCreateProgram(program, transformFlags);
|
||||||
|
if (UniformManager::ProgramSamplesOnlySingleLevelTextures(program, baseProgramObj)) {
|
||||||
|
transformFlags |= ProgramFactory::CompileOptionBit::ExplicitLod0Sampling;
|
||||||
|
}
|
||||||
|
m_lastLodDecisionValid = true;
|
||||||
|
m_lastLodProgramLifetimeId = lodProgramLifetimeId;
|
||||||
|
m_lastLodProgramVersion = lodProgramVersion;
|
||||||
|
m_lastLodBindGeneration = lodBindGeneration;
|
||||||
|
m_lastLodBaseFlags = baseFlags;
|
||||||
|
m_lastLodResultFlags = transformFlags;
|
||||||
|
m_lastLodParamsSum = 0; // filled below once the sampled set is known
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const auto& programObj = *programObjPtr;
|
const auto& programObj = m_programFactory->GetOrCreateProgram(program, transformFlags);
|
||||||
|
|
||||||
// Begin command recording if not yet
|
// Begin command recording if not yet
|
||||||
if (!frame.isCommandRecording) {
|
if (!frame.isCommandRecording) {
|
||||||
@@ -4464,6 +4680,18 @@ void main() {
|
|||||||
m_lastSampledSetTransformFlags = transformFlags;
|
m_lastSampledSetTransformFlags = transformFlags;
|
||||||
m_lastSampledSetBindGeneration = bindGeneration;
|
m_lastSampledSetBindGeneration = bindGeneration;
|
||||||
}
|
}
|
||||||
|
// Complete a freshly-made LOD decision (see above): its params sum
|
||||||
|
// can only be taken once the sampled set is known. A genuine
|
||||||
|
// all-zero sum merely re-probes next draw.
|
||||||
|
if (m_lastLodDecisionValid && m_lastLodParamsSum == 0) {
|
||||||
|
Uint64 paramsSum = 0;
|
||||||
|
for (const auto* sampledTexture : sampledTextures) {
|
||||||
|
if (sampledTexture != nullptr) {
|
||||||
|
paramsSum += sampledTexture->GetTextureParamsVersion();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_lastLodParamsSum = paramsSum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
MGLOG_D("SetupDraw: program=%u drawFbo=%u sampledTextureCount=%zu activeRenderPass=%s",
|
MGLOG_D("SetupDraw: program=%u drawFbo=%u sampledTextureCount=%zu activeRenderPass=%s",
|
||||||
program.GetExternalIndex(), drawFbo ? drawFbo->GetExternalIndex() : 0u, sampledTextures.size(),
|
program.GetExternalIndex(), drawFbo ? drawFbo->GetExternalIndex() : 0u, sampledTextures.size(),
|
||||||
@@ -4487,7 +4715,10 @@ void main() {
|
|||||||
activeRenderPass = nullptr;
|
activeRenderPass = nullptr;
|
||||||
}
|
}
|
||||||
Bool needSampledTextureTransitions = false;
|
Bool needSampledTextureTransitions = false;
|
||||||
for (auto* sampledTexture : sampledTextures) {
|
auto& sampledResources = m_sampledResourcesScratch;
|
||||||
|
sampledResources.assign(sampledTextures.size(), nullptr);
|
||||||
|
for (SizeT sampledIndex = 0; sampledIndex < sampledTextures.size(); ++sampledIndex) {
|
||||||
|
auto* sampledTexture = sampledTextures[sampledIndex];
|
||||||
if (!sampledTexture) {
|
if (!sampledTexture) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -4496,6 +4727,7 @@ void main() {
|
|||||||
MOBILEGL_ASSERT(textureResource != nullptr,
|
MOBILEGL_ASSERT(textureResource != nullptr,
|
||||||
"%s: SyncTextureAndGetDescriptor failed for textureId=%d",
|
"%s: SyncTextureAndGetDescriptor failed for textureId=%d",
|
||||||
__func__, sampledTexture->GetExternalIndex());
|
__func__, sampledTexture->GetExternalIndex());
|
||||||
|
sampledResources[sampledIndex] = textureResource;
|
||||||
MGLOG_D("SetupDraw: sampled textureId=%d layout(before)=%s(%d)",
|
MGLOG_D("SetupDraw: sampled textureId=%d layout(before)=%s(%d)",
|
||||||
sampledTexture->GetExternalIndex(), VkImageLayoutToString(textureResource->layout),
|
sampledTexture->GetExternalIndex(), VkImageLayoutToString(textureResource->layout),
|
||||||
static_cast<Int>(textureResource->layout));
|
static_cast<Int>(textureResource->layout));
|
||||||
@@ -4534,10 +4766,23 @@ void main() {
|
|||||||
activeRenderPass = nullptr;
|
activeRenderPass = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto* sampledTexture : sampledTextures) {
|
for (SizeT sampledIndex = 0; sampledIndex < sampledTextures.size(); ++sampledIndex) {
|
||||||
|
auto* sampledTexture = sampledTextures[sampledIndex];
|
||||||
if (!sampledTexture) {
|
if (!sampledTexture) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// Fast path: the first loop already resolved this texture, nothing
|
||||||
|
// is pending against it, and its layout is still sampleable (the
|
||||||
|
// layout re-check covers an EndRenderPass between the loops having
|
||||||
|
// rewritten an attachment's layout). Skipping the materialize +
|
||||||
|
// transition + re-resolve chain here is the difference between one
|
||||||
|
// pointer read and three calls per sampled texture per draw.
|
||||||
|
if (auto* fastResource = sampledResources[sampledIndex];
|
||||||
|
fastResource != nullptr && !m_clearManager->HasPendingClear(sampledTexture) &&
|
||||||
|
IsValidSampledImageLayout(fastResource->layout)) {
|
||||||
|
m_textureManager->StampResourceRecordingUse(*fastResource);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const Bool clearReady = MaterializePendingClearForTexture(frame.commandBuffer, *sampledTexture);
|
const Bool clearReady = MaterializePendingClearForTexture(frame.commandBuffer, *sampledTexture);
|
||||||
MOBILEGL_ASSERT(clearReady, "%s: MaterializePendingClearForTexture failed for textureId=%d",
|
MOBILEGL_ASSERT(clearReady, "%s: MaterializePendingClearForTexture failed for textureId=%d",
|
||||||
__func__, sampledTexture->GetExternalIndex());
|
__func__, sampledTexture->GetExternalIndex());
|
||||||
@@ -4601,7 +4846,7 @@ void main() {
|
|||||||
// Every genuinely disabled attribute the shader reads must have a current-value type we can
|
// Every genuinely disabled attribute the shader reads must have a current-value type we can
|
||||||
// synthesize a binding for; otherwise the upload below would push a null payload.
|
// synthesize a binding for; otherwise the upload below would push a null payload.
|
||||||
const Uint32 missingAttribMask =
|
const Uint32 missingAttribMask =
|
||||||
activeAttribMask & ~BuildVertexInputAttributeMask(vertexInputState.attributes);
|
activeAttribMask & ~vertexInputState.attributeLocationMask;
|
||||||
for (Uint32 location = 0; location < kMaxVertexAttribs; ++location) {
|
for (Uint32 location = 0; location < kMaxVertexAttribs; ++location) {
|
||||||
if ((missingAttribMask & (1u << location)) == 0) continue;
|
if ((missingAttribMask & (1u << location)) == 0) continue;
|
||||||
|
|
||||||
@@ -4629,7 +4874,11 @@ void main() {
|
|||||||
MOBILEGL_ASSERT(ok, "%s: BeginRenderPass failed", __func__);
|
MOBILEGL_ASSERT(ok, "%s: BeginRenderPass failed", __func__);
|
||||||
}
|
}
|
||||||
|
|
||||||
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
if (!g_dynamicStateShadow.graphicsPipelineValid || g_dynamicStateShadow.graphicsPipeline != pipeline) {
|
||||||
|
vkCmdBindPipeline(frame.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||||
|
g_dynamicStateShadow.graphicsPipelineValid = true;
|
||||||
|
g_dynamicStateShadow.graphicsPipeline = pipeline;
|
||||||
|
}
|
||||||
|
|
||||||
const Bool boundUniforms = m_uniformManager->BindProgramUniformBuffers(
|
const Bool boundUniforms = m_uniformManager->BindProgramUniformBuffers(
|
||||||
frame.commandBuffer, program, programObj, m_frameContext.GetCurrentFrameIndex());
|
frame.commandBuffer, program, programObj, m_frameContext.GetCurrentFrameIndex());
|
||||||
@@ -4670,6 +4919,48 @@ void main() {
|
|||||||
scissor.extent = { (Uint)renderPassEntry->extent.x(), (Uint)renderPassEntry->extent.y() };
|
scissor.extent = { (Uint)renderPassEntry->extent.x(), (Uint)renderPassEntry->extent.y() };
|
||||||
}
|
}
|
||||||
ShadowedSetScissor(frame.commandBuffer, scissor);
|
ShadowedSetScissor(frame.commandBuffer, scissor);
|
||||||
|
|
||||||
|
// Snapshot the fully resolved configuration for the consecutive-draw
|
||||||
|
// fast path (see TrySetupDrawFastPath).
|
||||||
|
{
|
||||||
|
auto& snap = m_setupDrawSnapshot;
|
||||||
|
const auto* nowActiveRenderPass = VkRenderPassManager::GetActiveRenderPass();
|
||||||
|
if (nowActiveRenderPass != nullptr && !programObj.hasStorageImages) {
|
||||||
|
snap.valid = true;
|
||||||
|
snap.aspects = aspects.GetRaw();
|
||||||
|
snap.mode = mode;
|
||||||
|
snap.programLifetimeId = program.GetLifetimeId();
|
||||||
|
snap.programVersion = program.GetBackendStateVersion();
|
||||||
|
snap.vao = &vao;
|
||||||
|
snap.vaoConfigVersion = vao.GetConfigVersion();
|
||||||
|
snap.drawFbo = drawFbo.get();
|
||||||
|
snap.fboVersion = drawFbo->GetObjectVersion();
|
||||||
|
snap.drawFboIsDefault = drawFbo->IsDefaultFramebuffer();
|
||||||
|
snap.renderStateVersion = MG_State::pGLContext->GetRenderStateParametersVersion();
|
||||||
|
snap.bindGeneration = MG_State::pGLContext->GetTextureBindGeneration();
|
||||||
|
snap.baseTransformFlags = GetShaderTransformFlags(m_swapchainObject.GetPreTransform()).GetRaw();
|
||||||
|
snap.resolvedTransformFlags = transformFlags.GetRaw();
|
||||||
|
snap.renderPassHash = nowActiveRenderPass->hash;
|
||||||
|
snap.imageIndex = m_imageIndexAcquired;
|
||||||
|
snap.textureEraseEpoch = m_textureManager->GetResourceEraseEpoch();
|
||||||
|
snap.textureImageEpoch = m_textureManager->GetTextureImageEpoch();
|
||||||
|
snap.renderbufferImageEpoch = m_renderPassManager->GetRenderbufferImageEpoch();
|
||||||
|
snap.renderPassExtent = renderPassEntry->extent;
|
||||||
|
snap.pipeline = pipeline;
|
||||||
|
Uint64 snapContentSum = 0;
|
||||||
|
Uint64 snapParamsSum = 0;
|
||||||
|
for (const auto* sampledTexture : sampledTextures) {
|
||||||
|
if (sampledTexture != nullptr) {
|
||||||
|
snapContentSum += sampledTexture->GetContentVersion();
|
||||||
|
snapParamsSum += sampledTexture->GetTextureParamsVersion();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
snap.sampledContentSum = snapContentSum;
|
||||||
|
snap.sampledParamsSum = snapParamsSum;
|
||||||
|
} else {
|
||||||
|
snap.valid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6412,7 +6703,7 @@ void main() {
|
|||||||
if (frame.isCommandRecording) {
|
if (frame.isCommandRecording) {
|
||||||
m_frameContext.EndCommandRecording();
|
m_frameContext.EndCommandRecording();
|
||||||
frame.hasCommandBufferRecorded = true;
|
frame.hasCommandBufferRecorded = true;
|
||||||
m_lastPipelineValid = false; // command-buffer boundary: drop the pipeline memo
|
InvalidatePipelineMemo(); // command-buffer boundary: drop the pipeline memo
|
||||||
}
|
}
|
||||||
// The pre-pass stream must never be submitted later than the recording
|
// The pre-pass stream must never be submitted later than the recording
|
||||||
// it was paired with (frame commands recorded after a pre-pass move
|
// it was paired with (frame commands recorded after a pre-pass move
|
||||||
@@ -7499,8 +7790,7 @@ void main() {
|
|||||||
m_programFactory->OnFrameBoundary();
|
m_programFactory->OnFrameBoundary();
|
||||||
}
|
}
|
||||||
if (m_pipelineFactory && m_pipelineFactory->OnFrameBoundary() > 0) {
|
if (m_pipelineFactory && m_pipelineFactory->OnFrameBoundary() > 0) {
|
||||||
m_lastPipelineValid = false;
|
InvalidatePipelineMemo();
|
||||||
m_lastPipelineResult = VK_NULL_HANDLE;
|
|
||||||
}
|
}
|
||||||
if (m_vertexInputStateFactory) {
|
if (m_vertexInputStateFactory) {
|
||||||
m_vertexInputStateFactory->OnFrameBoundary();
|
m_vertexInputStateFactory->OnFrameBoundary();
|
||||||
@@ -7617,8 +7907,7 @@ void main() {
|
|||||||
// cache and the aging sweep could destroy it while the flushed submission
|
// cache and the aging sweep could destroy it while the flushed submission
|
||||||
// still references it. Mirrors the drops at the readback and Present
|
// still references it. Mirrors the drops at the readback and Present
|
||||||
// boundaries; costs one full pipeline lookup on the next draw.
|
// boundaries; costs one full pipeline lookup on the next draw.
|
||||||
m_lastPipelineValid = false;
|
InvalidatePipelineMemo();
|
||||||
m_lastPipelineResult = VK_NULL_HANDLE;
|
|
||||||
|
|
||||||
// The submitted command buffer may still be executing; recording must
|
// The submitted command buffer may still be executing; recording must
|
||||||
// restart on a fresh one. If none can be allocated, fall back to
|
// restart on a fresh one. If none can be allocated, fall back to
|
||||||
@@ -7691,6 +7980,10 @@ void main() {
|
|||||||
void VulkanRenderer::OnFrameCommandRecordingBegan(VkCommandBuffer commandBuffer) {
|
void VulkanRenderer::OnFrameCommandRecordingBegan(VkCommandBuffer commandBuffer) {
|
||||||
// Dynamic state does not survive a command-buffer boundary.
|
// Dynamic state does not survive a command-buffer boundary.
|
||||||
ResetDynamicStateShadow();
|
ResetDynamicStateShadow();
|
||||||
|
m_setupDrawSnapshot.valid = false;
|
||||||
|
if (m_uniformManager) {
|
||||||
|
m_uniformManager->OnCommandBufferBoundary();
|
||||||
|
}
|
||||||
// Pre-pass stream bookkeeping: a fresh frame recording references no
|
// Pre-pass stream bookkeeping: a fresh frame recording references no
|
||||||
// textures yet.
|
// textures yet.
|
||||||
if (m_textureManager) {
|
if (m_textureManager) {
|
||||||
@@ -7771,7 +8064,7 @@ void main() {
|
|||||||
m_frameContext.AbandonPreCommandRecording();
|
m_frameContext.AbandonPreCommandRecording();
|
||||||
suspendedFrame.isCommandRecording = false;
|
suspendedFrame.isCommandRecording = false;
|
||||||
suspendedFrame.hasCommandBufferRecorded = false;
|
suspendedFrame.hasCommandBufferRecorded = false;
|
||||||
m_lastPipelineValid = false;
|
InvalidatePipelineMemo();
|
||||||
// The dropped recording is never submitted, so once the fence
|
// The dropped recording is never submitted, so once the fence
|
||||||
// poll shows the pre-suspension submissions complete the frame
|
// poll shows the pre-suspension submissions complete the frame
|
||||||
// transients (descriptor sets, transient arenas, deferred
|
// transients (descriptor sets, transient arenas, deferred
|
||||||
@@ -7807,8 +8100,11 @@ void main() {
|
|||||||
// performs a real, stamping lookup) and can never age out.
|
// performs a real, stamping lookup) and can never age out.
|
||||||
m_programFactory->OnFrameBoundary();
|
m_programFactory->OnFrameBoundary();
|
||||||
if (m_pipelineFactory->OnFrameBoundary() > 0) {
|
if (m_pipelineFactory->OnFrameBoundary() > 0) {
|
||||||
m_lastPipelineValid = false; // an aged-out pipeline may still be memoized
|
InvalidatePipelineMemo(); // an aged-out pipeline may still be memoized
|
||||||
m_lastPipelineResult = VK_NULL_HANDLE;
|
// A recreated pipeline could reuse a freed handle value and alias
|
||||||
|
// the bind-dedup shadow; force the next draw to re-bind.
|
||||||
|
g_dynamicStateShadow.graphicsPipelineValid = false;
|
||||||
|
m_setupDrawSnapshot.valid = false;
|
||||||
}
|
}
|
||||||
m_vertexInputStateFactory->OnFrameBoundary();
|
m_vertexInputStateFactory->OnFrameBoundary();
|
||||||
m_samplerManager->OnFrameBoundary();
|
m_samplerManager->OnFrameBoundary();
|
||||||
@@ -7831,7 +8127,7 @@ void main() {
|
|||||||
if (frame.isCommandRecording) {
|
if (frame.isCommandRecording) {
|
||||||
m_frameContext.EndCommandRecording();
|
m_frameContext.EndCommandRecording();
|
||||||
frame.hasCommandBufferRecorded = true;
|
frame.hasCommandBufferRecorded = true;
|
||||||
m_lastPipelineValid = false; // command-buffer boundary: drop the pipeline memo
|
InvalidatePipelineMemo(); // command-buffer boundary: drop the pipeline memo
|
||||||
}
|
}
|
||||||
m_frameContext.EndPreCommandRecordingIfOpen();
|
m_frameContext.EndPreCommandRecordingIfOpen();
|
||||||
|
|
||||||
@@ -8834,7 +9130,9 @@ void main() {
|
|||||||
if (m_pipelineFactory) {
|
if (m_pipelineFactory) {
|
||||||
m_pipelineFactory->DestroyAll();
|
m_pipelineFactory->DestroyAll();
|
||||||
}
|
}
|
||||||
m_lastPipelineValid = false; // pipelines freed -> the memoized handle would dangle
|
InvalidatePipelineMemo(); // pipelines freed -> the memoized handle would dangle
|
||||||
|
g_dynamicStateShadow.graphicsPipelineValid = false;
|
||||||
|
m_setupDrawSnapshot.valid = false;
|
||||||
DestroyComputePipelines();
|
DestroyComputePipelines();
|
||||||
if (m_frameContext.GetFrameCount() > 0) {
|
if (m_frameContext.GetFrameCount() > 0) {
|
||||||
m_frameContext.GetCurrent().isCommandRecording = false;
|
m_frameContext.GetCurrent().isCommandRecording = false;
|
||||||
@@ -8997,8 +9295,7 @@ void main() {
|
|||||||
// destroys them immediately. The memo must drop as well: it can hand out a
|
// destroys them immediately. The memo must drop as well: it can hand out a
|
||||||
// cached handle without touching the factory.
|
// cached handle without touching the factory.
|
||||||
if (m_pipelineFactory->EvictByRenderPasses(renderPasses) > 0) {
|
if (m_pipelineFactory->EvictByRenderPasses(renderPasses) > 0) {
|
||||||
m_lastPipelineValid = false;
|
InvalidatePipelineMemo();
|
||||||
m_lastPipelineResult = VK_NULL_HANDLE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -9017,8 +9314,7 @@ void main() {
|
|||||||
m_computePipelines.erase(computeIt);
|
m_computePipelines.erase(computeIt);
|
||||||
}
|
}
|
||||||
if (m_pipelineFactory != nullptr && m_pipelineFactory->EvictByProgramHash(programHash) > 0) {
|
if (m_pipelineFactory != nullptr && m_pipelineFactory->EvictByProgramHash(programHash) > 0) {
|
||||||
m_lastPipelineValid = false;
|
InvalidatePipelineMemo();
|
||||||
m_lastPipelineResult = VK_NULL_HANDLE;
|
|
||||||
}
|
}
|
||||||
if (m_uniformManager != nullptr) {
|
if (m_uniformManager != nullptr) {
|
||||||
m_uniformManager->OnDescriptorSetLayoutDestroyed(descriptorSetLayout);
|
m_uniformManager->OnDescriptorSetLayoutDestroyed(descriptorSetLayout);
|
||||||
|
|||||||
@@ -151,6 +151,14 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
Bool SetupDraw(FrameContext::FrameData& frame, GLenum mode, Flags<DrawSetupAspect> aspects,
|
Bool SetupDraw(FrameContext::FrameData& frame, GLenum mode, Flags<DrawSetupAspect> aspects,
|
||||||
const DrawCmdParam& drawParams,
|
const DrawCmdParam& drawParams,
|
||||||
const IndexBufferView* pIndexBufferView = nullptr);
|
const IndexBufferView* pIndexBufferView = nullptr);
|
||||||
|
// ANGLE-style consecutive-draw fast path: SetupDraw snapshots the fully
|
||||||
|
// resolved draw configuration; the next draw whose cheap version/identity
|
||||||
|
// checks all match skips the resolution half (LOD probe, sampled-set
|
||||||
|
// walk, render-pass and pipeline resolution) and jumps straight to the
|
||||||
|
// per-draw tail. Returns false (leaving no side effects that the full
|
||||||
|
// path cannot redo idempotently) whenever anything might have changed.
|
||||||
|
Bool TrySetupDrawFastPath(FrameContext::FrameData& frame, GLenum mode, Flags<DrawSetupAspect> aspects,
|
||||||
|
const DrawCmdParam& drawParams, const IndexBufferView* pIndexBufferView);
|
||||||
void ClearAttachmentsOnActiveRenderPass(VkCommandBuffer commandBuffer,
|
void ClearAttachmentsOnActiveRenderPass(VkCommandBuffer commandBuffer,
|
||||||
const RenderPassEntry& compatibleRenderPassEntry);
|
const RenderPassEntry& compatibleRenderPassEntry);
|
||||||
|
|
||||||
@@ -455,14 +463,30 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
// gather + synthetic vertex-input rebuild + payload hash + lookup) when the full pipeline
|
// gather + synthetic vertex-input rebuild + payload hash + lookup) when the full pipeline
|
||||||
// state is unchanged from the previous draw. The key provably covers every pipeline field.
|
// state is unchanged from the previous draw. The key provably covers every pipeline field.
|
||||||
// Reset per-frame and on pipeline destruction so the cached handle can never dangle.
|
// Reset per-frame and on pipeline destruction so the cached handle can never dangle.
|
||||||
Bool m_lastPipelineValid = false;
|
// Small N-way pipeline-resolution memo (round-robin replacement). A
|
||||||
GLenum m_lastPipelineMode = 0;
|
// single-entry memo thrashed on draw sequences that alternate a few
|
||||||
Uint64 m_lastPipelineProgramHash = 0;
|
// pipelines (GUI text/quad program ping-pong), paying the full
|
||||||
Uint64 m_lastPipelineVertexInputHash = 0;
|
// payload-hash lookup per draw; eight entries cover such working sets
|
||||||
Uint64 m_lastPipelineRenderPassHash = 0;
|
// while keeping the hit path a trivial linear scan.
|
||||||
Uint m_lastPipelineRenderStateVersion = 0;
|
struct PipelineMemoEntry {
|
||||||
ProgramFactory::CompileOptionFlags m_lastPipelineTransformFlags = {};
|
GLenum mode = 0;
|
||||||
VkPipeline m_lastPipelineResult = VK_NULL_HANDLE;
|
Uint64 programHash = 0;
|
||||||
|
Uint64 vertexInputHash = 0;
|
||||||
|
Uint64 renderPassHash = 0;
|
||||||
|
Uint renderStateVersion = 0;
|
||||||
|
ProgramFactory::CompileOptionFlags transformFlags = {};
|
||||||
|
VkPipeline pipeline = VK_NULL_HANDLE;
|
||||||
|
};
|
||||||
|
static constexpr Uint32 kPipelineMemoSize = 8;
|
||||||
|
PipelineMemoEntry m_pipelineMemo[kPipelineMemoSize];
|
||||||
|
Uint32 m_pipelineMemoCount = 0;
|
||||||
|
Uint32 m_pipelineMemoNext = 0;
|
||||||
|
// Drops every memoized pipeline handle. Required at command-buffer
|
||||||
|
// boundaries and whenever any pipeline may have been destroyed.
|
||||||
|
void InvalidatePipelineMemo() {
|
||||||
|
m_pipelineMemoCount = 0;
|
||||||
|
m_pipelineMemoNext = 0;
|
||||||
|
}
|
||||||
UnorderedMap<ProgramFactory::HashType, VkPipeline> m_computePipelines;
|
UnorderedMap<ProgramFactory::HashType, VkPipeline> m_computePipelines;
|
||||||
UniquePtr<ProgramFactory> m_programFactory;
|
UniquePtr<ProgramFactory> m_programFactory;
|
||||||
UniquePtr<UniformManager> m_uniformManager;
|
UniquePtr<UniformManager> m_uniformManager;
|
||||||
@@ -491,9 +515,61 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
|||||||
ProgramFactory::CompileOptionFlags m_lastSampledSetTransformFlags = {};
|
ProgramFactory::CompileOptionFlags m_lastSampledSetTransformFlags = {};
|
||||||
Uint64 m_lastSampledSetBindGeneration = 0;
|
Uint64 m_lastSampledSetBindGeneration = 0;
|
||||||
|
|
||||||
|
// Memo for the per-draw explicit-LOD-0 eligibility probe
|
||||||
|
// (ProgramSamplesOnlySingleLevelTextures): same key family as the
|
||||||
|
// sampled-set memo, plus the sampled textures' params-version sum so a
|
||||||
|
// level-range or filter change re-probes. On a hit the resolved
|
||||||
|
// transform flags are reused, which also collapses the two
|
||||||
|
// GetOrCreateProgram lookups into one.
|
||||||
|
Bool m_lastLodDecisionValid = false;
|
||||||
|
Uint64 m_lastLodProgramLifetimeId = 0;
|
||||||
|
Uint32 m_lastLodProgramVersion = 0;
|
||||||
|
Uint64 m_lastLodBindGeneration = 0;
|
||||||
|
Uint64 m_lastLodParamsSum = 0;
|
||||||
|
ProgramFactory::CompileOptionFlags m_lastLodBaseFlags = {};
|
||||||
|
ProgramFactory::CompileOptionFlags m_lastLodResultFlags = {};
|
||||||
|
|
||||||
|
// Snapshot behind TrySetupDrawFastPath. Values only: the program and
|
||||||
|
// render-pass caches are open-addressing maps whose entries move on
|
||||||
|
// insert, so no pointers into them are cached; the pipeline handle is
|
||||||
|
// protected by the command-buffer-boundary reset plus the mid-frame
|
||||||
|
// pipeline-destruction resets, and monotonic epochs guard everything
|
||||||
|
// that can be destroyed or recreated between draws.
|
||||||
|
struct SetupDrawSnapshot {
|
||||||
|
Bool valid = false;
|
||||||
|
Uint8 aspects = 0;
|
||||||
|
GLenum mode = 0;
|
||||||
|
Uint64 programLifetimeId = 0;
|
||||||
|
Uint32 programVersion = 0;
|
||||||
|
const void* vao = nullptr;
|
||||||
|
Uint32 vaoConfigVersion = 0;
|
||||||
|
const void* drawFbo = nullptr;
|
||||||
|
Uint16 fboVersion = 0;
|
||||||
|
Bool drawFboIsDefault = false;
|
||||||
|
Uint renderStateVersion = 0;
|
||||||
|
Uint64 bindGeneration = 0;
|
||||||
|
Uint32 baseTransformFlags = 0;
|
||||||
|
Uint32 resolvedTransformFlags = 0;
|
||||||
|
Uint64 renderPassHash = 0;
|
||||||
|
Uint32 imageIndex = 0;
|
||||||
|
Uint64 textureEraseEpoch = 0;
|
||||||
|
Uint64 textureImageEpoch = 0;
|
||||||
|
Uint64 renderbufferImageEpoch = 0;
|
||||||
|
Uint64 sampledContentSum = 0;
|
||||||
|
Uint64 sampledParamsSum = 0;
|
||||||
|
IntVec2 renderPassExtent = {0, 0};
|
||||||
|
VkPipeline pipeline = VK_NULL_HANDLE;
|
||||||
|
};
|
||||||
|
SetupDrawSnapshot m_setupDrawSnapshot;
|
||||||
|
|
||||||
// Per-draw scratch buffers (clear keeps capacity) — these paths run for every
|
// Per-draw scratch buffers (clear keeps capacity) — these paths run for every
|
||||||
// draw call and must not allocate.
|
// draw call and must not allocate.
|
||||||
Vector<MG_State::GLState::ITextureObject*> m_sampledTexturesScratch;
|
Vector<MG_State::GLState::ITextureObject*> m_sampledTexturesScratch;
|
||||||
|
// Parallel to m_sampledTexturesScratch, refilled by every SetupDraw's
|
||||||
|
// first sampled-texture loop: the resolved backend resources, so the
|
||||||
|
// post-transition loop can skip re-resolving textures whose layout is
|
||||||
|
// already sampleable.
|
||||||
|
Vector<VkTextureManager::TextureResource*> m_sampledResourcesScratch;
|
||||||
Vector<MG_State::GLState::ITextureObject*> m_storageImageTexturesScratch;
|
Vector<MG_State::GLState::ITextureObject*> m_storageImageTexturesScratch;
|
||||||
Vector<VkBuffer> m_vertexBuffersScratch;
|
Vector<VkBuffer> m_vertexBuffersScratch;
|
||||||
Vector<VkDeviceSize> m_vertexOffsetsScratch;
|
Vector<VkDeviceSize> m_vertexOffsetsScratch;
|
||||||
|
|||||||
@@ -104,6 +104,23 @@ namespace MobileGL {
|
|||||||
m_backendHashMemoVersion = m_configVersion;
|
m_backendHashMemoVersion = m_configVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Backend-owned resolved-state memo: an opaque pointer into the
|
||||||
|
// backend's vertex-input-state cache plus the cache's eviction
|
||||||
|
// epoch, valid while the config version matches. Lets the
|
||||||
|
// per-draw path skip the content hash AND the cache lookup; the
|
||||||
|
// epoch guards against the cache evicting the pointee.
|
||||||
|
Bool GetBackendStateMemo(const void*& outState, Uint64& outEpoch) const {
|
||||||
|
if (m_backendStateMemoVersion != m_configVersion) return false;
|
||||||
|
outState = m_backendStateMemo;
|
||||||
|
outEpoch = m_backendStateMemoEpoch;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
void SetBackendStateMemo(const void* state, Uint64 epoch) const {
|
||||||
|
m_backendStateMemo = state;
|
||||||
|
m_backendStateMemoEpoch = epoch;
|
||||||
|
m_backendStateMemoVersion = m_configVersion;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void BumpAttributeFormatVersion(Uint index);
|
void BumpAttributeFormatVersion(Uint index);
|
||||||
void BumpAttributeBufferVersion(Uint index);
|
void BumpAttributeBufferVersion(Uint index);
|
||||||
@@ -137,6 +154,9 @@ namespace MobileGL {
|
|||||||
Uint32 m_configVersion = 0;
|
Uint32 m_configVersion = 0;
|
||||||
mutable Uint64 m_backendHashMemo = 0;
|
mutable Uint64 m_backendHashMemo = 0;
|
||||||
mutable Uint32 m_backendHashMemoVersion = ~0u;
|
mutable Uint32 m_backendHashMemoVersion = ~0u;
|
||||||
|
mutable const void* m_backendStateMemo = nullptr;
|
||||||
|
mutable Uint64 m_backendStateMemoEpoch = 0;
|
||||||
|
mutable Uint32 m_backendStateMemoVersion = ~0u;
|
||||||
};
|
};
|
||||||
} // namespace GLState
|
} // namespace GLState
|
||||||
} // namespace MG_State
|
} // namespace MG_State
|
||||||
|
|||||||
Reference in New Issue
Block a user