Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / failover / fo_state.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 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
29 */
30
31 #include "fo_context.h"
32
33
34 /* This looks like a lot of work at the moment - we're keeping a
35 * duplicate copy of the state up-to-date.
36 *
37 * This can change in two ways:
38 * - With constant state objects we would only need to save a pointer,
39 * not the whole object.
40 * - By adding a callback in the state tracker to re-emit state. The
41 * state tracker knows the current state already and can re-emit it
42 * without additional complexity.
43 *
44 * This works as a proof-of-concept, but a final version will have
45 * lower overheads.
46 */
47
48
49
50 static void *
51 failover_create_blend_state( struct pipe_context *pipe,
52 const struct pipe_blend_state *blend )
53 {
54 struct fo_state *state = malloc(sizeof(struct fo_state));
55 struct failover_context *failover = failover_context(pipe);
56
57 state->sw_state = failover->sw->create_blend_state(failover->sw, blend);
58 state->hw_state = failover->hw->create_blend_state(failover->hw, blend);
59
60 return state;
61 }
62
63 static void
64 failover_bind_blend_state( struct pipe_context *pipe,
65 void *blend )
66 {
67 struct failover_context *failover = failover_context(pipe);
68 struct fo_state *state = (struct fo_state *)blend;
69 failover->blend = state;
70 failover->dirty |= FO_NEW_BLEND;
71 failover->sw->bind_blend_state( failover->sw, state->sw_state );
72 failover->hw->bind_blend_state( failover->hw, state->hw_state );
73 }
74
75 static void
76 failover_delete_blend_state( struct pipe_context *pipe,
77 void *blend )
78 {
79 struct fo_state *state = (struct fo_state*)blend;
80 struct failover_context *failover = failover_context(pipe);
81
82 failover->sw->delete_blend_state(failover->sw, state->sw_state);
83 failover->hw->delete_blend_state(failover->hw, state->hw_state);
84 state->sw_state = 0;
85 state->hw_state = 0;
86 free(state);
87 }
88
89 static void
90 failover_set_blend_color( struct pipe_context *pipe,
91 const struct pipe_blend_color *blend_color )
92 {
93 struct failover_context *failover = failover_context(pipe);
94
95 failover->blend_color = *blend_color;
96 failover->dirty |= FO_NEW_BLEND_COLOR;
97 failover->sw->set_blend_color( failover->sw, blend_color );
98 failover->hw->set_blend_color( failover->hw, blend_color );
99 }
100
101 static void
102 failover_set_clip_state( struct pipe_context *pipe,
103 const struct pipe_clip_state *clip )
104 {
105 struct failover_context *failover = failover_context(pipe);
106
107 failover->clip = *clip;
108 failover->dirty |= FO_NEW_CLIP;
109 failover->sw->set_clip_state( failover->sw, clip );
110 failover->hw->set_clip_state( failover->hw, clip );
111 }
112
113
114 static void *
115 failover_create_depth_stencil_state(struct pipe_context *pipe,
116 const struct pipe_depth_stencil_alpha_state *templ)
117 {
118 struct fo_state *state = malloc(sizeof(struct fo_state));
119 struct failover_context *failover = failover_context(pipe);
120
121 state->sw_state = failover->sw->create_depth_stencil_alpha_state(failover->sw, templ);
122 state->hw_state = failover->hw->create_depth_stencil_alpha_state(failover->hw, templ);
123
124 return state;
125 }
126
127 static void
128 failover_bind_depth_stencil_state(struct pipe_context *pipe,
129 void *depth_stencil)
130 {
131 struct failover_context *failover = failover_context(pipe);
132 struct fo_state *state = (struct fo_state *)depth_stencil;
133 failover->depth_stencil = state;
134 failover->dirty |= FO_NEW_DEPTH_STENCIL;
135 failover->sw->bind_depth_stencil_alpha_state(failover->sw, state->sw_state);
136 failover->hw->bind_depth_stencil_alpha_state(failover->hw, state->hw_state);
137 }
138
139 static void
140 failover_delete_depth_stencil_state(struct pipe_context *pipe,
141 void *ds)
142 {
143 struct fo_state *state = (struct fo_state*)ds;
144 struct failover_context *failover = failover_context(pipe);
145
146 failover->sw->delete_depth_stencil_alpha_state(failover->sw, state->sw_state);
147 failover->hw->delete_depth_stencil_alpha_state(failover->hw, state->hw_state);
148 state->sw_state = 0;
149 state->hw_state = 0;
150 free(state);
151 }
152
153 static void
154 failover_set_framebuffer_state(struct pipe_context *pipe,
155 const struct pipe_framebuffer_state *framebuffer)
156 {
157 struct failover_context *failover = failover_context(pipe);
158
159 failover->framebuffer = *framebuffer;
160 failover->dirty |= FO_NEW_FRAMEBUFFER;
161 failover->sw->set_framebuffer_state( failover->sw, framebuffer );
162 failover->hw->set_framebuffer_state( failover->hw, framebuffer );
163 }
164
165
166 static void *
167 failover_create_fs_state(struct pipe_context *pipe,
168 const struct pipe_shader_state *templ)
169 {
170 struct fo_state *state = malloc(sizeof(struct fo_state));
171 struct failover_context *failover = failover_context(pipe);
172
173 state->sw_state = failover->sw->create_fs_state(failover->sw, templ);
174 state->hw_state = failover->hw->create_fs_state(failover->hw, templ);
175
176 return state;
177 }
178
179 static void
180 failover_bind_fs_state(struct pipe_context *pipe, void *fs)
181 {
182 struct failover_context *failover = failover_context(pipe);
183 struct fo_state *state = (struct fo_state*)fs;
184 failover->fragment_shader = state;
185 failover->dirty |= FO_NEW_FRAGMENT_SHADER;
186 failover->sw->bind_fs_state(failover->sw, state->sw_state);
187 failover->hw->bind_fs_state(failover->hw, state->hw_state);
188 }
189
190 static void
191 failover_delete_fs_state(struct pipe_context *pipe,
192 void *fs)
193 {
194 struct fo_state *state = (struct fo_state*)fs;
195 struct failover_context *failover = failover_context(pipe);
196
197 failover->sw->delete_fs_state(failover->sw, state->sw_state);
198 failover->hw->delete_fs_state(failover->hw, state->hw_state);
199 state->sw_state = 0;
200 state->hw_state = 0;
201 free(state);
202 }
203
204 static void *
205 failover_create_vs_state(struct pipe_context *pipe,
206 const struct pipe_shader_state *templ)
207 {
208 struct fo_state *state = malloc(sizeof(struct fo_state));
209 struct failover_context *failover = failover_context(pipe);
210
211 state->sw_state = failover->sw->create_vs_state(failover->sw, templ);
212 state->hw_state = failover->hw->create_vs_state(failover->hw, templ);
213
214 return state;
215 }
216
217 static void
218 failover_bind_vs_state(struct pipe_context *pipe,
219 void *vs)
220 {
221 struct failover_context *failover = failover_context(pipe);
222
223 struct fo_state *state = (struct fo_state*)vs;
224 failover->vertex_shader = state;
225 failover->dirty |= FO_NEW_VERTEX_SHADER;
226 failover->sw->bind_vs_state(failover->sw, state->sw_state);
227 failover->hw->bind_vs_state(failover->hw, state->hw_state);
228 }
229
230 static void
231 failover_delete_vs_state(struct pipe_context *pipe,
232 void *vs)
233 {
234 struct fo_state *state = (struct fo_state*)vs;
235 struct failover_context *failover = failover_context(pipe);
236
237 failover->sw->delete_vs_state(failover->sw, state->sw_state);
238 failover->hw->delete_vs_state(failover->hw, state->hw_state);
239 state->sw_state = 0;
240 state->hw_state = 0;
241 free(state);
242 }
243
244 static void
245 failover_set_polygon_stipple( struct pipe_context *pipe,
246 const struct pipe_poly_stipple *stipple )
247 {
248 struct failover_context *failover = failover_context(pipe);
249
250 failover->poly_stipple = *stipple;
251 failover->dirty |= FO_NEW_STIPPLE;
252 failover->sw->set_polygon_stipple( failover->sw, stipple );
253 failover->hw->set_polygon_stipple( failover->hw, stipple );
254 }
255
256
257 static void *
258 failover_create_rasterizer_state(struct pipe_context *pipe,
259 const struct pipe_rasterizer_state *templ)
260 {
261 struct fo_state *state = malloc(sizeof(struct fo_state));
262 struct failover_context *failover = failover_context(pipe);
263
264 state->sw_state = failover->sw->create_rasterizer_state(failover->sw, templ);
265 state->hw_state = failover->hw->create_rasterizer_state(failover->hw, templ);
266
267 return state;
268 }
269
270 static void
271 failover_bind_rasterizer_state(struct pipe_context *pipe,
272 void *raster)
273 {
274 struct failover_context *failover = failover_context(pipe);
275
276 struct fo_state *state = (struct fo_state*)raster;
277 failover->rasterizer = state;
278 failover->dirty |= FO_NEW_RASTERIZER;
279 failover->sw->bind_rasterizer_state(failover->sw, state->sw_state);
280 failover->hw->bind_rasterizer_state(failover->hw, state->hw_state);
281 }
282
283 static void
284 failover_delete_rasterizer_state(struct pipe_context *pipe,
285 void *raster)
286 {
287 struct fo_state *state = (struct fo_state*)raster;
288 struct failover_context *failover = failover_context(pipe);
289
290 failover->sw->delete_rasterizer_state(failover->sw, state->sw_state);
291 failover->hw->delete_rasterizer_state(failover->hw, state->hw_state);
292 state->sw_state = 0;
293 state->hw_state = 0;
294 free(state);
295 }
296
297
298 static void
299 failover_set_scissor_state( struct pipe_context *pipe,
300 const struct pipe_scissor_state *scissor )
301 {
302 struct failover_context *failover = failover_context(pipe);
303
304 failover->scissor = *scissor;
305 failover->dirty |= FO_NEW_SCISSOR;
306 failover->sw->set_scissor_state( failover->sw, scissor );
307 failover->hw->set_scissor_state( failover->hw, scissor );
308 }
309
310
311 static void *
312 failover_create_sampler_state(struct pipe_context *pipe,
313 const struct pipe_sampler_state *templ)
314 {
315 struct fo_state *state = malloc(sizeof(struct fo_state));
316 struct failover_context *failover = failover_context(pipe);
317
318 state->sw_state = failover->sw->create_sampler_state(failover->sw, templ);
319 state->hw_state = failover->hw->create_sampler_state(failover->hw, templ);
320
321 return state;
322 }
323
324 static void
325 failover_bind_fragment_sampler_states(struct pipe_context *pipe,
326 unsigned num,
327 void **sampler)
328 {
329 struct failover_context *failover = failover_context(pipe);
330 struct fo_state *state = (struct fo_state*)sampler;
331 uint i;
332 assert(num <= PIPE_MAX_SAMPLERS);
333 /* Check for no-op */
334 if (num == failover->num_samplers &&
335 !memcmp(failover->sampler, sampler, num * sizeof(void *)))
336 return;
337 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
338 failover->sw_sampler_state[i] = i < num ? state[i].sw_state : NULL;
339 failover->hw_sampler_state[i] = i < num ? state[i].hw_state : NULL;
340 }
341 failover->dirty |= FO_NEW_SAMPLER;
342 failover->num_samplers = num;
343 failover->sw->bind_fragment_sampler_states(failover->sw, num,
344 failover->sw_sampler_state);
345 failover->hw->bind_fragment_sampler_states(failover->hw, num,
346 failover->hw_sampler_state);
347 }
348
349 static void
350 failover_bind_vertex_sampler_states(struct pipe_context *pipe,
351 unsigned num_samplers,
352 void **samplers)
353 {
354 struct failover_context *failover = failover_context(pipe);
355 struct fo_state *state = (struct fo_state*)samplers;
356 uint i;
357
358 assert(num_samplers <= PIPE_MAX_VERTEX_SAMPLERS);
359
360 /* Check for no-op */
361 if (num_samplers == failover->num_vertex_samplers &&
362 !memcmp(failover->vertex_samplers, samplers, num_samplers * sizeof(void *))) {
363 return;
364 }
365 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
366 failover->sw_vertex_sampler_state[i] = i < num_samplers ? state[i].sw_state : NULL;
367 failover->hw_vertex_sampler_state[i] = i < num_samplers ? state[i].hw_state : NULL;
368 }
369 failover->dirty |= FO_NEW_SAMPLER;
370 failover->num_vertex_samplers = num_samplers;
371 failover->sw->bind_vertex_sampler_states(failover->sw,
372 num_samplers,
373 failover->sw_vertex_sampler_state);
374 failover->hw->bind_vertex_sampler_states(failover->hw,
375 num_samplers,
376 failover->hw_vertex_sampler_state);
377 }
378
379 static void
380 failover_delete_sampler_state(struct pipe_context *pipe, void *sampler)
381 {
382 struct fo_state *state = (struct fo_state*)sampler;
383 struct failover_context *failover = failover_context(pipe);
384
385 failover->sw->delete_sampler_state(failover->sw, state->sw_state);
386 failover->hw->delete_sampler_state(failover->hw, state->hw_state);
387 state->sw_state = 0;
388 state->hw_state = 0;
389 free(state);
390 }
391
392
393 static void
394 failover_set_fragment_sampler_textures(struct pipe_context *pipe,
395 unsigned num,
396 struct pipe_texture **texture)
397 {
398 struct failover_context *failover = failover_context(pipe);
399 uint i;
400
401 assert(num <= PIPE_MAX_SAMPLERS);
402
403 /* Check for no-op */
404 if (num == failover->num_textures &&
405 !memcmp(failover->texture, texture, num * sizeof(struct pipe_texture *)))
406 return;
407 for (i = 0; i < num; i++)
408 pipe_texture_reference((struct pipe_texture **) &failover->texture[i],
409 texture[i]);
410 for (i = num; i < failover->num_textures; i++)
411 pipe_texture_reference((struct pipe_texture **) &failover->texture[i],
412 NULL);
413 failover->dirty |= FO_NEW_TEXTURE;
414 failover->num_textures = num;
415 failover->sw->set_fragment_sampler_textures( failover->sw, num, texture );
416 failover->hw->set_fragment_sampler_textures( failover->hw, num, texture );
417 }
418
419
420 static void
421 failover_set_vertex_sampler_textures(struct pipe_context *pipe,
422 unsigned num_textures,
423 struct pipe_texture **textures)
424 {
425 struct failover_context *failover = failover_context(pipe);
426 uint i;
427
428 assert(num_textures <= PIPE_MAX_VERTEX_SAMPLERS);
429
430 /* Check for no-op */
431 if (num_textures == failover->num_vertex_textures &&
432 !memcmp(failover->vertex_textures, textures, num_textures * sizeof(struct pipe_texture *))) {
433 return;
434 }
435 for (i = 0; i < num_textures; i++) {
436 pipe_texture_reference((struct pipe_texture **)&failover->vertex_textures[i],
437 textures[i]);
438 }
439 for (i = num_textures; i < failover->num_vertex_textures; i++) {
440 pipe_texture_reference((struct pipe_texture **)&failover->vertex_textures[i],
441 NULL);
442 }
443 failover->dirty |= FO_NEW_TEXTURE;
444 failover->num_vertex_textures = num_textures;
445 failover->sw->set_vertex_sampler_textures(failover->sw, num_textures, textures);
446 failover->hw->set_vertex_sampler_textures(failover->hw, num_textures, textures);
447 }
448
449
450 static void
451 failover_set_viewport_state( struct pipe_context *pipe,
452 const struct pipe_viewport_state *viewport )
453 {
454 struct failover_context *failover = failover_context(pipe);
455
456 failover->viewport = *viewport;
457 failover->dirty |= FO_NEW_VIEWPORT;
458 failover->sw->set_viewport_state( failover->sw, viewport );
459 failover->hw->set_viewport_state( failover->hw, viewport );
460 }
461
462
463 static void
464 failover_set_vertex_buffers(struct pipe_context *pipe,
465 unsigned count,
466 const struct pipe_vertex_buffer *vertex_buffers)
467 {
468 struct failover_context *failover = failover_context(pipe);
469
470 memcpy(failover->vertex_buffers, vertex_buffers,
471 count * sizeof(vertex_buffers[0]));
472 failover->dirty |= FO_NEW_VERTEX_BUFFER;
473 failover->num_vertex_buffers = count;
474 failover->sw->set_vertex_buffers( failover->sw, count, vertex_buffers );
475 failover->hw->set_vertex_buffers( failover->hw, count, vertex_buffers );
476 }
477
478
479 static void
480 failover_set_vertex_elements(struct pipe_context *pipe,
481 unsigned count,
482 const struct pipe_vertex_element *vertex_elements)
483 {
484 struct failover_context *failover = failover_context(pipe);
485
486 memcpy(failover->vertex_elements, vertex_elements,
487 count * sizeof(vertex_elements[0]));
488
489 failover->dirty |= FO_NEW_VERTEX_ELEMENT;
490 failover->num_vertex_elements = count;
491 failover->sw->set_vertex_elements( failover->sw, count, vertex_elements );
492 failover->hw->set_vertex_elements( failover->hw, count, vertex_elements );
493 }
494
495 void
496 failover_set_constant_buffer(struct pipe_context *pipe,
497 uint shader, uint index,
498 const struct pipe_constant_buffer *buf)
499 {
500 struct failover_context *failover = failover_context(pipe);
501
502 assert(shader < PIPE_SHADER_TYPES);
503 assert(index == 0);
504
505 failover->sw->set_constant_buffer(failover->sw, shader, index, buf);
506 failover->hw->set_constant_buffer(failover->hw, shader, index, buf);
507 }
508
509
510 void
511 failover_init_state_functions( struct failover_context *failover )
512 {
513 failover->pipe.create_blend_state = failover_create_blend_state;
514 failover->pipe.bind_blend_state = failover_bind_blend_state;
515 failover->pipe.delete_blend_state = failover_delete_blend_state;
516 failover->pipe.create_sampler_state = failover_create_sampler_state;
517 failover->pipe.bind_fragment_sampler_states = failover_bind_fragment_sampler_states;
518 failover->pipe.bind_vertex_sampler_states = failover_bind_vertex_sampler_states;
519 failover->pipe.delete_sampler_state = failover_delete_sampler_state;
520 failover->pipe.create_depth_stencil_alpha_state = failover_create_depth_stencil_state;
521 failover->pipe.bind_depth_stencil_alpha_state = failover_bind_depth_stencil_state;
522 failover->pipe.delete_depth_stencil_alpha_state = failover_delete_depth_stencil_state;
523 failover->pipe.create_rasterizer_state = failover_create_rasterizer_state;
524 failover->pipe.bind_rasterizer_state = failover_bind_rasterizer_state;
525 failover->pipe.delete_rasterizer_state = failover_delete_rasterizer_state;
526 failover->pipe.create_fs_state = failover_create_fs_state;
527 failover->pipe.bind_fs_state = failover_bind_fs_state;
528 failover->pipe.delete_fs_state = failover_delete_fs_state;
529 failover->pipe.create_vs_state = failover_create_vs_state;
530 failover->pipe.bind_vs_state = failover_bind_vs_state;
531 failover->pipe.delete_vs_state = failover_delete_vs_state;
532
533 failover->pipe.set_blend_color = failover_set_blend_color;
534 failover->pipe.set_clip_state = failover_set_clip_state;
535 failover->pipe.set_framebuffer_state = failover_set_framebuffer_state;
536 failover->pipe.set_polygon_stipple = failover_set_polygon_stipple;
537 failover->pipe.set_scissor_state = failover_set_scissor_state;
538 failover->pipe.set_fragment_sampler_textures = failover_set_fragment_sampler_textures;
539 failover->pipe.set_vertex_sampler_textures = failover_set_vertex_sampler_textures;
540 failover->pipe.set_viewport_state = failover_set_viewport_state;
541 failover->pipe.set_vertex_buffers = failover_set_vertex_buffers;
542 failover->pipe.set_vertex_elements = failover_set_vertex_elements;
543 failover->pipe.set_constant_buffer = failover_set_constant_buffer;
544 }