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/errors.h"
40 #include "main/imports.h"
41 #include "main/image.h"
42 #include "main/bufferobj.h"
43 #include "main/macros.h"
44 #include "main/varray.h"
46 #include "compiler/glsl/ir_uniform.h"
50 #include "st_context.h"
52 #include "st_cb_bitmap.h"
53 #include "st_cb_bufferobjects.h"
54 #include "st_cb_xformfb.h"
57 #include "st_program.h"
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "util/u_cpu_detect.h"
63 #include "util/u_inlines.h"
64 #include "util/u_format.h"
65 #include "util/u_prim.h"
66 #include "util/u_draw.h"
67 #include "util/u_upload_mgr.h"
68 #include "draw/draw_context.h"
69 #include "cso_cache/cso_context.h"
71 #if defined(PIPE_OS_LINUX) && !defined(ANDROID)
73 #define HAVE_SCHED_GETCPU 1
75 #define sched_getcpu() 0
76 #define HAVE_SCHED_GETCPU 0
80 * Set the restart index.
83 setup_primitive_restart(struct gl_context
*ctx
, struct pipe_draw_info
*info
)
85 if (ctx
->Array
._PrimitiveRestart
) {
86 unsigned index_size
= info
->index_size
;
89 _mesa_primitive_restart_index(ctx
, index_size
);
91 /* Enable primitive restart only when the restart index can have an
92 * effect. This is required for correctness in radeonsi VI support.
93 * Other hardware may also benefit from taking a faster, non-restart path
96 if (index_size
== 4 || info
->restart_index
< (1 << (index_size
* 8)))
97 info
->primitive_restart
= true;
103 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
104 * the corresponding Gallium type.
107 translate_prim(const struct gl_context
*ctx
, unsigned prim
)
109 /* GL prims should match Gallium prims, spot-check a few */
110 STATIC_ASSERT(GL_POINTS
== PIPE_PRIM_POINTS
);
111 STATIC_ASSERT(GL_QUADS
== PIPE_PRIM_QUADS
);
112 STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY
== PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
);
113 STATIC_ASSERT(GL_PATCHES
== PIPE_PRIM_PATCHES
);
119 prepare_draw(struct st_context
*st
, struct gl_context
*ctx
)
121 /* Mesa core state should have been validated already */
122 assert(ctx
->NewState
== 0x0);
124 if (unlikely(!st
->bitmap
.cache
.empty
))
125 st_flush_bitmap_cache(st
);
127 st_invalidate_readpix_cache(st
);
129 /* Validate state. */
130 if ((st
->dirty
| ctx
->NewDriverState
) & ST_PIPELINE_RENDER_STATE_MASK
||
131 st
->gfx_shaders_may_be_dirty
) {
132 st_validate_state(st
, ST_PIPELINE_RENDER
);
135 struct pipe_context
*pipe
= st
->pipe
;
137 /* Pin threads regularly to the same Zen CCX that the main thread is
138 * running on. The main thread can move between CCXs.
140 if (unlikely(HAVE_SCHED_GETCPU
&& /* Linux */
142 util_cpu_caps
.nr_cpus
!= util_cpu_caps
.cores_per_L3
&&
144 ctx
->CurrentClientDispatch
!= ctx
->MarshalExec
&&
146 pipe
->set_context_param
&&
147 /* do it occasionally */
148 ++st
->pin_thread_counter
% 512 == 0)) {
149 int cpu
= sched_getcpu();
151 unsigned L3_cache
= cpu
/ util_cpu_caps
.cores_per_L3
;
153 pipe
->set_context_param(pipe
,
154 PIPE_CONTEXT_PARAM_PIN_THREADS_TO_L3_CACHE
,
161 * This function gets plugged into the VBO module and is called when
162 * we have something to render.
163 * Basically, translate the information into the format expected by gallium.
165 * Try to keep this logic in sync with st_feedback_draw_vbo.
168 st_draw_vbo(struct gl_context
*ctx
,
169 const struct _mesa_prim
*prims
,
171 const struct _mesa_index_buffer
*ib
,
172 GLboolean index_bounds_valid
,
175 struct gl_transform_feedback_object
*tfb_vertcount
,
177 struct gl_buffer_object
*indirect
)
179 struct st_context
*st
= st_context(ctx
);
180 struct pipe_draw_info info
;
184 prepare_draw(st
, ctx
);
186 if (st
->vertex_array_out_of_memory
)
189 /* Initialize pipe_draw_info. */
190 info
.primitive_restart
= false;
191 info
.vertices_per_patch
= ctx
->TessCtrlProgram
.patch_vertices
;
192 info
.indirect
= NULL
;
193 info
.count_from_stream_output
= NULL
;
194 info
.restart_index
= 0;
197 struct gl_buffer_object
*bufobj
= ib
->obj
;
199 /* Get index bounds for user buffers. */
200 if (!index_bounds_valid
&& st
->draw_needs_minmax_index
) {
201 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
,
205 info
.index_size
= ib
->index_size
;
206 info
.min_index
= min_index
;
207 info
.max_index
= max_index
;
209 if (_mesa_is_bufferobj(bufobj
)) {
210 /* indices are in a real VBO */
211 info
.has_user_indices
= false;
212 info
.index
.resource
= st_buffer_object(bufobj
)->buffer
;
214 /* Return if the bound element array buffer doesn't have any backing
215 * storage. (nothing to do)
217 if (!info
.index
.resource
)
220 start
= pointer_to_offset(ib
->ptr
) / info
.index_size
;
222 /* indices are in user space memory */
223 info
.has_user_indices
= true;
224 info
.index
.user
= ib
->ptr
;
227 setup_primitive_restart(ctx
, &info
);
231 info
.has_user_indices
= false;
233 /* Transform feedback drawing is always non-indexed. */
234 /* Set info.count_from_stream_output. */
236 if (!st_transform_feedback_draw_init(tfb_vertcount
, stream
, &info
))
243 /* do actual drawing */
244 for (i
= 0; i
< nr_prims
; i
++) {
245 info
.count
= prims
[i
].count
;
247 /* Skip no-op draw calls. */
248 if (!info
.count
&& !tfb_vertcount
)
251 info
.mode
= translate_prim(ctx
, prims
[i
].mode
);
252 info
.start
= start
+ prims
[i
].start
;
253 info
.start_instance
= prims
[i
].base_instance
;
254 info
.instance_count
= prims
[i
].num_instances
;
255 info
.index_bias
= prims
[i
].basevertex
;
256 info
.drawid
= prims
[i
].draw_id
;
258 info
.min_index
= info
.start
;
259 info
.max_index
= info
.start
+ info
.count
- 1;
262 if (ST_DEBUG
& DEBUG_DRAW
) {
263 debug_printf("st/draw: mode %s start %u count %u index_size %d\n",
264 u_prim_name(info
.mode
),
270 /* Don't call u_trim_pipe_prim. Drivers should do it if they need it. */
271 cso_draw_vbo(st
->cso_context
, &info
);
276 st_indirect_draw_vbo(struct gl_context
*ctx
,
278 struct gl_buffer_object
*indirect_data
,
279 GLsizeiptr indirect_offset
,
282 struct gl_buffer_object
*indirect_draw_count
,
283 GLsizeiptr indirect_draw_count_offset
,
284 const struct _mesa_index_buffer
*ib
)
286 struct st_context
*st
= st_context(ctx
);
287 struct pipe_draw_info info
;
288 struct pipe_draw_indirect_info indirect
;
291 prepare_draw(st
, ctx
);
293 if (st
->vertex_array_out_of_memory
)
296 memset(&indirect
, 0, sizeof(indirect
));
297 util_draw_init_info(&info
);
298 info
.start
= 0; /* index offset / index size */
299 info
.max_index
= ~0u; /* so that u_vbuf can tell that it's unknown */
302 struct gl_buffer_object
*bufobj
= ib
->obj
;
304 /* indices are always in a real VBO */
305 assert(_mesa_is_bufferobj(bufobj
));
307 info
.index_size
= ib
->index_size
;
308 info
.index
.resource
= st_buffer_object(bufobj
)->buffer
;
309 info
.start
= pointer_to_offset(ib
->ptr
) / info
.index_size
;
311 /* Primitive restart is not handled by the VBO module in this case. */
312 setup_primitive_restart(ctx
, &info
);
315 info
.mode
= translate_prim(ctx
, mode
);
316 info
.vertices_per_patch
= ctx
->TessCtrlProgram
.patch_vertices
;
317 info
.indirect
= &indirect
;
318 indirect
.buffer
= st_buffer_object(indirect_data
)->buffer
;
319 indirect
.offset
= indirect_offset
;
321 if (ST_DEBUG
& DEBUG_DRAW
) {
322 debug_printf("st/draw indirect: mode %s drawcount %d index_size %d\n",
323 u_prim_name(info
.mode
),
328 if (!st
->has_multi_draw_indirect
) {
331 assert(!indirect_draw_count
);
332 indirect
.draw_count
= 1;
333 for (i
= 0; i
< draw_count
; i
++) {
335 cso_draw_vbo(st
->cso_context
, &info
);
336 indirect
.offset
+= stride
;
339 indirect
.draw_count
= draw_count
;
340 indirect
.stride
= stride
;
341 if (indirect_draw_count
) {
342 indirect
.indirect_draw_count
=
343 st_buffer_object(indirect_draw_count
)->buffer
;
344 indirect
.indirect_draw_count_offset
= indirect_draw_count_offset
;
346 cso_draw_vbo(st
->cso_context
, &info
);
352 st_init_draw_functions(struct dd_function_table
*functions
)
354 functions
->Draw
= st_draw_vbo
;
355 functions
->DrawIndirect
= st_indirect_draw_vbo
;
360 st_destroy_draw(struct st_context
*st
)
362 draw_destroy(st
->draw
);
366 * Getter for the draw_context, so that initialization of it can happen only
367 * when needed (the TGSI exec machines take up quite a bit of memory).
369 struct draw_context
*
370 st_get_draw_context(struct st_context
*st
)
373 st
->draw
= draw_create(st
->pipe
);
375 _mesa_error(st
->ctx
, GL_OUT_OF_MEMORY
, "feedback fallback allocation");
380 /* Disable draw options that might convert points/lines to tris, etc.
381 * as that would foul-up feedback/selection mode.
383 draw_wide_line_threshold(st
->draw
, 1000.0f
);
384 draw_wide_point_threshold(st
->draw
, 1000.0f
);
385 draw_enable_line_stipple(st
->draw
, FALSE
);
386 draw_enable_point_sprites(st
->draw
, FALSE
);
392 * Draw a quad with given position, texcoords and color.
395 st_draw_quad(struct st_context
*st
,
396 float x0
, float y0
, float x1
, float y1
, float z
,
397 float s0
, float t0
, float s1
, float t1
,
399 unsigned num_instances
)
401 struct pipe_vertex_buffer vb
= {0};
402 struct st_util_vertex
*verts
;
404 vb
.stride
= sizeof(struct st_util_vertex
);
406 u_upload_alloc(st
->pipe
->stream_uploader
, 0,
407 4 * sizeof(struct st_util_vertex
), 4,
408 &vb
.buffer_offset
, &vb
.buffer
.resource
, (void **) &verts
);
409 if (!vb
.buffer
.resource
) {
417 verts
[0].r
= color
[0];
418 verts
[0].g
= color
[1];
419 verts
[0].b
= color
[2];
420 verts
[0].a
= color
[3];
428 verts
[1].r
= color
[0];
429 verts
[1].g
= color
[1];
430 verts
[1].b
= color
[2];
431 verts
[1].a
= color
[3];
439 verts
[2].r
= color
[0];
440 verts
[2].g
= color
[1];
441 verts
[2].b
= color
[2];
442 verts
[2].a
= color
[3];
450 verts
[3].r
= color
[0];
451 verts
[3].g
= color
[1];
452 verts
[3].b
= color
[2];
453 verts
[3].a
= color
[3];
457 u_upload_unmap(st
->pipe
->stream_uploader
);
459 cso_set_vertex_buffers(st
->cso_context
, 0, 1, &vb
);
461 if (num_instances
> 1) {
462 cso_draw_arrays_instanced(st
->cso_context
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
465 cso_draw_arrays(st
->cso_context
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4);
468 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);