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