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