d7893b4f0f2c97ed14bd069f49eddd65e1fa27aa
[mesa.git] / src / gallium / drivers / nv20 / nv20_state.c
1 #include "draw/draw_context.h"
2 #include "pipe/p_state.h"
3 #include "pipe/p_defines.h"
4 #include "pipe/p_shader_tokens.h"
5 #include "pipe/p_inlines.h"
6
7 #include "tgsi/tgsi_parse.h"
8
9 #include "nv20_context.h"
10 #include "nv20_state.h"
11
12 static void *
13 nv20_blend_state_create(struct pipe_context *pipe,
14 const struct pipe_blend_state *cso)
15 {
16 struct nv20_blend_state *cb;
17
18 cb = MALLOC(sizeof(struct nv20_blend_state));
19
20 cb->b_enable = cso->blend_enable ? 1 : 0;
21 cb->b_srcfunc = ((nvgl_blend_func(cso->alpha_src_factor)<<16) |
22 (nvgl_blend_func(cso->rgb_src_factor)));
23 cb->b_dstfunc = ((nvgl_blend_func(cso->alpha_dst_factor)<<16) |
24 (nvgl_blend_func(cso->rgb_dst_factor)));
25
26 cb->c_mask = (((cso->colormask & PIPE_MASK_A) ? (0x01<<24) : 0) |
27 ((cso->colormask & PIPE_MASK_R) ? (0x01<<16) : 0) |
28 ((cso->colormask & PIPE_MASK_G) ? (0x01<< 8) : 0) |
29 ((cso->colormask & PIPE_MASK_B) ? (0x01<< 0) : 0));
30
31 cb->d_enable = cso->dither ? 1 : 0;
32
33 return (void *)cb;
34 }
35
36 static void
37 nv20_blend_state_bind(struct pipe_context *pipe, void *blend)
38 {
39 struct nv20_context *nv20 = nv20_context(pipe);
40
41 nv20->blend = (struct nv20_blend_state*)blend;
42
43 nv20->dirty |= NV20_NEW_BLEND;
44 }
45
46 static void
47 nv20_blend_state_delete(struct pipe_context *pipe, void *hwcso)
48 {
49 FREE(hwcso);
50 }
51
52
53 static INLINE unsigned
54 wrap_mode(unsigned wrap) {
55 unsigned ret;
56
57 switch (wrap) {
58 case PIPE_TEX_WRAP_REPEAT:
59 ret = NV20TCL_TX_WRAP_S_REPEAT;
60 break;
61 case PIPE_TEX_WRAP_MIRROR_REPEAT:
62 ret = NV20TCL_TX_WRAP_S_MIRRORED_REPEAT;
63 break;
64 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
65 ret = NV20TCL_TX_WRAP_S_CLAMP_TO_EDGE;
66 break;
67 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
68 ret = NV20TCL_TX_WRAP_S_CLAMP_TO_BORDER;
69 break;
70 case PIPE_TEX_WRAP_CLAMP:
71 ret = NV20TCL_TX_WRAP_S_CLAMP;
72 break;
73 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
74 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
75 case PIPE_TEX_WRAP_MIRROR_CLAMP:
76 default:
77 NOUVEAU_ERR("unknown wrap mode: %d\n", wrap);
78 ret = NV20TCL_TX_WRAP_S_REPEAT;
79 break;
80 }
81
82 return (ret >> NV20TCL_TX_WRAP_S_SHIFT);
83 }
84
85 static void *
86 nv20_sampler_state_create(struct pipe_context *pipe,
87 const struct pipe_sampler_state *cso)
88 {
89 struct nv20_sampler_state *ps;
90 uint32_t filter = 0;
91
92 ps = MALLOC(sizeof(struct nv20_sampler_state));
93
94 ps->wrap = ((wrap_mode(cso->wrap_s) << NV20TCL_TX_WRAP_S_SHIFT) |
95 (wrap_mode(cso->wrap_t) << NV20TCL_TX_WRAP_T_SHIFT));
96
97 ps->en = 0;
98 if (cso->max_anisotropy > 1.0) {
99 /* no idea, binary driver sets it, works without it.. meh.. */
100 ps->wrap |= (1 << 5);
101
102 /* if (cso->max_anisotropy >= 8.0) {
103 ps->en |= NV20TCL_TX_ENABLE_ANISO_8X;
104 } else
105 if (cso->max_anisotropy >= 4.0) {
106 ps->en |= NV20TCL_TX_ENABLE_ANISO_4X;
107 } else {
108 ps->en |= NV20TCL_TX_ENABLE_ANISO_2X;
109 }*/
110 }
111
112 switch (cso->mag_img_filter) {
113 case PIPE_TEX_FILTER_LINEAR:
114 filter |= NV20TCL_TX_FILTER_MAGNIFY_LINEAR;
115 break;
116 case PIPE_TEX_FILTER_NEAREST:
117 default:
118 filter |= NV20TCL_TX_FILTER_MAGNIFY_NEAREST;
119 break;
120 }
121
122 switch (cso->min_img_filter) {
123 case PIPE_TEX_FILTER_LINEAR:
124 switch (cso->min_mip_filter) {
125 case PIPE_TEX_MIPFILTER_NEAREST:
126 filter |=
127 NV20TCL_TX_FILTER_MINIFY_LINEAR_MIPMAP_NEAREST;
128 break;
129 case PIPE_TEX_MIPFILTER_LINEAR:
130 filter |= NV20TCL_TX_FILTER_MINIFY_LINEAR_MIPMAP_LINEAR;
131 break;
132 case PIPE_TEX_MIPFILTER_NONE:
133 default:
134 filter |= NV20TCL_TX_FILTER_MINIFY_LINEAR;
135 break;
136 }
137 break;
138 case PIPE_TEX_FILTER_NEAREST:
139 default:
140 switch (cso->min_mip_filter) {
141 case PIPE_TEX_MIPFILTER_NEAREST:
142 filter |=
143 NV20TCL_TX_FILTER_MINIFY_NEAREST_MIPMAP_NEAREST;
144 break;
145 case PIPE_TEX_MIPFILTER_LINEAR:
146 filter |=
147 NV20TCL_TX_FILTER_MINIFY_NEAREST_MIPMAP_LINEAR;
148 break;
149 case PIPE_TEX_MIPFILTER_NONE:
150 default:
151 filter |= NV20TCL_TX_FILTER_MINIFY_NEAREST;
152 break;
153 }
154 break;
155 }
156
157 ps->filt = filter;
158
159 /* if (cso->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
160 switch (cso->compare_func) {
161 case PIPE_FUNC_NEVER:
162 ps->wrap |= NV10TCL_TX_WRAP_RCOMP_NEVER;
163 break;
164 case PIPE_FUNC_GREATER:
165 ps->wrap |= NV10TCL_TX_WRAP_RCOMP_GREATER;
166 break;
167 case PIPE_FUNC_EQUAL:
168 ps->wrap |= NV10TCL_TX_WRAP_RCOMP_EQUAL;
169 break;
170 case PIPE_FUNC_GEQUAL:
171 ps->wrap |= NV10TCL_TX_WRAP_RCOMP_GEQUAL;
172 break;
173 case PIPE_FUNC_LESS:
174 ps->wrap |= NV10TCL_TX_WRAP_RCOMP_LESS;
175 break;
176 case PIPE_FUNC_NOTEQUAL:
177 ps->wrap |= NV10TCL_TX_WRAP_RCOMP_NOTEQUAL;
178 break;
179 case PIPE_FUNC_LEQUAL:
180 ps->wrap |= NV10TCL_TX_WRAP_RCOMP_LEQUAL;
181 break;
182 case PIPE_FUNC_ALWAYS:
183 ps->wrap |= NV10TCL_TX_WRAP_RCOMP_ALWAYS;
184 break;
185 default:
186 break;
187 }
188 }*/
189
190 ps->bcol = ((float_to_ubyte(cso->border_color[3]) << 24) |
191 (float_to_ubyte(cso->border_color[0]) << 16) |
192 (float_to_ubyte(cso->border_color[1]) << 8) |
193 (float_to_ubyte(cso->border_color[2]) << 0));
194
195 return (void *)ps;
196 }
197
198 static void
199 nv20_sampler_state_bind(struct pipe_context *pipe, unsigned nr, void **sampler)
200 {
201 struct nv20_context *nv20 = nv20_context(pipe);
202 unsigned unit;
203
204 for (unit = 0; unit < nr; unit++) {
205 nv20->tex_sampler[unit] = sampler[unit];
206 nv20->dirty_samplers |= (1 << unit);
207 }
208 }
209
210 static void
211 nv20_sampler_state_delete(struct pipe_context *pipe, void *hwcso)
212 {
213 FREE(hwcso);
214 }
215
216 static void
217 nv20_set_sampler_texture(struct pipe_context *pipe, unsigned nr,
218 struct pipe_texture **miptree)
219 {
220 struct nv20_context *nv20 = nv20_context(pipe);
221 unsigned unit;
222
223 for (unit = 0; unit < nr; unit++) {
224 nv20->tex_miptree[unit] = (struct nv20_miptree *)miptree[unit];
225 nv20->dirty_samplers |= (1 << unit);
226 }
227 }
228
229 static void *
230 nv20_rasterizer_state_create(struct pipe_context *pipe,
231 const struct pipe_rasterizer_state *cso)
232 {
233 struct nv20_rasterizer_state *rs;
234 int i;
235
236 /*XXX: ignored:
237 * light_twoside
238 * offset_cw/ccw -nohw
239 * scissor
240 * point_smooth -nohw
241 * multisample
242 * offset_units / offset_scale
243 */
244 rs = MALLOC(sizeof(struct nv20_rasterizer_state));
245
246 rs->templ = cso;
247
248 rs->shade_model = cso->flatshade ? NV20TCL_SHADE_MODEL_FLAT :
249 NV20TCL_SHADE_MODEL_SMOOTH;
250
251 rs->line_width = (unsigned char)(cso->line_width * 8.0) & 0xff;
252 rs->line_smooth_en = cso->line_smooth ? 1 : 0;
253
254 /* XXX: nv20 and nv25 different! */
255 rs->point_size = *(uint32_t*)&cso->point_size;
256
257 rs->poly_smooth_en = cso->poly_smooth ? 1 : 0;
258
259 if (cso->front_winding == PIPE_WINDING_CCW) {
260 rs->front_face = NV20TCL_FRONT_FACE_CCW;
261 rs->poly_mode_front = nvgl_polygon_mode(cso->fill_ccw);
262 rs->poly_mode_back = nvgl_polygon_mode(cso->fill_cw);
263 } else {
264 rs->front_face = NV20TCL_FRONT_FACE_CW;
265 rs->poly_mode_front = nvgl_polygon_mode(cso->fill_cw);
266 rs->poly_mode_back = nvgl_polygon_mode(cso->fill_ccw);
267 }
268
269 switch (cso->cull_mode) {
270 case PIPE_WINDING_CCW:
271 rs->cull_face_en = 1;
272 if (cso->front_winding == PIPE_WINDING_CCW)
273 rs->cull_face = NV20TCL_CULL_FACE_FRONT;
274 else
275 rs->cull_face = NV20TCL_CULL_FACE_BACK;
276 break;
277 case PIPE_WINDING_CW:
278 rs->cull_face_en = 1;
279 if (cso->front_winding == PIPE_WINDING_CW)
280 rs->cull_face = NV20TCL_CULL_FACE_FRONT;
281 else
282 rs->cull_face = NV20TCL_CULL_FACE_BACK;
283 break;
284 case PIPE_WINDING_BOTH:
285 rs->cull_face_en = 1;
286 rs->cull_face = NV20TCL_CULL_FACE_FRONT_AND_BACK;
287 break;
288 case PIPE_WINDING_NONE:
289 default:
290 rs->cull_face_en = 0;
291 rs->cull_face = 0;
292 break;
293 }
294
295 if (cso->point_sprite) {
296 rs->point_sprite = (1 << 0);
297 for (i = 0; i < 8; i++) {
298 if (cso->sprite_coord_mode[i] != PIPE_SPRITE_COORD_NONE)
299 rs->point_sprite |= (1 << (8 + i));
300 }
301 } else {
302 rs->point_sprite = 0;
303 }
304
305 return (void *)rs;
306 }
307
308 static void
309 nv20_rasterizer_state_bind(struct pipe_context *pipe, void *rast)
310 {
311 struct nv20_context *nv20 = nv20_context(pipe);
312
313 nv20->rast = (struct nv20_rasterizer_state*)rast;
314
315 draw_set_rasterizer_state(nv20->draw, (nv20->rast ? nv20->rast->templ : NULL));
316
317 nv20->dirty |= NV20_NEW_RAST;
318 }
319
320 static void
321 nv20_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
322 {
323 FREE(hwcso);
324 }
325
326 static void *
327 nv20_depth_stencil_alpha_state_create(struct pipe_context *pipe,
328 const struct pipe_depth_stencil_alpha_state *cso)
329 {
330 struct nv20_depth_stencil_alpha_state *hw;
331
332 hw = MALLOC(sizeof(struct nv20_depth_stencil_alpha_state));
333
334 hw->depth.func = nvgl_comparison_op(cso->depth.func);
335 hw->depth.write_enable = cso->depth.writemask ? 1 : 0;
336 hw->depth.test_enable = cso->depth.enabled ? 1 : 0;
337
338 hw->stencil.enable = cso->stencil[0].enabled ? 1 : 0;
339 hw->stencil.wmask = cso->stencil[0].writemask;
340 hw->stencil.func = nvgl_comparison_op(cso->stencil[0].func);
341 hw->stencil.ref = cso->stencil[0].ref_value;
342 hw->stencil.vmask = cso->stencil[0].valuemask;
343 hw->stencil.fail = nvgl_stencil_op(cso->stencil[0].fail_op);
344 hw->stencil.zfail = nvgl_stencil_op(cso->stencil[0].zfail_op);
345 hw->stencil.zpass = nvgl_stencil_op(cso->stencil[0].zpass_op);
346
347 hw->alpha.enabled = cso->alpha.enabled ? 1 : 0;
348 hw->alpha.func = nvgl_comparison_op(cso->alpha.func);
349 hw->alpha.ref = float_to_ubyte(cso->alpha.ref_value);
350
351 return (void *)hw;
352 }
353
354 static void
355 nv20_depth_stencil_alpha_state_bind(struct pipe_context *pipe, void *dsa)
356 {
357 struct nv20_context *nv20 = nv20_context(pipe);
358
359 nv20->dsa = (struct nv20_depth_stencil_alpha_state*)dsa;
360
361 nv20->dirty |= NV20_NEW_DSA;
362 }
363
364 static void
365 nv20_depth_stencil_alpha_state_delete(struct pipe_context *pipe, void *hwcso)
366 {
367 FREE(hwcso);
368 }
369
370 static void *
371 nv20_vp_state_create(struct pipe_context *pipe,
372 const struct pipe_shader_state *templ)
373 {
374 struct nv20_context *nv20 = nv20_context(pipe);
375
376 return draw_create_vertex_shader(nv20->draw, templ);
377 }
378
379 static void
380 nv20_vp_state_bind(struct pipe_context *pipe, void *shader)
381 {
382 struct nv20_context *nv20 = nv20_context(pipe);
383
384 draw_bind_vertex_shader(nv20->draw, (struct draw_vertex_shader *) shader);
385
386 nv20->dirty |= NV20_NEW_VERTPROG;
387 }
388
389 static void
390 nv20_vp_state_delete(struct pipe_context *pipe, void *shader)
391 {
392 struct nv20_context *nv20 = nv20_context(pipe);
393
394 draw_delete_vertex_shader(nv20->draw, (struct draw_vertex_shader *) shader);
395 }
396
397 static void *
398 nv20_fp_state_create(struct pipe_context *pipe,
399 const struct pipe_shader_state *cso)
400 {
401 struct nv20_fragment_program *fp;
402
403 fp = CALLOC(1, sizeof(struct nv20_fragment_program));
404 fp->pipe.tokens = tgsi_dup_tokens(cso->tokens);
405
406 tgsi_scan_shader(cso->tokens, &fp->info);
407
408 return (void *)fp;
409 }
410
411 static void
412 nv20_fp_state_bind(struct pipe_context *pipe, void *hwcso)
413 {
414 struct nv20_context *nv20 = nv20_context(pipe);
415 struct nv20_fragment_program *fp = hwcso;
416
417 nv20->fragprog.current = fp;
418 nv20->dirty |= NV20_NEW_FRAGPROG;
419 }
420
421 static void
422 nv20_fp_state_delete(struct pipe_context *pipe, void *hwcso)
423 {
424 struct nv20_context *nv20 = nv20_context(pipe);
425 struct nv20_fragment_program *fp = hwcso;
426
427 nv20_fragprog_destroy(nv20, fp);
428 FREE((void*)fp->pipe.tokens);
429 FREE(fp);
430 }
431
432 static void
433 nv20_set_blend_color(struct pipe_context *pipe,
434 const struct pipe_blend_color *bcol)
435 {
436 struct nv20_context *nv20 = nv20_context(pipe);
437
438 nv20->blend_color = (struct pipe_blend_color*)bcol;
439
440 nv20->dirty |= NV20_NEW_BLENDCOL;
441 }
442
443 static void
444 nv20_set_clip_state(struct pipe_context *pipe,
445 const struct pipe_clip_state *clip)
446 {
447 struct nv20_context *nv20 = nv20_context(pipe);
448
449 draw_set_clip_state(nv20->draw, clip);
450 }
451
452 static void
453 nv20_set_constant_buffer(struct pipe_context *pipe, uint shader, uint index,
454 const struct pipe_buffer *buf )
455 {
456 struct nv20_context *nv20 = nv20_context(pipe);
457 struct pipe_screen *pscreen = pipe->screen;
458
459 assert(shader < PIPE_SHADER_TYPES);
460 assert(index == 0);
461
462 if (buf) {
463 void *mapped;
464 if (buf->size &&
465 (mapped = pipe_buffer_map(pscreen, buf, PIPE_BUFFER_USAGE_CPU_READ)))
466 {
467 memcpy(nv20->constbuf[shader], mapped, buf->size);
468 nv20->constbuf_nr[shader] =
469 buf->size / (4 * sizeof(float));
470 pipe_buffer_unmap(pscreen, buf);
471 }
472 }
473 }
474
475 static void
476 nv20_set_framebuffer_state(struct pipe_context *pipe,
477 const struct pipe_framebuffer_state *fb)
478 {
479 struct nv20_context *nv20 = nv20_context(pipe);
480
481 nv20->framebuffer = (struct pipe_framebuffer_state*)fb;
482
483 nv20->dirty |= NV20_NEW_FRAMEBUFFER;
484 }
485
486 static void
487 nv20_set_polygon_stipple(struct pipe_context *pipe,
488 const struct pipe_poly_stipple *stipple)
489 {
490 NOUVEAU_ERR("line stipple hahaha\n");
491 }
492
493 static void
494 nv20_set_scissor_state(struct pipe_context *pipe,
495 const struct pipe_scissor_state *s)
496 {
497 struct nv20_context *nv20 = nv20_context(pipe);
498
499 nv20->scissor = (struct pipe_scissor_state*)s;
500
501 nv20->dirty |= NV20_NEW_SCISSOR;
502 }
503
504 static void
505 nv20_set_viewport_state(struct pipe_context *pipe,
506 const struct pipe_viewport_state *vpt)
507 {
508 struct nv20_context *nv20 = nv20_context(pipe);
509
510 nv20->viewport = (struct pipe_viewport_state*)vpt;
511
512 draw_set_viewport_state(nv20->draw, nv20->viewport);
513
514 nv20->dirty |= NV20_NEW_VIEWPORT;
515 }
516
517 static void
518 nv20_set_vertex_buffers(struct pipe_context *pipe, unsigned count,
519 const struct pipe_vertex_buffer *vb)
520 {
521 struct nv20_context *nv20 = nv20_context(pipe);
522
523 memcpy(nv20->vtxbuf, vb, sizeof(*vb) * count);
524 nv20->dirty |= NV20_NEW_VTXARRAYS;
525
526 draw_set_vertex_buffers(nv20->draw, count, vb);
527 }
528
529 static void
530 nv20_set_vertex_elements(struct pipe_context *pipe, unsigned count,
531 const struct pipe_vertex_element *ve)
532 {
533 struct nv20_context *nv20 = nv20_context(pipe);
534
535 memcpy(nv20->vtxelt, ve, sizeof(*ve) * count);
536 nv20->dirty |= NV20_NEW_VTXARRAYS;
537
538 draw_set_vertex_elements(nv20->draw, count, ve);
539 }
540
541 void
542 nv20_init_state_functions(struct nv20_context *nv20)
543 {
544 nv20->pipe.create_blend_state = nv20_blend_state_create;
545 nv20->pipe.bind_blend_state = nv20_blend_state_bind;
546 nv20->pipe.delete_blend_state = nv20_blend_state_delete;
547
548 nv20->pipe.create_sampler_state = nv20_sampler_state_create;
549 nv20->pipe.bind_fragment_sampler_states = nv20_sampler_state_bind;
550 nv20->pipe.delete_sampler_state = nv20_sampler_state_delete;
551 nv20->pipe.set_fragment_sampler_textures = nv20_set_sampler_texture;
552
553 nv20->pipe.create_rasterizer_state = nv20_rasterizer_state_create;
554 nv20->pipe.bind_rasterizer_state = nv20_rasterizer_state_bind;
555 nv20->pipe.delete_rasterizer_state = nv20_rasterizer_state_delete;
556
557 nv20->pipe.create_depth_stencil_alpha_state =
558 nv20_depth_stencil_alpha_state_create;
559 nv20->pipe.bind_depth_stencil_alpha_state =
560 nv20_depth_stencil_alpha_state_bind;
561 nv20->pipe.delete_depth_stencil_alpha_state =
562 nv20_depth_stencil_alpha_state_delete;
563
564 nv20->pipe.create_vs_state = nv20_vp_state_create;
565 nv20->pipe.bind_vs_state = nv20_vp_state_bind;
566 nv20->pipe.delete_vs_state = nv20_vp_state_delete;
567
568 nv20->pipe.create_fs_state = nv20_fp_state_create;
569 nv20->pipe.bind_fs_state = nv20_fp_state_bind;
570 nv20->pipe.delete_fs_state = nv20_fp_state_delete;
571
572 nv20->pipe.set_blend_color = nv20_set_blend_color;
573 nv20->pipe.set_clip_state = nv20_set_clip_state;
574 nv20->pipe.set_constant_buffer = nv20_set_constant_buffer;
575 nv20->pipe.set_framebuffer_state = nv20_set_framebuffer_state;
576 nv20->pipe.set_polygon_stipple = nv20_set_polygon_stipple;
577 nv20->pipe.set_scissor_state = nv20_set_scissor_state;
578 nv20->pipe.set_viewport_state = nv20_set_viewport_state;
579
580 nv20->pipe.set_vertex_buffers = nv20_set_vertex_buffers;
581 nv20->pipe.set_vertex_elements = nv20_set_vertex_elements;
582 }
583