Merge remote branch 'origin/nv50-compiler'
[mesa.git] / src / gallium / drivers / r600 / r600_state.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * Authors:
24 * Jerome Glisse
25 */
26 #include <stdio.h>
27 #include <errno.h>
28 #include "util/u_inlines.h"
29 #include "util/u_format.h"
30 #include "util/u_memory.h"
31 #include "util/u_pack_color.h"
32 #include "r600_screen.h"
33 #include "r600_context.h"
34 #include "r600_resource.h"
35
36 static struct r600_context_state *r600_new_context_state(unsigned type)
37 {
38 struct r600_context_state *rstate = CALLOC_STRUCT(r600_context_state);
39 if (rstate == NULL)
40 return NULL;
41 rstate->type = type;
42 rstate->refcount = 1;
43 return rstate;
44 }
45
46 static void *r600_create_blend_state(struct pipe_context *ctx,
47 const struct pipe_blend_state *state)
48 {
49 struct r600_context *rctx = r600_context(ctx);
50 struct r600_context_state *rstate;
51
52 rstate = r600_new_context_state(pipe_blend_type);
53 rstate->state.blend = *state;
54 rctx->vtbl->blend(rctx, &rstate->rstate[0], &rstate->state.blend);
55
56 return rstate;
57 }
58
59 static void *r600_create_dsa_state(struct pipe_context *ctx,
60 const struct pipe_depth_stencil_alpha_state *state)
61 {
62 struct r600_context_state *rstate;
63
64 rstate = r600_new_context_state(pipe_dsa_type);
65 rstate->state.dsa = *state;
66 return rstate;
67 }
68
69 static void *r600_create_rs_state(struct pipe_context *ctx,
70 const struct pipe_rasterizer_state *state)
71 {
72 struct r600_context_state *rstate;
73
74 rstate = r600_new_context_state(pipe_rasterizer_type);
75 rstate->state.rasterizer = *state;
76 return rstate;
77 }
78
79 static void *r600_create_sampler_state(struct pipe_context *ctx,
80 const struct pipe_sampler_state *state)
81 {
82 struct r600_context *rctx = r600_context(ctx);
83 struct r600_context_state *rstate;
84
85 rstate = r600_new_context_state(pipe_sampler_type);
86 rstate->state.sampler = *state;
87 rctx->vtbl->sampler(rctx, &rstate->rstate[0], &rstate->state.sampler, 0);
88 rctx->vtbl->sampler_border(rctx, &rstate->rstate[1], &rstate->state.sampler, 0);
89 return rstate;
90 }
91
92 static void r600_remove_sampler_view(struct r600_shader_sampler_states *sampler,
93 struct r600_context_state *rstate)
94 {
95 int i, j;
96
97 for (i = 0; i < sampler->nview; i++) {
98 for (j = 0; j < rstate->nrstate; j++) {
99 if (sampler->view[i] == &rstate->rstate[j])
100 sampler->view[i] = NULL;
101 }
102 }
103 }
104 static void r600_sampler_view_destroy(struct pipe_context *ctx,
105 struct pipe_sampler_view *state)
106 {
107 struct r600_context_state *rstate = (struct r600_context_state *)state;
108 struct r600_context *rctx = r600_context(ctx);
109
110 /* need to search list of vs/ps sampler views and remove it from any - uggh */
111 r600_remove_sampler_view(&rctx->ps_sampler, rstate);
112 r600_remove_sampler_view(&rctx->vs_sampler, rstate);
113 r600_context_state_decref(rstate);
114 }
115
116 static struct pipe_sampler_view *r600_create_sampler_view(struct pipe_context *ctx,
117 struct pipe_resource *texture,
118 const struct pipe_sampler_view *state)
119 {
120 struct r600_context_state *rstate;
121 struct r600_context *rctx = r600_context(ctx);
122
123 rstate = r600_new_context_state(pipe_sampler_view_type);
124 rstate->state.sampler_view = *state;
125 rstate->state.sampler_view.texture = NULL;
126 pipe_reference(NULL, &texture->reference);
127 rstate->state.sampler_view.texture = texture;
128 rstate->state.sampler_view.reference.count = 1;
129 rstate->state.sampler_view.context = ctx;
130 rctx->vtbl->resource(ctx, &rstate->rstate[0], &rstate->state.sampler_view, 0);
131 return &rstate->state.sampler_view;
132 }
133
134 static void r600_set_sampler_view(struct pipe_context *ctx,
135 unsigned count,
136 struct pipe_sampler_view **views,
137 struct r600_shader_sampler_states *sampler,
138 unsigned shader_id)
139 {
140 struct r600_context *rctx = r600_context(ctx);
141 struct r600_context_state *rstate;
142 unsigned i;
143
144 for (i = 0; i < sampler->nview; i++) {
145 radeon_draw_unbind(&rctx->draw, sampler->view[i]);
146 }
147
148 for (i = 0; i < count; i++) {
149 rstate = (struct r600_context_state *)views[i];
150 if (rstate) {
151 rstate->nrstate = 0;
152 }
153 }
154 for (i = 0; i < count; i++) {
155 rstate = (struct r600_context_state *)views[i];
156 if (rstate) {
157 if (rstate->nrstate >= R600_MAX_RSTATE)
158 continue;
159 if (rstate->nrstate) {
160 memcpy(&rstate->rstate[rstate->nrstate], &rstate->rstate[0], sizeof(struct radeon_state));
161 }
162 radeon_state_convert(&rstate->rstate[rstate->nrstate], R600_STATE_RESOURCE, i, shader_id);
163 sampler->view[i] = &rstate->rstate[rstate->nrstate];
164 rstate->nrstate++;
165 }
166 }
167 sampler->nview = count;
168 }
169
170 static void r600_set_ps_sampler_view(struct pipe_context *ctx,
171 unsigned count,
172 struct pipe_sampler_view **views)
173 {
174 struct r600_context *rctx = r600_context(ctx);
175 r600_set_sampler_view(ctx, count, views, &rctx->ps_sampler, R600_SHADER_PS);
176 }
177
178 static void r600_set_vs_sampler_view(struct pipe_context *ctx,
179 unsigned count,
180 struct pipe_sampler_view **views)
181 {
182 struct r600_context *rctx = r600_context(ctx);
183 r600_set_sampler_view(ctx, count, views, &rctx->vs_sampler, R600_SHADER_VS);
184 }
185
186 static void *r600_create_shader_state(struct pipe_context *ctx,
187 const struct pipe_shader_state *state)
188 {
189 struct r600_context *rctx = r600_context(ctx);
190 struct r600_context_state *rstate;
191 int r;
192
193 rstate = r600_new_context_state(pipe_shader_type);
194 rstate->state.shader = *state;
195 r = r600_pipe_shader_create(&rctx->context, rstate, rstate->state.shader.tokens);
196 if (r) {
197 r600_context_state_decref(rstate);
198 return NULL;
199 }
200 return rstate;
201 }
202
203 static void *r600_create_vertex_elements(struct pipe_context *ctx,
204 unsigned count,
205 const struct pipe_vertex_element *elements)
206 {
207 struct r600_vertex_element *v = CALLOC_STRUCT(r600_vertex_element);
208
209 assert(count < 32);
210 v->count = count;
211 memcpy(v->elements, elements, count * sizeof(struct pipe_vertex_element));
212 v->refcount = 1;
213 return v;
214 }
215
216 static void r600_delete_vertex_element(struct pipe_context *ctx, void *state)
217 {
218 struct r600_vertex_element *v = (struct r600_vertex_element*)state;
219
220 if (v == NULL)
221 return;
222 if (--v->refcount)
223 return;
224 free(v);
225 }
226
227 static void r600_bind_vertex_elements(struct pipe_context *ctx, void *state)
228 {
229 struct r600_context *rctx = r600_context(ctx);
230 struct r600_vertex_element *v = (struct r600_vertex_element*)state;
231
232 r600_delete_vertex_element(ctx, rctx->vertex_elements);
233 rctx->vertex_elements = v;
234 if (v) {
235 v->refcount++;
236 }
237 }
238
239 static void r600_bind_rasterizer_state(struct pipe_context *ctx, void *state)
240 {
241 struct r600_context *rctx = r600_context(ctx);
242 struct r600_context_state *rstate = (struct r600_context_state *)state;
243
244 if (state == NULL)
245 return;
246 rctx->rasterizer = r600_context_state_decref(rctx->rasterizer);
247 rctx->rasterizer = r600_context_state_incref(rstate);
248 }
249
250 static void r600_bind_blend_state(struct pipe_context *ctx, void *state)
251 {
252 struct r600_context *rctx = r600_context(ctx);
253 struct r600_context_state *rstate = (struct r600_context_state *)state;
254
255 if (state == NULL)
256 return;
257 rctx->blend = r600_context_state_decref(rctx->blend);
258 rctx->blend = r600_context_state_incref(rstate);
259
260 }
261
262 static void r600_bind_dsa_state(struct pipe_context *ctx, void *state)
263 {
264 struct r600_context *rctx = r600_context(ctx);
265 struct r600_context_state *rstate = (struct r600_context_state *)state;
266
267 if (state == NULL)
268 return;
269 rctx->dsa = r600_context_state_decref(rctx->dsa);
270 rctx->dsa = r600_context_state_incref(rstate);
271 }
272
273 static void r600_bind_ps_shader(struct pipe_context *ctx, void *state)
274 {
275 struct r600_context *rctx = r600_context(ctx);
276 struct r600_context_state *rstate = (struct r600_context_state *)state;
277
278 rctx->ps_shader = r600_context_state_decref(rctx->ps_shader);
279 rctx->ps_shader = r600_context_state_incref(rstate);
280 }
281
282 static void r600_bind_vs_shader(struct pipe_context *ctx, void *state)
283 {
284 struct r600_context *rctx = r600_context(ctx);
285 struct r600_context_state *rstate = (struct r600_context_state *)state;
286
287 rctx->vs_shader = r600_context_state_decref(rctx->vs_shader);
288 rctx->vs_shader = r600_context_state_incref(rstate);
289 }
290
291 static void r600_bind_sampler_shader(struct pipe_context *ctx,
292 unsigned count, void **states,
293 struct r600_shader_sampler_states *sampler, unsigned shader_id)
294 {
295 struct r600_context *rctx = r600_context(ctx);
296 struct r600_context_state *rstate;
297 unsigned i;
298
299 for (i = 0; i < sampler->nsampler; i++) {
300 radeon_draw_unbind(&rctx->draw, sampler->sampler[i]);
301 }
302 for (i = 0; i < sampler->nborder; i++) {
303 radeon_draw_unbind(&rctx->draw, sampler->border[i]);
304 }
305 for (i = 0; i < count; i++) {
306 rstate = (struct r600_context_state *)states[i];
307 if (rstate) {
308 rstate->nrstate = 0;
309 }
310 }
311 for (i = 0; i < count; i++) {
312 rstate = (struct r600_context_state *)states[i];
313 if (rstate) {
314 if (rstate->nrstate >= R600_MAX_RSTATE)
315 continue;
316 if (rstate->nrstate) {
317 memcpy(&rstate->rstate[rstate->nrstate], &rstate->rstate[0], sizeof(struct radeon_state));
318 memcpy(&rstate->rstate[rstate->nrstate+1], &rstate->rstate[1], sizeof(struct radeon_state));
319 }
320 radeon_state_convert(&rstate->rstate[rstate->nrstate], R600_STATE_SAMPLER, i, shader_id);
321 radeon_state_convert(&rstate->rstate[rstate->nrstate + 1], R600_STATE_SAMPLER_BORDER, i, shader_id);
322 sampler->sampler[i] = &rstate->rstate[rstate->nrstate];
323 sampler->border[i] = &rstate->rstate[rstate->nrstate + 1];
324 rstate->nrstate += 2;
325 }
326 }
327 sampler->nsampler = count;
328 sampler->nborder = count;
329 }
330
331 static void r600_bind_ps_sampler(struct pipe_context *ctx,
332 unsigned count, void **states)
333 {
334 struct r600_context *rctx = r600_context(ctx);
335 r600_bind_sampler_shader(ctx, count, states, &rctx->ps_sampler, R600_SHADER_PS);
336 }
337
338 static void r600_bind_vs_sampler(struct pipe_context *ctx,
339 unsigned count, void **states)
340 {
341 struct r600_context *rctx = r600_context(ctx);
342 r600_bind_sampler_shader(ctx, count, states, &rctx->vs_sampler, R600_SHADER_VS);
343 }
344
345 static void r600_delete_state(struct pipe_context *ctx, void *state)
346 {
347 struct r600_context_state *rstate = (struct r600_context_state *)state;
348
349 r600_context_state_decref(rstate);
350 }
351
352 static void r600_set_blend_color(struct pipe_context *ctx,
353 const struct pipe_blend_color *color)
354 {
355 struct r600_context *rctx = r600_context(ctx);
356
357 rctx->blend_color = *color;
358 }
359
360 static void r600_set_clip_state(struct pipe_context *ctx,
361 const struct pipe_clip_state *state)
362 {
363 struct r600_context *rctx = r600_context(ctx);
364 struct r600_context_state *rstate;
365
366 r600_context_state_decref(rctx->clip);
367
368 rstate = r600_new_context_state(pipe_clip_type);
369 rstate->state.clip = *state;
370 rctx->vtbl->ucp(rctx, &rstate->rstate[0], &rstate->state.clip);
371 rctx->clip = rstate;
372 }
373
374 static void r600_set_framebuffer_state(struct pipe_context *ctx,
375 const struct pipe_framebuffer_state *state)
376 {
377 struct r600_context *rctx = r600_context(ctx);
378 struct r600_context_state *rstate;
379 int i;
380
381 r600_context_state_decref(rctx->framebuffer);
382
383 rstate = r600_new_context_state(pipe_framebuffer_type);
384 rstate->state.framebuffer = *state;
385 for (i = 0; i < rstate->state.framebuffer.nr_cbufs; i++) {
386 pipe_reference(NULL, &state->cbufs[i]->reference);
387 }
388 pipe_reference(NULL, &state->zsbuf->reference);
389 rctx->framebuffer = rstate;
390 for (i = 0; i < state->nr_cbufs; i++) {
391 rctx->vtbl->cb(rctx, &rstate->rstate[i+1], state, i);
392 }
393 if (state->zsbuf) {
394 rctx->vtbl->db(rctx, &rstate->rstate[0], state);
395 }
396 return;
397 }
398
399 static void r600_set_polygon_stipple(struct pipe_context *ctx,
400 const struct pipe_poly_stipple *state)
401 {
402 }
403
404 static void r600_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
405 {
406 }
407
408 static void r600_set_scissor_state(struct pipe_context *ctx,
409 const struct pipe_scissor_state *state)
410 {
411 struct r600_context *rctx = r600_context(ctx);
412 struct r600_context_state *rstate;
413
414 r600_context_state_decref(rctx->scissor);
415
416 rstate = r600_new_context_state(pipe_scissor_type);
417 rstate->state.scissor = *state;
418 rctx->scissor = rstate;
419 }
420
421 static void r600_set_stencil_ref(struct pipe_context *ctx,
422 const struct pipe_stencil_ref *state)
423 {
424 struct r600_context *rctx = r600_context(ctx);
425 struct r600_context_state *rstate;
426
427 r600_context_state_decref(rctx->stencil_ref);
428
429 rstate = r600_new_context_state(pipe_stencil_ref_type);
430 rstate->state.stencil_ref = *state;
431 rctx->stencil_ref = rstate;
432 }
433
434 static void r600_set_vertex_buffers(struct pipe_context *ctx,
435 unsigned count,
436 const struct pipe_vertex_buffer *buffers)
437 {
438 struct r600_context *rctx = r600_context(ctx);
439 unsigned i;
440
441 for (i = 0; i < rctx->nvertex_buffer; i++) {
442 pipe_resource_reference(&rctx->vertex_buffer[i].buffer, NULL);
443 }
444 memcpy(rctx->vertex_buffer, buffers, sizeof(struct pipe_vertex_buffer) * count);
445 for (i = 0; i < count; i++) {
446 rctx->vertex_buffer[i].buffer = NULL;
447 pipe_resource_reference(&rctx->vertex_buffer[i].buffer, buffers[i].buffer);
448 }
449 rctx->nvertex_buffer = count;
450 }
451
452 static void r600_set_index_buffer(struct pipe_context *ctx,
453 const struct pipe_index_buffer *ib)
454 {
455 struct r600_context *rctx = r600_context(ctx);
456
457 if (ib) {
458 pipe_resource_reference(&rctx->index_buffer.buffer, ib->buffer);
459 memcpy(&rctx->index_buffer, ib, sizeof(rctx->index_buffer));
460 } else {
461 pipe_resource_reference(&rctx->index_buffer.buffer, NULL);
462 memset(&rctx->index_buffer, 0, sizeof(rctx->index_buffer));
463 }
464
465 /* TODO make this more like a state */
466 }
467
468 static void r600_set_viewport_state(struct pipe_context *ctx,
469 const struct pipe_viewport_state *state)
470 {
471 struct r600_context *rctx = r600_context(ctx);
472 struct r600_context_state *rstate;
473
474 r600_context_state_decref(rctx->viewport);
475
476 rstate = r600_new_context_state(pipe_viewport_type);
477 rstate->state.viewport = *state;
478 rctx->vtbl->viewport(rctx, &rstate->rstate[0], &rstate->state.viewport);
479 rctx->viewport = rstate;
480 }
481
482 void r600_init_state_functions(struct r600_context *rctx)
483 {
484 rctx->context.create_blend_state = r600_create_blend_state;
485 rctx->context.create_depth_stencil_alpha_state = r600_create_dsa_state;
486 rctx->context.create_fs_state = r600_create_shader_state;
487 rctx->context.create_rasterizer_state = r600_create_rs_state;
488 rctx->context.create_sampler_state = r600_create_sampler_state;
489 rctx->context.create_sampler_view = r600_create_sampler_view;
490 rctx->context.create_vertex_elements_state = r600_create_vertex_elements;
491 rctx->context.create_vs_state = r600_create_shader_state;
492 rctx->context.bind_blend_state = r600_bind_blend_state;
493 rctx->context.bind_depth_stencil_alpha_state = r600_bind_dsa_state;
494 rctx->context.bind_fragment_sampler_states = r600_bind_ps_sampler;
495 rctx->context.bind_fs_state = r600_bind_ps_shader;
496 rctx->context.bind_rasterizer_state = r600_bind_rasterizer_state;
497 rctx->context.bind_vertex_elements_state = r600_bind_vertex_elements;
498 rctx->context.bind_vertex_sampler_states = r600_bind_vs_sampler;
499 rctx->context.bind_vs_state = r600_bind_vs_shader;
500 rctx->context.delete_blend_state = r600_delete_state;
501 rctx->context.delete_depth_stencil_alpha_state = r600_delete_state;
502 rctx->context.delete_fs_state = r600_delete_state;
503 rctx->context.delete_rasterizer_state = r600_delete_state;
504 rctx->context.delete_sampler_state = r600_delete_state;
505 rctx->context.delete_vertex_elements_state = r600_delete_vertex_element;
506 rctx->context.delete_vs_state = r600_delete_state;
507 rctx->context.set_blend_color = r600_set_blend_color;
508 rctx->context.set_clip_state = r600_set_clip_state;
509
510 if (rctx->screen->chip_class == EVERGREEN)
511 rctx->context.set_constant_buffer = eg_set_constant_buffer;
512 else if (rctx->screen->use_mem_constant)
513 rctx->context.set_constant_buffer = r600_set_constant_buffer_mem;
514 else
515 rctx->context.set_constant_buffer = r600_set_constant_buffer_file;
516
517 rctx->context.set_fragment_sampler_views = r600_set_ps_sampler_view;
518 rctx->context.set_framebuffer_state = r600_set_framebuffer_state;
519 rctx->context.set_polygon_stipple = r600_set_polygon_stipple;
520 rctx->context.set_sample_mask = r600_set_sample_mask;
521 rctx->context.set_scissor_state = r600_set_scissor_state;
522 rctx->context.set_stencil_ref = r600_set_stencil_ref;
523 rctx->context.set_vertex_buffers = r600_set_vertex_buffers;
524 rctx->context.set_index_buffer = r600_set_index_buffer;
525 rctx->context.set_vertex_sampler_views = r600_set_vs_sampler_view;
526 rctx->context.set_viewport_state = r600_set_viewport_state;
527 rctx->context.sampler_view_destroy = r600_sampler_view_destroy;
528 }
529
530 struct r600_context_state *r600_context_state_incref(struct r600_context_state *rstate)
531 {
532 if (rstate == NULL)
533 return NULL;
534 rstate->refcount++;
535 return rstate;
536 }
537
538 struct r600_context_state *r600_context_state_decref(struct r600_context_state *rstate)
539 {
540 unsigned i;
541
542 if (rstate == NULL)
543 return NULL;
544 if (--rstate->refcount)
545 return NULL;
546 switch (rstate->type) {
547 case pipe_sampler_view_type:
548 pipe_resource_reference(&rstate->state.sampler_view.texture, NULL);
549 break;
550 case pipe_framebuffer_type:
551 for (i = 0; i < rstate->state.framebuffer.nr_cbufs; i++) {
552 pipe_surface_reference(&rstate->state.framebuffer.cbufs[i], NULL);
553 }
554 pipe_surface_reference(&rstate->state.framebuffer.zsbuf, NULL);
555 break;
556 case pipe_viewport_type:
557 case pipe_depth_type:
558 case pipe_rasterizer_type:
559 case pipe_poly_stipple_type:
560 case pipe_scissor_type:
561 case pipe_clip_type:
562 case pipe_stencil_type:
563 case pipe_alpha_type:
564 case pipe_dsa_type:
565 case pipe_blend_type:
566 case pipe_stencil_ref_type:
567 case pipe_shader_type:
568 case pipe_sampler_type:
569 break;
570 default:
571 R600_ERR("invalid type %d\n", rstate->type);
572 return NULL;
573 }
574 radeon_state_fini(&rstate->rstate[0]);
575 FREE(rstate);
576 return NULL;
577 }
578
579 static void r600_bind_shader_sampler(struct r600_context *rctx, struct r600_shader_sampler_states *sampler)
580 {
581 int i;
582
583 for (i = 0; i < sampler->nsampler; i++) {
584 if (sampler->sampler[i])
585 radeon_draw_bind(&rctx->draw, sampler->sampler[i]);
586 }
587
588 for (i = 0; i < sampler->nborder; i++) {
589 if (sampler->border[i])
590 radeon_draw_bind(&rctx->draw, sampler->border[i]);
591 }
592
593 for (i = 0; i < sampler->nview; i++) {
594 if (sampler->view[i])
595 radeon_draw_bind(&rctx->draw, sampler->view[i]);
596 }
597 }
598
599
600 static int setup_cb_flush(struct r600_context *rctx, struct radeon_state *flush)
601 {
602 struct r600_screen *rscreen = rctx->screen;
603 struct r600_resource_texture *rtex;
604 struct r600_resource *rbuffer;
605 struct pipe_surface *surf;
606 int i;
607
608 radeon_state_init(flush, rscreen->rw, R600_STATE_CB_FLUSH, 0, 0);
609
610 for (i = 0; i < rctx->framebuffer->state.framebuffer.nr_cbufs; i++) {
611 surf = rctx->framebuffer->state.framebuffer.cbufs[i];
612
613 rtex = (struct r600_resource_texture*)surf->texture;
614 rbuffer = &rtex->resource;
615 /* just need to the bo to the flush list */
616 flush->bo[i] = radeon_bo_incref(rscreen->rw, rbuffer->bo);
617 flush->placement[i] = RADEON_GEM_DOMAIN_VRAM;
618 }
619 flush->nbo = rctx->framebuffer->state.framebuffer.nr_cbufs;
620 return radeon_state_pm4(flush);
621 }
622
623 static int setup_db_flush(struct r600_context *rctx, struct radeon_state *flush)
624 {
625 struct r600_screen *rscreen = rctx->screen;
626 struct r600_resource_texture *rtex;
627 struct r600_resource *rbuffer;
628 struct pipe_surface *surf;
629
630 surf = rctx->framebuffer->state.framebuffer.zsbuf;
631
632 if (!surf)
633 return 0;
634
635 radeon_state_init(flush, rscreen->rw, R600_STATE_DB_FLUSH, 0, 0);
636 rtex = (struct r600_resource_texture*)surf->texture;
637 rbuffer = &rtex->resource;
638 /* just need to the bo to the flush list */
639 flush->bo[0] = radeon_bo_incref(rscreen->rw, rbuffer->bo);
640 flush->placement[0] = RADEON_GEM_DOMAIN_VRAM;
641
642 flush->nbo = 1;
643 return radeon_state_pm4(flush);
644 }
645
646 int r600_context_hw_states(struct pipe_context *ctx)
647 {
648 struct r600_context *rctx = r600_context(ctx);
649 unsigned i;
650
651 /* build new states */
652 rctx->vtbl->rasterizer(rctx, &rctx->hw_states.rasterizer);
653 rctx->vtbl->scissor(rctx, &rctx->hw_states.scissor);
654 rctx->vtbl->dsa(rctx, &rctx->hw_states.dsa);
655 rctx->vtbl->cb_cntl(rctx, &rctx->hw_states.cb_cntl);
656
657 /* setup flushes */
658 setup_db_flush(rctx, &rctx->hw_states.db_flush);
659 setup_cb_flush(rctx, &rctx->hw_states.cb_flush);
660
661 /* bind states */
662 radeon_draw_bind(&rctx->draw, &rctx->config);
663
664 radeon_draw_bind(&rctx->draw, &rctx->hw_states.rasterizer);
665 radeon_draw_bind(&rctx->draw, &rctx->hw_states.scissor);
666 radeon_draw_bind(&rctx->draw, &rctx->hw_states.dsa);
667 radeon_draw_bind(&rctx->draw, &rctx->hw_states.cb_cntl);
668
669 radeon_draw_bind(&rctx->draw, &rctx->hw_states.db_flush);
670 radeon_draw_bind(&rctx->draw, &rctx->hw_states.cb_flush);
671
672 radeon_draw_bind(&rctx->draw, &rctx->hw_states.db_flush);
673 radeon_draw_bind(&rctx->draw, &rctx->hw_states.cb_flush);
674
675 if (rctx->viewport) {
676 radeon_draw_bind(&rctx->draw, &rctx->viewport->rstate[0]);
677 }
678 if (rctx->blend) {
679 radeon_draw_bind(&rctx->draw, &rctx->blend->rstate[0]);
680 }
681 if (rctx->clip) {
682 radeon_draw_bind(&rctx->draw, &rctx->clip->rstate[0]);
683 }
684 for (i = 0; i < rctx->framebuffer->state.framebuffer.nr_cbufs; i++) {
685 radeon_draw_bind(&rctx->draw, &rctx->framebuffer->rstate[i+1]);
686 }
687 if (rctx->framebuffer->state.framebuffer.zsbuf) {
688 radeon_draw_bind(&rctx->draw, &rctx->framebuffer->rstate[0]);
689 }
690
691 r600_bind_shader_sampler(rctx, &rctx->vs_sampler);
692 r600_bind_shader_sampler(rctx, &rctx->ps_sampler);
693
694 return 0;
695 }