Skip to content

Commit 211ada8

Browse files
committed
Fix image upload sync bugs
1 parent 4319366 commit 211ada8

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
package net.vulkanmod.mixin.texture.update;
22

3-
import com.mojang.blaze3d.platform.NativeImage;
43
import net.minecraft.client.renderer.texture.SpriteContents;
54
import net.vulkanmod.render.texture.SpriteUpdateUtil;
65
import net.vulkanmod.vulkan.texture.VTextureSelector;
6+
import org.spongepowered.asm.mixin.Final;
77
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.Shadow;
89
import org.spongepowered.asm.mixin.injection.At;
910
import org.spongepowered.asm.mixin.injection.Inject;
1011
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1112

12-
@Mixin(SpriteContents.class)
13+
@Mixin(SpriteContents.Ticker.class)
1314
public class MSpriteContents {
1415

15-
@Inject(method = "upload", at = @At("HEAD"), cancellable = true)
16-
private void checkUpload(int i, int j, int k, int l, NativeImage[] nativeImages, CallbackInfo ci) {
17-
if (!SpriteUpdateUtil.shouldUpload()) {
16+
@Shadow int subFrame;
17+
@Shadow int frame;
18+
@Shadow @Final SpriteContents.AnimatedTexture animationInfo;
19+
20+
@Inject(method = "tickAndUpload", at = @At("HEAD"), cancellable = true)
21+
private void checkUpload(int i, int j, CallbackInfo ci) {
22+
if (!SpriteUpdateUtil.doUploadFrame()) {
23+
// Update animations frames even if no upload is scheduled
24+
++this.subFrame;
25+
SpriteContents.FrameInfo frameInfo = this.animationInfo.frames.get(this.frame);
26+
if (this.subFrame >= frameInfo.time) {
27+
this.frame = (this.frame + 1) % this.animationInfo.frames.size();
28+
this.subFrame = 0;
29+
}
30+
1831
ci.cancel();
1932
}
2033
else {
21-
SpriteUpdateUtil.addTransitionedLayout(VTextureSelector.getBoundTexture(0));
34+
SpriteUpdateUtil.addTransitionedLayout(VTextureSelector.getBoundTexture());
2235
}
2336
}
2437
}

src/main/java/net/vulkanmod/render/texture/SpriteUpdateUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
public abstract class SpriteUpdateUtil {
1111

12-
private static boolean doUpload = false;
12+
private static boolean doUpload = true;
1313
private static final Set<VulkanImage> transitionedLayouts = new HashSet<>();
1414

1515
public static void setDoUpload(boolean b) {
1616
doUpload = b;
1717
}
1818

19-
public static boolean shouldUpload() {
19+
public static boolean doUploadFrame() {
2020
return doUpload;
2121
}
2222

src/main/java/net/vulkanmod/vulkan/Renderer.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ private void createSyncObjects() {
182182
for (int i = 0; i < framesNum; i++) {
183183

184184
if (vkCreateSemaphore(device, semaphoreInfo, null, pImageAvailableSemaphore) != VK_SUCCESS
185-
|| vkCreateSemaphore(device, semaphoreInfo, null, pRenderFinishedSemaphore) != VK_SUCCESS
186-
|| vkCreateFence(device, fenceInfo, null, pFence) != VK_SUCCESS) {
185+
|| vkCreateSemaphore(device, semaphoreInfo, null, pRenderFinishedSemaphore) != VK_SUCCESS
186+
|| vkCreateFence(device, fenceInfo, null, pFence) != VK_SUCCESS) {
187187

188188
throw new RuntimeException("Failed to create synchronization objects for the frame: " + i);
189189
}
@@ -206,7 +206,7 @@ public void preInitFrame() {
206206
// runTick might be called recursively,
207207
// this check forces sync to avoid upload corruption
208208
if (lastReset == currentFrame) {
209-
Synchronization.INSTANCE.waitFences();
209+
waitFences();
210210
}
211211
lastReset = currentFrame;
212212

@@ -256,7 +256,7 @@ public void beginFrame() {
256256
IntBuffer pImageIndex = stack.mallocInt(1);
257257

258258
int vkResult = vkAcquireNextImageKHR(device, swapChain.getId(), VUtil.UINT64_MAX,
259-
imageAvailableSemaphores.get(currentFrame), VK_NULL_HANDLE, pImageIndex);
259+
imageAvailableSemaphores.get(currentFrame), VK_NULL_HANDLE, pImageIndex);
260260

261261
if (vkResult == VK_SUBOPTIMAL_KHR || vkResult == VK_ERROR_OUT_OF_DATE_KHR || swapChainUpdate) {
262262
swapChainUpdate = true;
@@ -303,10 +303,7 @@ public void endFrame() {
303303

304304
mainPass.end(currentCmdBuffer);
305305

306-
// Make sure there are no uploads/transitions scheduled
307-
ImageUploadHelper.INSTANCE.submitCommands();
308-
Synchronization.INSTANCE.waitFences();
309-
Vulkan.getStagingBuffer().reset();
306+
waitFences();
310307

311308
submitFrame();
312309
recordingCmds = false;
@@ -362,7 +359,7 @@ private void submitFrame() {
362359
}
363360

364361
/**
365-
* Called in case draw results are needed before the of the frame
362+
* Called in case draw results are needed before the of the frame
366363
*/
367364
public void flushCmds() {
368365
if (!this.recordingCmds)
@@ -381,7 +378,7 @@ public void flushCmds() {
381378

382379
vkResetFences(device, inFlightFences.get(currentFrame));
383380

384-
Synchronization.INSTANCE.waitFences();
381+
waitFences();
385382

386383
if ((vkResult = vkQueueSubmit(DeviceManager.getGraphicsQueue().queue(), submitInfo, inFlightFences.get(currentFrame))) != VK_SUCCESS) {
387384
vkResetFences(device, inFlightFences.get(currentFrame));
@@ -437,6 +434,13 @@ public void removeUsedPipeline(Pipeline pipeline) {
437434
usedPipelines.remove(pipeline);
438435
}
439436

437+
private void waitFences() {
438+
// Make sure there are no uploads/transitions scheduled
439+
ImageUploadHelper.INSTANCE.submitCommands();
440+
Synchronization.INSTANCE.waitFences();
441+
Vulkan.getStagingBuffer().reset();
442+
}
443+
440444
private void resetDescriptors() {
441445
for (Pipeline pipeline : usedPipelines) {
442446
pipeline.resetDescriptorPool(currentFrame);
@@ -454,9 +458,9 @@ void waitForSwapChain() {
454458
try (MemoryStack stack = MemoryStack.stackPush()) {
455459
//Empty Submit
456460
VkSubmitInfo info = VkSubmitInfo.calloc(stack)
457-
.sType$Default()
458-
.pWaitSemaphores(stack.longs(imageAvailableSemaphores.get(currentFrame)))
459-
.pWaitDstStageMask(stack.ints(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT));
461+
.sType$Default()
462+
.pWaitSemaphores(stack.longs(imageAvailableSemaphores.get(currentFrame)))
463+
.pWaitDstStageMask(stack.ints(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT));
460464

461465
vkQueueSubmit(DeviceManager.getGraphicsQueue().queue(), info, inFlightFences.get(currentFrame));
462466
vkWaitForFences(device, inFlightFences.get(currentFrame), true, -1);
@@ -465,7 +469,7 @@ void waitForSwapChain() {
465469

466470
@SuppressWarnings("UnreachableCode")
467471
private void recreateSwapChain() {
468-
Synchronization.INSTANCE.waitFences();
472+
waitFences();
469473
Vulkan.waitIdle();
470474

471475
commandBuffers.forEach(commandBuffer -> vkResetCommandBuffer(commandBuffer, 0));

src/main/java/net/vulkanmod/vulkan/texture/VTextureSelector.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public static void setActiveTexture(int activeTexture) {
105105
VTextureSelector.activeTexture = activeTexture;
106106
}
107107

108+
public static VulkanImage getBoundTexture() { return boundTextures[activeTexture]; }
109+
108110
public static VulkanImage getBoundTexture(int i) { return boundTextures[i]; }
109111

110112
public static VulkanImage getWhiteTexture() { return whiteTexture; }

src/main/resources/vulkanmod.accesswidener

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ accessible class net/minecraft/client/gui/Gui$HeartType
66

77
accessible class net/minecraft/client/renderer/OutlineBufferSource$EntityOutlineGenerator
88

9+
accessible class net/minecraft/client/renderer/texture/SpriteContents$Ticker
10+
accessible class net/minecraft/client/renderer/texture/SpriteContents$FrameInfo
11+
accessible class net/minecraft/client/renderer/texture/SpriteContents$AnimatedTexture
12+
accessible field net/minecraft/client/renderer/texture/SpriteContents$AnimatedTexture frames Ljava/util/List;
13+
accessible field net/minecraft/client/renderer/texture/SpriteContents$FrameInfo time I
14+
915
#1.20
1016
accessible field com/mojang/blaze3d/systems/RenderSystem vertexSorting Lcom/mojang/blaze3d/vertex/VertexSorting;
1117
accessible field net/minecraft/client/renderer/RenderStateShard name Ljava/lang/String;

0 commit comments

Comments
 (0)