Merge commit 'origin/master' into gallium-msaa
[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 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL, 2);
721 OUT_CS(rs->vap_vtx_state_cntl);
722 OUT_CS(rs->vap_vsm_vtx_assm);
723 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0, 2);
724 OUT_CS(rs->vap_out_vtx_fmt[0]);
725 OUT_CS(rs->vap_out_vtx_fmt[1]);
726
727 if (r300->screen->caps.is_r500) {
728 OUT_CS_REG_SEQ(R500_RS_IP_0, count);
729 } else {
730 OUT_CS_REG_SEQ(R300_RS_IP_0, count);
731 }
732 OUT_CS_TABLE(rs->ip, count);
733 for (i = 0; i < count; i++) {
734 DBG(r300, DBG_DRAW, " : ip %d: 0x%08x\n", i, rs->ip[i]);
735 }
736
737 OUT_CS_REG_SEQ(R300_RS_COUNT, 2);
738 OUT_CS(rs->count);
739 OUT_CS(rs->inst_count);
740
741 if (r300->screen->caps.is_r500) {
742 OUT_CS_REG_SEQ(R500_RS_INST_0, count);
743 } else {
744 OUT_CS_REG_SEQ(R300_RS_INST_0, count);
745 }
746 OUT_CS_TABLE(rs->inst, count);
747 for (i = 0; i < count; i++) {
748 DBG(r300, DBG_DRAW, " : inst %d: 0x%08x\n", i, rs->inst[i]);
749 }
750
751 DBG(r300, DBG_DRAW, " : count: 0x%08x inst_count: 0x%08x\n",
752 rs->count, rs->inst_count);
753
754 END_CS;
755 }
756
757 void r300_emit_scissor_state(struct r300_context* r300,
758 unsigned size, void* state)
759 {
760 struct pipe_scissor_state* scissor = (struct pipe_scissor_state*)state;
761 CS_LOCALS(r300);
762
763 BEGIN_CS(size);
764 OUT_CS_REG_SEQ(R300_SC_CLIPRECT_TL_0, 2);
765 if (r300->screen->caps.is_r500) {
766 OUT_CS((scissor->minx << R300_CLIPRECT_X_SHIFT) |
767 (scissor->miny << R300_CLIPRECT_Y_SHIFT));
768 OUT_CS(((scissor->maxx - 1) << R300_CLIPRECT_X_SHIFT) |
769 ((scissor->maxy - 1) << R300_CLIPRECT_Y_SHIFT));
770 } else {
771 OUT_CS(((scissor->minx + 1440) << R300_CLIPRECT_X_SHIFT) |
772 ((scissor->miny + 1440) << R300_CLIPRECT_Y_SHIFT));
773 OUT_CS(((scissor->maxx + 1440-1) << R300_CLIPRECT_X_SHIFT) |
774 ((scissor->maxy + 1440-1) << R300_CLIPRECT_Y_SHIFT));
775 }
776 END_CS;
777 }
778
779 void r300_emit_textures_state(struct r300_context *r300,
780 unsigned size, void *state)
781 {
782 struct r300_textures_state *allstate = (struct r300_textures_state*)state;
783 struct r300_texture_sampler_state *texstate;
784 unsigned i;
785 CS_LOCALS(r300);
786
787 BEGIN_CS(size);
788 OUT_CS_REG(R300_TX_ENABLE, allstate->tx_enable);
789
790 for (i = 0; i < allstate->count; i++) {
791 if ((1 << i) & allstate->tx_enable) {
792 texstate = &allstate->regs[i];
793
794 OUT_CS_REG(R300_TX_FILTER0_0 + (i * 4), texstate->filter0);
795 OUT_CS_REG(R300_TX_FILTER1_0 + (i * 4), texstate->filter1);
796 OUT_CS_REG(R300_TX_BORDER_COLOR_0 + (i * 4),
797 texstate->border_color);
798
799 OUT_CS_REG(R300_TX_FORMAT0_0 + (i * 4), texstate->format.format0);
800 OUT_CS_REG(R300_TX_FORMAT1_0 + (i * 4), texstate->format.format1);
801 OUT_CS_REG(R300_TX_FORMAT2_0 + (i * 4), texstate->format.format2);
802
803 OUT_CS_REG_SEQ(R300_TX_OFFSET_0 + (i * 4), 1);
804 OUT_CS_TEX_RELOC(r300_texture(allstate->sampler_views[i]->base.texture),
805 texstate->format.tile_config,
806 RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM, 0, 0);
807 }
808 }
809 END_CS;
810 }
811
812 void r300_emit_aos(struct r300_context* r300, unsigned offset, boolean indexed)
813 {
814 struct pipe_vertex_buffer *vb1, *vb2, *vbuf = r300->vertex_buffer;
815 struct pipe_vertex_element *velem = r300->velems->velem;
816 int i;
817 unsigned size1, size2, aos_count = r300->velems->count;
818 unsigned packet_size = (aos_count * 3 + 1) / 2;
819 CS_LOCALS(r300);
820
821 for (i = 0; i < aos_count; i++) {
822 if ((vbuf[velem[i].vertex_buffer_index].buffer_offset + velem[i].src_offset) % 4 != 0) {
823 /* XXX We must align the buffer. */
824 assert(0);
825 fprintf(stderr, "r300: Unaligned vertex buffer offsets aren't supported, aborting..\n");
826 abort();
827 }
828 }
829
830 BEGIN_CS(2 + packet_size + aos_count * 2);
831 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, packet_size);
832 OUT_CS(aos_count | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
833
834 for (i = 0; i < aos_count - 1; i += 2) {
835 vb1 = &vbuf[velem[i].vertex_buffer_index];
836 vb2 = &vbuf[velem[i+1].vertex_buffer_index];
837 size1 = util_format_get_blocksize(velem[i].src_format);
838 size2 = util_format_get_blocksize(velem[i+1].src_format);
839
840 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride) |
841 R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(vb2->stride));
842 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
843 OUT_CS(vb2->buffer_offset + velem[i+1].src_offset + offset * vb2->stride);
844 }
845
846 if (aos_count & 1) {
847 vb1 = &vbuf[velem[i].vertex_buffer_index];
848 size1 = util_format_get_blocksize(velem[i].src_format);
849
850 OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride));
851 OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
852 }
853
854 for (i = 0; i < aos_count; i++) {
855 OUT_CS_BUF_RELOC_NO_OFFSET(vbuf[velem[i].vertex_buffer_index].buffer,
856 RADEON_GEM_DOMAIN_GTT, 0, 0);
857 }
858 END_CS;
859 }
860
861 void r300_emit_vertex_buffer(struct r300_context* r300)
862 {
863 CS_LOCALS(r300);
864
865 DBG(r300, DBG_DRAW, "r300: Preparing vertex buffer %p for render, "
866 "vertex size %d\n", r300->vbo,
867 r300->vertex_info.size);
868 /* Set the pointer to our vertex buffer. The emitted values are this:
869 * PACKET3 [3D_LOAD_VBPNTR]
870 * COUNT [1]
871 * FORMAT [size | stride << 8]
872 * OFFSET [offset into BO]
873 * VBPNTR [relocated BO]
874 */
875 BEGIN_CS(7);
876 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, 3);
877 OUT_CS(1);
878 OUT_CS(r300->vertex_info.size |
879 (r300->vertex_info.size << 8));
880 OUT_CS(r300->vbo_offset);
881 OUT_CS_BUF_RELOC(r300->vbo, 0, RADEON_GEM_DOMAIN_GTT, 0, 0);
882 END_CS;
883 }
884
885 void r300_emit_vertex_stream_state(struct r300_context* r300,
886 unsigned size, void* state)
887 {
888 struct r300_vertex_stream_state *streams =
889 (struct r300_vertex_stream_state*)state;
890 unsigned i;
891 CS_LOCALS(r300);
892
893 DBG(r300, DBG_DRAW, "r300: PSC emit:\n");
894
895 BEGIN_CS(size);
896 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0, streams->count);
897 OUT_CS_TABLE(streams->vap_prog_stream_cntl, streams->count);
898 for (i = 0; i < streams->count; i++) {
899 DBG(r300, DBG_DRAW, " : prog_stream_cntl%d: 0x%08x\n", i,
900 streams->vap_prog_stream_cntl[i]);
901 }
902 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0, streams->count);
903 OUT_CS_TABLE(streams->vap_prog_stream_cntl_ext, streams->count);
904 for (i = 0; i < streams->count; i++) {
905 DBG(r300, DBG_DRAW, " : prog_stream_cntl_ext%d: 0x%08x\n", i,
906 streams->vap_prog_stream_cntl_ext[i]);
907 }
908 END_CS;
909 }
910
911 void r300_emit_pvs_flush(struct r300_context* r300, unsigned size, void* state)
912 {
913 CS_LOCALS(r300);
914
915 BEGIN_CS(size);
916 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG, 0x0);
917 END_CS;
918 }
919
920 void r300_emit_vs_state(struct r300_context* r300, unsigned size, void* state)
921 {
922 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)state;
923 struct r300_vertex_program_code* code = &vs->code;
924 struct r300_screen* r300screen = r300->screen;
925 unsigned instruction_count = code->length / 4;
926 unsigned i;
927
928 unsigned vtx_mem_size = r300screen->caps.is_r500 ? 128 : 72;
929 unsigned input_count = MAX2(util_bitcount(code->InputsRead), 1);
930 unsigned output_count = MAX2(util_bitcount(code->OutputsWritten), 1);
931 unsigned temp_count = MAX2(code->num_temporaries, 1);
932
933 unsigned pvs_num_slots = MIN3(vtx_mem_size / input_count,
934 vtx_mem_size / output_count, 10);
935 unsigned pvs_num_controllers = MIN2(vtx_mem_size / temp_count, 6);
936
937 unsigned imm_first = vs->externals_count;
938 unsigned imm_end = vs->code.constants.Count;
939 unsigned imm_count = vs->immediates_count;
940
941 CS_LOCALS(r300);
942
943 BEGIN_CS(size);
944 /* R300_VAP_PVS_CODE_CNTL_0
945 * R300_VAP_PVS_CONST_CNTL
946 * R300_VAP_PVS_CODE_CNTL_1
947 * See the r5xx docs for instructions on how to use these. */
948 OUT_CS_REG_SEQ(R300_VAP_PVS_CODE_CNTL_0, 3);
949 OUT_CS(R300_PVS_FIRST_INST(0) |
950 R300_PVS_XYZW_VALID_INST(instruction_count - 1) |
951 R300_PVS_LAST_INST(instruction_count - 1));
952 OUT_CS(R300_PVS_MAX_CONST_ADDR(code->constants.Count - 1));
953 OUT_CS(instruction_count - 1);
954
955 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG, 0);
956 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, code->length);
957 OUT_CS_TABLE(code->body.d, code->length);
958
959 OUT_CS_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(pvs_num_slots) |
960 R300_PVS_NUM_CNTLRS(pvs_num_controllers) |
961 R300_PVS_NUM_FPUS(r300screen->caps.num_vert_fpus) |
962 R300_PVS_VF_MAX_VTX_NUM(12) |
963 (r300screen->caps.is_r500 ? R500_TCL_STATE_OPTIMIZATION : 0));
964
965 /* Emit immediates. */
966 if (imm_count) {
967 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
968 (r300->screen->caps.is_r500 ?
969 R500_PVS_CONST_START : R300_PVS_CONST_START) +
970 imm_first);
971 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, imm_count * 4);
972 for (i = imm_first; i < imm_end; i++) {
973 const float *data = vs->code.constants.Constants[i].u.Immediate;
974 OUT_CS_TABLE(data, 4);
975 }
976 }
977 END_CS;
978 }
979
980 void r300_emit_vs_constants(struct r300_context* r300,
981 unsigned size, void *state)
982 {
983 unsigned count =
984 ((struct r300_vertex_shader*)r300->vs_state.state)->externals_count;
985 struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
986 CS_LOCALS(r300);
987
988 if (!count)
989 return;
990
991 BEGIN_CS(size);
992 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
993 (r300->screen->caps.is_r500 ?
994 R500_PVS_CONST_START : R300_PVS_CONST_START));
995 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, count * 4);
996 OUT_CS_TABLE(buf->constants, count * 4);
997 END_CS;
998 }
999
1000 void r300_emit_viewport_state(struct r300_context* r300,
1001 unsigned size, void* state)
1002 {
1003 struct r300_viewport_state* viewport = (struct r300_viewport_state*)state;
1004 CS_LOCALS(r300);
1005
1006 BEGIN_CS(size);
1007 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE, 6);
1008 OUT_CS_32F(viewport->xscale);
1009 OUT_CS_32F(viewport->xoffset);
1010 OUT_CS_32F(viewport->yscale);
1011 OUT_CS_32F(viewport->yoffset);
1012 OUT_CS_32F(viewport->zscale);
1013 OUT_CS_32F(viewport->zoffset);
1014 OUT_CS_REG(R300_VAP_VTE_CNTL, viewport->vte_control);
1015 END_CS;
1016 }
1017
1018 void r300_emit_ztop_state(struct r300_context* r300,
1019 unsigned size, void* state)
1020 {
1021 struct r300_ztop_state* ztop = (struct r300_ztop_state*)state;
1022 CS_LOCALS(r300);
1023
1024 BEGIN_CS(size);
1025 OUT_CS_REG(R300_ZB_ZTOP, ztop->z_buffer_top);
1026 END_CS;
1027 }
1028
1029 void r300_emit_texture_cache_inval(struct r300_context* r300, unsigned size, void* state)
1030 {
1031 CS_LOCALS(r300);
1032
1033 BEGIN_CS(size);
1034 OUT_CS_REG(R300_TX_INVALTAGS, 0);
1035 END_CS;
1036 }
1037
1038 void r300_emit_buffer_validate(struct r300_context *r300,
1039 boolean do_validate_vertex_buffers,
1040 struct pipe_resource *index_buffer)
1041 {
1042 struct pipe_framebuffer_state* fb =
1043 (struct pipe_framebuffer_state*)r300->fb_state.state;
1044 struct r300_textures_state *texstate =
1045 (struct r300_textures_state*)r300->textures_state.state;
1046 struct r300_texture* tex;
1047 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
1048 struct pipe_vertex_element *velem = r300->velems->velem;
1049 struct pipe_resource *pbuf;
1050 unsigned i;
1051 boolean invalid = FALSE;
1052
1053 /* upload buffers first */
1054 if (r300->any_user_vbs) {
1055 r300_upload_user_buffers(r300);
1056 r300->any_user_vbs = false;
1057 }
1058
1059 /* Clean out BOs. */
1060 r300->rws->reset_bos(r300->rws);
1061
1062 validate:
1063 /* Color buffers... */
1064 for (i = 0; i < fb->nr_cbufs; i++) {
1065 tex = r300_texture(fb->cbufs[i]->texture);
1066 assert(tex && tex->buffer && "cbuf is marked, but NULL!");
1067 if (!r300_add_texture(r300->rws, tex,
1068 0, RADEON_GEM_DOMAIN_VRAM)) {
1069 r300->context.flush(&r300->context, 0, NULL);
1070 goto validate;
1071 }
1072 }
1073 /* ...depth buffer... */
1074 if (fb->zsbuf) {
1075 tex = r300_texture(fb->zsbuf->texture);
1076 assert(tex && tex->buffer && "zsbuf is marked, but NULL!");
1077 if (!r300_add_texture(r300->rws, tex,
1078 0, RADEON_GEM_DOMAIN_VRAM)) {
1079 r300->context.flush(&r300->context, 0, NULL);
1080 goto validate;
1081 }
1082 }
1083 /* ...textures... */
1084 for (i = 0; i < texstate->count; i++) {
1085 if (!(texstate->tx_enable & (1 << i))) {
1086 continue;
1087 }
1088
1089 tex = r300_texture(texstate->sampler_views[i]->base.texture);
1090 if (!r300_add_texture(r300->rws, tex,
1091 RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM, 0)) {
1092 r300->context.flush(&r300->context, 0, NULL);
1093 goto validate;
1094 }
1095 }
1096 /* ...occlusion query buffer... */
1097 if (r300->query_start.dirty) {
1098 if (!r300_add_buffer(r300->rws, r300->oqbo,
1099 0, RADEON_GEM_DOMAIN_GTT)) {
1100 r300->context.flush(&r300->context, 0, NULL);
1101 goto validate;
1102 }
1103 }
1104 /* ...vertex buffer for SWTCL path... */
1105 if (r300->vbo) {
1106 if (!r300_add_buffer(r300->rws, r300->vbo,
1107 RADEON_GEM_DOMAIN_GTT, 0)) {
1108 r300->context.flush(&r300->context, 0, NULL);
1109 goto validate;
1110 }
1111 }
1112 /* ...vertex buffers for HWTCL path... */
1113 if (do_validate_vertex_buffers) {
1114 for (i = 0; i < r300->velems->count; i++) {
1115 pbuf = vbuf[velem[i].vertex_buffer_index].buffer;
1116
1117 if (!r300_add_buffer(r300->rws, pbuf,
1118 RADEON_GEM_DOMAIN_GTT, 0)) {
1119 r300->context.flush(&r300->context, 0, NULL);
1120 goto validate;
1121 }
1122 }
1123 }
1124 /* ...and index buffer for HWTCL path. */
1125 if (index_buffer) {
1126 if (!r300_add_buffer(r300->rws, index_buffer,
1127 RADEON_GEM_DOMAIN_GTT, 0)) {
1128 r300->context.flush(&r300->context, 0, NULL);
1129 goto validate;
1130 }
1131 }
1132 if (!r300->rws->validate(r300->rws)) {
1133 r300->context.flush(&r300->context, 0, NULL);
1134 if (invalid) {
1135 /* Well, hell. */
1136 fprintf(stderr, "r300: Stuck in validation loop, gonna quit now.\n");
1137 abort();
1138 }
1139 invalid = TRUE;
1140 goto validate;
1141 }
1142 }
1143
1144 unsigned r300_get_num_dirty_dwords(struct r300_context *r300)
1145 {
1146 struct r300_atom* atom;
1147 unsigned dwords = 0;
1148
1149 foreach(atom, &r300->atom_list) {
1150 if (atom->dirty) {
1151 dwords += atom->size;
1152 }
1153 }
1154
1155 /* emit_query_end is not atomized. */
1156 dwords += 26;
1157 /* let's reserve some more, just in case */
1158 dwords += 32;
1159
1160 return dwords;
1161 }
1162
1163 /* Emit all dirty state. */
1164 void r300_emit_dirty_state(struct r300_context* r300)
1165 {
1166 struct r300_screen* r300screen = r300->screen;
1167 struct r300_atom* atom;
1168
1169 foreach(atom, &r300->atom_list) {
1170 if (atom->dirty) {
1171 atom->emit(r300, atom->size, atom->state);
1172 if (SCREEN_DBG_ON(r300->screen, DBG_STATS)) {
1173 atom->counter++;
1174 }
1175 atom->dirty = FALSE;
1176 }
1177 }
1178
1179 /* Emit the VBO for SWTCL. */
1180 if (!r300screen->caps.has_tcl) {
1181 r300_emit_vertex_buffer(r300);
1182 }
1183
1184 r300->dirty_hw++;
1185 }