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 draw_flush(softpipe
->draw
);
96 if (softpipe
->fs
== fs
)
99 draw_flush(softpipe
->draw
);
103 draw_bind_fragment_shader(softpipe
->draw
,
104 (softpipe
->fs
? softpipe
->fs
->draw_shader
: NULL
));
106 softpipe
->dirty
|= SP_NEW_FS
;
111 softpipe_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
113 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
114 struct sp_fragment_shader
*state
= fs
;
116 assert(fs
!= softpipe_context(pipe
)->fs
);
118 if (softpipe
->fs_machine
->Tokens
== state
->shader
.tokens
) {
119 /* unbind the shader from the tgsi executor if we're
122 tgsi_exec_machine_bind_shader(softpipe
->fs_machine
, NULL
, 0, NULL
);
125 draw_delete_fragment_shader(softpipe
->draw
, state
->draw_shader
);
127 state
->delete( state
);
132 softpipe_create_vs_state(struct pipe_context
*pipe
,
133 const struct pipe_shader_state
*templ
)
135 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
136 struct sp_vertex_shader
*state
;
138 state
= CALLOC_STRUCT(sp_vertex_shader
);
142 /* copy shader tokens, the ones passed in will go away.
144 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
145 if (state
->shader
.tokens
== NULL
)
148 state
->draw_data
= draw_create_vertex_shader(softpipe
->draw
, templ
);
149 if (state
->draw_data
== NULL
)
152 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
158 FREE( (void *)state
->shader
.tokens
);
159 FREE( state
->draw_data
);
167 softpipe_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
169 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
171 softpipe
->vs
= (struct sp_vertex_shader
*) vs
;
173 draw_bind_vertex_shader(softpipe
->draw
,
174 (softpipe
->vs
? softpipe
->vs
->draw_data
: NULL
));
176 softpipe
->dirty
|= SP_NEW_VS
;
181 softpipe_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
183 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
185 struct sp_vertex_shader
*state
= (struct sp_vertex_shader
*) vs
;
187 draw_delete_vertex_shader(softpipe
->draw
, state
->draw_data
);
188 FREE( (void *)state
->shader
.tokens
);
194 softpipe_create_gs_state(struct pipe_context
*pipe
,
195 const struct pipe_shader_state
*templ
)
197 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
198 struct sp_geometry_shader
*state
;
200 state
= CALLOC_STRUCT(sp_geometry_shader
);
205 if (softpipe
->dump_gs
)
206 tgsi_dump(templ
->tokens
, 0);
208 /* copy shader tokens, the ones passed in will go away.
210 state
->shader
.tokens
= tgsi_dup_tokens(templ
->tokens
);
211 if (state
->shader
.tokens
== NULL
)
214 state
->draw_data
= draw_create_geometry_shader(softpipe
->draw
, templ
);
215 if (state
->draw_data
== NULL
)
218 state
->max_sampler
= state
->draw_data
->info
.file_max
[TGSI_FILE_SAMPLER
];
224 FREE( (void *)state
->shader
.tokens
);
225 FREE( state
->draw_data
);
233 softpipe_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
235 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
237 softpipe
->gs
= (struct sp_geometry_shader
*)gs
;
239 draw_bind_geometry_shader(softpipe
->draw
,
240 (softpipe
->gs
? softpipe
->gs
->draw_data
: NULL
));
242 softpipe
->dirty
|= SP_NEW_GS
;
247 softpipe_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
249 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
251 struct sp_geometry_shader
*state
=
252 (struct sp_geometry_shader
*)gs
;
254 draw_delete_geometry_shader(softpipe
->draw
,
255 (state
) ? state
->draw_data
: 0);
261 softpipe_set_constant_buffer(struct pipe_context
*pipe
,
262 uint shader
, uint index
,
263 struct pipe_resource
*constants
)
265 struct softpipe_context
*softpipe
= softpipe_context(pipe
);
266 unsigned size
= constants
? constants
->width0
: 0;
267 const void *data
= constants
? softpipe_resource(constants
)->data
: NULL
;
269 assert(shader
< PIPE_SHADER_TYPES
);
271 draw_flush(softpipe
->draw
);
273 /* note: reference counting */
274 pipe_resource_reference(&softpipe
->constants
[shader
][index
], constants
);
276 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
277 draw_set_mapped_constant_buffer(softpipe
->draw
, shader
, index
, data
, size
);
280 softpipe
->mapped_constants
[shader
][index
] = data
;
281 softpipe
->const_buffer_size
[shader
][index
] = size
;
283 softpipe
->dirty
|= SP_NEW_CONSTANTS
;
288 softpipe_init_shader_funcs(struct pipe_context
*pipe
)
290 pipe
->create_fs_state
= softpipe_create_fs_state
;
291 pipe
->bind_fs_state
= softpipe_bind_fs_state
;
292 pipe
->delete_fs_state
= softpipe_delete_fs_state
;
294 pipe
->create_vs_state
= softpipe_create_vs_state
;
295 pipe
->bind_vs_state
= softpipe_bind_vs_state
;
296 pipe
->delete_vs_state
= softpipe_delete_vs_state
;
298 pipe
->create_gs_state
= softpipe_create_gs_state
;
299 pipe
->bind_gs_state
= softpipe_bind_gs_state
;
300 pipe
->delete_gs_state
= softpipe_delete_gs_state
;
302 pipe
->set_constant_buffer
= softpipe_set_constant_buffer
;