i915_state_derived.c revision cc2629f5912d1c608f830ab63f6c4e0875d2fcef
1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**************************************************************************
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
3b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Permission is hereby granted, free of charge, to any person obtaining a
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * copy of this software and associated documentation files (the
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * "Software"), to deal in the Software without restriction, including
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * without limitation the rights to use, copy, modify, merge, publish,
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * distribute, sub license, and/or sell copies of the Software, and to
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * permit persons to whom the Software is furnished to do so, subject to
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the following conditions:
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * The above copyright notice and this permission notice (including the
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * next paragraph) shall be included in all copies or substantial portions
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * of the Software.
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru **************************************************************************/
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "pipe/p_util.h"
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "pipe/draw/draw_context.h"
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "pipe/draw/draw_vertex.h"
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "i915_context.h"
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "i915_state.h"
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "i915_reg.h"
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "i915_fpc.h"
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "pipe/tgsi/exec/tgsi_token.h"
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Determine which post-transform / pre-rasterization vertex attributes
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * we need.
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Derived from:  fs, setup states.
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void calculate_vertex_layout( struct i915_context *i915 )
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   const struct pipe_shader_state *fs = i915->fs;
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   const interp_mode colorInterp = i915->rasterizer->color_interp;
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   struct vertex_info *vinfo = &i915->current.vertex_info;
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   uint front0 = 0, back0 = 0, front1 = 0, back1 = 0;
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   boolean needW = 0;
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   uint i;
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   boolean texCoords[8];
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   memset(texCoords, 0, sizeof(texCoords));
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   memset(vinfo, 0, sizeof(*vinfo));
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   /* pos */
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   draw_emit_vertex_attr(vinfo, TGSI_ATTRIB_POS, FORMAT_3F, INTERP_LINEAR);
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   /* Note: we'll set the S4_VFMT_XYZ[W] bits below */
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru   for (i = 0; i < fs->num_inputs; i++) {
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      switch (fs->input_semantics[i]) {
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      case TGSI_SEMANTIC_POSITION:
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru         break;
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      case TGSI_SEMANTIC_COLOR0:
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru         front0 = draw_emit_vertex_attr(vinfo, TGSI_ATTRIB_COLOR0,
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                                        FORMAT_4UB, colorInterp);
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru         vinfo->hwfmt[0] |= S4_VFMT_COLOR;
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru         break;
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru      case TGSI_SEMANTIC_COLOR1:
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru         assert(0); /* untested */
72         front1 = draw_emit_vertex_attr(vinfo, TGSI_ATTRIB_COLOR1,
73                                        FORMAT_4UB, colorInterp);
74         vinfo->hwfmt[0] |= S4_VFMT_SPEC_FOG;
75         break;
76      case TGSI_SEMANTIC_TEX0:
77      case TGSI_SEMANTIC_TEX1:
78      case TGSI_SEMANTIC_TEX2:
79      case TGSI_SEMANTIC_TEX3:
80      case TGSI_SEMANTIC_TEX4:
81      case TGSI_SEMANTIC_TEX5:
82      case TGSI_SEMANTIC_TEX6:
83      case TGSI_SEMANTIC_TEX7:
84         {
85            const uint unit = fs->input_semantics[i] - TGSI_SEMANTIC_TEX0;
86            uint hwtc;
87            texCoords[unit] = TRUE;
88            draw_emit_vertex_attr(vinfo, TGSI_ATTRIB_TEX0 + i,
89                               FORMAT_4F, INTERP_PERSPECTIVE);
90            hwtc = TEXCOORDFMT_4D;
91            needW = TRUE;
92            vinfo->hwfmt[1] |= hwtc << (unit * 4);
93         }
94         break;
95      default:
96         assert(0);
97      }
98
99   }
100
101   /* finish up texcoord fields */
102   for (i = 0; i < 8; i++) {
103      if (!texCoords[i]) {
104         const uint hwtc = TEXCOORDFMT_NOT_PRESENT;
105         vinfo->hwfmt[1] |= hwtc << (i* 4);
106      }
107   }
108
109#if 0
110   /* color0 */
111   if (inputsRead & (1 << TGSI_ATTRIB_COLOR0)) {
112      front0 = draw_emit_vertex_attr(vinfo, TGSI_ATTRIB_COLOR0,
113                                     FORMAT_4UB, colorInterp);
114      vinfo->hwfmt[0] |= S4_VFMT_COLOR;
115   }
116
117   /* color 1 */
118   if (inputsRead & (1 << TGSI_ATTRIB_COLOR1)) {
119      assert(0); /* untested */
120      front1 = draw_emit_vertex_attr(vinfo, TGSI_ATTRIB_COLOR1,
121                                     FORMAT_4UB, colorInterp);
122      vinfo->hwfmt[0] |= S4_VFMT_SPEC_FOG;
123   }
124
125   /* XXX fog? */
126
127   /* texcoords */
128   {
129      uint i;
130      for (i = TGSI_ATTRIB_TEX0; i <= TGSI_ATTRIB_TEX7; i++) {
131         uint hwtc;
132         if (inputsRead & (1 << i)) {
133            draw_emit_vertex_attr(vinfo, i, FORMAT_4F, INTERP_PERSPECTIVE);
134            hwtc = TEXCOORDFMT_4D;
135            needW = TRUE;
136         }
137         else {
138            hwtc = TEXCOORDFMT_NOT_PRESENT;
139         }
140         vinfo->hwfmt[1] |= hwtc << ((i - TGSI_ATTRIB_TEX0) * 4);
141      }
142   }
143#endif
144
145   /* go back and fill in the vertex position info now that we have needW */
146   if (needW) {
147      vinfo->hwfmt[0] |= S4_VFMT_XYZW;
148      vinfo->format[0] = FORMAT_4F;
149   }
150   else {
151      vinfo->hwfmt[0] |= S4_VFMT_XYZ;
152      vinfo->format[0] = FORMAT_3F;
153   }
154
155   /* Additional attributes required for setup: Just twosided
156    * lighting.  Edgeflag is dealt with specially by setting bits in
157    * the vertex header.
158    */
159   if (i915->rasterizer->light_twoside) {
160      if (front0) {
161         back0 = draw_emit_vertex_attr(vinfo, TGSI_ATTRIB_BFC0,
162                                       FORMAT_OMIT, colorInterp);
163      }
164      if (back0) {
165         back1 = draw_emit_vertex_attr(vinfo, TGSI_ATTRIB_BFC1,
166                                       FORMAT_OMIT, colorInterp);
167      }
168   }
169
170   draw_compute_vertex_size(vinfo);
171
172   /* If the attributes have changed, tell the draw module about the new
173    * vertex layout.  We'll also update the hardware vertex format info.
174    */
175   draw_set_vertex_attributes( i915->draw,
176                               vinfo->slot_to_attrib,
177                               vinfo->interp_mode,
178			       vinfo->num_attribs);
179
180   draw_set_twoside_attributes(i915->draw,
181                               front0, back0, front1, back1);
182
183   /* Need to set this flag so that the LIS2/4 registers get set.
184    * It also means the i915_update_immediate() function must be called
185    * after this one, in i915_update_derived().
186    */
187   i915->dirty |= I915_NEW_VERTEX_FORMAT;
188}
189
190
191
192
193/* Hopefully this will remain quite simple, otherwise need to pull in
194 * something like the state tracker mechanism.
195 */
196void i915_update_derived( struct i915_context *i915 )
197{
198   if (i915->dirty & (I915_NEW_RASTERIZER | I915_NEW_FS))
199      calculate_vertex_layout( i915 );
200
201   if (i915->dirty & (I915_NEW_SAMPLER | I915_NEW_TEXTURE))
202      i915_update_samplers(i915);
203
204   if (i915->dirty & I915_NEW_TEXTURE)
205      i915_update_textures(i915);
206
207   if (i915->dirty)
208      i915_update_immediate( i915 );
209
210   if (i915->dirty)
211      i915_update_dynamic( i915 );
212
213   if (i915->dirty & I915_NEW_FS) {
214      i915_translate_fragment_program(i915);
215      i915->hardware_dirty |= I915_HW_PROGRAM; /* XXX right? */
216   }
217
218   /* HW emit currently references framebuffer state directly:
219    */
220   if (i915->dirty & I915_NEW_FRAMEBUFFER)
221      i915->hardware_dirty |= I915_HW_STATIC;
222
223   i915->dirty = 0;
224}
225