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