Hi fellow wiki editors!

To help newly registered users get more familiar with the wiki (and maybe older users too) there is now a {{Welcome to the wiki}} template. Have a look at it and feel free to add it to new users discussion pages (and perhaps your own).

I have tried to keep the template short, but meaningful. /Johan G

Difference between revisions of "Shader Coding - Best Practices"

From FlightGear wiki
Jump to: navigation, search
m (Vertex Shaders: removing as per i4dnf's expert advice)
(Fragment Shaders)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{WIP|Will be based on: http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg35934.html}}
 
{{WIP|Will be based on: http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg35934.html}}
 +
{{Rendering}}
 +
== File Header ==
 +
{{Note|It's a good idea to add a header to each shader detailing:
 +
* filename
 +
* purpose
 +
* license
 +
* author
 +
* rendering pipeline (rembrandt/default)
 +
* description
 +
<syntaxhighlight lang="glsl">
 +
// -*- mode: C; -*-
 +
// urban.frag
 +
// Licence: GPL v2
 +
// Author: Frederic Bouvier.
 +
//  Adapted from the paper by F. Policarpo et al. : Real-time Relief Mapping on Arbitrary Polygonal Surfaces
 +
//  Adapted from the paper and sources by M. Drobot in GPU Pro : Quadtree Displacement Mapping with Height Blending
 +
</syntaxhighlight>
 +
}}
 +
  
 
== Supported GLSL Versions ==
 
== Supported GLSL Versions ==
Line 6: Line 25:
 
<syntaxhighlight lang="glsl">
 
<syntaxhighlight lang="glsl">
 
#version 120
 
#version 120
 +
</syntaxhighlight>
 +
}}
 +
 +
== Enabling Extensions ==
 +
 +
{{Note|:To enable vendor specific extensions, use the '''#extension''' pragma at the top of the file, for example:
 +
<syntaxhighlight lang="glsl">
 +
#extension GL_ATI_shader_texture_lod : enable
 +
#extension GL_ARB_shader_texture_lod : enable
 
</syntaxhighlight>
 
</syntaxhighlight>
 
}}
 
}}
Line 22: Line 50:
 
== Fragment Shaders ==
 
== Fragment Shaders ==
  
{{Note|To check if your shader is working, add this as the last line, it should turn all pixels black:
+
{{Note|To check if your shader is working, use a trivial  shader like this one or just add the last line to the end of the shader, it should turn all pixels black:
 
<syntaxhighlight lang="glsl">
 
<syntaxhighlight lang="glsl">
 +
#version 120
 +
void main() {
 +
// turn all pixels into  black
 
gl_FragColor = vec4 (0.0,0.0,0.0,1.0);
 
gl_FragColor = vec4 (0.0,0.0,0.0,1.0);
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
}}
 
}}
Line 37: Line 69:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
}}
 
}}
 +
 +
[[Category:Shader development]]

Latest revision as of 15:06, 1 September 2019

WIP.png Work in progress
This article or section will be worked on in the upcoming hours or days.
Note: Will be based on: http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg35934.html
See history for the latest developments.

File Header

Note  It's a good idea to add a header to each shader detailing:
  • filename
  • purpose
  • license
  • author
  • rendering pipeline (rembrandt/default)
  • description
// -*- mode: C; -*-
// urban.frag
// Licence: GPL v2
// Author: Frederic Bouvier.
//  Adapted from the paper by F. Policarpo et al. : Real-time Relief Mapping on Arbitrary Polygonal Surfaces
//  Adapted from the paper and sources by M. Drobot in GPU Pro : Quadtree Displacement Mapping with Height Blending


Supported GLSL Versions

Note  Be aware that we currently _DO_NOT_ support glsl > 1.2x. Please don't mix '''glsl''' flavours: please stick to glsl 1.2x.(even though support for it is largely deprecated). The transition to OpenGL 3.0 / glsl >=1.3x is a very big undertaking, and not backward compatible. It is always a good idea to declare the expected GLSL version at the top of each file:
#version 120

Enabling Extensions

Note  :To enable vendor specific extensions, use the #extension pragma at the top of the file, for example:
#extension GL_ATI_shader_texture_lod : enable
#extension GL_ARB_shader_texture_lod : enable

Vertex Shaders

Note  For testing purposes, you can use a simple pass-through vertex shader:
#version 120
void main(void) {
        gl_Position = ftransform();
}

Fragment Shaders

Note  To check if your shader is working, use a trivial shader like this one or just add the last line to the end of the shader, it should turn all pixels black:
#version 120
void main() {
// turn all pixels into  black
gl_FragColor = vec4 (0.0,0.0,0.0,1.0);
}
Note  if4dnf: it's usually bad practice to do any kind of operations on gl_FragColor assignment. At most a vec4() swizzle is accepted, although even that one is treated differently based on the platform (some assign a temporary variable, some don't). Do wahtever you need in a separate vec4 variable and just assign it's value to gl_FragColor at the end.
Note  If the fragment isn't running, there should be an error message in the log file (I wish they'd still be written to the console). Might be that the texture isn't defined in the supporting framework for instance - the shader assumed that it is available, but you need to declare it in the matching C++ code/effect file first.
Note  This is how you can make a pixel darker:
gl_FragColor = color * 0.5;