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 "tgsi/tgsi_dump.h"
39 #include "tgsi/tgsi_exec.h"
40 #include "tgsi/tgsi_scan.h"
41 #include "tgsi/tgsi_parse.h"
45 softpipe_create_fs_state(struct pipe_context
*pipe
,
46 const struct pipe_shader_state
*templ
)
48 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
49 struct sp_fragment_shader
*state
;
53 if (softpipe
->dump_fs
)
54 tgsi_dump(templ
->tokens
, 0);
57 state
= softpipe_create_fs_sse( softpipe
, templ
);
59 state
= softpipe_create_fs_exec( softpipe
, templ
);
64 /* get/save the summary info for this shader */
65 tgsi_scan_shader(templ
->tokens
, &state
->info
);
67 for (i
= 0; i
< state
->info
.num_properties
; ++i
) {
68 if (state
->info
.properties
[i
].name
== TGSI_PROPERTY_FS_COORD_ORIGIN
)
69 state
->origin_lower_left
= state
->info
.properties
[i
].data
[0];
70 else if (state
->info
.properties
[i
].name
== TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
)
71 state
->pixel_center_integer
= state
->info
.properties
[i
].data
[0];
79 softpipe_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
81 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
83 draw_flush(softpipe
->draw
);
85 if (softpipe
->fs
== fs
)
88 draw_flush(softpipe
->draw
);
92 softpipe
->dirty
|= SP_NEW_FS
;
97 softpipe_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
99 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
100 struct sp_fragment_shader
*state
= fs
;
102 assert(fs
!= softpipe_context(pipe
)->fs
);
104 if (softpipe
->fs_machine
->Tokens
== state
->shader
.tokens
) {
105 /* unbind the shader from the tgsi executor if we're
108 tgsi_exec_machine_bind_shader(softpipe
->fs_machine
, NULL
, 0, NULL
);
111 state
->delete( state
);
116 softpipe_create_vs_state(struct pipe_context
*pipe
,
117 const struct pipe_shader_state
*templ
)
119 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
120 struct sp_vertex_shader
*state
;
122 state
= CALLOC_STRUCT(sp_vertex_shader
);
126 /* copy shader tokens, the ones passed in will go away.
128 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
129 if (state
->shader
.tokens
== NULL
)
132 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, templ
);
133 if (state
->draw_data
== NULL
)
136 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
142 FREE( (void *)state
->shader
.tokens
);
143 FREE( state
->draw_data
);
151 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
153 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
155 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
157 draw_bind_vertex_shader(softpipe
->draw
,
158 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
160 softpipe
->dirty
|= SP_NEW_VS
;
165 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
167 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
169 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
171 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
172 FREE( (void *)state
->shader
.tokens
);
177 softpipe_set_constant_buffer(struct pipe_context
*pipe
,
178 uint shader
, uint index
,
179 struct pipe_resource
*constants
)
181 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
182 unsigned size
= constants
? constants
->width0
: 0;
183 const void *data
= constants
? softpipe_resource(constants
)->data
: NULL
;
185 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
)
231 FREE( (void *)state
->shader
.tokens
);
232 FREE( state
->draw_data
);
240 softpipe_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
242 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
244 softpipe
->gs
= (struct sp_geometry_shader
*)gs
;
246 draw_bind_geometry_shader(softpipe
->draw
,
247 (softpipe
->gs
? softpipe
->gs
->draw_data
: NULL
));
249 softpipe
->dirty
|= SP_NEW_GS
;
254 softpipe_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
256 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
258 struct sp_geometry_shader
*state
=
259 (struct sp_geometry_shader
*)gs
;
261 draw_delete_geometry_shader(softpipe
->draw
,
262 (state
) ? state
->draw_data
: 0);