53256fc6dd36844ddf9b36877811894da311e5b5
[mesa.git] / src / gallium / drivers / r300 / r300_emit.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 /* r300_emit: Functions for emitting state. */
24
25 #include "r300_emit.h"
26
27 #include "r300_fs.h"
28 #include "r300_vs.h"
29
30 void r300_emit_blend_state(struct r300_context* r300,
31 struct r300_blend_state* blend)
32 {
33 CS_LOCALS(r300);
34 BEGIN_CS(7);
35 OUT_CS_REG_SEQ(R300_RB3D_CBLEND, 2);
36 OUT_CS(blend->blend_control);
37 OUT_CS(blend->alpha_blend_control);
38 OUT_CS_REG(R300_RB3D_ROPCNTL, blend->rop);
39 OUT_CS_REG(R300_RB3D_DITHER_CTL, blend->dither);
40 END_CS;
41 }
42
43 void r300_emit_blend_color_state(struct r300_context* r300,
44 struct r300_blend_color_state* bc)
45 {
46 struct r300_screen* r300screen = r300_screen(r300->context.screen);
47 CS_LOCALS(r300);
48
49 if (r300screen->caps->is_r500) {
50 BEGIN_CS(3);
51 OUT_CS_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR, 2);
52 OUT_CS(bc->blend_color_red_alpha);
53 OUT_CS(bc->blend_color_green_blue);
54 END_CS;
55 } else {
56 BEGIN_CS(2);
57 OUT_CS_REG(R300_RB3D_BLEND_COLOR, bc->blend_color);
58 END_CS;
59 }
60 }
61
62 void r300_emit_clip_state(struct r300_context* r300,
63 struct pipe_clip_state* clip)
64 {
65 int i;
66 struct r300_screen* r300screen = r300_screen(r300->context.screen);
67 CS_LOCALS(r300);
68
69 if (r300screen->caps->has_tcl) {
70 BEGIN_CS(5 + (6 * 4));
71 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
72 (r300screen->caps->is_r500 ?
73 R500_PVS_UCP_START : R300_PVS_UCP_START));
74 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, 6 * 4);
75 for (i = 0; i < 6; i++) {
76 OUT_CS_32F(clip->ucp[i][0]);
77 OUT_CS_32F(clip->ucp[i][1]);
78 OUT_CS_32F(clip->ucp[i][2]);
79 OUT_CS_32F(clip->ucp[i][3]);
80 }
81 OUT_CS_REG(R300_VAP_CLIP_CNTL, ((1 << clip->nr) - 1) |
82 R300_PS_UCP_MODE_CLIP_AS_TRIFAN);
83 END_CS;
84 } else {
85 BEGIN_CS(2);
86 OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
87 END_CS;
88 }
89
90 }
91
92 void r300_emit_dsa_state(struct r300_context* r300,
93 struct r300_dsa_state* dsa)
94 {
95 struct r300_screen* r300screen = r300_screen(r300->context.screen);
96 CS_LOCALS(r300);
97
98 BEGIN_CS(r300screen->caps->is_r500 ? 8 : 8);
99 OUT_CS_REG(R300_FG_ALPHA_FUNC, dsa->alpha_function);
100 /* XXX figure out the r300 counterpart for this */
101 if (r300screen->caps->is_r500) {
102 /* OUT_CS_REG(R500_FG_ALPHA_VALUE, dsa->alpha_reference); */
103 }
104 OUT_CS_REG_SEQ(R300_ZB_CNTL, 3);
105 OUT_CS(dsa->z_buffer_control);
106 OUT_CS(dsa->z_stencil_control);
107 OUT_CS(dsa->stencil_ref_mask);
108 OUT_CS_REG(R300_ZB_ZTOP, dsa->z_buffer_top);
109 if (r300screen->caps->is_r500) {
110 /* OUT_CS_REG(R500_ZB_STENCILREFMASK_BF, dsa->stencil_ref_bf); */
111 }
112 END_CS;
113 }
114
115 static const float * get_shader_constant(
116 struct r300_context * r300,
117 struct rc_constant * constant,
118 struct r300_constant_buffer * externals)
119 {
120 static const float zero[4] = { 0.0, 0.0, 0.0, 0.0 };
121 switch(constant->Type) {
122 case RC_CONSTANT_EXTERNAL:
123 return externals->constants[constant->u.External];
124
125 case RC_CONSTANT_IMMEDIATE:
126 return constant->u.Immediate;
127
128 default:
129 debug_printf("r300: Implementation error: Unhandled constant type %i\n",
130 constant->Type);
131 return zero;
132 }
133 }
134
135 /* Convert a normal single-precision float into the 7.16 format
136 * used by the R300 fragment shader.
137 */
138 static uint32_t pack_float24(float f)
139 {
140 union {
141 float fl;
142 uint32_t u;
143 } u;
144 float mantissa;
145 int exponent;
146 uint32_t float24 = 0;
147
148 if (f == 0.0)
149 return 0;
150
151 u.fl = f;
152
153 mantissa = frexpf(f, &exponent);
154
155 /* Handle -ve */
156 if (mantissa < 0) {
157 float24 |= (1 << 23);
158 mantissa = mantissa * -1.0;
159 }
160 /* Handle exponent, bias of 63 */
161 exponent += 62;
162 float24 |= (exponent << 16);
163 /* Kill 7 LSB of mantissa */
164 float24 |= (u.u & 0x7FFFFF) >> 7;
165
166 return float24;
167 }
168
169 void r300_emit_fragment_program_code(struct r300_context* r300,
170 struct rX00_fragment_program_code* generic_code,
171 struct r300_constant_buffer* externals)
172 {
173 struct r300_fragment_program_code * code = &generic_code->code.r300;
174 struct rc_constant_list * constants = &generic_code->constants;
175 int i;
176 CS_LOCALS(r300);
177
178 BEGIN_CS(15 +
179 code->alu.length * 4 +
180 (code->tex.length ? (1 + code->tex.length) : 0) +
181 (constants->Count ? (1 + constants->Count * 4) : 0));
182
183 OUT_CS_REG(R300_US_CONFIG, code->config);
184 OUT_CS_REG(R300_US_PIXSIZE, code->pixsize);
185 OUT_CS_REG(R300_US_CODE_OFFSET, code->code_offset);
186
187 OUT_CS_REG_SEQ(R300_US_CODE_ADDR_0, 4);
188 for(i = 0; i < 4; ++i)
189 OUT_CS(code->code_addr[i]);
190
191 OUT_CS_REG_SEQ(R300_US_ALU_RGB_INST_0, code->alu.length);
192 for (i = 0; i < code->alu.length; i++)
193 OUT_CS(code->alu.inst[i].rgb_inst);
194
195 OUT_CS_REG_SEQ(R300_US_ALU_RGB_ADDR_0, code->alu.length);
196 for (i = 0; i < code->alu.length; i++)
197 OUT_CS(code->alu.inst[i].rgb_addr);
198
199 OUT_CS_REG_SEQ(R300_US_ALU_ALPHA_INST_0, code->alu.length);
200 for (i = 0; i < code->alu.length; i++)
201 OUT_CS(code->alu.inst[i].alpha_inst);
202
203 OUT_CS_REG_SEQ(R300_US_ALU_ALPHA_ADDR_0, code->alu.length);
204 for (i = 0; i < code->alu.length; i++)
205 OUT_CS(code->alu.inst[i].alpha_addr);
206
207 if (code->tex.length) {
208 OUT_CS_REG_SEQ(R300_US_TEX_INST_0, code->tex.length);
209 for(i = 0; i < code->tex.length; ++i)
210 OUT_CS(code->tex.inst[i]);
211 }
212
213 if (constants->Count) {
214 OUT_CS_ONE_REG(R300_PFS_PARAM_0_X, constants->Count * 4);
215 for(i = 0; i < constants->Count; ++i) {
216 const float * data = get_shader_constant(r300, &constants->Constants[i], externals);
217 OUT_CS(pack_float24(data[0]));
218 OUT_CS(pack_float24(data[1]));
219 OUT_CS(pack_float24(data[2]));
220 OUT_CS(pack_float24(data[3]));
221 }
222 }
223
224 END_CS;
225 }
226
227 void r500_emit_fragment_program_code(struct r300_context* r300,
228 struct rX00_fragment_program_code* generic_code,
229 struct r300_constant_buffer* externals)
230 {
231 struct r500_fragment_program_code * code = &generic_code->code.r500;
232 struct rc_constant_list * constants = &generic_code->constants;
233 int i;
234 CS_LOCALS(r300);
235
236 BEGIN_CS(13 +
237 ((code->inst_end + 1) * 6) +
238 (constants->Count ? (3 + (constants->Count * 4)) : 0));
239 OUT_CS_REG(R500_US_CONFIG, 0);
240 OUT_CS_REG(R500_US_PIXSIZE, code->max_temp_idx);
241 OUT_CS_REG(R500_US_CODE_RANGE,
242 R500_US_CODE_RANGE_ADDR(0) | R500_US_CODE_RANGE_SIZE(code->inst_end));
243 OUT_CS_REG(R500_US_CODE_OFFSET, 0);
244 OUT_CS_REG(R500_US_CODE_ADDR,
245 R500_US_CODE_START_ADDR(0) | R500_US_CODE_END_ADDR(code->inst_end));
246
247 OUT_CS_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_INSTR);
248 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, (code->inst_end + 1) * 6);
249 for (i = 0; i <= code->inst_end; i++) {
250 OUT_CS(code->inst[i].inst0);
251 OUT_CS(code->inst[i].inst1);
252 OUT_CS(code->inst[i].inst2);
253 OUT_CS(code->inst[i].inst3);
254 OUT_CS(code->inst[i].inst4);
255 OUT_CS(code->inst[i].inst5);
256 }
257
258 if (constants->Count) {
259 OUT_CS_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_CONST);
260 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, constants->Count * 4);
261 for (i = 0; i < constants->Count; i++) {
262 const float * data = get_shader_constant(r300, &constants->Constants[i], externals);
263 OUT_CS_32F(data[0]);
264 OUT_CS_32F(data[1]);
265 OUT_CS_32F(data[2]);
266 OUT_CS_32F(data[3]);
267 }
268 }
269
270 END_CS;
271 }
272
273 void r300_emit_fb_state(struct r300_context* r300,
274 struct pipe_framebuffer_state* fb)
275 {
276 struct r300_texture* tex;
277 unsigned pixpitch;
278 int i;
279 CS_LOCALS(r300);
280
281 BEGIN_CS((10 * fb->nr_cbufs) + (fb->zsbuf ? 10 : 0) + 4);
282 for (i = 0; i < fb->nr_cbufs; i++) {
283 tex = (struct r300_texture*)fb->cbufs[i]->texture;
284 assert(tex && tex->buffer && "cbuf is marked, but NULL!");
285 pixpitch = tex->stride / tex->tex.block.size;
286
287 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0 + (4 * i), 1);
288 OUT_CS_RELOC(tex->buffer, 0, 0, RADEON_GEM_DOMAIN_VRAM, 0);
289
290 OUT_CS_REG_SEQ(R300_RB3D_COLORPITCH0 + (4 * i), 1);
291 OUT_CS_RELOC(tex->buffer, pixpitch |
292 r300_translate_colorformat(tex->tex.format), 0,
293 RADEON_GEM_DOMAIN_VRAM, 0);
294
295 OUT_CS_REG(R300_US_OUT_FMT_0 + (4 * i),
296 r300_translate_out_fmt(fb->cbufs[i]->format));
297 }
298
299 if (fb->zsbuf) {
300 tex = (struct r300_texture*)fb->zsbuf->texture;
301 assert(tex && tex->buffer && "zsbuf is marked, but NULL!");
302 pixpitch = tex->stride / tex->tex.block.size;
303
304 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET, 1);
305 OUT_CS_RELOC(tex->buffer, 0, 0, RADEON_GEM_DOMAIN_VRAM, 0);
306
307 OUT_CS_REG(R300_ZB_FORMAT, r300_translate_zsformat(tex->tex.format));
308
309 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH, 1);
310 OUT_CS_RELOC(tex->buffer, pixpitch, 0, RADEON_GEM_DOMAIN_VRAM, 0);
311 }
312
313 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT,
314 R300_RB3D_DSTCACHE_CTLSTAT_DC_FREE_FREE_3D_TAGS |
315 R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
316 OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT,
317 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
318 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE);
319 END_CS;
320 }
321
322 void r300_emit_rs_state(struct r300_context* r300, struct r300_rs_state* rs)
323 {
324 CS_LOCALS(r300);
325
326 BEGIN_CS(20);
327 OUT_CS_REG(R300_VAP_CNTL_STATUS, rs->vap_control_status);
328 OUT_CS_REG(R300_GA_POINT_SIZE, rs->point_size);
329 OUT_CS_REG_SEQ(R300_GA_POINT_MINMAX, 2);
330 OUT_CS(rs->point_minmax);
331 OUT_CS(rs->line_control);
332 OUT_CS_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE, 6);
333 OUT_CS(rs->depth_scale_front);
334 OUT_CS(rs->depth_offset_front);
335 OUT_CS(rs->depth_scale_back);
336 OUT_CS(rs->depth_offset_back);
337 OUT_CS(rs->polygon_offset_enable);
338 OUT_CS(rs->cull_mode);
339 OUT_CS_REG(R300_GA_LINE_STIPPLE_CONFIG, rs->line_stipple_config);
340 OUT_CS_REG(R300_GA_LINE_STIPPLE_VALUE, rs->line_stipple_value);
341 OUT_CS_REG(R300_GA_COLOR_CONTROL, rs->color_control);
342 END_CS;
343 }
344
345 void r300_emit_rs_block_state(struct r300_context* r300,
346 struct r300_rs_block* rs)
347 {
348 int i;
349 struct r300_screen* r300screen = r300_screen(r300->context.screen);
350 CS_LOCALS(r300);
351
352 BEGIN_CS(21);
353 if (r300screen->caps->is_r500) {
354 OUT_CS_REG_SEQ(R500_RS_IP_0, 8);
355 } else {
356 OUT_CS_REG_SEQ(R300_RS_IP_0, 8);
357 }
358 for (i = 0; i < 8; i++) {
359 OUT_CS(rs->ip[i]);
360 /* debug_printf("ip %d: 0x%08x\n", i, rs->ip[i]); */
361 }
362
363 OUT_CS_REG_SEQ(R300_RS_COUNT, 2);
364 OUT_CS(rs->count);
365 OUT_CS(rs->inst_count);
366
367 if (r300screen->caps->is_r500) {
368 OUT_CS_REG_SEQ(R500_RS_INST_0, 8);
369 } else {
370 OUT_CS_REG_SEQ(R300_RS_INST_0, 8);
371 }
372 for (i = 0; i < 8; i++) {
373 OUT_CS(rs->inst[i]);
374 /* debug_printf("inst %d: 0x%08x\n", i, rs->inst[i]); */
375 }
376
377 /* debug_printf("count: 0x%08x inst_count: 0x%08x\n", rs->count,
378 * rs->inst_count); */
379
380 END_CS;
381 }
382
383 void r300_emit_scissor_state(struct r300_context* r300,
384 struct r300_scissor_state* scissor)
385 {
386 CS_LOCALS(r300);
387
388 BEGIN_CS(3);
389 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL, 2);
390 OUT_CS(scissor->scissor_top_left);
391 OUT_CS(scissor->scissor_bottom_right);
392 END_CS;
393 }
394
395 void r300_emit_texture(struct r300_context* r300,
396 struct r300_sampler_state* sampler,
397 struct r300_texture* tex,
398 unsigned offset)
399 {
400 CS_LOCALS(r300);
401
402 BEGIN_CS(16);
403 OUT_CS_REG(R300_TX_FILTER0_0 + (offset * 4), sampler->filter0);
404 OUT_CS_REG(R300_TX_FILTER1_0 + (offset * 4), sampler->filter1);
405 OUT_CS_REG(R300_TX_BORDER_COLOR_0 + (offset * 4), sampler->border_color);
406
407 OUT_CS_REG(R300_TX_FORMAT0_0 + (offset * 4), tex->state.format0);
408 OUT_CS_REG(R300_TX_FORMAT1_0 + (offset * 4), tex->state.format1);
409 OUT_CS_REG(R300_TX_FORMAT2_0 + (offset * 4), tex->state.format2);
410 OUT_CS_REG_SEQ(R300_TX_OFFSET_0 + (offset * 4), 1);
411 OUT_CS_RELOC(tex->buffer, 0,
412 RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM, 0, 0);
413 END_CS;
414 }
415
416 void r300_emit_vertex_buffer(struct r300_context* r300)
417 {
418 CS_LOCALS(r300);
419
420 debug_printf("r300: Preparing vertex buffer %p for render, "
421 "vertex size %d\n", r300->vbo,
422 r300->vertex_info.vinfo.size);
423 /* Set the pointer to our vertex buffer. The emitted values are this:
424 * PACKET3 [3D_LOAD_VBPNTR]
425 * COUNT [1]
426 * FORMAT [size | stride << 8]
427 * OFFSET [offset into BO]
428 * VBPNTR [relocated BO]
429 */
430 BEGIN_CS(7);
431 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, 3);
432 OUT_CS(1);
433 OUT_CS(r300->vertex_info.vinfo.size |
434 (r300->vertex_info.vinfo.size << 8));
435 OUT_CS(r300->vbo_offset);
436 OUT_CS_RELOC(r300->vbo, 0, RADEON_GEM_DOMAIN_GTT, 0, 0);
437 END_CS;
438 }
439
440 void r300_emit_vertex_format_state(struct r300_context* r300)
441 {
442 int i;
443 CS_LOCALS(r300);
444
445 BEGIN_CS(26);
446 OUT_CS_REG(R300_VAP_VTX_SIZE, r300->vertex_info.vinfo.size);
447
448 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL, 2);
449 OUT_CS(r300->vertex_info.vinfo.hwfmt[0]);
450 OUT_CS(r300->vertex_info.vinfo.hwfmt[1]);
451 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0, 2);
452 OUT_CS(r300->vertex_info.vinfo.hwfmt[2]);
453 OUT_CS(r300->vertex_info.vinfo.hwfmt[3]);
454 /* for (i = 0; i < 4; i++) {
455 * debug_printf("hwfmt%d: 0x%08x\n", i,
456 * r300->vertex_info.vinfo.hwfmt[i]);
457 * } */
458
459 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0, 8);
460 for (i = 0; i < 8; i++) {
461 OUT_CS(r300->vertex_info.vap_prog_stream_cntl[i]);
462 /* debug_printf("prog_stream_cntl%d: 0x%08x\n", i,
463 * r300->vertex_info.vap_prog_stream_cntl[i]); */
464 }
465 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0, 8);
466 for (i = 0; i < 8; i++) {
467 OUT_CS(r300->vertex_info.vap_prog_stream_cntl_ext[i]);
468 /* debug_printf("prog_stream_cntl_ext%d: 0x%08x\n", i,
469 * r300->vertex_info.vap_prog_stream_cntl_ext[i]); */
470 }
471 END_CS;
472 }
473
474 void r300_emit_vertex_program_code(struct r300_context* r300,
475 struct r300_vertex_program_code* code,
476 struct r300_constant_buffer* constants)
477 {
478 int i;
479 struct r300_screen* r300screen = r300_screen(r300->context.screen);
480 unsigned instruction_count = code->length / 4;
481 CS_LOCALS(r300);
482
483 if (!r300screen->caps->has_tcl) {
484 debug_printf("r300: Implementation error: emit_vertex_shader called,"
485 " but has_tcl is FALSE!\n");
486 return;
487 }
488
489 if (code->constants.Count) {
490 BEGIN_CS(14 + code->length + (code->constants.Count * 4));
491 } else {
492 BEGIN_CS(11 + code->length);
493 }
494
495 /* R300_VAP_PVS_CODE_CNTL_0
496 * R300_VAP_PVS_CONST_CNTL
497 * R300_VAP_PVS_CODE_CNTL_1
498 * See the r5xx docs for instructions on how to use these.
499 * XXX these could be optimized to select better values... */
500 OUT_CS_REG_SEQ(R300_VAP_PVS_CODE_CNTL_0, 3);
501 OUT_CS(R300_PVS_FIRST_INST(0) |
502 R300_PVS_XYZW_VALID_INST(instruction_count - 1) |
503 R300_PVS_LAST_INST(instruction_count - 1));
504 OUT_CS(R300_PVS_MAX_CONST_ADDR(code->constants.Count - 1));
505 OUT_CS(instruction_count - 1);
506
507 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG, 0);
508 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, code->length);
509 for (i = 0; i < code->length; i++)
510 OUT_CS(code->body.d[i]);
511
512 if (code->constants.Count) {
513 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
514 (r300screen->caps->is_r500 ?
515 R500_PVS_CONST_START : R300_PVS_CONST_START));
516 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, code->constants.Count * 4);
517 for (i = 0; i < code->constants.Count; i++) {
518 const float * data = get_shader_constant(r300, &code->constants.Constants[i], constants);
519 OUT_CS_32F(data[0]);
520 OUT_CS_32F(data[1]);
521 OUT_CS_32F(data[2]);
522 OUT_CS_32F(data[3]);
523 }
524 }
525
526 OUT_CS_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(10) |
527 R300_PVS_NUM_CNTLRS(5) |
528 R300_PVS_NUM_FPUS(r300screen->caps->num_vert_fpus) |
529 R300_PVS_VF_MAX_VTX_NUM(12));
530 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG, 0x0);
531 END_CS;
532 }
533
534 void r300_emit_vertex_shader(struct r300_context* r300,
535 struct r300_vertex_shader* vs)
536 {
537 r300_emit_vertex_program_code(r300, &vs->code, &r300->shader_constants[PIPE_SHADER_VERTEX]);
538 }
539
540 void r300_emit_viewport_state(struct r300_context* r300,
541 struct r300_viewport_state* viewport)
542 {
543 CS_LOCALS(r300);
544
545 BEGIN_CS(9);
546 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE, 6);
547 OUT_CS_32F(viewport->xscale);
548 OUT_CS_32F(viewport->xoffset);
549 OUT_CS_32F(viewport->yscale);
550 OUT_CS_32F(viewport->yoffset);
551 OUT_CS_32F(viewport->zscale);
552 OUT_CS_32F(viewport->zoffset);
553
554 if (r300->rs_state->enable_vte) {
555 OUT_CS_REG(R300_VAP_VTE_CNTL, viewport->vte_control);
556 } else {
557 OUT_CS_REG(R300_VAP_VTE_CNTL, 0);
558 }
559 END_CS;
560 }
561
562 void r300_flush_textures(struct r300_context* r300)
563 {
564 CS_LOCALS(r300);
565
566 BEGIN_CS(4);
567 OUT_CS_REG(R300_TX_INVALTAGS, 0);
568 OUT_CS_REG(R300_TX_ENABLE, (1 << r300->texture_count) - 1);
569 END_CS;
570 }
571
572 /* Emit all dirty state. */
573 void r300_emit_dirty_state(struct r300_context* r300)
574 {
575 struct r300_screen* r300screen = r300_screen(r300->context.screen);
576 struct r300_texture* tex;
577 int i, dirty_tex = 0;
578 boolean invalid = FALSE;
579
580 if (!(r300->dirty_state)) {
581 return;
582 }
583
584 r300_update_derived_state(r300);
585
586 /* XXX check size */
587 validate:
588 /* Color buffers... */
589 for (i = 0; i < r300->framebuffer_state.nr_cbufs; i++) {
590 tex = (struct r300_texture*)r300->framebuffer_state.cbufs[i]->texture;
591 assert(tex && tex->buffer && "cbuf is marked, but NULL!");
592 if (!r300->winsys->add_buffer(r300->winsys, tex->buffer,
593 0, RADEON_GEM_DOMAIN_VRAM)) {
594 r300->context.flush(&r300->context, 0, NULL);
595 goto validate;
596 }
597 }
598 /* ...depth buffer... */
599 if (r300->framebuffer_state.zsbuf) {
600 tex = (struct r300_texture*)r300->framebuffer_state.zsbuf->texture;
601 assert(tex && tex->buffer && "zsbuf is marked, but NULL!");
602 if (!r300->winsys->add_buffer(r300->winsys, tex->buffer,
603 0, RADEON_GEM_DOMAIN_VRAM)) {
604 r300->context.flush(&r300->context, 0, NULL);
605 goto validate;
606 }
607 }
608 /* ...textures... */
609 for (i = 0; i < r300->texture_count; i++) {
610 tex = r300->textures[i];
611 assert(tex && tex->buffer && "texture is marked, but NULL!");
612 if (!r300->winsys->add_buffer(r300->winsys, tex->buffer,
613 RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM, 0)) {
614 r300->context.flush(&r300->context, 0, NULL);
615 goto validate;
616 }
617 }
618 /* ...and vertex buffer. */
619 if (r300->vbo) {
620 if (!r300->winsys->add_buffer(r300->winsys, r300->vbo,
621 RADEON_GEM_DOMAIN_GTT, 0)) {
622 r300->context.flush(&r300->context, 0, NULL);
623 goto validate;
624 }
625 } else {
626 debug_printf("No VBO while emitting dirty state!\n");
627 }
628 if (!r300->winsys->validate(r300->winsys)) {
629 r300->context.flush(&r300->context, 0, NULL);
630 if (invalid) {
631 /* Well, hell. */
632 debug_printf("r300: Stuck in validation loop, gonna quit now.");
633 exit(1);
634 }
635 invalid = TRUE;
636 goto validate;
637 }
638
639 if (r300->dirty_state & R300_NEW_BLEND) {
640 r300_emit_blend_state(r300, r300->blend_state);
641 r300->dirty_state &= ~R300_NEW_BLEND;
642 }
643
644 if (r300->dirty_state & R300_NEW_BLEND_COLOR) {
645 r300_emit_blend_color_state(r300, r300->blend_color_state);
646 r300->dirty_state &= ~R300_NEW_BLEND_COLOR;
647 }
648
649 if (r300->dirty_state & R300_NEW_CLIP) {
650 r300_emit_clip_state(r300, &r300->clip_state);
651 r300->dirty_state &= ~R300_NEW_CLIP;
652 }
653
654 if (r300->dirty_state & R300_NEW_DSA) {
655 r300_emit_dsa_state(r300, r300->dsa_state);
656 r300->dirty_state &= ~R300_NEW_DSA;
657 }
658
659 if (r300->dirty_state & R300_NEW_FRAGMENT_SHADER) {
660 if (r300screen->caps->is_r500) {
661 r500_emit_fragment_program_code(r300, &r300->fs->code, &r300->shader_constants[PIPE_SHADER_FRAGMENT]);
662 } else {
663 r300_emit_fragment_program_code(r300, &r300->fs->code, &r300->shader_constants[PIPE_SHADER_FRAGMENT]);
664 }
665 r300->dirty_state &= ~R300_NEW_FRAGMENT_SHADER;
666 }
667
668 if (r300->dirty_state & R300_NEW_FRAMEBUFFERS) {
669 r300_emit_fb_state(r300, &r300->framebuffer_state);
670 r300->dirty_state &= ~R300_NEW_FRAMEBUFFERS;
671 }
672
673 if (r300->dirty_state & R300_NEW_RASTERIZER) {
674 r300_emit_rs_state(r300, r300->rs_state);
675 r300->dirty_state &= ~R300_NEW_RASTERIZER;
676 }
677
678 if (r300->dirty_state & R300_NEW_RS_BLOCK) {
679 r300_emit_rs_block_state(r300, r300->rs_block);
680 r300->dirty_state &= ~R300_NEW_RS_BLOCK;
681 }
682
683 if (r300->dirty_state & R300_NEW_SCISSOR) {
684 r300_emit_scissor_state(r300, r300->scissor_state);
685 r300->dirty_state &= ~R300_NEW_SCISSOR;
686 }
687
688 /* Samplers and textures are tracked separately but emitted together. */
689 if (r300->dirty_state &
690 (R300_ANY_NEW_SAMPLERS | R300_ANY_NEW_TEXTURES)) {
691 for (i = 0; i < MIN2(r300->sampler_count, r300->texture_count); i++) {
692 if (r300->dirty_state &
693 ((R300_NEW_SAMPLER << i) | (R300_NEW_TEXTURE << i))) {
694 r300_emit_texture(r300,
695 r300->sampler_states[i],
696 r300->textures[i],
697 i);
698 r300->dirty_state &=
699 ~((R300_NEW_SAMPLER << i) | (R300_NEW_TEXTURE << i));
700 dirty_tex++;
701 }
702 }
703 r300->dirty_state &= ~(R300_ANY_NEW_SAMPLERS | R300_ANY_NEW_TEXTURES);
704 }
705
706 if (r300->dirty_state & R300_NEW_VIEWPORT) {
707 r300_emit_viewport_state(r300, r300->viewport_state);
708 r300->dirty_state &= ~R300_NEW_VIEWPORT;
709 }
710
711 if (dirty_tex) {
712 r300_flush_textures(r300);
713 }
714
715 if (r300->dirty_state & R300_NEW_VERTEX_FORMAT) {
716 r300_emit_vertex_format_state(r300);
717 r300->dirty_state &= ~R300_NEW_VERTEX_FORMAT;
718 }
719
720 if (r300->dirty_state & R300_NEW_VERTEX_SHADER) {
721 r300_emit_vertex_shader(r300, r300->vs);
722 r300->dirty_state &= ~R300_NEW_VERTEX_SHADER;
723 }
724
725 /* XXX
726 assert(r300->dirty_state == 0);
727 */
728
729 /* Finally, emit the VBO. */
730 r300_emit_vertex_buffer(r300);
731
732 r300->dirty_hw++;
733 }