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