llvmpipe: move rasterizer to screen instead of setup context
[profile/ivi/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 "tgsi/tgsi_parse.h"
37
38 #include "i915_context.h"
39 #include "i915_reg.h"
40 #include "i915_state_inlines.h"
41 #include "i915_fpc.h"
42 #include "i915_resource.h"
43
44 /* The i915 (and related graphics cores) do not support GL_CLAMP.  The
45  * Intel drivers for "other operating systems" implement GL_CLAMP as
46  * GL_CLAMP_TO_EDGE, so the same is done here.
47  */
48 static unsigned
49 translate_wrap_mode(unsigned wrap)
50 {
51    switch (wrap) {
52    case PIPE_TEX_WRAP_REPEAT:
53       return TEXCOORDMODE_WRAP;
54    case PIPE_TEX_WRAP_CLAMP:
55       return TEXCOORDMODE_CLAMP_EDGE;   /* not quite correct */
56    case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
57       return TEXCOORDMODE_CLAMP_EDGE;
58    case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
59       return TEXCOORDMODE_CLAMP_BORDER;
60    /*         
61    case PIPE_TEX_WRAP_MIRRORED_REPEAT:
62       return TEXCOORDMODE_MIRROR;
63     */
64    default:
65       return TEXCOORDMODE_WRAP;
66    }
67 }
68
69 static unsigned translate_img_filter( unsigned filter )
70 {
71    switch (filter) {
72    case PIPE_TEX_FILTER_NEAREST:
73       return FILTER_NEAREST;
74    case PIPE_TEX_FILTER_LINEAR:
75       return FILTER_LINEAR;
76    default:
77       assert(0);
78       return FILTER_NEAREST;
79    }
80 }
81
82 static unsigned translate_mip_filter( unsigned filter )
83 {
84    switch (filter) {
85    case PIPE_TEX_MIPFILTER_NONE:
86       return MIPFILTER_NONE;
87    case PIPE_TEX_MIPFILTER_NEAREST:
88       return MIPFILTER_NEAREST;
89    case PIPE_TEX_MIPFILTER_LINEAR:
90       return MIPFILTER_LINEAR;
91    default:
92       assert(0);
93       return MIPFILTER_NONE;
94    }
95 }
96
97
98 /* None of this state is actually used for anything yet.
99  */
100 static void *
101 i915_create_blend_state(struct pipe_context *pipe,
102                         const struct pipe_blend_state *blend)
103 {
104    struct i915_blend_state *cso_data = CALLOC_STRUCT( i915_blend_state );
105
106    {
107       unsigned eqRGB  = blend->rt[0].rgb_func;
108       unsigned srcRGB = blend->rt[0].rgb_src_factor;
109       unsigned dstRGB = blend->rt[0].rgb_dst_factor;
110
111       unsigned eqA    = blend->rt[0].alpha_func;
112       unsigned srcA   = blend->rt[0].alpha_src_factor;
113       unsigned dstA   = blend->rt[0].alpha_dst_factor;
114
115       /* Special handling for MIN/MAX filter modes handled at
116        * state_tracker level.
117        */
118
119       if (srcA != srcRGB ||
120           dstA != dstRGB ||
121           eqA != eqRGB) {
122
123          cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
124                           IAB_MODIFY_ENABLE |
125                           IAB_ENABLE |
126                           IAB_MODIFY_FUNC |
127                           IAB_MODIFY_SRC_FACTOR |
128                           IAB_MODIFY_DST_FACTOR |
129                           SRC_ABLND_FACT(i915_translate_blend_factor(srcA)) |
130                           DST_ABLND_FACT(i915_translate_blend_factor(dstA)) |
131                           (i915_translate_blend_func(eqA) << IAB_FUNC_SHIFT));
132       }
133       else {
134          cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
135                           IAB_MODIFY_ENABLE |
136                           0);
137       }
138    }
139
140    cso_data->modes4 |= (_3DSTATE_MODES_4_CMD |
141                         ENABLE_LOGIC_OP_FUNC |
142                         LOGIC_OP_FUNC(i915_translate_logic_op(blend->logicop_func)));
143
144    if (blend->logicop_enable)
145       cso_data->LIS5 |= S5_LOGICOP_ENABLE;
146
147    if (blend->dither)
148       cso_data->LIS5 |= S5_COLOR_DITHER_ENABLE;
149
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_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[0]);
283       ubyte g = float_to_ubyte(sampler->border_color[1]);
284       ubyte b = float_to_ubyte(sampler->border_color[2]);
285       ubyte a = float_to_ubyte(sampler->border_color[3]);
286       cso->state[2] = I915PACKCOLOR8888(r, g, b, a);
287    }
288    return cso;
289 }
290
291 static void i915_bind_sampler_states(struct pipe_context *pipe,
292                                      unsigned num, void **sampler)
293 {
294    struct i915_context *i915 = i915_context(pipe);
295    unsigned i;
296
297    assert(num <= PIPE_MAX_SAMPLERS);
298
299    /* Check for no-op */
300    if (num == i915->num_samplers &&
301        !memcmp(i915->sampler, sampler, num * sizeof(void *)))
302       return;
303
304    draw_flush(i915->draw);
305
306    for (i = 0; i < num; ++i)
307       i915->sampler[i] = sampler[i];
308    for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
309       i915->sampler[i] = NULL;
310
311    i915->num_samplers = num;
312
313    i915->dirty |= I915_NEW_SAMPLER;
314 }
315
316 static void i915_delete_sampler_state(struct pipe_context *pipe,
317                                       void *sampler)
318 {
319    FREE(sampler);
320 }
321
322
323 /** XXX move someday?  Or consolidate all these simple state setters
324  * into one file.
325  */
326
327 static void *
328 i915_create_depth_stencil_state(struct pipe_context *pipe,
329                                 const struct pipe_depth_stencil_alpha_state *depth_stencil)
330 {
331    struct i915_depth_stencil_state *cso = CALLOC_STRUCT( i915_depth_stencil_state );
332
333    {
334       int testmask = depth_stencil->stencil[0].valuemask & 0xff;
335       int writemask = depth_stencil->stencil[0].writemask & 0xff;
336
337       cso->stencil_modes4 |= (_3DSTATE_MODES_4_CMD |
338                               ENABLE_STENCIL_TEST_MASK |
339                               STENCIL_TEST_MASK(testmask) |
340                               ENABLE_STENCIL_WRITE_MASK |
341                               STENCIL_WRITE_MASK(writemask));
342    }
343
344    if (depth_stencil->stencil[0].enabled) {
345       int test = i915_translate_compare_func(depth_stencil->stencil[0].func);
346       int fop  = i915_translate_stencil_op(depth_stencil->stencil[0].fail_op);
347       int dfop = i915_translate_stencil_op(depth_stencil->stencil[0].zfail_op);
348       int dpop = i915_translate_stencil_op(depth_stencil->stencil[0].zpass_op);
349
350       cso->stencil_LIS5 |= (S5_STENCIL_TEST_ENABLE |
351                             S5_STENCIL_WRITE_ENABLE |
352                             (test << S5_STENCIL_TEST_FUNC_SHIFT) |
353                             (fop  << S5_STENCIL_FAIL_SHIFT) |
354                             (dfop << S5_STENCIL_PASS_Z_FAIL_SHIFT) |
355                             (dpop << S5_STENCIL_PASS_Z_PASS_SHIFT));
356    }
357
358    if (depth_stencil->stencil[1].enabled) {
359       int test  = i915_translate_compare_func(depth_stencil->stencil[1].func);
360       int fop   = i915_translate_stencil_op(depth_stencil->stencil[1].fail_op);
361       int dfop  = i915_translate_stencil_op(depth_stencil->stencil[1].zfail_op);
362       int dpop  = i915_translate_stencil_op(depth_stencil->stencil[1].zpass_op);
363       int tmask = depth_stencil->stencil[1].valuemask & 0xff;
364       int wmask = depth_stencil->stencil[1].writemask & 0xff;
365
366       cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
367                      BFO_ENABLE_STENCIL_FUNCS |
368                      BFO_ENABLE_STENCIL_TWO_SIDE |
369                      BFO_ENABLE_STENCIL_REF |
370                      BFO_STENCIL_TWO_SIDE |
371                      (test << BFO_STENCIL_TEST_SHIFT) |
372                      (fop  << BFO_STENCIL_FAIL_SHIFT) |
373                      (dfop << BFO_STENCIL_PASS_Z_FAIL_SHIFT) |
374                      (dpop << BFO_STENCIL_PASS_Z_PASS_SHIFT));
375
376       cso->bfo[1] = (_3DSTATE_BACKFACE_STENCIL_MASKS |
377                      BFM_ENABLE_STENCIL_TEST_MASK |
378                      BFM_ENABLE_STENCIL_WRITE_MASK |
379                      (tmask << BFM_STENCIL_TEST_MASK_SHIFT) |
380                      (wmask << BFM_STENCIL_WRITE_MASK_SHIFT));
381    }
382    else {
383       /* This actually disables two-side stencil: The bit set is a
384        * modify-enable bit to indicate we are changing the two-side
385        * setting.  Then there is a symbolic zero to show that we are
386        * setting the flag to zero/off.
387        */
388       cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
389                      BFO_ENABLE_STENCIL_TWO_SIDE |
390                      0);
391       cso->bfo[1] = 0;
392    }
393
394    if (depth_stencil->depth.enabled) {
395       int func = i915_translate_compare_func(depth_stencil->depth.func);
396
397       cso->depth_LIS6 |= (S6_DEPTH_TEST_ENABLE |
398                           (func << S6_DEPTH_TEST_FUNC_SHIFT));
399
400       if (depth_stencil->depth.writemask)
401          cso->depth_LIS6 |= S6_DEPTH_WRITE_ENABLE;
402    }
403
404    if (depth_stencil->alpha.enabled) {
405       int test = i915_translate_compare_func(depth_stencil->alpha.func);
406       ubyte refByte = float_to_ubyte(depth_stencil->alpha.ref_value);
407
408       cso->depth_LIS6 |= (S6_ALPHA_TEST_ENABLE |
409                           (test << S6_ALPHA_TEST_FUNC_SHIFT) |
410                           (((unsigned) refByte) << S6_ALPHA_REF_SHIFT));
411    }
412
413    return cso;
414 }
415
416 static void i915_bind_depth_stencil_state(struct pipe_context *pipe,
417                                           void *depth_stencil)
418 {
419    struct i915_context *i915 = i915_context(pipe);
420    draw_flush(i915->draw);
421
422    i915->depth_stencil = (const struct i915_depth_stencil_state *)depth_stencil;
423
424    i915->dirty |= I915_NEW_DEPTH_STENCIL;
425 }
426
427 static void i915_delete_depth_stencil_state(struct pipe_context *pipe,
428                                             void *depth_stencil)
429 {
430    FREE(depth_stencil);
431 }
432
433
434 static void i915_set_scissor_state( struct pipe_context *pipe,
435                                  const struct pipe_scissor_state *scissor )
436 {
437    struct i915_context *i915 = i915_context(pipe);
438    draw_flush(i915->draw);
439
440    memcpy( &i915->scissor, scissor, sizeof(*scissor) );
441    i915->dirty |= I915_NEW_SCISSOR;
442 }
443
444
445 static void i915_set_polygon_stipple( struct pipe_context *pipe,
446                                    const struct pipe_poly_stipple *stipple )
447 {
448 }
449
450
451
452 static void *
453 i915_create_fs_state(struct pipe_context *pipe,
454                      const struct pipe_shader_state *templ)
455 {
456    struct i915_context *i915 = i915_context(pipe);
457    struct i915_fragment_shader *ifs = CALLOC_STRUCT(i915_fragment_shader);
458    if (!ifs)
459       return NULL;
460
461    ifs->state.tokens = tgsi_dup_tokens(templ->tokens);
462
463    tgsi_scan_shader(templ->tokens, &ifs->info);
464
465    /* The shader's compiled to i915 instructions here */
466    i915_translate_fragment_program(i915, ifs);
467
468    return ifs;
469 }
470
471 static void
472 i915_bind_fs_state(struct pipe_context *pipe, void *shader)
473 {
474    struct i915_context *i915 = i915_context(pipe);
475    draw_flush(i915->draw);
476
477    i915->fs = (struct i915_fragment_shader*) shader;
478
479    i915->dirty |= I915_NEW_FS;
480 }
481
482 static
483 void i915_delete_fs_state(struct pipe_context *pipe, void *shader)
484 {
485    struct i915_fragment_shader *ifs = (struct i915_fragment_shader *) shader;
486
487    if (ifs->program)
488       FREE(ifs->program);
489    ifs->program_len = 0;
490
491    FREE((struct tgsi_token *)ifs->state.tokens);
492
493    FREE(ifs);
494 }
495
496
497 static void *
498 i915_create_vs_state(struct pipe_context *pipe,
499                      const struct pipe_shader_state *templ)
500 {
501    struct i915_context *i915 = i915_context(pipe);
502
503    /* just pass-through to draw module */
504    return draw_create_vertex_shader(i915->draw, templ);
505 }
506
507 static void i915_bind_vs_state(struct pipe_context *pipe, void *shader)
508 {
509    struct i915_context *i915 = i915_context(pipe);
510
511    /* just pass-through to draw module */
512    draw_bind_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
513
514    i915->dirty |= I915_NEW_VS;
515 }
516
517 static void i915_delete_vs_state(struct pipe_context *pipe, void *shader)
518 {
519    struct i915_context *i915 = i915_context(pipe);
520
521    /* just pass-through to draw module */
522    draw_delete_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
523 }
524
525 static void i915_set_constant_buffer(struct pipe_context *pipe,
526                                      uint shader, uint index,
527                                      struct pipe_resource *buf)
528 {
529    struct i915_context *i915 = i915_context(pipe);
530    draw_flush(i915->draw);
531
532    assert(shader < PIPE_SHADER_TYPES);
533    assert(index == 0);
534
535    /* Make a copy of shader constants.
536     * During fragment program translation we may add additional
537     * constants to the array.
538     *
539     * We want to consider the situation where some user constants
540     * (ex: a material color) may change frequently but the shader program
541     * stays the same.  In that case we should only be updating the first
542     * N constants, leaving any extras from shader translation alone.
543     */
544    if (buf) {
545       struct i915_buffer *ir = i915_buffer(buf);
546       memcpy(i915->current.constants[shader], ir->data, ir->b.b.width0);
547       i915->current.num_user_constants[shader] = (ir->b.b.width0 /
548                                                   4 * sizeof(float));
549    }
550    else {
551       i915->current.num_user_constants[shader] = 0;
552    }
553
554
555    i915->dirty |= I915_NEW_CONSTANTS;
556 }
557
558
559 static void i915_set_fragment_sampler_views(struct pipe_context *pipe,
560                                             unsigned num,
561                                             struct pipe_sampler_view **views)
562 {
563    struct i915_context *i915 = i915_context(pipe);
564    uint i;
565
566    assert(num <= PIPE_MAX_SAMPLERS);
567
568    /* Check for no-op */
569    if (num == i915->num_fragment_sampler_views &&
570        !memcmp(i915->fragment_sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
571       return;
572
573    /* Fixes wrong texture in texobj with VBUF */
574    draw_flush(i915->draw);
575
576    for (i = 0; i < num; i++)
577       pipe_sampler_view_reference(&i915->fragment_sampler_views[i],
578                                   views[i]);
579
580    for (i = num; i < i915->num_fragment_sampler_views; i++)
581       pipe_sampler_view_reference(&i915->fragment_sampler_views[i],
582                                   NULL);
583
584    i915->num_fragment_sampler_views = num;
585
586    i915->dirty |= I915_NEW_SAMPLER_VIEW;
587 }
588
589
590 static struct pipe_sampler_view *
591 i915_create_sampler_view(struct pipe_context *pipe,
592                          struct pipe_resource *texture,
593                          const struct pipe_sampler_view *templ)
594 {
595    struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
596
597    if (view) {
598       *view = *templ;
599       view->reference.count = 1;
600       view->texture = NULL;
601       pipe_resource_reference(&view->texture, texture);
602       view->context = pipe;
603    }
604
605    return view;
606 }
607
608
609 static void
610 i915_sampler_view_destroy(struct pipe_context *pipe,
611                           struct pipe_sampler_view *view)
612 {
613    pipe_resource_reference(&view->texture, NULL);
614    FREE(view);
615 }
616
617
618 static void i915_set_framebuffer_state(struct pipe_context *pipe,
619                                        const struct pipe_framebuffer_state *fb)
620 {
621    struct i915_context *i915 = i915_context(pipe);
622    int i;
623
624    draw_flush(i915->draw);
625
626    i915->framebuffer.width = fb->width;
627    i915->framebuffer.height = fb->height;
628    i915->framebuffer.nr_cbufs = fb->nr_cbufs;
629    for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
630       pipe_surface_reference(&i915->framebuffer.cbufs[i], fb->cbufs[i]);
631    }
632    pipe_surface_reference(&i915->framebuffer.zsbuf, fb->zsbuf);
633
634    i915->dirty |= I915_NEW_FRAMEBUFFER;
635 }
636
637
638
639 static void i915_set_clip_state( struct pipe_context *pipe,
640                              const struct pipe_clip_state *clip )
641 {
642    struct i915_context *i915 = i915_context(pipe);
643    draw_flush(i915->draw);
644
645    draw_set_clip_state(i915->draw, clip);
646
647    i915->dirty |= I915_NEW_CLIP;
648 }
649
650
651
652 /* Called when driver state tracker notices changes to the viewport
653  * matrix:
654  */
655 static void i915_set_viewport_state( struct pipe_context *pipe,
656                                      const struct pipe_viewport_state *viewport )
657 {
658    struct i915_context *i915 = i915_context(pipe);
659
660    i915->viewport = *viewport; /* struct copy */
661
662    /* pass the viewport info to the draw module */
663    draw_set_viewport_state(i915->draw, &i915->viewport);
664
665    i915->dirty |= I915_NEW_VIEWPORT;
666 }
667
668
669 static void *
670 i915_create_rasterizer_state(struct pipe_context *pipe,
671                              const struct pipe_rasterizer_state *rasterizer)
672 {
673    struct i915_rasterizer_state *cso = CALLOC_STRUCT( i915_rasterizer_state );
674
675    cso->templ = rasterizer;
676    cso->color_interp = rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
677    cso->light_twoside = rasterizer->light_twoside;
678    cso->ds[0].u = _3DSTATE_DEPTH_OFFSET_SCALE;
679    cso->ds[1].f = rasterizer->offset_scale;
680    if (rasterizer->poly_stipple_enable) {
681       cso->st |= ST1_ENABLE;
682    }
683
684    if (rasterizer->scissor)
685       cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT;
686    else
687       cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT;
688
689    switch (rasterizer->cull_face) {
690    case PIPE_FACE_NONE:
691       cso->LIS4 |= S4_CULLMODE_NONE;
692       break;
693    case PIPE_FACE_FRONT:
694       if (rasterizer->front_ccw)
695          cso->LIS4 |= S4_CULLMODE_CCW;
696       else 
697          cso->LIS4 |= S4_CULLMODE_CW;
698       break;
699    case PIPE_FACE_BACK:
700       if (rasterizer->front_ccw)
701          cso->LIS4 |= S4_CULLMODE_CW;
702       else 
703          cso->LIS4 |= S4_CULLMODE_CCW;
704       break;
705    case PIPE_FACE_FRONT_AND_BACK:
706       cso->LIS4 |= S4_CULLMODE_BOTH;
707       break;
708    }
709
710    {
711       int line_width = CLAMP((int)(rasterizer->line_width * 2), 1, 0xf);
712
713       cso->LIS4 |= line_width << S4_LINE_WIDTH_SHIFT;
714
715       if (rasterizer->line_smooth)
716          cso->LIS4 |= S4_LINE_ANTIALIAS_ENABLE;
717    }
718
719    {
720       int point_size = CLAMP((int) rasterizer->point_size, 1, 0xff);
721
722       cso->LIS4 |= point_size << S4_POINT_WIDTH_SHIFT;
723    }
724
725    if (rasterizer->flatshade) {
726       cso->LIS4 |= (S4_FLATSHADE_ALPHA |
727                     S4_FLATSHADE_COLOR |
728                     S4_FLATSHADE_SPECULAR);
729    }
730
731    cso->LIS7 = fui( rasterizer->offset_units );
732
733
734    return cso;
735 }
736
737 static void i915_bind_rasterizer_state( struct pipe_context *pipe,
738                                         void *raster )
739 {
740    struct i915_context *i915 = i915_context(pipe);
741
742    i915->rasterizer = (struct i915_rasterizer_state *)raster;
743
744    /* pass-through to draw module */
745    draw_set_rasterizer_state(i915->draw,
746                            (i915->rasterizer ? i915->rasterizer->templ : NULL),
747                            raster);
748
749    i915->dirty |= I915_NEW_RASTERIZER;
750 }
751
752 static void i915_delete_rasterizer_state(struct pipe_context *pipe,
753                                          void *raster)
754 {
755    FREE(raster);
756 }
757
758 static void i915_set_vertex_buffers(struct pipe_context *pipe,
759                                     unsigned count,
760                                     const struct pipe_vertex_buffer *buffers)
761 {
762    struct i915_context *i915 = i915_context(pipe);
763    /* Because we change state before the draw_set_vertex_buffers call
764     * we need a flush here, just to be sure.
765     */
766    draw_flush(i915->draw);
767
768    memcpy(i915->vertex_buffer, buffers, count * sizeof(buffers[0]));
769    i915->num_vertex_buffers = count;
770
771    /* pass-through to draw module */
772    draw_set_vertex_buffers(i915->draw, count, buffers);
773 }
774
775 static void *
776 i915_create_vertex_elements_state(struct pipe_context *pipe,
777                                   unsigned count,
778                                   const struct pipe_vertex_element *attribs)
779 {
780    struct i915_velems_state *velems;
781    assert(count <= PIPE_MAX_ATTRIBS);
782    velems = (struct i915_velems_state *) MALLOC(sizeof(struct i915_velems_state));
783    if (velems) {
784       velems->count = count;
785       memcpy(velems->velem, attribs, sizeof(*attribs) * count);
786    }
787    return velems;
788 }
789
790 static void
791 i915_bind_vertex_elements_state(struct pipe_context *pipe,
792                                 void *velems)
793 {
794    struct i915_context *i915 = i915_context(pipe);
795    struct i915_velems_state *i915_velems = (struct i915_velems_state *) velems;
796
797    /* Because we change state before the draw_set_vertex_buffers call
798     * we need a flush here, just to be sure.
799     */
800    draw_flush(i915->draw);
801
802    /* pass-through to draw module */
803    if (i915_velems) {
804       draw_set_vertex_elements(i915->draw,
805             i915_velems->count, i915_velems->velem);
806    }
807 }
808
809 static void
810 i915_delete_vertex_elements_state(struct pipe_context *pipe, void *velems)
811 {
812    FREE( velems );
813 }
814
815 static void
816 i915_set_sample_mask(struct pipe_context *pipe,
817                      unsigned sample_mask)
818 {
819 }
820
821 void
822 i915_init_state_functions( struct i915_context *i915 )
823 {
824    i915->base.create_blend_state = i915_create_blend_state;
825    i915->base.bind_blend_state = i915_bind_blend_state;
826    i915->base.delete_blend_state = i915_delete_blend_state;
827
828    i915->base.create_sampler_state = i915_create_sampler_state;
829    i915->base.bind_fragment_sampler_states = i915_bind_sampler_states;
830    i915->base.delete_sampler_state = i915_delete_sampler_state;
831
832    i915->base.create_depth_stencil_alpha_state = i915_create_depth_stencil_state;
833    i915->base.bind_depth_stencil_alpha_state = i915_bind_depth_stencil_state;
834    i915->base.delete_depth_stencil_alpha_state = i915_delete_depth_stencil_state;
835
836    i915->base.create_rasterizer_state = i915_create_rasterizer_state;
837    i915->base.bind_rasterizer_state = i915_bind_rasterizer_state;
838    i915->base.delete_rasterizer_state = i915_delete_rasterizer_state;
839    i915->base.create_fs_state = i915_create_fs_state;
840    i915->base.bind_fs_state = i915_bind_fs_state;
841    i915->base.delete_fs_state = i915_delete_fs_state;
842    i915->base.create_vs_state = i915_create_vs_state;
843    i915->base.bind_vs_state = i915_bind_vs_state;
844    i915->base.delete_vs_state = i915_delete_vs_state;
845    i915->base.create_vertex_elements_state = i915_create_vertex_elements_state;
846    i915->base.bind_vertex_elements_state = i915_bind_vertex_elements_state;
847    i915->base.delete_vertex_elements_state = i915_delete_vertex_elements_state;
848
849    i915->base.set_blend_color = i915_set_blend_color;
850    i915->base.set_stencil_ref = i915_set_stencil_ref;
851    i915->base.set_clip_state = i915_set_clip_state;
852    i915->base.set_sample_mask = i915_set_sample_mask;
853    i915->base.set_constant_buffer = i915_set_constant_buffer;
854    i915->base.set_framebuffer_state = i915_set_framebuffer_state;
855
856    i915->base.set_polygon_stipple = i915_set_polygon_stipple;
857    i915->base.set_scissor_state = i915_set_scissor_state;
858    i915->base.set_fragment_sampler_views = i915_set_fragment_sampler_views;
859    i915->base.create_sampler_view = i915_create_sampler_view;
860    i915->base.sampler_view_destroy = i915_sampler_view_destroy;
861    i915->base.set_viewport_state = i915_set_viewport_state;
862    i915->base.set_vertex_buffers = i915_set_vertex_buffers;
863 }