VAO caching
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2025-05-19 12:28:28 -04:00
parent e151bdc34e
commit cc61814095
17 changed files with 104 additions and 68 deletions

View File

@ -1900,6 +1900,7 @@ Visual shader uniform location caching
Remove old uniforms from shaders and code to push uniforms
Break out shader material into dedicated library file
Materials read some properties from assimp data
OpenGLState VAO caching

View File

@ -92,6 +92,11 @@ public class OpenGLState {
*/
private Map<Integer,UniformBlockBinding> indexBlockMap;
/**
* The current VAO pointer
*/
private int currentVaoPtr = 0;
/**
* Initializes the opengl state
@ -349,6 +354,10 @@ public class OpenGLState {
}
/**
* Sets the blending status
* @param blend true to blend, false otherwise
*/
public void glBlend(boolean blend){
// if(this.blendTest != blend){
this.blendTest = blend;
@ -394,4 +403,18 @@ public class OpenGLState {
}
}
/**
* Binds a vertex array object buffer
* @param vaoPointer The pointer to the VAO
*/
public void glBindVertexArray(int vaoPointer){
if(
DISABLE_CACHING ||
currentVaoPtr != vaoPointer
){
this.currentVaoPtr = vaoPointer;
GL45.glBindVertexArray(vaoPointer);
}
}
}

View File

@ -35,9 +35,9 @@ public class RenderUtils {
static int createScreenTextureVAO(){
static int createScreenTextureVAO(OpenGLState openGLState){
int rVal = GL40.glGenVertexArrays();
GL40.glBindVertexArray(rVal);
openGLState.glBindVertexArray(rVal);
//vertices
FloatBuffer vertexArrayBufferData = BufferUtils.createFloatBuffer(12);
vertexArrayBufferData.put(-1.0f);
@ -133,7 +133,8 @@ public class RenderUtils {
//
// VAO
//
particleMesh.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
particleMesh.generateVAO(openGLState);
@ -214,7 +215,7 @@ public class RenderUtils {
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
@ -245,7 +246,8 @@ public class RenderUtils {
//
// VAO
//
planeMesh.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
planeMesh.generateVAO(openGLState);
@ -327,7 +329,7 @@ public class RenderUtils {
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
@ -353,7 +355,8 @@ public class RenderUtils {
public static Model createUnitsphere(){
Model model = new Model();
Mesh sphereMesh = new Mesh("sphere");
sphereMesh.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
sphereMesh.generateVAO(openGLState);
//buffer coords
ParShapesMesh data = ParShapes.par_shapes_create_parametric_sphere(10, 5);
@ -392,7 +395,7 @@ public class RenderUtils {
mat.setDiffuse(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
sphereMesh.setMaterial(mat);
sphereMesh.setShader(VisualShader.smartAssembleShader());
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
sphereMesh.setParent(model);
model.getMeshes().add(sphereMesh);
@ -406,7 +409,8 @@ public class RenderUtils {
public static Model createUnitCylinder(){
Model model = new Model();
Mesh sphereMesh = new Mesh("cylinder");
sphereMesh.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
sphereMesh.generateVAO(openGLState);
//buffer coords
ParShapesMesh data = ParShapes.par_shapes_create_cylinder(10, 2);
@ -447,7 +451,7 @@ public class RenderUtils {
mat.setDiffuse(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
sphereMesh.setMaterial(mat);
sphereMesh.setShader(VisualShader.smartAssembleShader());
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
sphereMesh.setParent(model);
model.getMeshes().add(sphereMesh);
@ -461,7 +465,8 @@ public class RenderUtils {
public static Model createUnitCube(){
Model model = new Model();
Mesh sphereMesh = new Mesh("cube");
sphereMesh.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
sphereMesh.generateVAO(openGLState);
//buffer coords
int numTriangles = 12;
@ -534,7 +539,7 @@ public class RenderUtils {
mat.setDiffuse(AssetDataStrings.TEXTURE_TEAL_TRANSPARENT);
sphereMesh.setMaterial(mat);
sphereMesh.setShader(VisualShader.smartAssembleShader());
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
sphereMesh.setParent(model);
model.getMeshes().add(sphereMesh);
@ -548,7 +553,8 @@ public class RenderUtils {
public static Model createBlockSingleModel(){
Model model = new Model();
Mesh cubeMesh = new Mesh(RenderUtils.MESH_NAME_BLOCK_SINGLE);
cubeMesh.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
cubeMesh.generateVAO(openGLState);
//buffer coords
int numTriangles = 12;
@ -621,7 +627,7 @@ public class RenderUtils {
mat.setDiffuse(AssetDataStrings.TEXTURE_BLOCK_ATLAS);
cubeMesh.setMaterial(mat);
cubeMesh.setShader(VisualShader.loadSpecificShader(AssetDataStrings.SHADER_BLOCK_SINGLE_VERT, AssetDataStrings.SHADER_BLOCK_SINGLE_FRAG));
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
cubeMesh.setParent(model);
model.getMeshes().add(cubeMesh);
@ -634,7 +640,8 @@ public class RenderUtils {
Model rVal = new Model();
Mesh m = new Mesh(AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME);
m.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
m.generateVAO(openGLState);
//vertices
FloatBuffer vertexArrayBufferData = BufferUtils.createFloatBuffer(12);
vertexArrayBufferData.put( 0);
@ -704,7 +711,7 @@ public class RenderUtils {
m.setShader(VisualShader.loadSpecificShader("/Shaders/ui/font/basicbitmap/basicbitmap.vs", "/Shaders/ui/font/basicbitmap/basicbitmap.fs"));
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
m.setParent(rVal);
Material uiMat = new Material();
@ -731,7 +738,8 @@ public class RenderUtils {
Model rVal = new Model();
Mesh m = new Mesh(AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME);
m.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
m.generateVAO(openGLState);
//vertices
FloatBuffer vertexArrayBufferData = BufferUtils.createFloatBuffer(12);
vertexArrayBufferData.put(-1);
@ -801,7 +809,7 @@ public class RenderUtils {
m.setShader(VisualShader.loadSpecificShader("/Shaders/ui/font/bitmapchar/bitmapchar.vs", "/Shaders/ui/font/bitmapchar/bitmapchar.fs"));
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
m.setParent(rVal);
rVal.getMeshes().add(m);
@ -815,7 +823,8 @@ public class RenderUtils {
Model rVal = new Model();
Mesh m = new Mesh("plane");
m.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
m.generateVAO(openGLState);
//vertices
FloatBuffer vertexArrayBufferData = BufferUtils.createFloatBuffer(12);
vertexArrayBufferData.put(-1);
@ -885,7 +894,7 @@ public class RenderUtils {
m.setShader(VisualShader.loadSpecificShader(vertexShader, fragmentShader));
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
m.setParent(rVal);
rVal.getMeshes().add(m);
@ -896,7 +905,7 @@ public class RenderUtils {
public static Model createTerrainModelPrecomputedShader(float[][] heightfield, float[][] texturemap, VisualShader program, int stride){
public static Model createTerrainModelPrecomputedShader(OpenGLState openGLState, float[][] heightfield, float[][] texturemap, VisualShader program, int stride){
Model rVal = new Model();
Mesh m = new Mesh("terrain");
int width = heightfield.length;
@ -1043,7 +1052,7 @@ public class RenderUtils {
texture_coords.flip();
textureIndices.flip();
m.generateVAO();
m.generateVAO(openGLState);
//buffer vertices
m.bufferVertices(vertices, 3);
//buffer normals
@ -1055,7 +1064,7 @@ public class RenderUtils {
//texture indices
m.bufferCustomFloatAttribArray(textureIndices, 4, 5);
m.setShader(program);
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
m.setParent(rVal);
Material groundMat = new Material();
@ -1100,6 +1109,7 @@ public class RenderUtils {
Model rVal = new Model();
Mesh m = new Mesh("terrain");
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
int width = heightfield.length;
int height = heightfield[0].length;
@ -1599,7 +1609,7 @@ public class RenderUtils {
texture_coords.flip();
textureIndices.flip();
m.generateVAO();
m.generateVAO(openGLState);
//buffer vertices
m.bufferVertices(vertices, 3);
//buffer normals
@ -1611,7 +1621,7 @@ public class RenderUtils {
//texture indices
m.bufferCustomFloatAttribArray(textureIndices, 4, 5);
m.setShader(program);
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
m.setParent(rVal);
Material groundMat = new Material();

View File

@ -356,7 +356,7 @@ public class RenderingEngine {
//init screen rendering quadrant
screenTextureVAO = createScreenTextureVAO();
screenTextureVAO = createScreenTextureVAO(this.openGLState);
screenTextureShaders = VisualShader.loadSpecificShader("/Shaders/core/screentexture/simple1/simple1.vs", "/Shaders/core/screentexture/simple1/simple1.fs");
//default framebuffer

View File

@ -11,12 +11,12 @@ import org.joml.Vector2f;
import org.joml.Vector3f;
import org.joml.Vector3i;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL40;
import electrosphere.client.block.BlockChunkData;
import electrosphere.engine.Globals;
import electrosphere.entity.state.collidable.MultiShapeTriGeomData;
import electrosphere.entity.state.collidable.TriGeomData;
import electrosphere.renderer.OpenGLState;
import electrosphere.renderer.model.Material;
import electrosphere.renderer.model.Mesh;
import electrosphere.renderer.model.Model;
@ -497,14 +497,14 @@ public class BlockMeshgen {
* @param data The block mesh data object
* @return The mesh
*/
protected static Mesh generateBlockMesh(BlockMeshData meshData){
protected static Mesh generateBlockMesh(OpenGLState openGLState, BlockMeshData meshData){
Mesh mesh = new Mesh("blockChunk");
//
// VAO
//
mesh.generateVAO();
mesh.generateVAO(openGLState);
@ -566,7 +566,7 @@ public class BlockMeshgen {
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
return mesh;
}
@ -577,7 +577,7 @@ public class BlockMeshgen {
*/
public static Model generateBlockModel(BlockMeshData meshData){
Model rVal = new Model();
Mesh m = BlockMeshgen.generateBlockMesh(meshData);
Mesh m = BlockMeshgen.generateBlockMesh(Globals.renderingEngine.getOpenGLState(), meshData);
//construct the material for the chunk
Material groundMat = new Material();

View File

@ -12,14 +12,13 @@ import org.lwjgl.BufferUtils;
import electrosphere.engine.Globals;
import electrosphere.entity.types.fluid.FluidChunkModelData;
import electrosphere.renderer.OpenGLState;
import electrosphere.renderer.model.Material;
import electrosphere.renderer.model.Mesh;
import electrosphere.renderer.model.Model;
import electrosphere.renderer.shader.VisualShader;
import electrosphere.server.physics.terrain.manager.ServerTerrainChunk;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
public class FluidChunkModelGeneration {
//http://paulbourke.net/geometry/polygonise/
@ -672,7 +671,7 @@ public class FluidChunkModelGeneration {
* @param data The fluid chunk data object
* @return The mesh
*/
protected static Mesh generateFluidMesh(FluidChunkModelData data){
protected static Mesh generateFluidMesh(OpenGLState openGLState, FluidChunkModelData data){
Mesh mesh = new Mesh("fluidChunk");
@ -681,7 +680,7 @@ public class FluidChunkModelGeneration {
//
// VAO
//
mesh.generateVAO();
mesh.generateVAO(openGLState);
@ -777,7 +776,7 @@ public class FluidChunkModelGeneration {
);
glBindVertexArray(0);
openGLState.glBindVertexArray(0);
return mesh;
}
@ -789,7 +788,7 @@ public class FluidChunkModelGeneration {
*/
public static Model generateFluidModel(FluidChunkModelData data){
Model rVal = new Model();
Mesh m = generateFluidMesh(data);
Mesh m = generateFluidMesh(Globals.renderingEngine.getOpenGLState(),data);
Material groundMat = new Material();

View File

@ -9,6 +9,8 @@ import org.joml.Vector2f;
import org.joml.Vector3f;
import org.lwjgl.BufferUtils;
import electrosphere.engine.Globals;
import electrosphere.renderer.OpenGLState;
import electrosphere.renderer.model.Mesh;
public class GeometryMeshGen {
@ -25,7 +27,8 @@ public class GeometryMeshGen {
*/
public static Mesh genBox(float width, float height, float depth){
Mesh mesh = new Mesh("box");
mesh.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
mesh.generateVAO(openGLState);
List<Vector3f> verts = new LinkedList<Vector3f>();
List<Vector3f> normals = new LinkedList<Vector3f>();
List<Vector2f> uvs = new LinkedList<Vector2f>();

View File

@ -17,7 +17,9 @@ import org.lwjgl.assimp.AIVector3D;
import org.lwjgl.assimp.AIVertexWeight;
import electrosphere.engine.EngineState;
import electrosphere.engine.Globals;
import electrosphere.logger.LoggerInterface;
import electrosphere.renderer.OpenGLState;
import electrosphere.renderer.loading.ModelPretransforms;
import electrosphere.renderer.model.Bone;
import electrosphere.renderer.model.Mesh;
@ -36,7 +38,8 @@ public class MeshLoader {
//
//Check for headless to not call gl functions when not running with gpu
if(!EngineState.EngineFlags.HEADLESS){
rVal.generateVAO();
OpenGLState openGLState = Globals.renderingEngine.getOpenGLState();
rVal.generateVAO(openGLState);
}

View File

@ -10,11 +10,10 @@ import org.joml.Vector3f;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import electrosphere.client.terrain.cells.VoxelTextureAtlas;
import electrosphere.client.terrain.data.TerrainChunkData;
import electrosphere.engine.Globals;
import electrosphere.renderer.OpenGLState;
import electrosphere.renderer.model.Material;
import electrosphere.renderer.model.Mesh;
import electrosphere.renderer.model.Model;
@ -769,7 +768,7 @@ public class TerrainChunkModelGeneration {
* @param data The terrain chunk data object
* @return The mesh
*/
protected static Mesh generateTerrainMesh(TerrainChunkData data){
protected static Mesh generateTerrainMesh(OpenGLState openGLState, TerrainChunkData data){
Mesh mesh = new Mesh("terrainChunk");
@ -777,7 +776,7 @@ public class TerrainChunkModelGeneration {
//
// VAO
//
mesh.generateVAO();
mesh.generateVAO(openGLState);
@ -870,7 +869,7 @@ public class TerrainChunkModelGeneration {
glBindVertexArray(0);
openGLState.glBindVertexArray(0);
return mesh;
}
@ -884,7 +883,7 @@ public class TerrainChunkModelGeneration {
@Deprecated
public static Model generateTerrainModel(TerrainChunkData data, VoxelTextureAtlas atlas){
Model rVal = new Model();
Mesh m = TerrainChunkModelGeneration.generateTerrainMesh(data);
Mesh m = TerrainChunkModelGeneration.generateTerrainMesh(Globals.renderingEngine.getOpenGLState(), data);
//construct the material for the chunk
Material groundMat = new Material();

View File

@ -8,13 +8,13 @@ import java.util.List;
import java.util.Map;
import org.joml.Vector3f;
import org.lwjgl.opengl.GL40;
import electrosphere.client.terrain.cells.VoxelTextureAtlas;
import electrosphere.client.terrain.data.TerrainChunkData;
import electrosphere.engine.Globals;
import electrosphere.logger.LoggerInterface;
import electrosphere.mem.VectorPool;
import electrosphere.renderer.OpenGLState;
import electrosphere.renderer.model.Material;
import electrosphere.renderer.model.Mesh;
import electrosphere.renderer.model.Model;
@ -2179,7 +2179,7 @@ public class TransvoxelModelGeneration {
* @param data The terrain chunk data object
* @return The mesh
*/
protected static Mesh generateTerrainMesh(TerrainChunkData data){
protected static Mesh generateTerrainMesh(OpenGLState openGLState, TerrainChunkData data){
Mesh mesh = new Mesh("terrainChunk");
@ -2187,7 +2187,7 @@ public class TransvoxelModelGeneration {
//
// VAO
//
mesh.generateVAO();
mesh.generateVAO(openGLState);
@ -2280,7 +2280,7 @@ public class TransvoxelModelGeneration {
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
return mesh;
}
@ -2293,7 +2293,7 @@ public class TransvoxelModelGeneration {
*/
public static Model generateTerrainModel(TerrainChunkData data, VoxelTextureAtlas atlas){
Model rVal = new Model();
Mesh m = TransvoxelModelGeneration.generateTerrainMesh(data);
Mesh m = TransvoxelModelGeneration.generateTerrainMesh(Globals.renderingEngine.getOpenGLState(), data);
//construct the material for the chunk
Material groundMat = new Material();

View File

@ -175,9 +175,9 @@ public class Mesh {
/**
* Generates the VAO for this mesh
*/
public void generateVAO(){
public void generateVAO(OpenGLState openGLState){
vertexArrayObject = GL45.glGenVertexArrays();
GL45.glBindVertexArray(vertexArrayObject);
openGLState.glBindVertexArray(vertexArrayObject);
}
/**
@ -419,7 +419,7 @@ public class Mesh {
Globals.profiler.beginAggregateCpuSample("Mesh.complexDraw");
//bind vao off the rip
GL45.glBindVertexArray(vertexArrayObject);
openGLState.glBindVertexArray(vertexArrayObject);
Globals.renderingEngine.checkError();
if(renderPipelineState.getUseMeshShader()){
@ -590,8 +590,6 @@ public class Mesh {
}
}
}
GL45.glBindVertexArray(0);
Globals.renderingEngine.checkError();
Globals.profiler.endCpuSample();
}

View File

@ -22,7 +22,7 @@ public class CompositePipeline implements RenderPipeline {
RenderingEngine.screenFramebuffer.bind(openGLState);
GL40.glBindVertexArray(RenderingEngine.screenTextureVAO);
openGLState.glBindVertexArray(RenderingEngine.screenTextureVAO);
//
@ -72,7 +72,7 @@ public class CompositePipeline implements RenderPipeline {
//
//Close down pipeline
//
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
Globals.renderingEngine.defaultFramebuffer.bind(openGLState);

View File

@ -225,7 +225,7 @@ public class MainContentPipeline implements RenderPipeline {
// glBindVertexArray(0);
// openGLState.glBindVertexArray(0);
Globals.profiler.endCpuSample();
}

View File

@ -25,7 +25,7 @@ public class OutlineNormalsPipeline implements RenderPipeline {
if(program != null){
openGLState.setActiveShader(renderPipelineState, program);
GL40.glBindVertexArray(RenderingEngine.screenTextureVAO);
openGLState.glBindVertexArray(RenderingEngine.screenTextureVAO);
openGLState.glActiveTexture(GL40.GL_TEXTURE0);
openGLState.glBindTexture(GL40.GL_TEXTURE_2D, 0);
@ -39,7 +39,7 @@ public class OutlineNormalsPipeline implements RenderPipeline {
openGLState.glBindTexture(GL40.GL_TEXTURE_2D, RenderingEngine.gameImageNormalsTexture.getTexturePointer());
GL40.glDrawArrays(GL40.GL_TRIANGLES, 0, 6);
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
}
Globals.renderingEngine.defaultFramebuffer.bind(openGLState);

View File

@ -74,7 +74,7 @@ public class PostProcessingPipeline implements RenderPipeline {
//
//Draw
//
GL40.glBindVertexArray(RenderingEngine.screenTextureVAO);
openGLState.glBindVertexArray(RenderingEngine.screenTextureVAO);
GL40.glDrawArrays(GL40.GL_TRIANGLES, 0, 6);
@ -86,7 +86,7 @@ public class PostProcessingPipeline implements RenderPipeline {
//
//Close down pipeline
//
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
Globals.renderingEngine.defaultFramebuffer.bind(openGLState);

View File

@ -46,7 +46,7 @@ public class RenderScreenPipeline implements RenderPipeline {
//render full screen quad
openGLState.setActiveShader(renderPipelineState, RenderingEngine.screenTextureShaders);
GL40.glBindVertexArray(RenderingEngine.screenTextureVAO);
openGLState.glBindVertexArray(RenderingEngine.screenTextureVAO);
//aaa
switch(RenderingEngine.outputFramebuffer){
case 0: {
@ -80,7 +80,7 @@ public class RenderScreenPipeline implements RenderPipeline {
} break;
}
GL40.glDrawArrays(GL40.GL_TRIANGLES, 0, 6);
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
Globals.profiler.endCpuSample();
}

View File

@ -30,13 +30,13 @@ public class UIPipeline implements RenderPipeline {
if(Globals.renderingEngine.RENDER_FLAG_RENDER_BLACK_BACKGROUND){
openGLState.setActiveShader(renderPipelineState, RenderingEngine.screenTextureShaders);
openGLState.glDepthTest(false);
GL40.glBindVertexArray(RenderingEngine.screenTextureVAO);
openGLState.glBindVertexArray(RenderingEngine.screenTextureVAO);
Texture blackTexture = Globals.assetManager.fetchTexture(AssetDataStrings.TEXTURE_BLACK);
if(blackTexture != null){
blackTexture.bind(openGLState);
}
GL40.glDrawArrays(GL40.GL_TRIANGLES, 0, 6);
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
}
@ -46,13 +46,13 @@ public class UIPipeline implements RenderPipeline {
if(Globals.renderingEngine.RENDER_FLAG_RENDER_WHITE_BACKGROUND){
openGLState.setActiveShader(renderPipelineState, RenderingEngine.screenTextureShaders);
openGLState.glDepthTest(false);
GL40.glBindVertexArray(RenderingEngine.screenTextureVAO);
openGLState.glBindVertexArray(RenderingEngine.screenTextureVAO);
Texture blackTexture = Globals.assetManager.fetchTexture(AssetDataStrings.TEXTURE_OFF_WHITE);
if(blackTexture != null){
blackTexture.bind(openGLState);
}
GL40.glDrawArrays(GL40.GL_TRIANGLES, 0, 6);
GL40.glBindVertexArray(0);
openGLState.glBindVertexArray(0);
}