1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "util/u_inlines.h"
5 #include "draw/draw_context.h"
7 #include "tgsi/tgsi_parse.h"
9 #include "nvfx_context.h"
10 #include "nvfx_state.h"
14 nvfx_blend_state_create(struct pipe_context *pipe,
15 const struct pipe_blend_state *cso)
17 struct nvfx_context *nvfx = nvfx_context(pipe);
18 struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
19 struct nvfx_blend_state *bso = CALLOC(1, sizeof(*bso));
20 struct nouveau_stateobj *so = so_new(5, 8, 0);
22 if (cso->rt[0].blend_enable) {
23 so_method(so, eng3d, NV34TCL_BLEND_FUNC_ENABLE, 3);
25 so_data (so, (nvgl_blend_func(cso->rt[0].alpha_src_factor) << 16) |
26 nvgl_blend_func(cso->rt[0].rgb_src_factor));
27 so_data (so, nvgl_blend_func(cso->rt[0].alpha_dst_factor) << 16 |
28 nvgl_blend_func(cso->rt[0].rgb_dst_factor));
29 if(nvfx->screen->base.device->chipset < 0x40) {
30 so_method(so, eng3d, NV34TCL_BLEND_EQUATION, 1);
31 so_data (so, nvgl_blend_eqn(cso->rt[0].rgb_func));
33 so_method(so, eng3d, NV40TCL_BLEND_EQUATION, 1);
34 so_data (so, nvgl_blend_eqn(cso->rt[0].alpha_func) << 16 |
35 nvgl_blend_eqn(cso->rt[0].rgb_func));
38 so_method(so, eng3d, NV34TCL_BLEND_FUNC_ENABLE, 1);
42 so_method(so, eng3d, NV34TCL_COLOR_MASK, 1);
43 so_data (so, (((cso->rt[0].colormask & PIPE_MASK_A) ? (0x01 << 24) : 0) |
44 ((cso->rt[0].colormask & PIPE_MASK_R) ? (0x01 << 16) : 0) |
45 ((cso->rt[0].colormask & PIPE_MASK_G) ? (0x01 << 8) : 0) |
46 ((cso->rt[0].colormask & PIPE_MASK_B) ? (0x01 << 0) : 0)));
48 /* TODO: add NV40 MRT color mask */
50 if (cso->logicop_enable) {
51 so_method(so, eng3d, NV34TCL_COLOR_LOGIC_OP_ENABLE, 2);
53 so_data (so, nvgl_logicop_func(cso->logicop_func));
55 so_method(so, eng3d, NV34TCL_COLOR_LOGIC_OP_ENABLE, 1);
59 so_method(so, eng3d, NV34TCL_DITHER_ENABLE, 1);
60 so_data (so, cso->dither ? 1 : 0);
69 nvfx_blend_state_bind(struct pipe_context *pipe, void *hwcso)
71 struct nvfx_context *nvfx = nvfx_context(pipe);
74 nvfx->dirty |= NVFX_NEW_BLEND;
78 nvfx_blend_state_delete(struct pipe_context *pipe, void *hwcso)
80 struct nvfx_blend_state *bso = hwcso;
82 so_ref(NULL, &bso->so);
87 nvfx_sampler_state_create(struct pipe_context *pipe,
88 const struct pipe_sampler_state *cso)
90 struct nvfx_context *nvfx = nvfx_context(pipe);
91 struct nvfx_sampler_state *ps;
93 ps = MALLOC(sizeof(struct nvfx_sampler_state));
95 /* on nv30, we use this as an internal flag */
96 ps->fmt = cso->normalized_coords ? 0 : NV40TCL_TEX_FORMAT_RECT;
98 ps->filt = nvfx_tex_filter(cso);
99 ps->wrap = (nvfx_tex_wrap_mode(cso->wrap_s) << NV34TCL_TX_WRAP_S_SHIFT) |
100 (nvfx_tex_wrap_mode(cso->wrap_t) << NV34TCL_TX_WRAP_T_SHIFT) |
101 (nvfx_tex_wrap_mode(cso->wrap_r) << NV34TCL_TX_WRAP_R_SHIFT) |
102 nvfx_tex_wrap_compare_mode(cso);
103 ps->bcol = nvfx_tex_border_color(cso->border_color);
106 nv40_sampler_state_init(pipe, ps, cso);
108 nv30_sampler_state_init(pipe, ps, cso);
114 nvfx_sampler_state_bind(struct pipe_context *pipe, unsigned nr, void **sampler)
116 struct nvfx_context *nvfx = nvfx_context(pipe);
119 for (unit = 0; unit < nr; unit++) {
120 nvfx->tex_sampler[unit] = sampler[unit];
121 nvfx->dirty_samplers |= (1 << unit);
124 for (unit = nr; unit < nvfx->nr_samplers; unit++) {
125 nvfx->tex_sampler[unit] = NULL;
126 nvfx->dirty_samplers |= (1 << unit);
129 nvfx->nr_samplers = nr;
130 nvfx->dirty |= NVFX_NEW_SAMPLER;
134 nvfx_sampler_state_delete(struct pipe_context *pipe, void *hwcso)
140 nvfx_set_fragment_sampler_views(struct pipe_context *pipe,
142 struct pipe_sampler_view **views)
144 struct nvfx_context *nvfx = nvfx_context(pipe);
147 for (unit = 0; unit < nr; unit++) {
148 pipe_sampler_view_reference(&nvfx->fragment_sampler_views[unit],
150 nvfx->dirty_samplers |= (1 << unit);
153 for (unit = nr; unit < nvfx->nr_textures; unit++) {
154 pipe_sampler_view_reference(&nvfx->fragment_sampler_views[unit],
156 nvfx->dirty_samplers |= (1 << unit);
159 nvfx->nr_textures = nr;
160 nvfx->dirty |= NVFX_NEW_SAMPLER;
164 static struct pipe_sampler_view *
165 nvfx_create_sampler_view(struct pipe_context *pipe,
166 struct pipe_resource *texture,
167 const struct pipe_sampler_view *templ)
169 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
173 view->reference.count = 1;
174 view->texture = NULL;
175 pipe_resource_reference(&view->texture, texture);
176 view->context = pipe;
184 nvfx_sampler_view_destroy(struct pipe_context *pipe,
185 struct pipe_sampler_view *view)
187 pipe_resource_reference(&view->texture, NULL);
192 nvfx_rasterizer_state_create(struct pipe_context *pipe,
193 const struct pipe_rasterizer_state *cso)
195 struct nvfx_context *nvfx = nvfx_context(pipe);
196 struct nvfx_rasterizer_state *rsso = CALLOC(1, sizeof(*rsso));
197 struct nouveau_stateobj *so = so_new(9, 19, 0);
198 struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
206 so_method(so, eng3d, NV34TCL_SHADE_MODEL, 1);
207 so_data (so, cso->flatshade ? NV34TCL_SHADE_MODEL_FLAT :
208 NV34TCL_SHADE_MODEL_SMOOTH);
210 so_method(so, eng3d, NV34TCL_LINE_WIDTH, 2);
211 so_data (so, (unsigned char)(cso->line_width * 8.0) & 0xff);
212 so_data (so, cso->line_smooth ? 1 : 0);
213 so_method(so, eng3d, NV34TCL_LINE_STIPPLE_ENABLE, 2);
214 so_data (so, cso->line_stipple_enable ? 1 : 0);
215 so_data (so, (cso->line_stipple_pattern << 16) |
216 cso->line_stipple_factor);
218 so_method(so, eng3d, NV34TCL_POINT_SIZE, 1);
219 so_data (so, fui(cso->point_size));
221 so_method(so, eng3d, NV34TCL_POLYGON_MODE_FRONT, 6);
222 if (cso->front_winding == PIPE_WINDING_CCW) {
223 so_data(so, nvgl_polygon_mode(cso->fill_ccw));
224 so_data(so, nvgl_polygon_mode(cso->fill_cw));
225 switch (cso->cull_mode) {
226 case PIPE_WINDING_CCW:
227 so_data(so, NV34TCL_CULL_FACE_FRONT);
229 case PIPE_WINDING_CW:
230 so_data(so, NV34TCL_CULL_FACE_BACK);
232 case PIPE_WINDING_BOTH:
233 so_data(so, NV34TCL_CULL_FACE_FRONT_AND_BACK);
236 so_data(so, NV34TCL_CULL_FACE_BACK);
239 so_data(so, NV34TCL_FRONT_FACE_CCW);
241 so_data(so, nvgl_polygon_mode(cso->fill_cw));
242 so_data(so, nvgl_polygon_mode(cso->fill_ccw));
243 switch (cso->cull_mode) {
244 case PIPE_WINDING_CCW:
245 so_data(so, NV34TCL_CULL_FACE_BACK);
247 case PIPE_WINDING_CW:
248 so_data(so, NV34TCL_CULL_FACE_FRONT);
250 case PIPE_WINDING_BOTH:
251 so_data(so, NV34TCL_CULL_FACE_FRONT_AND_BACK);
254 so_data(so, NV34TCL_CULL_FACE_BACK);
257 so_data(so, NV34TCL_FRONT_FACE_CW);
259 so_data(so, cso->poly_smooth ? 1 : 0);
260 so_data(so, (cso->cull_mode != PIPE_WINDING_NONE) ? 1 : 0);
262 so_method(so, eng3d, NV34TCL_POLYGON_STIPPLE_ENABLE, 1);
263 so_data (so, cso->poly_stipple_enable ? 1 : 0);
265 so_method(so, eng3d, NV34TCL_POLYGON_OFFSET_POINT_ENABLE, 3);
266 if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_POINT) ||
267 (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_POINT))
271 if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_LINE) ||
272 (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_LINE))
276 if ((cso->offset_cw && cso->fill_cw == PIPE_POLYGON_MODE_FILL) ||
277 (cso->offset_ccw && cso->fill_ccw == PIPE_POLYGON_MODE_FILL))
281 if (cso->offset_cw || cso->offset_ccw) {
282 so_method(so, eng3d, NV34TCL_POLYGON_OFFSET_FACTOR, 2);
283 so_data (so, fui(cso->offset_scale));
284 so_data (so, fui(cso->offset_units * 2));
287 so_method(so, eng3d, NV34TCL_POINT_SPRITE, 1);
288 if (cso->point_quad_rasterization) {
289 unsigned psctl = (1 << 0), i;
291 for (i = 0; i < 8; i++) {
292 if ((cso->sprite_coord_enable >> i) & 1)
293 psctl |= (1 << (8 + i));
301 so_ref(so, &rsso->so);
308 nvfx_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)
310 struct nvfx_context *nvfx = nvfx_context(pipe);
312 nvfx->rasterizer = hwcso;
313 nvfx->dirty |= NVFX_NEW_RAST;
314 nvfx->draw_dirty |= NVFX_NEW_RAST;
318 nvfx_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
320 struct nvfx_rasterizer_state *rsso = hwcso;
322 so_ref(NULL, &rsso->so);
327 nvfx_depth_stencil_alpha_state_create(struct pipe_context *pipe,
328 const struct pipe_depth_stencil_alpha_state *cso)
330 struct nvfx_context *nvfx = nvfx_context(pipe);
331 struct nvfx_zsa_state *zsaso = CALLOC(1, sizeof(*zsaso));
332 struct nouveau_stateobj *so = so_new(6, 20, 0);
333 struct nouveau_grobj *eng3d = nvfx->screen->eng3d;
335 so_method(so, eng3d, NV34TCL_DEPTH_FUNC, 3);
336 so_data (so, nvgl_comparison_op(cso->depth.func));
337 so_data (so, cso->depth.writemask ? 1 : 0);
338 so_data (so, cso->depth.enabled ? 1 : 0);
340 so_method(so, eng3d, NV34TCL_ALPHA_FUNC_ENABLE, 3);
341 so_data (so, cso->alpha.enabled ? 1 : 0);
342 so_data (so, nvgl_comparison_op(cso->alpha.func));
343 so_data (so, float_to_ubyte(cso->alpha.ref_value));
345 if (cso->stencil[0].enabled) {
346 so_method(so, eng3d, NV34TCL_STENCIL_FRONT_ENABLE, 3);
347 so_data (so, cso->stencil[0].enabled ? 1 : 0);
348 so_data (so, cso->stencil[0].writemask);
349 so_data (so, nvgl_comparison_op(cso->stencil[0].func));
350 so_method(so, eng3d, NV34TCL_STENCIL_FRONT_FUNC_MASK, 4);
351 so_data (so, cso->stencil[0].valuemask);
352 so_data (so, nvgl_stencil_op(cso->stencil[0].fail_op));
353 so_data (so, nvgl_stencil_op(cso->stencil[0].zfail_op));
354 so_data (so, nvgl_stencil_op(cso->stencil[0].zpass_op));
356 so_method(so, eng3d, NV34TCL_STENCIL_FRONT_ENABLE, 1);
360 if (cso->stencil[1].enabled) {
361 so_method(so, eng3d, NV34TCL_STENCIL_BACK_ENABLE, 3);
362 so_data (so, cso->stencil[1].enabled ? 1 : 0);
363 so_data (so, cso->stencil[1].writemask);
364 so_data (so, nvgl_comparison_op(cso->stencil[1].func));
365 so_method(so, eng3d, NV34TCL_STENCIL_BACK_FUNC_MASK, 4);
366 so_data (so, cso->stencil[1].valuemask);
367 so_data (so, nvgl_stencil_op(cso->stencil[1].fail_op));
368 so_data (so, nvgl_stencil_op(cso->stencil[1].zfail_op));
369 so_data (so, nvgl_stencil_op(cso->stencil[1].zpass_op));
371 so_method(so, eng3d, NV34TCL_STENCIL_BACK_ENABLE, 1);
375 so_ref(so, &zsaso->so);
378 return (void *)zsaso;
382 nvfx_depth_stencil_alpha_state_bind(struct pipe_context *pipe, void *hwcso)
384 struct nvfx_context *nvfx = nvfx_context(pipe);
387 nvfx->dirty |= NVFX_NEW_ZSA;
391 nvfx_depth_stencil_alpha_state_delete(struct pipe_context *pipe, void *hwcso)
393 struct nvfx_zsa_state *zsaso = hwcso;
395 so_ref(NULL, &zsaso->so);
400 nvfx_vp_state_create(struct pipe_context *pipe,
401 const struct pipe_shader_state *cso)
403 struct nvfx_context *nvfx = nvfx_context(pipe);
404 struct nvfx_vertex_program *vp;
406 vp = CALLOC(1, sizeof(struct nvfx_vertex_program));
407 vp->pipe.tokens = tgsi_dup_tokens(cso->tokens);
408 vp->draw = draw_create_vertex_shader(nvfx->draw, &vp->pipe);
414 nvfx_vp_state_bind(struct pipe_context *pipe, void *hwcso)
416 struct nvfx_context *nvfx = nvfx_context(pipe);
418 nvfx->vertprog = hwcso;
419 nvfx->dirty |= NVFX_NEW_VERTPROG;
420 nvfx->draw_dirty |= NVFX_NEW_VERTPROG;
424 nvfx_vp_state_delete(struct pipe_context *pipe, void *hwcso)
426 struct nvfx_context *nvfx = nvfx_context(pipe);
427 struct nvfx_vertex_program *vp = hwcso;
429 draw_delete_vertex_shader(nvfx->draw, vp->draw);
430 nvfx_vertprog_destroy(nvfx, vp);
431 FREE((void*)vp->pipe.tokens);
436 nvfx_fp_state_create(struct pipe_context *pipe,
437 const struct pipe_shader_state *cso)
439 struct nvfx_fragment_program *fp;
441 fp = CALLOC(1, sizeof(struct nvfx_fragment_program));
442 fp->pipe.tokens = tgsi_dup_tokens(cso->tokens);
444 tgsi_scan_shader(fp->pipe.tokens, &fp->info);
450 nvfx_fp_state_bind(struct pipe_context *pipe, void *hwcso)
452 struct nvfx_context *nvfx = nvfx_context(pipe);
454 nvfx->fragprog = hwcso;
455 nvfx->dirty |= NVFX_NEW_FRAGPROG;
459 nvfx_fp_state_delete(struct pipe_context *pipe, void *hwcso)
461 struct nvfx_context *nvfx = nvfx_context(pipe);
462 struct nvfx_fragment_program *fp = hwcso;
464 nvfx_fragprog_destroy(nvfx, fp);
465 FREE((void*)fp->pipe.tokens);
470 nvfx_set_blend_color(struct pipe_context *pipe,
471 const struct pipe_blend_color *bcol)
473 struct nvfx_context *nvfx = nvfx_context(pipe);
475 nvfx->blend_colour = *bcol;
476 nvfx->dirty |= NVFX_NEW_BCOL;
480 nvfx_set_stencil_ref(struct pipe_context *pipe,
481 const struct pipe_stencil_ref *sr)
483 struct nvfx_context *nvfx = nvfx_context(pipe);
485 nvfx->stencil_ref = *sr;
486 nvfx->dirty |= NVFX_NEW_SR;
490 nvfx_set_clip_state(struct pipe_context *pipe,
491 const struct pipe_clip_state *clip)
493 struct nvfx_context *nvfx = nvfx_context(pipe);
496 nvfx->dirty |= NVFX_NEW_UCP;
497 nvfx->draw_dirty |= NVFX_NEW_UCP;
501 nvfx_set_constant_buffer(struct pipe_context *pipe, uint shader, uint index,
502 struct pipe_resource *buf )
504 struct nvfx_context *nvfx = nvfx_context(pipe);
506 nvfx->constbuf[shader] = buf;
507 nvfx->constbuf_nr[shader] = buf->width0 / (4 * sizeof(float));
509 if (shader == PIPE_SHADER_VERTEX) {
510 nvfx->dirty |= NVFX_NEW_VERTPROG;
512 if (shader == PIPE_SHADER_FRAGMENT) {
513 nvfx->dirty |= NVFX_NEW_FRAGPROG;
518 nvfx_set_framebuffer_state(struct pipe_context *pipe,
519 const struct pipe_framebuffer_state *fb)
521 struct nvfx_context *nvfx = nvfx_context(pipe);
523 nvfx->framebuffer = *fb;
524 nvfx->dirty |= NVFX_NEW_FB;
528 nvfx_set_polygon_stipple(struct pipe_context *pipe,
529 const struct pipe_poly_stipple *stipple)
531 struct nvfx_context *nvfx = nvfx_context(pipe);
533 memcpy(nvfx->stipple, stipple->stipple, 4 * 32);
534 nvfx->dirty |= NVFX_NEW_STIPPLE;
538 nvfx_set_scissor_state(struct pipe_context *pipe,
539 const struct pipe_scissor_state *s)
541 struct nvfx_context *nvfx = nvfx_context(pipe);
544 nvfx->dirty |= NVFX_NEW_SCISSOR;
548 nvfx_set_viewport_state(struct pipe_context *pipe,
549 const struct pipe_viewport_state *vpt)
551 struct nvfx_context *nvfx = nvfx_context(pipe);
553 nvfx->viewport = *vpt;
554 nvfx->dirty |= NVFX_NEW_VIEWPORT;
555 nvfx->draw_dirty |= NVFX_NEW_VIEWPORT;
559 nvfx_set_vertex_buffers(struct pipe_context *pipe, unsigned count,
560 const struct pipe_vertex_buffer *vb)
562 struct nvfx_context *nvfx = nvfx_context(pipe);
564 memcpy(nvfx->vtxbuf, vb, sizeof(*vb) * count);
565 nvfx->vtxbuf_nr = count;
567 nvfx->dirty |= NVFX_NEW_ARRAYS;
568 nvfx->draw_dirty |= NVFX_NEW_ARRAYS;
572 nvfx_vtxelts_state_create(struct pipe_context *pipe,
573 unsigned num_elements,
574 const struct pipe_vertex_element *elements)
576 struct nvfx_vtxelt_state *cso = CALLOC_STRUCT(nvfx_vtxelt_state);
578 assert(num_elements < 16); /* not doing fallbacks yet */
579 cso->num_elements = num_elements;
580 memcpy(cso->pipe, elements, num_elements * sizeof(*elements));
582 /* nvfx_vtxelt_construct(cso);*/
588 nvfx_vtxelts_state_delete(struct pipe_context *pipe, void *hwcso)
594 nvfx_vtxelts_state_bind(struct pipe_context *pipe, void *hwcso)
596 struct nvfx_context *nvfx = nvfx_context(pipe);
598 nvfx->vtxelt = hwcso;
599 nvfx->dirty |= NVFX_NEW_ARRAYS;
600 /*nvfx->draw_dirty |= NVFX_NEW_ARRAYS;*/
604 nvfx_init_state_functions(struct nvfx_context *nvfx)
606 nvfx->pipe.create_blend_state = nvfx_blend_state_create;
607 nvfx->pipe.bind_blend_state = nvfx_blend_state_bind;
608 nvfx->pipe.delete_blend_state = nvfx_blend_state_delete;
610 nvfx->pipe.create_sampler_state = nvfx_sampler_state_create;
611 nvfx->pipe.bind_fragment_sampler_states = nvfx_sampler_state_bind;
612 nvfx->pipe.delete_sampler_state = nvfx_sampler_state_delete;
613 nvfx->pipe.set_fragment_sampler_views = nvfx_set_fragment_sampler_views;
614 nvfx->pipe.create_sampler_view = nvfx_create_sampler_view;
615 nvfx->pipe.sampler_view_destroy = nvfx_sampler_view_destroy;
617 nvfx->pipe.create_rasterizer_state = nvfx_rasterizer_state_create;
618 nvfx->pipe.bind_rasterizer_state = nvfx_rasterizer_state_bind;
619 nvfx->pipe.delete_rasterizer_state = nvfx_rasterizer_state_delete;
621 nvfx->pipe.create_depth_stencil_alpha_state =
622 nvfx_depth_stencil_alpha_state_create;
623 nvfx->pipe.bind_depth_stencil_alpha_state =
624 nvfx_depth_stencil_alpha_state_bind;
625 nvfx->pipe.delete_depth_stencil_alpha_state =
626 nvfx_depth_stencil_alpha_state_delete;
628 nvfx->pipe.create_vs_state = nvfx_vp_state_create;
629 nvfx->pipe.bind_vs_state = nvfx_vp_state_bind;
630 nvfx->pipe.delete_vs_state = nvfx_vp_state_delete;
632 nvfx->pipe.create_fs_state = nvfx_fp_state_create;
633 nvfx->pipe.bind_fs_state = nvfx_fp_state_bind;
634 nvfx->pipe.delete_fs_state = nvfx_fp_state_delete;
636 nvfx->pipe.set_blend_color = nvfx_set_blend_color;
637 nvfx->pipe.set_stencil_ref = nvfx_set_stencil_ref;
638 nvfx->pipe.set_clip_state = nvfx_set_clip_state;
639 nvfx->pipe.set_constant_buffer = nvfx_set_constant_buffer;
640 nvfx->pipe.set_framebuffer_state = nvfx_set_framebuffer_state;
641 nvfx->pipe.set_polygon_stipple = nvfx_set_polygon_stipple;
642 nvfx->pipe.set_scissor_state = nvfx_set_scissor_state;
643 nvfx->pipe.set_viewport_state = nvfx_set_viewport_state;
645 nvfx->pipe.create_vertex_elements_state = nvfx_vtxelts_state_create;
646 nvfx->pipe.delete_vertex_elements_state = nvfx_vtxelts_state_delete;
647 nvfx->pipe.bind_vertex_elements_state = nvfx_vtxelts_state_bind;
649 nvfx->pipe.set_vertex_buffers = nvfx_set_vertex_buffers;