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_scan.h"
43 #include "tgsi/tgsi_parse.h"
47 * Create a new fragment shader variant.
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
)
54 struct sp_fragment_shader_variant
*var
;
55 struct pipe_shader_state
*curfs
= &fs
->shader
;
57 /* codegen, create variant object */
58 var
= softpipe_create_fs_variant_exec(softpipe
);
63 #if DO_PSTIPPLE_IN_HELPER_MODULE
64 if (key
->polygon_stipple
) {
65 /* get new shader that implements polygon stippling */
67 util_pstipple_create_fragment_shader(curfs
->tokens
,
68 &var
->stipple_sampler_unit
, 0,
74 var
->tokens
= tgsi_dup_tokens(curfs
->tokens
);
75 var
->stipple_sampler_unit
= 0;
78 tgsi_scan_shader(var
->tokens
, &var
->info
);
80 /* See comments elsewhere about draw fragment shaders */
83 var
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
85 if (!var
->draw_shader
) {
87 FREE((void *) var
->tokens
);
92 /* insert variant into linked list */
93 var
->next
= fs
->variants
;
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
)
106 struct sp_fragment_shader_variant
*var
;
108 for (var
= fs
->variants
; var
; var
= var
->next
) {
109 if (memcmp(&var
->key
, key
, sizeof(*key
)) == 0) {
115 return create_fs_variant(sp
, fs
, key
);
119 softpipe_create_shader_state(struct pipe_shader_state
*shader
,
120 const struct pipe_shader_state
*templ
,
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
);
128 shader
->stream_output
= templ
->stream_output
;
131 tgsi_dump(shader
->tokens
, 0);
135 softpipe_create_fs_state(struct pipe_context
*pipe
,
136 const struct pipe_shader_state
*templ
)
138 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
139 struct sp_fragment_shader
*state
= CALLOC_STRUCT(sp_fragment_shader
);
141 softpipe_create_shader_state(&state
->shader
, templ
, sp_debug
& SP_DBG_FS
);
143 /* draw's fs state */
144 state
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
146 if (!state
->draw_shader
) {
147 tgsi_free_tokens(state
->shader
.tokens
);
157 softpipe_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
159 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
160 struct sp_fragment_shader
*state
= (struct sp_fragment_shader
*) fs
;
162 if (softpipe
->fs
== fs
)
165 draw_flush(softpipe
->draw
);
169 /* This depends on the current fragment shader and must always be
170 * re-validated before use.
172 softpipe
->fs_variant
= NULL
;
175 draw_bind_fragment_shader(softpipe
->draw
,
178 draw_bind_fragment_shader(softpipe
->draw
, NULL
);
180 softpipe
->dirty
|= SP_NEW_FS
;
185 softpipe_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
187 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
188 struct sp_fragment_shader
*state
= fs
;
189 struct sp_fragment_shader_variant
*var
, *next_var
;
191 assert(fs
!= softpipe
->fs
);
193 /* delete variants */
194 for (var
= state
->variants
; var
; var
= next_var
) {
195 next_var
= var
->next
;
197 assert(var
!= softpipe
->fs_variant
);
199 /* See comments elsewhere about draw fragment shaders */
201 draw_delete_fragment_shader(softpipe
->draw
, var
->draw_shader
);
204 var
->delete(var
, softpipe
->fs_machine
);
207 draw_delete_fragment_shader(softpipe
->draw
, state
->draw_shader
);
209 tgsi_free_tokens(state
->shader
.tokens
);
215 softpipe_create_vs_state(struct pipe_context
*pipe
,
216 const struct pipe_shader_state
*templ
)
218 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
219 struct sp_vertex_shader
*state
;
221 state
= CALLOC_STRUCT(sp_vertex_shader
);
225 softpipe_create_shader_state(&state
->shader
, templ
, sp_debug
& SP_DBG_VS
);
226 if (!state
->shader
.tokens
)
229 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, &state
->shader
);
230 if (state
->draw_data
== NULL
)
233 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
239 tgsi_free_tokens(state
->shader
.tokens
);
240 FREE( state
->draw_data
);
248 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
250 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
252 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
254 draw_bind_vertex_shader(softpipe
->draw
,
255 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
257 softpipe
->dirty
|= SP_NEW_VS
;
262 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
264 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
266 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
268 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
269 tgsi_free_tokens(state
->shader
.tokens
);
275 softpipe_create_gs_state(struct pipe_context
*pipe
,
276 const struct pipe_shader_state
*templ
)
278 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
279 struct sp_geometry_shader
*state
;
281 state
= CALLOC_STRUCT(sp_geometry_shader
);
285 softpipe_create_shader_state(&state
->shader
, templ
, sp_debug
& SP_DBG_GS
);
288 state
->draw_data
= draw_create_geometry_shader(softpipe
->draw
, templ
);
289 if (state
->draw_data
== NULL
)
292 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
299 tgsi_free_tokens(state
->shader
.tokens
);
300 FREE( state
->draw_data
);
308 softpipe_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
310 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
312 softpipe
->gs
= (struct sp_geometry_shader
*)gs
;
314 draw_bind_geometry_shader(softpipe
->draw
,
315 (softpipe
->gs
? softpipe
->gs
->draw_data
: NULL
));
317 softpipe
->dirty
|= SP_NEW_GS
;
322 softpipe_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
324 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
326 struct sp_geometry_shader
*state
=
327 (struct sp_geometry_shader
*)gs
;
329 draw_delete_geometry_shader(softpipe
->draw
,
330 (state
) ? state
->draw_data
: 0);
332 tgsi_free_tokens(state
->shader
.tokens
);
338 softpipe_set_constant_buffer(struct pipe_context
*pipe
,
339 enum pipe_shader_type shader
, uint index
,
340 const struct pipe_constant_buffer
*cb
)
342 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
343 struct pipe_resource
*constants
= cb
? cb
->buffer
: NULL
;
347 assert(shader
< PIPE_SHADER_TYPES
);
349 if (cb
&& cb
->user_buffer
) {
350 constants
= softpipe_user_buffer_create(pipe
->screen
,
351 (void *) cb
->user_buffer
,
353 PIPE_BIND_CONSTANT_BUFFER
);
356 size
= cb
? cb
->buffer_size
: 0;
357 data
= constants
? softpipe_resource_data(constants
) : NULL
;
359 data
= (const char *) data
+ cb
->buffer_offset
;
361 draw_flush(softpipe
->draw
);
363 /* note: reference counting */
364 pipe_resource_reference(&softpipe
->constants
[shader
][index
], constants
);
366 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
367 draw_set_mapped_constant_buffer(softpipe
->draw
, shader
, index
, data
, size
);
370 softpipe
->mapped_constants
[shader
][index
] = data
;
371 softpipe
->const_buffer_size
[shader
][index
] = size
;
373 softpipe
->dirty
|= SP_NEW_CONSTANTS
;
375 if (cb
&& cb
->user_buffer
) {
376 pipe_resource_reference(&constants
, NULL
);
381 softpipe_create_compute_state(struct pipe_context
*pipe
,
382 const struct pipe_compute_state
*templ
)
384 const struct tgsi_token
*tokens
;
385 struct sp_compute_shader
*state
;
386 if (templ
->ir_type
!= PIPE_SHADER_IR_TGSI
)
389 tokens
= templ
->prog
;
391 if (sp_debug
& SP_DBG_CS
)
392 tgsi_dump(tokens
, 0);
394 state
= CALLOC_STRUCT(sp_compute_shader
);
396 state
->shader
= *templ
;
397 state
->tokens
= tgsi_dup_tokens(tokens
);
398 tgsi_scan_shader(state
->tokens
, &state
->info
);
400 state
->max_sampler
= state
->info
.file_max
[TGSI_FILE_SAMPLER
];
406 softpipe_bind_compute_state(struct pipe_context
*pipe
,
409 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
410 struct sp_compute_shader
*state
= (struct sp_compute_shader
*)cs
;
411 if (softpipe
->cs
== state
)
414 softpipe
->cs
= state
;
418 softpipe_delete_compute_state(struct pipe_context
*pipe
,
421 ASSERTED
struct softpipe_context
*softpipe
= softpipe_context(pipe
);
422 struct sp_compute_shader
*state
= (struct sp_compute_shader
*)cs
;
424 assert(softpipe
->cs
!= state
);
425 tgsi_free_tokens(state
->tokens
);
430 softpipe_init_shader_funcs(struct pipe_context
*pipe
)
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
;
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
;
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
;
444 pipe
->set_constant_buffer
= softpipe_set_constant_buffer
;
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
;