11package net .vulkanmod .mixin .util ;
22
3- import com .mojang .blaze3d .buffers .GpuBuffer ;
43import com .mojang .blaze3d .pipeline .RenderTarget ;
54import 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 ;
105import 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 ;
167import org .spongepowered .asm .mixin .Mixin ;
178import 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}
0 commit comments