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