• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IMAGE;
104 
105 	if (!(flags & flush_flags))
106 		return;
107 
108         /* We only need to flush jobs writing to SSBOs/images. */
109         perf_debug("Flushing all jobs for glMemoryBarrier(), could do better");
110         v3d_flush(pctx);
111 }
112 
113 static void
v3d_invalidate_resource(struct pipe_context * pctx,struct pipe_resource * prsc)114 v3d_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
115 {
116         struct v3d_context *v3d = v3d_context(pctx);
117         struct v3d_resource *rsc = v3d_resource(prsc);
118 
119         rsc->initialized_buffers = 0;
120 
121         struct hash_entry *entry = _mesa_hash_table_search(v3d->write_jobs,
122                                                            prsc);
123         if (!entry)
124                 return;
125 
126         struct v3d_job *job = entry->data;
127         if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
128                 job->store &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
129 }
130 
131 /**
132  * Flushes the current job to get up-to-date primitive counts written to the
133  * primitive counts BO, then accumulates the transform feedback primitive count
134  * in the context and the corresponding vertex counts in the bound stream
135  * output targets.
136  */
137 void
v3d_update_primitive_counters(struct v3d_context * v3d)138 v3d_update_primitive_counters(struct v3d_context *v3d)
139 {
140         struct v3d_job *job = v3d_get_job_for_fbo(v3d);
141         if (job->draw_calls_queued == 0)
142                 return;
143 
144         /* In order to get up-to-date primitive counts we need to submit
145          * the job for execution so we get the counts written to memory.
146          * Notice that this will require a sync wait for the buffer write.
147          */
148         uint32_t prims_before = v3d->tf_prims_generated;
149         v3d_job_submit(v3d, job);
150         uint32_t prims_after = v3d->tf_prims_generated;
151         if (prims_before == prims_after)
152                 return;
153 
154         enum mesa_prim prim_type = u_base_prim_type(v3d->prim_mode);
155         uint32_t num_verts = u_vertices_for_prims(prim_type,
156                                                   prims_after - prims_before);
157         for (int i = 0; i < v3d->streamout.num_targets; i++) {
158                 struct v3d_stream_output_target *so =
159                         v3d_stream_output_target(v3d->streamout.targets[i]);
160                 so->recorded_vertex_count += num_verts;
161         }
162 }
163 
164 bool
v3d_line_smoothing_enabled(struct v3d_context * v3d)165 v3d_line_smoothing_enabled(struct v3d_context *v3d)
166 {
167         if (!v3d->rasterizer->base.line_smooth)
168                 return false;
169 
170         /* According to the OpenGL docs, line smoothing shouldn’t be applied
171          * when multisampling
172          */
173         if (v3d->job->msaa || v3d->rasterizer->base.multisample)
174                 return false;
175 
176         if (v3d->framebuffer.nr_cbufs <= 0)
177                 return false;
178 
179         struct pipe_surface *cbuf = v3d->framebuffer.cbufs[0];
180         if (!cbuf)
181                 return false;
182 
183         /* Modifying the alpha for pure integer formats probably
184          * doesn’t make sense because we don’t know how the application
185          * uses the alpha value.
186          */
187         if (util_format_is_pure_integer(cbuf->format))
188                 return false;
189 
190         return true;
191 }
192 
193 float
v3d_get_real_line_width(struct v3d_context * v3d)194 v3d_get_real_line_width(struct v3d_context *v3d)
195 {
196         float width = v3d->rasterizer->base.line_width;
197 
198         if (v3d_line_smoothing_enabled(v3d)) {
199                 /* If line smoothing is enabled then we want to add some extra
200                  * pixels to the width in order to have some semi-transparent
201                  * edges.
202                  */
203                 width = floorf(M_SQRT2 * width) + 3;
204         }
205 
206         return width;
207 }
208 
209 void
v3d_ensure_prim_counts_allocated(struct v3d_context * ctx)210 v3d_ensure_prim_counts_allocated(struct v3d_context *ctx)
211 {
212         if (ctx->prim_counts)
213                 return;
214 
215         /* Init all 7 counters and 1 padding to 0 */
216         uint32_t zeroes[8] = { 0 };
217         u_upload_data(ctx->uploader,
218                       0, sizeof(zeroes), 32, zeroes,
219                       &ctx->prim_counts_offset,
220                       &ctx->prim_counts);
221 }
222 
223 void
v3d_flag_dirty_sampler_state(struct v3d_context * v3d,enum pipe_shader_type shader)224 v3d_flag_dirty_sampler_state(struct v3d_context *v3d,
225                              enum pipe_shader_type shader)
226 {
227         switch (shader) {
228         case PIPE_SHADER_VERTEX:
229                 v3d->dirty |= V3D_DIRTY_VERTTEX;
230                 break;
231         case PIPE_SHADER_GEOMETRY:
232                 v3d->dirty |= V3D_DIRTY_GEOMTEX;
233                 break;
234         case PIPE_SHADER_FRAGMENT:
235                 v3d->dirty |= V3D_DIRTY_FRAGTEX;
236                 break;
237         case PIPE_SHADER_COMPUTE:
238                 v3d->dirty |= V3D_DIRTY_COMPTEX;
239                 break;
240         default:
241                 unreachable("Unsupported shader stage");
242         }
243 }
244 
245 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)246 v3d_get_tile_buffer_size(const struct v3d_device_info *devinfo,
247                          bool is_msaa,
248                          bool double_buffer,
249                          uint32_t nr_cbufs,
250                          struct pipe_surface **cbufs,
251                          struct pipe_surface *bbuf,
252                          uint32_t *tile_width,
253                          uint32_t *tile_height,
254                          uint32_t *max_bpp)
255 {
256         assert(!is_msaa || !double_buffer);
257 
258         uint32_t max_cbuf_idx = 0;
259         uint32_t total_bpp = 0;
260         *max_bpp = 0;
261         for (int i = 0; i < nr_cbufs; i++) {
262                 if (cbufs[i]) {
263                         struct v3d_surface *surf = v3d_surface(cbufs[i]);
264                         *max_bpp = MAX2(*max_bpp, surf->internal_bpp);
265                         total_bpp += 4 * v3d_internal_bpp_words(surf->internal_bpp);
266                         max_cbuf_idx = MAX2(i, max_cbuf_idx);
267                 }
268         }
269 
270         if (bbuf) {
271                 struct v3d_surface *bsurf = v3d_surface(bbuf);
272                 assert(bbuf->texture->nr_samples <= 1 || is_msaa);
273                 *max_bpp = MAX2(*max_bpp, bsurf->internal_bpp);
274                 total_bpp += 4 * v3d_internal_bpp_words(bsurf->internal_bpp);
275         }
276 
277         v3d_choose_tile_size(devinfo, max_cbuf_idx + 1,
278                              *max_bpp, total_bpp,
279                              is_msaa, double_buffer,
280                              tile_width, tile_height);
281 }
282 
283 static void
v3d_context_destroy(struct pipe_context * pctx)284 v3d_context_destroy(struct pipe_context *pctx)
285 {
286         struct v3d_context *v3d = v3d_context(pctx);
287 
288         v3d_flush(pctx);
289 
290         if (v3d->blitter)
291                 util_blitter_destroy(v3d->blitter);
292 
293         if (v3d->uploader)
294                 u_upload_destroy(v3d->uploader);
295         if (v3d->state_uploader)
296                 u_upload_destroy(v3d->state_uploader);
297 
298         if (v3d->prim_counts)
299                 pipe_resource_reference(&v3d->prim_counts, NULL);
300 
301         slab_destroy_child(&v3d->transfer_pool);
302 
303         util_unreference_framebuffer_state(&v3d->framebuffer);
304 
305         if (v3d->sand8_blit_vs)
306                 pctx->delete_vs_state(pctx, v3d->sand8_blit_vs);
307         if (v3d->sand8_blit_fs_luma)
308                 pctx->delete_fs_state(pctx, v3d->sand8_blit_fs_luma);
309         if (v3d->sand8_blit_fs_chroma)
310                 pctx->delete_fs_state(pctx, v3d->sand8_blit_fs_chroma);
311         if (v3d->sand30_blit_vs)
312                 pctx->delete_vs_state(pctx, v3d->sand30_blit_vs);
313         if (v3d->sand30_blit_fs)
314                 pctx->delete_fs_state(pctx, v3d->sand30_blit_fs);
315 
316         v3d_program_fini(pctx);
317 
318         v3d_fence_context_finish(v3d);
319 
320         ralloc_free(v3d);
321 }
322 
323 static void
v3d_get_sample_position(struct pipe_context * pctx,unsigned sample_count,unsigned sample_index,float * xy)324 v3d_get_sample_position(struct pipe_context *pctx,
325                         unsigned sample_count, unsigned sample_index,
326                         float *xy)
327 {
328         if (sample_count <= 1) {
329                 xy[0] = 0.5;
330                 xy[1] = 0.5;
331         } else {
332                 static const int xoffsets[] = { -1, 3, -3, 1 };
333 
334                 xy[0] = 0.5 + xoffsets[sample_index] * .125;
335                 xy[1] = .125 + sample_index * .25;
336         }
337 }
338 
339 bool
v3d_render_condition_check(struct v3d_context * v3d)340 v3d_render_condition_check(struct v3d_context *v3d)
341 {
342         if (!v3d->cond_query)
343                 return true;
344 
345         perf_debug("Implementing conditional rendering on the CPU\n");
346 
347         union pipe_query_result res = { 0 };
348         bool wait =
349                 v3d->cond_mode != PIPE_RENDER_COND_NO_WAIT &&
350                 v3d->cond_mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
351 
352         struct pipe_context *pctx = (struct pipe_context *)v3d;
353         if (pctx->get_query_result(pctx, v3d->cond_query, wait, &res))
354                 return ((bool)res.u64) != v3d->cond_cond;
355 
356         return true;
357 }
358 
359 struct pipe_context *
v3d_context_create(struct pipe_screen * pscreen,void * priv,unsigned flags)360 v3d_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
361 {
362         struct v3d_screen *screen = v3d_screen(pscreen);
363         struct v3d_context *v3d;
364         struct v3d_device_info *devinfo = &screen->devinfo;
365 
366         /* Prevent dumping of the shaders built during context setup. */
367         uint32_t saved_shaderdb_flag = v3d_mesa_debug & V3D_DEBUG_SHADERDB;
368         v3d_mesa_debug &= ~V3D_DEBUG_SHADERDB;
369 
370         v3d = rzalloc(NULL, struct v3d_context);
371         if (!v3d)
372                 return NULL;
373         struct pipe_context *pctx = &v3d->base;
374 
375         v3d->screen = screen;
376 
377         int ret = drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
378                                    &v3d->out_sync);
379         if (ret) {
380                 ralloc_free(v3d);
381                 return NULL;
382         }
383 
384         pctx->screen = pscreen;
385         pctx->priv = priv;
386         pctx->destroy = v3d_context_destroy;
387         pctx->flush = v3d_pipe_flush;
388         pctx->memory_barrier = v3d_memory_barrier;
389         pctx->set_debug_callback = u_default_set_debug_callback;
390         pctx->invalidate_resource = v3d_invalidate_resource;
391         pctx->get_sample_position = v3d_get_sample_position;
392         pctx->texture_barrier = v3d_texture_barrier;
393 
394         v3d_X(devinfo, draw_init)(pctx);
395         v3d_X(devinfo, state_init)(pctx);
396         v3d_program_init(pctx);
397         v3d_query_init(pctx);
398         v3d_resource_context_init(pctx);
399 
400         v3d_job_init(v3d);
401 
402         v3d->fd = screen->fd;
403 
404         slab_create_child(&v3d->transfer_pool, &screen->transfer_pool);
405 
406         v3d->uploader = u_upload_create_default(&v3d->base);
407         v3d->base.stream_uploader = v3d->uploader;
408         v3d->base.const_uploader = v3d->uploader;
409         v3d->state_uploader = u_upload_create(&v3d->base,
410                                               4096,
411                                               PIPE_BIND_CONSTANT_BUFFER,
412                                               PIPE_USAGE_STREAM, 0);
413 
414         ret = v3d_fence_context_init(v3d);
415         if (ret)
416                 goto fail;
417 
418         v3d->blitter = util_blitter_create(pctx);
419         if (!v3d->blitter)
420                 goto fail;
421         v3d->blitter->use_index_buffer = true;
422 
423         v3d_mesa_debug |= saved_shaderdb_flag;
424 
425         v3d->sample_mask = (1 << V3D_MAX_SAMPLES) - 1;
426         v3d->active_queries = true;
427 
428         return &v3d->base;
429 
430 fail:
431         pctx->destroy(pctx);
432         return NULL;
433 }
434