Skip to content

Commit

Permalink
Global engine optimization
Browse files Browse the repository at this point in the history
removed all runtime glGetUniform calls
added vertical and horizontal boxes
  • Loading branch information
PierreEVEN committed Nov 15, 2020
1 parent 600ffcb commit c36fe3d
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 84 deletions.
23 changes: 14 additions & 9 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Collapsed=0
DockId=0x00000001,0

[Window][Debug##Default]
Pos=395,109
Pos=236,109
Size=910,1612
Collapsed=0

Expand All @@ -31,8 +31,8 @@ Size=709,885
Collapsed=0

[Window][Content browser]
Pos=0,816
Size=1465,262
Pos=0,549
Size=1465,529
Collapsed=0
DockId=0x00000005,0

Expand Down Expand Up @@ -75,7 +75,7 @@ Collapsed=0

[Window][viewport]
Pos=0,39
Size=1465,775
Size=1465,508
Collapsed=0
DockId=0x00000003,0

Expand Down Expand Up @@ -116,7 +116,7 @@ Collapsed=0

[Window][HUD Window]
Pos=-4,-4
Size=1928,1088
Size=1928,1065
Collapsed=0

[Window][Test Window]
Expand Down Expand Up @@ -741,7 +741,7 @@ Size=739,850
Collapsed=0

[Window][poplar2]
Pos=988,298
Pos=494,234
Size=799,749
Collapsed=0

Expand Down Expand Up @@ -855,13 +855,18 @@ Pos=911,216
Size=785,747
Collapsed=0

[Window][Roboto-Regular]
Pos=60,60
Size=594,319
Collapsed=0

[Docking][Data]
DockNode ID=0x00000007 Pos=581,216 Size=620,657 Selected=0xA80D9FF3
DockNode ID=0x0000000A Pos=270,302 Size=836,688 Selected=0xED1E1CDE
DockSpace ID=0x05CF85F3 Window=0xD4F6B0F2 Pos=0,39 Size=1920,1039 Split=X Selected=0x3B8B7B67
DockSpace ID=0x05CF85F3 Pos=0,39 Size=1920,1039 Split=X Selected=0x3B8B7B67
DockNode ID=0x00000001 Parent=0x05CF85F3 SizeRef=1465,1019 Split=Y Selected=0x602D6EAE
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1520,775 CentralNode=1 HiddenTabBar=1 Selected=0x602D6EAE
DockNode ID=0x00000005 Parent=0x00000001 SizeRef=1520,262 HiddenTabBar=1 Selected=0xF8AE6B2B
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1520,508 CentralNode=1 HiddenTabBar=1 Selected=0x602D6EAE
DockNode ID=0x00000005 Parent=0x00000001 SizeRef=1520,529 HiddenTabBar=1 Selected=0xF8AE6B2B
DockNode ID=0x00000002 Parent=0x05CF85F3 SizeRef=453,1019 Split=Y Selected=0xDAA2F35E
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=390,579 Selected=0xDAA2F35E
DockNode ID=0x00000006 Parent=0x00000002 SizeRef=390,456 HiddenTabBar=1 Selected=0xAB6613CF
Expand Down
2 changes: 1 addition & 1 deletion out/production/Coffee3D/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: coffee3D.Main
Main-Class: coffee3D.MainGame

2 changes: 1 addition & 1 deletion src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: coffee3D.Main
Main-Class: coffee3D.MainGame

5 changes: 3 additions & 2 deletions src/coffee3D/core/assets/types/MaterialInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import coffee3D.core.assets.Asset;
import coffee3D.core.assets.AssetReference;
import coffee3D.core.io.log.Log;
import coffee3D.core.renderer.RenderUtils;
import coffee3D.core.renderer.scene.RenderScene;
import coffee3D.core.renderer.scene.Scene;
import coffee3D.core.resources.types.MaterialResource;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void bindColor(Color colorOverride) {
private void bindShadowMaps(Scene context) {
if (((RenderScene)context).getShadowBuffer() == null) return;
getResource().setIntParameter("shadowMap", 0);
glActiveTexture(GL_TEXTURE0);
RenderUtils.ActivateTexture(0);
glBindTexture(GL_TEXTURE_2D, ((RenderScene)context).getShadowBuffer().getDepthTexture());
}

Expand All @@ -72,7 +73,7 @@ public void bindTextures(AssetReference<Texture2D>[] textureOverride) {
if (usedTexture != null)
{
getResource().setIntParameter("texture" + i, i + 1);
glActiveTexture(GL_TEXTURE1 + i);
RenderUtils.ActivateTexture(i + 1);
glBindTexture(GL_TEXTURE_2D, usedTexture.getTextureID());
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/coffee3D/core/maths/Interpolation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ public static float FInterpTo(float from, float to, float speed) {
float deltaSpeed = (float) Window.GetPrimaryWindow().getDeltaTime() * speed;
return from * (1 - deltaSpeed) + to * deltaSpeed;
}

public static float FInterpToConstant(float from, float to, float speed) {
float deltaSpeed = (float) Window.GetPrimaryWindow().getDeltaTime() * speed;
if (to - from > 0) return Math.min(to, from + deltaSpeed);
else return Math.max(to, from - deltaSpeed);
}
}
36 changes: 36 additions & 0 deletions src/coffee3D/core/renderer/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30;
import org.lwjgl.system.MemoryStack;

import java.nio.IntBuffer;
Expand All @@ -19,6 +21,11 @@
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import static org.lwjgl.opengl.GL.createCapabilities;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.glActiveTexture;
import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15C.glBindBuffer;
import static org.lwjgl.opengl.GL31C.GL_UNIFORM_BUFFER;
import static org.lwjgl.system.MemoryStack.stackPush;


Expand Down Expand Up @@ -107,6 +114,35 @@ public static void InitializeOpenGL() {
createCapabilities();
}

private static int LastActivatedTexture = -1;
public static void ActivateTexture(int index) {
if (LastActivatedTexture != index) {
LastActivatedTexture = index;
glActiveTexture(GL_TEXTURE0 + index);
}
}
private static int LastUniformBuffer = -1;
public static void BindUniformBuffer(int index) {
if (LastUniformBuffer != index) {
LastUniformBuffer = index;
glBindBuffer(GL_UNIFORM_BUFFER, index);
}
}
private static int LastEbo = -1;
public static void BindEbo(int ebo) {
if (LastEbo != ebo) {
LastEbo = ebo;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
}
}
private static int LastVao = -1;
public static void BindVao(int vao) {
if (LastVao != vao) {
LastVao = vao;
GL30.glBindVertexArray(vao);
}
}

public static void CheckGLErrors() {
int errCode;
while((errCode = glGetError()) != GL_NO_ERROR)
Expand Down
6 changes: 3 additions & 3 deletions src/coffee3D/core/renderer/scene/RenderScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ public boolean renderScene() {
glDisable(GL_CULL_FACE);
RenderUtils.getPostProcessMaterial().use(this);
RenderUtils.getPostProcessMaterial().getResource().setIntParameter("colorTexture", 0);
glActiveTexture(GL_TEXTURE0);
RenderUtils.ActivateTexture(0);
glBindTexture(GL_TEXTURE_2D, _colorBuffer.getColorTexture());
RenderUtils.getPostProcessMaterial().getResource().setIntParameter("depthTexture", 1);
glActiveTexture(GL_TEXTURE1);
RenderUtils.ActivateTexture(1);
glBindTexture(GL_TEXTURE_2D, _colorBuffer.getDepthTexture());
if (_sceneSettings.hasStencilBuffer()) {
RenderUtils.getPostProcessMaterial().getResource().setIntParameter("stencilTexture", 2);
glActiveTexture(GL_TEXTURE2);
RenderUtils.ActivateTexture(2);
glBindTexture(GL_TEXTURE_2D, _stencilBuffer.getColorTexture());
}
_viewportQuadMesh.use(this);
Expand Down
2 changes: 1 addition & 1 deletion src/coffee3D/core/renderer/scene/SceneComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected void drawBillboard(Scene context, Texture2D texture, float size) {
case Color:
GetBillboardMaterial().use(context);
GetBillboardMaterial().setVec4Parameter("position", getBound().position, size);
glActiveTexture(GL_TEXTURE0);
RenderUtils.ActivateTexture(0);
GetBillboardMaterial().setIntParameter("image", 0);
if (texture == null) glBindTexture(GL_TEXTURE_2D, getComponentIcon().getTextureHandle());
else glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
Expand Down
59 changes: 37 additions & 22 deletions src/coffee3D/core/resources/types/MaterialResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import org.joml.Vector4f;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.HashMap;

import static org.lwjgl.BufferUtils.createFloatBuffer;
import static org.lwjgl.BufferUtils.createIntBuffer;
import static org.lwjgl.opengl.GL46.*;

/**
Expand All @@ -25,6 +28,12 @@ public class MaterialResource extends GraphicResource {
private String _compilationMessage = null;
private String _vertexData;
private String _fragmentData;
private final HashMap<String, Integer> _uniforms = new HashMap<>();

private static final IntBuffer _bufferSize = createIntBuffer(1); // size of the variable
private static final IntBuffer _bufferType = createIntBuffer(1); // type of the variable (float, vec3 or mat4, etc)
private static final IntBuffer _bufferCount = createIntBuffer(1);


public MaterialResource(String resourceName, String vertexData, String fragmentData) {
super(resourceName);
Expand Down Expand Up @@ -79,6 +88,13 @@ public void load() {

if (getErrors() != null) ResourceManager.UnRegisterResource(this);
RenderUtils.CheckGLErrors();

glGetProgramiv(_materialHandle, GL_ACTIVE_UNIFORMS, _bufferCount);
for (int i = 0; i < _bufferCount.get(0); i++)
{
String name = glGetActiveUniform(_materialHandle, i, 64, _bufferSize, _bufferType);
_uniforms.put(name, glGetUniformLocation(_materialHandle, name));
}
}

@Override
Expand All @@ -100,58 +116,57 @@ public void use(Scene context) {

public void setIntParameter(String parameterName, int value) {
use(null);
int matHandle = glGetUniformLocation(_materialHandle, parameterName);
if (matHandle < 0) return;
glUniform1i(matHandle, value);
Integer location = _uniforms.get(parameterName);
if (location == null) return;
glUniform1i(location, value);
}

public void setFloatParameter(String parameterName, float value) {
use(null);
int matHandle = glGetUniformLocation(_materialHandle, parameterName);
if (matHandle < 0) return;
glUniform1f(matHandle, value);
Integer location = _uniforms.get(parameterName);
if (location == null) return;
glUniform1f(location, value);
}

public void setModelMatrix(Matrix4f value) {
use(null);
int matHandle = glGetUniformLocation(_materialHandle, "model");
if (matHandle < 0) return;
Integer location = _uniforms.get("model");
if (location == null) return;
final FloatBuffer bfr = createFloatBuffer(16);
value.get(bfr);
glUniformMatrix4fv(matHandle, false, bfr);
glUniformMatrix4fv(location, false, bfr);
}

public void setMatrixParameter(String parameterName, Matrix4f value) {
use(null);
int matHandle = glGetUniformLocation(_materialHandle, parameterName);
if (matHandle < 0) return;
Integer location = _uniforms.get(parameterName);
if (location == null) return;
final FloatBuffer bfr = createFloatBuffer(16);
value.get(bfr);
glUniformMatrix4fv(matHandle, false, bfr);
glUniformMatrix4fv(location, false, bfr);
}

public void setColorParameter(String parameterName, Color color) {
use(null);
int matHandle = glGetUniformLocation(_materialHandle, parameterName);
if (matHandle < 0) return;
glUniform4f(matHandle,
Integer location = _uniforms.get(parameterName);
if (location == null) return;
glUniform4f(location,
color.getVector().x * color.getPower(),
color.getVector().y * color.getPower(),
color.getVector().z * color.getPower(),
color.getVector().w * color.getPower());
RenderUtils.CheckGLErrors();
}

public void setVec4Parameter(String parameterName, Vector4f value) {
use(null);
int matHandle = glGetUniformLocation(_materialHandle, parameterName);
if (matHandle < 0) return;
glUniform4f(matHandle, value.x, value.y, value.z, value.w);
Integer location = _uniforms.get(parameterName);
if (location == null) return;
glUniform4f(location, value.x, value.y, value.z, value.w);
}
public void setVec4Parameter(String parameterName, Vector3f value, float w) {
use(null);
int matHandle = glGetUniformLocation(_materialHandle, parameterName);
if (matHandle < 0) return;
glUniform4f(matHandle, value.x, value.y, value.z, w);
Integer location = _uniforms.get(parameterName);
if (location == null) return;
glUniform4f(location, value.x, value.y, value.z, w);
}
}
15 changes: 4 additions & 11 deletions src/coffee3D/core/resources/types/MeshResource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package coffee3D.core.resources.types;

import coffee3D.core.io.log.Log;
import coffee3D.core.renderer.RenderUtils;
import coffee3D.core.renderer.scene.Scene;
import coffee3D.core.resources.GraphicResource;
import coffee3D.core.types.SphereBound;
Expand All @@ -9,7 +9,6 @@
import org.joml.Vector3f;
import org.lwjgl.BufferUtils;

import java.lang.reflect.Type;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

Expand Down Expand Up @@ -54,7 +53,7 @@ public void load() {

// Generate and load gpu vertex buffer
int vertexSize = TypeHelper.GetStructByteSize(Vertex.class);
glBindVertexArray(_meshVao);
RenderUtils.BindVao(_meshVao);
glBindBuffer(GL_ARRAY_BUFFER, _meshVbo);
glBufferData(GL_ARRAY_BUFFER, _vertexBuffer, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
Expand All @@ -66,12 +65,10 @@ public void load() {
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, false, vertexSize, 3 * 4 + 2 * 4 + 3 * 4);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

// Generate and load index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _meshEbo);
RenderUtils.BindEbo(_meshEbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

_vertexBuffer = null;
_indexBuffer = null;
Expand Down Expand Up @@ -107,11 +104,7 @@ public SphereBound getStaticBounds() {
}

public void use(Scene context) {
if (_lastDrawnMesh != this) {
_lastDrawnMesh = this;
glBindVertexArray(_meshVao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _meshEbo);
}
RenderUtils.BindVao(_meshVao);
glDrawElements(GL_TRIANGLES, _indexCount, GL_UNSIGNED_INT, 0);
}
}
Loading

0 comments on commit c36fe3d

Please sign in to comment.