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_buffer.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_scan.h"
40 #include "tgsi/tgsi_parse.h"
44 softpipe_create_fs_state(struct pipe_context *pipe,
45 const struct pipe_shader_state *templ)
47 struct softpipe_context *softpipe = softpipe_context(pipe);
48 struct sp_fragment_shader *state;
52 if (softpipe->dump_fs)
53 tgsi_dump(templ->tokens, 0);
56 state = softpipe_create_fs_sse( softpipe, templ );
58 state = softpipe_create_fs_exec( softpipe, templ );
63 /* get/save the summary info for this shader */
64 tgsi_scan_shader(templ->tokens, &state->info);
66 for (i = 0; i < state->info.num_properties; ++i) {
67 if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_ORIGIN)
68 state->origin_lower_left = state->info.properties[i].data[0];
69 else if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_PIXEL_CENTER)
70 state->pixel_center_integer = state->info.properties[i].data[0];
78 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
80 struct softpipe_context *softpipe = softpipe_context(pipe);
82 draw_flush(softpipe->draw);
84 if (softpipe->fs == fs)
87 draw_flush(softpipe->draw);
91 softpipe->dirty |= SP_NEW_FS;
96 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
98 struct sp_fragment_shader *state = fs;
100 assert(fs != softpipe_context(pipe)->fs);
102 state->delete( state );
107 softpipe_create_vs_state(struct pipe_context *pipe,
108 const struct pipe_shader_state *templ)
110 struct softpipe_context *softpipe = softpipe_context(pipe);
111 struct sp_vertex_shader *state;
113 state = CALLOC_STRUCT(sp_vertex_shader);
117 /* copy shader tokens, the ones passed in will go away.
119 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
120 if (state->shader.tokens == NULL)
123 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
124 if (state->draw_data == NULL)
127 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
133 FREE( (void *)state->shader.tokens );
134 FREE( state->draw_data );
142 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
144 struct softpipe_context *softpipe = softpipe_context(pipe);
146 softpipe->vs = (struct sp_vertex_shader *) vs;
148 draw_bind_vertex_shader(softpipe->draw,
149 (softpipe->vs ? softpipe->vs->draw_data : NULL));
151 softpipe->dirty |= SP_NEW_VS;
156 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
158 struct softpipe_context *softpipe = softpipe_context(pipe);
160 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
162 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
163 FREE( (void *)state->shader.tokens );
168 softpipe_set_constant_buffer(struct pipe_context *pipe,
169 uint shader, uint index,
170 struct pipe_buffer *constants)
172 struct softpipe_context *softpipe = softpipe_context(pipe);
173 unsigned size = constants ? constants->size : 0;
174 const void *data = constants ? softpipe_buffer(constants)->data : NULL;
176 assert(shader < PIPE_SHADER_TYPES);
179 draw_flush(softpipe->draw);
181 /* note: reference counting */
182 pipe_buffer_reference(&softpipe->constants[shader][index], constants);
184 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
185 draw_set_mapped_constant_buffer(softpipe->draw, shader, index, data, size);
188 softpipe->mapped_constants[shader][index] = data;
189 softpipe->dirty |= SP_NEW_CONSTANTS;
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)
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);