<feed xmlns='http://www.w3.org/2005/Atom'>
<title>slang.git/tests/cross-compile/vk-texture-indexing.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>2023-12-16T20:48:27+00:00</updated>
<entry>
<title>Fix nonuniform decoration on direct-to-spirv backend path. (#3338) (#3417)</title>
<updated>2023-12-16T20:48:27+00:00</updated>
<author>
<name>Pankaj Mistry</name>
<email>63069047+pmistryNV@users.noreply.github.com</email>
</author>
<published>2023-12-16T20:48:27+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=b6da04424aff71ddba9629c94401a9a897b152a0'/>
<id>urn:sha1:b6da04424aff71ddba9629c94401a9a897b152a0</id>
<content type='text'>
</content>
</entry>
<entry>
<title>Unify stdlib `Texture` types into one generic type. (#3327)</title>
<updated>2023-11-16T22:32:33+00:00</updated>
<author>
<name>Yong He</name>
<email>yonghe@outlook.com</email>
</author>
<published>2023-11-16T22:32:33+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=4c78efd0c34442866f20e9d00bbb6908115c9a01'/>
<id>urn:sha1:4c78efd0c34442866f20e9d00bbb6908115c9a01</id>
<content type='text'>
* Unify Texture types in stdlib into 1 generic type.

* Fixes.

* Fix.

* Fixes.

* Fix reflection.

* Fix binding reflection.

* Add gather intrinsics.

* Fix gather intrinsics.

* Fix texture type toText.

* Fix intrinsic.

* fix cuda intrinsic.

* Fix project files.

* cleanup.

* Fix.

* Fix.

* Fix sampler feedback test.

* Fix getDimension intrinsics.

* Fix spirv sample image intrinsics.

* Fix test.

* Fix GLSL intrinsic.

* Cleanup.

---------

Co-authored-by: Yong He &lt;yhe@nvidia.com&gt;</content>
</entry>
<entry>
<title>Fix handling of arrays of resources in type legalization (#896)</title>
<updated>2019-03-12T17:45:39+00:00</updated>
<author>
<name>Tim Foley</name>
<email>tfoleyNV@users.noreply.github.com</email>
</author>
<published>2019-03-12T17:45:39+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=9722990745f4e91ab1bf9fb682c72e173cd123b4'/>
<id>urn:sha1:9722990745f4e91ab1bf9fb682c72e173cd123b4</id>
<content type='text'>
Before type legalization we might have code like: (using pseudo-Slang-IR):

    struct P { ... Texture2D&lt;float&gt;[] t; }
    global_param p : ParameterBlock&lt;P&gt;;
    ...

    // p.t[someIndex].Load(...);
    //
    let ptrToArrayOfTextures = getFieldPtr(p, "t")                            : Ptr&lt;Texture2D&lt;float&gt;[]&gt;;
    let ptrToTexture         = getElementPtr(ptrToArrayOfTextures, someIndex) : Ptr&lt;Texture2D&lt;float&gt;&gt;;
    let texture              = load(ptrToTexture)                             : Texture2D&lt;float&gt;;
    let result               = call(loadFunc, texture, ...)                   : float;

Legalization needs to move the `t` array there out of the `p` parameter block, so the global declarations become something like:

    struct P_Ordinary { ... }; // no more "t" field
    global_param p_ordinary : ParameterBlock&lt;p_ordinary);
    global_param p_t : Texture2D&lt;float&gt;[];

In terms of the code to access `p.t[someIndex]` the problem is that `p_t` has one less level of indirection than `p.t` had. We solve this in the type legalization pass using "pseudo-types" and "pseudo-values," where one of the cases is `implicitIndirect` which holds a value of type `T`, but indicates that it should act like a value of type `T*`.

We then use some basic rules for dealing with `implicitIndirect` values, such as:

    load(implicitDeref(x)) : T =&gt; x : T
    getFieldPtr(implicitDeref(s), f) =&gt; implicitDeref(getField(s, f))
    getElementPtr(implicitDeref(a), i) =&gt; implicitDeref(getElement(a, i))

The bug here was that for the `getFieldPtr` and `getElementPtr` cases, we weren't computing the type of the `getField` or `getElement` instruction correctly. We were copying the type from the `getFieldPtr` or `getElementPtr` operation over directly, but those will be *pointer* types and we need the type of whatever they point to.

Once the types are fixed, we can properly generate legalized IR for `p.t[someIndex].Load(...) that looks like:

    let arrayOfTextures = p_t                                    : Texture2D&lt;float&gt;[];
    let texture         = getElement(arrayOfTextures, someIndex) : Texture2D&lt;float&gt;;
    let result           = call(loadFunc, texture, ...)          : float;

The old was giving the `texture` intermediate a type of `Ptr&lt;Texure2D&lt;float&gt;&gt;`. That didn't actually trip up too many things, because we mostly just went on to emit code from something with slightly incorrect types for intermediates that never show up in the generated HLSL/GLSL.

Where this caused a problem is for some of the intrinsic function definitions for the GLSL/Vulkan back-end, because those do things that inspect operand types. In particular the `$z` opcode in our intrinsic function strings triggers logic that looks at a texture operand, and uses its type to try to find the appropriate swizzle to get from a 4-component vector to the appropriate type for the operation (e.g., for a load from a `Texture2D&lt;float&gt;` we need to swizzle with `.x` to get a single scalar out of the matching GLSL texture fetch operation).

The main fix in this change is thus to make `getElementPtr` and `getFieldPtr` legalization properly account for the fact that when switching to `getElement` or `getField` we need a result type that is the "pointee" of the original result.

There was already logic to extract the pointed-to type from a pointer in `ir-specialize.cpp`, so I extracted that to a re-usable function in the IR as `tryGetPointedToType` (returns null if the type isn't actually a pointer).
This logic needed to be extended for type legalization, to deal with the various "pseudo-type" cases.

There is another fix in this change which is marking the `NonUniformResourceIndex` function as `[__readNone]`, which enables it to be more aggressively folded into use sites. Without that fix, we risk emitting code like:

```glsl
int tmp = nonUniformEXT(someIndex);
vec4 result = texelFetch(arrayOfTextures[tmp], ...);
```

The problem with that code is that (at least by my reading of the spec), assigning to the variable `tmp` that isn't declared with the `nonUniformEXT` qualifier effectively loses that qualifier, and drivers are free to assume that `tmp` is uniform when used to index into `arrayOfTextures`.

Marking the `NonUniformResourceIndex` function as `[__readNone]` indicates that it has no side effects, which should mean that our emit logic no longer needs to emit it was its own line of code to be safe.
The effects of this change are confirmed by both the new test case added, and the existing `non-uniform-indexing` test.</content>
</entry>
</feed>
