1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2010 Christoph Bumiller
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
12bf215546Sopenharmony_ci * all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci#include "pipe/p_context.h"
24bf215546Sopenharmony_ci#include "pipe/p_state.h"
25bf215546Sopenharmony_ci#include "util/u_draw.h"
26bf215546Sopenharmony_ci#include "util/u_inlines.h"
27bf215546Sopenharmony_ci#include "util/u_prim.h"
28bf215546Sopenharmony_ci#include "util/format/u_format.h"
29bf215546Sopenharmony_ci#include "translate/translate.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "nv50/nv50_context.h"
32bf215546Sopenharmony_ci#include "nv50/nv50_query_hw.h"
33bf215546Sopenharmony_ci#include "nv50/nv50_resource.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "nv50/nv50_3d.xml.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_civoid
38bf215546Sopenharmony_cinv50_vertex_state_delete(struct pipe_context *pipe,
39bf215546Sopenharmony_ci                         void *hwcso)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   struct nv50_vertex_stateobj *so = hwcso;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   if (so->translate)
44bf215546Sopenharmony_ci      so->translate->release(so->translate);
45bf215546Sopenharmony_ci   FREE(hwcso);
46bf215546Sopenharmony_ci}
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_civoid *
49bf215546Sopenharmony_cinv50_vertex_state_create(struct pipe_context *pipe,
50bf215546Sopenharmony_ci                         unsigned num_elements,
51bf215546Sopenharmony_ci                         const struct pipe_vertex_element *elements)
52bf215546Sopenharmony_ci{
53bf215546Sopenharmony_ci    struct nv50_vertex_stateobj *so;
54bf215546Sopenharmony_ci    struct translate_key transkey;
55bf215546Sopenharmony_ci    unsigned i;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci    so = MALLOC(sizeof(*so) +
58bf215546Sopenharmony_ci                num_elements * sizeof(struct nv50_vertex_element));
59bf215546Sopenharmony_ci    if (!so)
60bf215546Sopenharmony_ci        return NULL;
61bf215546Sopenharmony_ci    so->num_elements = num_elements;
62bf215546Sopenharmony_ci    so->instance_elts = 0;
63bf215546Sopenharmony_ci    so->instance_bufs = 0;
64bf215546Sopenharmony_ci    so->need_conversion = false;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci    memset(so->vb_access_size, 0, sizeof(so->vb_access_size));
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci    for (i = 0; i < PIPE_MAX_ATTRIBS; ++i)
69bf215546Sopenharmony_ci       so->min_instance_div[i] = 0xffffffff;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci    transkey.nr_elements = 0;
72bf215546Sopenharmony_ci    transkey.output_stride = 0;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci    for (i = 0; i < num_elements; ++i) {
75bf215546Sopenharmony_ci        const struct pipe_vertex_element *ve = &elements[i];
76bf215546Sopenharmony_ci        const unsigned vbi = ve->vertex_buffer_index;
77bf215546Sopenharmony_ci        unsigned size;
78bf215546Sopenharmony_ci        enum pipe_format fmt = ve->src_format;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci        so->element[i].pipe = elements[i];
81bf215546Sopenharmony_ci        so->element[i].state = nv50_vertex_format[fmt].vtx;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci        if (!so->element[i].state) {
84bf215546Sopenharmony_ci            switch (util_format_get_nr_components(fmt)) {
85bf215546Sopenharmony_ci            case 1: fmt = PIPE_FORMAT_R32_FLOAT; break;
86bf215546Sopenharmony_ci            case 2: fmt = PIPE_FORMAT_R32G32_FLOAT; break;
87bf215546Sopenharmony_ci            case 3: fmt = PIPE_FORMAT_R32G32B32_FLOAT; break;
88bf215546Sopenharmony_ci            case 4: fmt = PIPE_FORMAT_R32G32B32A32_FLOAT; break;
89bf215546Sopenharmony_ci            default:
90bf215546Sopenharmony_ci                assert(0);
91bf215546Sopenharmony_ci                FREE(so);
92bf215546Sopenharmony_ci                return NULL;
93bf215546Sopenharmony_ci            }
94bf215546Sopenharmony_ci            so->element[i].state = nv50_vertex_format[fmt].vtx;
95bf215546Sopenharmony_ci            so->need_conversion = true;
96bf215546Sopenharmony_ci            util_debug_message(&nouveau_context(pipe)->debug, FALLBACK,
97bf215546Sopenharmony_ci                               "Converting vertex element %d, no hw format %s",
98bf215546Sopenharmony_ci                               i, util_format_name(ve->src_format));
99bf215546Sopenharmony_ci        }
100bf215546Sopenharmony_ci        so->element[i].state |= i;
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci        size = util_format_get_blocksize(fmt);
103bf215546Sopenharmony_ci        if (so->vb_access_size[vbi] < (ve->src_offset + size))
104bf215546Sopenharmony_ci           so->vb_access_size[vbi] = ve->src_offset + size;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci        if (1) {
107bf215546Sopenharmony_ci            unsigned j = transkey.nr_elements++;
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci            transkey.element[j].type = TRANSLATE_ELEMENT_NORMAL;
110bf215546Sopenharmony_ci            transkey.element[j].input_format = ve->src_format;
111bf215546Sopenharmony_ci            transkey.element[j].input_buffer = vbi;
112bf215546Sopenharmony_ci            transkey.element[j].input_offset = ve->src_offset;
113bf215546Sopenharmony_ci            transkey.element[j].instance_divisor = ve->instance_divisor;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci            transkey.element[j].output_format = fmt;
116bf215546Sopenharmony_ci            transkey.element[j].output_offset = transkey.output_stride;
117bf215546Sopenharmony_ci            transkey.output_stride += (util_format_get_stride(fmt, 1) + 3) & ~3;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci            if (unlikely(ve->instance_divisor)) {
120bf215546Sopenharmony_ci               so->instance_elts |= 1 << i;
121bf215546Sopenharmony_ci               so->instance_bufs |= 1 << vbi;
122bf215546Sopenharmony_ci               if (ve->instance_divisor < so->min_instance_div[vbi])
123bf215546Sopenharmony_ci                  so->min_instance_div[vbi] = ve->instance_divisor;
124bf215546Sopenharmony_ci            }
125bf215546Sopenharmony_ci        }
126bf215546Sopenharmony_ci    }
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci    so->translate = translate_create(&transkey);
129bf215546Sopenharmony_ci    so->vertex_size = transkey.output_stride / 4;
130bf215546Sopenharmony_ci    so->packet_vertex_limit = NV04_PFIFO_MAX_PACKET_LEN /
131bf215546Sopenharmony_ci       MAX2(so->vertex_size, 1);
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci    return so;
134bf215546Sopenharmony_ci}
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci#define NV50_3D_VERTEX_ATTRIB_INACTIVE              \
137bf215546Sopenharmony_ci   NV50_3D_VERTEX_ARRAY_ATTRIB_TYPE_FLOAT |         \
138bf215546Sopenharmony_ci   NV50_3D_VERTEX_ARRAY_ATTRIB_FORMAT_32_32_32_32 | \
139bf215546Sopenharmony_ci   NV50_3D_VERTEX_ARRAY_ATTRIB_CONST
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_cistatic void
142bf215546Sopenharmony_cinv50_emit_vtxattr(struct nv50_context *nv50, struct pipe_vertex_buffer *vb,
143bf215546Sopenharmony_ci                  struct pipe_vertex_element *ve, unsigned attr)
144bf215546Sopenharmony_ci{
145bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = nv50->base.pushbuf;
146bf215546Sopenharmony_ci   const void *data = (const uint8_t *)vb->buffer.user + ve->src_offset;
147bf215546Sopenharmony_ci   float v[4];
148bf215546Sopenharmony_ci   const unsigned nc = util_format_get_nr_components(ve->src_format);
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci   assert(vb->is_user_buffer);
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   util_format_unpack_rgba(ve->src_format, v, data, 1);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   switch (nc) {
155bf215546Sopenharmony_ci   case 4:
156bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VTX_ATTR_4F_X(attr)), 4);
157bf215546Sopenharmony_ci      PUSH_DATAf(push, v[0]);
158bf215546Sopenharmony_ci      PUSH_DATAf(push, v[1]);
159bf215546Sopenharmony_ci      PUSH_DATAf(push, v[2]);
160bf215546Sopenharmony_ci      PUSH_DATAf(push, v[3]);
161bf215546Sopenharmony_ci      break;
162bf215546Sopenharmony_ci   case 3:
163bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(attr)), 3);
164bf215546Sopenharmony_ci      PUSH_DATAf(push, v[0]);
165bf215546Sopenharmony_ci      PUSH_DATAf(push, v[1]);
166bf215546Sopenharmony_ci      PUSH_DATAf(push, v[2]);
167bf215546Sopenharmony_ci      break;
168bf215546Sopenharmony_ci   case 2:
169bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(attr)), 2);
170bf215546Sopenharmony_ci      PUSH_DATAf(push, v[0]);
171bf215546Sopenharmony_ci      PUSH_DATAf(push, v[1]);
172bf215546Sopenharmony_ci      break;
173bf215546Sopenharmony_ci   case 1:
174bf215546Sopenharmony_ci      if (attr == nv50->vertprog->vp.edgeflag) {
175bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(EDGEFLAG), 1);
176bf215546Sopenharmony_ci         PUSH_DATA (push, v[0] ? 1 : 0);
177bf215546Sopenharmony_ci      }
178bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VTX_ATTR_1F(attr)), 1);
179bf215546Sopenharmony_ci      PUSH_DATAf(push, v[0]);
180bf215546Sopenharmony_ci      break;
181bf215546Sopenharmony_ci   default:
182bf215546Sopenharmony_ci      assert(0);
183bf215546Sopenharmony_ci      break;
184bf215546Sopenharmony_ci   }
185bf215546Sopenharmony_ci}
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_cistatic inline void
188bf215546Sopenharmony_cinv50_user_vbuf_range(struct nv50_context *nv50, unsigned vbi,
189bf215546Sopenharmony_ci                     uint32_t *base, uint32_t *size)
190bf215546Sopenharmony_ci{
191bf215546Sopenharmony_ci   assert(vbi < PIPE_MAX_ATTRIBS);
192bf215546Sopenharmony_ci   if (unlikely(nv50->vertex->instance_bufs & (1 << vbi))) {
193bf215546Sopenharmony_ci      const uint32_t div = nv50->vertex->min_instance_div[vbi];
194bf215546Sopenharmony_ci      *base = nv50->instance_off * nv50->vtxbuf[vbi].stride;
195bf215546Sopenharmony_ci      *size = (nv50->instance_max / div) * nv50->vtxbuf[vbi].stride +
196bf215546Sopenharmony_ci         nv50->vertex->vb_access_size[vbi];
197bf215546Sopenharmony_ci   } else {
198bf215546Sopenharmony_ci      /* NOTE: if there are user buffers, we *must* have index bounds */
199bf215546Sopenharmony_ci      assert(nv50->vb_elt_limit != ~0);
200bf215546Sopenharmony_ci      *base = nv50->vb_elt_first * nv50->vtxbuf[vbi].stride;
201bf215546Sopenharmony_ci      *size = nv50->vb_elt_limit * nv50->vtxbuf[vbi].stride +
202bf215546Sopenharmony_ci         nv50->vertex->vb_access_size[vbi];
203bf215546Sopenharmony_ci   }
204bf215546Sopenharmony_ci}
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_cistatic void
207bf215546Sopenharmony_cinv50_upload_user_buffers(struct nv50_context *nv50,
208bf215546Sopenharmony_ci                         uint64_t addrs[], uint32_t limits[])
209bf215546Sopenharmony_ci{
210bf215546Sopenharmony_ci   unsigned b;
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
213bf215546Sopenharmony_ci   for (b = 0; b < nv50->num_vtxbufs; ++b) {
214bf215546Sopenharmony_ci      struct nouveau_bo *bo;
215bf215546Sopenharmony_ci      const struct pipe_vertex_buffer *vb = &nv50->vtxbuf[b];
216bf215546Sopenharmony_ci      uint32_t base, size;
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci      if (!(nv50->vbo_user & (1 << b)) || !vb->stride)
219bf215546Sopenharmony_ci         continue;
220bf215546Sopenharmony_ci      nv50_user_vbuf_range(nv50, b, &base, &size);
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci      limits[b] = base + size - 1;
223bf215546Sopenharmony_ci      addrs[b] = nouveau_scratch_data(&nv50->base, vb->buffer.user, base, size,
224bf215546Sopenharmony_ci                                      &bo);
225bf215546Sopenharmony_ci      if (addrs[b])
226bf215546Sopenharmony_ci         BCTX_REFN_bo(nv50->bufctx_3d, 3D_VERTEX_TMP, NOUVEAU_BO_GART |
227bf215546Sopenharmony_ci                      NOUVEAU_BO_RD, bo);
228bf215546Sopenharmony_ci   }
229bf215546Sopenharmony_ci   nv50->base.vbo_dirty = true;
230bf215546Sopenharmony_ci}
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_cistatic void
233bf215546Sopenharmony_cinv50_update_user_vbufs(struct nv50_context *nv50)
234bf215546Sopenharmony_ci{
235bf215546Sopenharmony_ci   uint64_t address[PIPE_MAX_ATTRIBS];
236bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = nv50->base.pushbuf;
237bf215546Sopenharmony_ci   unsigned i;
238bf215546Sopenharmony_ci   uint32_t written = 0;
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci   for (i = 0; i < nv50->vertex->num_elements; ++i) {
241bf215546Sopenharmony_ci      struct pipe_vertex_element *ve = &nv50->vertex->element[i].pipe;
242bf215546Sopenharmony_ci      const unsigned b = ve->vertex_buffer_index;
243bf215546Sopenharmony_ci      struct pipe_vertex_buffer *vb;
244bf215546Sopenharmony_ci      uint32_t base, size;
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci      assert(b < PIPE_MAX_ATTRIBS);
247bf215546Sopenharmony_ci      vb = &nv50->vtxbuf[b];
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci      if (!(nv50->vbo_user & (1 << b)))
250bf215546Sopenharmony_ci         continue;
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci      if (!vb->stride) {
253bf215546Sopenharmony_ci         nv50_emit_vtxattr(nv50, vb, ve, i);
254bf215546Sopenharmony_ci         continue;
255bf215546Sopenharmony_ci      }
256bf215546Sopenharmony_ci      nv50_user_vbuf_range(nv50, b, &base, &size);
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci      if (!(written & (1 << b))) {
259bf215546Sopenharmony_ci         struct nouveau_bo *bo;
260bf215546Sopenharmony_ci         const uint32_t bo_flags = NOUVEAU_BO_GART | NOUVEAU_BO_RD;
261bf215546Sopenharmony_ci         written |= 1 << b;
262bf215546Sopenharmony_ci         address[b] = nouveau_scratch_data(&nv50->base, vb->buffer.user,
263bf215546Sopenharmony_ci                                           base, size, &bo);
264bf215546Sopenharmony_ci         if (address[b])
265bf215546Sopenharmony_ci            BCTX_REFN_bo(nv50->bufctx_3d, 3D_VERTEX_TMP, bo_flags, bo);
266bf215546Sopenharmony_ci      }
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_LIMIT_HIGH(i)), 2);
269bf215546Sopenharmony_ci      PUSH_DATAh(push, address[b] + base + size - 1);
270bf215546Sopenharmony_ci      PUSH_DATA (push, address[b] + base + size - 1);
271bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_START_HIGH(i)), 2);
272bf215546Sopenharmony_ci      PUSH_DATAh(push, address[b] + ve->src_offset);
273bf215546Sopenharmony_ci      PUSH_DATA (push, address[b] + ve->src_offset);
274bf215546Sopenharmony_ci   }
275bf215546Sopenharmony_ci   nv50->base.vbo_dirty = true;
276bf215546Sopenharmony_ci}
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_cistatic inline void
279bf215546Sopenharmony_cinv50_release_user_vbufs(struct nv50_context *nv50)
280bf215546Sopenharmony_ci{
281bf215546Sopenharmony_ci   if (nv50->vbo_user) {
282bf215546Sopenharmony_ci      nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_VERTEX_TMP);
283bf215546Sopenharmony_ci      nouveau_scratch_done(&nv50->base);
284bf215546Sopenharmony_ci   }
285bf215546Sopenharmony_ci}
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_civoid
288bf215546Sopenharmony_cinv50_vertex_arrays_validate(struct nv50_context *nv50)
289bf215546Sopenharmony_ci{
290bf215546Sopenharmony_ci   uint64_t addrs[PIPE_MAX_ATTRIBS];
291bf215546Sopenharmony_ci   uint32_t limits[PIPE_MAX_ATTRIBS];
292bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = nv50->base.pushbuf;
293bf215546Sopenharmony_ci   struct nv50_vertex_stateobj *vertex = nv50->vertex;
294bf215546Sopenharmony_ci   struct pipe_vertex_buffer *vb;
295bf215546Sopenharmony_ci   struct nv50_vertex_element *ve;
296bf215546Sopenharmony_ci   uint32_t mask;
297bf215546Sopenharmony_ci   uint32_t refd = 0;
298bf215546Sopenharmony_ci   unsigned i;
299bf215546Sopenharmony_ci   const unsigned n = MAX2(vertex->num_elements, nv50->state.num_vtxelts);
300bf215546Sopenharmony_ci
301bf215546Sopenharmony_ci   if (unlikely(vertex->need_conversion))
302bf215546Sopenharmony_ci      nv50->vbo_fifo = ~0;
303bf215546Sopenharmony_ci   else
304bf215546Sopenharmony_ci   if (nv50->vbo_user & ~nv50->vbo_constant)
305bf215546Sopenharmony_ci      nv50->vbo_fifo = nv50->vbo_push_hint ? ~0 : 0;
306bf215546Sopenharmony_ci   else
307bf215546Sopenharmony_ci      nv50->vbo_fifo = 0;
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   if (!nv50->vbo_fifo) {
310bf215546Sopenharmony_ci      /* if vertex buffer was written by GPU - flush VBO cache */
311bf215546Sopenharmony_ci      assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
312bf215546Sopenharmony_ci      for (i = 0; i < nv50->num_vtxbufs; ++i) {
313bf215546Sopenharmony_ci         struct nv04_resource *buf = nv04_resource(nv50->vtxbuf[i].buffer.resource);
314bf215546Sopenharmony_ci         if (!nv50->vtxbuf[i].is_user_buffer &&
315bf215546Sopenharmony_ci             buf && buf->status & NOUVEAU_BUFFER_STATUS_GPU_WRITING) {
316bf215546Sopenharmony_ci            buf->status &= ~NOUVEAU_BUFFER_STATUS_GPU_WRITING;
317bf215546Sopenharmony_ci            nv50->base.vbo_dirty = true;
318bf215546Sopenharmony_ci         }
319bf215546Sopenharmony_ci      }
320bf215546Sopenharmony_ci   }
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci   /* update vertex format state */
323bf215546Sopenharmony_ci   BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_ATTRIB(0)), n);
324bf215546Sopenharmony_ci   if (nv50->vbo_fifo) {
325bf215546Sopenharmony_ci      nv50->state.num_vtxelts = vertex->num_elements;
326bf215546Sopenharmony_ci      for (i = 0; i < vertex->num_elements; ++i)
327bf215546Sopenharmony_ci         PUSH_DATA (push, vertex->element[i].state);
328bf215546Sopenharmony_ci      for (; i < n; ++i)
329bf215546Sopenharmony_ci         PUSH_DATA (push, NV50_3D_VERTEX_ATTRIB_INACTIVE);
330bf215546Sopenharmony_ci      for (i = 0; i < n; ++i) {
331bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_FETCH(i)), 1);
332bf215546Sopenharmony_ci         PUSH_DATA (push, 0);
333bf215546Sopenharmony_ci      }
334bf215546Sopenharmony_ci      return;
335bf215546Sopenharmony_ci   }
336bf215546Sopenharmony_ci   for (i = 0; i < vertex->num_elements; ++i) {
337bf215546Sopenharmony_ci      const unsigned b = vertex->element[i].pipe.vertex_buffer_index;
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci      assert(b < PIPE_MAX_ATTRIBS);
340bf215546Sopenharmony_ci      ve = &vertex->element[i];
341bf215546Sopenharmony_ci      vb = &nv50->vtxbuf[b];
342bf215546Sopenharmony_ci
343bf215546Sopenharmony_ci      if (likely(vb->stride) || !(nv50->vbo_user & (1 << b)))
344bf215546Sopenharmony_ci         PUSH_DATA(push, ve->state);
345bf215546Sopenharmony_ci      else
346bf215546Sopenharmony_ci         PUSH_DATA(push, ve->state | NV50_3D_VERTEX_ARRAY_ATTRIB_CONST);
347bf215546Sopenharmony_ci   }
348bf215546Sopenharmony_ci   for (; i < n; ++i)
349bf215546Sopenharmony_ci      PUSH_DATA(push, NV50_3D_VERTEX_ATTRIB_INACTIVE);
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci   /* update per-instance enables */
352bf215546Sopenharmony_ci   mask = vertex->instance_elts ^ nv50->state.instance_elts;
353bf215546Sopenharmony_ci   while (mask) {
354bf215546Sopenharmony_ci      const int i = ffs(mask) - 1;
355bf215546Sopenharmony_ci      mask &= ~(1 << i);
356bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_PER_INSTANCE(i)), 1);
357bf215546Sopenharmony_ci      PUSH_DATA (push, (vertex->instance_elts >> i) & 1);
358bf215546Sopenharmony_ci   }
359bf215546Sopenharmony_ci   nv50->state.instance_elts = vertex->instance_elts;
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci   if (nv50->vbo_user & ~nv50->vbo_constant)
362bf215546Sopenharmony_ci      nv50_upload_user_buffers(nv50, addrs, limits);
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci   /* update buffers and set constant attributes */
365bf215546Sopenharmony_ci   for (i = 0; i < vertex->num_elements; ++i) {
366bf215546Sopenharmony_ci      uint64_t address, limit;
367bf215546Sopenharmony_ci      const unsigned b = vertex->element[i].pipe.vertex_buffer_index;
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci      assert(b < PIPE_MAX_ATTRIBS);
370bf215546Sopenharmony_ci      ve = &vertex->element[i];
371bf215546Sopenharmony_ci      vb = &nv50->vtxbuf[b];
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_ci      if (unlikely(nv50->vbo_constant & (1 << b))) {
374bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_FETCH(i)), 1);
375bf215546Sopenharmony_ci         PUSH_DATA (push, 0);
376bf215546Sopenharmony_ci         nv50_emit_vtxattr(nv50, vb, &ve->pipe, i);
377bf215546Sopenharmony_ci         continue;
378bf215546Sopenharmony_ci      } else
379bf215546Sopenharmony_ci      if (nv50->vbo_user & (1 << b)) {
380bf215546Sopenharmony_ci         address = addrs[b] + ve->pipe.src_offset;
381bf215546Sopenharmony_ci         limit = addrs[b] + limits[b];
382bf215546Sopenharmony_ci      } else
383bf215546Sopenharmony_ci      if (!vb->buffer.resource) {
384bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_FETCH(i)), 1);
385bf215546Sopenharmony_ci         PUSH_DATA (push, 0);
386bf215546Sopenharmony_ci         continue;
387bf215546Sopenharmony_ci      } else {
388bf215546Sopenharmony_ci         struct nv04_resource *buf = nv04_resource(vb->buffer.resource);
389bf215546Sopenharmony_ci         if (!(refd & (1 << b))) {
390bf215546Sopenharmony_ci            refd |= 1 << b;
391bf215546Sopenharmony_ci            BCTX_REFN(nv50->bufctx_3d, 3D_VERTEX, buf, RD);
392bf215546Sopenharmony_ci         }
393bf215546Sopenharmony_ci         address = buf->address + vb->buffer_offset + ve->pipe.src_offset;
394bf215546Sopenharmony_ci         limit = buf->address + buf->base.width0 - 1;
395bf215546Sopenharmony_ci      }
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci      if (unlikely(ve->pipe.instance_divisor)) {
398bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_FETCH(i)), 4);
399bf215546Sopenharmony_ci         PUSH_DATA (push, NV50_3D_VERTEX_ARRAY_FETCH_ENABLE | vb->stride);
400bf215546Sopenharmony_ci         PUSH_DATAh(push, address);
401bf215546Sopenharmony_ci         PUSH_DATA (push, address);
402bf215546Sopenharmony_ci         PUSH_DATA (push, ve->pipe.instance_divisor);
403bf215546Sopenharmony_ci      } else {
404bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_FETCH(i)), 3);
405bf215546Sopenharmony_ci         PUSH_DATA (push, NV50_3D_VERTEX_ARRAY_FETCH_ENABLE | vb->stride);
406bf215546Sopenharmony_ci         PUSH_DATAh(push, address);
407bf215546Sopenharmony_ci         PUSH_DATA (push, address);
408bf215546Sopenharmony_ci      }
409bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_LIMIT_HIGH(i)), 2);
410bf215546Sopenharmony_ci      PUSH_DATAh(push, limit);
411bf215546Sopenharmony_ci      PUSH_DATA (push, limit);
412bf215546Sopenharmony_ci   }
413bf215546Sopenharmony_ci   for (; i < nv50->state.num_vtxelts; ++i) {
414bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_FETCH(i)), 1);
415bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
416bf215546Sopenharmony_ci   }
417bf215546Sopenharmony_ci   nv50->state.num_vtxelts = vertex->num_elements;
418bf215546Sopenharmony_ci}
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci#define NV50_PRIM_GL_CASE(n) \
421bf215546Sopenharmony_ci   case PIPE_PRIM_##n: return NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_cistatic inline unsigned
424bf215546Sopenharmony_cinv50_prim_gl(unsigned prim)
425bf215546Sopenharmony_ci{
426bf215546Sopenharmony_ci   switch (prim) {
427bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(POINTS);
428bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(LINES);
429bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(LINE_LOOP);
430bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(LINE_STRIP);
431bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(TRIANGLES);
432bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(TRIANGLE_STRIP);
433bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(TRIANGLE_FAN);
434bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(QUADS);
435bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(QUAD_STRIP);
436bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(POLYGON);
437bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(LINES_ADJACENCY);
438bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(LINE_STRIP_ADJACENCY);
439bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(TRIANGLES_ADJACENCY);
440bf215546Sopenharmony_ci   NV50_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY);
441bf215546Sopenharmony_ci   default:
442bf215546Sopenharmony_ci      return NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS;
443bf215546Sopenharmony_ci      break;
444bf215546Sopenharmony_ci   }
445bf215546Sopenharmony_ci}
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci/* For pre-nva0 transform feedback. */
448bf215546Sopenharmony_cistatic const uint8_t nv50_pipe_prim_to_prim_size[PIPE_PRIM_MAX + 1] =
449bf215546Sopenharmony_ci{
450bf215546Sopenharmony_ci   [PIPE_PRIM_POINTS] = 1,
451bf215546Sopenharmony_ci   [PIPE_PRIM_LINES] = 2,
452bf215546Sopenharmony_ci   [PIPE_PRIM_LINE_LOOP] = 2,
453bf215546Sopenharmony_ci   [PIPE_PRIM_LINE_STRIP] = 2,
454bf215546Sopenharmony_ci   [PIPE_PRIM_TRIANGLES] = 3,
455bf215546Sopenharmony_ci   [PIPE_PRIM_TRIANGLE_STRIP] = 3,
456bf215546Sopenharmony_ci   [PIPE_PRIM_TRIANGLE_FAN] = 3,
457bf215546Sopenharmony_ci   [PIPE_PRIM_QUADS] = 3,
458bf215546Sopenharmony_ci   [PIPE_PRIM_QUAD_STRIP] = 3,
459bf215546Sopenharmony_ci   [PIPE_PRIM_POLYGON] = 3,
460bf215546Sopenharmony_ci   [PIPE_PRIM_LINES_ADJACENCY] = 2,
461bf215546Sopenharmony_ci   [PIPE_PRIM_LINE_STRIP_ADJACENCY] = 2,
462bf215546Sopenharmony_ci   [PIPE_PRIM_TRIANGLES_ADJACENCY] = 3,
463bf215546Sopenharmony_ci   [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = 3
464bf215546Sopenharmony_ci};
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_cistatic void
467bf215546Sopenharmony_cinv50_draw_arrays(struct nv50_context *nv50,
468bf215546Sopenharmony_ci                 unsigned mode, unsigned start, unsigned count,
469bf215546Sopenharmony_ci                 unsigned instance_count)
470bf215546Sopenharmony_ci{
471bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = nv50->base.pushbuf;
472bf215546Sopenharmony_ci   unsigned prim;
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci   if (nv50->state.index_bias) {
475bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VB_ELEMENT_BASE), 1);
476bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
477bf215546Sopenharmony_ci      if (nv50->screen->base.class_3d >= NV84_3D_CLASS) {
478bf215546Sopenharmony_ci         BEGIN_NV04(push, NV84_3D(VERTEX_ID_BASE), 1);
479bf215546Sopenharmony_ci         PUSH_DATA (push, 0);
480bf215546Sopenharmony_ci      }
481bf215546Sopenharmony_ci      nv50->state.index_bias = 0;
482bf215546Sopenharmony_ci   }
483bf215546Sopenharmony_ci
484bf215546Sopenharmony_ci   prim = nv50_prim_gl(mode);
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci   while (instance_count--) {
487bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_BEGIN_GL), 1);
488bf215546Sopenharmony_ci      PUSH_DATA (push, prim);
489bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_BUFFER_FIRST), 2);
490bf215546Sopenharmony_ci      PUSH_DATA (push, start);
491bf215546Sopenharmony_ci      PUSH_DATA (push, count);
492bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_END_GL), 1);
493bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_ci      prim |= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
496bf215546Sopenharmony_ci   }
497bf215546Sopenharmony_ci}
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_cistatic void
500bf215546Sopenharmony_cinv50_draw_elements_inline_u08(struct nouveau_pushbuf *push, const uint8_t *map,
501bf215546Sopenharmony_ci                              unsigned start, unsigned count)
502bf215546Sopenharmony_ci{
503bf215546Sopenharmony_ci   map += start;
504bf215546Sopenharmony_ci
505bf215546Sopenharmony_ci   if (count & 3) {
506bf215546Sopenharmony_ci      unsigned i;
507bf215546Sopenharmony_ci      BEGIN_NI04(push, NV50_3D(VB_ELEMENT_U32), count & 3);
508bf215546Sopenharmony_ci      for (i = 0; i < (count & 3); ++i)
509bf215546Sopenharmony_ci         PUSH_DATA(push, *map++);
510bf215546Sopenharmony_ci      count &= ~3;
511bf215546Sopenharmony_ci   }
512bf215546Sopenharmony_ci   while (count) {
513bf215546Sopenharmony_ci      unsigned i, nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN * 4) / 4;
514bf215546Sopenharmony_ci
515bf215546Sopenharmony_ci      BEGIN_NI04(push, NV50_3D(VB_ELEMENT_U8), nr);
516bf215546Sopenharmony_ci      for (i = 0; i < nr; ++i) {
517bf215546Sopenharmony_ci         PUSH_DATA(push,
518bf215546Sopenharmony_ci                   (map[3] << 24) | (map[2] << 16) | (map[1] << 8) | map[0]);
519bf215546Sopenharmony_ci         map += 4;
520bf215546Sopenharmony_ci      }
521bf215546Sopenharmony_ci      count -= nr * 4;
522bf215546Sopenharmony_ci   }
523bf215546Sopenharmony_ci}
524bf215546Sopenharmony_ci
525bf215546Sopenharmony_cistatic void
526bf215546Sopenharmony_cinv50_draw_elements_inline_u16(struct nouveau_pushbuf *push, const uint16_t *map,
527bf215546Sopenharmony_ci                              unsigned start, unsigned count)
528bf215546Sopenharmony_ci{
529bf215546Sopenharmony_ci   map += start;
530bf215546Sopenharmony_ci
531bf215546Sopenharmony_ci   if (count & 1) {
532bf215546Sopenharmony_ci      count &= ~1;
533bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VB_ELEMENT_U32), 1);
534bf215546Sopenharmony_ci      PUSH_DATA (push, *map++);
535bf215546Sopenharmony_ci   }
536bf215546Sopenharmony_ci   while (count) {
537bf215546Sopenharmony_ci      unsigned i, nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN * 2) / 2;
538bf215546Sopenharmony_ci
539bf215546Sopenharmony_ci      BEGIN_NI04(push, NV50_3D(VB_ELEMENT_U16), nr);
540bf215546Sopenharmony_ci      for (i = 0; i < nr; ++i) {
541bf215546Sopenharmony_ci         PUSH_DATA(push, (map[1] << 16) | map[0]);
542bf215546Sopenharmony_ci         map += 2;
543bf215546Sopenharmony_ci      }
544bf215546Sopenharmony_ci      count -= nr * 2;
545bf215546Sopenharmony_ci   }
546bf215546Sopenharmony_ci}
547bf215546Sopenharmony_ci
548bf215546Sopenharmony_cistatic void
549bf215546Sopenharmony_cinv50_draw_elements_inline_u32(struct nouveau_pushbuf *push, const uint32_t *map,
550bf215546Sopenharmony_ci                              unsigned start, unsigned count)
551bf215546Sopenharmony_ci{
552bf215546Sopenharmony_ci   map += start;
553bf215546Sopenharmony_ci
554bf215546Sopenharmony_ci   while (count) {
555bf215546Sopenharmony_ci      const unsigned nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN);
556bf215546Sopenharmony_ci
557bf215546Sopenharmony_ci      BEGIN_NI04(push, NV50_3D(VB_ELEMENT_U32), nr);
558bf215546Sopenharmony_ci      PUSH_DATAp(push, map, nr);
559bf215546Sopenharmony_ci
560bf215546Sopenharmony_ci      map += nr;
561bf215546Sopenharmony_ci      count -= nr;
562bf215546Sopenharmony_ci   }
563bf215546Sopenharmony_ci}
564bf215546Sopenharmony_ci
565bf215546Sopenharmony_cistatic void
566bf215546Sopenharmony_cinv50_draw_elements_inline_u32_short(struct nouveau_pushbuf *push,
567bf215546Sopenharmony_ci                                    const uint32_t *map,
568bf215546Sopenharmony_ci                                    unsigned start, unsigned count)
569bf215546Sopenharmony_ci{
570bf215546Sopenharmony_ci   map += start;
571bf215546Sopenharmony_ci
572bf215546Sopenharmony_ci   if (count & 1) {
573bf215546Sopenharmony_ci      count--;
574bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VB_ELEMENT_U32), 1);
575bf215546Sopenharmony_ci      PUSH_DATA (push, *map++);
576bf215546Sopenharmony_ci   }
577bf215546Sopenharmony_ci   while (count) {
578bf215546Sopenharmony_ci      unsigned i, nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN * 2) / 2;
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci      BEGIN_NI04(push, NV50_3D(VB_ELEMENT_U16), nr);
581bf215546Sopenharmony_ci      for (i = 0; i < nr; ++i) {
582bf215546Sopenharmony_ci         PUSH_DATA(push, (map[1] << 16) | map[0]);
583bf215546Sopenharmony_ci         map += 2;
584bf215546Sopenharmony_ci      }
585bf215546Sopenharmony_ci      count -= nr * 2;
586bf215546Sopenharmony_ci   }
587bf215546Sopenharmony_ci}
588bf215546Sopenharmony_ci
589bf215546Sopenharmony_cistatic void
590bf215546Sopenharmony_cinv50_draw_elements(struct nv50_context *nv50, bool shorten,
591bf215546Sopenharmony_ci                   const struct pipe_draw_info *info,
592bf215546Sopenharmony_ci                   unsigned mode, unsigned start, unsigned count,
593bf215546Sopenharmony_ci                   unsigned instance_count, int32_t index_bias,
594bf215546Sopenharmony_ci		   unsigned index_size)
595bf215546Sopenharmony_ci{
596bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = nv50->base.pushbuf;
597bf215546Sopenharmony_ci   unsigned prim;
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_ci   prim = nv50_prim_gl(mode);
600bf215546Sopenharmony_ci
601bf215546Sopenharmony_ci   if (index_bias != nv50->state.index_bias) {
602bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VB_ELEMENT_BASE), 1);
603bf215546Sopenharmony_ci      PUSH_DATA (push, index_bias);
604bf215546Sopenharmony_ci      if (nv50->screen->base.class_3d >= NV84_3D_CLASS) {
605bf215546Sopenharmony_ci         BEGIN_NV04(push, NV84_3D(VERTEX_ID_BASE), 1);
606bf215546Sopenharmony_ci         PUSH_DATA (push, index_bias);
607bf215546Sopenharmony_ci      }
608bf215546Sopenharmony_ci      nv50->state.index_bias = index_bias;
609bf215546Sopenharmony_ci   }
610bf215546Sopenharmony_ci
611bf215546Sopenharmony_ci   if (!info->has_user_indices) {
612bf215546Sopenharmony_ci      struct nv04_resource *buf = nv04_resource(info->index.resource);
613bf215546Sopenharmony_ci      unsigned pb_start;
614bf215546Sopenharmony_ci      unsigned pb_bytes;
615bf215546Sopenharmony_ci      const unsigned base = buf->offset & ~3;
616bf215546Sopenharmony_ci
617bf215546Sopenharmony_ci      start += (buf->offset & 3) >> (index_size >> 1);
618bf215546Sopenharmony_ci
619bf215546Sopenharmony_ci      assert(nouveau_resource_mapped_by_gpu(info->index.resource));
620bf215546Sopenharmony_ci
621bf215546Sopenharmony_ci      /* This shouldn't have to be here. The going theory is that the buffer
622bf215546Sopenharmony_ci       * is being filled in by PGRAPH, and it's not done yet by the time it
623bf215546Sopenharmony_ci       * gets submitted to PFIFO, which in turn starts immediately prefetching
624bf215546Sopenharmony_ci       * the not-yet-written data. Ideally this wait would only happen on
625bf215546Sopenharmony_ci       * pushbuf submit, but it's probably not a big performance difference.
626bf215546Sopenharmony_ci       */
627bf215546Sopenharmony_ci      if (buf->fence_wr && !nouveau_fence_signalled(buf->fence_wr))
628bf215546Sopenharmony_ci         nouveau_fence_wait(buf->fence_wr, &nv50->base.debug);
629bf215546Sopenharmony_ci
630bf215546Sopenharmony_ci      while (instance_count--) {
631bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_BEGIN_GL), 1);
632bf215546Sopenharmony_ci         PUSH_DATA (push, prim);
633bf215546Sopenharmony_ci
634bf215546Sopenharmony_ci         nouveau_pushbuf_space(push, 16, 0, 1);
635bf215546Sopenharmony_ci         PUSH_REFN(push, buf->bo, NOUVEAU_BO_RD | buf->domain);
636bf215546Sopenharmony_ci
637bf215546Sopenharmony_ci         switch (index_size) {
638bf215546Sopenharmony_ci         case 4:
639bf215546Sopenharmony_ci            BEGIN_NL50(push, NV50_3D(VB_ELEMENT_U32), count);
640bf215546Sopenharmony_ci            nouveau_pushbuf_data(push, buf->bo, base + start * 4, count * 4);
641bf215546Sopenharmony_ci            break;
642bf215546Sopenharmony_ci         case 2:
643bf215546Sopenharmony_ci            pb_start = (start & ~1) * 2;
644bf215546Sopenharmony_ci            pb_bytes = ((start + count + 1) & ~1) * 2 - pb_start;
645bf215546Sopenharmony_ci
646bf215546Sopenharmony_ci            BEGIN_NV04(push, NV50_3D(VB_ELEMENT_U16_SETUP), 1);
647bf215546Sopenharmony_ci            PUSH_DATA (push, (start << 31) | count);
648bf215546Sopenharmony_ci            BEGIN_NL50(push, NV50_3D(VB_ELEMENT_U16), pb_bytes / 4);
649bf215546Sopenharmony_ci            nouveau_pushbuf_data(push, buf->bo, base + pb_start, pb_bytes);
650bf215546Sopenharmony_ci            BEGIN_NV04(push, NV50_3D(VB_ELEMENT_U16_SETUP), 1);
651bf215546Sopenharmony_ci            PUSH_DATA (push, 0);
652bf215546Sopenharmony_ci            break;
653bf215546Sopenharmony_ci         default:
654bf215546Sopenharmony_ci            assert(index_size == 1);
655bf215546Sopenharmony_ci            pb_start = start & ~3;
656bf215546Sopenharmony_ci            pb_bytes = ((start + count + 3) & ~3) - pb_start;
657bf215546Sopenharmony_ci
658bf215546Sopenharmony_ci            BEGIN_NV04(push, NV50_3D(VB_ELEMENT_U8_SETUP), 1);
659bf215546Sopenharmony_ci            PUSH_DATA (push, (start << 30) | count);
660bf215546Sopenharmony_ci            BEGIN_NL50(push, NV50_3D(VB_ELEMENT_U8), pb_bytes / 4);
661bf215546Sopenharmony_ci            nouveau_pushbuf_data(push, buf->bo, base + pb_start, pb_bytes);
662bf215546Sopenharmony_ci            BEGIN_NV04(push, NV50_3D(VB_ELEMENT_U8_SETUP), 1);
663bf215546Sopenharmony_ci            PUSH_DATA (push, 0);
664bf215546Sopenharmony_ci            break;
665bf215546Sopenharmony_ci         }
666bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_END_GL), 1);
667bf215546Sopenharmony_ci         PUSH_DATA (push, 0);
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci         prim |= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
670bf215546Sopenharmony_ci      }
671bf215546Sopenharmony_ci   } else {
672bf215546Sopenharmony_ci      const void *data = info->index.user;
673bf215546Sopenharmony_ci
674bf215546Sopenharmony_ci      while (instance_count--) {
675bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_BEGIN_GL), 1);
676bf215546Sopenharmony_ci         PUSH_DATA (push, prim);
677bf215546Sopenharmony_ci         switch (index_size) {
678bf215546Sopenharmony_ci         case 1:
679bf215546Sopenharmony_ci            nv50_draw_elements_inline_u08(push, data, start, count);
680bf215546Sopenharmony_ci            break;
681bf215546Sopenharmony_ci         case 2:
682bf215546Sopenharmony_ci            nv50_draw_elements_inline_u16(push, data, start, count);
683bf215546Sopenharmony_ci            break;
684bf215546Sopenharmony_ci         case 4:
685bf215546Sopenharmony_ci            if (shorten)
686bf215546Sopenharmony_ci               nv50_draw_elements_inline_u32_short(push, data, start, count);
687bf215546Sopenharmony_ci            else
688bf215546Sopenharmony_ci               nv50_draw_elements_inline_u32(push, data, start, count);
689bf215546Sopenharmony_ci            break;
690bf215546Sopenharmony_ci         default:
691bf215546Sopenharmony_ci            assert(0);
692bf215546Sopenharmony_ci            return;
693bf215546Sopenharmony_ci         }
694bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(VERTEX_END_GL), 1);
695bf215546Sopenharmony_ci         PUSH_DATA (push, 0);
696bf215546Sopenharmony_ci
697bf215546Sopenharmony_ci         prim |= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
698bf215546Sopenharmony_ci      }
699bf215546Sopenharmony_ci   }
700bf215546Sopenharmony_ci   NOUVEAU_DRV_STAT(&nv50->screen->base, draw_calls_indexed, 1);
701bf215546Sopenharmony_ci}
702bf215546Sopenharmony_ci
703bf215546Sopenharmony_cistatic void
704bf215546Sopenharmony_cinva0_draw_stream_output(struct nv50_context *nv50,
705bf215546Sopenharmony_ci                        const struct pipe_draw_info *info,
706bf215546Sopenharmony_ci                        const struct pipe_draw_indirect_info *indirect)
707bf215546Sopenharmony_ci{
708bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = nv50->base.pushbuf;
709bf215546Sopenharmony_ci   struct nv50_so_target *so = nv50_so_target(indirect->count_from_stream_output);
710bf215546Sopenharmony_ci   struct nv04_resource *res = nv04_resource(so->pipe.buffer);
711bf215546Sopenharmony_ci   unsigned num_instances = info->instance_count;
712bf215546Sopenharmony_ci   unsigned mode = nv50_prim_gl(info->mode);
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci   if (unlikely(nv50->screen->base.class_3d < NVA0_3D_CLASS)) {
715bf215546Sopenharmony_ci      /* A proper implementation without waiting doesn't seem possible,
716bf215546Sopenharmony_ci       * so don't bother.
717bf215546Sopenharmony_ci       */
718bf215546Sopenharmony_ci      NOUVEAU_ERR("draw_stream_output not supported on pre-NVA0 cards\n");
719bf215546Sopenharmony_ci      return;
720bf215546Sopenharmony_ci   }
721bf215546Sopenharmony_ci
722bf215546Sopenharmony_ci   if (res->status & NOUVEAU_BUFFER_STATUS_GPU_WRITING) {
723bf215546Sopenharmony_ci      res->status &= ~NOUVEAU_BUFFER_STATUS_GPU_WRITING;
724bf215546Sopenharmony_ci      PUSH_SPACE(push, 4);
725bf215546Sopenharmony_ci      BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
726bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
727bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_FLUSH), 1);
728bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
729bf215546Sopenharmony_ci   }
730bf215546Sopenharmony_ci
731bf215546Sopenharmony_ci   assert(num_instances);
732bf215546Sopenharmony_ci   do {
733bf215546Sopenharmony_ci      PUSH_SPACE(push, 8);
734bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_BEGIN_GL), 1);
735bf215546Sopenharmony_ci      PUSH_DATA (push, mode);
736bf215546Sopenharmony_ci      BEGIN_NV04(push, NVA0_3D(DRAW_TFB_BASE), 1);
737bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
738bf215546Sopenharmony_ci      BEGIN_NV04(push, NVA0_3D(DRAW_TFB_STRIDE), 1);
739bf215546Sopenharmony_ci      PUSH_DATA (push, so->stride);
740bf215546Sopenharmony_ci      nv50_hw_query_pushbuf_submit(push, NVA0_3D_DRAW_TFB_BYTES,
741bf215546Sopenharmony_ci                                   nv50_query(so->pq), 0x4);
742bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_END_GL), 1);
743bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
744bf215546Sopenharmony_ci
745bf215546Sopenharmony_ci      mode |= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
746bf215546Sopenharmony_ci   } while (--num_instances);
747bf215546Sopenharmony_ci}
748bf215546Sopenharmony_ci
749bf215546Sopenharmony_cistatic void
750bf215546Sopenharmony_cinv50_draw_vbo_kick_notify(struct nouveau_pushbuf *chan)
751bf215546Sopenharmony_ci{
752bf215546Sopenharmony_ci   struct nv50_screen *screen = chan->user_priv;
753bf215546Sopenharmony_ci
754bf215546Sopenharmony_ci   nouveau_fence_update(&screen->base, true);
755bf215546Sopenharmony_ci
756bf215546Sopenharmony_ci   nv50_bufctx_fence(screen->cur_ctx->bufctx_3d, true);
757bf215546Sopenharmony_ci}
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_civoid
760bf215546Sopenharmony_cinv50_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info,
761bf215546Sopenharmony_ci              unsigned drawid_offset,
762bf215546Sopenharmony_ci              const struct pipe_draw_indirect_info *indirect,
763bf215546Sopenharmony_ci              const struct pipe_draw_start_count_bias *draws,
764bf215546Sopenharmony_ci              unsigned num_draws)
765bf215546Sopenharmony_ci{
766bf215546Sopenharmony_ci   if (num_draws > 1) {
767bf215546Sopenharmony_ci      util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);
768bf215546Sopenharmony_ci      return;
769bf215546Sopenharmony_ci   }
770bf215546Sopenharmony_ci
771bf215546Sopenharmony_ci   if (!indirect && (!draws[0].count || !info->instance_count))
772bf215546Sopenharmony_ci      return;
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_ci   /* We don't actually support indirect draws, so add a fallback for ES 3.1's
775bf215546Sopenharmony_ci    * benefit.
776bf215546Sopenharmony_ci    */
777bf215546Sopenharmony_ci   if (indirect && indirect->buffer) {
778bf215546Sopenharmony_ci      util_draw_indirect(pipe, info, indirect);
779bf215546Sopenharmony_ci      return;
780bf215546Sopenharmony_ci   }
781bf215546Sopenharmony_ci
782bf215546Sopenharmony_ci   struct nv50_context *nv50 = nv50_context(pipe);
783bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = nv50->base.pushbuf;
784bf215546Sopenharmony_ci   bool tex_dirty = false;
785bf215546Sopenharmony_ci   int s;
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci   if (info->index_size && !info->has_user_indices)
788bf215546Sopenharmony_ci      BCTX_REFN(nv50->bufctx_3d, 3D_INDEX, nv04_resource(info->index.resource), RD);
789bf215546Sopenharmony_ci
790bf215546Sopenharmony_ci   /* NOTE: caller must ensure that (min_index + index_bias) is >= 0 */
791bf215546Sopenharmony_ci   if (info->index_bounds_valid) {
792bf215546Sopenharmony_ci      nv50->vb_elt_first = info->min_index + (info->index_size ? draws->index_bias : 0);
793bf215546Sopenharmony_ci      nv50->vb_elt_limit = info->max_index - info->min_index;
794bf215546Sopenharmony_ci   } else {
795bf215546Sopenharmony_ci      nv50->vb_elt_first = 0;
796bf215546Sopenharmony_ci      nv50->vb_elt_limit = ~0;
797bf215546Sopenharmony_ci   }
798bf215546Sopenharmony_ci   nv50->instance_off = info->start_instance;
799bf215546Sopenharmony_ci   nv50->instance_max = info->instance_count - 1;
800bf215546Sopenharmony_ci
801bf215546Sopenharmony_ci   /* For picking only a few vertices from a large user buffer, push is better,
802bf215546Sopenharmony_ci    * if index count is larger and we expect repeated vertices, suggest upload.
803bf215546Sopenharmony_ci    */
804bf215546Sopenharmony_ci   nv50->vbo_push_hint = /* the 64 is heuristic */
805bf215546Sopenharmony_ci      !(info->index_size && ((nv50->vb_elt_limit + 64) < draws[0].count));
806bf215546Sopenharmony_ci
807bf215546Sopenharmony_ci   if (nv50->vbo_user && !(nv50->dirty_3d & (NV50_NEW_3D_ARRAYS | NV50_NEW_3D_VERTEX))) {
808bf215546Sopenharmony_ci      if (!!nv50->vbo_fifo != nv50->vbo_push_hint)
809bf215546Sopenharmony_ci         nv50->dirty_3d |= NV50_NEW_3D_ARRAYS;
810bf215546Sopenharmony_ci      else
811bf215546Sopenharmony_ci      if (!nv50->vbo_fifo)
812bf215546Sopenharmony_ci         nv50_update_user_vbufs(nv50);
813bf215546Sopenharmony_ci   }
814bf215546Sopenharmony_ci
815bf215546Sopenharmony_ci   if (unlikely(nv50->num_so_targets && !nv50->gmtyprog))
816bf215546Sopenharmony_ci      nv50->state.prim_size = nv50_pipe_prim_to_prim_size[info->mode];
817bf215546Sopenharmony_ci
818bf215546Sopenharmony_ci   nv50_state_validate_3d(nv50, ~0);
819bf215546Sopenharmony_ci
820bf215546Sopenharmony_ci   push->kick_notify = nv50_draw_vbo_kick_notify;
821bf215546Sopenharmony_ci
822bf215546Sopenharmony_ci   for (s = 0; s < NV50_MAX_3D_SHADER_STAGES && !nv50->cb_dirty; ++s) {
823bf215546Sopenharmony_ci      if (nv50->constbuf_coherent[s])
824bf215546Sopenharmony_ci         nv50->cb_dirty = true;
825bf215546Sopenharmony_ci   }
826bf215546Sopenharmony_ci
827bf215546Sopenharmony_ci   /* If there are any coherent constbufs, flush the cache */
828bf215546Sopenharmony_ci   if (nv50->cb_dirty) {
829bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(CODE_CB_FLUSH), 1);
830bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
831bf215546Sopenharmony_ci      nv50->cb_dirty = false;
832bf215546Sopenharmony_ci   }
833bf215546Sopenharmony_ci
834bf215546Sopenharmony_ci   for (s = 0; s < NV50_MAX_3D_SHADER_STAGES && !tex_dirty; ++s) {
835bf215546Sopenharmony_ci      if (nv50->textures_coherent[s])
836bf215546Sopenharmony_ci         tex_dirty = true;
837bf215546Sopenharmony_ci   }
838bf215546Sopenharmony_ci
839bf215546Sopenharmony_ci   if (tex_dirty) {
840bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(TEX_CACHE_CTL), 1);
841bf215546Sopenharmony_ci      PUSH_DATA (push, 0x20);
842bf215546Sopenharmony_ci   }
843bf215546Sopenharmony_ci
844bf215546Sopenharmony_ci   if (nv50->screen->base.class_3d >= NVA0_3D_CLASS &&
845bf215546Sopenharmony_ci       nv50->seamless_cube_map != nv50->state.seamless_cube_map) {
846bf215546Sopenharmony_ci      nv50->state.seamless_cube_map = nv50->seamless_cube_map;
847bf215546Sopenharmony_ci      BEGIN_NV04(push, SUBC_3D(NVA0_3D_TEX_MISC), 1);
848bf215546Sopenharmony_ci      PUSH_DATA (push, nv50->seamless_cube_map ? NVA0_3D_TEX_MISC_SEAMLESS_CUBE_MAP : 0);
849bf215546Sopenharmony_ci   }
850bf215546Sopenharmony_ci
851bf215546Sopenharmony_ci   if (nv50->vertprog->mul_zero_wins != nv50->state.mul_zero_wins) {
852bf215546Sopenharmony_ci      nv50->state.mul_zero_wins = nv50->vertprog->mul_zero_wins;
853bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(UNK1690), 1);
854bf215546Sopenharmony_ci      PUSH_DATA (push, 0x00010000 * !!nv50->state.mul_zero_wins);
855bf215546Sopenharmony_ci   }
856bf215546Sopenharmony_ci
857bf215546Sopenharmony_ci   /* Make starting/pausing streamout work pre-NVA0 enough for ES3.0. This
858bf215546Sopenharmony_ci    * means counting vertices in a vertex shader when it has so outputs.
859bf215546Sopenharmony_ci    */
860bf215546Sopenharmony_ci   if (nv50->screen->base.class_3d < NVA0_3D_CLASS &&
861bf215546Sopenharmony_ci       nv50->vertprog->pipe.stream_output.num_outputs) {
862bf215546Sopenharmony_ci      for (int i = 0; i < nv50->num_so_targets; i++) {
863bf215546Sopenharmony_ci         nv50->so_used[i] += info->instance_count *
864bf215546Sopenharmony_ci            u_stream_outputs_for_vertices(info->mode, draws[0].count) *
865bf215546Sopenharmony_ci            nv50->vertprog->pipe.stream_output.stride[i] * 4;
866bf215546Sopenharmony_ci      }
867bf215546Sopenharmony_ci   }
868bf215546Sopenharmony_ci
869bf215546Sopenharmony_ci   if (nv50->vbo_fifo) {
870bf215546Sopenharmony_ci      nv50_push_vbo(nv50, info, indirect, &draws[0]);
871bf215546Sopenharmony_ci      goto cleanup;
872bf215546Sopenharmony_ci   }
873bf215546Sopenharmony_ci
874bf215546Sopenharmony_ci   if (nv50->state.instance_base != info->start_instance) {
875bf215546Sopenharmony_ci      nv50->state.instance_base = info->start_instance;
876bf215546Sopenharmony_ci      /* NOTE: this does not affect the shader input, should it ? */
877bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VB_INSTANCE_BASE), 1);
878bf215546Sopenharmony_ci      PUSH_DATA (push, info->start_instance);
879bf215546Sopenharmony_ci   }
880bf215546Sopenharmony_ci
881bf215546Sopenharmony_ci   nv50->base.vbo_dirty |= !!nv50->vtxbufs_coherent;
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci   if (nv50->base.vbo_dirty) {
884bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(VERTEX_ARRAY_FLUSH), 1);
885bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
886bf215546Sopenharmony_ci      nv50->base.vbo_dirty = false;
887bf215546Sopenharmony_ci   }
888bf215546Sopenharmony_ci
889bf215546Sopenharmony_ci   if (info->index_size) {
890bf215546Sopenharmony_ci      bool shorten = info->index_bounds_valid && info->max_index <= 65535;
891bf215546Sopenharmony_ci
892bf215546Sopenharmony_ci      if (info->primitive_restart != nv50->state.prim_restart) {
893bf215546Sopenharmony_ci         if (info->primitive_restart) {
894bf215546Sopenharmony_ci            BEGIN_NV04(push, NV50_3D(PRIM_RESTART_ENABLE), 2);
895bf215546Sopenharmony_ci            PUSH_DATA (push, 1);
896bf215546Sopenharmony_ci            PUSH_DATA (push, info->restart_index);
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_ci            if (info->restart_index > 65535)
899bf215546Sopenharmony_ci               shorten = false;
900bf215546Sopenharmony_ci         } else {
901bf215546Sopenharmony_ci            BEGIN_NV04(push, NV50_3D(PRIM_RESTART_ENABLE), 1);
902bf215546Sopenharmony_ci            PUSH_DATA (push, 0);
903bf215546Sopenharmony_ci         }
904bf215546Sopenharmony_ci         nv50->state.prim_restart = info->primitive_restart;
905bf215546Sopenharmony_ci      } else
906bf215546Sopenharmony_ci      if (info->primitive_restart) {
907bf215546Sopenharmony_ci         BEGIN_NV04(push, NV50_3D(PRIM_RESTART_INDEX), 1);
908bf215546Sopenharmony_ci         PUSH_DATA (push, info->restart_index);
909bf215546Sopenharmony_ci
910bf215546Sopenharmony_ci         if (info->restart_index > 65535)
911bf215546Sopenharmony_ci            shorten = false;
912bf215546Sopenharmony_ci      }
913bf215546Sopenharmony_ci
914bf215546Sopenharmony_ci      nv50_draw_elements(nv50, shorten, info,
915bf215546Sopenharmony_ci                         info->mode, draws[0].start, draws[0].count,
916bf215546Sopenharmony_ci                         info->instance_count, draws->index_bias, info->index_size);
917bf215546Sopenharmony_ci   } else
918bf215546Sopenharmony_ci   if (unlikely(indirect && indirect->count_from_stream_output)) {
919bf215546Sopenharmony_ci      nva0_draw_stream_output(nv50, info, indirect);
920bf215546Sopenharmony_ci   } else {
921bf215546Sopenharmony_ci      nv50_draw_arrays(nv50,
922bf215546Sopenharmony_ci                       info->mode, draws[0].start, draws[0].count,
923bf215546Sopenharmony_ci                       info->instance_count);
924bf215546Sopenharmony_ci   }
925bf215546Sopenharmony_ci
926bf215546Sopenharmony_cicleanup:
927bf215546Sopenharmony_ci   push->kick_notify = nv50_default_kick_notify;
928bf215546Sopenharmony_ci
929bf215546Sopenharmony_ci   nv50_release_user_vbufs(nv50);
930bf215546Sopenharmony_ci
931bf215546Sopenharmony_ci   nouveau_pushbuf_bufctx(push, NULL);
932bf215546Sopenharmony_ci
933bf215546Sopenharmony_ci   nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_INDEX);
934bf215546Sopenharmony_ci}
935