Skip to content

Commit 34e22a0

Browse files
committed
centralized status checking
1 parent b21ae3d commit 34e22a0

File tree

2 files changed

+53
-44
lines changed

2 files changed

+53
-44
lines changed

src/main/java/com/marginallyclever/convenience/helpers/OpenGLHelper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import com.jogamp.opengl.GL3;
55
import com.jogamp.opengl.glu.GLU;
66

7-
import java.nio.FloatBuffer;
8-
import java.nio.IntBuffer;
97
import java.util.ArrayList;
108
import java.util.List;
119

src/main/java/com/marginallyclever/makelangelo/preview/ShaderProgram.java

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,18 @@ public ShaderProgram(GL3 gl, String[] vertexCode, String[] fragmentCode) {
4040
gl.glAttachShader(programId, vertexShaderId);
4141
gl.glAttachShader(programId, fragmentShaderId);
4242
gl.glLinkProgram(programId);
43-
if (!checkStatus(gl, programId, GL3.GL_LINK_STATUS)) {
44-
throw new IllegalStateException("Failed to link shader program.");
45-
}
46-
gl.glValidateProgram(programId);
47-
if (!checkStatus(gl, programId, GL3.GL_VALIDATE_STATUS)) {
48-
throw new IllegalStateException("Failed to validate shader program.");
49-
}
50-
}
43+
ensureStatus(gl, programId, GL3.GL_LINK_STATUS, "program link", false);
5144

52-
private void showProgramError(GL3 gl, String message) {
53-
int[] logLength = new int[1];
54-
gl.glGetProgramiv(programId, GL3.GL_INFO_LOG_LENGTH, logLength, 0);
55-
byte[] log = new byte[logLength[0]];
56-
gl.glGetProgramInfoLog(programId, logLength[0], null, 0, log, 0);
57-
logger.error(message + new String(log));
45+
gl.glValidateProgram(programId);
46+
ensureStatus(gl, programId, GL3.GL_VALIDATE_STATUS, "program validate", false);
5847
}
5948

6049
private int loadShader(GL3 gl, int type, String[] shaderCode, String name) {
6150
int shaderId = gl.glCreateShader(type);
6251
gl.glShaderSource(shaderId, shaderCode.length, shaderCode, null, 0);
6352
gl.glCompileShader(shaderId);
64-
if (!checkStatus(gl, shaderId, GL3.GL_COMPILE_STATUS)) {
65-
int[] logLength = new int[1];
66-
gl.glGetShaderiv(shaderId, GL3.GL_INFO_LOG_LENGTH, logLength, 0);
67-
68-
byte[] log = new byte[logLength[0]];
69-
gl.glGetShaderInfoLog(shaderId, logLength[0], null, 0, log, 0);
70-
71-
logger.error("Failed to compile "+name+" shader code: " + new String(log));
72-
}
53+
// ensure compile succeeded or throw with the shader log
54+
ensureStatus(gl, shaderId, GL3.GL_COMPILE_STATUS, "shader compile: " + name, true);
7355
return shaderId;
7456
}
7557

@@ -91,6 +73,35 @@ private boolean checkStatus(GL3 gl, int id, int param) {
9173
return result[0] != GL3.GL_FALSE;
9274
}
9375

76+
/**
77+
* Ensure the given shader/program status is OK. If not, retrieve and log the info log and throw an exception.
78+
* @param gl GL context
79+
* @param id shader or program id
80+
* @param param status to check (e.g. GL_COMPILE_STATUS, GL_LINK_STATUS)
81+
* @param name human friendly name to use in the log/exception
82+
* @param isShader whether id is a shader (true) or a program (false)
83+
*/
84+
private void ensureStatus(GL3 gl, int id, int param, String name, boolean isShader) {
85+
if (checkStatus(gl,id,param)) return;
86+
int[] logLength = new int[1];
87+
if (isShader) {
88+
gl.glGetShaderiv(id, GL3.GL_INFO_LOG_LENGTH, logLength, 0);
89+
if(logLength[0] > 0) {
90+
byte[] log = new byte[logLength[0]];
91+
gl.glGetShaderInfoLog(id, logLength[0], null, 0, log, 0);
92+
logger.error("{} failed: {}", name, new String(log));
93+
}
94+
} else {
95+
gl.glGetProgramiv(id, GL3.GL_INFO_LOG_LENGTH, logLength, 0);
96+
if(logLength[0] > 0) {
97+
byte[] log = new byte[logLength[0]];
98+
gl.glGetProgramInfoLog(id, logLength[0], null, 0, log, 0);
99+
logger.error("{} failed: {}", name, new String(log));
100+
}
101+
}
102+
throw new IllegalStateException(name + " failed");
103+
}
104+
94105
public void use(GL3 gl) {
95106
gl.glUseProgram(programId);
96107
}
@@ -140,27 +151,27 @@ public void setVector3d(GL3 gl, String name, Vector3d value) {
140151
OpenGLHelper.checkGLError(gl,logger);
141152
}
142153

143-
private float [] matrixToFloatBuffer(Matrix4d m) {
144-
int i = 0;
145-
matrixBuffer[i++] = (float)m.m00;
146-
matrixBuffer[i++] = (float)m.m01;
147-
matrixBuffer[i++] = (float)m.m02;
148-
matrixBuffer[i++] = (float)m.m03;
154+
private float [] matrixToFloatArray(Matrix4d m) {
155+
// Fill explicitly by index to avoid analyzer warnings about post-increment usage
156+
matrixBuffer[0] = (float)m.m00;
157+
matrixBuffer[1] = (float)m.m01;
158+
matrixBuffer[2] = (float)m.m02;
159+
matrixBuffer[3] = (float)m.m03;
149160

150-
matrixBuffer[i++] = (float)m.m10;
151-
matrixBuffer[i++] = (float)m.m11;
152-
matrixBuffer[i++] = (float)m.m12;
153-
matrixBuffer[i++] = (float)m.m13;
161+
matrixBuffer[4] = (float)m.m10;
162+
matrixBuffer[5] = (float)m.m11;
163+
matrixBuffer[6] = (float)m.m12;
164+
matrixBuffer[7] = (float)m.m13;
154165

155-
matrixBuffer[i++] = (float)m.m20;
156-
matrixBuffer[i++] = (float)m.m21;
157-
matrixBuffer[i++] = (float)m.m22;
158-
matrixBuffer[i++] = (float)m.m23;
166+
matrixBuffer[8] = (float)m.m20;
167+
matrixBuffer[9] = (float)m.m21;
168+
matrixBuffer[10] = (float)m.m22;
169+
matrixBuffer[11] = (float)m.m23;
159170

160-
matrixBuffer[i++] = (float)m.m30;
161-
matrixBuffer[i++] = (float)m.m31;
162-
matrixBuffer[i++] = (float)m.m32;
163-
matrixBuffer[i++] = (float)m.m33;
171+
matrixBuffer[12] = (float)m.m30;
172+
matrixBuffer[13] = (float)m.m31;
173+
matrixBuffer[14] = (float)m.m32;
174+
matrixBuffer[15] = (float)m.m33;
164175
return matrixBuffer;
165176
}
166177

@@ -173,7 +184,7 @@ public void setVector3d(GL3 gl, String name, Vector3d value) {
173184
* @param value the matrix to set
174185
*/
175186
public void setMatrix4d(GL3 gl, String name, Matrix4d value) {
176-
gl.glUniformMatrix4fv(getUniformLocation(gl, name), 1, true, matrixToFloatBuffer(value),0);
187+
gl.glUniformMatrix4fv(getUniformLocation(gl, name), 1, true, matrixToFloatArray(value),0);
177188
OpenGLHelper.checkGLError(gl,logger);
178189
}
179190

0 commit comments

Comments
 (0)