Choosing Attribute Data Types

Attribute data types should be chosen carefully to avoid unnecessary type conversions.

When passing data into a shader, such as uniforms, varyings, or attributes, always consider their usage. If the data is intended to be used in maths operations then always use a floating point data type to prevent unnecessary type conversions between integer and float. If the data is intended to be used solely as an integer, such as an index to an array, then the data will not require a conversion.

Note: This implicit conversion is performed in the Unified Shader Core (USC) and costs a few additional cycles, and so should be avoided.

The throughput (ops/cycle) of various data types should also be considered.

From fastest to slowest:

The conversion will be performed if the application uses an integer attribute type in a floating point operation, otherwise there is no additional cost associated with integer types.

The choice of attribute data type is a trade-off between shader cycles, bandwidth/storage requirements, and precision. It is important that type conversion is considered as bandwidth is always at a premium.

There are a couple of exceptions to this rule:

  • The data type 10F11F11F will require conversion to be performed by the hardware.
  • The data type 8-bit S/UNORM may not require conversion, depending on the hardware being deployed to.

Precision requirements should be checked carefully. The byte and short types are often sufficient, even for position information. For example, scaled to a range of 10m, the short types give a precision of 150 µm. Scaling and biasing those attribute values to fit a certain range can often be folded into other vertex shader calculations, such as multiplied into a transformation matrix.