| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeImage;
|
| 4 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.renderpearl.api.GpuFormat;
|
| 7 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 8 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 9 | import java.io.IOException;
|
| 10 | import java.nio.file.Path;
|
| 11 | import java.util.function.Supplier;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class DynamicTexture extends AbstractTexture implements Dumpable {
|
| 19 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 20 | private NativeImage pixels;
|
| 21 |
|
| 22 | public DynamicTexture(final Supplier<String> label, final NativeImage image) {
|
| 23 | this.pixels = image;
|
| 24 | this.createTexture(label);
|
| 25 | this.upload();
|
| 26 | }
|
| 27 |
|
| 28 | public DynamicTexture(final String label, final int width, final int height, final boolean zero) {
|
| 29 | this.pixels = new NativeImage(width, height, zero);
|
| 30 | this.createTexture(label);
|
| 31 | }
|
| 32 |
|
| 33 | public DynamicTexture(final Supplier<String> label, final int width, final int height, final boolean zero) {
|
| 34 | this.pixels = new NativeImage(width, height, zero);
|
| 35 | this.createTexture(label);
|
| 36 | }
|
| 37 |
|
| 38 | private void createTexture(final Supplier<String> label) {
|
| 39 | GpuDevice device = RenderSystem.getDevice();
|
| 40 | this.texture = device.createTexture(label, 5, GpuFormat.RGBA8_UNORM, this.pixels.getWidth(), this.pixels.getHeight(), 1, 1);
|
| 41 | this.sampler = RenderSystem.getSamplerCache().getRepeat(FilterMode.NEAREST);
|
| 42 | this.textureView = device.createTextureView(this.texture);
|
| 43 | }
|
| 44 |
|
| 45 | private void createTexture(final String label) {
|
| 46 | GpuDevice device = RenderSystem.getDevice();
|
| 47 | this.texture = device.createTexture(label, 5, GpuFormat.RGBA8_UNORM, this.pixels.getWidth(), this.pixels.getHeight(), 1, 1);
|
| 48 | this.sampler = RenderSystem.getSamplerCache().getRepeat(FilterMode.NEAREST);
|
| 49 | this.textureView = device.createTextureView(this.texture);
|
| 50 | }
|
| 51 |
|
| 52 | public void upload() {
|
| 53 | if (this.texture != null) {
|
| 54 | RenderSystem.getDevice().createCommandEncoder().writeToTexture(this.texture, this.pixels);
|
| 55 | } else {
|
| 56 | LOGGER.warn("Trying to upload disposed texture {}", this.getTexture().getLabel());
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | public NativeImage getPixels() {
|
| 61 | return this.pixels;
|
| 62 | }
|
| 63 |
|
| 64 | public void setPixels(final NativeImage pixels) {
|
| 65 | this.pixels.close();
|
| 66 | this.pixels = pixels;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void close() {
|
| 71 | this.pixels.close();
|
| 72 | super.close();
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void dumpContents(final Identifier selfId, final Path dir) throws IOException {
|
| 77 | if (!this.pixels.isClosed()) {
|
| 78 | String outputId = selfId.toDebugFileName() + ".png";
|
| 79 | Path path = dir.resolve(outputId);
|
| 80 | this.pixels.writeToFile(path);
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|