17ec681f3SmrgCoding Style 27ec681f3Smrg============ 37ec681f3Smrg 47ec681f3SmrgMesa is over 20 years old and the coding style has evolved over time. 57ec681f3SmrgSome old parts use a style that's a bit out of date. Different sections 67ec681f3Smrgof mesa can use different coding style as set in the local EditorConfig 77ec681f3Smrg(.editorconfig) and/or Emacs (.dir-locals.el) file. Alternatively the 87ec681f3Smrgfollowing is applicable. If the guidelines below don't cover something, 97ec681f3Smrgtry following the format of existing, neighboring code. 107ec681f3Smrg 117ec681f3SmrgBasic formatting guidelines 127ec681f3Smrg 137ec681f3Smrg- 3-space indentation, no tabs. 147ec681f3Smrg- Limit lines to 78 or fewer characters. The idea is to prevent line 157ec681f3Smrg wrapping in 80-column editors and terminals. There are exceptions, 167ec681f3Smrg such as if you're defining a large, static table of information. 177ec681f3Smrg- Opening braces go on the same line as the if/for/while statement. For 187ec681f3Smrg example: 197ec681f3Smrg 207ec681f3Smrg .. code-block:: c 217ec681f3Smrg 227ec681f3Smrg if (condition) { 237ec681f3Smrg foo; 247ec681f3Smrg } else { 257ec681f3Smrg bar; 267ec681f3Smrg } 277ec681f3Smrg 287ec681f3Smrg- Put a space before/after operators. For example, ``a = b + c;`` and 297ec681f3Smrg not ``a=b+c;`` 307ec681f3Smrg- This GNU indent command generally does the right thing for 317ec681f3Smrg formatting: 327ec681f3Smrg 337ec681f3Smrg .. code-block:: console 347ec681f3Smrg 357ec681f3Smrg indent -br -i3 -npcs --no-tabs infile.c -o outfile.c 367ec681f3Smrg 377ec681f3Smrg- Use comments wherever you think it would be helpful for other 387ec681f3Smrg developers. Several specific cases and style examples follow. Note 397ec681f3Smrg that we roughly follow `Doxygen <http://www.doxygen.nl>`__ 407ec681f3Smrg conventions. 417ec681f3Smrg 427ec681f3Smrg Single-line comments: 437ec681f3Smrg 447ec681f3Smrg .. code-block:: c 457ec681f3Smrg 467ec681f3Smrg /* null-out pointer to prevent dangling reference below */ 477ec681f3Smrg bufferObj = NULL; 487ec681f3Smrg 497ec681f3Smrg Or, 507ec681f3Smrg 517ec681f3Smrg .. code-block:: c 527ec681f3Smrg 537ec681f3Smrg bufferObj = NULL; /* prevent dangling reference below */ 547ec681f3Smrg 557ec681f3Smrg Multi-line comment: 567ec681f3Smrg 577ec681f3Smrg .. code-block:: c 587ec681f3Smrg 597ec681f3Smrg /* If this is a new buffer object id, or one which was generated but 607ec681f3Smrg * never used before, allocate a buffer object now. 617ec681f3Smrg */ 627ec681f3Smrg 637ec681f3Smrg We try to quote the OpenGL specification where prudent: 647ec681f3Smrg 657ec681f3Smrg .. code-block:: c 667ec681f3Smrg 677ec681f3Smrg /* Page 38 of the PDF of the OpenGL ES 3.0 spec says: 687ec681f3Smrg * 697ec681f3Smrg * "An INVALID_OPERATION error is generated for any of the following 707ec681f3Smrg * conditions: 717ec681f3Smrg * 727ec681f3Smrg * * <length> is zero." 737ec681f3Smrg * 747ec681f3Smrg * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec 757ec681f3Smrg * (30.10.2014) also says this, so it's no longer allowed for desktop GL, 767ec681f3Smrg * either. 777ec681f3Smrg */ 787ec681f3Smrg 797ec681f3Smrg Function comment example: 807ec681f3Smrg 817ec681f3Smrg .. code-block:: c 827ec681f3Smrg 837ec681f3Smrg /** 847ec681f3Smrg * Create and initialize a new buffer object. Called via the 857ec681f3Smrg * ctx->Driver.CreateObject() driver callback function. 867ec681f3Smrg * \param name integer name of the object 877ec681f3Smrg * \param type one of GL_FOO, GL_BAR, etc. 887ec681f3Smrg * \return pointer to new object or NULL if error 897ec681f3Smrg */ 907ec681f3Smrg struct gl_object * 917ec681f3Smrg _mesa_create_object(GLuint name, GLenum type) 927ec681f3Smrg { 937ec681f3Smrg /* function body */ 947ec681f3Smrg } 957ec681f3Smrg 967ec681f3Smrg- Put the function return type and qualifiers on one line and the 977ec681f3Smrg function name and parameters on the next, as seen above. This makes 987ec681f3Smrg it easy to use ``grep ^function_name dir/*`` to find function 997ec681f3Smrg definitions. Also, the opening brace goes on the next line by itself 1007ec681f3Smrg (see above.) 1017ec681f3Smrg- Function names follow various conventions depending on the type of 1027ec681f3Smrg function: 1037ec681f3Smrg 1047ec681f3Smrg +---------------------+------------------------------------------+ 1057ec681f3Smrg | Convention | Explanation | 1067ec681f3Smrg +=====================+==========================================+ 1077ec681f3Smrg | ``glFooBar()`` | a public GL entry point (in | 1087ec681f3Smrg | | :file:`glapi_dispatch.c`) | 1097ec681f3Smrg +---------------------+------------------------------------------+ 1107ec681f3Smrg | ``_mesa_FooBar()`` | the internal immediate mode function | 1117ec681f3Smrg +---------------------+------------------------------------------+ 1127ec681f3Smrg | ``save_FooBar()`` | retained mode (display list) function in | 1137ec681f3Smrg | | :file:`dlist.c` | 1147ec681f3Smrg +---------------------+------------------------------------------+ 1157ec681f3Smrg | ``foo_bar()`` | a static (private) function | 1167ec681f3Smrg +---------------------+------------------------------------------+ 1177ec681f3Smrg | ``_mesa_foo_bar()`` | an internal non-static Mesa function | 1187ec681f3Smrg +---------------------+------------------------------------------+ 1197ec681f3Smrg 1207ec681f3Smrg- Constants, macros and enum names are ``ALL_UPPERCASE``, with \_ 1217ec681f3Smrg between words. 1227ec681f3Smrg- Mesa usually uses camel case for local variables (Ex: 1237ec681f3Smrg ``localVarname``) while Gallium typically uses underscores (Ex: 1247ec681f3Smrg ``local_var_name``). 1257ec681f3Smrg- Global variables are almost never used because Mesa should be 1267ec681f3Smrg thread-safe. 1277ec681f3Smrg- Booleans. Places that are not directly visible to the GL API should 1287ec681f3Smrg prefer the use of ``bool``, ``true``, and ``false`` over 1297ec681f3Smrg ``GLboolean``, ``GL_TRUE``, and ``GL_FALSE``. In C code, this may 1307ec681f3Smrg mean that ``#include <stdbool.h>`` needs to be added. The 1317ec681f3Smrg ``try_emit_*`` methods in ``src/mesa/program/ir_to_mesa.cpp`` and 1327ec681f3Smrg ``src/mesa/state_tracker/st_glsl_to_tgsi.cpp`` can serve as examples. 133