1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "sp_context.h"
29 #include "sp_screen.h"
32 #include "sp_texture.h"
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"
49 * Create a new fragment shader variant.
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
)
56 struct sp_fragment_shader_variant
*var
;
57 struct pipe_shader_state
*curfs
= &fs
->shader
;
59 /* codegen, create variant object */
60 var
= softpipe_create_fs_variant_exec(softpipe
);
65 #if DO_PSTIPPLE_IN_HELPER_MODULE
66 if (key
->polygon_stipple
) {
67 /* get new shader that implements polygon stippling */
69 util_pstipple_create_fragment_shader(curfs
->tokens
,
70 &var
->stipple_sampler_unit
, 0,
76 var
->tokens
= tgsi_dup_tokens(curfs
->tokens
);
77 var
->stipple_sampler_unit
= 0;
80 tgsi_scan_shader(var
->tokens
, &var
->info
);
82 /* See comments elsewhere about draw fragment shaders */
85 var
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
87 if (!var
->draw_shader
) {
89 FREE((void *) var
->tokens
);
94 /* insert variant into linked list */
95 var
->next
= fs
->variants
;
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
)
108 struct sp_fragment_shader_variant
*var
;
110 for (var
= fs
->variants
; var
; var
= var
->next
) {
111 if (memcmp(&var
->key
, key
, sizeof(*key
)) == 0) {
117 return create_fs_variant(sp
, fs
, key
);
121 softpipe_shader_db(struct pipe_context
*pipe
, const struct tgsi_token
*tokens
)
123 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
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
);
137 softpipe_create_shader_state(struct pipe_context
*pipe
,
138 struct pipe_shader_state
*shader
,
139 const struct pipe_shader_state
*templ
,
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
);
147 shader
->stream_output
= templ
->stream_output
;
150 tgsi_dump(shader
->tokens
, 0);
152 softpipe_shader_db(pipe
, shader
->tokens
);
156 softpipe_create_fs_state(struct pipe_context
*pipe
,
157 const struct pipe_shader_state
*templ
)
159 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
160 struct sp_fragment_shader
*state
= CALLOC_STRUCT(sp_fragment_shader
);
162 softpipe_create_shader_state(pipe
, &state
->shader
, templ
,
163 sp_debug
& SP_DBG_FS
);
165 /* draw's fs state */
166 state
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
168 if (!state
->draw_shader
) {
169 tgsi_free_tokens(state
->shader
.tokens
);
179 softpipe_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
181 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
182 struct sp_fragment_shader
*state
= (struct sp_fragment_shader
*) fs
;
184 if (softpipe
->fs
== fs
)
187 draw_flush(softpipe
->draw
);
191 /* This depends on the current fragment shader and must always be
192 * re-validated before use.
194 softpipe
->fs_variant
= NULL
;
197 draw_bind_fragment_shader(softpipe
->draw
,
200 draw_bind_fragment_shader(softpipe
->draw
, NULL
);
202 softpipe
->dirty
|= SP_NEW_FS
;
207 softpipe_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
209 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
210 struct sp_fragment_shader
*state
= fs
;
211 struct sp_fragment_shader_variant
*var
, *next_var
;
213 assert(fs
!= softpipe
->fs
);
215 /* delete variants */
216 for (var
= state
->variants
; var
; var
= next_var
) {
217 next_var
= var
->next
;
219 assert(var
!= softpipe
->fs_variant
);
221 /* See comments elsewhere about draw fragment shaders */
223 draw_delete_fragment_shader(softpipe
->draw
, var
->draw_shader
);
226 var
->delete(var
, softpipe
->fs_machine
);
229 draw_delete_fragment_shader(softpipe
->draw
, state
->draw_shader
);
231 tgsi_free_tokens(state
->shader
.tokens
);
237 softpipe_create_vs_state(struct pipe_context
*pipe
,
238 const struct pipe_shader_state
*templ
)
240 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
241 struct sp_vertex_shader
*state
;
243 state
= CALLOC_STRUCT(sp_vertex_shader
);
247 softpipe_create_shader_state(pipe
, &state
->shader
, templ
,
248 sp_debug
& SP_DBG_VS
);
249 if (!state
->shader
.tokens
)
252 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, &state
->shader
);
253 if (state
->draw_data
== NULL
)
256 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
262 tgsi_free_tokens(state
->shader
.tokens
);
263 FREE( state
->draw_data
);
271 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
273 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
275 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
277 draw_bind_vertex_shader(softpipe
->draw
,
278 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
280 softpipe
->dirty
|= SP_NEW_VS
;
285 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
287 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
289 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
291 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
292 tgsi_free_tokens(state
->shader
.tokens
);
298 softpipe_create_gs_state(struct pipe_context
*pipe
,
299 const struct pipe_shader_state
*templ
)
301 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
302 struct sp_geometry_shader
*state
;
304 state
= CALLOC_STRUCT(sp_geometry_shader
);
308 softpipe_create_shader_state(pipe
, &state
->shader
, templ
,
309 sp_debug
& SP_DBG_GS
);
312 state
->draw_data
= draw_create_geometry_shader(softpipe
->draw
, templ
);
313 if (state
->draw_data
== NULL
)
316 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
323 tgsi_free_tokens(state
->shader
.tokens
);
324 FREE( state
->draw_data
);
332 softpipe_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
334 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
336 softpipe
->gs
= (struct sp_geometry_shader
*)gs
;
338 draw_bind_geometry_shader(softpipe
->draw
,
339 (softpipe
->gs
? softpipe
->gs
->draw_data
: NULL
));
341 softpipe
->dirty
|= SP_NEW_GS
;
346 softpipe_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
348 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
350 struct sp_geometry_shader
*state
=
351 (struct sp_geometry_shader
*)gs
;
353 draw_delete_geometry_shader(softpipe
->draw
,
354 (state
) ? state
->draw_data
: 0);
356 tgsi_free_tokens(state
->shader
.tokens
);
362 softpipe_set_constant_buffer(struct pipe_context
*pipe
,
363 enum pipe_shader_type shader
, uint index
,
364 const struct pipe_constant_buffer
*cb
)
366 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
367 struct pipe_resource
*constants
= cb
? cb
->buffer
: NULL
;
371 assert(shader
< PIPE_SHADER_TYPES
);
373 if (cb
&& cb
->user_buffer
) {
374 constants
= softpipe_user_buffer_create(pipe
->screen
,
375 (void *) cb
->user_buffer
,
377 PIPE_BIND_CONSTANT_BUFFER
);
380 size
= cb
? cb
->buffer_size
: 0;
381 data
= constants
? softpipe_resource_data(constants
) : NULL
;
383 data
= (const char *) data
+ cb
->buffer_offset
;
385 draw_flush(softpipe
->draw
);
387 /* note: reference counting */
388 pipe_resource_reference(&softpipe
->constants
[shader
][index
], constants
);
390 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
391 draw_set_mapped_constant_buffer(softpipe
->draw
, shader
, index
, data
, size
);
394 softpipe
->mapped_constants
[shader
][index
] = data
;
395 softpipe
->const_buffer_size
[shader
][index
] = size
;
397 softpipe
->dirty
|= SP_NEW_CONSTANTS
;
399 if (cb
&& cb
->user_buffer
) {
400 pipe_resource_reference(&constants
, NULL
);
405 softpipe_create_compute_state(struct pipe_context
*pipe
,
406 const struct pipe_compute_state
*templ
)
408 const struct tgsi_token
*tokens
;
409 struct sp_compute_shader
*state
;
410 if (templ
->ir_type
!= PIPE_SHADER_IR_TGSI
)
413 tokens
= templ
->prog
;
415 if (sp_debug
& SP_DBG_CS
)
416 tgsi_dump(tokens
, 0);
418 softpipe_shader_db(pipe
, tokens
);
420 state
= CALLOC_STRUCT(sp_compute_shader
);
422 state
->shader
= *templ
;
423 state
->tokens
= tgsi_dup_tokens(tokens
);
424 tgsi_scan_shader(state
->tokens
, &state
->info
);
426 state
->max_sampler
= state
->info
.file_max
[TGSI_FILE_SAMPLER
];
432 softpipe_bind_compute_state(struct pipe_context
*pipe
,
435 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
436 struct sp_compute_shader
*state
= (struct sp_compute_shader
*)cs
;
437 if (softpipe
->cs
== state
)
440 softpipe
->cs
= state
;
444 softpipe_delete_compute_state(struct pipe_context
*pipe
,
447 ASSERTED
struct softpipe_context
*softpipe
= softpipe_context(pipe
);
448 struct sp_compute_shader
*state
= (struct sp_compute_shader
*)cs
;
450 assert(softpipe
->cs
!= state
);
451 tgsi_free_tokens(state
->tokens
);
456 softpipe_init_shader_funcs(struct pipe_context
*pipe
)
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
;
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
;
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
;
470 pipe
->set_constant_buffer
= softpipe_set_constant_buffer
;
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
;