14c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
2a582b859f73c06e4fc7d4b17d8aaab9060fb9498Christoph Bumiller#include "util/u_math.h"
3a582b859f73c06e4fc7d4b17d8aaab9060fb9498Christoph Bumiller
44c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "nvc0_context.h"
54c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
64c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
74c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_zcull(struct nvc0_context *nvc0)
84c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
96d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    struct nouveau_pushbuf *push = nvc0->base.pushbuf;
104c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
11cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller    struct nv50_surface *sf = nv50_surface(fb->zsbuf);
12cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller    struct nv50_miptree *mt = nv50_miptree(sf->base.texture);
134c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    struct nouveau_bo *bo = mt->base.bo;
144c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    uint32_t size;
154c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    uint32_t offset = align(mt->total_size, 1 << 17);
164c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    unsigned width, height;
174c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
18ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller    assert(mt->base.base.depth0 == 1 && mt->base.base.array_size < 2);
19ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller
204c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    size = mt->total_size * 2;
214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
224c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    height = align(fb->height, 32);
234c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    width = fb->width % 224;
244c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    if (width)
254c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller       width = fb->width + (224 - width);
264c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    else
274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller       width = fb->width;
284c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
296d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(ZCULL_REGION), 1);
306d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, 0);
316d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(ZCULL_ADDRESS_HIGH), 2);
326d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAh(push, bo->offset + offset);
336d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, bo->offset + offset);
344c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    offset += 1 << 17;
356d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(ZCULL_LIMIT_HIGH), 2);
366d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAh(push, bo->offset + offset);
376d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, bo->offset + offset);
386d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, SUBC_3D(0x07e0), 2);
396d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, size);
406d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, size >> 16);
416d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, SUBC_3D(0x15c8), 1); /* bits 0x3 */
426d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, 2);
436d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(ZCULL_WIDTH), 4);
446d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, width);
456d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, height);
466d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, 1);
476d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, 0);
486d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(ZCULL_WINDOW_OFFSET_X), 2);
496d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, 0);
506d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, 0);
516d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(ZCULL_INVALIDATE), 1);
526d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, 0);
534c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
544c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
554c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
564c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_fb(struct nvc0_context *nvc0)
574c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
586d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    struct nouveau_pushbuf *push = nvc0->base.pushbuf;
594c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
604c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    unsigned i;
61b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller    unsigned ms_mode = NVC0_3D_MULTISAMPLE_MODE_MS1;
6267c7aefea33a7935e42ede30463eb7ca5009b068Christoph Bumiller    boolean serialize = FALSE;
634c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
646d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_FB);
654c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
666d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(RT_CONTROL), 1);
676d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, (076543210 << 4) | fb->nr_cbufs);
686d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(SCREEN_SCISSOR_HORIZ), 2);
696d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, fb->width << 16);
706d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, fb->height << 16);
71bf1df06773d6eca8b71a687f838edccd1a6c9cb8Christoph Bumiller
724c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    for (i = 0; i < fb->nr_cbufs; ++i) {
73cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller        struct nv50_surface *sf = nv50_surface(fb->cbufs[i]);
7428271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller        struct nv04_resource *res = nv04_resource(sf->base.texture);
7528271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller        struct nouveau_bo *bo = res->bo;
76bf1df06773d6eca8b71a687f838edccd1a6c9cb8Christoph Bumiller
776d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        BEGIN_NVC0(push, NVC0_3D(RT_ADDRESS_HIGH(i)), 9);
786d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATAh(push, res->address + sf->offset);
796d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, res->address + sf->offset);
806d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        if (likely(nouveau_bo_memtype(bo))) {
8128271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller           struct nv50_miptree *mt = nv50_miptree(sf->base.texture);
8228271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
8328271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller           assert(sf->base.texture->target != PIPE_BUFFER);
8428271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
856d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, sf->width);
866d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, sf->height);
876d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, nvc0_format_table[sf->base.format].rt);
886d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, (mt->layout_3d << 16) |
8928271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller                    mt->level[sf->base.u.tex.level].tile_mode);
906d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, sf->base.u.tex.first_layer + sf->depth);
916d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, mt->layer_stride >> 2);
926d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, sf->base.u.tex.first_layer);
9328271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
9428271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller           ms_mode = mt->ms_mode;
9528271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller        } else {
9628271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller           if (res->base.target == PIPE_BUFFER) {
976d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller              PUSH_DATA(push, 262144);
986d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller              PUSH_DATA(push, 1);
9928271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller           } else {
1006d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller              PUSH_DATA(push, nv50_miptree(sf->base.texture)->level[0].pitch);
1016d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller              PUSH_DATA(push, sf->height);
10228271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller           }
1036d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, nvc0_format_table[sf->base.format].rt);
1046d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, 1 << 12);
1056d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, 1);
1066d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, 0);
1076d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller           PUSH_DATA(push, 0);
10828271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
10928271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller           nvc0_resource_fence(res, NOUVEAU_BO_WR);
11028271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
11128271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller           assert(!fb->zsbuf);
11228271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller        }
11328271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller
11428271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller        if (res->status & NOUVEAU_BUFFER_STATUS_GPU_READING)
11567c7aefea33a7935e42ede30463eb7ca5009b068Christoph Bumiller           serialize = TRUE;
11628271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller        res->status |=  NOUVEAU_BUFFER_STATUS_GPU_WRITING;
11728271fd00dc5dd83f95b5cb890e0ab2c0ff6159dChristoph Bumiller        res->status &= ~NOUVEAU_BUFFER_STATUS_GPU_READING;
11867c7aefea33a7935e42ede30463eb7ca5009b068Christoph Bumiller
1194388817a678431146744a030bc7c0b8c01da9f72Christoph Bumiller        /* only register for writing, otherwise we'd always serialize here */
1206d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        BCTX_REFN(nvc0->bufctx_3d, FB, res, WR);
1214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    }
1224c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1234c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    if (fb->zsbuf) {
124cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller        struct nv50_miptree *mt = nv50_miptree(fb->zsbuf->texture);
125cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller        struct nv50_surface *sf = nv50_surface(fb->zsbuf);
126ca5deb0c355cc4a120b754a228ff5f51007fbceaChristoph Bumiller        int unk = mt->base.base.target == PIPE_TEXTURE_2D;
1276d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
1286d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        BEGIN_NVC0(push, NVC0_3D(ZETA_ADDRESS_HIGH), 5);
1296d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATAh(push, mt->base.address + sf->offset);
1306d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, mt->base.address + sf->offset);
1316d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, nvc0_format_table[fb->zsbuf->format].rt);
1326d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, mt->level[sf->base.u.tex.level].tile_mode);
1336d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, mt->layer_stride >> 2);
1346d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        BEGIN_NVC0(push, NVC0_3D(ZETA_ENABLE), 1);
1356d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, 1);
1366d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        BEGIN_NVC0(push, NVC0_3D(ZETA_HORIZ), 3);
1376d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, sf->width);
1386d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, sf->height);
1396d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, (unk << 16) |
1404fae7da9a3a3849ca08ffc6fcbdccc6a9c065ad2Christoph Bumiller                   (sf->base.u.tex.first_layer + sf->depth));
1416d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        BEGIN_NVC0(push, NVC0_3D(ZETA_BASE_LAYER), 1);
1426d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, sf->base.u.tex.first_layer);
1434c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
144b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller        ms_mode = mt->ms_mode;
145b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller
1464826cd0f6125b071530026143ffd8205d84b3d5eBen Skeggs        if (mt->base.status & NOUVEAU_BUFFER_STATUS_GPU_READING)
14767c7aefea33a7935e42ede30463eb7ca5009b068Christoph Bumiller           serialize = TRUE;
1484826cd0f6125b071530026143ffd8205d84b3d5eBen Skeggs        mt->base.status |=  NOUVEAU_BUFFER_STATUS_GPU_WRITING;
1494826cd0f6125b071530026143ffd8205d84b3d5eBen Skeggs        mt->base.status &= ~NOUVEAU_BUFFER_STATUS_GPU_READING;
15067c7aefea33a7935e42ede30463eb7ca5009b068Christoph Bumiller
1516d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        BCTX_REFN(nvc0->bufctx_3d, FB, &mt->base, WR);
1524c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    } else {
1536d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        BEGIN_NVC0(push, NVC0_3D(ZETA_ENABLE), 1);
1546d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA (push, 0);
1554c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    }
15667c7aefea33a7935e42ede30463eb7ca5009b068Christoph Bumiller
1576d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    IMMED_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), ms_mode);
158b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller
1596d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    if (serialize)
1606d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller       IMMED_NVC0(push, NVC0_3D(SERIALIZE), 0);
1614c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
1624c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1634c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
1644c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_blend_colour(struct nvc0_context *nvc0)
1654c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
1666d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
1674c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1686d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(BLEND_COLOR(0)), 4);
1696d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAf(push, nvc0->blend_colour.color[0]);
1706d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAf(push, nvc0->blend_colour.color[1]);
1716d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAf(push, nvc0->blend_colour.color[2]);
1726d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAf(push, nvc0->blend_colour.color[3]);
1734c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
1744c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1754c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
1764c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_stencil_ref(struct nvc0_context *nvc0)
1774c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
1786d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    struct nouveau_pushbuf *push = nvc0->base.pushbuf;
179d9ef97696770c8a6b04e4adf9a813614f9b3c741Christoph Bumiller    const ubyte *ref = &nvc0->stencil_ref.ref_value[0];
1804c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1816d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    IMMED_NVC0(push, NVC0_3D(STENCIL_FRONT_FUNC_REF), ref[0]);
1826d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    IMMED_NVC0(push, NVC0_3D(STENCIL_BACK_FUNC_REF), ref[1]);
1834c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
1844c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1854c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
1864c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_stipple(struct nvc0_context *nvc0)
1874c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
1886d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    struct nouveau_pushbuf *push = nvc0->base.pushbuf;
1894c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    unsigned i;
1904c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1916d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(POLYGON_STIPPLE_PATTERN(0)), 32);
1924c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    for (i = 0; i < 32; ++i)
1936d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller        PUSH_DATA(push, util_bswap32(nvc0->stipple.stipple[i]));
1944c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
1954c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1964c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
1974c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_scissor(struct nvc0_context *nvc0)
1984c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
1996d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    struct nouveau_pushbuf *push = nvc0->base.pushbuf;
2004c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    struct pipe_scissor_state *s = &nvc0->scissor;
2014c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
202e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    if (!(nvc0->dirty & NVC0_NEW_SCISSOR) &&
203e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller        nvc0->rast->pipe.scissor == nvc0->state.scissor)
2044c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller       return;
2054c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    nvc0->state.scissor = nvc0->rast->pipe.scissor;
2064c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
2076d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(SCISSOR_HORIZ(0)), 2);
208e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    if (nvc0->rast->pipe.scissor) {
2096d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller       PUSH_DATA(push, (s->maxx << 16) | s->minx);
2106d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller       PUSH_DATA(push, (s->maxy << 16) | s->miny);
2114c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    } else {
2126d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller       PUSH_DATA(push, (0xffff << 16) | 0);
2136d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller       PUSH_DATA(push, (0xffff << 16) | 0);
2144c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    }
2154c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
2164c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
2174c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
2184c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_viewport(struct nvc0_context *nvc0)
2194c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
2206d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    struct nouveau_pushbuf *push = nvc0->base.pushbuf;
221e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    struct pipe_viewport_state *vp = &nvc0->viewport;
222e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    int x, y, w, h;
223e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    float zmin, zmax;
2244c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
2256d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(VIEWPORT_TRANSLATE_X(0)), 3);
2266d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAf(push, vp->translate[0]);
2276d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAf(push, vp->translate[1]);
2286d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAf(push, vp->translate[2]);
2296d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(VIEWPORT_SCALE_X(0)), 3);
2306d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAf(push, vp->scale[0]);
2316d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAf(push, vp->scale[1]);
2326d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAf(push, vp->scale[2]);
233d047168d81cfeb39a98f3ae16416872facc6237cChristoph Bumiller
234e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    /* now set the viewport rectangle to viewport dimensions for clipping */
235e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller
236a582b859f73c06e4fc7d4b17d8aaab9060fb9498Christoph Bumiller    x = util_iround(MAX2(0.0f, vp->translate[0] - fabsf(vp->scale[0])));
237a582b859f73c06e4fc7d4b17d8aaab9060fb9498Christoph Bumiller    y = util_iround(MAX2(0.0f, vp->translate[1] - fabsf(vp->scale[1])));
238a582b859f73c06e4fc7d4b17d8aaab9060fb9498Christoph Bumiller    w = util_iround(vp->translate[0] + fabsf(vp->scale[0])) - x;
239a582b859f73c06e4fc7d4b17d8aaab9060fb9498Christoph Bumiller    h = util_iround(vp->translate[1] + fabsf(vp->scale[1])) - y;
240a582b859f73c06e4fc7d4b17d8aaab9060fb9498Christoph Bumiller
241e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    zmin = vp->translate[2] - fabsf(vp->scale[2]);
242e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    zmax = vp->translate[2] + fabsf(vp->scale[2]);
243e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller
2446d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(VIEWPORT_HORIZ(0)), 2);
2456d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, (w << 16) | x);
2466d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATA (push, (h << 16) | y);
2476d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    BEGIN_NVC0(push, NVC0_3D(DEPTH_RANGE_NEAR(0)), 2);
2486d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAf(push, zmin);
2496d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    PUSH_DATAf(push, zmax);
2504c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
2514c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
252dc4c821f0817a3db716f965692fb701079f66340Marek Olšákstatic INLINE void
253e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumillernvc0_upload_uclip_planes(struct nvc0_context *nvc0, unsigned s)
254dc4c821f0817a3db716f965692fb701079f66340Marek Olšák{
2556d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
256e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   struct nouveau_bo *bo = nvc0->screen->uniform_bo;
257dc4c821f0817a3db716f965692fb701079f66340Marek Olšák
2586d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
259e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   PUSH_DATA (push, 512);
260e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   PUSH_DATAh(push, bo->offset + (5 << 16) + (s << 9));
261e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   PUSH_DATA (push, bo->offset + (5 << 16) + (s << 9));
2626d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_1IC0(push, NVC0_3D(CB_POS), PIPE_MAX_CLIP_PLANES * 4 + 1);
263e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   PUSH_DATA (push, 256);
2646d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAp(push, &nvc0->clip.ucp[0][0], PIPE_MAX_CLIP_PLANES * 4);
265dc4c821f0817a3db716f965692fb701079f66340Marek Olšák}
266dc4c821f0817a3db716f965692fb701079f66340Marek Olšák
267dc4c821f0817a3db716f965692fb701079f66340Marek Olšákstatic INLINE void
268dc4c821f0817a3db716f965692fb701079f66340Marek Olšáknvc0_check_program_ucps(struct nvc0_context *nvc0,
269dc4c821f0817a3db716f965692fb701079f66340Marek Olšák                        struct nvc0_program *vp, uint8_t mask)
270dc4c821f0817a3db716f965692fb701079f66340Marek Olšák{
271dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   const unsigned n = util_logbase2(mask) + 1;
272dc4c821f0817a3db716f965692fb701079f66340Marek Olšák
273dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   if (vp->vp.num_ucps >= n)
274dc4c821f0817a3db716f965692fb701079f66340Marek Olšák      return;
275dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   nvc0_program_destroy(nvc0, vp);
276dc4c821f0817a3db716f965692fb701079f66340Marek Olšák
277dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   vp->vp.num_ucps = n;
278dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   if (likely(vp == nvc0->vertprog))
279dc4c821f0817a3db716f965692fb701079f66340Marek Olšák      nvc0_vertprog_validate(nvc0);
280dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   else
281dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   if (likely(vp == nvc0->gmtyprog))
282dc4c821f0817a3db716f965692fb701079f66340Marek Olšák      nvc0_vertprog_validate(nvc0);
283dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   else
284dc4c821f0817a3db716f965692fb701079f66340Marek Olšák      nvc0_tevlprog_validate(nvc0);
285dc4c821f0817a3db716f965692fb701079f66340Marek Olšák}
286dc4c821f0817a3db716f965692fb701079f66340Marek Olšák
2874c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
2884c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_clip(struct nvc0_context *nvc0)
2894c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
2906d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
291dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   struct nvc0_program *vp;
292e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   unsigned stage;
293b85e93c0ca5ce881a59412b3ae617b5ac2c3aeb6Christoph Bumiller   uint8_t clip_enable = nvc0->rast->pipe.clip_plane_enable;
2944c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
295e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   if (nvc0->gmtyprog) {
296e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller      stage = 3;
297e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller      vp = nvc0->gmtyprog;
298e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   } else
299e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   if (nvc0->tevlprog) {
300e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller      stage = 2;
301dc4c821f0817a3db716f965692fb701079f66340Marek Olšák      vp = nvc0->tevlprog;
302e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   } else {
303e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller      stage = 0;
304e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller      vp = nvc0->vertprog;
305dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   }
3065138ac033ad3708e2b82f2beebc887f65a77309eChristoph Bumiller
307b85e93c0ca5ce881a59412b3ae617b5ac2c3aeb6Christoph Bumiller   if (clip_enable && vp->vp.num_ucps < PIPE_MAX_CLIP_PLANES)
308b85e93c0ca5ce881a59412b3ae617b5ac2c3aeb6Christoph Bumiller      nvc0_check_program_ucps(nvc0, vp, clip_enable);
309b85e93c0ca5ce881a59412b3ae617b5ac2c3aeb6Christoph Bumiller
310e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller   if (nvc0->dirty & (NVC0_NEW_CLIP | (NVC0_NEW_VERTPROG << stage)))
311f09ee76c9892dc9ddbd4f74c485c4be8b55033a7Christoph Bumiller      if (vp->vp.num_ucps > 0 && vp->vp.num_ucps <= PIPE_MAX_CLIP_PLANES)
312e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller         nvc0_upload_uclip_planes(nvc0, stage);
313e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller
314b85e93c0ca5ce881a59412b3ae617b5ac2c3aeb6Christoph Bumiller   clip_enable &= vp->vp.clip_enable;
3155138ac033ad3708e2b82f2beebc887f65a77309eChristoph Bumiller
316dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   if (nvc0->state.clip_enable != clip_enable) {
317dc4c821f0817a3db716f965692fb701079f66340Marek Olšák      nvc0->state.clip_enable = clip_enable;
3186d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(CLIP_DISTANCE_ENABLE), clip_enable);
319dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   }
320dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   if (nvc0->state.clip_mode != vp->vp.clip_mode) {
321dc4c821f0817a3db716f965692fb701079f66340Marek Olšák      nvc0->state.clip_mode = vp->vp.clip_mode;
3226d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(CLIP_DISTANCE_MODE), 1);
3236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, vp->vp.clip_mode);
3245138ac033ad3708e2b82f2beebc887f65a77309eChristoph Bumiller   }
3254c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
3264c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
3284c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_blend(struct nvc0_context *nvc0)
3294c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
3306d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
3314c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3326d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_SPACE(push, nvc0->blend->size);
3336d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAp(push, nvc0->blend->state, nvc0->blend->size);
3344c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
3354c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3364c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
3374c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_zsa(struct nvc0_context *nvc0)
3384c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
3396d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
3404c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3416d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_SPACE(push, nvc0->zsa->size);
3426d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAp(push, nvc0->zsa->state, nvc0->zsa->size);
3434c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
3444c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3454c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
3464c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_validate_rasterizer(struct nvc0_context *nvc0)
3474c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
3486d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
3494c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3506d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_SPACE(push, nvc0->rast->size);
3516d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAp(push, nvc0->rast->state, nvc0->rast->size);
3524c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
3534c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3544c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
3554c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_constbufs_validate(struct nvc0_context *nvc0)
3564c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
3576d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
3584c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   unsigned s;
3594c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
3604c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   for (s = 0; s < 5; ++s) {
3614c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      while (nvc0->constbuf_dirty[s]) {
362fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller         int i = ffs(nvc0->constbuf_dirty[s]) - 1;
3634c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller         nvc0->constbuf_dirty[s] &= ~(1 << i);
3644c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
365fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller         if (nvc0->constbuf[s][i].user) {
366fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            struct nouveau_bo *bo = nvc0->screen->uniform_bo;
367fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            const unsigned base = s << 16;
368fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            const unsigned size = nvc0->constbuf[s][0].size;
369fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            assert(i == 0); /* we really only want OpenGL uniforms here */
370fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            assert(nvc0->constbuf[s][0].u.data);
371fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller
372fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            if (nvc0->state.uniform_buffer_bound[s] < size) {
373fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               nvc0->state.uniform_buffer_bound[s] = align(size, 0x100);
374fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller
375fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
376fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATA (push, nvc0->state.uniform_buffer_bound[s]);
377fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATAh(push, bo->offset + base);
378fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATA (push, bo->offset + base);
379fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               BEGIN_NVC0(push, NVC0_3D(CB_BIND(s)), 1);
380fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATA (push, (0 << 4) | 1);
3817fa7229560d1f3be17199685b34ab254afada24fChristoph Bumiller            }
382fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            nvc0_cb_push(&nvc0->base, bo, NOUVEAU_BO_VRAM,
383fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller                         base, nvc0->state.uniform_buffer_bound[s],
384fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller                         0, (size + 3) / 4,
385fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller                         nvc0->constbuf[s][0].u.data);
3864c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller         } else {
387fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            struct nv04_resource *res =
388fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               nv04_resource(nvc0->constbuf[s][i].u.buf);
389fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            if (res) {
390fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
391fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATA (push, nvc0->constbuf[s][i].size);
392fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATAh(push, res->address + nvc0->constbuf[s][i].offset);
393fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATA (push, res->address + nvc0->constbuf[s][i].offset);
394fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               BEGIN_NVC0(push, NVC0_3D(CB_BIND(s)), 1);
395fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATA (push, (i << 4) | 1);
396fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller
397fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               BCTX_REFN(nvc0->bufctx_3d, CB(s, i), res, RD);
398fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            } else {
399fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               BEGIN_NVC0(push, NVC0_3D(CB_BIND(s)), 1);
400fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller               PUSH_DATA (push, (i << 4) | 0);
401fcb28682101dfa127486373411043d5d97a0ff8fChristoph Bumiller            }
4027fa7229560d1f3be17199685b34ab254afada24fChristoph Bumiller            if (i == 0)
403f0f1cce9622d0bd21882071654733a6e5d41e7b0Christoph Bumiller               nvc0->state.uniform_buffer_bound[s] = 0;
4047fa7229560d1f3be17199685b34ab254afada24fChristoph Bumiller         }
4054c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      }
4064c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
4074c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
4084c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
409f0ee7d8bb46ac7694693f3fb8cd0f15c422f1befChristoph Bumillerstatic void
410b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumillernvc0_validate_sample_mask(struct nvc0_context *nvc0)
411b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller{
4126d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
413b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller
414b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller   unsigned mask[4] =
415b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller   {
416b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller      nvc0->sample_mask & 0xffff,
417b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller      nvc0->sample_mask & 0xffff,
418b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller      nvc0->sample_mask & 0xffff,
419b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller      nvc0->sample_mask & 0xffff
420b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller   };
421b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller
4226d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(MSAA_MASK(0)), 4);
4236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, mask[0]);
4246d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, mask[1]);
4256d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, mask[2]);
4266d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, mask[3]);
4276d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(SAMPLE_SHADING), 1);
4286d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, 0x01);
429b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller}
430b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller
431b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumillerstatic void
432f0ee7d8bb46ac7694693f3fb8cd0f15c422f1befChristoph Bumillernvc0_validate_derived_1(struct nvc0_context *nvc0)
433f0ee7d8bb46ac7694693f3fb8cd0f15c422f1befChristoph Bumiller{
4346d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
43514bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   boolean rasterizer_discard;
436f0ee7d8bb46ac7694693f3fb8cd0f15c422f1befChristoph Bumiller
43714bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   rasterizer_discard = (!nvc0->fragprog || !nvc0->fragprog->hdr[18]) &&
43814bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller      !nvc0->zsa->pipe.depth.enabled && !nvc0->zsa->pipe.stencil[0].enabled;
43914bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   rasterizer_discard = rasterizer_discard ||
44014bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller      nvc0->rast->pipe.rasterizer_discard;
44114bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller
44214bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   if (rasterizer_discard != nvc0->state.rasterizer_discard) {
44314bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller      nvc0->state.rasterizer_discard = rasterizer_discard;
4446d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(RASTERIZE_ENABLE), !rasterizer_discard);
44514bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   }
446f0ee7d8bb46ac7694693f3fb8cd0f15c422f1befChristoph Bumiller}
447f0ee7d8bb46ac7694693f3fb8cd0f15c422f1befChristoph Bumiller
448e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumillerstatic void
449e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumillernvc0_switch_pipe_context(struct nvc0_context *ctx_to)
450e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller{
451e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   struct nvc0_context *ctx_from = ctx_to->screen->cur_ctx;
452784f49e69624cba07616fd5a22ccb80ad3b5111bChristoph Bumiller   unsigned s;
453e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller
454e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   if (ctx_from)
455e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller      ctx_to->state = ctx_from->state;
456e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller
457e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   ctx_to->dirty = ~0;
458e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller
459784f49e69624cba07616fd5a22ccb80ad3b5111bChristoph Bumiller   for (s = 0; s < 5; ++s) {
460784f49e69624cba07616fd5a22ccb80ad3b5111bChristoph Bumiller      ctx_to->samplers_dirty[s] = ~0;
461784f49e69624cba07616fd5a22ccb80ad3b5111bChristoph Bumiller      ctx_to->textures_dirty[s] = ~0;
462784f49e69624cba07616fd5a22ccb80ad3b5111bChristoph Bumiller   }
463784f49e69624cba07616fd5a22ccb80ad3b5111bChristoph Bumiller
464e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   if (!ctx_to->vertex)
465e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller      ctx_to->dirty &= ~(NVC0_NEW_VERTEX | NVC0_NEW_ARRAYS);
46691fb5e0394f702a795f317c6c78af7e9676e46c3Christoph Bumiller   if (!ctx_to->idxbuf.buffer)
46791fb5e0394f702a795f317c6c78af7e9676e46c3Christoph Bumiller      ctx_to->dirty &= ~NVC0_NEW_IDXBUF;
468e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller
469e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   if (!ctx_to->vertprog)
470e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller      ctx_to->dirty &= ~NVC0_NEW_VERTPROG;
471e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   if (!ctx_to->fragprog)
472e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller      ctx_to->dirty &= ~NVC0_NEW_FRAGPROG;
473e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller
474e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   if (!ctx_to->blend)
475e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller      ctx_to->dirty &= ~NVC0_NEW_BLEND;
476e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   if (!ctx_to->rast)
477222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller      ctx_to->dirty &= ~(NVC0_NEW_RASTERIZER | NVC0_NEW_SCISSOR);
478e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   if (!ctx_to->zsa)
479e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller      ctx_to->dirty &= ~NVC0_NEW_ZSA;
480e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller
4813069a7eaa5d83e7f41616347ba244c0dc0d944aeChristoph Bumiller   ctx_to->screen->cur_ctx = ctx_to;
482e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller}
483e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller
4844c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic struct state_validate {
4854c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    void (*func)(struct nvc0_context *);
4864c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    uint32_t states;
4874c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller} validate_list[] = {
4884c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_fb,            NVC0_NEW_FRAMEBUFFER },
4894c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_blend,         NVC0_NEW_BLEND },
4904c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_zsa,           NVC0_NEW_ZSA },
491b2dcf880e8bcd61be59602f5a2d18c77a5fc60c1Christoph Bumiller    { nvc0_validate_sample_mask,   NVC0_NEW_SAMPLE_MASK },
4924c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_rasterizer,    NVC0_NEW_RASTERIZER },
4934c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_blend_colour,  NVC0_NEW_BLEND_COLOUR },
4944c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_stencil_ref,   NVC0_NEW_STENCIL_REF },
4954c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_stipple,       NVC0_NEW_STIPPLE },
496e7845e319679e3539274c37e9c16692a2dfe59feChristoph Bumiller    { nvc0_validate_scissor,       NVC0_NEW_SCISSOR | NVC0_NEW_RASTERIZER },
4974c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_viewport,      NVC0_NEW_VIEWPORT },
4984c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_vertprog_validate,      NVC0_NEW_VERTPROG },
4994c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_tctlprog_validate,      NVC0_NEW_TCTLPROG },
5004c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_tevlprog_validate,      NVC0_NEW_TEVLPROG },
5014c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_gmtyprog_validate,      NVC0_NEW_GMTYPROG },
5024c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_fragprog_validate,      NVC0_NEW_FRAGPROG },
50314bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller    { nvc0_validate_derived_1,     NVC0_NEW_FRAGPROG | NVC0_NEW_ZSA |
50414bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller                                   NVC0_NEW_RASTERIZER },
505dc4c821f0817a3db716f965692fb701079f66340Marek Olšák    { nvc0_validate_clip,          NVC0_NEW_CLIP | NVC0_NEW_RASTERIZER |
506dc4c821f0817a3db716f965692fb701079f66340Marek Olšák                                   NVC0_NEW_VERTPROG |
507dc4c821f0817a3db716f965692fb701079f66340Marek Olšák                                   NVC0_NEW_TEVLPROG |
508dc4c821f0817a3db716f965692fb701079f66340Marek Olšák                                   NVC0_NEW_GMTYPROG },
5097fa7229560d1f3be17199685b34ab254afada24fChristoph Bumiller    { nvc0_constbufs_validate,     NVC0_NEW_CONSTBUF },
5104c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_textures,      NVC0_NEW_TEXTURES },
5114c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    { nvc0_validate_samplers,      NVC0_NEW_SAMPLERS },
512e44089b2f79aa2dcaacf348911433d1e21235c0cChristoph Bumiller    { nve4_set_tex_handles,        NVC0_NEW_TEXTURES | NVC0_NEW_SAMPLERS },
513f8a7a0b6f30ff38b2743860cbc4caeab102c2c29Christoph Bumiller    { nvc0_vertex_arrays_validate, NVC0_NEW_VERTEX | NVC0_NEW_ARRAYS },
5146d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller    { nvc0_idxbuf_validate,        NVC0_NEW_IDXBUF },
51514bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller    { nvc0_tfb_validate,           NVC0_NEW_TFB_TARGETS | NVC0_NEW_GMTYPROG }
5164c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller};
5174c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#define validate_list_len (sizeof(validate_list) / sizeof(validate_list[0]))
5184c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5194c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerboolean
520222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumillernvc0_state_validate(struct nvc0_context *nvc0, uint32_t mask, unsigned words)
5214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
522222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller   uint32_t state_mask;
5236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   int ret;
5244c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   unsigned i;
525e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller
526e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller   if (nvc0->screen->cur_ctx != nvc0)
527e864ccb3f20e58acb31572708ea6fcc696cf6aceChristoph Bumiller      nvc0_switch_pipe_context(nvc0);
5284c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
529222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller   state_mask = nvc0->dirty & mask;
530222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller
531222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller   if (state_mask) {
5324c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      for (i = 0; i < validate_list_len; ++i) {
5334c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller         struct state_validate *validate = &validate_list[i];
5344c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
535222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller         if (state_mask & validate->states)
5364c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            validate->func(nvc0);
5374c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      }
538222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller      nvc0->dirty &= ~state_mask;
5396d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
5406d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      nvc0_bufctx_fence(nvc0, nvc0->bufctx_3d, FALSE);
5414c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
5424c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5436d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   nouveau_pushbuf_bufctx(nvc0->base.pushbuf, nvc0->bufctx_3d);
5446d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   ret = nouveau_pushbuf_validate(nvc0->base.pushbuf);
5456d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   if (unlikely(ret))
5466d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      return FALSE;
547222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller
5486d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   if (unlikely(nvc0->state.flushed))
5496d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      nvc0_bufctx_fence(nvc0, nvc0->bufctx_3d, TRUE);
5504c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5514c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   return TRUE;
5524c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
553