You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! I'm trying to work out how to use Uniform Buffer Objects with libgdx/lwjgl, and I can't seem to get the data to bind correctly.
Following the UniformBufferObjectsTest.java in #7037 I've put together the following code that should create a new float[] buffer, populate it, and then render it:
intprogramId = compiled.getShader().getHandle();
// Get the block index for the uniform blockintuniformBlockIndex = Gdx.gl30.glGetUniformBlockIndex(programId, "VertexData");
if (uniformBlockIndex < 0) {
thrownewIllegalStateException(String.format("found no uniformBlockIndex: %s", uniformBlockIndex));
}
// creates a simple FloatBufferfloat[] data = newfloat[4];
data[0] = 1;
data[1] = 1;
data[2] = 1;
data[3] = 1;
FloatBufferbuf = BufferUtils.newFloatBuffer(data.length);
buf.put(data);
System.out.println("buf[0] = " + buf.get(0)); // yes, the FloatBuffer is being set to 1.0f// Create a temporary buffer to create a new handle to store buffers (?)IntBuffertmpBuffer = BufferUtils.newIntBuffer(16);
Gdx.gl30.glGenBuffers(1, tmpBuffer);
intbufferHandle = tmpBuffer.get(0);
// create the actual UBO using this handleGdx.gl30.glBindBuffer(GL30.GL_UNIFORM_BUFFER, bufferHandle);
intsize = buf.capacity() * 4/* a float is 4 bytes */;
Gdx.gl30.glBufferData(GL30.GL_UNIFORM_BUFFER, size, buf, GL30.GL_STATIC_DRAW);
Gdx.gl30.glBindBuffer(GL30.GL_UNIFORM_BUFFER, 0);
// Use the index to bind to a binding point, then bind the bufferintbindingPoint = 0;
Gdx.gl30.glBindBuffer(GL30.GL_UNIFORM_BUFFER, bufferHandle);
Gdx.gl30.glUniformBlockBinding(programId, uniformBlockIndex, bindingPoint);
Gdx.gl30.glBindBufferBase(GL30.GL_UNIFORM_BUFFER, bindingPoint, bufferHandle);
Gdx.gl30.glBindBuffer(GL30.GL_UNIFORM_BUFFER, 0);
// fill the buffer? possibly not necessary?Gdx.gl30.glBindBuffer(GL30.GL_UNIFORM_BUFFER, bufferHandle);
Gdx.gl30.glBufferSubData(GL30.GL_UNIFORM_BUFFER, 0/* offset */, size/* size */, buf);
Gdx.gl30.glBindBuffer(GL30.GL_UNIFORM_BUFFER, 0);
// update the buffer data store (should only be in the render call)Gdx.gl30.glBindBuffer(GL30.GL_UNIFORM_BUFFER, bufferHandle);
Gdx.gl30.glBufferSubData(GL30.GL_UNIFORM_BUFFER, 0, buf.capacity() * 4, buf);
Gdx.gl30.glBindBuffer(GL30.GL_UNIFORM_BUFFER, 0);
// we're still able to bind other uniformscompiled.getShader().setUniformf("anotherValue", 0.5f);
// and then draw as normalbatch.draw(texture, area.x, area.y, area.width, area.height);
The fragment shader code I'm using to debug is:
#version150uniformsampler2D u_texture;
invec2 v_texCoords; // provided by vertex shaderlayout (std140) uniform VertexData {
vec2[4] v_polygon;
};
uniformfloat anotherValue;
outvec4 fragColor;
void main()
{
// we ignore the texture but libgdx asserts there's always u_texture: https://stackoverflow.com/q/27967788/39531vec4 sampledButIgnored = texture(u_texture, v_texCoords);
if (v_polygon[0].x ==0) {
// v_polygon is not being populated with any data :(
fragColor =vec4(anotherValue /* shows we're getting normal uniforms */, 1.0, 0.0, 1.0);
} else {
// we have data in v_polygon, we don't get here
fragColor =vec4(v_polygon[0].x, v_polygon[0].y, v_polygon[1].x, 1.0);
}
fragColor +=vec4(0, 0, 0, sampledButIgnored.a);
}
As far as I can tell I've got everything set up correctly but data just isn't getting loaded into the UBO/shader. Any ideas would be appreciated.
(I'd love to have first-class support for UBOs in a future version of libgdx!)
The text was updated successfully, but these errors were encountered:
After that I had a flickering problem but that was probably because I was creating a brand new buffer on every single frame. If instead I created a single UBO then the flickering stops.
float[] data = newfloat[8];
data[0] = 0.5f; // first vec2 is at offset=0data[1] = 0.5f;
data[4] = 0.5f; // second vec2 is at offset=16data[5] = 0.5f;
FloatBufferbuf = BufferUtils.newFloatBuffer(data.length);
buf.put(data);
buf.rewind(); // essential!intsize = buf.capacity() * 4; // a float is 4 bytes
Hello! I'm trying to work out how to use Uniform Buffer Objects with libgdx/lwjgl, and I can't seem to get the data to bind correctly.
Following the UniformBufferObjectsTest.java in #7037 I've put together the following code that should create a new
float[]
buffer, populate it, and then render it:The fragment shader code I'm using to debug is:
As far as I can tell I've got everything set up correctly but data just isn't getting loaded into the UBO/shader. Any ideas would be appreciated.
(I'd love to have first-class support for UBOs in a future version of libgdx!)
The text was updated successfully, but these errors were encountered: