gallium: change set_constant_buffer to be UBO-friendly
[mesa.git] / src / gallium / drivers / softpipe / sp_state_shader.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_texture.h"
32
33 #include "pipe/p_defines.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "util/u_pstipple.h"
37 #include "draw/draw_context.h"
38 #include "draw/draw_vs.h"
39 #include "draw/draw_gs.h"
40 #include "tgsi/tgsi_dump.h"
41 #include "tgsi/tgsi_exec.h"
42 #include "tgsi/tgsi_scan.h"
43 #include "tgsi/tgsi_parse.h"
44
45
46 /**
47 * Create a new fragment shader variant.
48 */
49 static struct sp_fragment_shader_variant *
50 create_fs_variant(struct softpipe_context *softpipe,
51 struct sp_fragment_shader *fs,
52 const struct sp_fragment_shader_variant_key *key)
53 {
54 struct sp_fragment_shader_variant *var;
55 struct pipe_shader_state *stipple_fs = NULL, *curfs = &fs->shader;
56 unsigned unit = 0;
57
58 #if DO_PSTIPPLE_IN_HELPER_MODULE
59 if (key->polygon_stipple) {
60 /* get new shader that implements polygon stippling */
61 stipple_fs = util_pstipple_create_fragment_shader(&softpipe->pipe,
62 curfs, &unit);
63 curfs = stipple_fs;
64 }
65 #endif
66
67 /* codegen, create variant object */
68 var = softpipe_create_fs_variant_exec(softpipe, curfs);
69
70 if (var) {
71 var->key = *key;
72 var->tokens = tgsi_dup_tokens(curfs->tokens);
73 var->stipple_sampler_unit = unit;
74
75 tgsi_scan_shader(var->tokens, &var->info);
76
77 /* See comments elsewhere about draw fragment shaders */
78 #if 0
79 /* draw's fs state */
80 var->draw_shader = draw_create_fragment_shader(softpipe->draw,
81 &fs->shader);
82 if (!var->draw_shader) {
83 var->delete(var);
84 FREE((void *) var->tokens);
85 return NULL;
86 }
87 #endif
88
89 /* insert variant into linked list */
90 var->next = fs->variants;
91 fs->variants = var;
92 }
93
94 if (stipple_fs) {
95 FREE((void *) stipple_fs->tokens);
96 FREE(stipple_fs);
97 }
98
99 return var;
100 }
101
102
103 struct sp_fragment_shader_variant *
104 softpipe_find_fs_variant(struct softpipe_context *sp,
105 struct sp_fragment_shader *fs,
106 const struct sp_fragment_shader_variant_key *key)
107 {
108 struct sp_fragment_shader_variant *var;
109
110 for (var = fs->variants; var; var = var->next) {
111 if (memcmp(&var->key, key, sizeof(*key)) == 0) {
112 /* found it */
113 return var;
114 }
115 }
116
117 return create_fs_variant(sp, fs, key);
118 }
119
120
121 static void *
122 softpipe_create_fs_state(struct pipe_context *pipe,
123 const struct pipe_shader_state *templ)
124 {
125 struct softpipe_context *softpipe = softpipe_context(pipe);
126 struct sp_fragment_shader *state = CALLOC_STRUCT(sp_fragment_shader);
127
128 /* debug */
129 if (softpipe->dump_fs)
130 tgsi_dump(templ->tokens, 0);
131
132 /* we need to keep a local copy of the tokens */
133 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
134
135 /* draw's fs state */
136 state->draw_shader = draw_create_fragment_shader(softpipe->draw,
137 &state->shader);
138 if (!state->draw_shader) {
139 FREE((void *) state->shader.tokens);
140 FREE(state);
141 return NULL;
142 }
143
144 return state;
145 }
146
147
148 static void
149 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
150 {
151 struct softpipe_context *softpipe = softpipe_context(pipe);
152 struct sp_fragment_shader *state = (struct sp_fragment_shader *) fs;
153
154 if (softpipe->fs == fs)
155 return;
156
157 draw_flush(softpipe->draw);
158
159 softpipe->fs = fs;
160
161 if (fs == NULL)
162 softpipe->fs_variant = NULL;
163
164 if (state)
165 draw_bind_fragment_shader(softpipe->draw,
166 state->draw_shader);
167 else
168 draw_bind_fragment_shader(softpipe->draw, NULL);
169
170 softpipe->dirty |= SP_NEW_FS;
171 }
172
173
174 static void
175 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
176 {
177 struct softpipe_context *softpipe = softpipe_context(pipe);
178 struct sp_fragment_shader *state = fs;
179 struct sp_fragment_shader_variant *var, *next_var;
180
181 assert(fs != softpipe->fs);
182
183 if (softpipe->fs_machine->Tokens == state->shader.tokens) {
184 /* unbind the shader from the tgsi executor if we're
185 * deleting it.
186 */
187 tgsi_exec_machine_bind_shader(softpipe->fs_machine, NULL, 0, NULL);
188 }
189
190 /* delete variants */
191 for (var = state->variants; var; var = next_var) {
192 next_var = var->next;
193
194 assert(var != softpipe->fs_variant);
195
196 /* See comments elsewhere about draw fragment shaders */
197 #if 0
198 draw_delete_fragment_shader(softpipe->draw, var->draw_shader);
199 #endif
200
201 var->delete(var);
202 }
203
204 draw_delete_fragment_shader(softpipe->draw, state->draw_shader);
205
206 FREE((void *) state->shader.tokens);
207 FREE(state);
208 }
209
210
211 static void *
212 softpipe_create_vs_state(struct pipe_context *pipe,
213 const struct pipe_shader_state *templ)
214 {
215 struct softpipe_context *softpipe = softpipe_context(pipe);
216 struct sp_vertex_shader *state;
217
218 state = CALLOC_STRUCT(sp_vertex_shader);
219 if (state == NULL )
220 goto fail;
221
222 /* copy shader tokens, the ones passed in will go away.
223 */
224 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
225 if (state->shader.tokens == NULL)
226 goto fail;
227
228 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
229 if (state->draw_data == NULL)
230 goto fail;
231
232 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
233
234 return state;
235
236 fail:
237 if (state) {
238 FREE( (void *)state->shader.tokens );
239 FREE( state->draw_data );
240 FREE( state );
241 }
242 return NULL;
243 }
244
245
246 static void
247 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
248 {
249 struct softpipe_context *softpipe = softpipe_context(pipe);
250
251 softpipe->vs = (struct sp_vertex_shader *) vs;
252
253 draw_bind_vertex_shader(softpipe->draw,
254 (softpipe->vs ? softpipe->vs->draw_data : NULL));
255
256 softpipe->dirty |= SP_NEW_VS;
257 }
258
259
260 static void
261 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
262 {
263 struct softpipe_context *softpipe = softpipe_context(pipe);
264
265 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
266
267 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
268 FREE( (void *)state->shader.tokens );
269 FREE( state );
270 }
271
272
273 static void *
274 softpipe_create_gs_state(struct pipe_context *pipe,
275 const struct pipe_shader_state *templ)
276 {
277 struct softpipe_context *softpipe = softpipe_context(pipe);
278 struct sp_geometry_shader *state;
279
280 state = CALLOC_STRUCT(sp_geometry_shader);
281 if (state == NULL )
282 goto fail;
283
284 /* debug */
285 if (softpipe->dump_gs)
286 tgsi_dump(templ->tokens, 0);
287
288 /* copy shader tokens, the ones passed in will go away.
289 */
290 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
291 if (state->shader.tokens == NULL)
292 goto fail;
293
294 state->draw_data = draw_create_geometry_shader(softpipe->draw, templ);
295 if (state->draw_data == NULL)
296 goto fail;
297
298 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
299
300 return state;
301
302 fail:
303 if (state) {
304 FREE( (void *)state->shader.tokens );
305 FREE( state->draw_data );
306 FREE( state );
307 }
308 return NULL;
309 }
310
311
312 static void
313 softpipe_bind_gs_state(struct pipe_context *pipe, void *gs)
314 {
315 struct softpipe_context *softpipe = softpipe_context(pipe);
316
317 softpipe->gs = (struct sp_geometry_shader *)gs;
318
319 draw_bind_geometry_shader(softpipe->draw,
320 (softpipe->gs ? softpipe->gs->draw_data : NULL));
321
322 softpipe->dirty |= SP_NEW_GS;
323 }
324
325
326 static void
327 softpipe_delete_gs_state(struct pipe_context *pipe, void *gs)
328 {
329 struct softpipe_context *softpipe = softpipe_context(pipe);
330
331 struct sp_geometry_shader *state =
332 (struct sp_geometry_shader *)gs;
333
334 draw_delete_geometry_shader(softpipe->draw,
335 (state) ? state->draw_data : 0);
336
337 FREE((void *) state->shader.tokens);
338 FREE(state);
339 }
340
341
342 static void
343 softpipe_set_constant_buffer(struct pipe_context *pipe,
344 uint shader, uint index,
345 struct pipe_constant_buffer *cb)
346 {
347 struct softpipe_context *softpipe = softpipe_context(pipe);
348 struct pipe_resource *constants = cb ? cb->buffer : NULL;
349 unsigned size = constants ? constants->width0 : 0;
350 const void *data = constants ? softpipe_resource(constants)->data : NULL;
351
352 assert(shader < PIPE_SHADER_TYPES);
353
354 draw_flush(softpipe->draw);
355
356 /* note: reference counting */
357 pipe_resource_reference(&softpipe->constants[shader][index], constants);
358
359 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
360 draw_set_mapped_constant_buffer(softpipe->draw, shader, index, data, size);
361 }
362
363 softpipe->mapped_constants[shader][index] = data;
364 softpipe->const_buffer_size[shader][index] = size;
365
366 softpipe->dirty |= SP_NEW_CONSTANTS;
367 }
368
369
370 void
371 softpipe_init_shader_funcs(struct pipe_context *pipe)
372 {
373 pipe->create_fs_state = softpipe_create_fs_state;
374 pipe->bind_fs_state = softpipe_bind_fs_state;
375 pipe->delete_fs_state = softpipe_delete_fs_state;
376
377 pipe->create_vs_state = softpipe_create_vs_state;
378 pipe->bind_vs_state = softpipe_bind_vs_state;
379 pipe->delete_vs_state = softpipe_delete_vs_state;
380
381 pipe->create_gs_state = softpipe_create_gs_state;
382 pipe->bind_gs_state = softpipe_bind_gs_state;
383 pipe->delete_gs_state = softpipe_delete_gs_state;
384
385 pipe->set_constant_buffer = softpipe_set_constant_buffer;
386 }