136

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?

Waqar
  • 6,944
  • 2
  • 26
  • 39
Samssonart
  • 3,193
  • 3
  • 25
  • 41
  • 10
    As far as I know, they don't have "correct" extensions, as OpenGL won't read them from disk anyways. – zneak Jun 21 '11 at 22:37
  • 2
    Some people call them .vs and .fs (and .gs) to make explicit what's inside. But like zneak said, it really doesn't matter, there is no "correct" thing. – Damon Jun 21 '11 at 22:39
  • 11
    GEdit uses `.glslv` and `.glslf` when choosing syntax highlighting. That's the only place I've seen where it matters. – Piotr Praszmo Jun 21 '11 at 22:44
  • I don't understand why this question is marked as opinion based. Like I understand that GLSL has no correct file extensions, but this is a perfectly valid non-opinion based question. If I asked the same thing for C++ files, then somebody would tell me it's `cpp`. In this case you just say there is none. The question is not at fault here. – Jaacko Torus May 18 '21 at 23:16

5 Answers5

103

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
Yves M.
  • 26,153
  • 20
  • 93
  • 125
Colonel Thirty Two
  • 18,351
  • 7
  • 32
  • 69
95

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.

Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829
  • 22
    I don't think .vert|.frag are good extension names for shaders. The extension is something which identifies the general class of a file. They should have probably called them vertex.glsl and fragment.glsl. – Autodidact Oct 11 '13 at 14:39
  • 5
    I was surprised too, but isn't there a slight difference in syntax between vertex and fragment shaders, @SandeepDatta? In the same way that .h and .c might share a lot in common, but they're used in different ways. – Joseph Humfrey Feb 04 '14 at 14:21
  • 7
    @SandeepDatta `.hpp` vs. `.cpp`? `.h` vs. `.c`? There are more syntax and semantic differences between vertex and fragment shaders than there are between C/C++ header and source files. – Miles Rout May 10 '14 at 03:44
  • 1
    @MilesRout Not even to speak about .cc –  Sep 10 '14 at 09:16
  • 45
    GLSLang, also known as the [GLSL Reference Parser](https://www.opengl.org/sdk/tools/glslang/) or [Reference Compiler](https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/), is one of the tools developed by [3Dlabs](http://en.wikipedia.org/wiki/3Dlabs). It's listed as an SDK tool on both opengl.org and khronos.org. The [README](https://www.opengl.org/sdk/tools/glslang/README.txt) lists the file extensions it expects for shader files: `.vert` (vertex), `.frag` (fragment), `.tesc` (tessellation control), `.tese` (tessellation evaluation), `.geom` (geometry), `.comp` (compute). – TachyonVortex Oct 09 '14 at 16:28
  • It would be nice to include the comment by @TachyonVortex in the answer, so we have all that usefull information right there. – tfrascaroli Jan 23 '17 at 12:04
20

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.

datenwolf
  • 149,702
  • 12
  • 167
  • 273
  • 5
    Downvoted because OS X has been primarily using the file extension for years. – Frederik Slijkerman Nov 14 '12 at 11:47
  • 6
    @FrederikSlijkerman: No, it doesn't. MacOS X is a Unix at its core and file extensions were never used there for identifying thing. Yes, standard types get standard file extensions, but that's only a human readability thing. Maybe the Finder may rely on file extensions as heuristics for some types. But if it can identify a file solely by its header or some magic bytes, it will use that. Like any Unix based system does. – datenwolf Nov 14 '12 at 12:14
  • 3
    +1 I name them as _effect-name_-fs.glsl or _effect-name_-vs.glsl. – legends2k Feb 19 '14 at 01:30
  • 10
    I realize that I'm two years late for the OS X argument, but I felt like I should throw in my two cents. Everything above the UNIX layer uses LaunchServices to determine file associations. Each file has a type identifier like `com.stackoverflow.file`, which is stored as metadata. LaunchServices tries to guess it from the MIME type first, if the metadata entry exists. If not, it will look for the extension. If it can't match it, it will look for an HFS creator code. If there isn't any, it gives up. LaunchServices never tries to identify a file by its header or magic bytes. – zneak Sep 09 '14 at 15:29
  • 3
    This means that the most common way of determining the type of a file on OS X is its extension. This is only for the purpose of identifying which application should be used to open which file, though. Once the application opens the file, it can decide for itself what it wants to do with it, regardless of if the extension is correct for the content type. – zneak Sep 09 '14 at 15:31
  • @zneak Thanks for the explanation, that'll explain how macOS can tell the difference between a PDF and an Adobe Illustrator document, when they're ultimately the same format... –  May 15 '18 at 09:00
7

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.

Weavermount
  • 688
  • 8
  • 19
  • 2
    I believe this is only true if you have a GLSL library installed in Sublime, e.g. https://github.com/WebGLTools/GL-Shader-Validator - my default Sublime does not recognise the extensions. – Air May 19 '14 at 00:25
  • The lib you linked is by far the most popular (and possibly only?) GLSL lib for sublime, and I called what that expects as what sublime expects. If you call that an overstatement I'll plead guilty. But yes you are right in at least as much as that sublime as a dutiful text editor will open any text doc while ignoring extensions it doesn't know about. – Weavermount May 19 '14 at 21:28
  • 1
    As of today sublime-glsl (https://github.com/euler0/sublime-glsl) is the most popular GLSL plugin and it accepts the following set of extensions: `vs, fs, gs, vsh, fsh, gsh, vshader, fshader, gshader, vert, frag, geom, tesc, tese, comp, glsl`. So there's a variety to choose from :) – F Lekschas Dec 12 '18 at 01:15
0

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...

user2439483
  • 117
  • 1
  • 1