• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/errors.h"
40 
41 #include "main/image.h"
42 #include "main/bufferobj.h"
43 #include "main/macros.h"
44 #include "main/varray.h"
45 
46 #include "compiler/glsl/ir_uniform.h"
47 
48 #include "vbo/vbo.h"
49 
50 #include "st_context.h"
51 #include "st_atom.h"
52 #include "st_cb_bitmap.h"
53 #include "st_cb_bufferobjects.h"
54 #include "st_cb_xformfb.h"
55 #include "st_debug.h"
56 #include "st_draw.h"
57 #include "st_program.h"
58 #include "st_util.h"
59 
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/format/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"
70 
71 
72 /**
73  * Set the restart index.
74  */
75 static void
setup_primitive_restart(struct gl_context * ctx,struct pipe_draw_info * info)76 setup_primitive_restart(struct gl_context *ctx, struct pipe_draw_info *info)
77 {
78    if (ctx->Array._PrimitiveRestart) {
79       unsigned index_size = info->index_size;
80 
81       info->restart_index = ctx->Array._RestartIndex[index_size - 1];
82 
83       /* Enable primitive restart only when the restart index can have an
84        * effect. This is required for correctness in radeonsi GFX8 support.
85        * Other hardware may also benefit from taking a faster, non-restart path
86        * when possible.
87        */
88       if (index_size == 4 || info->restart_index < (1 << (index_size * 8)))
89          info->primitive_restart = true;
90    }
91 }
92 
93 
94 /**
95  * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
96  * the corresponding Gallium type.
97  */
98 static unsigned
translate_prim(const struct gl_context * ctx,unsigned prim)99 translate_prim(const struct gl_context *ctx, unsigned prim)
100 {
101    /* GL prims should match Gallium prims, spot-check a few */
102    STATIC_ASSERT(GL_POINTS == PIPE_PRIM_POINTS);
103    STATIC_ASSERT(GL_QUADS == PIPE_PRIM_QUADS);
104    STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
105    STATIC_ASSERT(GL_PATCHES == PIPE_PRIM_PATCHES);
106 
107    return prim;
108 }
109 
110 static inline void
prepare_draw(struct st_context * st,struct gl_context * ctx)111 prepare_draw(struct st_context *st, struct gl_context *ctx)
112 {
113    /* Mesa core state should have been validated already */
114    assert(ctx->NewState == 0x0);
115 
116    if (unlikely(!st->bitmap.cache.empty))
117       st_flush_bitmap_cache(st);
118 
119    st_invalidate_readpix_cache(st);
120 
121    /* Validate state. */
122    if ((st->dirty | ctx->NewDriverState) & ST_PIPELINE_RENDER_STATE_MASK ||
123        st->gfx_shaders_may_be_dirty) {
124       st_validate_state(st, ST_PIPELINE_RENDER);
125    }
126 
127    struct pipe_context *pipe = st->pipe;
128 
129    /* Pin threads regularly to the same Zen CCX that the main thread is
130     * running on. The main thread can move between CCXs.
131     */
132    if (unlikely(/* AMD Zen */
133                 util_cpu_caps.nr_cpus != util_cpu_caps.cores_per_L3 &&
134                 /* no glthread */
135                 ctx->CurrentClientDispatch != ctx->MarshalExec &&
136                 /* driver support */
137                 pipe->set_context_param &&
138                 /* do it occasionally */
139                 ++st->pin_thread_counter % 512 == 0)) {
140       int cpu = util_get_current_cpu();
141       if (cpu >= 0) {
142          unsigned L3_cache = util_cpu_caps.cpu_to_L3[cpu];
143 
144          pipe->set_context_param(pipe,
145                                  PIPE_CONTEXT_PARAM_PIN_THREADS_TO_L3_CACHE,
146                                  L3_cache);
147       }
148    }
149 }
150 
151 /**
152  * This function gets plugged into the VBO module and is called when
153  * we have something to render.
154  * Basically, translate the information into the format expected by gallium.
155  *
156  * Try to keep this logic in sync with st_feedback_draw_vbo.
157  */
158 static void
st_draw_vbo(struct gl_context * ctx,const struct _mesa_prim * prims,GLuint nr_prims,const struct _mesa_index_buffer * ib,GLboolean index_bounds_valid,GLuint min_index,GLuint max_index,GLuint num_instances,GLuint base_instance,struct gl_transform_feedback_object * tfb_vertcount,unsigned stream)159 st_draw_vbo(struct gl_context *ctx,
160             const struct _mesa_prim *prims,
161             GLuint nr_prims,
162             const struct _mesa_index_buffer *ib,
163 	    GLboolean index_bounds_valid,
164             GLuint min_index,
165             GLuint max_index,
166             GLuint num_instances,
167             GLuint base_instance,
168             struct gl_transform_feedback_object *tfb_vertcount,
169             unsigned stream)
170 {
171    struct st_context *st = st_context(ctx);
172    struct pipe_draw_info info;
173    unsigned i;
174    unsigned start = 0;
175 
176    prepare_draw(st, ctx);
177 
178    /* Initialize pipe_draw_info. */
179    info.primitive_restart = false;
180    info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
181    info.indirect = NULL;
182    info.count_from_stream_output = NULL;
183    info.restart_index = 0;
184    info.start_instance = base_instance;
185    info.instance_count = num_instances;
186    info._pad = 0;
187 
188    if (ib) {
189       struct gl_buffer_object *bufobj = ib->obj;
190 
191       /* Get index bounds for user buffers. */
192       if (!index_bounds_valid && st->draw_needs_minmax_index) {
193          vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index,
194                                 nr_prims);
195       }
196 
197       info.index_size = 1 << ib->index_size_shift;
198       info.min_index = min_index;
199       info.max_index = max_index;
200 
201       if (bufobj) {
202          /* indices are in a real VBO */
203          info.has_user_indices = false;
204          info.index.resource = st_buffer_object(bufobj)->buffer;
205 
206          /* Return if the bound element array buffer doesn't have any backing
207           * storage. (nothing to do)
208           */
209          if (!info.index.resource)
210             return;
211 
212          start = pointer_to_offset(ib->ptr) >> ib->index_size_shift;
213       } else {
214          /* indices are in user space memory */
215          info.has_user_indices = true;
216          info.index.user = ib->ptr;
217       }
218 
219       setup_primitive_restart(ctx, &info);
220    }
221    else {
222       info.index_size = 0;
223       info.has_user_indices = false;
224 
225       /* Transform feedback drawing is always non-indexed. */
226       /* Set info.count_from_stream_output. */
227       if (tfb_vertcount) {
228          if (!st_transform_feedback_draw_init(tfb_vertcount, stream, &info))
229             return;
230       }
231    }
232 
233    /* do actual drawing */
234    for (i = 0; i < nr_prims; i++) {
235       info.count = prims[i].count;
236 
237       /* Skip no-op draw calls. */
238       if (!info.count && !tfb_vertcount)
239          continue;
240 
241       info.mode = translate_prim(ctx, prims[i].mode);
242       info.start = start + prims[i].start;
243       info.index_bias = prims[i].basevertex;
244       info.drawid = prims[i].draw_id;
245       if (!ib) {
246          info.min_index = info.start;
247          info.max_index = info.start + info.count - 1;
248       }
249 
250       if (ST_DEBUG & DEBUG_DRAW) {
251          debug_printf("st/draw: mode %s  start %u  count %u  index_size %d\n",
252                       u_prim_name(info.mode),
253                       info.start,
254                       info.count,
255                       info.index_size);
256       }
257 
258       /* Don't call u_trim_pipe_prim. Drivers should do it if they need it. */
259       cso_draw_vbo(st->cso_context, &info);
260    }
261 }
262 
263 static void
st_indirect_draw_vbo(struct gl_context * ctx,GLuint mode,struct gl_buffer_object * indirect_data,GLsizeiptr indirect_offset,unsigned draw_count,unsigned stride,struct gl_buffer_object * indirect_draw_count,GLsizeiptr indirect_draw_count_offset,const struct _mesa_index_buffer * ib)264 st_indirect_draw_vbo(struct gl_context *ctx,
265                      GLuint mode,
266                      struct gl_buffer_object *indirect_data,
267                      GLsizeiptr indirect_offset,
268                      unsigned draw_count,
269                      unsigned stride,
270                      struct gl_buffer_object *indirect_draw_count,
271                      GLsizeiptr indirect_draw_count_offset,
272                      const struct _mesa_index_buffer *ib)
273 {
274    struct st_context *st = st_context(ctx);
275    struct pipe_draw_info info;
276    struct pipe_draw_indirect_info indirect;
277 
278    assert(stride);
279    prepare_draw(st, ctx);
280 
281    memset(&indirect, 0, sizeof(indirect));
282    util_draw_init_info(&info);
283    info.start = 0; /* index offset / index size */
284    info.max_index = ~0u; /* so that u_vbuf can tell that it's unknown */
285 
286    if (ib) {
287       struct gl_buffer_object *bufobj = ib->obj;
288 
289       /* indices are always in a real VBO */
290       assert(bufobj);
291 
292       info.index_size = 1 << ib->index_size_shift;
293       info.index.resource = st_buffer_object(bufobj)->buffer;
294       info.start = pointer_to_offset(ib->ptr) >> ib->index_size_shift;
295 
296       /* Primitive restart is not handled by the VBO module in this case. */
297       setup_primitive_restart(ctx, &info);
298    }
299 
300    info.mode = translate_prim(ctx, mode);
301    info.vertices_per_patch = ctx->TessCtrlProgram.patch_vertices;
302    info.indirect = &indirect;
303    indirect.buffer = st_buffer_object(indirect_data)->buffer;
304    indirect.offset = indirect_offset;
305 
306    if (ST_DEBUG & DEBUG_DRAW) {
307       debug_printf("st/draw indirect: mode %s drawcount %d index_size %d\n",
308                    u_prim_name(info.mode),
309                    draw_count,
310                    info.index_size);
311    }
312 
313    if (!st->has_multi_draw_indirect) {
314       int i;
315 
316       assert(!indirect_draw_count);
317       indirect.draw_count = 1;
318       for (i = 0; i < draw_count; i++) {
319          info.drawid = i;
320          cso_draw_vbo(st->cso_context, &info);
321          indirect.offset += stride;
322       }
323    } else {
324       indirect.draw_count = draw_count;
325       indirect.stride = stride;
326       if (indirect_draw_count) {
327          indirect.indirect_draw_count =
328             st_buffer_object(indirect_draw_count)->buffer;
329          indirect.indirect_draw_count_offset = indirect_draw_count_offset;
330       }
331       cso_draw_vbo(st->cso_context, &info);
332    }
333 }
334 
335 
336 void
st_init_draw_functions(struct dd_function_table * functions)337 st_init_draw_functions(struct dd_function_table *functions)
338 {
339    functions->Draw = st_draw_vbo;
340    functions->DrawIndirect = st_indirect_draw_vbo;
341 }
342 
343 
344 void
st_destroy_draw(struct st_context * st)345 st_destroy_draw(struct st_context *st)
346 {
347    draw_destroy(st->draw);
348 }
349 
350 /**
351  * Getter for the draw_context, so that initialization of it can happen only
352  * when needed (the TGSI exec machines take up quite a bit of memory).
353  */
354 struct draw_context *
st_get_draw_context(struct st_context * st)355 st_get_draw_context(struct st_context *st)
356 {
357    if (!st->draw) {
358       st->draw = draw_create(st->pipe);
359       if (!st->draw) {
360          _mesa_error(st->ctx, GL_OUT_OF_MEMORY, "feedback fallback allocation");
361          return NULL;
362       }
363    }
364 
365    /* Disable draw options that might convert points/lines to tris, etc.
366     * as that would foul-up feedback/selection mode.
367     */
368    draw_wide_line_threshold(st->draw, 1000.0f);
369    draw_wide_point_threshold(st->draw, 1000.0f);
370    draw_enable_line_stipple(st->draw, FALSE);
371    draw_enable_point_sprites(st->draw, FALSE);
372 
373    return st->draw;
374 }
375 
376 /**
377  * Draw a quad with given position, texcoords and color.
378  */
379 bool
st_draw_quad(struct st_context * st,float x0,float y0,float x1,float y1,float z,float s0,float t0,float s1,float t1,const float * color,unsigned num_instances)380 st_draw_quad(struct st_context *st,
381              float x0, float y0, float x1, float y1, float z,
382              float s0, float t0, float s1, float t1,
383              const float *color,
384              unsigned num_instances)
385 {
386    struct pipe_vertex_buffer vb = {0};
387    struct st_util_vertex *verts;
388 
389    vb.stride = sizeof(struct st_util_vertex);
390 
391    u_upload_alloc(st->pipe->stream_uploader, 0,
392                   4 * sizeof(struct st_util_vertex), 4,
393                   &vb.buffer_offset, &vb.buffer.resource, (void **) &verts);
394    if (!vb.buffer.resource) {
395       return false;
396    }
397 
398    /* lower-left */
399    verts[0].x = x0;
400    verts[0].y = y1;
401    verts[0].z = z;
402    verts[0].r = color[0];
403    verts[0].g = color[1];
404    verts[0].b = color[2];
405    verts[0].a = color[3];
406    verts[0].s = s0;
407    verts[0].t = t0;
408 
409    /* lower-right */
410    verts[1].x = x1;
411    verts[1].y = y1;
412    verts[1].z = z;
413    verts[1].r = color[0];
414    verts[1].g = color[1];
415    verts[1].b = color[2];
416    verts[1].a = color[3];
417    verts[1].s = s1;
418    verts[1].t = t0;
419 
420    /* upper-right */
421    verts[2].x = x1;
422    verts[2].y = y0;
423    verts[2].z = z;
424    verts[2].r = color[0];
425    verts[2].g = color[1];
426    verts[2].b = color[2];
427    verts[2].a = color[3];
428    verts[2].s = s1;
429    verts[2].t = t1;
430 
431    /* upper-left */
432    verts[3].x = x0;
433    verts[3].y = y0;
434    verts[3].z = z;
435    verts[3].r = color[0];
436    verts[3].g = color[1];
437    verts[3].b = color[2];
438    verts[3].a = color[3];
439    verts[3].s = s0;
440    verts[3].t = t1;
441 
442    u_upload_unmap(st->pipe->stream_uploader);
443 
444    cso_set_vertex_buffers(st->cso_context, 0, 1, &vb);
445 
446    if (num_instances > 1) {
447       cso_draw_arrays_instanced(st->cso_context, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
448                                 0, num_instances);
449    } else {
450       cso_draw_arrays(st->cso_context, PIPE_PRIM_TRIANGLE_FAN, 0, 4);
451    }
452 
453    pipe_resource_reference(&vb.buffer.resource, NULL);
454 
455    return true;
456 }
457