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"
31 #include "sp_texture.h"
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_scan.h"
42 #include "tgsi/tgsi_parse.h"
46 * Create a new fragment shader variant.
48 static struct sp_fragment_shader_variant
*
49 create_fs_variant(struct softpipe_context
*softpipe
,
50 struct sp_fragment_shader
*fs
,
51 const struct sp_fragment_shader_variant_key
*key
)
53 struct sp_fragment_shader_variant
*var
;
54 struct pipe_shader_state
*curfs
= &fs
->shader
;
56 /* codegen, create variant object */
57 var
= softpipe_create_fs_variant_exec(softpipe
);
62 #if DO_PSTIPPLE_IN_HELPER_MODULE
63 if (key
->polygon_stipple
) {
64 /* get new shader that implements polygon stippling */
66 util_pstipple_create_fragment_shader(curfs
->tokens
,
67 &var
->stipple_sampler_unit
, 0);
72 var
->tokens
= tgsi_dup_tokens(curfs
->tokens
);
73 var
->stipple_sampler_unit
= 0;
76 tgsi_scan_shader(var
->tokens
, &var
->info
);
78 /* See comments elsewhere about draw fragment shaders */
81 var
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
83 if (!var
->draw_shader
) {
85 FREE((void *) var
->tokens
);
90 /* insert variant into linked list */
91 var
->next
= fs
->variants
;
99 struct sp_fragment_shader_variant
*
100 softpipe_find_fs_variant(struct softpipe_context
*sp
,
101 struct sp_fragment_shader
*fs
,
102 const struct sp_fragment_shader_variant_key
*key
)
104 struct sp_fragment_shader_variant
*var
;
106 for (var
= fs
->variants
; var
; var
= var
->next
) {
107 if (memcmp(&var
->key
, key
, sizeof(*key
)) == 0) {
113 return create_fs_variant(sp
, fs
, key
);
118 softpipe_create_fs_state(struct pipe_context
*pipe
,
119 const struct pipe_shader_state
*templ
)
121 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
122 struct sp_fragment_shader
*state
= CALLOC_STRUCT(sp_fragment_shader
);
125 if (softpipe
->dump_fs
)
126 tgsi_dump(templ
->tokens
, 0);
128 /* we need to keep a local copy of the tokens */
129 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
131 /* draw's fs state */
132 state
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
134 if (!state
->draw_shader
) {
135 tgsi_free_tokens(state
->shader
.tokens
);
145 softpipe_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
147 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
148 struct sp_fragment_shader
*state
= (struct sp_fragment_shader
*) fs
;
150 if (softpipe
->fs
== fs
)
153 draw_flush(softpipe
->draw
);
157 /* This depends on the current fragment shader and must always be
158 * re-validated before use.
160 softpipe
->fs_variant
= NULL
;
163 draw_bind_fragment_shader(softpipe
->draw
,
166 draw_bind_fragment_shader(softpipe
->draw
, NULL
);
168 softpipe
->dirty
|= SP_NEW_FS
;
173 softpipe_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
175 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
176 struct sp_fragment_shader
*state
= fs
;
177 struct sp_fragment_shader_variant
*var
, *next_var
;
179 assert(fs
!= softpipe
->fs
);
181 /* delete variants */
182 for (var
= state
->variants
; var
; var
= next_var
) {
183 next_var
= var
->next
;
185 assert(var
!= softpipe
->fs_variant
);
187 /* See comments elsewhere about draw fragment shaders */
189 draw_delete_fragment_shader(softpipe
->draw
, var
->draw_shader
);
192 var
->delete(var
, softpipe
->fs_machine
);
195 draw_delete_fragment_shader(softpipe
->draw
, state
->draw_shader
);
197 tgsi_free_tokens(state
->shader
.tokens
);
203 softpipe_create_vs_state(struct pipe_context
*pipe
,
204 const struct pipe_shader_state
*templ
)
206 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
207 struct sp_vertex_shader
*state
;
209 state
= CALLOC_STRUCT(sp_vertex_shader
);
213 /* copy shader tokens, the ones passed in will go away.
215 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
216 if (state
->shader
.tokens
== NULL
)
219 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, templ
);
220 if (state
->draw_data
== NULL
)
223 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
229 tgsi_free_tokens(state
->shader
.tokens
);
230 FREE( state
->draw_data
);
238 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
240 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
242 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
244 draw_bind_vertex_shader(softpipe
->draw
,
245 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
247 softpipe
->dirty
|= SP_NEW_VS
;
252 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
254 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
256 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
258 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
259 tgsi_free_tokens(state
->shader
.tokens
);
265 softpipe_create_gs_state(struct pipe_context
*pipe
,
266 const struct pipe_shader_state
*templ
)
268 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
269 struct sp_geometry_shader
*state
;
271 state
= CALLOC_STRUCT(sp_geometry_shader
);
275 state
->shader
= *templ
;
279 if (softpipe
->dump_gs
)
280 tgsi_dump(templ
->tokens
, 0);
282 /* copy shader tokens, the ones passed in will go away.
284 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
285 if (state
->shader
.tokens
== NULL
)
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 uint shader
, uint index
,
340 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
);
382 softpipe_init_shader_funcs(struct pipe_context
*pipe
)
384 pipe
->create_fs_state
= softpipe_create_fs_state
;
385 pipe
->bind_fs_state
= softpipe_bind_fs_state
;
386 pipe
->delete_fs_state
= softpipe_delete_fs_state
;
388 pipe
->create_vs_state
= softpipe_create_vs_state
;
389 pipe
->bind_vs_state
= softpipe_bind_vs_state
;
390 pipe
->delete_vs_state
= softpipe_delete_vs_state
;
392 pipe
->create_gs_state
= softpipe_create_gs_state
;
393 pipe
->bind_gs_state
= softpipe_bind_gs_state
;
394 pipe
->delete_gs_state
= softpipe_delete_gs_state
;
396 pipe
->set_constant_buffer
= softpipe_set_constant_buffer
;