|
7 | 7 | status: Under Consideration |
8 | 8 | --- |
9 | 9 |
|
10 | | - |
11 | | - |
12 | 10 | * Planned Version: 202x |
13 | 11 | * Issues: [DXC #4514](https://github.com/microsoft/DirectXShaderCompiler/issues/4514) |
14 | 12 |
|
@@ -69,39 +67,62 @@ To simplify HLSL's language semantics and the compiler implementation a new |
69 | 67 | grammar formation is adopted for cbuffers: |
70 | 68 |
|
71 | 69 | ```latex |
72 | | -
|
73 | 70 | \begin{grammar} |
74 | | - \define{cbuffer-declaration-group}\br |
75 | | - \terminal{cbuffer} identifier \opt{resource-binding} \terminal{\{} |
76 | | - \opt{cbuffer-declaration-seq} \terminal {\}}\br |
77 | | -
|
78 | | - \define{cbuffer-declaration-seq}\br |
79 | | - cbuffer-declaration\br |
80 | | - cbuffer-declaration-seq cbuffer-declaration\br |
81 | | -
|
82 | 71 | \define{cbuffer-declaration}\br |
83 | | - variable-declaration\br |
84 | | - empty-declaration\br |
| 72 | + \terminal{cbuffer} name \opt{resource-binding} \terminal{\{} |
| 73 | + \opt{cbuffer-member-seq} \terminal {\}}\br |
| 74 | +
|
| 75 | + \define{cbuffer-member-seq}\br |
| 76 | + cbuffer-member-declaration\br |
| 77 | + cbuffer-member-seq cbuffer-member-declaration\br |
| 78 | +
|
| 79 | + \define{cbuffer-member-declaration}\br |
| 80 | + block-declaration\br |
| 81 | + function-definition\br |
| 82 | + template-declaration\br |
| 83 | + empty-declaration |
85 | 84 | \end{grammar} |
86 | 85 | ``` |
87 | 86 |  |
88 | 87 |
|
89 | | -This simplified grammar disallows members of `cbuffer` declarations that do not |
90 | | -have semantic meaning, and allows a simplification of `cbuffer` scoping rules. |
| 88 | +This simplified grammar restricts the declarations that are valid within a |
| 89 | +cbuffer to declarations that are broadly valid inside block scopes as well as |
| 90 | +function and template declarations. This allows a cbuffer to contain |
| 91 | +declarations of classes and data types as well as functions and variables. |
91 | 92 |
|
92 | 93 | A `cbuffer` may only be declared at translation unit or namespace scope. A |
93 | | -`cbuffer` may only contain non-static non-constexpr variable (or empty) |
94 | | -declarations. All declarations within a `cbuffer` declare names in the immediate |
95 | | -enclosing scope. |
| 94 | +`cbuffer` may not contain a namespace declaration and may only contain a subset |
| 95 | +of valid global declarations. |
| 96 | + |
| 97 | +## Detailed Design |
| 98 | + |
| 99 | +> The following text will be included in the [Decl.cbuffer] section in the |
| 100 | +> language specification. |
| 101 | +
|
| 102 | +A _cbuffer declaration_ is declared with the \texttt{cbuffer} keyword. The name |
| 103 | +of the cbuffer declaration does not declare a name, and cannot be referenced |
| 104 | +from within the translation unit, nor is it required to be unique. Each cbuffer |
| 105 | +declaration refers to a unique constant buffer resource. |
| 106 | + |
| 107 | +Declarations within a cbuffer declaration that declare names, declare their |
| 108 | +names in the scope containing the cbuffer declaration. The cbuffer declaration |
| 109 | +itself does not declare a declaration scope. A cbuffer declaration may not |
| 110 | +contain a _namespace-declaration_ or _cbuffer-declaration_. |
| 111 | + |
| 112 | +Variable declarations with program storage duration in the cbuffer declaration |
| 113 | +are called _shader constants_. Shader constants are implicitly `const` |
| 114 | +and cannot be modified in program code. |
96 | 115 |
|
97 | | -A `cbuffer` is a colection of constant values provided to a shader at runtime. |
98 | | -As such, it should only contain constant variable declarations backed by |
99 | | -read-only storage in device memory that persists across the life of the |
100 | | -dispatch. |
| 116 | +## Long-term Considerations |
101 | 117 |
|
102 | | -Notably a `cbuffer` cannot contain function declarations, type declarations |
103 | | -(classes, enumerations, typedefs), namespace declarations, constexpr or static |
104 | | -variable declarations, `cbuffer` declarations, or any other declaration type not |
105 | | -explicitly listed as allowed. |
| 118 | +During our discussion in the language design meeting additional restrictions to |
| 119 | +cbuffer declarations were considered. The general consensus was that additional |
| 120 | +restrictions are desirable and will remove possible programmer errors, however |
| 121 | +there were concerns about HLSL 202x changing too much at once. |
106 | 122 |
|
| 123 | +In a future language version we may consider further restricting cbuffers to |
| 124 | +allow only variable declarations of _program storage duration_. |
107 | 125 |
|
| 126 | +In implementing cbuffer support in Clang we should consider implementing |
| 127 | +a diagnostic for all variable declarations that are not of program storage |
| 128 | +duration to identify potential programmer errors. |
0 commit comments