summaryrefslogtreecommitdiffstats
path: root/src/gallium
diff options
context:
space:
mode:
Diffstat (limited to 'src/gallium')
-rw-r--r--src/gallium/auxiliary/util/u_prim.h44
-rw-r--r--src/gallium/tests/unit/meson.build3
-rw-r--r--src/gallium/tests/unit/u_prim_verts_test.c45
3 files changed, 91 insertions, 1 deletions
diff --git a/src/gallium/auxiliary/util/u_prim.h b/src/gallium/auxiliary/util/u_prim.h
index fb9290d62ca..ae05788702c 100644
--- a/src/gallium/auxiliary/util/u_prim.h
+++ b/src/gallium/auxiliary/util/u_prim.h
@@ -283,6 +283,50 @@ u_reduced_prims_for_vertices(enum pipe_prim_type primitive, int vertices)
}
}
+static inline enum pipe_prim_type
+u_base_prim_type(enum pipe_prim_type prim_type)
+{
+ switch(prim_type) {
+ case PIPE_PRIM_POINTS:
+ return PIPE_PRIM_POINTS;
+ case PIPE_PRIM_LINES:
+ case PIPE_PRIM_LINE_LOOP:
+ case PIPE_PRIM_LINE_STRIP:
+ case PIPE_PRIM_LINES_ADJACENCY:
+ case PIPE_PRIM_LINE_STRIP_ADJACENCY:
+ return PIPE_PRIM_LINES;
+ case PIPE_PRIM_TRIANGLES:
+ case PIPE_PRIM_TRIANGLE_STRIP:
+ case PIPE_PRIM_TRIANGLE_FAN:
+ case PIPE_PRIM_TRIANGLES_ADJACENCY:
+ case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
+ return PIPE_PRIM_TRIANGLES;
+ case PIPE_PRIM_QUADS:
+ case PIPE_PRIM_QUAD_STRIP:
+ return PIPE_PRIM_QUADS;
+ default:
+ return prim_type;
+ }
+}
+
+static inline unsigned
+u_vertices_for_prims(enum pipe_prim_type prim_type, int count)
+{
+ if (count <= 0)
+ return 0;
+
+ /* We can only figure out the number of vertices from a number of primitives
+ * if we are using basic primitives (so no loops, strips, fans, etc).
+ */
+ assert(prim_type == u_base_prim_type(prim_type) &&
+ prim_type != PIPE_PRIM_PATCHES && prim_type != PIPE_PRIM_POLYGON);
+
+ const struct u_prim_vertex_count *info = u_prim_vertex_count(prim_type);
+ assert(info);
+
+ return info->min + (count - 1) * info->incr;
+}
+
const char *u_prim_name(enum pipe_prim_type pipe_prim);
diff --git a/src/gallium/tests/unit/meson.build b/src/gallium/tests/unit/meson.build
index d4f3aed0877..afde9840c37 100644
--- a/src/gallium/tests/unit/meson.build
+++ b/src/gallium/tests/unit/meson.build
@@ -19,7 +19,8 @@
# SOFTWARE.
foreach t : ['pipe_barrier_test', 'u_cache_test', 'u_half_test',
- 'u_format_test', 'u_format_compatible_test', 'translate_test']
+ 'u_format_test', 'u_format_compatible_test', 'translate_test',
+ 'u_prim_verts_test' ]
exe = executable(
t,
'@[email protected]'.format(t),
diff --git a/src/gallium/tests/unit/u_prim_verts_test.c b/src/gallium/tests/unit/u_prim_verts_test.c
new file mode 100644
index 00000000000..94b64f75eec
--- /dev/null
+++ b/src/gallium/tests/unit/u_prim_verts_test.c
@@ -0,0 +1,45 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "util/u_prim.h"
+
+struct test_info {
+ enum pipe_prim_type prim_type;
+ uint32_t count;
+ uint32_t expected;
+};
+
+struct test_info tests[] = {
+ { PIPE_PRIM_POINTS, 0, 0 },
+ { PIPE_PRIM_POINTS, 1, 1 },
+ { PIPE_PRIM_POINTS, 2, 2 },
+
+ { PIPE_PRIM_LINES, 0, 0 },
+ { PIPE_PRIM_LINES, 1, 2 },
+ { PIPE_PRIM_LINES, 2, 4 },
+
+ { PIPE_PRIM_TRIANGLES, 0, 0 },
+ { PIPE_PRIM_TRIANGLES, 1, 3 },
+ { PIPE_PRIM_TRIANGLES, 2, 6 },
+
+ { PIPE_PRIM_QUADS, 0, 0 },
+ { PIPE_PRIM_QUADS, 1, 4 },
+ { PIPE_PRIM_QUADS, 2, 8 },
+};
+
+int
+main(int argc, char **argv)
+{
+ for(int i = 0; i < ARRAY_SIZE(tests); i++) {
+ struct test_info *info = &tests[i];
+ uint32_t n = u_vertices_for_prims(info->prim_type, info->count);
+ if (n != info->expected) {
+ printf("Failure! Expected %u vertices for %u x %s, but got %u.\n",
+ info->expected, info->count, u_prim_name(info->prim_type), n);
+ return 1;
+ }
+ }
+
+ printf("Success!\n");
+ return 0;
+}