| 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.renderpearl.api.GpuFormat;
|
| 6 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 7 | import com.mojang.renderpearl.api.textures.AddressMode;
|
| 8 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 9 | import java.io.IOException;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public abstract class ReloadableTexture extends AbstractTexture {
|
| 17 | private final Identifier resourceId;
|
| 18 |
|
| 19 | public ReloadableTexture(final Identifier resourceId) {
|
| 20 | this.resourceId = resourceId;
|
| 21 | }
|
| 22 |
|
| 23 | public Identifier resourceId() {
|
| 24 | return this.resourceId;
|
| 25 | }
|
| 26 |
|
| 27 | public void apply(final TextureContents contents) {
|
| 28 | this.setSampler(contents);
|
| 29 |
|
| 30 | try (NativeImage image = contents.image()) {
|
| 31 | this.doLoad(image);
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | protected void setSampler(final TextureContents contents) {
|
| 36 | AddressMode addressMode = contents.clamp() ? AddressMode.CLAMP_TO_EDGE : AddressMode.REPEAT;
|
| 37 | FilterMode minMag = contents.blur() ? FilterMode.LINEAR : FilterMode.NEAREST;
|
| 38 | this.sampler = RenderSystem.getSamplerCache().getSampler(addressMode, addressMode, minMag, minMag, false);
|
| 39 | }
|
| 40 |
|
| 41 | protected void doLoad(final NativeImage image) {
|
| 42 | GpuDevice device = RenderSystem.getDevice();
|
| 43 | this.close();
|
| 44 | this.texture = device.createTexture(this.resourceId::toString, 5, GpuFormat.RGBA8_UNORM, image.getWidth(), image.getHeight(), 1, 1);
|
| 45 | this.textureView = device.createTextureView(this.texture);
|
| 46 | device.createCommandEncoder().writeToTexture(this.texture, image);
|
| 47 | }
|
| 48 |
|
| 49 | public abstract TextureContents loadContents(ResourceManager resourceManager) throws IOException;
|
| 50 | }
|
| 51 |
|