1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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.
35 * Keith Whitwell <keithw@vmware.com>
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"
45 #include "compiler/glsl/ir_uniform.h"
49 #include "st_context.h"
51 #include "st_cb_bitmap.h"
52 #include "st_cb_bufferobjects.h"
53 #include "st_cb_xformfb.h"
56 #include "st_program.h"
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"
70 * This is very similar to vbo_all_varyings_in_vbos() but we are
71 * only interested in per-vertex data. See bug 38626.
74 all_varyings_in_vbos(const struct gl_client_array
*arrays
[])
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
))
89 * Basically, translate Mesa's index buffer information into
90 * a pipe_index_buffer object.
91 * \return TRUE or FALSE for success/failure
94 setup_index_buffer(struct st_context
*st
,
95 const struct _mesa_index_buffer
*ib
,
96 struct pipe_index_buffer
*ibuffer
)
98 struct gl_buffer_object
*bufobj
= ib
->obj
;
100 ibuffer
->index_size
= vbo_sizeof_ib_type(ib
->type
);
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
);
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
) {
117 u_upload_unmap(st
->indexbuf_uploader
);
120 /* indices are in user space memory */
121 ibuffer
->user_buffer
= ib
->ptr
;
124 cso_set_index_buffer(st
->cso_context
, ibuffer
);
130 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
131 * the corresponding Gallium type.
134 translate_prim(const struct gl_context
*ctx
, unsigned prim
)
136 /* GL prims should match Gallium prims, spot-check a few */
137 STATIC_ASSERT(GL_POINTS
== PIPE_PRIM_POINTS
);
138 STATIC_ASSERT(GL_QUADS
== PIPE_PRIM_QUADS
);
139 STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY
== PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
);
140 STATIC_ASSERT(GL_PATCHES
== PIPE_PRIM_PATCHES
);
147 * This function gets plugged into the VBO module and is called when
148 * we have something to render.
149 * Basically, translate the information into the format expected by gallium.
152 st_draw_vbo(struct gl_context
*ctx
,
153 const struct _mesa_prim
*prims
,
155 const struct _mesa_index_buffer
*ib
,
156 GLboolean index_bounds_valid
,
159 struct gl_transform_feedback_object
*tfb_vertcount
,
161 struct gl_buffer_object
*indirect
)
163 struct st_context
*st
= st_context(ctx
);
164 struct pipe_index_buffer ibuffer
= {0};
165 struct pipe_draw_info info
;
166 const struct gl_client_array
**arrays
= ctx
->Array
._DrawArrays
;
169 /* Mesa core state should have been validated already */
170 assert(ctx
->NewState
== 0x0);
172 st_flush_bitmap_cache(st
);
173 st_invalidate_readpix_cache(st
);
175 /* Validate state. */
176 if (st
->dirty
.st
|| st
->dirty
.mesa
|| ctx
->NewDriverState
) {
177 st_validate_state(st
, ST_PIPELINE_RENDER
);
180 if (st
->vertex_array_out_of_memory
) {
184 util_draw_init_info(&info
);
187 /* Get index bounds for user buffers. */
188 if (!index_bounds_valid
)
189 if (!all_varyings_in_vbos(arrays
))
190 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
,
193 if (!setup_index_buffer(st
, ib
, &ibuffer
)) {
194 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBegin/DrawElements/DrawArray");
199 if (min_index
!= ~0U && max_index
!= ~0U) {
200 info
.min_index
= min_index
;
201 info
.max_index
= max_index
;
204 /* The VBO module handles restart for the non-indexed GLDrawArrays
205 * so we only set these fields for indexed drawing:
207 info
.primitive_restart
= ctx
->Array
._PrimitiveRestart
;
208 info
.restart_index
= _mesa_primitive_restart_index(ctx
, ib
->type
);
211 /* Transform feedback drawing is always non-indexed. */
212 /* Set info.count_from_stream_output. */
214 if (!st_transform_feedback_draw_init(tfb_vertcount
, stream
, &info
))
221 /* do actual drawing */
222 for (i
= 0; i
< nr_prims
; i
++) {
223 info
.mode
= translate_prim(ctx
, prims
[i
].mode
);
224 info
.start
= prims
[i
].start
;
225 info
.count
= prims
[i
].count
;
226 info
.start_instance
= prims
[i
].base_instance
;
227 info
.instance_count
= prims
[i
].num_instances
;
228 info
.vertices_per_patch
= ctx
->TessCtrlProgram
.patch_vertices
;
229 info
.index_bias
= prims
[i
].basevertex
;
230 info
.drawid
= prims
[i
].draw_id
;
232 info
.min_index
= info
.start
;
233 info
.max_index
= info
.start
+ info
.count
- 1;
236 if (ST_DEBUG
& DEBUG_DRAW
) {
237 debug_printf("st/draw: mode %s start %u count %u indexed %d\n",
238 u_prim_name(info
.mode
),
244 if (info
.count_from_stream_output
) {
245 cso_draw_vbo(st
->cso_context
, &info
);
247 else if (info
.primitive_restart
) {
248 /* don't trim, restarts might be inside index list */
249 cso_draw_vbo(st
->cso_context
, &info
);
251 else if (u_trim_pipe_prim(prims
[i
].mode
, &info
.count
)) {
252 cso_draw_vbo(st
->cso_context
, &info
);
256 if (ib
&& st
->indexbuf_uploader
&& !_mesa_is_bufferobj(ib
->obj
)) {
257 pipe_resource_reference(&ibuffer
.buffer
, NULL
);
262 st_indirect_draw_vbo(struct gl_context
*ctx
,
264 struct gl_buffer_object
*indirect_data
,
265 GLsizeiptr indirect_offset
,
268 struct gl_buffer_object
*indirect_params
,
269 GLsizeiptr indirect_params_offset
,
270 const struct _mesa_index_buffer
*ib
)
272 struct st_context
*st
= st_context(ctx
);
273 struct pipe_index_buffer ibuffer
= {0};
274 struct pipe_draw_info info
;
276 /* Mesa core state should have been validated already */
277 assert(ctx
->NewState
== 0x0);
280 /* Validate state. */
281 if (st
->dirty
.st
|| st
->dirty
.mesa
|| ctx
->NewDriverState
) {
282 st_validate_state(st
, ST_PIPELINE_RENDER
);
285 if (st
->vertex_array_out_of_memory
) {
289 util_draw_init_info(&info
);
292 if (!setup_index_buffer(st
, ib
, &ibuffer
)) {
293 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDrawElementsIndirect%s",
294 (draw_count
> 1) ? "Multi" : "",
295 indirect_params
? "CountARB" : "");
302 info
.mode
= translate_prim(ctx
, mode
);
303 info
.vertices_per_patch
= ctx
->TessCtrlProgram
.patch_vertices
;
304 info
.indirect
= st_buffer_object(indirect_data
)->buffer
;
305 info
.indirect_offset
= indirect_offset
;
307 /* Primitive restart is not handled by the VBO module in this case. */
308 info
.primitive_restart
= ctx
->Array
._PrimitiveRestart
;
309 info
.restart_index
= ctx
->Array
.RestartIndex
;
311 if (ST_DEBUG
& DEBUG_DRAW
) {
312 debug_printf("st/draw indirect: mode %s drawcount %d indexed %d\n",
313 u_prim_name(info
.mode
),
318 if (!st
->has_multi_draw_indirect
) {
321 assert(!indirect_params
);
322 info
.indirect_count
= 1;
323 for (i
= 0; i
< draw_count
; i
++) {
325 cso_draw_vbo(st
->cso_context
, &info
);
326 info
.indirect_offset
+= stride
;
329 info
.indirect_count
= draw_count
;
330 info
.indirect_stride
= stride
;
331 if (indirect_params
) {
332 info
.indirect_params
= st_buffer_object(indirect_params
)->buffer
;
333 info
.indirect_params_offset
= indirect_params_offset
;
335 cso_draw_vbo(st
->cso_context
, &info
);
341 st_init_draw(struct st_context
*st
)
343 struct gl_context
*ctx
= st
->ctx
;
345 vbo_set_draw_func(ctx
, st_draw_vbo
);
346 vbo_set_indirect_draw_func(ctx
, st_indirect_draw_vbo
);
348 st
->draw
= draw_create(st
->pipe
); /* for selection/feedback */
350 /* Disable draw options that might convert points/lines to tris, etc.
351 * as that would foul-up feedback/selection mode.
353 draw_wide_line_threshold(st
->draw
, 1000.0f
);
354 draw_wide_point_threshold(st
->draw
, 1000.0f
);
355 draw_enable_line_stipple(st
->draw
, FALSE
);
356 draw_enable_point_sprites(st
->draw
, FALSE
);
361 st_destroy_draw(struct st_context
*st
)
363 draw_destroy(st
->draw
);
368 * Draw a quad with given position, texcoords and color.
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
,
375 unsigned num_instances
)
377 struct pipe_vertex_buffer vb
= {0};
378 struct st_util_vertex
*verts
;
380 vb
.stride
= sizeof(struct st_util_vertex
);
382 u_upload_alloc(st
->uploader
, 0, 4 * sizeof(struct st_util_vertex
), 4,
383 &vb
.buffer_offset
, &vb
.buffer
, (void **) &verts
);
392 verts
[0].r
= color
[0];
393 verts
[0].g
= color
[1];
394 verts
[0].b
= color
[2];
395 verts
[0].a
= color
[3];
403 verts
[1].r
= color
[0];
404 verts
[1].g
= color
[1];
405 verts
[1].b
= color
[2];
406 verts
[1].a
= color
[3];
414 verts
[2].r
= color
[0];
415 verts
[2].g
= color
[1];
416 verts
[2].b
= color
[2];
417 verts
[2].a
= color
[3];
425 verts
[3].r
= color
[0];
426 verts
[3].g
= color
[1];
427 verts
[3].b
= color
[2];
428 verts
[3].a
= color
[3];
432 u_upload_unmap(st
->uploader
);
434 /* At the time of writing, cso_get_aux_vertex_buffer_slot() always returns
435 * zero. If that ever changes we need to audit the calls to that function
436 * and make sure the slot number is used consistently everywhere.
438 assert(cso_get_aux_vertex_buffer_slot(st
->cso_context
) == 0);
440 cso_set_vertex_buffers(st
->cso_context
,
441 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
444 if (num_instances
> 1) {
445 cso_draw_arrays_instanced(st
->cso_context
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
448 cso_draw_arrays(st
->cso_context
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4);
451 pipe_resource_reference(&vb
.buffer
, NULL
);