4c284d9fcbb76532856b2633aac3d616dace9da1
[mesa.git] / src / gallium / drivers / i915 / i915_state.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
29 */
30
31
32 #include "draw/draw_context.h"
33 #include "util/u_inlines.h"
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "util/u_transfer.h"
37 #include "tgsi/tgsi_parse.h"
38
39 #include "i915_context.h"
40 #include "i915_reg.h"
41 #include "i915_state_inlines.h"
42 #include "i915_fpc.h"
43 #include "i915_resource.h"
44
45 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
46 * Intel drivers for "other operating systems" implement GL_CLAMP as
47 * GL_CLAMP_TO_EDGE, so the same is done here.
48 */
49 static unsigned
50 translate_wrap_mode(unsigned wrap)
51 {
52 switch (wrap) {
53 case PIPE_TEX_WRAP_REPEAT:
54 return TEXCOORDMODE_WRAP;
55 case PIPE_TEX_WRAP_CLAMP:
56 return TEXCOORDMODE_CLAMP_EDGE; /* not quite correct */
57 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
58 return TEXCOORDMODE_CLAMP_EDGE;
59 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
60 return TEXCOORDMODE_CLAMP_BORDER;
61 case PIPE_TEX_WRAP_MIRROR_REPEAT:
62 return TEXCOORDMODE_MIRROR;
63 default:
64 return TEXCOORDMODE_WRAP;
65 }
66 }
67
68 static unsigned translate_img_filter( unsigned filter )
69 {
70 switch (filter) {
71 case PIPE_TEX_FILTER_NEAREST:
72 return FILTER_NEAREST;
73 case PIPE_TEX_FILTER_LINEAR:
74 return FILTER_LINEAR;
75 default:
76 assert(0);
77 return FILTER_NEAREST;
78 }
79 }
80
81 static unsigned translate_mip_filter( unsigned filter )
82 {
83 switch (filter) {
84 case PIPE_TEX_MIPFILTER_NONE:
85 return MIPFILTER_NONE;
86 case PIPE_TEX_MIPFILTER_NEAREST:
87 return MIPFILTER_NEAREST;
88 case PIPE_TEX_MIPFILTER_LINEAR:
89 return MIPFILTER_LINEAR;
90 default:
91 assert(0);
92 return MIPFILTER_NONE;
93 }
94 }
95
96
97 /* None of this state is actually used for anything yet.
98 */
99 static void *
100 i915_create_blend_state(struct pipe_context *pipe,
101 const struct pipe_blend_state *blend)
102 {
103 struct i915_blend_state *cso_data = CALLOC_STRUCT( i915_blend_state );
104
105 {
106 unsigned eqRGB = blend->rt[0].rgb_func;
107 unsigned srcRGB = blend->rt[0].rgb_src_factor;
108 unsigned dstRGB = blend->rt[0].rgb_dst_factor;
109
110 unsigned eqA = blend->rt[0].alpha_func;
111 unsigned srcA = blend->rt[0].alpha_src_factor;
112 unsigned dstA = blend->rt[0].alpha_dst_factor;
113
114 /* Special handling for MIN/MAX filter modes handled at
115 * state_tracker level.
116 */
117
118 if (srcA != srcRGB ||
119 dstA != dstRGB ||
120 eqA != eqRGB) {
121
122 cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
123 IAB_MODIFY_ENABLE |
124 IAB_ENABLE |
125 IAB_MODIFY_FUNC |
126 IAB_MODIFY_SRC_FACTOR |
127 IAB_MODIFY_DST_FACTOR |
128 SRC_ABLND_FACT(i915_translate_blend_factor(srcA)) |
129 DST_ABLND_FACT(i915_translate_blend_factor(dstA)) |
130 (i915_translate_blend_func(eqA) << IAB_FUNC_SHIFT));
131 }
132 else {
133 cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
134 IAB_MODIFY_ENABLE |
135 0);
136 }
137 }
138
139 cso_data->modes4 |= (_3DSTATE_MODES_4_CMD |
140 ENABLE_LOGIC_OP_FUNC |
141 LOGIC_OP_FUNC(i915_translate_logic_op(blend->logicop_func)));
142
143 if (blend->logicop_enable)
144 cso_data->LIS5 |= S5_LOGICOP_ENABLE;
145
146 if (blend->dither)
147 cso_data->LIS5 |= S5_COLOR_DITHER_ENABLE;
148
149 /* XXX here take the target fixup into account */
150 if ((blend->rt[0].colormask & PIPE_MASK_R) == 0)
151 cso_data->LIS5 |= S5_WRITEDISABLE_RED;
152
153 if ((blend->rt[0].colormask & PIPE_MASK_G) == 0)
154 cso_data->LIS5 |= S5_WRITEDISABLE_GREEN;
155
156 if ((blend->rt[0].colormask & PIPE_MASK_B) == 0)
157 cso_data->LIS5 |= S5_WRITEDISABLE_BLUE;
158
159 if ((blend->rt[0].colormask & PIPE_MASK_A) == 0)
160 cso_data->LIS5 |= S5_WRITEDISABLE_ALPHA;
161
162 if (blend->rt[0].blend_enable) {
163 unsigned funcRGB = blend->rt[0].rgb_func;
164 unsigned srcRGB = blend->rt[0].rgb_src_factor;
165 unsigned dstRGB = blend->rt[0].rgb_dst_factor;
166
167 cso_data->LIS6 |= (S6_CBUF_BLEND_ENABLE |
168 SRC_BLND_FACT(i915_translate_blend_factor(srcRGB)) |
169 DST_BLND_FACT(i915_translate_blend_factor(dstRGB)) |
170 (i915_translate_blend_func(funcRGB) << S6_CBUF_BLEND_FUNC_SHIFT));
171 }
172
173 return cso_data;
174 }
175
176 static void i915_bind_blend_state(struct pipe_context *pipe,
177 void *blend)
178 {
179 struct i915_context *i915 = i915_context(pipe);
180
181 if (i915->blend == blend)
182 return;
183
184 i915->blend = (struct i915_blend_state*)blend;
185
186 i915->dirty |= I915_NEW_BLEND;
187 }
188
189
190 static void i915_delete_blend_state(struct pipe_context *pipe, void *blend)
191 {
192 FREE(blend);
193 }
194
195 static void i915_set_blend_color( struct pipe_context *pipe,
196 const struct pipe_blend_color *blend_color )
197 {
198 struct i915_context *i915 = i915_context(pipe);
199
200 if (!blend_color)
201 return;
202
203 i915->blend_color = *blend_color;
204
205 i915->dirty |= I915_NEW_BLEND;
206 }
207
208 static void i915_set_stencil_ref( struct pipe_context *pipe,
209 const struct pipe_stencil_ref *stencil_ref )
210 {
211 struct i915_context *i915 = i915_context(pipe);
212
213 i915->stencil_ref = *stencil_ref;
214
215 i915->dirty |= I915_NEW_DEPTH_STENCIL;
216 }
217
218 static void *
219 i915_create_sampler_state(struct pipe_context *pipe,
220 const struct pipe_sampler_state *sampler)
221 {
222 struct i915_sampler_state *cso = CALLOC_STRUCT( i915_sampler_state );
223 const unsigned ws = sampler->wrap_s;
224 const unsigned wt = sampler->wrap_t;
225 const unsigned wr = sampler->wrap_r;
226 unsigned minFilt, magFilt;
227 unsigned mipFilt;
228
229 cso->templ = *sampler;
230
231 mipFilt = translate_mip_filter(sampler->min_mip_filter);
232 minFilt = translate_img_filter( sampler->min_img_filter );
233 magFilt = translate_img_filter( sampler->mag_img_filter );
234
235 if (sampler->max_anisotropy > 1)
236 minFilt = magFilt = FILTER_ANISOTROPIC;
237
238 if (sampler->max_anisotropy > 2) {
239 cso->state[0] |= SS2_MAX_ANISO_4;
240 }
241
242 {
243 int b = (int) (sampler->lod_bias * 16.0);
244 b = CLAMP(b, -256, 255);
245 cso->state[0] |= ((b << SS2_LOD_BIAS_SHIFT) & SS2_LOD_BIAS_MASK);
246 }
247
248 /* Shadow:
249 */
250 if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
251 {
252 cso->state[0] |= (SS2_SHADOW_ENABLE |
253 i915_translate_shadow_compare_func(sampler->compare_func));
254
255 minFilt = FILTER_4X4_FLAT;
256 magFilt = FILTER_4X4_FLAT;
257 }
258
259 cso->state[0] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
260 (mipFilt << SS2_MIP_FILTER_SHIFT) |
261 (magFilt << SS2_MAG_FILTER_SHIFT));
262
263 cso->state[1] |=
264 ((translate_wrap_mode(ws) << SS3_TCX_ADDR_MODE_SHIFT) |
265 (translate_wrap_mode(wt) << SS3_TCY_ADDR_MODE_SHIFT) |
266 (translate_wrap_mode(wr) << SS3_TCZ_ADDR_MODE_SHIFT));
267
268 if (sampler->normalized_coords)
269 cso->state[1] |= SS3_NORMALIZED_COORDS;
270
271 {
272 int minlod = (int) (16.0 * sampler->min_lod);
273 int maxlod = (int) (16.0 * sampler->max_lod);
274 minlod = CLAMP(minlod, 0, 16 * 11);
275 maxlod = CLAMP(maxlod, 0, 16 * 11);
276
277 if (minlod > maxlod)
278 maxlod = minlod;
279
280 cso->minlod = minlod;
281 cso->maxlod = maxlod;
282 }
283
284 {
285 ubyte r = float_to_ubyte(sampler->border_color.f[0]);
286 ubyte g = float_to_ubyte(sampler->border_color.f[1]);
287 ubyte b = float_to_ubyte(sampler->border_color.f[2]);
288 ubyte a = float_to_ubyte(sampler->border_color.f[3]);
289 cso->state[2] = I915PACKCOLOR8888(r, g, b, a);
290 }
291 return cso;
292 }
293
294 static void i915_fixup_bind_sampler_states(struct pipe_context *pipe,
295 unsigned num, void **sampler)
296 {
297 struct i915_context *i915 = i915_context(pipe);
298
299 i915->saved_nr_samplers = num;
300 memcpy(&i915->saved_samplers, sampler, sizeof(void *) * num);
301
302 i915->saved_bind_sampler_states(pipe, num, sampler);
303 }
304
305 static void
306 i915_bind_vertex_sampler_states(struct pipe_context *pipe,
307 unsigned num_samplers,
308 void **samplers)
309 {
310 struct i915_context *i915 = i915_context(pipe);
311 unsigned i;
312
313 assert(num_samplers <= PIPE_MAX_VERTEX_SAMPLERS);
314
315 /* Check for no-op */
316 if (num_samplers == i915->num_vertex_samplers &&
317 !memcmp(i915->vertex_samplers, samplers, num_samplers * sizeof(void *)))
318 return;
319
320 for (i = 0; i < num_samplers; ++i)
321 i915->vertex_samplers[i] = samplers[i];
322 for (i = num_samplers; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
323 i915->vertex_samplers[i] = NULL;
324
325 i915->num_vertex_samplers = num_samplers;
326
327 draw_set_samplers(i915->draw,
328 i915->vertex_samplers,
329 i915->num_vertex_samplers);
330 }
331
332
333
334 static void i915_bind_fragment_sampler_states(struct pipe_context *pipe,
335 unsigned num, void **sampler)
336 {
337 struct i915_context *i915 = i915_context(pipe);
338 unsigned i;
339
340 /* Check for no-op */
341 if (num == i915->num_samplers &&
342 !memcmp(i915->sampler, sampler, num * sizeof(void *)))
343 return;
344
345 for (i = 0; i < num; ++i)
346 i915->sampler[i] = sampler[i];
347 for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
348 i915->sampler[i] = NULL;
349
350 i915->num_samplers = num;
351
352 i915->dirty |= I915_NEW_SAMPLER;
353 }
354
355 static void i915_delete_sampler_state(struct pipe_context *pipe,
356 void *sampler)
357 {
358 FREE(sampler);
359 }
360
361
362 /**
363 * Called before drawing VBO to map vertex samplers and hand them to draw
364 */
365 void
366 i915_prepare_vertex_sampling(struct i915_context *i915)
367 {
368 struct i915_winsys *iws = i915->iws;
369 unsigned i,j;
370 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
371 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
372 const void* data[PIPE_MAX_TEXTURE_LEVELS];
373 unsigned num = i915->num_vertex_sampler_views;
374 struct pipe_sampler_view **views = i915->vertex_sampler_views;
375
376 assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
377 if (!num)
378 return;
379
380 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
381 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
382
383 if (view) {
384 struct pipe_resource *tex = view->texture;
385 struct i915_texture *i915_tex = i915_texture(tex);
386 ubyte *addr;
387
388 /* We're referencing the texture's internal data, so save a
389 * reference to it.
390 */
391 pipe_resource_reference(&i915->mapped_vs_tex[i], tex);
392
393 i915->mapped_vs_tex_buffer[i] = i915_tex->buffer;
394 addr = iws->buffer_map(iws,
395 i915_tex->buffer,
396 FALSE /* read only */);
397
398 /* Setup array of mipmap level pointers */
399 /* FIXME: handle 3D textures? */
400 for (j = view->u.tex.first_level; j <= tex->last_level; j++) {
401 unsigned offset = i915_texture_offset(i915_tex, j , 0 /* FIXME depth */);
402 data[j] = addr + offset;
403 row_stride[j] = i915_tex->stride;
404 img_stride[j] = 0; /* FIXME */;
405 }
406
407 draw_set_mapped_texture(i915->draw,
408 i,
409 tex->width0, tex->height0, tex->depth0,
410 view->u.tex.first_level, tex->last_level,
411 row_stride, img_stride, data);
412 } else
413 i915->mapped_vs_tex[i] = NULL;
414 }
415 }
416
417 void
418 i915_cleanup_vertex_sampling(struct i915_context *i915)
419 {
420 struct i915_winsys *iws = i915->iws;
421 unsigned i;
422 for (i = 0; i < Elements(i915->mapped_vs_tex); i++) {
423 if (i915->mapped_vs_tex_buffer[i]) {
424 iws->buffer_unmap(iws, i915->mapped_vs_tex_buffer[i]);
425 pipe_resource_reference(&i915->mapped_vs_tex[i], NULL);
426 }
427 }
428 }
429
430
431
432 /** XXX move someday? Or consolidate all these simple state setters
433 * into one file.
434 */
435
436 static void *
437 i915_create_depth_stencil_state(struct pipe_context *pipe,
438 const struct pipe_depth_stencil_alpha_state *depth_stencil)
439 {
440 struct i915_depth_stencil_state *cso = CALLOC_STRUCT( i915_depth_stencil_state );
441
442 {
443 int testmask = depth_stencil->stencil[0].valuemask & 0xff;
444 int writemask = depth_stencil->stencil[0].writemask & 0xff;
445
446 cso->stencil_modes4 |= (_3DSTATE_MODES_4_CMD |
447 ENABLE_STENCIL_TEST_MASK |
448 STENCIL_TEST_MASK(testmask) |
449 ENABLE_STENCIL_WRITE_MASK |
450 STENCIL_WRITE_MASK(writemask));
451 }
452
453 if (depth_stencil->stencil[0].enabled) {
454 int test = i915_translate_compare_func(depth_stencil->stencil[0].func);
455 int fop = i915_translate_stencil_op(depth_stencil->stencil[0].fail_op);
456 int dfop = i915_translate_stencil_op(depth_stencil->stencil[0].zfail_op);
457 int dpop = i915_translate_stencil_op(depth_stencil->stencil[0].zpass_op);
458
459 cso->stencil_LIS5 |= (S5_STENCIL_TEST_ENABLE |
460 S5_STENCIL_WRITE_ENABLE |
461 (test << S5_STENCIL_TEST_FUNC_SHIFT) |
462 (fop << S5_STENCIL_FAIL_SHIFT) |
463 (dfop << S5_STENCIL_PASS_Z_FAIL_SHIFT) |
464 (dpop << S5_STENCIL_PASS_Z_PASS_SHIFT));
465 }
466
467 if (depth_stencil->stencil[1].enabled) {
468 int test = i915_translate_compare_func(depth_stencil->stencil[1].func);
469 int fop = i915_translate_stencil_op(depth_stencil->stencil[1].fail_op);
470 int dfop = i915_translate_stencil_op(depth_stencil->stencil[1].zfail_op);
471 int dpop = i915_translate_stencil_op(depth_stencil->stencil[1].zpass_op);
472 int tmask = depth_stencil->stencil[1].valuemask & 0xff;
473 int wmask = depth_stencil->stencil[1].writemask & 0xff;
474
475 cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
476 BFO_ENABLE_STENCIL_FUNCS |
477 BFO_ENABLE_STENCIL_TWO_SIDE |
478 BFO_ENABLE_STENCIL_REF |
479 BFO_STENCIL_TWO_SIDE |
480 (test << BFO_STENCIL_TEST_SHIFT) |
481 (fop << BFO_STENCIL_FAIL_SHIFT) |
482 (dfop << BFO_STENCIL_PASS_Z_FAIL_SHIFT) |
483 (dpop << BFO_STENCIL_PASS_Z_PASS_SHIFT));
484
485 cso->bfo[1] = (_3DSTATE_BACKFACE_STENCIL_MASKS |
486 BFM_ENABLE_STENCIL_TEST_MASK |
487 BFM_ENABLE_STENCIL_WRITE_MASK |
488 (tmask << BFM_STENCIL_TEST_MASK_SHIFT) |
489 (wmask << BFM_STENCIL_WRITE_MASK_SHIFT));
490 }
491 else {
492 /* This actually disables two-side stencil: The bit set is a
493 * modify-enable bit to indicate we are changing the two-side
494 * setting. Then there is a symbolic zero to show that we are
495 * setting the flag to zero/off.
496 */
497 cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
498 BFO_ENABLE_STENCIL_TWO_SIDE |
499 0);
500 cso->bfo[1] = 0;
501 }
502
503 if (depth_stencil->depth.enabled) {
504 int func = i915_translate_compare_func(depth_stencil->depth.func);
505
506 cso->depth_LIS6 |= (S6_DEPTH_TEST_ENABLE |
507 (func << S6_DEPTH_TEST_FUNC_SHIFT));
508
509 if (depth_stencil->depth.writemask)
510 cso->depth_LIS6 |= S6_DEPTH_WRITE_ENABLE;
511 }
512
513 if (depth_stencil->alpha.enabled) {
514 int test = i915_translate_compare_func(depth_stencil->alpha.func);
515 ubyte refByte = float_to_ubyte(depth_stencil->alpha.ref_value);
516
517 cso->depth_LIS6 |= (S6_ALPHA_TEST_ENABLE |
518 (test << S6_ALPHA_TEST_FUNC_SHIFT) |
519 (((unsigned) refByte) << S6_ALPHA_REF_SHIFT));
520 }
521
522 return cso;
523 }
524
525 static void i915_bind_depth_stencil_state(struct pipe_context *pipe,
526 void *depth_stencil)
527 {
528 struct i915_context *i915 = i915_context(pipe);
529
530 if (i915->depth_stencil == depth_stencil)
531 return;
532
533 i915->depth_stencil = (const struct i915_depth_stencil_state *)depth_stencil;
534
535 i915->dirty |= I915_NEW_DEPTH_STENCIL;
536 }
537
538 static void i915_delete_depth_stencil_state(struct pipe_context *pipe,
539 void *depth_stencil)
540 {
541 FREE(depth_stencil);
542 }
543
544
545 static void i915_set_scissor_state( struct pipe_context *pipe,
546 const struct pipe_scissor_state *scissor )
547 {
548 struct i915_context *i915 = i915_context(pipe);
549
550 memcpy( &i915->scissor, scissor, sizeof(*scissor) );
551 i915->dirty |= I915_NEW_SCISSOR;
552 }
553
554
555 static void i915_set_polygon_stipple( struct pipe_context *pipe,
556 const struct pipe_poly_stipple *stipple )
557 {
558 }
559
560
561
562 static void *
563 i915_create_fs_state(struct pipe_context *pipe,
564 const struct pipe_shader_state *templ)
565 {
566 struct i915_context *i915 = i915_context(pipe);
567 struct i915_fragment_shader *ifs = CALLOC_STRUCT(i915_fragment_shader);
568 if (!ifs)
569 return NULL;
570
571 ifs->draw_data = draw_create_fragment_shader(i915->draw, templ);
572 ifs->state.tokens = tgsi_dup_tokens(templ->tokens);
573
574 tgsi_scan_shader(templ->tokens, &ifs->info);
575
576 /* The shader's compiled to i915 instructions here */
577 i915_translate_fragment_program(i915, ifs);
578
579 return ifs;
580 }
581
582 static void
583 i915_fixup_bind_fs_state(struct pipe_context *pipe, void *shader)
584 {
585 struct i915_context *i915 = i915_context(pipe);
586
587 if (i915->saved_fs == shader)
588 return;
589
590 i915->saved_fs = shader;
591
592 i915->saved_bind_fs_state(pipe, shader);
593 }
594
595 static void
596 i915_bind_fs_state(struct pipe_context *pipe, void *shader)
597 {
598 struct i915_context *i915 = i915_context(pipe);
599
600 if (i915->fs == shader)
601 return;
602
603 i915->fs = (struct i915_fragment_shader*) shader;
604
605 draw_bind_fragment_shader(i915->draw, (i915->fs ? i915->fs->draw_data : NULL));
606
607 i915->dirty |= I915_NEW_FS;
608 }
609
610 static
611 void i915_delete_fs_state(struct pipe_context *pipe, void *shader)
612 {
613 struct i915_fragment_shader *ifs = (struct i915_fragment_shader *) shader;
614
615 if (ifs->decl) {
616 FREE(ifs->decl);
617 ifs->decl = NULL;
618 }
619
620 if (ifs->program) {
621 FREE(ifs->program);
622 ifs->program = NULL;
623 FREE((struct tgsi_token *)ifs->state.tokens);
624 ifs->state.tokens = NULL;
625 }
626 ifs->program_len = 0;
627 ifs->decl_len = 0;
628
629 FREE(ifs);
630 }
631
632
633 static void *
634 i915_create_vs_state(struct pipe_context *pipe,
635 const struct pipe_shader_state *templ)
636 {
637 struct i915_context *i915 = i915_context(pipe);
638
639 /* just pass-through to draw module */
640 return draw_create_vertex_shader(i915->draw, templ);
641 }
642
643 static void i915_bind_vs_state(struct pipe_context *pipe, void *shader)
644 {
645 struct i915_context *i915 = i915_context(pipe);
646
647 if (i915->saved_vs == shader)
648 return;
649
650 i915->saved_vs = shader;
651
652 /* just pass-through to draw module */
653 draw_bind_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
654
655 i915->dirty |= I915_NEW_VS;
656 }
657
658 static void i915_delete_vs_state(struct pipe_context *pipe, void *shader)
659 {
660 struct i915_context *i915 = i915_context(pipe);
661
662 /* just pass-through to draw module */
663 draw_delete_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
664 }
665
666 static void i915_set_constant_buffer(struct pipe_context *pipe,
667 uint shader, uint index,
668 struct pipe_resource *buf)
669 {
670 struct i915_context *i915 = i915_context(pipe);
671 unsigned new_num = 0;
672 boolean diff = TRUE;
673
674
675 /* XXX don't support geom shaders now */
676 if (shader == PIPE_SHADER_GEOMETRY)
677 return;
678
679 /* if we have a new buffer compare it with the old one */
680 if (buf) {
681 struct i915_buffer *ibuf = i915_buffer(buf);
682 struct pipe_resource *old_buf = i915->constants[shader];
683 struct i915_buffer *old = old_buf ? i915_buffer(old_buf) : NULL;
684 unsigned old_num = i915->current.num_user_constants[shader];
685
686 new_num = ibuf->b.b.width0 / 4 * sizeof(float);
687
688 if (old_num == new_num) {
689 if (old_num == 0)
690 diff = FALSE;
691 #if 0
692 /* XXX no point in running this code since st/mesa only uses user buffers */
693 /* Can't compare the buffer data since they are userbuffers */
694 else if (old && old->free_on_destroy)
695 diff = memcmp(old->data, ibuf->data, ibuf->b.b.width0);
696 #else
697 (void)old;
698 #endif
699 }
700 } else {
701 diff = i915->current.num_user_constants[shader] != 0;
702 }
703
704 pipe_resource_reference(&i915->constants[shader], buf);
705 i915->current.num_user_constants[shader] = new_num;
706
707 if (diff)
708 i915->dirty |= shader == PIPE_SHADER_VERTEX ? I915_NEW_VS_CONSTANTS : I915_NEW_FS_CONSTANTS;
709 }
710
711
712 static void
713 i915_fixup_set_fragment_sampler_views(struct pipe_context *pipe,
714 unsigned num,
715 struct pipe_sampler_view **views)
716 {
717 struct i915_context *i915 = i915_context(pipe);
718 int i;
719
720 for (i = 0; i < num; i++)
721 pipe_sampler_view_reference(&i915->saved_sampler_views[i],
722 views[i]);
723
724 for (i = num; i < i915->saved_nr_sampler_views; i++)
725 pipe_sampler_view_reference(&i915->saved_sampler_views[i],
726 NULL);
727
728 i915->saved_nr_sampler_views = num;
729
730 i915->saved_set_sampler_views(pipe, num, views);
731 }
732
733 static void i915_set_fragment_sampler_views(struct pipe_context *pipe,
734 unsigned num,
735 struct pipe_sampler_view **views)
736 {
737 struct i915_context *i915 = i915_context(pipe);
738 uint i;
739
740 assert(num <= PIPE_MAX_SAMPLERS);
741
742 /* Check for no-op */
743 if (num == i915->num_fragment_sampler_views &&
744 !memcmp(i915->fragment_sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
745 return;
746
747 for (i = 0; i < num; i++)
748 pipe_sampler_view_reference(&i915->fragment_sampler_views[i],
749 views[i]);
750
751 for (i = num; i < i915->num_fragment_sampler_views; i++)
752 pipe_sampler_view_reference(&i915->fragment_sampler_views[i],
753 NULL);
754
755 i915->num_fragment_sampler_views = num;
756
757 i915->dirty |= I915_NEW_SAMPLER_VIEW;
758 }
759
760 static void
761 i915_set_vertex_sampler_views(struct pipe_context *pipe,
762 unsigned num,
763 struct pipe_sampler_view **views)
764 {
765 struct i915_context *i915 = i915_context(pipe);
766 uint i;
767
768 assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
769
770 /* Check for no-op */
771 if (num == i915->num_vertex_sampler_views &&
772 !memcmp(i915->vertex_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
773 return;
774 }
775
776 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
777 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
778
779 pipe_sampler_view_reference(&i915->vertex_sampler_views[i], view);
780 }
781
782 i915->num_vertex_sampler_views = num;
783
784 draw_set_sampler_views(i915->draw,
785 i915->vertex_sampler_views,
786 i915->num_vertex_sampler_views);
787 }
788
789
790 static struct pipe_sampler_view *
791 i915_create_sampler_view(struct pipe_context *pipe,
792 struct pipe_resource *texture,
793 const struct pipe_sampler_view *templ)
794 {
795 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
796
797 if (view) {
798 *view = *templ;
799 view->reference.count = 1;
800 view->texture = NULL;
801 pipe_resource_reference(&view->texture, texture);
802 view->context = pipe;
803 }
804
805 return view;
806 }
807
808
809 static void
810 i915_sampler_view_destroy(struct pipe_context *pipe,
811 struct pipe_sampler_view *view)
812 {
813 pipe_resource_reference(&view->texture, NULL);
814 FREE(view);
815 }
816
817
818 static void i915_set_framebuffer_state(struct pipe_context *pipe,
819 const struct pipe_framebuffer_state *fb)
820 {
821 struct i915_context *i915 = i915_context(pipe);
822 int i;
823
824 i915->framebuffer.width = fb->width;
825 i915->framebuffer.height = fb->height;
826 i915->framebuffer.nr_cbufs = fb->nr_cbufs;
827 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
828 pipe_surface_reference(&i915->framebuffer.cbufs[i],
829 i < fb->nr_cbufs ? fb->cbufs[i] : NULL);
830 }
831 pipe_surface_reference(&i915->framebuffer.zsbuf, fb->zsbuf);
832
833 i915->dirty |= I915_NEW_FRAMEBUFFER;
834 }
835
836
837
838 static void i915_set_clip_state( struct pipe_context *pipe,
839 const struct pipe_clip_state *clip )
840 {
841 struct i915_context *i915 = i915_context(pipe);
842
843 i915->saved_clip = *clip;
844
845 draw_set_clip_state(i915->draw, clip);
846
847 i915->dirty |= I915_NEW_CLIP;
848 }
849
850
851
852 /* Called when driver state tracker notices changes to the viewport
853 * matrix:
854 */
855 static void i915_set_viewport_state( struct pipe_context *pipe,
856 const struct pipe_viewport_state *viewport )
857 {
858 struct i915_context *i915 = i915_context(pipe);
859
860 i915->viewport = *viewport; /* struct copy */
861
862 /* pass the viewport info to the draw module */
863 draw_set_viewport_state(i915->draw, &i915->viewport);
864
865 i915->dirty |= I915_NEW_VIEWPORT;
866 }
867
868
869 static void *
870 i915_create_rasterizer_state(struct pipe_context *pipe,
871 const struct pipe_rasterizer_state *rasterizer)
872 {
873 struct i915_rasterizer_state *cso = CALLOC_STRUCT( i915_rasterizer_state );
874
875 cso->templ = *rasterizer;
876 cso->color_interp = rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
877 cso->light_twoside = rasterizer->light_twoside;
878 cso->ds[0].u = _3DSTATE_DEPTH_OFFSET_SCALE;
879 cso->ds[1].f = rasterizer->offset_scale;
880 if (rasterizer->poly_stipple_enable) {
881 cso->st |= ST1_ENABLE;
882 }
883
884 if (rasterizer->scissor)
885 cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT;
886 else
887 cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT;
888
889 switch (rasterizer->cull_face) {
890 case PIPE_FACE_NONE:
891 cso->LIS4 |= S4_CULLMODE_NONE;
892 break;
893 case PIPE_FACE_FRONT:
894 if (rasterizer->front_ccw)
895 cso->LIS4 |= S4_CULLMODE_CCW;
896 else
897 cso->LIS4 |= S4_CULLMODE_CW;
898 break;
899 case PIPE_FACE_BACK:
900 if (rasterizer->front_ccw)
901 cso->LIS4 |= S4_CULLMODE_CW;
902 else
903 cso->LIS4 |= S4_CULLMODE_CCW;
904 break;
905 case PIPE_FACE_FRONT_AND_BACK:
906 cso->LIS4 |= S4_CULLMODE_BOTH;
907 break;
908 }
909
910 {
911 int line_width = CLAMP((int)(rasterizer->line_width * 2), 1, 0xf);
912
913 cso->LIS4 |= line_width << S4_LINE_WIDTH_SHIFT;
914
915 if (rasterizer->line_smooth)
916 cso->LIS4 |= S4_LINE_ANTIALIAS_ENABLE;
917 }
918
919 {
920 int point_size = CLAMP((int) rasterizer->point_size, 1, 0xff);
921
922 cso->LIS4 |= point_size << S4_POINT_WIDTH_SHIFT;
923 }
924
925 if (rasterizer->flatshade) {
926 cso->LIS4 |= (S4_FLATSHADE_ALPHA |
927 S4_FLATSHADE_COLOR |
928 S4_FLATSHADE_SPECULAR);
929 }
930
931 cso->LIS7 = fui( rasterizer->offset_units );
932
933
934 return cso;
935 }
936
937 static void i915_bind_rasterizer_state( struct pipe_context *pipe,
938 void *raster )
939 {
940 struct i915_context *i915 = i915_context(pipe);
941
942 if (i915->rasterizer == raster)
943 return;
944
945 i915->rasterizer = (struct i915_rasterizer_state *)raster;
946
947 /* pass-through to draw module */
948 draw_set_rasterizer_state(i915->draw,
949 (i915->rasterizer ? &(i915->rasterizer->templ) : NULL),
950 raster);
951
952 i915->dirty |= I915_NEW_RASTERIZER;
953 }
954
955 static void i915_delete_rasterizer_state(struct pipe_context *pipe,
956 void *raster)
957 {
958 FREE(raster);
959 }
960
961 static void i915_set_vertex_buffers(struct pipe_context *pipe,
962 unsigned count,
963 const struct pipe_vertex_buffer *buffers)
964 {
965 struct i915_context *i915 = i915_context(pipe);
966 struct draw_context *draw = i915->draw;
967 int i;
968
969 util_copy_vertex_buffers(i915->saved_vertex_buffers,
970 &i915->saved_nr_vertex_buffers,
971 buffers, count);
972 #if 0
973 /* XXX doesn't look like this is needed */
974 /* unmap old */
975 for (i = 0; i < i915->num_vertex_buffers; i++) {
976 draw_set_mapped_vertex_buffer(draw, i, NULL);
977 }
978 #endif
979
980 /* pass-through to draw module */
981 draw_set_vertex_buffers(draw, count, buffers);
982
983 /* map new */
984 for (i = 0; i < count; i++) {
985 void *buf = i915_buffer(buffers[i].buffer)->data;
986 draw_set_mapped_vertex_buffer(draw, i, buf);
987 }
988 }
989
990 static void *
991 i915_create_vertex_elements_state(struct pipe_context *pipe,
992 unsigned count,
993 const struct pipe_vertex_element *attribs)
994 {
995 struct i915_velems_state *velems;
996 assert(count <= PIPE_MAX_ATTRIBS);
997 velems = (struct i915_velems_state *) MALLOC(sizeof(struct i915_velems_state));
998 if (velems) {
999 velems->count = count;
1000 memcpy(velems->velem, attribs, sizeof(*attribs) * count);
1001 }
1002 return velems;
1003 }
1004
1005 static void
1006 i915_bind_vertex_elements_state(struct pipe_context *pipe,
1007 void *velems)
1008 {
1009 struct i915_context *i915 = i915_context(pipe);
1010 struct i915_velems_state *i915_velems = (struct i915_velems_state *) velems;
1011
1012 if (i915->saved_velems == velems)
1013 return;
1014
1015 i915->saved_velems = velems;
1016
1017 /* pass-through to draw module */
1018 if (i915_velems) {
1019 draw_set_vertex_elements(i915->draw,
1020 i915_velems->count, i915_velems->velem);
1021 }
1022 }
1023
1024 static void
1025 i915_delete_vertex_elements_state(struct pipe_context *pipe, void *velems)
1026 {
1027 FREE( velems );
1028 }
1029
1030 static void i915_set_index_buffer(struct pipe_context *pipe,
1031 const struct pipe_index_buffer *ib)
1032 {
1033 struct i915_context *i915 = i915_context(pipe);
1034
1035 if (ib)
1036 memcpy(&i915->index_buffer, ib, sizeof(i915->index_buffer));
1037 else
1038 memset(&i915->index_buffer, 0, sizeof(i915->index_buffer));
1039
1040 /* pass-through to draw module */
1041 draw_set_index_buffer(i915->draw, ib);
1042 }
1043
1044 static void
1045 i915_set_sample_mask(struct pipe_context *pipe,
1046 unsigned sample_mask)
1047 {
1048 }
1049
1050 void
1051 i915_init_state_functions( struct i915_context *i915 )
1052 {
1053 i915->base.create_blend_state = i915_create_blend_state;
1054 i915->base.bind_blend_state = i915_bind_blend_state;
1055 i915->base.delete_blend_state = i915_delete_blend_state;
1056
1057 i915->base.create_sampler_state = i915_create_sampler_state;
1058 i915->base.bind_fragment_sampler_states = i915_bind_fragment_sampler_states;
1059 i915->base.bind_vertex_sampler_states = i915_bind_vertex_sampler_states;
1060 i915->base.delete_sampler_state = i915_delete_sampler_state;
1061
1062 i915->base.create_depth_stencil_alpha_state = i915_create_depth_stencil_state;
1063 i915->base.bind_depth_stencil_alpha_state = i915_bind_depth_stencil_state;
1064 i915->base.delete_depth_stencil_alpha_state = i915_delete_depth_stencil_state;
1065
1066 i915->base.create_rasterizer_state = i915_create_rasterizer_state;
1067 i915->base.bind_rasterizer_state = i915_bind_rasterizer_state;
1068 i915->base.delete_rasterizer_state = i915_delete_rasterizer_state;
1069 i915->base.create_fs_state = i915_create_fs_state;
1070 i915->base.bind_fs_state = i915_bind_fs_state;
1071 i915->base.delete_fs_state = i915_delete_fs_state;
1072 i915->base.create_vs_state = i915_create_vs_state;
1073 i915->base.bind_vs_state = i915_bind_vs_state;
1074 i915->base.delete_vs_state = i915_delete_vs_state;
1075 i915->base.create_vertex_elements_state = i915_create_vertex_elements_state;
1076 i915->base.bind_vertex_elements_state = i915_bind_vertex_elements_state;
1077 i915->base.delete_vertex_elements_state = i915_delete_vertex_elements_state;
1078
1079 i915->base.set_blend_color = i915_set_blend_color;
1080 i915->base.set_stencil_ref = i915_set_stencil_ref;
1081 i915->base.set_clip_state = i915_set_clip_state;
1082 i915->base.set_sample_mask = i915_set_sample_mask;
1083 i915->base.set_constant_buffer = i915_set_constant_buffer;
1084 i915->base.set_framebuffer_state = i915_set_framebuffer_state;
1085
1086 i915->base.set_polygon_stipple = i915_set_polygon_stipple;
1087 i915->base.set_scissor_state = i915_set_scissor_state;
1088 i915->base.set_fragment_sampler_views = i915_set_fragment_sampler_views;
1089 i915->base.set_vertex_sampler_views = i915_set_vertex_sampler_views;
1090 i915->base.create_sampler_view = i915_create_sampler_view;
1091 i915->base.sampler_view_destroy = i915_sampler_view_destroy;
1092 i915->base.set_viewport_state = i915_set_viewport_state;
1093 i915->base.set_vertex_buffers = i915_set_vertex_buffers;
1094 i915->base.set_index_buffer = i915_set_index_buffer;
1095 i915->base.redefine_user_buffer = u_default_redefine_user_buffer;
1096 }
1097
1098 void
1099 i915_init_fixup_state_functions( struct i915_context *i915 )
1100 {
1101 i915->saved_bind_fs_state = i915->base.bind_fs_state;
1102 i915->base.bind_fs_state = i915_fixup_bind_fs_state;
1103 i915->saved_bind_sampler_states = i915->base.bind_fragment_sampler_states;
1104 i915->base.bind_fragment_sampler_states = i915_fixup_bind_sampler_states;
1105 i915->saved_set_sampler_views = i915->base.set_fragment_sampler_views;
1106 i915->base.set_fragment_sampler_views = i915_fixup_set_fragment_sampler_views;
1107 }