Using structs with LWJGL in combination with SSBOs

Started by Lobo, July 04, 2015, 16:44:18

Previous topic - Next topic

Lobo

Hi,

I'm new here and I want to ask how I can bind buffers to structs in my vertex shader with LWJGL?

For example I have this struct in my VS
struct Particle
{
    vec4 pos;
    vec4 vel;
    float mass;
};

layout (std430, binding = 0) buffer ParticleBuffer
{
    Particle vertexPos[];
}; 
....


My question now is how can I will this struct with LWJGL? With c++ it is realtive simple I create just the same struct at C++ side, but in Java?
I cannot find anything about this topic in this forum, maybe I'm searching for the wrong keywords.

Actually I have 3 SSBOs instead of one struct:

layout (std430, binding = 0) buffer VertexPos
{
    vec4 vertexPos[];
}; 

layout (std430, binding = 1) buffer VertexVel
{
    vec4 vertexVel[];
};  

layout (std430, binding = 2) buffer VertexMass
{
    float vertexMass[];
}; 


That means I have to upload 3 buffers from Java to the VS with 3 binding calls:

positionBuffer.rewind();
		GL30.glBindBufferBase(GL43.GL_SHADER_STORAGE_BUFFER, 0, positionBufferHandle);
		GL15.glBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, 0, positionBuffer);
		
		velocityBuffer.rewind();
		GL30.glBindBufferBase(GL43.GL_SHADER_STORAGE_BUFFER, 1, velocityBufferHandle);
		GL15.glBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, 0, velocityBuffer);
		
		GL30.glBindBufferBase(GL43.GL_SHADER_STORAGE_BUFFER, 2, massBufferHandle);
		GL15.glBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, 0,massBuffer);


I think if it is possible to upload just one buffer (maybe all datas aligned) and just use one binding call it will run a little bit faster.

Would be great if anybody could help.

Thanks a lot!

Regards
Lobo

spasi

Yes, you can do that. You write all particle data in the same FloatBuffer and use a single call to upload it.

You must be careful with the field offsets and struct stride. It isn't easy in C/C++ either, because the alignment/padding rules for GLSL structs are not the same for C structs. The std layouts are fully defined in section 7.6.2.2 of the OpenGL spec. For packed and shared layouts, you must query the driver to get the correct offsets/stride.

For more info see section 4.4.5 of the GLSL spec and the OpenGL wiki.

Lobo

Thank you for the tip that everything can be uploaded in one float buffer. It works now :-)