i915g: Fix point sprites.
[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 if ((blend->rt[0].colormask & PIPE_MASK_R) == 0)
150 cso_data->LIS5 |= S5_WRITEDISABLE_RED;
151
152 if ((blend->rt[0].colormask & PIPE_MASK_G) == 0)
153 cso_data->LIS5 |= S5_WRITEDISABLE_GREEN;
154
155 if ((blend->rt[0].colormask & PIPE_MASK_B) == 0)
156 cso_data->LIS5 |= S5_WRITEDISABLE_BLUE;
157
158 if ((blend->rt[0].colormask & PIPE_MASK_A) == 0)
159 cso_data->LIS5 |= S5_WRITEDISABLE_ALPHA;
160
161 if (blend->rt[0].blend_enable) {
162 unsigned funcRGB = blend->rt[0].rgb_func;
163 unsigned srcRGB = blend->rt[0].rgb_src_factor;
164 unsigned dstRGB = blend->rt[0].rgb_dst_factor;
165
166 cso_data->LIS6 |= (S6_CBUF_BLEND_ENABLE |
167 SRC_BLND_FACT(i915_translate_blend_factor(srcRGB)) |
168 DST_BLND_FACT(i915_translate_blend_factor(dstRGB)) |
169 (i915_translate_blend_func(funcRGB) << S6_CBUF_BLEND_FUNC_SHIFT));
170 }
171
172 return cso_data;
173 }
174
175 static void i915_bind_blend_state(struct pipe_context *pipe,
176 void *blend)
177 {
178 struct i915_context *i915 = i915_context(pipe);
179 draw_flush(i915->draw);
180
181 i915->blend = (struct i915_blend_state*)blend;
182
183 i915->dirty |= I915_NEW_BLEND;
184 }
185
186
187 static void i915_delete_blend_state(struct pipe_context *pipe, void *blend)
188 {
189 FREE(blend);
190 }
191
192 static void i915_set_blend_color( struct pipe_context *pipe,
193 const struct pipe_blend_color *blend_color )
194 {
195 struct i915_context *i915 = i915_context(pipe);
196 draw_flush(i915->draw);
197
198 i915->blend_color = *blend_color;
199
200 i915->dirty |= I915_NEW_BLEND;
201 }
202
203 static void i915_set_stencil_ref( struct pipe_context *pipe,
204 const struct pipe_stencil_ref *stencil_ref )
205 {
206 struct i915_context *i915 = i915_context(pipe);
207 draw_flush(i915->draw);
208
209 i915->stencil_ref = *stencil_ref;
210
211 i915->dirty |= I915_NEW_DEPTH_STENCIL;
212 }
213
214 static void *
215 i915_create_sampler_state(struct pipe_context *pipe,
216 const struct pipe_sampler_state *sampler)
217 {
218 struct i915_sampler_state *cso = CALLOC_STRUCT( i915_sampler_state );
219 const unsigned ws = sampler->wrap_s;
220 const unsigned wt = sampler->wrap_t;
221 const unsigned wr = sampler->wrap_r;
222 unsigned minFilt, magFilt;
223 unsigned mipFilt;
224
225 cso->templ = sampler;
226
227 mipFilt = translate_mip_filter(sampler->min_mip_filter);
228 minFilt = translate_img_filter( sampler->min_img_filter );
229 magFilt = translate_img_filter( sampler->mag_img_filter );
230
231 if (sampler->max_anisotropy > 1)
232 minFilt = magFilt = FILTER_ANISOTROPIC;
233
234 if (sampler->max_anisotropy > 2) {
235 cso->state[0] |= SS2_MAX_ANISO_4;
236 }
237
238 {
239 int b = (int) (sampler->lod_bias * 16.0);
240 b = CLAMP(b, -256, 255);
241 cso->state[0] |= ((b << SS2_LOD_BIAS_SHIFT) & SS2_LOD_BIAS_MASK);
242 }
243
244 /* Shadow:
245 */
246 if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
247 {
248 cso->state[0] |= (SS2_SHADOW_ENABLE |
249 i915_translate_compare_func(sampler->compare_func));
250
251 minFilt = FILTER_4X4_FLAT;
252 magFilt = FILTER_4X4_FLAT;
253 }
254
255 cso->state[0] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
256 (mipFilt << SS2_MIP_FILTER_SHIFT) |
257 (magFilt << SS2_MAG_FILTER_SHIFT));
258
259 cso->state[1] |=
260 ((translate_wrap_mode(ws) << SS3_TCX_ADDR_MODE_SHIFT) |
261 (translate_wrap_mode(wt) << SS3_TCY_ADDR_MODE_SHIFT) |
262 (translate_wrap_mode(wr) << SS3_TCZ_ADDR_MODE_SHIFT));
263
264 if (sampler->normalized_coords)
265 cso->state[1] |= SS3_NORMALIZED_COORDS;
266
267 {
268 int minlod = (int) (16.0 * sampler->min_lod);
269 int maxlod = (int) (16.0 * sampler->max_lod);
270 minlod = CLAMP(minlod, 0, 16 * 11);
271 maxlod = CLAMP(maxlod, 0, 16 * 11);
272
273 if (minlod > maxlod)
274 maxlod = minlod;
275
276 cso->minlod = minlod;
277 cso->maxlod = maxlod;
278 }
279
280 {
281 ubyte r = float_to_ubyte(sampler->border_color[0]);
282 ubyte g = float_to_ubyte(sampler->border_color[1]);
283 ubyte b = float_to_ubyte(sampler->border_color[2]);
284 ubyte a = float_to_ubyte(sampler->border_color[3]);
285 cso->state[2] = I915PACKCOLOR8888(r, g, b, a);
286 }
287 return cso;
288 }
289
290 static void i915_fixup_bind_sampler_states(struct pipe_context *pipe,
291 unsigned num, void **sampler)
292 {
293 struct i915_context *i915 = i915_context(pipe);
294
295 i915->saved_nr_samplers = num;
296 memcpy(&i915->saved_samplers, sampler, sizeof(void *) * num);
297
298 i915->saved_bind_sampler_states(pipe, num, sampler);
299 }
300
301 static void i915_bind_sampler_states(struct pipe_context *pipe,
302 unsigned num, void **sampler)
303 {
304 struct i915_context *i915 = i915_context(pipe);
305 unsigned i;
306
307 /* Check for no-op */
308 if (num == i915->num_samplers &&
309 !memcmp(i915->sampler, sampler, num * sizeof(void *)))
310 return;
311
312 draw_flush(i915->draw);
313
314 for (i = 0; i < num; ++i)
315 i915->sampler[i] = sampler[i];
316 for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
317 i915->sampler[i] = NULL;
318
319 i915->num_samplers = num;
320
321 i915->dirty |= I915_NEW_SAMPLER;
322 }
323
324 static void i915_delete_sampler_state(struct pipe_context *pipe,
325 void *sampler)
326 {
327 FREE(sampler);
328 }
329
330
331 /** XXX move someday? Or consolidate all these simple state setters
332 * into one file.
333 */
334
335 static void *
336 i915_create_depth_stencil_state(struct pipe_context *pipe,
337 const struct pipe_depth_stencil_alpha_state *depth_stencil)
338 {
339 struct i915_depth_stencil_state *cso = CALLOC_STRUCT( i915_depth_stencil_state );
340
341 {
342 int testmask = depth_stencil->stencil[0].valuemask & 0xff;
343 int writemask = depth_stencil->stencil[0].writemask & 0xff;
344
345 cso->stencil_modes4 |= (_3DSTATE_MODES_4_CMD |
346 ENABLE_STENCIL_TEST_MASK |
347 STENCIL_TEST_MASK(testmask) |
348 ENABLE_STENCIL_WRITE_MASK |
349 STENCIL_WRITE_MASK(writemask));
350 }
351
352 if (depth_stencil->stencil[0].enabled) {
353 int test = i915_translate_compare_func(depth_stencil->stencil[0].func);
354 int fop = i915_translate_stencil_op(depth_stencil->stencil[0].fail_op);
355 int dfop = i915_translate_stencil_op(depth_stencil->stencil[0].zfail_op);
356 int dpop = i915_translate_stencil_op(depth_stencil->stencil[0].zpass_op);
357
358 cso->stencil_LIS5 |= (S5_STENCIL_TEST_ENABLE |
359 S5_STENCIL_WRITE_ENABLE |
360 (test << S5_STENCIL_TEST_FUNC_SHIFT) |
361 (fop << S5_STENCIL_FAIL_SHIFT) |
362 (dfop << S5_STENCIL_PASS_Z_FAIL_SHIFT) |
363 (dpop << S5_STENCIL_PASS_Z_PASS_SHIFT));
364 }
365
366 if (depth_stencil->stencil[1].enabled) {
367 int test = i915_translate_compare_func(depth_stencil->stencil[1].func);
368 int fop = i915_translate_stencil_op(depth_stencil->stencil[1].fail_op);
369 int dfop = i915_translate_stencil_op(depth_stencil->stencil[1].zfail_op);
370 int dpop = i915_translate_stencil_op(depth_stencil->stencil[1].zpass_op);
371 int tmask = depth_stencil->stencil[1].valuemask & 0xff;
372 int wmask = depth_stencil->stencil[1].writemask & 0xff;
373
374 cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
375 BFO_ENABLE_STENCIL_FUNCS |
376 BFO_ENABLE_STENCIL_TWO_SIDE |
377 BFO_ENABLE_STENCIL_REF |
378 BFO_STENCIL_TWO_SIDE |
379 (test << BFO_STENCIL_TEST_SHIFT) |
380 (fop << BFO_STENCIL_FAIL_SHIFT) |
381 (dfop << BFO_STENCIL_PASS_Z_FAIL_SHIFT) |
382 (dpop << BFO_STENCIL_PASS_Z_PASS_SHIFT));
383
384 cso->bfo[1] = (_3DSTATE_BACKFACE_STENCIL_MASKS |
385 BFM_ENABLE_STENCIL_TEST_MASK |
386 BFM_ENABLE_STENCIL_WRITE_MASK |
387 (tmask << BFM_STENCIL_TEST_MASK_SHIFT) |
388 (wmask << BFM_STENCIL_WRITE_MASK_SHIFT));
389 }
390 else {
391 /* This actually disables two-side stencil: The bit set is a
392 * modify-enable bit to indicate we are changing the two-side
393 * setting. Then there is a symbolic zero to show that we are
394 * setting the flag to zero/off.
395 */
396 cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
397 BFO_ENABLE_STENCIL_TWO_SIDE |
398 0);
399 cso->bfo[1] = 0;
400 }
401
402 if (depth_stencil->depth.enabled) {
403 int func = i915_translate_compare_func(depth_stencil->depth.func);
404
405 cso->depth_LIS6 |= (S6_DEPTH_TEST_ENABLE |
406 (func << S6_DEPTH_TEST_FUNC_SHIFT));
407
408 if (depth_stencil->depth.writemask)
409 cso->depth_LIS6 |= S6_DEPTH_WRITE_ENABLE;
410 }
411
412 if (depth_stencil->alpha.enabled) {
413 int test = i915_translate_compare_func(depth_stencil->alpha.func);
414 ubyte refByte = float_to_ubyte(depth_stencil->alpha.ref_value);
415
416 cso->depth_LIS6 |= (S6_ALPHA_TEST_ENABLE |
417 (test << S6_ALPHA_TEST_FUNC_SHIFT) |
418 (((unsigned) refByte) << S6_ALPHA_REF_SHIFT));
419 }
420
421 return cso;
422 }
423
424 static void i915_bind_depth_stencil_state(struct pipe_context *pipe,
425 void *depth_stencil)
426 {
427 struct i915_context *i915 = i915_context(pipe);
428 draw_flush(i915->draw);
429
430 i915->depth_stencil = (const struct i915_depth_stencil_state *)depth_stencil;
431
432 i915->dirty |= I915_NEW_DEPTH_STENCIL;
433 }
434
435 static void i915_delete_depth_stencil_state(struct pipe_context *pipe,
436 void *depth_stencil)
437 {
438 FREE(depth_stencil);
439 }
440
441
442 static void i915_set_scissor_state( struct pipe_context *pipe,
443 const struct pipe_scissor_state *scissor )
444 {
445 struct i915_context *i915 = i915_context(pipe);
446 draw_flush(i915->draw);
447
448 memcpy( &i915->scissor, scissor, sizeof(*scissor) );
449 i915->dirty |= I915_NEW_SCISSOR;
450 }
451
452
453 static void i915_set_polygon_stipple( struct pipe_context *pipe,
454 const struct pipe_poly_stipple *stipple )
455 {
456 }
457
458
459
460 static void *
461 i915_create_fs_state(struct pipe_context *pipe,
462 const struct pipe_shader_state *templ)
463 {
464 struct i915_context *i915 = i915_context(pipe);
465 struct i915_fragment_shader *ifs = CALLOC_STRUCT(i915_fragment_shader);
466 if (!ifs)
467 return NULL;
468
469 ifs->draw_data = draw_create_fragment_shader(i915->draw, templ);
470 ifs->state.tokens = tgsi_dup_tokens(templ->tokens);
471
472 tgsi_scan_shader(templ->tokens, &ifs->info);
473
474 /* The shader's compiled to i915 instructions here */
475 i915_translate_fragment_program(i915, ifs);
476
477 return ifs;
478 }
479
480 static void
481 i915_fixup_bind_fs_state(struct pipe_context *pipe, void *shader)
482 {
483 struct i915_context *i915 = i915_context(pipe);
484 draw_flush(i915->draw);
485
486 i915->saved_fs = shader;
487
488 i915->saved_bind_fs_state(pipe, shader);
489 }
490
491 static void
492 i915_bind_fs_state(struct pipe_context *pipe, void *shader)
493 {
494 struct i915_context *i915 = i915_context(pipe);
495 draw_flush(i915->draw);
496
497 i915->fs = (struct i915_fragment_shader*) shader;
498
499 draw_bind_fragment_shader(i915->draw, (i915->fs ? i915->fs->draw_data : NULL));
500
501 i915->dirty |= I915_NEW_FS;
502 }
503
504 static
505 void i915_delete_fs_state(struct pipe_context *pipe, void *shader)
506 {
507 struct i915_fragment_shader *ifs = (struct i915_fragment_shader *) shader;
508
509 if (ifs->program)
510 FREE(ifs->program);
511 ifs->program_len = 0;
512
513 FREE((struct tgsi_token *)ifs->state.tokens);
514
515 FREE(ifs);
516 }
517
518
519 static void *
520 i915_create_vs_state(struct pipe_context *pipe,
521 const struct pipe_shader_state *templ)
522 {
523 struct i915_context *i915 = i915_context(pipe);
524
525 /* just pass-through to draw module */
526 return draw_create_vertex_shader(i915->draw, templ);
527 }
528
529 static void i915_bind_vs_state(struct pipe_context *pipe, void *shader)
530 {
531 struct i915_context *i915 = i915_context(pipe);
532
533 i915->saved_vs = shader;
534
535 /* just pass-through to draw module */
536 draw_bind_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
537
538 i915->dirty |= I915_NEW_VS;
539 }
540
541 static void i915_delete_vs_state(struct pipe_context *pipe, void *shader)
542 {
543 struct i915_context *i915 = i915_context(pipe);
544
545 /* just pass-through to draw module */
546 draw_delete_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
547 }
548
549 static void i915_set_constant_buffer(struct pipe_context *pipe,
550 uint shader, uint index,
551 struct pipe_resource *buf)
552 {
553 struct i915_context *i915 = i915_context(pipe);
554 unsigned new_num = 0;
555 boolean diff = TRUE;
556
557
558 /* XXX don't support geom shaders now */
559 if (shader == PIPE_SHADER_GEOMETRY)
560 return;
561
562 /* if we have a new buffer compare it with the old one */
563 if (buf) {
564 struct i915_buffer *ibuf = i915_buffer(buf);
565 struct pipe_resource *old_buf = i915->constants[shader];
566 struct i915_buffer *old = old_buf ? i915_buffer(old_buf) : NULL;
567 unsigned old_num = i915->current.num_user_constants[shader];
568
569 new_num = ibuf->b.b.width0 / 4 * sizeof(float);
570
571 if (old_num == new_num) {
572 if (old_num == 0)
573 diff = FALSE;
574 #if 0
575 /* XXX no point in running this code since st/mesa only uses user buffers */
576 /* Can't compare the buffer data since they are userbuffers */
577 else if (old && old->free_on_destroy)
578 diff = memcmp(old->data, ibuf->data, ibuf->b.b.width0);
579 #else
580 (void)old;
581 #endif
582 }
583 } else {
584 diff = i915->current.num_user_constants[shader] != 0;
585 }
586
587 /*
588 * flush before updateing the state.
589 */
590 if (diff && shader == PIPE_SHADER_FRAGMENT)
591 draw_flush(i915->draw);
592
593 pipe_resource_reference(&i915->constants[shader], buf);
594 i915->current.num_user_constants[shader] = new_num;
595
596 if (diff)
597 i915->dirty |= shader == PIPE_SHADER_VERTEX ? I915_NEW_VS_CONSTANTS : I915_NEW_FS_CONSTANTS;
598 }
599
600
601 static void
602 i915_fixup_set_fragment_sampler_views(struct pipe_context *pipe,
603 unsigned num,
604 struct pipe_sampler_view **views)
605 {
606 struct i915_context *i915 = i915_context(pipe);
607 int i;
608
609 for (i = 0; i < num; i++)
610 pipe_sampler_view_reference(&i915->saved_sampler_views[i],
611 views[i]);
612
613 for (i = num; i < i915->saved_nr_sampler_views; i++)
614 pipe_sampler_view_reference(&i915->saved_sampler_views[i],
615 NULL);
616
617 i915->saved_nr_sampler_views = num;
618
619 i915->saved_set_sampler_views(pipe, num, views);
620 }
621
622 static void i915_set_fragment_sampler_views(struct pipe_context *pipe,
623 unsigned num,
624 struct pipe_sampler_view **views)
625 {
626 struct i915_context *i915 = i915_context(pipe);
627 uint i;
628
629 assert(num <= PIPE_MAX_SAMPLERS);
630
631 /* Check for no-op */
632 if (num == i915->num_fragment_sampler_views &&
633 !memcmp(i915->fragment_sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
634 return;
635
636 /* Fixes wrong texture in texobj with VBUF */
637 draw_flush(i915->draw);
638
639 for (i = 0; i < num; i++)
640 pipe_sampler_view_reference(&i915->fragment_sampler_views[i],
641 views[i]);
642
643 for (i = num; i < i915->num_fragment_sampler_views; i++)
644 pipe_sampler_view_reference(&i915->fragment_sampler_views[i],
645 NULL);
646
647 i915->num_fragment_sampler_views = num;
648
649 i915->dirty |= I915_NEW_SAMPLER_VIEW;
650 }
651
652
653 static struct pipe_sampler_view *
654 i915_create_sampler_view(struct pipe_context *pipe,
655 struct pipe_resource *texture,
656 const struct pipe_sampler_view *templ)
657 {
658 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
659
660 if (view) {
661 *view = *templ;
662 view->reference.count = 1;
663 view->texture = NULL;
664 pipe_resource_reference(&view->texture, texture);
665 view->context = pipe;
666 }
667
668 return view;
669 }
670
671
672 static void
673 i915_sampler_view_destroy(struct pipe_context *pipe,
674 struct pipe_sampler_view *view)
675 {
676 pipe_resource_reference(&view->texture, NULL);
677 FREE(view);
678 }
679
680
681 static void i915_set_framebuffer_state(struct pipe_context *pipe,
682 const struct pipe_framebuffer_state *fb)
683 {
684 struct i915_context *i915 = i915_context(pipe);
685 int i;
686
687 draw_flush(i915->draw);
688
689 i915->framebuffer.width = fb->width;
690 i915->framebuffer.height = fb->height;
691 i915->framebuffer.nr_cbufs = fb->nr_cbufs;
692 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
693 pipe_surface_reference(&i915->framebuffer.cbufs[i],
694 i < fb->nr_cbufs ? fb->cbufs[i] : NULL);
695 }
696 pipe_surface_reference(&i915->framebuffer.zsbuf, fb->zsbuf);
697
698 i915->dirty |= I915_NEW_FRAMEBUFFER;
699 }
700
701
702
703 static void i915_set_clip_state( struct pipe_context *pipe,
704 const struct pipe_clip_state *clip )
705 {
706 struct i915_context *i915 = i915_context(pipe);
707 draw_flush(i915->draw);
708
709 i915->saved_clip = *clip;
710
711 draw_set_clip_state(i915->draw, clip);
712
713 i915->dirty |= I915_NEW_CLIP;
714 }
715
716
717
718 /* Called when driver state tracker notices changes to the viewport
719 * matrix:
720 */
721 static void i915_set_viewport_state( struct pipe_context *pipe,
722 const struct pipe_viewport_state *viewport )
723 {
724 struct i915_context *i915 = i915_context(pipe);
725
726 i915->viewport = *viewport; /* struct copy */
727
728 /* pass the viewport info to the draw module */
729 draw_set_viewport_state(i915->draw, &i915->viewport);
730
731 i915->dirty |= I915_NEW_VIEWPORT;
732 }
733
734
735 static void *
736 i915_create_rasterizer_state(struct pipe_context *pipe,
737 const struct pipe_rasterizer_state *rasterizer)
738 {
739 struct i915_rasterizer_state *cso = CALLOC_STRUCT( i915_rasterizer_state );
740
741 cso->templ = *rasterizer;
742 cso->color_interp = rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
743 cso->light_twoside = rasterizer->light_twoside;
744 cso->ds[0].u = _3DSTATE_DEPTH_OFFSET_SCALE;
745 cso->ds[1].f = rasterizer->offset_scale;
746 if (rasterizer->poly_stipple_enable) {
747 cso->st |= ST1_ENABLE;
748 }
749
750 if (rasterizer->scissor)
751 cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT;
752 else
753 cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT;
754
755 switch (rasterizer->cull_face) {
756 case PIPE_FACE_NONE:
757 cso->LIS4 |= S4_CULLMODE_NONE;
758 break;
759 case PIPE_FACE_FRONT:
760 if (rasterizer->front_ccw)
761 cso->LIS4 |= S4_CULLMODE_CCW;
762 else
763 cso->LIS4 |= S4_CULLMODE_CW;
764 break;
765 case PIPE_FACE_BACK:
766 if (rasterizer->front_ccw)
767 cso->LIS4 |= S4_CULLMODE_CW;
768 else
769 cso->LIS4 |= S4_CULLMODE_CCW;
770 break;
771 case PIPE_FACE_FRONT_AND_BACK:
772 cso->LIS4 |= S4_CULLMODE_BOTH;
773 break;
774 }
775
776 {
777 int line_width = CLAMP((int)(rasterizer->line_width * 2), 1, 0xf);
778
779 cso->LIS4 |= line_width << S4_LINE_WIDTH_SHIFT;
780
781 if (rasterizer->line_smooth)
782 cso->LIS4 |= S4_LINE_ANTIALIAS_ENABLE;
783 }
784
785 {
786 int point_size = CLAMP((int) rasterizer->point_size, 1, 0xff);
787
788 cso->LIS4 |= point_size << S4_POINT_WIDTH_SHIFT;
789 }
790
791 if (rasterizer->flatshade) {
792 cso->LIS4 |= (S4_FLATSHADE_ALPHA |
793 S4_FLATSHADE_COLOR |
794 S4_FLATSHADE_SPECULAR);
795 }
796
797 cso->LIS7 = fui( rasterizer->offset_units );
798
799
800 return cso;
801 }
802
803 static void i915_bind_rasterizer_state( struct pipe_context *pipe,
804 void *raster )
805 {
806 struct i915_context *i915 = i915_context(pipe);
807
808 i915->rasterizer = (struct i915_rasterizer_state *)raster;
809
810 /* pass-through to draw module */
811 draw_set_rasterizer_state(i915->draw,
812 (i915->rasterizer ? &(i915->rasterizer->templ) : NULL),
813 raster);
814
815 i915->dirty |= I915_NEW_RASTERIZER;
816 }
817
818 static void i915_delete_rasterizer_state(struct pipe_context *pipe,
819 void *raster)
820 {
821 FREE(raster);
822 }
823
824 static void i915_set_vertex_buffers(struct pipe_context *pipe,
825 unsigned count,
826 const struct pipe_vertex_buffer *buffers)
827 {
828 struct i915_context *i915 = i915_context(pipe);
829 struct draw_context *draw = i915->draw;
830 int i;
831
832 util_copy_vertex_buffers(i915->saved_vertex_buffers,
833 &i915->saved_nr_vertex_buffers,
834 buffers, count);
835 #if 0
836 /* XXX doesn't look like this is needed */
837 /* unmap old */
838 for (i = 0; i < i915->num_vertex_buffers; i++) {
839 draw_set_mapped_vertex_buffer(draw, i, NULL);
840 }
841 #endif
842
843 /* pass-through to draw module */
844 draw_set_vertex_buffers(draw, count, buffers);
845
846 /* map new */
847 for (i = 0; i < count; i++) {
848 void *buf = i915_buffer(buffers[i].buffer)->data;
849 draw_set_mapped_vertex_buffer(draw, i, buf);
850 }
851 }
852
853 static void *
854 i915_create_vertex_elements_state(struct pipe_context *pipe,
855 unsigned count,
856 const struct pipe_vertex_element *attribs)
857 {
858 struct i915_velems_state *velems;
859 assert(count <= PIPE_MAX_ATTRIBS);
860 velems = (struct i915_velems_state *) MALLOC(sizeof(struct i915_velems_state));
861 if (velems) {
862 velems->count = count;
863 memcpy(velems->velem, attribs, sizeof(*attribs) * count);
864 }
865 return velems;
866 }
867
868 static void
869 i915_bind_vertex_elements_state(struct pipe_context *pipe,
870 void *velems)
871 {
872 struct i915_context *i915 = i915_context(pipe);
873 struct i915_velems_state *i915_velems = (struct i915_velems_state *) velems;
874
875 i915->saved_velems = velems;
876
877 /* pass-through to draw module */
878 if (i915_velems) {
879 draw_set_vertex_elements(i915->draw,
880 i915_velems->count, i915_velems->velem);
881 }
882 }
883
884 static void
885 i915_delete_vertex_elements_state(struct pipe_context *pipe, void *velems)
886 {
887 FREE( velems );
888 }
889
890 static void i915_set_index_buffer(struct pipe_context *pipe,
891 const struct pipe_index_buffer *ib)
892 {
893 struct i915_context *i915 = i915_context(pipe);
894
895 if (ib)
896 memcpy(&i915->index_buffer, ib, sizeof(i915->index_buffer));
897 else
898 memset(&i915->index_buffer, 0, sizeof(i915->index_buffer));
899
900 /* pass-through to draw module */
901 draw_set_index_buffer(i915->draw, ib);
902 }
903
904 static void
905 i915_set_sample_mask(struct pipe_context *pipe,
906 unsigned sample_mask)
907 {
908 }
909
910 void
911 i915_init_state_functions( struct i915_context *i915 )
912 {
913 i915->base.create_blend_state = i915_create_blend_state;
914 i915->base.bind_blend_state = i915_bind_blend_state;
915 i915->base.delete_blend_state = i915_delete_blend_state;
916
917 i915->base.create_sampler_state = i915_create_sampler_state;
918 i915->base.bind_fragment_sampler_states = i915_bind_sampler_states;
919 i915->base.delete_sampler_state = i915_delete_sampler_state;
920
921 i915->base.create_depth_stencil_alpha_state = i915_create_depth_stencil_state;
922 i915->base.bind_depth_stencil_alpha_state = i915_bind_depth_stencil_state;
923 i915->base.delete_depth_stencil_alpha_state = i915_delete_depth_stencil_state;
924
925 i915->base.create_rasterizer_state = i915_create_rasterizer_state;
926 i915->base.bind_rasterizer_state = i915_bind_rasterizer_state;
927 i915->base.delete_rasterizer_state = i915_delete_rasterizer_state;
928 i915->base.create_fs_state = i915_create_fs_state;
929 i915->base.bind_fs_state = i915_bind_fs_state;
930 i915->base.delete_fs_state = i915_delete_fs_state;
931 i915->base.create_vs_state = i915_create_vs_state;
932 i915->base.bind_vs_state = i915_bind_vs_state;
933 i915->base.delete_vs_state = i915_delete_vs_state;
934 i915->base.create_vertex_elements_state = i915_create_vertex_elements_state;
935 i915->base.bind_vertex_elements_state = i915_bind_vertex_elements_state;
936 i915->base.delete_vertex_elements_state = i915_delete_vertex_elements_state;
937
938 i915->base.set_blend_color = i915_set_blend_color;
939 i915->base.set_stencil_ref = i915_set_stencil_ref;
940 i915->base.set_clip_state = i915_set_clip_state;
941 i915->base.set_sample_mask = i915_set_sample_mask;
942 i915->base.set_constant_buffer = i915_set_constant_buffer;
943 i915->base.set_framebuffer_state = i915_set_framebuffer_state;
944
945 i915->base.set_polygon_stipple = i915_set_polygon_stipple;
946 i915->base.set_scissor_state = i915_set_scissor_state;
947 i915->base.set_fragment_sampler_views = i915_set_fragment_sampler_views;
948 i915->base.create_sampler_view = i915_create_sampler_view;
949 i915->base.sampler_view_destroy = i915_sampler_view_destroy;
950 i915->base.set_viewport_state = i915_set_viewport_state;
951 i915->base.set_vertex_buffers = i915_set_vertex_buffers;
952 i915->base.set_index_buffer = i915_set_index_buffer;
953 i915->base.redefine_user_buffer = u_default_redefine_user_buffer;
954 }
955
956 void
957 i915_init_fixup_state_functions( struct i915_context *i915 )
958 {
959 i915->saved_bind_fs_state = i915->base.bind_fs_state;
960 i915->base.bind_fs_state = i915_fixup_bind_fs_state;
961 i915->saved_bind_sampler_states = i915->base.bind_fragment_sampler_states;
962 i915->base.bind_fragment_sampler_states = i915_fixup_bind_sampler_states;
963 i915->saved_set_sampler_views = i915->base.set_fragment_sampler_views;
964 i915->base.set_fragment_sampler_views = i915_fixup_set_fragment_sampler_views;
965 }