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 "util/u_threaded_context.h"
69 #include "draw/draw_context.h"
70 #include "cso_cache/cso_context.h"
71 
72 
73 /**
74  * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
75  * the corresponding Gallium type.
76  */
77 static unsigned
translate_prim(const struct gl_context * ctx,unsigned prim)78 translate_prim(const struct gl_context *ctx, unsigned prim)
79 {
80    /* GL prims should match Gallium prims, spot-check a few */
81    STATIC_ASSERT(GL_POINTS == PIPE_PRIM_POINTS);
82    STATIC_ASSERT(GL_QUADS == PIPE_PRIM_QUADS);
83    STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
84    STATIC_ASSERT(GL_PATCHES == PIPE_PRIM_PATCHES);
85 
86    return prim;
87 }
88 
89 static inline void
prepare_draw(struct st_context * st,struct gl_context * ctx,uint64_t state_mask,enum st_pipeline pipeline)90 prepare_draw(struct st_context *st, struct gl_context *ctx, uint64_t state_mask,
91              enum st_pipeline pipeline)
92 {
93    /* Mesa core state should have been validated already */
94    assert(ctx->NewState == 0x0);
95 
96    if (unlikely(!st->bitmap.cache.empty))
97       st_flush_bitmap_cache(st);
98 
99    st_invalidate_readpix_cache(st);
100 
101    /* Validate state. */
102    if ((st->dirty | ctx->NewDriverState) & st->active_states & state_mask ||
103        st->gfx_shaders_may_be_dirty) {
104       st_validate_state(st, pipeline);
105    }
106 
107    /* Pin threads regularly to the same Zen CCX that the main thread is
108     * running on. The main thread can move between CCXs.
109     */
110    if (unlikely(st->pin_thread_counter != ST_L3_PINNING_DISABLED &&
111                 /* no glthread */
112                 ctx->CurrentClientDispatch != ctx->MarshalExec &&
113                 /* do it occasionally */
114                 ++st->pin_thread_counter % 512 == 0)) {
115       st->pin_thread_counter = 0;
116 
117       int cpu = util_get_current_cpu();
118       if (cpu >= 0) {
119          struct pipe_context *pipe = st->pipe;
120          uint16_t L3_cache = util_get_cpu_caps()->cpu_to_L3[cpu];
121 
122          if (L3_cache != U_CPU_INVALID_L3) {
123             pipe->set_context_param(pipe,
124                                     PIPE_CONTEXT_PARAM_PIN_THREADS_TO_L3_CACHE,
125                                     L3_cache);
126          }
127       }
128    }
129 }
130 
131 static bool ALWAYS_INLINE
prepare_indexed_draw(struct st_context * st,struct gl_context * ctx,struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)132 prepare_indexed_draw(/* pass both st and ctx to reduce dereferences */
133                      struct st_context *st,
134                      struct gl_context *ctx,
135                      struct pipe_draw_info *info,
136                      const struct pipe_draw_start_count_bias *draws,
137                      unsigned num_draws)
138 {
139    if (info->index_size) {
140       /* Get index bounds for user buffers. */
141       if (!info->index_bounds_valid &&
142           st->draw_needs_minmax_index) {
143          /* Return if this fails, which means all draws have count == 0. */
144          if (!vbo_get_minmax_indices_gallium(ctx, info, draws, num_draws))
145             return false;
146 
147          info->index_bounds_valid = true;
148       }
149 
150       if (!info->has_user_indices) {
151          if (st->pipe->draw_vbo == tc_draw_vbo) {
152             /* Fast path for u_threaded_context. This eliminates the atomic
153              * increment for the index buffer refcount when adding it into
154              * the threaded batch buffer.
155              */
156             info->index.resource =
157                st_get_buffer_reference(ctx, info->index.gl_bo);
158             info->take_index_buffer_ownership = true;
159          } else {
160             info->index.resource = st_buffer_object(info->index.gl_bo)->buffer;
161          }
162 
163          /* Return if the bound element array buffer doesn't have any backing
164           * storage. (nothing to do)
165           */
166          if (unlikely(!info->index.resource))
167             return false;
168       }
169    }
170    return true;
171 }
172 
173 static void
st_draw_gallium(struct gl_context * ctx,struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)174 st_draw_gallium(struct gl_context *ctx,
175                 struct pipe_draw_info *info,
176                 unsigned drawid_offset,
177                 const struct pipe_draw_start_count_bias *draws,
178                 unsigned num_draws)
179 {
180    struct st_context *st = st_context(ctx);
181 
182    prepare_draw(st, ctx, ST_PIPELINE_RENDER_STATE_MASK, ST_PIPELINE_RENDER);
183 
184    if (!prepare_indexed_draw(st, ctx, info, draws, num_draws))
185       return;
186 
187    cso_multi_draw(st->cso_context, info, drawid_offset, draws, num_draws);
188 }
189 
190 static void
st_draw_gallium_multimode(struct gl_context * ctx,struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draws,const unsigned char * mode,unsigned num_draws)191 st_draw_gallium_multimode(struct gl_context *ctx,
192                           struct pipe_draw_info *info,
193                           const struct pipe_draw_start_count_bias *draws,
194                           const unsigned char *mode,
195                           unsigned num_draws)
196 {
197    struct st_context *st = st_context(ctx);
198 
199    prepare_draw(st, ctx, ST_PIPELINE_RENDER_STATE_MASK, ST_PIPELINE_RENDER);
200 
201    if (!prepare_indexed_draw(st, ctx, info, draws, num_draws))
202       return;
203 
204    unsigned i, first;
205    struct cso_context *cso = st->cso_context;
206 
207    /* Find consecutive draws where mode doesn't vary. */
208    for (i = 0, first = 0; i <= num_draws; i++) {
209       if (i == num_draws || mode[i] != mode[first]) {
210          info->mode = mode[first];
211          cso_multi_draw(cso, info, 0, &draws[first], i - first);
212          first = i;
213 
214          /* We can pass the reference only once. st_buffer_object keeps
215           * the reference alive for later draws.
216           */
217          info->take_index_buffer_ownership = false;
218       }
219    }
220 }
221 
222 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,bool primitive_restart,unsigned restart_index)223 st_indirect_draw_vbo(struct gl_context *ctx,
224                      GLuint mode,
225                      struct gl_buffer_object *indirect_data,
226                      GLsizeiptr indirect_offset,
227                      unsigned draw_count,
228                      unsigned stride,
229                      struct gl_buffer_object *indirect_draw_count,
230                      GLsizeiptr indirect_draw_count_offset,
231                      const struct _mesa_index_buffer *ib,
232                      bool primitive_restart,
233                      unsigned restart_index)
234 {
235    struct st_context *st = st_context(ctx);
236    struct pipe_draw_info info;
237    struct pipe_draw_indirect_info indirect;
238    struct pipe_draw_start_count_bias draw = {0};
239 
240    assert(stride);
241    prepare_draw(st, ctx, ST_PIPELINE_RENDER_STATE_MASK, ST_PIPELINE_RENDER);
242 
243    memset(&indirect, 0, sizeof(indirect));
244    util_draw_init_info(&info);
245    info.max_index = ~0u; /* so that u_vbuf can tell that it's unknown */
246 
247    if (ib) {
248       struct gl_buffer_object *bufobj = ib->obj;
249 
250       /* indices are always in a real VBO */
251       assert(bufobj);
252 
253       info.index_size = 1 << ib->index_size_shift;
254       info.index.resource = st_buffer_object(bufobj)->buffer;
255       draw.start = pointer_to_offset(ib->ptr) >> ib->index_size_shift;
256 
257       info.restart_index = restart_index;
258       info.primitive_restart = primitive_restart;
259    }
260 
261    info.mode = translate_prim(ctx, mode);
262    indirect.buffer = st_buffer_object(indirect_data)->buffer;
263    indirect.offset = indirect_offset;
264 
265    /* Viewperf2020/Maya draws with a buffer that has no storage. */
266    if (!indirect.buffer)
267       return;
268 
269    if (!st->has_multi_draw_indirect) {
270       int i;
271 
272       assert(!indirect_draw_count);
273       indirect.draw_count = 1;
274       for (i = 0; i < draw_count; i++) {
275          cso_draw_vbo(st->cso_context, &info, i, &indirect, draw);
276          indirect.offset += stride;
277       }
278    } else {
279       indirect.draw_count = draw_count;
280       indirect.stride = stride;
281       if (indirect_draw_count) {
282          indirect.indirect_draw_count =
283             st_buffer_object(indirect_draw_count)->buffer;
284          indirect.indirect_draw_count_offset = indirect_draw_count_offset;
285       }
286       cso_draw_vbo(st->cso_context, &info, 0, &indirect, draw);
287    }
288 }
289 
290 static void
st_draw_transform_feedback(struct gl_context * ctx,GLenum mode,unsigned num_instances,unsigned stream,struct gl_transform_feedback_object * tfb_vertcount)291 st_draw_transform_feedback(struct gl_context *ctx, GLenum mode,
292                            unsigned num_instances, unsigned stream,
293                            struct gl_transform_feedback_object *tfb_vertcount)
294 {
295    struct st_context *st = st_context(ctx);
296    struct pipe_draw_info info;
297    struct pipe_draw_indirect_info indirect;
298    struct pipe_draw_start_count_bias draw = {0};
299 
300    prepare_draw(st, ctx, ST_PIPELINE_RENDER_STATE_MASK, ST_PIPELINE_RENDER);
301 
302    memset(&indirect, 0, sizeof(indirect));
303    util_draw_init_info(&info);
304    info.max_index = ~0u; /* so that u_vbuf can tell that it's unknown */
305    info.mode = translate_prim(ctx, mode);
306    info.instance_count = num_instances;
307 
308    /* Transform feedback drawing is always non-indexed. */
309    /* Set info.count_from_stream_output. */
310    if (!st_transform_feedback_draw_init(tfb_vertcount, stream, &indirect))
311       return;
312 
313    cso_draw_vbo(st->cso_context, &info, 0, &indirect, draw);
314 }
315 
316 static void
st_draw_gallium_vertex_state(struct gl_context * ctx,struct pipe_vertex_state * state,struct pipe_draw_vertex_state_info info,const struct pipe_draw_start_count_bias * draws,const uint8_t * mode,unsigned num_draws,bool per_vertex_edgeflags)317 st_draw_gallium_vertex_state(struct gl_context *ctx,
318                              struct pipe_vertex_state *state,
319                              struct pipe_draw_vertex_state_info info,
320                              const struct pipe_draw_start_count_bias *draws,
321                              const uint8_t *mode,
322                              unsigned num_draws,
323                              bool per_vertex_edgeflags)
324 {
325    struct st_context *st = st_context(ctx);
326    bool old_vertdata_edgeflags = st->vertdata_edgeflags;
327 
328    /* We don't flag any other states to make st_validate state update edge
329     * flags, so we need to update them here.
330     */
331    st_update_edgeflags(st, per_vertex_edgeflags);
332 
333    prepare_draw(st, ctx, ST_PIPELINE_RENDER_STATE_MASK_NO_VARRAYS,
334                 ST_PIPELINE_RENDER_NO_VARRAYS);
335 
336    struct pipe_context *pipe = st->pipe;
337    uint32_t velem_mask = ctx->VertexProgram._Current->info.inputs_read;
338 
339    if (!mode) {
340       pipe->draw_vertex_state(pipe, state, velem_mask, info, draws, num_draws);
341    } else {
342       /* Find consecutive draws where mode doesn't vary. */
343       for (unsigned i = 0, first = 0; i <= num_draws; i++) {
344          if (i == num_draws || mode[i] != mode[first]) {
345             unsigned current_num_draws = i - first;
346 
347             /* Increase refcount to be able to use take_vertex_state_ownership
348              * with all draws.
349              */
350             if (i != num_draws && info.take_vertex_state_ownership)
351                p_atomic_inc(&state->reference.count);
352 
353             info.mode = mode[first];
354             pipe->draw_vertex_state(pipe, state, velem_mask, info, &draws[first],
355                                     current_num_draws);
356             first = i;
357          }
358       }
359    }
360 
361    /* If per-vertex edge flags are different than the non-display-list state,
362     *  just flag ST_NEW_VERTEX_ARRAY, which will also completely revalidate
363     * edge flags in st_validate_state.
364     */
365    if (st->vertdata_edgeflags != old_vertdata_edgeflags)
366       st->dirty |= ST_NEW_VERTEX_ARRAYS;
367 }
368 
369 void
st_init_draw_functions(struct pipe_screen * screen,struct dd_function_table * functions)370 st_init_draw_functions(struct pipe_screen *screen,
371                        struct dd_function_table *functions)
372 {
373    functions->Draw = NULL;
374    functions->DrawGallium = st_draw_gallium;
375    functions->DrawGalliumMultiMode = st_draw_gallium_multimode;
376    functions->DrawIndirect = st_indirect_draw_vbo;
377    functions->DrawTransformFeedback = st_draw_transform_feedback;
378 
379    if (screen->get_param(screen, PIPE_CAP_DRAW_VERTEX_STATE)) {
380       functions->DrawGalliumVertexState = st_draw_gallium_vertex_state;
381       functions->CreateGalliumVertexState = st_create_gallium_vertex_state;
382    }
383 }
384 
385 
386 void
st_destroy_draw(struct st_context * st)387 st_destroy_draw(struct st_context *st)
388 {
389    draw_destroy(st->draw);
390 }
391 
392 /**
393  * Getter for the draw_context, so that initialization of it can happen only
394  * when needed (the TGSI exec machines take up quite a bit of memory).
395  */
396 struct draw_context *
st_get_draw_context(struct st_context * st)397 st_get_draw_context(struct st_context *st)
398 {
399    if (!st->draw) {
400       st->draw = draw_create(st->pipe);
401       if (!st->draw) {
402          _mesa_error(st->ctx, GL_OUT_OF_MEMORY, "feedback fallback allocation");
403          return NULL;
404       }
405    }
406 
407    /* Disable draw options that might convert points/lines to tris, etc.
408     * as that would foul-up feedback/selection mode.
409     */
410    draw_wide_line_threshold(st->draw, 1000.0f);
411    draw_wide_point_threshold(st->draw, 1000.0f);
412    draw_enable_line_stipple(st->draw, FALSE);
413    draw_enable_point_sprites(st->draw, FALSE);
414 
415    return st->draw;
416 }
417 
418 /**
419  * Draw a quad with given position, texcoords and color.
420  */
421 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)422 st_draw_quad(struct st_context *st,
423              float x0, float y0, float x1, float y1, float z,
424              float s0, float t0, float s1, float t1,
425              const float *color,
426              unsigned num_instances)
427 {
428    struct pipe_vertex_buffer vb = {0};
429    struct st_util_vertex *verts;
430 
431    vb.stride = sizeof(struct st_util_vertex);
432 
433    u_upload_alloc(st->pipe->stream_uploader, 0,
434                   4 * sizeof(struct st_util_vertex), 4,
435                   &vb.buffer_offset, &vb.buffer.resource, (void **) &verts);
436    if (!vb.buffer.resource) {
437       return false;
438    }
439 
440    /* lower-left */
441    verts[0].x = x0;
442    verts[0].y = y1;
443    verts[0].z = z;
444    verts[0].r = color[0];
445    verts[0].g = color[1];
446    verts[0].b = color[2];
447    verts[0].a = color[3];
448    verts[0].s = s0;
449    verts[0].t = t0;
450 
451    /* lower-right */
452    verts[1].x = x1;
453    verts[1].y = y1;
454    verts[1].z = z;
455    verts[1].r = color[0];
456    verts[1].g = color[1];
457    verts[1].b = color[2];
458    verts[1].a = color[3];
459    verts[1].s = s1;
460    verts[1].t = t0;
461 
462    /* upper-right */
463    verts[2].x = x1;
464    verts[2].y = y0;
465    verts[2].z = z;
466    verts[2].r = color[0];
467    verts[2].g = color[1];
468    verts[2].b = color[2];
469    verts[2].a = color[3];
470    verts[2].s = s1;
471    verts[2].t = t1;
472 
473    /* upper-left */
474    verts[3].x = x0;
475    verts[3].y = y0;
476    verts[3].z = z;
477    verts[3].r = color[0];
478    verts[3].g = color[1];
479    verts[3].b = color[2];
480    verts[3].a = color[3];
481    verts[3].s = s0;
482    verts[3].t = t1;
483 
484    u_upload_unmap(st->pipe->stream_uploader);
485 
486    cso_set_vertex_buffers(st->cso_context, 0, 1, &vb);
487    st->last_num_vbuffers = MAX2(st->last_num_vbuffers, 1);
488 
489    if (num_instances > 1) {
490       cso_draw_arrays_instanced(st->cso_context, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
491                                 0, num_instances);
492    } else {
493       cso_draw_arrays(st->cso_context, PIPE_PRIM_TRIANGLE_FAN, 0, 4);
494    }
495 
496    pipe_resource_reference(&vb.buffer.resource, NULL);
497 
498    return true;
499 }
500