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_exec.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
*stipple_fs
= NULL
, *curfs
= &fs
->shader
;
58 if (key
->polygon_stipple
) {
59 /* get new shader that implements polygon stippling */
60 stipple_fs
= util_pstipple_create_fragment_shader(&softpipe
->pipe
,
65 /* codegen, create variant object */
66 var
= softpipe_create_fs_variant_sse(softpipe
, curfs
);
68 var
= softpipe_create_fs_variant_exec(softpipe
, curfs
);
73 var
->tokens
= tgsi_dup_tokens(curfs
->tokens
);
74 var
->stipple_sampler_unit
= unit
;
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
;
96 free((void *) stipple_fs
->tokens
);
104 struct sp_fragment_shader_variant
*
105 softpipe_find_fs_variant(struct softpipe_context
*sp
,
106 struct sp_fragment_shader
*fs
,
107 const struct sp_fragment_shader_variant_key
*key
)
109 struct sp_fragment_shader_variant
*var
;
111 for (var
= fs
->variants
; var
; var
= var
->next
) {
112 if (memcmp(&var
->key
, key
, sizeof(*key
)) == 0) {
118 return create_fs_variant(sp
, fs
, key
);
123 softpipe_create_fs_state(struct pipe_context
*pipe
,
124 const struct pipe_shader_state
*templ
)
126 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
127 struct sp_fragment_shader
*state
= CALLOC_STRUCT(sp_fragment_shader
);
130 if (softpipe
->dump_fs
)
131 tgsi_dump(templ
->tokens
, 0);
133 /* we need to keep a local copy of the tokens */
134 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
136 /* draw's fs state */
137 state
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
,
139 if (!state
->draw_shader
) {
140 FREE((void *) state
->shader
.tokens
);
150 softpipe_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
152 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
153 struct sp_fragment_shader
*state
= (struct sp_fragment_shader
*) fs
;
155 if (softpipe
->fs
== fs
)
158 draw_flush(softpipe
->draw
);
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 if (softpipe
->fs_machine
->Tokens
== state
->shader
.tokens
) {
185 /* unbind the shader from the tgsi executor if we're
188 tgsi_exec_machine_bind_shader(softpipe
->fs_machine
, NULL
, 0, NULL
);
191 /* delete variants */
192 for (var
= state
->variants
; var
; var
= next_var
) {
193 next_var
= var
->next
;
195 assert(var
!= softpipe
->fs_variant
);
197 /* See comments elsewhere about draw fragment shaders */
199 draw_delete_fragment_shader(softpipe
->draw
, var
->draw_shader
);
205 draw_delete_fragment_shader(softpipe
->draw
, state
->draw_shader
);
207 FREE((void *) state
->shader
.tokens
);
212 softpipe_create_vs_state(struct pipe_context
*pipe
,
213 const struct pipe_shader_state
*templ
)
215 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
216 struct sp_vertex_shader
*state
;
218 state
= CALLOC_STRUCT(sp_vertex_shader
);
222 /* copy shader tokens, the ones passed in will go away.
224 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
225 if (state
->shader
.tokens
== NULL
)
228 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, templ
);
229 if (state
->draw_data
== NULL
)
232 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
238 FREE( (void *)state
->shader
.tokens
);
239 FREE( state
->draw_data
);
247 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
249 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
251 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
253 draw_bind_vertex_shader(softpipe
->draw
,
254 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
256 softpipe
->dirty
|= SP_NEW_VS
;
261 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
263 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
265 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
267 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
268 FREE( (void *)state
->shader
.tokens
);
274 softpipe_create_gs_state(struct pipe_context
*pipe
,
275 const struct pipe_shader_state
*templ
)
277 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
278 struct sp_geometry_shader
*state
;
280 state
= CALLOC_STRUCT(sp_geometry_shader
);
285 if (softpipe
->dump_gs
)
286 tgsi_dump(templ
->tokens
, 0);
288 /* copy shader tokens, the ones passed in will go away.
290 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
291 if (state
->shader
.tokens
== NULL
)
294 state
->draw_data
= draw_create_geometry_shader(softpipe
->draw
, templ
);
295 if (state
->draw_data
== NULL
)
298 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
304 FREE( (void *)state
->shader
.tokens
);
305 FREE( state
->draw_data
);
313 softpipe_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
315 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
317 softpipe
->gs
= (struct sp_geometry_shader
*)gs
;
319 draw_bind_geometry_shader(softpipe
->draw
,
320 (softpipe
->gs
? softpipe
->gs
->draw_data
: NULL
));
322 softpipe
->dirty
|= SP_NEW_GS
;
327 softpipe_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
329 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
331 struct sp_geometry_shader
*state
=
332 (struct sp_geometry_shader
*)gs
;
334 draw_delete_geometry_shader(softpipe
->draw
,
335 (state
) ? state
->draw_data
: 0);
341 softpipe_set_constant_buffer(struct pipe_context
*pipe
,
342 uint shader
, uint index
,
343 struct pipe_resource
*constants
)
345 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
346 unsigned size
= constants
? constants
->width0
: 0;
347 const void *data
= constants
? softpipe_resource(constants
)->data
: NULL
;
349 assert(shader
< PIPE_SHADER_TYPES
);
351 draw_flush(softpipe
->draw
);
353 /* note: reference counting */
354 pipe_resource_reference(&softpipe
->constants
[shader
][index
], constants
);
356 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
357 draw_set_mapped_constant_buffer(softpipe
->draw
, shader
, index
, data
, size
);
360 softpipe
->mapped_constants
[shader
][index
] = data
;
361 softpipe
->const_buffer_size
[shader
][index
] = size
;
363 softpipe
->dirty
|= SP_NEW_CONSTANTS
;
368 softpipe_init_shader_funcs(struct pipe_context
*pipe
)
370 pipe
->create_fs_state
= softpipe_create_fs_state
;
371 pipe
->bind_fs_state
= softpipe_bind_fs_state
;
372 pipe
->delete_fs_state
= softpipe_delete_fs_state
;
374 pipe
->create_vs_state
= softpipe_create_vs_state
;
375 pipe
->bind_vs_state
= softpipe_bind_vs_state
;
376 pipe
->delete_vs_state
= softpipe_delete_vs_state
;
378 pipe
->create_gs_state
= softpipe_create_gs_state
;
379 pipe
->bind_gs_state
= softpipe_bind_gs_state
;
380 pipe
->delete_gs_state
= softpipe_delete_gs_state
;
382 pipe
->set_constant_buffer
= softpipe_set_constant_buffer
;