1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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
*stipple_fs
= NULL
, *curfs
= &fs
->shader
;
57 #if DO_PSTIPPLE_IN_HELPER_MODULE
58 if (key
->polygon_stipple
) {
59 /* get new shader that implements polygon stippling */
60 stipple_fs
= util_pstipple_create_fragment_shader(&softpipe
->pipe
,
66 /* codegen, create variant object */
67 var
= softpipe_create_fs_variant_exec(softpipe
, curfs
);
71 var
->tokens
= tgsi_dup_tokens(curfs
->tokens
);
72 var
->stipple_sampler_unit
= unit
;
74 tgsi_scan_shader(var
->tokens
, &var
->info
);
76 /* See comments elsewhere about draw fragment shaders */
79 var
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
81 if (!var
->draw_shader
) {
83 FREE((void *) var
->tokens
);
88 /* insert variant into linked list */
89 var
->next
= fs
->variants
;
94 FREE((void *) stipple_fs
->tokens
);
102 struct sp_fragment_shader_variant
*
103 softpipe_find_fs_variant(struct softpipe_context
*sp
,
104 struct sp_fragment_shader
*fs
,
105 const struct sp_fragment_shader_variant_key
*key
)
107 struct sp_fragment_shader_variant
*var
;
109 for (var
= fs
->variants
; var
; var
= var
->next
) {
110 if (memcmp(&var
->key
, key
, sizeof(*key
)) == 0) {
116 return create_fs_variant(sp
, fs
, key
);
121 softpipe_create_fs_state(struct pipe_context
*pipe
,
122 const struct pipe_shader_state
*templ
)
124 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
125 struct sp_fragment_shader
*state
= CALLOC_STRUCT(sp_fragment_shader
);
128 if (softpipe
->dump_fs
)
129 tgsi_dump(templ
->tokens
, 0);
131 /* we need to keep a local copy of the tokens */
132 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
134 /* draw's fs state */
135 state
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
137 if (!state
->draw_shader
) {
138 FREE((void *) state
->shader
.tokens
);
148 softpipe_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
150 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
151 struct sp_fragment_shader
*state
= (struct sp_fragment_shader
*) fs
;
153 if (softpipe
->fs
== fs
)
156 draw_flush(softpipe
->draw
);
160 /* This depends on the current fragment shader and must always be
161 * re-validated before use.
163 softpipe
->fs_variant
= NULL
;
166 draw_bind_fragment_shader(softpipe
->draw
,
169 draw_bind_fragment_shader(softpipe
->draw
, NULL
);
171 softpipe
->dirty
|= SP_NEW_FS
;
176 softpipe_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
178 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
179 struct sp_fragment_shader
*state
= fs
;
180 struct sp_fragment_shader_variant
*var
, *next_var
;
182 assert(fs
!= softpipe
->fs
);
184 /* delete variants */
185 for (var
= state
->variants
; var
; var
= next_var
) {
186 next_var
= var
->next
;
188 assert(var
!= softpipe
->fs_variant
);
190 /* See comments elsewhere about draw fragment shaders */
192 draw_delete_fragment_shader(softpipe
->draw
, var
->draw_shader
);
195 var
->delete(var
, softpipe
->fs_machine
);
198 draw_delete_fragment_shader(softpipe
->draw
, state
->draw_shader
);
200 FREE((void *) state
->shader
.tokens
);
206 softpipe_create_vs_state(struct pipe_context
*pipe
,
207 const struct pipe_shader_state
*templ
)
209 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
210 struct sp_vertex_shader
*state
;
212 state
= CALLOC_STRUCT(sp_vertex_shader
);
216 /* copy shader tokens, the ones passed in will go away.
218 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
219 if (state
->shader
.tokens
== NULL
)
222 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, templ
);
223 if (state
->draw_data
== NULL
)
226 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
232 FREE( (void *)state
->shader
.tokens
);
233 FREE( state
->draw_data
);
241 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
243 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
245 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
247 draw_bind_vertex_shader(softpipe
->draw
,
248 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
250 softpipe
->dirty
|= SP_NEW_VS
;
255 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
257 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
259 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
261 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
262 FREE( (void *)state
->shader
.tokens
);
268 softpipe_create_gs_state(struct pipe_context
*pipe
,
269 const struct pipe_shader_state
*templ
)
271 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
272 struct sp_geometry_shader
*state
;
274 state
= CALLOC_STRUCT(sp_geometry_shader
);
278 state
->shader
= *templ
;
282 if (softpipe
->dump_gs
)
283 tgsi_dump(templ
->tokens
, 0);
285 /* copy shader tokens, the ones passed in will go away.
287 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
288 if (state
->shader
.tokens
== NULL
)
291 state
->draw_data
= draw_create_geometry_shader(softpipe
->draw
, templ
);
292 if (state
->draw_data
== NULL
)
295 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
302 FREE( (void *)state
->shader
.tokens
);
303 FREE( state
->draw_data
);
311 softpipe_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
313 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
315 softpipe
->gs
= (struct sp_geometry_shader
*)gs
;
317 draw_bind_geometry_shader(softpipe
->draw
,
318 (softpipe
->gs
? softpipe
->gs
->draw_data
: NULL
));
320 softpipe
->dirty
|= SP_NEW_GS
;
325 softpipe_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
327 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
329 struct sp_geometry_shader
*state
=
330 (struct sp_geometry_shader
*)gs
;
332 draw_delete_geometry_shader(softpipe
->draw
,
333 (state
) ? state
->draw_data
: 0);
335 FREE((void *) state
->shader
.tokens
);
341 softpipe_set_constant_buffer(struct pipe_context
*pipe
,
342 uint shader
, uint index
,
343 struct pipe_constant_buffer
*cb
)
345 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
346 struct pipe_resource
*constants
= cb
? cb
->buffer
: NULL
;
350 if (cb
&& cb
->user_buffer
) {
351 constants
= softpipe_user_buffer_create(pipe
->screen
,
352 (void *) cb
->user_buffer
,
354 PIPE_BIND_CONSTANT_BUFFER
);
357 size
= constants
? constants
->width0
: 0;
358 data
= constants
? softpipe_resource(constants
)->data
: NULL
;
360 assert(shader
< PIPE_SHADER_TYPES
);
362 draw_flush(softpipe
->draw
);
364 /* note: reference counting */
365 pipe_resource_reference(&softpipe
->constants
[shader
][index
], constants
);
367 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
368 draw_set_mapped_constant_buffer(softpipe
->draw
, shader
, index
, data
, size
);
371 softpipe
->mapped_constants
[shader
][index
] = data
;
372 softpipe
->const_buffer_size
[shader
][index
] = size
;
374 softpipe
->dirty
|= SP_NEW_CONSTANTS
;
376 if (cb
&& cb
->user_buffer
) {
377 pipe_resource_reference(&constants
, NULL
);
383 softpipe_init_shader_funcs(struct pipe_context
*pipe
)
385 pipe
->create_fs_state
= softpipe_create_fs_state
;
386 pipe
->bind_fs_state
= softpipe_bind_fs_state
;
387 pipe
->delete_fs_state
= softpipe_delete_fs_state
;
389 pipe
->create_vs_state
= softpipe_create_vs_state
;
390 pipe
->bind_vs_state
= softpipe_bind_vs_state
;
391 pipe
->delete_vs_state
= softpipe_delete_vs_state
;
393 pipe
->create_gs_state
= softpipe_create_gs_state
;
394 pipe
->bind_gs_state
= softpipe_bind_gs_state
;
395 pipe
->delete_gs_state
= softpipe_delete_gs_state
;
397 pipe
->set_constant_buffer
= softpipe_set_constant_buffer
;