b43ed98367655902713f4e8d13828ad5254ae919
[mesa.git] / src / mesa / vbo / vbo_minmax_index.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright 2003 VMware, Inc.
5 * Copyright 2009 VMware, Inc.
6 * All Rights Reserved.
7 * Copyright (C) 2016 Advanced Micro Devices, Inc.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/varray.h"
32 #include "main/macros.h"
33 #include "main/sse_minmax.h"
34 #include "x86/common_x86_asm.h"
35
36
37 /**
38 * Compute min and max elements by scanning the index buffer for
39 * glDraw[Range]Elements() calls.
40 * If primitive restart is enabled, we need to ignore restart
41 * indexes when computing min/max.
42 */
43 static void
44 vbo_get_minmax_index(struct gl_context *ctx,
45 const struct _mesa_prim *prim,
46 const struct _mesa_index_buffer *ib,
47 GLuint *min_index, GLuint *max_index,
48 const GLuint count)
49 {
50 const GLboolean restart = ctx->Array._PrimitiveRestart;
51 const GLuint restartIndex = _mesa_primitive_restart_index(ctx, ib->type);
52 const int index_size = vbo_sizeof_ib_type(ib->type);
53 const char *indices;
54 GLuint i;
55
56 indices = (char *) ib->ptr + prim->start * index_size;
57 if (_mesa_is_bufferobj(ib->obj)) {
58 GLsizeiptr size = MIN2(count * index_size, ib->obj->Size);
59 indices = ctx->Driver.MapBufferRange(ctx, (GLintptr) indices, size,
60 GL_MAP_READ_BIT, ib->obj,
61 MAP_INTERNAL);
62 }
63
64 switch (ib->type) {
65 case GL_UNSIGNED_INT: {
66 const GLuint *ui_indices = (const GLuint *)indices;
67 GLuint max_ui = 0;
68 GLuint min_ui = ~0U;
69 if (restart) {
70 for (i = 0; i < count; i++) {
71 if (ui_indices[i] != restartIndex) {
72 if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
73 if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
74 }
75 }
76 }
77 else {
78 #if defined(USE_SSE41)
79 if (cpu_has_sse4_1) {
80 _mesa_uint_array_min_max(ui_indices, &min_ui, &max_ui, count);
81 }
82 else
83 #endif
84 for (i = 0; i < count; i++) {
85 if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
86 if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
87 }
88 }
89 *min_index = min_ui;
90 *max_index = max_ui;
91 break;
92 }
93 case GL_UNSIGNED_SHORT: {
94 const GLushort *us_indices = (const GLushort *)indices;
95 GLuint max_us = 0;
96 GLuint min_us = ~0U;
97 if (restart) {
98 for (i = 0; i < count; i++) {
99 if (us_indices[i] != restartIndex) {
100 if (us_indices[i] > max_us) max_us = us_indices[i];
101 if (us_indices[i] < min_us) min_us = us_indices[i];
102 }
103 }
104 }
105 else {
106 for (i = 0; i < count; i++) {
107 if (us_indices[i] > max_us) max_us = us_indices[i];
108 if (us_indices[i] < min_us) min_us = us_indices[i];
109 }
110 }
111 *min_index = min_us;
112 *max_index = max_us;
113 break;
114 }
115 case GL_UNSIGNED_BYTE: {
116 const GLubyte *ub_indices = (const GLubyte *)indices;
117 GLuint max_ub = 0;
118 GLuint min_ub = ~0U;
119 if (restart) {
120 for (i = 0; i < count; i++) {
121 if (ub_indices[i] != restartIndex) {
122 if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
123 if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
124 }
125 }
126 }
127 else {
128 for (i = 0; i < count; i++) {
129 if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
130 if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
131 }
132 }
133 *min_index = min_ub;
134 *max_index = max_ub;
135 break;
136 }
137 default:
138 unreachable("not reached");
139 }
140
141 if (_mesa_is_bufferobj(ib->obj)) {
142 ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL);
143 }
144 }
145
146 /**
147 * Compute min and max elements for nr_prims
148 */
149 void
150 vbo_get_minmax_indices(struct gl_context *ctx,
151 const struct _mesa_prim *prims,
152 const struct _mesa_index_buffer *ib,
153 GLuint *min_index,
154 GLuint *max_index,
155 GLuint nr_prims)
156 {
157 GLuint tmp_min, tmp_max;
158 GLuint i;
159 GLuint count;
160
161 *min_index = ~0;
162 *max_index = 0;
163
164 for (i = 0; i < nr_prims; i++) {
165 const struct _mesa_prim *start_prim;
166
167 start_prim = &prims[i];
168 count = start_prim->count;
169 /* Do combination if possible to reduce map/unmap count */
170 while ((i + 1 < nr_prims) &&
171 (prims[i].start + prims[i].count == prims[i+1].start)) {
172 count += prims[i+1].count;
173 i++;
174 }
175 vbo_get_minmax_index(ctx, start_prim, ib, &tmp_min, &tmp_max, count);
176 *min_index = MIN2(*min_index, tmp_min);
177 *max_index = MAX2(*max_index, tmp_max);
178 }
179 }