i965g: include interpolation info in fs signature
[mesa.git] / src / gallium / drivers / i965 / brw_pipe_shader.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "util/u_memory.h"
33
34 #include "tgsi/tgsi_parse.h"
35 #include "tgsi/tgsi_scan.h"
36
37 #include "brw_context.h"
38 #include "brw_util.h"
39 #include "brw_wm.h"
40
41
42 /**
43 * Determine if the given shader uses complex features such as flow
44 * conditionals, loops, subroutines.
45 */
46 static GLboolean has_flow_control(const struct tgsi_shader_info *info)
47 {
48 return (info->opcode_count[TGSI_OPCODE_ARL] > 0 ||
49 info->opcode_count[TGSI_OPCODE_IF] > 0 ||
50 info->opcode_count[TGSI_OPCODE_ENDIF] > 0 || /* redundant - IF */
51 info->opcode_count[TGSI_OPCODE_CAL] > 0 ||
52 info->opcode_count[TGSI_OPCODE_BRK] > 0 || /* redundant - BGNLOOP */
53 info->opcode_count[TGSI_OPCODE_RET] > 0 || /* redundant - CAL */
54 info->opcode_count[TGSI_OPCODE_BGNLOOP] > 0);
55 }
56
57
58
59 static void brw_bind_fs_state( struct pipe_context *pipe, void *prog )
60 {
61 struct brw_fragment_shader *fs = (struct brw_fragment_shader *)prog;
62 struct brw_context *brw = brw_context(pipe);
63
64 if (brw->curr.fragment_shader == fs)
65 return;
66
67 if (brw->curr.fragment_shader == NULL ||
68 fs == NULL ||
69 memcmp(&brw->curr.fragment_shader->signature, &fs->signature,
70 brw_fs_signature_size(&fs->signature)) != 0) {
71 brw->state.dirty.mesa |= PIPE_NEW_FRAGMENT_SIGNATURE;
72 }
73
74 brw->curr.fragment_shader = fs;
75 brw->state.dirty.mesa |= PIPE_NEW_FRAGMENT_SHADER;
76 }
77
78 static void brw_bind_vs_state( struct pipe_context *pipe, void *prog )
79 {
80 struct brw_context *brw = brw_context(pipe);
81
82 brw->curr.vertex_shader = (struct brw_vertex_shader *)prog;
83 brw->state.dirty.mesa |= PIPE_NEW_VERTEX_SHADER;
84 }
85
86
87
88 static void *brw_create_fs_state( struct pipe_context *pipe,
89 const struct pipe_shader_state *shader )
90 {
91 struct brw_context *brw = brw_context(pipe);
92 struct brw_fragment_shader *fs;
93 int i;
94
95 fs = CALLOC_STRUCT(brw_fragment_shader);
96 if (fs == NULL)
97 return NULL;
98
99 /* Duplicate tokens, scan shader
100 */
101 fs->id = brw->program_id++;
102 fs->has_flow_control = has_flow_control(&fs->info);
103
104 fs->tokens = tgsi_dup_tokens(shader->tokens);
105 if (fs->tokens == NULL)
106 goto fail;
107
108 tgsi_scan_shader(fs->tokens, &fs->info);
109
110 fs->signature.nr_inputs = fs->info.num_inputs;
111 for (i = 0; i < fs->info.num_inputs; i++) {
112 fs->signature.input[i].interp = fs->info.input_interpolate[i];
113 fs->signature.input[i].semantic = fs->info.input_semantic_name[i];
114 fs->signature.input[i].semantic_index = fs->info.input_semantic_index[i];
115 }
116
117 for (i = 0; i < fs->info.num_inputs; i++)
118 if (fs->info.input_semantic_name[i] == TGSI_SEMANTIC_POSITION)
119 fs->uses_depth = 1;
120
121 if (fs->info.uses_kill)
122 fs->iz_lookup |= IZ_PS_KILL_ALPHATEST_BIT;
123
124 if (fs->info.writes_z)
125 fs->iz_lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
126
127 return (void *)fs;
128
129 fail:
130 FREE(fs);
131 return NULL;
132 }
133
134
135 static void *brw_create_vs_state( struct pipe_context *pipe,
136 const struct pipe_shader_state *shader )
137 {
138 struct brw_context *brw = brw_context(pipe);
139 struct brw_vertex_shader *vs;
140 unsigned i;
141
142 vs = CALLOC_STRUCT(brw_vertex_shader);
143 if (vs == NULL)
144 return NULL;
145
146 /* Duplicate tokens, scan shader
147 */
148 vs->tokens = tgsi_dup_tokens(shader->tokens);
149 if (vs->tokens == NULL)
150 goto fail;
151
152 tgsi_scan_shader(vs->tokens, &vs->info);
153
154 vs->id = brw->program_id++;
155 vs->has_flow_control = has_flow_control(&vs->info);
156
157 for (i = 0; i < vs->info.num_outputs; i++) {
158 int index = vs->info.output_semantic_index[i];
159 switch (vs->info.output_semantic_name[i]) {
160 case TGSI_SEMANTIC_POSITION:
161 vs->output_hpos = i;
162 break;
163 case TGSI_SEMANTIC_COLOR:
164 if (index == 0)
165 vs->output_color0 = i;
166 else
167 vs->output_color1 = i;
168 break;
169 case TGSI_SEMANTIC_BCOLOR:
170 if (index == 0)
171 vs->output_bfc0 = i;
172 else
173 vs->output_bfc1 = i;
174 break;
175 #if 0
176 case TGSI_SEMANTIC_EDGEFLAG:
177 vs->output_edgeflag = i;
178 break;
179 #endif
180 }
181 }
182
183
184
185 /* Done:
186 */
187 return (void *)vs;
188
189 fail:
190 FREE(vs);
191 return NULL;
192 }
193
194
195 static void brw_delete_fs_state( struct pipe_context *pipe, void *prog )
196 {
197 struct brw_fragment_shader *fs = (struct brw_fragment_shader *)prog;
198
199 bo_reference(&fs->const_buffer, NULL);
200 FREE( (void *)fs->tokens );
201 FREE( fs );
202 }
203
204
205 static void brw_delete_vs_state( struct pipe_context *pipe, void *prog )
206 {
207 struct brw_fragment_shader *vs = (struct brw_fragment_shader *)prog;
208
209 /* Delete draw shader
210 */
211 FREE( (void *)vs->tokens );
212 FREE( vs );
213 }
214
215
216 static void brw_set_constant_buffer(struct pipe_context *pipe,
217 uint shader, uint index,
218 const struct pipe_constant_buffer *buf)
219 {
220 struct brw_context *brw = brw_context(pipe);
221
222 assert(index == 0);
223
224 if (shader == PIPE_SHADER_FRAGMENT) {
225 pipe_buffer_reference( &brw->curr.fragment_constants,
226 buf->buffer );
227
228 brw->state.dirty.mesa |= PIPE_NEW_FRAGMENT_CONSTANTS;
229 }
230 else {
231 pipe_buffer_reference( &brw->curr.vertex_constants,
232 buf->buffer );
233
234 brw->state.dirty.mesa |= PIPE_NEW_VERTEX_CONSTANTS;
235 }
236 }
237
238
239 void brw_pipe_shader_init( struct brw_context *brw )
240 {
241 brw->base.set_constant_buffer = brw_set_constant_buffer;
242
243 brw->base.create_vs_state = brw_create_vs_state;
244 brw->base.bind_vs_state = brw_bind_vs_state;
245 brw->base.delete_vs_state = brw_delete_vs_state;
246
247 brw->base.create_fs_state = brw_create_fs_state;
248 brw->base.bind_fs_state = brw_bind_fs_state;
249 brw->base.delete_fs_state = brw_delete_fs_state;
250 }
251
252 void brw_pipe_shader_cleanup( struct brw_context *brw )
253 {
254 pipe_buffer_reference( &brw->curr.fragment_constants, NULL );
255 pipe_buffer_reference( &brw->curr.vertex_constants, NULL );
256 }