17ec681f3SmrgShading Language 27ec681f3Smrg================ 37ec681f3Smrg 47ec681f3SmrgThis page describes the features and status of Mesa's support for the 57ec681f3Smrg`OpenGL Shading Language <https://opengl.org/documentation/glsl/>`__. 67ec681f3Smrg 77ec681f3Smrg.. _envvars: 87ec681f3Smrg 97ec681f3SmrgEnvironment Variables 107ec681f3Smrg--------------------- 117ec681f3Smrg 127ec681f3SmrgThe **MESA_GLSL** environment variable can be set to a comma-separated 137ec681f3Smrglist of keywords to control some aspects of the GLSL compiler and shader 147ec681f3Smrgexecution. These are generally used for debugging. 157ec681f3Smrg 167ec681f3Smrg- **dump** - print GLSL shader code to stdout at link time 177ec681f3Smrg- **log** - log all GLSL shaders to files. The filenames will be 187ec681f3Smrg "shader_X.vert" or "shader_X.frag" where X the shader ID. 197ec681f3Smrg- **cache_info** - print debug information about shader cache 207ec681f3Smrg- **cache_fb** - force cached shaders to be ignored and do a full 217ec681f3Smrg recompile via the fallback path 227ec681f3Smrg- **uniform** - print message to stdout when glUniform is called 237ec681f3Smrg- **nopvert** - force vertex shaders to be a simple shader that just 247ec681f3Smrg transforms the vertex position with ftransform() and passes through 257ec681f3Smrg the color and texcoord[0] attributes. 267ec681f3Smrg- **nopfrag** - force fragment shader to be a simple shader that passes 277ec681f3Smrg through the color attribute. 287ec681f3Smrg- **useprog** - log glUseProgram calls to stderr 297ec681f3Smrg- **errors** - GLSL compilation and link errors will be reported to 307ec681f3Smrg stderr. 317ec681f3Smrg 327ec681f3SmrgExample: export MESA_GLSL=dump,nopt 337ec681f3Smrg 347ec681f3Smrg.. _replacement: 357ec681f3Smrg 367ec681f3SmrgExperimenting with Shader Replacements 377ec681f3Smrg~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 387ec681f3Smrg 397ec681f3SmrgShaders can be dumped and replaced on runtime for debugging purposes. 407ec681f3SmrgThis is controlled via following environment variables: 417ec681f3Smrg 427ec681f3Smrg- **MESA_SHADER_DUMP_PATH** - path where shader sources are dumped 437ec681f3Smrg- **MESA_SHADER_READ_PATH** - path where replacement shaders are read 447ec681f3Smrg 457ec681f3SmrgNote, path set must exist before running for dumping or replacing to 467ec681f3Smrgwork. When both are set, these paths should be different so the dumped 477ec681f3Smrgshaders do not clobber the replacement shaders. Also, the filenames of 487ec681f3Smrgthe replacement shaders should match the filenames of the corresponding 497ec681f3Smrgdumped shaders. 507ec681f3Smrg 517ec681f3Smrg.. _capture: 527ec681f3Smrg 537ec681f3SmrgCapturing Shaders 547ec681f3Smrg~~~~~~~~~~~~~~~~~ 557ec681f3Smrg 567ec681f3SmrgSetting **MESA_SHADER_CAPTURE_PATH** to a directory will cause the 577ec681f3Smrgcompiler to write ``.shader_test`` files for use with 587ec681f3Smrg`shader-db <https://gitlab.freedesktop.org/mesa/shader-db>`__, a tool 597ec681f3Smrgwhich compiler developers can use to gather statistics about shaders 607ec681f3Smrg(instructions, cycles, memory accesses, and so on). 617ec681f3Smrg 627ec681f3SmrgNotably, this captures linked GLSL shaders - with all stages together - 637ec681f3Smrgas well as ARB programs. 647ec681f3Smrg 657ec681f3SmrgGLSL Version 667ec681f3Smrg------------ 677ec681f3Smrg 687ec681f3SmrgThe GLSL compiler currently supports version 3.30 of the shading 697ec681f3Smrglanguage. 707ec681f3Smrg 717ec681f3SmrgSeveral GLSL extensions are also supported: 727ec681f3Smrg 737ec681f3Smrg- GL_ARB_draw_buffers 747ec681f3Smrg- GL_ARB_fragment_coord_conventions 757ec681f3Smrg- GL_ARB_shader_bit_encoding 767ec681f3Smrg 777ec681f3SmrgUnsupported Features 787ec681f3Smrg-------------------- 797ec681f3Smrg 807ec681f3SmrgXXX update this section 817ec681f3Smrg 827ec681f3SmrgThe following features of the shading language are not yet fully 837ec681f3Smrgsupported in Mesa: 847ec681f3Smrg 857ec681f3Smrg- Linking of multiple shaders does not always work. Currently, linking 867ec681f3Smrg is implemented through shader concatenation and re-compiling. This 877ec681f3Smrg doesn't always work because of some #pragma and preprocessor issues. 887ec681f3Smrg- The gl_Color and gl_SecondaryColor varying vars are interpolated 897ec681f3Smrg without perspective correction 907ec681f3Smrg 917ec681f3SmrgAll other major features of the shading language should function. 927ec681f3Smrg 937ec681f3SmrgImplementation Notes 947ec681f3Smrg-------------------- 957ec681f3Smrg 967ec681f3Smrg- Shading language programs are compiled into low-level programs very 977ec681f3Smrg similar to those of GL_ARB_vertex/fragment_program. 987ec681f3Smrg- All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full 997ec681f3Smrg float[4] registers. 1007ec681f3Smrg- Float constants and variables are packed so that up to four floats 1017ec681f3Smrg can occupy one program parameter/register. 1027ec681f3Smrg- All function calls are inlined. 1037ec681f3Smrg- Shaders which use too many registers will not compile. 1047ec681f3Smrg- The quality of generated code is pretty good, register usage is fair. 1057ec681f3Smrg- Shader error detection and reporting of errors (InfoLog) is not very 1067ec681f3Smrg good yet. 1077ec681f3Smrg- The ftransform() function doesn't necessarily match the results of 1087ec681f3Smrg fixed-function transformation. 1097ec681f3Smrg 1107ec681f3SmrgThese issues will be addressed/resolved in the future. 1117ec681f3Smrg 1127ec681f3SmrgProgramming Hints 1137ec681f3Smrg----------------- 1147ec681f3Smrg 1157ec681f3Smrg- Use the built-in library functions whenever possible. For example, 1167ec681f3Smrg instead of writing this: 1177ec681f3Smrg 1187ec681f3Smrg .. code-block:: glsl 1197ec681f3Smrg 1207ec681f3Smrg float x = 1.0 / sqrt(y); 1217ec681f3Smrg 1227ec681f3Smrg Write this: 1237ec681f3Smrg 1247ec681f3Smrg .. code-block:: glsl 1257ec681f3Smrg 1267ec681f3Smrg float x = inversesqrt(y); 1277ec681f3Smrg 1287ec681f3SmrgStand-alone GLSL Compiler 1297ec681f3Smrg------------------------- 1307ec681f3Smrg 1317ec681f3SmrgThe stand-alone GLSL compiler program can be used to compile GLSL 1327ec681f3Smrgshaders into low-level GPU code. 1337ec681f3Smrg 1347ec681f3SmrgThis tool is useful for: 1357ec681f3Smrg 1367ec681f3Smrg- Inspecting GPU code to gain insight into compilation 1377ec681f3Smrg- Generating initial GPU code for subsequent hand-tuning 1387ec681f3Smrg- Debugging the GLSL compiler itself 1397ec681f3Smrg 1407ec681f3SmrgAfter building Mesa, the compiler can be found at 1417ec681f3Smrgsrc/compiler/glsl/glsl_compiler 1427ec681f3Smrg 1437ec681f3SmrgHere's an example of using the compiler to compile a vertex shader and 1447ec681f3Smrgemit GL_ARB_vertex_program-style instructions: 1457ec681f3Smrg 1467ec681f3Smrg.. code-block:: console 1477ec681f3Smrg 1487ec681f3Smrg src/compiler/glsl/glsl_compiler --version XXX --dump-ast myshader.vert 1497ec681f3Smrg 1507ec681f3SmrgOptions include 1517ec681f3Smrg 1527ec681f3Smrg- **--dump-ast** - dump GPU code 1537ec681f3Smrg- **--dump-hir** - dump high-level IR code 1547ec681f3Smrg- **--dump-lir** - dump low-level IR code 1557ec681f3Smrg- **--dump-builder** - dump GLSL IR code 1567ec681f3Smrg- **--link** - link shaders 1577ec681f3Smrg- **--just-log** - display only shader / linker info if exist, without 1587ec681f3Smrg any header or separator 1597ec681f3Smrg- **--version** - [Mandatory] define the GLSL version to use 1607ec681f3Smrg 1617ec681f3SmrgCompiler Implementation 1627ec681f3Smrg----------------------- 1637ec681f3Smrg 1647ec681f3SmrgThe source code for Mesa's shading language compiler is in the 1657ec681f3Smrg``src/compiler/glsl/`` directory. 1667ec681f3Smrg 1677ec681f3SmrgXXX provide some info about the compiler.... 1687ec681f3Smrg 1697ec681f3SmrgThe final vertex and fragment programs may be interpreted in software 1707ec681f3Smrg(see prog_execute.c) or translated into a specific hardware architecture 1717ec681f3Smrg(see drivers/dri/i915/i915_fragprog.c for example). 1727ec681f3Smrg 1737ec681f3SmrgCompiler Validation 1747ec681f3Smrg------------------- 1757ec681f3Smrg 1767ec681f3SmrgDevelopers working on the GLSL compiler should test frequently to avoid 1777ec681f3Smrgregressions. 1787ec681f3Smrg 1797ec681f3SmrgThe `Piglit <https://piglit.freedesktop.org/>`__ project has many GLSL 1807ec681f3Smrgtests. 1817ec681f3Smrg 1827ec681f3SmrgThe Mesa demos repository also has some good GLSL tests. 183