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