1
/**************************************************************************
3
* Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
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.
26
**************************************************************************/
28
/* Authors: Keith Whitwell <keith@tungstengraphics.com>
31
#include "util/u_inlines.h"
32
#include "util/u_memory.h"
33
#include "util/u_transfer.h"
35
#include "fo_context.h"
38
/* This looks like a lot of work at the moment - we're keeping a
39
* duplicate copy of the state up-to-date.
41
* This can change in two ways:
42
* - With constant state objects we would only need to save a pointer,
43
* not the whole object.
44
* - By adding a callback in the state tracker to re-emit state. The
45
* state tracker knows the current state already and can re-emit it
46
* without additional complexity.
48
* This works as a proof-of-concept, but a final version will have
55
failover_create_blend_state( struct pipe_context *pipe,
56
const struct pipe_blend_state *blend )
58
struct fo_state *state = MALLOC(sizeof(struct fo_state));
59
struct failover_context *failover = failover_context(pipe);
61
state->sw_state = failover->sw->create_blend_state(failover->sw, blend);
62
state->hw_state = failover->hw->create_blend_state(failover->hw, blend);
68
failover_bind_blend_state( struct pipe_context *pipe,
71
struct failover_context *failover = failover_context(pipe);
72
struct fo_state *state = (struct fo_state *)blend;
73
failover->blend = state;
74
failover->dirty |= FO_NEW_BLEND;
75
failover->sw->bind_blend_state( failover->sw, state->sw_state );
76
failover->hw->bind_blend_state( failover->hw, state->hw_state );
80
failover_delete_blend_state( struct pipe_context *pipe,
83
struct fo_state *state = (struct fo_state*)blend;
84
struct failover_context *failover = failover_context(pipe);
86
failover->sw->delete_blend_state(failover->sw, state->sw_state);
87
failover->hw->delete_blend_state(failover->hw, state->hw_state);
94
failover_set_blend_color( struct pipe_context *pipe,
95
const struct pipe_blend_color *blend_color )
97
struct failover_context *failover = failover_context(pipe);
99
failover->blend_color = *blend_color;
100
failover->dirty |= FO_NEW_BLEND_COLOR;
101
failover->sw->set_blend_color( failover->sw, blend_color );
102
failover->hw->set_blend_color( failover->hw, blend_color );
106
failover_set_stencil_ref( struct pipe_context *pipe,
107
const struct pipe_stencil_ref *stencil_ref )
109
struct failover_context *failover = failover_context(pipe);
111
failover->stencil_ref = *stencil_ref;
112
failover->dirty |= FO_NEW_STENCIL_REF;
113
failover->sw->set_stencil_ref( failover->sw, stencil_ref );
114
failover->hw->set_stencil_ref( failover->hw, stencil_ref );
118
failover_set_clip_state( struct pipe_context *pipe,
119
const struct pipe_clip_state *clip )
121
struct failover_context *failover = failover_context(pipe);
123
failover->clip = *clip;
124
failover->dirty |= FO_NEW_CLIP;
125
failover->sw->set_clip_state( failover->sw, clip );
126
failover->hw->set_clip_state( failover->hw, clip );
130
failover_set_sample_mask(struct pipe_context *pipe,
131
unsigned sample_mask)
133
struct failover_context *failover = failover_context(pipe);
135
failover->sample_mask = sample_mask;
136
failover->dirty |= FO_NEW_SAMPLE_MASK;
137
failover->sw->set_sample_mask( failover->sw, sample_mask );
138
failover->hw->set_sample_mask( failover->hw, sample_mask );
144
failover_create_depth_stencil_state(struct pipe_context *pipe,
145
const struct pipe_depth_stencil_alpha_state *templ)
147
struct fo_state *state = MALLOC(sizeof(struct fo_state));
148
struct failover_context *failover = failover_context(pipe);
150
state->sw_state = failover->sw->create_depth_stencil_alpha_state(failover->sw, templ);
151
state->hw_state = failover->hw->create_depth_stencil_alpha_state(failover->hw, templ);
157
failover_bind_depth_stencil_state(struct pipe_context *pipe,
160
struct failover_context *failover = failover_context(pipe);
161
struct fo_state *state = (struct fo_state *)depth_stencil;
162
failover->depth_stencil = state;
163
failover->dirty |= FO_NEW_DEPTH_STENCIL;
164
failover->sw->bind_depth_stencil_alpha_state(failover->sw, state->sw_state);
165
failover->hw->bind_depth_stencil_alpha_state(failover->hw, state->hw_state);
169
failover_delete_depth_stencil_state(struct pipe_context *pipe,
172
struct fo_state *state = (struct fo_state*)ds;
173
struct failover_context *failover = failover_context(pipe);
175
failover->sw->delete_depth_stencil_alpha_state(failover->sw, state->sw_state);
176
failover->hw->delete_depth_stencil_alpha_state(failover->hw, state->hw_state);
183
failover_set_framebuffer_state(struct pipe_context *pipe,
184
const struct pipe_framebuffer_state *framebuffer)
186
struct failover_context *failover = failover_context(pipe);
188
failover->framebuffer = *framebuffer;
189
failover->dirty |= FO_NEW_FRAMEBUFFER;
190
failover->sw->set_framebuffer_state( failover->sw, framebuffer );
191
failover->hw->set_framebuffer_state( failover->hw, framebuffer );
196
failover_create_fs_state(struct pipe_context *pipe,
197
const struct pipe_shader_state *templ)
199
struct fo_state *state = MALLOC(sizeof(struct fo_state));
200
struct failover_context *failover = failover_context(pipe);
202
state->sw_state = failover->sw->create_fs_state(failover->sw, templ);
203
state->hw_state = failover->hw->create_fs_state(failover->hw, templ);
209
failover_bind_fs_state(struct pipe_context *pipe, void *fs)
211
struct failover_context *failover = failover_context(pipe);
212
struct fo_state *state = (struct fo_state*)fs;
213
failover->fragment_shader = state;
214
failover->dirty |= FO_NEW_FRAGMENT_SHADER;
215
failover->sw->bind_fs_state(failover->sw, state->sw_state);
216
failover->hw->bind_fs_state(failover->hw, state->hw_state);
220
failover_delete_fs_state(struct pipe_context *pipe,
223
struct fo_state *state = (struct fo_state*)fs;
224
struct failover_context *failover = failover_context(pipe);
226
failover->sw->delete_fs_state(failover->sw, state->sw_state);
227
failover->hw->delete_fs_state(failover->hw, state->hw_state);
234
failover_create_vs_state(struct pipe_context *pipe,
235
const struct pipe_shader_state *templ)
237
struct fo_state *state = MALLOC(sizeof(struct fo_state));
238
struct failover_context *failover = failover_context(pipe);
240
state->sw_state = failover->sw->create_vs_state(failover->sw, templ);
241
state->hw_state = failover->hw->create_vs_state(failover->hw, templ);
247
failover_bind_vs_state(struct pipe_context *pipe,
250
struct failover_context *failover = failover_context(pipe);
252
struct fo_state *state = (struct fo_state*)vs;
253
failover->vertex_shader = state;
254
failover->dirty |= FO_NEW_VERTEX_SHADER;
255
failover->sw->bind_vs_state(failover->sw, state->sw_state);
256
failover->hw->bind_vs_state(failover->hw, state->hw_state);
260
failover_delete_vs_state(struct pipe_context *pipe,
263
struct fo_state *state = (struct fo_state*)vs;
264
struct failover_context *failover = failover_context(pipe);
266
failover->sw->delete_vs_state(failover->sw, state->sw_state);
267
failover->hw->delete_vs_state(failover->hw, state->hw_state);
276
failover_create_vertex_elements_state( struct pipe_context *pipe,
278
const struct pipe_vertex_element *velems )
280
struct fo_state *state = MALLOC(sizeof(struct fo_state));
281
struct failover_context *failover = failover_context(pipe);
283
state->sw_state = failover->sw->create_vertex_elements_state(failover->sw, count, velems);
284
state->hw_state = failover->hw->create_vertex_elements_state(failover->hw, count, velems);
290
failover_bind_vertex_elements_state(struct pipe_context *pipe,
293
struct failover_context *failover = failover_context(pipe);
294
struct fo_state *state = (struct fo_state*)velems;
296
failover->vertex_elements = state;
297
failover->dirty |= FO_NEW_VERTEX_ELEMENT;
298
failover->sw->bind_vertex_elements_state( failover->sw, velems );
299
failover->hw->bind_vertex_elements_state( failover->hw, velems );
303
failover_delete_vertex_elements_state( struct pipe_context *pipe,
306
struct fo_state *state = (struct fo_state*)velems;
307
struct failover_context *failover = failover_context(pipe);
309
failover->sw->delete_vertex_elements_state(failover->sw, state->sw_state);
310
failover->hw->delete_vertex_elements_state(failover->hw, state->hw_state);
317
failover_set_polygon_stipple( struct pipe_context *pipe,
318
const struct pipe_poly_stipple *stipple )
320
struct failover_context *failover = failover_context(pipe);
322
failover->poly_stipple = *stipple;
323
failover->dirty |= FO_NEW_STIPPLE;
324
failover->sw->set_polygon_stipple( failover->sw, stipple );
325
failover->hw->set_polygon_stipple( failover->hw, stipple );
330
failover_create_rasterizer_state(struct pipe_context *pipe,
331
const struct pipe_rasterizer_state *templ)
333
struct fo_state *state = MALLOC(sizeof(struct fo_state));
334
struct failover_context *failover = failover_context(pipe);
336
state->sw_state = failover->sw->create_rasterizer_state(failover->sw, templ);
337
state->hw_state = failover->hw->create_rasterizer_state(failover->hw, templ);
343
failover_bind_rasterizer_state(struct pipe_context *pipe,
346
struct failover_context *failover = failover_context(pipe);
348
struct fo_state *state = (struct fo_state*)raster;
349
failover->rasterizer = state;
350
failover->dirty |= FO_NEW_RASTERIZER;
351
failover->sw->bind_rasterizer_state(failover->sw, state->sw_state);
352
failover->hw->bind_rasterizer_state(failover->hw, state->hw_state);
356
failover_delete_rasterizer_state(struct pipe_context *pipe,
359
struct fo_state *state = (struct fo_state*)raster;
360
struct failover_context *failover = failover_context(pipe);
362
failover->sw->delete_rasterizer_state(failover->sw, state->sw_state);
363
failover->hw->delete_rasterizer_state(failover->hw, state->hw_state);
371
failover_set_scissor_state( struct pipe_context *pipe,
372
const struct pipe_scissor_state *scissor )
374
struct failover_context *failover = failover_context(pipe);
376
failover->scissor = *scissor;
377
failover->dirty |= FO_NEW_SCISSOR;
378
failover->sw->set_scissor_state( failover->sw, scissor );
379
failover->hw->set_scissor_state( failover->hw, scissor );
384
failover_create_sampler_state(struct pipe_context *pipe,
385
const struct pipe_sampler_state *templ)
387
struct fo_state *state = MALLOC(sizeof(struct fo_state));
388
struct failover_context *failover = failover_context(pipe);
390
state->sw_state = failover->sw->create_sampler_state(failover->sw, templ);
391
state->hw_state = failover->hw->create_sampler_state(failover->hw, templ);
397
failover_bind_fragment_sampler_states(struct pipe_context *pipe,
401
struct failover_context *failover = failover_context(pipe);
402
struct fo_state *state = (struct fo_state*)sampler;
404
assert(num <= PIPE_MAX_SAMPLERS);
405
/* Check for no-op */
406
if (num == failover->num_samplers &&
407
!memcmp(failover->sampler, sampler, num * sizeof(void *)))
409
for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
410
failover->sw_sampler_state[i] = i < num ? state[i].sw_state : NULL;
411
failover->hw_sampler_state[i] = i < num ? state[i].hw_state : NULL;
413
failover->dirty |= FO_NEW_SAMPLER;
414
failover->num_samplers = num;
415
failover->sw->bind_fragment_sampler_states(failover->sw, num,
416
failover->sw_sampler_state);
417
failover->hw->bind_fragment_sampler_states(failover->hw, num,
418
failover->hw_sampler_state);
422
failover_bind_vertex_sampler_states(struct pipe_context *pipe,
423
unsigned num_samplers,
426
struct failover_context *failover = failover_context(pipe);
427
struct fo_state *state = (struct fo_state*)samplers;
430
assert(num_samplers <= PIPE_MAX_VERTEX_SAMPLERS);
432
/* Check for no-op */
433
if (num_samplers == failover->num_vertex_samplers &&
434
!memcmp(failover->vertex_samplers, samplers, num_samplers * sizeof(void *))) {
437
for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
438
failover->sw_vertex_sampler_state[i] = i < num_samplers ? state[i].sw_state : NULL;
439
failover->hw_vertex_sampler_state[i] = i < num_samplers ? state[i].hw_state : NULL;
441
failover->dirty |= FO_NEW_SAMPLER;
442
failover->num_vertex_samplers = num_samplers;
443
failover->sw->bind_vertex_sampler_states(failover->sw,
445
failover->sw_vertex_sampler_state);
446
failover->hw->bind_vertex_sampler_states(failover->hw,
448
failover->hw_vertex_sampler_state);
452
failover_delete_sampler_state(struct pipe_context *pipe, void *sampler)
454
struct fo_state *state = (struct fo_state*)sampler;
455
struct failover_context *failover = failover_context(pipe);
457
failover->sw->delete_sampler_state(failover->sw, state->sw_state);
458
failover->hw->delete_sampler_state(failover->hw, state->hw_state);
465
static struct pipe_sampler_view *
466
failover_create_sampler_view(struct pipe_context *pipe,
467
struct pipe_resource *texture,
468
const struct pipe_sampler_view *templ)
470
struct fo_sampler_view *view = MALLOC(sizeof(struct fo_sampler_view));
471
struct failover_context *failover = failover_context(pipe);
473
view->sw = failover->sw->create_sampler_view(failover->sw, texture, templ);
474
view->hw = failover->hw->create_sampler_view(failover->hw, texture, templ);
477
view->base.reference.count = 1;
478
view->base.texture = NULL;
479
pipe_resource_reference(&view->base.texture, texture);
480
view->base.context = pipe;
486
failover_sampler_view_destroy(struct pipe_context *pipe,
487
struct pipe_sampler_view *view)
489
struct fo_sampler_view *fo_view = (struct fo_sampler_view *)view;
490
struct failover_context *failover = failover_context(pipe);
492
failover->sw->sampler_view_destroy(failover->sw, fo_view->sw);
493
failover->hw->sampler_view_destroy(failover->hw, fo_view->hw);
495
pipe_resource_reference(&fo_view->base.texture, NULL);
500
failover_set_fragment_sampler_views(struct pipe_context *pipe,
502
struct pipe_sampler_view **views)
504
struct failover_context *failover = failover_context(pipe);
505
struct pipe_sampler_view *hw_views[PIPE_MAX_SAMPLERS];
508
assert(num <= PIPE_MAX_SAMPLERS);
510
/* Check for no-op */
511
if (num == failover->num_fragment_sampler_views &&
512
!memcmp(failover->fragment_sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
514
for (i = 0; i < num; i++) {
515
struct fo_sampler_view *fo_view = (struct fo_sampler_view *)views[i];
517
pipe_sampler_view_reference((struct pipe_sampler_view **)&failover->fragment_sampler_views[i], views[i]);
518
hw_views[i] = fo_view->hw;
520
for (i = num; i < failover->num_fragment_sampler_views; i++)
521
pipe_sampler_view_reference((struct pipe_sampler_view **)&failover->fragment_sampler_views[i], NULL);
522
failover->dirty |= FO_NEW_SAMPLER_VIEW;
523
failover->num_fragment_sampler_views = num;
524
failover->hw->set_fragment_sampler_views(failover->hw, num, hw_views);
529
failover_set_vertex_sampler_views(struct pipe_context *pipe,
531
struct pipe_sampler_view **views)
533
struct failover_context *failover = failover_context(pipe);
534
struct pipe_sampler_view *hw_views[PIPE_MAX_VERTEX_SAMPLERS];
537
assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
539
/* Check for no-op */
540
if (num == failover->num_vertex_sampler_views &&
541
!memcmp(failover->vertex_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
544
for (i = 0; i < num; i++) {
545
struct fo_sampler_view *fo_view = (struct fo_sampler_view *)views[i];
547
pipe_sampler_view_reference((struct pipe_sampler_view **)&failover->vertex_sampler_views[i], views[i]);
548
hw_views[i] = fo_view->hw;
550
for (i = num; i < failover->num_vertex_sampler_views; i++)
551
pipe_sampler_view_reference((struct pipe_sampler_view **)&failover->vertex_sampler_views[i], NULL);
552
failover->dirty |= FO_NEW_SAMPLER_VIEW;
553
failover->num_vertex_sampler_views = num;
554
failover->hw->set_vertex_sampler_views(failover->hw, num, hw_views);
559
failover_set_viewport_state( struct pipe_context *pipe,
560
const struct pipe_viewport_state *viewport )
562
struct failover_context *failover = failover_context(pipe);
564
failover->viewport = *viewport;
565
failover->dirty |= FO_NEW_VIEWPORT;
566
failover->sw->set_viewport_state( failover->sw, viewport );
567
failover->hw->set_viewport_state( failover->hw, viewport );
572
failover_set_vertex_buffers(struct pipe_context *pipe,
574
const struct pipe_vertex_buffer *vertex_buffers)
576
struct failover_context *failover = failover_context(pipe);
578
util_copy_vertex_buffers(failover->vertex_buffers,
579
&failover->num_vertex_buffers,
580
vertex_buffers, count);
581
failover->dirty |= FO_NEW_VERTEX_BUFFER;
582
failover->sw->set_vertex_buffers( failover->sw, count, vertex_buffers );
583
failover->hw->set_vertex_buffers( failover->hw, count, vertex_buffers );
588
failover_set_index_buffer(struct pipe_context *pipe,
589
const struct pipe_index_buffer *ib)
591
struct failover_context *failover = failover_context(pipe);
594
memcpy(&failover->index_buffer, ib, sizeof(failover->index_buffer));
596
memset(&failover->index_buffer, 0, sizeof(failover->index_buffer));
598
failover->dirty |= FO_NEW_INDEX_BUFFER;
599
failover->sw->set_index_buffer( failover->sw, ib );
600
failover->hw->set_index_buffer( failover->hw, ib );
605
failover_set_constant_buffer(struct pipe_context *pipe,
606
uint shader, uint index,
607
struct pipe_resource *res)
609
struct failover_context *failover = failover_context(pipe);
611
assert(shader < PIPE_SHADER_TYPES);
614
failover->sw->set_constant_buffer(failover->sw, shader, index, res);
615
failover->hw->set_constant_buffer(failover->hw, shader, index, res);
620
failover_init_state_functions( struct failover_context *failover )
622
failover->pipe.create_blend_state = failover_create_blend_state;
623
failover->pipe.bind_blend_state = failover_bind_blend_state;
624
failover->pipe.delete_blend_state = failover_delete_blend_state;
625
failover->pipe.create_sampler_state = failover_create_sampler_state;
626
failover->pipe.bind_fragment_sampler_states = failover_bind_fragment_sampler_states;
627
failover->pipe.bind_vertex_sampler_states = failover_bind_vertex_sampler_states;
628
failover->pipe.delete_sampler_state = failover_delete_sampler_state;
629
failover->pipe.create_depth_stencil_alpha_state = failover_create_depth_stencil_state;
630
failover->pipe.bind_depth_stencil_alpha_state = failover_bind_depth_stencil_state;
631
failover->pipe.delete_depth_stencil_alpha_state = failover_delete_depth_stencil_state;
632
failover->pipe.create_rasterizer_state = failover_create_rasterizer_state;
633
failover->pipe.bind_rasterizer_state = failover_bind_rasterizer_state;
634
failover->pipe.delete_rasterizer_state = failover_delete_rasterizer_state;
635
failover->pipe.create_fs_state = failover_create_fs_state;
636
failover->pipe.bind_fs_state = failover_bind_fs_state;
637
failover->pipe.delete_fs_state = failover_delete_fs_state;
638
failover->pipe.create_vs_state = failover_create_vs_state;
639
failover->pipe.bind_vs_state = failover_bind_vs_state;
640
failover->pipe.delete_vs_state = failover_delete_vs_state;
641
failover->pipe.create_vertex_elements_state = failover_create_vertex_elements_state;
642
failover->pipe.bind_vertex_elements_state = failover_bind_vertex_elements_state;
643
failover->pipe.delete_vertex_elements_state = failover_delete_vertex_elements_state;
645
failover->pipe.set_blend_color = failover_set_blend_color;
646
failover->pipe.set_stencil_ref = failover_set_stencil_ref;
647
failover->pipe.set_clip_state = failover_set_clip_state;
648
failover->pipe.set_sample_mask = failover_set_sample_mask;
649
failover->pipe.set_framebuffer_state = failover_set_framebuffer_state;
650
failover->pipe.set_polygon_stipple = failover_set_polygon_stipple;
651
failover->pipe.set_scissor_state = failover_set_scissor_state;
652
failover->pipe.set_fragment_sampler_views = failover_set_fragment_sampler_views;
653
failover->pipe.set_vertex_sampler_views = failover_set_vertex_sampler_views;
654
failover->pipe.set_viewport_state = failover_set_viewport_state;
655
failover->pipe.set_vertex_buffers = failover_set_vertex_buffers;
656
failover->pipe.set_index_buffer = failover_set_index_buffer;
657
failover->pipe.set_constant_buffer = failover_set_constant_buffer;
658
failover->pipe.create_sampler_view = failover_create_sampler_view;
659
failover->pipe.sampler_view_destroy = failover_sampler_view_destroy;
660
failover->pipe.redefine_user_buffer = u_default_redefine_user_buffer;