What is the correct file extension for GLSL shaders?

OpenglGlslShader

Opengl Problem Overview


I'm learning glsl shading and I've come across different file formats. I've seen people giving their vertex and fragment shaders .vert and .frag extensions. But I've also seen .vsh and .fsh extensions, and even both shaders together in a single .glsl file. So I'm wondering if there is a standard file format, or which way is the 'correct' one?

Opengl Solutions


Solution 1 - Opengl

There's no official extension in the spec. OpenGL doesn't handle loading shaders from files; you just pass in the shader code as a string, so there's no specific file format.

However, glslang, Khronos' reference GLSL compiler/validator, uses the following extensions to determine what type of shader that the file is for:

>* .vert - a vertex shader >* .tesc - a tessellation control shader >* .tese - a tessellation evaluation shader >* .geom - a geometry shader >* .frag - a fragment shader >* .comp - a compute shader

Solution 2 - Opengl

The glslang compiler created by Khronos makes assumptions about the shader stage based on the extension, but there is no standard extension outside of this (and quite a few projects make up their own). The glslang compiler keys off of .vert, .tesc (TESsellation Control shaders), .tese (TESsellation Evaluation shaders), .geom, .frag, and .comp.

But that's about it for any form of standard extension.

Solution 3 - Opengl

Identifying file type by extension is a thing specific to Windows. All other operating systems use different approaches: MacOS X stores the file type in a special metadata structure in the file system entries. Most *nixes identify files by testing their internal structure against a database of known "magic bytes"; however text editors use the extension.

Anyway, GLSL sources are just like any other program source file: plain text, and that's their file type.

The extension you may choose as you wish. I use the following naming:

  • ts.glsl
  • gs.glsl
  • vs.glsl
  • fs.glsl

but that's my choice and technically my programs don't even enforce any naming or extension scheme. The naming is for humans to read and know what's in it; having a common major extension requires me to have an syntax highlighing rule for only one file extension set.

Solution 4 - Opengl

As others have mentioned there isn't a correct answer in the strictest sense. It does bear mentioning that Sublime (confirmed for v2 and v3) also expects .vert and .frag for syntax highlighting and validation.

Solution 5 - Opengl

There are two ways of writing shaders.

You can store the vertex shader and fragment shader content in a char * variable and compile, link and attach the shader to a program.

Another way is to write the seperate vertex and fragment shader file with whatever extension you like and read it to compile, link and attach the shader to the program.

So the naming convention, like .vert/.frag, .vsdr/.fsdr, etc. are all valid as long as you know how to read it...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamssonartView Question on Stackoverflow
Solution 1 - OpenglColonel Thirty TwoView Answer on Stackoverflow
Solution 2 - OpenglNicol BolasView Answer on Stackoverflow
Solution 3 - OpengldatenwolfView Answer on Stackoverflow
Solution 4 - OpenglWeavermountView Answer on Stackoverflow
Solution 5 - Opengluser2439483View Answer on Stackoverflow