gallium/i915: overhaul of fragment shader compilation, constant/immediate allocation
[mesa.git] / src / gallium / drivers / i915simple / 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 "pipe/p_util.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_vertex.h"
32 #include "i915_context.h"
33 #include "i915_state.h"
34 #include "i915_reg.h"
35 #include "i915_fpc.h"
36 #include "pipe/p_shader_tokens.h"
37
38
39 /**
40 * Determine which post-transform / pre-rasterization vertex attributes
41 * we need.
42 * Derived from: fs, setup states.
43 */
44 static void calculate_vertex_layout( struct i915_context *i915 )
45 {
46 const struct pipe_shader_state *fs = &i915->fs->state;
47 const enum interp_mode colorInterp = i915->rasterizer->color_interp;
48 struct vertex_info vinfo;
49 uint front0 = 0, back0 = 0, front1 = 0, back1 = 0;
50 boolean needW = 0;
51 uint i;
52 boolean texCoords[8];
53 uint src = 0;
54
55 memset(texCoords, 0, sizeof(texCoords));
56 memset(&vinfo, 0, sizeof(vinfo));
57
58 /* pos */
59 draw_emit_vertex_attr(&vinfo, EMIT_3F, INTERP_LINEAR, src++);
60 /* Note: we'll set the S4_VFMT_XYZ[W] bits below */
61
62 for (i = 0; i < fs->num_inputs; i++) {
63 switch (fs->input_semantic_name[i]) {
64 case TGSI_SEMANTIC_POSITION:
65 break;
66 case TGSI_SEMANTIC_COLOR:
67 if (fs->input_semantic_index[i] == 0) {
68 front0 = draw_emit_vertex_attr(&vinfo, EMIT_4UB, colorInterp, src++);
69 vinfo.hwfmt[0] |= S4_VFMT_COLOR;
70 }
71 else {
72 assert(fs->input_semantic_index[i] == 1);
73 front1 = draw_emit_vertex_attr(&vinfo, EMIT_4UB, colorInterp, src++);
74 vinfo.hwfmt[0] |= S4_VFMT_SPEC_FOG;
75 }
76 break;
77 case TGSI_SEMANTIC_GENERIC:
78 /* usually a texcoord */
79 {
80 const uint unit = fs->input_semantic_index[i];
81 uint hwtc;
82 texCoords[unit] = TRUE;
83 draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_PERSPECTIVE, src++);
84 hwtc = TEXCOORDFMT_4D;
85 needW = TRUE;
86 vinfo.hwfmt[1] |= hwtc << (unit * 4);
87 }
88 break;
89 case TGSI_SEMANTIC_FOG:
90 debug_printf("i915 fogcoord not implemented yet\n");
91 draw_emit_vertex_attr(&vinfo, EMIT_1F, INTERP_PERSPECTIVE, src++);
92 break;
93 default:
94 assert(0);
95 }
96
97 }
98
99 /* finish up texcoord fields */
100 for (i = 0; i < 8; i++) {
101 if (!texCoords[i]) {
102 const uint hwtc = TEXCOORDFMT_NOT_PRESENT;
103 vinfo.hwfmt[1] |= hwtc << (i* 4);
104 }
105 }
106
107 /* go back and fill in the vertex position info now that we have needW */
108 if (needW) {
109 vinfo.hwfmt[0] |= S4_VFMT_XYZW;
110 vinfo.emit[0] = EMIT_4F;
111 }
112 else {
113 vinfo.hwfmt[0] |= S4_VFMT_XYZ;
114 vinfo.emit[0] = EMIT_3F;
115 }
116
117 /* Additional attributes required for setup: Just twosided
118 * lighting. Edgeflag is dealt with specially by setting bits in
119 * the vertex header.
120 */
121 if (i915->rasterizer->light_twoside) {
122 if (front0) {
123 back0 = draw_emit_vertex_attr(&vinfo, EMIT_OMIT, colorInterp, src++);
124 }
125 if (back0) {
126 back1 = draw_emit_vertex_attr(&vinfo, EMIT_OMIT, colorInterp, src++);
127 }
128 }
129
130 draw_compute_vertex_size(&vinfo);
131
132 if (memcmp(&i915->current.vertex_info, &vinfo, sizeof(vinfo))) {
133 /* Need to set this flag so that the LIS2/4 registers get set.
134 * It also means the i915_update_immediate() function must be called
135 * after this one, in i915_update_derived().
136 */
137 i915->dirty |= I915_NEW_VERTEX_FORMAT;
138
139 memcpy(&i915->current.vertex_info, &vinfo, sizeof(vinfo));
140 }
141 }
142
143
144
145
146 /* Hopefully this will remain quite simple, otherwise need to pull in
147 * something like the state tracker mechanism.
148 */
149 void i915_update_derived( struct i915_context *i915 )
150 {
151 if (i915->dirty & (I915_NEW_RASTERIZER | I915_NEW_FS))
152 calculate_vertex_layout( i915 );
153
154 if (i915->dirty & (I915_NEW_SAMPLER | I915_NEW_TEXTURE))
155 i915_update_samplers(i915);
156
157 if (i915->dirty & I915_NEW_TEXTURE)
158 i915_update_textures(i915);
159
160 if (i915->dirty)
161 i915_update_immediate( i915 );
162
163 if (i915->dirty)
164 i915_update_dynamic( i915 );
165
166 if (i915->dirty & I915_NEW_FS) {
167 i915->hardware_dirty |= I915_HW_PROGRAM; /* XXX right? */
168 }
169
170 /* HW emit currently references framebuffer state directly:
171 */
172 if (i915->dirty & I915_NEW_FRAMEBUFFER)
173 i915->hardware_dirty |= I915_HW_STATIC;
174
175 i915->dirty = 0;
176 }