softpipe: use the polygon stipple utility module
[mesa.git] / src / gallium / drivers / softpipe / sp_state_shader.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 "util/u_pstipple.h"
37 #include "draw/draw_context.h"
38 #include "draw/draw_vs.h"
39 #include "draw/draw_gs.h"
40 #include "tgsi/tgsi_dump.h"
41 #include "tgsi/tgsi_exec.h"
42 #include "tgsi/tgsi_scan.h"
43 #include "tgsi/tgsi_parse.h"
44
45
46 /**
47 * Create a new fragment shader variant.
48 */
49 static struct sp_fragment_shader_variant *
50 create_fs_variant(struct softpipe_context *softpipe,
51 struct sp_fragment_shader *fs,
52 const struct sp_fragment_shader_variant_key *key)
53 {
54 struct sp_fragment_shader_variant *var;
55 struct pipe_shader_state *stipple_fs = NULL, *curfs = &fs->shader;
56 unsigned unit = 0;
57
58 if (key->polygon_stipple) {
59 /* get new shader that implements polygon stippling */
60 stipple_fs = util_pstipple_create_fragment_shader(&softpipe->pipe,
61 curfs, &unit);
62 curfs = stipple_fs;
63 }
64
65 /* codegen, create variant object */
66 var = softpipe_create_fs_variant_sse(softpipe, curfs);
67 if (!var) {
68 var = softpipe_create_fs_variant_exec(softpipe, curfs);
69 }
70
71 if (var) {
72 var->key = *key;
73 var->tokens = tgsi_dup_tokens(curfs->tokens);
74 var->stipple_sampler_unit = unit;
75
76 tgsi_scan_shader(var->tokens, &var->info);
77
78 /* See comments elsewhere about draw fragment shaders */
79 #if 0
80 /* draw's fs state */
81 var->draw_shader = draw_create_fragment_shader(softpipe->draw,
82 &fs->shader);
83 if (!var->draw_shader) {
84 var->delete(var);
85 FREE((void *) var->tokens);
86 return NULL;
87 }
88 #endif
89
90 /* insert variant into linked list */
91 var->next = fs->variants;
92 fs->variants = var;
93 }
94
95 if (stipple_fs) {
96 free((void *) stipple_fs->tokens);
97 free(stipple_fs);
98 }
99
100 return var;
101 }
102
103
104 struct sp_fragment_shader_variant *
105 softpipe_find_fs_variant(struct softpipe_context *sp,
106 struct sp_fragment_shader *fs,
107 const struct sp_fragment_shader_variant_key *key)
108 {
109 struct sp_fragment_shader_variant *var;
110
111 for (var = fs->variants; var; var = var->next) {
112 if (memcmp(&var->key, key, sizeof(*key)) == 0) {
113 /* found it */
114 return var;
115 }
116 }
117
118 return create_fs_variant(sp, fs, key);
119 }
120
121
122 static void *
123 softpipe_create_fs_state(struct pipe_context *pipe,
124 const struct pipe_shader_state *templ)
125 {
126 struct softpipe_context *softpipe = softpipe_context(pipe);
127 struct sp_fragment_shader *state = CALLOC_STRUCT(sp_fragment_shader);
128
129 /* debug */
130 if (softpipe->dump_fs)
131 tgsi_dump(templ->tokens, 0);
132
133 /* we need to keep a local copy of the tokens */
134 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
135
136 /* draw's fs state */
137 state->draw_shader = draw_create_fragment_shader(softpipe->draw,
138 &state->shader);
139 if (!state->draw_shader) {
140 FREE((void *) state->shader.tokens);
141 FREE(state);
142 return NULL;
143 }
144
145 return state;
146 }
147
148
149 static void
150 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
151 {
152 struct softpipe_context *softpipe = softpipe_context(pipe);
153 struct sp_fragment_shader *state = (struct sp_fragment_shader *) fs;
154
155 if (softpipe->fs == fs)
156 return;
157
158 draw_flush(softpipe->draw);
159
160 softpipe->fs = fs;
161
162 if (fs == NULL)
163 softpipe->fs_variant = NULL;
164
165 if (state)
166 draw_bind_fragment_shader(softpipe->draw,
167 state->draw_shader);
168 else
169 draw_bind_fragment_shader(softpipe->draw, NULL);
170
171 softpipe->dirty |= SP_NEW_FS;
172 }
173
174
175 static void
176 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
177 {
178 struct softpipe_context *softpipe = softpipe_context(pipe);
179 struct sp_fragment_shader *state = fs;
180 struct sp_fragment_shader_variant *var, *next_var;
181
182 assert(fs != softpipe->fs);
183
184 if (softpipe->fs_machine->Tokens == state->shader.tokens) {
185 /* unbind the shader from the tgsi executor if we're
186 * deleting it.
187 */
188 tgsi_exec_machine_bind_shader(softpipe->fs_machine, NULL, 0, NULL);
189 }
190
191 /* delete variants */
192 for (var = state->variants; var; var = next_var) {
193 next_var = var->next;
194
195 assert(var != softpipe->fs_variant);
196
197 /* See comments elsewhere about draw fragment shaders */
198 #if 0
199 draw_delete_fragment_shader(softpipe->draw, var->draw_shader);
200 #endif
201
202 var->delete(var);
203 }
204
205 draw_delete_fragment_shader(softpipe->draw, state->draw_shader);
206
207 FREE((void *) state->shader.tokens);
208 }
209
210
211 static void *
212 softpipe_create_vs_state(struct pipe_context *pipe,
213 const struct pipe_shader_state *templ)
214 {
215 struct softpipe_context *softpipe = softpipe_context(pipe);
216 struct sp_vertex_shader *state;
217
218 state = CALLOC_STRUCT(sp_vertex_shader);
219 if (state == NULL )
220 goto fail;
221
222 /* copy shader tokens, the ones passed in will go away.
223 */
224 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
225 if (state->shader.tokens == NULL)
226 goto fail;
227
228 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
229 if (state->draw_data == NULL)
230 goto fail;
231
232 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
233
234 return state;
235
236 fail:
237 if (state) {
238 FREE( (void *)state->shader.tokens );
239 FREE( state->draw_data );
240 FREE( state );
241 }
242 return NULL;
243 }
244
245
246 static void
247 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
248 {
249 struct softpipe_context *softpipe = softpipe_context(pipe);
250
251 softpipe->vs = (struct sp_vertex_shader *) vs;
252
253 draw_bind_vertex_shader(softpipe->draw,
254 (softpipe->vs ? softpipe->vs->draw_data : NULL));
255
256 softpipe->dirty |= SP_NEW_VS;
257 }
258
259
260 static void
261 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
262 {
263 struct softpipe_context *softpipe = softpipe_context(pipe);
264
265 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
266
267 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
268 FREE( (void *)state->shader.tokens );
269 FREE( state );
270 }
271
272
273 static void *
274 softpipe_create_gs_state(struct pipe_context *pipe,
275 const struct pipe_shader_state *templ)
276 {
277 struct softpipe_context *softpipe = softpipe_context(pipe);
278 struct sp_geometry_shader *state;
279
280 state = CALLOC_STRUCT(sp_geometry_shader);
281 if (state == NULL )
282 goto fail;
283
284 /* debug */
285 if (softpipe->dump_gs)
286 tgsi_dump(templ->tokens, 0);
287
288 /* copy shader tokens, the ones passed in will go away.
289 */
290 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
291 if (state->shader.tokens == NULL)
292 goto fail;
293
294 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
295 if (state->draw_data == NULL)
296 goto fail;
297
298 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
299
300 return state;
301
302 fail:
303 if (state) {
304 FREE( (void *)state->shader.tokens );
305 FREE( state->draw_data );
306 FREE( state );
307 }
308 return NULL;
309 }
310
311
312 static void
313 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
314 {
315 struct softpipe_context *softpipe = softpipe_context(pipe);
316
317 softpipe->gs = (struct sp_geometry_shader *)gs;
318
319 draw_bind_geometry_shader(softpipe->draw,
320 (softpipe->gs ? softpipe->gs->draw_data : NULL));
321
322 softpipe->dirty |= SP_NEW_GS;
323 }
324
325
326 static void
327 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
328 {
329 struct softpipe_context *softpipe = softpipe_context(pipe);
330
331 struct sp_geometry_shader *state =
332 (struct sp_geometry_shader *)gs;
333
334 draw_delete_geometry_shader(softpipe->draw,
335 (state) ? state->draw_data : 0);
336 FREE(state);
337 }
338
339
340 static void
341 softpipe_set_constant_buffer(struct pipe_context *pipe,
342 uint shader, uint index,
343 struct pipe_resource *constants)
344 {
345 struct softpipe_context *softpipe = softpipe_context(pipe);
346 unsigned size = constants ? constants->width0 : 0;
347 const void *data = constants ? softpipe_resource(constants)->data : NULL;
348
349 assert(shader < PIPE_SHADER_TYPES);
350
351 draw_flush(softpipe->draw);
352
353 /* note: reference counting */
354 pipe_resource_reference(&softpipe->constants[shader][index], constants);
355
356 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
357 draw_set_mapped_constant_buffer(softpipe->draw, shader, index, data, size);
358 }
359
360 softpipe->mapped_constants[shader][index] = data;
361 softpipe->const_buffer_size[shader][index] = size;
362
363 softpipe->dirty |= SP_NEW_CONSTANTS;
364 }
365
366
367 void
368 softpipe_init_shader_funcs(struct pipe_context *pipe)
369 {
370 pipe->create_fs_state = softpipe_create_fs_state;
371 pipe->bind_fs_state = softpipe_bind_fs_state;
372 pipe->delete_fs_state = softpipe_delete_fs_state;
373
374 pipe->create_vs_state = softpipe_create_vs_state;
375 pipe->bind_vs_state = softpipe_bind_vs_state;
376 pipe->delete_vs_state = softpipe_delete_vs_state;
377
378 pipe->create_gs_state = softpipe_create_gs_state;
379 pipe->bind_gs_state = softpipe_bind_gs_state;
380 pipe->delete_gs_state = softpipe_delete_gs_state;
381
382 pipe->set_constant_buffer = softpipe_set_constant_buffer;
383 }