i965g: consult fs inputs when laying out vs output regs
[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_context *brw = brw_context(pipe);
62
63 brw->curr.fragment_shader = (struct brw_fragment_shader *)prog;
64 brw->state.dirty.mesa |= PIPE_NEW_FRAGMENT_SHADER;
65 }
66
67 static void brw_bind_vs_state( struct pipe_context *pipe, void *prog )
68 {
69 struct brw_context *brw = brw_context(pipe);
70
71 brw->curr.vertex_shader = (struct brw_vertex_shader *)prog;
72 brw->state.dirty.mesa |= PIPE_NEW_VERTEX_SHADER;
73 }
74
75
76
77 static void *brw_create_fs_state( struct pipe_context *pipe,
78 const struct pipe_shader_state *shader )
79 {
80 struct brw_context *brw = brw_context(pipe);
81 struct brw_fragment_shader *fs;
82 int i;
83
84 fs = CALLOC_STRUCT(brw_fragment_shader);
85 if (fs == NULL)
86 return NULL;
87
88 /* Duplicate tokens, scan shader
89 */
90 fs->id = brw->program_id++;
91 fs->has_flow_control = has_flow_control(&fs->info);
92
93 fs->tokens = tgsi_dup_tokens(shader->tokens);
94 if (fs->tokens == NULL)
95 goto fail;
96
97 tgsi_scan_shader(fs->tokens, &fs->info);
98
99 fs->signature.nr_inputs = fs->info.num_inputs;
100 for (i = 0; i < fs->info.num_inputs; i++) {
101 fs->signature.input[i].semantic = fs->info.input_semantic_name[i];
102 fs->signature.input[i].semantic_index = fs->info.input_semantic_index[i];
103 }
104
105 for (i = 0; i < fs->info.num_inputs; i++)
106 if (fs->info.input_semantic_name[i] == TGSI_SEMANTIC_POSITION)
107 fs->uses_depth = 1;
108
109 if (fs->info.uses_kill)
110 fs->iz_lookup |= IZ_PS_KILL_ALPHATEST_BIT;
111
112 if (fs->info.writes_z)
113 fs->iz_lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
114
115 return (void *)fs;
116
117 fail:
118 FREE(fs);
119 return NULL;
120 }
121
122
123 static void *brw_create_vs_state( struct pipe_context *pipe,
124 const struct pipe_shader_state *shader )
125 {
126 struct brw_context *brw = brw_context(pipe);
127
128 struct brw_vertex_shader *vs = CALLOC_STRUCT(brw_vertex_shader);
129 if (vs == NULL)
130 return NULL;
131
132 /* Duplicate tokens, scan shader
133 */
134 vs->id = brw->program_id++;
135 vs->has_flow_control = has_flow_control(&vs->info);
136
137 vs->tokens = tgsi_dup_tokens(shader->tokens);
138 if (vs->tokens == NULL)
139 goto fail;
140
141 tgsi_scan_shader(vs->tokens, &vs->info);
142
143 /* Done:
144 */
145 return (void *)vs;
146
147 fail:
148 FREE(vs);
149 return NULL;
150 }
151
152
153 static void brw_delete_fs_state( struct pipe_context *pipe, void *prog )
154 {
155 struct brw_fragment_shader *fs = (struct brw_fragment_shader *)prog;
156
157 bo_reference(&fs->const_buffer, NULL);
158 FREE( (void *)fs->tokens );
159 FREE( fs );
160 }
161
162
163 static void brw_delete_vs_state( struct pipe_context *pipe, void *prog )
164 {
165 struct brw_fragment_shader *vs = (struct brw_fragment_shader *)prog;
166
167 /* Delete draw shader
168 */
169 FREE( (void *)vs->tokens );
170 FREE( vs );
171 }
172
173
174 static void brw_set_constant_buffer(struct pipe_context *pipe,
175 uint shader, uint index,
176 const struct pipe_constant_buffer *buf)
177 {
178 struct brw_context *brw = brw_context(pipe);
179
180 assert(index == 0);
181
182 if (shader == PIPE_SHADER_FRAGMENT) {
183 pipe_buffer_reference( &brw->curr.fragment_constants,
184 buf->buffer );
185
186 brw->state.dirty.mesa |= PIPE_NEW_FRAGMENT_CONSTANTS;
187 }
188 else {
189 pipe_buffer_reference( &brw->curr.vertex_constants,
190 buf->buffer );
191
192 brw->state.dirty.mesa |= PIPE_NEW_VERTEX_CONSTANTS;
193 }
194 }
195
196
197 void brw_pipe_shader_init( struct brw_context *brw )
198 {
199 brw->base.set_constant_buffer = brw_set_constant_buffer;
200
201 brw->base.create_vs_state = brw_create_vs_state;
202 brw->base.bind_vs_state = brw_bind_vs_state;
203 brw->base.delete_vs_state = brw_delete_vs_state;
204
205 brw->base.create_fs_state = brw_create_fs_state;
206 brw->base.bind_fs_state = brw_bind_fs_state;
207 brw->base.delete_fs_state = brw_delete_fs_state;
208 }
209
210 void brw_pipe_shader_cleanup( struct brw_context *brw )
211 {
212 pipe_buffer_reference( &brw->curr.fragment_constants, NULL );
213 pipe_buffer_reference( &brw->curr.vertex_constants, NULL );
214 }