package net.minecraft.client.renderer.texture; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.textures.TextureFormat; import com.mojang.logging.LogUtils; import java.io.IOException; import java.nio.file.Path; import java.util.function.Supplier; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @Environment(EnvType.CLIENT) public class DynamicTexture extends AbstractTexture implements Dumpable { private static final Logger LOGGER = LogUtils.getLogger(); @Nullable private NativeImage pixels; public DynamicTexture(Supplier label, NativeImage pixels) { this.pixels = pixels; this.texture = RenderSystem.getDevice().createTexture(label, TextureFormat.RGBA8, this.pixels.getWidth(), this.pixels.getHeight(), 1); this.upload(); } public DynamicTexture(String label, int width, int height, boolean useCalloc) { this.pixels = new NativeImage(width, height, useCalloc); this.texture = RenderSystem.getDevice().createTexture(label, TextureFormat.RGBA8, this.pixels.getWidth(), this.pixels.getHeight(), 1); } public DynamicTexture(Supplier label, int width, int height, boolean useCalloc) { this.pixels = new NativeImage(width, height, useCalloc); this.texture = RenderSystem.getDevice().createTexture(label, TextureFormat.RGBA8, this.pixels.getWidth(), this.pixels.getHeight(), 1); } public void upload() { if (this.pixels != null && this.texture != null) { RenderSystem.getDevice().createCommandEncoder().writeToTexture(this.texture, this.pixels); } else { LOGGER.warn("Trying to upload disposed texture {}", this.getTexture().getLabel()); } } @Nullable public NativeImage getPixels() { return this.pixels; } public void setPixels(NativeImage pixels) { if (this.pixels != null) { this.pixels.close(); } this.pixels = pixels; } @Override public void close() { if (this.pixels != null) { this.pixels.close(); this.pixels = null; } super.close(); } @Override public void dumpContents(ResourceLocation resourceLocation, Path path) throws IOException { if (this.pixels != null) { String string = resourceLocation.toDebugFileName() + ".png"; Path path2 = path.resolve(string); this.pixels.writeToFile(path2); } } }