Merge remote branch 'origin/lp-binning'
[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 "util/u_inlines.h"
35 #include "draw/draw_context.h"
36 #include "draw/draw_vs.h"
37 #include "tgsi/tgsi_dump.h"
38 #include "tgsi/tgsi_scan.h"
39 #include "tgsi/tgsi_parse.h"
40
41
42 void *
43 softpipe_create_fs_state(struct pipe_context *pipe,
44 const struct pipe_shader_state *templ)
45 {
46 struct softpipe_context *softpipe = softpipe_context(pipe);
47 struct sp_fragment_shader *state;
48 unsigned i;
49
50 /* debug */
51 if (softpipe->dump_fs)
52 tgsi_dump(templ->tokens, 0);
53
54 /* codegen */
55 state = softpipe_create_fs_sse( softpipe, templ );
56 if (!state) {
57 state = softpipe_create_fs_exec( softpipe, templ );
58 }
59
60 assert(state);
61
62 /* get/save the summary info for this shader */
63 tgsi_scan_shader(templ->tokens, &state->info);
64
65 for (i = 0; i < state->info.num_properties; ++i) {
66 if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_ORIGIN)
67 state->origin_lower_left = state->info.properties[i].data[0];
68 else if (state->info.properties[i].name == TGSI_PROPERTY_FS_COORD_PIXEL_CENTER)
69 state->pixel_center_integer = state->info.properties[i].data[0];
70 }
71
72 return state;
73 }
74
75
76 void
77 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
78 {
79 struct softpipe_context *softpipe = softpipe_context(pipe);
80
81 draw_flush(softpipe->draw);
82
83 if (softpipe->fs == fs)
84 return;
85
86 draw_flush(softpipe->draw);
87
88 softpipe->fs = fs;
89
90 softpipe->dirty |= SP_NEW_FS;
91 }
92
93
94 void
95 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
96 {
97 struct sp_fragment_shader *state = fs;
98
99 assert(fs != softpipe_context(pipe)->fs);
100
101 state->delete( state );
102 }
103
104
105 void *
106 softpipe_create_vs_state(struct pipe_context *pipe,
107 const struct pipe_shader_state *templ)
108 {
109 struct softpipe_context *softpipe = softpipe_context(pipe);
110 struct sp_vertex_shader *state;
111
112 state = CALLOC_STRUCT(sp_vertex_shader);
113 if (state == NULL )
114 goto fail;
115
116 /* copy shader tokens, the ones passed in will go away.
117 */
118 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
119 if (state->shader.tokens == NULL)
120 goto fail;
121
122 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
123 if (state->draw_data == NULL)
124 goto fail;
125
126 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
127
128 return state;
129
130 fail:
131 if (state) {
132 FREE( (void *)state->shader.tokens );
133 FREE( state->draw_data );
134 FREE( state );
135 }
136 return NULL;
137 }
138
139
140 void
141 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
142 {
143 struct softpipe_context *softpipe = softpipe_context(pipe);
144
145 softpipe->vs = (struct sp_vertex_shader *) vs;
146
147 draw_bind_vertex_shader(softpipe->draw,
148 (softpipe->vs ? softpipe->vs->draw_data : NULL));
149
150 softpipe->dirty |= SP_NEW_VS;
151 }
152
153
154 void
155 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
156 {
157 struct softpipe_context *softpipe = softpipe_context(pipe);
158
159 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
160
161 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
162 FREE( (void *)state->shader.tokens );
163 FREE( state );
164 }
165
166
167
168 void
169 softpipe_set_constant_buffer(struct pipe_context *pipe,
170 uint shader, uint index,
171 struct pipe_buffer *buf)
172 {
173 struct softpipe_context *softpipe = softpipe_context(pipe);
174
175 assert(shader < PIPE_SHADER_TYPES);
176 assert(index < PIPE_MAX_CONSTANT_BUFFERS);
177
178 draw_flush(softpipe->draw);
179
180 /* note: reference counting */
181 pipe_buffer_reference(&softpipe->constants[shader][index], buf);
182
183 softpipe->dirty |= SP_NEW_CONSTANTS;
184 }
185
186 void *
187 softpipe_create_gs_state(struct pipe_context *pipe,
188 const struct pipe_shader_state *templ)
189 {
190 struct softpipe_context *softpipe = softpipe_context(pipe);
191 struct sp_geometry_shader *state;
192
193 state = CALLOC_STRUCT(sp_geometry_shader);
194 if (state == NULL )
195 goto fail;
196
197 /* debug */
198 if (softpipe->dump_gs)
199 tgsi_dump(templ->tokens, 0);
200
201 /* copy shader tokens, the ones passed in will go away.
202 */
203 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
204 if (state->shader.tokens == NULL)
205 goto fail;
206
207 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
208 if (state->draw_data == NULL)
209 goto fail;
210
211 return state;
212
213 fail:
214 if (state) {
215 FREE( (void *)state->shader.tokens );
216 FREE( state->draw_data );
217 FREE( state );
218 }
219 return NULL;
220 }
221
222
223 void
224 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
225 {
226 struct softpipe_context *softpipe = softpipe_context(pipe);
227
228 softpipe->gs = (struct sp_geometry_shader *)gs;
229
230 draw_bind_geometry_shader(softpipe->draw,
231 (softpipe->gs ? softpipe->gs->draw_data : NULL));
232
233 softpipe->dirty |= SP_NEW_GS;
234 }
235
236
237 void
238 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
239 {
240 struct softpipe_context *softpipe = softpipe_context(pipe);
241
242 struct sp_geometry_shader *state =
243 (struct sp_geometry_shader *)gs;
244
245 draw_delete_geometry_shader(softpipe->draw,
246 (state) ? state->draw_data : 0);
247 FREE(state);
248 }