4 Mesa is over 20 years old and the coding style has evolved over time.
5 Some old parts use a style that's a bit out of date. Different sections
6 of mesa can use different coding style as set in the local EditorConfig
7 (.editorconfig) and/or Emacs (.dir-locals.el) file. Alternatively the
8 following is applicable. If the guidelines below don't cover something,
9 try following the format of existing, neighboring code.
11 Basic formatting guidelines
13 - 3-space indentation, no tabs.
14 - Limit lines to 78 or fewer characters. The idea is to prevent line
15 wrapping in 80-column editors and terminals. There are exceptions,
16 such as if you're defining a large, static table of information.
17 - Opening braces go on the same line as the if/for/while statement. For
28 - Put a space before/after operators. For example, ``a = b + c;`` and
30 - This GNU indent command generally does the right thing for
35 indent -br -i3 -npcs --no-tabs infile.c -o outfile.c
37 - Use comments wherever you think it would be helpful for other
38 developers. Several specific cases and style examples follow. Note
39 that we roughly follow `Doxygen <http://www.doxygen.nl>`__
46 /* null-out pointer to prevent dangling reference below */
53 bufferObj = NULL; /* prevent dangling reference below */
59 /* If this is a new buffer object id, or one which was generated but
60 * never used before, allocate a buffer object now.
63 We try to quote the OpenGL specification where prudent:
67 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
69 * "An INVALID_OPERATION error is generated for any of the following
72 * * <length> is zero."
74 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
75 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
79 Function comment example:
84 * Create and initialize a new buffer object. Called via the
85 * ctx->Driver.CreateObject() driver callback function.
86 * \param name integer name of the object
87 * \param type one of GL_FOO, GL_BAR, etc.
88 * \return pointer to new object or NULL if error
91 _mesa_create_object(GLuint name, GLenum type)
96 - Put the function return type and qualifiers on one line and the
97 function name and parameters on the next, as seen above. This makes
98 it easy to use ``grep ^function_name dir/*`` to find function
99 definitions. Also, the opening brace goes on the next line by itself
101 - Function names follow various conventions depending on the type of
106 glFooBar() - a public GL entry point (in glapi_dispatch.c)
107 _mesa_FooBar() - the internal immediate mode function
108 save_FooBar() - retained mode (display list) function in dlist.c
109 foo_bar() - a static (private) function
110 _mesa_foo_bar() - an internal non-static Mesa function
112 - Constants, macros and enum names are ``ALL_UPPERCASE``, with \_
114 - Mesa usually uses camel case for local variables (Ex:
115 ``localVarname``) while gallium typically uses underscores (Ex:
117 - Global variables are almost never used because Mesa should be
119 - Booleans. Places that are not directly visible to the GL API should
120 prefer the use of ``bool``, ``true``, and ``false`` over
121 ``GLboolean``, ``GL_TRUE``, and ``GL_FALSE``. In C code, this may
122 mean that ``#include <stdbool.h>`` needs to be added. The
123 ``try_emit_*`` methods in ``src/mesa/program/ir_to_mesa.cpp`` and
124 ``src/mesa/state_tracker/st_glsl_to_tgsi.cpp`` can serve as examples.