| 1 |
Varyings |
| 2 |
======== |
| 3 |
|
| 4 |
This is the naming convention followed by the hardware (slightly different |
| 5 |
from Vulkan naming): |
| 6 |
|
| 7 |
- "Attribute": per-vertex data |
| 8 |
- "Varying": per-fragment data |
| 9 |
|
| 10 |
Thus vertex shaders always interact with attributes and fragment/pixel shaders |
| 11 |
only interact with varyings (commonly shortened to "VAR"). |
| 12 |
|
| 13 |
|
| 14 |
Hardware descriptors |
| 15 |
-------------------- |
| 16 |
|
| 17 |
Before Valhall there were two types of hardware descriptors: |
| 18 |
|
| 19 |
- ``AttributeDescriptor``: Descriptor for a single attribute in a larger buffer, |
| 20 |
contains offset, stride and the in-memory format. |
| 21 |
- ``BufferDescriptor``: Buffer, when used for attributes and varyings it |
| 22 |
contains: pointer, size and stride |
| 23 |
|
| 24 |
Midgard (v5) |
| 25 |
------------ |
| 26 |
Attributes are loaded/stored using ``LD_ATTR``/``ST_ATTR``, those query the |
| 27 |
attribute descriptors and buffer descriptors to find the real global memory |
| 28 |
offset before loading/storing. |
| 29 |
All varyings are loaded using ``LD_VAR``, including flat attributes (using a |
| 30 |
modifier), and special attributes (position, psiz, ecc...). Special attributes |
| 31 |
require a special descriptor, that is not backed by any buffer, it generates |
| 32 |
data on-the-fly. |
| 33 |
There is a concept of in-memory format and register format, the first is written |
| 34 |
inside of the ``AttributeDescriptor`` while the register format is a modifier of |
| 35 |
the instructions, when those two disagree the hardware will try to convert them, |
| 36 |
this is only supported between int-int or float-float operations. |
| 37 |
|
| 38 |
|
| 39 |
Bifrost (v6) |
| 40 |
------------ |
| 41 |
Changes: |
| 42 |
|
| 43 |
- ``ST_ATTR`` is replaced by ``LEA_ATTR`` and ``ST_CVT`` pairs. |
| 44 |
|
| 45 |
- ``LEA_ATTR`` queries memory address and format from the appropriate |
| 46 |
descriptor. |
| 47 |
- ``ST_CVT`` performs the actual conversion and memory store. |
| 48 |
- A new ``LD_VAR_SPECIAL`` is introduced, now special attributes do not need |
| 49 |
special descriptors. |
| 50 |
- ``LD_VAR`` only handles interpolated varyings, flat varyings are now handled |
| 51 |
by ``LD_VAR_FLAT``. |
| 52 |
|
| 53 |
Valhall (v9) |
| 54 |
------------ |
| 55 |
Valhall introduced layout-specific instructions, ``LEA_BUF`` and ``LD_VAR_BUF``, |
| 56 |
those do not use ``AttributeDescriptor`` but read ``BufferDescriptor`` directly. |
| 57 |
These new instructions bake the offset and in-memory format of the attribute |
| 58 |
inside of the shader code itself, saving a lookup. If all shaders in a program |
| 59 |
use those instructions, we can remove all ``AttributeDescriptor``. If the |
| 60 |
shaders aren't compiled together, one of the shaders cannot know the data layout, |
| 61 |
and it needs to fall back to the old ``AttributeDescriptor``-based path. We |
| 62 |
also have to fall back to ``AttributeDescriptor`` completely if the attribute |
| 63 |
offset overflows the immediate field. |
| 64 |
|
| 65 |
``LD_VAR_BUF`` only supports ``.f16`` and ``.f32`` as register modifiers, we can |
| 66 |
also load (flat) ints with those. We need to specify no conversion |
| 67 |
(``.f16.src_flat16`` or ``.f32.src_flat32``), but with those we can load both |
| 68 |
16-bit and 32-bit integers. |
| 69 |
|
| 70 |
In this architecture we assume the VS always uses IDVS, emitting attribute data |
| 71 |
with ``LEA_BUF`` (instead of ``LEA_ATTR``), and never emit varying |
| 72 |
``AttributeDescriptor`` for VS. |
| 73 |
|
| 74 |
Challenges |
| 75 |
---------- |
| 76 |
Theoretically, the output types from the VS and input types from the FS should |
| 77 |
always agree. In practice we cannot trust the varyings types given to us, |
| 78 |
here are some challenging examples: |
| 79 |
|
| 80 |
* Shaders are allowed to use the same types with different precision modifiers. |
| 81 |
* ``nir_opt_varying`` erases highp flat types with "float32" and packs them |
| 82 |
together. |
| 83 |
* Some games like to play with ``intBitsToFloat`` / ``floatBitsToInt``, so it's |
| 84 |
possible to have VS write int and FS read float (in that case, we treat the |
| 85 |
underlying bits as float, allowing for NaN normalization) |
| 86 |
|
| 87 |
In practice you have to follow these rules: |
| 88 |
|
| 89 |
* Only FS knows a varying smoothness, VS sees everything as flat. |
| 90 |
* When linked together, you cannot trust highp flat types, but you can trust |
| 91 |
precision |
| 92 |
* While loading/storing data, mistaking a float for an int is ok, mistaking an |
| 93 |
int for a float seems to be lossy even without bit-size conversions. Be |
| 94 |
conservative on what you say is a float. |
| 95 |
* You cannot lower flat varyings after they passed through ``nir_opt_varying`` |
| 96 |
|
| 97 |
Current implementation rules: |
| 98 |
|
| 99 |
* VS decides the memory layout |
| 100 |
* Descriptor formats can mismatch between stages (eg. VS int - FS float) |
| 101 |
* Descriptor formats always match what the instructions are using |
| 102 |
|
| 103 |
|
| 104 |
To regain the type information: |
| 105 |
|
| 106 |
* in VS, every highp varying is written as U32 |
| 107 |
* in FS, highp flat varyings are read as U32, highp smooth are read as F32 |
| 108 |
* for mediump, we trust the original type |