Criss
Criss

Reputation: 621

Behaviour of length function on buffer sized not divisible by size of its type

What will return the length function if buffer bound to 0 SSBO binding point is of size=36 (not divisible by size of uvec4 = 16)? And what's the rule?..

#version 430 core
layout(local_size_x=256) in;

layout(std430, binding=0) buffer B { uvec4 data[]; };

void main() {
    uint s = data.length();
    //some other code...
}

Upvotes: 1

Views: 255

Answers (1)

Rabbid76
Rabbid76

Reputation: 211166

For a shader storage block, the length() method, on the unsized (run-time sized) array as its last member, will return an value of type int, calculated by the following formula:

max((buffer_object_size - offset_of_array) / stride_of_array, 0)    

This means if a buffer with a size of 36 bytes is bound to the following shader storage block

layout(std430, binding=0) buffer B { uvec4 data[]; };

then data.length() will return 2.

buffer_object_size = 36
offset_of_array    =  0
stride_of_array    = 16

max((36 - 0) / 16, 0) = 2 

See ARB_shader_storage_buffer_object; Issue (19) (far at the end of the document):

In this expression, we allow unsized arrays at the end of shader storage blocks, and allow the ".length()" method to be used to determine the size of such arrays based on the size of the provided buffer object.
The derived array size can be derived by reversing the process described in issue (16):

array.length() =
    max((buffer_object_size - offset_of_array) / stride_of_array, 0)

Upvotes: 2

Related Questions