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