Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / softpipe / sp_state_fs.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "sp_context.h"
29 #include "sp_state.h"
30 #include "sp_fs.h"
31
32 #include "pipe/p_defines.h"
33 #include "util/u_memory.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_vs.h"
36 #include "tgsi/tgsi_dump.h"
37 #include "tgsi/tgsi_scan.h"
38 #include "tgsi/tgsi_parse.h"
39
40
41 void *
42 softpipe_create_fs_state(struct pipe_context *pipe,
43 const struct pipe_shader_state *templ)
44 {
45 struct softpipe_context *softpipe = softpipe_context(pipe);
46 struct sp_fragment_shader *state;
47
48 /* debug */
49 if (softpipe->dump_fs)
50 tgsi_dump(templ->tokens, 0);
51
52 /* codegen */
53 state = softpipe_create_fs_sse( softpipe, templ );
54 if (!state) {
55 state = softpipe_create_fs_exec( softpipe, templ );
56 }
57
58 assert(state);
59
60 /* get/save the summary info for this shader */
61 tgsi_scan_shader(templ->tokens, &state->info);
62
63 return state;
64 }
65
66
67 void
68 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
69 {
70 struct softpipe_context *softpipe = softpipe_context(pipe);
71
72 draw_flush(softpipe->draw);
73
74 if (softpipe->fs == fs)
75 return;
76
77 draw_flush(softpipe->draw);
78
79 softpipe->fs = fs;
80
81 softpipe->dirty |= SP_NEW_FS;
82 }
83
84
85 void
86 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
87 {
88 struct sp_fragment_shader *state = fs;
89
90 assert(fs != softpipe_context(pipe)->fs);
91
92 state->delete( state );
93 }
94
95
96 void *
97 softpipe_create_vs_state(struct pipe_context *pipe,
98 const struct pipe_shader_state *templ)
99 {
100 struct softpipe_context *softpipe = softpipe_context(pipe);
101 struct sp_vertex_shader *state;
102
103 state = CALLOC_STRUCT(sp_vertex_shader);
104 if (state == NULL )
105 goto fail;
106
107 /* copy shader tokens, the ones passed in will go away.
108 */
109 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
110 if (state->shader.tokens == NULL)
111 goto fail;
112
113 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
114 if (state->draw_data == NULL)
115 goto fail;
116
117 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
118
119 return state;
120
121 fail:
122 if (state) {
123 FREE( (void *)state->shader.tokens );
124 FREE( state->draw_data );
125 FREE( state );
126 }
127 return NULL;
128 }
129
130
131 void
132 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
133 {
134 struct softpipe_context *softpipe = softpipe_context(pipe);
135
136 softpipe->vs = (struct sp_vertex_shader *) vs;
137
138 draw_bind_vertex_shader(softpipe->draw,
139 (softpipe->vs ? softpipe->vs->draw_data : NULL));
140
141 softpipe->dirty |= SP_NEW_VS;
142 }
143
144
145 void
146 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
147 {
148 struct softpipe_context *softpipe = softpipe_context(pipe);
149
150 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
151
152 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
153 FREE( (void *)state->shader.tokens );
154 FREE( state );
155 }
156
157
158
159 void
160 softpipe_set_constant_buffer(struct pipe_context *pipe,
161 uint shader, uint index,
162 struct pipe_buffer *buf)
163 {
164 struct softpipe_context *softpipe = softpipe_context(pipe);
165
166 assert(shader < PIPE_SHADER_TYPES);
167 assert(index == 0);
168
169 draw_flush(softpipe->draw);
170
171 /* note: reference counting */
172 pipe_buffer_reference(&softpipe->constants[shader], buf);
173
174 softpipe->dirty |= SP_NEW_CONSTANTS;
175 }
176
177 void *
178 softpipe_create_gs_state(struct pipe_context *pipe,
179 const struct pipe_shader_state *templ)
180 {
181 struct softpipe_context *softpipe = softpipe_context(pipe);
182 struct sp_geometry_shader *state;
183
184 state = CALLOC_STRUCT(sp_geometry_shader);
185 if (state == NULL )
186 goto fail;
187
188 /* debug */
189 if (softpipe->dump_gs)
190 tgsi_dump(templ->tokens, 0);
191
192 /* copy shader tokens, the ones passed in will go away.
193 */
194 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
195 if (state->shader.tokens == NULL)
196 goto fail;
197
198 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
199 if (state->draw_data == NULL)
200 goto fail;
201
202 return state;
203
204 fail:
205 if (state) {
206 FREE( (void *)state->shader.tokens );
207 FREE( state->draw_data );
208 FREE( state );
209 }
210 return NULL;
211 }
212
213
214 void
215 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
216 {
217 struct softpipe_context *softpipe = softpipe_context(pipe);
218
219 softpipe->gs = (struct sp_geometry_shader *)gs;
220
221 draw_bind_geometry_shader(softpipe->draw,
222 (softpipe->gs ? softpipe->gs->draw_data : NULL));
223
224 softpipe->dirty |= SP_NEW_GS;
225 }
226
227
228 void
229 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
230 {
231 struct softpipe_context *softpipe = softpipe_context(pipe);
232
233 struct sp_geometry_shader *state =
234 (struct sp_geometry_shader *)gs;
235
236 draw_delete_geometry_shader(softpipe->draw,
237 (state) ? state->draw_data : 0);
238 FREE(state);
239 }