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