draw: clamp the viewports to always be between 0 and max
authorZack Rusin <zackr@vmware.com>
Sat, 25 May 2013 05:02:46 +0000 (01:02 -0400)
committerZack Rusin <zackr@vmware.com>
Sat, 25 May 2013 13:49:20 +0000 (09:49 -0400)
If the viewport index is larger than the PIPE_MAX_VIEWPORTS,
then the first (0-th) viewport should be used.

Signed-off-by: Zack Rusin <zackr@vmware.com>
Reviewed-by: José Fonseca<jfonseca@vmware.com>
Reviewed-by: Brian Paul <brianp@vmware.com>
src/gallium/auxiliary/draw/draw_cliptest_tmp.h
src/gallium/auxiliary/draw/draw_context.c
src/gallium/auxiliary/draw/draw_pipe_clip.c
src/gallium/auxiliary/draw/draw_private.h
src/gallium/auxiliary/draw/draw_vs_variant.c

index 09e1fd713341ca7dd23e8a46f37ce3e120119ce6..d316b777154bfe4b292d098218d29ae7e799c0e0 100644 (file)
@@ -25,8 +25,6 @@
  * 
  **************************************************************************/
 
-
-
 static boolean TAG(do_cliptest)( struct pt_post_vs *pvs,
                                  struct draw_vertex_info *info )
 {
@@ -57,8 +55,10 @@ static boolean TAG(do_cliptest)( struct pt_post_vs *pvs,
          draw_current_shader_uses_viewport_index(pvs->draw) ?
          *((unsigned*)out->data[viewport_index_output]): 0;
       unsigned mask = 0x0;
-      const float *scale = pvs->draw->viewports[viewport_index].scale;
-      const float *trans = pvs->draw->viewports[viewport_index].translate;
+      const float *scale = pvs->draw->viewports[
+         draw_clamp_viewport_idx(viewport_index)].scale;
+      const float *trans = pvs->draw->viewports[
+         draw_clamp_viewport_idx(viewport_index)].translate;
   
       initialize_vertex_header(out);
 
index 63ccf386f298cc9d9ac67480a2ef19a7fad9a55b..58ce270cdd073ac2f42d09fc77ec221099e26997 100644 (file)
@@ -319,14 +319,12 @@ void draw_set_viewport_states( struct draw_context *draw,
    const struct pipe_viewport_state *viewport = vps;
    draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
 
-   if (start_slot > PIPE_MAX_VIEWPORTS)
-      return;
-
-   if ((start_slot + num_viewports) > PIPE_MAX_VIEWPORTS)
-      num_viewports = PIPE_MAX_VIEWPORTS - start_slot;
+   debug_assert(start_slot < PIPE_MAX_VIEWPORTS);
+   debug_assert((start_slot + num_viewports) <= PIPE_MAX_VIEWPORTS);
 
    memcpy(draw->viewports + start_slot, vps,
           sizeof(struct pipe_viewport_state) * num_viewports);
+
    draw->identity_viewport = (num_viewports == 1) &&
       (viewport->scale[0] == 1.0f &&
        viewport->scale[1] == 1.0f &&
index b01e51994cba9eaa22b7cd09f8cc522760a0e74a..aacda15f46679c6a253998b7e63bfcf0ec7a5c70 100644 (file)
@@ -112,8 +112,6 @@ static void copy_flat( struct draw_stage *stage,
    }
 }
 
-
-
 /* Interpolate between two vertices to produce a third.  
  */
 static void interp( const struct clip_stage *clip,
@@ -152,9 +150,11 @@ static void interp( const struct clip_stage *clip,
          *((unsigned*)in->data[viewport_index_output]) : 0;
       const float *pos = dst->pre_clip_pos;
       const float *scale =
-         clip->stage.draw->viewports[viewport_index].scale;
+         clip->stage.draw->viewports[
+            draw_clamp_viewport_idx(viewport_index)].scale;
       const float *trans =
-         clip->stage.draw->viewports[viewport_index].translate;
+         clip->stage.draw->viewports[
+            draw_clamp_viewport_idx(viewport_index)].translate;
       const float oow = 1.0f / pos[3];
 
       dst->data[pos_attr][0] = pos[0] * oow * scale[0] + trans[0];
index e5f192b75763396b894bdcc1d465bd4bed09a460..f30f9af388b7329ad1ed44a42e53520f119acae4 100644 (file)
@@ -469,4 +469,15 @@ draw_get_rasterizer_no_cull( struct draw_context *draw,
 #define DRAW_GET_IDX(_elts, _i)                   \
    (((_i) >= draw->pt.user.eltMax) ? 0 : (_elts)[_i])
 
+/**
+ * Return index of the given viewport clamping it
+ * to be between 0 <= and < PIPE_MAX_VIEWPORTS
+ */
+static INLINE unsigned
+draw_clamp_viewport_idx(int idx)
+{
+   return ((PIPE_MAX_VIEWPORTS > idx || idx < 0) ? idx : 0);
+}
+
+
 #endif /* DRAW_PRIVATE_H */
index 0387eafbbce4e59ec60bf55d8316d64924f8863c..152c130318348a3d3e464f059bb25b2ba6264f1f 100644 (file)
@@ -91,9 +91,8 @@ find_viewport(struct draw_context *draw,
    int viewport_index =
       draw_current_shader_uses_viewport_index(draw) ?
       data[viewport_index_output * 4] : 0;
-   
-   debug_assert(viewport_index < PIPE_MAX_VIEWPORTS);
-   viewport_index = MIN2(viewport_index, PIPE_MAX_VIEWPORTS - 1);
+
+   viewport_index = draw_clamp_viewport_idx(viewport_index);
 
    return &draw->viewports[viewport_index];
 }