Merge branch '7.8'
[mesa.git] / src / gallium / drivers / r300 / r300_emit.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 /* r300_emit: Functions for emitting state. */
25
26 #include "util/u_format.h"
27 #include "util/u_math.h"
28 #include "util/u_simple_list.h"
29
30 #include "r300_context.h"
31 #include "r300_cs.h"
32 #include "r300_emit.h"
33 #include "r300_fs.h"
34 #include "r300_screen.h"
35 #include "r300_screen_buffer.h"
36 #include "r300_vs.h"
37
38 void r300_emit_blend_state(struct r300_context* r300,
39 unsigned size, void* state)
40 {
41 struct r300_blend_state* blend = (struct r300_blend_state*)state;
42 struct pipe_framebuffer_state* fb =
43 (struct pipe_framebuffer_state*)r300->fb_state.state;
44 CS_LOCALS(r300);
45
46 BEGIN_CS(size);
47 OUT_CS_REG(R300_RB3D_ROPCNTL, blend->rop);
48 OUT_CS_REG_SEQ(R300_RB3D_CBLEND, 3);
49 if (fb->nr_cbufs) {
50 OUT_CS(blend->blend_control);
51 OUT_CS(blend->alpha_blend_control);
52 OUT_CS(blend->color_channel_mask);
53 } else {
54 OUT_CS(0);
55 OUT_CS(0);
56 OUT_CS(0);
57 /* XXX also disable fastfill here once it's supported */
58 }
59 OUT_CS_REG(R300_RB3D_DITHER_CTL, blend->dither);
60 END_CS;
61 }
62
63 void r300_emit_blend_color_state(struct r300_context* r300,
64 unsigned size, void* state)
65 {
66 struct r300_blend_color_state* bc = (struct r300_blend_color_state*)state;
67 CS_LOCALS(r300);
68
69 if (r300->screen->caps.is_r500) {
70 BEGIN_CS(size);
71 OUT_CS_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR, 2);
72 OUT_CS(bc->blend_color_red_alpha);
73 OUT_CS(bc->blend_color_green_blue);
74 END_CS;
75 } else {
76 BEGIN_CS(size);
77 OUT_CS_REG(R300_RB3D_BLEND_COLOR, bc->blend_color);
78 END_CS;
79 }
80 }
81
82 void r300_emit_clip_state(struct r300_context* r300,
83 unsigned size, void* state)
84 {
85 struct pipe_clip_state* clip = (struct pipe_clip_state*)state;
86 CS_LOCALS(r300);
87
88 if (r300->screen->caps.has_tcl) {
89 BEGIN_CS(size);
90 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
91 (r300->screen->caps.is_r500 ?
92 R500_PVS_UCP_START : R300_PVS_UCP_START));
93 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, 6 * 4);
94 OUT_CS_TABLE(clip->ucp, 6 * 4);
95 OUT_CS_REG(R300_VAP_CLIP_CNTL, ((1 << clip->nr) - 1) |
96 R300_PS_UCP_MODE_CLIP_AS_TRIFAN);
97 END_CS;
98 } else {
99 BEGIN_CS(size);
100 OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
101 END_CS;
102 }
103 }
104
105 void r300_emit_dsa_state(struct r300_context* r300, unsigned size, void* state)
106 {
107 struct r300_dsa_state* dsa = (struct r300_dsa_state*)state;
108 struct pipe_framebuffer_state* fb =
109 (struct pipe_framebuffer_state*)r300->fb_state.state;
110 struct pipe_stencil_ref stencil_ref = r300->stencil_ref;
111 CS_LOCALS(r300);
112
113 BEGIN_CS(size);
114 OUT_CS_REG(R300_FG_ALPHA_FUNC, dsa->alpha_function);
115 OUT_CS_REG_SEQ(R300_ZB_CNTL, 3);
116
117 if (fb->zsbuf) {
118 OUT_CS(dsa->z_buffer_control);
119 OUT_CS(dsa->z_stencil_control);
120 } else {
121 OUT_CS(0);
122 OUT_CS(0);
123 }
124
125 OUT_CS(dsa->stencil_ref_mask | stencil_ref.ref_value[0]);
126
127 if (r300->screen->caps.is_r500) {
128 OUT_CS_REG(R500_ZB_STENCILREFMASK_BF, dsa->stencil_ref_bf | stencil_ref.ref_value[1]);
129 }
130 END_CS;
131 }
132
133 static const float * get_rc_constant_state(
134 struct r300_context * r300,
135 struct rc_constant * constant)
136 {
137 struct r300_viewport_state* viewport = r300->viewport_state.state;
138 struct r300_textures_state* texstate = r300->textures_state.state;
139 static float vec[4] = { 0.0, 0.0, 0.0, 1.0 };
140 struct pipe_resource *tex;
141
142 assert(constant->Type == RC_CONSTANT_STATE);
143
144 switch (constant->u.State[0]) {
145 /* Factor for converting rectangle coords to
146 * normalized coords. Should only show up on non-r500. */
147 case RC_STATE_R300_TEXRECT_FACTOR:
148 tex = texstate->sampler_views[constant->u.State[1]]->base.texture;
149 vec[0] = 1.0 / tex->width0;
150 vec[1] = 1.0 / tex->height0;
151 break;
152
153 case RC_STATE_R300_VIEWPORT_SCALE:
154 vec[0] = viewport->xscale;
155 vec[1] = viewport->yscale;
156 vec[2] = viewport->zscale;
157 break;
158
159 case RC_STATE_R300_VIEWPORT_OFFSET:
160 vec[0] = viewport->xoffset;
161 vec[1] = viewport->yoffset;
162 vec[2] = viewport->zoffset;
163 break;
164
165 default:
166 fprintf(stderr, "r300: Implementation error: "
167 "Unknown RC_CONSTANT type %d\n", constant->u.State[0]);
168 }
169
170 /* This should either be (0, 0, 0, 1), which should be a relatively safe
171 * RGBA or STRQ value, or it could be one of the RC_CONSTANT_STATE
172 * state factors. */
173 return vec;
174 }
175
176 /* Convert a normal single-precision float into the 7.16 format
177 * used by the R300 fragment shader.
178 */
179 static uint32_t pack_float24(float f)
180 {
181 union {
182 float fl;
183 uint32_t u;
184 } u;
185 float mantissa;
186 int exponent;
187 uint32_t float24 = 0;
188
189 if (f == 0.0)
190 return 0;
191
192 u.fl = f;
193
194 mantissa = frexpf(f, &exponent);
195
196 /* Handle -ve */
197 if (mantissa < 0) {
198 float24 |= (1 << 23);
199 mantissa = mantissa * -1.0;
200 }
201 /* Handle exponent, bias of 63 */
202 exponent += 62;
203 float24 |= (exponent << 16);
204 /* Kill 7 LSB of mantissa */
205 float24 |= (u.u & 0x7FFFFF) >> 7;
206
207 return float24;
208 }
209
210 unsigned r300_get_fs_atom_size(struct r300_context *r300)
211 {
212 struct r300_fragment_shader *fs = r300_fs(r300);
213 unsigned imm_count = fs->shader->immediates_count;
214 struct r300_fragment_program_code *code = &fs->shader->code.code.r300;
215
216 return 19 +
217 code->alu.length * 4 +
218 (code->tex.length ? (1 + code->tex.length) : 0) +
219 (imm_count ? imm_count * 5 : 0);
220 }
221
222 void r300_emit_fs(struct r300_context* r300, unsigned size, void *state)
223 {
224 struct r300_fragment_shader *fs = r300_fs(r300);
225 struct rX00_fragment_program_code* generic_code = &fs->shader->code;
226 struct r300_fragment_program_code * code = &generic_code->code.r300;
227 unsigned i;
228 unsigned imm_count = fs->shader->immediates_count;
229 unsigned imm_first = fs->shader->externals_count;
230 unsigned imm_end = generic_code->constants.Count;
231 struct rc_constant *constants = generic_code->constants.Constants;
232 CS_LOCALS(r300);
233
234 BEGIN_CS(size);
235 OUT_CS_REG(R300_US_CONFIG, code->config);
236 OUT_CS_REG(R300_US_PIXSIZE, code->pixsize);
237 OUT_CS_REG(R300_US_CODE_OFFSET, code->code_offset);
238
239 OUT_CS_REG_SEQ(R300_US_CODE_ADDR_0, 4);
240 OUT_CS_TABLE(code->code_addr, 4);
241
242 OUT_CS_REG_SEQ(R300_US_ALU_RGB_INST_0, code->alu.length);
243 for (i = 0; i < code->alu.length; i++)
244 OUT_CS(code->alu.inst[i].rgb_inst);
245
246 OUT_CS_REG_SEQ(R300_US_ALU_RGB_ADDR_0, code->alu.length);
247 for (i = 0; i < code->alu.length; i++)
248 OUT_CS(code->alu.inst[i].rgb_addr);
249
250 OUT_CS_REG_SEQ(R300_US_ALU_ALPHA_INST_0, code->alu.length);
251 for (i = 0; i < code->alu.length; i++)
252 OUT_CS(code->alu.inst[i].alpha_inst);
253
254 OUT_CS_REG_SEQ(R300_US_ALU_ALPHA_ADDR_0, code->alu.length);
255 for (i = 0; i < code->alu.length; i++)
256 OUT_CS(code->alu.inst[i].alpha_addr);
257
258 if (code->tex.length) {
259 OUT_CS_REG_SEQ(R300_US_TEX_INST_0, code->tex.length);
260 OUT_CS_TABLE(code->tex.inst, code->tex.length);
261 }
262
263 /* Emit immediates. */
264 if (imm_count) {
265 for(i = imm_first; i < imm_end; ++i) {
266 if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {
267 const float *data = constants[i].u.Immediate;
268
269 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X + i * 16, 4);
270 OUT_CS(pack_float24(data[0]));
271 OUT_CS(pack_float24(data[1]));
272 OUT_CS(pack_float24(data[2]));
273 OUT_CS(pack_float24(data[3]));
274 }
275 }
276 }
277
278 OUT_CS_REG(R300_FG_DEPTH_SRC, fs->shader->fg_depth_src);
279 OUT_CS_REG(R300_US_W_FMT, fs->shader->us_out_w);
280 END_CS;
281 }
282
283 void r300_emit_fs_constants(struct r300_context* r300, unsigned size, void *state)
284 {
285 struct r300_fragment_shader *fs = r300_fs(r300);
286 struct rc_constant_list *constants = &fs->shader->code.constants;
287 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
288 unsigned i, count = fs->shader->externals_count;
289 CS_LOCALS(r300);
290
291 if (count == 0)
292 return;
293
294 BEGIN_CS(size);
295 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X, count * 4);
296 for(i = 0; i < count; ++i) {
297 const float *data;
298 assert(constants->Constants[i].Type == RC_CONSTANT_EXTERNAL);
299 data = buf->constants[i];
300 OUT_CS(pack_float24(data[0]));
301 OUT_CS(pack_float24(data[1]));
302 OUT_CS(pack_float24(data[2]));
303 OUT_CS(pack_float24(data[3]));
304 }
305 END_CS;
306 }
307
308 void r300_emit_fs_rc_constant_state(struct r300_context* r300, unsigned size, void *state)
309 {
310 struct r300_fragment_shader *fs = r300_fs(r300);
311 struct rc_constant_list *constants = &fs->shader->code.constants;
312 unsigned i;
313 unsigned count = fs->shader->rc_state_count;
314 unsigned first = fs->shader->externals_count;
315 unsigned end = constants->Count;
316 CS_LOCALS(r300);
317
318 if (count == 0)
319 return;
320
321 BEGIN_CS(size);
322 for(i = first; i < end; ++i) {
323 if (constants->Constants[i].Type == RC_CONSTANT_STATE) {
324 const float *data =
325 get_rc_constant_state(r300, &constants->Constants[i]);
326
327 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X + i * 16, 4);
328 OUT_CS(pack_float24(data[0]));
329 OUT_CS(pack_float24(data[1]));
330 OUT_CS(pack_float24(data[2]));
331 OUT_CS(pack_float24(data[3]));
332 }
333 }
334 END_CS;
335 }
336
337 unsigned r500_get_fs_atom_size(struct r300_context *r300)
338 {
339 struct r300_fragment_shader *fs = r300_fs(r300);
340 unsigned imm_count = fs->shader->immediates_count;
341 struct r500_fragment_program_code *code = &fs->shader->code.code.r500;
342
343 return 17 +
344 ((code->inst_end + 1) * 6) +
345 (imm_count ? imm_count * 7 : 0);
346 }
347
348 void r500_emit_fs(struct r300_context* r300, unsigned size, void *state)
349 {
350 struct r300_fragment_shader *fs = r300_fs(r300);
351 struct rX00_fragment_program_code* generic_code = &fs->shader->code;
352 struct r500_fragment_program_code * code = &generic_code->code.r500;
353 unsigned i;
354 unsigned imm_count = fs->shader->immediates_count;
355 unsigned imm_first = fs->shader->externals_count;
356 unsigned imm_end = generic_code->constants.Count;
357 struct rc_constant *constants = generic_code->constants.Constants;
358 CS_LOCALS(r300);
359
360 BEGIN_CS(size);
361 OUT_CS_REG(R500_US_CONFIG, R500_ZERO_TIMES_ANYTHING_EQUALS_ZERO);
362 OUT_CS_REG(R500_US_PIXSIZE, code->max_temp_idx);
363 OUT_CS_REG(R500_US_CODE_RANGE,
364 R500_US_CODE_RANGE_ADDR(0) | R500_US_CODE_RANGE_SIZE(code->inst_end));
365 OUT_CS_REG(R500_US_CODE_OFFSET, 0);
366 OUT_CS_REG(R500_US_CODE_ADDR,
367 R500_US_CODE_START_ADDR(0) | R500_US_CODE_END_ADDR(code->inst_end));
368
369 OUT_CS_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_INSTR);
370 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, (code->inst_end + 1) * 6);
371 for (i = 0; i <= code->inst_end; i++) {
372 OUT_CS(code->inst[i].inst0);
373 OUT_CS(code->inst[i].inst1);
374 OUT_CS(code->inst[i].inst2);
375 OUT_CS(code->inst[i].inst3);
376 OUT_CS(code->inst[i].inst4);
377 OUT_CS(code->inst[i].inst5);
378 }
379
380 /* Emit immediates. */
381 if (imm_count) {
382 for(i = imm_first; i < imm_end; ++i) {
383 if (constants[i].Type == RC_CONSTANT_IMMEDIATE) {
384 const float *data = constants[i].u.Immediate;
385
386 OUT_CS_REG(R500_GA_US_VECTOR_INDEX,
387 R500_GA_US_VECTOR_INDEX_TYPE_CONST |
388 (i & R500_GA_US_VECTOR_INDEX_MASK));
389 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, 4);
390 OUT_CS_TABLE(data, 4);
391 }
392 }
393 }
394
395 OUT_CS_REG(R300_FG_DEPTH_SRC, fs->shader->fg_depth_src);
396 OUT_CS_REG(R300_US_W_FMT, fs->shader->us_out_w);
397 END_CS;
398 }
399
400 void r500_emit_fs_constants(struct r300_context* r300, unsigned size, void *state)
401 {
402 struct r300_fragment_shader *fs = r300_fs(r300);
403 struct rc_constant_list *constants = &fs->shader->code.constants;
404 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
405 unsigned i, count = fs->shader->externals_count;
406 CS_LOCALS(r300);
407
408 if (count == 0)
409 return;
410
411 BEGIN_CS(size);
412 OUT_CS_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_CONST);
413 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, count * 4);
414 for(i = 0; i < count; ++i) {
415 assert(constants->Constants[i].Type == RC_CONSTANT_EXTERNAL);
416 }
417 OUT_CS_TABLE(buf->constants, count * 4);
418 END_CS;
419 }
420
421 void r500_emit_fs_rc_constant_state(struct r300_context* r300, unsigned size, void *state)
422 {
423 struct r300_fragment_shader *fs = r300_fs(r300);
424 struct rc_constant_list *constants = &fs->shader->code.constants;
425 unsigned i;
426 unsigned count = fs->shader->rc_state_count;
427 unsigned first = fs->shader->externals_count;
428 unsigned end = constants->Count;
429 CS_LOCALS(r300);
430
431 if (count == 0)
432 return;
433
434 BEGIN_CS(size);
435 for(i = first; i < end; ++i) {
436 if (constants->Constants[i].Type == RC_CONSTANT_STATE) {
437 const float *data =
438 get_rc_constant_state(r300, &constants->Constants[i]);
439
440 OUT_CS_REG(R500_GA_US_VECTOR_INDEX,
441 R500_GA_US_VECTOR_INDEX_TYPE_CONST |
442 (i & R500_GA_US_VECTOR_INDEX_MASK));
443 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, 4);
444 OUT_CS_TABLE(data, 4);
445 }
446 }
447 END_CS;
448 }
449
450 void r300_emit_fb_state(struct r300_context* r300, unsigned size, void* state)
451 {
452 struct pipe_framebuffer_state* fb = (struct pipe_framebuffer_state*)state;
453 struct r300_texture* tex;
454 struct pipe_surface* surf;
455 int i;
456 CS_LOCALS(r300);
457
458 BEGIN_CS(size);
459
460 /* Flush and free renderbuffer caches. */
461 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT,
462 R300_RB3D_DSTCACHE_CTLSTAT_DC_FREE_FREE_3D_TAGS |
463 R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
464 OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT,
465 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
466 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE);
467
468 /* Set the number of colorbuffers. */
469 if (fb->nr_cbufs > 1) {
470 if (r300->screen->caps.is_r500) {
471 OUT_CS_REG(R300_RB3D_CCTL,
472 R300_RB3D_CCTL_NUM_MULTIWRITES(fb->nr_cbufs) |
473 R300_RB3D_CCTL_INDEPENDENT_COLORFORMAT_ENABLE_ENABLE);
474 } else {
475 OUT_CS_REG(R300_RB3D_CCTL,
476 R300_RB3D_CCTL_NUM_MULTIWRITES(fb->nr_cbufs));
477 }
478 } else {
479 OUT_CS_REG(R300_RB3D_CCTL, 0x0);
480 }
481
482 /* Set up colorbuffers. */
483 for (i = 0; i < fb->nr_cbufs; i++) {
484 surf = fb->cbufs[i];
485 tex = r300_texture(surf->texture);
486 assert(tex && tex->buffer && "cbuf is marked, but NULL!");
487
488 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0 + (4 * i), 1);
489 OUT_CS_TEX_RELOC(tex, surf->offset, 0, RADEON_GEM_DOMAIN_VRAM, 0);
490
491 OUT_CS_REG_SEQ(R300_RB3D_COLORPITCH0 + (4 * i), 1);
492 OUT_CS_TEX_RELOC(tex, tex->fb_state.colorpitch[surf->level],
493 0, RADEON_GEM_DOMAIN_VRAM, 0);
494
495 OUT_CS_REG(R300_US_OUT_FMT_0 + (4 * i), tex->fb_state.us_out_fmt);
496 }
497 for (; i < 4; i++) {
498 OUT_CS_REG(R300_US_OUT_FMT_0 + (4 * i), R300_US_OUT_FMT_UNUSED);
499 }
500
501 /* Set up a zbuffer. */
502 if (fb->zsbuf) {
503 surf = fb->zsbuf;
504 tex = r300_texture(surf->texture);
505 assert(tex && tex->buffer && "zsbuf is marked, but NULL!");
506
507 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET, 1);
508 OUT_CS_TEX_RELOC(tex, surf->offset, 0, RADEON_GEM_DOMAIN_VRAM, 0);
509
510 OUT_CS_REG(R300_ZB_FORMAT, tex->fb_state.zb_format);
511
512 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH, 1);
513 OUT_CS_TEX_RELOC(tex, tex->fb_state.depthpitch[surf->level],
514 0, RADEON_GEM_DOMAIN_VRAM, 0);
515 }
516
517 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL, 2);
518 if (r300->screen->caps.is_r500) {
519 OUT_CS(0);
520 OUT_CS(((fb->width - 1) << R300_SCISSORS_X_SHIFT) |
521 ((fb->height - 1) << R300_SCISSORS_Y_SHIFT));
522 } else {
523 OUT_CS((1440 << R300_SCISSORS_X_SHIFT) |
524 (1440 << R300_SCISSORS_Y_SHIFT));
525 OUT_CS(((fb->width + 1440-1) << R300_SCISSORS_X_SHIFT) |
526 ((fb->height + 1440-1) << R300_SCISSORS_Y_SHIFT));
527 }
528 OUT_CS_REG(R300_GA_POINT_MINMAX,
529 (MAX2(fb->width, fb->height) * 6) << R300_GA_POINT_MINMAX_MAX_SHIFT);
530 END_CS;
531 }
532
533 void r300_emit_query_start(struct r300_context *r300, unsigned size, void*state)
534 {
535 struct r300_query *query = r300->query_current;
536 CS_LOCALS(r300);
537
538 if (!query)
539 return;
540
541 BEGIN_CS(size);
542 if (r300->screen->caps.family == CHIP_FAMILY_RV530) {
543 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
544 } else {
545 OUT_CS_REG(R300_SU_REG_DEST, R300_RASTER_PIPE_SELECT_ALL);
546 }
547 OUT_CS_REG(R300_ZB_ZPASS_DATA, 0);
548 END_CS;
549 query->begin_emitted = TRUE;
550 }
551
552
553 static void r300_emit_query_finish(struct r300_context *r300,
554 struct r300_query *query)
555 {
556 struct r300_capabilities* caps = &r300->screen->caps;
557 CS_LOCALS(r300);
558
559 assert(caps->num_frag_pipes);
560
561 BEGIN_CS(6 * caps->num_frag_pipes + 2);
562 /* I'm not so sure I like this switch, but it's hard to be elegant
563 * when there's so many special cases...
564 *
565 * So here's the basic idea. For each pipe, enable writes to it only,
566 * then put out the relocation for ZPASS_ADDR, taking into account a
567 * 4-byte offset for each pipe. RV380 and older are special; they have
568 * only two pipes, and the second pipe's enable is on bit 3, not bit 1,
569 * so there's a chipset cap for that. */
570 switch (caps->num_frag_pipes) {
571 case 4:
572 /* pipe 3 only */
573 OUT_CS_REG(R300_SU_REG_DEST, 1 << 3);
574 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
575 OUT_CS_BUF_RELOC(r300->oqbo, query->offset + (sizeof(uint32_t) * 3),
576 0, RADEON_GEM_DOMAIN_GTT, 0);
577 case 3:
578 /* pipe 2 only */
579 OUT_CS_REG(R300_SU_REG_DEST, 1 << 2);
580 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
581 OUT_CS_BUF_RELOC(r300->oqbo, query->offset + (sizeof(uint32_t) * 2),
582 0, RADEON_GEM_DOMAIN_GTT, 0);
583 case 2:
584 /* pipe 1 only */
585 /* As mentioned above, accomodate RV380 and older. */
586 OUT_CS_REG(R300_SU_REG_DEST,
587 1 << (caps->high_second_pipe ? 3 : 1));
588 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
589 OUT_CS_BUF_RELOC(r300->oqbo, query->offset + (sizeof(uint32_t) * 1),
590 0, RADEON_GEM_DOMAIN_GTT, 0);
591 case 1:
592 /* pipe 0 only */
593 OUT_CS_REG(R300_SU_REG_DEST, 1 << 0);
594 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
595 OUT_CS_BUF_RELOC(r300->oqbo, query->offset + (sizeof(uint32_t) * 0),
596 0, RADEON_GEM_DOMAIN_GTT, 0);
597 break;
598 default:
599 fprintf(stderr, "r300: Implementation error: Chipset reports %d"
600 " pixel pipes!\n", caps->num_frag_pipes);
601 abort();
602 }
603
604 /* And, finally, reset it to normal... */
605 OUT_CS_REG(R300_SU_REG_DEST, 0xF);
606 END_CS;
607 }
608
609 static void rv530_emit_query_single(struct r300_context *r300,
610 struct r300_query *query)
611 {
612 CS_LOCALS(r300);
613
614 BEGIN_CS(8);
615 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
616 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
617 OUT_CS_BUF_RELOC(r300->oqbo, query->offset, 0, RADEON_GEM_DOMAIN_GTT, 0);
618 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
619 END_CS;
620 }
621
622 static void rv530_emit_query_double(struct r300_context *r300,
623 struct r300_query *query)
624 {
625 CS_LOCALS(r300);
626
627 BEGIN_CS(14);
628 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
629 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
630 OUT_CS_BUF_RELOC(r300->oqbo, query->offset, 0, RADEON_GEM_DOMAIN_GTT, 0);
631 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_1);
632 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR, 1);
633 OUT_CS_BUF_RELOC(r300->oqbo, query->offset + sizeof(uint32_t), 0, RADEON_GEM_DOMAIN_GTT, 0);
634 OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
635 END_CS;
636 }
637
638 void r300_emit_query_end(struct r300_context* r300)
639 {
640 struct r300_capabilities *caps = &r300->screen->caps;
641 struct r300_query *query = r300->query_current;
642
643 if (!query)
644 return;
645
646 if (query->begin_emitted == FALSE)
647 return;
648
649 if (caps->family == CHIP_FAMILY_RV530) {
650 if (caps->num_z_pipes == 2)
651 rv530_emit_query_double(r300, query);
652 else
653 rv530_emit_query_single(r300, query);
654 } else
655 r300_emit_query_finish(r300, query);
656 }
657
658 void r300_emit_rs_state(struct r300_context* r300, unsigned size, void* state)
659 {
660 struct r300_rs_state* rs = (struct r300_rs_state*)state;
661 float scale, offset;
662 CS_LOCALS(r300);
663
664 BEGIN_CS(size);
665 OUT_CS_REG(R300_VAP_CNTL_STATUS, rs->vap_control_status);
666
667 OUT_CS_REG(R300_GB_AA_CONFIG, rs->antialiasing_config);
668
669 OUT_CS_REG(R300_GA_POINT_SIZE, rs->point_size);
670 OUT_CS_REG(R300_GA_LINE_CNTL, rs->line_control);
671
672 if (rs->polygon_offset_enable) {
673 scale = rs->depth_scale * 12;
674 offset = rs->depth_offset;
675
676 switch (r300->zbuffer_bpp) {
677 case 16:
678 offset *= 4;
679 break;
680 case 24:
681 offset *= 2;
682 break;
683 }
684
685 OUT_CS_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE, 4);
686 OUT_CS_32F(scale);
687 OUT_CS_32F(offset);
688 OUT_CS_32F(scale);
689 OUT_CS_32F(offset);
690 }
691
692 OUT_CS_REG_SEQ(R300_SU_POLY_OFFSET_ENABLE, 2);
693 OUT_CS(rs->polygon_offset_enable);
694 OUT_CS(rs->cull_mode);
695 OUT_CS_REG(R300_GA_LINE_STIPPLE_CONFIG, rs->line_stipple_config);
696 OUT_CS_REG(R300_GA_LINE_STIPPLE_VALUE, rs->line_stipple_value);
697 OUT_CS_REG(R300_GA_POLY_MODE, rs->polygon_mode);
698 OUT_CS_REG(R300_SC_CLIP_RULE, rs->clip_rule);
699 OUT_CS_REG(R300_GB_ENABLE, rs->stuffing_enable);
700 OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
701 OUT_CS_32F(rs->point_texcoord_left);
702 OUT_CS_32F(rs->point_texcoord_bottom);
703 OUT_CS_32F(rs->point_texcoord_right);
704 OUT_CS_32F(rs->point_texcoord_top);
705 END_CS;
706 }
707
708 void r300_emit_rs_block_state(struct r300_context* r300,
709 unsigned size, void* state)
710 {
711 struct r300_rs_block* rs = (struct r300_rs_block*)state;
712 unsigned i;
713 /* It's the same for both INST and IP tables */
714 unsigned count = (rs->inst_count & R300_RS_INST_COUNT_MASK) + 1;
715 CS_LOCALS(r300);
716
717 DBG(r300, DBG_DRAW, "r300: RS emit:\n");
718
719 BEGIN_CS(size);
720 if (r300->screen->caps.is_r500) {
721 OUT_CS_REG_SEQ(R500_RS_IP_0, count);
722 } else {
723 OUT_CS_REG_SEQ(R300_RS_IP_0, count);
724 }
725 OUT_CS_TABLE(rs->ip, count);
726 for (i = 0; i < count; i++) {
727 DBG(r300, DBG_DRAW, " : ip %d: 0x%08x\n", i, rs->ip[i]);
728 }
729
730 OUT_CS_REG_SEQ(R300_RS_COUNT, 2);
731 OUT_CS(rs->count);
732 OUT_CS(rs->inst_count);
733
734 if (r300->screen->caps.is_r500) {
735 OUT_CS_REG_SEQ(R500_RS_INST_0, count);
736 } else {
737 OUT_CS_REG_SEQ(R300_RS_INST_0, count);
738 }
739 OUT_CS_TABLE(rs->inst, count);
740 for (i = 0; i < count; i++) {
741 DBG(r300, DBG_DRAW, " : inst %d: 0x%08x\n", i, rs->inst[i]);
742 }
743
744 DBG(r300, DBG_DRAW, " : count: 0x%08x inst_count: 0x%08x\n",
745 rs->count, rs->inst_count);
746
747 END_CS;
748 }
749
750 void r300_emit_scissor_state(struct r300_context* r300,
751 unsigned size, void* state)
752 {
753 struct pipe_scissor_state* scissor = (struct pipe_scissor_state*)state;
754 CS_LOCALS(r300);
755
756 BEGIN_CS(size);
757 OUT_CS_REG_SEQ(R300_SC_CLIPRECT_TL_0, 2);
758 if (r300->screen->caps.is_r500) {
759 OUT_CS((scissor->minx << R300_CLIPRECT_X_SHIFT) |
760 (scissor->miny << R300_CLIPRECT_Y_SHIFT));
761 OUT_CS(((scissor->maxx - 1) << R300_CLIPRECT_X_SHIFT) |
762 ((scissor->maxy - 1) << R300_CLIPRECT_Y_SHIFT));
763 } else {
764 OUT_CS(((scissor->minx + 1440) << R300_CLIPRECT_X_SHIFT) |
765 ((scissor->miny + 1440) << R300_CLIPRECT_Y_SHIFT));
766 OUT_CS(((scissor->maxx + 1440-1) << R300_CLIPRECT_X_SHIFT) |
767 ((scissor->maxy + 1440-1) << R300_CLIPRECT_Y_SHIFT));
768 }
769 END_CS;
770 }
771
772 void r300_emit_textures_state(struct r300_context *r300,
773 unsigned size, void *state)
774 {
775 struct r300_textures_state *allstate = (struct r300_textures_state*)state;
776 struct r300_texture_sampler_state *texstate;
777 unsigned i;
778 CS_LOCALS(r300);
779
780 BEGIN_CS(size);
781 OUT_CS_REG(R300_TX_ENABLE, allstate->tx_enable);
782
783 for (i = 0; i < allstate->count; i++) {
784 if ((1 << i) & allstate->tx_enable) {
785 texstate = &allstate->regs[i];
786
787 OUT_CS_REG(R300_TX_FILTER0_0 + (i * 4), texstate->filter0);
788 OUT_CS_REG(R300_TX_FILTER1_0 + (i * 4), texstate->filter1);
789 OUT_CS_REG(R300_TX_BORDER_COLOR_0 + (i * 4),
790 texstate->border_color);
791
792 OUT_CS_REG(R300_TX_FORMAT0_0 + (i * 4), texstate->format.format0);
793 OUT_CS_REG(R300_TX_FORMAT1_0 + (i * 4), texstate->format.format1);
794 OUT_CS_REG(R300_TX_FORMAT2_0 + (i * 4), texstate->format.format2);
795
796 OUT_CS_REG_SEQ(R300_TX_OFFSET_0 + (i * 4), 1);
797 OUT_CS_TEX_RELOC(r300_texture(allstate->sampler_views[i]->base.texture),
798 texstate->format.tile_config,
799 RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM, 0, 0);
800 }
801 }
802 END_CS;
803 }
804
805 void r300_emit_aos(struct r300_context* r300, unsigned offset, boolean indexed)
806 {
807 struct pipe_vertex_buffer *vb1, *vb2, *vbuf = r300->vertex_buffer;
808 struct pipe_vertex_element *velem = r300->velems->velem;
809 int i;
810 unsigned size1, size2, aos_count = r300->velems->count;
811 unsigned packet_size = (aos_count * 3 + 1) / 2;
812 CS_LOCALS(r300);
813
814 for (i = 0; i < aos_count; i++) {
815 if ((vbuf[velem[i].vertex_buffer_index].buffer_offset + velem[i].src_offset) % 4 != 0) {
816 /* XXX We must align the buffer. */
817 assert(0);
818 fprintf(stderr, "r300: Unaligned vertex buffer offsets aren't supported, aborting..\n");
819 abort();
820 }
821 }
822
823 BEGIN_CS(2 + packet_size + aos_count * 2);
824 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, packet_size);
825 OUT_CS(aos_count | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
826
827 for (i = 0; i < aos_count - 1; i += 2) {
828 vb1 = &vbuf[velem[i].vertex_buffer_index];
829 vb2 = &vbuf[velem[i+1].vertex_buffer_index];
830 size1 = util_format_get_blocksize(velem[i].src_format);
831 size2 = util_format_get_blocksize(velem[i+1].src_format);
832
833 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride) |
834 R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(vb2->stride));
835 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
836 OUT_CS(vb2->buffer_offset + velem[i+1].src_offset + offset * vb2->stride);
837 }
838
839 if (aos_count & 1) {
840 vb1 = &vbuf[velem[i].vertex_buffer_index];
841 size1 = util_format_get_blocksize(velem[i].src_format);
842
843 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride));
844 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
845 }
846
847 for (i = 0; i < aos_count; i++) {
848 OUT_CS_BUF_RELOC_NO_OFFSET(vbuf[velem[i].vertex_buffer_index].buffer,
849 RADEON_GEM_DOMAIN_GTT, 0, 0);
850 }
851 END_CS;
852 }
853
854 void r300_emit_vertex_buffer(struct r300_context* r300)
855 {
856 CS_LOCALS(r300);
857
858 DBG(r300, DBG_DRAW, "r300: Preparing vertex buffer %p for render, "
859 "vertex size %d\n", r300->vbo,
860 r300->vertex_info.size);
861 /* Set the pointer to our vertex buffer. The emitted values are this:
862 * PACKET3 [3D_LOAD_VBPNTR]
863 * COUNT [1]
864 * FORMAT [size | stride << 8]
865 * OFFSET [offset into BO]
866 * VBPNTR [relocated BO]
867 */
868 BEGIN_CS(7);
869 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, 3);
870 OUT_CS(1);
871 OUT_CS(r300->vertex_info.size |
872 (r300->vertex_info.size << 8));
873 OUT_CS(r300->vbo_offset);
874 OUT_CS_BUF_RELOC(r300->vbo, 0, RADEON_GEM_DOMAIN_GTT, 0, 0);
875 END_CS;
876 }
877
878 void r300_emit_vertex_stream_state(struct r300_context* r300,
879 unsigned size, void* state)
880 {
881 struct r300_vertex_stream_state *streams =
882 (struct r300_vertex_stream_state*)state;
883 unsigned i;
884 CS_LOCALS(r300);
885
886 DBG(r300, DBG_DRAW, "r300: PSC emit:\n");
887
888 BEGIN_CS(size);
889 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0, streams->count);
890 OUT_CS_TABLE(streams->vap_prog_stream_cntl, streams->count);
891 for (i = 0; i < streams->count; i++) {
892 DBG(r300, DBG_DRAW, " : prog_stream_cntl%d: 0x%08x\n", i,
893 streams->vap_prog_stream_cntl[i]);
894 }
895 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0, streams->count);
896 OUT_CS_TABLE(streams->vap_prog_stream_cntl_ext, streams->count);
897 for (i = 0; i < streams->count; i++) {
898 DBG(r300, DBG_DRAW, " : prog_stream_cntl_ext%d: 0x%08x\n", i,
899 streams->vap_prog_stream_cntl_ext[i]);
900 }
901 END_CS;
902 }
903
904 void r300_emit_vap_output_state(struct r300_context* r300,
905 unsigned size, void* state)
906 {
907 struct r300_vap_output_state *vap_out_state =
908 (struct r300_vap_output_state*)state;
909 CS_LOCALS(r300);
910
911 DBG(r300, DBG_DRAW, "r300: VAP emit:\n");
912
913 BEGIN_CS(size);
914 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL, 2);
915 OUT_CS(vap_out_state->vap_vtx_state_cntl);
916 OUT_CS(vap_out_state->vap_vsm_vtx_assm);
917 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0, 2);
918 OUT_CS(vap_out_state->vap_out_vtx_fmt[0]);
919 OUT_CS(vap_out_state->vap_out_vtx_fmt[1]);
920 END_CS;
921 }
922
923 void r300_emit_pvs_flush(struct r300_context* r300, unsigned size, void* state)
924 {
925 CS_LOCALS(r300);
926
927 BEGIN_CS(size);
928 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG, 0x0);
929 END_CS;
930 }
931
932 void r300_emit_vs_state(struct r300_context* r300, unsigned size, void* state)
933 {
934 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)state;
935 struct r300_vertex_program_code* code = &vs->code;
936 struct r300_screen* r300screen = r300->screen;
937 unsigned instruction_count = code->length / 4;
938 unsigned i;
939
940 unsigned vtx_mem_size = r300screen->caps.is_r500 ? 128 : 72;
941 unsigned input_count = MAX2(util_bitcount(code->InputsRead), 1);
942 unsigned output_count = MAX2(util_bitcount(code->OutputsWritten), 1);
943 unsigned temp_count = MAX2(code->num_temporaries, 1);
944
945 unsigned pvs_num_slots = MIN3(vtx_mem_size / input_count,
946 vtx_mem_size / output_count, 10);
947 unsigned pvs_num_controllers = MIN2(vtx_mem_size / temp_count, 6);
948
949 unsigned imm_first = vs->externals_count;
950 unsigned imm_end = vs->code.constants.Count;
951 unsigned imm_count = vs->immediates_count;
952
953 CS_LOCALS(r300);
954
955 BEGIN_CS(size);
956 /* R300_VAP_PVS_CODE_CNTL_0
957 * R300_VAP_PVS_CONST_CNTL
958 * R300_VAP_PVS_CODE_CNTL_1
959 * See the r5xx docs for instructions on how to use these. */
960 OUT_CS_REG_SEQ(R300_VAP_PVS_CODE_CNTL_0, 3);
961 OUT_CS(R300_PVS_FIRST_INST(0) |
962 R300_PVS_XYZW_VALID_INST(instruction_count - 1) |
963 R300_PVS_LAST_INST(instruction_count - 1));
964 OUT_CS(R300_PVS_MAX_CONST_ADDR(code->constants.Count - 1));
965 OUT_CS(instruction_count - 1);
966
967 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG, 0);
968 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, code->length);
969 OUT_CS_TABLE(code->body.d, code->length);
970
971 OUT_CS_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(pvs_num_slots) |
972 R300_PVS_NUM_CNTLRS(pvs_num_controllers) |
973 R300_PVS_NUM_FPUS(r300screen->caps.num_vert_fpus) |
974 R300_PVS_VF_MAX_VTX_NUM(12) |
975 (r300screen->caps.is_r500 ? R500_TCL_STATE_OPTIMIZATION : 0));
976
977 /* Emit immediates. */
978 if (imm_count) {
979 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
980 (r300->screen->caps.is_r500 ?
981 R500_PVS_CONST_START : R300_PVS_CONST_START) +
982 imm_first);
983 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, imm_count * 4);
984 for (i = imm_first; i < imm_end; i++) {
985 const float *data = vs->code.constants.Constants[i].u.Immediate;
986 OUT_CS_TABLE(data, 4);
987 }
988 }
989 END_CS;
990 }
991
992 void r300_emit_vs_constants(struct r300_context* r300,
993 unsigned size, void *state)
994 {
995 unsigned count =
996 ((struct r300_vertex_shader*)r300->vs_state.state)->externals_count;
997 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
998 CS_LOCALS(r300);
999
1000 if (!count)
1001 return;
1002
1003 BEGIN_CS(size);
1004 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
1005 (r300->screen->caps.is_r500 ?
1006 R500_PVS_CONST_START : R300_PVS_CONST_START));
1007 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, count * 4);
1008 OUT_CS_TABLE(buf->constants, count * 4);
1009 END_CS;
1010 }
1011
1012 void r300_emit_viewport_state(struct r300_context* r300,
1013 unsigned size, void* state)
1014 {
1015 struct r300_viewport_state* viewport = (struct r300_viewport_state*)state;
1016 CS_LOCALS(r300);
1017
1018 BEGIN_CS(size);
1019 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE, 6);
1020 OUT_CS_32F(viewport->xscale);
1021 OUT_CS_32F(viewport->xoffset);
1022 OUT_CS_32F(viewport->yscale);
1023 OUT_CS_32F(viewport->yoffset);
1024 OUT_CS_32F(viewport->zscale);
1025 OUT_CS_32F(viewport->zoffset);
1026 OUT_CS_REG(R300_VAP_VTE_CNTL, viewport->vte_control);
1027 END_CS;
1028 }
1029
1030 void r300_emit_ztop_state(struct r300_context* r300,
1031 unsigned size, void* state)
1032 {
1033 struct r300_ztop_state* ztop = (struct r300_ztop_state*)state;
1034 CS_LOCALS(r300);
1035
1036 BEGIN_CS(size);
1037 OUT_CS_REG(R300_ZB_ZTOP, ztop->z_buffer_top);
1038 END_CS;
1039 }
1040
1041 void r300_emit_texture_cache_inval(struct r300_context* r300, unsigned size, void* state)
1042 {
1043 CS_LOCALS(r300);
1044
1045 BEGIN_CS(size);
1046 OUT_CS_REG(R300_TX_INVALTAGS, 0);
1047 END_CS;
1048 }
1049
1050 void r300_emit_buffer_validate(struct r300_context *r300,
1051 boolean do_validate_vertex_buffers,
1052 struct pipe_resource *index_buffer)
1053 {
1054 struct pipe_framebuffer_state* fb =
1055 (struct pipe_framebuffer_state*)r300->fb_state.state;
1056 struct r300_textures_state *texstate =
1057 (struct r300_textures_state*)r300->textures_state.state;
1058 struct r300_texture* tex;
1059 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
1060 struct pipe_vertex_element *velem = r300->velems->velem;
1061 struct pipe_resource *pbuf;
1062 unsigned i;
1063 boolean invalid = FALSE;
1064
1065 /* upload buffers first */
1066 if (r300->any_user_vbs) {
1067 r300_upload_user_buffers(r300);
1068 r300->any_user_vbs = false;
1069 }
1070
1071 /* Clean out BOs. */
1072 r300->rws->reset_bos(r300->rws);
1073
1074 validate:
1075 /* Color buffers... */
1076 for (i = 0; i < fb->nr_cbufs; i++) {
1077 tex = r300_texture(fb->cbufs[i]->texture);
1078 assert(tex && tex->buffer && "cbuf is marked, but NULL!");
1079 if (!r300_add_texture(r300->rws, tex,
1080 0, RADEON_GEM_DOMAIN_VRAM)) {
1081 r300->context.flush(&r300->context, 0, NULL);
1082 goto validate;
1083 }
1084 }
1085 /* ...depth buffer... */
1086 if (fb->zsbuf) {
1087 tex = r300_texture(fb->zsbuf->texture);
1088 assert(tex && tex->buffer && "zsbuf is marked, but NULL!");
1089 if (!r300_add_texture(r300->rws, tex,
1090 0, RADEON_GEM_DOMAIN_VRAM)) {
1091 r300->context.flush(&r300->context, 0, NULL);
1092 goto validate;
1093 }
1094 }
1095 /* ...textures... */
1096 for (i = 0; i < texstate->count; i++) {
1097 if (!(texstate->tx_enable & (1 << i))) {
1098 continue;
1099 }
1100
1101 tex = r300_texture(texstate->sampler_views[i]->base.texture);
1102 if (!r300_add_texture(r300->rws, tex,
1103 RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM, 0)) {
1104 r300->context.flush(&r300->context, 0, NULL);
1105 goto validate;
1106 }
1107 }
1108 /* ...occlusion query buffer... */
1109 if (r300->query_start.dirty) {
1110 if (!r300_add_buffer(r300->rws, r300->oqbo,
1111 0, RADEON_GEM_DOMAIN_GTT)) {
1112 r300->context.flush(&r300->context, 0, NULL);
1113 goto validate;
1114 }
1115 }
1116 /* ...vertex buffer for SWTCL path... */
1117 if (r300->vbo) {
1118 if (!r300_add_buffer(r300->rws, r300->vbo,
1119 RADEON_GEM_DOMAIN_GTT, 0)) {
1120 r300->context.flush(&r300->context, 0, NULL);
1121 goto validate;
1122 }
1123 }
1124 /* ...vertex buffers for HWTCL path... */
1125 if (do_validate_vertex_buffers) {
1126 for (i = 0; i < r300->velems->count; i++) {
1127 pbuf = vbuf[velem[i].vertex_buffer_index].buffer;
1128
1129 if (!r300_add_buffer(r300->rws, pbuf,
1130 RADEON_GEM_DOMAIN_GTT, 0)) {
1131 r300->context.flush(&r300->context, 0, NULL);
1132 goto validate;
1133 }
1134 }
1135 }
1136 /* ...and index buffer for HWTCL path. */
1137 if (index_buffer) {
1138 if (!r300_add_buffer(r300->rws, index_buffer,
1139 RADEON_GEM_DOMAIN_GTT, 0)) {
1140 r300->context.flush(&r300->context, 0, NULL);
1141 goto validate;
1142 }
1143 }
1144 if (!r300->rws->validate(r300->rws)) {
1145 r300->context.flush(&r300->context, 0, NULL);
1146 if (invalid) {
1147 /* Well, hell. */
1148 fprintf(stderr, "r300: Stuck in validation loop, gonna quit now.\n");
1149 abort();
1150 }
1151 invalid = TRUE;
1152 goto validate;
1153 }
1154 }
1155
1156 unsigned r300_get_num_dirty_dwords(struct r300_context *r300)
1157 {
1158 struct r300_atom* atom;
1159 unsigned dwords = 0;
1160
1161 foreach(atom, &r300->atom_list) {
1162 if (atom->dirty) {
1163 dwords += atom->size;
1164 }
1165 }
1166
1167 /* emit_query_end is not atomized. */
1168 dwords += 26;
1169 /* let's reserve some more, just in case */
1170 dwords += 32;
1171
1172 return dwords;
1173 }
1174
1175 /* Emit all dirty state. */
1176 void r300_emit_dirty_state(struct r300_context* r300)
1177 {
1178 struct r300_screen* r300screen = r300->screen;
1179 struct r300_atom* atom;
1180
1181 foreach(atom, &r300->atom_list) {
1182 if (atom->dirty) {
1183 atom->emit(r300, atom->size, atom->state);
1184 if (SCREEN_DBG_ON(r300->screen, DBG_STATS)) {
1185 atom->counter++;
1186 }
1187 atom->dirty = FALSE;
1188 }
1189 }
1190
1191 /* Emit the VBO for SWTCL. */
1192 if (!r300screen->caps.has_tcl) {
1193 r300_emit_vertex_buffer(r300);
1194 }
1195
1196 r300->dirty_hw++;
1197 }