Home | History | Annotate | Download | only in nvc0
      1 /*
      2  * Copyright 2010 Christoph Bumiller
      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 shall be included in
     12  * all copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     17  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     18  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
     19  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     20  * SOFTWARE.
     21  */
     22 
     23 #include "pipe/p_defines.h"
     24 #include "util/u_framebuffer.h"
     25 
     26 #ifdef NVC0_WITH_DRAW_MODULE
     27 #include "draw/draw_context.h"
     28 #endif
     29 
     30 #include "nvc0_context.h"
     31 #include "nvc0_screen.h"
     32 #include "nvc0_resource.h"
     33 
     34 static void
     35 nvc0_flush(struct pipe_context *pipe,
     36            struct pipe_fence_handle **fence)
     37 {
     38    struct nvc0_context *nvc0 = nvc0_context(pipe);
     39    struct nouveau_screen *screen = &nvc0->screen->base;
     40 
     41    if (fence)
     42       nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
     43 
     44    PUSH_KICK(nvc0->base.pushbuf); /* fencing handled in kick_notify */
     45 }
     46 
     47 static void
     48 nvc0_texture_barrier(struct pipe_context *pipe)
     49 {
     50    struct nouveau_pushbuf *push = nvc0_context(pipe)->base.pushbuf;
     51 
     52    IMMED_NVC0(push, NVC0_3D(SERIALIZE), 0);
     53    IMMED_NVC0(push, NVC0_3D(TEX_CACHE_CTL), 0);
     54 }
     55 
     56 static void
     57 nvc0_context_unreference_resources(struct nvc0_context *nvc0)
     58 {
     59    unsigned s, i;
     60 
     61    nouveau_bufctx_del(&nvc0->bufctx_3d);
     62    nouveau_bufctx_del(&nvc0->bufctx);
     63 
     64    util_unreference_framebuffer_state(&nvc0->framebuffer);
     65 
     66    for (i = 0; i < nvc0->num_vtxbufs; ++i)
     67       pipe_resource_reference(&nvc0->vtxbuf[i].buffer, NULL);
     68 
     69    pipe_resource_reference(&nvc0->idxbuf.buffer, NULL);
     70 
     71    for (s = 0; s < 5; ++s) {
     72       for (i = 0; i < nvc0->num_textures[s]; ++i)
     73          pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
     74 
     75       for (i = 0; i < NVC0_MAX_PIPE_CONSTBUFS; ++i)
     76          if (!nvc0->constbuf[s][i].user)
     77             pipe_resource_reference(&nvc0->constbuf[s][i].u.buf, NULL);
     78    }
     79 
     80    for (i = 0; i < nvc0->num_tfbbufs; ++i)
     81       pipe_so_target_reference(&nvc0->tfbbuf[i], NULL);
     82 }
     83 
     84 static void
     85 nvc0_destroy(struct pipe_context *pipe)
     86 {
     87    struct nvc0_context *nvc0 = nvc0_context(pipe);
     88 
     89    if (nvc0->screen->cur_ctx == nvc0) {
     90       nvc0->base.pushbuf->kick_notify = NULL;
     91       nvc0->screen->cur_ctx = NULL;
     92       nouveau_pushbuf_bufctx(nvc0->base.pushbuf, NULL);
     93    }
     94    nouveau_pushbuf_kick(nvc0->base.pushbuf, nvc0->base.pushbuf->channel);
     95 
     96    nvc0_context_unreference_resources(nvc0);
     97 
     98 #ifdef NVC0_WITH_DRAW_MODULE
     99    draw_destroy(nvc0->draw);
    100 #endif
    101 
    102    nouveau_context_destroy(&nvc0->base);
    103 }
    104 
    105 void
    106 nvc0_default_kick_notify(struct nouveau_pushbuf *push)
    107 {
    108    struct nvc0_screen *screen = push->user_priv;
    109 
    110    if (screen) {
    111       nouveau_fence_next(&screen->base);
    112       nouveau_fence_update(&screen->base, TRUE);
    113       if (screen->cur_ctx)
    114          screen->cur_ctx->state.flushed = TRUE;
    115    }
    116 }
    117 
    118 struct pipe_context *
    119 nvc0_create(struct pipe_screen *pscreen, void *priv)
    120 {
    121    struct nvc0_screen *screen = nvc0_screen(pscreen);
    122    struct nvc0_context *nvc0;
    123    struct pipe_context *pipe;
    124    int ret;
    125    uint32_t flags;
    126 
    127    nvc0 = CALLOC_STRUCT(nvc0_context);
    128    if (!nvc0)
    129       return NULL;
    130    pipe = &nvc0->base.pipe;
    131 
    132    nvc0->base.pushbuf = screen->base.pushbuf;
    133 
    134    ret = nouveau_bufctx_new(screen->base.client, NVC0_BIND_COUNT,
    135                             &nvc0->bufctx_3d);
    136    if (!ret)
    137       nouveau_bufctx_new(screen->base.client, 2, &nvc0->bufctx);
    138    if (ret)
    139       goto out_err;
    140 
    141    nvc0->screen = screen;
    142    nvc0->base.screen = &screen->base;
    143 
    144    pipe->screen = pscreen;
    145    pipe->priv = priv;
    146 
    147    pipe->destroy = nvc0_destroy;
    148 
    149    pipe->draw_vbo = nvc0_draw_vbo;
    150    pipe->clear = nvc0_clear;
    151 
    152    pipe->flush = nvc0_flush;
    153    pipe->texture_barrier = nvc0_texture_barrier;
    154 
    155    if (!screen->cur_ctx) {
    156       screen->cur_ctx = nvc0;
    157       nouveau_pushbuf_bufctx(screen->base.pushbuf, nvc0->bufctx);
    158    }
    159    screen->base.pushbuf->kick_notify = nvc0_default_kick_notify;
    160 
    161    nvc0_init_query_functions(nvc0);
    162    nvc0_init_surface_functions(nvc0);
    163    nvc0_init_state_functions(nvc0);
    164    nvc0_init_transfer_functions(nvc0);
    165    nvc0_init_resource_functions(pipe);
    166 
    167 #ifdef NVC0_WITH_DRAW_MODULE
    168    /* no software fallbacks implemented */
    169    nvc0->draw = draw_create(pipe);
    170    assert(nvc0->draw);
    171    draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
    172 #endif
    173 
    174    nouveau_context_init_vdec(&nvc0->base);
    175 
    176    /* shader builtin library is per-screen, but we need a context for m2mf */
    177    nvc0_program_library_upload(nvc0);
    178 
    179    /* add permanently resident buffers to bufctxts */
    180 
    181    flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
    182 
    183    BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->text);
    184    BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->uniform_bo);
    185    BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->txc);
    186    BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->poly_cache);
    187 
    188    flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
    189 
    190    BCTX_REFN_bo(nvc0->bufctx_3d, SCREEN, flags, screen->fence.bo);
    191    BCTX_REFN_bo(nvc0->bufctx, FENCE, flags, screen->fence.bo);
    192 
    193    nvc0->base.scratch.bo_size = 2 << 20;
    194 
    195    return pipe;
    196 
    197 out_err:
    198    if (nvc0) {
    199       if (nvc0->bufctx_3d)
    200          nouveau_bufctx_del(&nvc0->bufctx_3d);
    201       if (nvc0->bufctx)
    202          nouveau_bufctx_del(&nvc0->bufctx);
    203       FREE(nvc0);
    204    }
    205    return NULL;
    206 }
    207 
    208 void
    209 nvc0_bufctx_fence(struct nvc0_context *nvc0, struct nouveau_bufctx *bufctx,
    210                   boolean on_flush)
    211 {
    212    struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
    213    struct nouveau_list *it;
    214 
    215    for (it = list->next; it != list; it = it->next) {
    216       struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
    217       struct nv04_resource *res = ref->priv;
    218       if (res)
    219          nvc0_resource_validate(res, (unsigned)ref->priv_data);
    220    }
    221 }
    222