Skip to content

Commit 4ad7a97

Browse files
committed
Improve frame flow
1 parent fdbf57f commit 4ad7a97

File tree

8 files changed

+85
-87
lines changed

8 files changed

+85
-87
lines changed

src/main/java/net/vulkanmod/gl/VkGlFramebuffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public static VkGlFramebuffer getFramebuffer(int id) {
157157
}
158158

159159
boolean beginRendering() {
160-
return Renderer.getInstance().beginRendering(this.renderPass, this.framebuffer);
160+
return Renderer.getInstance().beginRenderPass(this.renderPass, this.framebuffer);
161161
}
162162

163163
public void setAttachmentTexture(int attachment, int id) {

src/main/java/net/vulkanmod/mixin/render/frame/MinecraftMixin.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.mojang.blaze3d.systems.CommandEncoder;
55
import com.mojang.blaze3d.textures.GpuTexture;
66
import net.minecraft.client.Minecraft;
7+
import net.vulkanmod.render.texture.ImageUploadHelper;
78
import net.vulkanmod.vulkan.Renderer;
89
import org.lwjgl.opengl.GL11;
910
import org.spongepowered.asm.mixin.Mixin;
10-
import org.spongepowered.asm.mixin.Shadow;
1111
import org.spongepowered.asm.mixin.injection.At;
1212
import org.spongepowered.asm.mixin.injection.Inject;
1313
import org.spongepowered.asm.mixin.injection.Redirect;
@@ -16,23 +16,17 @@
1616
@Mixin(Minecraft.class)
1717
public class MinecraftMixin {
1818

19-
@Shadow public boolean noRender;
20-
2119
@Inject(method = "runTick", at = @At(value = "HEAD"))
22-
private void preFrameOps(boolean bl, CallbackInfo ci) {
23-
Renderer.getInstance().preInitFrame();
24-
}
25-
26-
//Main target (framebuffer) ops
27-
@Redirect(method = "runTick", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/CommandEncoder;clearColorAndDepthTextures(Lcom/mojang/blaze3d/textures/GpuTexture;ILcom/mojang/blaze3d/textures/GpuTexture;D)V"))
28-
private void beginRender(CommandEncoder instance, GpuTexture gpuTexture, int i, GpuTexture gpuTexture2, double v) {
20+
private void beginFrame(boolean bl, CallbackInfo ci) {
2921
Renderer.getInstance().beginFrame();
3022
Renderer.clearAttachments(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_COLOR_BUFFER_BIT);
3123
}
3224

33-
@Inject(method = "disconnect(Lnet/minecraft/client/gui/screens/Screen;Z)V", at = @At(value = "RETURN"))
34-
private void beginRender2(CallbackInfo ci) {
35-
Renderer.getInstance().beginFrame();
25+
@Redirect(method = "runTick", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/CommandEncoder;clearColorAndDepthTextures(Lcom/mojang/blaze3d/textures/GpuTexture;ILcom/mojang/blaze3d/textures/GpuTexture;D)V"))
26+
private void redirectClear(CommandEncoder instance, GpuTexture gpuTexture, int i, GpuTexture gpuTexture2, double v) {
27+
// Remove framebuffer clear as it's not needed
28+
29+
ImageUploadHelper.INSTANCE.submitCommands();
3630
}
3731

3832
@Redirect(method = "runTick", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/pipeline/RenderTarget;blitToScreen()V"))

src/main/java/net/vulkanmod/mixin/texture/update/GameRendererM.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/main/java/net/vulkanmod/render/engine/VkCommandEncoder.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,36 @@ public RenderPass createRenderPass(Supplier<String> supplier, GpuTextureView col
142142
}
143143

144144
@Override
145-
public void clearColorTexture(GpuTexture colorAttachment, int color) {
145+
public void clearColorTexture(GpuTexture colorAttachment, int clearColor) {
146146
if (this.inRenderPass) {
147147
throw new IllegalStateException("Close the existing render pass before creating a new one!");
148148
}
149149
else if (Renderer.isRecording()) {
150-
VkGpuTexture vkGpuTexture = (VkGpuTexture) colorAttachment;
151-
VkGlFramebuffer.bindFramebuffer(GL30.GL_FRAMEBUFFER, framebufferId);
152-
VkGlFramebuffer.framebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, vkGpuTexture.glId(), 0);
150+
if (Minecraft.getInstance().getMainRenderTarget().getColorTexture() == colorAttachment) {
151+
Renderer.getInstance().getMainPass().rebindMainTarget();
152+
153+
VRenderSystem.setClearColor(ARGB.redFloat(clearColor), ARGB.greenFloat(clearColor), ARGB.blueFloat(clearColor), ARGB.alphaFloat(clearColor));
154+
Renderer.clearAttachments(0x4000);
155+
}
156+
else {
157+
VkGpuTexture vkGpuTexture = (VkGpuTexture) colorAttachment;
158+
VkGlFramebuffer.bindFramebuffer(GL30.GL_FRAMEBUFFER, framebufferId);
159+
VkGlFramebuffer.framebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, vkGpuTexture.glId(), 0);
160+
161+
VkGlFramebuffer.beginRendering(VkGlFramebuffer.getFramebuffer(framebufferId));
162+
VRenderSystem.setClearColor(ARGB.redFloat(clearColor), ARGB.greenFloat(clearColor), ARGB.blueFloat(clearColor), ARGB.alphaFloat(clearColor));
163+
Renderer.clearAttachments(0x4000);
164+
Renderer.getInstance().endRenderPass();
153165

154-
VkGlFramebuffer.beginRendering(VkGlFramebuffer.getFramebuffer(framebufferId));
155-
VRenderSystem.setClearColor(ARGB.redFloat(color), ARGB.greenFloat(color), ARGB.blueFloat(color), ARGB.alphaFloat(color));
156-
Renderer.clearAttachments(16384);
157-
Renderer.getInstance().endRenderPass();
166+
VkFbo fbo = ((VkGpuTexture)colorAttachment).getFbo(null);
167+
168+
((VkGpuTexture) colorAttachment).setClearColor(clearColor);
169+
170+
Framebuffer boundFramebuffer = Renderer.getInstance().getBoundFramebuffer();
171+
if (boundFramebuffer != null && boundFramebuffer.getColorAttachment() == ((VkGpuTexture) colorAttachment).getVulkanImage()) {
172+
fbo.clearAttachments();
173+
}
174+
}
158175
}
159176
else {
160177
GraphicsQueue graphicsQueue = DeviceManager.getGraphicsQueue();
@@ -171,14 +188,13 @@ else if (Renderer.isRecording()) {
171188
framebuffer.beginRenderPass(commandBuffer.handle, renderPass, stack);
172189
}
173190

174-
VRenderSystem.setClearColor(ARGB.redFloat(color), ARGB.greenFloat(color), ARGB.blueFloat(color), ARGB.alphaFloat(color));
175-
Renderer.clearAttachments(commandBuffer.handle, 16384);
191+
VRenderSystem.setClearColor(ARGB.redFloat(clearColor), ARGB.greenFloat(clearColor), ARGB.blueFloat(clearColor), ARGB.alphaFloat(clearColor));
192+
Renderer.clearAttachments(commandBuffer.handle, 0x4000, 0, 0, framebuffer.getWidth(), framebuffer.getHeight());
176193
renderPass.endRenderPass(commandBuffer.handle);
177194

178195
long fence = graphicsQueue.submitCommands(commandBuffer);
179196
Synchronization.waitFence(fence);
180197
}
181-
182198
}
183199

184200
@Override

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

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static int getCurrentImage() {
8686
private SwapChain swapChain;
8787

8888
private int framesNum;
89-
private List<VkCommandBuffer> commandBuffers;
89+
private List<VkCommandBuffer> mainCommandBuffers;
9090
private ArrayList<Long> imageAvailableSemaphores;
9191
private ArrayList<Long> renderFinishedSemaphores;
9292
private ArrayList<Long> inFlightFences;
@@ -100,6 +100,7 @@ public static int getCurrentImage() {
100100
private static int lastReset = -1;
101101
private VkCommandBuffer currentCmdBuffer;
102102
private boolean recordingCmds = false;
103+
int recursion = 0;
103104

104105
MainPass mainPass;
105106

@@ -136,11 +137,11 @@ private void init() {
136137
}
137138

138139
private void allocateCommandBuffers() {
139-
if (commandBuffers != null) {
140-
commandBuffers.forEach(commandBuffer -> vkFreeCommandBuffers(device, Vulkan.getCommandPool(), commandBuffer));
140+
if (mainCommandBuffers != null) {
141+
mainCommandBuffers.forEach(commandBuffer -> vkFreeCommandBuffers(device, Vulkan.getCommandPool(), commandBuffer));
141142
}
142143

143-
commandBuffers = new ArrayList<>(framesNum);
144+
mainCommandBuffers = new ArrayList<>(framesNum);
144145

145146
try (MemoryStack stack = stackPush()) {
146147
VkCommandBufferAllocateInfo allocInfo = VkCommandBufferAllocateInfo.calloc(stack);
@@ -157,7 +158,7 @@ private void allocateCommandBuffers() {
157158
}
158159

159160
for (int i = 0; i < framesNum; i++) {
160-
commandBuffers.add(new VkCommandBuffer(pCommandBuffers.get(i), device));
161+
mainCommandBuffers.add(new VkCommandBuffer(pCommandBuffers.get(i), device));
161162
}
162163
}
163164

@@ -232,10 +233,6 @@ public void preInitFrame() {
232233
}
233234

234235
public void beginFrame() {
235-
Profiler p = Profiler.getMainProfiler();
236-
p.pop();
237-
p.push("Frame_fence");
238-
239236
if (swapChainUpdate) {
240237
recreateSwapChain();
241238
swapChainUpdate = false;
@@ -249,9 +246,22 @@ public void beginFrame() {
249246
}
250247
}
251248

252-
253-
if (skipRendering || recordingCmds)
249+
if (skipRendering) {
254250
return;
251+
}
252+
253+
this.recursion++;
254+
255+
// In case this is a recursive call end prev frame
256+
if (this.recursion > 1) {
257+
this.endFrame();
258+
}
259+
260+
this.preInitFrame();
261+
262+
Profiler p = Profiler.getMainProfiler();
263+
p.pop();
264+
p.push("Frame_fence");
255265

256266
vkWaitForFences(device, inFlightFences.get(currentFrame), true, VUtil.UINT64_MAX);
257267

@@ -263,11 +273,10 @@ public void beginFrame() {
263273

264274
resetDescriptors();
265275

266-
currentCmdBuffer = commandBuffers.get(currentFrame);
276+
currentCmdBuffer = mainCommandBuffers.get(currentFrame);
267277
vkResetCommandBuffer(currentCmdBuffer, 0);
268278

269279
try (MemoryStack stack = stackPush()) {
270-
271280
IntBuffer pImageIndex = stack.mallocInt(1);
272281

273282
int vkResult = vkAcquireNextImageKHR(device, swapChain.getId(), VUtil.UINT64_MAX,
@@ -276,7 +285,7 @@ public void beginFrame() {
276285
if (vkResult == VK_SUBOPTIMAL_KHR || vkResult == VK_ERROR_OUT_OF_DATE_KHR || swapChainUpdate) {
277286
swapChainUpdate = true;
278287
skipRendering = true;
279-
beginFrame();
288+
this.beginFrame();
280289

281290
return;
282291
} else if (vkResult != VK_SUCCESS) {
@@ -285,13 +294,13 @@ public void beginFrame() {
285294

286295
imageIndex = pImageIndex.get(0);
287296

288-
this.beginRenderPass(stack);
297+
this.beginMainRenderPass(stack);
289298
}
290299

291300
p.pop();
292301
}
293302

294-
private void beginRenderPass(MemoryStack stack) {
303+
private void beginMainRenderPass(MemoryStack stack) {
295304
VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.calloc(stack);
296305
beginInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO);
297306
beginInfo.flags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
@@ -313,6 +322,11 @@ public void endFrame() {
313322
if (skipRendering || !recordingCmds)
314323
return;
315324

325+
if (this.recursion == 0) {
326+
return;
327+
}
328+
this.recursion--;
329+
316330
Profiler p = Profiler.getMainProfiler();
317331
p.push("End_rendering");
318332

@@ -418,7 +432,7 @@ public void flushCmds() {
418432

419433
vkWaitForFences(device, inFlightFences.get(currentFrame), true, VUtil.UINT64_MAX);
420434

421-
this.beginRenderPass(stack);
435+
this.beginMainRenderPass(stack);
422436
}
423437
}
424438

@@ -455,10 +469,17 @@ public void endRenderPass(VkCommandBuffer commandBuffer) {
455469
VkGlFramebuffer.resetBoundFramebuffer();
456470
}
457471

458-
public boolean beginRendering(RenderPass renderPass, Framebuffer framebuffer) {
459-
if (skipRendering || !recordingCmds)
472+
public boolean beginRenderPass(RenderPass renderPass, Framebuffer framebuffer) {
473+
// TODO: minimizing could trigger this preventing rendering (e.g. texture atlas uploads)
474+
if (skipRendering)
460475
return false;
461476

477+
if (!recordingCmds) {
478+
this.beginFrame();
479+
480+
recordingCmds = true;
481+
}
482+
462483
if (this.boundFramebuffer != framebuffer) {
463484
this.endRenderPass(currentCmdBuffer);
464485

@@ -467,7 +488,12 @@ public boolean beginRendering(RenderPass renderPass, Framebuffer framebuffer) {
467488
}
468489

469490
this.boundFramebuffer = framebuffer;
491+
this.boundRenderPass = renderPass;
492+
493+
Renderer.setViewportState(0, 0, framebuffer.getWidth(), framebuffer.getHeight());
494+
Renderer.setScissor(0, 0, framebuffer.getWidth(), framebuffer.getHeight());
470495
}
496+
471497
return true;
472498
}
473499

@@ -517,7 +543,7 @@ private void recreateSwapChain() {
517543
waitFences();
518544
Vulkan.waitIdle();
519545

520-
commandBuffers.forEach(commandBuffer -> vkResetCommandBuffer(commandBuffer, 0));
546+
mainCommandBuffers.forEach(commandBuffer -> vkResetCommandBuffer(commandBuffer, 0));
521547
recordingCmds = false;
522548

523549
swapChain.recreate();
@@ -671,11 +697,11 @@ public static void clearAttachments(VkCommandBuffer commandBuffer, int attachmen
671697
}
672698

673699
public static void clearAttachments(int attachments, int width, int height) {
674-
clearAttachments(INSTANCE.currentCmdBuffer, attachments, width ,height);
700+
clearAttachments(INSTANCE.currentCmdBuffer, attachments, width , height);
675701
}
676702

677703
public static void clearAttachments(int attachments, int x, int y, int width, int height) {
678-
clearAttachments(INSTANCE.currentCmdBuffer, attachments, x, y, width ,height);
704+
clearAttachments(INSTANCE.currentCmdBuffer, attachments, x, y, width , height);
679705
}
680706

681707
public static void clearAttachments(VkCommandBuffer commandBuffer, int attachments, int width, int height) {

src/main/java/net/vulkanmod/vulkan/framebuffer/Framebuffer.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ public void beginRenderPass(VkCommandBuffer commandBuffer, RenderPass renderPass
122122
} else {
123123
renderPass.beginDynamicRendering(commandBuffer, stack);
124124
}
125-
126-
Renderer.getInstance().setBoundRenderPass(renderPass);
127-
Renderer.getInstance().setBoundFramebuffer(this);
128-
129-
Renderer.setViewportState(0, 0, this.width, this.height);
130-
Renderer.setScissor(0, 0, this.width, this.height);
131125
}
132126

133127
protected long getFramebufferId(RenderPass renderPass) {

src/main/java/net/vulkanmod/vulkan/pass/DefaultMainPass.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void begin(VkCommandBuffer commandBuffer, MemoryStack stack) {
6464
VulkanImage colorAttachment = framebuffer.getColorAttachment();
6565
colorAttachment.transitionImageLayout(stack, commandBuffer, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
6666

67-
framebuffer.beginRenderPass(commandBuffer, this.mainRenderPass, stack);
67+
Renderer.getInstance().beginRenderPass(this.mainRenderPass, framebuffer);
6868

6969
Renderer.setViewport(0, 0, framebuffer.getWidth(), framebuffer.getHeight(), stack);
7070

@@ -108,11 +108,7 @@ public void rebindMainTarget() {
108108
return;
109109

110110
Renderer.getInstance().endRenderPass(commandBuffer);
111-
112-
try (MemoryStack stack = MemoryStack.stackPush()) {
113-
swapChain.beginRenderPass(commandBuffer, this.auxRenderPass, stack);
114-
}
115-
111+
Renderer.getInstance().beginRenderPass(this.auxRenderPass, swapChain);
116112
}
117113

118114
@Override

src/main/resources/vulkanmod.mixins.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484

8585
"texture.mip.MipmapGeneratorM",
8686
"texture.image.NativeImageAccessor",
87-
"texture.update.GameRendererM",
8887
"texture.update.MLightTexture",
8988
"texture.update.MSpriteContents",
9089
"texture.update.MTextureManager",

0 commit comments

Comments
 (0)