softpipe: Convert to comma-separated SOFTPIPE_DEBUG for debug options.
[mesa.git] / src / gallium / drivers / softpipe / sp_state_shader.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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_screen.h"
30 #include "sp_state.h"
31 #include "sp_fs.h"
32 #include "sp_texture.h"
33
34 #include "pipe/p_defines.h"
35 #include "util/u_memory.h"
36 #include "util/u_inlines.h"
37 #include "util/u_pstipple.h"
38 #include "draw/draw_context.h"
39 #include "draw/draw_vs.h"
40 #include "draw/draw_gs.h"
41 #include "tgsi/tgsi_dump.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 *curfs = &fs->shader;
56
57 /* codegen, create variant object */
58 var = softpipe_create_fs_variant_exec(softpipe);
59
60 if (var) {
61 var->key = *key;
62
63 #if DO_PSTIPPLE_IN_HELPER_MODULE
64 if (key->polygon_stipple) {
65 /* get new shader that implements polygon stippling */
66 var->tokens =
67 util_pstipple_create_fragment_shader(curfs->tokens,
68 &var->stipple_sampler_unit, 0,
69 TGSI_FILE_INPUT);
70 }
71 else
72 #endif
73 {
74 var->tokens = tgsi_dup_tokens(curfs->tokens);
75 var->stipple_sampler_unit = 0;
76 }
77
78 tgsi_scan_shader(var->tokens, &var->info);
79
80 /* See comments elsewhere about draw fragment shaders */
81 #if 0
82 /* draw's fs state */
83 var->draw_shader = draw_create_fragment_shader(softpipe->draw,
84 &fs->shader);
85 if (!var->draw_shader) {
86 var->delete(var);
87 FREE((void *) var->tokens);
88 return NULL;
89 }
90 #endif
91
92 /* insert variant into linked list */
93 var->next = fs->variants;
94 fs->variants = var;
95 }
96
97 return var;
98 }
99
100
101 struct sp_fragment_shader_variant *
102 softpipe_find_fs_variant(struct softpipe_context *sp,
103 struct sp_fragment_shader *fs,
104 const struct sp_fragment_shader_variant_key *key)
105 {
106 struct sp_fragment_shader_variant *var;
107
108 for (var = fs->variants; var; var = var->next) {
109 if (memcmp(&var->key, key, sizeof(*key)) == 0) {
110 /* found it */
111 return var;
112 }
113 }
114
115 return create_fs_variant(sp, fs, key);
116 }
117
118 static void
119 softpipe_create_shader_state(struct pipe_shader_state *shader,
120 const struct pipe_shader_state *templ,
121 bool debug)
122 {
123 assert(templ->type == PIPE_SHADER_IR_TGSI);
124 shader->type = PIPE_SHADER_IR_TGSI;
125 /* we need to keep a local copy of the tokens */
126 shader->tokens = tgsi_dup_tokens(templ->tokens);
127
128 shader->stream_output = templ->stream_output;
129
130 if (debug)
131 tgsi_dump(shader->tokens, 0);
132 }
133
134 static void *
135 softpipe_create_fs_state(struct pipe_context *pipe,
136 const struct pipe_shader_state *templ)
137 {
138 struct softpipe_context *softpipe = softpipe_context(pipe);
139 struct sp_fragment_shader *state = CALLOC_STRUCT(sp_fragment_shader);
140
141 softpipe_create_shader_state(&state->shader, templ, sp_debug & SP_DBG_FS);
142
143 /* draw's fs state */
144 state->draw_shader = draw_create_fragment_shader(softpipe->draw,
145 &state->shader);
146 if (!state->draw_shader) {
147 tgsi_free_tokens(state->shader.tokens);
148 FREE(state);
149 return NULL;
150 }
151
152 return state;
153 }
154
155
156 static void
157 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
158 {
159 struct softpipe_context *softpipe = softpipe_context(pipe);
160 struct sp_fragment_shader *state = (struct sp_fragment_shader *) fs;
161
162 if (softpipe->fs == fs)
163 return;
164
165 draw_flush(softpipe->draw);
166
167 softpipe->fs = fs;
168
169 /* This depends on the current fragment shader and must always be
170 * re-validated before use.
171 */
172 softpipe->fs_variant = NULL;
173
174 if (state)
175 draw_bind_fragment_shader(softpipe->draw,
176 state->draw_shader);
177 else
178 draw_bind_fragment_shader(softpipe->draw, NULL);
179
180 softpipe->dirty |= SP_NEW_FS;
181 }
182
183
184 static void
185 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
186 {
187 struct softpipe_context *softpipe = softpipe_context(pipe);
188 struct sp_fragment_shader *state = fs;
189 struct sp_fragment_shader_variant *var, *next_var;
190
191 assert(fs != softpipe->fs);
192
193 /* delete variants */
194 for (var = state->variants; var; var = next_var) {
195 next_var = var->next;
196
197 assert(var != softpipe->fs_variant);
198
199 /* See comments elsewhere about draw fragment shaders */
200 #if 0
201 draw_delete_fragment_shader(softpipe->draw, var->draw_shader);
202 #endif
203
204 var->delete(var, softpipe->fs_machine);
205 }
206
207 draw_delete_fragment_shader(softpipe->draw, state->draw_shader);
208
209 tgsi_free_tokens(state->shader.tokens);
210 FREE(state);
211 }
212
213
214 static void *
215 softpipe_create_vs_state(struct pipe_context *pipe,
216 const struct pipe_shader_state *templ)
217 {
218 struct softpipe_context *softpipe = softpipe_context(pipe);
219 struct sp_vertex_shader *state;
220
221 state = CALLOC_STRUCT(sp_vertex_shader);
222 if (!state)
223 goto fail;
224
225 softpipe_create_shader_state(&state->shader, templ, sp_debug & SP_DBG_VS);
226 if (!state->shader.tokens)
227 goto fail;
228
229 state->draw_data = draw_create_vertex_shader(softpipe->draw, &state->shader);
230 if (state->draw_data == NULL)
231 goto fail;
232
233 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
234
235 return state;
236
237 fail:
238 if (state) {
239 tgsi_free_tokens(state->shader.tokens);
240 FREE( state->draw_data );
241 FREE( state );
242 }
243 return NULL;
244 }
245
246
247 static void
248 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
249 {
250 struct softpipe_context *softpipe = softpipe_context(pipe);
251
252 softpipe->vs = (struct sp_vertex_shader *) vs;
253
254 draw_bind_vertex_shader(softpipe->draw,
255 (softpipe->vs ? softpipe->vs->draw_data : NULL));
256
257 softpipe->dirty |= SP_NEW_VS;
258 }
259
260
261 static void
262 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
263 {
264 struct softpipe_context *softpipe = softpipe_context(pipe);
265
266 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
267
268 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
269 tgsi_free_tokens(state->shader.tokens);
270 FREE( state );
271 }
272
273
274 static void *
275 softpipe_create_gs_state(struct pipe_context *pipe,
276 const struct pipe_shader_state *templ)
277 {
278 struct softpipe_context *softpipe = softpipe_context(pipe);
279 struct sp_geometry_shader *state;
280
281 state = CALLOC_STRUCT(sp_geometry_shader);
282 if (!state)
283 goto fail;
284
285 softpipe_create_shader_state(&state->shader, templ, sp_debug & SP_DBG_GS);
286
287 if (templ->tokens) {
288 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
289 if (state->draw_data == NULL)
290 goto fail;
291
292 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
293 }
294
295 return state;
296
297 fail:
298 if (state) {
299 tgsi_free_tokens(state->shader.tokens);
300 FREE( state->draw_data );
301 FREE( state );
302 }
303 return NULL;
304 }
305
306
307 static void
308 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
309 {
310 struct softpipe_context *softpipe = softpipe_context(pipe);
311
312 softpipe->gs = (struct sp_geometry_shader *)gs;
313
314 draw_bind_geometry_shader(softpipe->draw,
315 (softpipe->gs ? softpipe->gs->draw_data : NULL));
316
317 softpipe->dirty |= SP_NEW_GS;
318 }
319
320
321 static void
322 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
323 {
324 struct softpipe_context *softpipe = softpipe_context(pipe);
325
326 struct sp_geometry_shader *state =
327 (struct sp_geometry_shader *)gs;
328
329 draw_delete_geometry_shader(softpipe->draw,
330 (state) ? state->draw_data : 0);
331
332 tgsi_free_tokens(state->shader.tokens);
333 FREE(state);
334 }
335
336
337 static void
338 softpipe_set_constant_buffer(struct pipe_context *pipe,
339 enum pipe_shader_type shader, uint index,
340 const struct pipe_constant_buffer *cb)
341 {
342 struct softpipe_context *softpipe = softpipe_context(pipe);
343 struct pipe_resource *constants = cb ? cb->buffer : NULL;
344 unsigned size;
345 const void *data;
346
347 assert(shader < PIPE_SHADER_TYPES);
348
349 if (cb && cb->user_buffer) {
350 constants = softpipe_user_buffer_create(pipe->screen,
351 (void *) cb->user_buffer,
352 cb->buffer_size,
353 PIPE_BIND_CONSTANT_BUFFER);
354 }
355
356 size = cb ? cb->buffer_size : 0;
357 data = constants ? softpipe_resource_data(constants) : NULL;
358 if (data)
359 data = (const char *) data + cb->buffer_offset;
360
361 draw_flush(softpipe->draw);
362
363 /* note: reference counting */
364 pipe_resource_reference(&softpipe->constants[shader][index], constants);
365
366 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
367 draw_set_mapped_constant_buffer(softpipe->draw, shader, index, data, size);
368 }
369
370 softpipe->mapped_constants[shader][index] = data;
371 softpipe->const_buffer_size[shader][index] = size;
372
373 softpipe->dirty |= SP_NEW_CONSTANTS;
374
375 if (cb && cb->user_buffer) {
376 pipe_resource_reference(&constants, NULL);
377 }
378 }
379
380 static void *
381 softpipe_create_compute_state(struct pipe_context *pipe,
382 const struct pipe_compute_state *templ)
383 {
384 const struct tgsi_token *tokens;
385 struct sp_compute_shader *state;
386 if (templ->ir_type != PIPE_SHADER_IR_TGSI)
387 return NULL;
388
389 tokens = templ->prog;
390 /* debug */
391 if (sp_debug & SP_DBG_CS)
392 tgsi_dump(tokens, 0);
393
394 state = CALLOC_STRUCT(sp_compute_shader);
395
396 state->shader = *templ;
397 state->tokens = tgsi_dup_tokens(tokens);
398 tgsi_scan_shader(state->tokens, &state->info);
399
400 state->max_sampler = state->info.file_max[TGSI_FILE_SAMPLER];
401
402 return state;
403 }
404
405 static void
406 softpipe_bind_compute_state(struct pipe_context *pipe,
407 void *cs)
408 {
409 struct softpipe_context *softpipe = softpipe_context(pipe);
410 struct sp_compute_shader *state = (struct sp_compute_shader *)cs;
411 if (softpipe->cs == state)
412 return;
413
414 softpipe->cs = state;
415 }
416
417 static void
418 softpipe_delete_compute_state(struct pipe_context *pipe,
419 void *cs)
420 {
421 ASSERTED struct softpipe_context *softpipe = softpipe_context(pipe);
422 struct sp_compute_shader *state = (struct sp_compute_shader *)cs;
423
424 assert(softpipe->cs != state);
425 tgsi_free_tokens(state->tokens);
426 FREE(state);
427 }
428
429 void
430 softpipe_init_shader_funcs(struct pipe_context *pipe)
431 {
432 pipe->create_fs_state = softpipe_create_fs_state;
433 pipe->bind_fs_state = softpipe_bind_fs_state;
434 pipe->delete_fs_state = softpipe_delete_fs_state;
435
436 pipe->create_vs_state = softpipe_create_vs_state;
437 pipe->bind_vs_state = softpipe_bind_vs_state;
438 pipe->delete_vs_state = softpipe_delete_vs_state;
439
440 pipe->create_gs_state = softpipe_create_gs_state;
441 pipe->bind_gs_state = softpipe_bind_gs_state;
442 pipe->delete_gs_state = softpipe_delete_gs_state;
443
444 pipe->set_constant_buffer = softpipe_set_constant_buffer;
445
446 pipe->create_compute_state = softpipe_create_compute_state;
447 pipe->bind_compute_state = softpipe_bind_compute_state;
448 pipe->delete_compute_state = softpipe_delete_compute_state;
449 }