Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / drivers / nouveau / nouveau_util.h
1 #ifndef __NOUVEAU_UTIL_H__
2 #define __NOUVEAU_UTIL_H__
3
4 /* Determine how many vertices can be pushed into the command stream.
5 * Where the remaining space isn't large enough to represent all verices,
6 * split the buffer at primitive boundaries.
7 *
8 * Returns a count of vertices that can be rendered, and an index to
9 * restart drawing at after a flush.
10 */
11 static INLINE unsigned
12 nouveau_vbuf_split(unsigned remaining, unsigned overhead, unsigned vpp,
13 unsigned mode, unsigned start, unsigned count,
14 unsigned *restart)
15 {
16 int max, adj = 0;
17
18 max = remaining - overhead;
19 if (max < 0)
20 return 0;
21
22 max *= vpp;
23 if (max >= count)
24 return count;
25
26 switch (mode) {
27 case PIPE_PRIM_POINTS:
28 break;
29 case PIPE_PRIM_LINES:
30 max = max & 1;
31 break;
32 case PIPE_PRIM_TRIANGLES:
33 max = max - (max % 3);
34 break;
35 case PIPE_PRIM_QUADS:
36 max = max & 3;
37 break;
38 case PIPE_PRIM_LINE_LOOP:
39 case PIPE_PRIM_LINE_STRIP:
40 if (max < 2)
41 max = 0;
42 adj = 1;
43 break;
44 case PIPE_PRIM_POLYGON:
45 case PIPE_PRIM_TRIANGLE_STRIP:
46 case PIPE_PRIM_TRIANGLE_FAN:
47 if (max < 3)
48 max = 0;
49 adj = 2;
50 break;
51 case PIPE_PRIM_QUAD_STRIP:
52 if (max < 4)
53 max = 0;
54 adj = 3;
55 break;
56 default:
57 assert(0);
58 }
59
60 *restart = start + max - adj;
61 return max;
62 }
63
64 /* Integer base-2 logarithm, rounded towards zero. */
65 static INLINE unsigned log2i(unsigned i)
66 {
67 unsigned r = 0;
68
69 if (i & 0xffff0000) {
70 i >>= 16;
71 r += 16;
72 }
73 if (i & 0x0000ff00) {
74 i >>= 8;
75 r += 8;
76 }
77 if (i & 0x000000f0) {
78 i >>= 4;
79 r += 4;
80 }
81 if (i & 0x0000000c) {
82 i >>= 2;
83 r += 2;
84 }
85 if (i & 0x00000002) {
86 r += 1;
87 }
88 return r;
89 }
90
91 #endif