Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shading.html 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <HTML>
  2. <TITLE>Shading Language Support</TITLE>
  3. <link rel="stylesheet" type="text/css" href="mesa.css"></head>
  4. <BODY>
  5. <H1>Shading Language Support</H1>
  6. <p>
  7. This page describes the features and status of Mesa's support for the
  8. <a href="http://opengl.org/documentation/glsl/" target="_parent">
  9. OpenGL Shading Language</a>.
  10. </p>
  11. <p>
  12. Last updated on 26 March 2007.
  13. </p>
  14. <p>
  15. Contents
  16. </p>
  17. <ul>
  18. <li><a href="#unsup">Unsupported Features</a>
  19. <li><a href="#notes">Implementation Notes</a>
  20. <li><a href="#hints">Programming Hints</a>
  21. <li><a href="#standalone">Stand-alone Compiler</a>
  22. <li><a href="#implementation">Compiler Implementation</a>
  23. </ul>
  24. <a name="unsup">
  25. <h2>Unsupported Features</h2>
  26. <p>
  27. The following features of the shading language are not yet supported
  28. in Mesa:
  29. </p>
  30. <ul>
  31. <li>Dereferencing arrays with non-constant indexes
  32. <li>Comparison of user-defined structs
  33. <li>Linking of multiple shaders is not supported
  34. <li>gl_ClipVertex
  35. </ul>
  36. <p>
  37. All other major features of the shading language should function.
  38. </p>
  39. <a name="notes">
  40. <h2>Implementation Notes</h2>
  41. <ul>
  42. <li>Shading language programs are compiled into low-level programs
  43. very similar to those of GL_ARB_vertex/fragment_program.
  44. <li>All vector types (vec2, vec3, vec4, bvec2, etc) currently occupy full
  45. float[4] registers.
  46. <li>Float constants and variables are packed so that up to four floats
  47. can occupy one program parameter/register.
  48. <li>All function calls are inlined.
  49. <li>Shaders which use too many registers will not compile.
  50. <li>The quality of generated code is pretty good, register usage is fair.
  51. <li>Shader error detection and reporting of errors (InfoLog) is not
  52. very good yet.
  53. <li>There are known memory leaks in the compiler.
  54. </ul>
  55. <p>
  56. These issues will be addressed/resolved in the future.
  57. </p>
  58. <a name="hints">
  59. <h2>Programming Hints</h2>
  60. <ul>
  61. <li>Declare <em>in</em> function parameters as <em>const</em> whenever possible.
  62. This improves the efficiency of function inlining.
  63. </li>
  64. <br>
  65. <li>To reduce register usage, declare variables within smaller scopes.
  66. For example, the following code:
  67. <pre>
  68. void main()
  69. {
  70. vec4 a1, a2, b1, b2;
  71. gl_Position = expression using a1, a2.
  72. gl_Color = expression using b1, b2;
  73. }
  74. </pre>
  75. Can be rewritten as follows to use half as many registers:
  76. <pre>
  77. void main()
  78. {
  79. {
  80. vec4 a1, a2;
  81. gl_Position = expression using a1, a2.
  82. }
  83. {
  84. vec4 b1, b2;
  85. gl_Color = expression using b1, b2;
  86. }
  87. }
  88. </pre>
  89. Alternately, rather than using several float variables, use
  90. a vec4 instead. Use swizzling and writemasks to access the
  91. components of the vec4 as floats.
  92. </li>
  93. <br>
  94. <li>Use the built-in library functions whenever possible.
  95. For example, instead of writing this:
  96. <pre>
  97. float x = 1.0 / sqrt(y);
  98. </pre>
  99. Write this:
  100. <pre>
  101. float x = inversesqrt(y);
  102. </pre>
  103. </ul>
  104. <a name="standalone">
  105. <h2>Stand-alone Compiler</h2>
  106. <p>
  107. A unique stand-alone GLSL compiler driver has been added to Mesa.
  108. <p>
  109. <p>
  110. The stand-alone compiler (like a conventional command-line compiler)
  111. is a tool that accepts Shading Language programs and emits low-level
  112. GPU programs.
  113. </p>
  114. <p>
  115. This tool is useful for:
  116. <p>
  117. <ul>
  118. <li>Inspecting GPU code to gain insight into compilation
  119. <li>Generating initial GPU code for subsequent hand-tuning
  120. <li>Debugging the GLSL compiler itself
  121. </ul>
  122. <p>
  123. To build the glslcompiler program (this will be improved someday):
  124. </p>
  125. <pre>
  126. cd src/mesa
  127. make libmesa.a
  128. cd drivers/glslcompiler
  129. make
  130. </pre>
  131. <p>
  132. Here's an example of using the compiler to compile a vertex shader and
  133. emit GL_ARB_vertex_program-style instructions:
  134. </p>
  135. <pre>
  136. glslcompiler --arb --linenumbers --vs vertshader.txt
  137. </pre>
  138. <p>
  139. The output may look similar to this:
  140. </p>
  141. <pre>
  142. !!ARBvp1.0
  143. 0: MOV result.texcoord[0], vertex.texcoord[0];
  144. 1: DP4 temp0.x, state.matrix.mvp.row[0], vertex.position;
  145. 2: DP4 temp0.y, state.matrix.mvp.row[1], vertex.position;
  146. 3: DP4 temp0.z, state.matrix.mvp.row[2], vertex.position;
  147. 4: DP4 temp0.w, state.matrix.mvp.row[3], vertex.position;
  148. 5: MOV result.position, temp0;
  149. 6: END
  150. </pre>
  151. <p>
  152. Note that some shading language constructs (such as uniform and varying
  153. variables) aren't expressible in ARB or NV-style programs.
  154. Therefore, the resulting output is not always legal by definition of
  155. those program languages.
  156. </p>
  157. <p>
  158. Also note that this compiler driver is still under development.
  159. Over time, the correctness of the GPU programs, with respect to the ARB
  160. and NV languagues, should improve.
  161. </p>
  162. <a name="implementation">
  163. <h2>Compiler Implementation</h2>
  164. <p>
  165. The source code for Mesa's shading language compiler is in the
  166. <code>src/mesa/shader/slang/</code> directory.
  167. </p>
  168. <p>
  169. The compiler follows a fairly standard design and basically works as follows:
  170. </p>
  171. <ul>
  172. <li>The input string is tokenized (see grammar.c) and parsed
  173. (see slang_compiler_*.c) to produce an Abstract Syntax Tree (AST).
  174. The nodes in this tree are slang_operation structures
  175. (see slang_compile_operation.h).
  176. The nodes are decorated with symbol table, scoping and datatype information.
  177. <li>The AST is converted into an Intermediate representation (IR) tree
  178. (see the slang_codegen.c file).
  179. The IR nodes represent basic GPU instructions, like add, dot product,
  180. move, etc.
  181. The IR tree is mostly a binary tree, but a few nodes have three or four
  182. children.
  183. In principle, the IR tree could be executed by doing an in-order traversal.
  184. <li>The IR tree is traversed in-order to emit code (see slang_emit.c).
  185. This is also when registers are allocated to store variables and temps.
  186. <li>In the future, a pattern-matching code generator-generator may be
  187. used for code generation.
  188. Programs such as L-BURG (Bottom-Up Rewrite Generator) and Twig look for
  189. patterns in IR trees, compute weights for subtrees and use the weights
  190. to select the best instructions to represent the sub-tree.
  191. <li>The emitted GPU instructions (see prog_instruction.h) are stored in a
  192. gl_program object (see mtypes.h).
  193. <li>When a fragment shader and vertex shader are linked (see slang_link.c)
  194. the varying vars are matched up, uniforms are merged, and vertex
  195. attributes are resolved (rewriting instructions as needed).
  196. </ul>
  197. <p>
  198. The final vertex and fragment programs may be interpreted in software
  199. (see prog_execute.c) or translated into a specific hardware architecture
  200. (see drivers/dri/i915/i915_fragprog.c for example).
  201. </p>
  202. <h3>Code Generation Options</h3>
  203. <p>
  204. Internally, there are several options that control the compiler's code
  205. generation and instruction selection.
  206. These options are seen in the gl_shader_state struct and may be set
  207. by the device driver to indicate its preferences:
  208. <pre>
  209. struct gl_shader_state
  210. {
  211. ...
  212. /** Driver-selectable options: */
  213. GLboolean EmitHighLevelInstructions;
  214. GLboolean EmitCondCodes;
  215. GLboolean EmitComments;
  216. };
  217. </pre>
  218. <ul>
  219. <li>EmitHighLevelInstructions
  220. <br>
  221. This option controls instruction selection for loops and conditionals.
  222. If the option is set high-level IF/ELSE/ENDIF, LOOP/ENDLOOP, CONT/BRK
  223. instructions will be emitted.
  224. Otherwise, those constructs will be implemented with BRA instructions.
  225. </li>
  226. <li>EmitCondCodes
  227. <br>
  228. If set, condition codes (ala GL_NV_fragment_program) will be used for
  229. branching and looping.
  230. Otherwise, ordinary registers will be used (the IF instruction will
  231. examine the first operand's X component and do the if-part if non-zero).
  232. This option is only relevant if EmitHighLevelInstructions is set.
  233. </li>
  234. <li>EmitComments
  235. <br>
  236. If set, instructions will be annoted with comments to help with debugging.
  237. Extra NOP instructions will also be inserted.
  238. </br>
  239. </ul>
  240. </BODY>
  241. </HTML>