Skip to content

Instantly share code, notes, and snippets.

@spite
Last active August 29, 2015 14:23
Show Gist options
  • Save spite/7ae92212b4f28076ba29 to your computer and use it in GitHub Desktop.
Save spite/7ae92212b4f28076ba29 to your computer and use it in GitHub Desktop.
A Modest Proposal: Standard for naming shaders and programs

##A Modest Proposal##

As per version 1.0.13 of the Shader Editor Extension, it will look for #define SHADERNAME name in both the vertex shader code and the fragment shader code.

If name is defined for both shaders, and it's the same string, the name of the program is that string, e.g.: "Shadertoy". common-name

If only one of the two is defined, the name of the program is "vs_name / fs_name", e.g.: "Quad / Shadertoy" mixed-name

If there's no #define on any of the sources, the program name is the program number, e.g.: "Program 3" no-name

At first I thought using a #pragma, but I was getting warnings: screen shot 2015-06-03 at 12 42 58

The SHADERNAME string is pretty random, just thinking of something that wouldn't collide with other #defines. It can be changed, but I think the important part is that we all agree.

Does it make sense? Would this be something it'd be interesting for developers to identify shaders, programs, materials, etc. regardless of frameworks or libraries?

@hughsk
Copy link

hughsk commented Jun 22, 2015

@spite perfect :D

@mattdesl
Copy link

Does that compile on all platforms? Just wondering, most macros that I've seen are valid GLSL code like:

#define FOO vec4(1.0)

Does getShaderSource preserve comments? If so it could also be done like so:

/*@name fancy-shader*/

@marklundin
Copy link

+1 SHADER_NAME

@spite
Copy link
Author

spite commented Jun 23, 2015

@mattdesl there are many ways, of course. I discarded #pragma because of the compiling warnings; and discarded using comments because it feels less hacky to use #define; I think getShaderSource can't be entirely relied on (from an instrumenting tool perspective) since a lot of WebGL programs delete the shaders once they're attached to the program.

I think the way the coder gets the relevant string (analyse the source independently, augment the WebGLProgram methods, inject set/getShaderSource) is not that important, as long as we agree on the basic things: it's in the source code, it's a #define, and it's called SHADER_NAME

I've tested OSX and Windows/ANGLE and the #define works correctly. Most of three.js #defines are not correct GLSL and there are no problems that I'm aware of.

@mrdoob
Copy link

mrdoob commented Jun 23, 2015

@spite
Copy link
Author

spite commented Jun 25, 2015

Cool! Updated on v1.0.14 (spite/ShaderEditorExtension#10)

@brianchirls
Copy link

@spite
Copy link
Author

spite commented Jun 25, 2015

@brianchirls Awesome! 😄

@craftfortress
Copy link

awesome thanks dude

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment