Aeva / m.grl

Midnight Graphics & Recreation Library
http://mgrl.midnightsisters.org
GNU Lesser General Public License v3.0
44 stars 3 forks source link

'instanced' type for switching between uniforms and attributes #197

Closed Aeva closed 7 years ago

Aeva commented 8 years ago

This is an example of how the syntax might look:

attribute vec3 position;
instanced mat4 world_matrix;

void main(void) {
  // do some stuff
}

And this is an example of how the compiler output might look:

attribute vec3 position;

uniform bool _instanced_render;
attribute vec4 _inst_attra_world_matrix;
attribute vec4 _inst_attrb_world_matrix;
attribute vec4 _inst_attrc_world_matrix;
attribute vec4 _inst_attrd_world_matrix;
uniform mat4 _inst_uni_world_matrix;
mat4 world_matrix;

void main(void) {
  if (_instanced_render) {
    world_matrix[0] = _inst_attra_world_matrix;
    world_matrix[1] = _inst_attrb_world_matrix;
    world_matrix[2] = _inst_attrc_world_matrix;
    world_matrix[3] = _inst_attrd_world_matrix;
  }
  else {
    world_matrix = _inst_uni_world_matrix;
  }

  // do some stuff
}

The variable _instanced_render would be used to control when instancing is active, in which case the instanced variables would be populated from their respective attribute (or attributes, in the case of mat3 and mat4s). When it is false, the respective uniforms are referenced instead.

The generated uniforms would alias back to the expected value so that the application interface stays sensible. The attributes should not be handled per object, but rather be disabled by default and explicitly enabled when instancing is needed and be disabled afterwards - or there could be some kind of explicit mechanism for enabling instancing vis disabling it to eliminate state changes.

Aeva commented 8 years ago

Adding this feature should be fairly easy, especially because of the refactoring done as part of #196. The steps are something like so:

Aeva commented 8 years ago

I came up with a better keyword for this while figuring out what to do with #203:

attribute/uniform vec3 position;

I like this version because syntax highlighting still works in github :)

Aeva commented 7 years ago

Probably going to go with this for now, because it is more compact:

in/uniform vec3 position;
Aeva commented 7 years ago

Started working on this in the "instancing" branch from "fast_graph".

Things to do next:

Actually setting up the buffers for instancing is the scope of another issue (possibly #246).