2
* Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3
* Copyright 2010 Marek Olšák <maraeo@gmail.com>
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:
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
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. */
24
/* r300_render: Vertex and index buffer primitive emission. Contains both
25
* HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
27
#include "draw/draw_context.h"
28
#include "draw/draw_vbuf.h"
30
#include "util/u_inlines.h"
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"
39
#include "r300_context.h"
40
#include "r300_screen_buffer.h"
41
#include "r300_emit.h"
46
#define IMMD_DWORDS 32
48
static uint32_t r300_translate_primitive(unsigned prim)
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,
66
unsigned hwprim = prim_conv[prim];
72
static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
75
struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
76
uint32_t color_control = rs->color_control;
78
/* By default (see r300_state.c:r300_create_rs_state) color_control is
79
* initialized to provoking the first vertex.
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)
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.
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.
96
if (rs->rs.flatshade_first) {
98
case PIPE_PRIM_TRIANGLE_FAN:
99
color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
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;
107
color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
111
color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
114
return color_control;
117
void r500_emit_index_bias(struct r300_context *r300, int index_bias)
122
OUT_CS_REG(R500_VAP_INDEX_OFFSET,
123
(index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
127
static void r300_emit_draw_init(struct r300_context *r300, unsigned mode,
132
assert(max_index < (1 << 24));
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);
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)
152
struct pipe_vertex_buffer *vb, *vbufs = r300->vertex_buffer;
153
struct pipe_vertex_element *velem = r300->velems->velem;
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
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);
168
/* Now set the minimum allowed value. */
169
*buffer_offset = MAX2(-max_neg_bias, index_bias);
171
/* A positive index bias is OK. */
172
*buffer_offset = index_bias;
175
*index_offset = index_bias - *buffer_offset;
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? */
187
* Check if the requested number of dwords is available in the CS and
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
194
static boolean r300_reserve_cs_dwords(struct r300_context *r300,
195
enum r300_prepare_flags flags,
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;
203
/* Add dirty state, index offset, and AOS. */
205
cs_dwords += r300_get_num_dirty_dwords(r300);
207
if (r300->screen->caps.is_r500)
208
cs_dwords += 2; /* emit_index_offset */
210
if (emit_vertex_arrays)
211
cs_dwords += 55; /* emit_vertex_arrays */
213
if (emit_vertex_arrays_swtcl)
214
cs_dwords += 7; /* emit_vertex_arrays_swtcl */
216
cs_dwords += r300_get_num_cs_end_dwords(r300);
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);
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
237
static boolean r300_emit_states(struct r300_context *r300,
238
enum r300_prepare_flags flags,
239
struct pipe_resource *index_buffer,
241
int index_bias, int instance_id)
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;
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,
253
fprintf(stderr, "r300: CS space validation failed. "
254
"(not enough memory?) Skipping rendering.\n");
260
r300_emit_dirty_state(r300);
262
if (r300->screen->caps.is_r500) {
263
if (r300->screen->caps.has_tcl)
264
r500_emit_index_bias(r300, index_bias);
266
r500_emit_index_bias(r300, 0);
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);
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;
282
if (emit_vertex_arrays_swtcl)
283
r300_emit_vertex_arrays_swtcl(r300, indexed);
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
300
static boolean r300_prepare_for_rendering(struct r300_context *r300,
301
enum r300_prepare_flags flags,
302
struct pipe_resource *index_buffer,
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;
312
return r300_emit_states(r300, flags, index_buffer, buffer_offset,
313
index_bias, instance_id);
316
static boolean immd_is_good_idea(struct r300_context *r300,
319
if (DBG_ON(r300, DBG_NO_IMMD)) {
323
if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
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). */
332
/*****************************************************************************
333
* The HWTCL draw functions. *
334
****************************************************************************/
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)
340
struct pipe_vertex_element* velem;
341
struct pipe_vertex_buffer* vbuf;
342
unsigned vertex_element_count = r300->velems->count;
345
/* Size of the vertex, in dwords. */
346
unsigned vertex_size = r300->velems->vertex_size_dwords;
348
/* The number of dwords for this draw operation. */
349
unsigned dwords = 4 + draw->count * vertex_size;
351
/* Size of the vertex element, in dwords. */
352
unsigned size[PIPE_MAX_ATTRIBS];
354
/* Stride to the same attrib in the next vertex in the vertex buffer,
356
unsigned stride[PIPE_MAX_ATTRIBS];
358
/* Mapped vertex buffers. */
359
uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
360
uint32_t* mapelem[PIPE_MAX_ATTRIBS];
364
if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
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;
375
/* Map the buffer. */
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;
382
mapelem[i] = map[vbi] + (velem->src_offset / 4);
385
r300_emit_draw_init(r300, info->mode, draw->count-1);
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));
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]);
402
static void r300_emit_draw_arrays(struct r300_context *r300,
406
boolean alt_num_verts = count > 65535;
409
if (count >= (1 << 24)) {
410
fprintf(stderr, "r300: Got a huge number of vertices: %i, "
411
"refusing to render.\n", count);
415
r300_emit_draw_init(r300, mode, count-1);
417
BEGIN_CS(2 + (alt_num_verts ? 2 : 0));
419
OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
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));
428
static void r300_emit_draw_elements(struct r300_context *r300,
429
struct pipe_resource* indexBuffer,
435
uint16_t *imm_indices3)
437
uint32_t count_dwords, offset_dwords;
438
boolean alt_num_verts = count > 65535;
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);
447
DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, max %u\n",
450
r300_emit_draw_init(r300, mode, max_index);
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) {
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]);
471
offset_dwords = indexSize * start / sizeof(uint32_t);
473
BEGIN_CS(8 + (alt_num_verts ? 2 : 0));
475
OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
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));
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));
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));
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)
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;
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))
518
r300_emit_draw_init(r300, info->mode, info->max_index);
520
BEGIN_CS(2 + count_dwords);
521
OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, count_dwords);
523
switch (index_size) {
525
ptr1 = (uint8_t*)info->index.user;
528
OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (draw->count << 16) |
529
r300_translate_primitive(info->mode));
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));
537
OUT_CS(ptr1[i] + draw->index_bias);
539
for (i = 0; i < draw->count-1; i += 2)
540
OUT_CS(((ptr1[i+1]) << 16) |
549
ptr2 = (uint16_t*)info->index.user;
552
OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (draw->count << 16) |
553
r300_translate_primitive(info->mode));
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));
561
OUT_CS(ptr2[i] + draw->index_bias);
563
OUT_CS_TABLE(ptr2, count_dwords);
568
ptr4 = (uint32_t*)info->index.user;
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));
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);
579
OUT_CS_TABLE(ptr4, count_dwords);
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,
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 &&
599
unsigned short_count;
600
int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
601
uint16_t indices3[3];
603
if (draw->index_bias && !r300->screen->caps.is_r500) {
604
r300_split_index_bias(r300, draw->index_bias, &buffer_offset,
608
r300_translate_index_buffer(r300, info, &indexBuffer,
609
&indexSize, index_offset, &start, count);
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,
617
PIPE_MAP_UNSYNCHRONIZED);
619
if (info->mode == PIPE_PRIM_TRIANGLES) {
620
memcpy(indices3, ptr + start, 6);
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);
629
if (info->has_user_indices)
630
r300_upload_index_buffer(r300, &indexBuffer, indexSize,
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,
642
if (alt_num_verts || count <= 65535) {
643
r300_emit_draw_elements(r300, indexBuffer, indexSize,
644
info->max_index, info->mode, start, count,
648
/* The maximum must be divisible by 4 and 3,
649
* so that quad and triangle lists are split correctly.
651
* Strips, loops, and fans won't work. */
652
short_count = MIN2(count, 65532);
654
r300_emit_draw_elements(r300, indexBuffer, indexSize,
656
info->mode, start, short_count, indices3);
658
start += short_count;
659
count -= short_count;
661
/* 15 dwords for emit_draw_elements */
663
if (!r300_prepare_for_rendering(r300,
664
PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS | PREP_INDEXED,
665
indexBuffer, 19, buffer_offset, draw->index_bias,
673
if (indexBuffer != orgIndexBuffer) {
674
pipe_resource_reference( &indexBuffer, NULL );
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,
683
boolean alt_num_verts = r300->screen->caps.is_r500 &&
685
unsigned start = draw->start;
686
unsigned count = draw->count;
687
unsigned short_count;
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))
695
if (alt_num_verts || count <= 65535) {
696
r300_emit_draw_arrays(r300, info->mode, count);
699
/* The maximum must be divisible by 4 and 3,
700
* so that quad and triangle lists are split correctly.
702
* Strips, loops, and fans won't work. */
703
short_count = MIN2(count, 65532);
704
r300_emit_draw_arrays(r300, info->mode, short_count);
706
start += short_count;
707
count -= short_count;
709
/* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
711
if (!r300_prepare_for_rendering(r300,
712
PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS, NULL, 9,
713
start, 0, instance_id))
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)
726
for (i = 0; i < info->instance_count; i++)
727
r300_draw_arrays(r300, info, draw, i);
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)
736
for (i = 0; i < info->instance_count; i++)
737
r300_draw_elements(r300, info, draw, i);
740
static unsigned r300_max_vertex_count(struct r300_context *r300)
742
unsigned i, nr = r300->velems->count;
743
struct pipe_vertex_element *velems = r300->velems->velem;
744
unsigned result = ~0;
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;
751
/* We're not interested in constant and per-instance attribs. */
752
if (!vb->buffer.resource ||
754
velems[i].instance_divisor) {
758
size = vb->buffer.resource->width0;
760
/* Subtract buffer_offset. */
761
value = vb->buffer_offset;
767
/* Subtract src_offset. */
768
value = velems[i].src_offset;
774
/* Subtract format_size. */
775
value = r300->velems->format_size[i];
781
/* Compute the max count. */
782
max_count = 1 + size / vb->stride;
783
result = MIN2(result, max_count);
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,
797
util_draw_multi(pipe, dinfo, drawid_offset, indirect, draws, num_draws);
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];
805
if (r300->skip_rendering ||
806
!u_trim_pipe_prim(info.mode, &draw.count)) {
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);
816
r300_update_derived_state(r300);
819
if (info.index_size) {
820
unsigned max_count = r300_max_vertex_count(r300);
823
fprintf(stderr, "r300: Skipping a draw command. There is a buffer "
824
" which is too small to be used for rendering.\n");
828
if (max_count == ~0) {
829
/* There are no per-vertex vertex elements. Use the hardware maximum. */
830
max_count = 0xffffff;
833
info.max_index = max_count - 1;
835
if (info.instance_count <= 1) {
836
if (draw.count <= 8 && info.has_user_indices) {
837
r300_draw_elements_immediate(r300, &info, &draw);
839
r300_draw_elements(r300, &info, &draw, -1);
842
r300_draw_elements_instanced(r300, &info, &draw);
845
if (info.instance_count <= 1) {
846
if (immd_is_good_idea(r300, draw.count)) {
847
r300_draw_arrays_immediate(r300, &info, &draw);
849
r300_draw_arrays(r300, &info, &draw, -1);
852
r300_draw_arrays_instanced(r300, &info, &draw);
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
***************************************************************************/
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,
871
util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);
875
struct r300_context* r300 = r300_context(pipe);
876
struct pipe_draw_start_count_bias draw = draws[0];
878
if (r300->skip_rendering) {
882
if (!u_trim_pipe_prim(info->mode, &draw.count))
885
if (info->index_size) {
886
draw_set_indexes(r300->draw,
887
info->has_user_indices ?
889
r300_resource(info->index.resource)->malloced_buffer,
890
info->index_size, ~0);
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);
899
r300_update_derived_state(r300);
901
draw_vbo(r300->draw, info, drawid_offset, NULL, &draw, 1, 0);
902
draw_flush(r300->draw);
905
/* Object for rendering using Draw. */
908
struct vbuf_render base;
911
struct r300_context* r300;
913
/* Vertex information */
923
static inline struct r300_render*
924
r300_render(struct vbuf_render* render)
926
return (struct r300_render*)render;
929
static const struct vertex_info*
930
r300_render_get_vertex_info(struct vbuf_render* render)
932
struct r300_render* r300render = r300_render(render);
933
struct r300_context* r300 = r300render->r300;
935
return &r300->vertex_info;
938
static boolean r300_render_allocate_vertices(struct vbuf_render* render,
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;
947
DBG(r300, DBG_DRAW, "r300: render_allocate_vertices (size: %d)\n", size);
949
if (!r300->vbo || size + r300->draw_vbo_offset > r300->vbo->size) {
950
pb_reference(&r300->vbo, NULL);
952
r300render->vbo_ptr = NULL;
954
r300->vbo = rws->buffer_create(rws,
955
MAX2(R300_MAX_DRAW_VBO_SIZE, size),
956
R300_BUFFER_ALIGNMENT,
958
RADEON_FLAG_NO_INTERPROCESS_SHARING);
962
r300->draw_vbo_offset = 0;
963
r300render->vbo_ptr = rws->buffer_map(rws, r300->vbo, &r300->cs,
967
r300render->vertex_size = vertex_size;
971
static void* r300_render_map_vertices(struct vbuf_render* render)
973
struct r300_render* r300render = r300_render(render);
974
struct r300_context* r300 = r300render->r300;
976
DBG(r300, DBG_DRAW, "r300: render_map_vertices\n");
978
assert(r300render->vbo_ptr);
979
return r300render->vbo_ptr + r300->draw_vbo_offset;
982
static void r300_render_unmap_vertices(struct vbuf_render* render,
986
struct r300_render* r300render = r300_render(render);
987
struct r300_context* r300 = r300render->r300;
989
DBG(r300, DBG_DRAW, "r300: render_unmap_vertices\n");
991
r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
992
r300render->vertex_size * (max + 1));
995
static void r300_render_release_vertices(struct vbuf_render* render)
997
struct r300_render* r300render = r300_render(render);
998
struct r300_context* r300 = r300render->r300;
1000
DBG(r300, DBG_DRAW, "r300: render_release_vertices\n");
1002
r300->draw_vbo_offset += r300render->vbo_max_used;
1003
r300render->vbo_max_used = 0;
1006
static void r300_render_set_primitive(struct vbuf_render* render,
1007
enum pipe_prim_type prim)
1009
struct r300_render* r300render = r300_render(render);
1011
r300render->prim = prim;
1012
r300render->hwprim = r300_translate_primitive(prim);
1015
static void r300_render_draw_arrays(struct vbuf_render* render,
1019
struct r300_render* r300render = r300_render(render);
1020
struct r300_context* r300 = r300render->r300;
1023
unsigned dwords = 6;
1026
(void) i; (void) ptr;
1029
assert(count < (1 << 16));
1031
DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
1033
if (!r300_prepare_for_rendering(r300,
1034
PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL,
1035
NULL, dwords, 0, 0, -1)) {
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);
1049
static void r300_render_draw_elements(struct vbuf_render* render,
1050
const ushort* indices,
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;
1061
DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
1063
u_upload_data(r300->uploader, 0, count * 2, 4, indices,
1064
&index_buffer_offset, &index_buffer);
1065
if (!index_buffer) {
1069
if (!r300_prepare_for_rendering(r300,
1071
PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,
1072
index_buffer, 12, 0, 0, -1)) {
1073
pipe_resource_reference(&index_buffer, NULL);
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);
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);
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));
1093
pipe_resource_reference(&index_buffer, NULL);
1096
static void r300_render_destroy(struct vbuf_render* render)
1101
static struct vbuf_render* r300_render_create(struct r300_context* r300)
1103
struct r300_render* r300render = CALLOC_STRUCT(r300_render);
1105
r300render->r300 = r300;
1107
r300render->base.max_vertex_buffer_bytes = R300_MAX_DRAW_VBO_SIZE;
1108
r300render->base.max_indices = 16 * 1024;
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;
1120
return &r300render->base;
1123
struct draw_stage* r300_draw_stage(struct r300_context* r300)
1125
struct vbuf_render* render;
1126
struct draw_stage* stage;
1128
render = r300_render_create(r300);
1134
stage = draw_vbuf_stage(r300->draw, render);
1137
render->destroy(render);
1141
draw_set_render(r300->draw, render);
1146
/****************************************************************************
1147
* End of SW TCL functions *
1148
***************************************************************************/
1150
/* This functions is used to draw a rectangle for the blitter module.
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)
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;
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,
1182
depth, num_instances, type, attrib);
1186
if (r300->skip_rendering)
1189
r300->context.bind_vertex_elements_state(&r300->context, vertex_elements_cso);
1190
r300->context.bind_vs_state(&r300->context, get_vs(blitter));
1192
if (type == UTIL_BLITTER_ATTRIB_TEXCOORD_XY) {
1193
r300->sprite_coord_enable = 1;
1194
r300->is_point = true;
1197
r300_update_derived_state(r300);
1199
/* Mark some states we don't care about as non-dirty. */
1200
r300->viewport_state.dirty = FALSE;
1202
if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
1205
DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
1209
OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
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);
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);
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);
1235
OUT_CS_32F(x1 + width * 0.5f);
1236
OUT_CS_32F(y1 + height * 0.5f);
1240
if (vertex_size == 8) {
1243
OUT_CS_TABLE(attrib->color, 4);
1248
/* Restore the state. */
1249
r300_mark_atom_dirty(r300, &r300->rs_state);
1250
r300_mark_atom_dirty(r300, &r300->viewport_state);
1252
r300->sprite_coord_enable = last_sprite_coord_enable;
1253
r300->is_point = last_is_point;
1256
void r300_init_render_functions(struct r300_context *r300)
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;
1262
r300->context.draw_vbo = r300_swtcl_draw_vbo;
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);