bf6b30a4530ddd6dc3e1753bbbbe195248a40c23
[mesa.git] / src / gallium / drivers / i915 / i915_state_derived.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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
29 #include "util/u_memory.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "draw/draw_context.h"
32 #include "draw/draw_vertex.h"
33 #include "i915_context.h"
34 #include "i915_state.h"
35 #include "i915_debug.h"
36 #include "i915_reg.h"
37
38 static uint find_mapping(struct i915_fragment_shader* fs, int unit)
39 {
40 int i;
41 for (i = 0; i < I915_TEX_UNITS ; i++)
42 {
43 if (fs->generic_mapping[i] == unit)
44 return i;
45 }
46 debug_printf("Mapping not found\n");
47 return 0;
48 }
49
50
51
52 /***********************************************************************
53 * Determine the hardware vertex layout.
54 * Depends on vertex/fragment shader state.
55 */
56 static void calculate_vertex_layout(struct i915_context *i915)
57 {
58 const struct i915_fragment_shader *fs = i915->fs;
59 const enum interp_mode colorInterp = i915->rasterizer->color_interp;
60 struct vertex_info vinfo;
61 boolean texCoords[I915_TEX_UNITS], colors[2], fog, needW;
62 uint i;
63 int src;
64
65 memset(texCoords, 0, sizeof(texCoords));
66 colors[0] = colors[1] = fog = needW = FALSE;
67 memset(&vinfo, 0, sizeof(vinfo));
68
69 /* Determine which fragment program inputs are needed. Setup HW vertex
70 * layout below, in the HW-specific attribute order.
71 */
72 for (i = 0; i < fs->info.num_inputs; i++) {
73 switch (fs->info.input_semantic_name[i]) {
74 case TGSI_SEMANTIC_POSITION:
75 break;
76 case TGSI_SEMANTIC_COLOR:
77 assert(fs->info.input_semantic_index[i] < 2);
78 colors[fs->info.input_semantic_index[i]] = TRUE;
79 break;
80 case TGSI_SEMANTIC_GENERIC:
81 {
82 /* texcoords/varyings/other generic */
83 /* XXX handle back/front face and point size */
84 uint unit = fs->info.input_semantic_index[i];
85
86 texCoords[find_mapping(fs, unit)] = TRUE;
87 needW = TRUE;
88 }
89 break;
90 case TGSI_SEMANTIC_FOG:
91 fog = TRUE;
92 break;
93 default:
94 assert(0);
95 }
96 }
97
98
99 /* pos */
100 src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_POSITION, 0);
101 if (needW) {
102 draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_LINEAR, src);
103 vinfo.hwfmt[0] |= S4_VFMT_XYZW;
104 vinfo.attrib[0].emit = EMIT_4F;
105 }
106 else {
107 draw_emit_vertex_attr(&vinfo, EMIT_3F, INTERP_LINEAR, src);
108 vinfo.hwfmt[0] |= S4_VFMT_XYZ;
109 vinfo.attrib[0].emit = EMIT_3F;
110 }
111
112 /* hardware point size */
113 /* XXX todo */
114
115 /* primary color */
116 if (colors[0]) {
117 src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_COLOR, 0);
118 draw_emit_vertex_attr(&vinfo, EMIT_4UB_BGRA, colorInterp, src);
119 vinfo.hwfmt[0] |= S4_VFMT_COLOR;
120 }
121
122 /* secondary color */
123 if (colors[1]) {
124 src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_COLOR, 1);
125 draw_emit_vertex_attr(&vinfo, EMIT_4UB_BGRA, colorInterp, src);
126 vinfo.hwfmt[0] |= S4_VFMT_SPEC_FOG;
127 }
128
129 /* fog coord, not fog blend factor */
130 if (fog) {
131 src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_FOG, 0);
132 draw_emit_vertex_attr(&vinfo, EMIT_1F, INTERP_PERSPECTIVE, src);
133 vinfo.hwfmt[0] |= S4_VFMT_FOG_PARAM;
134 }
135
136 /* texcoords/varyings */
137 for (i = 0; i < I915_TEX_UNITS; i++) {
138 uint hwtc;
139 if (texCoords[i]) {
140 hwtc = TEXCOORDFMT_4D;
141 src = draw_find_shader_output(i915->draw, TGSI_SEMANTIC_GENERIC, fs->generic_mapping[i]);
142 draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
143 }
144 else {
145 hwtc = TEXCOORDFMT_NOT_PRESENT;
146 }
147 vinfo.hwfmt[1] |= hwtc << (i * 4);
148 }
149
150 draw_compute_vertex_size(&vinfo);
151
152 if (memcmp(&i915->current.vertex_info, &vinfo, sizeof(vinfo))) {
153 /* Need to set this flag so that the LIS2/4 registers get set.
154 * It also means the i915_update_immediate() function must be called
155 * after this one, in i915_update_derived().
156 */
157 i915->dirty |= I915_NEW_VERTEX_FORMAT;
158
159 memcpy(&i915->current.vertex_info, &vinfo, sizeof(vinfo));
160 }
161 }
162
163 struct i915_tracked_state i915_update_vertex_layout = {
164 "vertex_layout",
165 calculate_vertex_layout,
166 I915_NEW_RASTERIZER | I915_NEW_FS | I915_NEW_VS
167 };
168
169
170
171 /***********************************************************************
172 */
173 static struct i915_tracked_state *atoms[] = {
174 &i915_update_vertex_layout,
175 &i915_hw_samplers,
176 &i915_hw_sampler_views,
177 &i915_hw_immediate,
178 &i915_hw_dynamic,
179 &i915_hw_fs,
180 &i915_hw_framebuffer,
181 &i915_hw_dst_buf_vars,
182 &i915_hw_constants,
183 NULL,
184 };
185
186 void i915_update_derived(struct i915_context *i915)
187 {
188 int i;
189
190 if (I915_DBG_ON(DBG_ATOMS))
191 i915_dump_dirty(i915, __FUNCTION__);
192
193 for (i = 0; atoms[i]; i++)
194 if (atoms[i]->dirty & i915->dirty)
195 atoms[i]->update(i915);
196
197 i915->dirty = 0;
198 }