Skip to content

Add index operators to XMFLOAT2/3/4 #256

@ModulePillow

Description

@ModulePillow

Issue Description

It's valuable to add subscript operators for the XMINT2~4 and XMFLOAT2~4 types.

An example is shown below:

// For XMFLOAT4
float& operator [] (In_range(0, 3) int32_t index) noexcept { return reinterpret_cast<float*>(this)[index]; }
const float operator [] (In_range(0, 3) int32_t index) const noexcept { return reinterpret_cast<const float*>(this)[index]; }

Pros & Reasons

While I am developing my own renderer, I found that a new project relying on DirectXMath can use data types like XMFLOAT4 to store key data in its business code.

This decision has many advantages:

  • Reduces memory copy. (like: a float array -> XMFLOAT4 -> XMVECTOR). The business code uses XMFLOAT4 to store key data; those operators make a vector behave like a regular array.
  • Eliminates non-straightforward code. There are other weird ways to reduce memory copy, like this:

XMVECTOR v = XMLoadFloat4(reinterpret_cast<const XMFLOAT4*>(ObjA.FloatArray));

Cons

  • May lack runtime boundary checks for indices.
  • Abundance for some projects. For existing projects using DirectXMath as a plugin, they may not wanna use XMFLOAT4 to store core data directly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions