gallium: rename clearRT / clearDS to clear_render_target / clear_depth_stencil
[profile/ivi/mesa.git] / src / gallium / drivers / rbug / rbug_context.c
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
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 VMWARE 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
29 #include "pipe/p_context.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/u_simple_list.h"
33
34 #include "rbug/rbug_context.h"
35
36 #include "rbug_context.h"
37 #include "rbug_objects.h"
38
39
40 static void
41 rbug_destroy(struct pipe_context *_pipe)
42 {
43    struct rbug_context *rb_pipe = rbug_context(_pipe);
44    struct pipe_context *pipe = rb_pipe->pipe;
45
46    pipe->destroy(pipe);
47
48    FREE(rb_pipe);
49 }
50
51 static void
52 rbug_draw_block_locked(struct rbug_context *rb_pipe, int flag)
53 {
54
55    if (rb_pipe->draw_blocker & flag) {
56       rb_pipe->draw_blocked |= flag;
57    } else if ((rb_pipe->draw_rule.blocker & flag) &&
58               (rb_pipe->draw_blocker & RBUG_BLOCK_RULE)) {
59       int k;
60       boolean block = FALSE;
61       debug_printf("%s (%p %p) (%p %p) (%p %u) (%p %u)\n", __FUNCTION__,
62                    (void *) rb_pipe->draw_rule.fs, (void *) rb_pipe->curr.fs,
63                    (void *) rb_pipe->draw_rule.vs, (void *) rb_pipe->curr.vs,
64                    (void *) rb_pipe->draw_rule.surf, 0,
65                    (void *) rb_pipe->draw_rule.texture, 0);
66       if (rb_pipe->draw_rule.fs &&
67           rb_pipe->draw_rule.fs == rb_pipe->curr.fs)
68          block = TRUE;
69       if (rb_pipe->draw_rule.vs &&
70           rb_pipe->draw_rule.vs == rb_pipe->curr.vs)
71          block = TRUE;
72       if (rb_pipe->draw_rule.surf &&
73           rb_pipe->draw_rule.surf == rb_pipe->curr.zsbuf)
74             block = TRUE;
75       if (rb_pipe->draw_rule.surf)
76          for (k = 0; k < rb_pipe->curr.nr_cbufs; k++)
77             if (rb_pipe->draw_rule.surf == rb_pipe->curr.cbufs[k])
78                block = TRUE;
79       if (rb_pipe->draw_rule.texture) {
80          for (k = 0; k < rb_pipe->curr.num_fs_views; k++)
81             if (rb_pipe->draw_rule.texture == rb_pipe->curr.fs_texs[k])
82                block = TRUE;
83          for (k = 0; k < rb_pipe->curr.num_vs_views; k++) {
84             if (rb_pipe->draw_rule.texture == rb_pipe->curr.vs_texs[k]) {
85                block = TRUE;
86             }
87          }
88       }
89
90       if (block)
91          rb_pipe->draw_blocked |= (flag | RBUG_BLOCK_RULE);
92    }
93
94    if (rb_pipe->draw_blocked)
95       rbug_notify_draw_blocked(rb_pipe);
96
97    /* wait for rbug to clear the blocked flag */
98    while (rb_pipe->draw_blocked & flag) {
99       rb_pipe->draw_blocked |= flag;
100 #ifdef PIPE_THREAD_HAVE_CONDVAR
101       pipe_condvar_wait(rb_pipe->draw_cond, rb_pipe->draw_mutex);
102 #else
103       pipe_mutex_unlock(rb_pipe->draw_mutex);
104 #ifdef PIPE_SUBSYSTEM_WINDOWS_USER
105       Sleep(1);
106 #endif
107       pipe_mutex_lock(rb_pipe->draw_mutex);
108 #endif
109    }
110
111 }
112
113 static void
114 rbug_draw_arrays(struct pipe_context *_pipe,
115                  unsigned prim,
116                  unsigned start,
117                  unsigned count)
118 {
119    struct rbug_context *rb_pipe = rbug_context(_pipe);
120    struct pipe_context *pipe = rb_pipe->pipe;
121
122    pipe_mutex_lock(rb_pipe->draw_mutex);
123    rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_BEFORE);
124
125    pipe->draw_arrays(pipe,
126                      prim,
127                      start,
128                      count);
129
130    rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_AFTER);
131    pipe_mutex_unlock(rb_pipe->draw_mutex);
132 }
133
134 static void
135 rbug_draw_elements(struct pipe_context *_pipe,
136                    struct pipe_resource *_indexResource,
137                    unsigned indexSize,
138                    int indexBias,
139                    unsigned prim,
140                    unsigned start,
141                    unsigned count)
142 {
143    struct rbug_context *rb_pipe = rbug_context(_pipe);
144    struct rbug_resource *rb_resource = rbug_resource(_indexResource);
145    struct pipe_context *pipe = rb_pipe->pipe;
146    struct pipe_resource *indexResource = rb_resource->resource;
147
148    pipe_mutex_lock(rb_pipe->draw_mutex);
149    rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_BEFORE);
150
151    pipe->draw_elements(pipe,
152                        indexResource,
153                        indexSize,
154                        indexBias,
155                        prim,
156                        start,
157                        count);
158
159    rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_AFTER);
160    pipe_mutex_unlock(rb_pipe->draw_mutex);
161 }
162
163 static void
164 rbug_draw_range_elements(struct pipe_context *_pipe,
165                          struct pipe_resource *_indexResource,
166                          unsigned indexSize,
167                          int indexBias,
168                          unsigned minIndex,
169                          unsigned maxIndex,
170                          unsigned mode,
171                          unsigned start,
172                          unsigned count)
173 {
174    struct rbug_context *rb_pipe = rbug_context(_pipe);
175    struct rbug_resource *rb_resource = rbug_resource(_indexResource);
176    struct pipe_context *pipe = rb_pipe->pipe;
177    struct pipe_resource *indexResource = rb_resource->resource;
178
179    pipe_mutex_lock(rb_pipe->draw_mutex);
180    rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_BEFORE);
181
182    pipe->draw_range_elements(pipe,
183                              indexResource,
184                              indexSize,
185                              indexBias,
186                              minIndex,
187                              maxIndex,
188                              mode,
189                              start,
190                              count);
191
192    rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_AFTER);
193    pipe_mutex_unlock(rb_pipe->draw_mutex);
194 }
195
196 static struct pipe_query *
197 rbug_create_query(struct pipe_context *_pipe,
198                   unsigned query_type)
199 {
200    struct rbug_context *rb_pipe = rbug_context(_pipe);
201    struct pipe_context *pipe = rb_pipe->pipe;
202
203    return pipe->create_query(pipe,
204                              query_type);
205 }
206
207 static void
208 rbug_destroy_query(struct pipe_context *_pipe,
209                    struct pipe_query *query)
210 {
211    struct rbug_context *rb_pipe = rbug_context(_pipe);
212    struct pipe_context *pipe = rb_pipe->pipe;
213
214    pipe->destroy_query(pipe,
215                        query);
216 }
217
218 static void
219 rbug_begin_query(struct pipe_context *_pipe,
220                  struct pipe_query *query)
221 {
222    struct rbug_context *rb_pipe = rbug_context(_pipe);
223    struct pipe_context *pipe = rb_pipe->pipe;
224
225    pipe->begin_query(pipe,
226                      query);
227 }
228
229 static void
230 rbug_end_query(struct pipe_context *_pipe,
231                struct pipe_query *query)
232 {
233    struct rbug_context *rb_pipe = rbug_context(_pipe);
234    struct pipe_context *pipe = rb_pipe->pipe;
235
236    pipe->end_query(pipe,
237                    query);
238 }
239
240 static boolean
241 rbug_get_query_result(struct pipe_context *_pipe,
242                       struct pipe_query *query,
243                       boolean wait,
244                       uint64_t *result)
245 {
246    struct rbug_context *rb_pipe = rbug_context(_pipe);
247    struct pipe_context *pipe = rb_pipe->pipe;
248
249    return pipe->get_query_result(pipe,
250                                  query,
251                                  wait,
252                                  result);
253 }
254
255 static void *
256 rbug_create_blend_state(struct pipe_context *_pipe,
257                         const struct pipe_blend_state *blend)
258 {
259    struct rbug_context *rb_pipe = rbug_context(_pipe);
260    struct pipe_context *pipe = rb_pipe->pipe;
261
262    return pipe->create_blend_state(pipe,
263                                    blend);
264 }
265
266 static void
267 rbug_bind_blend_state(struct pipe_context *_pipe,
268                       void *blend)
269 {
270    struct rbug_context *rb_pipe = rbug_context(_pipe);
271    struct pipe_context *pipe = rb_pipe->pipe;
272
273    pipe->bind_blend_state(pipe,
274                               blend);
275 }
276
277 static void
278 rbug_delete_blend_state(struct pipe_context *_pipe,
279                         void *blend)
280 {
281    struct rbug_context *rb_pipe = rbug_context(_pipe);
282    struct pipe_context *pipe = rb_pipe->pipe;
283
284    pipe->delete_blend_state(pipe,
285                             blend);
286 }
287
288 static void *
289 rbug_create_sampler_state(struct pipe_context *_pipe,
290                           const struct pipe_sampler_state *sampler)
291 {
292    struct rbug_context *rb_pipe = rbug_context(_pipe);
293    struct pipe_context *pipe = rb_pipe->pipe;
294
295    return pipe->create_sampler_state(pipe,
296                                      sampler);
297 }
298
299 static void
300 rbug_bind_fragment_sampler_states(struct pipe_context *_pipe,
301                                   unsigned num_samplers,
302                                   void **samplers)
303 {
304    struct rbug_context *rb_pipe = rbug_context(_pipe);
305    struct pipe_context *pipe = rb_pipe->pipe;
306
307    pipe->bind_fragment_sampler_states(pipe,
308                                       num_samplers,
309                                       samplers);
310 }
311
312 static void
313 rbug_bind_vertex_sampler_states(struct pipe_context *_pipe,
314                                 unsigned num_samplers,
315                                 void **samplers)
316 {
317    struct rbug_context *rb_pipe = rbug_context(_pipe);
318    struct pipe_context *pipe = rb_pipe->pipe;
319
320    pipe->bind_vertex_sampler_states(pipe,
321                                     num_samplers,
322                                     samplers);
323 }
324
325 static void
326 rbug_delete_sampler_state(struct pipe_context *_pipe,
327                           void *sampler)
328 {
329    struct rbug_context *rb_pipe = rbug_context(_pipe);
330    struct pipe_context *pipe = rb_pipe->pipe;
331
332    pipe->delete_sampler_state(pipe,
333                               sampler);
334 }
335
336 static void *
337 rbug_create_rasterizer_state(struct pipe_context *_pipe,
338                              const struct pipe_rasterizer_state *rasterizer)
339 {
340    struct rbug_context *rb_pipe = rbug_context(_pipe);
341    struct pipe_context *pipe = rb_pipe->pipe;
342
343    return pipe->create_rasterizer_state(pipe,
344                                         rasterizer);
345 }
346
347 static void
348 rbug_bind_rasterizer_state(struct pipe_context *_pipe,
349                            void *rasterizer)
350 {
351    struct rbug_context *rb_pipe = rbug_context(_pipe);
352    struct pipe_context *pipe = rb_pipe->pipe;
353
354    pipe->bind_rasterizer_state(pipe,
355                                rasterizer);
356 }
357
358 static void
359 rbug_delete_rasterizer_state(struct pipe_context *_pipe,
360                              void *rasterizer)
361 {
362    struct rbug_context *rb_pipe = rbug_context(_pipe);
363    struct pipe_context *pipe = rb_pipe->pipe;
364
365    pipe->delete_rasterizer_state(pipe,
366                                  rasterizer);
367 }
368
369 static void *
370 rbug_create_depth_stencil_alpha_state(struct pipe_context *_pipe,
371                                       const struct pipe_depth_stencil_alpha_state *depth_stencil_alpha)
372 {
373    struct rbug_context *rb_pipe = rbug_context(_pipe);
374    struct pipe_context *pipe = rb_pipe->pipe;
375
376    return pipe->create_depth_stencil_alpha_state(pipe,
377                                                  depth_stencil_alpha);
378 }
379
380 static void
381 rbug_bind_depth_stencil_alpha_state(struct pipe_context *_pipe,
382                                     void *depth_stencil_alpha)
383 {
384    struct rbug_context *rb_pipe = rbug_context(_pipe);
385    struct pipe_context *pipe = rb_pipe->pipe;
386
387    pipe->bind_depth_stencil_alpha_state(pipe,
388                                         depth_stencil_alpha);
389 }
390
391 static void
392 rbug_delete_depth_stencil_alpha_state(struct pipe_context *_pipe,
393                                       void *depth_stencil_alpha)
394 {
395    struct rbug_context *rb_pipe = rbug_context(_pipe);
396    struct pipe_context *pipe = rb_pipe->pipe;
397
398    pipe->delete_depth_stencil_alpha_state(pipe,
399                                           depth_stencil_alpha);
400 }
401
402 static void *
403 rbug_create_fs_state(struct pipe_context *_pipe,
404                      const struct pipe_shader_state *state)
405 {
406    struct rbug_context *rb_pipe = rbug_context(_pipe);
407    struct pipe_context *pipe = rb_pipe->pipe;
408    void *result;
409
410    result = pipe->create_fs_state(pipe, state);
411    if (!result)
412       return NULL;
413
414    return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_FRAGMENT);
415 }
416
417 static void
418 rbug_bind_fs_state(struct pipe_context *_pipe,
419                    void *_fs)
420 {
421    struct rbug_context *rb_pipe = rbug_context(_pipe);
422    struct pipe_context *pipe = rb_pipe->pipe;
423    void *fs;
424
425    fs = rbug_shader_unwrap(_fs);
426    rb_pipe->curr.fs = rbug_shader(_fs);
427    pipe->bind_fs_state(pipe,
428                        fs);
429 }
430
431 static void
432 rbug_delete_fs_state(struct pipe_context *_pipe,
433                      void *_fs)
434 {
435    struct rbug_context *rb_pipe = rbug_context(_pipe);
436    struct rbug_shader *rb_shader = rbug_shader(_fs);
437
438    rbug_shader_destroy(rb_pipe, rb_shader);
439 }
440
441 static void *
442 rbug_create_vs_state(struct pipe_context *_pipe,
443                      const struct pipe_shader_state *state)
444 {
445    struct rbug_context *rb_pipe = rbug_context(_pipe);
446    struct pipe_context *pipe = rb_pipe->pipe;
447    void *result;
448
449    result = pipe->create_vs_state(pipe, state);
450    if (!result)
451       return NULL;
452
453    return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_VERTEX);
454 }
455
456 static void
457 rbug_bind_vs_state(struct pipe_context *_pipe,
458                    void *_vs)
459 {
460    struct rbug_context *rb_pipe = rbug_context(_pipe);
461    struct pipe_context *pipe = rb_pipe->pipe;
462    void *vs;
463
464    vs = rbug_shader_unwrap(_vs);
465    rb_pipe->curr.vs = rbug_shader(_vs);
466    pipe->bind_vs_state(pipe,
467                        vs);
468 }
469
470 static void
471 rbug_delete_vs_state(struct pipe_context *_pipe,
472                      void *_vs)
473 {
474    struct rbug_context *rb_pipe = rbug_context(_pipe);
475    struct rbug_shader *rb_shader = rbug_shader(_vs);
476
477    rbug_shader_destroy(rb_pipe, rb_shader);
478 }
479
480 static void *
481 rbug_create_gs_state(struct pipe_context *_pipe,
482                      const struct pipe_shader_state *state)
483 {
484    struct rbug_context *rb_pipe = rbug_context(_pipe);
485    struct pipe_context *pipe = rb_pipe->pipe;
486    void *result;
487
488    result = pipe->create_gs_state(pipe, state);
489    if (!result)
490       return NULL;
491
492    return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_GEOM);
493 }
494
495 static void
496 rbug_bind_gs_state(struct pipe_context *_pipe,
497                    void *_gs)
498 {
499    struct rbug_context *rb_pipe = rbug_context(_pipe);
500    struct pipe_context *pipe = rb_pipe->pipe;
501    void *gs;
502
503    gs = rbug_shader_unwrap(_gs);
504    rb_pipe->curr.gs = rbug_shader(_gs);
505    pipe->bind_gs_state(pipe,
506                        gs);
507 }
508
509 static void
510 rbug_delete_gs_state(struct pipe_context *_pipe,
511                      void *_gs)
512 {
513    struct rbug_context *rb_pipe = rbug_context(_pipe);
514    struct rbug_shader *rb_shader = rbug_shader(_gs);
515
516    rbug_shader_destroy(rb_pipe, rb_shader);
517 }
518
519 static void *
520 rbug_create_vertex_elements_state(struct pipe_context *_pipe,
521                                   unsigned num_elements,
522                                   const struct pipe_vertex_element *vertex_elements)
523 {
524    struct rbug_context *rb_pipe = rbug_context(_pipe);
525    struct pipe_context *pipe = rb_pipe->pipe;
526
527    return pipe->create_vertex_elements_state(pipe,
528                                              num_elements,
529                                              vertex_elements);
530 }
531
532 static void
533 rbug_bind_vertex_elements_state(struct pipe_context *_pipe,
534                                 void *velems)
535 {
536    struct rbug_context *rb_pipe = rbug_context(_pipe);
537    struct pipe_context *pipe = rb_pipe->pipe;
538
539    pipe->bind_vertex_elements_state(pipe,
540                                     velems);
541 }
542
543 static void
544 rbug_delete_vertex_elements_state(struct pipe_context *_pipe,
545                                   void *velems)
546 {
547    struct rbug_context *rb_pipe = rbug_context(_pipe);
548    struct pipe_context *pipe = rb_pipe->pipe;
549
550    pipe->delete_vertex_elements_state(pipe,
551                                       velems);
552 }
553
554 static void
555 rbug_set_blend_color(struct pipe_context *_pipe,
556                      const struct pipe_blend_color *blend_color)
557 {
558    struct rbug_context *rb_pipe = rbug_context(_pipe);
559    struct pipe_context *pipe = rb_pipe->pipe;
560
561    pipe->set_blend_color(pipe,
562                          blend_color);
563 }
564
565 static void
566 rbug_set_stencil_ref(struct pipe_context *_pipe,
567                      const struct pipe_stencil_ref *stencil_ref)
568 {
569    struct rbug_context *rb_pipe = rbug_context(_pipe);
570    struct pipe_context *pipe = rb_pipe->pipe;
571
572    pipe->set_stencil_ref(pipe,
573                          stencil_ref);
574 }
575
576 static void
577 rbug_set_clip_state(struct pipe_context *_pipe,
578                     const struct pipe_clip_state *clip)
579 {
580    struct rbug_context *rb_pipe = rbug_context(_pipe);
581    struct pipe_context *pipe = rb_pipe->pipe;
582
583    pipe->set_clip_state(pipe,
584                         clip);
585 }
586
587 static void
588 rbug_set_constant_buffer(struct pipe_context *_pipe,
589                          uint shader,
590                          uint index,
591                          struct pipe_resource *_resource)
592 {
593    struct rbug_context *rb_pipe = rbug_context(_pipe);
594    struct pipe_context *pipe = rb_pipe->pipe;
595    struct pipe_resource *unwrapped_resource;
596    struct pipe_resource *resource = NULL;
597
598    /* XXX hmm? unwrap the input state */
599    if (_resource) {
600       unwrapped_resource = rbug_resource_unwrap(_resource);
601       resource = unwrapped_resource;
602    }
603
604    pipe->set_constant_buffer(pipe,
605                              shader,
606                              index,
607                              resource);
608 }
609
610 static void
611 rbug_set_framebuffer_state(struct pipe_context *_pipe,
612                            const struct pipe_framebuffer_state *_state)
613 {
614    struct rbug_context *rb_pipe = rbug_context(_pipe);
615    struct pipe_context *pipe = rb_pipe->pipe;
616    struct pipe_framebuffer_state unwrapped_state;
617    struct pipe_framebuffer_state *state = NULL;
618    unsigned i;
619
620    rb_pipe->curr.nr_cbufs = 0;
621    memset(rb_pipe->curr.cbufs, 0, sizeof(rb_pipe->curr.cbufs));
622
623    /* unwrap the input state */
624    if (_state) {
625       memcpy(&unwrapped_state, _state, sizeof(unwrapped_state));
626
627       rb_pipe->curr.nr_cbufs = _state->nr_cbufs;
628       for(i = 0; i < _state->nr_cbufs; i++) {
629          unwrapped_state.cbufs[i] = rbug_surface_unwrap(_state->cbufs[i]);
630          if (_state->cbufs[i])
631             rb_pipe->curr.cbufs[i] = rbug_resource(_state->cbufs[i]->texture);
632       }
633       unwrapped_state.zsbuf = rbug_surface_unwrap(_state->zsbuf);
634       state = &unwrapped_state;
635    }
636
637    pipe->set_framebuffer_state(pipe,
638                                state);
639 }
640
641 static void
642 rbug_set_polygon_stipple(struct pipe_context *_pipe,
643                          const struct pipe_poly_stipple *poly_stipple)
644 {
645    struct rbug_context *rb_pipe = rbug_context(_pipe);
646    struct pipe_context *pipe = rb_pipe->pipe;
647
648    pipe->set_polygon_stipple(pipe,
649                              poly_stipple);
650 }
651
652 static void
653 rbug_set_scissor_state(struct pipe_context *_pipe,
654                        const struct pipe_scissor_state *scissor)
655 {
656    struct rbug_context *rb_pipe = rbug_context(_pipe);
657    struct pipe_context *pipe = rb_pipe->pipe;
658
659    pipe->set_scissor_state(pipe,
660                            scissor);
661 }
662
663 static void
664 rbug_set_viewport_state(struct pipe_context *_pipe,
665                         const struct pipe_viewport_state *viewport)
666 {
667    struct rbug_context *rb_pipe = rbug_context(_pipe);
668    struct pipe_context *pipe = rb_pipe->pipe;
669
670    pipe->set_viewport_state(pipe,
671                             viewport);
672 }
673
674 static void
675 rbug_set_fragment_sampler_views(struct pipe_context *_pipe,
676                                 unsigned num,
677                                 struct pipe_sampler_view **_views)
678 {
679    struct rbug_context *rb_pipe = rbug_context(_pipe);
680    struct pipe_context *pipe = rb_pipe->pipe;
681    struct pipe_sampler_view *unwrapped_views[PIPE_MAX_SAMPLERS];
682    struct pipe_sampler_view **views = NULL;
683    unsigned i;
684
685    rb_pipe->curr.num_fs_views = 0;
686    memset(rb_pipe->curr.fs_views, 0, sizeof(rb_pipe->curr.fs_views));
687    memset(rb_pipe->curr.fs_texs, 0, sizeof(rb_pipe->curr.fs_texs));
688    memset(unwrapped_views, 0, sizeof(unwrapped_views));
689
690    if (_views) {
691       rb_pipe->curr.num_fs_views = num;
692       for (i = 0; i < num; i++) {
693          rb_pipe->curr.fs_views[i] = rbug_sampler_view(_views[i]);
694          rb_pipe->curr.fs_texs[i] = rbug_resource(_views[i] ? _views[i]->texture : NULL);
695          unwrapped_views[i] = rbug_sampler_view_unwrap(_views[i]);
696       }
697       views = unwrapped_views;
698    }
699
700    pipe->set_fragment_sampler_views(pipe, num, views);
701 }
702
703 static void
704 rbug_set_vertex_sampler_views(struct pipe_context *_pipe,
705                               unsigned num,
706                               struct pipe_sampler_view **_views)
707 {
708    struct rbug_context *rb_pipe = rbug_context(_pipe);
709    struct pipe_context *pipe = rb_pipe->pipe;
710    struct pipe_sampler_view *unwrapped_views[PIPE_MAX_VERTEX_SAMPLERS];
711    struct pipe_sampler_view **views = NULL;
712    unsigned i;
713
714    rb_pipe->curr.num_vs_views = 0;
715    memset(rb_pipe->curr.vs_views, 0, sizeof(rb_pipe->curr.vs_views));
716    memset(rb_pipe->curr.vs_texs, 0, sizeof(rb_pipe->curr.vs_texs));
717    memset(unwrapped_views, 0, sizeof(unwrapped_views));
718
719    if (_views) {
720       rb_pipe->curr.num_vs_views = num;
721       for (i = 0; i < num; i++) {
722          rb_pipe->curr.vs_views[i] = rbug_sampler_view(_views[i]);
723          rb_pipe->curr.vs_texs[i] = rbug_resource(_views[i]->texture);
724          unwrapped_views[i] = rbug_sampler_view_unwrap(_views[i]);
725       }
726       views = unwrapped_views;
727    }
728
729    pipe->set_vertex_sampler_views(pipe, num, views);
730 }
731
732 static void
733 rbug_set_vertex_buffers(struct pipe_context *_pipe,
734                         unsigned num_buffers,
735                         const struct pipe_vertex_buffer *_buffers)
736 {
737    struct rbug_context *rb_pipe = rbug_context(_pipe);
738    struct pipe_context *pipe = rb_pipe->pipe;
739    struct pipe_vertex_buffer unwrapped_buffers[PIPE_MAX_SHADER_INPUTS];
740    struct pipe_vertex_buffer *buffers = NULL;
741    unsigned i;
742
743    if (num_buffers) {
744       memcpy(unwrapped_buffers, _buffers, num_buffers * sizeof(*_buffers));
745       for (i = 0; i < num_buffers; i++)
746          unwrapped_buffers[i].buffer = rbug_resource_unwrap(_buffers[i].buffer);
747       buffers = unwrapped_buffers;
748    }
749
750    pipe->set_vertex_buffers(pipe,
751                             num_buffers,
752                             buffers);
753 }
754
755 static void
756 rbug_set_sample_mask(struct pipe_context *_pipe,
757                      unsigned sample_mask)
758 {
759    struct rbug_context *rb_pipe = rbug_context(_pipe);
760    struct pipe_context *pipe = rb_pipe->pipe;
761
762    pipe->set_sample_mask(pipe, sample_mask);
763 }
764
765 static void
766 rbug_resource_copy_region(struct pipe_context *_pipe,
767                           struct pipe_resource *_dst,
768                           struct pipe_subresource subdst,
769                           unsigned dstx,
770                           unsigned dsty,
771                           unsigned dstz,
772                           struct pipe_resource *_src,
773                           struct pipe_subresource subsrc,
774                           unsigned srcx,
775                           unsigned srcy,
776                           unsigned srcz,
777                           unsigned width,
778                           unsigned height)
779 {
780    struct rbug_context *rb_pipe = rbug_context(_pipe);
781    struct rbug_resource *rb_resource_dst = rbug_resource(_dst);
782    struct rbug_resource *rb_resource_src = rbug_resource(_src);
783    struct pipe_context *pipe = rb_pipe->pipe;
784    struct pipe_resource *dst = rb_resource_dst->resource;
785    struct pipe_resource *src = rb_resource_src->resource;
786
787    pipe->resource_copy_region(pipe,
788                               dst,
789                               subdst,
790                               dstx,
791                               dsty,
792                               dstz,
793                               src,
794                               subsrc,
795                               srcx,
796                               srcy,
797                               srcz,
798                               width,
799                               height);
800 }
801
802 static void
803 rbug_clear(struct pipe_context *_pipe,
804            unsigned buffers,
805            const float *rgba,
806            double depth,
807            unsigned stencil)
808 {
809    struct rbug_context *rb_pipe = rbug_context(_pipe);
810    struct pipe_context *pipe = rb_pipe->pipe;
811
812    pipe->clear(pipe,
813                buffers,
814                rgba,
815                depth,
816                stencil);
817 }
818
819 static void
820 rbug_clear_render_target(struct pipe_context *_pipe,
821                          struct pipe_surface *_dst,
822                          const float *rgba,
823                          unsigned dstx, unsigned dsty,
824                          unsigned width, unsigned height)
825 {
826    struct rbug_context *rb_pipe = rbug_context(_pipe);
827    struct rbug_surface *rb_surface_dst = rbug_surface(_dst);
828    struct pipe_context *pipe = rb_pipe->pipe;
829    struct pipe_surface *dst = rb_surface_dst->surface;
830
831    pipe->clear_render_target(pipe,
832                              dst,
833                              rgba,
834                              dstx,
835                              dsty,
836                              width,
837                              height);
838 }
839
840 static void
841 rbug_clear_depth_stencil(struct pipe_context *_pipe,
842                          struct pipe_surface *_dst,
843                          unsigned clear_flags,
844                          double depth,
845                          unsigned stencil,
846                          unsigned dstx, unsigned dsty,
847                          unsigned width, unsigned height)
848 {
849    struct rbug_context *rb_pipe = rbug_context(_pipe);
850    struct rbug_surface *rb_surface_dst = rbug_surface(_dst);
851    struct pipe_context *pipe = rb_pipe->pipe;
852    struct pipe_surface *dst = rb_surface_dst->surface;
853
854    pipe->clear_depth_stencil(pipe,
855                              dst,
856                              clear_flags,
857                              depth,
858                              stencil,
859                              dstx,
860                              dsty,
861                              width,
862                              height);
863 }
864
865 static void
866 rbug_flush(struct pipe_context *_pipe,
867            unsigned flags,
868            struct pipe_fence_handle **fence)
869 {
870    struct rbug_context *rb_pipe = rbug_context(_pipe);
871    struct pipe_context *pipe = rb_pipe->pipe;
872
873    pipe->flush(pipe,
874                flags,
875                fence);
876 }
877
878 static unsigned int
879 rbug_is_resource_referenced(struct pipe_context *_pipe,
880                             struct pipe_resource *_resource,
881                             unsigned face,
882                             unsigned level)
883 {
884    struct rbug_context *rb_pipe = rbug_context(_pipe);
885    struct rbug_resource *rb_resource = rbug_resource(_resource);
886    struct pipe_context *pipe = rb_pipe->pipe;
887    struct pipe_resource *resource = rb_resource->resource;
888
889    return pipe->is_resource_referenced(pipe,
890                                        resource,
891                                        face,
892                                        level);
893 }
894
895 static struct pipe_sampler_view *
896 rbug_context_create_sampler_view(struct pipe_context *_pipe,
897                                  struct pipe_resource *_resource,
898                                  const struct pipe_sampler_view *templ)
899 {
900    struct rbug_context *rb_pipe = rbug_context(_pipe);
901    struct rbug_resource *rb_resource = rbug_resource(_resource);
902    struct pipe_context *pipe = rb_pipe->pipe;
903    struct pipe_resource *resource = rb_resource->resource;
904    struct pipe_sampler_view *result;
905
906    result = pipe->create_sampler_view(pipe,
907                                       resource,
908                                       templ);
909
910    if (result)
911       return rbug_sampler_view_create(rb_pipe, rb_resource, result);
912    return NULL;
913 }
914
915 static void
916 rbug_context_sampler_view_destroy(struct pipe_context *_pipe,
917                                   struct pipe_sampler_view *_view)
918 {
919    rbug_sampler_view_destroy(rbug_context(_pipe),
920                              rbug_sampler_view(_view));
921 }
922
923 static struct pipe_transfer *
924 rbug_context_get_transfer(struct pipe_context *_context,
925                           struct pipe_resource *_resource,
926                           struct pipe_subresource sr,
927                           unsigned usage,
928                           const struct pipe_box *box)
929 {
930    struct rbug_context *rb_pipe = rbug_context(_context);
931    struct rbug_resource *rb_resource = rbug_resource(_resource);
932    struct pipe_context *context = rb_pipe->pipe;
933    struct pipe_resource *resource = rb_resource->resource;
934    struct pipe_transfer *result;
935
936    result = context->get_transfer(context,
937                                   resource,
938                                   sr,
939                                   usage,
940                                   box);
941
942    if (result)
943       return rbug_transfer_create(rb_pipe, rb_resource, result);
944    return NULL;
945 }
946
947 static void
948 rbug_context_transfer_destroy(struct pipe_context *_pipe,
949                               struct pipe_transfer *_transfer)
950 {
951    rbug_transfer_destroy(rbug_context(_pipe),
952                              rbug_transfer(_transfer));
953 }
954
955 static void *
956 rbug_context_transfer_map(struct pipe_context *_context,
957                           struct pipe_transfer *_transfer)
958 {
959    struct rbug_context *rb_pipe = rbug_context(_context);
960    struct rbug_transfer *rb_transfer = rbug_transfer(_transfer);
961    struct pipe_context *context = rb_pipe->pipe;
962    struct pipe_transfer *transfer = rb_transfer->transfer;
963
964    return context->transfer_map(context,
965                                 transfer);
966 }
967
968
969
970 static void
971 rbug_context_transfer_flush_region(struct pipe_context *_context,
972                                    struct pipe_transfer *_transfer,
973                                    const struct pipe_box *box)
974 {
975    struct rbug_context *rb_pipe = rbug_context(_context);
976    struct rbug_transfer *rb_transfer = rbug_transfer(_transfer);
977    struct pipe_context *context = rb_pipe->pipe;
978    struct pipe_transfer *transfer = rb_transfer->transfer;
979
980    context->transfer_flush_region(context,
981                                   transfer,
982                                   box);
983 }
984
985
986 static void
987 rbug_context_transfer_unmap(struct pipe_context *_context,
988                             struct pipe_transfer *_transfer)
989 {
990    struct rbug_context *rb_pipe = rbug_context(_context);
991    struct rbug_transfer *rb_transfer = rbug_transfer(_transfer);
992    struct pipe_context *context = rb_pipe->pipe;
993    struct pipe_transfer *transfer = rb_transfer->transfer;
994
995    context->transfer_unmap(context,
996                            transfer);
997 }
998
999
1000 static void
1001 rbug_context_transfer_inline_write(struct pipe_context *_context,
1002                                    struct pipe_resource *_resource,
1003                                    struct pipe_subresource sr,
1004                                    unsigned usage,
1005                                    const struct pipe_box *box,
1006                                    const void *data,
1007                                    unsigned stride,
1008                                    unsigned slice_stride)
1009 {
1010    struct rbug_context *rb_pipe = rbug_context(_context);
1011    struct rbug_resource *rb_resource = rbug_resource(_resource);
1012    struct pipe_context *context = rb_pipe->pipe;
1013    struct pipe_resource *resource = rb_resource->resource;
1014
1015    context->transfer_inline_write(context,
1016                                   resource,
1017                                   sr,
1018                                   usage,
1019                                   box,
1020                                   data,
1021                                   stride,
1022                                   slice_stride);
1023 }
1024
1025
1026 struct pipe_context *
1027 rbug_context_create(struct pipe_screen *_screen, struct pipe_context *pipe)
1028 {
1029    struct rbug_context *rb_pipe;
1030    struct rbug_screen *rb_screen = rbug_screen(_screen);
1031
1032    if (!rb_screen)
1033       return NULL;
1034
1035    rb_pipe = CALLOC_STRUCT(rbug_context);
1036    if (!rb_pipe)
1037       return NULL;
1038
1039    pipe_mutex_init(rb_pipe->draw_mutex);
1040    pipe_condvar_init(rb_pipe->draw_cond);
1041    pipe_mutex_init(rb_pipe->call_mutex);
1042    pipe_mutex_init(rb_pipe->list_mutex);
1043    make_empty_list(&rb_pipe->shaders);
1044
1045    rb_pipe->base.winsys = NULL;
1046    rb_pipe->base.screen = _screen;
1047    rb_pipe->base.priv = pipe->priv; /* expose wrapped data */
1048    rb_pipe->base.draw = NULL;
1049
1050    rb_pipe->base.destroy = rbug_destroy;
1051    rb_pipe->base.draw_arrays = rbug_draw_arrays;
1052    rb_pipe->base.draw_elements = rbug_draw_elements;
1053    rb_pipe->base.draw_range_elements = rbug_draw_range_elements;
1054    rb_pipe->base.create_query = rbug_create_query;
1055    rb_pipe->base.destroy_query = rbug_destroy_query;
1056    rb_pipe->base.begin_query = rbug_begin_query;
1057    rb_pipe->base.end_query = rbug_end_query;
1058    rb_pipe->base.get_query_result = rbug_get_query_result;
1059    rb_pipe->base.create_blend_state = rbug_create_blend_state;
1060    rb_pipe->base.bind_blend_state = rbug_bind_blend_state;
1061    rb_pipe->base.delete_blend_state = rbug_delete_blend_state;
1062    rb_pipe->base.create_sampler_state = rbug_create_sampler_state;
1063    rb_pipe->base.bind_fragment_sampler_states = rbug_bind_fragment_sampler_states;
1064    rb_pipe->base.bind_vertex_sampler_states = rbug_bind_vertex_sampler_states;
1065    rb_pipe->base.delete_sampler_state = rbug_delete_sampler_state;
1066    rb_pipe->base.create_rasterizer_state = rbug_create_rasterizer_state;
1067    rb_pipe->base.bind_rasterizer_state = rbug_bind_rasterizer_state;
1068    rb_pipe->base.delete_rasterizer_state = rbug_delete_rasterizer_state;
1069    rb_pipe->base.create_depth_stencil_alpha_state = rbug_create_depth_stencil_alpha_state;
1070    rb_pipe->base.bind_depth_stencil_alpha_state = rbug_bind_depth_stencil_alpha_state;
1071    rb_pipe->base.delete_depth_stencil_alpha_state = rbug_delete_depth_stencil_alpha_state;
1072    rb_pipe->base.create_fs_state = rbug_create_fs_state;
1073    rb_pipe->base.bind_fs_state = rbug_bind_fs_state;
1074    rb_pipe->base.delete_fs_state = rbug_delete_fs_state;
1075    rb_pipe->base.create_vs_state = rbug_create_vs_state;
1076    rb_pipe->base.bind_vs_state = rbug_bind_vs_state;
1077    rb_pipe->base.delete_vs_state = rbug_delete_vs_state;
1078    rb_pipe->base.create_gs_state = rbug_create_gs_state;
1079    rb_pipe->base.bind_gs_state = rbug_bind_gs_state;
1080    rb_pipe->base.delete_gs_state = rbug_delete_gs_state;
1081    rb_pipe->base.create_vertex_elements_state = rbug_create_vertex_elements_state;
1082    rb_pipe->base.bind_vertex_elements_state = rbug_bind_vertex_elements_state;
1083    rb_pipe->base.delete_vertex_elements_state = rbug_delete_vertex_elements_state;
1084    rb_pipe->base.set_blend_color = rbug_set_blend_color;
1085    rb_pipe->base.set_stencil_ref = rbug_set_stencil_ref;
1086    rb_pipe->base.set_clip_state = rbug_set_clip_state;
1087    rb_pipe->base.set_constant_buffer = rbug_set_constant_buffer;
1088    rb_pipe->base.set_framebuffer_state = rbug_set_framebuffer_state;
1089    rb_pipe->base.set_polygon_stipple = rbug_set_polygon_stipple;
1090    rb_pipe->base.set_scissor_state = rbug_set_scissor_state;
1091    rb_pipe->base.set_viewport_state = rbug_set_viewport_state;
1092    rb_pipe->base.set_fragment_sampler_views = rbug_set_fragment_sampler_views;
1093    rb_pipe->base.set_vertex_sampler_views = rbug_set_vertex_sampler_views;
1094    rb_pipe->base.set_vertex_buffers = rbug_set_vertex_buffers;
1095    rb_pipe->base.set_sample_mask = rbug_set_sample_mask;
1096    rb_pipe->base.resource_copy_region = rbug_resource_copy_region;
1097    rb_pipe->base.clear = rbug_clear;
1098    rb_pipe->base.clear_render_target = rbug_clear_render_target;
1099    rb_pipe->base.clear_depth_stencil = rbug_clear_depth_stencil;
1100    rb_pipe->base.flush = rbug_flush;
1101    rb_pipe->base.is_resource_referenced = rbug_is_resource_referenced;
1102    rb_pipe->base.create_sampler_view = rbug_context_create_sampler_view;
1103    rb_pipe->base.sampler_view_destroy = rbug_context_sampler_view_destroy;
1104    rb_pipe->base.get_transfer = rbug_context_get_transfer;
1105    rb_pipe->base.transfer_destroy = rbug_context_transfer_destroy;
1106    rb_pipe->base.transfer_map = rbug_context_transfer_map;
1107    rb_pipe->base.transfer_unmap = rbug_context_transfer_unmap;
1108    rb_pipe->base.transfer_flush_region = rbug_context_transfer_flush_region;
1109    rb_pipe->base.transfer_inline_write = rbug_context_transfer_inline_write;
1110
1111    rb_pipe->pipe = pipe;
1112
1113    rbug_screen_add_to_list(rb_screen, contexts, rb_pipe);
1114
1115    return &rb_pipe->base;
1116 }