c16280892850e218954c9ff75ac687bf8c3a0723
[profile/ivi/mesa.git] / src / gallium / drivers / nv50 / nv50_state.c
1 /*
2  * Copyright 2008 Ben Skeggs
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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "util/u_inlines.h"
26
27 #include "tgsi/tgsi_parse.h"
28
29 #include "nv50_context.h"
30 #include "nv50_texture.h"
31
32 #include "nouveau/nouveau_stateobj.h"
33
34 static INLINE uint32_t
35 nv50_colormask(unsigned mask)
36 {
37         uint32_t cmask = 0;
38
39         if (mask & PIPE_MASK_R)
40                 cmask |= 0x0001;
41         if (mask & PIPE_MASK_G)
42                 cmask |= 0x0010;
43         if (mask & PIPE_MASK_B)
44                 cmask |= 0x0100;
45         if (mask & PIPE_MASK_A)
46                 cmask |= 0x1000;
47
48         return cmask;
49 }
50
51 static void *
52 nv50_blend_state_create(struct pipe_context *pipe,
53                         const struct pipe_blend_state *cso)
54 {
55         struct nouveau_stateobj *so = so_new(5, 24, 0);
56         struct nouveau_grobj *tesla = nv50_context(pipe)->screen->tesla;
57         struct nv50_blend_stateobj *bso = CALLOC_STRUCT(nv50_blend_stateobj);
58         unsigned i, blend_enabled = 0;
59
60         /*XXX ignored:
61          *      - dither
62          */
63
64         so_method(so, tesla, NV50TCL_BLEND_ENABLE(0), 8);
65         if (cso->independent_blend_enable) {
66                 for (i = 0; i < 8; ++i) {
67                         so_data(so, cso->rt[i].blend_enable);
68                         if (cso->rt[i].blend_enable)
69                                 blend_enabled = 1;
70                 }
71         } else
72         if (cso->rt[0].blend_enable) {
73                 blend_enabled = 1;
74                 for (i = 0; i < 8; i++)
75                         so_data(so, 1);
76         } else {
77                 for (i = 0; i < 8; i++)
78                         so_data(so, 0);
79         }
80         if (blend_enabled) {
81                 so_method(so, tesla, NV50TCL_BLEND_EQUATION_RGB, 5);
82                 so_data  (so, nvgl_blend_eqn(cso->rt[0].rgb_func));
83                 so_data  (so, 0x4000 | nvgl_blend_func(cso->rt[0].rgb_src_factor));
84                 so_data  (so, 0x4000 | nvgl_blend_func(cso->rt[0].rgb_dst_factor));
85                 so_data  (so, nvgl_blend_eqn(cso->rt[0].alpha_func));
86                 so_data  (so, 0x4000 | nvgl_blend_func(cso->rt[0].alpha_src_factor));
87                 so_method(so, tesla, NV50TCL_BLEND_FUNC_DST_ALPHA, 1);
88                 so_data  (so, 0x4000 | nvgl_blend_func(cso->rt[0].alpha_dst_factor));
89         }
90
91         if (cso->logicop_enable == 0 ) {
92                 so_method(so, tesla, NV50TCL_LOGIC_OP_ENABLE, 1);
93                 so_data  (so, 0);
94         } else {
95                 so_method(so, tesla, NV50TCL_LOGIC_OP_ENABLE, 2);
96                 so_data  (so, 1);
97                 so_data  (so, nvgl_logicop_func(cso->logicop_func));
98         }
99
100         so_method(so, tesla, NV50TCL_COLOR_MASK(0), 8);
101         if (cso->independent_blend_enable)
102                 for (i = 0; i < 8; ++i)
103                         so_data(so, nv50_colormask(cso->rt[i].colormask));
104         else {
105                 uint32_t cmask = nv50_colormask(cso->rt[0].colormask);
106                 for (i = 0; i < 8; i++)
107                         so_data(so, cmask);
108         }
109
110         bso->pipe = *cso;
111         so_ref(so, &bso->so);
112         so_ref(NULL, &so);
113         return (void *)bso;
114 }
115
116 static void
117 nv50_blend_state_bind(struct pipe_context *pipe, void *hwcso)
118 {
119         struct nv50_context *nv50 = nv50_context(pipe);
120
121         nv50->blend = hwcso;
122         nv50->dirty |= NV50_NEW_BLEND;
123 }
124
125 static void
126 nv50_blend_state_delete(struct pipe_context *pipe, void *hwcso)
127 {
128         struct nv50_blend_stateobj *bso = hwcso;
129
130         so_ref(NULL, &bso->so);
131         FREE(bso);
132 }
133
134 static INLINE unsigned
135 wrap_mode(unsigned wrap)
136 {
137         switch (wrap) {
138         case PIPE_TEX_WRAP_REPEAT:
139                 return NV50TSC_1_0_WRAPS_REPEAT;
140         case PIPE_TEX_WRAP_MIRROR_REPEAT:
141                 return NV50TSC_1_0_WRAPS_MIRROR_REPEAT;
142         case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
143                 return NV50TSC_1_0_WRAPS_CLAMP_TO_EDGE;
144         case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
145                 return NV50TSC_1_0_WRAPS_CLAMP_TO_BORDER;
146         case PIPE_TEX_WRAP_CLAMP:
147                 return NV50TSC_1_0_WRAPS_CLAMP;
148         case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
149                 return NV50TSC_1_0_WRAPS_MIRROR_CLAMP_TO_EDGE;
150         case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
151                 return NV50TSC_1_0_WRAPS_MIRROR_CLAMP_TO_BORDER;
152         case PIPE_TEX_WRAP_MIRROR_CLAMP:
153                 return NV50TSC_1_0_WRAPS_MIRROR_CLAMP;
154         default:
155                 NOUVEAU_ERR("unknown wrap mode: %d\n", wrap);
156                 return NV50TSC_1_0_WRAPS_REPEAT;
157         }
158 }
159 static void *
160 nv50_sampler_state_create(struct pipe_context *pipe,
161                           const struct pipe_sampler_state *cso)
162 {
163         struct nv50_sampler_stateobj *sso = CALLOC(1, sizeof(*sso));
164         unsigned *tsc = sso->tsc;
165         float limit;
166
167         tsc[0] = (0x00026000 |
168                   (wrap_mode(cso->wrap_s) << 0) |
169                   (wrap_mode(cso->wrap_t) << 3) |
170                   (wrap_mode(cso->wrap_r) << 6));
171
172         switch (cso->mag_img_filter) {
173         case PIPE_TEX_FILTER_LINEAR:
174                 tsc[1] |= NV50TSC_1_1_MAGF_LINEAR;
175                 break;
176         case PIPE_TEX_FILTER_NEAREST:
177         default:
178                 tsc[1] |= NV50TSC_1_1_MAGF_NEAREST;
179                 break;
180         }
181
182         switch (cso->min_img_filter) {
183         case PIPE_TEX_FILTER_LINEAR:
184                 tsc[1] |= NV50TSC_1_1_MINF_LINEAR;
185                 break;
186         case PIPE_TEX_FILTER_NEAREST:
187         default:
188                 tsc[1] |= NV50TSC_1_1_MINF_NEAREST;
189                 break;
190         }
191
192         switch (cso->min_mip_filter) {
193         case PIPE_TEX_MIPFILTER_LINEAR:
194                 tsc[1] |= NV50TSC_1_1_MIPF_LINEAR;
195                 break;
196         case PIPE_TEX_MIPFILTER_NEAREST:
197                 tsc[1] |= NV50TSC_1_1_MIPF_NEAREST;
198                 break;
199         case PIPE_TEX_MIPFILTER_NONE:
200         default:
201                 tsc[1] |= NV50TSC_1_1_MIPF_NONE;
202                 break;
203         }
204
205         if (cso->max_anisotropy >= 16)
206                 tsc[0] |= (7 << 20);
207         else
208         if (cso->max_anisotropy >= 12)
209                 tsc[0] |= (6 << 20);
210         else {
211                 tsc[0] |= (cso->max_anisotropy >> 1) << 20;
212
213                 if (cso->max_anisotropy >= 4)
214                         tsc[1] |= NV50TSC_1_1_UNKN_ANISO_35;
215                 else
216                 if (cso->max_anisotropy >= 2)
217                         tsc[1] |= NV50TSC_1_1_UNKN_ANISO_15;
218         }
219
220         if (cso->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
221                 /* XXX: must be deactivated for non-shadow textures */
222                 tsc[0] |= (1 << 9);
223                 tsc[0] |= (nvgl_comparison_op(cso->compare_func) & 0x7) << 10;
224         }
225
226         limit = CLAMP(cso->lod_bias, -16.0, 15.0);
227         tsc[1] |= ((int)(limit * 256.0) & 0x1fff) << 12;
228
229         tsc[2] |= ((int)CLAMP(cso->max_lod, 0.0, 15.0) << 20) |
230                   ((int)CLAMP(cso->min_lod, 0.0, 15.0) << 8);
231
232         tsc[4] = fui(cso->border_color[0]);
233         tsc[5] = fui(cso->border_color[1]);
234         tsc[6] = fui(cso->border_color[2]);
235         tsc[7] = fui(cso->border_color[3]);
236
237         sso->normalized = cso->normalized_coords;
238         return (void *)sso;
239 }
240
241 /* type == 0 for VPs, 1 for GPs, 2 for FPs, which is how the
242  * relevant tesla methods are indexed (NV50TCL_BIND_TSC etc.)
243  */
244 static INLINE void
245 nv50_sampler_state_bind(struct pipe_context *pipe, unsigned type,
246                         unsigned nr, void **sampler)
247 {
248         struct nv50_context *nv50 = nv50_context(pipe);
249
250         memcpy(nv50->sampler[type], sampler, nr * sizeof(void *));
251
252         nv50->sampler_nr[type] = nr;
253         nv50->dirty |= NV50_NEW_SAMPLER;
254 }
255
256 static void
257 nv50_vp_sampler_state_bind(struct pipe_context *pipe, unsigned nr, void **s)
258 {
259         nv50_sampler_state_bind(pipe, 0, nr, s);
260 }
261
262 static void
263 nv50_fp_sampler_state_bind(struct pipe_context *pipe, unsigned nr, void **s)
264 {
265         nv50_sampler_state_bind(pipe, 2, nr, s);
266 }
267
268 static void
269 nv50_sampler_state_delete(struct pipe_context *pipe, void *hwcso)
270 {
271         FREE(hwcso);
272 }
273
274 static INLINE void
275 nv50_set_sampler_views(struct pipe_context *pipe, unsigned p,
276                        unsigned nr,
277                        struct pipe_sampler_view **views)
278 {
279         struct nv50_context *nv50 = nv50_context(pipe);
280         unsigned i;
281
282         for (i = 0; i < nr; i++)
283                 pipe_sampler_view_reference(&nv50->sampler_views[p][i],
284                                             views[i]);
285
286         for (i = nr; i < nv50->sampler_view_nr[p]; i++)
287                 pipe_sampler_view_reference(&nv50->sampler_views[p][i], NULL);
288
289         nv50->sampler_view_nr[p] = nr;
290         nv50->dirty |= NV50_NEW_TEXTURE;
291 }
292
293 static void
294 nv50_set_vp_sampler_views(struct pipe_context *pipe,
295                           unsigned nr,
296                           struct pipe_sampler_view **views)
297 {
298         nv50_set_sampler_views(pipe, 0, nr, views);
299 }
300
301 static void
302 nv50_set_fp_sampler_views(struct pipe_context *pipe,
303                           unsigned nr,
304                           struct pipe_sampler_view **views)
305 {
306         nv50_set_sampler_views(pipe, 2, nr, views);
307 }
308
309 static void
310 nv50_sampler_view_destroy(struct pipe_context *pipe,
311                           struct pipe_sampler_view *view)
312 {
313         pipe_texture_reference(&view->texture, NULL);
314         FREE(nv50_sampler_view(view));
315 }
316
317 static struct pipe_sampler_view *
318 nv50_create_sampler_view(struct pipe_context *pipe,
319                          struct pipe_texture *texture,
320                          const struct pipe_sampler_view *templ)
321 {
322         struct nv50_sampler_view *view = CALLOC_STRUCT(nv50_sampler_view);
323
324         view->pipe = *templ;
325         view->pipe.reference.count = 1;
326         view->pipe.texture = NULL;
327         pipe_texture_reference(&view->pipe.texture, texture);
328         view->pipe.context = pipe;
329
330         if (!nv50_tex_construct(view)) {
331                 nv50_sampler_view_destroy(pipe, &view->pipe);
332                 return NULL;
333         }
334         return &view->pipe;
335 }
336
337
338 static void *
339 nv50_rasterizer_state_create(struct pipe_context *pipe,
340                              const struct pipe_rasterizer_state *cso)
341 {
342         struct nouveau_stateobj *so = so_new(16, 22, 0);
343         struct nouveau_grobj *tesla = nv50_context(pipe)->screen->tesla;
344         struct nv50_rasterizer_stateobj *rso =
345                 CALLOC_STRUCT(nv50_rasterizer_stateobj);
346
347         /*XXX: ignored
348          *      - light_twosize
349          *      - point_smooth
350          *      - multisample
351          *      - point_sprite / sprite_coord_mode
352          */
353
354         so_method(so, tesla, NV50TCL_SCISSOR_ENABLE(0), 1);
355         so_data  (so, cso->scissor);
356
357         so_method(so, tesla, NV50TCL_SHADE_MODEL, 1);
358         so_data  (so, cso->flatshade ? NV50TCL_SHADE_MODEL_FLAT :
359                                        NV50TCL_SHADE_MODEL_SMOOTH);
360         so_method(so, tesla, NV50TCL_PROVOKING_VERTEX_LAST, 1);
361         so_data  (so, cso->flatshade_first ? 0 : 1);
362
363         so_method(so, tesla, NV50TCL_VERTEX_TWO_SIDE_ENABLE, 1);
364         so_data  (so, cso->light_twoside);
365
366         so_method(so, tesla, NV50TCL_LINE_WIDTH, 1);
367         so_data  (so, fui(cso->line_width));
368         so_method(so, tesla, NV50TCL_LINE_SMOOTH_ENABLE, 1);
369         so_data  (so, cso->line_smooth ? 1 : 0);
370         if (cso->line_stipple_enable) {
371                 so_method(so, tesla, NV50TCL_LINE_STIPPLE_ENABLE, 1);
372                 so_data  (so, 1);
373                 so_method(so, tesla, NV50TCL_LINE_STIPPLE_PATTERN, 1);
374                 so_data  (so, (cso->line_stipple_pattern << 8) |
375                                cso->line_stipple_factor);
376         } else {
377                 so_method(so, tesla, NV50TCL_LINE_STIPPLE_ENABLE, 1);
378                 so_data  (so, 0);
379         }
380
381         so_method(so, tesla, NV50TCL_POINT_SIZE, 1);
382         so_data  (so, fui(cso->point_size));
383
384         so_method(so, tesla, NV50TCL_POINT_SPRITE_ENABLE, 1);
385         so_data  (so, cso->point_quad_rasterization ? 1 : 0);
386
387         so_method(so, tesla, NV50TCL_POLYGON_MODE_FRONT, 3);
388         if (cso->front_winding == PIPE_WINDING_CCW) {
389                 so_data(so, nvgl_polygon_mode(cso->fill_ccw));
390                 so_data(so, nvgl_polygon_mode(cso->fill_cw));
391         } else {
392                 so_data(so, nvgl_polygon_mode(cso->fill_cw));
393                 so_data(so, nvgl_polygon_mode(cso->fill_ccw));
394         }
395         so_data(so, cso->poly_smooth ? 1 : 0);
396
397         so_method(so, tesla, NV50TCL_CULL_FACE_ENABLE, 3);
398         so_data  (so, cso->cull_mode != PIPE_WINDING_NONE);
399         if (cso->front_winding == PIPE_WINDING_CCW) {
400                 so_data(so, NV50TCL_FRONT_FACE_CCW);
401                 switch (cso->cull_mode) {
402                 case PIPE_WINDING_CCW:
403                         so_data(so, NV50TCL_CULL_FACE_FRONT);
404                         break;
405                 case PIPE_WINDING_CW:
406                         so_data(so, NV50TCL_CULL_FACE_BACK);
407                         break;
408                 case PIPE_WINDING_BOTH:
409                         so_data(so, NV50TCL_CULL_FACE_FRONT_AND_BACK);
410                         break;
411                 default:
412                         so_data(so, NV50TCL_CULL_FACE_BACK);
413                         break;
414                 }
415         } else {
416                 so_data(so, NV50TCL_FRONT_FACE_CW);
417                 switch (cso->cull_mode) {
418                 case PIPE_WINDING_CCW:
419                         so_data(so, NV50TCL_CULL_FACE_BACK);
420                         break;
421                 case PIPE_WINDING_CW:
422                         so_data(so, NV50TCL_CULL_FACE_FRONT);
423                         break;
424                 case PIPE_WINDING_BOTH:
425                         so_data(so, NV50TCL_CULL_FACE_FRONT_AND_BACK);
426                         break;
427                 default:
428                         so_data(so, NV50TCL_CULL_FACE_BACK);
429                         break;
430                 }
431         }
432
433         so_method(so, tesla, NV50TCL_POLYGON_STIPPLE_ENABLE, 1);
434         so_data  (so, cso->poly_stipple_enable ? 1 : 0);
435
436         so_method(so, tesla, NV50TCL_POLYGON_OFFSET_POINT_ENABLE, 3);
437         if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_POINT) ||
438             (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_POINT))
439                 so_data(so, 1);
440         else
441                 so_data(so, 0);
442         if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_LINE) ||
443             (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_LINE))
444                 so_data(so, 1);
445         else
446                 so_data(so, 0);
447         if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_FILL) ||
448             (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_FILL))
449                 so_data(so, 1);
450         else
451                 so_data(so, 0);
452
453         if (cso->offset_cw || cso->offset_ccw) {
454                 so_method(so, tesla, NV50TCL_POLYGON_OFFSET_FACTOR, 1);
455                 so_data  (so, fui(cso->offset_scale));
456                 so_method(so, tesla, NV50TCL_POLYGON_OFFSET_UNITS, 1);
457                 so_data  (so, fui(cso->offset_units * 2.0f));
458         }
459
460         rso->pipe = *cso;
461         so_ref(so, &rso->so);
462         so_ref(NULL, &so);
463         return (void *)rso;
464 }
465
466 static void
467 nv50_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)
468 {
469         struct nv50_context *nv50 = nv50_context(pipe);
470
471         nv50->rasterizer = hwcso;
472         nv50->dirty |= NV50_NEW_RASTERIZER;
473 }
474
475 static void
476 nv50_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
477 {
478         struct nv50_rasterizer_stateobj *rso = hwcso;
479
480         so_ref(NULL, &rso->so);
481         FREE(rso);
482 }
483
484 static void *
485 nv50_depth_stencil_alpha_state_create(struct pipe_context *pipe,
486                         const struct pipe_depth_stencil_alpha_state *cso)
487 {
488         struct nouveau_grobj *tesla = nv50_context(pipe)->screen->tesla;
489         struct nv50_zsa_stateobj *zsa = CALLOC_STRUCT(nv50_zsa_stateobj);
490         struct nouveau_stateobj *so = so_new(9, 21, 0);
491
492         so_method(so, tesla, NV50TCL_DEPTH_WRITE_ENABLE, 1);
493         so_data  (so, cso->depth.writemask ? 1 : 0);
494         if (cso->depth.enabled) {
495                 so_method(so, tesla, NV50TCL_DEPTH_TEST_ENABLE, 1);
496                 so_data  (so, 1);
497                 so_method(so, tesla, NV50TCL_DEPTH_TEST_FUNC, 1);
498                 so_data  (so, nvgl_comparison_op(cso->depth.func));
499         } else {
500                 so_method(so, tesla, NV50TCL_DEPTH_TEST_ENABLE, 1);
501                 so_data  (so, 0);
502         }
503
504         if (cso->stencil[0].enabled) {
505                 so_method(so, tesla, NV50TCL_STENCIL_FRONT_ENABLE, 5);
506                 so_data  (so, 1);
507                 so_data  (so, nvgl_stencil_op(cso->stencil[0].fail_op));
508                 so_data  (so, nvgl_stencil_op(cso->stencil[0].zfail_op));
509                 so_data  (so, nvgl_stencil_op(cso->stencil[0].zpass_op));
510                 so_data  (so, nvgl_comparison_op(cso->stencil[0].func));
511                 so_method(so, tesla, NV50TCL_STENCIL_FRONT_MASK, 2);
512                 so_data  (so, cso->stencil[0].writemask);
513                 so_data  (so, cso->stencil[0].valuemask);
514         } else {
515                 so_method(so, tesla, NV50TCL_STENCIL_FRONT_ENABLE, 1);
516                 so_data  (so, 0);
517         }
518
519         if (cso->stencil[1].enabled) {
520                 so_method(so, tesla, NV50TCL_STENCIL_BACK_ENABLE, 5);
521                 so_data  (so, 1);
522                 so_data  (so, nvgl_stencil_op(cso->stencil[1].fail_op));
523                 so_data  (so, nvgl_stencil_op(cso->stencil[1].zfail_op));
524                 so_data  (so, nvgl_stencil_op(cso->stencil[1].zpass_op));
525                 so_data  (so, nvgl_comparison_op(cso->stencil[1].func));
526                 so_method(so, tesla, NV50TCL_STENCIL_BACK_MASK, 2);
527                 so_data  (so, cso->stencil[1].writemask);
528                 so_data  (so, cso->stencil[1].valuemask);
529         } else {
530                 so_method(so, tesla, NV50TCL_STENCIL_BACK_ENABLE, 1);
531                 so_data  (so, 0);
532         }
533
534         if (cso->alpha.enabled) {
535                 so_method(so, tesla, NV50TCL_ALPHA_TEST_ENABLE, 1);
536                 so_data  (so, 1);
537                 so_method(so, tesla, NV50TCL_ALPHA_TEST_REF, 2);
538                 so_data  (so, fui(cso->alpha.ref_value));
539                 so_data  (so, nvgl_comparison_op(cso->alpha.func));
540         } else {
541                 so_method(so, tesla, NV50TCL_ALPHA_TEST_ENABLE, 1);
542                 so_data  (so, 0);
543         }
544
545         zsa->pipe = *cso;
546         so_ref(so, &zsa->so);
547         so_ref(NULL, &so);
548         return (void *)zsa;
549 }
550
551 static void
552 nv50_depth_stencil_alpha_state_bind(struct pipe_context *pipe, void *hwcso)
553 {
554         struct nv50_context *nv50 = nv50_context(pipe);
555
556         nv50->zsa = hwcso;
557         nv50->dirty |= NV50_NEW_ZSA;
558 }
559
560 static void
561 nv50_depth_stencil_alpha_state_delete(struct pipe_context *pipe, void *hwcso)
562 {
563         struct nv50_zsa_stateobj *zsa = hwcso;
564
565         so_ref(NULL, &zsa->so);
566         FREE(zsa);
567 }
568
569 static void *
570 nv50_vp_state_create(struct pipe_context *pipe,
571                      const struct pipe_shader_state *cso)
572 {
573         struct nv50_program *p = CALLOC_STRUCT(nv50_program);
574
575         p->pipe.tokens = tgsi_dup_tokens(cso->tokens);
576         p->type = PIPE_SHADER_VERTEX;
577         tgsi_scan_shader(p->pipe.tokens, &p->info);
578         return (void *)p;
579 }
580
581 static void
582 nv50_vp_state_bind(struct pipe_context *pipe, void *hwcso)
583 {
584         struct nv50_context *nv50 = nv50_context(pipe);
585
586         nv50->vertprog = hwcso;
587         nv50->dirty |= NV50_NEW_VERTPROG;
588 }
589
590 static void
591 nv50_vp_state_delete(struct pipe_context *pipe, void *hwcso)
592 {
593         struct nv50_context *nv50 = nv50_context(pipe);
594         struct nv50_program *p = hwcso;
595
596         nv50_program_destroy(nv50, p);
597         FREE((void *)p->pipe.tokens);
598         FREE(p);
599 }
600
601 static void *
602 nv50_fp_state_create(struct pipe_context *pipe,
603                      const struct pipe_shader_state *cso)
604 {
605         struct nv50_program *p = CALLOC_STRUCT(nv50_program);
606
607         p->pipe.tokens = tgsi_dup_tokens(cso->tokens);
608         p->type = PIPE_SHADER_FRAGMENT;
609         tgsi_scan_shader(p->pipe.tokens, &p->info);
610         return (void *)p;
611 }
612
613 static void
614 nv50_fp_state_bind(struct pipe_context *pipe, void *hwcso)
615 {
616         struct nv50_context *nv50 = nv50_context(pipe);
617
618         nv50->fragprog = hwcso;
619         nv50->dirty |= NV50_NEW_FRAGPROG;
620 }
621
622 static void
623 nv50_fp_state_delete(struct pipe_context *pipe, void *hwcso)
624 {
625         struct nv50_context *nv50 = nv50_context(pipe);
626         struct nv50_program *p = hwcso;
627
628         nv50_program_destroy(nv50, p);
629         FREE((void *)p->pipe.tokens);
630         FREE(p);
631 }
632
633 static void *
634 nv50_gp_state_create(struct pipe_context *pipe,
635                      const struct pipe_shader_state *cso)
636 {
637         struct nv50_program *p = CALLOC_STRUCT(nv50_program);
638
639         p->pipe.tokens = tgsi_dup_tokens(cso->tokens);
640         p->type = PIPE_SHADER_GEOMETRY;
641         tgsi_scan_shader(p->pipe.tokens, &p->info);
642         return (void *)p;
643 }
644
645 static void
646 nv50_gp_state_bind(struct pipe_context *pipe, void *hwcso)
647 {
648         struct nv50_context *nv50 = nv50_context(pipe);
649
650         nv50->fragprog = hwcso;
651         nv50->dirty |= NV50_NEW_GEOMPROG;
652 }
653
654 static void
655 nv50_gp_state_delete(struct pipe_context *pipe, void *hwcso)
656 {
657         struct nv50_context *nv50 = nv50_context(pipe);
658         struct nv50_program *p = hwcso;
659
660         nv50_program_destroy(nv50, p);
661         FREE((void *)p->pipe.tokens);
662         FREE(p);
663 }
664
665 static void
666 nv50_set_blend_color(struct pipe_context *pipe,
667                      const struct pipe_blend_color *bcol)
668 {
669         struct nv50_context *nv50 = nv50_context(pipe);
670
671         nv50->blend_colour = *bcol;
672         nv50->dirty |= NV50_NEW_BLEND_COLOUR;
673 }
674
675  static void
676 nv50_set_stencil_ref(struct pipe_context *pipe,
677                      const struct pipe_stencil_ref *sr)
678 {
679         struct nv50_context *nv50 = nv50_context(pipe);
680
681         nv50->stencil_ref = *sr;
682         nv50->dirty |= NV50_NEW_STENCIL_REF;
683 }
684
685 static void
686 nv50_set_clip_state(struct pipe_context *pipe,
687                     const struct pipe_clip_state *clip)
688 {
689 }
690
691 static void
692 nv50_set_constant_buffer(struct pipe_context *pipe, uint shader, uint index,
693                          struct pipe_buffer *buf )
694 {
695         struct nv50_context *nv50 = nv50_context(pipe);
696
697         if (shader == PIPE_SHADER_VERTEX) {
698                 nv50->constbuf[PIPE_SHADER_VERTEX] = buf;
699                 nv50->dirty |= NV50_NEW_VERTPROG_CB;
700         } else
701         if (shader == PIPE_SHADER_FRAGMENT) {
702                 nv50->constbuf[PIPE_SHADER_FRAGMENT] = buf;
703                 nv50->dirty |= NV50_NEW_FRAGPROG_CB;
704         } else
705         if (shader == PIPE_SHADER_GEOMETRY) {
706                 nv50->constbuf[PIPE_SHADER_GEOMETRY] = buf;
707                 nv50->dirty |= NV50_NEW_GEOMPROG_CB;
708         }
709 }
710
711 static void
712 nv50_set_framebuffer_state(struct pipe_context *pipe,
713                            const struct pipe_framebuffer_state *fb)
714 {
715         struct nv50_context *nv50 = nv50_context(pipe);
716
717         nv50->framebuffer = *fb;
718         nv50->dirty |= NV50_NEW_FRAMEBUFFER;
719 }
720
721 static void
722 nv50_set_polygon_stipple(struct pipe_context *pipe,
723                          const struct pipe_poly_stipple *stipple)
724 {
725         struct nv50_context *nv50 = nv50_context(pipe);
726
727         nv50->stipple = *stipple;
728         nv50->dirty |= NV50_NEW_STIPPLE;
729 }
730
731 static void
732 nv50_set_scissor_state(struct pipe_context *pipe,
733                        const struct pipe_scissor_state *s)
734 {
735         struct nv50_context *nv50 = nv50_context(pipe);
736
737         nv50->scissor = *s;
738         nv50->dirty |= NV50_NEW_SCISSOR;
739 }
740
741 static void
742 nv50_set_viewport_state(struct pipe_context *pipe,
743                         const struct pipe_viewport_state *vpt)
744 {
745         struct nv50_context *nv50 = nv50_context(pipe);
746
747         nv50->viewport = *vpt;
748         nv50->dirty |= NV50_NEW_VIEWPORT;
749 }
750
751 static void
752 nv50_set_vertex_buffers(struct pipe_context *pipe, unsigned count,
753                         const struct pipe_vertex_buffer *vb)
754 {
755         struct nv50_context *nv50 = nv50_context(pipe);
756
757         memcpy(nv50->vtxbuf, vb, sizeof(*vb) * count);
758         nv50->vtxbuf_nr = count;
759
760         nv50->dirty |= NV50_NEW_ARRAYS;
761 }
762
763 static void *
764 nv50_vtxelts_state_create(struct pipe_context *pipe,
765                           unsigned num_elements,
766                           const struct pipe_vertex_element *elements)
767 {
768         struct nv50_vtxelt_stateobj *cso = CALLOC_STRUCT(nv50_vtxelt_stateobj);
769
770         assert(num_elements < 16); /* not doing fallbacks yet */
771         cso->num_elements = num_elements;
772         memcpy(cso->pipe, elements, num_elements * sizeof(*elements));
773
774         nv50_vtxelt_construct(cso);
775
776         return (void *)cso;
777 }
778
779 static void
780 nv50_vtxelts_state_delete(struct pipe_context *pipe, void *hwcso)
781 {
782         FREE(hwcso);
783 }
784
785 static void
786 nv50_vtxelts_state_bind(struct pipe_context *pipe, void *hwcso)
787 {
788         struct nv50_context *nv50 = nv50_context(pipe);
789
790         nv50->vtxelt = hwcso;
791         nv50->dirty |= NV50_NEW_ARRAYS;
792 }
793
794 void
795 nv50_init_state_functions(struct nv50_context *nv50)
796 {
797         nv50->pipe.create_blend_state = nv50_blend_state_create;
798         nv50->pipe.bind_blend_state = nv50_blend_state_bind;
799         nv50->pipe.delete_blend_state = nv50_blend_state_delete;
800
801         nv50->pipe.create_sampler_state = nv50_sampler_state_create;
802         nv50->pipe.delete_sampler_state = nv50_sampler_state_delete;
803         nv50->pipe.bind_fragment_sampler_states = nv50_fp_sampler_state_bind;
804         nv50->pipe.bind_vertex_sampler_states   = nv50_vp_sampler_state_bind;
805         nv50->pipe.set_fragment_sampler_views = nv50_set_fp_sampler_views;
806         nv50->pipe.set_vertex_sampler_views   = nv50_set_vp_sampler_views;
807         nv50->pipe.create_sampler_view = nv50_create_sampler_view;
808         nv50->pipe.sampler_view_destroy = nv50_sampler_view_destroy;
809
810         nv50->pipe.create_rasterizer_state = nv50_rasterizer_state_create;
811         nv50->pipe.bind_rasterizer_state = nv50_rasterizer_state_bind;
812         nv50->pipe.delete_rasterizer_state = nv50_rasterizer_state_delete;
813
814         nv50->pipe.create_depth_stencil_alpha_state =
815                 nv50_depth_stencil_alpha_state_create;
816         nv50->pipe.bind_depth_stencil_alpha_state =
817                 nv50_depth_stencil_alpha_state_bind;
818         nv50->pipe.delete_depth_stencil_alpha_state =
819                 nv50_depth_stencil_alpha_state_delete;
820
821         nv50->pipe.create_vs_state = nv50_vp_state_create;
822         nv50->pipe.bind_vs_state = nv50_vp_state_bind;
823         nv50->pipe.delete_vs_state = nv50_vp_state_delete;
824
825         nv50->pipe.create_fs_state = nv50_fp_state_create;
826         nv50->pipe.bind_fs_state = nv50_fp_state_bind;
827         nv50->pipe.delete_fs_state = nv50_fp_state_delete;
828
829         nv50->pipe.create_gs_state = nv50_gp_state_create;
830         nv50->pipe.bind_gs_state = nv50_gp_state_bind;
831         nv50->pipe.delete_gs_state = nv50_gp_state_delete;
832
833         nv50->pipe.set_blend_color = nv50_set_blend_color;
834         nv50->pipe.set_stencil_ref = nv50_set_stencil_ref;
835         nv50->pipe.set_clip_state = nv50_set_clip_state;
836         nv50->pipe.set_constant_buffer = nv50_set_constant_buffer;
837         nv50->pipe.set_framebuffer_state = nv50_set_framebuffer_state;
838         nv50->pipe.set_polygon_stipple = nv50_set_polygon_stipple;
839         nv50->pipe.set_scissor_state = nv50_set_scissor_state;
840         nv50->pipe.set_viewport_state = nv50_set_viewport_state;
841
842         nv50->pipe.create_vertex_elements_state = nv50_vtxelts_state_create;
843         nv50->pipe.delete_vertex_elements_state = nv50_vtxelts_state_delete;
844         nv50->pipe.bind_vertex_elements_state = nv50_vtxelts_state_bind;
845
846         nv50->pipe.set_vertex_buffers = nv50_set_vertex_buffers;
847 }
848