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