r300_render.c revision 60628c65c902f68c600b3d79c06e928aa3286285
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 "indices/u_indices.h"
30
31#include "pipe/p_inlines.h"
32
33#include "util/u_memory.h"
34#include "util/u_prim.h"
35
36#include "r300_cs.h"
37#include "r300_context.h"
38#include "r300_emit.h"
39#include "r300_reg.h"
40#include "r300_render.h"
41#include "r300_state_derived.h"
42
43/* r300_render: Vertex and index buffer primitive emission. */
44#define R300_MAX_VBO_SIZE  (1024 * 1024)
45
46uint32_t r300_translate_primitive(unsigned prim)
47{
48    switch (prim) {
49        case PIPE_PRIM_POINTS:
50            return R300_VAP_VF_CNTL__PRIM_POINTS;
51        case PIPE_PRIM_LINES:
52            return R300_VAP_VF_CNTL__PRIM_LINES;
53        case PIPE_PRIM_LINE_LOOP:
54            return R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
55        case PIPE_PRIM_LINE_STRIP:
56            return R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
57        case PIPE_PRIM_TRIANGLES:
58            return R300_VAP_VF_CNTL__PRIM_TRIANGLES;
59        case PIPE_PRIM_TRIANGLE_STRIP:
60            return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
61        case PIPE_PRIM_TRIANGLE_FAN:
62            return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
63        case PIPE_PRIM_QUADS:
64            return R300_VAP_VF_CNTL__PRIM_QUADS;
65        case PIPE_PRIM_QUAD_STRIP:
66            return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
67        case PIPE_PRIM_POLYGON:
68            return R300_VAP_VF_CNTL__PRIM_POLYGON;
69        default:
70            return 0;
71    }
72}
73
74static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
75                                            unsigned mode)
76{
77    struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
78    uint32_t color_control = rs->color_control;
79
80    /* By default (see r300_state.c:r300_create_rs_state) color_control is
81     * initialized to provoking the first vertex.
82     *
83     * Triangle fans must be reduced to the second vertex, not the first, in
84     * Gallium flatshade-first mode, as per the GL spec.
85     * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
86     *
87     * Quads never provoke correctly in flatshade-first mode. The first
88     * vertex is never considered as provoking, so only the second, third,
89     * and fourth vertices can be selected, and both "third" and "last" modes
90     * select the fourth vertex. This is probably due to D3D lacking quads.
91     *
92     * Similarly, polygons reduce to the first, not the last, vertex, when in
93     * "last" mode, and all other modes start from the second vertex.
94     *
95     * ~ C.
96     */
97
98    if (rs->rs.flatshade_first) {
99        switch (mode) {
100            case PIPE_PRIM_TRIANGLE_FAN:
101                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
102                break;
103            case PIPE_PRIM_QUADS:
104            case PIPE_PRIM_QUAD_STRIP:
105            case PIPE_PRIM_POLYGON:
106                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
107                break;
108            default:
109                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
110                break;
111        }
112    } else {
113        color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
114    }
115
116    return color_control;
117}
118
119static void r300_emit_draw_immediate(struct r300_context *r300,
120                                     unsigned mode,
121                                     unsigned start,
122                                     unsigned count)
123{
124    struct pipe_buffer* vbo = r300->vertex_buffer[0].buffer;
125    unsigned vertex_size = r300->vertex_buffer[0].stride / sizeof(float);
126    unsigned i;
127    uint32_t* map;
128    CS_LOCALS(r300);
129
130    map = (uint32_t*)pipe_buffer_map_range(r300->context.screen, vbo,
131            start * vertex_size, count * vertex_size,
132            PIPE_BUFFER_USAGE_CPU_READ);
133
134    BEGIN_CS(10 + count * vertex_size);
135    OUT_CS_REG(R300_GA_COLOR_CONTROL,
136            r300_provoking_vertex_fixes(r300, mode));
137    OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
138    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
139    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
140    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size);
141    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) |
142            r300_translate_primitive(mode));
143    //debug_printf("r300: Immd %d verts, %d attrs\n", count, vertex_size);
144    for (i = 0; i < count * vertex_size; i++) {
145        if (i % vertex_size == 0) {
146            //debug_printf("r300: -- vert --\n");
147        }
148        //debug_printf("r300: 0x%08x\n", *map);
149        OUT_CS(*map);
150        map++;
151    }
152    END_CS;
153
154    pipe_buffer_unmap(r300->context.screen, vbo);
155}
156
157static void r300_emit_draw_arrays(struct r300_context *r300,
158                                  unsigned mode,
159                                  unsigned count)
160{
161    CS_LOCALS(r300);
162
163    BEGIN_CS(8);
164    OUT_CS_REG(R300_GA_COLOR_CONTROL,
165            r300_provoking_vertex_fixes(r300, mode));
166    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
167    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
168    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
169    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
170           r300_translate_primitive(mode));
171    END_CS;
172}
173
174static void r300_emit_draw_elements(struct r300_context *r300,
175                                    struct pipe_buffer* indexBuffer,
176                                    unsigned indexSize,
177                                    unsigned minIndex,
178                                    unsigned maxIndex,
179                                    unsigned mode,
180                                    unsigned start,
181                                    unsigned count)
182{
183    uint32_t count_dwords;
184    uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
185    CS_LOCALS(r300);
186
187    /* XXX most of these are stupid */
188    assert(indexSize == 4 || indexSize == 2);
189    assert((start * indexSize)  % 4 == 0);
190    assert(offset_dwords == 0);
191
192    BEGIN_CS(14);
193    OUT_CS_REG(R300_GA_COLOR_CONTROL,
194            r300_provoking_vertex_fixes(r300, mode));
195    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, minIndex);
196    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex);
197    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
198    if (indexSize == 4) {
199        count_dwords = count + start;
200        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
201               R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
202               r300_translate_primitive(mode));
203    } else {
204        count_dwords = (count + start + 1) / 2;
205        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
206               r300_translate_primitive(mode));
207    }
208
209    /* INDX_BUFFER is a truly special packet3.
210     * Unlike most other packet3, where the offset is after the count,
211     * the order is reversed, so the relocation ends up carrying the
212     * size of the indexbuf instead of the offset.
213     *
214     * XXX Fix offset
215     */
216    OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
217    OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
218           (0 << R300_INDX_BUFFER_SKIP_SHIFT));
219    OUT_CS(offset_dwords);
220    OUT_CS_RELOC(indexBuffer, count_dwords,
221        RADEON_GEM_DOMAIN_GTT, 0, 0);
222
223    END_CS;
224}
225
226
227static boolean r300_setup_vertex_buffers(struct r300_context *r300)
228{
229    struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
230    struct pipe_vertex_element *velem = r300->vertex_element;
231
232validate:
233    for (int i = 0; i < r300->vertex_element_count; i++) {
234        if (!r300->winsys->add_buffer(r300->winsys,
235                vbuf[velem[i].vertex_buffer_index].buffer,
236            RADEON_GEM_DOMAIN_GTT, 0)) {
237            r300->context.flush(&r300->context, 0, NULL);
238            goto validate;
239        }
240    }
241
242    if (!r300->winsys->validate(r300->winsys)) {
243        r300->context.flush(&r300->context, 0, NULL);
244        return r300->winsys->validate(r300->winsys);
245    }
246
247    return TRUE;
248}
249
250static struct pipe_buffer* r300_translate_elts(struct r300_context* r300,
251                                               struct pipe_buffer* elts,
252                                               unsigned* size,
253                                               unsigned* mode,
254                                               unsigned* count)
255{
256    struct pipe_screen* screen = r300->context.screen;
257    struct pipe_buffer* new_elts;
258    void *in_map, *out_map;
259    unsigned out_prim, out_index_size, out_nr;
260    u_translate_func out_translate;
261
262    (void)u_index_translator(~0, *mode, *size, *count, PV_LAST, PV_LAST,
263        &out_prim, &out_index_size, &out_nr, &out_translate);
264
265    debug_printf("r300: old mode %d, new mode %d\n", *mode, out_prim);
266    debug_printf("r300: old count %d, new count %d\n", *count, out_nr);
267    debug_printf("r300: old size %d, new size %d\n", *size, out_index_size);
268
269    new_elts = screen->buffer_create(screen, 32,
270                                     PIPE_BUFFER_USAGE_INDEX |
271                                     PIPE_BUFFER_USAGE_CPU_WRITE |
272                                     PIPE_BUFFER_USAGE_GPU_READ,
273                                     out_index_size * out_nr);
274
275    in_map = pipe_buffer_map(screen, elts, PIPE_BUFFER_USAGE_CPU_READ);
276    out_map = pipe_buffer_map(screen, new_elts, PIPE_BUFFER_USAGE_CPU_WRITE);
277
278    out_translate(in_map, *count, out_map);
279
280    pipe_buffer_unmap(screen, elts);
281    pipe_buffer_unmap(screen, new_elts);
282
283    *size = out_index_size;
284    *mode = out_prim;
285    *count = out_nr;
286
287    return new_elts;
288}
289
290/* This is the fast-path drawing & emission for HW TCL. */
291void r300_draw_range_elements(struct pipe_context* pipe,
292                              struct pipe_buffer* indexBuffer,
293                              unsigned indexSize,
294                              unsigned minIndex,
295                              unsigned maxIndex,
296                              unsigned mode,
297                              unsigned start,
298                              unsigned count)
299{
300    struct r300_context* r300 = r300_context(pipe);
301
302    if (!u_trim_pipe_prim(mode, &count)) {
303        return;
304    }
305
306    if (count > 65535) {
307       /* XXX: use aux/indices functions to split this into smaller
308        * primitives.
309        */
310        return;
311    }
312
313    r300_update_derived_state(r300);
314
315    if (!r300_setup_vertex_buffers(r300)) {
316        return;
317    }
318
319	struct pipe_buffer* orgIndexBuffer = indexBuffer;
320    if (indexSize == 1) {
321        indexBuffer = r300_translate_elts(r300, indexBuffer,
322            &indexSize, &mode, &count);
323    }
324
325    if (!r300->winsys->add_buffer(r300->winsys, indexBuffer,
326                                  RADEON_GEM_DOMAIN_GTT, 0)) {
327        goto cleanup;
328    }
329
330    if (!r300->winsys->validate(r300->winsys)) {
331        goto cleanup;
332    }
333
334    r300_emit_dirty_state(r300);
335
336    r300_emit_aos(r300, 0);
337
338    r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex, maxIndex,
339                            mode, start, count);
340
341cleanup:
342    if (indexBuffer != orgIndexBuffer) {
343        pipe->screen->buffer_destroy(indexBuffer);
344    }
345}
346
347/* Simple helpers for context setup. Should probably be moved to util. */
348void r300_draw_elements(struct pipe_context* pipe,
349                        struct pipe_buffer* indexBuffer,
350                        unsigned indexSize, unsigned mode,
351                        unsigned start, unsigned count)
352{
353   pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0, ~0,
354                             mode, start, count);
355}
356
357void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
358                      unsigned start, unsigned count)
359{
360    struct r300_context* r300 = r300_context(pipe);
361
362    if (!u_trim_pipe_prim(mode, &count)) {
363        return;
364    }
365
366    if (count > 65535) {
367        /* XXX: driver needs to handle this -- use the functions in
368         * aux/indices to split this into several smaller primitives.
369         */
370        return;
371    }
372
373    r300_update_derived_state(r300);
374
375    if (!r300_setup_vertex_buffers(r300)) {
376        return;
377    }
378
379    r300_emit_dirty_state(r300);
380
381    if (FALSE && count <= 4 && r300->vertex_buffer_count == 1) {
382        r300_emit_draw_immediate(r300, mode, start, count);
383    } else {
384        r300_emit_aos(r300, start);
385        r300_emit_draw_arrays(r300, mode, count);
386    }
387}
388
389/****************************************************************************
390 * The rest of this file is for SW TCL rendering only. Please be polite and *
391 * keep these functions separated so that they are easier to locate. ~C.    *
392 ***************************************************************************/
393
394/* SW TCL arrays, using Draw. */
395void r300_swtcl_draw_arrays(struct pipe_context* pipe,
396                               unsigned mode,
397                               unsigned start,
398                               unsigned count)
399{
400    struct r300_context* r300 = r300_context(pipe);
401    int i;
402
403    if (!u_trim_pipe_prim(mode, &count)) {
404        return;
405    }
406
407    for (i = 0; i < r300->vertex_buffer_count; i++) {
408        void* buf = pipe_buffer_map(pipe->screen,
409                                    r300->vertex_buffer[i].buffer,
410                                    PIPE_BUFFER_USAGE_CPU_READ);
411        draw_set_mapped_vertex_buffer(r300->draw, i, buf);
412    }
413
414    draw_set_mapped_element_buffer(r300->draw, 0, NULL);
415
416    draw_set_mapped_constant_buffer(r300->draw,
417				    PIPE_SHADER_VERTEX,
418				    r300->shader_constants[PIPE_SHADER_VERTEX].constants,
419				    r300->shader_constants[PIPE_SHADER_VERTEX].count *
420                (sizeof(float) * 4));
421
422    draw_arrays(r300->draw, mode, start, count);
423
424    for (i = 0; i < r300->vertex_buffer_count; i++) {
425        pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
426        draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
427    }
428}
429
430/* SW TCL elements, using Draw. */
431void r300_swtcl_draw_range_elements(struct pipe_context* pipe,
432                                       struct pipe_buffer* indexBuffer,
433                                       unsigned indexSize,
434                                       unsigned minIndex,
435                                       unsigned maxIndex,
436                                       unsigned mode,
437                                       unsigned start,
438                                       unsigned count)
439{
440    struct r300_context* r300 = r300_context(pipe);
441    int i;
442    void* indices;
443
444    if (!u_trim_pipe_prim(mode, &count)) {
445        return;
446    }
447
448    for (i = 0; i < r300->vertex_buffer_count; i++) {
449        void* buf = pipe_buffer_map(pipe->screen,
450                                    r300->vertex_buffer[i].buffer,
451                                    PIPE_BUFFER_USAGE_CPU_READ);
452        draw_set_mapped_vertex_buffer(r300->draw, i, buf);
453    }
454
455    indices = pipe_buffer_map(pipe->screen, indexBuffer,
456                              PIPE_BUFFER_USAGE_CPU_READ);
457    draw_set_mapped_element_buffer_range(r300->draw, indexSize,
458                                         minIndex, maxIndex, indices);
459
460    draw_set_mapped_constant_buffer(r300->draw,
461				    PIPE_SHADER_VERTEX,
462            r300->shader_constants[PIPE_SHADER_VERTEX].constants,
463            r300->shader_constants[PIPE_SHADER_VERTEX].count *
464                (sizeof(float) * 4));
465
466    draw_arrays(r300->draw, mode, start, count);
467
468    for (i = 0; i < r300->vertex_buffer_count; i++) {
469        pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
470        draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
471    }
472
473    pipe_buffer_unmap(pipe->screen, indexBuffer);
474    draw_set_mapped_element_buffer_range(r300->draw, 0, start,
475                                         start + count - 1, NULL);
476}
477
478/* Object for rendering using Draw. */
479struct r300_render {
480    /* Parent class */
481    struct vbuf_render base;
482
483    /* Pipe context */
484    struct r300_context* r300;
485
486    /* Vertex information */
487    size_t vertex_size;
488    unsigned prim;
489    unsigned hwprim;
490
491    /* VBO */
492    struct pipe_buffer* vbo;
493    size_t vbo_size;
494    size_t vbo_offset;
495    size_t vbo_max_used;
496    void * vbo_ptr;
497};
498
499static INLINE struct r300_render*
500r300_render(struct vbuf_render* render)
501{
502    return (struct r300_render*)render;
503}
504
505static const struct vertex_info*
506r300_render_get_vertex_info(struct vbuf_render* render)
507{
508    struct r300_render* r300render = r300_render(render);
509    struct r300_context* r300 = r300render->r300;
510
511    r300_update_derived_state(r300);
512
513    return &r300->vertex_info->vinfo;
514}
515
516static boolean r300_render_allocate_vertices(struct vbuf_render* render,
517                                                   ushort vertex_size,
518                                                   ushort count)
519{
520    struct r300_render* r300render = r300_render(render);
521    struct r300_context* r300 = r300render->r300;
522    struct pipe_screen* screen = r300->context.screen;
523    size_t size = (size_t)vertex_size * (size_t)count;
524
525    if (size + r300render->vbo_offset > r300render->vbo_size)
526    {
527        pipe_buffer_reference(&r300->vbo, NULL);
528        r300render->vbo = pipe_buffer_create(screen,
529                                             64,
530                                             PIPE_BUFFER_USAGE_VERTEX,
531                                             R300_MAX_VBO_SIZE);
532        r300render->vbo_offset = 0;
533        r300render->vbo_size = R300_MAX_VBO_SIZE;
534    }
535
536    r300render->vertex_size = vertex_size;
537    r300->vbo = r300render->vbo;
538    r300->vbo_offset = r300render->vbo_offset;
539
540    return (r300render->vbo) ? TRUE : FALSE;
541}
542
543static void* r300_render_map_vertices(struct vbuf_render* render)
544{
545    struct r300_render* r300render = r300_render(render);
546    struct pipe_screen* screen = r300render->r300->context.screen;
547
548    r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
549                                          PIPE_BUFFER_USAGE_CPU_WRITE);
550
551    return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset);
552}
553
554static void r300_render_unmap_vertices(struct vbuf_render* render,
555                                             ushort min,
556                                             ushort max)
557{
558    struct r300_render* r300render = r300_render(render);
559    struct pipe_screen* screen = r300render->r300->context.screen;
560    CS_LOCALS(r300render->r300);
561    BEGIN_CS(2);
562    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
563    END_CS;
564
565    r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
566                                    r300render->vertex_size * (max + 1));
567    pipe_buffer_unmap(screen, r300render->vbo);
568}
569
570static void r300_render_release_vertices(struct vbuf_render* render)
571{
572    struct r300_render* r300render = r300_render(render);
573
574    r300render->vbo_offset += r300render->vbo_max_used;
575    r300render->vbo_max_used = 0;
576}
577
578static boolean r300_render_set_primitive(struct vbuf_render* render,
579                                               unsigned prim)
580{
581    struct r300_render* r300render = r300_render(render);
582
583    r300render->prim = prim;
584    r300render->hwprim = r300_translate_primitive(prim);
585
586    return TRUE;
587}
588
589static void r300_render_draw_arrays(struct vbuf_render* render,
590                                          unsigned start,
591                                          unsigned count)
592{
593    struct r300_render* r300render = r300_render(render);
594    struct r300_context* r300 = r300render->r300;
595
596    CS_LOCALS(r300);
597
598    r300_emit_dirty_state(r300);
599
600    DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
601
602    BEGIN_CS(2);
603    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
604    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
605           r300render->hwprim);
606    END_CS;
607}
608
609static void r300_render_draw(struct vbuf_render* render,
610                                   const ushort* indices,
611                                   uint count)
612{
613    struct r300_render* r300render = r300_render(render);
614    struct r300_context* r300 = r300render->r300;
615    int i;
616
617    CS_LOCALS(r300);
618
619    r300_emit_dirty_state(r300);
620
621    BEGIN_CS(2 + (count+1)/2);
622    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
623    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
624           r300render->hwprim);
625    for (i = 0; i < count-1; i += 2) {
626        OUT_CS(indices[i+1] << 16 | indices[i]);
627    }
628    if (count % 2) {
629        OUT_CS(indices[count-1]);
630    }
631    END_CS;
632}
633
634static void r300_render_destroy(struct vbuf_render* render)
635{
636    FREE(render);
637}
638
639static struct vbuf_render* r300_render_create(struct r300_context* r300)
640{
641    struct r300_render* r300render = CALLOC_STRUCT(r300_render);
642
643    r300render->r300 = r300;
644
645    /* XXX find real numbers plz */
646    r300render->base.max_vertex_buffer_bytes = 128 * 1024;
647    r300render->base.max_indices = 16 * 1024;
648
649    r300render->base.get_vertex_info = r300_render_get_vertex_info;
650    r300render->base.allocate_vertices = r300_render_allocate_vertices;
651    r300render->base.map_vertices = r300_render_map_vertices;
652    r300render->base.unmap_vertices = r300_render_unmap_vertices;
653    r300render->base.set_primitive = r300_render_set_primitive;
654    r300render->base.draw = r300_render_draw;
655    r300render->base.draw_arrays = r300_render_draw_arrays;
656    r300render->base.release_vertices = r300_render_release_vertices;
657    r300render->base.destroy = r300_render_destroy;
658
659    r300render->vbo = NULL;
660    r300render->vbo_size = 0;
661    r300render->vbo_offset = 0;
662
663    return &r300render->base;
664}
665
666struct draw_stage* r300_draw_stage(struct r300_context* r300)
667{
668    struct vbuf_render* render;
669    struct draw_stage* stage;
670
671    render = r300_render_create(r300);
672
673    if (!render) {
674        return NULL;
675    }
676
677    stage = draw_vbuf_stage(r300->draw, render);
678
679    if (!stage) {
680        render->destroy(render);
681        return NULL;
682    }
683
684    draw_set_render(r300->draw, render);
685
686    return stage;
687}
688