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