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 8.4KB

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