-
Notifications
You must be signed in to change notification settings - Fork 63
Higher-performance laser rendering 更高性能的激光渲染 #2792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev/1.21/1.6
Are you sure you want to change the base?
Changes from 2 commits
4665578
64b0022
9d5ef4b
aa8ece0
0c020a1
a21474a
8641622
75d7f46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation; | ||
|
|
||
| public interface Disposable { | ||
| /** | ||
| * It is guaranteed to run on Render Thread. | ||
| */ | ||
| void dispose(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation; | ||
|
|
||
| import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex.QuadSortingState; | ||
| import it.unimi.dsi.fastutil.ints.IntArrays; | ||
| import org.joml.Vector3f; | ||
|
|
||
| public class QuadSorter { | ||
| public static int[] buildSortedIndexByDistance(QuadSortingState state, Vector3f point){ | ||
| float[] distances = new float[state.quadCenters().size()]; | ||
| int[] indexes = new int[state.quadCenters().size()]; | ||
|
|
||
| for (int i = 0; i < state.quadCenters().size(); i++) { | ||
| distances[i] = state.quadCenters().get(i).distanceSquared(point); | ||
| indexes[i] = i; | ||
| } | ||
| IntArrays.mergeSort(indexes, (a,b) -> Float.compare(distances[a], distances[b])); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return indexes; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation.buffer; | ||
|
|
||
| import dev.dubhe.anvilcraft.api.rendering.foundation.Disposable; | ||
| import org.lwjgl.opengl.GL; | ||
|
|
||
| import static org.lwjgl.opengl.GL45.*; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| public abstract class GlBufferStorage implements Disposable { | ||
| public static final boolean BUFFER_STORAGE_SUPPORT = GL.getCapabilities().GL_ARB_buffer_storage; | ||
| protected final int glBufferId; | ||
| protected final int target; | ||
| protected boolean valid = true; | ||
|
|
||
| GlBufferStorage(int target) { | ||
| this.target = target; | ||
| this.glBufferId = glGenBuffers(); | ||
| } | ||
|
|
||
| public abstract void setupBufferState(); | ||
|
|
||
| /** | ||
| * Runs on worker thread | ||
| */ | ||
| public abstract void upload(long ptr, long size); | ||
|
|
||
| public void bind() { | ||
| glBindBuffer(target, glBufferId); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispose() { | ||
| if (!valid) return; | ||
| bind(); | ||
| glDeleteBuffers(glBufferId); | ||
| unbind(); | ||
| valid = false; | ||
| } | ||
|
|
||
| public void unbind() { | ||
| glBindBuffer(target, 0); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation.buffer; | ||
|
|
||
| import com.mojang.blaze3d.systems.RenderSystem; | ||
|
|
||
| import static org.lwjgl.opengl.GL45.*; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| public abstract class GlBufferStorageLegacy extends GlBufferStorage{ | ||
|
|
||
| protected GlBufferStorageLegacy(int target) { | ||
| super(target); | ||
| bind(); | ||
| this.setupBufferState(); | ||
| unbind(); | ||
| } | ||
|
|
||
| @Override | ||
| public void upload(long ptr, long size) { | ||
| RenderSystem.recordRenderCall(() -> | ||
| nglBufferData(this.target, size, ptr, GL_STATIC_DRAW) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation.buffer; | ||
|
|
||
| import net.minecraft.client.renderer.RenderType; | ||
| import org.lwjgl.opengl.ARBBufferStorage; | ||
| import org.lwjgl.system.MemoryUtil; | ||
|
|
||
| import static org.lwjgl.opengl.GL45.*; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| public abstract class GlBufferStorageModern extends GlBufferStorage { | ||
| public static final long BUFFER_SIZE = RenderType.SMALL_BUFFER_SIZE; | ||
| public static final int FLAGS = ARBBufferStorage.GL_MAP_PERSISTENT_BIT | ||
| | ARBBufferStorage.GL_MAP_COHERENT_BIT | ||
| | GL_MAP_WRITE_BIT | ||
| | GL_DYNAMIC_STORAGE_BIT; | ||
|
|
||
| private long clientPtr; | ||
|
|
||
| protected GlBufferStorageModern(int target) { | ||
| super(target); | ||
| bind(); | ||
| this.setupBufferState(); | ||
| ARBBufferStorage.glBufferStorage( | ||
| target, | ||
| BUFFER_SIZE, | ||
| FLAGS | ||
| ); | ||
| clientPtr = nglMapBufferRange(target, 0, BUFFER_SIZE, FLAGS); | ||
| unbind(); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispose() { | ||
| if (!valid)return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| bind(); | ||
| glUnmapBuffer(target); | ||
| clientPtr = 0; | ||
| super.dispose(); | ||
| } | ||
|
|
||
| @Override | ||
| public void upload(long ptr, long size) { | ||
| MemoryUtil.memCopy(ptr, clientPtr, size); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex; | ||
|
|
||
| import com.mojang.blaze3d.vertex.VertexFormat; | ||
| import dev.dubhe.anvilcraft.api.rendering.foundation.Disposable; | ||
| import dev.dubhe.anvilcraft.api.rendering.foundation.QuadSorter; | ||
| import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.GlBufferStorage; | ||
| import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex.index.GlIndexBuffer; | ||
| import net.minecraft.client.renderer.RenderType; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.joml.Vector3f; | ||
|
|
||
| import static org.lwjgl.opengl.GL45.*; | ||
|
|
||
| public class GlVertexArray implements Disposable { | ||
| private final int arrayObjectId; | ||
| private final GlBufferStorage vertexBuffer; | ||
| private final GlIndexBuffer indexBuffer; | ||
| private final RenderType renderType; | ||
| private final VertexFormat.IndexType indexType; | ||
| private QuadSortingState sortingState; | ||
|
|
||
| public GlVertexArray(RenderType renderType) { | ||
| this(renderType, VertexFormat.IndexType.SHORT); | ||
| } | ||
|
|
||
| public GlVertexArray(RenderType renderType, VertexFormat.IndexType indexType) { | ||
| this.indexType = indexType; | ||
| if (renderType.mode != VertexFormat.Mode.QUADS) throw new UnsupportedOperationException(); | ||
| this.indexBuffer = GlIndexBuffer.forQuad(indexType); | ||
| this.renderType = renderType; | ||
| this.arrayObjectId = glGenVertexArrays(); | ||
| glBindVertexArray(arrayObjectId); | ||
| this.vertexBuffer = GlVertexBufferStorage.create(renderType.format); | ||
| glBindVertexArray(0); | ||
| } | ||
|
|
||
| public void upload(long ptr, int size, int indexCount) { | ||
| this.upload(ptr, size, indexCount, null, null); | ||
| } | ||
|
|
||
| public void upload(long ptr, int size, int indexCount, @Nullable QuadSortingState sortingState, @Nullable Vector3f origin) { | ||
| this.sortingState = sortingState; | ||
| if (sortingState == null) { | ||
| indexBuffer.fillContents(indexCount); | ||
| } else { | ||
| resortVerticles(origin); | ||
| } | ||
| this.vertexBuffer.upload(ptr, size); | ||
| } | ||
|
|
||
| public void resortVerticles(Vector3f origin) { | ||
| int[] indexes = QuadSorter.buildSortedIndexByDistance(sortingState, origin); | ||
| indexBuffer.fromSorted(indexes); | ||
| } | ||
|
|
||
| @Override | ||
| public void dispose() { | ||
| vertexBuffer.dispose(); | ||
| indexBuffer.dispose(); | ||
| glDeleteVertexArrays(arrayObjectId); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex; | ||
|
|
||
| import com.mojang.blaze3d.platform.GlConst; | ||
| import com.mojang.blaze3d.vertex.VertexFormat; | ||
| import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.GlBufferStorage; | ||
| import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.GlBufferStorageLegacy; | ||
| import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.GlBufferStorageModern; | ||
|
|
||
| public final class GlVertexBufferStorage { | ||
|
|
||
| public static GlBufferStorage create(VertexFormat format) { | ||
| if (GlBufferStorage.BUFFER_STORAGE_SUPPORT) { | ||
| return new Modern(format); | ||
| } | ||
| return new Legacy(format); | ||
| } | ||
|
|
||
| public static class Legacy extends GlBufferStorageLegacy { | ||
| private final VertexFormat format; | ||
|
|
||
| public Legacy(VertexFormat format) { | ||
| super(GlConst.GL_ARRAY_BUFFER); | ||
| this.format = format; | ||
| } | ||
|
|
||
| @Override | ||
| public void setupBufferState() { | ||
| format.setupBufferState(); | ||
| } | ||
| } | ||
|
|
||
| public static class Modern extends GlBufferStorageModern { | ||
| private final VertexFormat format; | ||
|
|
||
| public Modern(VertexFormat format) { | ||
| super(GlConst.GL_ARRAY_BUFFER); | ||
| this.format = format; | ||
| } | ||
|
|
||
| @Override | ||
| public void setupBufferState() { | ||
| format.setupBufferState(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex; | ||
|
|
||
| import org.joml.Vector3f; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record QuadSortingState(List<Vector3f> quadCenters) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex.index; | ||
|
|
||
| import com.mojang.blaze3d.vertex.VertexFormat; | ||
| import dev.dubhe.anvilcraft.api.rendering.foundation.Disposable; | ||
|
|
||
| public interface GlIndexBuffer extends Disposable { | ||
| /** | ||
| * Called on worker threads | ||
| */ | ||
| void fillContents(int indexCount); | ||
|
|
||
| /** | ||
| * Called on worker threads= | ||
| */ | ||
| void fromSorted(int[] sortedIndex); | ||
|
|
||
| void bind(); | ||
|
|
||
| void unbind(); | ||
|
|
||
| static GlIndexBuffer forQuad(VertexFormat.IndexType indexType){ | ||
| return new GlQuadIndexBuffer(indexType); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WhitespaceAround: '{' is not preceded with whitespace.