~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/gallium/drivers/r300/r300_render.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3
 
 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4
 
 *
5
 
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 
 * copy of this software and associated documentation files (the "Software"),
7
 
 * to deal in the Software without restriction, including without limitation
8
 
 * on the rights to use, copy, modify, merge, publish, distribute, sub
9
 
 * license, and/or sell copies of the Software, and to permit persons to whom
10
 
 * the Software is furnished to do so, subject to the following conditions:
11
 
 *
12
 
 * The above copyright notice and this permission notice (including the next
13
 
 * paragraph) shall be included in all copies or substantial portions of the
14
 
 * Software.
15
 
 *
16
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19
 
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20
 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
 
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22
 
 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
 
 
24
 
/* r300_render: Vertex and index buffer primitive emission. Contains both
25
 
 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
26
 
 
27
 
#include "draw/draw_context.h"
28
 
#include "draw/draw_vbuf.h"
29
 
 
30
 
#include "util/u_inlines.h"
31
 
 
32
 
#include "util/format/u_format.h"
33
 
#include "util/u_draw.h"
34
 
#include "util/u_memory.h"
35
 
#include "util/u_upload_mgr.h"
36
 
#include "util/u_prim.h"
37
 
 
38
 
#include "r300_cs.h"
39
 
#include "r300_context.h"
40
 
#include "r300_screen_buffer.h"
41
 
#include "r300_emit.h"
42
 
#include "r300_reg.h"
43
 
 
44
 
#include <limits.h>
45
 
 
46
 
#define IMMD_DWORDS 32
47
 
 
48
 
static uint32_t r300_translate_primitive(unsigned prim)
49
 
{
50
 
    static const int prim_conv[] = {
51
 
        R300_VAP_VF_CNTL__PRIM_POINTS,
52
 
        R300_VAP_VF_CNTL__PRIM_LINES,
53
 
        R300_VAP_VF_CNTL__PRIM_LINE_LOOP,
54
 
        R300_VAP_VF_CNTL__PRIM_LINE_STRIP,
55
 
        R300_VAP_VF_CNTL__PRIM_TRIANGLES,
56
 
        R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP,
57
 
        R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN,
58
 
        R300_VAP_VF_CNTL__PRIM_QUADS,
59
 
        R300_VAP_VF_CNTL__PRIM_QUAD_STRIP,
60
 
        R300_VAP_VF_CNTL__PRIM_POLYGON,
61
 
        -1,
62
 
        -1,
63
 
        -1,
64
 
        -1
65
 
    };
66
 
    unsigned hwprim = prim_conv[prim];
67
 
 
68
 
    assert(hwprim != -1);
69
 
    return hwprim;
70
 
}
71
 
 
72
 
static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
73
 
                                            unsigned mode)
74
 
{
75
 
    struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
76
 
    uint32_t color_control = rs->color_control;
77
 
 
78
 
    /* By default (see r300_state.c:r300_create_rs_state) color_control is
79
 
     * initialized to provoking the first vertex.
80
 
     *
81
 
     * Triangle fans must be reduced to the second vertex, not the first, in
82
 
     * Gallium flatshade-first mode, as per the GL spec.
83
 
     * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
84
 
     *
85
 
     * Quads never provoke correctly in flatshade-first mode. The first
86
 
     * vertex is never considered as provoking, so only the second, third,
87
 
     * and fourth vertices can be selected, and both "third" and "last" modes
88
 
     * select the fourth vertex. This is probably due to D3D lacking quads.
89
 
     *
90
 
     * Similarly, polygons reduce to the first, not the last, vertex, when in
91
 
     * "last" mode, and all other modes start from the second vertex.
92
 
     *
93
 
     * ~ C.
94
 
     */
95
 
 
96
 
    if (rs->rs.flatshade_first) {
97
 
        switch (mode) {
98
 
            case PIPE_PRIM_TRIANGLE_FAN:
99
 
                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
100
 
                break;
101
 
            case PIPE_PRIM_QUADS:
102
 
            case PIPE_PRIM_QUAD_STRIP:
103
 
            case PIPE_PRIM_POLYGON:
104
 
                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
105
 
                break;
106
 
            default:
107
 
                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
108
 
                break;
109
 
        }
110
 
    } else {
111
 
        color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
112
 
    }
113
 
 
114
 
    return color_control;
115
 
}
116
 
 
117
 
void r500_emit_index_bias(struct r300_context *r300, int index_bias)
118
 
{
119
 
    CS_LOCALS(r300);
120
 
 
121
 
    BEGIN_CS(2);
122
 
    OUT_CS_REG(R500_VAP_INDEX_OFFSET,
123
 
               (index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
124
 
    END_CS;
125
 
}
126
 
 
127
 
static void r300_emit_draw_init(struct r300_context *r300, unsigned mode,
128
 
                                unsigned max_index)
129
 
{
130
 
    CS_LOCALS(r300);
131
 
 
132
 
    assert(max_index < (1 << 24));
133
 
 
134
 
    BEGIN_CS(5);
135
 
    OUT_CS_REG(R300_GA_COLOR_CONTROL,
136
 
            r300_provoking_vertex_fixes(r300, mode));
137
 
    OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
138
 
    OUT_CS(max_index);
139
 
    OUT_CS(0);
140
 
    END_CS;
141
 
}
142
 
 
143
 
/* This function splits the index bias value into two parts:
144
 
 * - buffer_offset: the value that can be safely added to buffer offsets
145
 
 *   in r300_emit_vertex_arrays (it must yield a positive offset when added to
146
 
 *   a vertex buffer offset)
147
 
 * - index_offset: the value that must be manually subtracted from indices
148
 
 *   in an index buffer to achieve negative offsets. */
149
 
static void r300_split_index_bias(struct r300_context *r300, int index_bias,
150
 
                                  int *buffer_offset, int *index_offset)
151
 
{
152
 
    struct pipe_vertex_buffer *vb, *vbufs = r300->vertex_buffer;
153
 
    struct pipe_vertex_element *velem = r300->velems->velem;
154
 
    unsigned i, size;
155
 
    int max_neg_bias;
156
 
 
157
 
    if (index_bias < 0) {
158
 
        /* See how large index bias we may subtract. We must be careful
159
 
         * here because negative buffer offsets are not allowed
160
 
         * by the DRM API. */
161
 
        max_neg_bias = INT_MAX;
162
 
        for (i = 0; i < r300->velems->count; i++) {
163
 
            vb = &vbufs[velem[i].vertex_buffer_index];
164
 
            size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;
165
 
            max_neg_bias = MIN2(max_neg_bias, size);
166
 
        }
167
 
 
168
 
        /* Now set the minimum allowed value. */
169
 
        *buffer_offset = MAX2(-max_neg_bias, index_bias);
170
 
    } else {
171
 
        /* A positive index bias is OK. */
172
 
        *buffer_offset = index_bias;
173
 
    }
174
 
 
175
 
    *index_offset = index_bias - *buffer_offset;
176
 
}
177
 
 
178
 
enum r300_prepare_flags {
179
 
    PREP_EMIT_STATES    = (1 << 0), /* call emit_dirty_state and friends? */
180
 
    PREP_VALIDATE_VBOS  = (1 << 1), /* validate VBOs? */
181
 
    PREP_EMIT_VARRAYS       = (1 << 2), /* call emit_vertex_arrays? */
182
 
    PREP_EMIT_VARRAYS_SWTCL = (1 << 3), /* call emit_vertex_arrays_swtcl? */
183
 
    PREP_INDEXED        = (1 << 4)  /* is this draw_elements? */
184
 
};
185
 
 
186
 
/**
187
 
 * Check if the requested number of dwords is available in the CS and
188
 
 * if not, flush.
189
 
 * \param r300          The context.
190
 
 * \param flags         See r300_prepare_flags.
191
 
 * \param cs_dwords     The number of dwords to reserve in CS.
192
 
 * \return TRUE if the CS was flushed
193
 
 */
194
 
static boolean r300_reserve_cs_dwords(struct r300_context *r300,
195
 
                                      enum r300_prepare_flags flags,
196
 
                                      unsigned cs_dwords)
197
 
{
198
 
    boolean flushed        = FALSE;
199
 
    boolean emit_states    = flags & PREP_EMIT_STATES;
200
 
    boolean emit_vertex_arrays       = flags & PREP_EMIT_VARRAYS;
201
 
    boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_VARRAYS_SWTCL;
202
 
 
203
 
    /* Add dirty state, index offset, and AOS. */
204
 
    if (emit_states)
205
 
        cs_dwords += r300_get_num_dirty_dwords(r300);
206
 
 
207
 
    if (r300->screen->caps.is_r500)
208
 
        cs_dwords += 2; /* emit_index_offset */
209
 
 
210
 
    if (emit_vertex_arrays)
211
 
        cs_dwords += 55; /* emit_vertex_arrays */
212
 
 
213
 
    if (emit_vertex_arrays_swtcl)
214
 
        cs_dwords += 7; /* emit_vertex_arrays_swtcl */
215
 
 
216
 
    cs_dwords += r300_get_num_cs_end_dwords(r300);
217
 
 
218
 
    /* Reserve requested CS space. */
219
 
    if (!r300->rws->cs_check_space(&r300->cs, cs_dwords)) {
220
 
        r300_flush(&r300->context, PIPE_FLUSH_ASYNC, NULL);
221
 
        flushed = TRUE;
222
 
    }
223
 
 
224
 
    return flushed;
225
 
}
226
 
 
227
 
/**
228
 
 * Validate buffers and emit dirty state.
229
 
 * \param r300          The context.
230
 
 * \param flags         See r300_prepare_flags.
231
 
 * \param index_buffer  The index buffer to validate. The parameter may be NULL.
232
 
 * \param buffer_offset The offset passed to emit_vertex_arrays.
233
 
 * \param index_bias    The index bias to emit.
234
 
 * \param instance_id   Index of instance to render
235
 
 * \return TRUE if rendering should be skipped
236
 
 */
237
 
static boolean r300_emit_states(struct r300_context *r300,
238
 
                                enum r300_prepare_flags flags,
239
 
                                struct pipe_resource *index_buffer,
240
 
                                int buffer_offset,
241
 
                                int index_bias, int instance_id)
242
 
{
243
 
    boolean emit_states    = flags & PREP_EMIT_STATES;
244
 
    boolean emit_vertex_arrays       = flags & PREP_EMIT_VARRAYS;
245
 
    boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_VARRAYS_SWTCL;
246
 
    boolean indexed        = flags & PREP_INDEXED;
247
 
    boolean validate_vbos  = flags & PREP_VALIDATE_VBOS;
248
 
 
249
 
    /* Validate buffers and emit dirty state if needed. */
250
 
    if (emit_states || (emit_vertex_arrays && validate_vbos)) {
251
 
        if (!r300_emit_buffer_validate(r300, validate_vbos,
252
 
                                       index_buffer)) {
253
 
           fprintf(stderr, "r300: CS space validation failed. "
254
 
                   "(not enough memory?) Skipping rendering.\n");
255
 
           return FALSE;
256
 
        }
257
 
    }
258
 
 
259
 
    if (emit_states)
260
 
        r300_emit_dirty_state(r300);
261
 
 
262
 
    if (r300->screen->caps.is_r500) {
263
 
        if (r300->screen->caps.has_tcl)
264
 
            r500_emit_index_bias(r300, index_bias);
265
 
        else
266
 
            r500_emit_index_bias(r300, 0);
267
 
    }
268
 
 
269
 
    if (emit_vertex_arrays &&
270
 
        (r300->vertex_arrays_dirty ||
271
 
         r300->vertex_arrays_indexed != indexed ||
272
 
         r300->vertex_arrays_offset != buffer_offset ||
273
 
         r300->vertex_arrays_instance_id != instance_id)) {
274
 
        r300_emit_vertex_arrays(r300, buffer_offset, indexed, instance_id);
275
 
 
276
 
        r300->vertex_arrays_dirty = FALSE;
277
 
        r300->vertex_arrays_indexed = indexed;
278
 
        r300->vertex_arrays_offset = buffer_offset;
279
 
        r300->vertex_arrays_instance_id = instance_id;
280
 
    }
281
 
 
282
 
    if (emit_vertex_arrays_swtcl)
283
 
        r300_emit_vertex_arrays_swtcl(r300, indexed);
284
 
 
285
 
    return TRUE;
286
 
}
287
 
 
288
 
/**
289
 
 * Check if the requested number of dwords is available in the CS and
290
 
 * if not, flush. Then validate buffers and emit dirty state.
291
 
 * \param r300          The context.
292
 
 * \param flags         See r300_prepare_flags.
293
 
 * \param index_buffer  The index buffer to validate. The parameter may be NULL.
294
 
 * \param cs_dwords     The number of dwords to reserve in CS.
295
 
 * \param buffer_offset The offset passed to emit_vertex_arrays.
296
 
 * \param index_bias    The index bias to emit.
297
 
 * \param instance_id The instance to render.
298
 
 * \return TRUE if rendering should be skipped
299
 
 */
300
 
static boolean r300_prepare_for_rendering(struct r300_context *r300,
301
 
                                          enum r300_prepare_flags flags,
302
 
                                          struct pipe_resource *index_buffer,
303
 
                                          unsigned cs_dwords,
304
 
                                          int buffer_offset,
305
 
                                          int index_bias,
306
 
                                          int instance_id)
307
 
{
308
 
    /* Make sure there is enough space in the command stream and emit states. */
309
 
    if (r300_reserve_cs_dwords(r300, flags, cs_dwords))
310
 
        flags |= PREP_EMIT_STATES;
311
 
 
312
 
    return r300_emit_states(r300, flags, index_buffer, buffer_offset,
313
 
                            index_bias, instance_id);
314
 
}
315
 
 
316
 
static boolean immd_is_good_idea(struct r300_context *r300,
317
 
                                 unsigned count)
318
 
{
319
 
    if (DBG_ON(r300, DBG_NO_IMMD)) {
320
 
        return FALSE;
321
 
    }
322
 
 
323
 
    if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
324
 
        return FALSE;
325
 
    }
326
 
 
327
 
    /* Buffers can only be used for read by r300 (except query buffers, but
328
 
     * those can't be bound by an gallium frontend as vertex buffers). */
329
 
    return TRUE;
330
 
}
331
 
 
332
 
/*****************************************************************************
333
 
 * The HWTCL draw functions.                                                 *
334
 
 ****************************************************************************/
335
 
 
336
 
static void r300_draw_arrays_immediate(struct r300_context *r300,
337
 
                                       const struct pipe_draw_info *info,
338
 
                                       const struct pipe_draw_start_count_bias *draw)
339
 
{
340
 
    struct pipe_vertex_element* velem;
341
 
    struct pipe_vertex_buffer* vbuf;
342
 
    unsigned vertex_element_count = r300->velems->count;
343
 
    unsigned i, v, vbi;
344
 
 
345
 
    /* Size of the vertex, in dwords. */
346
 
    unsigned vertex_size = r300->velems->vertex_size_dwords;
347
 
 
348
 
    /* The number of dwords for this draw operation. */
349
 
    unsigned dwords = 4 + draw->count * vertex_size;
350
 
 
351
 
    /* Size of the vertex element, in dwords. */
352
 
    unsigned size[PIPE_MAX_ATTRIBS];
353
 
 
354
 
    /* Stride to the same attrib in the next vertex in the vertex buffer,
355
 
     * in dwords. */
356
 
    unsigned stride[PIPE_MAX_ATTRIBS];
357
 
 
358
 
    /* Mapped vertex buffers. */
359
 
    uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
360
 
    uint32_t* mapelem[PIPE_MAX_ATTRIBS];
361
 
 
362
 
    CS_LOCALS(r300);
363
 
 
364
 
    if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
365
 
        return;
366
 
 
367
 
    /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
368
 
    for (i = 0; i < vertex_element_count; i++) {
369
 
        velem = &r300->velems->velem[i];
370
 
        size[i] = r300->velems->format_size[i] / 4;
371
 
        vbi = velem->vertex_buffer_index;
372
 
        vbuf = &r300->vertex_buffer[vbi];
373
 
        stride[i] = vbuf->stride / 4;
374
 
 
375
 
        /* Map the buffer. */
376
 
        if (!map[vbi]) {
377
 
            map[vbi] = (uint32_t*)r300->rws->buffer_map(r300->rws,
378
 
                r300_resource(vbuf->buffer.resource)->buf,
379
 
                &r300->cs, PIPE_MAP_READ | PIPE_MAP_UNSYNCHRONIZED);
380
 
            map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * draw->start;
381
 
        }
382
 
        mapelem[i] = map[vbi] + (velem->src_offset / 4);
383
 
    }
384
 
 
385
 
    r300_emit_draw_init(r300, info->mode, draw->count-1);
386
 
 
387
 
    BEGIN_CS(dwords);
388
 
    OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
389
 
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, draw->count * vertex_size);
390
 
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (draw->count << 16) |
391
 
            r300_translate_primitive(info->mode));
392
 
 
393
 
    /* Emit vertices. */
394
 
    for (v = 0; v < draw->count; v++) {
395
 
        for (i = 0; i < vertex_element_count; i++) {
396
 
            OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);
397
 
        }
398
 
    }
399
 
    END_CS;
400
 
}
401
 
 
402
 
static void r300_emit_draw_arrays(struct r300_context *r300,
403
 
                                  unsigned mode,
404
 
                                  unsigned count)
405
 
{
406
 
    boolean alt_num_verts = count > 65535;
407
 
    CS_LOCALS(r300);
408
 
 
409
 
    if (count >= (1 << 24)) {
410
 
        fprintf(stderr, "r300: Got a huge number of vertices: %i, "
411
 
                "refusing to render.\n", count);
412
 
        return;
413
 
    }
414
 
 
415
 
    r300_emit_draw_init(r300, mode, count-1);
416
 
 
417
 
    BEGIN_CS(2 + (alt_num_verts ? 2 : 0));
418
 
    if (alt_num_verts) {
419
 
        OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
420
 
    }
421
 
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
422
 
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
423
 
           r300_translate_primitive(mode) |
424
 
           (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
425
 
    END_CS;
426
 
}
427
 
 
428
 
static void r300_emit_draw_elements(struct r300_context *r300,
429
 
                                    struct pipe_resource* indexBuffer,
430
 
                                    unsigned indexSize,
431
 
                                    unsigned max_index,
432
 
                                    unsigned mode,
433
 
                                    unsigned start,
434
 
                                    unsigned count,
435
 
                                    uint16_t *imm_indices3)
436
 
{
437
 
    uint32_t count_dwords, offset_dwords;
438
 
    boolean alt_num_verts = count > 65535;
439
 
    CS_LOCALS(r300);
440
 
 
441
 
    if (count >= (1 << 24)) {
442
 
        fprintf(stderr, "r300: Got a huge number of vertices: %i, "
443
 
                "refusing to render (max_index: %i).\n", count, max_index);
444
 
        return;
445
 
    }
446
 
 
447
 
    DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, max %u\n",
448
 
        count, max_index);
449
 
 
450
 
    r300_emit_draw_init(r300, mode, max_index);
451
 
 
452
 
    /* If start is odd, render the first triangle with indices embedded
453
 
     * in the command stream. This will increase start by 3 and make it
454
 
     * even. We can then proceed without a fallback. */
455
 
    if (indexSize == 2 && (start & 1) &&
456
 
        mode == PIPE_PRIM_TRIANGLES) {
457
 
        BEGIN_CS(4);
458
 
        OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 2);
459
 
        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (3 << 16) |
460
 
               R300_VAP_VF_CNTL__PRIM_TRIANGLES);
461
 
        OUT_CS(imm_indices3[1] << 16 | imm_indices3[0]);
462
 
        OUT_CS(imm_indices3[2]);
463
 
        END_CS;
464
 
 
465
 
        start += 3;
466
 
        count -= 3;
467
 
        if (!count)
468
 
           return;
469
 
    }
470
 
 
471
 
    offset_dwords = indexSize * start / sizeof(uint32_t);
472
 
 
473
 
    BEGIN_CS(8 + (alt_num_verts ? 2 : 0));
474
 
    if (alt_num_verts) {
475
 
        OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
476
 
    }
477
 
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
478
 
    if (indexSize == 4) {
479
 
        count_dwords = count;
480
 
        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
481
 
               R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
482
 
               r300_translate_primitive(mode) |
483
 
               (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
484
 
    } else {
485
 
        count_dwords = (count + 1) / 2;
486
 
        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
487
 
               r300_translate_primitive(mode) |
488
 
               (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
489
 
    }
490
 
 
491
 
    OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
492
 
    OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
493
 
           (0 << R300_INDX_BUFFER_SKIP_SHIFT));
494
 
    OUT_CS(offset_dwords << 2);
495
 
    OUT_CS(count_dwords);
496
 
    OUT_CS_RELOC(r300_resource(indexBuffer));
497
 
    END_CS;
498
 
}
499
 
 
500
 
static void r300_draw_elements_immediate(struct r300_context *r300,
501
 
                                         const struct pipe_draw_info *info,
502
 
                                         const struct pipe_draw_start_count_bias *draw)
503
 
{
504
 
    const uint8_t *ptr1;
505
 
    const uint16_t *ptr2;
506
 
    const uint32_t *ptr4;
507
 
    unsigned index_size = info->index_size;
508
 
    unsigned i, count_dwords = index_size == 4 ? draw->count :
509
 
                                                 (draw->count + 1) / 2;
510
 
    CS_LOCALS(r300);
511
 
 
512
 
    /* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */
513
 
    if (!r300_prepare_for_rendering(r300,
514
 
            PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS |
515
 
            PREP_INDEXED, NULL, 2+count_dwords, 0, draw->index_bias, -1))
516
 
        return;
517
 
 
518
 
    r300_emit_draw_init(r300, info->mode, info->max_index);
519
 
 
520
 
    BEGIN_CS(2 + count_dwords);
521
 
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, count_dwords);
522
 
 
523
 
    switch (index_size) {
524
 
    case 1:
525
 
        ptr1 = (uint8_t*)info->index.user;
526
 
        ptr1 += draw->start;
527
 
 
528
 
        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (draw->count << 16) |
529
 
               r300_translate_primitive(info->mode));
530
 
 
531
 
        if (draw->index_bias && !r300->screen->caps.is_r500) {
532
 
            for (i = 0; i < draw->count-1; i += 2)
533
 
                OUT_CS(((ptr1[i+1] + draw->index_bias) << 16) |
534
 
                        (ptr1[i]   + draw->index_bias));
535
 
 
536
 
            if (draw->count & 1)
537
 
                OUT_CS(ptr1[i] + draw->index_bias);
538
 
        } else {
539
 
            for (i = 0; i < draw->count-1; i += 2)
540
 
                OUT_CS(((ptr1[i+1]) << 16) |
541
 
                        (ptr1[i]  ));
542
 
 
543
 
            if (draw->count & 1)
544
 
                OUT_CS(ptr1[i]);
545
 
        }
546
 
        break;
547
 
 
548
 
    case 2:
549
 
        ptr2 = (uint16_t*)info->index.user;
550
 
        ptr2 += draw->start;
551
 
 
552
 
        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (draw->count << 16) |
553
 
               r300_translate_primitive(info->mode));
554
 
 
555
 
        if (draw->index_bias && !r300->screen->caps.is_r500) {
556
 
            for (i = 0; i < draw->count-1; i += 2)
557
 
                OUT_CS(((ptr2[i+1] + draw->index_bias) << 16) |
558
 
                        (ptr2[i]   + draw->index_bias));
559
 
 
560
 
            if (draw->count & 1)
561
 
                OUT_CS(ptr2[i] + draw->index_bias);
562
 
        } else {
563
 
            OUT_CS_TABLE(ptr2, count_dwords);
564
 
        }
565
 
        break;
566
 
 
567
 
    case 4:
568
 
        ptr4 = (uint32_t*)info->index.user;
569
 
        ptr4 += draw->start;
570
 
 
571
 
        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (draw->count << 16) |
572
 
               R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
573
 
               r300_translate_primitive(info->mode));
574
 
 
575
 
        if (draw->index_bias && !r300->screen->caps.is_r500) {
576
 
            for (i = 0; i < draw->count; i++)
577
 
                OUT_CS(ptr4[i] + draw->index_bias);
578
 
        } else {
579
 
            OUT_CS_TABLE(ptr4, count_dwords);
580
 
        }
581
 
        break;
582
 
    }
583
 
    END_CS;
584
 
}
585
 
 
586
 
static void r300_draw_elements(struct r300_context *r300,
587
 
                               const struct pipe_draw_info *info,
588
 
                               const struct pipe_draw_start_count_bias *draw,
589
 
                               int instance_id)
590
 
{
591
 
    struct pipe_resource *indexBuffer =
592
 
       info->has_user_indices ? NULL : info->index.resource;
593
 
    unsigned indexSize = info->index_size;
594
 
    struct pipe_resource* orgIndexBuffer = indexBuffer;
595
 
    unsigned start = draw->start;
596
 
    unsigned count = draw->count;
597
 
    boolean alt_num_verts = r300->screen->caps.is_r500 &&
598
 
                            count > 65536;
599
 
    unsigned short_count;
600
 
    int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
601
 
    uint16_t indices3[3];
602
 
 
603
 
    if (draw->index_bias && !r300->screen->caps.is_r500) {
604
 
        r300_split_index_bias(r300, draw->index_bias, &buffer_offset,
605
 
                              &index_offset);
606
 
    }
607
 
 
608
 
    r300_translate_index_buffer(r300, info, &indexBuffer,
609
 
                                &indexSize, index_offset, &start, count);
610
 
 
611
 
    /* Fallback for misaligned ushort indices. */
612
 
    if (indexSize == 2 && (start & 1) && indexBuffer) {
613
 
        /* If we got here, then orgIndexBuffer == indexBuffer. */
614
 
        uint16_t *ptr = r300->rws->buffer_map(r300->rws, r300_resource(orgIndexBuffer)->buf,
615
 
                                              &r300->cs,
616
 
                                              PIPE_MAP_READ |
617
 
                                              PIPE_MAP_UNSYNCHRONIZED);
618
 
 
619
 
        if (info->mode == PIPE_PRIM_TRIANGLES) {
620
 
           memcpy(indices3, ptr + start, 6);
621
 
        } else {
622
 
            /* Copy the mapped index buffer directly to the upload buffer.
623
 
             * The start index will be aligned simply from the fact that
624
 
             * every sub-buffer in the upload buffer is aligned. */
625
 
            r300_upload_index_buffer(r300, &indexBuffer, indexSize, &start,
626
 
                                     count, (uint8_t*)ptr);
627
 
        }
628
 
    } else {
629
 
        if (info->has_user_indices)
630
 
            r300_upload_index_buffer(r300, &indexBuffer, indexSize,
631
 
                                     &start, count,
632
 
                                     info->index.user);
633
 
    }
634
 
 
635
 
    /* 19 dwords for emit_draw_elements. Give up if the function fails. */
636
 
    if (!r300_prepare_for_rendering(r300,
637
 
            PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS |
638
 
            PREP_INDEXED, indexBuffer, 19, buffer_offset, draw->index_bias,
639
 
            instance_id))
640
 
        goto done;
641
 
 
642
 
    if (alt_num_verts || count <= 65535) {
643
 
        r300_emit_draw_elements(r300, indexBuffer, indexSize,
644
 
                                info->max_index, info->mode, start, count,
645
 
                                indices3);
646
 
    } else {
647
 
        do {
648
 
            /* The maximum must be divisible by 4 and 3,
649
 
             * so that quad and triangle lists are split correctly.
650
 
             *
651
 
             * Strips, loops, and fans won't work. */
652
 
            short_count = MIN2(count, 65532);
653
 
 
654
 
            r300_emit_draw_elements(r300, indexBuffer, indexSize,
655
 
                                     info->max_index,
656
 
                                     info->mode, start, short_count, indices3);
657
 
 
658
 
            start += short_count;
659
 
            count -= short_count;
660
 
 
661
 
            /* 15 dwords for emit_draw_elements */
662
 
            if (count) {
663
 
                if (!r300_prepare_for_rendering(r300,
664
 
                        PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS | PREP_INDEXED,
665
 
                        indexBuffer, 19, buffer_offset, draw->index_bias,
666
 
                        instance_id))
667
 
                    goto done;
668
 
            }
669
 
        } while (count);
670
 
    }
671
 
 
672
 
done:
673
 
    if (indexBuffer != orgIndexBuffer) {
674
 
        pipe_resource_reference( &indexBuffer, NULL );
675
 
    }
676
 
}
677
 
 
678
 
static void r300_draw_arrays(struct r300_context *r300,
679
 
                             const struct pipe_draw_info *info,
680
 
                             const struct pipe_draw_start_count_bias *draw,
681
 
                             int instance_id)
682
 
{
683
 
    boolean alt_num_verts = r300->screen->caps.is_r500 &&
684
 
                            draw->count > 65536;
685
 
    unsigned start = draw->start;
686
 
    unsigned count = draw->count;
687
 
    unsigned short_count;
688
 
 
689
 
    /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
690
 
    if (!r300_prepare_for_rendering(r300,
691
 
                                    PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS,
692
 
                                    NULL, 9, start, 0, instance_id))
693
 
        return;
694
 
 
695
 
    if (alt_num_verts || count <= 65535) {
696
 
        r300_emit_draw_arrays(r300, info->mode, count);
697
 
    } else {
698
 
        do {
699
 
            /* The maximum must be divisible by 4 and 3,
700
 
             * so that quad and triangle lists are split correctly.
701
 
             *
702
 
             * Strips, loops, and fans won't work. */
703
 
            short_count = MIN2(count, 65532);
704
 
            r300_emit_draw_arrays(r300, info->mode, short_count);
705
 
 
706
 
            start += short_count;
707
 
            count -= short_count;
708
 
 
709
 
            /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
710
 
            if (count) {
711
 
                if (!r300_prepare_for_rendering(r300,
712
 
                                                PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS, NULL, 9,
713
 
                                                start, 0, instance_id))
714
 
                    return;
715
 
            }
716
 
        } while (count);
717
 
    }
718
 
}
719
 
 
720
 
static void r300_draw_arrays_instanced(struct r300_context *r300,
721
 
                                       const struct pipe_draw_info *info,
722
 
                                       const struct pipe_draw_start_count_bias *draw)
723
 
{
724
 
    int i;
725
 
 
726
 
    for (i = 0; i < info->instance_count; i++)
727
 
        r300_draw_arrays(r300, info, draw, i);
728
 
}
729
 
 
730
 
static void r300_draw_elements_instanced(struct r300_context *r300,
731
 
                                         const struct pipe_draw_info *info,
732
 
                                         const struct pipe_draw_start_count_bias *draw)
733
 
{
734
 
    int i;
735
 
 
736
 
    for (i = 0; i < info->instance_count; i++)
737
 
        r300_draw_elements(r300, info, draw, i);
738
 
}
739
 
 
740
 
static unsigned r300_max_vertex_count(struct r300_context *r300)
741
 
{
742
 
   unsigned i, nr = r300->velems->count;
743
 
   struct pipe_vertex_element *velems = r300->velems->velem;
744
 
   unsigned result = ~0;
745
 
 
746
 
   for (i = 0; i < nr; i++) {
747
 
      struct pipe_vertex_buffer *vb =
748
 
            &r300->vertex_buffer[velems[i].vertex_buffer_index];
749
 
      unsigned size, max_count, value;
750
 
 
751
 
      /* We're not interested in constant and per-instance attribs. */
752
 
      if (!vb->buffer.resource ||
753
 
          !vb->stride ||
754
 
          velems[i].instance_divisor) {
755
 
         continue;
756
 
      }
757
 
 
758
 
      size = vb->buffer.resource->width0;
759
 
 
760
 
      /* Subtract buffer_offset. */
761
 
      value = vb->buffer_offset;
762
 
      if (value >= size) {
763
 
         return 0;
764
 
      }
765
 
      size -= value;
766
 
 
767
 
      /* Subtract src_offset. */
768
 
      value = velems[i].src_offset;
769
 
      if (value >= size) {
770
 
         return 0;
771
 
      }
772
 
      size -= value;
773
 
 
774
 
      /* Subtract format_size. */
775
 
      value = r300->velems->format_size[i];
776
 
      if (value >= size) {
777
 
         return 0;
778
 
      }
779
 
      size -= value;
780
 
 
781
 
      /* Compute the max count. */
782
 
      max_count = 1 + size / vb->stride;
783
 
      result = MIN2(result, max_count);
784
 
   }
785
 
   return result;
786
 
}
787
 
 
788
 
 
789
 
static void r300_draw_vbo(struct pipe_context* pipe,
790
 
                          const struct pipe_draw_info *dinfo,
791
 
                          unsigned drawid_offset,
792
 
                          const struct pipe_draw_indirect_info *indirect,
793
 
                          const struct pipe_draw_start_count_bias *draws,
794
 
                          unsigned num_draws)
795
 
{
796
 
   if (num_draws > 1) {
797
 
      util_draw_multi(pipe, dinfo, drawid_offset, indirect, draws, num_draws);
798
 
      return;
799
 
   }
800
 
 
801
 
    struct r300_context* r300 = r300_context(pipe);
802
 
    struct pipe_draw_info info = *dinfo;
803
 
    struct pipe_draw_start_count_bias draw = draws[0];
804
 
 
805
 
    if (r300->skip_rendering ||
806
 
        !u_trim_pipe_prim(info.mode, &draw.count)) {
807
 
        return;
808
 
    }
809
 
 
810
 
    if (r300->sprite_coord_enable != 0)
811
 
        if ((info.mode == PIPE_PRIM_POINTS) != r300->is_point) {
812
 
            r300->is_point = !r300->is_point;
813
 
            r300_mark_atom_dirty(r300, &r300->rs_block_state);
814
 
        }
815
 
 
816
 
    r300_update_derived_state(r300);
817
 
 
818
 
    /* Draw. */
819
 
    if (info.index_size) {
820
 
        unsigned max_count = r300_max_vertex_count(r300);
821
 
 
822
 
        if (!max_count) {
823
 
           fprintf(stderr, "r300: Skipping a draw command. There is a buffer "
824
 
                   " which is too small to be used for rendering.\n");
825
 
           return;
826
 
        }
827
 
 
828
 
        if (max_count == ~0) {
829
 
           /* There are no per-vertex vertex elements. Use the hardware maximum. */
830
 
           max_count = 0xffffff;
831
 
        }
832
 
 
833
 
        info.max_index = max_count - 1;
834
 
 
835
 
        if (info.instance_count <= 1) {
836
 
            if (draw.count <= 8 && info.has_user_indices) {
837
 
                r300_draw_elements_immediate(r300, &info, &draw);
838
 
            } else {
839
 
                r300_draw_elements(r300, &info, &draw, -1);
840
 
            }
841
 
        } else {
842
 
            r300_draw_elements_instanced(r300, &info, &draw);
843
 
        }
844
 
    } else {
845
 
        if (info.instance_count <= 1) {
846
 
            if (immd_is_good_idea(r300, draw.count)) {
847
 
                r300_draw_arrays_immediate(r300, &info, &draw);
848
 
            } else {
849
 
                r300_draw_arrays(r300, &info, &draw, -1);
850
 
            }
851
 
        } else {
852
 
            r300_draw_arrays_instanced(r300, &info, &draw);
853
 
        }
854
 
    }
855
 
}
856
 
 
857
 
/****************************************************************************
858
 
 * The rest of this file is for SW TCL rendering only. Please be polite and *
859
 
 * keep these functions separated so that they are easier to locate. ~C.    *
860
 
 ***************************************************************************/
861
 
 
862
 
/* SW TCL elements, using Draw. */
863
 
static void r300_swtcl_draw_vbo(struct pipe_context* pipe,
864
 
                                const struct pipe_draw_info *info,
865
 
                                unsigned drawid_offset,
866
 
                                const struct pipe_draw_indirect_info *indirect,
867
 
                                const struct pipe_draw_start_count_bias *draws,
868
 
                                unsigned num_draws)
869
 
{
870
 
   if (num_draws > 1) {
871
 
      util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);
872
 
      return;
873
 
   }
874
 
 
875
 
    struct r300_context* r300 = r300_context(pipe);
876
 
    struct pipe_draw_start_count_bias draw = draws[0];
877
 
 
878
 
    if (r300->skip_rendering) {
879
 
        return;
880
 
    }
881
 
 
882
 
    if (!u_trim_pipe_prim(info->mode, &draw.count))
883
 
       return;
884
 
 
885
 
    if (info->index_size) {
886
 
        draw_set_indexes(r300->draw,
887
 
                         info->has_user_indices ?
888
 
                             info->index.user :
889
 
                             r300_resource(info->index.resource)->malloced_buffer,
890
 
                         info->index_size, ~0);
891
 
    }
892
 
 
893
 
    if (r300->sprite_coord_enable != 0)
894
 
        if ((info->mode == PIPE_PRIM_POINTS) != r300->is_point) {
895
 
            r300->is_point = !r300->is_point;
896
 
            r300_mark_atom_dirty(r300, &r300->rs_block_state);
897
 
        }
898
 
 
899
 
    r300_update_derived_state(r300);
900
 
 
901
 
    draw_vbo(r300->draw, info, drawid_offset, NULL, &draw, 1, 0);
902
 
    draw_flush(r300->draw);
903
 
}
904
 
 
905
 
/* Object for rendering using Draw. */
906
 
struct r300_render {
907
 
    /* Parent class */
908
 
    struct vbuf_render base;
909
 
 
910
 
    /* Pipe context */
911
 
    struct r300_context* r300;
912
 
 
913
 
    /* Vertex information */
914
 
    size_t vertex_size;
915
 
    unsigned prim;
916
 
    unsigned hwprim;
917
 
 
918
 
    /* VBO */
919
 
    size_t vbo_max_used;
920
 
    uint8_t *vbo_ptr;
921
 
};
922
 
 
923
 
static inline struct r300_render*
924
 
r300_render(struct vbuf_render* render)
925
 
{
926
 
    return (struct r300_render*)render;
927
 
}
928
 
 
929
 
static const struct vertex_info*
930
 
r300_render_get_vertex_info(struct vbuf_render* render)
931
 
{
932
 
    struct r300_render* r300render = r300_render(render);
933
 
    struct r300_context* r300 = r300render->r300;
934
 
 
935
 
    return &r300->vertex_info;
936
 
}
937
 
 
938
 
static boolean r300_render_allocate_vertices(struct vbuf_render* render,
939
 
                                             ushort vertex_size,
940
 
                                             ushort count)
941
 
{
942
 
    struct r300_render* r300render = r300_render(render);
943
 
    struct r300_context* r300 = r300render->r300;
944
 
    struct radeon_winsys *rws = r300->rws;
945
 
    size_t size = (size_t)vertex_size * (size_t)count;
946
 
 
947
 
    DBG(r300, DBG_DRAW, "r300: render_allocate_vertices (size: %d)\n", size);
948
 
 
949
 
    if (!r300->vbo || size + r300->draw_vbo_offset > r300->vbo->size) {
950
 
        pb_reference(&r300->vbo, NULL);
951
 
        r300->vbo = NULL;
952
 
        r300render->vbo_ptr = NULL;
953
 
 
954
 
        r300->vbo = rws->buffer_create(rws,
955
 
                                       MAX2(R300_MAX_DRAW_VBO_SIZE, size),
956
 
                                       R300_BUFFER_ALIGNMENT,
957
 
                                       RADEON_DOMAIN_GTT,
958
 
                                       RADEON_FLAG_NO_INTERPROCESS_SHARING);
959
 
        if (!r300->vbo) {
960
 
            return FALSE;
961
 
        }
962
 
        r300->draw_vbo_offset = 0;
963
 
        r300render->vbo_ptr = rws->buffer_map(rws, r300->vbo, &r300->cs,
964
 
                                              PIPE_MAP_WRITE);
965
 
    }
966
 
 
967
 
    r300render->vertex_size = vertex_size;
968
 
    return TRUE;
969
 
}
970
 
 
971
 
static void* r300_render_map_vertices(struct vbuf_render* render)
972
 
{
973
 
    struct r300_render* r300render = r300_render(render);
974
 
    struct r300_context* r300 = r300render->r300;
975
 
 
976
 
    DBG(r300, DBG_DRAW, "r300: render_map_vertices\n");
977
 
 
978
 
    assert(r300render->vbo_ptr);
979
 
    return r300render->vbo_ptr + r300->draw_vbo_offset;
980
 
}
981
 
 
982
 
static void r300_render_unmap_vertices(struct vbuf_render* render,
983
 
                                             ushort min,
984
 
                                             ushort max)
985
 
{
986
 
    struct r300_render* r300render = r300_render(render);
987
 
    struct r300_context* r300 = r300render->r300;
988
 
 
989
 
    DBG(r300, DBG_DRAW, "r300: render_unmap_vertices\n");
990
 
 
991
 
    r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
992
 
                                    r300render->vertex_size * (max + 1));
993
 
}
994
 
 
995
 
static void r300_render_release_vertices(struct vbuf_render* render)
996
 
{
997
 
    struct r300_render* r300render = r300_render(render);
998
 
    struct r300_context* r300 = r300render->r300;
999
 
 
1000
 
    DBG(r300, DBG_DRAW, "r300: render_release_vertices\n");
1001
 
 
1002
 
    r300->draw_vbo_offset += r300render->vbo_max_used;
1003
 
    r300render->vbo_max_used = 0;
1004
 
}
1005
 
 
1006
 
static void r300_render_set_primitive(struct vbuf_render* render,
1007
 
                                      enum pipe_prim_type prim)
1008
 
{
1009
 
    struct r300_render* r300render = r300_render(render);
1010
 
 
1011
 
    r300render->prim = prim;
1012
 
    r300render->hwprim = r300_translate_primitive(prim);
1013
 
}
1014
 
 
1015
 
static void r300_render_draw_arrays(struct vbuf_render* render,
1016
 
                                    unsigned start,
1017
 
                                    unsigned count)
1018
 
{
1019
 
    struct r300_render* r300render = r300_render(render);
1020
 
    struct r300_context* r300 = r300render->r300;
1021
 
    uint8_t* ptr;
1022
 
    unsigned i;
1023
 
    unsigned dwords = 6;
1024
 
 
1025
 
    CS_LOCALS(r300);
1026
 
    (void) i; (void) ptr;
1027
 
 
1028
 
    assert(start == 0);
1029
 
    assert(count < (1 << 16));
1030
 
 
1031
 
    DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
1032
 
 
1033
 
    if (!r300_prepare_for_rendering(r300,
1034
 
                                    PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL,
1035
 
                                    NULL, dwords, 0, 0, -1)) {
1036
 
        return;
1037
 
    }
1038
 
 
1039
 
    BEGIN_CS(dwords);
1040
 
    OUT_CS_REG(R300_GA_COLOR_CONTROL,
1041
 
            r300_provoking_vertex_fixes(r300, r300render->prim));
1042
 
    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
1043
 
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
1044
 
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
1045
 
           r300render->hwprim);
1046
 
    END_CS;
1047
 
}
1048
 
 
1049
 
static void r300_render_draw_elements(struct vbuf_render* render,
1050
 
                                      const ushort* indices,
1051
 
                                      uint count)
1052
 
{
1053
 
    struct r300_render* r300render = r300_render(render);
1054
 
    struct r300_context* r300 = r300render->r300;
1055
 
    unsigned max_index = (r300->vbo->size - r300->draw_vbo_offset) /
1056
 
                         (r300render->r300->vertex_info.size * 4) - 1;
1057
 
    struct pipe_resource *index_buffer = NULL;
1058
 
    unsigned index_buffer_offset;
1059
 
 
1060
 
    CS_LOCALS(r300);
1061
 
    DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
1062
 
 
1063
 
    u_upload_data(r300->uploader, 0, count * 2, 4, indices,
1064
 
                  &index_buffer_offset, &index_buffer);
1065
 
    if (!index_buffer) {
1066
 
        return;
1067
 
    }
1068
 
 
1069
 
    if (!r300_prepare_for_rendering(r300,
1070
 
                                    PREP_EMIT_STATES |
1071
 
                                    PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,
1072
 
                                    index_buffer, 12, 0, 0, -1)) {
1073
 
        pipe_resource_reference(&index_buffer, NULL);
1074
 
        return;
1075
 
    }
1076
 
 
1077
 
    BEGIN_CS(12);
1078
 
    OUT_CS_REG(R300_GA_COLOR_CONTROL,
1079
 
               r300_provoking_vertex_fixes(r300, r300render->prim));
1080
 
    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);
1081
 
 
1082
 
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
1083
 
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
1084
 
           r300render->hwprim);
1085
 
 
1086
 
    OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
1087
 
    OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2));
1088
 
    OUT_CS(index_buffer_offset);
1089
 
    OUT_CS((count + 1) / 2);
1090
 
    OUT_CS_RELOC(r300_resource(index_buffer));
1091
 
    END_CS;
1092
 
 
1093
 
    pipe_resource_reference(&index_buffer, NULL);
1094
 
}
1095
 
 
1096
 
static void r300_render_destroy(struct vbuf_render* render)
1097
 
{
1098
 
    FREE(render);
1099
 
}
1100
 
 
1101
 
static struct vbuf_render* r300_render_create(struct r300_context* r300)
1102
 
{
1103
 
    struct r300_render* r300render = CALLOC_STRUCT(r300_render);
1104
 
 
1105
 
    r300render->r300 = r300;
1106
 
 
1107
 
    r300render->base.max_vertex_buffer_bytes = R300_MAX_DRAW_VBO_SIZE;
1108
 
    r300render->base.max_indices = 16 * 1024;
1109
 
 
1110
 
    r300render->base.get_vertex_info = r300_render_get_vertex_info;
1111
 
    r300render->base.allocate_vertices = r300_render_allocate_vertices;
1112
 
    r300render->base.map_vertices = r300_render_map_vertices;
1113
 
    r300render->base.unmap_vertices = r300_render_unmap_vertices;
1114
 
    r300render->base.set_primitive = r300_render_set_primitive;
1115
 
    r300render->base.draw_elements = r300_render_draw_elements;
1116
 
    r300render->base.draw_arrays = r300_render_draw_arrays;
1117
 
    r300render->base.release_vertices = r300_render_release_vertices;
1118
 
    r300render->base.destroy = r300_render_destroy;
1119
 
 
1120
 
    return &r300render->base;
1121
 
}
1122
 
 
1123
 
struct draw_stage* r300_draw_stage(struct r300_context* r300)
1124
 
{
1125
 
    struct vbuf_render* render;
1126
 
    struct draw_stage* stage;
1127
 
 
1128
 
    render = r300_render_create(r300);
1129
 
 
1130
 
    if (!render) {
1131
 
        return NULL;
1132
 
    }
1133
 
 
1134
 
    stage = draw_vbuf_stage(r300->draw, render);
1135
 
 
1136
 
    if (!stage) {
1137
 
        render->destroy(render);
1138
 
        return NULL;
1139
 
    }
1140
 
 
1141
 
    draw_set_render(r300->draw, render);
1142
 
 
1143
 
    return stage;
1144
 
}
1145
 
 
1146
 
/****************************************************************************
1147
 
 *                         End of SW TCL functions                          *
1148
 
 ***************************************************************************/
1149
 
 
1150
 
/* This functions is used to draw a rectangle for the blitter module.
1151
 
 *
1152
 
 * If we rendered a quad, the pixels on the main diagonal
1153
 
 * would be computed and stored twice, which makes the clear/copy codepaths
1154
 
 * somewhat inefficient. Instead we use a rectangular point sprite. */
1155
 
void r300_blitter_draw_rectangle(struct blitter_context *blitter,
1156
 
                                 void *vertex_elements_cso,
1157
 
                                 blitter_get_vs_func get_vs,
1158
 
                                 int x1, int y1, int x2, int y2,
1159
 
                                 float depth, unsigned num_instances,
1160
 
                                 enum blitter_attrib_type type,
1161
 
                                 const union blitter_attrib *attrib)
1162
 
{
1163
 
    struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));
1164
 
    unsigned last_sprite_coord_enable = r300->sprite_coord_enable;
1165
 
    unsigned last_is_point = r300->is_point;
1166
 
    unsigned width = x2 - x1;
1167
 
    unsigned height = y2 - y1;
1168
 
    unsigned vertex_size =
1169
 
            type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;
1170
 
    unsigned dwords = 13 + vertex_size +
1171
 
                      (type == UTIL_BLITTER_ATTRIB_TEXCOORD_XY ? 7 : 0);
1172
 
    static const union blitter_attrib zeros;
1173
 
    CS_LOCALS(r300);
1174
 
 
1175
 
    /* XXX workaround for a lockup in MSAA resolve on SWTCL chipsets, this
1176
 
     * function most probably doesn't handle type=NONE correctly */
1177
 
    if ((!r300->screen->caps.has_tcl && type == UTIL_BLITTER_ATTRIB_NONE) ||
1178
 
        type == UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW ||
1179
 
        num_instances > 1) {
1180
 
        util_blitter_draw_rectangle(blitter, vertex_elements_cso, get_vs,
1181
 
                                    x1, y1, x2, y2,
1182
 
                                    depth, num_instances, type, attrib);
1183
 
        return;
1184
 
    }
1185
 
 
1186
 
    if (r300->skip_rendering)
1187
 
        return;
1188
 
 
1189
 
    r300->context.bind_vertex_elements_state(&r300->context, vertex_elements_cso);
1190
 
    r300->context.bind_vs_state(&r300->context, get_vs(blitter));
1191
 
 
1192
 
    if (type == UTIL_BLITTER_ATTRIB_TEXCOORD_XY) {
1193
 
        r300->sprite_coord_enable = 1;
1194
 
        r300->is_point = true;
1195
 
    }
1196
 
 
1197
 
    r300_update_derived_state(r300);
1198
 
 
1199
 
    /* Mark some states we don't care about as non-dirty. */
1200
 
    r300->viewport_state.dirty = FALSE;
1201
 
 
1202
 
    if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
1203
 
        goto done;
1204
 
 
1205
 
    DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
1206
 
 
1207
 
    BEGIN_CS(dwords);
1208
 
    /* Set up GA. */
1209
 
    OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
1210
 
 
1211
 
    if (type == UTIL_BLITTER_ATTRIB_TEXCOORD_XY) {
1212
 
        /* Set up the GA to generate texcoords. */
1213
 
        OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |
1214
 
                   (R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));
1215
 
        OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
1216
 
        OUT_CS_32F(attrib->texcoord.x1);
1217
 
        OUT_CS_32F(attrib->texcoord.y2);
1218
 
        OUT_CS_32F(attrib->texcoord.x2);
1219
 
        OUT_CS_32F(attrib->texcoord.y1);
1220
 
    }
1221
 
 
1222
 
    /* Set up VAP controls. */
1223
 
    OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
1224
 
    OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);
1225
 
    OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
1226
 
    OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
1227
 
    OUT_CS(1);
1228
 
    OUT_CS(0);
1229
 
 
1230
 
    /* Draw. */
1231
 
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);
1232
 
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |
1233
 
           R300_VAP_VF_CNTL__PRIM_POINTS);
1234
 
 
1235
 
    OUT_CS_32F(x1 + width * 0.5f);
1236
 
    OUT_CS_32F(y1 + height * 0.5f);
1237
 
    OUT_CS_32F(depth);
1238
 
    OUT_CS_32F(1);
1239
 
 
1240
 
    if (vertex_size == 8) {
1241
 
        if (!attrib)
1242
 
            attrib = &zeros;
1243
 
        OUT_CS_TABLE(attrib->color, 4);
1244
 
    }
1245
 
    END_CS;
1246
 
 
1247
 
done:
1248
 
    /* Restore the state. */
1249
 
    r300_mark_atom_dirty(r300, &r300->rs_state);
1250
 
    r300_mark_atom_dirty(r300, &r300->viewport_state);
1251
 
 
1252
 
    r300->sprite_coord_enable = last_sprite_coord_enable;
1253
 
    r300->is_point = last_is_point;
1254
 
}
1255
 
 
1256
 
void r300_init_render_functions(struct r300_context *r300)
1257
 
{
1258
 
    /* Set draw functions based on presence of HW TCL. */
1259
 
    if (r300->screen->caps.has_tcl) {
1260
 
        r300->context.draw_vbo = r300_draw_vbo;
1261
 
    } else {
1262
 
        r300->context.draw_vbo = r300_swtcl_draw_vbo;
1263
 
    }
1264
 
 
1265
 
    /* Plug in the two-sided stencil reference value fallback if needed. */
1266
 
    if (!r300->screen->caps.is_r500)
1267
 
        r300_plug_in_stencil_ref_fallback(r300);
1268
 
}