On 7 November 2011 11:12, Ian Romanick <span dir="ltr"><<a href="mailto:idr@freedesktop.org">idr@freedesktop.org</a>></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 <<a href="mailto:ian.d.romanick@intel.com">ian.d.romanick@intel.com</a>><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 <<a href="mailto:ian.d.romanick@intel.com">ian.d.romanick@intel.com</a>><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 "ir_visitor.h"<br>
#include "ir_optimization.h"<br>
#include "glsl_types.h"<br>
+#include "main/macros.h"<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->progress = true;<br>
- return new(ctx) ir_swizzle(deref->array,<br>
- ir_constant->value.i[0], 0, 0, 0, 1);<br>
+<br>
+ /* Page 40 of the GLSL 1.20 spec says:<br>
+ *<br>
+ * "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."<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->value.i[0], 0),<br>
+ (deref->array->type->vector_elements - 1));<br>
+<br>
+ return new(ctx) ir_swizzle(deref->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 <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>><br></div></div>