Cleanup
This commit is contained in:
@@ -163,18 +163,18 @@ public class Renderer implements GLEventListener {
|
||||
|
||||
timer.stop("compute");
|
||||
|
||||
timer.start("draw");
|
||||
|
||||
explosionComputeHandler.render();
|
||||
|
||||
timer.stop("draw");
|
||||
|
||||
timer.start("getGpuData");
|
||||
|
||||
explosionComputeHandler.getGpuData();
|
||||
|
||||
timer.stop("getGpuData");
|
||||
|
||||
timer.start("draw");
|
||||
|
||||
explosionComputeHandler.render();
|
||||
|
||||
timer.stop("draw");
|
||||
|
||||
timer.log();
|
||||
|
||||
if (lastLog < System.nanoTime() - TimeUnit.SECONDS.toNanos(1)) {
|
||||
|
||||
@@ -22,7 +22,7 @@ public class ExplosionComputeHandler {
|
||||
private final static Logger logger = LoggerFactory.getLogger(ExplosionComputeHandler.class);
|
||||
|
||||
private final static int EXPLOSION_PARTICLES = 256;
|
||||
private final static int MAX_EXPLOSION_PARTICLES = 1000000;
|
||||
private final static int MAX_EXPLOSION_PARTICLES = 250000;
|
||||
|
||||
private final Random random = new Random(System.nanoTime());
|
||||
|
||||
@@ -52,7 +52,7 @@ public class ExplosionComputeHandler {
|
||||
float x = random.nextFloat() * 2f - 1f;
|
||||
float y = random.nextFloat() * 2f - 1f;
|
||||
|
||||
if (newParticleCount > (MAX_EXPLOSION_PARTICLES - EXPLOSION_PARTICLES)) {
|
||||
if ((particleCount + newParticleCount) > (MAX_EXPLOSION_PARTICLES - EXPLOSION_PARTICLES)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -108,10 +108,10 @@ public class ExplosionComputeHandler {
|
||||
gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, explHandle);
|
||||
|
||||
// transfer data to VBO, this perform the copy of data from CPU -> GPU memory
|
||||
// gl.glBufferData(GL.GL_ARRAY_BUFFER, particleCount * 4 * 8, explBuffer, GL.GL_DYNAMIC_DRAW);
|
||||
gl.glBufferSubData(GL.GL_ARRAY_BUFFER, particleCount * 4 * 8, newParticleCount * 4 * 8, explBuffer);
|
||||
|
||||
// Select the VBO, GPU memory data, to use for vertices
|
||||
gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0);
|
||||
|
||||
gl.glBindBuffer(GL4.GL_ATOMIC_COUNTER_BUFFER, atomicHandle);
|
||||
|
||||
atomicBuffer.put(0, particleCount + newParticleCount);
|
||||
@@ -119,7 +119,6 @@ public class ExplosionComputeHandler {
|
||||
// transfer data to VBO, this perform the copy of data from CPU -> GPU memory
|
||||
gl.glBufferData(GL4.GL_ATOMIC_COUNTER_BUFFER, 4, atomicBuffer, GL.GL_DYNAMIC_DRAW);
|
||||
|
||||
gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0);
|
||||
gl.glBindBuffer(GL4.GL_ATOMIC_COUNTER_BUFFER, 0);
|
||||
|
||||
newParticleCount = 0;
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
#version 430 core
|
||||
#define pi 3.141592653589793238462643383279
|
||||
|
||||
precision mediump float;
|
||||
precision mediump int;
|
||||
|
||||
struct explosion {
|
||||
vec4 positionVelocity;
|
||||
vec4 color;
|
||||
@@ -29,7 +26,6 @@ void main() {
|
||||
vec4 color = e[gid].color;
|
||||
|
||||
if (color.a > 0.0) {
|
||||
//velocity = velocity * 0.99;
|
||||
position = position + velocity * delta * color.a;
|
||||
color.a = color.a - (delta / 2);
|
||||
} else {
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
#version 430 core
|
||||
#define pi 3.141592653589793238462643383279
|
||||
|
||||
precision mediump float;
|
||||
precision mediump int;
|
||||
|
||||
struct explosion {
|
||||
vec4 positionVelocity;
|
||||
@@ -22,10 +18,8 @@ void main() {
|
||||
uint last = atomicCounter(lastCounter);
|
||||
|
||||
if (gid < last && last > 0) {
|
||||
vec4 color = e[gid].color;
|
||||
|
||||
if (color.a == 0.0) {
|
||||
uint old = atomicCounterDecrement(lastCounter);
|
||||
if (e[gid].color.a == 0.0) {
|
||||
uint old = atomicCounterDecrement(lastCounter) - 1;
|
||||
|
||||
e[gid].positionVelocity = e[old].positionVelocity;
|
||||
e[gid].color = e[old].color;
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
package com.persesgames.jogl.shader;
|
||||
|
||||
import com.persesgames.jogl.Renderer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.media.opengl.GL2ES2;
|
||||
import javax.media.opengl.GL4;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/*-----------------------------------------------------+
|
||||
| App |
|
||||
@@ -27,100 +20,32 @@ import java.util.Map;
|
||||
| |
|
||||
+-----------------------------------------------------*/
|
||||
|
||||
public class ComputeProgram {
|
||||
private final static Logger logger = LoggerFactory.getLogger(Renderer.class);
|
||||
public class ComputeProgram extends Program {
|
||||
|
||||
protected GL4 gl;
|
||||
|
||||
private int shaderProgram;
|
||||
private GL4 gl4;
|
||||
private int computeShader;
|
||||
|
||||
private Map<String, Integer> uniformLocations = new HashMap<>();
|
||||
private Map<String, Integer> attribLocations = new HashMap<>();
|
||||
|
||||
public ComputeProgram(GL4 gl, String compute) {
|
||||
this.gl = gl;
|
||||
|
||||
super(gl);
|
||||
this.gl4 = gl;
|
||||
|
||||
computeShader = createAndCompileShader(GL4.GL_COMPUTE_SHADER, compute);
|
||||
|
||||
shaderProgram = gl.glCreateProgram();
|
||||
program = gl.glCreateProgram();
|
||||
|
||||
gl.glAttachShader(shaderProgram, computeShader);
|
||||
gl.glAttachShader(program, computeShader);
|
||||
|
||||
gl.glLinkProgram(shaderProgram);
|
||||
}
|
||||
|
||||
public int getUniformLocation(String uniform) {
|
||||
Integer result = uniformLocations.get(uniform);
|
||||
|
||||
if (result == null) {
|
||||
result = gl.glGetUniformLocation(shaderProgram, uniform);
|
||||
|
||||
uniformLocations.put(uniform, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public int getAttribLocation(String attrib) {
|
||||
Integer result = attribLocations.get(attrib);
|
||||
|
||||
if (result == null) {
|
||||
result = gl.glGetAttribLocation(shaderProgram, attrib);
|
||||
|
||||
attribLocations.put(attrib, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void bindAttributeLocation(int location, String name) {
|
||||
gl.glBindAttribLocation(shaderProgram, location, name);
|
||||
}
|
||||
|
||||
public void begin() {
|
||||
gl.glUseProgram(shaderProgram);
|
||||
gl.glLinkProgram(program);
|
||||
}
|
||||
|
||||
public void compute(int x, int y, int z) {
|
||||
gl.glDispatchCompute(x, y, z);
|
||||
}
|
||||
|
||||
public void end() {
|
||||
gl.glUseProgram(0);
|
||||
gl4.glDispatchCompute(x, y, z);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
gl.glDetachShader(shaderProgram, computeShader);
|
||||
gl.glDetachShader(program, computeShader);
|
||||
gl.glDeleteShader(computeShader);
|
||||
|
||||
gl.glDeleteProgram(shaderProgram);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
private int createAndCompileShader(int type, String shaderString) {
|
||||
int shader = gl.glCreateShader(type);
|
||||
|
||||
String[] vlines = new String[]{shaderString};
|
||||
int[] vlengths = new int[]{vlines[0].length()};
|
||||
|
||||
gl.glShaderSource(shader, vlines.length, vlines, vlengths, 0);
|
||||
gl.glCompileShader(shader);
|
||||
|
||||
int[] compiled = new int[1];
|
||||
gl.glGetShaderiv(shader, GL2ES2.GL_COMPILE_STATUS, compiled, 0);
|
||||
|
||||
if (compiled[0] == 0) {
|
||||
int[] logLength = new int[1];
|
||||
gl.glGetShaderiv(shader, GL2ES2.GL_INFO_LOG_LENGTH, logLength, 0);
|
||||
|
||||
byte[] log = new byte[logLength[0]];
|
||||
gl.glGetShaderInfoLog(shader, logLength[0], (int[]) null, 0, log, 0);
|
||||
|
||||
throw new IllegalStateException("Error compiling the shader: " + new String(log));
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
107
src/com/persesgames/jogl/shader/Program.java
Normal file
107
src/com/persesgames/jogl/shader/Program.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package com.persesgames.jogl.shader;
|
||||
|
||||
import com.persesgames.jogl.Renderer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.media.opengl.GL2ES2;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/*-----------------------------------------------------+
|
||||
| App |
|
||||
| pPPAPPp |
|
||||
| APP PPa |
|
||||
| APA pPP PapA PapA |
|
||||
| PPA APA pP P pP P |
|
||||
| APPPPPPPA PPp Ap Ap Ap Ap |
|
||||
| apPPA aPP P P |
|
||||
| APA pPP p p |
|
||||
| pPP PPA |
|
||||
| PPp PPPp |
|
||||
| |
|
||||
| Created by: App Software |
|
||||
| Email: info@appsoftware.nl |
|
||||
| Web: http://www.appsoftware.nl/ |
|
||||
| |
|
||||
+-----------------------------------------------------*/
|
||||
|
||||
public abstract class Program {
|
||||
|
||||
protected GL2ES2 gl;
|
||||
|
||||
protected int program;
|
||||
|
||||
private Map<String, Integer> uniformLocations = new HashMap<>();
|
||||
private Map<String, Integer> attribLocations = new HashMap<>();
|
||||
|
||||
public Program(GL2ES2 gl) {
|
||||
this.gl = gl;
|
||||
}
|
||||
|
||||
public int getUniformLocation(String uniform) {
|
||||
Integer result = uniformLocations.get(uniform);
|
||||
|
||||
if (result == null) {
|
||||
result = gl.glGetUniformLocation(program, uniform);
|
||||
|
||||
uniformLocations.put(uniform, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public int getAttribLocation(String attrib) {
|
||||
Integer result = attribLocations.get(attrib);
|
||||
|
||||
if (result == null) {
|
||||
result = gl.glGetAttribLocation(program, attrib);
|
||||
|
||||
attribLocations.put(attrib, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void bindAttributeLocation(int location, String name) {
|
||||
gl.glBindAttribLocation(program, location, name);
|
||||
}
|
||||
|
||||
public void begin() {
|
||||
gl.glUseProgram(program);
|
||||
}
|
||||
|
||||
public void end() {
|
||||
gl.glUseProgram(0);
|
||||
}
|
||||
|
||||
protected void dispose() {
|
||||
gl.glDeleteProgram(program);
|
||||
}
|
||||
|
||||
protected int createAndCompileShader(int type, String shaderString) {
|
||||
int shader = gl.glCreateShader(type);
|
||||
|
||||
String[] vlines = new String[]{shaderString};
|
||||
int[] vlengths = new int[]{vlines[0].length()};
|
||||
|
||||
gl.glShaderSource(shader, vlines.length, vlines, vlengths, 0);
|
||||
gl.glCompileShader(shader);
|
||||
|
||||
int[] compiled = new int[1];
|
||||
gl.glGetShaderiv(shader, GL2ES2.GL_COMPILE_STATUS, compiled, 0);
|
||||
|
||||
if (compiled[0] == 0) {
|
||||
int[] logLength = new int[1];
|
||||
gl.glGetShaderiv(shader, GL2ES2.GL_INFO_LOG_LENGTH, logLength, 0);
|
||||
|
||||
byte[] log = new byte[logLength[0]];
|
||||
gl.glGetShaderInfoLog(shader, logLength[0], (int[]) null, 0, log, 0);
|
||||
|
||||
throw new IllegalStateException("Error compiling the shader: " + new String(log));
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,8 +5,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.media.opengl.GL2ES2;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/*-----------------------------------------------------+
|
||||
| App |
|
||||
@@ -26,20 +24,14 @@ import java.util.Map;
|
||||
| |
|
||||
+-----------------------------------------------------*/
|
||||
|
||||
public class ShaderProgram {
|
||||
public class ShaderProgram extends Program {
|
||||
private final static Logger logger = LoggerFactory.getLogger(Renderer.class);
|
||||
|
||||
private GL2ES2 gl;
|
||||
|
||||
private int shaderProgram;
|
||||
private int vertShader;
|
||||
private int fragShader;
|
||||
|
||||
private Map<String, Integer> uniformLocations = new HashMap<>();
|
||||
private Map<String, Integer> attribLocations = new HashMap<>();
|
||||
|
||||
public ShaderProgram(GL2ES2 gl, String vertex, String fragment) {
|
||||
this.gl = gl;
|
||||
super(gl);
|
||||
|
||||
if (gl.isGL3core()) {
|
||||
logger.info("GL3 core detected: explicit adding #version 130 to shaders");
|
||||
@@ -51,81 +43,21 @@ public class ShaderProgram {
|
||||
vertShader = createAndCompileShader(GL2ES2.GL_VERTEX_SHADER, vertex);
|
||||
fragShader = createAndCompileShader(GL2ES2.GL_FRAGMENT_SHADER, fragment);
|
||||
|
||||
shaderProgram = gl.glCreateProgram();
|
||||
program = gl.glCreateProgram();
|
||||
|
||||
gl.glAttachShader(shaderProgram, vertShader);
|
||||
gl.glAttachShader(shaderProgram, fragShader);
|
||||
gl.glAttachShader(program, vertShader);
|
||||
gl.glAttachShader(program, fragShader);
|
||||
|
||||
gl.glLinkProgram(shaderProgram);
|
||||
}
|
||||
|
||||
public int getUniformLocation(String uniform) {
|
||||
Integer result = uniformLocations.get(uniform);
|
||||
|
||||
if (result == null) {
|
||||
result = gl.glGetUniformLocation(shaderProgram, uniform);
|
||||
|
||||
uniformLocations.put(uniform, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public int getAttribLocation(String attrib) {
|
||||
Integer result = attribLocations.get(attrib);
|
||||
|
||||
if (result == null) {
|
||||
result = gl.glGetAttribLocation(shaderProgram, attrib);
|
||||
|
||||
attribLocations.put(attrib, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void bindAttributeLocation(int location, String name) {
|
||||
gl.glBindAttribLocation(shaderProgram, location, name);
|
||||
}
|
||||
|
||||
public void begin() {
|
||||
gl.glUseProgram(shaderProgram);
|
||||
}
|
||||
|
||||
public void end() {
|
||||
gl.glUseProgram(0);
|
||||
gl.glLinkProgram(program);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
gl.glDetachShader(shaderProgram, vertShader);
|
||||
gl.glDetachShader(program, vertShader);
|
||||
gl.glDeleteShader(vertShader);
|
||||
gl.glDetachShader(shaderProgram, fragShader);
|
||||
gl.glDetachShader(program, fragShader);
|
||||
gl.glDeleteShader(fragShader);
|
||||
gl.glDeleteProgram(shaderProgram);
|
||||
}
|
||||
|
||||
private int createAndCompileShader(int type, String shaderString) {
|
||||
int shader = gl.glCreateShader(type);
|
||||
|
||||
String[] vlines = new String[]{shaderString};
|
||||
int[] vlengths = new int[]{vlines[0].length()};
|
||||
|
||||
gl.glShaderSource(shader, vlines.length, vlines, vlengths, 0);
|
||||
gl.glCompileShader(shader);
|
||||
|
||||
int[] compiled = new int[1];
|
||||
gl.glGetShaderiv(shader, GL2ES2.GL_COMPILE_STATUS, compiled, 0);
|
||||
|
||||
if (compiled[0] == 0) {
|
||||
int[] logLength = new int[1];
|
||||
gl.glGetShaderiv(shader, GL2ES2.GL_INFO_LOG_LENGTH, logLength, 0);
|
||||
|
||||
byte[] log = new byte[logLength[0]];
|
||||
gl.glGetShaderInfoLog(shader, logLength[0], (int[]) null, 0, log, 0);
|
||||
|
||||
throw new IllegalStateException("Error compiling the shader: " + new String(log));
|
||||
}
|
||||
|
||||
return shader;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user