<feed xmlns='http://www.w3.org/2005/Atom'>
<title>slang.git/tests/diagnostics/global-uniform.slang, branch master</title>
<subtitle>Making it easier to work with shaders</subtitle>
<id>https://git.yummers.dev/slang.git/atom?h=master</id>
<link rel='self' href='https://git.yummers.dev/slang.git/atom?h=master'/>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/'/>
<updated>2025-07-16T06:36:18+00:00</updated>
<entry>
<title>Diagnose on initExpr of a global `const` decl (#7711)</title>
<updated>2025-07-16T06:36:18+00:00</updated>
<author>
<name>Yong He</name>
<email>yonghe@outlook.com</email>
</author>
<published>2025-07-16T06:36:18+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=56e91e91e425b77a9d1512cca3d26ce446781935'/>
<id>urn:sha1:56e91e91e425b77a9d1512cca3d26ce446781935</id>
<content type='text'>
* Add diagnostic for const/uniform global variables with initializers

Global const and uniform variables without the static modifier are
treated as uniform parameters in HLSL, which cannot have default values.
This change adds error 31224 to catch this case early and provide clear
guidance to users.

Fixes #7701

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Yong He &lt;csyonghe@users.noreply.github.com&gt;

* Add test case for global const/uniform variable diagnostic

Add test for diagnostic 31224 which catches global const and uniform
variables with initializers that aren't declared static. The test
verifies both error cases and valid cases to ensure the diagnostic
works correctly.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Yong He &lt;csyonghe@users.noreply.github.com&gt;

* Fix failing tests due to new global const diagnostic

Add 'static' keyword to global const variables with initializers
to resolve diagnostic 31224: "global const variable with initializer
must be declared static"

Co-authored-by: Jay Kwak &lt;jkwak-work@users.noreply.github.com&gt;

* Allow specialization constants with initializers without static

- Modified diagnostic logic in slang-check-decl.cpp to allow specialization
  constants (SpecializationConstantAttribute and VkConstantIdAttribute) to
  have initializers without requiring the static keyword
- Updated 8 SPIRV test files to remove unnecessary static keywords from
  specialization constant declarations
- Enhanced diagnostic test case to include specialization constant examples
- All tests passing with the new behavior

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Yong He &lt;csyonghe@users.noreply.github.com&gt;

* format code (#7765)

Co-authored-by: slangbot &lt;186143334+slangbot@users.noreply.github.com&gt;

* Remove static keyword from specialization constants and add comprehensive test cases

- Remove static from specialization constants in test files as they are semantically meaningless
- Update tests to use existing diagnostic 31219 for static specialization constant errors
- Add comprehensive test cases that verify the error occurs when static is used with specialization constants
- Fixes suggested in PR review to make static specialization constants an error

Co-authored-by: ArielG-NV &lt;ArielG-NV@users.noreply.github.com&gt;

* Add error for static specialization constants

Static specialization constants are semantically meaningless and should
produce an error. This change adds logic to trigger diagnostic 31219
when both 'static' and specialization constant attributes are present.

The existing diagnostic 31219 'push or specialization constants cannot
be static' is now correctly triggered for:
- [SpecializationConstant] static const variables
- [vk::constant_id] static const variables

Co-authored-by: ArielG-NV &lt;ArielG-NV@users.noreply.github.com&gt;

* Update source/slang/slang-check-decl.cpp

* Update tests/glsl/compute-shader-layout-id.slang

* Update tests/glsl/compute-shader-layout-id.slang

* Fix test case Claude broke

* Update global-uniform.slang

* Update global-uniform.slang.expected

---------

Co-authored-by: github-actions[bot] &lt;41898282+github-actions[bot]@users.noreply.github.com&gt;
Co-authored-by: Yong He &lt;csyonghe@users.noreply.github.com&gt;
Co-authored-by: ArielG-NV &lt;159081215+ArielG-NV@users.noreply.github.com&gt;
Co-authored-by: Jay Kwak &lt;jkwak-work@users.noreply.github.com&gt;
Co-authored-by: slangbot &lt;ellieh+slangbot@nvidia.com&gt;
Co-authored-by: slangbot &lt;186143334+slangbot@users.noreply.github.com&gt;
Co-authored-by: ArielG-NV &lt;ArielG-NV@users.noreply.github.com&gt;</content>
</entry>
<entry>
<title>Add support for global uniform shader parameters (#1433)</title>
<updated>2020-07-08T20:52:40+00:00</updated>
<author>
<name>Tim Foley</name>
<email>tfoleyNV@users.noreply.github.com</email>
</author>
<published>2020-07-08T20:52:40+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=9590948e075b81fc2bf2d5ce84e9253861080048'/>
<id>urn:sha1:9590948e075b81fc2bf2d5ce84e9253861080048</id>
<content type='text'>
* Adding support for global uniform shader parameters

This change adds support for Slang programmers to declare shader parameters of "ordinary" types at global scope:

```hlsl
uniform float gScaleFactor;

void main() { ... *= gScaleFactor; ... }
```

The generated HLSL/GLSL/DXIL/SPIR-V output will be something along the lines of:

```hlsl
struct GlobalParams
{
    float gScaleFactor;
}

cbuffer globalParams
{
    GlobalParams globalParams;
}

void main() { ... *= globalParams.gScaleFactor; ... }
```

The binding information used for the implicit `globalParams` constant buffer will be determined by the existing implicit parameter binding logic (which already had support for this kind of transformation).

The reason this change is being pursued right now is because it is one step toward removing the implicit `KernelContext` type that is used to wrap the generated code for our CPU and CUDA C++ targets. Handling global-scope parameters of ordinary type requires an IR pass that synthesizes the `GlobalParams` structure type above, and that step ends up removing the need for the similar `UniformState` structure that was being used in the CPU/CUDA emit logic.

A more detailed guide to the changes included follows:

* The diagnostic for a global-scope variable that is implicitly a shader parameter was kept, but changed to a warning. Users can opt out of the warning by decorating their parameter as a `uniform` (since that keyword is already being used to mark entry-point parameters that should be treated as uniform shader parameters).

* To simplify the task of finding the global shader parameters, the `CLikeSourceEmitter` type has been given an `m_irModule` member. The previous emit logic for `UniformState` was having to do a roundabout solution involving the `EmitAction`s to deal with not having direct access to the module.

* Removed a few dead declarations in the emit logic (related to a much earlier point where emit was based on the AST instead of the IR).

* Made the computation of type names in C++ emit take into account `ConstantBuffer&lt;T&gt;` and `ParameterBlock&lt;T&gt;`. As far as I can tell, these were being handled with some special-case hacks in the emit logic instead of being supported more fundamentally. It might actually be good to pass these through as `ConstantBuffer&lt;T&gt;` and `ParameterBlock&lt;T&gt;` in the C++ output, and allow the prelude to customize their translation (defaulting to defining them as `T*`).

* Removed the special-case C++ emit logic for references to global shader parameters. There are now at most two global shader parameters to deal with, and the default emit logic (referring to them by name) does the Right Thing.

* Changed the handling of entry points for C++ (both CPU and CUDA) so that it handles the bundled-up shader paameters for the global and entry-point scopes the same way. The main complication here is OptiX, where parameter data is passed very differently than it is for CUDA compute kernels.

* Reverted changes to `ir-entry-point-uniforms` that had made its logic depend on the compilation target. The parameter binding logic was already responsible for deciding if a given target needed to wrap up its entry-point parameters in a constant buffer, and the IR pass was respecting that layout information. The current workaround had been removing the `ConstantBuffer&lt;T&gt;` indirection from this IR pass for CPU/CUDA, but then reintroducing the same indirection later on in the emit step.

* Added an explicit IR pass with the task of collecting global-scope parameters of uniform/ordinary type and packaging them up into a `struct`, and then optionally packaging that `struct` up in a constant buffer. This pass bases its decisions on the IR layout information that was already computed, so it should match whatever policy choices were made at the layout level.

* Changed the "key" operand on IR `struct` layout information to not assume an `IRStructKey`. The problem here is that the global scope gets a `StructTypeLayout` to represent its members, and this is convenient (rather than having to always special-case logic that handles the global scope), but the "fields" of that struct are global variables which do not have `IRStructKey`s associated with them. The simplest solution is to use the variables themselves as the keys, which required removing the assumption in the IR encoding.

* Updated the IR layout process to compute a layout for the global scope of an entire program, and to attach that to the `IRModule` via a decoration. Updated the IR linking process to carry through that decoration to the linked output. This is necessary so that the IR pass that transforms global parameters can access the global-scope layout information.

An important concern with this approach is that the contents and layout of the monolithic `GlobalParams` structure depends on the exact set of modules that were linked (and the order in which they were specified, in some cases). This isn't really a new thing with this change, but it becomes more important as we start to think of how to generalize things to better support separate compilation and linking.

There are changes that can (and should) be made to the way that IR layouts are computed for programs (e.g., so that we compute layout per-module and then combine them rather than as a whole-program step). In this case, the problem of forming the combined/linked global layout can be moved down the IR level and not be reliant on AST-level information.

Just changing the way layout and linking interact would not change the fundamental problem that global shader parameters as they currently exist in Slang/HLSL/GLSL are not readily compatible with true separate compilation. We either need to find a solution strategy that we can apply to allow existing shaders to work with separate compilation *or* we need to incrementally work toward removing support for global-scope shader parameters in favor of explicit entry-point parameters in all cases.

* fixup: missing files

* fixup: comment the new code</content>
</entry>
<entry>
<title>Feature/test improvements (#934)</title>
<updated>2019-04-02T13:22:13+00:00</updated>
<author>
<name>jsmall-nvidia</name>
<email>jsmall@nvidia.com</email>
</author>
<published>2019-04-02T13:22:13+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=2896aa39a529a00673a30ef4a9c3ebe76f401fea'/>
<id>urn:sha1:2896aa39a529a00673a30ef4a9c3ebe76f401fea</id>
<content type='text'>
* First pass extract the test information by 'running tests'.

* * Checking renderer availablilty
* Using TestInfo to determine which tests are run and synthesized
* Display if test is synthesized and what render api it's targetting

* * Improved comments
* Removed some dead code

* Display ignored tests.

* TestInfo -&gt; TestRequirements

* * Added DIAGNOSTIC_TEST type - test always runs (ie has no requirements).
* Made diagnostic tests use DIAGNOSTIC_TEST
* TestInfo -&gt; TestRequirements
* TestDetails holds TestRequirements and TestOptions

* Fix debug typo.
</content>
</entry>
<entry>
<title>Feature/hash for source identity (#786)</title>
<updated>2019-01-17T22:50:48+00:00</updated>
<author>
<name>jsmall-nvidia</name>
<email>jsmall@nvidia.com</email>
</author>
<published>2019-01-17T22:50:48+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=3c7e1be0098f963225afd0ebe83340a991392892'/>
<id>urn:sha1:3c7e1be0098f963225afd0ebe83340a991392892</id>
<content type='text'>
* * Added COMMAND_LINE_SIMPLE test type
* Made how spawning works controllable by paramter/type SpawnType
* Made break-outside-loop and global-uniform run command line slangc

* calcRelativePath -&gt; calcCombinedPath

* Add 64 bit version of GetHash.

* Add support for Hash based mode for CacheFileSystem.
</content>
</entry>
<entry>
<title>Add an error for global uniform parameter declarations (#773)</title>
<updated>2019-01-14T22:27:44+00:00</updated>
<author>
<name>Tim Foley</name>
<email>tfoleyNV@users.noreply.github.com</email>
</author>
<published>2019-01-14T22:27:44+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=5b0c5076326b98d0e9ee0f7bddda7f619676707c'/>
<id>urn:sha1:5b0c5076326b98d0e9ee0f7bddda7f619676707c</id>
<content type='text'>
A global uniform parameter in HLSL might canonically be defined like this:

```hlsl
uniform float gSomeParameter;
```

The fxc and dxc compilers automatically collect all such parameters into a synthesized constant buffer, along the lines of:

```hlsl
cbuffer $Globals
{
    float gSomeParameter;
}
```

Slang currently supports parsing and semantic checking of declarations like the above, and computes shader parameter layout/binding information that is appropriate for a constant buffer like `$Globals` above, but it does not include the support to emit HLSL or GLSL code that matches that layout, so that use of global uniforms in Slang is silently unsupported.

Making this problem worse, the HLSL language is quite lax, and will parse the following as shader parameters as well:

```hlsl
int gCounter = 0;
const float kScaleFactor = 2.0f;
```

Each of those declarations introduces a global shader parameter, and then provides a default value for it via the initializer. These declarations do *not* introduce an ordinary global variable or constant as might be expected.

(For anybody who wants to know, `static` is required to introduce a "real" global variable (although it will be a *thread-local* global in practice), while `static const` is required to introduce a global constant)

I was not too worried about users trying to use global-scope uniforms and failing (since that has fallen out of common HLSL/GLSL practice), but the possibility that users might try to declare global variables/constants and get shader parameters by mistake creates more of a risk so that this hole is worth plugging.

The right long-term fix is of course to support the intended semantics of global-scope uniforms, but that feature needs to be prioritized against other requests.

A few of the Slang tests were unwittingly relying on this functionality, including some compute tests that seemingly got away with it based on the DXBC generated from the HLSL output by Slang just happening to match the layout they expected. These tests have all been tweaked to use explicit `cbuffer`s or `ParameterBlock`s instead.</content>
</entry>
</feed>
