On 7 November 2011 11:12, Ian Romanick <span dir="ltr">&lt;<a href="mailto:idr@freedesktop.org">idr@freedesktop.org</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
From: Ian Romanick &lt;<a href="mailto:ian.d.romanick@intel.com">ian.d.romanick@intel.com</a>&gt;<br>
<br>
This prevents other code from seeing a swizzle of the 16th component<br>
of a vector, for example.<br>
<br>
NOTE: This is a candidate for the 7.11 branch.<br>
<br>
Signed-off-by: Ian Romanick &lt;<a href="mailto:ian.d.romanick@intel.com">ian.d.romanick@intel.com</a>&gt;<br>
Bugzilla: <a href="https://bugs.freedesktop.org/show_bug.cgi?id=42517" target="_blank">https://bugs.freedesktop.org/show_bug.cgi?id=42517</a><br>
---<br>
 src/glsl/lower_vec_index_to_swizzle.cpp |   17 +++++++++++++++--<br>
 1 files changed, 15 insertions(+), 2 deletions(-)<br>
<br>
diff --git a/src/glsl/lower_vec_index_to_swizzle.cpp b/src/glsl/lower_vec_index_to_swizzle.cpp<br>
index c7630c2..ebc0833 100644<br>
--- a/src/glsl/lower_vec_index_to_swizzle.cpp<br>
+++ b/src/glsl/lower_vec_index_to_swizzle.cpp<br>
@@ -33,6 +33,7 @@<br>
 #include &quot;ir_visitor.h&quot;<br>
 #include &quot;ir_optimization.h&quot;<br>
 #include &quot;glsl_types.h&quot;<br>
+#include &quot;main/macros.h&quot;<br>
<br>
 /**<br>
  * Visitor class for replacing expressions with ir_constant values.<br>
@@ -76,8 +77,20 @@ ir_vec_index_to_swizzle_visitor::convert_vec_index_to_swizzle(ir_rvalue *ir)<br>
<br>
    void *ctx = ralloc_parent(ir);<br>
    this-&gt;progress = true;<br>
-   return new(ctx) ir_swizzle(deref-&gt;array,<br>
-                             ir_constant-&gt;value.i[0], 0, 0, 0, 1);<br>
+<br>
+   /* Page 40 of the GLSL 1.20 spec says:<br>
+    *<br>
+    *     &quot;When indexing with non-constant expressions, behavior is undefined<br>
+    *     if the index is negative, or greater than or equal to the size of<br>
+    *     the vector.&quot;<br>
+    *<br>
+    * The ir_swizzle constructor gets angry if the index is negative or too<br>
+    * large.  For simplicity sake, just clamp the index to [0, size-1].<br>
+    */<br>
+   const int i = MIN2(MAX2(ir_constant-&gt;value.i[0], 0),<br>
+                     (deref-&gt;array-&gt;type-&gt;vector_elements - 1));<br>
+<br>
+   return new(ctx) ir_swizzle(deref-&gt;array, i, 0, 0, 0, 1);<br>
 }<br>
<br>
 ir_visitor_status<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.7.6.4<br>
<br></font></span></blockquote><div><br>Reviewed-by: Paul Berry &lt;<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>&gt;<br></div></div>