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