[Mesa-dev] [PATCH 2/4] glsl: Replace constant-index vector array accesses with swizzles

Paul Berry stereotype441 at gmail.com
Fri Mar 29 14:15:15 PDT 2013


On 27 March 2013 09:30, Ian Romanick <idr at freedesktop.org> wrote:

> From: Ian Romanick <ian.d.romanick at intel.com>
>
> Search and replace:
>
>     ][0] -> ].x
>     ][1] -> ].y
>     ][2] -> ].z
>     ][3] -> ].w
>
> Fixes piglit tests inverse-mat[234].{vert,frag}.  These tests call the
> inverse function with constant parameters and expect proper constant
> folding to happen.  My suspicion is that this patch papers over some bug
> in constant propagation involving array accesses.
>
> Either way, all of these accesses eventually get lowered to swizzles.
> This cuts out the middle man (saving a trivial amount of CPU).
>
> NOTE: This is a candidate for the 9.1 branch.
>

I've tracked down the constant propagation bug that this patch papers over,
and I just sent out a bug fix ("glsl: Fix array indexing when constant
folding built-in functions.")

Assuming my bug fix is correct, can we NAK this patch?  I think it makes
the source code harder to read, and as you point out the benefit is just to
save a trivial amount of CPU.


>
> Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
> Cc: Eric Anholt <eric at anholt.net>
> Cc: Paul Berry <stereotype441 at gmail.com>
> ---
>  src/glsl/builtins/glsl/determinant.glsl |  62 +++++++++---------
>  src/glsl/builtins/glsl/inverse.glsl     | 112
> ++++++++++++++++----------------
>  2 files changed, 87 insertions(+), 87 deletions(-)
>
> diff --git a/src/glsl/builtins/glsl/determinant.glsl
> b/src/glsl/builtins/glsl/determinant.glsl
> index 32695a8..78751a6 100644
> --- a/src/glsl/builtins/glsl/determinant.glsl
> +++ b/src/glsl/builtins/glsl/determinant.glsl
> @@ -24,47 +24,47 @@
>  #version 120
>  float determinant(mat2 m)
>  {
> -   return m[0][0] * m[1][1] - m[1][0] * m[0][1];
> +   return m[0].x * m[1].y - m[1].x * m[0].y;
>  }
>
>  float determinant(mat3 m)
>  {
> -   return (+ m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
> -           - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
> -           + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]));
> +   return (+ m[0].x * (m[1].y * m[2].z - m[1].z * m[2].y)
> +           - m[0].y * (m[1].x * m[2].z - m[1].z * m[2].x)
> +           + m[0].z * (m[1].x * m[2].y - m[1].y * m[2].x));
>  }
>
>  float determinant(mat4 m)
>  {
> -   float SubFactor00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
> -   float SubFactor01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
> -   float SubFactor02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
> -   float SubFactor03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
> -   float SubFactor04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
> -   float SubFactor05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
> -   float SubFactor06 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
> -   float SubFactor07 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
> -   float SubFactor08 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
> -   float SubFactor09 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
> -   float SubFactor10 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
> -   float SubFactor11 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
> -   float SubFactor12 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
> -   float SubFactor13 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
> -   float SubFactor14 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
> -   float SubFactor15 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
> -   float SubFactor16 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
> -   float SubFactor17 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
> -   float SubFactor18 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
> +   float SubFactor00 = m[2].z * m[3].w - m[3].z * m[2].w;
> +   float SubFactor01 = m[2].y * m[3].w - m[3].y * m[2].w;
> +   float SubFactor02 = m[2].y * m[3].z - m[3].y * m[2].z;
> +   float SubFactor03 = m[2].x * m[3].w - m[3].x * m[2].w;
> +   float SubFactor04 = m[2].x * m[3].z - m[3].x * m[2].z;
> +   float SubFactor05 = m[2].x * m[3].y - m[3].x * m[2].y;
> +   float SubFactor06 = m[1].z * m[3].w - m[3].z * m[1].w;
> +   float SubFactor07 = m[1].y * m[3].w - m[3].y * m[1].w;
> +   float SubFactor08 = m[1].y * m[3].z - m[3].y * m[1].z;
> +   float SubFactor09 = m[1].x * m[3].w - m[3].x * m[1].w;
> +   float SubFactor10 = m[1].x * m[3].z - m[3].x * m[1].z;
> +   float SubFactor11 = m[1].y * m[3].w - m[3].y * m[1].w;
> +   float SubFactor12 = m[1].x * m[3].y - m[3].x * m[1].y;
> +   float SubFactor13 = m[1].z * m[2].w - m[2].z * m[1].w;
> +   float SubFactor14 = m[1].y * m[2].w - m[2].y * m[1].w;
> +   float SubFactor15 = m[1].y * m[2].z - m[2].y * m[1].z;
> +   float SubFactor16 = m[1].x * m[2].w - m[2].x * m[1].w;
> +   float SubFactor17 = m[1].x * m[2].z - m[2].x * m[1].z;
> +   float SubFactor18 = m[1].x * m[2].y - m[2].x * m[1].y;
>
>     vec4 adj_0;
>
> -   adj_0[0] = + (m[1][1] * SubFactor00 - m[1][2] * SubFactor01 + m[1][3]
> * SubFactor02);
> -   adj_0[1] = - (m[1][0] * SubFactor00 - m[1][2] * SubFactor03 + m[1][3]
> * SubFactor04);
> -   adj_0[2] = + (m[1][0] * SubFactor01 - m[1][1] * SubFactor03 + m[1][3]
> * SubFactor05);
> -   adj_0[3] = - (m[1][0] * SubFactor02 - m[1][1] * SubFactor04 + m[1][2]
> * SubFactor05);
> +   adj_0.x = + (m[1].y * SubFactor00 - m[1].z * SubFactor01 + m[1].w *
> SubFactor02);
> +   adj_0.y = - (m[1].x * SubFactor00 - m[1].z * SubFactor03 + m[1].w *
> SubFactor04);
> +   adj_0.z = + (m[1].x * SubFactor01 - m[1].y * SubFactor03 + m[1].w *
> SubFactor05);
> +   adj_0.w = - (m[1].x * SubFactor02 - m[1].y * SubFactor04 + m[1].z *
> SubFactor05);
>
> -   return (+ m[0][0] * adj_0[0]
> -           + m[0][1] * adj_0[1]
> -           + m[0][2] * adj_0[2]
> -           + m[0][3] * adj_0[3]);
> +   return (+ m[0].x * adj_0.x
> +           + m[0].y * adj_0.y
> +           + m[0].z * adj_0.z
> +           + m[0].w * adj_0.w);
>  }
> diff --git a/src/glsl/builtins/glsl/inverse.glsl
> b/src/glsl/builtins/glsl/inverse.glsl
> index ffb84f9..3578d98 100644
> --- a/src/glsl/builtins/glsl/inverse.glsl
> +++ b/src/glsl/builtins/glsl/inverse.glsl
> @@ -24,82 +24,82 @@
>  mat2 inverse(mat2 m)
>  {
>     mat2 adj;
> -   adj[0][0] = m[1][1];
> -   adj[0][1] = -m[0][1];
> -   adj[1][0] = -m[1][0];
> -   adj[1][1] = m[0][0];
> -   float det = m[0][0] * m[1][1] - m[1][0] * m[0][1];
> +   adj[0].x = m[1].y;
> +   adj[0].y = -m[0].y;
> +   adj[1].x = -m[1].x;
> +   adj[1].y = m[0].x;
> +   float det = m[0].x * m[1].y - m[1].x * m[0].y;
>     return adj / det;
>  }
>
>  mat3 inverse(mat3 m)
>  {
>     mat3 adj;
> -   adj[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
> -   adj[1][0] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
> -   adj[2][0] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
> -   adj[0][1] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
> -   adj[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
> -   adj[2][1] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
> -   adj[0][2] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
> -   adj[1][2] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
> -   adj[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
> +   adj[0].x = + (m[1].y * m[2].z - m[2].y * m[1].z);
> +   adj[1].x = - (m[1].x * m[2].z - m[2].x * m[1].z);
> +   adj[2].x = + (m[1].x * m[2].y - m[2].x * m[1].y);
> +   adj[0].y = - (m[0].y * m[2].z - m[2].y * m[0].z);
> +   adj[1].y = + (m[0].x * m[2].z - m[2].x * m[0].z);
> +   adj[2].y = - (m[0].x * m[2].y - m[2].x * m[0].y);
> +   adj[0].z = + (m[0].y * m[1].z - m[1].y * m[0].z);
> +   adj[1].z = - (m[0].x * m[1].z - m[1].x * m[0].z);
> +   adj[2].z = + (m[0].x * m[1].y - m[1].x * m[0].y);
>
> -   float det = (+ m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
> -               - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
> -               + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]));
> +   float det = (+ m[0].x * (m[1].y * m[2].z - m[1].z * m[2].y)
> +               - m[0].y * (m[1].x * m[2].z - m[1].z * m[2].x)
> +               + m[0].z * (m[1].x * m[2].y - m[1].y * m[2].x));
>
>     return adj / det;
>  }
>
>  mat4 inverse(mat4 m)
>  {
> -   float SubFactor00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
> -   float SubFactor01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
> -   float SubFactor02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
> -   float SubFactor03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
> -   float SubFactor04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
> -   float SubFactor05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
> -   float SubFactor06 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
> -   float SubFactor07 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
> -   float SubFactor08 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
> -   float SubFactor09 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
> -   float SubFactor10 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
> -   float SubFactor11 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
> -   float SubFactor12 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
> -   float SubFactor13 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
> -   float SubFactor14 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
> -   float SubFactor15 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
> -   float SubFactor16 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
> -   float SubFactor17 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
> -   float SubFactor18 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
> +   float SubFactor00 = m[2].z * m[3].w - m[3].z * m[2].w;
> +   float SubFactor01 = m[2].y * m[3].w - m[3].y * m[2].w;
> +   float SubFactor02 = m[2].y * m[3].z - m[3].y * m[2].z;
> +   float SubFactor03 = m[2].x * m[3].w - m[3].x * m[2].w;
> +   float SubFactor04 = m[2].x * m[3].z - m[3].x * m[2].z;
> +   float SubFactor05 = m[2].x * m[3].y - m[3].x * m[2].y;
> +   float SubFactor06 = m[1].z * m[3].w - m[3].z * m[1].w;
> +   float SubFactor07 = m[1].y * m[3].w - m[3].y * m[1].w;
> +   float SubFactor08 = m[1].y * m[3].z - m[3].y * m[1].z;
> +   float SubFactor09 = m[1].x * m[3].w - m[3].x * m[1].w;
> +   float SubFactor10 = m[1].x * m[3].z - m[3].x * m[1].z;
> +   float SubFactor11 = m[1].y * m[3].w - m[3].y * m[1].w;
> +   float SubFactor12 = m[1].x * m[3].y - m[3].x * m[1].y;
> +   float SubFactor13 = m[1].z * m[2].w - m[2].z * m[1].w;
> +   float SubFactor14 = m[1].y * m[2].w - m[2].y * m[1].w;
> +   float SubFactor15 = m[1].y * m[2].z - m[2].y * m[1].z;
> +   float SubFactor16 = m[1].x * m[2].w - m[2].x * m[1].w;
> +   float SubFactor17 = m[1].x * m[2].z - m[2].x * m[1].z;
> +   float SubFactor18 = m[1].x * m[2].y - m[2].x * m[1].y;
>
>     mat4 adj;
>
> -   adj[0][0] = + (m[1][1] * SubFactor00 - m[1][2] * SubFactor01 + m[1][3]
> * SubFactor02);
> -   adj[1][0] = - (m[1][0] * SubFactor00 - m[1][2] * SubFactor03 + m[1][3]
> * SubFactor04);
> -   adj[2][0] = + (m[1][0] * SubFactor01 - m[1][1] * SubFactor03 + m[1][3]
> * SubFactor05);
> -   adj[3][0] = - (m[1][0] * SubFactor02 - m[1][1] * SubFactor04 + m[1][2]
> * SubFactor05);
> +   adj[0].x = + (m[1].y * SubFactor00 - m[1].z * SubFactor01 + m[1].w *
> SubFactor02);
> +   adj[1].x = - (m[1].x * SubFactor00 - m[1].z * SubFactor03 + m[1].w *
> SubFactor04);
> +   adj[2].x = + (m[1].x * SubFactor01 - m[1].y * SubFactor03 + m[1].w *
> SubFactor05);
> +   adj[3].x = - (m[1].x * SubFactor02 - m[1].y * SubFactor04 + m[1].z *
> SubFactor05);
>
> -   adj[0][1] = - (m[0][1] * SubFactor00 - m[0][2] * SubFactor01 + m[0][3]
> * SubFactor02);
> -   adj[1][1] = + (m[0][0] * SubFactor00 - m[0][2] * SubFactor03 + m[0][3]
> * SubFactor04);
> -   adj[2][1] = - (m[0][0] * SubFactor01 - m[0][1] * SubFactor03 + m[0][3]
> * SubFactor05);
> -   adj[3][1] = + (m[0][0] * SubFactor02 - m[0][1] * SubFactor04 + m[0][2]
> * SubFactor05);
> +   adj[0].y = - (m[0].y * SubFactor00 - m[0].z * SubFactor01 + m[0].w *
> SubFactor02);
> +   adj[1].y = + (m[0].x * SubFactor00 - m[0].z * SubFactor03 + m[0].w *
> SubFactor04);
> +   adj[2].y = - (m[0].x * SubFactor01 - m[0].y * SubFactor03 + m[0].w *
> SubFactor05);
> +   adj[3].y = + (m[0].x * SubFactor02 - m[0].y * SubFactor04 + m[0].z *
> SubFactor05);
>
> -   adj[0][2] = + (m[0][1] * SubFactor06 - m[0][2] * SubFactor07 + m[0][3]
> * SubFactor08);
> -   adj[1][2] = - (m[0][0] * SubFactor06 - m[0][2] * SubFactor09 + m[0][3]
> * SubFactor10);
> -   adj[2][2] = + (m[0][0] * SubFactor11 - m[0][1] * SubFactor09 + m[0][3]
> * SubFactor12);
> -   adj[3][2] = - (m[0][0] * SubFactor08 - m[0][1] * SubFactor10 + m[0][2]
> * SubFactor12);
> +   adj[0].z = + (m[0].y * SubFactor06 - m[0].z * SubFactor07 + m[0].w *
> SubFactor08);
> +   adj[1].z = - (m[0].x * SubFactor06 - m[0].z * SubFactor09 + m[0].w *
> SubFactor10);
> +   adj[2].z = + (m[0].x * SubFactor11 - m[0].y * SubFactor09 + m[0].w *
> SubFactor12);
> +   adj[3].z = - (m[0].x * SubFactor08 - m[0].y * SubFactor10 + m[0].z *
> SubFactor12);
>
> -   adj[0][3] = - (m[0][1] * SubFactor13 - m[0][2] * SubFactor14 + m[0][3]
> * SubFactor15);
> -   adj[1][3] = + (m[0][0] * SubFactor13 - m[0][2] * SubFactor16 + m[0][3]
> * SubFactor17);
> -   adj[2][3] = - (m[0][0] * SubFactor14 - m[0][1] * SubFactor16 + m[0][3]
> * SubFactor18);
> -   adj[3][3] = + (m[0][0] * SubFactor15 - m[0][1] * SubFactor17 + m[0][2]
> * SubFactor18);
> +   adj[0].w = - (m[0].y * SubFactor13 - m[0].z * SubFactor14 + m[0].w *
> SubFactor15);
> +   adj[1].w = + (m[0].x * SubFactor13 - m[0].z * SubFactor16 + m[0].w *
> SubFactor17);
> +   adj[2].w = - (m[0].x * SubFactor14 - m[0].y * SubFactor16 + m[0].w *
> SubFactor18);
> +   adj[3].w = + (m[0].x * SubFactor15 - m[0].y * SubFactor17 + m[0].z *
> SubFactor18);
>
> -   float det = (+ m[0][0] * adj[0][0]
> -               + m[0][1] * adj[1][0]
> -               + m[0][2] * adj[2][0]
> -               + m[0][3] * adj[3][0]);
> +   float det = (+ m[0].x * adj[0].x
> +               + m[0].y * adj[1].x
> +               + m[0].z * adj[2].x
> +               + m[0].w * adj[3].x);
>
>     return adj / det;
>  }
> --
> 1.8.1.4
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20130329/706aba13/attachment-0001.html>


More information about the mesa-dev mailing list