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_vertex_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.
93 setup_index_buffer(struct st_context
*st
,
94 const struct _mesa_index_buffer
*ib
)
96 struct pipe_index_buffer ibuffer
;
97 struct gl_buffer_object
*bufobj
= ib
->obj
;
99 ibuffer
.index_size
= ib
->index_size
;
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
;
109 /* indices are in user space memory */
110 ibuffer
.buffer
= NULL
;
112 ibuffer
.user_buffer
= ib
->ptr
;
115 cso_set_index_buffer(st
->cso_context
, &ibuffer
);
120 * Set the restart index.
123 setup_primitive_restart(struct gl_context
*ctx
, struct pipe_draw_info
*info
,
126 if (ctx
->Array
._PrimitiveRestart
) {
127 info
->restart_index
=
128 _mesa_primitive_restart_index(ctx
, index_size
);
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
135 if (index_size
== 4 || info
->restart_index
< (1 << (index_size
* 8)))
136 info
->primitive_restart
= true;
142 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
143 * the corresponding Gallium type.
146 translate_prim(const struct gl_context
*ctx
, unsigned prim
)
148 /* GL prims should match Gallium prims, spot-check a few */
149 STATIC_ASSERT(GL_POINTS
== PIPE_PRIM_POINTS
);
150 STATIC_ASSERT(GL_QUADS
== PIPE_PRIM_QUADS
);
151 STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY
== PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
);
152 STATIC_ASSERT(GL_PATCHES
== PIPE_PRIM_PATCHES
);
159 * This function gets plugged into the VBO module and is called when
160 * we have something to render.
161 * Basically, translate the information into the format expected by gallium.
164 st_draw_vbo(struct gl_context
*ctx
,
165 const struct _mesa_prim
*prims
,
167 const struct _mesa_index_buffer
*ib
,
168 GLboolean index_bounds_valid
,
171 struct gl_transform_feedback_object
*tfb_vertcount
,
173 struct gl_buffer_object
*indirect
)
175 struct st_context
*st
= st_context(ctx
);
176 struct pipe_draw_info info
;
177 const struct gl_vertex_array
**arrays
= ctx
->Array
._DrawArrays
;
180 /* Mesa core state should have been validated already */
181 assert(ctx
->NewState
== 0x0);
183 if (unlikely(!st
->bitmap
.cache
.empty
))
184 st_flush_bitmap_cache(st
);
186 st_invalidate_readpix_cache(st
);
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
);
194 if (st
->vertex_array_out_of_memory
) {
198 util_draw_init_info(&info
);
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
,
207 setup_index_buffer(st
, ib
);
210 info
.min_index
= min_index
;
211 info
.max_index
= max_index
;
213 setup_primitive_restart(ctx
, &info
, ib
->index_size
);
216 /* Transform feedback drawing is always non-indexed. */
217 /* Set info.count_from_stream_output. */
219 if (!st_transform_feedback_draw_init(tfb_vertcount
, stream
, &info
))
226 /* do actual drawing */
227 for (i
= 0; i
< nr_prims
; i
++) {
228 info
.mode
= translate_prim(ctx
, prims
[i
].mode
);
229 info
.start
= prims
[i
].start
;
230 info
.count
= prims
[i
].count
;
231 info
.start_instance
= prims
[i
].base_instance
;
232 info
.instance_count
= prims
[i
].num_instances
;
233 info
.vertices_per_patch
= ctx
->TessCtrlProgram
.patch_vertices
;
234 info
.index_bias
= prims
[i
].basevertex
;
235 info
.drawid
= prims
[i
].draw_id
;
237 info
.min_index
= info
.start
;
238 info
.max_index
= info
.start
+ info
.count
- 1;
241 if (ST_DEBUG
& DEBUG_DRAW
) {
242 debug_printf("st/draw: mode %s start %u count %u indexed %d\n",
243 u_prim_name(info
.mode
),
249 /* Don't call u_trim_pipe_prim. Drivers should do it if they need it. */
250 cso_draw_vbo(st
->cso_context
, &info
);
255 st_indirect_draw_vbo(struct gl_context
*ctx
,
257 struct gl_buffer_object
*indirect_data
,
258 GLsizeiptr indirect_offset
,
261 struct gl_buffer_object
*indirect_params
,
262 GLsizeiptr indirect_params_offset
,
263 const struct _mesa_index_buffer
*ib
)
265 struct st_context
*st
= st_context(ctx
);
266 struct pipe_draw_info info
;
267 struct pipe_draw_indirect_info indirect
;
269 /* Mesa core state should have been validated already */
270 assert(ctx
->NewState
== 0x0);
273 st_invalidate_readpix_cache(st
);
275 /* Validate state. */
276 if ((st
->dirty
| ctx
->NewDriverState
) & ST_PIPELINE_RENDER_STATE_MASK
||
277 st
->gfx_shaders_may_be_dirty
) {
278 st_validate_state(st
, ST_PIPELINE_RENDER
);
281 if (st
->vertex_array_out_of_memory
) {
285 memset(&indirect
, 0, sizeof(indirect
));
286 util_draw_init_info(&info
);
289 setup_index_buffer(st
, ib
);
293 /* Primitive restart is not handled by the VBO module in this case. */
294 setup_primitive_restart(ctx
, &info
, ib
->index_size
);
297 info
.mode
= translate_prim(ctx
, mode
);
298 info
.vertices_per_patch
= ctx
->TessCtrlProgram
.patch_vertices
;
299 info
.indirect
= &indirect
;
300 indirect
.buffer
= st_buffer_object(indirect_data
)->buffer
;
301 indirect
.offset
= indirect_offset
;
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
),
310 if (!st
->has_multi_draw_indirect
) {
313 assert(!indirect_params
);
314 indirect
.draw_count
= 1;
315 for (i
= 0; i
< draw_count
; i
++) {
317 cso_draw_vbo(st
->cso_context
, &info
);
318 indirect
.offset
+= stride
;
321 indirect
.draw_count
= draw_count
;
322 indirect
.stride
= stride
;
323 if (indirect_params
) {
324 indirect
.indirect_draw_count
= st_buffer_object(indirect_params
)->buffer
;
325 indirect
.indirect_draw_count_offset
= indirect_params_offset
;
327 cso_draw_vbo(st
->cso_context
, &info
);
333 st_init_draw(struct st_context
*st
)
335 struct gl_context
*ctx
= st
->ctx
;
337 vbo_set_draw_func(ctx
, st_draw_vbo
);
338 vbo_set_indirect_draw_func(ctx
, st_indirect_draw_vbo
);
343 st_destroy_draw(struct st_context
*st
)
345 draw_destroy(st
->draw
);
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).
352 struct draw_context
*
353 st_get_draw_context(struct st_context
*st
)
356 st
->draw
= draw_create(st
->pipe
);
358 _mesa_error(st
->ctx
, GL_OUT_OF_MEMORY
, "feedback fallback allocation");
363 /* Disable draw options that might convert points/lines to tris, etc.
364 * as that would foul-up feedback/selection mode.
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
);
375 * Draw a quad with given position, texcoords and color.
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
,
382 unsigned num_instances
)
384 struct pipe_vertex_buffer vb
= {0};
385 struct st_util_vertex
*verts
;
387 vb
.stride
= sizeof(struct st_util_vertex
);
389 u_upload_alloc(st
->pipe
->stream_uploader
, 0,
390 4 * sizeof(struct st_util_vertex
), 4,
391 &vb
.buffer_offset
, &vb
.buffer
.resource
, (void **) &verts
);
392 if (!vb
.buffer
.resource
) {
400 verts
[0].r
= color
[0];
401 verts
[0].g
= color
[1];
402 verts
[0].b
= color
[2];
403 verts
[0].a
= color
[3];
411 verts
[1].r
= color
[0];
412 verts
[1].g
= color
[1];
413 verts
[1].b
= color
[2];
414 verts
[1].a
= color
[3];
422 verts
[2].r
= color
[0];
423 verts
[2].g
= color
[1];
424 verts
[2].b
= color
[2];
425 verts
[2].a
= color
[3];
433 verts
[3].r
= color
[0];
434 verts
[3].g
= color
[1];
435 verts
[3].b
= color
[2];
436 verts
[3].a
= color
[3];
440 u_upload_unmap(st
->pipe
->stream_uploader
);
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.
446 assert(cso_get_aux_vertex_buffer_slot(st
->cso_context
) == 0);
448 cso_set_vertex_buffers(st
->cso_context
,
449 cso_get_aux_vertex_buffer_slot(st
->cso_context
),
452 if (num_instances
> 1) {
453 cso_draw_arrays_instanced(st
->cso_context
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
456 cso_draw_arrays(st
->cso_context
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4);
459 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);