Skip to content

Commit

Permalink
Convert MappedBuffer to Record
Browse files Browse the repository at this point in the history
  • Loading branch information
thr3343 committed Dec 22, 2023
1 parent 5cef229 commit d71c16c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 46 deletions.
12 changes: 6 additions & 6 deletions src/main/java/net/vulkanmod/mixin/render/RenderSystemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,13 @@ public static void _setShaderLights(Vector3f p_157174_, Vector3f p_157175_) {
shaderLightDirections[0] = p_157174_;
shaderLightDirections[1] = p_157175_;

VRenderSystem.lightDirection0.buffer.putFloat(0, p_157174_.x());
VRenderSystem.lightDirection0.buffer.putFloat(4, p_157174_.y());
VRenderSystem.lightDirection0.buffer.putFloat(8, p_157174_.z());
VRenderSystem.lightDirection0.buffer().putFloat(0, p_157174_.x());
VRenderSystem.lightDirection0.buffer().putFloat(4, p_157174_.y());
VRenderSystem.lightDirection0.buffer().putFloat(8, p_157174_.z());

VRenderSystem.lightDirection1.buffer.putFloat(0, p_157175_.x());
VRenderSystem.lightDirection1.buffer.putFloat(4, p_157175_.y());
VRenderSystem.lightDirection1.buffer.putFloat(8, p_157175_.z());
VRenderSystem.lightDirection1.buffer().putFloat(0, p_157175_.x());
VRenderSystem.lightDirection1.buffer().putFloat(4, p_157175_.y());
VRenderSystem.lightDirection1.buffer().putFloat(8, p_157175_.z());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ private void updateChunkAreaOrigin(double camX, double camY, double camZ, VkComm
float y = (float)(camY-(this.origin.y));
float z = (float)(camZ-(this.origin.z));

Matrix4f MVP = new Matrix4f().set(VRenderSystem.MVP.buffer.asFloatBuffer());
Matrix4f MV = new Matrix4f().set(VRenderSystem.modelViewMatrix.buffer.asFloatBuffer());
Matrix4f MVP = new Matrix4f().set(VRenderSystem.MVP.buffer().asFloatBuffer());
Matrix4f MV = new Matrix4f().set(VRenderSystem.modelViewMatrix.buffer().asFloatBuffer());

MVP.translate(-x, -y, -z).get(mPtr);
MV.translate(-x, -y, -z).get(16,mPtr);
Expand Down
46 changes: 19 additions & 27 deletions src/main/java/net/vulkanmod/vulkan/VRenderSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
import net.vulkanmod.vulkan.shader.PipelineState;
import net.vulkanmod.vulkan.util.ColorUtil;
import net.vulkanmod.vulkan.util.MappedBuffer;
import net.vulkanmod.vulkan.util.VUtil;
import org.joml.Math;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.lwjgl.system.MemoryUtil;

import java.nio.ByteBuffer;
Expand All @@ -33,21 +26,21 @@ public abstract class VRenderSystem {
public static boolean cull = true;

public static final float clearDepth = 1.0f;
public static FloatBuffer clearColor = MemoryUtil.memAllocFloat(4);
public static FloatBuffer clearColor = MemoryUtil.memCallocFloat(4); //Avoid the driver caching dirty memory as a clear Color

public static MappedBuffer modelViewMatrix = new MappedBuffer(16 * 4);
public static MappedBuffer projectionMatrix = new MappedBuffer(16 * 4);
public static MappedBuffer TextureMatrix = new MappedBuffer(16 * 4);
public static MappedBuffer MVP = new MappedBuffer(16 * 4);
public static final MappedBuffer modelViewMatrix = new MappedBuffer(MemoryUtil.memAlloc(16 * 4));
public static final MappedBuffer projectionMatrix = new MappedBuffer(MemoryUtil.memAlloc(16 * 4));
public static final MappedBuffer TextureMatrix = new MappedBuffer(MemoryUtil.memAlloc(16 * 4));
public static final MappedBuffer MVP = new MappedBuffer(MemoryUtil.memAlloc(16 * 4));

public static MappedBuffer ChunkOffset = new MappedBuffer(3 * 4);
public static MappedBuffer lightDirection0 = new MappedBuffer(3 * 4);
public static MappedBuffer lightDirection1 = new MappedBuffer(3 * 4);
public static final MappedBuffer ChunkOffset = new MappedBuffer(MemoryUtil.memAlloc(3 * 4));
public static final MappedBuffer lightDirection0 = new MappedBuffer(MemoryUtil.memAlloc(3 * 4));
public static final MappedBuffer lightDirection1 = new MappedBuffer(MemoryUtil.memAlloc(3 * 4));

public static MappedBuffer shaderColor = new MappedBuffer(4 * 4);
public static MappedBuffer shaderFogColor = new MappedBuffer(4 * 4);
public static final MappedBuffer shaderColor = new MappedBuffer(MemoryUtil.memAlloc(4 * 4));
public static final MappedBuffer shaderFogColor = new MappedBuffer(MemoryUtil.memAlloc(4 * 4));

public static MappedBuffer screenSize = new MappedBuffer(2 * 4);
public static final MappedBuffer screenSize = new MappedBuffer(MemoryUtil.memAlloc(2 * 4));

public static float alphaCutout = 0.0f;

Expand All @@ -60,7 +53,7 @@ public static void initRenderer()
Vulkan.initVulkan(window);
}

public static ByteBuffer getChunkOffset() { return ChunkOffset.buffer; }
public static ByteBuffer getChunkOffset() { return ChunkOffset.buffer(); }

public static int maxSupportedTextureSize() {
return DeviceManager.deviceProperties.limits().maxImageDimension2D();
Expand All @@ -73,23 +66,22 @@ public static void applyMVP(Matrix4f MV, Matrix4f P) {
}

public static void applyModelViewMatrix(Matrix4f mat) {
mat.get(modelViewMatrix.buffer.asFloatBuffer());
mat.get(modelViewMatrix.buffer().asFloatBuffer());
//MemoryUtil.memPutFloat(MemoryUtil.memAddress(modelViewMatrix), 1);
}

public static void applyProjectionMatrix(Matrix4f mat) {
mat.get(projectionMatrix.buffer.asFloatBuffer());
mat.get(projectionMatrix.buffer().asFloatBuffer());
}

public static void calculateMVP() {
org.joml.Matrix4f MV = new org.joml.Matrix4f(modelViewMatrix.buffer.asFloatBuffer());
org.joml.Matrix4f P = new org.joml.Matrix4f(projectionMatrix.buffer.asFloatBuffer());

P.mul(MV).get(MVP.buffer);
org.joml.Matrix4f MV = new org.joml.Matrix4f(modelViewMatrix.buffer().asFloatBuffer());
org.joml.Matrix4f P = new org.joml.Matrix4f(projectionMatrix.buffer().asFloatBuffer());
P.mul(MV).get(MVP.buffer());
}

public static void setTextureMatrix(Matrix4f mat) {
mat.get(TextureMatrix.buffer.asFloatBuffer());
mat.get(TextureMatrix.buffer().asFloatBuffer());
}

public static MappedBuffer getTextureMatrix() {
Expand All @@ -109,7 +101,7 @@ public static MappedBuffer getMVP() {
}

public static void setChunkOffset(float f1, float f2, float f3) {
long ptr = ChunkOffset.ptr;
long ptr = ChunkOffset.ptr();
VUtil.UNSAFE.putFloat(ptr, f1);
VUtil.UNSAFE.putFloat(ptr + 4, f2);
VUtil.UNSAFE.putFloat(ptr + 8, f3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String getName() {
void update(long ptr) {
MappedBuffer src = values.get();

MemoryUtil.memCopy(src.ptr, ptr + this.offset, this.size);
MemoryUtil.memCopy(src.ptr(), ptr + this.offset, this.size);
}

public static Uniform createField(Info info) {
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/net/vulkanmod/vulkan/util/MappedBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@

import java.nio.ByteBuffer;

public class MappedBuffer {
public record MappedBuffer( ByteBuffer buffer, long ptr) {

public final ByteBuffer buffer;
public final long ptr;

public static MappedBuffer createFromBuffer(ByteBuffer buffer) {
return new MappedBuffer(buffer, MemoryUtil.memAddress0(buffer));
return new MappedBuffer(buffer);
}
MappedBuffer(ByteBuffer buffer, long ptr) {
this.buffer = buffer;
this.ptr = ptr;
public static MappedBuffer getMappedBuffer(ByteBuffer buffer, long ptr) {
return new MappedBuffer(buffer, ptr);
}

public MappedBuffer(int size) {
this.buffer = MemoryUtil.memAlloc(size);
this.ptr = MemoryUtil.memAddress0(this.buffer);
public MappedBuffer(ByteBuffer buffer) {
this(buffer, MemoryUtil.memAddress0(buffer));
}

public void putFloat(int idx, float f) {
Expand Down

0 comments on commit d71c16c

Please sign in to comment.