i915_state.c revision 25bb04a1ee9b3f28bfa6e60d7ce71ff23726c5b6
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 "pipe/internal/p_winsys_screen.h"
34#include "pipe/p_inlines.h"
35#include "util/u_math.h"
36#include "util/u_memory.h"
37#include "tgsi/tgsi_parse.h"
38
39#include "i915_context.h"
40#include "i915_reg.h"
41#include "i915_state.h"
42#include "i915_state_inlines.h"
43#include "i915_fpc.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 */
49static unsigned
50translate_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/*
62   case PIPE_TEX_WRAP_MIRRORED_REPEAT:
63      return TEXCOORDMODE_MIRROR;
64*/
65   default:
66      return TEXCOORDMODE_WRAP;
67   }
68}
69
70static unsigned translate_img_filter( unsigned filter )
71{
72   switch (filter) {
73   case PIPE_TEX_FILTER_NEAREST:
74      return FILTER_NEAREST;
75   case PIPE_TEX_FILTER_LINEAR:
76      return FILTER_LINEAR;
77   case PIPE_TEX_FILTER_ANISO:
78      return FILTER_ANISOTROPIC;
79   default:
80      assert(0);
81      return FILTER_NEAREST;
82   }
83}
84
85static unsigned translate_mip_filter( unsigned filter )
86{
87   switch (filter) {
88   case PIPE_TEX_MIPFILTER_NONE:
89      return MIPFILTER_NONE;
90   case PIPE_TEX_MIPFILTER_NEAREST:
91      return MIPFILTER_NEAREST;
92   case PIPE_TEX_MIPFILTER_LINEAR:
93      return MIPFILTER_LINEAR;
94   default:
95      assert(0);
96      return MIPFILTER_NONE;
97   }
98}
99
100
101/* None of this state is actually used for anything yet.
102 */
103static void *
104i915_create_blend_state(struct pipe_context *pipe,
105                        const struct pipe_blend_state *blend)
106{
107   struct i915_blend_state *cso_data = CALLOC_STRUCT( i915_blend_state );
108
109   {
110      unsigned eqRGB  = blend->rgb_func;
111      unsigned srcRGB = blend->rgb_src_factor;
112      unsigned dstRGB = blend->rgb_dst_factor;
113
114      unsigned eqA    = blend->alpha_func;
115      unsigned srcA   = blend->alpha_src_factor;
116      unsigned dstA   = blend->alpha_dst_factor;
117
118      /* Special handling for MIN/MAX filter modes handled at
119       * state_tracker level.
120       */
121
122      if (srcA != srcRGB ||
123	  dstA != dstRGB ||
124	  eqA != eqRGB) {
125
126	 cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
127                          IAB_MODIFY_ENABLE |
128                          IAB_ENABLE |
129                          IAB_MODIFY_FUNC |
130                          IAB_MODIFY_SRC_FACTOR |
131                          IAB_MODIFY_DST_FACTOR |
132                          SRC_ABLND_FACT(i915_translate_blend_factor(srcA)) |
133                          DST_ABLND_FACT(i915_translate_blend_factor(dstA)) |
134                          (i915_translate_blend_func(eqA) << IAB_FUNC_SHIFT));
135      }
136      else {
137	 cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
138                          IAB_MODIFY_ENABLE |
139                          0);
140      }
141   }
142
143   cso_data->modes4 |= (_3DSTATE_MODES_4_CMD |
144                        ENABLE_LOGIC_OP_FUNC |
145                        LOGIC_OP_FUNC(i915_translate_logic_op(blend->logicop_func)));
146
147   if (blend->logicop_enable)
148      cso_data->LIS5 |= S5_LOGICOP_ENABLE;
149
150   if (blend->dither)
151      cso_data->LIS5 |= S5_COLOR_DITHER_ENABLE;
152
153   if ((blend->colormask & PIPE_MASK_R) == 0)
154      cso_data->LIS5 |= S5_WRITEDISABLE_RED;
155
156   if ((blend->colormask & PIPE_MASK_G) == 0)
157      cso_data->LIS5 |= S5_WRITEDISABLE_GREEN;
158
159   if ((blend->colormask & PIPE_MASK_B) == 0)
160      cso_data->LIS5 |= S5_WRITEDISABLE_BLUE;
161
162   if ((blend->colormask & PIPE_MASK_A) == 0)
163      cso_data->LIS5 |= S5_WRITEDISABLE_ALPHA;
164
165   if (blend->blend_enable) {
166      unsigned funcRGB = blend->rgb_func;
167      unsigned srcRGB  = blend->rgb_src_factor;
168      unsigned dstRGB  = blend->rgb_dst_factor;
169
170      cso_data->LIS6 |= (S6_CBUF_BLEND_ENABLE |
171                         SRC_BLND_FACT(i915_translate_blend_factor(srcRGB)) |
172                         DST_BLND_FACT(i915_translate_blend_factor(dstRGB)) |
173                         (i915_translate_blend_func(funcRGB) << S6_CBUF_BLEND_FUNC_SHIFT));
174   }
175
176   return cso_data;
177}
178
179static void i915_bind_blend_state(struct pipe_context *pipe,
180                                  void *blend)
181{
182   struct i915_context *i915 = i915_context(pipe);
183   draw_flush(i915->draw);
184
185   i915->blend = (struct i915_blend_state*)blend;
186
187   i915->dirty |= I915_NEW_BLEND;
188}
189
190
191static void i915_delete_blend_state(struct pipe_context *pipe, void *blend)
192{
193   FREE(blend);
194}
195
196static void i915_set_blend_color( struct pipe_context *pipe,
197			     const struct pipe_blend_color *blend_color )
198{
199   struct i915_context *i915 = i915_context(pipe);
200   draw_flush(i915->draw);
201
202   i915->blend_color = *blend_color;
203
204   i915->dirty |= I915_NEW_BLEND;
205}
206
207static void *
208i915_create_sampler_state(struct pipe_context *pipe,
209                          const struct pipe_sampler_state *sampler)
210{
211   struct i915_sampler_state *cso = CALLOC_STRUCT( i915_sampler_state );
212   const unsigned ws = sampler->wrap_s;
213   const unsigned wt = sampler->wrap_t;
214   const unsigned wr = sampler->wrap_r;
215   unsigned minFilt, magFilt;
216   unsigned mipFilt;
217
218   cso->templ = sampler;
219
220   mipFilt = translate_mip_filter(sampler->min_mip_filter);
221   minFilt = translate_img_filter( sampler->min_img_filter );
222   magFilt = translate_img_filter( sampler->mag_img_filter );
223
224   if (sampler->max_anisotropy > 2.0) {
225      cso->state[0] |= SS2_MAX_ANISO_4;
226   }
227
228   {
229      int b = (int) (sampler->lod_bias * 16.0);
230      b = CLAMP(b, -256, 255);
231      cso->state[0] |= ((b << SS2_LOD_BIAS_SHIFT) & SS2_LOD_BIAS_MASK);
232   }
233
234   /* Shadow:
235    */
236   if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
237   {
238      cso->state[0] |= (SS2_SHADOW_ENABLE |
239                        i915_translate_compare_func(sampler->compare_func));
240
241      minFilt = FILTER_4X4_FLAT;
242      magFilt = FILTER_4X4_FLAT;
243   }
244
245   cso->state[0] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
246                     (mipFilt << SS2_MIP_FILTER_SHIFT) |
247                     (magFilt << SS2_MAG_FILTER_SHIFT));
248
249   cso->state[1] |=
250      ((translate_wrap_mode(ws) << SS3_TCX_ADDR_MODE_SHIFT) |
251       (translate_wrap_mode(wt) << SS3_TCY_ADDR_MODE_SHIFT) |
252       (translate_wrap_mode(wr) << SS3_TCZ_ADDR_MODE_SHIFT));
253
254   if (sampler->normalized_coords)
255      cso->state[1] |= SS3_NORMALIZED_COORDS;
256
257   {
258      int minlod = (int) (16.0 * sampler->min_lod);
259      int maxlod = (int) (16.0 * sampler->max_lod);
260      minlod = CLAMP(minlod, 0, 16 * 11);
261      maxlod = CLAMP(maxlod, 0, 16 * 11);
262
263      if (minlod > maxlod)
264	 maxlod = minlod;
265
266      cso->minlod = minlod;
267      cso->maxlod = maxlod;
268   }
269
270   {
271      ubyte r = float_to_ubyte(sampler->border_color[0]);
272      ubyte g = float_to_ubyte(sampler->border_color[1]);
273      ubyte b = float_to_ubyte(sampler->border_color[2]);
274      ubyte a = float_to_ubyte(sampler->border_color[3]);
275      cso->state[2] = I915PACKCOLOR8888(r, g, b, a);
276   }
277   return cso;
278}
279
280static void i915_bind_sampler_states(struct pipe_context *pipe,
281                                     unsigned num, void **sampler)
282{
283   struct i915_context *i915 = i915_context(pipe);
284   unsigned i;
285
286   assert(num <= PIPE_MAX_SAMPLERS);
287
288   /* Check for no-op */
289   if (num == i915->num_samplers &&
290       !memcmp(i915->sampler, sampler, num * sizeof(void *)))
291      return;
292
293   draw_flush(i915->draw);
294
295   for (i = 0; i < num; ++i)
296      i915->sampler[i] = sampler[i];
297   for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
298      i915->sampler[i] = NULL;
299
300   i915->num_samplers = num;
301
302   i915->dirty |= I915_NEW_SAMPLER;
303}
304
305static void i915_delete_sampler_state(struct pipe_context *pipe,
306                                      void *sampler)
307{
308   FREE(sampler);
309}
310
311
312/** XXX move someday?  Or consolidate all these simple state setters
313 * into one file.
314 */
315
316static void *
317i915_create_depth_stencil_state(struct pipe_context *pipe,
318				const struct pipe_depth_stencil_alpha_state *depth_stencil)
319{
320   struct i915_depth_stencil_state *cso = CALLOC_STRUCT( i915_depth_stencil_state );
321
322   {
323      int testmask = depth_stencil->stencil[0].valuemask & 0xff;
324      int writemask = depth_stencil->stencil[0].writemask & 0xff;
325
326      cso->stencil_modes4 |= (_3DSTATE_MODES_4_CMD |
327                              ENABLE_STENCIL_TEST_MASK |
328                              STENCIL_TEST_MASK(testmask) |
329                              ENABLE_STENCIL_WRITE_MASK |
330                              STENCIL_WRITE_MASK(writemask));
331   }
332
333   if (depth_stencil->stencil[0].enabled) {
334      int test = i915_translate_compare_func(depth_stencil->stencil[0].func);
335      int fop  = i915_translate_stencil_op(depth_stencil->stencil[0].fail_op);
336      int dfop = i915_translate_stencil_op(depth_stencil->stencil[0].zfail_op);
337      int dpop = i915_translate_stencil_op(depth_stencil->stencil[0].zpass_op);
338      int ref  = depth_stencil->stencil[0].ref_value & 0xff;
339
340      cso->stencil_LIS5 |= (S5_STENCIL_TEST_ENABLE |
341                            S5_STENCIL_WRITE_ENABLE |
342                            (ref  << S5_STENCIL_REF_SHIFT) |
343                            (test << S5_STENCIL_TEST_FUNC_SHIFT) |
344                            (fop  << S5_STENCIL_FAIL_SHIFT) |
345                            (dfop << S5_STENCIL_PASS_Z_FAIL_SHIFT) |
346                            (dpop << S5_STENCIL_PASS_Z_PASS_SHIFT));
347   }
348
349   if (depth_stencil->stencil[1].enabled) {
350      int test  = i915_translate_compare_func(depth_stencil->stencil[1].func);
351      int fop   = i915_translate_stencil_op(depth_stencil->stencil[1].fail_op);
352      int dfop  = i915_translate_stencil_op(depth_stencil->stencil[1].zfail_op);
353      int dpop  = i915_translate_stencil_op(depth_stencil->stencil[1].zpass_op);
354      int ref   = depth_stencil->stencil[1].ref_value & 0xff;
355      int tmask = depth_stencil->stencil[1].valuemask & 0xff;
356      int wmask = depth_stencil->stencil[1].writemask & 0xff;
357
358      cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
359                     BFO_ENABLE_STENCIL_FUNCS |
360                     BFO_ENABLE_STENCIL_TWO_SIDE |
361                     BFO_ENABLE_STENCIL_REF |
362                     BFO_STENCIL_TWO_SIDE |
363                     (ref  << BFO_STENCIL_REF_SHIFT) |
364                     (test << BFO_STENCIL_TEST_SHIFT) |
365                     (fop  << BFO_STENCIL_FAIL_SHIFT) |
366                     (dfop << BFO_STENCIL_PASS_Z_FAIL_SHIFT) |
367                     (dpop << BFO_STENCIL_PASS_Z_PASS_SHIFT));
368
369      cso->bfo[1] = (_3DSTATE_BACKFACE_STENCIL_MASKS |
370                     BFM_ENABLE_STENCIL_TEST_MASK |
371                     BFM_ENABLE_STENCIL_WRITE_MASK |
372                     (tmask << BFM_STENCIL_TEST_MASK_SHIFT) |
373                     (wmask << BFM_STENCIL_WRITE_MASK_SHIFT));
374   }
375   else {
376      /* This actually disables two-side stencil: The bit set is a
377       * modify-enable bit to indicate we are changing the two-side
378       * setting.  Then there is a symbolic zero to show that we are
379       * setting the flag to zero/off.
380       */
381      cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
382                     BFO_ENABLE_STENCIL_TWO_SIDE |
383                     0);
384      cso->bfo[1] = 0;
385   }
386
387   if (depth_stencil->depth.enabled) {
388      int func = i915_translate_compare_func(depth_stencil->depth.func);
389
390      cso->depth_LIS6 |= (S6_DEPTH_TEST_ENABLE |
391                          (func << S6_DEPTH_TEST_FUNC_SHIFT));
392
393      if (depth_stencil->depth.writemask)
394	 cso->depth_LIS6 |= S6_DEPTH_WRITE_ENABLE;
395   }
396
397   if (depth_stencil->alpha.enabled) {
398      int test = i915_translate_compare_func(depth_stencil->alpha.func);
399      ubyte refByte = float_to_ubyte(depth_stencil->alpha.ref_value);
400
401      cso->depth_LIS6 |= (S6_ALPHA_TEST_ENABLE |
402			  (test << S6_ALPHA_TEST_FUNC_SHIFT) |
403			  (((unsigned) refByte) << S6_ALPHA_REF_SHIFT));
404   }
405
406   return cso;
407}
408
409static void i915_bind_depth_stencil_state(struct pipe_context *pipe,
410                                          void *depth_stencil)
411{
412   struct i915_context *i915 = i915_context(pipe);
413   draw_flush(i915->draw);
414
415   i915->depth_stencil = (const struct i915_depth_stencil_state *)depth_stencil;
416
417   i915->dirty |= I915_NEW_DEPTH_STENCIL;
418}
419
420static void i915_delete_depth_stencil_state(struct pipe_context *pipe,
421                                            void *depth_stencil)
422{
423   FREE(depth_stencil);
424}
425
426
427static void i915_set_scissor_state( struct pipe_context *pipe,
428                                 const struct pipe_scissor_state *scissor )
429{
430   struct i915_context *i915 = i915_context(pipe);
431   draw_flush(i915->draw);
432
433   memcpy( &i915->scissor, scissor, sizeof(*scissor) );
434   i915->dirty |= I915_NEW_SCISSOR;
435}
436
437
438static void i915_set_polygon_stipple( struct pipe_context *pipe,
439                                   const struct pipe_poly_stipple *stipple )
440{
441}
442
443
444
445static void *
446i915_create_fs_state(struct pipe_context *pipe,
447                     const struct pipe_shader_state *templ)
448{
449   struct i915_context *i915 = i915_context(pipe);
450   struct i915_fragment_shader *ifs = CALLOC_STRUCT(i915_fragment_shader);
451   if (!ifs)
452      return NULL;
453
454   ifs->state.tokens = tgsi_dup_tokens(templ->tokens);
455
456   tgsi_scan_shader(templ->tokens, &ifs->info);
457
458   /* The shader's compiled to i915 instructions here */
459   i915_translate_fragment_program(i915, ifs);
460
461   return ifs;
462}
463
464static void
465i915_bind_fs_state(struct pipe_context *pipe, void *shader)
466{
467   struct i915_context *i915 = i915_context(pipe);
468   draw_flush(i915->draw);
469
470   i915->fs = (struct i915_fragment_shader*) shader;
471
472   i915->dirty |= I915_NEW_FS;
473}
474
475static
476void i915_delete_fs_state(struct pipe_context *pipe, void *shader)
477{
478   struct i915_fragment_shader *ifs = (struct i915_fragment_shader *) shader;
479
480   if (ifs->program)
481      FREE(ifs->program);
482   ifs->program_len = 0;
483
484   FREE((struct tgsi_token *)ifs->state.tokens);
485
486   FREE(ifs);
487}
488
489
490static void *
491i915_create_vs_state(struct pipe_context *pipe,
492                     const struct pipe_shader_state *templ)
493{
494   struct i915_context *i915 = i915_context(pipe);
495
496   /* just pass-through to draw module */
497   return draw_create_vertex_shader(i915->draw, templ);
498}
499
500static void i915_bind_vs_state(struct pipe_context *pipe, void *shader)
501{
502   struct i915_context *i915 = i915_context(pipe);
503
504   /* just pass-through to draw module */
505   draw_bind_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
506
507   i915->dirty |= I915_NEW_VS;
508}
509
510static void i915_delete_vs_state(struct pipe_context *pipe, void *shader)
511{
512   struct i915_context *i915 = i915_context(pipe);
513
514   /* just pass-through to draw module */
515   draw_delete_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
516}
517
518static void i915_set_constant_buffer(struct pipe_context *pipe,
519                                     uint shader, uint index,
520                                     const struct pipe_constant_buffer *buf)
521{
522   struct i915_context *i915 = i915_context(pipe);
523   struct pipe_screen *screen = pipe->screen;
524   draw_flush(i915->draw);
525
526   assert(shader < PIPE_SHADER_TYPES);
527   assert(index == 0);
528
529   /* Make a copy of shader constants.
530    * During fragment program translation we may add additional
531    * constants to the array.
532    *
533    * We want to consider the situation where some user constants
534    * (ex: a material color) may change frequently but the shader program
535    * stays the same.  In that case we should only be updating the first
536    * N constants, leaving any extras from shader translation alone.
537    */
538   if (buf) {
539      void *mapped;
540      if (buf->buffer && buf->buffer->size &&
541          (mapped = pipe_buffer_map(screen, buf->buffer,
542                                    PIPE_BUFFER_USAGE_CPU_READ))) {
543         memcpy(i915->current.constants[shader], mapped, buf->buffer->size);
544         pipe_buffer_unmap(screen, buf->buffer);
545         i915->current.num_user_constants[shader]
546            = buf->buffer->size / (4 * sizeof(float));
547      }
548      else {
549         i915->current.num_user_constants[shader] = 0;
550      }
551   }
552
553   i915->dirty |= I915_NEW_CONSTANTS;
554}
555
556
557static void i915_set_sampler_textures(struct pipe_context *pipe,
558                                      unsigned num,
559                                      struct pipe_texture **texture)
560{
561   struct i915_context *i915 = i915_context(pipe);
562   uint i;
563
564   assert(num <= PIPE_MAX_SAMPLERS);
565
566   /* Check for no-op */
567   if (num == i915->num_textures &&
568       !memcmp(i915->texture, texture, num * sizeof(struct pipe_texture *)))
569      return;
570
571   /* Fixes wrong texture in texobj with VBUF */
572   draw_flush(i915->draw);
573
574   for (i = 0; i < num; i++)
575      pipe_texture_reference((struct pipe_texture **) &i915->texture[i],
576                             texture[i]);
577
578   for (i = num; i < i915->num_textures; i++)
579      pipe_texture_reference((struct pipe_texture **) &i915->texture[i],
580                             NULL);
581
582   i915->num_textures = num;
583
584   i915->dirty |= I915_NEW_TEXTURE;
585}
586
587
588
589static void i915_set_framebuffer_state(struct pipe_context *pipe,
590				       const struct pipe_framebuffer_state *fb)
591{
592   struct i915_context *i915 = i915_context(pipe);
593   int i;
594
595   draw_flush(i915->draw);
596
597   i915->framebuffer.width = fb->width;
598   i915->framebuffer.height = fb->height;
599   i915->framebuffer.nr_cbufs = fb->nr_cbufs;
600   for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
601      pipe_surface_reference(&i915->framebuffer.cbufs[i], fb->cbufs[i]);
602   }
603   pipe_surface_reference(&i915->framebuffer.zsbuf, fb->zsbuf);
604
605   i915->dirty |= I915_NEW_FRAMEBUFFER;
606}
607
608
609
610static void i915_set_clip_state( struct pipe_context *pipe,
611			     const struct pipe_clip_state *clip )
612{
613   struct i915_context *i915 = i915_context(pipe);
614   draw_flush(i915->draw);
615
616   draw_set_clip_state(i915->draw, clip);
617
618   i915->dirty |= I915_NEW_CLIP;
619}
620
621
622
623/* Called when driver state tracker notices changes to the viewport
624 * matrix:
625 */
626static void i915_set_viewport_state( struct pipe_context *pipe,
627				     const struct pipe_viewport_state *viewport )
628{
629   struct i915_context *i915 = i915_context(pipe);
630
631   i915->viewport = *viewport; /* struct copy */
632
633   /* pass the viewport info to the draw module */
634   draw_set_viewport_state(i915->draw, &i915->viewport);
635
636   i915->dirty |= I915_NEW_VIEWPORT;
637}
638
639
640static void *
641i915_create_rasterizer_state(struct pipe_context *pipe,
642                             const struct pipe_rasterizer_state *rasterizer)
643{
644   struct i915_rasterizer_state *cso = CALLOC_STRUCT( i915_rasterizer_state );
645
646   cso->templ = rasterizer;
647   cso->color_interp = rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
648   cso->light_twoside = rasterizer->light_twoside;
649   cso->ds[0].u = _3DSTATE_DEPTH_OFFSET_SCALE;
650   cso->ds[1].f = rasterizer->offset_scale;
651   if (rasterizer->poly_stipple_enable) {
652      cso->st |= ST1_ENABLE;
653   }
654
655   if (rasterizer->scissor)
656      cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT;
657   else
658      cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT;
659
660   switch (rasterizer->cull_mode) {
661   case PIPE_WINDING_NONE:
662      cso->LIS4 |= S4_CULLMODE_NONE;
663      break;
664   case PIPE_WINDING_CW:
665      cso->LIS4 |= S4_CULLMODE_CW;
666      break;
667   case PIPE_WINDING_CCW:
668      cso->LIS4 |= S4_CULLMODE_CCW;
669      break;
670   case PIPE_WINDING_BOTH:
671      cso->LIS4 |= S4_CULLMODE_BOTH;
672      break;
673   }
674
675   {
676      int line_width = CLAMP((int)(rasterizer->line_width * 2), 1, 0xf);
677
678      cso->LIS4 |= line_width << S4_LINE_WIDTH_SHIFT;
679
680      if (rasterizer->line_smooth)
681	 cso->LIS4 |= S4_LINE_ANTIALIAS_ENABLE;
682   }
683
684   {
685      int point_size = CLAMP((int) rasterizer->point_size, 1, 0xff);
686
687      cso->LIS4 |= point_size << S4_POINT_WIDTH_SHIFT;
688   }
689
690   if (rasterizer->flatshade) {
691      cso->LIS4 |= (S4_FLATSHADE_ALPHA |
692                    S4_FLATSHADE_COLOR |
693                    S4_FLATSHADE_SPECULAR);
694   }
695
696   cso->LIS7 = fui( rasterizer->offset_units );
697
698
699   return cso;
700}
701
702static void i915_bind_rasterizer_state( struct pipe_context *pipe,
703                                        void *raster )
704{
705   struct i915_context *i915 = i915_context(pipe);
706
707   i915->rasterizer = (struct i915_rasterizer_state *)raster;
708
709   /* pass-through to draw module */
710   draw_set_rasterizer_state(i915->draw,
711                          (i915->rasterizer ? i915->rasterizer->templ : NULL));
712
713   i915->dirty |= I915_NEW_RASTERIZER;
714}
715
716static void i915_delete_rasterizer_state(struct pipe_context *pipe,
717                                         void *raster)
718{
719   FREE(raster);
720}
721
722static void i915_set_vertex_buffers(struct pipe_context *pipe,
723                                    unsigned count,
724                                    const struct pipe_vertex_buffer *buffers)
725{
726   struct i915_context *i915 = i915_context(pipe);
727   /* Because we change state before the draw_set_vertex_buffers call
728    * we need a flush here, just to be sure.
729    */
730   draw_flush(i915->draw);
731
732   memcpy(i915->vertex_buffer, buffers, count * sizeof(buffers[0]));
733   i915->num_vertex_buffers = count;
734
735   /* pass-through to draw module */
736   draw_set_vertex_buffers(i915->draw, count, buffers);
737}
738
739static void i915_set_vertex_elements(struct pipe_context *pipe,
740                                     unsigned count,
741                                     const struct pipe_vertex_element *elements)
742{
743   struct i915_context *i915 = i915_context(pipe);
744   /* Because we change state before the draw_set_vertex_buffers call
745    * we need a flush here, just to be sure.
746    */
747   draw_flush(i915->draw);
748
749   i915->num_vertex_elements = count;
750   /* pass-through to draw module */
751   draw_set_vertex_elements(i915->draw, count, elements);
752}
753
754
755static void i915_set_edgeflags(struct pipe_context *pipe,
756                               const unsigned *bitfield)
757{
758   /* TODO do something here */
759}
760
761void
762i915_init_state_functions( struct i915_context *i915 )
763{
764   i915->base.set_edgeflags = i915_set_edgeflags;
765   i915->base.create_blend_state = i915_create_blend_state;
766   i915->base.bind_blend_state = i915_bind_blend_state;
767   i915->base.delete_blend_state = i915_delete_blend_state;
768
769   i915->base.create_sampler_state = i915_create_sampler_state;
770   i915->base.bind_fragment_sampler_states = i915_bind_sampler_states;
771   i915->base.delete_sampler_state = i915_delete_sampler_state;
772
773   i915->base.create_depth_stencil_alpha_state = i915_create_depth_stencil_state;
774   i915->base.bind_depth_stencil_alpha_state = i915_bind_depth_stencil_state;
775   i915->base.delete_depth_stencil_alpha_state = i915_delete_depth_stencil_state;
776
777   i915->base.create_rasterizer_state = i915_create_rasterizer_state;
778   i915->base.bind_rasterizer_state = i915_bind_rasterizer_state;
779   i915->base.delete_rasterizer_state = i915_delete_rasterizer_state;
780   i915->base.create_fs_state = i915_create_fs_state;
781   i915->base.bind_fs_state = i915_bind_fs_state;
782   i915->base.delete_fs_state = i915_delete_fs_state;
783   i915->base.create_vs_state = i915_create_vs_state;
784   i915->base.bind_vs_state = i915_bind_vs_state;
785   i915->base.delete_vs_state = i915_delete_vs_state;
786
787   i915->base.set_blend_color = i915_set_blend_color;
788   i915->base.set_clip_state = i915_set_clip_state;
789   i915->base.set_constant_buffer = i915_set_constant_buffer;
790   i915->base.set_framebuffer_state = i915_set_framebuffer_state;
791
792   i915->base.set_polygon_stipple = i915_set_polygon_stipple;
793   i915->base.set_scissor_state = i915_set_scissor_state;
794   i915->base.set_fragment_sampler_textures = i915_set_sampler_textures;
795   i915->base.set_viewport_state = i915_set_viewport_state;
796   i915->base.set_vertex_buffers = i915_set_vertex_buffers;
797   i915->base.set_vertex_elements = i915_set_vertex_elements;
798}
799