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