Friday, February 25, 2011

OpenGL ES Shading Language

Coming from an OpenGL 1.5 + Nvidia Cg environment there are some noticable differences when migrating over to OpenGL ES 2.0. This post is like a cheat sheet for myself and others to use as a quick reference to look up some major keypoints of the Opengl ES Shading Language.

Precision & range of variables

The OpenGL ES Shading Language, used in e.g. IPhone OpenGL ES 2.0, lets you decide upon the range and precision used to represent integer or floating point variables.

Precision qualifiers:

  • highp (e.g. highp mat4 myMatrix)
  • mediump (e.g. varying mediump vec2 myTexCoord)
  • lowp (e.g. lowp float myColor)

Minimum ranges and precision

Bits FP Range FP Precision Int Range
highp 16 (-262, 262) Relative 2-16 (-216, 216)
mediump 10 (-214, 214) Relative 2-10 (-210, 210)
lowp 8 (-2, 2) Absolute 2-8 (-28, 28)

default vertex shader precisions

  • precision highp float
  • precision highp int
  • precision lowp sampler2D
  • precision lowp samplerCube

default FRAGMENT shader precisions

  • precision mediump int
  • precision lowp sampler2D
  • precision lowp samplerCube

Notice the fragment shader is missing a default precision for float. This means you must include precision qualifier for floats.

Built-in Variables

Vertex shader

Type Precision
gl_Position vec4 highp
gl_PointSize float mediump

Fragment shader

Type Precision
gl_FragCoord vec4 mediump
gl_FrontFacing bool
gl_FragColor vec4 mediump
gl_FragData[gl_MaxDrawBuffers] vec4 mediump
gl_PointCoord vec2 mediump

No comments:

Post a Comment