From dbf94d105a48b7aafb2c8cf64d8b4392d87efea1 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Fri, 15 Mar 2013 18:05:55 -0700 Subject: [PATCH] glsl: Replace constant-index vector array accesses with swizzles 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. Signed-off-by: Ian Romanick Reviewed-by: Matt Turner Reviewed-by: Kenneth Graunke Reviewed-by: Jordan Justen Cc: Eric Anholt Cc: Paul Berry --- 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 32695a874af..78751a6dca3 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 ffb84f90794..3578d988833 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; } -- 2.30.2