2
votes

im basically trying to provide diffrent shape informations about objects to a ray tracer written in glsl.

shapes like

sphere: vec3 center; float radius;

box: vec3 center; vec3 halfSize;

etc.

the problem are the following requirements:

  1. in fragment shader have access to all shapes
  2. do not send them every frame using uniform variables, cache them somehow for longer time until manually removed.
  3. be able to update data from the application
  4. data is of variable length each type (count of spheres, boxes, ...)

the data is not interleaved.

  • so uniform variables send too much data.
  • attributes do not seem to be ment for very many objects (attribute arrays use a location each element)
  • textures change the data unwantedly and are ugly to access shape information

so how can one do that?

preferably also having a struct represent the data. like struct Sphere { vec3 p; float r; }; ...

1
Generally, in a fragment shader the only way you are going to have access to "all" of something is if you use textures or more recently (OpenGL 4.2+) image load/store for state storage. Compute Shaders are a much closer fit for this sort of thing, so it would be nice to know what generation of hardware you are targeting. - Andon M. Coleman

1 Answers

2
votes

You can use Uniform Buffer Objects, if the amount of storage you need isn't too gigantic. These are basically blocks of uniforms that are all bound together with glUniformBlockBinding instead of individually. That way the whole buffer is written to once, and then just bound before each shader invocation. UBOs are required to have a max size of at least 16KB, so you should be okay unless you have a large number of primitives/primitive types. (Note that arrays must be of fixed size, so you'll need to either recompile the shader every time you change the sizes of the primitive arrays, or allocate enough to hold the maximum number you'll ever need.

Otherwise, you'd need OpenGL 4.3 and Shader Storage Buffer Objects, which are basically UBOs but bigger and can be written to (reads and writes are incoherent, so you'll need to use the same memory barrier when using SSBOs as you would for an image uniform).