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