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