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