vbo/dlist: apply start_offset after indices construction
authorPierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Tue, 10 Aug 2021 09:42:28 +0000 (11:42 +0200)
committerPierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Thu, 9 Sep 2021 14:42:16 +0000 (16:42 +0200)
This will make our life easier for the next commit.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12646>

src/mesa/vbo/vbo_save_api.c

index 8c00e3a..906f376 100644 (file)
@@ -484,18 +484,13 @@ static void _free_entry(struct hash_entry *entry)
  */
 static uint32_t
 add_vertex(struct vbo_save_context *save, struct hash_table *hash_to_index,
-           uint32_t index, uint32_t base_index, fi_type *new_buffer,
-           uint32_t *max_index)
+           uint32_t index, fi_type *new_buffer, uint32_t *max_index)
 {
    /* If vertex deduplication is disabled return the original index. */
    if (!hash_to_index)
       return index;
 
-   fi_type *vert = save->buffer_map;
-   /* Cancel the start_offset trick.
-    * This way we get the correct offset in all cases (= start_offset being 0 or not).
-    */
-   vert += save->vertex_size * (index - base_index);
+   fi_type *vert = save->buffer_map + save->vertex_size * index;
 
    struct vertex_key *key = malloc(sizeof(struct vertex_key));
    key->vertex_size = save->vertex_size;
@@ -512,18 +507,18 @@ add_vertex(struct vbo_save_context *save, struct hash_table *hash_to_index,
        * starting at index 0.
        */
       uint32_t n = _mesa_hash_table_num_entries(hash_to_index);
-      *max_index = MAX2(n + base_index, *max_index);
+      *max_index = MAX2(n, *max_index);
 
       memcpy(&new_buffer[save->vertex_size * n],
              vert,
              save->vertex_size * sizeof(fi_type));
 
-      _mesa_hash_table_insert(hash_to_index, key, (void*)(uintptr_t)(n + base_index));
+      _mesa_hash_table_insert(hash_to_index, key, (void*)(uintptr_t)(n));
 
       /* The index buffer is shared between list compilations, so add the base index to get
        * the final index.
        */
-      return n + base_index;
+      return n;
    }
 }
 
@@ -653,15 +648,6 @@ compile_vertex_list(struct gl_context *ctx)
 
    merge_prims(ctx, node->cold->prims, &node->cold->prim_count);
 
-   /* Correct the primitive starts, we can only do this here as copy_vertices
-    * and convert_line_loop_to_strip above consume the uncorrected starts.
-    * On the other hand the _vbo_loopback_vertex_list call below needs the
-    * primitves to be corrected already.
-    */
-   for (unsigned i = 0; i < node->cold->prim_count; i++) {
-      node->cold->prims[i].start += start_offset;
-   }
-
    /* Create an index buffer. */
    node->cold->min_index = node->cold->max_index = 0;
    if (save->vert_count == 0 || save->prim_count == 0)
@@ -739,14 +725,14 @@ compile_vertex_list(struct gl_context *ctx)
 
          indices[idx] = indices[idx - 1];
          indices[idx + 1] = add_vertex(save, vertex_to_index, original_prims[i].start,
-                                       start_offset, temp_vertices_buffer, &max_index);
+                                       temp_vertices_buffer, &max_index);
          idx += 2;
          merged_prims[last_valid_prim].count += 2;
 
          if (tri_count % 2) {
             /* Add another index to preserve winding order */
             indices[idx++] = add_vertex(save, vertex_to_index, original_prims[i].start,
-                                        start_offset, temp_vertices_buffer, &max_index);
+                                        temp_vertices_buffer, &max_index);
             merged_prims[last_valid_prim].count++;
          }
       }
@@ -764,11 +750,11 @@ compile_vertex_list(struct gl_context *ctx)
              original_prims[i + 1].mode == GL_LINES)))) {
          for (unsigned j = 0; j < vertex_count; j++) {
             indices[idx++] = add_vertex(save, vertex_to_index, original_prims[i].start + j,
-                                        start_offset, temp_vertices_buffer, &max_index);
+                                        temp_vertices_buffer, &max_index);
             /* Repeat all but the first/last indices. */
             if (j && j != vertex_count - 1) {
                indices[idx++] = add_vertex(save, vertex_to_index, original_prims[i].start + j,
-                                           start_offset, temp_vertices_buffer, &max_index);
+                                           temp_vertices_buffer, &max_index);
             }
          }
       } else {
@@ -777,7 +763,7 @@ compile_vertex_list(struct gl_context *ctx)
 
          for (unsigned j = 0; j < vertex_count; j++) {
             indices[idx++] = add_vertex(save, vertex_to_index, original_prims[i].start + j,
-                                        start_offset, temp_vertices_buffer, &max_index);
+                                        temp_vertices_buffer, &max_index);
          }
       }
 
@@ -803,6 +789,21 @@ compile_vertex_list(struct gl_context *ctx)
    node->cold->ib.count = idx;
    node->cold->ib.index_size_shift = (GL_UNSIGNED_INT - GL_UNSIGNED_BYTE) >> 1;
 
+   /* Correct the primitive starts, we can only do this here as copy_vertices
+    * and convert_line_loop_to_strip above consume the uncorrected starts.
+    * On the other hand the _vbo_loopback_vertex_list call below needs the
+    * primitives to be corrected already.
+    */
+   for (unsigned i = 0; i < node->cold->prim_count; i++) {
+      node->cold->prims[i].start += start_offset;
+   }
+   /* start_offset shifts vertices (so v[0] becomes v[start_offset]), so we have
+    * to apply this transformation to all indices and max_index.
+    */
+   for (unsigned i = 0; i < idx; i++)
+      indices[i] += start_offset;
+   max_index += start_offset;
+
    if (!indices_offset) {
       /* Allocate a new index buffer */
       _mesa_reference_buffer_object(ctx, &save->previous_ib, NULL);