-
Notifications
You must be signed in to change notification settings - Fork 65
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: releases/1.21
Are you sure you want to change the base?
Changes from all commits
4665578
64b0022
9d5ef4b
aa8ece0
0c020a1
a21474a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dev.dubhe.anvilcraft.api.rendering.foundation; | ||
|
||
import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex.GlVertexBuffer; | ||
import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex.QuadSortingState; | ||
import lombok.EqualsAndHashCode; | ||
import net.minecraft.client.renderer.RenderType; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.joml.Vector3f; | ||
import org.lwjgl.system.MemoryUtil; | ||
|
||
@EqualsAndHashCode | ||
public final class CompileResult implements Disposable { | ||
private static final MemoryUtil.MemoryAllocator ALLOCATOR = MemoryUtil.getAllocator(false); | ||
private final RenderType renderType; | ||
private final int vertexCount; | ||
private final int vertexSize; | ||
private final long vertexBufferPtr; | ||
private final int indexCount; | ||
@Nullable | ||
private final QuadSortingState sortingState; | ||
private boolean freed = false; | ||
|
||
public CompileResult( | ||
RenderType renderType, | ||
int vertexCount, | ||
int vertexSize, | ||
long vertexBufferPtr, | ||
int indexCount, | ||
@Nullable QuadSortingState sortingState | ||
) { | ||
this.renderType = renderType; | ||
this.vertexCount = vertexCount; | ||
this.vertexSize = vertexSize; | ||
this.vertexBufferPtr = vertexBufferPtr; | ||
this.indexCount = indexCount; | ||
this.sortingState = sortingState; | ||
} | ||
|
||
public void upload(GlVertexBuffer vertexBuffer) { | ||
vertexBuffer.upload(vertexBufferPtr, vertexCount * vertexSize, vertexCount, indexCount, sortingState, new Vector3f(), this); | ||
} | ||
|
||
public void dispose() { | ||
if (freed) return; | ||
ALLOCATOR.free(vertexBufferPtr); | ||
freed = true; | ||
} | ||
} |
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().length]; | ||
int[] indexes = new int[state.quadCenters().length]; | ||
|
||
for (int i = 0; i < state.quadCenters().length; i++) { | ||
distances[i] = state.quadCenters()[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,4 @@ | ||
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer; | ||
|
||
public interface BufferHost { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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<C> implements Disposable { | ||
public static final boolean BUFFER_STORAGE_SUPPORT = GL.getCapabilities().GL_ARB_buffer_storage && System.getProperty("anvilcraft.enforceLegacyBuffers") == null; | ||
protected final int glBufferId; | ||
protected final int target; | ||
protected boolean valid = true; | ||
|
||
static { | ||
if (BUFFER_STORAGE_SUPPORT) { | ||
System.out.println("Using GL_ARB_buffer_storage as buffer storage."); | ||
} | ||
} | ||
|
||
GlBufferStorage(int target, C configureContext) { | ||
this.target = target; | ||
this.glBufferId = glGenBuffers(); | ||
} | ||
|
||
public abstract void setupBufferState(C configureContext); | ||
|
||
public void upload(long ptr, long size, Disposable uploadSrc) { | ||
this.upload(ptr, size); | ||
uploadSrc.dispose(); | ||
} | ||
|
||
/** | ||
* 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,40 @@ | ||
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer; | ||
|
||
import com.mojang.blaze3d.systems.RenderSystem; | ||
import dev.dubhe.anvilcraft.api.rendering.foundation.Disposable; | ||
|
||
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<C> extends GlBufferStorage<C> { | ||
|
||
protected GlBufferStorageLegacy(int target,C configureContext) { | ||
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.
|
||
super(target, configureContext); | ||
bind(); | ||
this.setupBufferState(configureContext); | ||
} | ||
|
||
@Override | ||
public void upload(long ptr, long size, Disposable disposable) { | ||
if (RenderSystem.isOnRenderThread()) { | ||
nglBufferData(this.target, size, ptr, GL_STATIC_DRAW); | ||
disposable.dispose(); | ||
return; | ||
} | ||
RenderSystem.recordRenderCall(() -> { | ||
nglBufferData(this.target, size, ptr, GL_STATIC_DRAW); | ||
disposable.dispose(); | ||
} | ||
); | ||
} | ||
|
||
public void upload(long ptr, long size) { | ||
if (RenderSystem.isOnRenderThread()) { | ||
nglBufferData(this.target, size, ptr, GL_STATIC_DRAW); | ||
return; | ||
} | ||
RenderSystem.recordRenderCall(() -> { | ||
nglBufferData(this.target, size, ptr, GL_STATIC_DRAW); | ||
} | ||
); | ||
} | ||
} |
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.