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