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 static inline void
util_draw_init_info_with_mode(struct pipe_draw_info * info,enum mesa_prim mode)51 util_draw_init_info_with_mode(struct pipe_draw_info *info,
52 enum mesa_prim mode)
53 {
54 util_draw_init_info(info);
55 #if defined(__GNUC__) /* See conditional definition of struct pipe_draw_info */
56 info->mode = mode;
57 #else
58 info->mode = (uint8_t)mode;
59 #endif
60 }
61
62 static inline void
util_draw_arrays(struct pipe_context * pipe,enum mesa_prim mode,unsigned start,unsigned count)63 util_draw_arrays(struct pipe_context *pipe,
64 enum mesa_prim mode,
65 unsigned start,
66 unsigned count)
67 {
68 struct pipe_draw_info info;
69 struct pipe_draw_start_count_bias draw;
70
71 util_draw_init_info_with_mode(&info, mode);
72 info.min_index = start;
73 info.max_index = start + count - 1;
74
75 draw.start = start;
76 draw.count = count;
77 draw.index_bias = 0;
78
79 pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
80 }
81
82 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)83 util_draw_elements(struct pipe_context *pipe,
84 void *indices,
85 unsigned index_size,
86 int index_bias, enum mesa_prim mode,
87 unsigned start,
88 unsigned count)
89 {
90 struct pipe_draw_info info;
91 struct pipe_draw_start_count_bias draw;
92
93 util_draw_init_info_with_mode(&info, mode);
94 info.index.user = indices;
95 info.has_user_indices = true;
96 info.index_size = (uint16_t)index_size;
97 draw.index_bias = index_bias;
98
99 draw.start = start;
100 draw.count = count;
101
102 pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
103 }
104
105 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)106 util_draw_arrays_instanced(struct pipe_context *pipe,
107 enum mesa_prim mode,
108 unsigned start,
109 unsigned count,
110 unsigned start_instance,
111 unsigned instance_count)
112 {
113 struct pipe_draw_info info;
114 struct pipe_draw_start_count_bias draw;
115
116 util_draw_init_info_with_mode(&info, mode);
117 info.start_instance = start_instance;
118 info.instance_count = instance_count;
119 info.index_bounds_valid = true;
120 info.min_index = start;
121 info.max_index = start + count - 1;
122
123 draw.start = start;
124 draw.count = count;
125 draw.index_bias = 0;
126
127 pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
128 }
129
130 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)131 util_draw_elements_instanced(struct pipe_context *pipe,
132 void *indices,
133 unsigned index_size,
134 int index_bias,
135 enum mesa_prim mode,
136 unsigned start,
137 unsigned count,
138 unsigned start_instance,
139 unsigned instance_count)
140 {
141 struct pipe_draw_info info;
142 struct pipe_draw_start_count_bias draw;
143
144 util_draw_init_info_with_mode(&info, mode);
145 info.index.user = indices;
146 info.has_user_indices = true;
147 info.index_size = (uint16_t)index_size;
148 draw.index_bias = index_bias;
149 info.start_instance = start_instance;
150 info.instance_count = instance_count;
151
152 draw.start = start;
153 draw.count = count;
154
155 pipe->draw_vbo(pipe, &info, 0, NULL, &draw, 1);
156 }
157
158 struct u_indirect_params {
159 struct pipe_draw_info info;
160 struct pipe_draw_start_count_bias draw;
161 };
162
163 /* caller must free the return value */
164 struct u_indirect_params *
165 util_draw_indirect_read(struct pipe_context *pipe,
166 const struct pipe_draw_info *info_in,
167 const struct pipe_draw_indirect_info *indirect,
168 unsigned *num_draws);
169
170 /* This converts an indirect draw into a direct draw by mapping the indirect
171 * buffer, extracting its arguments, and calling pipe->draw_vbo.
172 */
173 void
174 util_draw_indirect(struct pipe_context *pipe,
175 const struct pipe_draw_info *info,
176 unsigned drawid_offset,
177 const struct pipe_draw_indirect_info *indirect);
178
179 /* Helper to handle multi-draw by splitting into individual draws. You
180 * don't want to call this if num_draws==1
181 */
182 void
183 util_draw_multi(struct pipe_context *pctx, const struct pipe_draw_info *info,
184 unsigned drawid_offset,
185 const struct pipe_draw_indirect_info *indirect,
186 const struct pipe_draw_start_count_bias *draws,
187 unsigned num_draws);
188
189 unsigned
190 util_draw_max_index(
191 const struct pipe_vertex_buffer *vertex_buffers,
192 const struct pipe_vertex_element *vertex_elements,
193 unsigned nr_vertex_elements,
194 const struct pipe_draw_info *info);
195
196
197 #ifdef __cplusplus
198 }
199 #endif
200
201 #endif /* !U_DRAW_H */
202