14c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller/*
24c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * Copyright 2010 Christoph Bumiller
34c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller *
44c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * Permission is hereby granted, free of charge, to any person obtaining a
54c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * copy of this software and associated documentation files (the "Software"),
64c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * to deal in the Software without restriction, including without limitation
74c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * and/or sell copies of the Software, and to permit persons to whom the
94c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * Software is furnished to do so, subject to the following conditions:
104c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller *
114c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * The above copyright notice and this permission notice shall be included in
124c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * all copies or substantial portions of the Software.
134c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller *
144c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
184c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
194c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
204c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller * SOFTWARE.
214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller */
224c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller#define NVC0_PUSH_EXPLICIT_SPACE_CHECKING
246d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
254c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "pipe/p_context.h"
264c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "pipe/p_state.h"
274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "util/u_inlines.h"
284c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "util/u_format.h"
294c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "translate/translate.h"
304c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
314c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "nvc0_context.h"
324c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "nvc0_resource.h"
334c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
344c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#include "nvc0_3d.xml.h"
354c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
364c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillervoid
374c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_vertex_state_delete(struct pipe_context *pipe,
384c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         void *hwcso)
394c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
404c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   struct nvc0_vertex_stateobj *so = hwcso;
414c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
424c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (so->translate)
434c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      so->translate->release(so->translate);
444c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   FREE(hwcso);
454c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
464c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
474c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillervoid *
484c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_vertex_state_create(struct pipe_context *pipe,
494c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         unsigned num_elements,
504c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         const struct pipe_vertex_element *elements)
514c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
524c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    struct nvc0_vertex_stateobj *so;
534c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    struct translate_key transkey;
544c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    unsigned i;
55e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller    unsigned src_offset_max = 0;
564c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
574c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    so = MALLOC(sizeof(*so) +
58f8a7a0b6f30ff38b2743860cbc4caeab102c2c29Christoph Bumiller                num_elements * sizeof(struct nvc0_vertex_element));
594c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    if (!so)
604c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        return NULL;
614c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    so->num_elements = num_elements;
62a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller    so->instance_elts = 0;
63a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller    so->instance_bufs = 0;
64e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller    so->shared_slots = FALSE;
6517d680cc537acf8a967d9e36f7006afab560122aChristoph Bumiller    so->need_conversion = FALSE;
664c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
670e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller    memset(so->vb_access_size, 0, sizeof(so->vb_access_size));
680e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller
69e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller    for (i = 0; i < PIPE_MAX_ATTRIBS; ++i)
70e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller       so->min_instance_div[i] = 0xffffffff;
71e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
724c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    transkey.nr_elements = 0;
734c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    transkey.output_stride = 0;
744c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
754c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    for (i = 0; i < num_elements; ++i) {
764c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        const struct pipe_vertex_element *ve = &elements[i];
774c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        const unsigned vbi = ve->vertex_buffer_index;
780e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller        unsigned size;
794c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        enum pipe_format fmt = ve->src_format;
804c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
814c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        so->element[i].pipe = elements[i];
824c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        so->element[i].state = nvc0_format_table[fmt].vtx;
834c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
844c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        if (!so->element[i].state) {
854c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            switch (util_format_get_nr_components(fmt)) {
864c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            case 1: fmt = PIPE_FORMAT_R32_FLOAT; break;
874c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            case 2: fmt = PIPE_FORMAT_R32G32_FLOAT; break;
884c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            case 3: fmt = PIPE_FORMAT_R32G32B32_FLOAT; break;
894c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            case 4: fmt = PIPE_FORMAT_R32G32B32A32_FLOAT; break;
904c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            default:
914c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                assert(0);
924c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                return NULL;
934c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            }
944c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            so->element[i].state = nvc0_format_table[fmt].vtx;
9517d680cc537acf8a967d9e36f7006afab560122aChristoph Bumiller            so->need_conversion = TRUE;
964c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        }
970e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller        size = util_format_get_blocksize(fmt);
980e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller
99e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller        src_offset_max = MAX2(src_offset_max, ve->src_offset);
100e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
1010e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller        if (so->vb_access_size[vbi] < (ve->src_offset + size))
1020e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller           so->vb_access_size[vbi] = ve->src_offset + size;
103ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
104ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller        if (unlikely(ve->instance_divisor)) {
105ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller           so->instance_elts |= 1 << i;
106ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller           so->instance_bufs |= 1 << vbi;
107e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller           if (ve->instance_divisor < so->min_instance_div[vbi])
108e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller              so->min_instance_div[vbi] = ve->instance_divisor;
109ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller        }
1104c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
111a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller        if (1) {
112ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            unsigned ca;
1134c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            unsigned j = transkey.nr_elements++;
1144c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
115ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            ca = util_format_description(fmt)->channel[0].size / 8;
116ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            if (ca != 1 && ca != 2)
117ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller               ca = 4;
118ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
1194c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            transkey.element[j].type = TRANSLATE_ELEMENT_NORMAL;
1204c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            transkey.element[j].input_format = ve->src_format;
1214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            transkey.element[j].input_buffer = vbi;
1224c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            transkey.element[j].input_offset = ve->src_offset;
1234c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            transkey.element[j].instance_divisor = ve->instance_divisor;
1244c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
125ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            transkey.output_stride = align(transkey.output_stride, ca);
1264c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            transkey.element[j].output_format = fmt;
1274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller            transkey.element[j].output_offset = transkey.output_stride;
1280e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller            transkey.output_stride += size;
129a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller
130ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            so->element[i].state_alt = so->element[i].state;
131ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            so->element[i].state_alt |= transkey.element[j].output_offset << 7;
1324c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller        }
133ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
134ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller        so->element[i].state |= i << NVC0_3D_VERTEX_ATTRIB_FORMAT_BUFFER__SHIFT;
1354c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    }
136ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller    transkey.output_stride = align(transkey.output_stride, 4);
1374c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
138ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller    so->size = transkey.output_stride;
1394c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    so->translate = translate_create(&transkey);
1404c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
141e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller    if (so->instance_elts || src_offset_max >= (1 << 14))
142e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller       return so;
143e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller    so->shared_slots = TRUE;
144e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
145e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller    for (i = 0; i < num_elements; ++i) {
146e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller       const unsigned b = elements[i].vertex_buffer_index;
147e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller       const unsigned s = elements[i].src_offset;
148e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller       so->element[i].state &= ~NVC0_3D_VERTEX_ATTRIB_FORMAT_BUFFER__MASK;
149e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller       so->element[i].state |= b << NVC0_3D_VERTEX_ATTRIB_FORMAT_BUFFER__SHIFT;
150e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller       so->element[i].state |= s << NVC0_3D_VERTEX_ATTRIB_FORMAT_OFFSET__SHIFT;
151e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller    }
1524c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller    return so;
1534c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
1544c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
1554c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#define NVC0_3D_VERTEX_ATTRIB_INACTIVE                                       \
1564c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_FLOAT |                                 \
1574c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32 | NVC0_3D_VERTEX_ATTRIB_FORMAT_CONST
1584c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
159a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller#define VTX_ATTR(a, c, t, s)                            \
160a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller   ((NVC0_3D_VTX_ATTR_DEFINE_TYPE_##t) |                \
161a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller    (NVC0_3D_VTX_ATTR_DEFINE_SIZE_##s) |                \
162a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller    ((a) << NVC0_3D_VTX_ATTR_DEFINE_ATTR__SHIFT) |      \
163a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller    ((c) << NVC0_3D_VTX_ATTR_DEFINE_COMP__SHIFT))
164a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller
165a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumillerstatic void
166e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillernvc0_set_constant_vertex_attrib(struct nvc0_context *nvc0, const unsigned a)
167a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller{
168e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
169e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   struct pipe_vertex_element *ve = &nvc0->vertex->element[a].pipe;
170e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   struct pipe_vertex_buffer *vb = &nvc0->vtxbuf[ve->vertex_buffer_index];
171e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   uint32_t mode;
172e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   const struct util_format_description *desc;
173e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   void *dst;
174e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   const void *src = (const uint8_t *)vb->user_buffer + ve->src_offset;
175e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   assert(!vb->buffer);
176a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller
177e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   desc = util_format_description(ve->src_format);
178edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller
179e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   PUSH_SPACE(push, 6);
180e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(VTX_ATTR_DEFINE), 5);
181e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   dst = &push->cur[1];
182e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   if (desc->channel[0].pure_integer) {
183e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
184e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         mode = VTX_ATTR(a, 4, SINT, 32);
185e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         desc->unpack_rgba_sint(dst, 0, src, 0, 1, 1);
186edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller      } else {
187e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         mode = VTX_ATTR(a, 4, UINT, 32);
188e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         desc->unpack_rgba_uint(dst, 0, src, 0, 1, 1);
189edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller      }
190e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   } else {
191e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      mode = VTX_ATTR(a, 4, FLOAT, 32);
192e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      desc->unpack_rgba_float(dst, 0, src, 0, 1, 1);
193edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   }
194e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   push->cur[0] = mode;
195e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   push->cur += 5;
196a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller}
197a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller
198a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumillerstatic INLINE void
199e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillernvc0_user_vbuf_range(struct nvc0_context *nvc0, int vbi,
200e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller                     uint32_t *base, uint32_t *size)
201a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller{
202a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller   if (unlikely(nvc0->vertex->instance_bufs & (1 << vbi))) {
203e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      const uint32_t div = nvc0->vertex->min_instance_div[vbi];
204e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      *base = nvc0->instance_off * nvc0->vtxbuf[vbi].stride;
205e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      *size = (nvc0->instance_max / div) * nvc0->vtxbuf[vbi].stride +
206e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         nvc0->vertex->vb_access_size[vbi];
207a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller   } else {
2080e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller      /* NOTE: if there are user buffers, we *must* have index bounds */
2090e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller      assert(nvc0->vb_elt_limit != ~0);
2100e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller      *base = nvc0->vb_elt_first * nvc0->vtxbuf[vbi].stride;
2110e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller      *size = nvc0->vb_elt_limit * nvc0->vtxbuf[vbi].stride +
2120e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller         nvc0->vertex->vb_access_size[vbi];
213a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller   }
214a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller}
215a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller
216e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillerstatic INLINE void
217e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillernvc0_release_user_vbufs(struct nvc0_context *nvc0)
2184c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
219e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   if (nvc0->vbo_user) {
220e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_VTX_TMP);
221e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nouveau_scratch_done(&nvc0->base);
2221ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller   }
2231ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller}
2241ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller
2251ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumillerstatic void
2261ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumillernvc0_update_user_vbufs(struct nvc0_context *nvc0)
2271ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller{
228e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   uint64_t address[PIPE_MAX_ATTRIBS];
2296d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
2301ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller   int i;
2311ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller   uint32_t written = 0;
2321ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller
2336d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_SPACE(push, nvc0->vertex->num_elements * 8);
2341ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller   for (i = 0; i < nvc0->vertex->num_elements; ++i) {
2351ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller      struct pipe_vertex_element *ve = &nvc0->vertex->element[i].pipe;
236e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      const unsigned b = ve->vertex_buffer_index;
2371ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller      struct pipe_vertex_buffer *vb = &nvc0->vtxbuf[b];
238e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      uint32_t base, size;
2391ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller
240e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (!(nvc0->vbo_user & (1 << b)))
241e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         continue;
242e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (!vb->stride) {
243e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         nvc0_set_constant_vertex_attrib(nvc0, i);
2441ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller         continue;
245e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      }
246e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nvc0_user_vbuf_range(nvc0, b, &base, &size);
2471ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller
2481ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller      if (!(written & (1 << b))) {
249e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         struct nouveau_bo *bo;
250e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         const uint32_t bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART;
2511ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller         written |= 1 << b;
252e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         address[b] = nouveau_scratch_data(&nvc0->base, vb->user_buffer,
253e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller                                           base, size, &bo);
2542206a7f5132905bb1b53d8343d11847010f6b63aChristoph Bumiller         if (bo)
2552206a7f5132905bb1b53d8343d11847010f6b63aChristoph Bumiller            BCTX_REFN_bo(nvc0->bufctx_3d, VTX_TMP, bo_flags, bo);
2563ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      }
257a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller
2587a40ae4a31f3ab526706fdfda7631d54f094512aChristoph Bumiller      BEGIN_1IC0(push, NVC0_3D(MACRO_VERTEX_ARRAY_SELECT), 5);
2596d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, i);
260e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATAh(push, address[b] + base + size - 1);
261e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, address[b] + base + size - 1);
262e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATAh(push, address[b] + ve->src_offset);
263e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, address[b] + ve->src_offset);
264a10b1c12045c514c98d3bc343c26cea5f2497b9fChristoph Bumiller   }
2651ba8e9510812f155359d380bda6876cdee5ba21eBen Skeggs   nvc0->base.vbo_dirty = TRUE;
2661ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller}
2671ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller
268e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillerstatic void
269e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillernvc0_update_user_vbufs_shared(struct nvc0_context *nvc0)
270a3c62afa7c7f3435b3c28bee417e652c9bb018e6Christoph Bumiller{
271e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
272e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   uint32_t mask = nvc0->vbo_user & ~nvc0->constant_vbos;
273e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
274e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   PUSH_SPACE(push, nvc0->num_vtxbufs * 8);
275e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   while (mask) {
276e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      struct nouveau_bo *bo;
277e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      const uint32_t bo_flags = NOUVEAU_BO_RD | NOUVEAU_BO_GART;
278e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      uint64_t address;
279e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      uint32_t base, size;
280e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      const int b = ffs(mask) - 1;
281e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      mask &= ~(1 << b);
282e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
283e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nvc0_user_vbuf_range(nvc0, b, &base, &size);
284e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
285e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      address = nouveau_scratch_data(&nvc0->base, nvc0->vtxbuf[b].user_buffer,
286e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller                                     base, size, &bo);
287e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (bo)
288e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         BCTX_REFN_bo(nvc0->bufctx_3d, VTX_TMP, bo_flags, bo);
289e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
290e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      BEGIN_1IC0(push, NVC0_3D(MACRO_VERTEX_ARRAY_SELECT), 5);
291e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, b);
292e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATAh(push, address + base + size - 1);
293e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, address + base + size - 1);
294e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATAh(push, address);
295e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, address);
296e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   }
297e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
298e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   mask = nvc0->state.constant_elts;
299e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   while (mask) {
300e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      int i = ffs(mask) - 1;
301e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      mask &= ~(1 << i);
302e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nvc0_set_constant_vertex_attrib(nvc0, i);
303a3c62afa7c7f3435b3c28bee417e652c9bb018e6Christoph Bumiller   }
304a3c62afa7c7f3435b3c28bee417e652c9bb018e6Christoph Bumiller}
305a3c62afa7c7f3435b3c28bee417e652c9bb018e6Christoph Bumiller
306e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillerstatic void
307e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillernvc0_validate_vertex_buffers(struct nvc0_context *nvc0)
308e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller{
309e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
310e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   const struct nvc0_vertex_stateobj *vertex = nvc0->vertex;
311e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   uint32_t refd = 0;
312e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   unsigned i;
313e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
314e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   PUSH_SPACE(push, vertex->num_elements * 8);
315e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   for (i = 0; i < vertex->num_elements; ++i) {
316e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      const struct nvc0_vertex_element *ve;
317e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      const struct pipe_vertex_buffer *vb;
318e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      struct nv04_resource *res;
319e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      unsigned b;
320e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      unsigned limit, offset;
321e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
322e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (nvc0->state.constant_elts & (1 << i))
323e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         continue;
324e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      ve = &vertex->element[i];
325e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      b = ve->pipe.vertex_buffer_index;
326e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      vb = &nvc0->vtxbuf[b];
327e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
328e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (!vb->buffer) {
329e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         if (vb->stride) {
330e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            if (ve->pipe.instance_divisor) {
331e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller               BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_DIVISOR(i)), 1);
332e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller               PUSH_DATA (push, ve->pipe.instance_divisor);
333e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            }
334e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(i)), 1);
335e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            PUSH_DATA (push, (1 << 12) | vb->stride);
336e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         }
337e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         /* address/value set in nvc0_update_user_vbufs */
338e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         continue;
339e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      }
340e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      res = nv04_resource(vb->buffer);
341e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      offset = ve->pipe.src_offset + vb->buffer_offset;
342e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      limit = vb->buffer->width0 - 1;
343e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
344e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (unlikely(ve->pipe.instance_divisor)) {
345e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(i)), 4);
346e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         PUSH_DATA (push, (1 << 12) | vb->stride);
347e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         PUSH_DATAh(push, res->address + offset);
348e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         PUSH_DATA (push, res->address + offset);
349e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         PUSH_DATA (push, ve->pipe.instance_divisor);
350e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      } else {
351e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(i)), 3);
352e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         PUSH_DATA (push, (1 << 12) | vb->stride);
353e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         PUSH_DATAh(push, res->address + offset);
354e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         PUSH_DATA (push, res->address + offset);
355e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      }
356e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_LIMIT_HIGH(i)), 2);
357e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATAh(push, res->address + limit);
358e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, res->address + limit);
359e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
360e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (!(refd & (1 << b))) {
361e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         refd |= 1 << b;
362e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         BCTX_REFN(nvc0->bufctx_3d, VTX, res, RD);
363e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      }
364e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   }
365e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   if (nvc0->vbo_user)
366e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nvc0_update_user_vbufs(nvc0);
367e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller}
368e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
369e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillerstatic void
370e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumillernvc0_validate_vertex_buffers_shared(struct nvc0_context *nvc0)
371e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller{
372e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
373e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   unsigned b;
374e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   const uint32_t mask = nvc0->vbo_user;
375e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
376e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   PUSH_SPACE(push, nvc0->num_vtxbufs * 8);
377e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   for (b = 0; b < nvc0->num_vtxbufs; ++b) {
378e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      struct pipe_vertex_buffer *vb = &nvc0->vtxbuf[b];
379e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      struct nv04_resource *buf;
380e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      uint32_t offset, limit;
381e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
382e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (mask & (1 << b)) {
383e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         if (vb->stride) {
384e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(b)), 1);
385e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            PUSH_DATA (push, NVC0_3D_VERTEX_ARRAY_FETCH_ENABLE | vb->stride);
386e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         }
387e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         /* address/value set in nvc0_update_user_vbufs_shared */
388e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         continue;
389e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      }
390e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      buf = nv04_resource(vb->buffer);
391e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      offset = vb->buffer_offset;
392e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      limit = buf->base.width0 - 1;
393e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
394e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(b)), 3);
395e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, NVC0_3D_VERTEX_ARRAY_FETCH_ENABLE | vb->stride);
396e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATAh(push, buf->address + offset);
397e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, buf->address + offset);
398e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_LIMIT_HIGH(b)), 2);
399e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATAh(push, buf->address + limit);
400e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      PUSH_DATA (push, buf->address + limit);
401e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
402e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      BCTX_REFN(nvc0->bufctx_3d, VTX, buf, RD);
403e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   }
404e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   if (nvc0->vbo_user)
405e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nvc0_update_user_vbufs_shared(nvc0);
406e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller}
407e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
4081ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumillervoid
4091ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumillernvc0_vertex_arrays_validate(struct nvc0_context *nvc0)
4101ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller{
4116d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
4121ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller   struct nvc0_vertex_stateobj *vertex = nvc0->vertex;
4131ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller   struct nvc0_vertex_element *ve;
414edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   uint32_t const_vbos;
4151ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller   unsigned i;
416ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller   uint8_t vbo_mode;
417edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   boolean update_vertex;
4181ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller
419e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_VTX);
420e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
4217b6881932a71b36dd47f63200c9dbee8e2b9af4fChristoph Bumiller   if (unlikely(vertex->need_conversion) ||
4227b6881932a71b36dd47f63200c9dbee8e2b9af4fChristoph Bumiller       unlikely(nvc0->vertprog->vp.edgeflag < PIPE_MAX_ATTRIBS)) {
423ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      vbo_mode = 3;
42417d680cc537acf8a967d9e36f7006afab560122aChristoph Bumiller   } else {
425e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      vbo_mode = (nvc0->vbo_user && nvc0->vbo_push_hint) ? 1 : 0;
42617d680cc537acf8a967d9e36f7006afab560122aChristoph Bumiller   }
427ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller   const_vbos = vbo_mode ? 0 : nvc0->constant_vbos;
428edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller
429edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   update_vertex = (nvc0->dirty & NVC0_NEW_VERTEX) ||
430ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      (const_vbos != nvc0->state.constant_vbos) ||
431ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      (vbo_mode != nvc0->state.vbo_mode);
432ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
433edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   if (update_vertex) {
434edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller      const unsigned n = MAX2(vertex->num_elements, nvc0->state.num_vtxelts);
435edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller
436edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller      nvc0->state.constant_vbos = const_vbos;
437edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller      nvc0->state.constant_elts = 0;
438ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      nvc0->state.num_vtxelts = vertex->num_elements;
439ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      nvc0->state.vbo_mode = vbo_mode;
440ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
441ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      if (unlikely(vbo_mode)) {
442ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         if (unlikely(nvc0->state.instance_elts & 3)) {
443ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            /* translate mode uses only 2 vertex buffers */
444ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            nvc0->state.instance_elts &= ~3;
445ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            PUSH_SPACE(push, 3);
446ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_PER_INSTANCE(0)), 2);
447ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            PUSH_DATA (push, 0);
448ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            PUSH_DATA (push, 0);
449ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         }
450ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
451ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         PUSH_SPACE(push, n * 2 + 4);
452edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller
453ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         BEGIN_NVC0(push, NVC0_3D(VERTEX_ATTRIB_FORMAT(0)), n);
454ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         for (i = 0; i < vertex->num_elements; ++i)
455ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            PUSH_DATA(push, vertex->element[i].state_alt);
456ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         for (; i < n; ++i)
457ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            PUSH_DATA(push, NVC0_3D_VERTEX_ATTRIB_INACTIVE);
458ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
459ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         BEGIN_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(0)), 1);
460ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         PUSH_DATA (push, (1 << 12) | vertex->size);
461ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         for (i = 1; i < n; ++i)
462ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            IMMED_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(i)), 0);
463ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      } else {
464ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         uint32_t *restrict data;
465ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
466ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         if (unlikely(vertex->instance_elts != nvc0->state.instance_elts)) {
467ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            nvc0->state.instance_elts = vertex->instance_elts;
468ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            assert(n); /* if (n == 0), both masks should be 0 */
469ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            PUSH_SPACE(push, 3);
470ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            BEGIN_NVC0(push, NVC0_3D(MACRO_VERTEX_ARRAY_PER_INSTANCE), 2);
471ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            PUSH_DATA (push, n);
472ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            PUSH_DATA (push, vertex->instance_elts);
473ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         }
474ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller
475ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         PUSH_SPACE(push, n * 2 + 1);
476ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         BEGIN_NVC0(push, NVC0_3D(VERTEX_ATTRIB_FORMAT(0)), n);
477ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         data = push->cur;
478ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         push->cur += n;
479ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         for (i = 0; i < vertex->num_elements; ++i) {
480ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            ve = &vertex->element[i];
481ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            data[i] = ve->state;
482ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            if (unlikely(const_vbos & (1 << ve->pipe.vertex_buffer_index))) {
483ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller               nvc0->state.constant_elts |= 1 << i;
484ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller               data[i] |= NVC0_3D_VERTEX_ATTRIB_FORMAT_CONST;
485ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller               IMMED_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(i)), 0);
486ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            }
487ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         }
488ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller         for (; i < n; ++i) {
489ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller            data[i] = NVC0_3D_VERTEX_ATTRIB_INACTIVE;
490edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller            IMMED_NVC0(push, NVC0_3D(VERTEX_ARRAY_FETCH(i)), 0);
491edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller         }
4924c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      }
4934c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
494ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller   if (nvc0->state.vbo_mode) /* using translate, don't set up arrays here */
495ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      return;
4964c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
497e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   if (vertex->shared_slots)
498e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nvc0_validate_vertex_buffers_shared(nvc0);
499e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   else
500e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      nvc0_validate_vertex_buffers(nvc0);
5014c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
5024c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5036d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumillervoid
5046d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumillernvc0_idxbuf_validate(struct nvc0_context *nvc0)
5056d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller{
5066d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
5076d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nv04_resource *buf = nv04_resource(nvc0->idxbuf.buffer);
5086d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
5096d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   assert(buf);
510ef7bb281292c17b762b57779306e874704c87328Christoph Bumiller   assert(nouveau_resource_mapped_by_gpu(&buf->base));
5116d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
5126d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_SPACE(push, 6);
5136d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BEGIN_NVC0(push, NVC0_3D(INDEX_ARRAY_START_HIGH), 5);
5146d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAh(push, buf->address + nvc0->idxbuf.offset);
5156d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, buf->address + nvc0->idxbuf.offset);
5166d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATAh(push, buf->address + buf->base.width0 - 1);
5176d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, buf->address + buf->base.width0 - 1);
5186d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_DATA (push, nvc0->idxbuf.index_size >> 1);
5196d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
5206d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   BCTX_REFN(nvc0->bufctx_3d, IDX, buf, RD);
5216d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller}
5226d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
5234c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller#define NVC0_PRIM_GL_CASE(n) \
5244c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   case PIPE_PRIM_##n: return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n
5254c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5264c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic INLINE unsigned
5274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_prim_gl(unsigned prim)
5284c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
5294c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   switch (prim) {
5304c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(POINTS);
5314c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(LINES);
5324c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(LINE_LOOP);
5334c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(LINE_STRIP);
5344c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(TRIANGLES);
5354c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(TRIANGLE_STRIP);
5364c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(TRIANGLE_FAN);
5374c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(QUADS);
5384c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(QUAD_STRIP);
5394c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(POLYGON);
5404c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(LINES_ADJACENCY);
5414c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(LINE_STRIP_ADJACENCY);
5424c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(TRIANGLES_ADJACENCY);
5434c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY);
5444c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   /*
5454c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   NVC0_PRIM_GL_CASE(PATCHES); */
5464c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   default:
5474c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS;
5484c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
5494c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
5504c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5514c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
5526d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumillernvc0_draw_vbo_kick_notify(struct nouveau_pushbuf *push)
5534c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
5546d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nvc0_screen *screen = push->user_priv;
5554c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5563069a7eaa5d83e7f41616347ba244c0dc0d944aeChristoph Bumiller   nouveau_fence_update(&screen->base, TRUE);
5574c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
5584c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5594c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
5604c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_draw_arrays(struct nvc0_context *nvc0,
5614c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                 unsigned mode, unsigned start, unsigned count,
5625655f8d42d919270791588162399ac7a2c718733Christoph Bumiller                 unsigned instance_count)
5634c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
5646d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
5654c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   unsigned prim;
5664c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5676994b57a509cff1a1d0d013c0b175ec4d83117a0Christoph Bumiller   if (nvc0->state.index_bias) {
5686d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 1);
5696d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(VB_ELEMENT_BASE), 0);
5706994b57a509cff1a1d0d013c0b175ec4d83117a0Christoph Bumiller      nvc0->state.index_bias = 0;
5716994b57a509cff1a1d0d013c0b175ec4d83117a0Christoph Bumiller   }
5726994b57a509cff1a1d0d013c0b175ec4d83117a0Christoph Bumiller
5734c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   prim = nvc0_prim_gl(mode);
5744c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5754c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   while (instance_count--) {
5766d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 6);
5776d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VERTEX_BEGIN_GL), 1);
5786d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, prim);
5796d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VERTEX_BUFFER_FIRST), 2);
5806d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, start);
5816d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, count);
5826d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(VERTEX_END_GL), 0);
5834c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5844c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      prim |= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
5854c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
5864c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
5874c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5884c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
589ef7bb281292c17b762b57779306e874704c87328Christoph Bumillernvc0_draw_elements_inline_u08(struct nouveau_pushbuf *push, const uint8_t *map,
5904c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                              unsigned start, unsigned count)
5914c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
5924c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   map += start;
5934c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
5944c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (count & 3) {
5954c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      unsigned i;
5966d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 4);
5976d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NIC0(push, NVC0_3D(VB_ELEMENT_U32), count & 3);
5984c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      for (i = 0; i < (count & 3); ++i)
5996d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA(push, *map++);
6004c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      count &= ~3;
6014c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
6024c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   while (count) {
6033ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      unsigned i, nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN * 4) / 4;
6044c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6056d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, nr + 1);
6066d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NIC0(push, NVC0_3D(VB_ELEMENT_U8), nr);
6074c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      for (i = 0; i < nr; ++i) {
6086d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA(push,
6094c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                  (map[3] << 24) | (map[2] << 16) | (map[1] << 8) | map[0]);
6104c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller         map += 4;
6114c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      }
6123ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      count -= nr * 4;
6134c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
6144c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
6154c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6164c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
617ef7bb281292c17b762b57779306e874704c87328Christoph Bumillernvc0_draw_elements_inline_u16(struct nouveau_pushbuf *push, const uint16_t *map,
6184c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                              unsigned start, unsigned count)
6194c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
6204c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   map += start;
6214c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6224c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   if (count & 1) {
6234c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      count &= ~1;
6246d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 2);
6256d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VB_ELEMENT_U32), 1);
6266d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, *map++);
6274c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
6284c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   while (count) {
6293ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      unsigned i, nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN * 2) / 2;
6304c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6316d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, nr + 1);
6326d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NIC0(push, NVC0_3D(VB_ELEMENT_U16), nr);
6334c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      for (i = 0; i < nr; ++i) {
6346d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA(push, (map[1] << 16) | map[0]);
6354c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller         map += 2;
6364c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      }
6373ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      count -= nr * 2;
6384c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
6394c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
6404c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6414c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
642ef7bb281292c17b762b57779306e874704c87328Christoph Bumillernvc0_draw_elements_inline_u32(struct nouveau_pushbuf *push, const uint32_t *map,
6434c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                              unsigned start, unsigned count)
6444c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
6454c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   map += start;
6464c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6474c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   while (count) {
6483ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      const unsigned nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN);
6494c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6506d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, nr + 1);
6516d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NIC0(push, NVC0_3D(VB_ELEMENT_U32), nr);
6526d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATAp(push, map, nr);
6534c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6543ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      map += nr;
6554c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller      count -= nr;
6564c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
6574c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
6584c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6594c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillerstatic void
660ef7bb281292c17b762b57779306e874704c87328Christoph Bumillernvc0_draw_elements_inline_u32_short(struct nouveau_pushbuf *push,
661ef7bb281292c17b762b57779306e874704c87328Christoph Bumiller                                    const uint32_t *map,
6623ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller                                    unsigned start, unsigned count)
6633ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller{
6643ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller   map += start;
6653ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller
6663ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller   if (count & 1) {
6673ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      count--;
6686d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 1);
6696d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VB_ELEMENT_U32), 1);
6706d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, *map++);
6713ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller   }
6723ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller   while (count) {
6733ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      unsigned i, nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN * 2) / 2;
6743ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller
6756d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, nr + 1);
6766d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NIC0(push, NVC0_3D(VB_ELEMENT_U16), nr);
6773ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      for (i = 0; i < nr; ++i) {
6786d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA(push, (map[1] << 16) | map[0]);
6793ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller         map += 2;
6803ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      }
6813ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      count -= nr * 2;
6823ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller   }
6833ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller}
6843ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller
6853ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumillerstatic void
6863ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumillernvc0_draw_elements(struct nvc0_context *nvc0, boolean shorten,
6874c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                   unsigned mode, unsigned start, unsigned count,
6882bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller                   unsigned instance_count, int32_t index_bias)
6894c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
6906d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
6914c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   unsigned prim;
6924884ca5f67a0138659e48700e8b2f8821400c995Christoph Bumiller   const unsigned index_size = nvc0->idxbuf.index_size;
6934c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6944c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   prim = nvc0_prim_gl(mode);
6954c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
6962bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller   if (index_bias != nvc0->state.index_bias) {
6976d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 2);
6986d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VB_ELEMENT_BASE), 1);
6996d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, index_bias);
7002bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller      nvc0->state.index_bias = index_bias;
7012bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller   }
7024c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
703ef7bb281292c17b762b57779306e874704c87328Christoph Bumiller   if (nvc0->idxbuf.buffer) {
7046d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 1);
7056d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(VERTEX_BEGIN_GL), prim);
7066d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      do {
7076d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_SPACE(push, 7);
7086d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         BEGIN_NVC0(push, NVC0_3D(INDEX_BATCH_FIRST), 2);
7096d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA (push, start);
7106d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA (push, count);
7116d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         if (--instance_count) {
7126d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            BEGIN_NVC0(push, NVC0_3D(VERTEX_END_GL), 2);
7136d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            PUSH_DATA (push, 0);
7146d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            PUSH_DATA (push, prim | NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT);
7156d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         }
7166d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      } while (instance_count);
7176d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(VERTEX_END_GL), 0);
7182bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller   } else {
719ef7bb281292c17b762b57779306e874704c87328Christoph Bumiller      const void *data = nvc0->idxbuf.user_buffer;
7205655f8d42d919270791588162399ac7a2c718733Christoph Bumiller
7212bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller      while (instance_count--) {
7226d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_SPACE(push, 2);
7236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         BEGIN_NVC0(push, NVC0_3D(VERTEX_BEGIN_GL), 1);
7246d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA (push, prim);
7252bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller         switch (index_size) {
7262bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller         case 1:
7276d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            nvc0_draw_elements_inline_u08(push, data, start, count);
7282bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller            break;
7292bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller         case 2:
7306d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            nvc0_draw_elements_inline_u16(push, data, start, count);
7312bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller            break;
7322bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller         case 4:
7333ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller            if (shorten)
7346d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller               nvc0_draw_elements_inline_u32_short(push, data, start, count);
7353ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller            else
7366d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller               nvc0_draw_elements_inline_u32(push, data, start, count);
7372bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller            break;
7382bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller         default:
7392bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller            assert(0);
7402bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller            return;
7412bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller         }
7426d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_SPACE(push, 1);
7436d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         IMMED_NVC0(push, NVC0_3D(VERTEX_END_GL), 0);
7442bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller
7452bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller         prim |= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
7462bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller      }
7474c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
7484c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
7494c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
75014bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumillerstatic void
75114bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumillernvc0_draw_stream_output(struct nvc0_context *nvc0,
75214bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller                        const struct pipe_draw_info *info)
75314bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller{
7546d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
75514bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   struct nvc0_so_target *so = nvc0_so_target(info->count_from_stream_output);
75614bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   struct nv04_resource *res = nv04_resource(so->pipe.buffer);
75714bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   unsigned mode = nvc0_prim_gl(info->mode);
75814bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   unsigned num_instances = info->instance_count;
75914bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller
76014bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   if (res->status & NOUVEAU_BUFFER_STATUS_GPU_WRITING) {
76114bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller      res->status &= ~NOUVEAU_BUFFER_STATUS_GPU_WRITING;
7626d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 2);
7636d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(SERIALIZE), 0);
7646d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      nvc0_query_fifo_wait(push, so->pq);
7656d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(VERTEX_ARRAY_FLUSH), 0);
76614bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   }
76714bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller
76814bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   while (num_instances--) {
7696d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_SPACE(push, 8);
7706d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VERTEX_BEGIN_GL), 1);
7716d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, mode);
7726d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(DRAW_TFB_BASE), 1);
7736d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, 0);
7746d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(DRAW_TFB_STRIDE), 1);
7756d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, so->stride);
7766d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(DRAW_TFB_BYTES), 1);
7776d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      nvc0_query_pushbuf_submit(push, so->pq, 0x4);
7786d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      IMMED_NVC0(push, NVC0_3D(VERTEX_END_GL), 0);
77914bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller
78014bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller      mode |= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
78114bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller   }
78214bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller}
78314bd9d764802b5fedb652c791faafe4d13b65262Christoph Bumiller
7844c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillervoid
7854c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumillernvc0_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
7864c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller{
7874c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   struct nvc0_context *nvc0 = nvc0_context(pipe);
7886d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   struct nouveau_pushbuf *push = nvc0->base.pushbuf;
7894c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
7900e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller   /* NOTE: caller must ensure that (min_index + index_bias) is >= 0 */
7910e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller   nvc0->vb_elt_first = info->min_index + info->index_bias;
7920e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller   nvc0->vb_elt_limit = info->max_index - info->min_index;
793e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   nvc0->instance_off = info->start_instance;
794e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   nvc0->instance_max = info->instance_count - 1;
7950e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller
7963ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller   /* For picking only a few vertices from a large user buffer, push is better,
7973ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller    * if index count is larger and we expect repeated vertices, suggest upload.
7983ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller    */
799ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller   nvc0->vbo_push_hint =
8000e8ad4cc749626eebbc7f916e551bbfd80b75023Christoph Bumiller      info->indexed && (nvc0->vb_elt_limit >= (info->count * 2));
801b2a79953a6e68853b44dbb20f4c0c4c67dc5f75bChristoph Bumiller
802e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   /* Check whether we want to switch vertex-submission mode. */
803e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller   if (nvc0->vbo_user && !(nvc0->dirty & (NVC0_NEW_ARRAYS | NVC0_NEW_VERTEX))) {
804e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (nvc0->vbo_push_hint != !!nvc0->state.vbo_mode)
805e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         if (nvc0->state.vbo_mode != 3)
806e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            nvc0->dirty |= NVC0_NEW_ARRAYS;
807e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller
808e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller      if (!(nvc0->dirty & NVC0_NEW_ARRAYS) && nvc0->state.vbo_mode == 0) {
809e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         if (nvc0->vertex->shared_slots)
810e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            nvc0_update_user_vbufs_shared(nvc0);
811e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller         else
812e6caafd9d7fbfcb5906d22be9d6a3c1714e078acChristoph Bumiller            nvc0_update_user_vbufs(nvc0);
813ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller      }
814ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller   }
8151ae982adfd8144d11334895c39aca65dd30ad946Christoph Bumiller
816222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller   /* 8 as minimum to avoid immediate double validation of new buffers */
817222b3ea653e5525a4afa57e6a2353335953012d4Christoph Bumiller   nvc0_state_validate(nvc0, ~0, 8);
8184c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
8196d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   push->kick_notify = nvc0_draw_vbo_kick_notify;
8209c5d15e929f47f517f90977f5420e11dfbd3db67Christoph Bumiller
821ce713cd520792707e9097ef9e843ef7ab57b0eabChristoph Bumiller   if (nvc0->state.vbo_mode) {
822a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller      nvc0_push_vbo(nvc0, info);
8236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      push->kick_notify = nvc0_default_kick_notify;
824a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller      return;
825a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller   }
826a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller
8276d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   /* space for base instance, flush, and prim restart */
8286d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   PUSH_SPACE(push, 8);
8296d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
8305655f8d42d919270791588162399ac7a2c718733Christoph Bumiller   if (nvc0->state.instance_base != info->start_instance) {
8315655f8d42d919270791588162399ac7a2c718733Christoph Bumiller      nvc0->state.instance_base = info->start_instance;
832a4742c6a07179f01eebfc486e6cd21be05d9c8aeChristoph Bumiller      /* NOTE: this does not affect the shader input, should it ? */
8336d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      BEGIN_NVC0(push, NVC0_3D(VB_INSTANCE_BASE), 1);
8346d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      PUSH_DATA (push, info->start_instance);
8355655f8d42d919270791588162399ac7a2c718733Christoph Bumiller   }
8365655f8d42d919270791588162399ac7a2c718733Christoph Bumiller
8371ba8e9510812f155359d380bda6876cdee5ba21eBen Skeggs   if (nvc0->base.vbo_dirty) {
838edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller      IMMED_NVC0(push, NVC0_3D(VERTEX_ARRAY_FLUSH), 0);
8391ba8e9510812f155359d380bda6876cdee5ba21eBen Skeggs      nvc0->base.vbo_dirty = FALSE;
8404c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
8414c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller
842edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   if (info->indexed) {
8433ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      boolean shorten = info->max_index <= 65535;
8443ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller
8455655f8d42d919270791588162399ac7a2c718733Christoph Bumiller      if (info->primitive_restart != nvc0->state.prim_restart) {
8465655f8d42d919270791588162399ac7a2c718733Christoph Bumiller         if (info->primitive_restart) {
8476d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            BEGIN_NVC0(push, NVC0_3D(PRIM_RESTART_ENABLE), 2);
8486d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            PUSH_DATA (push, 1);
8496d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            PUSH_DATA (push, info->restart_index);
8503ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller
8513ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller            if (info->restart_index > 65535)
8523ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller               shorten = FALSE;
8535655f8d42d919270791588162399ac7a2c718733Christoph Bumiller         } else {
8546d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            IMMED_NVC0(push, NVC0_3D(PRIM_RESTART_ENABLE), 0);
8555655f8d42d919270791588162399ac7a2c718733Christoph Bumiller         }
8565655f8d42d919270791588162399ac7a2c718733Christoph Bumiller         nvc0->state.prim_restart = info->primitive_restart;
8575655f8d42d919270791588162399ac7a2c718733Christoph Bumiller      } else
8585655f8d42d919270791588162399ac7a2c718733Christoph Bumiller      if (info->primitive_restart) {
8596d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         BEGIN_NVC0(push, NVC0_3D(PRIM_RESTART_INDEX), 1);
8606d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         PUSH_DATA (push, info->restart_index);
861703f3597ad0e2b7134d4ea0fc2194eb67161910aChristoph Bumiller
862703f3597ad0e2b7134d4ea0fc2194eb67161910aChristoph Bumiller         if (info->restart_index > 65535)
863703f3597ad0e2b7134d4ea0fc2194eb67161910aChristoph Bumiller            shorten = FALSE;
8645655f8d42d919270791588162399ac7a2c718733Christoph Bumiller      }
8655655f8d42d919270791588162399ac7a2c718733Christoph Bumiller
8663ef1616b63507db01f54efa882a9cf28839cfdf3Christoph Bumiller      nvc0_draw_elements(nvc0, shorten,
8674c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller                         info->mode, info->start, info->count,
8682bb377ee02cc956cb3328b6f13320ac709987dd0Christoph Bumiller                         info->instance_count, info->index_bias);
869edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   } else
870edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   if (unlikely(info->count_from_stream_output)) {
871edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller      nvc0_draw_stream_output(nvc0, info);
872edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller   } else {
873edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller      nvc0_draw_arrays(nvc0,
874edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller                       info->mode, info->start, info->count,
875edbfeed56f1ebd8517840ef48f8c87e24bb98157Christoph Bumiller                       info->instance_count);
8764c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller   }
8776d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   push->kick_notify = nvc0_default_kick_notify;
878a3c62afa7c7f3435b3c28bee417e652c9bb018e6Christoph Bumiller
879a3c62afa7c7f3435b3c28bee417e652c9bb018e6Christoph Bumiller   nvc0_release_user_vbufs(nvc0);
8804c2247538394a313e1e90bfcd07c1ab9c7d41281Christoph Bumiller}
881