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
);
67 state
->draw_shader
= draw_create_fragment_shader(softpipe
->draw
, templ
);
68 if (!state
->draw_shader
) {
69 state
->delete( state
);
73 /* get/save the summary info for this shader */
74 tgsi_scan_shader(templ
->tokens
, &state
->info
);
76 for (i
= 0; i
< state
->info
.num_properties
; ++i
) {
77 if (state
->info
.properties
[i
].name
== TGSI_PROPERTY_FS_COORD_ORIGIN
)
78 state
->origin_lower_left
= state
->info
.properties
[i
].data
[0];
79 else if (state
->info
.properties
[i
].name
== TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
)
80 state
->pixel_center_integer
= state
->info
.properties
[i
].data
[0];
81 else if (state
->info
.properties
[i
].name
== TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
82 state
->color0_writes_all_cbufs
= state
->info
.properties
[i
].data
[0];
90 softpipe_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
92 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
94 if (softpipe
->fs
== fs
)
97 draw_flush(softpipe
->draw
);
101 draw_bind_fragment_shader(softpipe
->draw
,
102 (softpipe
->fs
? softpipe
->fs
->draw_shader
: NULL
));
104 softpipe
->dirty
|= SP_NEW_FS
;
109 softpipe_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
111 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
112 struct sp_fragment_shader
*state
= fs
;
114 assert(fs
!= softpipe_context(pipe
)->fs
);
116 if (softpipe
->fs_machine
->Tokens
== state
->shader
.tokens
) {
117 /* unbind the shader from the tgsi executor if we're
120 tgsi_exec_machine_bind_shader(softpipe
->fs_machine
, NULL
, 0, NULL
);
123 draw_delete_fragment_shader(softpipe
->draw
, state
->draw_shader
);
125 state
->delete( state
);
130 softpipe_create_vs_state(struct pipe_context
*pipe
,
131 const struct pipe_shader_state
*templ
)
133 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
134 struct sp_vertex_shader
*state
;
136 state
= CALLOC_STRUCT(sp_vertex_shader
);
140 /* copy shader tokens, the ones passed in will go away.
142 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
143 if (state
->shader
.tokens
== NULL
)
146 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, templ
);
147 if (state
->draw_data
== NULL
)
150 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
156 FREE( (void *)state
->shader
.tokens
);
157 FREE( state
->draw_data
);
165 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
167 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
169 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
171 draw_bind_vertex_shader(softpipe
->draw
,
172 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
174 softpipe
->dirty
|= SP_NEW_VS
;
179 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
181 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
183 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
185 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
186 FREE( (void *)state
->shader
.tokens
);
192 softpipe_create_gs_state(struct pipe_context
*pipe
,
193 const struct pipe_shader_state
*templ
)
195 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
196 struct sp_geometry_shader
*state
;
198 state
= CALLOC_STRUCT(sp_geometry_shader
);
203 if (softpipe
->dump_gs
)
204 tgsi_dump(templ
->tokens
, 0);
206 /* copy shader tokens, the ones passed in will go away.
208 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
209 if (state
->shader
.tokens
== NULL
)
212 state
->draw_data
= draw_create_geometry_shader(softpipe
->draw
, templ
);
213 if (state
->draw_data
== NULL
)
216 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
222 FREE( (void *)state
->shader
.tokens
);
223 FREE( state
->draw_data
);
231 softpipe_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
233 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
235 softpipe
->gs
= (struct sp_geometry_shader
*)gs
;
237 draw_bind_geometry_shader(softpipe
->draw
,
238 (softpipe
->gs
? softpipe
->gs
->draw_data
: NULL
));
240 softpipe
->dirty
|= SP_NEW_GS
;
245 softpipe_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
247 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
249 struct sp_geometry_shader
*state
=
250 (struct sp_geometry_shader
*)gs
;
252 draw_delete_geometry_shader(softpipe
->draw
,
253 (state
) ? state
->draw_data
: 0);
259 softpipe_set_constant_buffer(struct pipe_context
*pipe
,
260 uint shader
, uint index
,
261 struct pipe_resource
*constants
)
263 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
264 unsigned size
= constants
? constants
->width0
: 0;
265 const void *data
= constants
? softpipe_resource(constants
)->data
: NULL
;
267 assert(shader
< PIPE_SHADER_TYPES
);
269 draw_flush(softpipe
->draw
);
271 /* note: reference counting */
272 pipe_resource_reference(&softpipe
->constants
[shader
][index
], constants
);
274 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
275 draw_set_mapped_constant_buffer(softpipe
->draw
, shader
, index
, data
, size
);
278 softpipe
->mapped_constants
[shader
][index
] = data
;
279 softpipe
->const_buffer_size
[shader
][index
] = size
;
281 softpipe
->dirty
|= SP_NEW_CONSTANTS
;
286 softpipe_init_shader_funcs(struct pipe_context
*pipe
)
288 pipe
->create_fs_state
= softpipe_create_fs_state
;
289 pipe
->bind_fs_state
= softpipe_bind_fs_state
;
290 pipe
->delete_fs_state
= softpipe_delete_fs_state
;
292 pipe
->create_vs_state
= softpipe_create_vs_state
;
293 pipe
->bind_vs_state
= softpipe_bind_vs_state
;
294 pipe
->delete_vs_state
= softpipe_delete_vs_state
;
296 pipe
->create_gs_state
= softpipe_create_gs_state
;
297 pipe
->bind_gs_state
= softpipe_bind_gs_state
;
298 pipe
->delete_gs_state
= softpipe_delete_gs_state
;
300 pipe
->set_constant_buffer
= softpipe_set_constant_buffer
;