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