Skip to content

Commit 6bc78c7

Browse files
committed
Refactor
1 parent 78a7212 commit 6bc78c7

File tree

3 files changed

+90
-77
lines changed

3 files changed

+90
-77
lines changed
Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package net.vulkanmod.mixin.util;
22

3-
import com.mojang.blaze3d.buffers.GpuBuffer;
43
import com.mojang.blaze3d.pipeline.RenderTarget;
54
import com.mojang.blaze3d.platform.NativeImage;
6-
import com.mojang.blaze3d.systems.CommandEncoder;
7-
import com.mojang.blaze3d.systems.RenderSystem;
8-
import com.mojang.blaze3d.textures.GpuTexture;
9-
import com.mojang.blaze3d.textures.TextureFormat;
105
import net.minecraft.client.Screenshot;
11-
import net.minecraft.util.ARGB;
12-
import net.vulkanmod.render.engine.VkGpuTexture;
13-
import net.vulkanmod.vulkan.Renderer;
14-
import net.vulkanmod.vulkan.util.ColorUtil;
15-
import org.lwjgl.vulkan.VK10;
6+
import net.vulkanmod.vulkan.util.ScreenshotUtil;
167
import org.spongepowered.asm.mixin.Mixin;
178
import org.spongepowered.asm.mixin.Overwrite;
189

@@ -23,72 +14,7 @@ public class ScreenshotMixin {
2314

2415
@Overwrite
2516
public static void takeScreenshot(RenderTarget renderTarget, int mipLevel, Consumer<NativeImage> consumer) {
26-
int width = renderTarget.width;
27-
int height = renderTarget.height;
28-
GpuTexture gpuTexture = renderTarget.getColorTexture();
29-
if (gpuTexture == null) {
30-
throw new IllegalStateException("Tried to capture screenshot of an incomplete framebuffer");
31-
} else {
32-
// Need to submit and wait cmds if screenshot was requested
33-
// before the end of the frame
34-
Renderer.getInstance().flushCmds();
35-
36-
int pixelSize = TextureFormat.RGBA8.pixelSize();
37-
GpuBuffer gpuBuffer = RenderSystem.getDevice()
38-
.createBuffer(() -> "Screenshot buffer", 9, width * height * pixelSize);
39-
CommandEncoder commandEncoder = RenderSystem.getDevice().createCommandEncoder();
40-
RenderSystem.getDevice().createCommandEncoder().copyTextureToBuffer(gpuTexture, gpuBuffer, 0, () -> {
41-
try (GpuBuffer.MappedView readView = commandEncoder.mapBuffer(gpuBuffer, true, false)) {
42-
NativeImage nativeImage = new NativeImage(width, height, false);
43-
44-
var colorAttachment = ((VkGpuTexture) Renderer.getInstance()
45-
.getMainPass()
46-
.getColorAttachment());
47-
boolean isBgraFormat = (colorAttachment.getVulkanImage().format == VK10.VK_FORMAT_B8G8R8A8_UNORM);
48-
49-
int size = mipLevel * mipLevel;
50-
51-
for (int y = 0; y < height; y++) {
52-
for (int x = 0; x < width; x++) {
53-
54-
if (mipLevel == 1) {
55-
int color = readView.data().getInt((x + y * width) * pixelSize);
56-
57-
if (isBgraFormat) {
58-
color = ColorUtil.BGRAtoRGBA(color);
59-
}
60-
61-
nativeImage.setPixelABGR(x, y, color | 0xFF000000);
62-
} else {
63-
int red = 0;
64-
int green = 0;
65-
int blue = 0;
66-
67-
for (int x1 = 0; x1 < mipLevel; x1++) {
68-
for (int y1 = 0; y1 < mipLevel; y1++) {
69-
int color = readView.data().getInt(((x + x1) + (y + y1) * width) * pixelSize);
70-
71-
if (isBgraFormat) {
72-
color = ColorUtil.BGRAtoRGBA(color);
73-
}
74-
75-
red += ARGB.red(color);
76-
green += ARGB.green(color);
77-
blue += ARGB.blue(color);
78-
}
79-
}
80-
81-
nativeImage.setPixelABGR(x, y, ARGB.color(255, red / size, green / size, blue / size));
82-
}
83-
}
84-
}
85-
86-
consumer.accept(nativeImage);
87-
}
88-
89-
gpuBuffer.close();
90-
}, 0);
91-
}
17+
ScreenshotUtil.takeScreenshot(renderTarget, mipLevel, consumer);
9218
}
9319

9420
}

src/main/java/net/vulkanmod/render/chunk/WorldRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public void setupRenderer(Camera camera, Frustum frustum, boolean isCapturedFrus
200200
//Debug
201201
// this.graphNeedsUpdate = true;
202202

203-
if (this.graphNeedsUpdate) {
203+
if (this.graphNeedsUpdate()) {
204204
this.graphNeedsUpdate = false;
205205
this.lastCameraX = cameraX;
206206
this.lastCameraY = cameraY;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package net.vulkanmod.vulkan.util;
2+
3+
import com.mojang.blaze3d.buffers.GpuBuffer;
4+
import com.mojang.blaze3d.pipeline.RenderTarget;
5+
import com.mojang.blaze3d.platform.NativeImage;
6+
import com.mojang.blaze3d.systems.CommandEncoder;
7+
import com.mojang.blaze3d.systems.RenderSystem;
8+
import com.mojang.blaze3d.textures.GpuTexture;
9+
import com.mojang.blaze3d.textures.TextureFormat;
10+
import net.minecraft.util.ARGB;
11+
import net.vulkanmod.render.engine.VkGpuTexture;
12+
import net.vulkanmod.vulkan.Renderer;
13+
import org.lwjgl.vulkan.VK10;
14+
15+
import java.util.function.Consumer;
16+
17+
public abstract class ScreenshotUtil {
18+
19+
public static void takeScreenshot(RenderTarget renderTarget, int mipLevel, Consumer<NativeImage> consumer) {
20+
int width = renderTarget.width;
21+
int height = renderTarget.height;
22+
GpuTexture gpuTexture = renderTarget.getColorTexture();
23+
if (gpuTexture == null) {
24+
throw new IllegalStateException("Tried to capture screenshot of an incomplete framebuffer");
25+
} else {
26+
// Need to submit and wait cmds if screenshot was requested
27+
// before the end of the frame
28+
Renderer.getInstance().flushCmds();
29+
30+
int pixelSize = TextureFormat.RGBA8.pixelSize();
31+
GpuBuffer gpuBuffer = RenderSystem.getDevice()
32+
.createBuffer(() -> "Screenshot buffer", 9, width * height * pixelSize);
33+
CommandEncoder commandEncoder = RenderSystem.getDevice().createCommandEncoder();
34+
RenderSystem.getDevice().createCommandEncoder().copyTextureToBuffer(gpuTexture, gpuBuffer, 0, () -> {
35+
try (GpuBuffer.MappedView readView = commandEncoder.mapBuffer(gpuBuffer, true, false)) {
36+
NativeImage nativeImage = new NativeImage(width, height, false);
37+
38+
var colorAttachment = ((VkGpuTexture) Renderer.getInstance()
39+
.getMainPass()
40+
.getColorAttachment());
41+
boolean isBgraFormat = (colorAttachment.getVulkanImage().format == VK10.VK_FORMAT_B8G8R8A8_UNORM);
42+
43+
int size = mipLevel * mipLevel;
44+
45+
for (int y = 0; y < height; y++) {
46+
for (int x = 0; x < width; x++) {
47+
48+
if (mipLevel == 1) {
49+
int color = readView.data().getInt((x + y * width) * pixelSize);
50+
51+
if (isBgraFormat) {
52+
color = ColorUtil.BGRAtoRGBA(color);
53+
}
54+
55+
nativeImage.setPixelABGR(x, y, color | 0xFF000000);
56+
} else {
57+
int red = 0;
58+
int green = 0;
59+
int blue = 0;
60+
61+
for (int x1 = 0; x1 < mipLevel; x1++) {
62+
for (int y1 = 0; y1 < mipLevel; y1++) {
63+
int color = readView.data().getInt(((x + x1) + (y + y1) * width) * pixelSize);
64+
65+
if (isBgraFormat) {
66+
color = ColorUtil.BGRAtoRGBA(color);
67+
}
68+
69+
red += ARGB.red(color);
70+
green += ARGB.green(color);
71+
blue += ARGB.blue(color);
72+
}
73+
}
74+
75+
nativeImage.setPixelABGR(x, y, ARGB.color(255, red / size, green / size, blue / size));
76+
}
77+
}
78+
}
79+
80+
consumer.accept(nativeImage);
81+
}
82+
83+
gpuBuffer.close();
84+
}, 0);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)