• 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 "pipe/p_screen.h"
36 
37 #include "v3d_screen.h"
38 #include "v3d_context.h"
39 #include "v3d_resource.h"
40 #include "broadcom/compiler/v3d_compiler.h"
41 
42 void
v3d_flush(struct pipe_context * pctx)43 v3d_flush(struct pipe_context *pctx)
44 {
45         struct v3d_context *v3d = v3d_context(pctx);
46 
47         hash_table_foreach(v3d->jobs, entry) {
48                 struct v3d_job *job = entry->data;
49                 v3d_job_submit(v3d, job);
50         }
51 }
52 
53 static void
v3d_pipe_flush(struct pipe_context * pctx,struct pipe_fence_handle ** fence,unsigned flags)54 v3d_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
55                unsigned flags)
56 {
57         struct v3d_context *v3d = v3d_context(pctx);
58 
59         v3d_flush(pctx);
60 
61         if (fence) {
62                 struct pipe_screen *screen = pctx->screen;
63                 struct v3d_fence *f = v3d_fence_create(v3d);
64                 screen->fence_reference(screen, fence, NULL);
65                 *fence = (struct pipe_fence_handle *)f;
66         }
67 }
68 
69 static void
v3d_memory_barrier(struct pipe_context * pctx,unsigned int flags)70 v3d_memory_barrier(struct pipe_context *pctx, unsigned int flags)
71 {
72         struct v3d_context *v3d = v3d_context(pctx);
73 
74         /* We only need to flush for SSBOs and images, because for everything
75          * else we flush the job automatically when we needed.
76          */
77         const unsigned int flush_flags = PIPE_BARRIER_SHADER_BUFFER |
78                                          PIPE_BARRIER_IMAGE;
79 
80 	if (!(flags & flush_flags))
81 		return;
82 
83         /* We only need to flush jobs writing to SSBOs/images. */
84         perf_debug("Flushing all jobs for glMemoryBarrier(), could do better");
85         v3d_flush(pctx);
86 }
87 
88 static void
v3d_set_debug_callback(struct pipe_context * pctx,const struct pipe_debug_callback * cb)89 v3d_set_debug_callback(struct pipe_context *pctx,
90                        const struct pipe_debug_callback *cb)
91 {
92         struct v3d_context *v3d = v3d_context(pctx);
93 
94         if (cb)
95                 v3d->debug = *cb;
96         else
97                 memset(&v3d->debug, 0, sizeof(v3d->debug));
98 }
99 
100 static void
v3d_invalidate_resource(struct pipe_context * pctx,struct pipe_resource * prsc)101 v3d_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
102 {
103         struct v3d_context *v3d = v3d_context(pctx);
104         struct v3d_resource *rsc = v3d_resource(prsc);
105 
106         rsc->initialized_buffers = 0;
107 
108         struct hash_entry *entry = _mesa_hash_table_search(v3d->write_jobs,
109                                                            prsc);
110         if (!entry)
111                 return;
112 
113         struct v3d_job *job = entry->data;
114         if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
115                 job->store &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
116 }
117 
118 /**
119  * Flushes the current job to get up-to-date primitive counts written to the
120  * primitive counts BO, then accumulates the transform feedback primitive count
121  * in the context and the corresponding vertex counts in the bound stream
122  * output targets.
123  */
124 void
v3d_update_primitive_counters(struct v3d_context * v3d)125 v3d_update_primitive_counters(struct v3d_context *v3d)
126 {
127         struct v3d_job *job = v3d_get_job_for_fbo(v3d);
128         if (job->draw_calls_queued == 0)
129                 return;
130 
131         /* In order to get up-to-date primitive counts we need to submit
132          * the job for execution so we get the counts written to memory.
133          * Notice that this will require a sync wait for the buffer write.
134          */
135         uint32_t prims_before = v3d->tf_prims_generated;
136         v3d_job_submit(v3d, job);
137         uint32_t prims_after = v3d->tf_prims_generated;
138         if (prims_before == prims_after)
139                 return;
140 
141         enum pipe_prim_type prim_type = u_base_prim_type(v3d->prim_mode);
142         uint32_t num_verts = u_vertices_for_prims(prim_type,
143                                                   prims_after - prims_before);
144         for (int i = 0; i < v3d->streamout.num_targets; i++) {
145                 struct v3d_stream_output_target *so =
146                         v3d_stream_output_target(v3d->streamout.targets[i]);
147                 so->recorded_vertex_count += num_verts;
148         }
149 }
150 
151 bool
v3d_line_smoothing_enabled(struct v3d_context * v3d)152 v3d_line_smoothing_enabled(struct v3d_context *v3d)
153 {
154         if (!v3d->rasterizer->base.line_smooth)
155                 return false;
156 
157         /* According to the OpenGL docs, line smoothing shouldn’t be applied
158          * when multisampling
159          */
160         if (v3d->job->msaa || v3d->rasterizer->base.multisample)
161                 return false;
162 
163         if (v3d->framebuffer.nr_cbufs <= 0)
164                 return false;
165 
166         struct pipe_surface *cbuf = v3d->framebuffer.cbufs[0];
167         if (!cbuf)
168                 return false;
169 
170         /* Modifying the alpha for pure integer formats probably
171          * doesn’t make sense because we don’t know how the application
172          * uses the alpha value.
173          */
174         if (util_format_is_pure_integer(cbuf->format))
175                 return false;
176 
177         return true;
178 }
179 
180 float
v3d_get_real_line_width(struct v3d_context * v3d)181 v3d_get_real_line_width(struct v3d_context *v3d)
182 {
183         float width = v3d->rasterizer->base.line_width;
184 
185         if (v3d_line_smoothing_enabled(v3d)) {
186                 /* If line smoothing is enabled then we want to add some extra
187                  * pixels to the width in order to have some semi-transparent
188                  * edges.
189                  */
190                 width = floorf(M_SQRT2 * width) + 3;
191         }
192 
193         return width;
194 }
195 
196 void
v3d_ensure_prim_counts_allocated(struct v3d_context * ctx)197 v3d_ensure_prim_counts_allocated(struct v3d_context *ctx)
198 {
199         if (ctx->prim_counts)
200                 return;
201 
202         /* Init all 7 counters and 1 padding to 0 */
203         uint32_t zeroes[8] = { 0 };
204         u_upload_data(ctx->uploader,
205                       0, sizeof(zeroes), 32, zeroes,
206                       &ctx->prim_counts_offset,
207                       &ctx->prim_counts);
208 }
209 
210 void
v3d_flag_dirty_sampler_state(struct v3d_context * v3d,enum pipe_shader_type shader)211 v3d_flag_dirty_sampler_state(struct v3d_context *v3d,
212                              enum pipe_shader_type shader)
213 {
214         switch (shader) {
215         case PIPE_SHADER_VERTEX:
216                 v3d->dirty |= V3D_DIRTY_VERTTEX;
217                 break;
218         case PIPE_SHADER_GEOMETRY:
219                 v3d->dirty |= V3D_DIRTY_GEOMTEX;
220                 break;
221         case PIPE_SHADER_FRAGMENT:
222                 v3d->dirty |= V3D_DIRTY_FRAGTEX;
223                 break;
224         case PIPE_SHADER_COMPUTE:
225                 v3d->dirty |= V3D_DIRTY_COMPTEX;
226                 break;
227         default:
228                 unreachable("Unsupported shader stage");
229         }
230 }
231 
232 void
v3d_create_texture_shader_state_bo(struct v3d_context * v3d,struct v3d_sampler_view * so)233 v3d_create_texture_shader_state_bo(struct v3d_context *v3d,
234                                    struct v3d_sampler_view *so)
235 {
236         if (v3d->screen->devinfo.ver >= 41)
237                 v3d41_create_texture_shader_state_bo(v3d, so);
238         else
239                 v3d33_create_texture_shader_state_bo(v3d, so);
240 }
241 
242 void
v3d_get_tile_buffer_size(bool is_msaa,uint32_t nr_cbufs,struct pipe_surface ** cbufs,struct pipe_surface * bbuf,uint32_t * tile_width,uint32_t * tile_height,uint32_t * max_bpp)243 v3d_get_tile_buffer_size(bool is_msaa,
244                          uint32_t nr_cbufs,
245                          struct pipe_surface **cbufs,
246                          struct pipe_surface *bbuf,
247                          uint32_t *tile_width,
248                          uint32_t *tile_height,
249                          uint32_t *max_bpp)
250 {
251         static const uint8_t tile_sizes[] = {
252                 64, 64,
253                 64, 32,
254                 32, 32,
255                 32, 16,
256                 16, 16,
257         };
258         int tile_size_index = 0;
259         if (is_msaa)
260                 tile_size_index += 2;
261 
262         if (cbufs[3] || cbufs[2])
263                 tile_size_index += 2;
264         else if (cbufs[1])
265                 tile_size_index++;
266 
267         *max_bpp = 0;
268         for (int i = 0; i < nr_cbufs; i++) {
269                 if (cbufs[i]) {
270                         struct v3d_surface *surf = v3d_surface(cbufs[i]);
271                         *max_bpp = MAX2(*max_bpp, surf->internal_bpp);
272                 }
273         }
274 
275         if (bbuf) {
276                 struct v3d_surface *bsurf = v3d_surface(bbuf);
277                 assert(bbuf->texture->nr_samples <= 1 || is_msaa);
278                 *max_bpp = MAX2(*max_bpp, bsurf->internal_bpp);
279         }
280 
281         tile_size_index += *max_bpp;
282 
283         assert(tile_size_index < ARRAY_SIZE(tile_sizes));
284         *tile_width = tile_sizes[tile_size_index * 2 + 0];
285         *tile_height = tile_sizes[tile_size_index * 2 + 1];
286 }
287 
288 static void
v3d_context_destroy(struct pipe_context * pctx)289 v3d_context_destroy(struct pipe_context *pctx)
290 {
291         struct v3d_context *v3d = v3d_context(pctx);
292 
293         v3d_flush(pctx);
294 
295         if (v3d->blitter)
296                 util_blitter_destroy(v3d->blitter);
297 
298         if (v3d->uploader)
299                 u_upload_destroy(v3d->uploader);
300         if (v3d->state_uploader)
301                 u_upload_destroy(v3d->state_uploader);
302 
303         if (v3d->prim_counts)
304                 pipe_resource_reference(&v3d->prim_counts, NULL);
305 
306         slab_destroy_child(&v3d->transfer_pool);
307 
308         pipe_surface_reference(&v3d->framebuffer.cbufs[0], NULL);
309         pipe_surface_reference(&v3d->framebuffer.zsbuf, NULL);
310 
311         if (v3d->sand8_blit_vs)
312                 pctx->delete_vs_state(pctx, v3d->sand8_blit_vs);
313         if (v3d->sand8_blit_fs_luma)
314                 pctx->delete_fs_state(pctx, v3d->sand8_blit_fs_luma);
315         if (v3d->sand8_blit_fs_chroma)
316                 pctx->delete_fs_state(pctx, v3d->sand8_blit_fs_chroma);
317 
318         v3d_program_fini(pctx);
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         struct v3d_context *v3d = v3d_context(pctx);
329 
330         if (sample_count <= 1) {
331                 xy[0] = 0.5;
332                 xy[1] = 0.5;
333         } else {
334                 static const int xoffsets_v33[] = { 1, -3, 3, -1 };
335                 static const int xoffsets_v42[] = { -1, 3, -3, 1 };
336                 const int *xoffsets = (v3d->screen->devinfo.ver >= 42 ?
337                                        xoffsets_v42 : xoffsets_v33);
338 
339                 xy[0] = 0.5 + xoffsets[sample_index] * .125;
340                 xy[1] = .125 + sample_index * .25;
341         }
342 }
343 
344 struct pipe_context *
v3d_context_create(struct pipe_screen * pscreen,void * priv,unsigned flags)345 v3d_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
346 {
347         struct v3d_screen *screen = v3d_screen(pscreen);
348         struct v3d_context *v3d;
349 
350         /* Prevent dumping of the shaders built during context setup. */
351         uint32_t saved_shaderdb_flag = V3D_DEBUG & V3D_DEBUG_SHADERDB;
352         V3D_DEBUG &= ~V3D_DEBUG_SHADERDB;
353 
354         v3d = rzalloc(NULL, struct v3d_context);
355         if (!v3d)
356                 return NULL;
357         struct pipe_context *pctx = &v3d->base;
358 
359         v3d->screen = screen;
360 
361         int ret = drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
362                                    &v3d->out_sync);
363         if (ret) {
364                 ralloc_free(v3d);
365                 return NULL;
366         }
367 
368         pctx->screen = pscreen;
369         pctx->priv = priv;
370         pctx->destroy = v3d_context_destroy;
371         pctx->flush = v3d_pipe_flush;
372         pctx->memory_barrier = v3d_memory_barrier;
373         pctx->set_debug_callback = v3d_set_debug_callback;
374         pctx->invalidate_resource = v3d_invalidate_resource;
375         pctx->get_sample_position = v3d_get_sample_position;
376 
377         if (screen->devinfo.ver >= 41) {
378                 v3d41_draw_init(pctx);
379                 v3d41_state_init(pctx);
380         } else {
381                 v3d33_draw_init(pctx);
382                 v3d33_state_init(pctx);
383         }
384         v3d_program_init(pctx);
385         v3d_query_init(pctx);
386         v3d_resource_context_init(pctx);
387 
388         v3d_job_init(v3d);
389 
390         v3d->fd = screen->fd;
391 
392         slab_create_child(&v3d->transfer_pool, &screen->transfer_pool);
393 
394         v3d->uploader = u_upload_create_default(&v3d->base);
395         v3d->base.stream_uploader = v3d->uploader;
396         v3d->base.const_uploader = v3d->uploader;
397         v3d->state_uploader = u_upload_create(&v3d->base,
398                                               4096,
399                                               PIPE_BIND_CONSTANT_BUFFER,
400                                               PIPE_USAGE_STREAM, 0);
401 
402         v3d->blitter = util_blitter_create(pctx);
403         if (!v3d->blitter)
404                 goto fail;
405         v3d->blitter->use_index_buffer = true;
406 
407         V3D_DEBUG |= saved_shaderdb_flag;
408 
409         v3d->sample_mask = (1 << V3D_MAX_SAMPLES) - 1;
410         v3d->active_queries = true;
411 
412         return &v3d->base;
413 
414 fail:
415         pctx->destroy(pctx);
416         return NULL;
417 }
418