r300g: only check for an empty shader if there are no compile errors
[mesa.git] / src / gallium / drivers / softpipe / sp_state_fs.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 #include "sp_context.h"
29 #include "sp_state.h"
30 #include "sp_fs.h"
31 #include "sp_texture.h"
32
33 #include "pipe/p_defines.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "draw/draw_context.h"
37 #include "draw/draw_vs.h"
38 #include "draw/draw_gs.h"
39 #include "tgsi/tgsi_dump.h"
40 #include "tgsi/tgsi_exec.h"
41 #include "tgsi/tgsi_scan.h"
42 #include "tgsi/tgsi_parse.h"
43
44
45 void *
46 softpipe_create_fs_state(struct pipe_context *pipe,
47 const struct pipe_shader_state *templ)
48 {
49 struct softpipe_context *softpipe = softpipe_context(pipe);
50 struct sp_fragment_shader *state;
51 unsigned i;
52
53 /* debug */
54 if (softpipe->dump_fs)
55 tgsi_dump(templ->tokens, 0);
56
57 /* codegen */
58 state = softpipe_create_fs_sse( softpipe, templ );
59 if (!state) {
60 state = softpipe_create_fs_exec( softpipe, templ );
61 }
62
63 assert(state);
64
65 /* get/save the summary info for this shader */
66 tgsi_scan_shader(templ->tokens, &state->info);
67
68 for (i = 0; i < state->info.num_properties; ++i) {
69 if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_ORIGIN)
70 state->origin_lower_left = state->info.properties[i].data[0];
71 else if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_PIXEL_CENTER)
72 state->pixel_center_integer = state->info.properties[i].data[0];
73 }
74
75 return state;
76 }
77
78
79 void
80 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
81 {
82 struct softpipe_context *softpipe = softpipe_context(pipe);
83
84 draw_flush(softpipe->draw);
85
86 if (softpipe->fs == fs)
87 return;
88
89 draw_flush(softpipe->draw);
90
91 softpipe->fs = fs;
92
93 softpipe->dirty |= SP_NEW_FS;
94 }
95
96
97 void
98 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
99 {
100 struct softpipe_context *softpipe = softpipe_context(pipe);
101 struct sp_fragment_shader *state = fs;
102
103 assert(fs != softpipe_context(pipe)->fs);
104
105 if (softpipe->fs_machine->Tokens == state->shader.tokens) {
106 /* unbind the shader from the tgsi executor if we're
107 * deleting it.
108 */
109 tgsi_exec_machine_bind_shader(softpipe->fs_machine, NULL, 0, NULL);
110 }
111
112 state->delete( state );
113 }
114
115
116 void *
117 softpipe_create_vs_state(struct pipe_context *pipe,
118 const struct pipe_shader_state *templ)
119 {
120 struct softpipe_context *softpipe = softpipe_context(pipe);
121 struct sp_vertex_shader *state;
122
123 state = CALLOC_STRUCT(sp_vertex_shader);
124 if (state == NULL )
125 goto fail;
126
127 /* copy shader tokens, the ones passed in will go away.
128 */
129 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
130 if (state->shader.tokens == NULL)
131 goto fail;
132
133 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
134 if (state->draw_data == NULL)
135 goto fail;
136
137 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
138
139 return state;
140
141 fail:
142 if (state) {
143 FREE( (void *)state->shader.tokens );
144 FREE( state->draw_data );
145 FREE( state );
146 }
147 return NULL;
148 }
149
150
151 void
152 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
153 {
154 struct softpipe_context *softpipe = softpipe_context(pipe);
155
156 softpipe->vs = (struct sp_vertex_shader *) vs;
157
158 draw_bind_vertex_shader(softpipe->draw,
159 (softpipe->vs ? softpipe->vs->draw_data : NULL));
160
161 softpipe->dirty |= SP_NEW_VS;
162 }
163
164
165 void
166 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
167 {
168 struct softpipe_context *softpipe = softpipe_context(pipe);
169
170 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
171
172 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
173 FREE( (void *)state->shader.tokens );
174 FREE( state );
175 }
176
177 void
178 softpipe_set_constant_buffer(struct pipe_context *pipe,
179 uint shader, uint index,
180 struct pipe_resource *constants)
181 {
182 struct softpipe_context *softpipe = softpipe_context(pipe);
183 unsigned size = constants ? constants->width0 : 0;
184 const void *data = constants ? softpipe_resource(constants)->data : NULL;
185
186 assert(shader < PIPE_SHADER_TYPES);
187
188 draw_flush(softpipe->draw);
189
190 /* note: reference counting */
191 pipe_resource_reference(&softpipe->constants[shader][index], constants);
192
193 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
194 draw_set_mapped_constant_buffer(softpipe->draw, shader, index, data, size);
195 }
196
197 softpipe->mapped_constants[shader][index] = data;
198 softpipe->const_buffer_size[shader][index] = size;
199
200 softpipe->dirty |= SP_NEW_CONSTANTS;
201 }
202
203
204 void *
205 softpipe_create_gs_state(struct pipe_context *pipe,
206 const struct pipe_shader_state *templ)
207 {
208 struct softpipe_context *softpipe = softpipe_context(pipe);
209 struct sp_geometry_shader *state;
210
211 state = CALLOC_STRUCT(sp_geometry_shader);
212 if (state == NULL )
213 goto fail;
214
215 /* debug */
216 if (softpipe->dump_gs)
217 tgsi_dump(templ->tokens, 0);
218
219 /* copy shader tokens, the ones passed in will go away.
220 */
221 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
222 if (state->shader.tokens == NULL)
223 goto fail;
224
225 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
226 if (state->draw_data == NULL)
227 goto fail;
228
229 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
230
231 return state;
232
233 fail:
234 if (state) {
235 FREE( (void *)state->shader.tokens );
236 FREE( state->draw_data );
237 FREE( state );
238 }
239 return NULL;
240 }
241
242
243 void
244 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
245 {
246 struct softpipe_context *softpipe = softpipe_context(pipe);
247
248 softpipe->gs = (struct sp_geometry_shader *)gs;
249
250 draw_bind_geometry_shader(softpipe->draw,
251 (softpipe->gs ? softpipe->gs->draw_data : NULL));
252
253 softpipe->dirty |= SP_NEW_GS;
254 }
255
256
257 void
258 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
259 {
260 struct softpipe_context *softpipe = softpipe_context(pipe);
261
262 struct sp_geometry_shader *state =
263 (struct sp_geometry_shader *)gs;
264
265 draw_delete_geometry_shader(softpipe->draw,
266 (state) ? state->draw_data : 0);
267 FREE(state);
268 }