Merge commit mesa-public/master into vulkan
[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_quad.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_client_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 * \return TRUE or FALSE for success/failure
92 */
93 static boolean
94 setup_index_buffer(struct st_context *st,
95 const struct _mesa_index_buffer *ib,
96 struct pipe_index_buffer *ibuffer)
97 {
98 struct gl_buffer_object *bufobj = ib->obj;
99
100 ibuffer->index_size = vbo_sizeof_ib_type(ib->type);
101
102 /* get/create the index buffer object */
103 if (_mesa_is_bufferobj(bufobj)) {
104 /* indices are in a real VBO */
105 ibuffer->buffer = st_buffer_object(bufobj)->buffer;
106 ibuffer->offset = pointer_to_offset(ib->ptr);
107 }
108 else if (st->indexbuf_uploader) {
109 /* upload indexes from user memory into a real buffer */
110 u_upload_data(st->indexbuf_uploader, 0,
111 ib->count * ibuffer->index_size, 4, ib->ptr,
112 &ibuffer->offset, &ibuffer->buffer);
113 if (!ibuffer->buffer) {
114 /* out of memory */
115 return FALSE;
116 }
117 u_upload_unmap(st->indexbuf_uploader);
118 }
119 else {
120 /* indices are in user space memory */
121 ibuffer->user_buffer = ib->ptr;
122 }
123
124 cso_set_index_buffer(st->cso_context, ibuffer);
125 return TRUE;
126 }
127
128
129 /**
130 * Prior to drawing, check that any uniforms referenced by the
131 * current shader have been set. If a uniform has not been set,
132 * issue a warning.
133 */
134 static void
135 check_uniforms(struct gl_context *ctx)
136 {
137 struct gl_shader_program **shProg = ctx->_Shader->CurrentProgram;
138 unsigned j;
139
140 for (j = 0; j < 3; j++) {
141 unsigned i;
142
143 if (shProg[j] == NULL || !shProg[j]->LinkStatus)
144 continue;
145
146 for (i = 0; i < shProg[j]->NumUniformStorage; i++) {
147 const struct gl_uniform_storage *u = &shProg[j]->UniformStorage[i];
148 if (!u->initialized) {
149 _mesa_warning(ctx,
150 "Using shader with uninitialized uniform: %s",
151 u->name);
152 }
153 }
154 }
155 }
156
157
158 /**
159 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
160 * the corresponding Gallium type.
161 */
162 static unsigned
163 translate_prim(const struct gl_context *ctx, unsigned prim)
164 {
165 /* GL prims should match Gallium prims, spot-check a few */
166 STATIC_ASSERT(GL_POINTS == PIPE_PRIM_POINTS);
167 STATIC_ASSERT(GL_QUADS == PIPE_PRIM_QUADS);
168 STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
169 STATIC_ASSERT(GL_PATCHES == PIPE_PRIM_PATCHES);
170
171 return prim;
172 }
173
174
175 /**
176 * This function gets plugged into the VBO module and is called when
177 * we have something to render.
178 * Basically, translate the information into the format expected by gallium.
179 */
180 void
181 st_draw_vbo(struct gl_context *ctx,
182 const struct _mesa_prim *prims,
183 GLuint nr_prims,
184 const struct _mesa_index_buffer *ib,
185 GLboolean index_bounds_valid,
186 GLuint min_index,
187 GLuint max_index,
188 struct gl_transform_feedback_object *tfb_vertcount,
189 unsigned stream,
190 struct gl_buffer_object *indirect)
191 {
192 struct st_context *st = st_context(ctx);
193 struct pipe_index_buffer ibuffer = {0};
194 struct pipe_draw_info info;
195 const struct gl_client_array **arrays = ctx->Array._DrawArrays;
196 unsigned i;
197
198 /* Mesa core state should have been validated already */
199 assert(ctx->NewState == 0x0);
200
201 st_flush_bitmap_cache(st);
202
203 /* Validate state. */
204 if (st->dirty.st || ctx->NewDriverState) {
205 st_validate_state(st);
206
207 #if 0
208 if (MESA_VERBOSE & VERBOSE_GLSL) {
209 check_uniforms(ctx);
210 }
211 #else
212 (void) check_uniforms;
213 #endif
214 }
215
216 if (st->vertex_array_out_of_memory) {
217 return;
218 }
219
220 util_draw_init_info(&info);
221
222 if (ib) {
223 /* Get index bounds for user buffers. */
224 if (!index_bounds_valid)
225 if (!all_varyings_in_vbos(arrays))
226 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index,
227 nr_prims);
228
229 if (!setup_index_buffer(st, ib, &ibuffer)) {
230 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBegin/DrawElements/DrawArray");
231 return;
232 }
233
234 info.indexed = TRUE;
235 if (min_index != ~0U && max_index != ~0U) {
236 info.min_index = min_index;
237 info.max_index = max_index;
238 }
239
240 /* The VBO module handles restart for the non-indexed GLDrawArrays
241 * so we only set these fields for indexed drawing:
242 */
243 info.primitive_restart = ctx->Array._PrimitiveRestart;
244 info.restart_index = _mesa_primitive_restart_index(ctx, ib->type);
245 }
246 else {
247 /* Transform feedback drawing is always non-indexed. */
248 /* Set info.count_from_stream_output. */
249 if (tfb_vertcount) {
250 if (!st_transform_feedback_draw_init(tfb_vertcount, stream, &info))
251 return;
252 }
253 }
254
255 assert(!indirect);
256
257 /* do actual drawing */
258 for (i = 0; i < nr_prims; i++) {
259 info.mode = translate_prim(ctx, prims[i].mode);
260 info.start = prims[i].start;
261 info.count = prims[i].count;
262 info.start_instance = prims[i].base_instance;
263 info.instance_count = prims[i].num_instances;
264 info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
265 info.index_bias = prims[i].basevertex;
266 info.drawid = prims[i].draw_id;
267 if (!ib) {
268 info.min_index = info.start;
269 info.max_index = info.start + info.count - 1;
270 }
271
272 if (ST_DEBUG & DEBUG_DRAW) {
273 debug_printf("st/draw: mode %s start %u count %u indexed %d\n",
274 u_prim_name(info.mode),
275 info.start,
276 info.count,
277 info.indexed);
278 }
279
280 if (info.count_from_stream_output) {
281 cso_draw_vbo(st->cso_context, &info);
282 }
283 else if (info.primitive_restart) {
284 /* don't trim, restarts might be inside index list */
285 cso_draw_vbo(st->cso_context, &info);
286 }
287 else if (u_trim_pipe_prim(prims[i].mode, &info.count)) {
288 cso_draw_vbo(st->cso_context, &info);
289 }
290 }
291
292 if (ib && st->indexbuf_uploader && !_mesa_is_bufferobj(ib->obj)) {
293 pipe_resource_reference(&ibuffer.buffer, NULL);
294 }
295 }
296
297 static void
298 st_indirect_draw_vbo(struct gl_context *ctx,
299 GLuint mode,
300 struct gl_buffer_object *indirect_data,
301 GLsizeiptr indirect_offset,
302 unsigned draw_count,
303 unsigned stride,
304 struct gl_buffer_object *indirect_params,
305 GLsizeiptr indirect_params_offset,
306 const struct _mesa_index_buffer *ib)
307 {
308 struct st_context *st = st_context(ctx);
309 struct pipe_index_buffer ibuffer = {0};
310 struct pipe_draw_info info;
311
312 /* Mesa core state should have been validated already */
313 assert(ctx->NewState == 0x0);
314 assert(stride);
315
316 /* Validate state. */
317 if (st->dirty.st || ctx->NewDriverState) {
318 st_validate_state(st);
319 }
320
321 if (st->vertex_array_out_of_memory) {
322 return;
323 }
324
325 util_draw_init_info(&info);
326
327 if (ib) {
328 if (!setup_index_buffer(st, ib, &ibuffer)) {
329 _mesa_error(ctx, GL_OUT_OF_MEMORY, "gl%sDrawElementsIndirect%s",
330 (draw_count > 1) ? "Multi" : "",
331 indirect_params ? "CountARB" : "");
332 return;
333 }
334
335 info.indexed = TRUE;
336 }
337
338 info.mode = translate_prim(ctx, mode);
339 info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
340 info.indirect = st_buffer_object(indirect_data)->buffer;
341 info.indirect_offset = indirect_offset;
342
343 /* Primitive restart is not handled by the VBO module in this case. */
344 info.primitive_restart = ctx->Array._PrimitiveRestart;
345 info.restart_index = ctx->Array.RestartIndex;
346
347 if (ST_DEBUG & DEBUG_DRAW) {
348 debug_printf("st/draw indirect: mode %s drawcount %d indexed %d\n",
349 u_prim_name(info.mode),
350 draw_count,
351 info.indexed);
352 }
353
354 if (!st->has_multi_draw_indirect) {
355 int i;
356
357 assert(!indirect_params);
358 info.indirect_count = 1;
359 for (i = 0; i < draw_count; i++) {
360 info.drawid = i;
361 cso_draw_vbo(st->cso_context, &info);
362 info.indirect_offset += stride;
363 }
364 } else {
365 info.indirect_count = draw_count;
366 info.indirect_stride = stride;
367 if (indirect_params) {
368 info.indirect_params = st_buffer_object(indirect_params)->buffer;
369 info.indirect_params_offset = indirect_params_offset;
370 }
371 cso_draw_vbo(st->cso_context, &info);
372 }
373 }
374
375
376 void
377 st_init_draw(struct st_context *st)
378 {
379 struct gl_context *ctx = st->ctx;
380
381 vbo_set_draw_func(ctx, st_draw_vbo);
382 vbo_set_indirect_draw_func(ctx, st_indirect_draw_vbo);
383
384 st->draw = draw_create(st->pipe); /* for selection/feedback */
385
386 /* Disable draw options that might convert points/lines to tris, etc.
387 * as that would foul-up feedback/selection mode.
388 */
389 draw_wide_line_threshold(st->draw, 1000.0f);
390 draw_wide_point_threshold(st->draw, 1000.0f);
391 draw_enable_line_stipple(st->draw, FALSE);
392 draw_enable_point_sprites(st->draw, FALSE);
393 }
394
395
396 void
397 st_destroy_draw(struct st_context *st)
398 {
399 draw_destroy(st->draw);
400 }