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

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