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