| 1 | package net.minecraft.client.gui.render;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.ProjectionType;
|
| 4 | import com.mojang.blaze3d.platform.Lighting;
|
| 5 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 6 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 7 | import com.mojang.renderpearl.api.GpuFormat;
|
| 8 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 9 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 10 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 11 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.OptionalDouble;
|
| 14 | import java.util.Set;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.client.Minecraft;
|
| 18 | import net.minecraft.client.renderer.Projection;
|
| 19 | import net.minecraft.client.renderer.ProjectionMatrixBuffer;
|
| 20 | import net.minecraft.client.renderer.SubmitNodeStorage;
|
| 21 | import net.minecraft.client.renderer.feature.FeatureRenderDispatcher;
|
| 22 | import net.minecraft.client.renderer.item.ItemStackRenderState;
|
| 23 | import net.minecraft.client.renderer.item.TrackingItemStackRenderState;
|
| 24 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class GuiItemAtlas implements AutoCloseable {
|
| 30 | private static final int MINIMUM_TEXTURE_SIZE = 512;
|
| 31 | private final SubmitNodeStorage submitNodeStorage = new SubmitNodeStorage();
|
| 32 | private final FeatureRenderDispatcher featureRenderDispatcher;
|
| 33 | private final int textureSize;
|
| 34 | private final int slotTextureSize;
|
| 35 | private final GpuTexture texture;
|
| 36 | private final GpuTextureView textureView;
|
| 37 | private final GpuTexture depthTexture;
|
| 38 | private final GpuTextureView depthTextureView;
|
| 39 | private final DynamicAtlasAllocator<Object> allocator;
|
| 40 | private final PoseStack poseStack = new PoseStack();
|
| 41 | private final Projection projection = new Projection();
|
| 42 | private final ProjectionMatrixBuffer projectionMatrixBuffer = new ProjectionMatrixBuffer("items");
|
| 43 |
|
| 44 | public GuiItemAtlas(final FeatureRenderDispatcher featureRenderDispatcher, final int textureSize, final int slotTextureSize) {
|
| 45 | this.featureRenderDispatcher = featureRenderDispatcher;
|
| 46 | int storageSize = textureSize / slotTextureSize;
|
| 47 | this.textureSize = textureSize;
|
| 48 | this.slotTextureSize = slotTextureSize;
|
| 49 | GpuDevice device = RenderSystem.getDevice();
|
| 50 | this.texture = device.createTexture("UI items atlas", 13, GpuFormat.RGBA8_UNORM, textureSize, textureSize, 1, 1);
|
| 51 | this.textureView = device.createTextureView(this.texture);
|
| 52 | this.depthTexture = device.createTexture("UI items atlas depth", 9, GpuFormat.D32_FLOAT, textureSize, textureSize, 1, 1);
|
| 53 | this.depthTextureView = device.createTextureView(this.depthTexture);
|
| 54 | this.allocator = new DynamicAtlasAllocator<>(storageSize, storageSize);
|
| 55 | device.createCommandEncoder().clearColorAndDepthTextures(this.texture, GuiRenderer.CLEAR_COLOR, this.depthTexture, 0.0);
|
| 56 | }
|
| 57 |
|
| 58 | public static int computeTextureSizeFor(final int slotTextureSize, final int requiredSlotCount) {
|
| 59 | int preferredSlotCount = requiredSlotCount + requiredSlotCount / 2;
|
| 60 | int atlasSize = Mth.smallestSquareSide(preferredSlotCount);
|
| 61 | return Math.clamp(
|
| 62 | Mth.smallestEncompassingPowerOfTwo(atlasSize * slotTextureSize),
|
| 63 | 512,
|
| 64 | RenderSystem.getDevice().getDeviceInfo().limits().maxTextureSizeForFormat(GpuFormat.RGBA8_UNORM)
|
| 65 | );
|
| 66 | }
|
| 67 |
|
| 68 | public void endFrame() {
|
| 69 | this.allocator.endFrame();
|
| 70 | }
|
| 71 |
|
| 72 | public boolean tryPrepareFor(final Set<Object> items) {
|
| 73 | return this.allocator.hasSpaceForAll(items) ? true : this.allocator.reclaimSpaceFor(items);
|
| 74 | }
|
| 75 |
|
| 76 | public GuiItemAtlas.@Nullable SlotView getOrUpdate(final TrackingItemStackRenderState item) {
|
| 77 | DynamicAtlasAllocator.Slot slot = this.allocator.getOrAllocate(item.getModelIdentity(), item.isAnimated());
|
| 78 | if (slot == null) {
|
| 79 | return null;
|
| 80 | }
|
| 81 |
|
| 82 | switch (slot.state()) {
|
| 83 | case EMPTY:
|
| 84 | this.drawToSlot(slot.x(), slot.y(), false, item);
|
| 85 | break;
|
| 86 | case STALE:
|
| 87 | this.drawToSlot(slot.x(), slot.y(), true, item);
|
| 88 | case READY:
|
| 89 | }
|
| 90 |
|
| 91 | float slotUvSize = (float)this.slotTextureSize / this.textureSize;
|
| 92 | float u0 = slot.x() * slotUvSize;
|
| 93 | float v0 = 1.0F - slot.y() * slotUvSize;
|
| 94 | return new GuiItemAtlas.SlotView(this.textureView, u0, v0, u0 + slotUvSize, v0 - slotUvSize);
|
| 95 | }
|
| 96 |
|
| 97 | private void drawToSlot(final int slotX, final int slotY, final boolean clear, final ItemStackRenderState item) {
|
| 98 | int left = slotX * this.slotTextureSize;
|
| 99 | int top = slotY * this.slotTextureSize;
|
| 100 | int bottom = top + this.slotTextureSize;
|
| 101 | GpuDevice device = RenderSystem.getDevice();
|
| 102 | if (clear) {
|
| 103 | device.createCommandEncoder()
|
| 104 | .clearColorAndDepthTextures(
|
| 105 | this.texture, GuiRenderer.CLEAR_COLOR, this.depthTexture, 0.0, left, this.textureSize - bottom, this.slotTextureSize, this.slotTextureSize, 0
|
| 106 | );
|
| 107 | }
|
| 108 |
|
| 109 | this.poseStack.pushPose();
|
| 110 | this.poseStack.translate(left + this.slotTextureSize / 2.0F, top + this.slotTextureSize / 2.0F, 0.0F);
|
| 111 | this.poseStack.scale(this.slotTextureSize, -this.slotTextureSize, this.slotTextureSize);
|
| 112 | this.projection.setupOrtho(-1000.0F, 1000.0F, this.textureSize, this.textureSize, true);
|
| 113 | RenderSystem.setProjectionMatrix(this.projectionMatrixBuffer.getBuffer(this.projection), ProjectionType.ORTHOGRAPHIC);
|
| 114 | RenderSystem.enableScissorForRenderTypeDraws(left, this.textureSize - bottom, this.slotTextureSize, this.slotTextureSize);
|
| 115 | Lighting.Entry lighting = item.usesBlockLight() ? Lighting.Entry.ITEMS_3D : Lighting.Entry.ITEMS_FLAT;
|
| 116 | Minecraft.getInstance().gameRenderer.lighting().setupFor(lighting);
|
| 117 | item.submit(this.poseStack, this.submitNodeStorage, 15728880, OverlayTexture.NO_OVERLAY, 0);
|
| 118 |
|
| 119 | try (
|
| 120 | FeatureRenderDispatcher.PreparedFrame frame = this.featureRenderDispatcher.prepareFrame(this.submitNodeStorage);
|
| 121 | RenderPass renderPass = RenderSystem.getDevice()
|
| 122 | .createCommandEncoder()
|
| 123 | .createRenderPass(() -> "Item to GUI item atlas", this.textureView, Optional.empty(), this.depthTextureView, OptionalDouble.empty());
|
| 124 | ) {
|
| 125 | RenderSystem.bindDefaultUniforms(renderPass);
|
| 126 | FeatureRenderDispatcher.renderAllFeatures(renderPass, frame);
|
| 127 | }
|
| 128 |
|
| 129 | RenderSystem.disableScissorForRenderTypeDraws();
|
| 130 | this.poseStack.popPose();
|
| 131 | }
|
| 132 |
|
| 133 | public int textureSize() {
|
| 134 | return this.textureSize;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public void close() {
|
| 139 | this.texture.close();
|
| 140 | this.textureView.close();
|
| 141 | this.depthTexture.close();
|
| 142 | this.depthTextureView.close();
|
| 143 | this.projectionMatrixBuffer.close();
|
| 144 | }
|
| 145 |
|
| 146 | @Environment(EnvType.CLIENT)
|
| 147 | public record SlotView(GpuTextureView textureView, float u0, float v0, float u1, float v1) {
|
| 148 | }
|
| 149 | }
|
| 150 |
|