intel/compiler: fix -Wsign-compare warning
[mesa.git] / src / mesa / state_tracker / st_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /*
29 * This file implements the st_draw_vbo() function which is called from
30 * Mesa's VBO module. All point/line/triangle rendering is done through
31 * this function whether the user called glBegin/End, glDrawArrays,
32 * glDrawElements, glEvalMesh, or glCalList, etc.
33 *
34 * Authors:
35 * Keith Whitwell <keithw@vmware.com>
36 */
37
38
39 #include "main/errors.h"
40 #include "main/imports.h"
41 #include "main/image.h"
42 #include "main/bufferobj.h"
43 #include "main/macros.h"
44 #include "main/varray.h"
45
46 #include "compiler/glsl/ir_uniform.h"
47
48 #include "vbo/vbo.h"
49
50 #include "st_context.h"
51 #include "st_atom.h"
52 #include "st_cb_bitmap.h"
53 #include "st_cb_bufferobjects.h"
54 #include "st_cb_xformfb.h"
55 #include "st_debug.h"
56 #include "st_draw.h"
57 #include "st_program.h"
58
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "util/u_inlines.h"
62 #include "util/u_format.h"
63 #include "util/u_prim.h"
64 #include "util/u_draw.h"
65 #include "util/u_upload_mgr.h"
66 #include "draw/draw_context.h"
67 #include "cso_cache/cso_context.h"
68
69
70 /**
71 * Set the restart index.
72 */
73 static void
74 setup_primitive_restart(struct gl_context *ctx, struct pipe_draw_info *info)
75 {
76 if (ctx->Array._PrimitiveRestart) {
77 unsigned index_size = info->index_size;
78
79 info->restart_index =
80 _mesa_primitive_restart_index(ctx, index_size);
81
82 /* Enable primitive restart only when the restart index can have an
83 * effect. This is required for correctness in radeonsi VI support.
84 * Other hardware may also benefit from taking a faster, non-restart path
85 * when possible.
86 */
87 if (index_size == 4 || info->restart_index < (1 << (index_size * 8)))
88 info->primitive_restart = true;
89 }
90 }
91
92
93 /**
94 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
95 * the corresponding Gallium type.
96 */
97 static unsigned
98 translate_prim(const struct gl_context *ctx, unsigned prim)
99 {
100 /* GL prims should match Gallium prims, spot-check a few */
101 STATIC_ASSERT(GL_POINTS == PIPE_PRIM_POINTS);
102 STATIC_ASSERT(GL_QUADS == PIPE_PRIM_QUADS);
103 STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
104 STATIC_ASSERT(GL_PATCHES == PIPE_PRIM_PATCHES);
105
106 return prim;
107 }
108
109 static inline void
110 prepare_draw(struct st_context *st, struct gl_context *ctx)
111 {
112 /* Mesa core state should have been validated already */
113 assert(ctx->NewState == 0x0);
114
115 if (unlikely(!st->bitmap.cache.empty))
116 st_flush_bitmap_cache(st);
117
118 st_invalidate_readpix_cache(st);
119
120 /* Validate state. */
121 if ((st->dirty | ctx->NewDriverState) & ST_PIPELINE_RENDER_STATE_MASK ||
122 st->gfx_shaders_may_be_dirty) {
123 st_validate_state(st, ST_PIPELINE_RENDER);
124 }
125 }
126
127 /**
128 * This function gets plugged into the VBO module and is called when
129 * we have something to render.
130 * Basically, translate the information into the format expected by gallium.
131 */
132 static void
133 st_draw_vbo(struct gl_context *ctx,
134 const struct _mesa_prim *prims,
135 GLuint nr_prims,
136 const struct _mesa_index_buffer *ib,
137 GLboolean index_bounds_valid,
138 GLuint min_index,
139 GLuint max_index,
140 struct gl_transform_feedback_object *tfb_vertcount,
141 unsigned stream,
142 struct gl_buffer_object *indirect)
143 {
144 struct st_context *st = st_context(ctx);
145 struct pipe_draw_info info;
146 unsigned i;
147 unsigned start = 0;
148
149 prepare_draw(st, ctx);
150
151 if (st->vertex_array_out_of_memory)
152 return;
153
154 /* Initialize pipe_draw_info. */
155 info.primitive_restart = false;
156 info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
157 info.indirect = NULL;
158 info.count_from_stream_output = NULL;
159 info.restart_index = 0;
160
161 if (ib) {
162 struct gl_buffer_object *bufobj = ib->obj;
163
164 /* Get index bounds for user buffers. */
165 if (!index_bounds_valid && st->draw_needs_minmax_index) {
166 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index,
167 nr_prims);
168 }
169
170 info.index_size = ib->index_size;
171 info.min_index = min_index;
172 info.max_index = max_index;
173
174 if (_mesa_is_bufferobj(bufobj)) {
175 /* indices are in a real VBO */
176 info.has_user_indices = false;
177 info.index.resource = st_buffer_object(bufobj)->buffer;
178
179 /* Return if the bound element array buffer doesn't have any backing
180 * storage. (nothing to do)
181 */
182 if (!info.index.resource)
183 return;
184
185 start = pointer_to_offset(ib->ptr) / info.index_size;
186 } else {
187 /* indices are in user space memory */
188 info.has_user_indices = true;
189 info.index.user = ib->ptr;
190 }
191
192 setup_primitive_restart(ctx, &info);
193 }
194 else {
195 info.index_size = 0;
196 info.has_user_indices = false;
197
198 /* Transform feedback drawing is always non-indexed. */
199 /* Set info.count_from_stream_output. */
200 if (tfb_vertcount) {
201 if (!st_transform_feedback_draw_init(tfb_vertcount, stream, &info))
202 return;
203 }
204 }
205
206 assert(!indirect);
207
208 /* do actual drawing */
209 for (i = 0; i < nr_prims; i++) {
210 info.count = prims[i].count;
211
212 /* Skip no-op draw calls. */
213 if (!info.count && !tfb_vertcount)
214 continue;
215
216 info.mode = translate_prim(ctx, prims[i].mode);
217 info.start = start + prims[i].start;
218 info.start_instance = prims[i].base_instance;
219 info.instance_count = prims[i].num_instances;
220 info.index_bias = prims[i].basevertex;
221 info.drawid = prims[i].draw_id;
222 if (!ib) {
223 info.min_index = info.start;
224 info.max_index = info.start + info.count - 1;
225 }
226
227 if (ST_DEBUG & DEBUG_DRAW) {
228 debug_printf("st/draw: mode %s start %u count %u index_size %d\n",
229 u_prim_name(info.mode),
230 info.start,
231 info.count,
232 info.index_size);
233 }
234
235 /* Don't call u_trim_pipe_prim. Drivers should do it if they need it. */
236 cso_draw_vbo(st->cso_context, &info);
237 }
238 }
239
240 static void
241 st_indirect_draw_vbo(struct gl_context *ctx,
242 GLuint mode,
243 struct gl_buffer_object *indirect_data,
244 GLsizeiptr indirect_offset,
245 unsigned draw_count,
246 unsigned stride,
247 struct gl_buffer_object *indirect_draw_count,
248 GLsizeiptr indirect_draw_count_offset,
249 const struct _mesa_index_buffer *ib)
250 {
251 struct st_context *st = st_context(ctx);
252 struct pipe_draw_info info;
253 struct pipe_draw_indirect_info indirect;
254
255 assert(stride);
256 prepare_draw(st, ctx);
257
258 if (st->vertex_array_out_of_memory)
259 return;
260
261 memset(&indirect, 0, sizeof(indirect));
262 util_draw_init_info(&info);
263 info.start = 0; /* index offset / index size */
264
265 if (ib) {
266 struct gl_buffer_object *bufobj = ib->obj;
267
268 /* indices are always in a real VBO */
269 assert(_mesa_is_bufferobj(bufobj));
270
271 info.index_size = ib->index_size;
272 info.index.resource = st_buffer_object(bufobj)->buffer;
273 info.start = pointer_to_offset(ib->ptr) / info.index_size;
274
275 /* Primitive restart is not handled by the VBO module in this case. */
276 setup_primitive_restart(ctx, &info);
277 }
278
279 info.mode = translate_prim(ctx, mode);
280 info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
281 info.indirect = &indirect;
282 indirect.buffer = st_buffer_object(indirect_data)->buffer;
283 indirect.offset = indirect_offset;
284
285 if (ST_DEBUG & DEBUG_DRAW) {
286 debug_printf("st/draw indirect: mode %s drawcount %d index_size %d\n",
287 u_prim_name(info.mode),
288 draw_count,
289 info.index_size);
290 }
291
292 if (!st->has_multi_draw_indirect) {
293 int i;
294
295 assert(!indirect_draw_count);
296 indirect.draw_count = 1;
297 for (i = 0; i < draw_count; i++) {
298 info.drawid = i;
299 cso_draw_vbo(st->cso_context, &info);
300 indirect.offset += stride;
301 }
302 } else {
303 indirect.draw_count = draw_count;
304 indirect.stride = stride;
305 if (indirect_draw_count) {
306 indirect.indirect_draw_count =
307 st_buffer_object(indirect_draw_count)->buffer;
308 indirect.indirect_draw_count_offset = indirect_draw_count_offset;
309 }
310 cso_draw_vbo(st->cso_context, &info);
311 }
312 }
313
314
315 void
316 st_init_draw_functions(struct dd_function_table *functions)
317 {
318 functions->Draw = st_draw_vbo;
319 functions->DrawIndirect = st_indirect_draw_vbo;
320 }
321
322
323 void
324 st_destroy_draw(struct st_context *st)
325 {
326 draw_destroy(st->draw);
327 }
328
329 /**
330 * Getter for the draw_context, so that initialization of it can happen only
331 * when needed (the TGSI exec machines take up quite a bit of memory).
332 */
333 struct draw_context *
334 st_get_draw_context(struct st_context *st)
335 {
336 if (!st->draw) {
337 st->draw = draw_create(st->pipe);
338 if (!st->draw) {
339 _mesa_error(st->ctx, GL_OUT_OF_MEMORY, "feedback fallback allocation");
340 return NULL;
341 }
342 }
343
344 /* Disable draw options that might convert points/lines to tris, etc.
345 * as that would foul-up feedback/selection mode.
346 */
347 draw_wide_line_threshold(st->draw, 1000.0f);
348 draw_wide_point_threshold(st->draw, 1000.0f);
349 draw_enable_line_stipple(st->draw, FALSE);
350 draw_enable_point_sprites(st->draw, FALSE);
351
352 return st->draw;
353 }
354
355 /**
356 * Draw a quad with given position, texcoords and color.
357 */
358 bool
359 st_draw_quad(struct st_context *st,
360 float x0, float y0, float x1, float y1, float z,
361 float s0, float t0, float s1, float t1,
362 const float *color,
363 unsigned num_instances)
364 {
365 struct pipe_vertex_buffer vb = {0};
366 struct st_util_vertex *verts;
367
368 vb.stride = sizeof(struct st_util_vertex);
369
370 u_upload_alloc(st->pipe->stream_uploader, 0,
371 4 * sizeof(struct st_util_vertex), 4,
372 &vb.buffer_offset, &vb.buffer.resource, (void **) &verts);
373 if (!vb.buffer.resource) {
374 return false;
375 }
376
377 /* lower-left */
378 verts[0].x = x0;
379 verts[0].y = y1;
380 verts[0].z = z;
381 verts[0].r = color[0];
382 verts[0].g = color[1];
383 verts[0].b = color[2];
384 verts[0].a = color[3];
385 verts[0].s = s0;
386 verts[0].t = t0;
387
388 /* lower-right */
389 verts[1].x = x1;
390 verts[1].y = y1;
391 verts[1].z = z;
392 verts[1].r = color[0];
393 verts[1].g = color[1];
394 verts[1].b = color[2];
395 verts[1].a = color[3];
396 verts[1].s = s1;
397 verts[1].t = t0;
398
399 /* upper-right */
400 verts[2].x = x1;
401 verts[2].y = y0;
402 verts[2].z = z;
403 verts[2].r = color[0];
404 verts[2].g = color[1];
405 verts[2].b = color[2];
406 verts[2].a = color[3];
407 verts[2].s = s1;
408 verts[2].t = t1;
409
410 /* upper-left */
411 verts[3].x = x0;
412 verts[3].y = y0;
413 verts[3].z = z;
414 verts[3].r = color[0];
415 verts[3].g = color[1];
416 verts[3].b = color[2];
417 verts[3].a = color[3];
418 verts[3].s = s0;
419 verts[3].t = t1;
420
421 u_upload_unmap(st->pipe->stream_uploader);
422
423 cso_set_vertex_buffers(st->cso_context, 0, 1, &vb);
424
425 if (num_instances > 1) {
426 cso_draw_arrays_instanced(st->cso_context, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
427 0, num_instances);
428 } else {
429 cso_draw_arrays(st->cso_context, PIPE_PRIM_TRIANGLE_FAN, 0, 4);
430 }
431
432 pipe_resource_reference(&vb.buffer.resource, NULL);
433
434 return true;
435 }