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