Skip to content

Commit 1265c83

Browse files
committed
Fixes for opengl compliance
1 parent d28bed6 commit 1265c83

File tree

6 files changed

+23
-41
lines changed

6 files changed

+23
-41
lines changed

src/main/java/me/cortex/voxy/client/core/VoxelCore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void renderOpaque(MatrixStack matrices, double cameraX, double cameraY, d
180180
this.postProcessing.computeSSAO(projection, matrices);
181181

182182
//We can render the translucent directly after as it is the furthest translucent objects
183-
this.renderer.renderFarAwayTranslucent();
183+
this.renderer.renderFarAwayTranslucent(viewport);
184184

185185

186186
this.postProcessing.renderPost(projection, RenderSystem.getProjectionMatrix(), boundFB);

src/main/java/me/cortex/voxy/client/core/gl/GlPersistentMappedBuffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class GlPersistentMappedBuffer extends TrackedObject {
1313
public GlPersistentMappedBuffer(long size, int flags) {
1414
this.id = glCreateBuffers();
1515
this.size = size;
16-
glNamedBufferStorage(this.id, size, GL_CLIENT_STORAGE_BIT|GL_MAP_PERSISTENT_BIT|(flags&(GL_MAP_WRITE_BIT|GL_MAP_READ_BIT)));
16+
glNamedBufferStorage(this.id, size, GL_CLIENT_STORAGE_BIT|GL_MAP_PERSISTENT_BIT|(flags&(GL_MAP_COHERENT_BIT|GL_MAP_WRITE_BIT|GL_MAP_READ_BIT)));
1717
this.addr = nglMapNamedBufferRange(this.id, 0, size, flags|GL_MAP_PERSISTENT_BIT);
1818
}
1919

src/main/java/me/cortex/voxy/client/core/rendering/AbstractFarWorldRenderer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
//Todo: tinker with having the compute shader where each thread is a position to render? maybe idk
4141
public abstract class AbstractFarWorldRenderer <T extends Viewport> {
42-
protected final int vao = glGenVertexArrays();
42+
public static final int STATIC_VAO = glGenVertexArrays();
4343

4444
protected final GlBuffer uniformBuffer;
4545
protected final GeometryManager geometry;
@@ -65,8 +65,6 @@ public AbstractFarWorldRenderer(int geometrySize, int maxSections) {
6565
this.models = new ModelManager(16);
6666
}
6767

68-
protected abstract void setupVao();
69-
7068
public void setupRender(Frustum frustum, Camera camera) {
7169
this.frustum = frustum.frustumIntersection;
7270

@@ -126,7 +124,7 @@ public void setupRender(Frustum frustum, Camera camera) {
126124

127125
public abstract void renderFarAwayOpaque(T viewport);
128126

129-
public abstract void renderFarAwayTranslucent();
127+
public abstract void renderFarAwayTranslucent(T viewport);
130128

131129
public void enqueueResult(BuiltSection result) {
132130
this.geometry.enqueueResult(result);
@@ -145,7 +143,6 @@ public void addDebugData(List<String> debug) {
145143
}
146144

147145
public void shutdown() {
148-
glDeleteVertexArrays(this.vao);
149146
this.models.free();
150147
this.geometry.free();
151148
this.uniformBuffer.free();

src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.lwjgl.opengl.GL42.GL_FRAMEBUFFER_BARRIER_BIT;
3030
import static org.lwjgl.opengl.GL43.*;
3131
import static org.lwjgl.opengl.GL43.GL_SHADER_STORAGE_BUFFER;
32+
import static org.lwjgl.opengl.GL45C.glBindTextureUnit;
3233
import static org.lwjgl.opengl.GL45C.glClearNamedBufferData;
3334

3435
public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer<Gl46Viewport> {
@@ -56,12 +57,9 @@ public Gl46FarWorldRenderer(int geometryBuffer, int maxSections) {
5657
this.glCommandBuffer = new GlBuffer(maxSections*5L*4 * 6);
5758
this.glCommandCountBuffer = new GlBuffer(4*2);
5859
glClearNamedBufferData(this.glCommandBuffer.id, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, new int[1]);
59-
setupVao();
6060
}
6161

62-
@Override
63-
protected void setupVao() {
64-
glBindVertexArray(this.vao);
62+
protected void bindResources(Gl46Viewport viewport) {
6563
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, this.glCommandBuffer.id);
6664
glBindBuffer(GL_PARAMETER_BUFFER_ARB, this.glCommandCountBuffer.id);
6765
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedIndexBuffer.INSTANCE.id());
@@ -70,10 +68,10 @@ protected void setupVao() {
7068
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, this.glCommandBuffer.id);
7169
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, this.glCommandCountBuffer.id);
7270
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, this.geometry.metaId());
71+
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, viewport.visibilityBuffer.id);
7372
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, this.models.getBufferId());
7473
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, this.models.getColourBufferId());
7574
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 8, this.lightDataBuffer.id);//Lighting LUT
76-
glBindVertexArray(0);
7775
}
7876

7977
//FIXME: dont do something like this as it breaks multiviewport mods
@@ -115,30 +113,26 @@ public void renderFarAwayOpaque(Gl46Viewport viewport) {
115113

116114

117115
RenderLayer.getCutoutMipped().startDrawing();
118-
int oldActiveTexture = glGetInteger(GL_ACTIVE_TEXTURE);
119116
//RenderSystem.enableBlend();
120117
//RenderSystem.defaultBlendFunc();
121118

122119
this.updateUniformBuffer(viewport);
123-
124120
UploadStream.INSTANCE.commit();
125-
126-
glBindVertexArray(this.vao);
127-
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, viewport.visibilityBuffer.id);
121+
glBindVertexArray(AbstractFarWorldRenderer.STATIC_VAO);
128122

129123

130124
//Bind the texture atlas
131-
glActiveTexture(GL_TEXTURE0);
132-
int oldBoundTexture = glGetInteger(GL_TEXTURE_BINDING_2D);
133125
glBindSampler(0, this.models.getSamplerId());
134-
glBindTexture(GL_TEXTURE_2D, this.models.getTextureId());
126+
glBindTextureUnit(0, this.models.getTextureId());
135127

136128
glClearNamedBufferData(this.glCommandCountBuffer.id, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, new int[1]);
137129
this.commandGen.bind();
130+
this.bindResources(viewport);
138131
glDispatchCompute((this.geometry.getSectionCount()+127)/128, 1, 1);
139132
glMemoryBarrier(GL_COMMAND_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT | GL_UNIFORM_BARRIER_BIT);
140133

141134
this.lodShader.bind();
135+
this.bindResources(viewport);
142136
glDisable(GL_CULL_FACE);
143137
//glPointSize(10);
144138
glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 0, 0, (int) (this.geometry.getSectionCount()*4.4), 0);
@@ -159,6 +153,7 @@ public void renderFarAwayOpaque(Gl46Viewport viewport) {
159153
glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT | GL_FRAMEBUFFER_BARRIER_BIT);
160154

161155
this.cullShader.bind();
156+
this.bindResources(viewport);
162157

163158
glColorMask(false, false, false, false);
164159
glDepthMask(false);
@@ -174,15 +169,14 @@ public void renderFarAwayOpaque(Gl46Viewport viewport) {
174169

175170
glBindVertexArray(0);
176171
glBindSampler(0, 0);
177-
GL11C.glBindTexture(GL_TEXTURE_2D, oldBoundTexture);
178-
glActiveTexture(oldActiveTexture);
172+
glBindTextureUnit(0, 0);
179173
RenderLayer.getCutoutMipped().endDrawing();
180174
}
181175

182176
@Override
183-
public void renderFarAwayTranslucent() {
177+
public void renderFarAwayTranslucent(Gl46Viewport viewport) {
184178
RenderLayer.getTranslucent().startDrawing();
185-
glBindVertexArray(this.vao);
179+
glBindVertexArray(AbstractFarWorldRenderer.STATIC_VAO);
186180
glDisable(GL_CULL_FACE);
187181
glEnable(GL_BLEND);
188182

@@ -192,11 +186,11 @@ public void renderFarAwayTranslucent() {
192186

193187

194188
glBindSampler(0, this.models.getSamplerId());
195-
glActiveTexture(GL_TEXTURE0);
196-
glBindTexture(GL_TEXTURE_2D, this.models.getTextureId());
189+
glBindTextureUnit(0, this.models.getTextureId());
197190

198191
//RenderSystem.blendFunc(GlStateManager.SrcFactor.ONE, GlStateManager.DstFactor.ONE);
199192
this.lodShader.bind();
193+
this.bindResources(viewport);
200194

201195
glDepthMask(false);
202196
glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 400_000 * 4 * 5, 4, this.geometry.getSectionCount(), 0);
@@ -207,6 +201,7 @@ public void renderFarAwayTranslucent() {
207201

208202

209203
glBindSampler(0, 0);
204+
glBindTextureUnit(0, 0);
210205
glDisable(GL_BLEND);
211206

212207
RenderLayer.getTranslucent().endDrawing();

src/main/java/me/cortex/voxy/client/core/rendering/util/DownloadStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.lwjgl.opengl.GL42C.GL_BUFFER_UPDATE_BARRIER_BIT;
2222
import static org.lwjgl.opengl.GL43.GL_SHADER_STORAGE_BARRIER_BIT;
2323
import static org.lwjgl.opengl.GL44.GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT;
24+
import static org.lwjgl.opengl.GL44.GL_MAP_COHERENT_BIT;
2425

2526
public class DownloadStream {
2627
public interface DownloadResultConsumer {
@@ -36,7 +37,7 @@ public interface DownloadResultConsumer {
3637
private final ArrayList<DownloadData> thisFrameDownloadList = new ArrayList<>();
3738

3839
public DownloadStream(long size) {
39-
this.downloadBuffer = new GlPersistentMappedBuffer(size, GL_MAP_READ_BIT);
40+
this.downloadBuffer = new GlPersistentMappedBuffer(size, GL_MAP_READ_BIT|GL_MAP_COHERENT_BIT);
4041
this.allocationArena.setLimit(size);
4142
}
4243

src/main/java/me/cortex/voxy/client/core/rendering/util/UploadStream.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.lwjgl.opengl.GL42C.GL_BUFFER_UPDATE_BARRIER_BIT;
2020
import static org.lwjgl.opengl.GL43.GL_SHADER_STORAGE_BARRIER_BIT;
2121
import static org.lwjgl.opengl.GL44.GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT;
22+
import static org.lwjgl.opengl.GL44.GL_MAP_COHERENT_BIT;
2223

2324
public class UploadStream {
2425
private final AllocationArena allocationArena = new AllocationArena();
@@ -27,10 +28,9 @@ public class UploadStream {
2728
private final Deque<UploadFrame> frames = new ArrayDeque<>();
2829
private final LongArrayList thisFrameAllocations = new LongArrayList();
2930
private final Deque<UploadData> uploadList = new ArrayDeque<>();
30-
private final LongArrayList flushList = new LongArrayList();
3131

3232
public UploadStream(long size) {
33-
this.uploadBuffer = new GlPersistentMappedBuffer(size,GL_MAP_WRITE_BIT|GL_MAP_UNSYNCHRONIZED_BIT|GL_MAP_FLUSH_EXPLICIT_BIT);
33+
this.uploadBuffer = new GlPersistentMappedBuffer(size,GL_MAP_WRITE_BIT|GL_MAP_UNSYNCHRONIZED_BIT|GL_MAP_COHERENT_BIT);
3434
this.allocationArena.setLimit(size);
3535
}
3636

@@ -59,7 +59,7 @@ public long upload(GlBuffer buffer, long destOffset, long size) {
5959
throw new IllegalStateException("Could not allocate memory segment big enough for upload even after force flush");
6060
}
6161
}
62-
this.flushList.add(this.caddr);
62+
this.thisFrameAllocations.add(this.caddr);
6363
this.offset = size;
6464
addr = this.caddr;
6565
} else {//Could expand the allocation so just update it
@@ -78,17 +78,6 @@ public long upload(GlBuffer buffer, long destOffset, long size) {
7878

7979

8080
public void commit() {
81-
//First flush all the allocations and enqueue them to be freed
82-
{
83-
for (long alloc : flushList) {
84-
glFlushMappedNamedBufferRange(this.uploadBuffer.id, alloc, this.allocationArena.getSize(alloc));
85-
this.thisFrameAllocations.add(alloc);
86-
}
87-
this.flushList.clear();
88-
}
89-
glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT);
90-
glMemoryBarrier(GL_ALL_BARRIER_BITS);
91-
9281
//Execute all the copies
9382
for (var entry : this.uploadList) {
9483
glCopyNamedBufferSubData(this.uploadBuffer.id, entry.target.id, entry.uploadOffset, entry.targetOffset, entry.size);

0 commit comments

Comments
 (0)