Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / nv50 / nv50_state.c
1 /*
2  * Copyright 2010 Christoph Bumiller
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_defines.h"
24 #include "util/u_inlines.h"
25 #include "util/u_transfer.h"
26
27 #include "tgsi/tgsi_parse.h"
28
29 #include "nv50_stateobj.h"
30 #include "nv50_context.h"
31
32 #include "nv50_3d.xml.h"
33 #include "nv50_texture.xml.h"
34
35 #include "nouveau/nouveau_gldefs.h"
36
37 /* Caveats:
38  *  ! pipe_sampler_state.normalized_coords is ignored - rectangle textures will
39  *     use non-normalized coordinates, everything else won't
40  *    (The relevant bit is in the TIC entry and not the TSC entry.)
41  *
42  *  ! pipe_sampler_state.seamless_cube_map is ignored - seamless filtering is
43  *     always activated on NVA0 +
44  *    (Give me the global bit, otherwise it's not worth the CPU work.)
45  *
46  *  ! pipe_sampler_state.border_color is not swizzled according to the texture
47  *     swizzle in pipe_sampler_view
48  *    (This will be ugly with indirect independent texture/sampler access,
49  *     we'd have to emulate the logic in the shader. GL doesn't have that,
50  *     D3D doesn't have swizzle, if we knew what we were implementing we'd be
51  *     good.)
52  *
53  *  ! pipe_rasterizer_state.line_last_pixel is ignored - it is never drawn
54  *
55  *  ! pipe_rasterizer_state.flatshade_first also applies to QUADS
56  *    (There's a GL query for that, forcing an exception is just ridiculous.)
57  *
58  *  ! pipe_rasterizer_state.gl_rasterization_rules is ignored - pixel centers
59  *     are always at half integer coordinates and the top-left rule applies
60  *    (There does not seem to be a hardware switch for this.)
61  *
62  *  ! pipe_rasterizer_state.sprite_coord_enable is masked with 0xff on NVC0
63  *    (The hardware only has 8 slots meant for TexCoord and we have to assign
64  *     in advance to maintain elegant separate shader objects.)
65  */
66
67 static INLINE uint32_t
68 nv50_colormask(unsigned mask)
69 {
70    uint32_t ret = 0;
71
72    if (mask & PIPE_MASK_R)
73       ret |= 0x0001;
74    if (mask & PIPE_MASK_G)
75       ret |= 0x0010;
76    if (mask & PIPE_MASK_B)
77       ret |= 0x0100;
78    if (mask & PIPE_MASK_A)
79       ret |= 0x1000;
80
81    return ret;
82 }
83
84 #define NV50_BLEND_FACTOR_CASE(a, b) \
85    case PIPE_BLENDFACTOR_##a: return NV50_3D_BLEND_FACTOR_##b
86
87 static INLINE uint32_t
88 nv50_blend_fac(unsigned factor)
89 {
90    switch (factor) {
91    NV50_BLEND_FACTOR_CASE(ONE, ONE);
92    NV50_BLEND_FACTOR_CASE(SRC_COLOR, SRC_COLOR);
93    NV50_BLEND_FACTOR_CASE(SRC_ALPHA, SRC_ALPHA);
94    NV50_BLEND_FACTOR_CASE(DST_ALPHA, DST_ALPHA);
95    NV50_BLEND_FACTOR_CASE(DST_COLOR, DST_COLOR);
96    NV50_BLEND_FACTOR_CASE(SRC_ALPHA_SATURATE, SRC_ALPHA_SATURATE);
97    NV50_BLEND_FACTOR_CASE(CONST_COLOR, CONSTANT_COLOR);
98    NV50_BLEND_FACTOR_CASE(CONST_ALPHA, CONSTANT_ALPHA);
99    NV50_BLEND_FACTOR_CASE(SRC1_COLOR, SRC1_COLOR);
100    NV50_BLEND_FACTOR_CASE(SRC1_ALPHA, SRC1_ALPHA);
101    NV50_BLEND_FACTOR_CASE(ZERO, ZERO);
102    NV50_BLEND_FACTOR_CASE(INV_SRC_COLOR, ONE_MINUS_SRC_COLOR);
103    NV50_BLEND_FACTOR_CASE(INV_SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
104    NV50_BLEND_FACTOR_CASE(INV_DST_ALPHA, ONE_MINUS_DST_ALPHA);
105    NV50_BLEND_FACTOR_CASE(INV_DST_COLOR, ONE_MINUS_DST_COLOR);
106    NV50_BLEND_FACTOR_CASE(INV_CONST_COLOR, ONE_MINUS_CONSTANT_COLOR);
107    NV50_BLEND_FACTOR_CASE(INV_CONST_ALPHA, ONE_MINUS_CONSTANT_ALPHA);
108    NV50_BLEND_FACTOR_CASE(INV_SRC1_COLOR, ONE_MINUS_SRC1_COLOR);
109    NV50_BLEND_FACTOR_CASE(INV_SRC1_ALPHA, ONE_MINUS_SRC1_ALPHA);
110    default:
111       return NV50_3D_BLEND_FACTOR_ZERO;
112    }
113 }
114
115 static void *
116 nv50_blend_state_create(struct pipe_context *pipe,
117                         const struct pipe_blend_state *cso)
118 {
119    struct nv50_blend_stateobj *so = CALLOC_STRUCT(nv50_blend_stateobj);
120    int i;
121    boolean emit_common_func = cso->rt[0].blend_enable;
122
123    if (nv50_context(pipe)->screen->tesla->grclass >= NVA3_3D) {
124       SB_BEGIN_3D(so, BLEND_INDEPENDENT, 1);
125       SB_DATA    (so, cso->independent_blend_enable);
126    }
127
128    so->pipe = *cso;
129
130    SB_BEGIN_3D(so, COLOR_MASK_COMMON, 1);
131    SB_DATA    (so, !cso->independent_blend_enable);
132
133    SB_BEGIN_3D(so, BLEND_ENABLE_COMMON, 1);
134    SB_DATA    (so, !cso->independent_blend_enable);
135
136    if (cso->independent_blend_enable) {
137       SB_BEGIN_3D(so, BLEND_ENABLE(0), 8);
138       for (i = 0; i < 8; ++i) {
139          SB_DATA(so, cso->rt[i].blend_enable);
140          if (cso->rt[i].blend_enable)
141             emit_common_func = TRUE;
142       }
143
144       if (nv50_context(pipe)->screen->tesla->grclass >= NVA3_3D) {
145          emit_common_func = FALSE;
146
147          for (i = 0; i < 8; ++i) {
148             if (!cso->rt[i].blend_enable)
149                continue;
150             SB_BEGIN_3D_(so, NVA3_3D_IBLEND_EQUATION_RGB(i), 6);
151             SB_DATA     (so, nvgl_blend_eqn(cso->rt[i].rgb_func));
152             SB_DATA     (so, nv50_blend_fac(cso->rt[i].rgb_src_factor));
153             SB_DATA     (so, nv50_blend_fac(cso->rt[i].rgb_dst_factor));
154             SB_DATA     (so, nvgl_blend_eqn(cso->rt[i].alpha_func));
155             SB_DATA     (so, nv50_blend_fac(cso->rt[i].alpha_src_factor));
156             SB_DATA     (so, nv50_blend_fac(cso->rt[i].alpha_dst_factor));
157          }
158       }
159    } else {
160       SB_BEGIN_3D(so, BLEND_ENABLE(0), 1);
161       SB_DATA    (so, cso->rt[0].blend_enable);
162    }
163
164    if (emit_common_func) {
165       SB_BEGIN_3D(so, BLEND_EQUATION_RGB, 5);
166       SB_DATA    (so, nvgl_blend_eqn(cso->rt[0].rgb_func));
167       SB_DATA    (so, nv50_blend_fac(cso->rt[0].rgb_src_factor));
168       SB_DATA    (so, nv50_blend_fac(cso->rt[0].rgb_dst_factor));
169       SB_DATA    (so, nvgl_blend_eqn(cso->rt[0].alpha_func));
170       SB_DATA    (so, nv50_blend_fac(cso->rt[0].alpha_src_factor));
171       SB_BEGIN_3D(so, BLEND_FUNC_DST_ALPHA, 1);
172       SB_DATA    (so, nv50_blend_fac(cso->rt[0].alpha_dst_factor));
173    }
174
175    if (cso->logicop_enable) {
176       SB_BEGIN_3D(so, LOGIC_OP_ENABLE, 2);
177       SB_DATA    (so, 1);
178       SB_DATA    (so, nvgl_logicop_func(cso->logicop_func));
179    } else {
180       SB_BEGIN_3D(so, LOGIC_OP_ENABLE, 1);
181       SB_DATA    (so, 0);
182    }
183
184    if (cso->independent_blend_enable) {
185       SB_BEGIN_3D(so, COLOR_MASK(0), 8);
186       for (i = 0; i < 8; ++i)
187          SB_DATA(so, nv50_colormask(cso->rt[i].colormask));
188    } else {
189       SB_BEGIN_3D(so, COLOR_MASK(0), 1);
190       SB_DATA    (so, nv50_colormask(cso->rt[0].colormask));
191    }
192
193    assert(so->size <= (sizeof(so->state) / sizeof(so->state[0])));
194    return so;
195 }
196
197 static void
198 nv50_blend_state_bind(struct pipe_context *pipe, void *hwcso)
199 {
200    struct nv50_context *nv50 = nv50_context(pipe);
201
202    nv50->blend = hwcso;
203    nv50->dirty |= NV50_NEW_BLEND;
204 }
205
206 static void
207 nv50_blend_state_delete(struct pipe_context *pipe, void *hwcso)
208 {
209    FREE(hwcso);
210 }
211
212 /* NOTE: ignoring line_last_pixel, using FALSE (set on screen init) */
213 static void *
214 nv50_rasterizer_state_create(struct pipe_context *pipe,
215                              const struct pipe_rasterizer_state *cso)
216 {
217    struct nv50_rasterizer_stateobj *so;
218
219    so = CALLOC_STRUCT(nv50_rasterizer_stateobj);
220    if (!so)
221       return NULL;
222    so->pipe = *cso;
223
224 #ifndef NV50_SCISSORS_CLIPPING
225    SB_BEGIN_3D(so, SCISSOR_ENABLE(0), 1);
226    SB_DATA    (so, cso->scissor);
227 #endif
228     
229    SB_BEGIN_3D(so, SHADE_MODEL, 1);
230    SB_DATA    (so, cso->flatshade ? NV50_3D_SHADE_MODEL_FLAT :
231                                     NV50_3D_SHADE_MODEL_SMOOTH);
232    SB_BEGIN_3D(so, PROVOKING_VERTEX_LAST, 1);
233    SB_DATA    (so, !cso->flatshade_first);
234    SB_BEGIN_3D(so, VERTEX_TWO_SIDE_ENABLE, 1);
235    SB_DATA    (so, cso->light_twoside);
236
237    SB_BEGIN_3D(so, FRAG_COLOR_CLAMP_EN, 1);
238    SB_DATA    (so, cso->clamp_fragment_color ? 0x11111111 : 0x00000000);
239
240    SB_BEGIN_3D(so, LINE_WIDTH, 1);
241    SB_DATA    (so, fui(cso->line_width));
242    SB_BEGIN_3D(so, LINE_SMOOTH_ENABLE, 1);
243    SB_DATA    (so, cso->line_smooth);
244
245    SB_BEGIN_3D(so, LINE_STIPPLE_ENABLE, 1);
246    if (cso->line_stipple_enable) {
247       SB_DATA    (so, 1);
248       SB_BEGIN_3D(so, LINE_STIPPLE, 1);
249       SB_DATA    (so, (cso->line_stipple_pattern << 8) |
250                   cso->line_stipple_factor);
251    } else {
252       SB_DATA    (so, 0);
253    }
254
255    if (!cso->point_size_per_vertex) {
256       SB_BEGIN_3D(so, POINT_SIZE, 1);
257       SB_DATA    (so, fui(cso->point_size));
258    }
259    SB_BEGIN_3D(so, POINT_SPRITE_ENABLE, 1);
260    SB_DATA    (so, cso->point_quad_rasterization);
261    SB_BEGIN_3D(so, POINT_SMOOTH_ENABLE, 1);
262    SB_DATA    (so, cso->point_smooth);
263
264    SB_BEGIN_3D(so, POLYGON_MODE_FRONT, 3);
265    SB_DATA    (so, nvgl_polygon_mode(cso->fill_front));
266    SB_DATA    (so, nvgl_polygon_mode(cso->fill_back));
267    SB_DATA    (so, cso->poly_smooth);
268
269    SB_BEGIN_3D(so, CULL_FACE_ENABLE, 3);
270    SB_DATA    (so, cso->cull_face != PIPE_FACE_NONE);
271    SB_DATA    (so, cso->front_ccw ? NV50_3D_FRONT_FACE_CCW :
272                                     NV50_3D_FRONT_FACE_CW);
273    switch (cso->cull_face) {
274    case PIPE_FACE_FRONT_AND_BACK:
275       SB_DATA(so, NV50_3D_CULL_FACE_FRONT_AND_BACK);
276       break;
277    case PIPE_FACE_FRONT:
278       SB_DATA(so, NV50_3D_CULL_FACE_FRONT);
279       break;
280    case PIPE_FACE_BACK:
281    default:
282      SB_DATA(so, NV50_3D_CULL_FACE_BACK);
283      break;
284    }
285
286    SB_BEGIN_3D(so, POLYGON_STIPPLE_ENABLE, 1);
287    SB_DATA    (so, cso->poly_stipple_enable);
288    SB_BEGIN_3D(so, POLYGON_OFFSET_POINT_ENABLE, 3);
289    SB_DATA    (so, cso->offset_point);
290    SB_DATA    (so, cso->offset_line);
291    SB_DATA    (so, cso->offset_tri);
292
293    if (cso->offset_point || cso->offset_line || cso->offset_tri) {
294       SB_BEGIN_3D(so, POLYGON_OFFSET_FACTOR, 1);
295       SB_DATA    (so, fui(cso->offset_scale));
296       SB_BEGIN_3D(so, POLYGON_OFFSET_UNITS, 1);
297       SB_DATA    (so, fui(cso->offset_units * 2.0f));
298    }
299
300    assert(so->size <= (sizeof(so->state) / sizeof(so->state[0])));
301    return (void *)so;
302 }
303
304 static void
305 nv50_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)
306 {
307    struct nv50_context *nv50 = nv50_context(pipe);
308
309    nv50->rast = hwcso;
310    nv50->dirty |= NV50_NEW_RASTERIZER;
311 }
312
313 static void
314 nv50_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
315 {
316    FREE(hwcso);
317 }
318
319 static void *
320 nv50_zsa_state_create(struct pipe_context *pipe,
321                       const struct pipe_depth_stencil_alpha_state *cso)
322 {
323    struct nv50_zsa_stateobj *so = CALLOC_STRUCT(nv50_zsa_stateobj);
324
325    so->pipe = *cso;
326
327    SB_BEGIN_3D(so, DEPTH_WRITE_ENABLE, 1);
328    SB_DATA    (so, cso->depth.writemask);
329    SB_BEGIN_3D(so, DEPTH_TEST_ENABLE, 1);
330    if (cso->depth.enabled) {
331       SB_DATA    (so, 1);
332       SB_BEGIN_3D(so, DEPTH_TEST_FUNC, 1);
333       SB_DATA    (so, nvgl_comparison_op(cso->depth.func));
334    } else {
335       SB_DATA    (so, 0);
336    }
337
338    if (cso->stencil[0].enabled) {
339       SB_BEGIN_3D(so, STENCIL_ENABLE, 5);
340       SB_DATA    (so, 1);
341       SB_DATA    (so, nvgl_stencil_op(cso->stencil[0].fail_op));
342       SB_DATA    (so, nvgl_stencil_op(cso->stencil[0].zfail_op));
343       SB_DATA    (so, nvgl_stencil_op(cso->stencil[0].zpass_op));
344       SB_DATA    (so, nvgl_comparison_op(cso->stencil[0].func));
345       SB_BEGIN_3D(so, STENCIL_FRONT_MASK, 2);
346       SB_DATA    (so, cso->stencil[0].writemask);
347       SB_DATA    (so, cso->stencil[0].valuemask);
348    } else {
349       SB_BEGIN_3D(so, STENCIL_ENABLE, 1);
350       SB_DATA    (so, 0);
351    }
352
353    if (cso->stencil[1].enabled) {
354       assert(cso->stencil[0].enabled);
355       SB_BEGIN_3D(so, STENCIL_TWO_SIDE_ENABLE, 5);
356       SB_DATA    (so, 1);
357       SB_DATA    (so, nvgl_stencil_op(cso->stencil[1].fail_op));
358       SB_DATA    (so, nvgl_stencil_op(cso->stencil[1].zfail_op));
359       SB_DATA    (so, nvgl_stencil_op(cso->stencil[1].zpass_op));
360       SB_DATA    (so, nvgl_comparison_op(cso->stencil[1].func));
361       SB_BEGIN_3D(so, STENCIL_BACK_MASK, 2);
362       SB_DATA    (so, cso->stencil[1].writemask);
363       SB_DATA    (so, cso->stencil[1].valuemask);
364    } else {
365       SB_BEGIN_3D(so, STENCIL_TWO_SIDE_ENABLE, 1);
366       SB_DATA    (so, 0);
367    }
368     
369    SB_BEGIN_3D(so, ALPHA_TEST_ENABLE, 1);
370    if (cso->alpha.enabled) {
371       SB_DATA    (so, 1);
372       SB_BEGIN_3D(so, ALPHA_TEST_REF, 2);
373       SB_DATA    (so, fui(cso->alpha.ref_value));
374       SB_DATA    (so, nvgl_comparison_op(cso->alpha.func));
375    } else {
376       SB_DATA    (so, 0);
377    }
378
379    assert(so->size <= (sizeof(so->state) / sizeof(so->state[0])));
380    return (void *)so;
381 }
382
383 static void
384 nv50_zsa_state_bind(struct pipe_context *pipe, void *hwcso)
385 {
386    struct nv50_context *nv50 = nv50_context(pipe);
387
388    nv50->zsa = hwcso;
389    nv50->dirty |= NV50_NEW_ZSA;
390 }
391
392 static void
393 nv50_zsa_state_delete(struct pipe_context *pipe, void *hwcso)
394 {
395    FREE(hwcso);
396 }
397
398 /* ====================== SAMPLERS AND TEXTURES ================================
399  */
400
401 #define NV50_TSC_WRAP_CASE(n) \
402     case PIPE_TEX_WRAP_##n: return NV50_TSC_WRAP_##n
403
404 static INLINE unsigned
405 nv50_tsc_wrap_mode(unsigned wrap)
406 {
407    switch (wrap) {
408    NV50_TSC_WRAP_CASE(REPEAT);
409    NV50_TSC_WRAP_CASE(MIRROR_REPEAT);
410    NV50_TSC_WRAP_CASE(CLAMP_TO_EDGE);
411    NV50_TSC_WRAP_CASE(CLAMP_TO_BORDER);
412    NV50_TSC_WRAP_CASE(CLAMP);
413    NV50_TSC_WRAP_CASE(MIRROR_CLAMP_TO_EDGE);
414    NV50_TSC_WRAP_CASE(MIRROR_CLAMP_TO_BORDER);
415    NV50_TSC_WRAP_CASE(MIRROR_CLAMP);
416    default:
417        NOUVEAU_ERR("unknown wrap mode: %d\n", wrap);
418        return NV50_TSC_WRAP_REPEAT;
419    }
420 }
421
422 void *
423 nv50_sampler_state_create(struct pipe_context *pipe,
424                           const struct pipe_sampler_state *cso)
425 {
426    struct nv50_tsc_entry *so = CALLOC_STRUCT(nv50_tsc_entry);
427    float f[2];
428
429    so->id = -1;
430
431    so->tsc[0] = (0x00026000 |
432                  (nv50_tsc_wrap_mode(cso->wrap_s) << 0) |
433                  (nv50_tsc_wrap_mode(cso->wrap_t) << 3) |
434                  (nv50_tsc_wrap_mode(cso->wrap_r) << 6));
435
436    switch (cso->mag_img_filter) {
437    case PIPE_TEX_FILTER_LINEAR:
438       so->tsc[1] |= NV50_TSC_1_MAGF_LINEAR;
439       break;
440    case PIPE_TEX_FILTER_NEAREST:
441    default:
442       so->tsc[1] |= NV50_TSC_1_MAGF_NEAREST;
443       break;
444    }
445
446    switch (cso->min_img_filter) {
447    case PIPE_TEX_FILTER_LINEAR:
448       so->tsc[1] |= NV50_TSC_1_MINF_LINEAR;
449       break;
450    case PIPE_TEX_FILTER_NEAREST:
451    default:
452       so->tsc[1] |= NV50_TSC_1_MINF_NEAREST;
453       break;
454    }
455
456    switch (cso->min_mip_filter) {
457    case PIPE_TEX_MIPFILTER_LINEAR:
458       so->tsc[1] |= NV50_TSC_1_MIPF_LINEAR;
459       break;
460    case PIPE_TEX_MIPFILTER_NEAREST:
461       so->tsc[1] |= NV50_TSC_1_MIPF_NEAREST;
462       break;
463    case PIPE_TEX_MIPFILTER_NONE:
464    default:
465       so->tsc[1] |= NV50_TSC_1_MIPF_NONE;
466       break;
467    }
468
469    if (cso->max_anisotropy >= 16)
470       so->tsc[0] |= (7 << 20);
471    else
472    if (cso->max_anisotropy >= 12)
473       so->tsc[0] |= (6 << 20);
474    else {
475       so->tsc[0] |= (cso->max_anisotropy >> 1) << 20;
476
477       if (cso->max_anisotropy >= 4)
478          so->tsc[1] |= NV50_TSC_1_UNKN_ANISO_35;
479       else
480       if (cso->max_anisotropy >= 2)
481          so->tsc[1] |= NV50_TSC_1_UNKN_ANISO_15;
482    }
483
484    if (cso->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
485       /* NOTE: must be deactivated for non-shadow textures */
486       so->tsc[0] |= (1 << 9);
487       so->tsc[0] |= (nvgl_comparison_op(cso->compare_func) & 0x7) << 10;
488    }
489
490    f[0] = CLAMP(cso->lod_bias, -16.0f, 15.0f);
491    so->tsc[1] |= ((int)(f[0] * 256.0f) & 0x1fff) << 12;
492
493    f[0] = CLAMP(cso->min_lod, 0.0f, 15.0f);
494    f[1] = CLAMP(cso->max_lod, 0.0f, 15.0f);
495    so->tsc[2] |=
496       (((int)(f[1] * 256.0f) & 0xfff) << 12) | ((int)(f[0] * 256.0f) & 0xfff);
497
498    so->tsc[4] = fui(cso->border_color[0]);
499    so->tsc[5] = fui(cso->border_color[1]);
500    so->tsc[6] = fui(cso->border_color[2]);
501    so->tsc[7] = fui(cso->border_color[3]);
502
503    return (void *)so;
504 }
505
506 static void
507 nv50_sampler_state_delete(struct pipe_context *pipe, void *hwcso)
508 {
509    unsigned s, i;
510
511    for (s = 0; s < 3; ++s)
512       for (i = 0; i < nv50_context(pipe)->num_samplers[s]; ++i)
513          if (nv50_context(pipe)->samplers[s][i] == hwcso)
514             nv50_context(pipe)->samplers[s][i] = NULL;
515
516    nv50_screen_tsc_free(nv50_context(pipe)->screen, nv50_tsc_entry(hwcso));
517
518    FREE(hwcso);
519 }
520
521 static INLINE void
522 nv50_stage_sampler_states_bind(struct nv50_context *nv50, int s,
523                                unsigned nr, void **hwcso)
524 {
525    unsigned i;
526
527    for (i = 0; i < nr; ++i) {
528       struct nv50_tsc_entry *old = nv50->samplers[s][i];
529
530       nv50->samplers[s][i] = nv50_tsc_entry(hwcso[i]);
531       if (old)
532          nv50_screen_tsc_unlock(nv50->screen, old);
533    }
534    for (; i < nv50->num_samplers[s]; ++i)
535       if (nv50->samplers[s][i])
536          nv50_screen_tsc_unlock(nv50->screen, nv50->samplers[s][i]);
537
538    nv50->num_samplers[s] = nr;
539
540    nv50->dirty |= NV50_NEW_SAMPLERS;
541 }
542
543 static void
544 nv50_vp_sampler_states_bind(struct pipe_context *pipe, unsigned nr, void **s)
545 {
546    nv50_stage_sampler_states_bind(nv50_context(pipe), 0, nr, s);
547 }
548
549 static void
550 nv50_fp_sampler_states_bind(struct pipe_context *pipe, unsigned nr, void **s)
551 {
552    nv50_stage_sampler_states_bind(nv50_context(pipe), 2, nr, s);
553 }
554
555 static void
556 nv50_gp_sampler_states_bind(struct pipe_context *pipe, unsigned nr, void **s)
557 {
558    nv50_stage_sampler_states_bind(nv50_context(pipe), 1, nr, s);
559 }
560
561 /* NOTE: only called when not referenced anywhere, won't be bound */
562 static void
563 nv50_sampler_view_destroy(struct pipe_context *pipe,
564                           struct pipe_sampler_view *view)
565 {
566    pipe_resource_reference(&view->texture, NULL);
567
568    nv50_screen_tic_free(nv50_context(pipe)->screen, nv50_tic_entry(view));
569
570    FREE(nv50_tic_entry(view));
571 }
572
573 static INLINE void
574 nv50_stage_set_sampler_views(struct nv50_context *nv50, int s,
575                              unsigned nr,
576                              struct pipe_sampler_view **views)
577 {
578    unsigned i;
579
580    for (i = 0; i < nr; ++i) {
581       struct nv50_tic_entry *old = nv50_tic_entry(nv50->textures[s][i]);
582       if (old)
583          nv50_screen_tic_unlock(nv50->screen, old);
584
585       pipe_sampler_view_reference(&nv50->textures[s][i], views[i]);
586    }
587
588    for (i = nr; i < nv50->num_textures[s]; ++i) {
589       struct nv50_tic_entry *old = nv50_tic_entry(nv50->textures[s][i]);
590       if (!old)
591          continue;
592       nv50_screen_tic_unlock(nv50->screen, old);
593
594       pipe_sampler_view_reference(&nv50->textures[s][i], NULL);
595    }
596
597    nv50->num_textures[s] = nr;
598
599    nv50_bufctx_reset(nv50, NV50_BUFCTX_TEXTURES);
600
601    nv50->dirty |= NV50_NEW_TEXTURES;
602 }
603
604 static void
605 nv50_vp_set_sampler_views(struct pipe_context *pipe,
606                           unsigned nr,
607                           struct pipe_sampler_view **views)
608 {
609    nv50_stage_set_sampler_views(nv50_context(pipe), 0, nr, views);
610 }
611
612 static void
613 nv50_fp_set_sampler_views(struct pipe_context *pipe,
614                           unsigned nr,
615                           struct pipe_sampler_view **views)
616 {
617    nv50_stage_set_sampler_views(nv50_context(pipe), 2, nr, views);
618 }
619
620 static void
621 nv50_gp_set_sampler_views(struct pipe_context *pipe,
622                           unsigned nr,
623                           struct pipe_sampler_view **views)
624 {
625    nv50_stage_set_sampler_views(nv50_context(pipe), 1, nr, views);
626 }
627
628 /* ============================= SHADERS =======================================
629  */
630
631 static void *
632 nv50_sp_state_create(struct pipe_context *pipe,
633                      const struct pipe_shader_state *cso, unsigned type)
634 {
635    struct nv50_program *prog;
636
637    prog = CALLOC_STRUCT(nv50_program);
638    if (!prog)
639       return NULL;
640
641    prog->type = type;
642    prog->pipe.tokens = tgsi_dup_tokens(cso->tokens);
643
644    return (void *)prog;
645 }
646
647 static void
648 nv50_sp_state_delete(struct pipe_context *pipe, void *hwcso)
649 {
650    struct nv50_program *prog = (struct nv50_program *)hwcso;
651
652    nv50_program_destroy(nv50_context(pipe), prog);
653
654    FREE((void *)prog->pipe.tokens);
655    FREE(prog);
656 }
657
658 static void *
659 nv50_vp_state_create(struct pipe_context *pipe,
660                      const struct pipe_shader_state *cso)
661 {
662    return nv50_sp_state_create(pipe, cso, PIPE_SHADER_VERTEX);
663 }
664
665 static void
666 nv50_vp_state_bind(struct pipe_context *pipe, void *hwcso)
667 {
668     struct nv50_context *nv50 = nv50_context(pipe);
669
670     nv50->vertprog = hwcso;
671     nv50->dirty |= NV50_NEW_VERTPROG;
672 }
673
674 static void *
675 nv50_fp_state_create(struct pipe_context *pipe,
676                      const struct pipe_shader_state *cso)
677 {
678    return nv50_sp_state_create(pipe, cso, PIPE_SHADER_FRAGMENT);
679 }
680
681 static void
682 nv50_fp_state_bind(struct pipe_context *pipe, void *hwcso)
683 {
684     struct nv50_context *nv50 = nv50_context(pipe);
685
686     nv50->fragprog = hwcso;
687     nv50->dirty |= NV50_NEW_FRAGPROG;
688 }
689
690 static void *
691 nv50_gp_state_create(struct pipe_context *pipe,
692                      const struct pipe_shader_state *cso)
693 {
694    return nv50_sp_state_create(pipe, cso, PIPE_SHADER_GEOMETRY);
695 }
696
697 static void
698 nv50_gp_state_bind(struct pipe_context *pipe, void *hwcso)
699 {
700     struct nv50_context *nv50 = nv50_context(pipe);
701
702     nv50->gmtyprog = hwcso;
703     nv50->dirty |= NV50_NEW_GMTYPROG;
704 }
705
706 static void
707 nv50_set_constant_buffer(struct pipe_context *pipe, uint shader, uint index,
708                          struct pipe_resource *res)
709 {
710    struct nv50_context *nv50 = nv50_context(pipe);
711
712    if (nv50->constbuf[shader][index])
713       nv50_bufctx_del_resident(nv50, NV50_BUFCTX_CONSTANT,
714                                nv04_resource(nv50->constbuf[shader][index]));
715
716    pipe_resource_reference(&nv50->constbuf[shader][index], res);
717
718    nv50->constbuf_dirty[shader] |= 1 << index;
719
720    nv50->dirty |= NV50_NEW_CONSTBUF;
721 }
722
723 /* =============================================================================
724  */
725
726 static void
727 nv50_set_blend_color(struct pipe_context *pipe,
728                      const struct pipe_blend_color *bcol)
729 {
730    struct nv50_context *nv50 = nv50_context(pipe);
731
732    nv50->blend_colour = *bcol;
733    nv50->dirty |= NV50_NEW_BLEND_COLOUR;
734 }
735
736 static void
737 nv50_set_stencil_ref(struct pipe_context *pipe,
738                      const struct pipe_stencil_ref *sr)
739 {
740    struct nv50_context *nv50 = nv50_context(pipe);
741
742    nv50->stencil_ref = *sr;
743    nv50->dirty |= NV50_NEW_STENCIL_REF;
744 }
745
746 static void
747 nv50_set_clip_state(struct pipe_context *pipe,
748                     const struct pipe_clip_state *clip)
749 {
750    struct nv50_context *nv50 = nv50_context(pipe);
751    const unsigned size = clip->nr * sizeof(clip->ucp[0]);
752
753    memcpy(&nv50->clip.ucp[0][0], &clip->ucp[0][0], size);
754    nv50->clip.nr = clip->nr;
755
756    nv50->clip.depth_clamp = clip->depth_clamp;
757
758    nv50->dirty |= NV50_NEW_CLIP;
759 }
760
761 static void
762 nv50_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
763 {
764    struct nv50_context *nv50 = nv50_context(pipe);
765
766    nv50->sample_mask = sample_mask;
767    nv50->dirty |= NV50_NEW_SAMPLE_MASK;
768 }
769
770
771 static void
772 nv50_set_framebuffer_state(struct pipe_context *pipe,
773                            const struct pipe_framebuffer_state *fb)
774 {
775    struct nv50_context *nv50 = nv50_context(pipe);
776
777    nv50->framebuffer = *fb;
778    nv50->dirty |= NV50_NEW_FRAMEBUFFER;
779 }
780
781 static void
782 nv50_set_polygon_stipple(struct pipe_context *pipe,
783                          const struct pipe_poly_stipple *stipple)
784 {
785    struct nv50_context *nv50 = nv50_context(pipe);
786
787    nv50->stipple = *stipple;
788    nv50->dirty |= NV50_NEW_STIPPLE;
789 }
790
791 static void
792 nv50_set_scissor_state(struct pipe_context *pipe,
793                        const struct pipe_scissor_state *scissor)
794 {
795    struct nv50_context *nv50 = nv50_context(pipe);
796
797    nv50->scissor = *scissor;
798    nv50->dirty |= NV50_NEW_SCISSOR;
799 }
800
801 static void
802 nv50_set_viewport_state(struct pipe_context *pipe,
803                         const struct pipe_viewport_state *vpt)
804 {
805    struct nv50_context *nv50 = nv50_context(pipe);
806
807    nv50->viewport = *vpt;
808    nv50->dirty |= NV50_NEW_VIEWPORT;
809 }
810
811 static void
812 nv50_set_vertex_buffers(struct pipe_context *pipe,
813                         unsigned count,
814                         const struct pipe_vertex_buffer *vb)
815 {
816    struct nv50_context *nv50 = nv50_context(pipe);
817    unsigned i;
818
819    for (i = 0; i < count; ++i)
820       pipe_resource_reference(&nv50->vtxbuf[i].buffer, vb[i].buffer);
821    for (; i < nv50->num_vtxbufs; ++i)
822       pipe_resource_reference(&nv50->vtxbuf[i].buffer, NULL);
823
824    memcpy(nv50->vtxbuf, vb, sizeof(*vb) * count);
825    nv50->num_vtxbufs = count;
826
827    nv50_bufctx_reset(nv50, NV50_BUFCTX_VERTEX);
828
829    nv50->dirty |= NV50_NEW_ARRAYS;
830 }
831
832 static void
833 nv50_set_index_buffer(struct pipe_context *pipe,
834                       const struct pipe_index_buffer *ib)
835 {
836    struct nv50_context *nv50 = nv50_context(pipe);
837
838    if (ib) {
839       pipe_resource_reference(&nv50->idxbuf.buffer, ib->buffer);
840
841       memcpy(&nv50->idxbuf, ib, sizeof(nv50->idxbuf));
842    } else {
843       pipe_resource_reference(&nv50->idxbuf.buffer, NULL);
844    }
845 }
846
847 static void
848 nv50_vertex_state_bind(struct pipe_context *pipe, void *hwcso)
849 {
850    struct nv50_context *nv50 = nv50_context(pipe);
851
852    nv50->vertex = hwcso;
853    nv50->dirty |= NV50_NEW_VERTEX;
854 }
855
856 void
857 nv50_init_state_functions(struct nv50_context *nv50)
858 {
859    struct pipe_context *pipe = &nv50->base.pipe;
860
861    pipe->create_blend_state = nv50_blend_state_create;
862    pipe->bind_blend_state = nv50_blend_state_bind;
863    pipe->delete_blend_state = nv50_blend_state_delete;
864
865    pipe->create_rasterizer_state = nv50_rasterizer_state_create;
866    pipe->bind_rasterizer_state = nv50_rasterizer_state_bind;
867    pipe->delete_rasterizer_state = nv50_rasterizer_state_delete;
868
869    pipe->create_depth_stencil_alpha_state = nv50_zsa_state_create;
870    pipe->bind_depth_stencil_alpha_state = nv50_zsa_state_bind;
871    pipe->delete_depth_stencil_alpha_state = nv50_zsa_state_delete;
872
873    pipe->create_sampler_state = nv50_sampler_state_create;
874    pipe->delete_sampler_state = nv50_sampler_state_delete;
875    pipe->bind_vertex_sampler_states   = nv50_vp_sampler_states_bind;
876    pipe->bind_fragment_sampler_states = nv50_fp_sampler_states_bind;
877    pipe->bind_geometry_sampler_states = nv50_gp_sampler_states_bind;
878
879    pipe->create_sampler_view = nv50_create_sampler_view;
880    pipe->sampler_view_destroy = nv50_sampler_view_destroy;
881    pipe->set_vertex_sampler_views   = nv50_vp_set_sampler_views;
882    pipe->set_fragment_sampler_views = nv50_fp_set_sampler_views;
883    pipe->set_geometry_sampler_views = nv50_gp_set_sampler_views;
884  
885    pipe->create_vs_state = nv50_vp_state_create;
886    pipe->create_fs_state = nv50_fp_state_create;
887    pipe->create_gs_state = nv50_gp_state_create;
888    pipe->bind_vs_state = nv50_vp_state_bind;
889    pipe->bind_fs_state = nv50_fp_state_bind;
890    pipe->bind_gs_state = nv50_gp_state_bind;
891    pipe->delete_vs_state = nv50_sp_state_delete;
892    pipe->delete_fs_state = nv50_sp_state_delete;
893    pipe->delete_gs_state = nv50_sp_state_delete;
894
895    pipe->set_blend_color = nv50_set_blend_color;
896    pipe->set_stencil_ref = nv50_set_stencil_ref;
897    pipe->set_clip_state = nv50_set_clip_state;
898    pipe->set_sample_mask = nv50_set_sample_mask;
899    pipe->set_constant_buffer = nv50_set_constant_buffer;
900    pipe->set_framebuffer_state = nv50_set_framebuffer_state;
901    pipe->set_polygon_stipple = nv50_set_polygon_stipple;
902    pipe->set_scissor_state = nv50_set_scissor_state;
903    pipe->set_viewport_state = nv50_set_viewport_state;
904
905    pipe->create_vertex_elements_state = nv50_vertex_state_create;
906    pipe->delete_vertex_elements_state = nv50_vertex_state_delete;
907    pipe->bind_vertex_elements_state = nv50_vertex_state_bind;
908
909    pipe->set_vertex_buffers = nv50_set_vertex_buffers;
910    pipe->set_index_buffer = nv50_set_index_buffer;
911
912    pipe->redefine_user_buffer = u_default_redefine_user_buffer;
913 }
914