summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i915simple/i915_state_derived.c
blob: 4767584fc608eecbf91dfbffd8738be7fbf53749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**************************************************************************
 * 
 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/


#include "pipe/p_util.h"
#include "draw/draw_context.h"
#include "draw/draw_vertex.h"
#include "i915_context.h"
#include "i915_state.h"
#include "i915_reg.h"
#include "i915_fpc.h"
#include "pipe/p_shader_tokens.h"


/**
 * Determine which post-transform / pre-rasterization vertex attributes
 * we need.
 * Derived from:  fs, setup states.
 */
static void calculate_vertex_layout( struct i915_context *i915 )
{
   const struct pipe_shader_state *fs = i915->fs;
   const enum interp_mode colorInterp = i915->rasterizer->color_interp;
   struct vertex_info vinfo;
   uint front0 = 0, back0 = 0, front1 = 0, back1 = 0;
   boolean needW = 0;
   uint i;
   boolean texCoords[8];
   uint src = 0;

   memset(texCoords, 0, sizeof(texCoords));
   memset(&vinfo, 0, sizeof(vinfo));

   /* pos */
   draw_emit_vertex_attr(&vinfo, EMIT_3F, INTERP_LINEAR, src++);
   /* Note: we'll set the S4_VFMT_XYZ[W] bits below */

   for (i = 0; i < fs->num_inputs; i++) {
      switch (fs->input_semantic_name[i]) {
      case TGSI_SEMANTIC_POSITION:
         break;
      case TGSI_SEMANTIC_COLOR:
         if (fs->input_semantic_index[i] == 0) {
            front0 = draw_emit_vertex_attr(&vinfo, EMIT_4UB, colorInterp, src++);
            vinfo.hwfmt[0] |= S4_VFMT_COLOR;
         }
         else {
            assert(fs->input_semantic_index[i] == 1);
            front1 = draw_emit_vertex_attr(&vinfo, EMIT_4UB, colorInterp, src++);
            vinfo.hwfmt[0] |= S4_VFMT_SPEC_FOG;
         }
         break;
      case TGSI_SEMANTIC_GENERIC:
         /* usually a texcoord */
         {
            const uint unit = fs->input_semantic_index[i];
            uint hwtc;
            texCoords[unit] = TRUE;
            draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_PERSPECTIVE, src++);
            hwtc = TEXCOORDFMT_4D;
            needW = TRUE;
            vinfo.hwfmt[1] |= hwtc << (unit * 4);
         }
         break;
      case TGSI_SEMANTIC_FOG:
         debug_printf("i915 fogcoord not implemented yet\n");
         draw_emit_vertex_attr(&vinfo, EMIT_1F, INTERP_PERSPECTIVE, src++);
         break;
      default:
         assert(0);
      }

   }

   /* finish up texcoord fields */
   for (i = 0; i < 8; i++) {
      if (!texCoords[i]) {
         const uint hwtc = TEXCOORDFMT_NOT_PRESENT;
         vinfo.hwfmt[1] |= hwtc << (i* 4);
      }
   }

   /* go back and fill in the vertex position info now that we have needW */
   if (needW) {
      vinfo.hwfmt[0] |= S4_VFMT_XYZW;
      vinfo.emit[0] = EMIT_4F;
   }
   else {
      vinfo.hwfmt[0] |= S4_VFMT_XYZ;
      vinfo.emit[0] = EMIT_3F;
   }

   /* Additional attributes required for setup: Just twosided
    * lighting.  Edgeflag is dealt with specially by setting bits in
    * the vertex header.
    */
   if (i915->rasterizer->light_twoside) {
      if (front0) {
         back0 = draw_emit_vertex_attr(&vinfo, EMIT_OMIT, colorInterp, src++);
      }
      if (back0) {
         back1 = draw_emit_vertex_attr(&vinfo, EMIT_OMIT, colorInterp, src++);
      }
   }

   draw_compute_vertex_size(&vinfo);

   if (memcmp(&i915->current.vertex_info, &vinfo, sizeof(vinfo))) {
      /* Need to set this flag so that the LIS2/4 registers get set.
       * It also means the i915_update_immediate() function must be called
       * after this one, in i915_update_derived().
       */
      i915->dirty |= I915_NEW_VERTEX_FORMAT;

      memcpy(&i915->current.vertex_info, &vinfo, sizeof(vinfo));
   }
}




/* Hopefully this will remain quite simple, otherwise need to pull in
 * something like the state tracker mechanism.
 */
void i915_update_derived( struct i915_context *i915 )
{
   if (i915->dirty & (I915_NEW_RASTERIZER | I915_NEW_FS))
      calculate_vertex_layout( i915 );

   if (i915->dirty & (I915_NEW_SAMPLER | I915_NEW_TEXTURE))
      i915_update_samplers(i915);

   if (i915->dirty & I915_NEW_TEXTURE)
      i915_update_textures(i915);

   if (i915->dirty)
      i915_update_immediate( i915 );

   if (i915->dirty)
      i915_update_dynamic( i915 );

   if (i915->dirty & I915_NEW_FS) {
      i915_translate_fragment_program(i915);
      i915->hardware_dirty |= I915_HW_PROGRAM; /* XXX right? */
   }

   /* HW emit currently references framebuffer state directly:
    */
   if (i915->dirty & I915_NEW_FRAMEBUFFER)
      i915->hardware_dirty |= I915_HW_STATIC;

   i915->dirty = 0;
}