Home | History | Annotate | Download | only in vc4
      1 /*
      2  * Copyright  2014 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/ralloc.h"
     29 #include "util/u_inlines.h"
     30 #include "util/u_memory.h"
     31 #include "util/u_blitter.h"
     32 #include "util/u_upload_mgr.h"
     33 #include "indices/u_primconvert.h"
     34 #include "pipe/p_screen.h"
     35 
     36 #include "vc4_screen.h"
     37 #include "vc4_context.h"
     38 #include "vc4_resource.h"
     39 
     40 void
     41 vc4_flush(struct pipe_context *pctx)
     42 {
     43         struct vc4_context *vc4 = vc4_context(pctx);
     44 
     45         struct hash_entry *entry;
     46         hash_table_foreach(vc4->jobs, entry) {
     47                 struct vc4_job *job = entry->data;
     48                 vc4_job_submit(vc4, job);
     49         }
     50 }
     51 
     52 static void
     53 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
     54                unsigned flags)
     55 {
     56         struct vc4_context *vc4 = vc4_context(pctx);
     57 
     58         vc4_flush(pctx);
     59 
     60         if (fence) {
     61                 struct pipe_screen *screen = pctx->screen;
     62                 struct vc4_fence *f = vc4_fence_create(vc4->screen,
     63                                                        vc4->last_emit_seqno);
     64                 screen->fence_reference(screen, fence, NULL);
     65                 *fence = (struct pipe_fence_handle *)f;
     66         }
     67 }
     68 
     69 static void
     70 vc4_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
     71 {
     72         struct vc4_context *vc4 = vc4_context(pctx);
     73         struct vc4_resource *rsc = vc4_resource(prsc);
     74 
     75         rsc->initialized_buffers = 0;
     76 
     77         struct hash_entry *entry = _mesa_hash_table_search(vc4->write_jobs,
     78                                                            prsc);
     79         if (!entry)
     80                 return;
     81 
     82         struct vc4_job *job = entry->data;
     83         if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
     84                 job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
     85 }
     86 
     87 static void
     88 vc4_context_destroy(struct pipe_context *pctx)
     89 {
     90         struct vc4_context *vc4 = vc4_context(pctx);
     91 
     92         vc4_flush(pctx);
     93 
     94         if (vc4->blitter)
     95                 util_blitter_destroy(vc4->blitter);
     96 
     97         if (vc4->primconvert)
     98                 util_primconvert_destroy(vc4->primconvert);
     99 
    100         if (vc4->uploader)
    101                 u_upload_destroy(vc4->uploader);
    102 
    103         slab_destroy_child(&vc4->transfer_pool);
    104 
    105         pipe_surface_reference(&vc4->framebuffer.cbufs[0], NULL);
    106         pipe_surface_reference(&vc4->framebuffer.zsbuf, NULL);
    107 
    108         vc4_program_fini(pctx);
    109 
    110         ralloc_free(vc4);
    111 }
    112 
    113 struct pipe_context *
    114 vc4_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
    115 {
    116         struct vc4_screen *screen = vc4_screen(pscreen);
    117         struct vc4_context *vc4;
    118 
    119         /* Prevent dumping of the shaders built during context setup. */
    120         uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
    121         vc4_debug &= ~VC4_DEBUG_SHADERDB;
    122 
    123         vc4 = rzalloc(NULL, struct vc4_context);
    124         if (!vc4)
    125                 return NULL;
    126         struct pipe_context *pctx = &vc4->base;
    127 
    128         vc4->screen = screen;
    129 
    130         pctx->screen = pscreen;
    131         pctx->priv = priv;
    132         pctx->destroy = vc4_context_destroy;
    133         pctx->flush = vc4_pipe_flush;
    134         pctx->invalidate_resource = vc4_invalidate_resource;
    135 
    136         vc4_draw_init(pctx);
    137         vc4_state_init(pctx);
    138         vc4_program_init(pctx);
    139         vc4_query_init(pctx);
    140         vc4_resource_context_init(pctx);
    141 
    142         vc4_job_init(vc4);
    143 
    144         vc4->fd = screen->fd;
    145 
    146         slab_create_child(&vc4->transfer_pool, &screen->transfer_pool);
    147         vc4->blitter = util_blitter_create(pctx);
    148         if (!vc4->blitter)
    149                 goto fail;
    150 
    151         vc4->primconvert = util_primconvert_create(pctx,
    152                                                    (1 << PIPE_PRIM_QUADS) - 1);
    153         if (!vc4->primconvert)
    154                 goto fail;
    155 
    156         vc4->uploader = u_upload_create(pctx, 16 * 1024,
    157                                         PIPE_BIND_INDEX_BUFFER,
    158                                         PIPE_USAGE_STREAM);
    159 
    160         vc4_debug |= saved_shaderdb_flag;
    161 
    162         vc4->sample_mask = (1 << VC4_MAX_SAMPLES) - 1;
    163 
    164         return &vc4->base;
    165 
    166 fail:
    167         pctx->destroy(pctx);
    168         return NULL;
    169 }
    170