| 1 | package com.mojang.blaze3d.platform;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.renderpearl.api.GpuFormat;
|
| 6 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 7 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 8 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 9 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
|
| 11 | import java.io.IOException;
|
| 12 | import java.io.InputStream;
|
| 13 | import java.nio.ByteBuffer;
|
| 14 | import java.nio.channels.Channels;
|
| 15 | import java.nio.channels.ReadableByteChannel;
|
| 16 | import java.nio.channels.SeekableByteChannel;
|
| 17 | import java.nio.file.Path;
|
| 18 | import java.util.Arrays;
|
| 19 | import java.util.concurrent.atomic.AtomicInteger;
|
| 20 | import java.util.function.IntUnaryOperator;
|
| 21 | import net.fabricmc.api.EnvType;
|
| 22 | import net.fabricmc.api.Environment;
|
| 23 | import net.minecraft.util.ARGB;
|
| 24 | import org.lwjgl.system.MemoryUtil;
|
| 25 | import org.slf4j.Logger;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public class TextureUtil {
|
| 29 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 30 | public static final int MIN_MIPMAP_LEVEL = 0;
|
| 31 | private static final int DEFAULT_IMAGE_BUFFER_SIZE = 8192;
|
| 32 | private static final int[][] DIRECTIONS = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
|
| 33 |
|
| 34 | public static ByteBuffer readResource(final InputStream inputStream) throws IOException {
|
| 35 | ReadableByteChannel channel = Channels.newChannel(inputStream);
|
| 36 | return channel instanceof SeekableByteChannel seekableChannel ? readResource(channel, (int)seekableChannel.size() + 1) : readResource(channel, 8192);
|
| 37 | }
|
| 38 |
|
| 39 | private static ByteBuffer readResource(final ReadableByteChannel channel, final int expectedSize) throws IOException {
|
| 40 | ByteBuffer buffer = MemoryUtil.memAlloc(expectedSize);
|
| 41 |
|
| 42 | try {
|
| 43 | while (channel.read(buffer) != -1) {
|
| 44 | if (!buffer.hasRemaining()) {
|
| 45 | buffer = MemoryUtil.memRealloc(buffer, buffer.capacity() * 2);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | buffer.flip();
|
| 50 | return buffer;
|
| 51 | } catch (IOException e) {
|
| 52 | MemoryUtil.memFree(buffer);
|
| 53 | throw e;
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | public static void writeAsPNG(final Path dir, final String prefix, final GpuTexture texture, final int maxMipLevel, final IntUnaryOperator pixelModifier) {
|
| 58 | RenderSystem.assertOnRenderThread();
|
| 59 | long bufferLength = 0L;
|
| 60 |
|
| 61 | for (int i = 0; i <= maxMipLevel; i++) {
|
| 62 | bufferLength += (long)texture.getFormat().blockSize() * texture.getWidth(i) * texture.getHeight(i);
|
| 63 | }
|
| 64 |
|
| 65 | if (bufferLength > 2147483647L) {
|
| 66 | throw new IllegalArgumentException("Exporting textures larger than 2GB is not supported");
|
| 67 | }
|
| 68 |
|
| 69 | if (texture.getFormat() != GpuFormat.RGBA8_UNORM) {
|
| 70 | throw new IllegalArgumentException("Exporting textures other than RGBA8_UNORM is not supported");
|
| 71 | }
|
| 72 |
|
| 73 | GpuBuffer buffer = RenderSystem.getDevice().createBuffer(() -> "Texture output buffer", 9, bufferLength);
|
| 74 | CommandEncoder commandEncoder = RenderSystem.getDevice().createCommandEncoder();
|
| 75 | Runnable onCopyComplete = () -> {
|
| 76 | try (GpuBufferSlice.MappedView read = buffer.map(true, false)) {
|
| 77 | ByteBuffer data = read.data();
|
| 78 | IntUnaryOperator decodeTexel = data::getInt;
|
| 79 | int offsetx = 0;
|
| 80 |
|
| 81 | for (int ix = 0; ix <= maxMipLevel; ix++) {
|
| 82 | int mipWidth = texture.getWidth(ix);
|
| 83 | int mipHeight = texture.getHeight(ix);
|
| 84 |
|
| 85 | try (NativeImage image = new NativeImage(mipWidth, mipHeight, false)) {
|
| 86 | for (int y = 0; y < mipHeight; y++) {
|
| 87 | for (int x = 0; x < mipWidth; x++) {
|
| 88 | int argb = decodeTexel.applyAsInt(offsetx + (x + y * mipWidth) * texture.getFormat().blockSize());
|
| 89 | image.setPixelABGR(x, y, pixelModifier.applyAsInt(argb));
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | Path target = dir.resolve(prefix + "_" + ix + ".png");
|
| 94 | image.writeToFile(target);
|
| 95 | LOGGER.debug("Exported png to: {}", target.toAbsolutePath());
|
| 96 | } catch (IOException e) {
|
| 97 | LOGGER.debug("Unable to write: ", e);
|
| 98 | }
|
| 99 |
|
| 100 | offsetx += texture.getFormat().blockSize() * mipWidth * mipHeight;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | buffer.close();
|
| 105 | };
|
| 106 | AtomicInteger completedCopies = new AtomicInteger();
|
| 107 | int offset = 0;
|
| 108 |
|
| 109 | for (int i = 0; i <= maxMipLevel; i++) {
|
| 110 | commandEncoder.copyTextureToBuffer(texture, buffer, offset, () -> {
|
| 111 | if (completedCopies.getAndIncrement() == maxMipLevel) {
|
| 112 | onCopyComplete.run();
|
| 113 | }
|
| 114 | }, i);
|
| 115 | offset += texture.getFormat().blockSize() * texture.getWidth(i) * texture.getHeight(i);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | public static Path getDebugTexturePath(final Path root) {
|
| 120 | return root.resolve("screenshots").resolve("debug");
|
| 121 | }
|
| 122 |
|
| 123 | public static Path getDebugTexturePath() {
|
| 124 | return getDebugTexturePath(Path.of("."));
|
| 125 | }
|
| 126 |
|
| 127 | public static void solidify(final NativeImage image) {
|
| 128 | int width = image.getWidth();
|
| 129 | int height = image.getHeight();
|
| 130 | int[] nearestColor = new int[width * height];
|
| 131 | int[] distances = new int[width * height];
|
| 132 | Arrays.fill(distances, Integer.MAX_VALUE);
|
| 133 | IntArrayFIFOQueue queue = new IntArrayFIFOQueue();
|
| 134 |
|
| 135 | for (int x = 0; x < width; x++) {
|
| 136 | for (int y = 0; y < height; y++) {
|
| 137 | int color = image.getPixel(x, y);
|
| 138 | if (ARGB.alpha(color) != 0) {
|
| 139 | int packedCoordinates = pack(x, y, width);
|
| 140 | distances[packedCoordinates] = 0;
|
| 141 | nearestColor[packedCoordinates] = color;
|
| 142 | queue.enqueue(packedCoordinates);
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | while (!queue.isEmpty()) {
|
| 148 | int packedCoordinates = queue.dequeueInt();
|
| 149 | int x = x(packedCoordinates, width);
|
| 150 | int y = y(packedCoordinates, width);
|
| 151 |
|
| 152 | for (int[] direction : DIRECTIONS) {
|
| 153 | int neighborX = x + direction[0];
|
| 154 | int neighborY = y + direction[1];
|
| 155 | int packedNeighborCoordinates = pack(neighborX, neighborY, width);
|
| 156 | if (neighborX >= 0 && neighborY >= 0 && neighborX < width && neighborY < height && distances[packedNeighborCoordinates] > distances[packedCoordinates] + 1) {
|
| 157 | distances[packedNeighborCoordinates] = distances[packedCoordinates] + 1;
|
| 158 | nearestColor[packedNeighborCoordinates] = nearestColor[packedCoordinates];
|
| 159 | queue.enqueue(packedNeighborCoordinates);
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | for (int x = 0; x < width; x++) {
|
| 165 | for (int y = 0; y < height; y++) {
|
| 166 | int color = image.getPixel(x, y);
|
| 167 | if (ARGB.alpha(color) == 0) {
|
| 168 | image.setPixel(x, y, ARGB.color(0, nearestColor[pack(x, y, width)]));
|
| 169 | } else {
|
| 170 | image.setPixel(x, y, color);
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | public static void fillEmptyAreasWithDarkColor(final NativeImage image) {
|
| 177 | int width = image.getWidth();
|
| 178 | int height = image.getHeight();
|
| 179 | int darkestColor = -1;
|
| 180 | int minBrightness = Integer.MAX_VALUE;
|
| 181 |
|
| 182 | for (int x = 0; x < width; x++) {
|
| 183 | for (int y = 0; y < height; y++) {
|
| 184 | int color = image.getPixel(x, y);
|
| 185 | int alpha = ARGB.alpha(color);
|
| 186 | if (alpha != 0) {
|
| 187 | int red = ARGB.red(color);
|
| 188 | int green = ARGB.green(color);
|
| 189 | int blue = ARGB.blue(color);
|
| 190 | int brightness = red + green + blue;
|
| 191 | if (brightness < minBrightness) {
|
| 192 | minBrightness = brightness;
|
| 193 | darkestColor = color;
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | int darkRed = 3 * ARGB.red(darkestColor) / 4;
|
| 200 | int darkGreen = 3 * ARGB.green(darkestColor) / 4;
|
| 201 | int darkBlue = 3 * ARGB.blue(darkestColor) / 4;
|
| 202 | int darkenedColor = ARGB.color(0, darkRed, darkGreen, darkBlue);
|
| 203 |
|
| 204 | for (int x = 0; x < width; x++) {
|
| 205 | for (int y = 0; y < height; y++) {
|
| 206 | int color = image.getPixel(x, y);
|
| 207 | if (ARGB.alpha(color) == 0) {
|
| 208 | image.setPixel(x, y, darkenedColor);
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | private static int pack(final int x, final int y, final int width) {
|
| 215 | return x + y * width;
|
| 216 | }
|
| 217 |
|
| 218 | private static int x(final int packed, final int width) {
|
| 219 | return packed % width;
|
| 220 | }
|
| 221 |
|
| 222 | private static int y(final int packed, final int width) {
|
| 223 | return packed / width;
|
| 224 | }
|
| 225 | }
|
| 226 |
|