i965g: Move bootstrap code to targets
[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
187 draw_flush(softpipe->draw);
188
189 /* note: reference counting */
190 pipe_resource_reference(&softpipe->constants[shader][index], constants);
191
192 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
193 draw_set_mapped_constant_buffer(softpipe->draw, shader, index, data, size);
194 }
195
196 softpipe->mapped_constants[shader][index] = data;
197 softpipe->dirty |= SP_NEW_CONSTANTS;
198 }
199
200
201 void *
202 softpipe_create_gs_state(struct pipe_context *pipe,
203 const struct pipe_shader_state *templ)
204 {
205 struct softpipe_context *softpipe = softpipe_context(pipe);
206 struct sp_geometry_shader *state;
207
208 state = CALLOC_STRUCT(sp_geometry_shader);
209 if (state == NULL )
210 goto fail;
211
212 /* debug */
213 if (softpipe->dump_gs)
214 tgsi_dump(templ->tokens, 0);
215
216 /* copy shader tokens, the ones passed in will go away.
217 */
218 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
219 if (state->shader.tokens == NULL)
220 goto fail;
221
222 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
223 if (state->draw_data == NULL)
224 goto fail;
225
226 return state;
227
228 fail:
229 if (state) {
230 FREE( (void *)state->shader.tokens );
231 FREE( state->draw_data );
232 FREE( state );
233 }
234 return NULL;
235 }
236
237
238 void
239 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
240 {
241 struct softpipe_context *softpipe = softpipe_context(pipe);
242
243 softpipe->gs = (struct sp_geometry_shader *)gs;
244
245 draw_bind_geometry_shader(softpipe->draw,
246 (softpipe->gs ? softpipe->gs->draw_data : NULL));
247
248 softpipe->dirty |= SP_NEW_GS;
249 }
250
251
252 void
253 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
254 {
255 struct softpipe_context *softpipe = softpipe_context(pipe);
256
257 struct sp_geometry_shader *state =
258 (struct sp_geometry_shader *)gs;
259
260 draw_delete_geometry_shader(softpipe->draw,
261 (state) ? state->draw_data : 0);
262 FREE(state);
263 }