minecraft-src/net/minecraft/client/renderer/PerspectiveProjectionMatrixBuffer.java
2025-09-18 12:27:44 +00:00

37 lines
1.3 KiB
Java

package net.minecraft.client.renderer;
import com.mojang.blaze3d.buffers.GpuBuffer;
import com.mojang.blaze3d.buffers.GpuBufferSlice;
import com.mojang.blaze3d.buffers.Std140Builder;
import com.mojang.blaze3d.systems.GpuDevice;
import com.mojang.blaze3d.systems.RenderSystem;
import java.nio.ByteBuffer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import org.joml.Matrix4f;
import org.lwjgl.system.MemoryStack;
@Environment(EnvType.CLIENT)
public class PerspectiveProjectionMatrixBuffer implements AutoCloseable {
private final GpuBuffer buffer;
private final GpuBufferSlice bufferSlice;
public PerspectiveProjectionMatrixBuffer(String label) {
GpuDevice gpuDevice = RenderSystem.getDevice();
this.buffer = gpuDevice.createBuffer(() -> "Projection matrix UBO " + label, 136, RenderSystem.PROJECTION_MATRIX_UBO_SIZE);
this.bufferSlice = this.buffer.slice(0, RenderSystem.PROJECTION_MATRIX_UBO_SIZE);
}
public GpuBufferSlice getBuffer(Matrix4f pose) {
try (MemoryStack memoryStack = MemoryStack.stackPush()) {
ByteBuffer byteBuffer = Std140Builder.onStack(memoryStack, RenderSystem.PROJECTION_MATRIX_UBO_SIZE).putMat4f(pose).get();
RenderSystem.getDevice().createCommandEncoder().writeToBuffer(this.buffer.slice(), byteBuffer);
}
return this.bufferSlice;
}
public void close() {
this.buffer.close();
}
}