Merge branch 'gallium-embedded'
[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_inlines.h"
33 #include "util/u_memory.h"
34
35 #include "tgsi/tgsi_parse.h"
36 #include "tgsi/tgsi_scan.h"
37
38 #include "brw_context.h"
39 #include "brw_util.h"
40 #include "brw_wm.h"
41
42
43 /**
44 * Determine if the given shader uses complex features such as flow
45 * conditionals, loops, subroutines.
46 */
47 static GLboolean has_flow_control(const struct tgsi_shader_info *info)
48 {
49 return (info->opcode_count[TGSI_OPCODE_ARL] > 0 ||
50 info->opcode_count[TGSI_OPCODE_IF] > 0 ||
51 info->opcode_count[TGSI_OPCODE_ENDIF] > 0 || /* redundant - IF */
52 info->opcode_count[TGSI_OPCODE_CAL] > 0 ||
53 info->opcode_count[TGSI_OPCODE_BRK] > 0 || /* redundant - BGNLOOP */
54 info->opcode_count[TGSI_OPCODE_RET] > 0 || /* redundant - CAL */
55 info->opcode_count[TGSI_OPCODE_BGNLOOP] > 0);
56 }
57
58
59 static void scan_immediates(const struct tgsi_token *tokens,
60 const struct tgsi_shader_info *info,
61 struct brw_immediate_data *imm)
62 {
63 struct tgsi_parse_context parse;
64 boolean done = FALSE;
65
66 imm->nr = 0;
67 imm->data = MALLOC(info->immediate_count * 4 * sizeof(float));
68
69 tgsi_parse_init( &parse, tokens );
70 while (!tgsi_parse_end_of_tokens( &parse ) && !done) {
71 tgsi_parse_token( &parse );
72
73 switch (parse.FullToken.Token.Type) {
74 case TGSI_TOKEN_TYPE_DECLARATION:
75 break;
76
77 case TGSI_TOKEN_TYPE_IMMEDIATE: {
78 static const float id[4] = {0,0,0,1};
79 const float *value = &parse.FullToken.FullImmediate.u[0].Float;
80 unsigned size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
81 unsigned i;
82
83 for (i = 0; i < size; i++)
84 imm->data[imm->nr][i] = value[i];
85
86 for (; i < 4; i++)
87 imm->data[imm->nr][i] = id[i];
88
89 imm->nr++;
90 break;
91 }
92
93 case TGSI_TOKEN_TYPE_INSTRUCTION:
94 done = 1;
95 break;
96 }
97 }
98 }
99
100
101 static void brw_bind_fs_state( struct pipe_context *pipe, void *prog )
102 {
103 struct brw_fragment_shader *fs = (struct brw_fragment_shader *)prog;
104 struct brw_context *brw = brw_context(pipe);
105
106 if (brw->curr.fragment_shader == fs)
107 return;
108
109 if (brw->curr.fragment_shader == NULL ||
110 fs == NULL ||
111 memcmp(&brw->curr.fragment_shader->signature, &fs->signature,
112 brw_fs_signature_size(&fs->signature)) != 0) {
113 brw->state.dirty.mesa |= PIPE_NEW_FRAGMENT_SIGNATURE;
114 }
115
116 brw->curr.fragment_shader = fs;
117 brw->state.dirty.mesa |= PIPE_NEW_FRAGMENT_SHADER;
118 }
119
120 static void brw_bind_vs_state( struct pipe_context *pipe, void *prog )
121 {
122 struct brw_context *brw = brw_context(pipe);
123
124 brw->curr.vertex_shader = (struct brw_vertex_shader *)prog;
125 brw->state.dirty.mesa |= PIPE_NEW_VERTEX_SHADER;
126 }
127
128
129
130 static void *brw_create_fs_state( struct pipe_context *pipe,
131 const struct pipe_shader_state *shader )
132 {
133 struct brw_context *brw = brw_context(pipe);
134 struct brw_fragment_shader *fs;
135 int i;
136
137 fs = CALLOC_STRUCT(brw_fragment_shader);
138 if (fs == NULL)
139 return NULL;
140
141 /* Duplicate tokens, scan shader
142 */
143 fs->id = brw->program_id++;
144 fs->has_flow_control = has_flow_control(&fs->info);
145
146 fs->tokens = tgsi_dup_tokens(shader->tokens);
147 if (fs->tokens == NULL)
148 goto fail;
149
150 tgsi_scan_shader(fs->tokens, &fs->info);
151 scan_immediates(fs->tokens, &fs->info, &fs->immediates);
152
153 fs->signature.nr_inputs = fs->info.num_inputs;
154 for (i = 0; i < fs->info.num_inputs; i++) {
155 fs->signature.input[i].interp = fs->info.input_interpolate[i];
156 fs->signature.input[i].semantic = fs->info.input_semantic_name[i];
157 fs->signature.input[i].semantic_index = fs->info.input_semantic_index[i];
158 }
159
160 for (i = 0; i < fs->info.num_inputs; i++)
161 if (fs->info.input_semantic_name[i] == TGSI_SEMANTIC_POSITION)
162 fs->uses_depth = 1;
163
164 if (fs->info.uses_kill)
165 fs->iz_lookup |= IZ_PS_KILL_ALPHATEST_BIT;
166
167 if (fs->info.writes_z)
168 fs->iz_lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
169
170 return (void *)fs;
171
172 fail:
173 FREE(fs);
174 return NULL;
175 }
176
177
178 static void *brw_create_vs_state( struct pipe_context *pipe,
179 const struct pipe_shader_state *shader )
180 {
181 struct brw_context *brw = brw_context(pipe);
182 struct brw_vertex_shader *vs;
183 unsigned i;
184
185 vs = CALLOC_STRUCT(brw_vertex_shader);
186 if (vs == NULL)
187 return NULL;
188
189 /* Duplicate tokens, scan shader
190 */
191 vs->tokens = tgsi_dup_tokens(shader->tokens);
192 if (vs->tokens == NULL)
193 goto fail;
194
195 tgsi_scan_shader(vs->tokens, &vs->info);
196 scan_immediates(vs->tokens, &vs->info, &vs->immediates);
197
198 vs->id = brw->program_id++;
199 vs->has_flow_control = has_flow_control(&vs->info);
200
201 vs->output_hpos = BRW_OUTPUT_NOT_PRESENT;
202 vs->output_color0 = BRW_OUTPUT_NOT_PRESENT;
203 vs->output_color1 = BRW_OUTPUT_NOT_PRESENT;
204 vs->output_bfc0 = BRW_OUTPUT_NOT_PRESENT;
205 vs->output_bfc1 = BRW_OUTPUT_NOT_PRESENT;
206 vs->output_edgeflag = BRW_OUTPUT_NOT_PRESENT;
207
208 for (i = 0; i < vs->info.num_outputs; i++) {
209 int index = vs->info.output_semantic_index[i];
210 switch (vs->info.output_semantic_name[i]) {
211 case TGSI_SEMANTIC_POSITION:
212 vs->output_hpos = i;
213 break;
214 case TGSI_SEMANTIC_COLOR:
215 if (index == 0)
216 vs->output_color0 = i;
217 else
218 vs->output_color1 = i;
219 break;
220 case TGSI_SEMANTIC_BCOLOR:
221 if (index == 0)
222 vs->output_bfc0 = i;
223 else
224 vs->output_bfc1 = i;
225 break;
226 case TGSI_SEMANTIC_EDGEFLAG:
227 vs->output_edgeflag = i;
228 break;
229 }
230 }
231
232
233 /* Done:
234 */
235 return (void *)vs;
236
237 fail:
238 FREE(vs);
239 return NULL;
240 }
241
242
243 static void brw_delete_fs_state( struct pipe_context *pipe, void *prog )
244 {
245 struct brw_fragment_shader *fs = (struct brw_fragment_shader *)prog;
246
247 bo_reference(&fs->const_buffer, NULL);
248 FREE( (void *)fs->tokens );
249 FREE( fs );
250 }
251
252
253 static void brw_delete_vs_state( struct pipe_context *pipe, void *prog )
254 {
255 struct brw_fragment_shader *vs = (struct brw_fragment_shader *)prog;
256
257 /* Delete draw shader
258 */
259 FREE( (void *)vs->tokens );
260 FREE( vs );
261 }
262
263
264 static void brw_set_constant_buffer(struct pipe_context *pipe,
265 uint shader, uint index,
266 struct pipe_buffer *buf)
267 {
268 struct brw_context *brw = brw_context(pipe);
269
270 assert(index == 0);
271
272 if (shader == PIPE_SHADER_FRAGMENT) {
273 pipe_buffer_reference( &brw->curr.fragment_constants,
274 buf );
275
276 brw->state.dirty.mesa |= PIPE_NEW_FRAGMENT_CONSTANTS;
277 }
278 else {
279 pipe_buffer_reference( &brw->curr.vertex_constants,
280 buf );
281
282 brw->state.dirty.mesa |= PIPE_NEW_VERTEX_CONSTANTS;
283 }
284 }
285
286
287 void brw_pipe_shader_init( struct brw_context *brw )
288 {
289 brw->base.set_constant_buffer = brw_set_constant_buffer;
290
291 brw->base.create_vs_state = brw_create_vs_state;
292 brw->base.bind_vs_state = brw_bind_vs_state;
293 brw->base.delete_vs_state = brw_delete_vs_state;
294
295 brw->base.create_fs_state = brw_create_fs_state;
296 brw->base.bind_fs_state = brw_bind_fs_state;
297 brw->base.delete_fs_state = brw_delete_fs_state;
298 }
299
300 void brw_pipe_shader_cleanup( struct brw_context *brw )
301 {
302 pipe_buffer_reference( &brw->curr.fragment_constants, NULL );
303 pipe_buffer_reference( &brw->curr.vertex_constants, NULL );
304 }