r300_render.c revision dba7ad895333b9b0988239266a217edeebe6a3b3
1/*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23/* r300_render: Vertex and index buffer primitive emission. Contains both
24 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
25
26#include "draw/draw_context.h"
27#include "draw/draw_vbuf.h"
28
29#include "util/u_inlines.h"
30
31#include "util/u_format.h"
32#include "util/u_memory.h"
33#include "util/u_prim.h"
34
35#include "r300_cs.h"
36#include "r300_context.h"
37#include "r300_emit.h"
38#include "r300_reg.h"
39#include "r300_render.h"
40#include "r300_state_derived.h"
41
42/* r300_render: Vertex and index buffer primitive emission. */
43#define R300_MAX_VBO_SIZE  (1024 * 1024)
44
45/* XXX The DRM rejects VAP_ALT_NUM_VERTICES.. */
46//#define ENABLE_ALT_NUM_VERTS
47
48uint32_t r300_translate_primitive(unsigned prim)
49{
50    switch (prim) {
51        case PIPE_PRIM_POINTS:
52            return R300_VAP_VF_CNTL__PRIM_POINTS;
53        case PIPE_PRIM_LINES:
54            return R300_VAP_VF_CNTL__PRIM_LINES;
55        case PIPE_PRIM_LINE_LOOP:
56            return R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
57        case PIPE_PRIM_LINE_STRIP:
58            return R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
59        case PIPE_PRIM_TRIANGLES:
60            return R300_VAP_VF_CNTL__PRIM_TRIANGLES;
61        case PIPE_PRIM_TRIANGLE_STRIP:
62            return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
63        case PIPE_PRIM_TRIANGLE_FAN:
64            return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
65        case PIPE_PRIM_QUADS:
66            return R300_VAP_VF_CNTL__PRIM_QUADS;
67        case PIPE_PRIM_QUAD_STRIP:
68            return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
69        case PIPE_PRIM_POLYGON:
70            return R300_VAP_VF_CNTL__PRIM_POLYGON;
71        default:
72            return 0;
73    }
74}
75
76static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
77                                            unsigned mode)
78{
79    struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
80    uint32_t color_control = rs->color_control;
81
82    /* By default (see r300_state.c:r300_create_rs_state) color_control is
83     * initialized to provoking the first vertex.
84     *
85     * Triangle fans must be reduced to the second vertex, not the first, in
86     * Gallium flatshade-first mode, as per the GL spec.
87     * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
88     *
89     * Quads never provoke correctly in flatshade-first mode. The first
90     * vertex is never considered as provoking, so only the second, third,
91     * and fourth vertices can be selected, and both "third" and "last" modes
92     * select the fourth vertex. This is probably due to D3D lacking quads.
93     *
94     * Similarly, polygons reduce to the first, not the last, vertex, when in
95     * "last" mode, and all other modes start from the second vertex.
96     *
97     * ~ C.
98     */
99
100    if (rs->rs.flatshade_first) {
101        switch (mode) {
102            case PIPE_PRIM_TRIANGLE_FAN:
103                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
104                break;
105            case PIPE_PRIM_QUADS:
106            case PIPE_PRIM_QUAD_STRIP:
107            case PIPE_PRIM_POLYGON:
108                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
109                break;
110            default:
111                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
112                break;
113        }
114    } else {
115        color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
116    }
117
118    return color_control;
119}
120
121/* Check if the requested number of dwords is available in the CS and
122 * if not, flush. Return TRUE if the flush occured. */
123static boolean r300_reserve_cs_space(struct r300_context *r300,
124                                     unsigned dwords)
125{
126    if (!r300->winsys->check_cs(r300->winsys, dwords)) {
127        r300->context.flush(&r300->context, 0, NULL);
128        return TRUE;
129    }
130    return FALSE;
131}
132
133static boolean immd_is_good_idea(struct r300_context *r300,
134                                      unsigned count)
135{
136    return count <= 4;
137}
138
139static void r300_emit_draw_arrays_immediate(struct r300_context *r300,
140                                            unsigned mode,
141                                            unsigned start,
142                                            unsigned count)
143{
144    struct pipe_vertex_element* velem;
145    struct pipe_vertex_buffer* vbuf;
146    unsigned vertex_element_count = r300->vertex_element_count;
147    unsigned i, v, vbi, dw, elem_offset, dwords;
148
149    /* Size of the vertex, in dwords. */
150    unsigned vertex_size = 0;
151
152    /* Offsets of the attribute, in dwords, from the start of the vertex. */
153    unsigned offset[PIPE_MAX_ATTRIBS];
154
155    /* Size of the vertex element, in dwords. */
156    unsigned size[PIPE_MAX_ATTRIBS];
157
158    /* Stride to the same attrib in the next vertex in the vertex buffer,
159     * in dwords. */
160    unsigned stride[PIPE_MAX_ATTRIBS] = {0};
161
162    /* Mapped vertex buffers. */
163    uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
164
165    CS_LOCALS(r300);
166
167    /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
168    for (i = 0; i < vertex_element_count; i++) {
169        velem = &r300->vertex_element[i];
170        offset[i] = velem->src_offset / 4;
171        size[i] = util_format_get_blocksize(velem->src_format) / 4;
172        vertex_size += size[i];
173        vbi = velem->vertex_buffer_index;
174
175        /* Map the buffer. */
176        if (!map[vbi]) {
177            vbuf = &r300->vertex_buffer[vbi];
178            map[vbi] = (uint32_t*)pipe_buffer_map(r300->context.screen,
179                                                  vbuf->buffer,
180                                                  PIPE_BUFFER_USAGE_CPU_READ);
181            map[vbi] += vbuf->buffer_offset / 4;
182            stride[vbi] = vbuf->stride / 4;
183        }
184    }
185
186    dwords = 10 + count * vertex_size;
187
188    r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + dwords);
189    r300_emit_dirty_state(r300);
190
191    BEGIN_CS(dwords);
192    OUT_CS_REG(R300_GA_COLOR_CONTROL,
193            r300_provoking_vertex_fixes(r300, mode));
194    OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
195    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
196    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
197    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size);
198    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) |
199            r300_translate_primitive(mode));
200
201    /* Emit vertices. */
202    for (v = 0; v < count; v++) {
203        for (i = 0; i < vertex_element_count; i++) {
204            velem = &r300->vertex_element[i];
205            vbi = velem->vertex_buffer_index;
206            elem_offset = offset[i] + stride[vbi] * (v + start);
207
208            for (dw = 0; dw < size[i]; dw++) {
209                OUT_CS(map[vbi][elem_offset + dw]);
210            }
211        }
212    }
213    END_CS;
214
215    /* Unmap buffers. */
216    for (i = 0; i < vertex_element_count; i++) {
217        vbi = r300->vertex_element[i].vertex_buffer_index;
218
219        if (map[vbi]) {
220            vbuf = &r300->vertex_buffer[vbi];
221            pipe_buffer_unmap(r300->context.screen, vbuf->buffer);
222            map[vbi] = NULL;
223        }
224    }
225}
226
227static void r300_emit_draw_arrays(struct r300_context *r300,
228                                  unsigned mode,
229                                  unsigned count)
230{
231#if defined(ENABLE_ALT_NUM_VERTS)
232    boolean alt_num_verts = count > 65535;
233#else
234    boolean alt_num_verts = FALSE;
235#endif
236    CS_LOCALS(r300);
237
238    if (alt_num_verts) {
239        assert(count < (1 << 24));
240        BEGIN_CS(10);
241        OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
242    } else {
243        BEGIN_CS(8);
244    }
245    OUT_CS_REG(R300_GA_COLOR_CONTROL,
246            r300_provoking_vertex_fixes(r300, mode));
247    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
248    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
249    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
250    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
251           r300_translate_primitive(mode) |
252           (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
253    END_CS;
254}
255
256static void r300_emit_draw_elements(struct r300_context *r300,
257                                    struct pipe_buffer* indexBuffer,
258                                    unsigned indexSize,
259                                    unsigned minIndex,
260                                    unsigned maxIndex,
261                                    unsigned mode,
262                                    unsigned start,
263                                    unsigned count)
264{
265    uint32_t count_dwords;
266    uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
267#if defined(ENABLE_ALT_NUM_VERTS)
268    boolean alt_num_verts = count > 65535;
269#else
270    boolean alt_num_verts = FALSE;
271#endif
272    CS_LOCALS(r300);
273
274    assert((start * indexSize)  % 4 == 0);
275
276    if (alt_num_verts) {
277        assert(count < (1 << 24));
278        BEGIN_CS(16);
279        OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
280    } else {
281        BEGIN_CS(14);
282    }
283    OUT_CS_REG(R300_GA_COLOR_CONTROL,
284            r300_provoking_vertex_fixes(r300, mode));
285    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, minIndex);
286    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex);
287    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
288    if (indexSize == 4) {
289        count_dwords = count;
290        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
291               R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
292               r300_translate_primitive(mode) |
293               (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
294    } else {
295        count_dwords = (count + 1) / 2;
296        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
297               r300_translate_primitive(mode) |
298               (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
299    }
300
301    /* INDX_BUFFER is a truly special packet3.
302     * Unlike most other packet3, where the offset is after the count,
303     * the order is reversed, so the relocation ends up carrying the
304     * size of the indexbuf instead of the offset.
305     */
306    OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
307    OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
308           (0 << R300_INDX_BUFFER_SKIP_SHIFT));
309    OUT_CS(offset_dwords << 2);
310    OUT_CS_RELOC(indexBuffer, count_dwords,
311        RADEON_GEM_DOMAIN_GTT, 0, 0);
312
313    END_CS;
314}
315
316static boolean r300_setup_vertex_buffers(struct r300_context *r300)
317{
318    struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
319    struct pipe_vertex_element *velem = r300->vertex_element;
320    struct pipe_buffer *pbuf;
321
322validate:
323    for (int i = 0; i < r300->vertex_element_count; i++) {
324        pbuf = vbuf[velem[i].vertex_buffer_index].buffer;
325
326        if (!r300->winsys->add_buffer(r300->winsys, pbuf,
327                                      RADEON_GEM_DOMAIN_GTT, 0)) {
328            r300->context.flush(&r300->context, 0, NULL);
329            goto validate;
330        }
331    }
332
333    if (!r300->winsys->validate(r300->winsys)) {
334        r300->context.flush(&r300->context, 0, NULL);
335        return r300->winsys->validate(r300->winsys);
336    }
337
338    return TRUE;
339}
340
341static void r300_shorten_ubyte_elts(struct r300_context* r300,
342                                    struct pipe_buffer** elts,
343                                    unsigned count)
344{
345    struct pipe_screen* screen = r300->context.screen;
346    struct pipe_buffer* new_elts;
347    unsigned char *in_map;
348    unsigned short *out_map;
349    unsigned i;
350
351    new_elts = screen->buffer_create(screen, 32,
352                                     PIPE_BUFFER_USAGE_INDEX |
353                                     PIPE_BUFFER_USAGE_CPU_WRITE |
354                                     PIPE_BUFFER_USAGE_GPU_READ,
355                                     2 * count);
356
357    in_map = pipe_buffer_map(screen, *elts, PIPE_BUFFER_USAGE_CPU_READ);
358    out_map = pipe_buffer_map(screen, new_elts, PIPE_BUFFER_USAGE_CPU_WRITE);
359
360    for (i = 0; i < count; i++) {
361        *out_map = (unsigned short)*in_map;
362        in_map++;
363        out_map++;
364    }
365
366    pipe_buffer_unmap(screen, *elts);
367    pipe_buffer_unmap(screen, new_elts);
368
369    *elts = new_elts;
370}
371
372/* This is the fast-path drawing & emission for HW TCL. */
373void r300_draw_range_elements(struct pipe_context* pipe,
374                              struct pipe_buffer* indexBuffer,
375                              unsigned indexSize,
376                              unsigned minIndex,
377                              unsigned maxIndex,
378                              unsigned mode,
379                              unsigned start,
380                              unsigned count)
381{
382    struct r300_context* r300 = r300_context(pipe);
383    struct pipe_buffer* orgIndexBuffer = indexBuffer;
384#if defined(ENABLE_ALT_NUM_VERTS)
385    boolean alt_num_verts = r300_screen(pipe->screen)->caps->is_r500 &&
386                            count > 65536;
387#else
388    boolean alt_num_verts = FALSE;
389#endif
390    unsigned short_count;
391
392    if (!u_trim_pipe_prim(mode, &count)) {
393        return;
394    }
395
396    r300_update_derived_state(r300);
397
398    r300_emit_buffer_validate(r300);
399
400    if (!r300_setup_vertex_buffers(r300)) {
401        return;
402    }
403
404    if (indexSize == 1) {
405        r300_shorten_ubyte_elts(r300, &indexBuffer, count);
406        indexSize = 2;
407    }
408
409    if (!r300->winsys->add_buffer(r300->winsys, indexBuffer,
410                                  RADEON_GEM_DOMAIN_GTT, 0)) {
411        goto cleanup;
412    }
413
414    if (!r300->winsys->validate(r300->winsys)) {
415        goto cleanup;
416    }
417
418    /* 128 dwords for emit_aos and emit_draw_elements */
419    r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 128);
420    r300_emit_dirty_state(r300);
421    r300_emit_aos(r300, 0);
422
423    if (alt_num_verts || count <= 65535) {
424        r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
425                                maxIndex, mode, start, count);
426    } else {
427        do {
428            short_count = MIN2(count, 65534);
429            r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
430                                    maxIndex, mode, start, short_count);
431
432            start += short_count;
433            count -= short_count;
434
435            /* 16 spare dwords are enough for emit_draw_elements. */
436            if (count && r300_reserve_cs_space(r300, 16)) {
437                r300_emit_dirty_state(r300);
438                r300_emit_aos(r300, 0);
439            }
440        } while (count);
441    }
442
443cleanup:
444    if (indexBuffer != orgIndexBuffer) {
445        pipe->screen->buffer_destroy(indexBuffer);
446    }
447}
448
449/* Simple helpers for context setup. Should probably be moved to util. */
450void r300_draw_elements(struct pipe_context* pipe,
451                        struct pipe_buffer* indexBuffer,
452                        unsigned indexSize, unsigned mode,
453                        unsigned start, unsigned count)
454{
455   pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0, ~0,
456                             mode, start, count);
457}
458
459void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
460                      unsigned start, unsigned count)
461{
462    struct r300_context* r300 = r300_context(pipe);
463#if defined(ENABLE_ALT_NUM_VERTS)
464    boolean alt_num_verts = r300_screen(pipe->screen)->caps->is_r500 &&
465                            count > 65536;
466#else
467    boolean alt_num_verts = FALSE;
468#endif
469    unsigned short_count;
470
471    if (!u_trim_pipe_prim(mode, &count)) {
472        return;
473    }
474
475    r300_update_derived_state(r300);
476
477    r300_emit_buffer_validate(r300);
478
479    if (immd_is_good_idea(r300, count)) {
480        r300_emit_draw_arrays_immediate(r300, mode, start, count);
481    } else {
482        if (!r300_setup_vertex_buffers(r300)) {
483            return;
484        }
485
486        /* Make sure there are at least 128 spare dwords in the command buffer.
487         * (most of it being consumed by emit_aos) */
488        r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 128);
489        r300_emit_dirty_state(r300);
490
491        if (alt_num_verts || count <= 65535) {
492            r300_emit_aos(r300, start);
493            r300_emit_draw_arrays(r300, mode, count);
494        } else {
495            do {
496                short_count = MIN2(count, 65535);
497                r300_emit_aos(r300, start);
498                r300_emit_draw_arrays(r300, mode, short_count);
499
500                start += short_count;
501                count -= short_count;
502
503                /* Again, we emit both AOS and draw_arrays so there should be
504                 * at least 128 spare dwords. */
505                if (count && r300_reserve_cs_space(r300, 128)) {
506                    r300_emit_dirty_state(r300);
507                }
508            } while (count);
509        }
510    }
511}
512
513/****************************************************************************
514 * The rest of this file is for SW TCL rendering only. Please be polite and *
515 * keep these functions separated so that they are easier to locate. ~C.    *
516 ***************************************************************************/
517
518/* SW TCL arrays, using Draw. */
519void r300_swtcl_draw_arrays(struct pipe_context* pipe,
520                               unsigned mode,
521                               unsigned start,
522                               unsigned count)
523{
524    struct r300_context* r300 = r300_context(pipe);
525    int i;
526
527    if (!u_trim_pipe_prim(mode, &count)) {
528        return;
529    }
530
531    for (i = 0; i < r300->vertex_buffer_count; i++) {
532        void* buf = pipe_buffer_map(pipe->screen,
533                                    r300->vertex_buffer[i].buffer,
534                                    PIPE_BUFFER_USAGE_CPU_READ);
535        draw_set_mapped_vertex_buffer(r300->draw, i, buf);
536    }
537
538    draw_set_mapped_element_buffer(r300->draw, 0, NULL);
539
540    draw_set_mapped_constant_buffer(r300->draw,
541				    PIPE_SHADER_VERTEX,
542                                    0,
543				    r300->shader_constants[PIPE_SHADER_VERTEX].constants,
544				    r300->shader_constants[PIPE_SHADER_VERTEX].count *
545                (sizeof(float) * 4));
546
547    draw_arrays(r300->draw, mode, start, count);
548
549    for (i = 0; i < r300->vertex_buffer_count; i++) {
550        pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
551        draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
552    }
553}
554
555/* SW TCL elements, using Draw. */
556void r300_swtcl_draw_range_elements(struct pipe_context* pipe,
557                                       struct pipe_buffer* indexBuffer,
558                                       unsigned indexSize,
559                                       unsigned minIndex,
560                                       unsigned maxIndex,
561                                       unsigned mode,
562                                       unsigned start,
563                                       unsigned count)
564{
565    struct r300_context* r300 = r300_context(pipe);
566    int i;
567    void* indices;
568
569    if (!u_trim_pipe_prim(mode, &count)) {
570        return;
571    }
572
573    for (i = 0; i < r300->vertex_buffer_count; i++) {
574        void* buf = pipe_buffer_map(pipe->screen,
575                                    r300->vertex_buffer[i].buffer,
576                                    PIPE_BUFFER_USAGE_CPU_READ);
577        draw_set_mapped_vertex_buffer(r300->draw, i, buf);
578    }
579
580    indices = pipe_buffer_map(pipe->screen, indexBuffer,
581                              PIPE_BUFFER_USAGE_CPU_READ);
582    draw_set_mapped_element_buffer_range(r300->draw, indexSize,
583                                         minIndex, maxIndex, indices);
584
585    draw_set_mapped_constant_buffer(r300->draw,
586				    PIPE_SHADER_VERTEX,
587                                    0,
588            r300->shader_constants[PIPE_SHADER_VERTEX].constants,
589            r300->shader_constants[PIPE_SHADER_VERTEX].count *
590                (sizeof(float) * 4));
591
592    draw_arrays(r300->draw, mode, start, count);
593
594    for (i = 0; i < r300->vertex_buffer_count; i++) {
595        pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
596        draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
597    }
598
599    pipe_buffer_unmap(pipe->screen, indexBuffer);
600    draw_set_mapped_element_buffer_range(r300->draw, 0, start,
601                                         start + count - 1, NULL);
602}
603
604/* Object for rendering using Draw. */
605struct r300_render {
606    /* Parent class */
607    struct vbuf_render base;
608
609    /* Pipe context */
610    struct r300_context* r300;
611
612    /* Vertex information */
613    size_t vertex_size;
614    unsigned prim;
615    unsigned hwprim;
616
617    /* VBO */
618    struct pipe_buffer* vbo;
619    size_t vbo_size;
620    size_t vbo_offset;
621    size_t vbo_max_used;
622    void * vbo_ptr;
623};
624
625static INLINE struct r300_render*
626r300_render(struct vbuf_render* render)
627{
628    return (struct r300_render*)render;
629}
630
631static const struct vertex_info*
632r300_render_get_vertex_info(struct vbuf_render* render)
633{
634    struct r300_render* r300render = r300_render(render);
635    struct r300_context* r300 = r300render->r300;
636
637    r300_update_derived_state(r300);
638
639    return (struct vertex_info*)r300->vertex_format_state.state;
640}
641
642static boolean r300_render_allocate_vertices(struct vbuf_render* render,
643                                                   ushort vertex_size,
644                                                   ushort count)
645{
646    struct r300_render* r300render = r300_render(render);
647    struct r300_context* r300 = r300render->r300;
648    struct pipe_screen* screen = r300->context.screen;
649    size_t size = (size_t)vertex_size * (size_t)count;
650
651    if (size + r300render->vbo_offset > r300render->vbo_size)
652    {
653        pipe_buffer_reference(&r300->vbo, NULL);
654        r300render->vbo = pipe_buffer_create(screen,
655                                             64,
656                                             PIPE_BUFFER_USAGE_VERTEX,
657                                             R300_MAX_VBO_SIZE);
658        r300render->vbo_offset = 0;
659        r300render->vbo_size = R300_MAX_VBO_SIZE;
660    }
661
662    r300render->vertex_size = vertex_size;
663    r300->vbo = r300render->vbo;
664    r300->vbo_offset = r300render->vbo_offset;
665
666    return (r300render->vbo) ? TRUE : FALSE;
667}
668
669static void* r300_render_map_vertices(struct vbuf_render* render)
670{
671    struct r300_render* r300render = r300_render(render);
672    struct pipe_screen* screen = r300render->r300->context.screen;
673
674    r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
675                                          PIPE_BUFFER_USAGE_CPU_WRITE);
676
677    return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset);
678}
679
680static void r300_render_unmap_vertices(struct vbuf_render* render,
681                                             ushort min,
682                                             ushort max)
683{
684    struct r300_render* r300render = r300_render(render);
685    struct pipe_screen* screen = r300render->r300->context.screen;
686    CS_LOCALS(r300render->r300);
687    BEGIN_CS(2);
688    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
689    END_CS;
690
691    r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
692                                    r300render->vertex_size * (max + 1));
693    pipe_buffer_unmap(screen, r300render->vbo);
694}
695
696static void r300_render_release_vertices(struct vbuf_render* render)
697{
698    struct r300_render* r300render = r300_render(render);
699
700    r300render->vbo_offset += r300render->vbo_max_used;
701    r300render->vbo_max_used = 0;
702}
703
704static boolean r300_render_set_primitive(struct vbuf_render* render,
705                                               unsigned prim)
706{
707    struct r300_render* r300render = r300_render(render);
708
709    r300render->prim = prim;
710    r300render->hwprim = r300_translate_primitive(prim);
711
712    return TRUE;
713}
714
715static void r300_render_draw_arrays(struct vbuf_render* render,
716                                          unsigned start,
717                                          unsigned count)
718{
719    struct r300_render* r300render = r300_render(render);
720    struct r300_context* r300 = r300render->r300;
721
722    CS_LOCALS(r300);
723
724    r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 2);
725    r300_emit_dirty_state(r300);
726
727    DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
728
729    BEGIN_CS(2);
730    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
731    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
732           r300render->hwprim);
733    END_CS;
734}
735
736static void r300_render_draw(struct vbuf_render* render,
737                                   const ushort* indices,
738                                   uint count)
739{
740    struct r300_render* r300render = r300_render(render);
741    struct r300_context* r300 = r300render->r300;
742    int i;
743    unsigned dwords = 2 + (count+1)/2;
744
745    CS_LOCALS(r300);
746
747    r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + dwords);
748    r300_emit_dirty_state(r300);
749
750    BEGIN_CS(dwords);
751    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
752    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
753           r300render->hwprim);
754    for (i = 0; i < count-1; i += 2) {
755        OUT_CS(indices[i+1] << 16 | indices[i]);
756    }
757    if (count % 2) {
758        OUT_CS(indices[count-1]);
759    }
760    END_CS;
761}
762
763static void r300_render_destroy(struct vbuf_render* render)
764{
765    FREE(render);
766}
767
768static struct vbuf_render* r300_render_create(struct r300_context* r300)
769{
770    struct r300_render* r300render = CALLOC_STRUCT(r300_render);
771
772    r300render->r300 = r300;
773
774    /* XXX find real numbers plz */
775    r300render->base.max_vertex_buffer_bytes = 128 * 1024;
776    r300render->base.max_indices = 16 * 1024;
777
778    r300render->base.get_vertex_info = r300_render_get_vertex_info;
779    r300render->base.allocate_vertices = r300_render_allocate_vertices;
780    r300render->base.map_vertices = r300_render_map_vertices;
781    r300render->base.unmap_vertices = r300_render_unmap_vertices;
782    r300render->base.set_primitive = r300_render_set_primitive;
783    r300render->base.draw = r300_render_draw;
784    r300render->base.draw_arrays = r300_render_draw_arrays;
785    r300render->base.release_vertices = r300_render_release_vertices;
786    r300render->base.destroy = r300_render_destroy;
787
788    r300render->vbo = NULL;
789    r300render->vbo_size = 0;
790    r300render->vbo_offset = 0;
791
792    return &r300render->base;
793}
794
795struct draw_stage* r300_draw_stage(struct r300_context* r300)
796{
797    struct vbuf_render* render;
798    struct draw_stage* stage;
799
800    render = r300_render_create(r300);
801
802    if (!render) {
803        return NULL;
804    }
805
806    stage = draw_vbuf_stage(r300->draw, render);
807
808    if (!stage) {
809        render->destroy(render);
810        return NULL;
811    }
812
813    draw_set_render(r300->draw, render);
814
815    return stage;
816}
817