Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20.  * OTHER DEALINGS IN THE SOFTWARE.
  21.  */
  22.  
  23. #include "pipe/p_defines.h"
  24. #include "util/u_framebuffer.h"
  25.  
  26. #include "nv50/nv50_context.h"
  27. #include "nv50/nv50_screen.h"
  28. #include "nv50/nv50_resource.h"
  29.  
  30. static void
  31. nv50_flush(struct pipe_context *pipe,
  32.            struct pipe_fence_handle **fence,
  33.            unsigned flags)
  34. {
  35.    struct nouveau_screen *screen = nouveau_screen(pipe->screen);
  36.  
  37.    if (fence)
  38.       nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
  39.  
  40.    PUSH_KICK(screen->pushbuf);
  41.  
  42.    nouveau_context_update_frame_stats(nouveau_context(pipe));
  43. }
  44.  
  45. static void
  46. nv50_texture_barrier(struct pipe_context *pipe)
  47. {
  48.    struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
  49.  
  50.    BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
  51.    PUSH_DATA (push, 0);
  52.    BEGIN_NV04(push, NV50_3D(TEX_CACHE_CTL), 1);
  53.    PUSH_DATA (push, 0x20);
  54. }
  55.  
  56. static void
  57. nv50_memory_barrier(struct pipe_context *pipe, unsigned flags)
  58. {
  59.    struct nv50_context *nv50 = nv50_context(pipe);
  60.    int i, s;
  61.  
  62.    if (flags & PIPE_BARRIER_MAPPED_BUFFER) {
  63.       for (i = 0; i < nv50->num_vtxbufs; ++i) {
  64.          if (!nv50->vtxbuf[i].buffer)
  65.             continue;
  66.          if (nv50->vtxbuf[i].buffer->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
  67.             nv50->base.vbo_dirty = TRUE;
  68.       }
  69.  
  70.       if (nv50->idxbuf.buffer &&
  71.           nv50->idxbuf.buffer->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
  72.          nv50->base.vbo_dirty = TRUE;
  73.  
  74.       for (s = 0; s < 3 && !nv50->cb_dirty; ++s) {
  75.          uint32_t valid = nv50->constbuf_valid[s];
  76.  
  77.          while (valid && !nv50->cb_dirty) {
  78.             const unsigned i = ffs(valid) - 1;
  79.             struct pipe_resource *res;
  80.  
  81.             valid &= ~(1 << i);
  82.             if (nv50->constbuf[s][i].user)
  83.                continue;
  84.  
  85.             res = nv50->constbuf[s][i].u.buf;
  86.             if (!res)
  87.                continue;
  88.  
  89.             if (res->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
  90.                nv50->cb_dirty = TRUE;
  91.          }
  92.       }
  93.    }
  94. }
  95.  
  96. void
  97. nv50_default_kick_notify(struct nouveau_pushbuf *push)
  98. {
  99.    struct nv50_screen *screen = push->user_priv;
  100.  
  101.    if (screen) {
  102.       nouveau_fence_next(&screen->base);
  103.       nouveau_fence_update(&screen->base, TRUE);
  104.       if (screen->cur_ctx)
  105.          screen->cur_ctx->state.flushed = TRUE;
  106.    }
  107. }
  108.  
  109. static void
  110. nv50_context_unreference_resources(struct nv50_context *nv50)
  111. {
  112.    unsigned s, i;
  113.  
  114.    nouveau_bufctx_del(&nv50->bufctx_3d);
  115.    nouveau_bufctx_del(&nv50->bufctx);
  116.  
  117.    util_unreference_framebuffer_state(&nv50->framebuffer);
  118.  
  119.    assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
  120.    for (i = 0; i < nv50->num_vtxbufs; ++i)
  121.       pipe_resource_reference(&nv50->vtxbuf[i].buffer, NULL);
  122.  
  123.    pipe_resource_reference(&nv50->idxbuf.buffer, NULL);
  124.  
  125.    for (s = 0; s < 3; ++s) {
  126.       assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
  127.       for (i = 0; i < nv50->num_textures[s]; ++i)
  128.          pipe_sampler_view_reference(&nv50->textures[s][i], NULL);
  129.  
  130.       for (i = 0; i < NV50_MAX_PIPE_CONSTBUFS; ++i)
  131.          if (!nv50->constbuf[s][i].user)
  132.             pipe_resource_reference(&nv50->constbuf[s][i].u.buf, NULL);
  133.    }
  134. }
  135.  
  136. static void
  137. nv50_destroy(struct pipe_context *pipe)
  138. {
  139.    struct nv50_context *nv50 = nv50_context(pipe);
  140.  
  141.    if (nv50->screen->cur_ctx == nv50) {
  142.       nv50->screen->cur_ctx = NULL;
  143.       /* Save off the state in case another context gets created */
  144.       nv50->screen->save_state = nv50->state;
  145.    }
  146.    nouveau_pushbuf_bufctx(nv50->base.pushbuf, NULL);
  147.    nouveau_pushbuf_kick(nv50->base.pushbuf, nv50->base.pushbuf->channel);
  148.  
  149.    nv50_context_unreference_resources(nv50);
  150.  
  151.    FREE(nv50->blit);
  152.  
  153.    nouveau_context_destroy(&nv50->base);
  154. }
  155.  
  156. static int
  157. nv50_invalidate_resource_storage(struct nouveau_context *ctx,
  158.                                  struct pipe_resource *res,
  159.                                  int ref)
  160. {
  161.    struct nv50_context *nv50 = nv50_context(&ctx->pipe);
  162.    unsigned s, i;
  163.  
  164.    if (res->bind & PIPE_BIND_RENDER_TARGET) {
  165.       assert(nv50->framebuffer.nr_cbufs <= PIPE_MAX_COLOR_BUFS);
  166.       for (i = 0; i < nv50->framebuffer.nr_cbufs; ++i) {
  167.          if (nv50->framebuffer.cbufs[i] &&
  168.              nv50->framebuffer.cbufs[i]->texture == res) {
  169.             nv50->dirty |= NV50_NEW_FRAMEBUFFER;
  170.             nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
  171.             if (!--ref)
  172.                return ref;
  173.          }
  174.       }
  175.    }
  176.    if (res->bind & PIPE_BIND_DEPTH_STENCIL) {
  177.       if (nv50->framebuffer.zsbuf &&
  178.           nv50->framebuffer.zsbuf->texture == res) {
  179.          nv50->dirty |= NV50_NEW_FRAMEBUFFER;
  180.          nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
  181.          if (!--ref)
  182.             return ref;
  183.       }
  184.    }
  185.  
  186.    if (res->bind & (PIPE_BIND_VERTEX_BUFFER |
  187.                     PIPE_BIND_INDEX_BUFFER |
  188.                     PIPE_BIND_CONSTANT_BUFFER |
  189.                     PIPE_BIND_STREAM_OUTPUT |
  190.                     PIPE_BIND_SAMPLER_VIEW)) {
  191.  
  192.       assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
  193.       for (i = 0; i < nv50->num_vtxbufs; ++i) {
  194.          if (nv50->vtxbuf[i].buffer == res) {
  195.             nv50->dirty |= NV50_NEW_ARRAYS;
  196.             nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_VERTEX);
  197.             if (!--ref)
  198.                return ref;
  199.          }
  200.       }
  201.  
  202.       if (nv50->idxbuf.buffer == res)
  203.          if (!--ref)
  204.             return ref;
  205.  
  206.       for (s = 0; s < 3; ++s) {
  207.       assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
  208.       for (i = 0; i < nv50->num_textures[s]; ++i) {
  209.          if (nv50->textures[s][i] &&
  210.              nv50->textures[s][i]->texture == res) {
  211.             nv50->dirty |= NV50_NEW_TEXTURES;
  212.             nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_TEXTURES);
  213.             if (!--ref)
  214.                return ref;
  215.          }
  216.       }
  217.       }
  218.  
  219.       for (s = 0; s < 3; ++s) {
  220.       for (i = 0; i < NV50_MAX_PIPE_CONSTBUFS; ++i) {
  221.          if (!(nv50->constbuf_valid[s] & (1 << i)))
  222.             continue;
  223.          if (!nv50->constbuf[s][i].user &&
  224.              nv50->constbuf[s][i].u.buf == res) {
  225.             nv50->dirty |= NV50_NEW_CONSTBUF;
  226.             nv50->constbuf_dirty[s] |= 1 << i;
  227.             nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_CB(s, i));
  228.             if (!--ref)
  229.                return ref;
  230.          }
  231.       }
  232.       }
  233.    }
  234.  
  235.    return ref;
  236. }
  237.  
  238. static void
  239. nv50_context_get_sample_position(struct pipe_context *, unsigned, unsigned,
  240.                                  float *);
  241.  
  242. struct pipe_context *
  243. nv50_create(struct pipe_screen *pscreen, void *priv)
  244. {
  245.    struct nv50_screen *screen = nv50_screen(pscreen);
  246.    struct nv50_context *nv50;
  247.    struct pipe_context *pipe;
  248.    int ret;
  249.    uint32_t flags;
  250.  
  251.    nv50 = CALLOC_STRUCT(nv50_context);
  252.    if (!nv50)
  253.       return NULL;
  254.    pipe = &nv50->base.pipe;
  255.  
  256.    if (!nv50_blitctx_create(nv50))
  257.       goto out_err;
  258.  
  259.    nv50->base.pushbuf = screen->base.pushbuf;
  260.    nv50->base.client = screen->base.client;
  261.  
  262.    ret = nouveau_bufctx_new(screen->base.client, NV50_BIND_COUNT,
  263.                             &nv50->bufctx_3d);
  264.    if (!ret)
  265.       ret = nouveau_bufctx_new(screen->base.client, 2, &nv50->bufctx);
  266.    if (ret)
  267.       goto out_err;
  268.  
  269.    nv50->base.screen    = &screen->base;
  270.    nv50->base.copy_data = nv50_m2mf_copy_linear;
  271.    nv50->base.push_data = nv50_sifc_linear_u8;
  272.    /* FIXME: Make it possible to use this again. The problem is that there is
  273.     * some clever logic in the card that allows for multiple renders to happen
  274.     * when there are only constbuf changes. However that relies on the
  275.     * constbuf updates happening to the right constbuf slots. Currently
  276.     * implementation just makes it go through a separate slot which doesn't
  277.     * properly update the right constbuf data.
  278.    nv50->base.push_cb   = nv50_cb_push;
  279.     */
  280.  
  281.    nv50->screen = screen;
  282.    pipe->screen = pscreen;
  283.    pipe->priv = priv;
  284.  
  285.    pipe->destroy = nv50_destroy;
  286.  
  287.    pipe->draw_vbo = nv50_draw_vbo;
  288.    pipe->clear = nv50_clear;
  289.  
  290.    pipe->flush = nv50_flush;
  291.    pipe->texture_barrier = nv50_texture_barrier;
  292.    pipe->memory_barrier = nv50_memory_barrier;
  293.    pipe->get_sample_position = nv50_context_get_sample_position;
  294.  
  295.    if (!screen->cur_ctx) {
  296.       /* Restore the last context's state here, normally handled during
  297.        * context switch
  298.        */
  299.       nv50->state = screen->save_state;
  300.       screen->cur_ctx = nv50;
  301.       nouveau_pushbuf_bufctx(screen->base.pushbuf, nv50->bufctx);
  302.    }
  303.    nv50->base.pushbuf->kick_notify = nv50_default_kick_notify;
  304.  
  305.    nv50_init_query_functions(nv50);
  306.    nv50_init_surface_functions(nv50);
  307.    nv50_init_state_functions(nv50);
  308.    nv50_init_resource_functions(pipe);
  309.  
  310.    nv50->base.invalidate_resource_storage = nv50_invalidate_resource_storage;
  311.  
  312.    if (screen->base.device->chipset < 0x84 ||
  313.        debug_get_bool_option("NOUVEAU_PMPEG", FALSE)) {
  314.       /* PMPEG */
  315.       nouveau_context_init_vdec(&nv50->base);
  316.    } else if (screen->base.device->chipset < 0x98 ||
  317.               screen->base.device->chipset == 0xa0) {
  318.       /* VP2 */
  319.       pipe->create_video_codec = nv84_create_decoder;
  320.       pipe->create_video_buffer = nv84_video_buffer_create;
  321.    } else {
  322.       /* VP3/4 */
  323.       pipe->create_video_codec = nv98_create_decoder;
  324.       pipe->create_video_buffer = nv98_video_buffer_create;
  325.    }
  326.  
  327.    flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
  328.  
  329.    BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->code);
  330.    BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->uniforms);
  331.    BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->txc);
  332.    BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->stack_bo);
  333.  
  334.    flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
  335.  
  336.    BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->fence.bo);
  337.    BCTX_REFN_bo(nv50->bufctx, FENCE, flags, screen->fence.bo);
  338.  
  339.    nv50->base.scratch.bo_size = 2 << 20;
  340.  
  341.    return pipe;
  342.  
  343. out_err:
  344.    if (nv50->bufctx_3d)
  345.       nouveau_bufctx_del(&nv50->bufctx_3d);
  346.    if (nv50->bufctx)
  347.       nouveau_bufctx_del(&nv50->bufctx);
  348.    FREE(nv50->blit);
  349.    FREE(nv50);
  350.    return NULL;
  351. }
  352.  
  353. void
  354. nv50_bufctx_fence(struct nouveau_bufctx *bufctx, boolean on_flush)
  355. {
  356.    struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
  357.    struct nouveau_list *it;
  358.  
  359.    for (it = list->next; it != list; it = it->next) {
  360.       struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
  361.       struct nv04_resource *res = ref->priv;
  362.       if (res)
  363.          nv50_resource_validate(res, (unsigned)ref->priv_data);
  364.    }
  365. }
  366.  
  367. static void
  368. nv50_context_get_sample_position(struct pipe_context *pipe,
  369.                                  unsigned sample_count, unsigned sample_index,
  370.                                  float *xy)
  371. {
  372.    static const uint8_t ms1[1][2] = { { 0x8, 0x8 } };
  373.    static const uint8_t ms2[2][2] = {
  374.       { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
  375.    static const uint8_t ms4[4][2] = {
  376.       { 0x6, 0x2 }, { 0xe, 0x6 },   /* (0,0), (1,0) */
  377.       { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
  378.    static const uint8_t ms8[8][2] = {
  379.       { 0x1, 0x7 }, { 0x5, 0x3 },   /* (0,0), (1,0) */
  380.       { 0x3, 0xd }, { 0x7, 0xb },   /* (0,1), (1,1) */
  381.       { 0x9, 0x5 }, { 0xf, 0x1 },   /* (2,0), (3,0) */
  382.       { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
  383. #if 0
  384.    /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
  385.    static const uint8_t ms8_alt[8][2] = {
  386.       { 0x9, 0x5 }, { 0x7, 0xb },   /* (2,0), (1,1) */
  387.       { 0xd, 0x9 }, { 0x5, 0x3 },   /* (3,1), (1,0) */
  388.       { 0x3, 0xd }, { 0x1, 0x7 },   /* (0,1), (0,0) */
  389.       { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
  390. #endif
  391.  
  392.    const uint8_t (*ptr)[2];
  393.  
  394.    switch (sample_count) {
  395.    case 0:
  396.    case 1: ptr = ms1; break;
  397.    case 2: ptr = ms2; break;
  398.    case 4: ptr = ms4; break;
  399.    case 8: ptr = ms8; break;
  400.    default:
  401.       assert(0);
  402.       return; /* bad sample count -> undefined locations */
  403.    }
  404.    xy[0] = ptr[sample_index][0] * 0.0625f;
  405.    xy[1] = ptr[sample_index][1] * 0.0625f;
  406. }
  407.