Shader, Uniform variables and buffer sizes

Started by Qudus, March 26, 2007, 18:20:09

Previous topic - Next topic

elias

Quote from: Qudus on March 29, 2007, 00:47:40
The position and limit HAVE the correct values and I'm storing the falues of a float array to the buffer as you can see here:
if (value instanceof float[])
{
    float[] v = (float[])value;
    
    setupTempFloatBuffer( v.length );
    
    tmpFloatBuffer.put( v );
    tmpFloatBuffer.rewind();
    ARBShaderObjects.glUniform1ARB( location, tmpFloatBuffer );
}


Rewind is not the best choice, but it should work in this case.

private void setupTempFloatBuffer(int minCap)
{
    if (tmpFloatBuffer.capacity() < minCap)
    {
        tmpFloatBuffer = BufferUtils.createFloatBuffer( (int)(minCap * 1.5) );
    }
    
    if (tmpFloatBuffer.limit() != minCap)
    {
        tmpFloatBuffer.limit( minCap );
    }
    
    tmpFloatBuffer.clear();
}


This is wrong, you're clear()ing the buffer right after setting the limit. Clearing means setting the position to 0, and the limit to the buffer capacity. I'd do something like this (untested):

private void setupTempFloatBuffer(int minCap)
{
    if (tmpFloatBuffer.capacity() < minCap)
    {
        tmpFloatBuffer = BufferUtils.createFloatBuffer( (int)(minCap * 1.5) );
    }
    
    tmpFloatBuffer.clear();
}

if (value instanceof float[])
{
    float[] v = (float[])value;
    
    setupTempFloatBuffer( v.length );
    
    tmpFloatBuffer.put( v );
    tmpFloatBuffer.flip();
    ARBShaderObjects.glUniform1ARB( location, tmpFloatBuffer );
}


Notice that the limit is not set anymore, and rewind is replaced with flip() which will set the position to 0 and the limit to the old position.

Regarding the "java.lang.IllegalArgumentException: Missing null termination" exception, you need to have the last byte in the uniform name be 0, to satisfy OpenGL requirements of a C string. When you didn't reuse buffers, the trailing bytes were always 0, so it didn't appear as a problem before.

- elias

Qudus

Stupid me. pushing an additional (byte)0 to the tmpByteBuffer did the trick. I had that once in my coding, but it didn't help. Now it solved my problem.

Thanks a lot for all your help :).

Marvin