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 "draw/draw_context.h"
37 #include "draw/draw_vs.h"
38 #include "draw/draw_gs.h"
39 #include "tgsi/tgsi_dump.h"
40 #include "tgsi/tgsi_exec.h"
41 #include "tgsi/tgsi_scan.h"
42 #include "tgsi/tgsi_parse.h"
46 softpipe_create_fs_state(struct pipe_context
*pipe
,
47 const struct pipe_shader_state
*templ
)
49 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
50 struct sp_fragment_shader
*state
;
54 if (softpipe
->dump_fs
)
55 tgsi_dump(templ
->tokens
, 0);
58 state
= softpipe_create_fs_sse( softpipe
, templ
);
60 state
= softpipe_create_fs_exec( softpipe
, templ
);
65 /* get/save the summary info for this shader */
66 tgsi_scan_shader(templ
->tokens
, &state
->info
);
68 for (i
= 0; i
< state
->info
.num_properties
; ++i
) {
69 if (state
->info
.properties
[i
].name
== TGSI_PROPERTY_FS_COORD_ORIGIN
)
70 state
->origin_lower_left
= state
->info
.properties
[i
].data
[0];
71 else if (state
->info
.properties
[i
].name
== TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
)
72 state
->pixel_center_integer
= state
->info
.properties
[i
].data
[0];
80 softpipe_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
82 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
84 draw_flush(softpipe
->draw
);
86 if (softpipe
->fs
== fs
)
89 draw_flush(softpipe
->draw
);
93 softpipe
->dirty
|= SP_NEW_FS
;
98 softpipe_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
100 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
101 struct sp_fragment_shader
*state
= fs
;
103 assert(fs
!= softpipe_context(pipe
)->fs
);
105 if (softpipe
->fs_machine
->Tokens
== state
->shader
.tokens
) {
106 /* unbind the shader from the tgsi executor if we're
109 tgsi_exec_machine_bind_shader(softpipe
->fs_machine
, NULL
, 0, NULL
);
112 state
->delete( state
);
117 softpipe_create_vs_state(struct pipe_context
*pipe
,
118 const struct pipe_shader_state
*templ
)
120 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
121 struct sp_vertex_shader
*state
;
123 state
= CALLOC_STRUCT(sp_vertex_shader
);
127 /* copy shader tokens, the ones passed in will go away.
129 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
130 if (state
->shader
.tokens
== NULL
)
133 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, templ
);
134 if (state
->draw_data
== NULL
)
137 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
143 FREE( (void *)state
->shader
.tokens
);
144 FREE( state
->draw_data
);
152 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
154 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
156 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
158 draw_bind_vertex_shader(softpipe
->draw
,
159 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
161 softpipe
->dirty
|= SP_NEW_VS
;
166 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
168 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
170 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
172 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
173 FREE( (void *)state
->shader
.tokens
);
178 softpipe_set_constant_buffer(struct pipe_context
*pipe
,
179 uint shader
, uint index
,
180 struct pipe_resource
*constants
)
182 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
183 unsigned size
= constants
? constants
->width0
: 0;
184 const void *data
= constants
? softpipe_resource(constants
)->data
: NULL
;
186 assert(shader
< PIPE_SHADER_TYPES
);
188 draw_flush(softpipe
->draw
);
190 /* note: reference counting */
191 pipe_resource_reference(&softpipe
->constants
[shader
][index
], constants
);
193 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
194 draw_set_mapped_constant_buffer(softpipe
->draw
, shader
, index
, data
, size
);
197 softpipe
->mapped_constants
[shader
][index
] = data
;
198 softpipe
->dirty
|= SP_NEW_CONSTANTS
;
203 softpipe_create_gs_state(struct pipe_context
*pipe
,
204 const struct pipe_shader_state
*templ
)
206 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
207 struct sp_geometry_shader
*state
;
209 state
= CALLOC_STRUCT(sp_geometry_shader
);
214 if (softpipe
->dump_gs
)
215 tgsi_dump(templ
->tokens
, 0);
217 /* copy shader tokens, the ones passed in will go away.
219 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
220 if (state
->shader
.tokens
== NULL
)
223 state
->draw_data
= draw_create_geometry_shader(softpipe
->draw
, templ
);
224 if (state
->draw_data
== NULL
)
227 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
233 FREE( (void *)state
->shader
.tokens
);
234 FREE( state
->draw_data
);
242 softpipe_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
244 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
246 softpipe
->gs
= (struct sp_geometry_shader
*)gs
;
248 draw_bind_geometry_shader(softpipe
->draw
,
249 (softpipe
->gs
? softpipe
->gs
->draw_data
: NULL
));
251 softpipe
->dirty
|= SP_NEW_GS
;
256 softpipe_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
258 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
260 struct sp_geometry_shader
*state
=
261 (struct sp_geometry_shader
*)gs
;
263 draw_delete_geometry_shader(softpipe
->draw
,
264 (state
) ? state
->draw_data
: 0);