xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/v3d/v3d_context.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014-2017 Broadcom
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <xf86drm.h>
25 #include <err.h>
26 
27 #include "pipe/p_defines.h"
28 #include "util/hash_table.h"
29 #include "util/ralloc.h"
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_blitter.h"
33 #include "util/u_upload_mgr.h"
34 #include "util/u_prim.h"
35 #include "util/u_debug_cb.h"
36 #include "pipe/p_screen.h"
37 
38 #include "v3d_screen.h"
39 #include "v3d_context.h"
40 #include "v3d_resource.h"
41 #include "broadcom/compiler/v3d_compiler.h"
42 #include "broadcom/common/v3d_util.h"
43 
44 void
v3d_flush(struct pipe_context * pctx)45 v3d_flush(struct pipe_context *pctx)
46 {
47         struct v3d_context *v3d = v3d_context(pctx);
48 
49         hash_table_foreach(v3d->jobs, entry) {
50                 struct v3d_job *job = entry->data;
51                 v3d_job_submit(v3d, job);
52         }
53 }
54 
55 static void
v3d_pipe_flush(struct pipe_context * pctx,struct pipe_fence_handle ** fence,unsigned flags)56 v3d_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
57                unsigned flags)
58 {
59         struct v3d_context *v3d = v3d_context(pctx);
60 
61         v3d_flush(pctx);
62 
63         if (fence) {
64                 int fd = -1;
65                 /* Snapshot the last V3D rendering's out fence.  We'd rather
66                  * have another syncobj instead of a sync file, but this is all
67                  * we get. (HandleToFD/FDToHandle just gives you another syncobj
68                  * ID for the same syncobj).
69                  */
70                 drmSyncobjExportSyncFile(v3d->fd, v3d->out_sync, &fd);
71                 if (fd == -1) {
72                         fprintf(stderr, "export failed\n");
73                         *fence = NULL;
74                         return;
75                 }
76 
77                 struct pipe_screen *screen = pctx->screen;
78                 struct v3d_fence *f = v3d_fence_create(v3d, fd);
79                 screen->fence_reference(screen, fence, NULL);
80                 *fence = (struct pipe_fence_handle *)f;
81         }
82 }
83 
84 /* We can't flush the texture cache within rendering a tile, so we have to
85  * flush all rendering to the kernel so that the next job reading from the
86  * tile gets a flushed cache.
87  */
88 static void
v3d_texture_barrier(struct pipe_context * pctx,unsigned int flags)89 v3d_texture_barrier(struct pipe_context *pctx, unsigned int flags)
90 {
91         v3d_flush(pctx);
92 }
93 
94 static void
v3d_memory_barrier(struct pipe_context * pctx,unsigned int flags)95 v3d_memory_barrier(struct pipe_context *pctx, unsigned int flags)
96 {
97         struct v3d_context *v3d = v3d_context(pctx);
98 
99         /* We only need to flush for SSBOs and images, because for everything
100          * else we flush the job automatically when we needed.
101          */
102         const unsigned int flush_flags = PIPE_BARRIER_SHADER_BUFFER |
103                                          PIPE_BARRIER_GLOBAL_BUFFER |
104                                          PIPE_BARRIER_IMAGE;
105 
106 	if (!(flags & flush_flags))
107 		return;
108 
109         /* We only need to flush jobs writing to SSBOs/images. */
110         perf_debug("Flushing all jobs for glMemoryBarrier(), could do better");
111         v3d_flush(pctx);
112 }
113 
114 static void
v3d_invalidate_resource(struct pipe_context * pctx,struct pipe_resource * prsc)115 v3d_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
116 {
117         struct v3d_context *v3d = v3d_context(pctx);
118         struct v3d_resource *rsc = v3d_resource(prsc);
119 
120         rsc->initialized_buffers = 0;
121         rsc->invalidated = true;
122 
123         struct hash_entry *entry = _mesa_hash_table_search(v3d->write_jobs,
124                                                            prsc);
125         if (!entry)
126                 return;
127 
128         struct v3d_job *job = entry->data;
129         if (job->key.zsbuf && job->key.zsbuf->texture == prsc) {
130                 job->store &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
131                 return;
132         }
133 
134         for (int i = 0; i < job->nr_cbufs; i++) {
135                 if (job->cbufs[i] && job->cbufs[i]->texture == prsc) {
136                         job->store &= ~(PIPE_CLEAR_COLOR0 << i);
137                         return;
138                 }
139         }
140 }
141 
142 /**
143  * Flushes the current job to get up-to-date primitive counts written to the
144  * primitive counts BO, then accumulates the transform feedback primitive count
145  * in the context and the corresponding vertex counts in the bound stream
146  * output targets.
147  */
148 void
v3d_update_primitive_counters(struct v3d_context * v3d)149 v3d_update_primitive_counters(struct v3d_context *v3d)
150 {
151         struct v3d_job *job = v3d_get_job_for_fbo(v3d);
152         if (job->draw_calls_queued == 0)
153                 return;
154 
155         /* In order to get up-to-date primitive counts we need to submit
156          * the job for execution so we get the counts written to memory.
157          * Notice that this will require a sync wait for the buffer write.
158          */
159         uint32_t prims_before = v3d->tf_prims_generated;
160         v3d_job_submit(v3d, job);
161         uint32_t prims_after = v3d->tf_prims_generated;
162         if (prims_before == prims_after)
163                 return;
164 
165         enum mesa_prim prim_type = u_base_prim_type(v3d->prim_mode);
166         uint32_t num_verts = u_vertices_for_prims(prim_type,
167                                                   prims_after - prims_before);
168         for (int i = 0; i < v3d->streamout.num_targets; i++) {
169                 struct v3d_stream_output_target *so =
170                         v3d_stream_output_target(v3d->streamout.targets[i]);
171                 so->recorded_vertex_count += num_verts;
172         }
173 }
174 
175 bool
v3d_line_smoothing_enabled(struct v3d_context * v3d)176 v3d_line_smoothing_enabled(struct v3d_context *v3d)
177 {
178         if (!v3d->rasterizer->base.line_smooth)
179                 return false;
180 
181         /* According to the OpenGL docs, line smoothing shouldn’t be applied
182          * when multisampling
183          */
184         if (v3d->job->msaa || v3d->rasterizer->base.multisample)
185                 return false;
186 
187         if (v3d->framebuffer.nr_cbufs <= 0)
188                 return false;
189 
190         struct pipe_surface *cbuf = v3d->framebuffer.cbufs[0];
191         if (!cbuf)
192                 return false;
193 
194         /* Modifying the alpha for pure integer formats probably
195          * doesn’t make sense because we don’t know how the application
196          * uses the alpha value.
197          */
198         if (util_format_is_pure_integer(cbuf->format))
199                 return false;
200 
201         return true;
202 }
203 
204 float
v3d_get_real_line_width(struct v3d_context * v3d)205 v3d_get_real_line_width(struct v3d_context *v3d)
206 {
207         float width = v3d->rasterizer->base.line_width;
208 
209         if (v3d_line_smoothing_enabled(v3d)) {
210                 /* If line smoothing is enabled then we want to add some extra
211                  * pixels to the width in order to have some semi-transparent
212                  * edges.
213                  */
214                 width = floorf(M_SQRT2 * width) + 3;
215         }
216 
217         return width;
218 }
219 
220 void
v3d_ensure_prim_counts_allocated(struct v3d_context * ctx)221 v3d_ensure_prim_counts_allocated(struct v3d_context *ctx)
222 {
223         if (ctx->prim_counts)
224                 return;
225 
226         /* Init all 7 counters and 1 padding to 0 */
227         uint32_t zeroes[8] = { 0 };
228         u_upload_data(ctx->uploader,
229                       0, sizeof(zeroes), 32, zeroes,
230                       &ctx->prim_counts_offset,
231                       &ctx->prim_counts);
232 }
233 
234 void
v3d_flag_dirty_sampler_state(struct v3d_context * v3d,enum pipe_shader_type shader)235 v3d_flag_dirty_sampler_state(struct v3d_context *v3d,
236                              enum pipe_shader_type shader)
237 {
238         switch (shader) {
239         case PIPE_SHADER_VERTEX:
240                 v3d->dirty |= V3D_DIRTY_VERTTEX;
241                 break;
242         case PIPE_SHADER_GEOMETRY:
243                 v3d->dirty |= V3D_DIRTY_GEOMTEX;
244                 break;
245         case PIPE_SHADER_FRAGMENT:
246                 v3d->dirty |= V3D_DIRTY_FRAGTEX;
247                 break;
248         case PIPE_SHADER_COMPUTE:
249                 v3d->dirty |= V3D_DIRTY_COMPTEX;
250                 break;
251         default:
252                 unreachable("Unsupported shader stage");
253         }
254 }
255 
256 void
v3d_get_tile_buffer_size(const struct v3d_device_info * devinfo,bool is_msaa,bool double_buffer,uint32_t nr_cbufs,struct pipe_surface ** cbufs,struct pipe_surface * bbuf,uint32_t * tile_width,uint32_t * tile_height,uint32_t * max_bpp)257 v3d_get_tile_buffer_size(const struct v3d_device_info *devinfo,
258                          bool is_msaa,
259                          bool double_buffer,
260                          uint32_t nr_cbufs,
261                          struct pipe_surface **cbufs,
262                          struct pipe_surface *bbuf,
263                          uint32_t *tile_width,
264                          uint32_t *tile_height,
265                          uint32_t *max_bpp)
266 {
267         assert(!is_msaa || !double_buffer);
268 
269         uint32_t max_cbuf_idx = 0;
270         uint32_t total_bpp = 0;
271         *max_bpp = 0;
272         for (int i = 0; i < nr_cbufs; i++) {
273                 if (cbufs[i]) {
274                         struct v3d_surface *surf = v3d_surface(cbufs[i]);
275                         *max_bpp = MAX2(*max_bpp, surf->internal_bpp);
276                         total_bpp += 4 * v3d_internal_bpp_words(surf->internal_bpp);
277                         max_cbuf_idx = MAX2(i, max_cbuf_idx);
278                 }
279         }
280 
281         if (bbuf) {
282                 struct v3d_surface *bsurf = v3d_surface(bbuf);
283                 assert(bbuf->texture->nr_samples <= 1 || is_msaa);
284                 *max_bpp = MAX2(*max_bpp, bsurf->internal_bpp);
285                 total_bpp += 4 * v3d_internal_bpp_words(bsurf->internal_bpp);
286         }
287 
288         v3d_choose_tile_size(devinfo, max_cbuf_idx + 1,
289                              *max_bpp, total_bpp,
290                              is_msaa, double_buffer,
291                              tile_width, tile_height);
292 }
293 
294 static void
v3d_context_destroy(struct pipe_context * pctx)295 v3d_context_destroy(struct pipe_context *pctx)
296 {
297         struct v3d_context *v3d = v3d_context(pctx);
298 
299         v3d_flush(pctx);
300 
301         util_dynarray_foreach(&v3d->global_buffers, struct pipe_resource *, res) {
302                 pipe_resource_reference(res, NULL);
303         }
304 
305         if (v3d->blitter)
306                 util_blitter_destroy(v3d->blitter);
307 
308         if (v3d->uploader)
309                 u_upload_destroy(v3d->uploader);
310         if (v3d->state_uploader)
311                 u_upload_destroy(v3d->state_uploader);
312 
313         if (v3d->prim_counts)
314                 pipe_resource_reference(&v3d->prim_counts, NULL);
315 
316         slab_destroy_child(&v3d->transfer_pool);
317 
318         util_unreference_framebuffer_state(&v3d->framebuffer);
319 
320         if (v3d->sand8_blit_vs)
321                 pctx->delete_vs_state(pctx, v3d->sand8_blit_vs);
322         if (v3d->sand8_blit_fs_luma)
323                 pctx->delete_fs_state(pctx, v3d->sand8_blit_fs_luma);
324         if (v3d->sand8_blit_fs_chroma)
325                 pctx->delete_fs_state(pctx, v3d->sand8_blit_fs_chroma);
326         if (v3d->sand30_blit_vs)
327                 pctx->delete_vs_state(pctx, v3d->sand30_blit_vs);
328         if (v3d->sand30_blit_fs)
329                 pctx->delete_fs_state(pctx, v3d->sand30_blit_fs);
330 
331         v3d_program_fini(pctx);
332 
333         v3d_fence_context_finish(v3d);
334 
335         ralloc_free(v3d);
336 }
337 
338 static void
v3d_get_sample_position(struct pipe_context * pctx,unsigned sample_count,unsigned sample_index,float * xy)339 v3d_get_sample_position(struct pipe_context *pctx,
340                         unsigned sample_count, unsigned sample_index,
341                         float *xy)
342 {
343         if (sample_count <= 1) {
344                 xy[0] = 0.5;
345                 xy[1] = 0.5;
346         } else {
347                 static const int xoffsets[] = { -1, 3, -3, 1 };
348 
349                 xy[0] = 0.5 + xoffsets[sample_index] * .125;
350                 xy[1] = .125 + sample_index * .25;
351         }
352 }
353 
354 bool
v3d_render_condition_check(struct v3d_context * v3d)355 v3d_render_condition_check(struct v3d_context *v3d)
356 {
357         if (!v3d->cond_query)
358                 return true;
359 
360         perf_debug("Implementing conditional rendering on the CPU\n");
361 
362         union pipe_query_result res = { 0 };
363         bool wait =
364                 v3d->cond_mode != PIPE_RENDER_COND_NO_WAIT &&
365                 v3d->cond_mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
366 
367         struct pipe_context *pctx = (struct pipe_context *)v3d;
368         if (pctx->get_query_result(pctx, v3d->cond_query, wait, &res))
369                 return ((bool)res.u64) != v3d->cond_cond;
370 
371         return true;
372 }
373 
374 struct pipe_context *
v3d_context_create(struct pipe_screen * pscreen,void * priv,unsigned flags)375 v3d_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
376 {
377         struct v3d_screen *screen = v3d_screen(pscreen);
378         struct v3d_context *v3d;
379         struct v3d_device_info *devinfo = &screen->devinfo;
380 
381         /* Prevent dumping of the shaders built during context setup. */
382         uint32_t saved_shaderdb_flag = v3d_mesa_debug & V3D_DEBUG_SHADERDB;
383         v3d_mesa_debug &= ~V3D_DEBUG_SHADERDB;
384 
385         v3d = rzalloc(NULL, struct v3d_context);
386         if (!v3d)
387                 return NULL;
388         struct pipe_context *pctx = &v3d->base;
389 
390         v3d->screen = screen;
391 
392         int ret = drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
393                                    &v3d->out_sync);
394         if (ret) {
395                 ralloc_free(v3d);
396                 return NULL;
397         }
398 
399         pctx->screen = pscreen;
400         pctx->priv = priv;
401         pctx->destroy = v3d_context_destroy;
402         pctx->flush = v3d_pipe_flush;
403         pctx->memory_barrier = v3d_memory_barrier;
404         pctx->set_debug_callback = u_default_set_debug_callback;
405         pctx->invalidate_resource = v3d_invalidate_resource;
406         pctx->get_sample_position = v3d_get_sample_position;
407         pctx->texture_barrier = v3d_texture_barrier;
408 
409         v3d_X(devinfo, draw_init)(pctx);
410         v3d_X(devinfo, state_init)(pctx);
411         v3d_program_init(pctx);
412         v3d_query_init(pctx);
413         v3d_resource_context_init(pctx);
414 
415         v3d_job_init(v3d);
416 
417         v3d->fd = screen->fd;
418 
419         slab_create_child(&v3d->transfer_pool, &screen->transfer_pool);
420 
421         v3d->uploader = u_upload_create_default(&v3d->base);
422         v3d->base.stream_uploader = v3d->uploader;
423         v3d->base.const_uploader = v3d->uploader;
424         v3d->state_uploader = u_upload_create(&v3d->base,
425                                               4096,
426                                               PIPE_BIND_CONSTANT_BUFFER,
427                                               PIPE_USAGE_STREAM, 0);
428 
429         ret = v3d_fence_context_init(v3d);
430         if (ret)
431                 goto fail;
432 
433         v3d->blitter = util_blitter_create(pctx);
434         if (!v3d->blitter)
435                 goto fail;
436         v3d->blitter->use_index_buffer = true;
437 
438         v3d_mesa_debug |= saved_shaderdb_flag;
439 
440         v3d->sample_mask = (1 << V3D_MAX_SAMPLES) - 1;
441         v3d->active_queries = true;
442         util_dynarray_init(&v3d->global_buffers, v3d);
443 
444         return &v3d->base;
445 
446 fail:
447         pctx->destroy(pctx);
448         return NULL;
449 }
450