~ubuntu-branches/ubuntu/natty/mesa/natty-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Robert Hooker, Robert Hooker, Christopher James Halse Rogers
  • Date: 2010-09-14 08:55:40 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100914085540-m4fpl0hdjlfd4jgz
Tags: 7.9~git20100909-0ubuntu1
[ Robert Hooker ]
* New upstream git snapshot up to commit 94118fe2d4b1e5 (LP: #631413)
* New features include ATI HD5xxx series support in r600, and a vastly
  improved glsl compiler.
* Remove pre-generated .pc's, use the ones generated at build time
  instead.
* Remove all references to mesa-utils now that its no longer shipped
  with the mesa source.
* Disable the experimental ARB_fragment_shader option by default on
  i915, it exposes incomplete functionality that breaks KDE compositing
  among other things. It can be enabled via driconf still. (LP: #628930).

[ Christopher James Halse Rogers ]
* debian/patches/04_osmesa_version.diff:
  - Refresh for new upstream
* Bugs fixed in this release:
  - Fixes severe rendering corruption in Unity on radeon (LP: #628727,
    LP: #596292, LP: #599741, LP: #630315, LP: #613694, LP: #599741).
  - Also fixes rendering in gnome-shell (LP: #578619).
  - Flickering in OpenGL apps on radeon (LP: #626943, LP: #610541).
  - Provides preliminary support for new intel chips (LP: #601052).
* debian/rules:
  - Update configure flags to match upstream reshuffling.
  - Explicitly remove gallium DRI drivers that we don't want to ship.
* Update debian/gbp.conf for this Maverick-specific packaging
* libegl1-mesa-dri-x11,kms: There are no longer separate kms or x11 drivers
  for EGL, libegl1-mesa-drivers now contains a single driver that provides
  both backends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
 
3
 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
3
4
 *
4
5
 * Permission is hereby granted, free of charge, to any person obtaining a
5
6
 * copy of this software and associated documentation files (the "Software"),
30
31
 
31
32
#include "util/u_format.h"
32
33
#include "util/u_memory.h"
 
34
#include "util/u_upload_mgr.h"
33
35
#include "util/u_prim.h"
34
36
 
35
37
#include "r300_cs.h"
36
38
#include "r300_context.h"
 
39
#include "r300_screen_buffer.h"
37
40
#include "r300_emit.h"
38
41
#include "r300_reg.h"
39
 
#include "r300_render.h"
40
42
#include "r300_state_derived.h"
41
43
 
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
 
 
48
 
uint32_t r300_translate_primitive(unsigned prim)
 
44
#include <limits.h>
 
45
 
 
46
#define IMMD_DWORDS 32
 
47
 
 
48
static uint32_t r300_translate_primitive(unsigned prim)
49
49
{
50
50
    switch (prim) {
51
51
        case PIPE_PRIM_POINTS:
118
118
    return color_control;
119
119
}
120
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. */
123
 
static boolean r300_reserve_cs_space(struct r300_context *r300,
124
 
                                     unsigned dwords)
125
 
{
126
 
    if (!r300->winsys->check_cs(r300->winsys, dwords)) {
 
121
boolean r500_index_bias_supported(struct r300_context *r300)
 
122
{
 
123
    return r300->screen->caps.is_r500 &&
 
124
           r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
 
125
}
 
126
 
 
127
void r500_emit_index_bias(struct r300_context *r300, int index_bias)
 
128
{
 
129
    CS_LOCALS(r300);
 
130
 
 
131
    BEGIN_CS(2);
 
132
    OUT_CS_REG(R500_VAP_INDEX_OFFSET,
 
133
               (index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
 
134
    END_CS;
 
135
}
 
136
 
 
137
/* This function splits the index bias value into two parts:
 
138
 * - buffer_offset: the value that can be safely added to buffer offsets
 
139
 *   in r300_emit_aos (it must yield a positive offset when added to
 
140
 *   a vertex buffer offset)
 
141
 * - index_offset: the value that must be manually subtracted from indices
 
142
 *   in an index buffer to achieve negative offsets. */
 
143
static void r300_split_index_bias(struct r300_context *r300, int index_bias,
 
144
                                  int *buffer_offset, int *index_offset)
 
145
{
 
146
    struct pipe_vertex_buffer *vb, *vbufs = r300->vertex_buffer;
 
147
    struct pipe_vertex_element *velem = r300->velems->velem;
 
148
    unsigned i, size;
 
149
    int max_neg_bias;
 
150
 
 
151
    if (index_bias < 0) {
 
152
        /* See how large index bias we may subtract. We must be careful
 
153
         * here because negative buffer offsets are not allowed
 
154
         * by the DRM API. */
 
155
        max_neg_bias = INT_MAX;
 
156
        for (i = 0; i < r300->velems->count; i++) {
 
157
            vb = &vbufs[velem[i].vertex_buffer_index];
 
158
            size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;
 
159
            max_neg_bias = MIN2(max_neg_bias, size);
 
160
        }
 
161
 
 
162
        /* Now set the minimum allowed value. */
 
163
        *buffer_offset = MAX2(-max_neg_bias, index_bias);
 
164
    } else {
 
165
        /* A positive index bias is OK. */
 
166
        *buffer_offset = index_bias;
 
167
    }
 
168
 
 
169
    *index_offset = index_bias - *buffer_offset;
 
170
}
 
171
 
 
172
enum r300_prepare_flags {
 
173
    PREP_FIRST_DRAW     = (1 << 0), /* call emit_dirty_state and friends? */
 
174
    PREP_VALIDATE_VBOS  = (1 << 1), /* validate VBOs? */
 
175
    PREP_EMIT_AOS       = (1 << 2), /* call emit_aos? */
 
176
    PREP_EMIT_AOS_SWTCL = (1 << 3), /* call emit_aos_swtcl? */
 
177
    PREP_INDEXED        = (1 << 4)  /* is this draw_elements? */
 
178
};
 
179
 
 
180
/**
 
181
 * Check if the requested number of dwords is available in the CS and
 
182
 * if not, flush. Then validate buffers and emit dirty state.
 
183
 * \param r300          The context.
 
184
 * \param flags         See r300_prepare_flags.
 
185
 * \param index_buffer  The index buffer to validate. The parameter may be NULL.
 
186
 * \param cs_dwords     The number of dwords to reserve in CS.
 
187
 * \param aos_offset    The offset passed to emit_aos.
 
188
 * \param index_bias    The index bias to emit.
 
189
 */
 
190
static void r300_prepare_for_rendering(struct r300_context *r300,
 
191
                                       enum r300_prepare_flags flags,
 
192
                                       struct pipe_resource *index_buffer,
 
193
                                       unsigned cs_dwords,
 
194
                                       int aos_offset,
 
195
                                       int index_bias)
 
196
{
 
197
    boolean flushed        = FALSE;
 
198
    boolean first_draw     = flags & PREP_FIRST_DRAW;
 
199
    boolean emit_aos       = flags & PREP_EMIT_AOS;
 
200
    boolean emit_aos_swtcl = flags & PREP_EMIT_AOS_SWTCL;
 
201
    boolean indexed        = flags & PREP_INDEXED;
 
202
    boolean hw_index_bias  = r500_index_bias_supported(r300);
 
203
 
 
204
    /* Add dirty state, index offset, and AOS. */
 
205
    if (first_draw) {
 
206
        cs_dwords += r300_get_num_dirty_dwords(r300);
 
207
 
 
208
        if (hw_index_bias)
 
209
            cs_dwords += 2; /* emit_index_offset */
 
210
 
 
211
        if (emit_aos)
 
212
            cs_dwords += 55; /* emit_aos */
 
213
 
 
214
        if (emit_aos_swtcl)
 
215
            cs_dwords += 7; /* emit_aos_swtcl */
 
216
    }
 
217
 
 
218
    cs_dwords += r300_get_num_cs_end_dwords(r300);
 
219
 
 
220
    /* Reserve requested CS space. */
 
221
    if (cs_dwords > (r300->cs->ndw - r300->cs->cdw)) {
127
222
        r300->context.flush(&r300->context, 0, NULL);
128
 
        return TRUE;
129
 
    }
130
 
    return FALSE;
 
223
        flushed = TRUE;
 
224
    }
 
225
 
 
226
    /* Validate buffers and emit dirty state if needed. */
 
227
    if (first_draw || flushed) {
 
228
        r300_emit_buffer_validate(r300, flags & PREP_VALIDATE_VBOS, index_buffer);
 
229
        r300_emit_dirty_state(r300);
 
230
        if (hw_index_bias) {
 
231
            if (r300->screen->caps.has_tcl)
 
232
                r500_emit_index_bias(r300, index_bias);
 
233
            else
 
234
                r500_emit_index_bias(r300, 0);
 
235
        }
 
236
 
 
237
        if (emit_aos)
 
238
            r300_emit_aos(r300, aos_offset, indexed);
 
239
 
 
240
        if (emit_aos_swtcl)
 
241
            r300_emit_aos_swtcl(r300, indexed);
 
242
    }
131
243
}
132
244
 
133
245
static boolean immd_is_good_idea(struct r300_context *r300,
134
 
                                      unsigned count)
 
246
                                 unsigned count)
135
247
{
136
 
    return count <= 4;
 
248
    struct pipe_vertex_element* velem;
 
249
    struct pipe_vertex_buffer* vbuf;
 
250
    boolean checked[PIPE_MAX_ATTRIBS] = {0};
 
251
    unsigned vertex_element_count = r300->velems->count;
 
252
    unsigned i, vbi;
 
253
 
 
254
    if (DBG_ON(r300, DBG_NO_IMMD)) {
 
255
        return FALSE;
 
256
    }
 
257
 
 
258
    if (r300->draw) {
 
259
        return FALSE;
 
260
    }
 
261
 
 
262
    if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
 
263
        return FALSE;
 
264
    }
 
265
 
 
266
    /* We shouldn't map buffers referenced by CS, busy buffers,
 
267
     * and ones placed in VRAM. */
 
268
    for (i = 0; i < vertex_element_count; i++) {
 
269
        velem = &r300->velems->velem[i];
 
270
        vbi = velem->vertex_buffer_index;
 
271
 
 
272
        if (!checked[vbi]) {
 
273
            vbuf = &r300->vertex_buffer[vbi];
 
274
 
 
275
            if (!(r300_buffer(vbuf->buffer)->domain & R300_DOMAIN_GTT)) {
 
276
                return FALSE;
 
277
            }
 
278
 
 
279
            if (r300_buffer_is_referenced(&r300->context,
 
280
                                          vbuf->buffer,
 
281
                                          R300_REF_CS | R300_REF_HW)) {
 
282
                /* It's a very bad idea to map it... */
 
283
                return FALSE;
 
284
            }
 
285
            checked[vbi] = TRUE;
 
286
        }
 
287
    }
 
288
    return TRUE;
137
289
}
138
290
 
 
291
/*****************************************************************************
 
292
 * The HWTCL draw functions.                                                 *
 
293
 ****************************************************************************/
 
294
 
139
295
static void r300_emit_draw_arrays_immediate(struct r300_context *r300,
140
296
                                            unsigned mode,
141
297
                                            unsigned start,
143
299
{
144
300
    struct pipe_vertex_element* velem;
145
301
    struct pipe_vertex_buffer* vbuf;
146
 
    unsigned vertex_element_count = r300->vertex_element_count;
147
 
    unsigned i, v, vbi, dw, elem_offset, dwords;
 
302
    unsigned vertex_element_count = r300->velems->count;
 
303
    unsigned i, v, vbi, dwords;
148
304
 
149
305
    /* 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];
 
306
    unsigned vertex_size = r300->velems->vertex_size_dwords;
154
307
 
155
308
    /* Size of the vertex element, in dwords. */
156
309
    unsigned size[PIPE_MAX_ATTRIBS];
157
310
 
158
311
    /* Stride to the same attrib in the next vertex in the vertex buffer,
159
312
     * in dwords. */
160
 
    unsigned stride[PIPE_MAX_ATTRIBS] = {0};
 
313
    unsigned stride[PIPE_MAX_ATTRIBS];
161
314
 
162
315
    /* Mapped vertex buffers. */
163
 
    uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
 
316
    uint32_t* map[PIPE_MAX_ATTRIBS];
 
317
    uint32_t* mapelem[PIPE_MAX_ATTRIBS];
 
318
    struct pipe_transfer* transfer[PIPE_MAX_ATTRIBS] = {0};
164
319
 
165
320
    CS_LOCALS(r300);
166
321
 
167
322
    /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
168
323
    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];
 
324
        velem = &r300->velems->velem[i];
 
325
        size[i] = r300->velems->hw_format_size[i] / 4;
173
326
        vbi = velem->vertex_buffer_index;
 
327
        vbuf = &r300->vertex_buffer[vbi];
 
328
        stride[i] = vbuf->stride / 4;
174
329
 
175
330
        /* Map the buffer. */
176
 
        if (!map[vbi]) {
177
 
            vbuf = &r300->vertex_buffer[vbi];
178
 
            map[vbi] = (uint32_t*)pipe_buffer_map(r300->context.screen,
 
331
        if (!transfer[vbi]) {
 
332
            map[vbi] = (uint32_t*)pipe_buffer_map(&r300->context,
179
333
                                                  vbuf->buffer,
180
 
                                                  PIPE_BUFFER_USAGE_CPU_READ);
181
 
            map[vbi] += vbuf->buffer_offset / 4;
182
 
            stride[vbi] = vbuf->stride / 4;
 
334
                                                  PIPE_TRANSFER_READ,
 
335
                                                  &transfer[vbi]);
 
336
            map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * start;
183
337
        }
 
338
        mapelem[i] = map[vbi] + (velem->src_offset / 4);
184
339
    }
185
340
 
186
 
    dwords = 10 + count * vertex_size;
 
341
    dwords = 9 + count * vertex_size;
187
342
 
188
 
    r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + dwords);
189
 
    r300_emit_buffer_validate(r300, FALSE, 0);
190
 
    r300_emit_dirty_state(r300);
 
343
    r300_prepare_for_rendering(r300, PREP_FIRST_DRAW, NULL, dwords, 0, 0);
191
344
 
192
345
    BEGIN_CS(dwords);
193
346
    OUT_CS_REG(R300_GA_COLOR_CONTROL,
194
347
            r300_provoking_vertex_fixes(r300, mode));
195
348
    OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
196
 
    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
197
 
    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
 
349
    OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
 
350
    OUT_CS(count - 1);
 
351
    OUT_CS(0);
198
352
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size);
199
353
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) |
200
354
            r300_translate_primitive(mode));
202
356
    /* Emit vertices. */
203
357
    for (v = 0; v < count; v++) {
204
358
        for (i = 0; i < vertex_element_count; i++) {
205
 
            velem = &r300->vertex_element[i];
206
 
            vbi = velem->vertex_buffer_index;
207
 
            elem_offset = offset[i] + stride[vbi] * (v + start);
208
 
 
209
 
            for (dw = 0; dw < size[i]; dw++) {
210
 
                OUT_CS(map[vbi][elem_offset + dw]);
211
 
            }
 
359
            OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);
212
360
        }
213
361
    }
214
362
    END_CS;
215
363
 
216
364
    /* Unmap buffers. */
217
365
    for (i = 0; i < vertex_element_count; i++) {
218
 
        vbi = r300->vertex_element[i].vertex_buffer_index;
 
366
        vbi = r300->velems->velem[i].vertex_buffer_index;
219
367
 
220
 
        if (map[vbi]) {
 
368
        if (transfer[vbi]) {
221
369
            vbuf = &r300->vertex_buffer[vbi];
222
 
            pipe_buffer_unmap(r300->context.screen, vbuf->buffer);
223
 
            map[vbi] = NULL;
 
370
            pipe_buffer_unmap(&r300->context, vbuf->buffer, transfer[vbi]);
 
371
            transfer[vbi] = NULL;
224
372
        }
225
373
    }
226
374
}
229
377
                                  unsigned mode,
230
378
                                  unsigned count)
231
379
{
232
 
#if defined(ENABLE_ALT_NUM_VERTS)
233
380
    boolean alt_num_verts = count > 65535;
234
 
#else
235
 
    boolean alt_num_verts = FALSE;
236
 
#endif
237
381
    CS_LOCALS(r300);
238
382
 
 
383
    if (count >= (1 << 24)) {
 
384
        fprintf(stderr, "r300: Got a huge number of vertices: %i, "
 
385
                "refusing to render.\n", count);
 
386
        return;
 
387
    }
 
388
 
 
389
    BEGIN_CS(7 + (alt_num_verts ? 2 : 0));
239
390
    if (alt_num_verts) {
240
 
        assert(count < (1 << 24));
241
 
        BEGIN_CS(10);
242
391
        OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
243
 
    } else {
244
 
        BEGIN_CS(8);
245
392
    }
246
393
    OUT_CS_REG(R300_GA_COLOR_CONTROL,
247
394
            r300_provoking_vertex_fixes(r300, mode));
248
 
    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
249
 
    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
 
395
    OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
 
396
    OUT_CS(count - 1);
 
397
    OUT_CS(0);
250
398
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
251
399
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
252
400
           r300_translate_primitive(mode) |
255
403
}
256
404
 
257
405
static void r300_emit_draw_elements(struct r300_context *r300,
258
 
                                    struct pipe_buffer* indexBuffer,
 
406
                                    struct pipe_resource* indexBuffer,
259
407
                                    unsigned indexSize,
260
408
                                    unsigned minIndex,
261
409
                                    unsigned maxIndex,
265
413
{
266
414
    uint32_t count_dwords;
267
415
    uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
268
 
#if defined(ENABLE_ALT_NUM_VERTS)
269
416
    boolean alt_num_verts = count > 65535;
270
 
#else
271
 
    boolean alt_num_verts = FALSE;
272
 
#endif
273
417
    CS_LOCALS(r300);
274
418
 
275
 
    assert((start * indexSize)  % 4 == 0);
276
 
    assert(count < (1 << 24));
 
419
    if (count >= (1 << 24)) {
 
420
        fprintf(stderr, "r300: Got a huge number of vertices: %i, "
 
421
                "refusing to render.\n", count);
 
422
        return;
 
423
    }
 
424
 
 
425
    maxIndex = MIN2(maxIndex, r300->vertex_buffer_max_index);
277
426
 
278
427
    DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, min %u max %u\n",
279
428
        count, minIndex, maxIndex);
280
429
 
281
 
    maxIndex = MIN2(maxIndex, ((1 << 24) - 1));
282
 
 
 
430
    BEGIN_CS(13 + (alt_num_verts ? 2 : 0));
283
431
    if (alt_num_verts) {
284
 
        BEGIN_CS(16);
285
432
        OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
286
 
    } else {
287
 
        BEGIN_CS(14);
288
433
    }
289
434
    OUT_CS_REG(R300_GA_COLOR_CONTROL,
290
435
            r300_provoking_vertex_fixes(r300, mode));
291
 
    OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, minIndex);
292
 
    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex);
 
436
    OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
 
437
    OUT_CS(maxIndex);
 
438
    OUT_CS(minIndex);
293
439
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
294
440
    if (indexSize == 4) {
295
441
        count_dwords = count;
313
459
    OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
314
460
           (0 << R300_INDX_BUFFER_SKIP_SHIFT));
315
461
    OUT_CS(offset_dwords << 2);
316
 
    OUT_CS_RELOC(indexBuffer, count_dwords,
317
 
        RADEON_GEM_DOMAIN_GTT, 0, 0);
 
462
    OUT_CS_BUF_RELOC(indexBuffer, count_dwords,
 
463
                     r300_buffer(indexBuffer)->domain, 0);
318
464
 
319
465
    END_CS;
320
466
}
321
467
 
322
 
static void r300_shorten_ubyte_elts(struct r300_context* r300,
323
 
                                    struct pipe_buffer** elts,
324
 
                                    unsigned count)
325
 
{
326
 
    struct pipe_screen* screen = r300->context.screen;
327
 
    struct pipe_buffer* new_elts;
328
 
    unsigned char *in_map;
329
 
    unsigned short *out_map;
330
 
    unsigned i;
331
 
 
332
 
    new_elts = screen->buffer_create(screen, 32,
333
 
                                     PIPE_BUFFER_USAGE_INDEX |
334
 
                                     PIPE_BUFFER_USAGE_CPU_WRITE |
335
 
                                     PIPE_BUFFER_USAGE_GPU_READ,
336
 
                                     2 * count);
337
 
 
338
 
    in_map = pipe_buffer_map(screen, *elts, PIPE_BUFFER_USAGE_CPU_READ);
339
 
    out_map = pipe_buffer_map(screen, new_elts, PIPE_BUFFER_USAGE_CPU_WRITE);
340
 
 
341
 
    for (i = 0; i < count; i++) {
342
 
        *out_map = (unsigned short)*in_map;
343
 
        in_map++;
344
 
        out_map++;
345
 
    }
346
 
 
347
 
    pipe_buffer_unmap(screen, *elts);
348
 
    pipe_buffer_unmap(screen, new_elts);
349
 
 
350
 
    *elts = new_elts;
351
 
}
352
 
 
353
468
/* This is the fast-path drawing & emission for HW TCL. */
354
 
void r300_draw_range_elements(struct pipe_context* pipe,
355
 
                              struct pipe_buffer* indexBuffer,
356
 
                              unsigned indexSize,
357
 
                              unsigned minIndex,
358
 
                              unsigned maxIndex,
359
 
                              unsigned mode,
360
 
                              unsigned start,
361
 
                              unsigned count)
 
469
static void r300_draw_range_elements(struct pipe_context* pipe,
 
470
                                     struct pipe_resource* indexBuffer,
 
471
                                     unsigned indexSize,
 
472
                                     int indexBias,
 
473
                                     unsigned minIndex,
 
474
                                     unsigned maxIndex,
 
475
                                     unsigned mode,
 
476
                                     unsigned start,
 
477
                                     unsigned count)
362
478
{
363
479
    struct r300_context* r300 = r300_context(pipe);
364
 
    struct pipe_buffer* orgIndexBuffer = indexBuffer;
365
 
#if defined(ENABLE_ALT_NUM_VERTS)
366
 
    boolean alt_num_verts = r300_screen(pipe->screen)->caps->is_r500 &&
367
 
                            count > 65536;
368
 
#else
369
 
    boolean alt_num_verts = FALSE;
370
 
#endif
 
480
    struct pipe_resource* orgIndexBuffer = indexBuffer;
 
481
    boolean alt_num_verts = r300->screen->caps.is_r500 &&
 
482
                            count > 65536 &&
 
483
                            r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
371
484
    unsigned short_count;
 
485
    int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
 
486
    boolean translate = FALSE;
 
487
    unsigned new_offset;
 
488
 
 
489
    if (r300->skip_rendering) {
 
490
        return;
 
491
    }
372
492
 
373
493
    if (!u_trim_pipe_prim(mode, &count)) {
374
494
        return;
375
495
    }
376
496
 
377
 
    if (indexSize == 1) {
378
 
        r300_shorten_ubyte_elts(r300, &indexBuffer, count);
379
 
        indexSize = 2;
380
 
    }
 
497
    /* Index buffer range checking. */
 
498
    if ((start + count) * indexSize > indexBuffer->width0) {
 
499
        fprintf(stderr, "r300: Invalid index buffer range. Skipping rendering.\n");
 
500
        return;
 
501
    }
 
502
 
 
503
    /* Set up fallback for incompatible vertex layout if needed. */
 
504
    if (r300->incompatible_vb_layout || r300->velems->incompatible_layout) {
 
505
        r300_begin_vertex_translate(r300);
 
506
        translate = TRUE;
 
507
    }
 
508
 
 
509
    if (indexBias && !r500_index_bias_supported(r300)) {
 
510
        r300_split_index_bias(r300, indexBias, &buffer_offset, &index_offset);
 
511
    }
 
512
 
 
513
    r300_translate_index_buffer(r300, &indexBuffer, &indexSize, index_offset,
 
514
                                &start, count);
381
515
 
382
516
    r300_update_derived_state(r300);
 
517
    r300_upload_index_buffer(r300, &indexBuffer, indexSize, start, count, &new_offset);
383
518
 
384
 
    /* 128 dwords for emit_aos and emit_draw_elements */
385
 
    r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 128);
386
 
    r300_emit_buffer_validate(r300, TRUE, indexBuffer);
387
 
    r300_emit_dirty_state(r300);
388
 
    r300_emit_aos(r300, 0);
 
519
    start = new_offset;
 
520
    /* 15 dwords for emit_draw_elements */
 
521
    r300_prepare_for_rendering(r300,
 
522
        PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
 
523
        indexBuffer, 15, buffer_offset, indexBias);
389
524
 
390
525
    if (alt_num_verts || count <= 65535) {
391
 
        r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
392
 
                                maxIndex, mode, start, count);
 
526
        r300_emit_draw_elements(r300, indexBuffer, indexSize,
 
527
                                minIndex, maxIndex, mode, start, count);
393
528
    } else {
394
529
        do {
395
530
            short_count = MIN2(count, 65534);
396
 
            r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
397
 
                                    maxIndex, mode, start, short_count);
 
531
            r300_emit_draw_elements(r300, indexBuffer, indexSize,
 
532
                                     minIndex, maxIndex,
 
533
                                     mode, start, short_count);
398
534
 
399
535
            start += short_count;
400
536
            count -= short_count;
401
537
 
402
 
            /* 16 spare dwords are enough for emit_draw_elements. */
403
 
            if (count && r300_reserve_cs_space(r300, 16)) {
404
 
                r300_emit_buffer_validate(r300, TRUE, indexBuffer);
405
 
                r300_emit_dirty_state(r300);
406
 
                r300_emit_aos(r300, 0);
 
538
            /* 15 dwords for emit_draw_elements */
 
539
            if (count) {
 
540
                r300_prepare_for_rendering(r300,
 
541
                    PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
 
542
                    indexBuffer, 15, buffer_offset, indexBias);
407
543
            }
408
544
        } while (count);
409
545
    }
410
546
 
411
547
    if (indexBuffer != orgIndexBuffer) {
412
 
        pipe->screen->buffer_destroy(indexBuffer);
413
 
    }
414
 
}
415
 
 
416
 
/* Simple helpers for context setup. Should probably be moved to util. */
417
 
void r300_draw_elements(struct pipe_context* pipe,
418
 
                        struct pipe_buffer* indexBuffer,
419
 
                        unsigned indexSize, unsigned mode,
420
 
                        unsigned start, unsigned count)
421
 
{
422
 
    struct r300_context *r300 = r300_context(pipe);
423
 
 
424
 
    pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0,
425
 
                              r300->vertex_buffer_max_index,
426
 
                              mode, start, count);
427
 
}
428
 
 
429
 
void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
430
 
                      unsigned start, unsigned count)
 
548
        pipe_resource_reference( &indexBuffer, NULL );
 
549
    }
 
550
 
 
551
    if (translate) {
 
552
        r300_end_vertex_translate(r300);
 
553
    }
 
554
}
 
555
 
 
556
static void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
 
557
                             unsigned start, unsigned count)
431
558
{
432
559
    struct r300_context* r300 = r300_context(pipe);
433
 
#if defined(ENABLE_ALT_NUM_VERTS)
434
 
    boolean alt_num_verts = r300_screen(pipe->screen)->caps->is_r500 &&
435
 
                            count > 65536;
436
 
#else
437
 
    boolean alt_num_verts = FALSE;
438
 
#endif
 
560
    boolean alt_num_verts = r300->screen->caps.is_r500 &&
 
561
                            count > 65536 &&
 
562
                            r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
439
563
    unsigned short_count;
 
564
    boolean translate = FALSE;
 
565
 
 
566
    if (r300->skip_rendering) {
 
567
        return;
 
568
    }
440
569
 
441
570
    if (!u_trim_pipe_prim(mode, &count)) {
442
571
        return;
443
572
    }
444
573
 
 
574
    /* Set up fallback for incompatible vertex layout if needed. */
 
575
    if (r300->incompatible_vb_layout || r300->velems->incompatible_layout) {
 
576
        r300_begin_vertex_translate(r300);
 
577
        translate = TRUE;
 
578
    }
 
579
 
445
580
    r300_update_derived_state(r300);
446
581
 
447
582
    if (immd_is_good_idea(r300, count)) {
448
583
        r300_emit_draw_arrays_immediate(r300, mode, start, count);
449
584
    } else {
450
 
        /* Make sure there are at least 128 spare dwords in the command buffer.
451
 
         * (most of it being consumed by emit_aos) */
452
 
        r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 128);
453
 
        r300_emit_buffer_validate(r300, TRUE, 0);
454
 
        r300_emit_dirty_state(r300);
 
585
        /* 9 spare dwords for emit_draw_arrays. */
 
586
        r300_prepare_for_rendering(r300, PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS,
 
587
                               NULL, 9, start, 0);
455
588
 
456
589
        if (alt_num_verts || count <= 65535) {
457
 
            r300_emit_aos(r300, start);
458
590
            r300_emit_draw_arrays(r300, mode, count);
459
591
        } else {
460
592
            do {
461
593
                short_count = MIN2(count, 65535);
462
 
                r300_emit_aos(r300, start);
463
594
                r300_emit_draw_arrays(r300, mode, short_count);
464
595
 
465
596
                start += short_count;
466
597
                count -= short_count;
467
598
 
468
 
                /* Again, we emit both AOS and draw_arrays so there should be
469
 
                 * at least 128 spare dwords. */
470
 
                if (count && r300_reserve_cs_space(r300, 128)) {
471
 
                    r300_emit_buffer_validate(r300, TRUE, 0);
472
 
                    r300_emit_dirty_state(r300);
 
599
                /* 9 spare dwords for emit_draw_arrays. */
 
600
                if (count) {
 
601
                    r300_prepare_for_rendering(r300,
 
602
                        PREP_VALIDATE_VBOS | PREP_EMIT_AOS, NULL, 9,
 
603
                        start, 0);
473
604
                }
474
605
            } while (count);
475
606
        }
 
607
        u_upload_flush(r300->upload_vb);
 
608
    }
 
609
 
 
610
    if (translate) {
 
611
        r300_end_vertex_translate(r300);
 
612
    }
 
613
}
 
614
 
 
615
static void r300_draw_vbo(struct pipe_context* pipe,
 
616
                          const struct pipe_draw_info *info)
 
617
{
 
618
    struct r300_context* r300 = r300_context(pipe);
 
619
 
 
620
    if (!r300->velems->count || !r300->vertex_buffer_count)
 
621
            return;
 
622
 
 
623
    if (info->indexed && r300->index_buffer.buffer) {
 
624
        unsigned offset;
 
625
 
 
626
        assert(r300->index_buffer.offset % r300->index_buffer.index_size == 0);
 
627
        offset = r300->index_buffer.offset / r300->index_buffer.index_size;
 
628
 
 
629
        r300_draw_range_elements(pipe,
 
630
                                 r300->index_buffer.buffer,
 
631
                                 r300->index_buffer.index_size,
 
632
                                 info->index_bias,
 
633
                                 info->min_index,
 
634
                                 info->max_index,
 
635
                                 info->mode,
 
636
                                 info->start + offset,
 
637
                                 info->count);
 
638
    }
 
639
    else {
 
640
        r300_draw_arrays(pipe,
 
641
                         info->mode,
 
642
                         info->start,
 
643
                         info->count);
476
644
    }
477
645
}
478
646
 
481
649
 * keep these functions separated so that they are easier to locate. ~C.    *
482
650
 ***************************************************************************/
483
651
 
484
 
/* SW TCL arrays, using Draw. */
485
 
void r300_swtcl_draw_arrays(struct pipe_context* pipe,
486
 
                               unsigned mode,
487
 
                               unsigned start,
488
 
                               unsigned count)
489
 
{
490
 
    struct r300_context* r300 = r300_context(pipe);
491
 
    int i;
492
 
 
493
 
    if (!u_trim_pipe_prim(mode, &count)) {
494
 
        return;
495
 
    }
496
 
 
497
 
    for (i = 0; i < r300->vertex_buffer_count; i++) {
498
 
        void* buf = pipe_buffer_map(pipe->screen,
499
 
                                    r300->vertex_buffer[i].buffer,
500
 
                                    PIPE_BUFFER_USAGE_CPU_READ);
501
 
        draw_set_mapped_vertex_buffer(r300->draw, i, buf);
502
 
    }
503
 
 
504
 
    draw_set_mapped_element_buffer(r300->draw, 0, NULL);
505
 
 
506
 
    draw_set_mapped_constant_buffer(r300->draw,
507
 
                                    PIPE_SHADER_VERTEX,
508
 
                                    0,
509
 
                                    r300->shader_constants[PIPE_SHADER_VERTEX].constants,
510
 
                                    r300->shader_constants[PIPE_SHADER_VERTEX].count *
511
 
                (sizeof(float) * 4));
512
 
 
513
 
    draw_arrays(r300->draw, mode, start, count);
514
 
 
515
 
    for (i = 0; i < r300->vertex_buffer_count; i++) {
516
 
        pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
517
 
        draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
518
 
    }
519
 
}
520
 
 
521
652
/* SW TCL elements, using Draw. */
522
 
void r300_swtcl_draw_range_elements(struct pipe_context* pipe,
523
 
                                       struct pipe_buffer* indexBuffer,
524
 
                                       unsigned indexSize,
525
 
                                       unsigned minIndex,
526
 
                                       unsigned maxIndex,
527
 
                                       unsigned mode,
528
 
                                       unsigned start,
529
 
                                       unsigned count)
 
653
static void r300_swtcl_draw_vbo(struct pipe_context* pipe,
 
654
                                const struct pipe_draw_info *info)
530
655
{
531
656
    struct r300_context* r300 = r300_context(pipe);
 
657
    struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
 
658
    struct pipe_transfer *ib_transfer = NULL;
 
659
    unsigned count = info->count;
532
660
    int i;
533
 
    void* indices;
534
 
 
535
 
    if (!u_trim_pipe_prim(mode, &count)) {
536
 
        return;
537
 
    }
538
 
 
539
 
    for (i = 0; i < r300->vertex_buffer_count; i++) {
540
 
        void* buf = pipe_buffer_map(pipe->screen,
541
 
                                    r300->vertex_buffer[i].buffer,
542
 
                                    PIPE_BUFFER_USAGE_CPU_READ);
543
 
        draw_set_mapped_vertex_buffer(r300->draw, i, buf);
544
 
    }
545
 
 
546
 
    indices = pipe_buffer_map(pipe->screen, indexBuffer,
547
 
                              PIPE_BUFFER_USAGE_CPU_READ);
548
 
    draw_set_mapped_element_buffer_range(r300->draw, indexSize,
549
 
                                         minIndex, maxIndex, indices);
550
 
 
551
 
    draw_set_mapped_constant_buffer(r300->draw,
552
 
                                    PIPE_SHADER_VERTEX,
553
 
                                    0,
554
 
            r300->shader_constants[PIPE_SHADER_VERTEX].constants,
555
 
            r300->shader_constants[PIPE_SHADER_VERTEX].count *
556
 
                (sizeof(float) * 4));
557
 
 
558
 
    draw_arrays(r300->draw, mode, start, count);
559
 
 
560
 
    for (i = 0; i < r300->vertex_buffer_count; i++) {
561
 
        pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
562
 
        draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
563
 
    }
564
 
 
565
 
    pipe_buffer_unmap(pipe->screen, indexBuffer);
566
 
    draw_set_mapped_element_buffer_range(r300->draw, 0, start,
567
 
                                         start + count - 1, NULL);
 
661
    void* indices = NULL;
 
662
 
 
663
    if (r300->skip_rendering) {
 
664
        return;
 
665
    }
 
666
 
 
667
    if (!u_trim_pipe_prim(info->mode, &count)) {
 
668
        return;
 
669
    }
 
670
 
 
671
    r300_update_derived_state(r300);
 
672
 
 
673
    for (i = 0; i < r300->vertex_buffer_count; i++) {
 
674
        if (r300->vertex_buffer[i].buffer) {
 
675
            void *buf = pipe_buffer_map(pipe,
 
676
                                  r300->vertex_buffer[i].buffer,
 
677
                                  PIPE_TRANSFER_READ,
 
678
                                  &vb_transfer[i]);
 
679
            draw_set_mapped_vertex_buffer(r300->draw, i, buf);
 
680
        }
 
681
    }
 
682
 
 
683
    if (info->indexed && r300->index_buffer.buffer) {
 
684
        indices = pipe_buffer_map(pipe, r300->index_buffer.buffer,
 
685
                                  PIPE_TRANSFER_READ, &ib_transfer);
 
686
    }
 
687
 
 
688
    draw_set_mapped_index_buffer(r300->draw, indices);
 
689
 
 
690
    draw_vbo(r300->draw, info);
 
691
 
 
692
    /* XXX Not sure whether this is the best fix.
 
693
     * It prevents CS from being rejected and weird assertion failures. */
 
694
    draw_flush(r300->draw);
 
695
 
 
696
    for (i = 0; i < r300->vertex_buffer_count; i++) {
 
697
        if (r300->vertex_buffer[i].buffer) {
 
698
            pipe_buffer_unmap(pipe, r300->vertex_buffer[i].buffer,
 
699
                              vb_transfer[i]);
 
700
            draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
 
701
        }
 
702
    }
 
703
 
 
704
    if (ib_transfer) {
 
705
        pipe_buffer_unmap(pipe, r300->index_buffer.buffer, ib_transfer);
 
706
        draw_set_mapped_index_buffer(r300->draw, NULL);
 
707
    }
568
708
}
569
709
 
570
710
/* Object for rendering using Draw. */
581
721
    unsigned hwprim;
582
722
 
583
723
    /* VBO */
584
 
    struct pipe_buffer* vbo;
585
 
    size_t vbo_size;
586
724
    size_t vbo_offset;
587
725
    size_t vbo_max_used;
588
726
    void * vbo_ptr;
 
727
 
 
728
    struct pipe_transfer *vbo_transfer;
589
729
};
590
730
 
591
731
static INLINE struct r300_render*
600
740
    struct r300_render* r300render = r300_render(render);
601
741
    struct r300_context* r300 = r300render->r300;
602
742
 
603
 
    r300_update_derived_state(r300);
604
 
 
605
743
    return &r300->vertex_info;
606
744
}
607
745
 
614
752
    struct pipe_screen* screen = r300->context.screen;
615
753
    size_t size = (size_t)vertex_size * (size_t)count;
616
754
 
617
 
    if (size + r300render->vbo_offset > r300render->vbo_size)
 
755
    if (size + r300render->vbo_offset > r300->draw_vbo_size)
618
756
    {
619
 
        pipe_buffer_reference(&r300->vbo, NULL);
620
 
        r300render->vbo = pipe_buffer_create(screen,
621
 
                                             64,
622
 
                                             PIPE_BUFFER_USAGE_VERTEX,
623
 
                                             R300_MAX_VBO_SIZE);
 
757
        pipe_resource_reference(&r300->vbo, NULL);
 
758
        r300->vbo = pipe_buffer_create(screen,
 
759
                                       PIPE_BIND_VERTEX_BUFFER,
 
760
                                       R300_MAX_DRAW_VBO_SIZE);
624
761
        r300render->vbo_offset = 0;
625
 
        r300render->vbo_size = R300_MAX_VBO_SIZE;
 
762
        r300->draw_vbo_size = R300_MAX_DRAW_VBO_SIZE;
626
763
    }
627
764
 
628
765
    r300render->vertex_size = vertex_size;
629
 
    r300->vbo = r300render->vbo;
630
766
    r300->vbo_offset = r300render->vbo_offset;
631
767
 
632
 
    return (r300render->vbo) ? TRUE : FALSE;
 
768
    return (r300->vbo) ? TRUE : FALSE;
633
769
}
634
770
 
635
771
static void* r300_render_map_vertices(struct vbuf_render* render)
636
772
{
637
773
    struct r300_render* r300render = r300_render(render);
638
 
    struct pipe_screen* screen = r300render->r300->context.screen;
639
 
 
640
 
    r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
641
 
                                          PIPE_BUFFER_USAGE_CPU_WRITE);
 
774
    struct r300_context* r300 = r300render->r300;
 
775
 
 
776
    assert(!r300render->vbo_transfer);
 
777
 
 
778
    r300render->vbo_ptr = pipe_buffer_map(&r300render->r300->context,
 
779
                                          r300->vbo,
 
780
                                          PIPE_TRANSFER_WRITE,
 
781
                                          &r300render->vbo_transfer);
 
782
 
 
783
    assert(r300render->vbo_ptr);
642
784
 
643
785
    return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset);
644
786
}
648
790
                                             ushort max)
649
791
{
650
792
    struct r300_render* r300render = r300_render(render);
651
 
    struct pipe_screen* screen = r300render->r300->context.screen;
652
 
    CS_LOCALS(r300render->r300);
653
 
    BEGIN_CS(2);
654
 
    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
655
 
    END_CS;
 
793
    struct pipe_context* context = &r300render->r300->context;
 
794
    struct r300_context* r300 = r300render->r300;
 
795
 
 
796
    assert(r300render->vbo_transfer);
656
797
 
657
798
    r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
658
799
                                    r300render->vertex_size * (max + 1));
659
 
    pipe_buffer_unmap(screen, r300render->vbo);
 
800
    pipe_buffer_unmap(context, r300->vbo, r300render->vbo_transfer);
 
801
 
 
802
    r300render->vbo_transfer = NULL;
660
803
}
661
804
 
662
805
static void r300_render_release_vertices(struct vbuf_render* render)
679
822
}
680
823
 
681
824
static void r300_render_draw_arrays(struct vbuf_render* render,
682
 
                                          unsigned start,
683
 
                                          unsigned count)
 
825
                                    unsigned start,
 
826
                                    unsigned count)
684
827
{
685
828
    struct r300_render* r300render = r300_render(render);
686
829
    struct r300_context* r300 = r300render->r300;
 
830
    uint8_t* ptr;
 
831
    unsigned i;
 
832
    unsigned dwords = 6;
687
833
 
688
834
    CS_LOCALS(r300);
689
 
 
690
 
    r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 2);
691
 
    r300_emit_dirty_state(r300);
692
 
 
693
 
    DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
694
 
 
695
 
    BEGIN_CS(2);
 
835
    (void) i; (void) ptr;
 
836
 
 
837
    r300_prepare_for_rendering(r300, PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL,
 
838
                               NULL, dwords, 0, 0);
 
839
 
 
840
    DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
 
841
 
 
842
    /* Uncomment to dump all VBOs rendered through this interface.
 
843
     * Slow and noisy!
 
844
    ptr = pipe_buffer_map(&r300render->r300->context,
 
845
                          r300render->vbo, PIPE_TRANSFER_READ,
 
846
                          &r300render->vbo_transfer);
 
847
 
 
848
    for (i = 0; i < count; i++) {
 
849
        printf("r300: Vertex %d\n", i);
 
850
        draw_dump_emitted_vertex(&r300->vertex_info, ptr);
 
851
        ptr += r300->vertex_info.size * 4;
 
852
        printf("\n");
 
853
    }
 
854
 
 
855
    pipe_buffer_unmap(&r300render->r300->context, r300render->vbo,
 
856
        r300render->vbo_transfer);
 
857
    */
 
858
 
 
859
    BEGIN_CS(dwords);
 
860
    OUT_CS_REG(R300_GA_COLOR_CONTROL,
 
861
            r300_provoking_vertex_fixes(r300, r300render->prim));
 
862
    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
696
863
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
697
864
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
698
865
           r300render->hwprim);
699
866
    END_CS;
700
867
}
701
868
 
702
 
static void r300_render_draw(struct vbuf_render* render,
703
 
                                   const ushort* indices,
704
 
                                   uint count)
 
869
static void r300_render_draw_elements(struct vbuf_render* render,
 
870
                                      const ushort* indices,
 
871
                                      uint count)
705
872
{
706
873
    struct r300_render* r300render = r300_render(render);
707
874
    struct r300_context* r300 = r300render->r300;
708
875
    int i;
709
 
    unsigned dwords = 2 + (count+1)/2;
 
876
    unsigned end_cs_dwords;
 
877
    unsigned max_index = (r300->draw_vbo_size - r300render->vbo_offset) /
 
878
                         (r300render->r300->vertex_info.size * 4) - 1;
 
879
    unsigned short_count;
 
880
    unsigned free_dwords;
710
881
 
711
882
    CS_LOCALS(r300);
712
 
 
713
 
    r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + dwords);
714
 
    r300_emit_dirty_state(r300);
715
 
 
716
 
    BEGIN_CS(dwords);
717
 
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
718
 
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
719
 
           r300render->hwprim);
720
 
    for (i = 0; i < count-1; i += 2) {
721
 
        OUT_CS(indices[i+1] << 16 | indices[i]);
722
 
    }
723
 
    if (count % 2) {
724
 
        OUT_CS(indices[count-1]);
725
 
    }
726
 
    END_CS;
 
883
    DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
 
884
 
 
885
    /* Reserve at least 256 dwords.
 
886
     *
 
887
     * Below we manage the CS space manually because there may be more
 
888
     * indices than it can fit in CS. */
 
889
    r300_prepare_for_rendering(r300,
 
890
        PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
 
891
        NULL, 256, 0, 0);
 
892
    end_cs_dwords = r300_get_num_cs_end_dwords(r300);
 
893
 
 
894
    while (count) {
 
895
        free_dwords = r300->cs->ndw - r300->cs->cdw;
 
896
 
 
897
        short_count = MIN2(count, (free_dwords - end_cs_dwords - 6) * 2);
 
898
 
 
899
        BEGIN_CS(6 + (short_count+1)/2);
 
900
        OUT_CS_REG(R300_GA_COLOR_CONTROL,
 
901
                r300_provoking_vertex_fixes(r300, r300render->prim));
 
902
        OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);
 
903
        OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (short_count+1)/2);
 
904
        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (short_count << 16) |
 
905
               r300render->hwprim);
 
906
        for (i = 0; i < short_count-1; i += 2) {
 
907
            OUT_CS(indices[i+1] << 16 | indices[i]);
 
908
        }
 
909
        if (short_count % 2) {
 
910
            OUT_CS(indices[short_count-1]);
 
911
        }
 
912
        END_CS;
 
913
 
 
914
        /* OK now subtract the emitted indices and see if we need to emit
 
915
         * another draw packet. */
 
916
        indices += short_count;
 
917
        count -= short_count;
 
918
 
 
919
        if (count) {
 
920
            r300_prepare_for_rendering(r300,
 
921
                PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
 
922
                NULL, 256, 0, 0);
 
923
            end_cs_dwords = r300_get_num_cs_end_dwords(r300);
 
924
        }
 
925
    }
727
926
}
728
927
 
729
928
static void r300_render_destroy(struct vbuf_render* render)
746
945
    r300render->base.map_vertices = r300_render_map_vertices;
747
946
    r300render->base.unmap_vertices = r300_render_unmap_vertices;
748
947
    r300render->base.set_primitive = r300_render_set_primitive;
749
 
    r300render->base.draw = r300_render_draw;
 
948
    r300render->base.draw_elements = r300_render_draw_elements;
750
949
    r300render->base.draw_arrays = r300_render_draw_arrays;
751
950
    r300render->base.release_vertices = r300_render_release_vertices;
752
951
    r300render->base.destroy = r300_render_destroy;
753
952
 
754
 
    r300render->vbo = NULL;
755
 
    r300render->vbo_size = 0;
756
953
    r300render->vbo_offset = 0;
757
954
 
758
955
    return &r300render->base;
780
977
 
781
978
    return stage;
782
979
}
 
980
 
 
981
void r300_draw_flush_vbuf(struct r300_context *r300)
 
982
{
 
983
    pipe_resource_reference(&r300->vbo, NULL);
 
984
    r300->draw_vbo_size = 0;
 
985
}
 
986
 
 
987
/****************************************************************************
 
988
 *                         End of SW TCL functions                          *
 
989
 ***************************************************************************/
 
990
 
 
991
/* If we used a quad to draw a rectangle, the pixels on the main diagonal
 
992
 * would be computed and stored twice, which makes the clear/copy codepaths
 
993
 * somewhat inefficient. Instead we use a rectangular point sprite. */
 
994
static void r300_blitter_draw_rectangle(struct blitter_context *blitter,
 
995
                                        unsigned x1, unsigned y1,
 
996
                                        unsigned x2, unsigned y2,
 
997
                                        float depth,
 
998
                                        enum blitter_attrib_type type,
 
999
                                        const float attrib[4])
 
1000
{
 
1001
    struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));
 
1002
    unsigned last_sprite_coord_enable = r300->sprite_coord_enable;
 
1003
    unsigned width = x2 - x1;
 
1004
    unsigned height = y2 - y1;
 
1005
    unsigned vertex_size =
 
1006
            type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;
 
1007
    unsigned dwords = 13 + vertex_size +
 
1008
                      (type == UTIL_BLITTER_ATTRIB_TEXCOORD ? 7 : 0);
 
1009
    const float zeros[4] = {0, 0, 0, 0};
 
1010
    CS_LOCALS(r300);
 
1011
 
 
1012
    if (type == UTIL_BLITTER_ATTRIB_TEXCOORD)
 
1013
        r300->sprite_coord_enable = 1;
 
1014
 
 
1015
    r300_update_derived_state(r300);
 
1016
 
 
1017
    /* Mark some states we don't care about as non-dirty. */
 
1018
    r300->clip_state.dirty = FALSE;
 
1019
    r300->viewport_state.dirty = FALSE;
 
1020
 
 
1021
    r300_prepare_for_rendering(r300, PREP_FIRST_DRAW, NULL, dwords, 0, 0);
 
1022
 
 
1023
    DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
 
1024
 
 
1025
    BEGIN_CS(dwords);
 
1026
    /* Set up GA. */
 
1027
    OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
 
1028
 
 
1029
    if (type == UTIL_BLITTER_ATTRIB_TEXCOORD) {
 
1030
        /* Set up the GA to generate texcoords. */
 
1031
        OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |
 
1032
                   (R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));
 
1033
        OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
 
1034
        OUT_CS_32F(attrib[0]);
 
1035
        OUT_CS_32F(attrib[3]);
 
1036
        OUT_CS_32F(attrib[2]);
 
1037
        OUT_CS_32F(attrib[1]);
 
1038
    }
 
1039
 
 
1040
    /* Set up VAP controls. */
 
1041
    OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
 
1042
    OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);
 
1043
    OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
 
1044
    OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
 
1045
    OUT_CS(1);
 
1046
    OUT_CS(0);
 
1047
 
 
1048
    /* Draw. */
 
1049
    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);
 
1050
    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |
 
1051
           R300_VAP_VF_CNTL__PRIM_POINTS);
 
1052
 
 
1053
    OUT_CS_32F(x1 + width * 0.5f);
 
1054
    OUT_CS_32F(y1 + height * 0.5f);
 
1055
    OUT_CS_32F(depth);
 
1056
    OUT_CS_32F(1);
 
1057
 
 
1058
    if (vertex_size == 8) {
 
1059
        if (!attrib)
 
1060
            attrib = zeros;
 
1061
        OUT_CS_TABLE(attrib, 4);
 
1062
    }
 
1063
    END_CS;
 
1064
 
 
1065
    /* Restore the state. */
 
1066
    r300->clip_state.dirty = TRUE;
 
1067
    r300->rs_state.dirty = TRUE;
 
1068
    r300->viewport_state.dirty = TRUE;
 
1069
 
 
1070
    r300->sprite_coord_enable = last_sprite_coord_enable;
 
1071
}
 
1072
 
 
1073
static void r300_resource_resolve(struct pipe_context* pipe,
 
1074
                                  struct pipe_resource* dest,
 
1075
                                  struct pipe_subresource subdest,
 
1076
                                  struct pipe_resource* src,
 
1077
                                  struct pipe_subresource subsrc)
 
1078
{
 
1079
    struct r300_context* r300 = r300_context(pipe);
 
1080
    struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
 
1081
    struct pipe_surface* srcsurf = src->screen->get_tex_surface(src->screen,
 
1082
            src, subsrc.face, subsrc.level, 0, 0);
 
1083
    float color[] = {0, 0, 0, 0};
 
1084
 
 
1085
    DBG(r300, DBG_DRAW, "r300: Resolving resource...\n");
 
1086
 
 
1087
    /* Enable AA resolve. */
 
1088
    aa->dest = r300_surface(
 
1089
            dest->screen->get_tex_surface(dest->screen, dest, subdest.face,
 
1090
                                          subdest.level, 0, 0));
 
1091
 
 
1092
    aa->aaresolve_ctl =
 
1093
        R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE |
 
1094
        R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE;
 
1095
    r300->aa_state.size = 12;
 
1096
    r300->aa_state.dirty = TRUE;
 
1097
 
 
1098
    /* Resolve the surface. */
 
1099
    r300->context.clear_render_target(pipe,
 
1100
        srcsurf, color, 0, 0, src->width0, src->height0);
 
1101
 
 
1102
    /* Disable AA resolve. */
 
1103
    aa->aaresolve_ctl = 0;
 
1104
    r300->aa_state.size = 4;
 
1105
    r300->aa_state.dirty = TRUE;
 
1106
 
 
1107
    pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
 
1108
    pipe_surface_reference((struct pipe_surface**)&aa->dest, NULL);
 
1109
}
 
1110
 
 
1111
void r300_init_render_functions(struct r300_context *r300)
 
1112
{
 
1113
    /* Set draw functions based on presence of HW TCL. */
 
1114
    if (r300->screen->caps.has_tcl) {
 
1115
        r300->context.draw_vbo = r300_draw_vbo;
 
1116
    } else {
 
1117
        r300->context.draw_vbo = r300_swtcl_draw_vbo;
 
1118
    }
 
1119
 
 
1120
    r300->context.resource_resolve = r300_resource_resolve;
 
1121
    r300->blitter->draw_rectangle = r300_blitter_draw_rectangle;
 
1122
 
 
1123
    /* Plug in the two-sided stencil reference value fallback if needed. */
 
1124
    if (!r300->screen->caps.is_r500)
 
1125
        r300_plug_in_stencil_ref_fallback(r300);
 
1126
}