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