u_upload_mgr: pass alignment to u_upload_data manually
[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 "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_bufferobjects.h"
52 #include "st_cb_xformfb.h"
53 #include "st_debug.h"
54 #include "st_draw.h"
55 #include "st_program.h"
56
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "util/u_format.h"
61 #include "util/u_prim.h"
62 #include "util/u_draw_quad.h"
63 #include "util/u_upload_mgr.h"
64 #include "draw/draw_context.h"
65 #include "cso_cache/cso_context.h"
66
67
68 /**
69 * This is very similar to vbo_all_varyings_in_vbos() but we are
70 * only interested in per-vertex data. See bug 38626.
71 */
72 static GLboolean
73 all_varyings_in_vbos(const struct gl_client_array *arrays[])
74 {
75 GLuint i;
76
77 for (i = 0; i < VERT_ATTRIB_MAX; i++)
78 if (arrays[i]->StrideB &&
79 !arrays[i]->InstanceDivisor &&
80 !_mesa_is_bufferobj(arrays[i]->BufferObj))
81 return GL_FALSE;
82
83 return GL_TRUE;
84 }
85
86
87 /**
88 * Basically, translate Mesa's index buffer information into
89 * a pipe_index_buffer object.
90 * \return TRUE or FALSE for success/failure
91 */
92 static boolean
93 setup_index_buffer(struct st_context *st,
94 const struct _mesa_index_buffer *ib,
95 struct pipe_index_buffer *ibuffer)
96 {
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 }
107 else if (st->indexbuf_uploader) {
108 /* upload indexes from user memory into a real buffer */
109 u_upload_data(st->indexbuf_uploader, 0,
110 ib->count * ibuffer->index_size, 4, ib->ptr,
111 &ibuffer->offset, &ibuffer->buffer);
112 if (!ibuffer->buffer) {
113 /* out of memory */
114 return FALSE;
115 }
116 u_upload_unmap(st->indexbuf_uploader);
117 }
118 else {
119 /* indices are in user space memory */
120 ibuffer->user_buffer = ib->ptr;
121 }
122
123 cso_set_index_buffer(st->cso_context, ibuffer);
124 return TRUE;
125 }
126
127
128 /**
129 * Prior to drawing, check that any uniforms referenced by the
130 * current shader have been set. If a uniform has not been set,
131 * issue a warning.
132 */
133 static void
134 check_uniforms(struct gl_context *ctx)
135 {
136 struct gl_shader_program **shProg = ctx->_Shader->CurrentProgram;
137 unsigned j;
138
139 for (j = 0; j < 3; j++) {
140 unsigned i;
141
142 if (shProg[j] == NULL || !shProg[j]->LinkStatus)
143 continue;
144
145 for (i = 0; i < shProg[j]->NumUniformStorage; i++) {
146 const struct gl_uniform_storage *u = &shProg[j]->UniformStorage[i];
147 if (!u->initialized) {
148 _mesa_warning(ctx,
149 "Using shader with uninitialized uniform: %s",
150 u->name);
151 }
152 }
153 }
154 }
155
156
157 /**
158 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
159 * the corresponding Gallium type.
160 */
161 static unsigned
162 translate_prim(const struct gl_context *ctx, unsigned prim)
163 {
164 /* GL prims should match Gallium prims, spot-check a few */
165 STATIC_ASSERT(GL_POINTS == PIPE_PRIM_POINTS);
166 STATIC_ASSERT(GL_QUADS == PIPE_PRIM_QUADS);
167 STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
168 STATIC_ASSERT(GL_PATCHES == PIPE_PRIM_PATCHES);
169
170 return prim;
171 }
172
173
174 /**
175 * This function gets plugged into the VBO module and is called when
176 * we have something to render.
177 * Basically, translate the information into the format expected by gallium.
178 */
179 void
180 st_draw_vbo(struct gl_context *ctx,
181 const struct _mesa_prim *prims,
182 GLuint nr_prims,
183 const struct _mesa_index_buffer *ib,
184 GLboolean index_bounds_valid,
185 GLuint min_index,
186 GLuint max_index,
187 struct gl_transform_feedback_object *tfb_vertcount,
188 unsigned stream,
189 struct gl_buffer_object *indirect)
190 {
191 struct st_context *st = st_context(ctx);
192 struct pipe_index_buffer ibuffer = {0};
193 struct pipe_draw_info info;
194 const struct gl_client_array **arrays = ctx->Array._DrawArrays;
195 unsigned i;
196
197 /* Mesa core state should have been validated already */
198 assert(ctx->NewState == 0x0);
199
200 /* Validate state. */
201 if (st->dirty.st || ctx->NewDriverState) {
202 st_validate_state(st);
203
204 #if 0
205 if (MESA_VERBOSE & VERBOSE_GLSL) {
206 check_uniforms(ctx);
207 }
208 #else
209 (void) check_uniforms;
210 #endif
211 }
212
213 if (st->vertex_array_out_of_memory) {
214 return;
215 }
216
217 util_draw_init_info(&info);
218
219 if (ib) {
220 /* Get index bounds for user buffers. */
221 if (!index_bounds_valid)
222 if (!all_varyings_in_vbos(arrays))
223 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index,
224 nr_prims);
225
226 if (!setup_index_buffer(st, ib, &ibuffer)) {
227 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBegin/DrawElements/DrawArray");
228 return;
229 }
230
231 info.indexed = TRUE;
232 if (min_index != ~0U && max_index != ~0U) {
233 info.min_index = min_index;
234 info.max_index = max_index;
235 }
236
237 /* The VBO module handles restart for the non-indexed GLDrawArrays
238 * so we only set these fields for indexed drawing:
239 */
240 info.primitive_restart = ctx->Array._PrimitiveRestart;
241 info.restart_index = _mesa_primitive_restart_index(ctx, ib->type);
242 }
243 else {
244 /* Transform feedback drawing is always non-indexed. */
245 /* Set info.count_from_stream_output. */
246 if (tfb_vertcount) {
247 if (!st_transform_feedback_draw_init(tfb_vertcount, stream, &info))
248 return;
249 }
250 }
251
252 if (indirect) {
253 info.indirect = st_buffer_object(indirect)->buffer;
254
255 /* Primitive restart is not handled by the VBO module in this case. */
256 info.primitive_restart = ctx->Array._PrimitiveRestart;
257 info.restart_index = ctx->Array.RestartIndex;
258 }
259
260 /* do actual drawing */
261 for (i = 0; i < nr_prims; i++) {
262 info.mode = translate_prim(ctx, prims[i].mode);
263 info.start = prims[i].start;
264 info.count = prims[i].count;
265 info.start_instance = prims[i].base_instance;
266 info.instance_count = prims[i].num_instances;
267 info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
268 info.index_bias = prims[i].basevertex;
269 info.drawid = prims[i].draw_id;
270 if (!ib) {
271 info.min_index = info.start;
272 info.max_index = info.start + info.count - 1;
273 }
274 info.indirect_offset = prims[i].indirect_offset;
275
276 if (ST_DEBUG & DEBUG_DRAW) {
277 debug_printf("st/draw: mode %s start %u count %u indexed %d\n",
278 u_prim_name(info.mode),
279 info.start,
280 info.count,
281 info.indexed);
282 }
283
284 if (info.count_from_stream_output || info.indirect) {
285 cso_draw_vbo(st->cso_context, &info);
286 }
287 else if (info.primitive_restart) {
288 /* don't trim, restarts might be inside index list */
289 cso_draw_vbo(st->cso_context, &info);
290 }
291 else if (u_trim_pipe_prim(prims[i].mode, &info.count)) {
292 cso_draw_vbo(st->cso_context, &info);
293 }
294 }
295
296 if (ib && st->indexbuf_uploader && !_mesa_is_bufferobj(ib->obj)) {
297 pipe_resource_reference(&ibuffer.buffer, NULL);
298 }
299 }
300
301
302 void
303 st_init_draw(struct st_context *st)
304 {
305 struct gl_context *ctx = st->ctx;
306
307 vbo_set_draw_func(ctx, st_draw_vbo);
308
309 st->draw = draw_create(st->pipe); /* for selection/feedback */
310
311 /* Disable draw options that might convert points/lines to tris, etc.
312 * as that would foul-up feedback/selection mode.
313 */
314 draw_wide_line_threshold(st->draw, 1000.0f);
315 draw_wide_point_threshold(st->draw, 1000.0f);
316 draw_enable_line_stipple(st->draw, FALSE);
317 draw_enable_point_sprites(st->draw, FALSE);
318 }
319
320
321 void
322 st_destroy_draw(struct st_context *st)
323 {
324 draw_destroy(st->draw);
325 }