| 1 |
Shading Language |
| 2 |
================ |
| 3 |
|
| 4 |
This page describes the features and status of Mesa's support for the |
| 5 |
`OpenGL Shading Language <https://wikis.khronos.org/opengl/OpenGL_Shading_Language>`__. |
| 6 |
|
| 7 |
.. _envvars: |
| 8 |
|
| 9 |
Environment Variables |
| 10 |
--------------------- |
| 11 |
|
| 12 |
The **MESA_GLSL** environment variable can be set to a comma-separated |
| 13 |
list of keywords to control some aspects of the GLSL compiler and shader |
| 14 |
execution. These are generally used for debugging. |
| 15 |
|
| 16 |
- **dump** - print GLSL shader code, IR, and NIR to stdout at link time |
| 17 |
- **source** - print GLSL shader code to stdout at link time |
| 18 |
- **log** - log all GLSL shaders to files. The filenames will be |
| 19 |
"shader_X.vert" or "shader_X.frag" where X the shader ID. |
| 20 |
- **cache_info** - print debug information about shader cache |
| 21 |
- **cache_fb** - force cached shaders to be ignored and do a full |
| 22 |
recompile via the fallback path |
| 23 |
- **uniform** - print message to stdout when glUniform is called |
| 24 |
- **nopvert** - force vertex shaders to be a simple shader that just |
| 25 |
transforms the vertex position with ftransform() and passes through |
| 26 |
the color and texcoord[0] attributes. |
| 27 |
- **nopfrag** - force fragment shader to be a simple shader that passes |
| 28 |
through the color attribute. |
| 29 |
- **useprog** - log glUseProgram calls to stderr |
| 30 |
- **errors** - GLSL compilation and link errors will be reported to |
| 31 |
stderr. |
| 32 |
|
| 33 |
Example: export MESA_GLSL=dump,nopt |
| 34 |
|
| 35 |
.. _replacement: |
| 36 |
|
| 37 |
Experimenting with Shader Replacements |
| 38 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 39 |
|
| 40 |
Shaders can be dumped and replaced on runtime for debugging purposes. |
| 41 |
This is controlled via following environment variables: |
| 42 |
|
| 43 |
- **MESA_SHADER_DUMP_PATH** - path where shader sources are dumped |
| 44 |
- **MESA_SHADER_READ_PATH** - path where replacement shaders are read |
| 45 |
|
| 46 |
Note, path set must exist before running for dumping or replacing to |
| 47 |
work. When both are set, these paths should be different so the dumped |
| 48 |
shaders do not clobber the replacement shaders. Also, the filenames of |
| 49 |
the replacement shaders should match the filenames of the corresponding |
| 50 |
dumped shaders. |
| 51 |
|
| 52 |
.. _capture: |
| 53 |
|
| 54 |
Capturing Shaders |
| 55 |
~~~~~~~~~~~~~~~~~ |
| 56 |
|
| 57 |
Setting **MESA_SHADER_CAPTURE_PATH** to a directory will cause the |
| 58 |
compiler to write ``.shader_test`` files for use with |
| 59 |
`shader-db <https://gitlab.freedesktop.org/mesa/shader-db>`__, a tool |
| 60 |
which compiler developers can use to gather statistics about shaders |
| 61 |
(instructions, cycles, memory accesses, and so on). |
| 62 |
|
| 63 |
Notably, this captures linked GLSL shaders - with all stages together - |
| 64 |
as well as ARB programs. |
| 65 |
|
| 66 |
GLSL Version |
| 67 |
------------ |
| 68 |
|
| 69 |
The GLSL compiler currently supports version 3.30 of the shading |
| 70 |
language. |
| 71 |
|
| 72 |
Several GLSL extensions are also supported: |
| 73 |
|
| 74 |
- :ext:`GL_ARB_draw_buffers` |
| 75 |
- :ext:`GL_ARB_fragment_coord_conventions` |
| 76 |
- :ext:`GL_ARB_shader_bit_encoding` |
| 77 |
|
| 78 |
Unsupported Features |
| 79 |
-------------------- |
| 80 |
|
| 81 |
XXX update this section |
| 82 |
|
| 83 |
The following features of the shading language are not yet fully |
| 84 |
supported in Mesa: |
| 85 |
|
| 86 |
- Linking of multiple shaders does not always work. Currently, linking |
| 87 |
is implemented through shader concatenation and re-compiling. This |
| 88 |
doesn't always work because of some #pragma and preprocessor issues. |
| 89 |
- The gl_Color and gl_SecondaryColor varying vars are interpolated |
| 90 |
without perspective correction |
| 91 |
|
| 92 |
All other major features of the shading language should function. |
| 93 |
|
| 94 |
Implementation Notes |
| 95 |
-------------------- |
| 96 |
|
| 97 |
- Shading language programs are compiled into low-level programs very |
| 98 |
similar to those of :ext:`GL_ARB_vertex_program` / |
| 99 |
:ext:`GL_ARB_fragment_program`. |
| 100 |
- All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full |
| 101 |
float[4] registers. |
| 102 |
- Float constants and variables are packed so that up to four floats |
| 103 |
can occupy one program parameter/register. |
| 104 |
- All function calls are inlined. |
| 105 |
- Shaders which use too many registers will not compile. |
| 106 |
- The quality of generated code is pretty good, register usage is fair. |
| 107 |
- Shader error detection and reporting of errors (InfoLog) is not very |
| 108 |
good yet. |
| 109 |
- The ftransform() function doesn't necessarily match the results of |
| 110 |
fixed-function transformation. |
| 111 |
|
| 112 |
These issues will be addressed/resolved in the future. |
| 113 |
|
| 114 |
Programming Hints |
| 115 |
----------------- |
| 116 |
|
| 117 |
- Use the built-in library functions whenever possible. For example, |
| 118 |
instead of writing this: |
| 119 |
|
| 120 |
.. code-block:: glsl |
| 121 |
|
| 122 |
float x = 1.0 / sqrt(y); |
| 123 |
|
| 124 |
Write this: |
| 125 |
|
| 126 |
.. code-block:: glsl |
| 127 |
|
| 128 |
float x = inversesqrt(y); |
| 129 |
|
| 130 |
Stand-alone GLSL Compiler |
| 131 |
------------------------- |
| 132 |
|
| 133 |
The stand-alone GLSL compiler program can be used to compile GLSL |
| 134 |
shaders into GLSL IR code. |
| 135 |
|
| 136 |
This tool is useful for: |
| 137 |
|
| 138 |
- Inspecting GLSL frontend behavior to gain insight into compilation |
| 139 |
- Debugging the GLSL compiler itself |
| 140 |
|
| 141 |
After building Mesa with the ``-Dtools=glsl`` meson option, the compiler will be |
| 142 |
installed as the binary ``glsl_compiler``. |
| 143 |
|
| 144 |
Here's an example of using the compiler to compile a vertex shader and |
| 145 |
emit :ext:`GL_ARB_vertex_program`-style instructions: |
| 146 |
|
| 147 |
.. code-block:: sh |
| 148 |
|
| 149 |
src/compiler/glsl/glsl_compiler --version XXX --dump-ast myshader.vert |
| 150 |
|
| 151 |
Options include |
| 152 |
|
| 153 |
.. option:: --dump-ast |
| 154 |
|
| 155 |
dump source syntax tree |
| 156 |
|
| 157 |
.. option:: --dump-hir |
| 158 |
|
| 159 |
dump high-level IR code |
| 160 |
|
| 161 |
.. option:: --dump-lir |
| 162 |
|
| 163 |
dump low-level IR code |
| 164 |
|
| 165 |
.. option:: --link |
| 166 |
|
| 167 |
link shaders |
| 168 |
|
| 169 |
.. option:: --just-log |
| 170 |
|
| 171 |
display only shader / linker info if exist, without |
| 172 |
any header or separator |
| 173 |
|
| 174 |
.. option:: --version |
| 175 |
|
| 176 |
[Mandatory] define the GLSL version to use |
| 177 |
|
| 178 |
Compiler Implementation |
| 179 |
----------------------- |
| 180 |
|
| 181 |
The source code for Mesa's shading language compiler is in the |
| 182 |
``src/compiler/glsl/`` directory. |
| 183 |
|
| 184 |
XXX provide some info about the compiler.... |
| 185 |
|
| 186 |
The final vertex and fragment programs may be interpreted in software |
| 187 |
(see prog_execute.c) or translated into a specific hardware architecture |
| 188 |
(see drivers/dri/i915/i915_fragprog.c for example). |
| 189 |
|
| 190 |
Compiler Validation |
| 191 |
------------------- |
| 192 |
|
| 193 |
Developers working on the GLSL compiler should test frequently to avoid |
| 194 |
regressions. |
| 195 |
|
| 196 |
The `Piglit <https://piglit.freedesktop.org/>`__ project has many GLSL |
| 197 |
tests. |
| 198 |
|
| 199 |
The Mesa demos repository also has some good GLSL tests. |