Skip to content

Commit dda3295

Browse files
authored
Add control flow attributes section to GLSL migration guide (#126)
* Add control flow attributes section to GLSL migration guide Add details on the mapping between GLSL and Slang for some of the attributes from GL_EXT_control_flow_attributes. * Documentation clarifications/updates * Mention [ForceUnroll] * Clarify attributes apply to the driver compiler
1 parent ba1a9c3 commit dda3295

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

docs/coming-from-glsl.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,63 @@ When porting GLSL shaders to Slang, most common mathematical functions (sin, cos
313313
| `m1 * m2` (matrix multiply) | `mul(m1, m2)` | Matrix multiplication |
314314
| `v * m` (row vector) | `mul(m, v)` (column vector) | Vector-matrix multiplication |
315315

316+
## Control Flow Attributes
317+
318+
Slang provides control flow hint attributes similar to those in the GL_EXT_control_flow_attributes GLSL extension. These attributes provide optimization hints for loops and conditional statements to the driver compiler. Note that these are hints only and may be ignored by the driver compiler.
319+
320+
### Attribute Mapping
321+
322+
| GLSL Attribute | Slang Attribute | Applies To | Description |
323+
|----------------|-----------------|------------|-------------|
324+
| `[[branch]]` | `[branch]` | if/switch | Hint to keep selection control flow (use actual branching) |
325+
| `[[flatten]]` | `[flatten]` | if/switch | Hint to remove selection control flow (evaluate both branches) |
326+
| `[[dont_flatten]]` | `[branch]` | if/switch | Same as branch |
327+
| `[[loop]]` | `[loop]` | loops | Hint to keep loop control flow (against unrolling) |
328+
| `[[unroll]]` | `[unroll]` | loops | Hint to remove or reduce loop control flow (unroll fully) |
329+
| `[[dont_unroll]]` | `[loop]` | loops | Same as loop |
330+
331+
Note: Slang also supports [unroll(N)] for partial unrolling hints.
332+
333+
Note: Slang supports the [ForceUnroll] attribute which unrolls the loop before emitting SPIR-V or other target code.
334+
335+
### Usage Example
336+
337+
**GLSL:**
338+
339+
```glsl
340+
#extension GL_EXT_control_flow_attributes : enable
341+
342+
[[unroll]]
343+
for(int i = 0; i < 8; i++) {
344+
// loop body
345+
}
346+
347+
[[flatten]]
348+
if(condition) {
349+
// if body
350+
} else {
351+
// else body
352+
}
353+
```
354+
355+
**Slang:**
356+
357+
```hlsl
358+
[unroll]
359+
for(int i = 0; i < 8; i++) {
360+
// loop body
361+
}
362+
363+
[flatten]
364+
if(condition) {
365+
// if body
366+
} else {
367+
// else body
368+
}
369+
```
370+
371+
These attributes are applied directly before the control flow statements in both languages.
372+
316373
## Resource Handling
317374

318375
This section covers how to convert GLSL resource declarations to Slang, including different buffer types, textures, and specialized resources.

0 commit comments

Comments
 (0)