<p>Wow, NV assembly, a blast from the past! The semantics of this are that the calculation is done as if on a full 4-component vector, and then the destination register is masked so that the write only occurs to the selected component.</p>
<p>So this should be effectively like</p>
<pre><code>vec4 temp = R2.xyzw * R1.xyzw + R1.wwww;
R1.y = temp.y;
</code></pre>
<p>(See <a href="https://registry.khronos.org/OpenGL/extensions/NV/NV_gpu_program4.txt" rel="nofollow noreferrer">NV_gpu_program4</a> spec, Section 2.X.4.3, &quot;Program Destination Variable Update&quot;.)</p>
<p>But that agrees with the result being <code>R2.y * R1.y + R1.w</code> which you said doesn't seem to be correct, so I'm not sure what's going on.</p>

CC BY-SA 4.0
2022-09-29T21:45:07.840Z
I wasn't sure about the correctness, I compared the results I got to a glsl version (decompiled) of the shader I'm trying to understand, and the code didn't seem to match, so I tought maybe my hypothesis was wrong. But since it turns out to be correct, I'll go throught both versions again, I probably made a mistake reading it. Thanks you very much for your answer, this doc you've provided completes the other one on several points, it helps a lot.
Aulaulz
CC BY-SA 4.0
2022-09-29T18:07:18.127Z
2022-09-29T18:07:18.127Z
Nathan Reed
<p>I am trying to understand some shader code written in ARB. I am mostly using <a href="https://registry.khronos.org/OpenGL/extensions/NV/NV_gpu_program5.txt" rel="nofollow noreferrer">this</a> to understand it. However the doc doesn't explain (or I missed it) how vector operands are transformed into scalars, for example in this expression</p>
<pre><code>MAD.F32 R1.y, R2, R1, R1.w;
</code></pre>
<p>R2 and R1 are both 4 components vectors, but the swizzle is unspectified for R2 and the second R1. What is this instruction really doing? My hypothesis was that unspecified components were deduced using the result's component, thus the instruction would by equal to</p>
<pre><code>MAD.F32 R1.y, R2.y, R1.y, R1.w;
</code></pre>
<p>but it doesn't seems to be correct.</p>

<opengl><shader>
How does swizzling works in ARB shaders?
Aulaulz
# How does swizzling works in ARB shaders?

(Asked by: username_0 on 2022-09-28)

I am trying to understand some shader code written in ARB. I am mostly using [this](https://registry.khronos.org/OpenGL/extensions/NV/NV_gpu_program5.txt) to understand it. However the doc doesn't explain (or I missed it) how vector operands are transformed into scalars, for example in this expression

```
MAD.F32 R1.y, R2, R1, R1.w;

```

R2 and R1 are both 4 components vectors, but the swizzle is unspectified for R2 and the second R1. What is this instruction really doing? My hypothesis was that unspecified components were deduced using the result's component, thus the instruction would by equal to

```
MAD.F32 R1.y, R2.y, R1.y, R1.w;

```

but it doesn't seems to be correct.

**Tags:** opengl, shader


---

## 1 answers

### Answer by username_1 on 2022-09-29. Score: 1 | Accepted answer

Wow, NV assembly, a blast from the past! The semantics of this are that the calculation is done as if on a full 4-component vector, and then the destination register is masked so that the write only occurs to the selected component.

So this should be effectively like

```
vec4 temp = R2.xyzw * R1.xyzw + R1.wwww;
R1.y = temp.y;

```

(See [NV\_gpu\_program4](https://registry.khronos.org/OpenGL/extensions/NV/NV_gpu_program4.txt) spec, Section 2.X.4.3, "Program Destination Variable Update".)

But that agrees with the result being `R2.y * R1.y + R1.w` which you said doesn't seem to be correct, so I'm not sure what's going on.

> **Comments:**
> **username_0**: I wasn't sure about the correctness, I compared the results I got to a glsl version (decompiled) of the shader I'm trying to understand, and the code didn't seem to match, so I tought maybe my hypothesis was wrong. But since it turns out to be correct, I'll go throught both versions again, I probably made a mistake reading it. Thanks you very much for your answer, this doc you've provided completes the other one on several points, it helps a lot.  

---

