Skip to content

Commit

Permalink
Fix GL 2/3 flip/limit hack
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Sep 8, 2024
1 parent c4ec6cd commit 1b1d48a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static class CustomIntMap<T extends JSObject> implements JSObject {
@JSBody(params = { "key" }, script = "var value = this[key]; delete this[key]; return value;")
public native T remove (int key);

@JSBody(params = { "value" }, script = "for (i = 0; i < this.length; i++) { if (value === this[i]) { return i; } }")
@JSBody(params = { "value" }, script = "for (var i = 0; i < this.length; i++) { if (value === this[i]) { return i; } }")
public native int getKey (T value);
}

Expand Down Expand Up @@ -393,9 +393,8 @@ public void glGenBuffers(int n, IntBuffer buffers) {
for(int i = 0; i < n; i++) {
WebGLBufferWrapper buffer = gl.createBuffer();
int id = this.buffers.add(buffer);
buffers.put(id);
buffers.put(i, id);
}
buffers.limit(n);
}

@Override
Expand All @@ -414,9 +413,8 @@ public void glGenFramebuffers(int n, IntBuffer framebuffers) {
for(int i = 0; i < n; i++) {
WebGLFramebufferWrapper fb = gl.createFramebuffer();
int id = this.frameBuffers.add(fb);
framebuffers.put(id);
framebuffers.put(i, id);
}
framebuffers.limit(n);
}

@Override
Expand All @@ -430,9 +428,8 @@ public void glGenRenderbuffers(int n, IntBuffer renderbuffers) {
for(int i = 0; i < n; i++) {
WebGLRenderbufferWrapper rb = gl.createRenderbuffer();
int id = this.renderBuffers.add(rb);
renderbuffers.put(id);
renderbuffers.put(i, id);
}
renderbuffers.limit(n);
}

@Override
Expand All @@ -446,28 +443,23 @@ public void glGenTextures(int n, IntBuffer textures) {
for(int i = 0; i < n; i++) {
WebGLTextureWrapper texture = gl.createTexture();
int id = this.textures.add(texture);
textures.put(id);
textures.put(i, id);
}
textures.limit(n);
}

@Override
public String glGetActiveAttrib(int program, int index, IntBuffer size, IntBuffer type) {
WebGLActiveInfoWrapper activeUniform = gl.getActiveAttrib(programs.get(program), index);
size.put(activeUniform.getSize());
size.flip();
type.put(activeUniform.getType());
type.flip();
size.put(0, activeUniform.getSize());
type.put(0, activeUniform.getType());
return activeUniform.getName();
}

@Override
public String glGetActiveUniform(int program, int index, IntBuffer size, IntBuffer type) {
WebGLActiveInfoWrapper activeUniform = gl.getActiveUniform(programs.get(program), index);
size.put(activeUniform.getSize());
size.flip();
type.put(activeUniform.getType());
type.flip();
size.put(0, activeUniform.getSize());
type.put(0, activeUniform.getType());
return activeUniform.getName();
}

Expand Down Expand Up @@ -504,7 +496,6 @@ public void glGetFloatv(int pname, FloatBuffer params) {
if(pname == GL20.GL_DEPTH_CLEAR_VALUE || pname == GL20.GL_LINE_WIDTH || pname == GL20.GL_POLYGON_OFFSET_FACTOR
|| pname == GL20.GL_POLYGON_OFFSET_UNITS || pname == GL20.GL_SAMPLE_COVERAGE_VALUE) {
params.put(0, gl.getParameterf(pname));
params.limit(1);
}
else
throw new GdxRuntimeException("glGetFloat not supported by WebGL backend");
Expand All @@ -517,17 +508,15 @@ public void glGetFramebufferAttachmentParameteriv(int target, int attachment, in
case GL20.GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
case GL20.GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
params.put(0, gl.getFramebufferAttachmentParameteri(target, attachment, pname));
params.limit(1);
break;
case GL20.GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
WebGLTextureWrapper tex = (WebGLTextureWrapper)gl.getParametero(pname);
if(tex == null) {
params.put(0);
params.put(0, 0);
}
else {
params.put(textures.getKey(tex));
params.put(0, textures.getKey(tex));
}
params.flip();
return;
default:
throw new GdxRuntimeException("glGetFramebufferAttachmentParameteriv Invalid enum for WebGL backend.");
Expand Down Expand Up @@ -556,25 +545,22 @@ public void glGetIntegerv(int pname, IntBuffer params) {
|| pname == GL20.GL_STENCIL_PASS_DEPTH_PASS || pname == GL20.GL_STENCIL_REF || pname == GL20.GL_STENCIL_VALUE_MASK
|| pname == GL20.GL_STENCIL_WRITEMASK || pname == GL20.GL_SUBPIXEL_BITS || pname == GL20.GL_UNPACK_ALIGNMENT) {
params.put(0, gl.getParameteri(pname));
params.limit(1);
}
else if(pname == GL20.GL_VIEWPORT) {
Int32ArrayWrapper array = (Int32ArrayWrapper)gl.getParameterv(pname);
params.put(0, array.get(0));
params.put(1, array.get(1));
params.put(2, array.get(2));
params.put(3, array.get(3));
params.limit(4);
}
else if(pname == GL20.GL_FRAMEBUFFER_BINDING) {
WebGLFramebufferWrapper fbo = (WebGLFramebufferWrapper)gl.getParametero(pname);
if(fbo == null) {
params.put(0);
params.put(0, 0);
}
else {
params.put(frameBuffers.getKey(fbo));
params.put(0, frameBuffers.getKey(fbo));
}
params.flip();
}
else
throw new GdxRuntimeException("glGetInteger not supported by WebGL backend");
Expand All @@ -589,12 +575,11 @@ public String glGetProgramInfoLog(int program) {
public void glGetProgramiv(int program, int pname, IntBuffer params) {
if(pname == GL20.GL_DELETE_STATUS || pname == GL20.GL_LINK_STATUS || pname == GL20.GL_VALIDATE_STATUS) {
boolean result = gl.getProgramParameterb(programs.get(program), pname);
params.put(result ? GL20.GL_TRUE : GL20.GL_FALSE);
params.put(0, result ? GL20.GL_TRUE : GL20.GL_FALSE);
}
else {
params.put(gl.getProgramParameteri(programs.get(program), pname));
params.put(0, gl.getProgramParameteri(programs.get(program), pname));
}
params.flip();
}

@Override
Expand All @@ -612,13 +597,12 @@ public String glGetShaderInfoLog(int shader) {
public void glGetShaderiv(int shader, int pname, IntBuffer params) {
if(pname == GL20.GL_COMPILE_STATUS || pname == GL20.GL_DELETE_STATUS) {
boolean result = gl.getShaderParameterb(shaders.get(shader), pname);
params.put(result ? GL20.GL_TRUE : GL20.GL_FALSE);
params.put(0, result ? GL20.GL_TRUE : GL20.GL_FALSE);
}
else {
int result = gl.getShaderParameteri(shaders.get(shader), pname);
params.put(result);
params.put(0, result);
}
params.flip();
}

@Override
Expand Down Expand Up @@ -756,7 +740,6 @@ public void glReadPixels(int x, int y, int width, int height, int format, int ty
int size = 4 * width * height;
Uint8ArrayWrapper buffer = TypedArrays.createUint8Array(typedArray.getBuffer(), typedArray.getByteOffset(), size);
gl.readPixels(x, y, width, height, format, type, buffer);
pixels.limit(size);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,11 @@ public void glGenQueries(int n, int[] ids, int offset) {

@Override
public void glGenQueries(int n, IntBuffer ids) {
int startPosition = ids.position();
for(int i = 0; i < n; i++) {
WebGLQueryWrapper query = gl.createQuery();
int id = queries.add(query);
ids.put(id);
ids.put(i, id);
}
ids.position(startPosition);
}

@Override
Expand All @@ -306,13 +304,11 @@ public void glGenSamplers(int count, int[] samplerr, int offset) {

@Override
public void glGenSamplers(int n, IntBuffer ids) {
int startPosition = ids.position();
for(int i = 0; i < n; i++) {
WebGLSamplerWrapper sampler = gl.createSampler();
int id = samplers.add(sampler);
ids.put(id);
ids.put(i, id);
}
ids.position(startPosition);
}

@Override
Expand All @@ -326,13 +322,11 @@ public void glGenTransformFeedbacks(int n, int[] ids, int offset) {

@Override
public void glGenTransformFeedbacks(int n, IntBuffer ids) {
int startPosition = ids.position();
for(int i = 0; i < n; i++) {
WebGLTransformFeedbackWrapper feedback = gl.createTransformFeedback();
int id = feedbacks.add(feedback);
ids.put(id);
ids.put(i, id);
}
ids.position(startPosition);
}

@Override
Expand All @@ -346,36 +340,33 @@ public void glGenVertexArrays(int n, int[] arrays, int offset) {

@Override
public void glGenVertexArrays(int n, IntBuffer ids) {
int startPosition = ids.position();
for(int i = 0; i < n; i++) {
WebGLVertexArrayObjectWrapper vArray = gl.createVertexArray();
int id = vertexArrays.add(vArray);
ids.put(id);
ids.put(i, id);
}
ids.position(startPosition);
}

@Override
public void glGetActiveUniformBlockiv(int program, int uniformBlockIndex, int pname, IntBuffer params) {
if(pname == GL30.GL_UNIFORM_BLOCK_BINDING || pname == GL30.GL_UNIFORM_BLOCK_DATA_SIZE
|| pname == GL30.GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS) {
params.put(gl.getActiveUniformBlockParameteri(programs.get(program), uniformBlockIndex, pname));
params.put(0, gl.getActiveUniformBlockParameteri(programs.get(program), uniformBlockIndex, pname));
}
else if(pname == GL30.GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES) {
Uint32ArrayWrapper array = gl.getActiveUniformBlockParameterv(programs.get(program), uniformBlockIndex, pname);
for(int i = 0; i < array.getLength(); i++) {
params.put(i, (int)array.get(i));
params.put(i, array.get(i));
}
}
else if(pname == GL30.GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER
|| pname == GL30.GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER) {
boolean result = gl.getActiveUniformBlockParameterb(programs.get(program), uniformBlockIndex, pname);
params.put(result ? GL20.GL_TRUE : GL20.GL_FALSE);
params.put(0, result ? GL20.GL_TRUE : GL20.GL_FALSE);
}
else {
throw new GdxRuntimeException("Unsupported pname passed to glGetActiveUniformBlockiv");
}
params.flip();
}

@Override
Expand All @@ -402,7 +393,6 @@ public void glGetActiveUniformsiv(int program, int uniformCount, IntBuffer unifo
params.put(i, arr.get(i));
}
}
params.limit(uniformCount);
}

@Override
Expand All @@ -420,7 +410,6 @@ public void glGetFloatv(int pname, FloatBuffer params) {
// Override GwtGL20 method to check if it's a pname introduced with GL30.
if(pname == GL30.GL_MAX_TEXTURE_LOD_BIAS) {
params.put(0, gl.getParameterf(pname));
params.limit(1);
}
else {
super.glGetFloatv(pname, params);
Expand Down Expand Up @@ -478,39 +467,35 @@ public void glGetIntegerv(int pname, IntBuffer params) {
case GL30.GL_UNPACK_SKIP_PIXELS:
case GL30.GL_UNPACK_SKIP_ROWS:
params.put(0, gl.getParameteri(pname));
params.limit(1);
return;
case GL30.GL_DRAW_FRAMEBUFFER_BINDING:
case GL30.GL_READ_FRAMEBUFFER_BINDING:
WebGLFramebufferWrapper fbo = (WebGLFramebufferWrapper)gl.getParametero(pname);
if(fbo == null) {
params.put(0);
params.put(0, 0);
}
else {
params.put(frameBuffers.getKey(fbo));
params.put(0, frameBuffers.getKey(fbo));
}
params.flip();
return;
case GL30.GL_TEXTURE_BINDING_2D_ARRAY:
case GL30.GL_TEXTURE_BINDING_3D:
WebGLTextureWrapper tex = (WebGLTextureWrapper)gl.getParametero(pname);
if(tex == null) {
params.put(0);
params.put(0, 0);
}
else {
params.put(textures.getKey(tex));
params.put(0, textures.getKey(tex));
}
params.flip();
return;
case GL30.GL_VERTEX_ARRAY_BINDING:
WebGLVertexArrayObjectWrapper obj = (WebGLVertexArrayObjectWrapper)gl.getParametero(pname);
if(obj == null) {
params.put(0);
params.put(0, 0);
}
else {
params.put(vertexArrays.getKey(obj));
params.put(0, vertexArrays.getKey(obj));
}
params.flip();
return;
default:
// Assume it is a GL20 pname
Expand All @@ -526,8 +511,7 @@ public void glGetInteger64v(int pname, LongBuffer params) {
case GL30.GL_MAX_ELEMENT_INDEX:
case GL30.GL_MAX_SERVER_WAIT_TIMEOUT:
case GL30.GL_MAX_UNIFORM_BLOCK_SIZE:
params.put(gl.getParameteri64(pname));
params.flip();
params.put(0, gl.getParameteri64(pname));
return;
default:
throw new UnsupportedOperationException("Given glGetInteger64v enum not supported on WebGL2");
Expand All @@ -547,7 +531,6 @@ public void glGetFramebufferAttachmentParameteriv (int target, int attachment, i
case GL30.GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
case GL30.GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER:
params.put(0, gl.getFramebufferAttachmentParameteri(target, attachment, pname));
params.limit(1);
break;
default:
// Assume it is a GL20 pname
Expand All @@ -561,40 +544,36 @@ public void glGetQueryiv(int target, int pname, IntBuffer params) {
// Similar to how GwtGL20 handles FBO in glGetIntegerv
WebGLQueryWrapper query = gl.getQuery(target, pname);
if(query == null) {
params.put(0);
params.put(0, 0);
}
else {
params.put(queries.getKey(query));
params.put(0, queries.getKey(query));
}
params.flip();
}

@Override
public void glGetQueryObjectuiv(int id, int pname, IntBuffer params) {
// In WebGL2 getQueryObject was renamed to getQueryParameter
if(pname == GL30.GL_QUERY_RESULT) {
params.put(gl.getQueryParameteri(queries.get(id), pname));
params.put(0, gl.getQueryParameteri(queries.get(id), pname));
}
else if(pname == GL30.GL_QUERY_RESULT_AVAILABLE) {
boolean result = gl.getQueryParameterb(queries.get(id), pname);
params.put(result ? GL20.GL_TRUE : GL20.GL_FALSE);
params.put(0, result ? GL20.GL_TRUE : GL20.GL_FALSE);
}
else {
throw new GdxRuntimeException("Unsupported pname passed to glGetQueryObjectuiv");
}
params.flip();
}

@Override
public void glGetSamplerParameterfv(int sampler, int pname, FloatBuffer params) {
params.put(gl.getSamplerParameterf(samplers.get(sampler), pname));
params.flip();
params.put(0, gl.getSamplerParameterf(samplers.get(sampler), pname));
}

@Override
public void glGetSamplerParameteriv(int sampler, int pname, IntBuffer params) {
params.put(gl.getSamplerParameteri(samplers.get(sampler), pname));
params.flip();
params.put(0, gl.getSamplerParameteri(samplers.get(sampler), pname));
}

@Override
Expand All @@ -614,7 +593,6 @@ public void glGetUniformIndices(int program, String[] uniformNames, IntBuffer un
for(int i = 0; i < length; i++) {
uniformIndices.put(i, array.get(i));
}
uniformIndices.limit(length);
}

@Override
Expand Down

0 comments on commit 1b1d48a

Please sign in to comment.