summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/i915/i915_state_derived.c
blob: 7809010d945a0890930b9e49f33e5d782777ebf6 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**************************************************************************
 * 
 * Copyright 2003 VMware, Inc.
 * 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 VMWARE 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 "util/u_memory.h"
#include "pipe/p_shader_tokens.h"
#include "draw/draw_context.h"
#include "draw/draw_vertex.h"
#include "i915_context.h"
#include "i915_state.h"
#include "i915_debug.h"
#include "i915_fpc.h"
#include "i915_reg.h"

static uint find_mapping(const struct i915_fragment_shader* fs, int unit)
{
   int i;
   for (i = 0; i < I915_TEX_UNITS ; i++)
   {
      if (fs->generic_mapping[i] == unit)
         return i;
   }
   debug_printf("Mapping not found\n");
   return 0;
}



/***********************************************************************
 * Determine the hardware vertex layout.
 * Depends on vertex/fragment shader state.
 */
static void calculate_vertex_layout(struct i915_context *i915)
{
   const struct i915_fragment_shader *fs = i915->fs;
   struct vertex_info vinfo;
   boolean texCoords[I915_TEX_UNITS], colors[2], fog, needW, face;
   uint i;
   int src;

   memset(texCoords, 0, sizeof(texCoords));
   colors[0] = colors[1] = fog = needW = face = FALSE;
   memset(&vinfo, 0, sizeof(vinfo));

   /* Determine which fragment program inputs are needed.  Setup HW vertex
    * layout below, in the HW-specific attribute order.
    */
   for (i = 0; i < fs->info.num_inputs; i++) {
      switch (fs->info.input_semantic_name[i]) {
      case TGSI_SEMANTIC_POSITION:
         {
            uint unit = I915_SEMANTIC_POS;
            texCoords[find_mapping(fs, unit)] = TRUE;
         }
         break;
      case TGSI_SEMANTIC_COLOR:
         assert(fs->info.input_semantic_index[i] < 2);
         colors[fs->info.input_semantic_index[i]] = TRUE;
         break;
      case TGSI_SEMANTIC_GENERIC:
         {
            /* texcoords/varyings/other generic */
            uint unit = fs->info.input_semantic_index[i];

            texCoords[find_mapping(fs, unit)] = TRUE;
            needW = TRUE;
         }
         break;
      case TGSI_SEMANTIC_FOG:
         fog = TRUE;
         break;
      case TGSI_SEMANTIC_FACE:
         face = TRUE;
         break;
      default:
         debug_printf("Unknown input type %d\n", fs->info.input_semantic_name[i]);
         assert(0);
      }
   }


   /* pos */
   src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_POSITION, 0);
   if (needW) {
      draw_emit_vertex_attr(&vinfo, EMIT_4F, src);
      vinfo.hwfmt[0] |= S4_VFMT_XYZW;
      vinfo.attrib[0].emit = EMIT_4F;
   }
   else {
      draw_emit_vertex_attr(&vinfo, EMIT_3F, src);
      vinfo.hwfmt[0] |= S4_VFMT_XYZ;
      vinfo.attrib[0].emit = EMIT_3F;
   }

   /* hardware point size */
   /* XXX todo */

   /* primary color */
   if (colors[0]) {
      src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_COLOR, 0);
      draw_emit_vertex_attr(&vinfo, EMIT_4UB_BGRA, src);
      vinfo.hwfmt[0] |= S4_VFMT_COLOR;
   }

   /* secondary color */
   if (colors[1]) {
      src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_COLOR, 1);
      draw_emit_vertex_attr(&vinfo, EMIT_4UB_BGRA, src);
      vinfo.hwfmt[0] |= S4_VFMT_SPEC_FOG;
   }

   /* fog coord, not fog blend factor */
   if (fog) {
      src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_FOG, 0);
      draw_emit_vertex_attr(&vinfo, EMIT_1F, src);
      vinfo.hwfmt[0] |= S4_VFMT_FOG_PARAM;
   }

   /* texcoords/varyings */
   for (i = 0; i < I915_TEX_UNITS; i++) {
      uint hwtc;
      if (texCoords[i]) {
         hwtc = TEXCOORDFMT_4D;
         if (fs->generic_mapping[i] == I915_SEMANTIC_POS) {
            src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_POSITION, 0);
         }
         else {
            src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_GENERIC, fs->generic_mapping[i]);
         }
         draw_emit_vertex_attr(&vinfo, EMIT_4F, src);
      }
      else {
         hwtc = TEXCOORDFMT_NOT_PRESENT;
      }
      vinfo.hwfmt[1] |= hwtc << (i * 4);
   }

   /* front/back face */
   if (face) {
      uint slot = find_mapping(fs, I915_SEMANTIC_FACE);
      debug_printf("Front/back face is broken\n");
      /* XXX Because of limitations in the draw module, currently src will be 0
       * for SEMANTIC_FACE, so this aliases to POS. We need to fix in the draw
       * module by adding an extra shader output.
       */
      src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_FACE, 0);
      draw_emit_vertex_attr(&vinfo, EMIT_1F, src);
      vinfo.hwfmt[1] &= ~(TEXCOORDFMT_NOT_PRESENT << (slot * 4));
      vinfo.hwfmt[1] |= TEXCOORDFMT_1D << (slot * 4);
   }

   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));
   }
}

struct i915_tracked_state i915_update_vertex_layout = {
   "vertex_layout",
   calculate_vertex_layout,
   I915_NEW_RASTERIZER | I915_NEW_FS | I915_NEW_VS
};



/***********************************************************************
 */
static struct i915_tracked_state *atoms[] = {
   &i915_update_vertex_layout,
   &i915_hw_samplers,
   &i915_hw_sampler_views,
   &i915_hw_immediate,
   &i915_hw_dynamic,
   &i915_hw_fs,
   &i915_hw_framebuffer,
   &i915_hw_dst_buf_vars,
   &i915_hw_constants,
   NULL,
};

void i915_update_derived(struct i915_context *i915)
{
   int i;

   if (I915_DBG_ON(DBG_ATOMS))
      i915_dump_dirty(i915, __FUNCTION__);

   if (!i915->fs) {
      i915->dirty &= ~(I915_NEW_FS_CONSTANTS | I915_NEW_FS);
      i915->hardware_dirty &= ~(I915_HW_PROGRAM | I915_HW_CONSTANTS);
   }

   if (!i915->vs)
      i915->dirty &= ~I915_NEW_VS;

   if (!i915->blend)
      i915->dirty &= ~I915_NEW_BLEND;

   if (!i915->rasterizer)
      i915->dirty &= ~I915_NEW_RASTERIZER;

   if (!i915->depth_stencil)
      i915->dirty &= ~I915_NEW_DEPTH_STENCIL;
   
   for (i = 0; atoms[i]; i++)
      if (atoms[i]->dirty & i915->dirty)
         atoms[i]->update(i915);

   i915->dirty = 0;
}