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