Files
Triforce-Source/gpu/vk/vk_work.h
ruckus7382sfgsgdf e6f2f248f2 Triforce v0.7.0 "Sage Of Courage"
The 0.6 crashes after warps and area changes fixed (no write-back into memory the capture leaves out), invisible objects fixed (occlusion gate), DLSS frame generation flicker fixed, achievements, the renderer built with a play profile, faster translated game code, helper threads judged on settled scenes, memory, shader preparation, audio effects, launcher, controls and Linux fixes.
2026-11-22 11:59:06 -06:00

171 lines
16 KiB
C++

// (hookshot) Every Vulkan command that does GPU work, or orders it, counts in work_epoch. full_barrier (vk_texture.cpp) leaves out a
// barrier recorded right after another one on the same command buffer with nothing in between: the second orders nothing the first
// did not. Include this header AFTER the file's other includes: it turns the calls below into a count and the call (a header
// included after it that names one of them with a '(' after it would be changed too).
// ((Triforce, P4) The count is the command buffer's own now (cmd_work.h, after NX2navi's per-context state): each macro counts its
// command in the state of the command buffer its first argument names (RX_WORK_ON: that argument is read once), a begin starts that
// state afresh and a submit is counted for what crosses command buffers (work_between). RX_VK_BARRIER_CONTEXT=0: the one count of
// every thread's commands, as before)
#pragma once
#include <atomic>
#include "vk_loader.h"
#include "vk_recorder.h" // (RX_VK_RECORDER: each macro below calls its packer, which calls the driver directly when the recorder is off)
#include "cmd_work.h" // ((Triforce) note_work, work_begun, work_submitted)
#include "gpu_wait_kinds.h" // ((Triforce, 11-22, SYNC-27) the host's time in the calls below that block on the GPU, by kind
// ((Triforce, 11-22, the performance audit's SYNC-27, after Source A's) The five calls that block on the GPU, each timed
// under its kind when it blocks (gpu_wait_kinds.h: a fence or a semaphore wait with a timeout above 0, a query read with the
// wait bit, an idle always). Their macros below call these after the recorder's boundary with the driver's call first, then
// its arguments, each evaluated once as before. RX_VK_GPU_WAITS=0: timed() is the call alone
namespace mkwvk::gpu_waits_vk {
template <class Call>
inline VkResult queue_idle(Call&& call, VkQueue queue) {
return ::gpu_wait_kinds::timed(::gpu_wait_kinds::IDLE, true, [&] { return call(queue); });
}
template <class Call>
inline VkResult device_idle(Call&& call, VkDevice device) {
return ::gpu_wait_kinds::timed(::gpu_wait_kinds::IDLE, true, [&] { return call(device); });
}
template <class Call>
inline VkResult fences(Call&& call, VkDevice device, uint32_t count, const VkFence* list, VkBool32 all, uint64_t timeout) {
return ::gpu_wait_kinds::timed(::gpu_wait_kinds::FENCE, timeout != 0, [&] { return call(device, count, list, all, timeout); });
}
template <class Call>
inline VkResult semaphores(Call&& call, VkDevice device, const VkSemaphoreWaitInfo* info, uint64_t timeout) {
return ::gpu_wait_kinds::timed(::gpu_wait_kinds::TIMELINE, timeout != 0, [&] { return call(device, info, timeout); });
}
template <class Call>
inline VkResult query_results(Call&& call, VkDevice device, VkQueryPool pool, uint32_t first, uint32_t count, size_t size, void* data,
VkDeviceSize stride, VkQueryResultFlags flags) {
return ::gpu_wait_kinds::timed(::gpu_wait_kinds::QUERY, (flags & VK_QUERY_RESULT_WAIT_BIT) != 0,
[&] { return call(device, pool, first, count, size, data, stride, flags); });
}
} // namespace mkwvk::gpu_waits_vk
// ((Triforce) the command buffer, the first argument, counted as it is passed on: work_on returns it. __VA_OPT__: a call with no
// argument after it, vkCmdEndRendering's)
#define RX_WORK_ON(cmd, ...) ::mkwvk::work_on(cmd) __VA_OPT__(, ) __VA_ARGS__
#define RX_WORK_BEGUN(cmd, ...) ::mkwvk::work_begun(cmd), __VA_ARGS__
#define RX_WORK_SUBMIT(queue, ...) ::mkwvk::work_submitted(queue), __VA_ARGS__
#define vkCmdPipelineBarrier2(...) (::mkwvk::rec::rx_vkCmdPipelineBarrier2(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdBeginRendering(...) (::mkwvk::rec::rx_vkCmdBeginRendering(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdEndRendering(...) (::mkwvk::rec::rx_vkCmdEndRendering(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDraw(...) (::mkwvk::rec::rx_vkCmdDraw(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDrawIndexed(...) (::mkwvk::rec::rx_vkCmdDrawIndexed(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDrawIndirect(...) (::mkwvk::rec::rx_vkCmdDrawIndirect(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDrawIndexedIndirect(...) (::mkwvk::rec::rx_vkCmdDrawIndexedIndirect(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDrawIndirectCount(...) (::mkwvk::rec::rx_vkCmdDrawIndirectCount(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDrawIndexedIndirectCount(...) (::mkwvk::rec::rx_vkCmdDrawIndexedIndirectCount(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDrawMeshTasksEXT(...) (::mkwvk::rec::rx_vkCmdDrawMeshTasksEXT(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDrawMeshTasksIndirectEXT(...) (::mkwvk::rec::rx_vkCmdDrawMeshTasksIndirectEXT(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDrawMeshTasksIndirectCountEXT(...) (::mkwvk::rec::rx_vkCmdDrawMeshTasksIndirectCountEXT(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDispatch(...) (::mkwvk::rec::rx_vkCmdDispatch(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdDispatchIndirect(...) (::mkwvk::rec::rx_vkCmdDispatchIndirect(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdCopyBuffer(...) (::mkwvk::rec::rx_vkCmdCopyBuffer(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdCopyBufferToImage(...) (::mkwvk::rec::rx_vkCmdCopyBufferToImage(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdCopyImageToBuffer(...) (::mkwvk::rec::rx_vkCmdCopyImageToBuffer(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdCopyImage(...) (::mkwvk::rec::rx_vkCmdCopyImage(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdBlitImage(...) (::mkwvk::rec::rx_vkCmdBlitImage(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdResolveImage(...) (::mkwvk::rec::rx_vkCmdResolveImage(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdClearColorImage(...) (::mkwvk::rec::rx_vkCmdClearColorImage(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdClearDepthStencilImage(...) (::mkwvk::rec::rx_vkCmdClearDepthStencilImage(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdClearAttachments(...) (::mkwvk::rec::rx_vkCmdClearAttachments(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdFillBuffer(...) (::mkwvk::rec::rx_vkCmdFillBuffer(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdUpdateBuffer(...) (::mkwvk::rec::rx_vkCmdUpdateBuffer(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdBeginQuery(...) (::mkwvk::rec::rx_vkCmdBeginQuery(RX_WORK_ON(__VA_ARGS__)))
#define vkCmdEndQuery(...) (::mkwvk::rec::rx_vkCmdEndQuery(RX_WORK_ON(__VA_ARGS__)))
// (RX_VK_RECORDER) the calls that set state, bind or mark: not work (no count), but in the command buffer in order with the work, so
// through the recorder too
#define vkCmdBindPipeline(...) (::mkwvk::rec::rx_vkCmdBindPipeline(__VA_ARGS__))
#define vkCmdBindDescriptorSets(...) (::mkwvk::rec::rx_vkCmdBindDescriptorSets(__VA_ARGS__))
#define vkCmdBindIndexBuffer(...) (::mkwvk::rec::rx_vkCmdBindIndexBuffer(__VA_ARGS__))
#define vkCmdBindVertexBuffers2(...) (::mkwvk::rec::rx_vkCmdBindVertexBuffers2(__VA_ARGS__))
#define vkCmdPushConstants(...) (::mkwvk::rec::rx_vkCmdPushConstants(__VA_ARGS__))
#define vkCmdPushDescriptorSetKHR(...) (::mkwvk::rec::rx_vkCmdPushDescriptorSetKHR(__VA_ARGS__))
#define vkCmdPushDescriptorSetWithTemplateKHR(...) (::mkwvk::rec::rx_vkCmdPushDescriptorSetWithTemplateKHR(__VA_ARGS__))
#define vkCmdSetViewport(...) (::mkwvk::rec::rx_vkCmdSetViewport(__VA_ARGS__))
#define vkCmdSetScissor(...) (::mkwvk::rec::rx_vkCmdSetScissor(__VA_ARGS__))
#define vkCmdSetViewportWithCount(...) (::mkwvk::rec::rx_vkCmdSetViewportWithCount(__VA_ARGS__))
#define vkCmdSetScissorWithCount(...) (::mkwvk::rec::rx_vkCmdSetScissorWithCount(__VA_ARGS__))
#define vkCmdSetBlendConstants(...) (::mkwvk::rec::rx_vkCmdSetBlendConstants(__VA_ARGS__))
#define vkCmdSetStencilReference(...) (::mkwvk::rec::rx_vkCmdSetStencilReference(__VA_ARGS__))
#define vkCmdSetStencilCompareMask(...) (::mkwvk::rec::rx_vkCmdSetStencilCompareMask(__VA_ARGS__))
#define vkCmdSetStencilWriteMask(...) (::mkwvk::rec::rx_vkCmdSetStencilWriteMask(__VA_ARGS__))
#define vkCmdSetStencilOp(...) (::mkwvk::rec::rx_vkCmdSetStencilOp(__VA_ARGS__))
#define vkCmdSetDepthBias(...) (::mkwvk::rec::rx_vkCmdSetDepthBias(__VA_ARGS__))
#define vkCmdSetDepthBiasEnable(...) (::mkwvk::rec::rx_vkCmdSetDepthBiasEnable(__VA_ARGS__))
#define vkCmdSetLineWidth(...) (::mkwvk::rec::rx_vkCmdSetLineWidth(__VA_ARGS__))
#define vkCmdSetDepthBoundsTestEnable(...) (::mkwvk::rec::rx_vkCmdSetDepthBoundsTestEnable(__VA_ARGS__))
#define vkCmdSetCullMode(...) (::mkwvk::rec::rx_vkCmdSetCullMode(__VA_ARGS__))
#define vkCmdSetFrontFace(...) (::mkwvk::rec::rx_vkCmdSetFrontFace(__VA_ARGS__))
#define vkCmdSetPrimitiveTopology(...) (::mkwvk::rec::rx_vkCmdSetPrimitiveTopology(__VA_ARGS__))
#define vkCmdSetPrimitiveRestartEnable(...) (::mkwvk::rec::rx_vkCmdSetPrimitiveRestartEnable(__VA_ARGS__))
#define vkCmdSetDepthTestEnable(...) (::mkwvk::rec::rx_vkCmdSetDepthTestEnable(__VA_ARGS__))
#define vkCmdSetDepthWriteEnable(...) (::mkwvk::rec::rx_vkCmdSetDepthWriteEnable(__VA_ARGS__))
#define vkCmdSetDepthCompareOp(...) (::mkwvk::rec::rx_vkCmdSetDepthCompareOp(__VA_ARGS__))
#define vkCmdSetStencilTestEnable(...) (::mkwvk::rec::rx_vkCmdSetStencilTestEnable(__VA_ARGS__))
#define vkCmdSetRasterizerDiscardEnable(...) (::mkwvk::rec::rx_vkCmdSetRasterizerDiscardEnable(__VA_ARGS__))
#define vkCmdSetPolygonModeEXT(...) (::mkwvk::rec::rx_vkCmdSetPolygonModeEXT(__VA_ARGS__))
#define vkCmdSetDepthClampEnableEXT(...) (::mkwvk::rec::rx_vkCmdSetDepthClampEnableEXT(__VA_ARGS__))
#define vkCmdSetAlphaToCoverageEnableEXT(...) (::mkwvk::rec::rx_vkCmdSetAlphaToCoverageEnableEXT(__VA_ARGS__))
#define vkCmdSetColorBlendEnableEXT(...) (::mkwvk::rec::rx_vkCmdSetColorBlendEnableEXT(__VA_ARGS__))
#define vkCmdSetColorBlendEquationEXT(...) (::mkwvk::rec::rx_vkCmdSetColorBlendEquationEXT(__VA_ARGS__))
#define vkCmdSetColorWriteMaskEXT(...) (::mkwvk::rec::rx_vkCmdSetColorWriteMaskEXT(__VA_ARGS__))
#define vkCmdSetFragmentShadingRateKHR(...) (::mkwvk::rec::rx_vkCmdSetFragmentShadingRateKHR(__VA_ARGS__))
#define vkCmdWriteBufferMarkerAMD(...) (::mkwvk::rec::rx_vkCmdWriteBufferMarkerAMD(__VA_ARGS__))
#define vkCmdSetCheckpointNV(...) (::mkwvk::rec::rx_vkCmdSetCheckpointNV(__VA_ARGS__))
#define vkCmdWriteTimestamp2(...) (::mkwvk::rec::rx_vkCmdWriteTimestamp2(__VA_ARGS__))
#define vkCmdResetQueryPool(...) (::mkwvk::rec::rx_vkCmdResetQueryPool(__VA_ARGS__))
// (RX_VK_RECORDER=defer) the calls that begin, end or reset command buffers, submit, present or wait, read back what the GPU did,
// or let go of what recorded commands use: the thread's held packets reach the driver first (rec::boundary(): a relaxed load and
// a branch unless deferred; RX_VK_RECORDER=thread: the replay lock's holder waits for the recorder thread to run all it put in its ring;
// the kind is for the recorder's stats line). RX_REC_BOUNDARIES in vk_recorder.h lists them
#define vkBeginCommandBuffer(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkBeginCommandBuffer), ::vkx::vkBeginCommandBuffer(RX_WORK_BEGUN(__VA_ARGS__)))
#define vkEndCommandBuffer(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkEndCommandBuffer), ::vkx::vkEndCommandBuffer(__VA_ARGS__))
#define vkResetCommandPool(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkResetCommandPool), ::vkx::vkResetCommandPool(__VA_ARGS__))
#define vkDestroyCommandPool(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyCommandPool), ::vkx::vkDestroyCommandPool(__VA_ARGS__))
#define vkQueueSubmit2(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkQueueSubmit2), ::vkx::vkQueueSubmit2(RX_WORK_SUBMIT(__VA_ARGS__)))
#define vkQueueWaitIdle(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkQueueWaitIdle), ::mkwvk::gpu_waits_vk::queue_idle(::vkx::vkQueueWaitIdle, __VA_ARGS__))
#define vkQueueBindSparse(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkQueueBindSparse), ::vkx::vkQueueBindSparse(__VA_ARGS__))
#define vkQueuePresentKHR(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkQueuePresentKHR), ::vkx::vkQueuePresentKHR(__VA_ARGS__))
#define vkAcquireNextImageKHR(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkAcquireNextImageKHR), ::vkx::vkAcquireNextImageKHR(__VA_ARGS__))
#define vkDeviceWaitIdle(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDeviceWaitIdle), ::mkwvk::gpu_waits_vk::device_idle(::vkx::vkDeviceWaitIdle, __VA_ARGS__))
#define vkWaitForFences(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkWaitForFences), ::mkwvk::gpu_waits_vk::fences(::vkx::vkWaitForFences, __VA_ARGS__))
#define vkResetFences(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkResetFences), ::vkx::vkResetFences(__VA_ARGS__))
#define vkWaitSemaphores(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkWaitSemaphores), ::mkwvk::gpu_waits_vk::semaphores(::vkx::vkWaitSemaphores, __VA_ARGS__))
#define vkGetSemaphoreCounterValue(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkGetSemaphoreCounterValue), ::vkx::vkGetSemaphoreCounterValue(__VA_ARGS__))
#define vkGetQueryPoolResults(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkGetQueryPoolResults), ::mkwvk::gpu_waits_vk::query_results(::vkx::vkGetQueryPoolResults, __VA_ARGS__))
#define vkResetQueryPool(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkResetQueryPool), ::vkx::vkResetQueryPool(__VA_ARGS__))
#define vkGetQueueCheckpointDataNV(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkGetQueueCheckpointDataNV), ::vkx::vkGetQueueCheckpointDataNV(__VA_ARGS__))
#define vkGetDeviceFaultInfoEXT(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkGetDeviceFaultInfoEXT), ::vkx::vkGetDeviceFaultInfoEXT(__VA_ARGS__))
#define vkResetDescriptorPool(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkResetDescriptorPool), ::vkx::vkResetDescriptorPool(__VA_ARGS__))
#define vkDestroyBuffer(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyBuffer), ::vkx::vkDestroyBuffer(__VA_ARGS__))
#define vkDestroyImage(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyImage), ::vkx::vkDestroyImage(__VA_ARGS__))
#define vkDestroyImageView(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyImageView), ::vkx::vkDestroyImageView(__VA_ARGS__))
#define vkDestroyBufferView(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyBufferView), ::vkx::vkDestroyBufferView(__VA_ARGS__))
#define vkDestroySampler(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroySampler), ::vkx::vkDestroySampler(__VA_ARGS__))
#define vkDestroyPipeline(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyPipeline), ::vkx::vkDestroyPipeline(__VA_ARGS__))
#define vkDestroyPipelineLayout(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyPipelineLayout), ::vkx::vkDestroyPipelineLayout(__VA_ARGS__))
#define vkDestroyDescriptorSetLayout(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyDescriptorSetLayout), ::vkx::vkDestroyDescriptorSetLayout(__VA_ARGS__))
#define vkDestroyDescriptorPool(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyDescriptorPool), ::vkx::vkDestroyDescriptorPool(__VA_ARGS__))
#define vkDestroyQueryPool(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyQueryPool), ::vkx::vkDestroyQueryPool(__VA_ARGS__))
#define vkDestroyShaderModule(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyShaderModule), ::vkx::vkDestroyShaderModule(__VA_ARGS__))
#define vkDestroyPipelineCache(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyPipelineCache), ::vkx::vkDestroyPipelineCache(__VA_ARGS__))
#define vkDestroyFence(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyFence), ::vkx::vkDestroyFence(__VA_ARGS__))
#define vkDestroySemaphore(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroySemaphore), ::vkx::vkDestroySemaphore(__VA_ARGS__))
#define vkDestroySwapchainKHR(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroySwapchainKHR), ::vkx::vkDestroySwapchainKHR(__VA_ARGS__))
#define vkDestroySurfaceKHR(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroySurfaceKHR), ::vkx::vkDestroySurfaceKHR(__VA_ARGS__))
#define vkDestroyDevice(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyDevice), ::vkx::vkDestroyDevice(__VA_ARGS__))
#define vkDestroyInstance(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkDestroyInstance), ::vkx::vkDestroyInstance(__VA_ARGS__))
#define vkFreeMemory(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vkFreeMemory), ::vkx::vkFreeMemory(__VA_ARGS__))
#define vmaDestroyBuffer(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vmaDestroyBuffer), ::vmaDestroyBuffer(__VA_ARGS__))
#define vmaDestroyImage(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vmaDestroyImage), ::vmaDestroyImage(__VA_ARGS__))
#define vmaInvalidateAllocation(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vmaInvalidateAllocation), ::vmaInvalidateAllocation(__VA_ARGS__))
// (Triforce: the memory of a mirror's piece, which recorded copies and bindings use, and the blocks made ahead: vk_vma.cpp)
#define vmaFreeMemory(...) (::mkwvk::rec::boundary(::mkwvk::rec::B_vmaFreeMemory), ::vmaFreeMemory(__VA_ARGS__))