Reformat comment and optimize the bind/unbind calls

This commit is contained in:
James Seibel
2022-04-13 21:06:47 -05:00
parent c19e2613b4
commit f012ac44a7
3 changed files with 103 additions and 51 deletions
@@ -1,59 +1,106 @@
/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU GPL v3 License.
*
* Copyright (C) 2020-2022 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.seibel.lod.core.objects.opengl;
import com.seibel.lod.core.api.ApiShared;
import org.lwjgl.system.MemoryUtil;
import static org.lwjgl.opengl.GL11C.GL_UNSIGNED_INT;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL31.GL_COPY_WRITE_BUFFER;
public class QuadIBO {
//Datatype of the stored indices (can be GL_UNSIGNED_INT, GL_UNSIGNED_SHORT, GL_UNSIGNED_BYTE)
public int type;
//IBO object
int id;
//Current capacity (in quads)
int ccap;
//Global object, used for sharing the IBO for any draw calls
public static QuadIBO GLOBAL = new QuadIBO();
public QuadIBO() {
id = glGenBuffers();
}
public void resize(int cap) {
//If requested capacity is less than or equal to current capacity, ignore
if (cap <= ccap)
return;
//Not really necessary, just to stop constant resizes
cap *= 1.5;
System.out.println("Resizing from "+ccap+" to " + cap);
ccap = cap;
//TODO: DO DYNAMIC TYPES, just makes things more efficent
type = GL_UNSIGNED_INT;
int DT_SIZE = 4;//Datatype size (int: 4, short: 2, byte: 1)
glBindBuffer(GL_COPY_WRITE_BUFFER, id);
//Resize the buffer
glBufferData(GL_COPY_WRITE_BUFFER, (long) DT_SIZE * 6 * cap, GL_STATIC_DRAW);// 4L is datatype
//Map and write the index data to the buffer
long ptr = nglMapBuffer(GL_COPY_WRITE_BUFFER, GL_WRITE_ONLY);
for (int base = 0; base < cap; base++) {
//Write index's
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*0),(int)(base*4 + 0));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*1),(int)(base*4 + 1));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*2),(int)(base*4 + 2));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*3),(int)(base*4 + 2));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*4),(int)(base*4 + 3));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*5),(int)(base*4 + 0));
}
glUnmapBuffer(GL_COPY_WRITE_BUFFER);
}
public void bind(int capacity) {
resize(capacity);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
/**
* Represents a OpenGL Index Buffer Object.
*
* @author Cotex
* @version 4-13-2022
*/
public class QuadIBO
{
/**
* Datatype of the stored indices
* (can be GL_UNSIGNED_INT, GL_UNSIGNED_SHORT, GL_UNSIGNED_BYTE)
*/
public int type;
/** OpenGL ID of this IBO object */
int id;
/** Current capacity (in quads) */
int currentQuadCapacity;
/** Global IBO object, used for sharing the IBO for any draw calls */
public static QuadIBO GLOBAL = new QuadIBO();
public QuadIBO()
{
id = glGenBuffers();
}
/** Should only be called on a thread that has a OpenGL context. */
public void resizeIfNecessary(int newQuadCapacity)
{
//If the requested capacity is less than or equal to current capacity, ignore
if (newQuadCapacity <= currentQuadCapacity)
return;
// create the new capacity bigger than necessary to prevent constant updates
newQuadCapacity *= 1.5;
ApiShared.LOGGER.info("Quad IBO Resizing from [" + currentQuadCapacity + "] to [" + newQuadCapacity + "]");
currentQuadCapacity = newQuadCapacity;
//TODO: DO DYNAMIC TYPES, just makes things more efficient
type = GL_UNSIGNED_INT;
int DT_SIZE = 4; //Datatype size (int: 4, short: 2, byte: 1)
glBindBuffer(GL_COPY_WRITE_BUFFER, id);
//Resize the buffer
glBufferData(GL_COPY_WRITE_BUFFER, (long) DT_SIZE * 6 * newQuadCapacity, GL_STATIC_DRAW);// 4L is datatype
//Map and write the index data to the buffer
long arrayPointer = nglMapBuffer(GL_COPY_WRITE_BUFFER, GL_WRITE_ONLY);
for (int base = 0; base < newQuadCapacity; base++)
{
// Add the new quad's indices
MemoryUtil.memPutInt(arrayPointer + (base * 6L * DT_SIZE + DT_SIZE * 0), (int) (base * 4 + 0));
MemoryUtil.memPutInt(arrayPointer + (base * 6L * DT_SIZE + DT_SIZE * 1), (int) (base * 4 + 1));
MemoryUtil.memPutInt(arrayPointer + (base * 6L * DT_SIZE + DT_SIZE * 2), (int) (base * 4 + 2));
MemoryUtil.memPutInt(arrayPointer + (base * 6L * DT_SIZE + DT_SIZE * 3), (int) (base * 4 + 2));
MemoryUtil.memPutInt(arrayPointer + (base * 6L * DT_SIZE + DT_SIZE * 4), (int) (base * 4 + 3));
MemoryUtil.memPutInt(arrayPointer + (base * 6L * DT_SIZE + DT_SIZE * 5), (int) (base * 4 + 0));
}
glUnmapBuffer(GL_COPY_WRITE_BUFFER);
}
/** Binds the IBO */
public void bind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
/** Unbinds the IBO */
public void unbind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
}
@@ -80,7 +80,7 @@ public class SimpleRenderBuffer extends RenderBuffer
hasRendered = true;
GL32.glBindBuffer(GL32.GL_ARRAY_BUFFER, vbo.id);
shaderProgram.bindVertexBuffer(vbo.id);
QuadIBO.GLOBAL.bind(vbo.vertexCount/4);
QuadIBO.GLOBAL.resizeIfNecessary(vbo.vertexCount/4);
GL32.glDrawElements(GL32.GL_TRIANGLES, (vbo.vertexCount/4)*6, QuadIBO.GLOBAL.type, 0);
//LodRenderer.tickLogger.info("Vertex buffer: {}", vbo);
}
@@ -29,6 +29,7 @@ import com.seibel.lod.core.logging.ConfigBasedSpamLogger;
import com.seibel.lod.core.logging.SpamReducedLogger;
import com.seibel.lod.core.objects.BoolType;
import com.seibel.lod.core.objects.Pos2D;
import com.seibel.lod.core.objects.opengl.QuadIBO;
import com.seibel.lod.core.render.objects.GLState;
import com.seibel.lod.core.util.*;
import com.seibel.lod.core.util.gridList.*;
@@ -318,6 +319,9 @@ public class LodRenderer
LagSpikeCatcher drawFillLightmap = new LagSpikeCatcher();
ILightMapWrapper lightmap = MC_RENDER.getLightmapWrapper();
lightmap.bind();
QuadIBO.GLOBAL.bind();
//lightmapTexture.fillData(MC_RENDER.getLightmapTextureWidth(), MC_RENDER.getLightmapTextureHeight(), MC_RENDER.getLightmapPixels());
drawFillLightmap.end("drawFillLightmap");
drawFillData.end("DrawFillData");
@@ -372,6 +376,7 @@ public class LodRenderer
profiler.popPush("LOD cleanup");
LagSpikeCatcher drawCleanup = new LagSpikeCatcher();
lightmap.unbind();
QuadIBO.GLOBAL.unbind();
GL32.glBindBuffer(GL32.GL_ARRAY_BUFFER, 0);