Skip to content

Commit

Permalink
Use hardware copy for render target blits where possible (#2676)
Browse files Browse the repository at this point in the history
Minecraft performs a blit using a fragment shader, which is unnecessary when blending is not used. Using the fixed function hardware to perform the blit is much faster and doesn't utilize the rasterization engine.
  • Loading branch information
jellysquid3 authored Aug 16, 2024
1 parent 1744347 commit cd18e7f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ protected MixinConfig() {

this.addMixinRule("features.render", true);

this.addMixinRule("features.render.compositing", true);

this.addMixinRule("features.render.entity", true);
this.addMixinRule("features.render.entity.cull", true);
this.addMixinRule("features.render.entity.shadow", true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.caffeinemc.mods.sodium.mixin.features.render.compositing;


import com.mojang.blaze3d.pipeline.RenderTarget;
import org.lwjgl.opengl.GL32C;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(RenderTarget.class)
public class RenderTargetMixin {
@Shadow
public int frameBufferId;

@Shadow
public int width;

@Shadow
public int height;

/**
* @author JellySquid
* @reason Use fixed function hardware for framebuffer blits
*/
@Inject(method = "blitToScreen(IIZ)V", at = @At("HEAD"), cancellable = true)
public void blitToScreen(int width, int height, boolean disableBlend, CallbackInfo ci) {
if (disableBlend) {
ci.cancel();

// When blending is not used, we can directly copy the contents of one
// framebuffer to another using the blitting engine. This can save a lot of time
// when compared to going through the rasterization pipeline.
GL32C.glBindFramebuffer(GL32C.GL_READ_FRAMEBUFFER, this.frameBufferId);
GL32C.glBlitFramebuffer(
0, 0, width, height,
0, 0, width, height,
GL32C.GL_COLOR_BUFFER_BIT, GL32C.GL_LINEAR);
GL32C.glBindFramebuffer(GL32C.GL_READ_FRAMEBUFFER, 0);
}
}

}
1 change: 1 addition & 0 deletions common/src/main/resources/sodium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"features.options.render_layers.LeavesBlockMixin",
"features.options.render_layers.ItemBlockRenderTypesMixin",
"features.options.weather.LevelRendererMixin",
"features.render.compositing.RenderTargetMixin",
"features.render.entity.CubeMixin",
"features.render.entity.ModelPartMixin",
"features.render.entity.cull.EntityRendererMixin",
Expand Down

0 comments on commit cd18e7f

Please sign in to comment.