2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "draw/draw_context.h"
27 #include "draw/draw_vertex.h"
28 #include "draw/draw_pipe.h"
29 #include "draw/draw_vbuf.h"
30 #include "draw/draw_private.h"
32 #include "nv_object.xml.h"
33 #include "nv30/nv30-40_3d.xml.h"
34 #include "nv30/nv30_context.h"
35 #include "nv30/nv30_format.h"
38 struct vbuf_render base;
39 struct nv30_context *nv30;
41 struct pipe_transfer *transfer;
42 struct pipe_resource *buffer;
46 struct vertex_info vertex_info;
48 struct nouveau_heap *vertprog;
49 uint32_t vtxprog[16][4];
55 static INLINE struct nv30_render *
56 nv30_render(struct vbuf_render *render)
58 return (struct nv30_render *)render;
61 static const struct vertex_info *
62 nv30_render_get_vertex_info(struct vbuf_render *render)
64 return &nv30_render(render)->vertex_info;
68 nv30_render_allocate_vertices(struct vbuf_render *render,
69 ushort vertex_size, ushort nr_vertices)
71 struct nv30_render *r = nv30_render(render);
72 struct nv30_context *nv30 = r->nv30;
74 r->length = vertex_size * nr_vertices;
76 if (r->offset + r->length >= render->max_vertex_buffer_bytes) {
77 pipe_resource_reference(&r->buffer, NULL);
78 r->buffer = pipe_buffer_create(&nv30->screen->base.base,
79 PIPE_BIND_VERTEX_BUFFER, 0,
80 render->max_vertex_buffer_bytes);
91 nv30_render_map_vertices(struct vbuf_render *render)
93 struct nv30_render *r = nv30_render(render);
94 char *map = pipe_buffer_map(&r->nv30->base.pipe, r->buffer,
96 PIPE_TRANSFER_UNSYNCHRONIZED, &r->transfer);
97 return map + r->offset;
101 nv30_render_unmap_vertices(struct vbuf_render *render,
102 ushort min_index, ushort max_index)
104 struct nv30_render *r = nv30_render(render);
105 pipe_buffer_unmap(&r->nv30->base.pipe, r->transfer);
110 nv30_render_set_primitive(struct vbuf_render *render, unsigned prim)
112 struct nv30_render *r = nv30_render(render);
114 r->prim = nv30_prim_gl(prim);
118 nv30_render_draw_elements(struct vbuf_render *render,
119 const ushort *indices, uint count)
121 struct nv30_render *r = nv30_render(render);
122 struct nv30_context *nv30 = r->nv30;
123 struct nouveau_pushbuf *push = nv30->screen->base.pushbuf;
126 BEGIN_NV04(push, NV30_3D(VTXBUF(0)), r->vertex_info.num_attribs);
127 for (i = 0; i < r->vertex_info.num_attribs; i++) {
128 PUSH_RESRC(push, NV30_3D(VTXBUF(i)), BUFCTX_VTXTMP,
129 nv04_resource(r->buffer), r->offset + r->vtxptr[i],
130 NOUVEAU_BO_LOW | NOUVEAU_BO_RD, 0, 0);
133 if (!nv30_state_validate(nv30, ~0, FALSE))
136 BEGIN_NV04(push, NV30_3D(VERTEX_BEGIN_END), 1);
137 PUSH_DATA (push, r->prim);
140 BEGIN_NV04(push, NV30_3D(VB_ELEMENT_U32), 1);
141 PUSH_DATA (push, *indices++);
146 unsigned npush = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN);
149 BEGIN_NI04(push, NV30_3D(VB_ELEMENT_U16), npush);
151 PUSH_DATA(push, (indices[1] << 16) | indices[0]);
156 BEGIN_NV04(push, NV30_3D(VERTEX_BEGIN_END), 1);
157 PUSH_DATA (push, NV30_3D_VERTEX_BEGIN_END_STOP);
158 PUSH_RESET(push, BUFCTX_VTXTMP);
162 nv30_render_draw_arrays(struct vbuf_render *render, unsigned start, uint nr)
164 struct nv30_render *r = nv30_render(render);
165 struct nv30_context *nv30 = r->nv30;
166 struct nouveau_pushbuf *push = nv30->base.pushbuf;
167 unsigned fn = nr >> 8, pn = nr & 0xff;
168 unsigned ps = fn + (pn ? 1 : 0);
171 BEGIN_NV04(push, NV30_3D(VTXBUF(0)), r->vertex_info.num_attribs);
172 for (i = 0; i < r->vertex_info.num_attribs; i++) {
173 PUSH_RESRC(push, NV30_3D(VTXBUF(i)), BUFCTX_VTXTMP,
174 nv04_resource(r->buffer), r->offset + r->vtxptr[i],
175 NOUVEAU_BO_LOW | NOUVEAU_BO_RD, 0, 0);
178 if (!nv30_state_validate(nv30, ~0, FALSE))
181 BEGIN_NV04(push, NV30_3D(VERTEX_BEGIN_END), 1);
182 PUSH_DATA (push, r->prim);
184 BEGIN_NI04(push, NV30_3D(VB_VERTEX_BATCH), ps);
186 PUSH_DATA (push, 0xff000000 | start);
191 PUSH_DATA (push, ((pn - 1) << 24) | start);
193 BEGIN_NV04(push, NV30_3D(VERTEX_BEGIN_END), 1);
194 PUSH_DATA (push, NV30_3D_VERTEX_BEGIN_END_STOP);
195 PUSH_RESET(push, BUFCTX_VTXTMP);
199 nv30_render_release_vertices(struct vbuf_render *render)
201 struct nv30_render *r = nv30_render(render);
202 r->offset += r->length;
205 static const struct {
212 [TGSI_SEMANTIC_POSITION] = { EMIT_4F, INTERP_PERSPECTIVE, 0, 0, 0x00000000 },
213 [TGSI_SEMANTIC_COLOR ] = { EMIT_4F, INTERP_LINEAR , 3, 1, 0x00000001 },
214 [TGSI_SEMANTIC_BCOLOR ] = { EMIT_4F, INTERP_LINEAR , 1, 3, 0x00000004 },
215 [TGSI_SEMANTIC_FOG ] = { EMIT_4F, INTERP_PERSPECTIVE, 5, 5, 0x00000010 },
216 [TGSI_SEMANTIC_PSIZE ] = { EMIT_1F_PSIZE, INTERP_POS , 6, 6, 0x00000020 },
217 [TGSI_SEMANTIC_GENERIC ] = { EMIT_4F, INTERP_PERSPECTIVE, 8, 7, 0x00004000 }
221 vroute_add(struct nv30_render *r, uint attrib, uint sem, uint *idx)
223 struct pipe_screen *pscreen = &r->nv30->screen->base.base;
224 struct nv30_fragprog *fp = r->nv30->fragprog.program;
225 struct vertex_info *vinfo = &r->vertex_info;
226 enum pipe_format format;
227 uint emit = EMIT_OMIT;
230 if (sem == TGSI_SEMANTIC_GENERIC && result >= 8) {
231 for (result = 0; result < 8; result++) {
232 if (fp->texcoord[result] == *idx) {
233 emit = vroute[sem].emit;
238 emit = vroute[sem].emit;
241 if (emit == EMIT_OMIT)
244 draw_emit_vertex_attr(vinfo, emit, vroute[sem].interp, attrib);
245 format = draw_translate_vinfo_format(emit);
247 r->vtxfmt[attrib] = nv30_vtxfmt(pscreen, format)->hw;
248 r->vtxptr[attrib] = vinfo->size | NV30_3D_VTXBUF_DMA1;
249 vinfo->size += draw_translate_vinfo_size(emit);
251 if (nv30_screen(pscreen)->eng3d->oclass < NV40_3D_CLASS) {
252 r->vtxprog[attrib][0] = 0x001f38d8;
253 r->vtxprog[attrib][1] = 0x0080001b | (attrib << 9);
254 r->vtxprog[attrib][2] = 0x0836106c;
255 r->vtxprog[attrib][3] = 0x2000f800 | (result + vroute[sem].vp30) << 2;
257 r->vtxprog[attrib][0] = 0x401f9c6c;
258 r->vtxprog[attrib][1] = 0x0040000d | (attrib << 8);
259 r->vtxprog[attrib][2] = 0x8106c083;
260 r->vtxprog[attrib][3] = 0x6041ff80 | (result + vroute[sem].vp40) << 2;
263 *idx = vroute[sem].ow40 << result;
268 nv30_render_validate(struct nv30_context *nv30)
270 struct nv30_render *r = nv30_render(nv30->draw->render);
271 struct nv30_rasterizer_stateobj *rast = nv30->rast;
272 struct pipe_screen *pscreen = &nv30->screen->base.base;
273 struct nouveau_pushbuf *push = nv30->screen->base.pushbuf;
274 struct nouveau_object *eng3d = nv30->screen->eng3d;
275 struct nv30_vertprog *vp = nv30->vertprog.program;
276 struct vertex_info *vinfo = &r->vertex_info;
277 unsigned vp_attribs = 0;
278 unsigned vp_results = 0;
284 struct nouveau_heap *heap = nv30_screen(pscreen)->vp_exec_heap;
285 if (nouveau_heap_alloc(heap, 16, &r->vertprog, &r->vertprog)) {
286 while (heap->next && heap->size < 16) {
287 struct nouveau_heap **evict = heap->next->priv;
288 nouveau_heap_free(evict);
291 if (nouveau_heap_alloc(heap, 16, &r->vertprog, &r->vertprog))
296 vinfo->num_attribs = 0;
299 /* setup routing for all necessary vp outputs */
300 for (i = 0; i < vp->info.num_outputs && attrib < 16; i++) {
301 uint semantic = vp->info.output_semantic_name[i];
302 uint index = vp->info.output_semantic_index[i];
303 if (vroute_add(r, attrib, semantic, &index)) {
304 vp_attribs |= (1 << attrib++);
309 /* setup routing for replaced point coords not written by vp */
310 if (rast && rast->pipe.point_quad_rasterization)
311 pntc = rast->pipe.sprite_coord_enable & 0x000002ff;
315 while (pntc && attrib < 16) {
316 uint index = ffs(pntc) - 1; pntc &= ~(1 << index);
317 if (vroute_add(r, attrib, TGSI_SEMANTIC_GENERIC, &index)) {
318 vp_attribs |= (1 << attrib++);
323 /* modify vertex format for correct stride, and stub out unused ones */
324 BEGIN_NV04(push, NV30_3D(VP_UPLOAD_FROM_ID), 1);
325 PUSH_DATA (push, r->vertprog->start);
326 r->vtxprog[attrib - 1][3] |= 1;
327 for (i = 0; i < attrib; i++) {
328 BEGIN_NV04(push, NV30_3D(VP_UPLOAD_INST(0)), 4);
329 PUSH_DATAp(push, r->vtxprog[i], 4);
330 r->vtxfmt[i] |= vinfo->size << 8;
333 r->vtxfmt[i] = NV30_3D_VTXFMT_TYPE_V32_FLOAT;
335 BEGIN_NV04(push, NV30_3D(VIEWPORT_TRANSLATE_X), 8);
336 PUSH_DATAf(push, 0.0);
337 PUSH_DATAf(push, 0.0);
338 PUSH_DATAf(push, 0.0);
339 PUSH_DATAf(push, 0.0);
340 PUSH_DATAf(push, 1.0);
341 PUSH_DATAf(push, 1.0);
342 PUSH_DATAf(push, 1.0);
343 PUSH_DATAf(push, 1.0);
344 BEGIN_NV04(push, NV30_3D(DEPTH_RANGE_NEAR), 2);
345 PUSH_DATAf(push, 0.0);
346 PUSH_DATAf(push, 1.0);
348 BEGIN_NV04(push, NV30_3D(VTXFMT(0)), 16);
349 PUSH_DATAp(push, r->vtxfmt, 16);
351 BEGIN_NV04(push, NV30_3D(VP_START_FROM_ID), 1);
352 PUSH_DATA (push, r->vertprog->start);
353 BEGIN_NV04(push, NV30_3D(ENGINE), 1);
354 PUSH_DATA (push, 0x00000103);
355 if (eng3d->oclass >= NV40_3D_CLASS) {
356 BEGIN_NV04(push, NV40_3D(VP_ATTRIB_EN), 2);
357 PUSH_DATA (push, vp_attribs);
358 PUSH_DATA (push, vp_results);
366 nv30_render_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
368 struct nv30_context *nv30 = nv30_context(pipe);
369 struct draw_context *draw = nv30->draw;
370 struct pipe_transfer *transfer[PIPE_MAX_ATTRIBS] = {NULL};
371 struct pipe_transfer *transferi = NULL;
374 nv30_render_validate(nv30);
376 if (nv30->draw_dirty & NV30_NEW_VIEWPORT)
377 draw_set_viewport_states(draw, 0, 1, &nv30->viewport);
378 if (nv30->draw_dirty & NV30_NEW_RASTERIZER)
379 draw_set_rasterizer_state(draw, &nv30->rast->pipe, NULL);
380 if (nv30->draw_dirty & NV30_NEW_CLIP)
381 draw_set_clip_state(draw, &nv30->clip);
382 if (nv30->draw_dirty & NV30_NEW_ARRAYS) {
383 draw_set_vertex_buffers(draw, 0, nv30->num_vtxbufs, nv30->vtxbuf);
384 draw_set_vertex_elements(draw, nv30->vertex->num_elements, nv30->vertex->pipe);
386 if (nv30->draw_dirty & NV30_NEW_FRAGPROG) {
387 struct nv30_fragprog *fp = nv30->fragprog.program;
389 fp->draw = draw_create_fragment_shader(draw, &fp->pipe);
390 draw_bind_fragment_shader(draw, fp->draw);
392 if (nv30->draw_dirty & NV30_NEW_VERTPROG) {
393 struct nv30_vertprog *vp = nv30->vertprog.program;
395 vp->draw = draw_create_vertex_shader(draw, &vp->pipe);
396 draw_bind_vertex_shader(draw, vp->draw);
398 if (nv30->draw_dirty & NV30_NEW_VERTCONST) {
399 if (nv30->vertprog.constbuf) {
400 void *map = nv04_resource(nv30->vertprog.constbuf)->data;
401 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0,
402 map, nv30->vertprog.constbuf_nr * 16);
404 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0, NULL, 0);
408 for (i = 0; i < nv30->num_vtxbufs; i++) {
409 const void *map = nv30->vtxbuf[i].user_buffer;
411 if (nv30->vtxbuf[i].buffer)
412 map = pipe_buffer_map(pipe, nv30->vtxbuf[i].buffer,
413 PIPE_TRANSFER_UNSYNCHRONIZED |
414 PIPE_TRANSFER_READ, &transfer[i]);
416 draw_set_mapped_vertex_buffer(draw, i, map, ~0);
420 const void *map = nv30->idxbuf.user_buffer;
422 map = pipe_buffer_map(pipe, nv30->idxbuf.buffer,
423 PIPE_TRANSFER_UNSYNCHRONIZED |
424 PIPE_TRANSFER_READ, &transferi);
425 draw_set_indexes(draw,
426 (ubyte *) map + nv30->idxbuf.offset,
427 nv30->idxbuf.index_size, ~0);
429 draw_set_indexes(draw, NULL, 0, 0);
432 draw_vbo(draw, info);
435 if (info->indexed && transferi)
436 pipe_buffer_unmap(pipe, transferi);
437 for (i = 0; i < nv30->num_vtxbufs; i++)
439 pipe_buffer_unmap(pipe, transfer[i]);
441 nv30->draw_dirty = 0;
442 nv30_state_release(nv30);
446 nv30_render_destroy(struct vbuf_render *render)
448 struct nv30_render *r = nv30_render(render);
451 pipe_buffer_unmap(&r->nv30->base.pipe, r->transfer);
452 pipe_resource_reference(&r->buffer, NULL);
453 nouveau_heap_free(&r->vertprog);
457 static struct vbuf_render *
458 nv30_render_create(struct nv30_context *nv30)
460 struct nv30_render *r = CALLOC_STRUCT(nv30_render);
465 r->offset = 1 * 1024 * 1024;
467 r->base.max_indices = 16 * 1024;
468 r->base.max_vertex_buffer_bytes = r->offset;
470 r->base.get_vertex_info = nv30_render_get_vertex_info;
471 r->base.allocate_vertices = nv30_render_allocate_vertices;
472 r->base.map_vertices = nv30_render_map_vertices;
473 r->base.unmap_vertices = nv30_render_unmap_vertices;
474 r->base.set_primitive = nv30_render_set_primitive;
475 r->base.draw_elements = nv30_render_draw_elements;
476 r->base.draw_arrays = nv30_render_draw_arrays;
477 r->base.release_vertices = nv30_render_release_vertices;
478 r->base.destroy = nv30_render_destroy;
483 nv30_draw_init(struct pipe_context *pipe)
485 struct nv30_context *nv30 = nv30_context(pipe);
486 struct vbuf_render *render;
487 struct draw_context *draw;
488 struct draw_stage *stage;
490 draw = draw_create(pipe);
494 render = nv30_render_create(nv30);
500 stage = draw_vbuf_stage(draw, render);
502 render->destroy(render);
507 draw_set_render(draw, render);
508 draw_set_rasterize_stage(draw, stage);
509 draw_wide_line_threshold(draw, 10000000.f);
510 draw_wide_point_threshold(draw, 10000000.f);
511 draw_wide_point_sprites(draw, TRUE);