codingstyle.html revision b8e80941
1b8e80941Smrg<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2b8e80941Smrg<html lang="en">
3b8e80941Smrg<head>
4b8e80941Smrg  <meta http-equiv="content-type" content="text/html; charset=utf-8">
5b8e80941Smrg  <title>Coding Style</title>
6b8e80941Smrg  <link rel="stylesheet" type="text/css" href="mesa.css">
7b8e80941Smrg</head>
8b8e80941Smrg<body>
9b8e80941Smrg
10b8e80941Smrg<div class="header">
11b8e80941Smrg  <h1>The Mesa 3D Graphics Library</h1>
12b8e80941Smrg</div>
13b8e80941Smrg
14b8e80941Smrg<iframe src="contents.html"></iframe>
15b8e80941Smrg<div class="content">
16b8e80941Smrg
17b8e80941Smrg<h1>Coding Style</h1>
18b8e80941Smrg
19b8e80941Smrg<p>
20b8e80941SmrgMesa is over 20 years old and the coding style has evolved over time.
21b8e80941SmrgSome old parts use a style that's a bit out of date.
22b8e80941Smrg
23b8e80941SmrgDifferent sections of mesa can use different coding style as set in the local
24b8e80941SmrgEditorConfig (.editorconfig) and/or Emacs (.dir-locals.el) file.
25b8e80941Smrg
26b8e80941SmrgAlternatively the following is applicable.
27b8e80941Smrg
28b8e80941SmrgIf the guidelines below don't cover something, try following the format of
29b8e80941Smrgexisting, neighboring code.
30b8e80941Smrg</p>
31b8e80941Smrg
32b8e80941Smrg<p>
33b8e80941SmrgBasic formatting guidelines
34b8e80941Smrg</p>
35b8e80941Smrg
36b8e80941Smrg<ul>
37b8e80941Smrg<li>3-space indentation, no tabs.
38b8e80941Smrg<li>Limit lines to 78 or fewer characters.  The idea is to prevent line
39b8e80941Smrgwrapping in 80-column editors and terminals.  There are exceptions, such
40b8e80941Smrgas if you're defining a large, static table of information.
41b8e80941Smrg<li>Opening braces go on the same line as the if/for/while statement.
42b8e80941SmrgFor example:
43b8e80941Smrg<pre>
44b8e80941Smrg   if (condition) {
45b8e80941Smrg      foo;
46b8e80941Smrg   } else {
47b8e80941Smrg      bar;
48b8e80941Smrg   }
49b8e80941Smrg</pre>
50b8e80941Smrg
51b8e80941Smrg<li>Put a space before/after operators.  For example, <tt>a = b + c;</tt>
52b8e80941Smrgand not <tt>a=b+c;</tt>
53b8e80941Smrg
54b8e80941Smrg<li>This GNU indent command generally does the right thing for formatting:
55b8e80941Smrg<pre>
56b8e80941Smrg   indent -br -i3 -npcs --no-tabs infile.c -o outfile.c
57b8e80941Smrg</pre>
58b8e80941Smrg
59b8e80941Smrg<li>Use comments wherever you think it would be helpful for other developers.
60b8e80941SmrgSeveral specific cases and style examples follow.  Note that we roughly
61b8e80941Smrgfollow <a href="https://www.stack.nl/~dimitri/doxygen/">Doxygen</a> conventions.
62b8e80941Smrg<br>
63b8e80941Smrg<br>
64b8e80941SmrgSingle-line comments:
65b8e80941Smrg<pre>
66b8e80941Smrg   /* null-out pointer to prevent dangling reference below */
67b8e80941Smrg   bufferObj = NULL;
68b8e80941Smrg</pre>
69b8e80941SmrgOr,
70b8e80941Smrg<pre>
71b8e80941Smrg   bufferObj = NULL;  /* prevent dangling reference below */
72b8e80941Smrg</pre>
73b8e80941SmrgMulti-line comment:
74b8e80941Smrg<pre>
75b8e80941Smrg   /* If this is a new buffer object id, or one which was generated but
76b8e80941Smrg    * never used before, allocate a buffer object now.
77b8e80941Smrg    */
78b8e80941Smrg</pre>
79b8e80941SmrgWe try to quote the OpenGL specification where prudent:
80b8e80941Smrg<pre>
81b8e80941Smrg   /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
82b8e80941Smrg    *
83b8e80941Smrg    *     "An INVALID_OPERATION error is generated for any of the following
84b8e80941Smrg    *     conditions:
85b8e80941Smrg    *
86b8e80941Smrg    *     * &lt;length&gt; is zero."
87b8e80941Smrg    *
88b8e80941Smrg    * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
89b8e80941Smrg    * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
90b8e80941Smrg    * either.
91b8e80941Smrg    */
92b8e80941Smrg</pre>
93b8e80941SmrgFunction comment example:
94b8e80941Smrg<pre>
95b8e80941Smrg   /**
96b8e80941Smrg    * Create and initialize a new buffer object.  Called via the
97b8e80941Smrg    * ctx-&gt;Driver.CreateObject() driver callback function.
98b8e80941Smrg    * \param  name  integer name of the object
99b8e80941Smrg    * \param  type  one of GL_FOO, GL_BAR, etc.
100b8e80941Smrg    * \return  pointer to new object or NULL if error
101b8e80941Smrg    */
102b8e80941Smrg   struct gl_object *
103b8e80941Smrg   _mesa_create_object(GLuint name, GLenum type)
104b8e80941Smrg   {
105b8e80941Smrg      /* function body */
106b8e80941Smrg   }
107b8e80941Smrg</pre>
108b8e80941Smrg
109b8e80941Smrg<li>Put the function return type and qualifiers on one line and the function
110b8e80941Smrgname and parameters on the next, as seen above.  This makes it easy to use
111b8e80941Smrg<code>grep ^function_name dir/*</code> to find function definitions.  Also,
112b8e80941Smrgthe opening brace goes on the next line by itself (see above.)
113b8e80941Smrg
114b8e80941Smrg<li>Function names follow various conventions depending on the type of function:
115b8e80941Smrg<pre>
116b8e80941Smrg   glFooBar()       - a public GL entry point (in glapi_dispatch.c)
117b8e80941Smrg   _mesa_FooBar()   - the internal immediate mode function
118b8e80941Smrg   save_FooBar()    - retained mode (display list) function in dlist.c
119b8e80941Smrg   foo_bar()        - a static (private) function
120b8e80941Smrg   _mesa_foo_bar()  - an internal non-static Mesa function
121b8e80941Smrg</pre>
122b8e80941Smrg
123b8e80941Smrg<li>Constants, macros and enum names are ALL_UPPERCASE, with _ between
124b8e80941Smrgwords.
125b8e80941Smrg<li>Mesa usually uses camel case for local variables (Ex: "localVarname")
126b8e80941Smrgwhile gallium typically uses underscores (Ex: "local_var_name").
127b8e80941Smrg<li>Global variables are almost never used because Mesa should be thread-safe.
128b8e80941Smrg
129b8e80941Smrg<li>Booleans.  Places that are not directly visible to the GL API
130b8e80941Smrgshould prefer the use of <tt>bool</tt>, <tt>true</tt>, and
131b8e80941Smrg<tt>false</tt> over <tt>GLboolean</tt>, <tt>GL_TRUE</tt>, and
132b8e80941Smrg<tt>GL_FALSE</tt>.  In C code, this may mean that
133b8e80941Smrg<tt>#include &lt;stdbool.h&gt;</tt> needs to be added.  The
134b8e80941Smrg<tt>try_emit_</tt>* methods in src/mesa/program/ir_to_mesa.cpp and
135b8e80941Smrgsrc/mesa/state_tracker/st_glsl_to_tgsi.cpp can serve as examples.
136b8e80941Smrg
137b8e80941Smrg</ul>
138b8e80941Smrg
139b8e80941Smrg</div>
140b8e80941Smrg</body>
141b8e80941Smrg</html>
142