1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef U_DRAW_H
29 #define U_DRAW_H
30
31
32 #include "util/compiler.h"
33 #include "pipe/p_context.h"
34 #include "pipe/p_state.h"
35
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41
42 static inline void
util_draw_init_info(struct pipe_draw_info * info)43 util_draw_init_info(struct pipe_draw_info *info)
44 {
45 memset(info, 0, sizeof(*info));
46 info->instance_count = 1;
47 info->max_index = 0xffffffff;
48 }
49
50
51 static inline void
util_draw_arrays(struct pipe_context * pipe,enum mesa_prim mode,unsigned start,unsigned count)52 util_draw_arrays(struct pipe_context *pipe,
53 enum mesa_prim mode,
54 unsigned start,
55 unsigned count)
56 {
57 struct pipe_draw_info info;
58 struct pipe_draw_start_count_bias draw;
59
60 util_draw_init_info(&info);
61 info.mode = mode;
62 info.min_index = start;
63 info.max_index = start + count - 1;
64
65 draw.start = start;
66 draw.count = count;
67 draw.index_bias = 0;
68
69 pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
70 }
71
72 static inline void
util_draw_elements(struct pipe_context * pipe,void * indices,unsigned index_size,int index_bias,enum mesa_prim mode,unsigned start,unsigned count)73 util_draw_elements(struct pipe_context *pipe,
74 void *indices,
75 unsigned index_size,
76 int index_bias, enum mesa_prim mode,
77 unsigned start,
78 unsigned count)
79 {
80 struct pipe_draw_info info;
81 struct pipe_draw_start_count_bias draw;
82
83 util_draw_init_info(&info);
84 info.index.user = indices;
85 info.has_user_indices = true;
86 info.index_size = index_size;
87 info.mode = mode;
88 draw.index_bias = index_bias;
89
90 draw.start = start;
91 draw.count = count;
92
93 pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
94 }
95
96 static inline void
util_draw_arrays_instanced(struct pipe_context * pipe,enum mesa_prim mode,unsigned start,unsigned count,unsigned start_instance,unsigned instance_count)97 util_draw_arrays_instanced(struct pipe_context *pipe,
98 enum mesa_prim mode,
99 unsigned start,
100 unsigned count,
101 unsigned start_instance,
102 unsigned instance_count)
103 {
104 struct pipe_draw_info info;
105 struct pipe_draw_start_count_bias draw;
106
107 util_draw_init_info(&info);
108 info.mode = mode;
109 info.start_instance = start_instance;
110 info.instance_count = instance_count;
111 info.index_bounds_valid = true;
112 info.min_index = start;
113 info.max_index = start + count - 1;
114
115 draw.start = start;
116 draw.count = count;
117 draw.index_bias = 0;
118
119 pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
120 }
121
122 static inline void
util_draw_elements_instanced(struct pipe_context * pipe,void * indices,unsigned index_size,int index_bias,enum mesa_prim mode,unsigned start,unsigned count,unsigned start_instance,unsigned instance_count)123 util_draw_elements_instanced(struct pipe_context *pipe,
124 void *indices,
125 unsigned index_size,
126 int index_bias,
127 enum mesa_prim mode,
128 unsigned start,
129 unsigned count,
130 unsigned start_instance,
131 unsigned instance_count)
132 {
133 struct pipe_draw_info info;
134 struct pipe_draw_start_count_bias draw;
135
136 util_draw_init_info(&info);
137 info.index.user = indices;
138 info.has_user_indices = true;
139 info.index_size = index_size;
140 info.mode = mode;
141 draw.index_bias = index_bias;
142 info.start_instance = start_instance;
143 info.instance_count = instance_count;
144
145 draw.start = start;
146 draw.count = count;
147
148 pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
149 }
150
151 struct u_indirect_params {
152 struct pipe_draw_info info;
153 struct pipe_draw_start_count_bias draw;
154 };
155
156 /* caller must free the return value */
157 struct u_indirect_params *
158 util_draw_indirect_read(struct pipe_context *pipe,
159 const struct pipe_draw_info *info_in,
160 const struct pipe_draw_indirect_info *indirect,
161 unsigned *num_draws);
162
163 /* This converts an indirect draw into a direct draw by mapping the indirect
164 * buffer, extracting its arguments, and calling pipe->draw_vbo.
165 */
166 void
167 util_draw_indirect(struct pipe_context *pipe,
168 const struct pipe_draw_info *info,
169 unsigned drawid_offset,
170 const struct pipe_draw_indirect_info *indirect);
171
172 /* Helper to handle multi-draw by splitting into individual draws. You
173 * don't want to call this if num_draws==1
174 */
175 void
176 util_draw_multi(struct pipe_context *pctx, const struct pipe_draw_info *info,
177 unsigned drawid_offset,
178 const struct pipe_draw_indirect_info *indirect,
179 const struct pipe_draw_start_count_bias *draws,
180 unsigned num_draws);
181
182 unsigned
183 util_draw_max_index(
184 const struct pipe_vertex_buffer *vertex_buffers,
185 const struct pipe_vertex_element *vertex_elements,
186 unsigned nr_vertex_elements,
187 const struct pipe_draw_info *info);
188
189
190 #ifdef __cplusplus
191 }
192 #endif
193
194 #endif /* !U_DRAW_H */
195