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