minecraft_25w45a_unobfuscated/net/minecraft/client/renderer/CachedPerspectiveProjectionMatrixBuffer.java
2025-11-24 22:52:51 +03:00

63 lines
2.2 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* org.joml.Matrix4f
* org.joml.Matrix4fc
* org.lwjgl.system.MemoryStack
*/
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 org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.lwjgl.system.MemoryStack;
public class CachedPerspectiveProjectionMatrixBuffer
implements AutoCloseable {
private final GpuBuffer buffer;
private final GpuBufferSlice bufferSlice;
private final float zNear;
private final float zFar;
private int width;
private int height;
private float fov;
public CachedPerspectiveProjectionMatrixBuffer(String name, float zNear, float zFar) {
this.zNear = zNear;
this.zFar = zFar;
GpuDevice device = RenderSystem.getDevice();
this.buffer = device.createBuffer(() -> "Projection matrix UBO " + name, 136, RenderSystem.PROJECTION_MATRIX_UBO_SIZE);
this.bufferSlice = this.buffer.slice(0, RenderSystem.PROJECTION_MATRIX_UBO_SIZE);
}
public GpuBufferSlice getBuffer(int width, int height, float fov) {
if (this.width != width || this.height != height || this.fov != fov) {
Matrix4f projectionMatrix = this.createProjectionMatrix(width, height, fov);
try (MemoryStack stack = MemoryStack.stackPush();){
ByteBuffer byteBuffer = Std140Builder.onStack(stack, RenderSystem.PROJECTION_MATRIX_UBO_SIZE).putMat4f((Matrix4fc)projectionMatrix).get();
RenderSystem.getDevice().createCommandEncoder().writeToBuffer(this.buffer.slice(), byteBuffer);
}
this.width = width;
this.height = height;
this.fov = fov;
}
return this.bufferSlice;
}
private Matrix4f createProjectionMatrix(int width, int height, float fov) {
return new Matrix4f().perspective(fov * ((float)Math.PI / 180), (float)width / (float)height, this.zNear, this.zFar);
}
@Override
public void close() {
this.buffer.close();
}
}