Merge branch '7.8'
[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 static INLINE void
242 nv50_sampler_state_bind(struct pipe_context *pipe, unsigned type,
243 unsigned nr, void **sampler)
244 {
245 struct nv50_context *nv50 = nv50_context(pipe);
246
247 memcpy(nv50->sampler[type], sampler, nr * sizeof(void *));
248
249 nv50->sampler_nr[type] = nr;
250 nv50->dirty |= NV50_NEW_SAMPLER;
251 }
252
253 static void
254 nv50_vp_sampler_state_bind(struct pipe_context *pipe, unsigned nr, void **s)
255 {
256 nv50_sampler_state_bind(pipe, PIPE_SHADER_VERTEX, nr, s);
257 }
258
259 static void
260 nv50_fp_sampler_state_bind(struct pipe_context *pipe, unsigned nr, void **s)
261 {
262 nv50_sampler_state_bind(pipe, PIPE_SHADER_FRAGMENT, nr, s);
263 }
264
265 static void
266 nv50_sampler_state_delete(struct pipe_context *pipe, void *hwcso)
267 {
268 FREE(hwcso);
269 }
270
271 static INLINE void
272 nv50_set_sampler_texture(struct pipe_context *pipe, unsigned type,
273 unsigned nr, struct pipe_texture **pt)
274 {
275 struct nv50_context *nv50 = nv50_context(pipe);
276 unsigned i;
277
278 for (i = 0; i < nr; i++)
279 pipe_texture_reference((void *)&nv50->miptree[type][i], pt[i]);
280 for (i = nr; i < nv50->miptree_nr[type]; i++)
281 pipe_texture_reference((void *)&nv50->miptree[type][i], NULL);
282
283 nv50->miptree_nr[type] = nr;
284 nv50->dirty |= NV50_NEW_TEXTURE;
285 }
286
287 static void
288 nv50_set_vp_sampler_textures(struct pipe_context *pipe,
289 unsigned nr, struct pipe_texture **pt)
290 {
291 nv50_set_sampler_texture(pipe, PIPE_SHADER_VERTEX, nr, pt);
292 }
293
294 static void
295 nv50_set_fp_sampler_textures(struct pipe_context *pipe,
296 unsigned nr, struct pipe_texture **pt)
297 {
298 nv50_set_sampler_texture(pipe, PIPE_SHADER_FRAGMENT, nr, pt);
299 }
300
301 static void *
302 nv50_rasterizer_state_create(struct pipe_context *pipe,
303 const struct pipe_rasterizer_state *cso)
304 {
305 struct nouveau_stateobj *so = so_new(16, 22, 0);
306 struct nouveau_grobj *tesla = nv50_context(pipe)->screen->tesla;
307 struct nv50_rasterizer_stateobj *rso =
308 CALLOC_STRUCT(nv50_rasterizer_stateobj);
309
310 /*XXX: ignored
311 * - light_twosize
312 * - point_smooth
313 * - multisample
314 * - point_sprite / sprite_coord_mode
315 */
316
317 so_method(so, tesla, NV50TCL_SCISSOR_ENABLE(0), 1);
318 so_data (so, cso->scissor);
319
320 so_method(so, tesla, NV50TCL_SHADE_MODEL, 1);
321 so_data (so, cso->flatshade ? NV50TCL_SHADE_MODEL_FLAT :
322 NV50TCL_SHADE_MODEL_SMOOTH);
323 so_method(so, tesla, NV50TCL_PROVOKING_VERTEX_LAST, 1);
324 so_data (so, cso->flatshade_first ? 0 : 1);
325
326 so_method(so, tesla, NV50TCL_VERTEX_TWO_SIDE_ENABLE, 1);
327 so_data (so, cso->light_twoside);
328
329 so_method(so, tesla, NV50TCL_LINE_WIDTH, 1);
330 so_data (so, fui(cso->line_width));
331 so_method(so, tesla, NV50TCL_LINE_SMOOTH_ENABLE, 1);
332 so_data (so, cso->line_smooth ? 1 : 0);
333 if (cso->line_stipple_enable) {
334 so_method(so, tesla, NV50TCL_LINE_STIPPLE_ENABLE, 1);
335 so_data (so, 1);
336 so_method(so, tesla, NV50TCL_LINE_STIPPLE_PATTERN, 1);
337 so_data (so, (cso->line_stipple_pattern << 8) |
338 cso->line_stipple_factor);
339 } else {
340 so_method(so, tesla, NV50TCL_LINE_STIPPLE_ENABLE, 1);
341 so_data (so, 0);
342 }
343
344 so_method(so, tesla, NV50TCL_POINT_SIZE, 1);
345 so_data (so, fui(cso->point_size));
346
347 so_method(so, tesla, NV50TCL_POINT_SPRITE_ENABLE, 1);
348 so_data (so, cso->point_quad_rasterization ? 1 : 0);
349
350 so_method(so, tesla, NV50TCL_POLYGON_MODE_FRONT, 3);
351 if (cso->front_winding == PIPE_WINDING_CCW) {
352 so_data(so, nvgl_polygon_mode(cso->fill_ccw));
353 so_data(so, nvgl_polygon_mode(cso->fill_cw));
354 } else {
355 so_data(so, nvgl_polygon_mode(cso->fill_cw));
356 so_data(so, nvgl_polygon_mode(cso->fill_ccw));
357 }
358 so_data(so, cso->poly_smooth ? 1 : 0);
359
360 so_method(so, tesla, NV50TCL_CULL_FACE_ENABLE, 3);
361 so_data (so, cso->cull_mode != PIPE_WINDING_NONE);
362 if (cso->front_winding == PIPE_WINDING_CCW) {
363 so_data(so, NV50TCL_FRONT_FACE_CCW);
364 switch (cso->cull_mode) {
365 case PIPE_WINDING_CCW:
366 so_data(so, NV50TCL_CULL_FACE_FRONT);
367 break;
368 case PIPE_WINDING_CW:
369 so_data(so, NV50TCL_CULL_FACE_BACK);
370 break;
371 case PIPE_WINDING_BOTH:
372 so_data(so, NV50TCL_CULL_FACE_FRONT_AND_BACK);
373 break;
374 default:
375 so_data(so, NV50TCL_CULL_FACE_BACK);
376 break;
377 }
378 } else {
379 so_data(so, NV50TCL_FRONT_FACE_CW);
380 switch (cso->cull_mode) {
381 case PIPE_WINDING_CCW:
382 so_data(so, NV50TCL_CULL_FACE_BACK);
383 break;
384 case PIPE_WINDING_CW:
385 so_data(so, NV50TCL_CULL_FACE_FRONT);
386 break;
387 case PIPE_WINDING_BOTH:
388 so_data(so, NV50TCL_CULL_FACE_FRONT_AND_BACK);
389 break;
390 default:
391 so_data(so, NV50TCL_CULL_FACE_BACK);
392 break;
393 }
394 }
395
396 so_method(so, tesla, NV50TCL_POLYGON_STIPPLE_ENABLE, 1);
397 so_data (so, cso->poly_stipple_enable ? 1 : 0);
398
399 so_method(so, tesla, NV50TCL_POLYGON_OFFSET_POINT_ENABLE, 3);
400 if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_POINT) ||
401 (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_POINT))
402 so_data(so, 1);
403 else
404 so_data(so, 0);
405 if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_LINE) ||
406 (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_LINE))
407 so_data(so, 1);
408 else
409 so_data(so, 0);
410 if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_FILL) ||
411 (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_FILL))
412 so_data(so, 1);
413 else
414 so_data(so, 0);
415
416 if (cso->offset_cw || cso->offset_ccw) {
417 so_method(so, tesla, NV50TCL_POLYGON_OFFSET_FACTOR, 1);
418 so_data (so, fui(cso->offset_scale));
419 so_method(so, tesla, NV50TCL_POLYGON_OFFSET_UNITS, 1);
420 so_data (so, fui(cso->offset_units * 2.0f));
421 }
422
423 rso->pipe = *cso;
424 so_ref(so, &rso->so);
425 so_ref(NULL, &so);
426 return (void *)rso;
427 }
428
429 static void
430 nv50_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)
431 {
432 struct nv50_context *nv50 = nv50_context(pipe);
433
434 nv50->rasterizer = hwcso;
435 nv50->dirty |= NV50_NEW_RASTERIZER;
436 }
437
438 static void
439 nv50_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
440 {
441 struct nv50_rasterizer_stateobj *rso = hwcso;
442
443 so_ref(NULL, &rso->so);
444 FREE(rso);
445 }
446
447 static void *
448 nv50_depth_stencil_alpha_state_create(struct pipe_context *pipe,
449 const struct pipe_depth_stencil_alpha_state *cso)
450 {
451 struct nouveau_grobj *tesla = nv50_context(pipe)->screen->tesla;
452 struct nv50_zsa_stateobj *zsa = CALLOC_STRUCT(nv50_zsa_stateobj);
453 struct nouveau_stateobj *so = so_new(9, 21, 0);
454
455 so_method(so, tesla, NV50TCL_DEPTH_WRITE_ENABLE, 1);
456 so_data (so, cso->depth.writemask ? 1 : 0);
457 if (cso->depth.enabled) {
458 so_method(so, tesla, NV50TCL_DEPTH_TEST_ENABLE, 1);
459 so_data (so, 1);
460 so_method(so, tesla, NV50TCL_DEPTH_TEST_FUNC, 1);
461 so_data (so, nvgl_comparison_op(cso->depth.func));
462 } else {
463 so_method(so, tesla, NV50TCL_DEPTH_TEST_ENABLE, 1);
464 so_data (so, 0);
465 }
466
467 if (cso->stencil[0].enabled) {
468 so_method(so, tesla, NV50TCL_STENCIL_FRONT_ENABLE, 5);
469 so_data (so, 1);
470 so_data (so, nvgl_stencil_op(cso->stencil[0].fail_op));
471 so_data (so, nvgl_stencil_op(cso->stencil[0].zfail_op));
472 so_data (so, nvgl_stencil_op(cso->stencil[0].zpass_op));
473 so_data (so, nvgl_comparison_op(cso->stencil[0].func));
474 so_method(so, tesla, NV50TCL_STENCIL_FRONT_MASK, 2);
475 so_data (so, cso->stencil[0].writemask);
476 so_data (so, cso->stencil[0].valuemask);
477 } else {
478 so_method(so, tesla, NV50TCL_STENCIL_FRONT_ENABLE, 1);
479 so_data (so, 0);
480 }
481
482 if (cso->stencil[1].enabled) {
483 so_method(so, tesla, NV50TCL_STENCIL_BACK_ENABLE, 5);
484 so_data (so, 1);
485 so_data (so, nvgl_stencil_op(cso->stencil[1].fail_op));
486 so_data (so, nvgl_stencil_op(cso->stencil[1].zfail_op));
487 so_data (so, nvgl_stencil_op(cso->stencil[1].zpass_op));
488 so_data (so, nvgl_comparison_op(cso->stencil[1].func));
489 so_method(so, tesla, NV50TCL_STENCIL_BACK_MASK, 2);
490 so_data (so, cso->stencil[1].writemask);
491 so_data (so, cso->stencil[1].valuemask);
492 } else {
493 so_method(so, tesla, NV50TCL_STENCIL_BACK_ENABLE, 1);
494 so_data (so, 0);
495 }
496
497 if (cso->alpha.enabled) {
498 so_method(so, tesla, NV50TCL_ALPHA_TEST_ENABLE, 1);
499 so_data (so, 1);
500 so_method(so, tesla, NV50TCL_ALPHA_TEST_REF, 2);
501 so_data (so, fui(cso->alpha.ref_value));
502 so_data (so, nvgl_comparison_op(cso->alpha.func));
503 } else {
504 so_method(so, tesla, NV50TCL_ALPHA_TEST_ENABLE, 1);
505 so_data (so, 0);
506 }
507
508 zsa->pipe = *cso;
509 so_ref(so, &zsa->so);
510 so_ref(NULL, &so);
511 return (void *)zsa;
512 }
513
514 static void
515 nv50_depth_stencil_alpha_state_bind(struct pipe_context *pipe, void *hwcso)
516 {
517 struct nv50_context *nv50 = nv50_context(pipe);
518
519 nv50->zsa = hwcso;
520 nv50->dirty |= NV50_NEW_ZSA;
521 }
522
523 static void
524 nv50_depth_stencil_alpha_state_delete(struct pipe_context *pipe, void *hwcso)
525 {
526 struct nv50_zsa_stateobj *zsa = hwcso;
527
528 so_ref(NULL, &zsa->so);
529 FREE(zsa);
530 }
531
532 static void *
533 nv50_vp_state_create(struct pipe_context *pipe,
534 const struct pipe_shader_state *cso)
535 {
536 struct nv50_program *p = CALLOC_STRUCT(nv50_program);
537
538 p->pipe.tokens = tgsi_dup_tokens(cso->tokens);
539 p->type = PIPE_SHADER_VERTEX;
540 tgsi_scan_shader(p->pipe.tokens, &p->info);
541 return (void *)p;
542 }
543
544 static void
545 nv50_vp_state_bind(struct pipe_context *pipe, void *hwcso)
546 {
547 struct nv50_context *nv50 = nv50_context(pipe);
548
549 nv50->vertprog = hwcso;
550 nv50->dirty |= NV50_NEW_VERTPROG;
551 }
552
553 static void
554 nv50_vp_state_delete(struct pipe_context *pipe, void *hwcso)
555 {
556 struct nv50_context *nv50 = nv50_context(pipe);
557 struct nv50_program *p = hwcso;
558
559 nv50_program_destroy(nv50, p);
560 FREE((void *)p->pipe.tokens);
561 FREE(p);
562 }
563
564 static void *
565 nv50_fp_state_create(struct pipe_context *pipe,
566 const struct pipe_shader_state *cso)
567 {
568 struct nv50_program *p = CALLOC_STRUCT(nv50_program);
569
570 p->pipe.tokens = tgsi_dup_tokens(cso->tokens);
571 p->type = PIPE_SHADER_FRAGMENT;
572 tgsi_scan_shader(p->pipe.tokens, &p->info);
573 return (void *)p;
574 }
575
576 static void
577 nv50_fp_state_bind(struct pipe_context *pipe, void *hwcso)
578 {
579 struct nv50_context *nv50 = nv50_context(pipe);
580
581 nv50->fragprog = hwcso;
582 nv50->dirty |= NV50_NEW_FRAGPROG;
583 }
584
585 static void
586 nv50_fp_state_delete(struct pipe_context *pipe, void *hwcso)
587 {
588 struct nv50_context *nv50 = nv50_context(pipe);
589 struct nv50_program *p = hwcso;
590
591 nv50_program_destroy(nv50, p);
592 FREE((void *)p->pipe.tokens);
593 FREE(p);
594 }
595
596 static void *
597 nv50_gp_state_create(struct pipe_context *pipe,
598 const struct pipe_shader_state *cso)
599 {
600 struct nv50_program *p = CALLOC_STRUCT(nv50_program);
601
602 p->pipe.tokens = tgsi_dup_tokens(cso->tokens);
603 p->type = PIPE_SHADER_GEOMETRY;
604 tgsi_scan_shader(p->pipe.tokens, &p->info);
605 return (void *)p;
606 }
607
608 static void
609 nv50_gp_state_bind(struct pipe_context *pipe, void *hwcso)
610 {
611 struct nv50_context *nv50 = nv50_context(pipe);
612
613 nv50->fragprog = hwcso;
614 nv50->dirty |= NV50_NEW_GEOMPROG;
615 }
616
617 static void
618 nv50_gp_state_delete(struct pipe_context *pipe, void *hwcso)
619 {
620 struct nv50_context *nv50 = nv50_context(pipe);
621 struct nv50_program *p = hwcso;
622
623 nv50_program_destroy(nv50, p);
624 FREE((void *)p->pipe.tokens);
625 FREE(p);
626 }
627
628 static void
629 nv50_set_blend_color(struct pipe_context *pipe,
630 const struct pipe_blend_color *bcol)
631 {
632 struct nv50_context *nv50 = nv50_context(pipe);
633
634 nv50->blend_colour = *bcol;
635 nv50->dirty |= NV50_NEW_BLEND_COLOUR;
636 }
637
638 static void
639 nv50_set_stencil_ref(struct pipe_context *pipe,
640 const struct pipe_stencil_ref *sr)
641 {
642 struct nv50_context *nv50 = nv50_context(pipe);
643
644 nv50->stencil_ref = *sr;
645 nv50->dirty |= NV50_NEW_STENCIL_REF;
646 }
647
648 static void
649 nv50_set_clip_state(struct pipe_context *pipe,
650 const struct pipe_clip_state *clip)
651 {
652 }
653
654 static void
655 nv50_set_constant_buffer(struct pipe_context *pipe, uint shader, uint index,
656 struct pipe_buffer *buf )
657 {
658 struct nv50_context *nv50 = nv50_context(pipe);
659
660 if (shader == PIPE_SHADER_VERTEX) {
661 nv50->constbuf[PIPE_SHADER_VERTEX] = buf;
662 nv50->dirty |= NV50_NEW_VERTPROG_CB;
663 } else
664 if (shader == PIPE_SHADER_FRAGMENT) {
665 nv50->constbuf[PIPE_SHADER_FRAGMENT] = buf;
666 nv50->dirty |= NV50_NEW_FRAGPROG_CB;
667 } else
668 if (shader == PIPE_SHADER_GEOMETRY) {
669 nv50->constbuf[PIPE_SHADER_GEOMETRY] = buf;
670 nv50->dirty |= NV50_NEW_GEOMPROG_CB;
671 }
672 }
673
674 static void
675 nv50_set_framebuffer_state(struct pipe_context *pipe,
676 const struct pipe_framebuffer_state *fb)
677 {
678 struct nv50_context *nv50 = nv50_context(pipe);
679
680 nv50->framebuffer = *fb;
681 nv50->dirty |= NV50_NEW_FRAMEBUFFER;
682 }
683
684 static void
685 nv50_set_polygon_stipple(struct pipe_context *pipe,
686 const struct pipe_poly_stipple *stipple)
687 {
688 struct nv50_context *nv50 = nv50_context(pipe);
689
690 nv50->stipple = *stipple;
691 nv50->dirty |= NV50_NEW_STIPPLE;
692 }
693
694 static void
695 nv50_set_scissor_state(struct pipe_context *pipe,
696 const struct pipe_scissor_state *s)
697 {
698 struct nv50_context *nv50 = nv50_context(pipe);
699
700 nv50->scissor = *s;
701 nv50->dirty |= NV50_NEW_SCISSOR;
702 }
703
704 static void
705 nv50_set_viewport_state(struct pipe_context *pipe,
706 const struct pipe_viewport_state *vpt)
707 {
708 struct nv50_context *nv50 = nv50_context(pipe);
709
710 nv50->viewport = *vpt;
711 nv50->dirty |= NV50_NEW_VIEWPORT;
712 }
713
714 static void
715 nv50_set_vertex_buffers(struct pipe_context *pipe, unsigned count,
716 const struct pipe_vertex_buffer *vb)
717 {
718 struct nv50_context *nv50 = nv50_context(pipe);
719
720 memcpy(nv50->vtxbuf, vb, sizeof(*vb) * count);
721 nv50->vtxbuf_nr = count;
722
723 nv50->dirty |= NV50_NEW_ARRAYS;
724 }
725
726 static void *
727 nv50_vtxelts_state_create(struct pipe_context *pipe,
728 unsigned num_elements,
729 const struct pipe_vertex_element *elements)
730 {
731 struct nv50_vtxelt_stateobj *cso = CALLOC_STRUCT(nv50_vtxelt_stateobj);
732
733 assert(num_elements < 16); /* not doing fallbacks yet */
734 cso->num_elements = num_elements;
735 memcpy(cso->pipe, elements, num_elements * sizeof(*elements));
736
737 nv50_vtxelt_construct(cso);
738
739 return (void *)cso;
740 }
741
742 static void
743 nv50_vtxelts_state_delete(struct pipe_context *pipe, void *hwcso)
744 {
745 FREE(hwcso);
746 }
747
748 static void
749 nv50_vtxelts_state_bind(struct pipe_context *pipe, void *hwcso)
750 {
751 struct nv50_context *nv50 = nv50_context(pipe);
752
753 nv50->vtxelt = hwcso;
754 nv50->dirty |= NV50_NEW_ARRAYS;
755 }
756
757 void
758 nv50_init_state_functions(struct nv50_context *nv50)
759 {
760 nv50->pipe.create_blend_state = nv50_blend_state_create;
761 nv50->pipe.bind_blend_state = nv50_blend_state_bind;
762 nv50->pipe.delete_blend_state = nv50_blend_state_delete;
763
764 nv50->pipe.create_sampler_state = nv50_sampler_state_create;
765 nv50->pipe.delete_sampler_state = nv50_sampler_state_delete;
766 nv50->pipe.bind_fragment_sampler_states = nv50_fp_sampler_state_bind;
767 nv50->pipe.bind_vertex_sampler_states = nv50_vp_sampler_state_bind;
768 nv50->pipe.set_fragment_sampler_textures = nv50_set_fp_sampler_textures;
769 nv50->pipe.set_vertex_sampler_textures = nv50_set_vp_sampler_textures;
770
771 nv50->pipe.create_rasterizer_state = nv50_rasterizer_state_create;
772 nv50->pipe.bind_rasterizer_state = nv50_rasterizer_state_bind;
773 nv50->pipe.delete_rasterizer_state = nv50_rasterizer_state_delete;
774
775 nv50->pipe.create_depth_stencil_alpha_state =
776 nv50_depth_stencil_alpha_state_create;
777 nv50->pipe.bind_depth_stencil_alpha_state =
778 nv50_depth_stencil_alpha_state_bind;
779 nv50->pipe.delete_depth_stencil_alpha_state =
780 nv50_depth_stencil_alpha_state_delete;
781
782 nv50->pipe.create_vs_state = nv50_vp_state_create;
783 nv50->pipe.bind_vs_state = nv50_vp_state_bind;
784 nv50->pipe.delete_vs_state = nv50_vp_state_delete;
785
786 nv50->pipe.create_fs_state = nv50_fp_state_create;
787 nv50->pipe.bind_fs_state = nv50_fp_state_bind;
788 nv50->pipe.delete_fs_state = nv50_fp_state_delete;
789
790 nv50->pipe.create_gs_state = nv50_gp_state_create;
791 nv50->pipe.bind_gs_state = nv50_gp_state_bind;
792 nv50->pipe.delete_gs_state = nv50_gp_state_delete;
793
794 nv50->pipe.set_blend_color = nv50_set_blend_color;
795 nv50->pipe.set_stencil_ref = nv50_set_stencil_ref;
796 nv50->pipe.set_clip_state = nv50_set_clip_state;
797 nv50->pipe.set_constant_buffer = nv50_set_constant_buffer;
798 nv50->pipe.set_framebuffer_state = nv50_set_framebuffer_state;
799 nv50->pipe.set_polygon_stipple = nv50_set_polygon_stipple;
800 nv50->pipe.set_scissor_state = nv50_set_scissor_state;
801 nv50->pipe.set_viewport_state = nv50_set_viewport_state;
802
803 nv50->pipe.create_vertex_elements_state = nv50_vtxelts_state_create;
804 nv50->pipe.delete_vertex_elements_state = nv50_vtxelts_state_delete;
805 nv50->pipe.bind_vertex_elements_state = nv50_vtxelts_state_bind;
806
807 nv50->pipe.set_vertex_buffers = nv50_set_vertex_buffers;
808 }
809