VISTAS-IVES / pyvistas

VISualization of Terrestrial-Aquatic Systems
BSD 3-Clause "New" or "Revised" License
0 stars 2 forks source link

Add shader library for individual shader snippets #84

Open TaylorMutch opened 7 years ago

TaylorMutch commented 7 years ago

Many of our shaders share common elements - they all include the projectionMatrix, modelViewMatrix, and cameraPosition uniforms, as well as the position attribute. However, we currently re-implement the majority of these in every shader we write.

Adding a shader snippet library and integrating that into how the shaders are hot-reloaded would be greatly beneficial and ensures that shaders are written in a consistent way. The base ShaderProgram could then have arguments for indicating which builtin uniforms and attributes should be used, as well as the name to use for each (which can have a set of defaults).

It is also sometimes necessary to change certain attribute names in the shader, depending on what the intended use of the vertex buffer is for. For example, TerrainColorGeometry overrides the color array (i.e. standard location 3) to be value, which has an impact in how the shader is written. This would need to be handled by how the ShaderProgram is compiled.

The result for us is that the GLSL shaders could then be written with a #include common which is compiled to include necessary inputs determined above. For example, the bbox_vert.glsl shader goes from:

#version 330

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform vec3 cameraPosition;

layout(location = 0) in vec3 position;

void main() {
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

to

#include common
void main() {
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
nikmolnar commented 7 years ago

This would be awesome. I looked into doing it a while back in CVISTAS. From what I found, it basically means writing a preprocessor. Is that your take?

TaylorMutch commented 7 years ago

Essentially, yes. It would need to be hooked into the hot-reloader so that we can still tinker with shaders while the app is live, but I think it's totally doable.