r600g: clean up valgrind issues on maxtargets test.
[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 if (rctx->framebuffer) {
386 for (i = 0; i < rctx->framebuffer->state.framebuffer.nr_cbufs; i++)
387 radeon_draw_unbind(&rctx->draw, &rctx->framebuffer->rstate[i+1]);
388 radeon_draw_unbind(&rctx->draw, &rctx->framebuffer->rstate[0]);
389 }
390 clean_flush(rctx, &rctx->hw_states.cb_flush);
391 clean_flush(rctx, &rctx->hw_states.db_flush);
392
393 r600_context_state_decref(rctx->framebuffer);
394
395 rstate = r600_new_context_state(pipe_framebuffer_type);
396 rstate->state.framebuffer = *state;
397 for (i = 0; i < rstate->state.framebuffer.nr_cbufs; i++) {
398 pipe_reference(NULL, &state->cbufs[i]->reference);
399 }
400 pipe_reference(NULL, &state->zsbuf->reference);
401 rctx->framebuffer = rstate;
402 for (i = 0; i < state->nr_cbufs; i++) {
403 rctx->vtbl->cb(rctx, &rstate->rstate[i+1], state, i);
404 }
405 if (state->zsbuf) {
406 rctx->vtbl->db(rctx, &rstate->rstate[0], state);
407 }
408 /* setup flush states */
409 setup_cb_flush(rctx, &rctx->hw_states.cb_flush);
410 setup_db_flush(rctx, &rctx->hw_states.db_flush);
411
412 return;
413 }
414
415 static void r600_set_polygon_stipple(struct pipe_context *ctx,
416 const struct pipe_poly_stipple *state)
417 {
418 }
419
420 static void r600_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
421 {
422 }
423
424 static void r600_set_scissor_state(struct pipe_context *ctx,
425 const struct pipe_scissor_state *state)
426 {
427 struct r600_context *rctx = r600_context(ctx);
428 struct r600_context_state *rstate;
429
430 r600_context_state_decref(rctx->scissor);
431
432 rstate = r600_new_context_state(pipe_scissor_type);
433 rstate->state.scissor = *state;
434 rctx->scissor = rstate;
435 }
436
437 static void r600_set_stencil_ref(struct pipe_context *ctx,
438 const struct pipe_stencil_ref *state)
439 {
440 struct r600_context *rctx = r600_context(ctx);
441 struct r600_context_state *rstate;
442
443 r600_context_state_decref(rctx->stencil_ref);
444
445 rstate = r600_new_context_state(pipe_stencil_ref_type);
446 rstate->state.stencil_ref = *state;
447 rctx->stencil_ref = rstate;
448 }
449
450 static void r600_set_vertex_buffers(struct pipe_context *ctx,
451 unsigned count,
452 const struct pipe_vertex_buffer *buffers)
453 {
454 struct r600_context *rctx = r600_context(ctx);
455 unsigned i;
456 boolean any_user_buffers = FALSE;
457
458 for (i = 0; i < rctx->nvertex_buffer; i++) {
459 pipe_resource_reference(&rctx->vertex_buffer[i].buffer, NULL);
460 }
461 memcpy(rctx->vertex_buffer, buffers, sizeof(struct pipe_vertex_buffer) * count);
462 for (i = 0; i < count; i++) {
463 rctx->vertex_buffer[i].buffer = NULL;
464 if (r600_buffer_is_user_buffer(buffers[i].buffer))
465 any_user_buffers = TRUE;
466 pipe_resource_reference(&rctx->vertex_buffer[i].buffer, buffers[i].buffer);
467 }
468 rctx->any_user_vbs = any_user_buffers;
469 rctx->nvertex_buffer = count;
470 }
471
472 static void r600_set_index_buffer(struct pipe_context *ctx,
473 const struct pipe_index_buffer *ib)
474 {
475 struct r600_context *rctx = r600_context(ctx);
476
477 if (ib) {
478 pipe_resource_reference(&rctx->index_buffer.buffer, ib->buffer);
479 memcpy(&rctx->index_buffer, ib, sizeof(rctx->index_buffer));
480 } else {
481 pipe_resource_reference(&rctx->index_buffer.buffer, NULL);
482 memset(&rctx->index_buffer, 0, sizeof(rctx->index_buffer));
483 }
484
485 /* TODO make this more like a state */
486 }
487
488 static void r600_set_viewport_state(struct pipe_context *ctx,
489 const struct pipe_viewport_state *state)
490 {
491 struct r600_context *rctx = r600_context(ctx);
492 struct r600_context_state *rstate;
493
494 r600_context_state_decref(rctx->viewport);
495
496 rstate = r600_new_context_state(pipe_viewport_type);
497 rstate->state.viewport = *state;
498 rctx->vtbl->viewport(rctx, &rstate->rstate[0], &rstate->state.viewport);
499 rctx->viewport = rstate;
500 }
501
502 void r600_init_state_functions(struct r600_context *rctx)
503 {
504 rctx->context.create_blend_state = r600_create_blend_state;
505 rctx->context.create_depth_stencil_alpha_state = r600_create_dsa_state;
506 rctx->context.create_fs_state = r600_create_shader_state;
507 rctx->context.create_rasterizer_state = r600_create_rs_state;
508 rctx->context.create_sampler_state = r600_create_sampler_state;
509 rctx->context.create_sampler_view = r600_create_sampler_view;
510 rctx->context.create_vertex_elements_state = r600_create_vertex_elements;
511 rctx->context.create_vs_state = r600_create_shader_state;
512 rctx->context.bind_blend_state = r600_bind_blend_state;
513 rctx->context.bind_depth_stencil_alpha_state = r600_bind_dsa_state;
514 rctx->context.bind_fragment_sampler_states = r600_bind_ps_sampler;
515 rctx->context.bind_fs_state = r600_bind_ps_shader;
516 rctx->context.bind_rasterizer_state = r600_bind_rasterizer_state;
517 rctx->context.bind_vertex_elements_state = r600_bind_vertex_elements;
518 rctx->context.bind_vertex_sampler_states = r600_bind_vs_sampler;
519 rctx->context.bind_vs_state = r600_bind_vs_shader;
520 rctx->context.delete_blend_state = r600_delete_state;
521 rctx->context.delete_depth_stencil_alpha_state = r600_delete_state;
522 rctx->context.delete_fs_state = r600_delete_state;
523 rctx->context.delete_rasterizer_state = r600_delete_state;
524 rctx->context.delete_sampler_state = r600_delete_state;
525 rctx->context.delete_vertex_elements_state = r600_delete_vertex_element;
526 rctx->context.delete_vs_state = r600_delete_state;
527 rctx->context.set_blend_color = r600_set_blend_color;
528 rctx->context.set_clip_state = r600_set_clip_state;
529
530 if (rctx->screen->chip_class == EVERGREEN)
531 rctx->context.set_constant_buffer = eg_set_constant_buffer;
532 else if (rctx->screen->use_mem_constant)
533 rctx->context.set_constant_buffer = r600_set_constant_buffer_mem;
534 else
535 rctx->context.set_constant_buffer = r600_set_constant_buffer_file;
536
537 rctx->context.set_fragment_sampler_views = r600_set_ps_sampler_view;
538 rctx->context.set_framebuffer_state = r600_set_framebuffer_state;
539 rctx->context.set_polygon_stipple = r600_set_polygon_stipple;
540 rctx->context.set_sample_mask = r600_set_sample_mask;
541 rctx->context.set_scissor_state = r600_set_scissor_state;
542 rctx->context.set_stencil_ref = r600_set_stencil_ref;
543 rctx->context.set_vertex_buffers = r600_set_vertex_buffers;
544 rctx->context.set_index_buffer = r600_set_index_buffer;
545 rctx->context.set_vertex_sampler_views = r600_set_vs_sampler_view;
546 rctx->context.set_viewport_state = r600_set_viewport_state;
547 rctx->context.sampler_view_destroy = r600_sampler_view_destroy;
548 }
549
550 struct r600_context_state *r600_context_state_incref(struct r600_context_state *rstate)
551 {
552 if (rstate == NULL)
553 return NULL;
554 rstate->refcount++;
555 return rstate;
556 }
557
558 struct r600_context_state *r600_context_state_decref(struct r600_context_state *rstate)
559 {
560 unsigned i;
561
562 if (rstate == NULL)
563 return NULL;
564 if (--rstate->refcount)
565 return NULL;
566 switch (rstate->type) {
567 case pipe_sampler_view_type:
568 pipe_resource_reference(&rstate->state.sampler_view.texture, NULL);
569 break;
570 case pipe_framebuffer_type:
571 for (i = 0; i < rstate->state.framebuffer.nr_cbufs; i++) {
572 pipe_surface_reference(&rstate->state.framebuffer.cbufs[i], NULL);
573 radeon_state_fini(&rstate->rstate[i+1]);
574 }
575 pipe_surface_reference(&rstate->state.framebuffer.zsbuf, NULL);
576 break;
577 case pipe_viewport_type:
578 case pipe_depth_type:
579 case pipe_rasterizer_type:
580 case pipe_poly_stipple_type:
581 case pipe_scissor_type:
582 case pipe_clip_type:
583 case pipe_stencil_type:
584 case pipe_alpha_type:
585 case pipe_dsa_type:
586 case pipe_blend_type:
587 case pipe_stencil_ref_type:
588 case pipe_shader_type:
589 case pipe_sampler_type:
590 break;
591 default:
592 R600_ERR("invalid type %d\n", rstate->type);
593 return NULL;
594 }
595 radeon_state_fini(&rstate->rstate[0]);
596 FREE(rstate);
597 return NULL;
598 }
599
600 static void r600_bind_shader_sampler(struct r600_context *rctx, struct r600_shader_sampler_states *sampler)
601 {
602 int i;
603
604 for (i = 0; i < sampler->nsampler; i++) {
605 if (sampler->sampler[i])
606 radeon_draw_bind(&rctx->draw, sampler->sampler[i]);
607 }
608
609 for (i = 0; i < sampler->nborder; i++) {
610 if (sampler->border[i])
611 radeon_draw_bind(&rctx->draw, sampler->border[i]);
612 }
613
614 for (i = 0; i < sampler->nview; i++) {
615 if (sampler->view[i])
616 radeon_draw_bind(&rctx->draw, sampler->view[i]);
617 }
618 }
619
620 static void clean_flush(struct r600_context *rctx, struct radeon_state *flush)
621 {
622 struct r600_screen *rscreen = rctx->screen;
623 int i;
624
625 for (i = 0 ; i < flush->nbo; i++) {
626 radeon_ws_bo_reference(rscreen->rw, &flush->bo[i], NULL);
627 }
628 flush->nbo = 0;
629 radeon_state_fini(flush);
630 }
631
632 static int setup_cb_flush(struct r600_context *rctx, struct radeon_state *flush)
633 {
634 struct r600_screen *rscreen = rctx->screen;
635 struct r600_resource_texture *rtex;
636 struct r600_resource *rbuffer;
637 struct pipe_surface *surf;
638 int i;
639
640 radeon_state_init(flush, rscreen->rw, R600_STATE_CB_FLUSH, 0, 0);
641
642 for (i = 0; i < rctx->framebuffer->state.framebuffer.nr_cbufs; i++) {
643 surf = rctx->framebuffer->state.framebuffer.cbufs[i];
644
645 rtex = (struct r600_resource_texture*)surf->texture;
646 rbuffer = &rtex->resource;
647 /* just need to the bo to the flush list */
648 radeon_ws_bo_reference(rscreen->rw, &flush->bo[i], rbuffer->bo);
649 flush->placement[i] = RADEON_GEM_DOMAIN_VRAM;
650 }
651 flush->nbo = rctx->framebuffer->state.framebuffer.nr_cbufs;
652 return radeon_state_pm4(flush);
653 }
654
655 static int setup_db_flush(struct r600_context *rctx, struct radeon_state *flush)
656 {
657 struct r600_screen *rscreen = rctx->screen;
658 struct r600_resource_texture *rtex;
659 struct r600_resource *rbuffer;
660 struct pipe_surface *surf;
661
662 surf = rctx->framebuffer->state.framebuffer.zsbuf;
663
664 if (!surf)
665 return 0;
666
667 radeon_state_init(flush, rscreen->rw, R600_STATE_DB_FLUSH, 0, 0);
668 rtex = (struct r600_resource_texture*)surf->texture;
669 rbuffer = &rtex->resource;
670 /* just need to the bo to the flush list */
671 radeon_ws_bo_reference(rscreen->rw, &flush->bo[0], rbuffer->bo);
672 flush->placement[0] = RADEON_GEM_DOMAIN_VRAM;
673
674 flush->nbo = 1;
675 return radeon_state_pm4(flush);
676 }
677
678 int r600_context_hw_states(struct pipe_context *ctx)
679 {
680 struct r600_context *rctx = r600_context(ctx);
681 unsigned i;
682
683 /* build new states */
684 rctx->vtbl->rasterizer(rctx, &rctx->hw_states.rasterizer);
685 rctx->vtbl->scissor(rctx, &rctx->hw_states.scissor);
686 rctx->vtbl->dsa(rctx, &rctx->hw_states.dsa);
687 rctx->vtbl->cb_cntl(rctx, &rctx->hw_states.cb_cntl);
688
689 /* bind states */
690 radeon_draw_bind(&rctx->draw, &rctx->config);
691
692 radeon_draw_bind(&rctx->draw, &rctx->hw_states.rasterizer);
693 radeon_draw_bind(&rctx->draw, &rctx->hw_states.scissor);
694 radeon_draw_bind(&rctx->draw, &rctx->hw_states.dsa);
695 radeon_draw_bind(&rctx->draw, &rctx->hw_states.cb_cntl);
696
697 radeon_draw_bind(&rctx->draw, &rctx->hw_states.db_flush);
698 radeon_draw_bind(&rctx->draw, &rctx->hw_states.cb_flush);
699
700 if (rctx->viewport) {
701 radeon_draw_bind(&rctx->draw, &rctx->viewport->rstate[0]);
702 }
703 if (rctx->blend) {
704 radeon_draw_bind(&rctx->draw, &rctx->blend->rstate[0]);
705 }
706 if (rctx->clip) {
707 radeon_draw_bind(&rctx->draw, &rctx->clip->rstate[0]);
708 }
709 for (i = 0; i < rctx->framebuffer->state.framebuffer.nr_cbufs; i++) {
710 radeon_draw_bind(&rctx->draw, &rctx->framebuffer->rstate[i+1]);
711 }
712 if (rctx->framebuffer->state.framebuffer.zsbuf) {
713 radeon_draw_bind(&rctx->draw, &rctx->framebuffer->rstate[0]);
714 }
715
716 r600_bind_shader_sampler(rctx, &rctx->vs_sampler);
717 r600_bind_shader_sampler(rctx, &rctx->ps_sampler);
718
719 return 0;
720 }