Skip to content

Commit 28e975e

Browse files
committed
GPU Vulkan Display: don't recreate command buffer for mixing images
1 parent 1def08d commit 28e975e

4 files changed

Lines changed: 25 additions & 14 deletions

File tree

‎GPU/GPUTracking/display/GPUDisplay.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ class GPUDisplay : public GPUDisplayInterface
297297
HighResTimer mAnimationTimer;
298298
int32_t mAnimationFrame = 0;
299299
int32_t mAnimationLastBase = 0;
300+
int32_t mAnimationMixBase = -1; // Target keyframe of the current config blend (mixing), -1 = none
300301
int32_t mAnimateScreenshot = 0;
301302
int32_t mAnimationExport = 0;
302303
bool mAnimationChangeConfig = true;

‎GPU/GPUTracking/display/backend/GPUDisplayBackendVulkan.cxx‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,8 @@ void GPUDisplayBackendVulkan::createCommandBuffers()
623623
allocInfo.commandPool = mCommandPool;
624624
allocInfo.level = vk::CommandBufferLevel::ePrimary;
625625
allocInfo.commandBufferCount = mImageCount;
626-
mCommandBufferUpToDate.resize(mImageCount, false);
626+
mCommandBufferUpToDate.assign(mImageCount, false);
627+
mCommandBufferMixUpToDate.assign(mImageCount, false);
627628
mCommandBuffers = mDevice.allocateCommandBuffers(allocInfo);
628629
mCommandBuffersText = mDevice.allocateCommandBuffers(allocInfo);
629630
mCommandBuffersTexture = mDevice.allocateCommandBuffers(allocInfo);
@@ -1448,7 +1449,7 @@ uint32_t GPUDisplayBackendVulkan::drawVertices(const vboList& v, const drawType
14481449
if (count == 0) {
14491450
return 0;
14501451
}
1451-
if (mCommandBufferUpToDate[mCurrentImageIndex]) {
1452+
if (upToDate()[mCurrentImageIndex]) {
14521453
return count;
14531454
}
14541455

@@ -1470,7 +1471,7 @@ uint32_t GPUDisplayBackendVulkan::drawVertices(const vboList& v, const drawType
14701471

14711472
bool GPUDisplayBackendVulkan::prepareDraw(const hmm_mat4& proj, const hmm_mat4& view, bool requestScreenshot, bool toMixBuffer, float includeMixImage)
14721473
{
1473-
if (mDisplay->updateDrawCommands() || toMixBuffer || includeMixImage > 0) {
1474+
if (mDisplay->updateDrawCommands()) { // Main and mix command buffers stay cached while mixing, only the blend pass with the alpha value is recorded per frame
14741475
needRecordCommandBuffers();
14751476
}
14761477

@@ -1535,7 +1536,8 @@ bool GPUDisplayBackendVulkan::prepareDraw(const hmm_mat4& proj, const hmm_mat4&
15351536

15361537
mCurrentCommandBuffer = toMixBuffer ? mCommandBuffersMix[mCurrentImageIndex] : mCommandBuffers[mCurrentImageIndex];
15371538
mCurrentCommandBufferLastPipeline = -1;
1538-
if (!mCommandBufferUpToDate[mCurrentImageIndex]) {
1539+
mRecordingMix = toMixBuffer;
1540+
if (!upToDate()[mCurrentImageIndex]) {
15391541
startFillCommandBuffer(mCurrentCommandBuffer, mCurrentImageIndex, toMixBuffer);
15401542
}
15411543
return true;
@@ -1553,11 +1555,9 @@ void GPUDisplayBackendVulkan::handleSwapChainResult(vk::Result retVal)
15531555

15541556
void GPUDisplayBackendVulkan::finishDraw(bool doScreenshot, bool toMixBuffer, float includeMixImage)
15551557
{
1556-
if (!mCommandBufferUpToDate[mCurrentImageIndex]) {
1558+
if (!upToDate()[mCurrentImageIndex]) {
15571559
endFillCommandBuffer(mCurrentCommandBuffer);
1558-
if (!toMixBuffer && includeMixImage == 0.f) {
1559-
mCommandBufferUpToDate[mCurrentImageIndex] = true;
1560-
}
1560+
upToDate()[mCurrentImageIndex] = true;
15611561
}
15621562
}
15631563

@@ -1762,15 +1762,15 @@ void GPUDisplayBackendVulkan::mixImages(vk::CommandBuffer commandBuffer, float m
17621762

17631763
void GPUDisplayBackendVulkan::ActivateColor(std::array<float, 4>& color)
17641764
{
1765-
if (mCommandBufferUpToDate[mCurrentImageIndex]) {
1765+
if (upToDate()[mCurrentImageIndex]) {
17661766
return;
17671767
}
17681768
mCurrentCommandBuffer.pushConstants(mPipelineLayout, vk::ShaderStageFlagBits::eFragment, 0, sizeof(color), color.data());
17691769
}
17701770

17711771
void GPUDisplayBackendVulkan::pointSizeFactor(float factor)
17721772
{
1773-
if (mCommandBufferUpToDate[mCurrentImageIndex]) {
1773+
if (upToDate()[mCurrentImageIndex]) {
17741774
return;
17751775
}
17761776
float size = std::min(mDisplay->cfgL().pointSize * mDownsampleFactor * factor + (mDisplay->cfgL().smoothPoints ? SMOOTH_POINT_EXTRA_SIZE : 0.f), mMaxPointSize);
@@ -1789,7 +1789,7 @@ bool GPUDisplayBackendVulkan::useSmoothLineShader() const
17891789

17901790
void GPUDisplayBackendVulkan::lineWidthFactor(float factor)
17911791
{
1792-
if (mCommandBufferUpToDate[mCurrentImageIndex]) {
1792+
if (upToDate()[mCurrentImageIndex]) {
17931793
return;
17941794
}
17951795
float width = mDisplay->cfgL().lineWidth * mDownsampleFactor * factor;
@@ -1805,6 +1805,7 @@ void GPUDisplayBackendVulkan::lineWidthFactor(float factor)
18051805
void GPUDisplayBackendVulkan::needRecordCommandBuffers()
18061806
{
18071807
std::fill(mCommandBufferUpToDate.begin(), mCommandBufferUpToDate.end(), false);
1808+
std::fill(mCommandBufferMixUpToDate.begin(), mCommandBufferMixUpToDate.end(), false);
18081809
}
18091810

18101811
void GPUDisplayBackendVulkan::addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data)
@@ -2027,5 +2028,5 @@ uint32_t GPUDisplayBackendVulkan::DepthBits()
20272028

20282029
bool GPUDisplayBackendVulkan::backendNeedRedraw()
20292030
{
2030-
return !mCommandBufferUpToDate[mCurrentImageIndex];
2031+
return !upToDate()[mCurrentImageIndex];
20312032
}

‎GPU/GPUTracking/display/backend/GPUDisplayBackendVulkan.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ class GPUDisplayBackendVulkan : public GPUDisplayBackend
185185
std::vector<vk::CommandBuffer> mCommandBuffersMix;
186186
std::vector<vk::CommandBuffer> mCommandBuffersPresent;
187187
std::vector<vk::CommandBufferSubmitInfo> mFrameCommandBuffers; // All command buffers of the current frame, submitted as one batch, ordered by barriers
188-
std::vector<bool> mCommandBufferUpToDate;
188+
std::vector<bool> mCommandBufferUpToDate; // Per image: main command buffer recorded and still valid (cached across frames)
189+
std::vector<bool> mCommandBufferMixUpToDate; // Per image: same for the mix buffer command buffer (animation config blending)
190+
bool mRecordingMix = false; // Current command buffer renders to the mix buffer
191+
std::vector<bool>& upToDate() { return mRecordingMix ? mCommandBufferMixUpToDate : mCommandBufferUpToDate; }
189192
std::vector<vk::Semaphore> mImageAvailableSemaphore;
190193
std::vector<vk::Semaphore> mPresentSemaphore;
191194
vk::Semaphore mTimelineSemaphore; // Signaled with an increasing value by every queue submit, replaces fences

‎GPU/GPUTracking/display/helpers/GPUDisplayAnimation.cxx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ void GPUDisplay::startAnimation()
102102
mAnimationFrame = 0;
103103
mAnimate = 1;
104104
mAnimationLastBase = 0;
105+
mAnimationMixBase = -1;
105106
}
106107

107108
int32_t GPUDisplay::animateCamera(float& animateTime, float& mixSlaveImage, hmm_mat4& nextViewMatrix)
@@ -141,7 +142,12 @@ int32_t GPUDisplay::animateCamera(float& animateTime, float& mixSlaveImage, hmm_
141142
mAnimationLastBase = base - 1;
142143
}
143144

144-
if (base != mAnimationLastBase && mAnimateVectors[0][mAnimationLastBase] != mAnimateVectors[0][base] && memcmp(&mAnimateConfig[base], &mAnimateConfig[mAnimationLastBase], sizeof(mAnimateConfig[base]))) {
145+
const bool mixing = base != mAnimationLastBase && mAnimateVectors[0][mAnimationLastBase] != mAnimateVectors[0][base] && memcmp(&mAnimateConfig[base], &mAnimateConfig[mAnimationLastBase], sizeof(mAnimateConfig[base]));
146+
if ((mixing ? base : -1) != mAnimationMixBase) { // Blended configs changed: cached (Vulkan) command buffers must be re-recorded, otherwise they are kept while mixing
147+
mAnimationMixBase = mixing ? base : -1;
148+
mUpdateDrawCommands = true;
149+
}
150+
if (mixing) {
145151
mixSlaveImage = 1.f - (time - mAnimateVectors[0][mAnimationLastBase]) / (mAnimateVectors[0][base] - mAnimateVectors[0][mAnimationLastBase]);
146152
if (mixSlaveImage > 0) {
147153
mCfgL = mAnimateConfig[mAnimationLastBase];

0 commit comments

Comments
 (0)