glhd_context.c revision a508d2dddcc67d0f92cc36b9ed6f36a9bbfc579d
1/**************************************************************************
2 *
3 * Copyright 2009 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
31#include "util/u_format.h"
32#include "util/u_memory.h"
33#include "util/u_inlines.h"
34
35#include "glhd_context.h"
36#include "glhd_objects.h"
37
38
39static void
40galahad_destroy(struct pipe_context *_pipe)
41{
42   struct galahad_context *glhd_pipe = galahad_context(_pipe);
43   struct pipe_context *pipe = glhd_pipe->pipe;
44
45   pipe->destroy(pipe);
46
47   FREE(glhd_pipe);
48}
49
50static void
51galahad_draw_vbo(struct pipe_context *_pipe,
52                 const struct pipe_draw_info *info)
53{
54   struct galahad_context *glhd_pipe = galahad_context(_pipe);
55   struct pipe_context *pipe = glhd_pipe->pipe;
56
57   /* XXX we should check that all bound resources are unmapped
58    * before drawing.
59    */
60
61   pipe->draw_vbo(pipe, info);
62}
63
64static struct pipe_query *
65galahad_create_query(struct pipe_context *_pipe,
66                      unsigned query_type)
67{
68   struct galahad_context *glhd_pipe = galahad_context(_pipe);
69   struct pipe_context *pipe = glhd_pipe->pipe;
70
71   if (query_type == PIPE_QUERY_OCCLUSION_COUNTER &&
72      !pipe->screen->get_param(pipe->screen, PIPE_CAP_OCCLUSION_QUERY)) {
73      glhd_error("Occlusion query requested but not supported");
74   }
75
76   if (query_type == PIPE_QUERY_TIME_ELAPSED &&
77      !pipe->screen->get_param(pipe->screen, PIPE_CAP_TIMER_QUERY)) {
78      glhd_error("Timer query requested but not supported");
79   }
80
81   return pipe->create_query(pipe,
82                             query_type);
83}
84
85static void
86galahad_destroy_query(struct pipe_context *_pipe,
87                       struct pipe_query *query)
88{
89   struct galahad_context *glhd_pipe = galahad_context(_pipe);
90   struct pipe_context *pipe = glhd_pipe->pipe;
91
92   pipe->destroy_query(pipe,
93                       query);
94}
95
96static void
97galahad_begin_query(struct pipe_context *_pipe,
98                     struct pipe_query *query)
99{
100   struct galahad_context *glhd_pipe = galahad_context(_pipe);
101   struct pipe_context *pipe = glhd_pipe->pipe;
102
103   pipe->begin_query(pipe,
104                     query);
105}
106
107static void
108galahad_end_query(struct pipe_context *_pipe,
109                   struct pipe_query *query)
110{
111   struct galahad_context *glhd_pipe = galahad_context(_pipe);
112   struct pipe_context *pipe = glhd_pipe->pipe;
113
114   pipe->end_query(pipe,
115                   query);
116}
117
118static boolean
119galahad_get_query_result(struct pipe_context *_pipe,
120                          struct pipe_query *query,
121                          boolean wait,
122                          void *result)
123{
124   struct galahad_context *glhd_pipe = galahad_context(_pipe);
125   struct pipe_context *pipe = glhd_pipe->pipe;
126
127   return pipe->get_query_result(pipe,
128                                 query,
129                                 wait,
130                                 result);
131}
132
133static void *
134galahad_create_blend_state(struct pipe_context *_pipe,
135                            const struct pipe_blend_state *blend)
136{
137   struct galahad_context *glhd_pipe = galahad_context(_pipe);
138   struct pipe_context *pipe = glhd_pipe->pipe;
139
140   if (blend->logicop_enable) {
141      if (blend->rt[0].blend_enable) {
142         glhd_warn("Blending enabled for render target 0, but logicops "
143            "are enabled");
144      }
145   }
146
147   return pipe->create_blend_state(pipe,
148                                   blend);
149}
150
151static void
152galahad_bind_blend_state(struct pipe_context *_pipe,
153                          void *blend)
154{
155   struct galahad_context *glhd_pipe = galahad_context(_pipe);
156   struct pipe_context *pipe = glhd_pipe->pipe;
157
158   pipe->bind_blend_state(pipe,
159                              blend);
160}
161
162static void
163galahad_delete_blend_state(struct pipe_context *_pipe,
164                            void *blend)
165{
166   struct galahad_context *glhd_pipe = galahad_context(_pipe);
167   struct pipe_context *pipe = glhd_pipe->pipe;
168
169   pipe->delete_blend_state(pipe,
170                            blend);
171}
172
173static void *
174galahad_create_sampler_state(struct pipe_context *_pipe,
175                              const struct pipe_sampler_state *sampler)
176{
177   struct galahad_context *glhd_pipe = galahad_context(_pipe);
178   struct pipe_context *pipe = glhd_pipe->pipe;
179
180   return pipe->create_sampler_state(pipe,
181                                     sampler);
182}
183
184static void
185galahad_bind_fragment_sampler_states(struct pipe_context *_pipe,
186                                      unsigned num_samplers,
187                                      void **samplers)
188{
189   struct galahad_context *glhd_pipe = galahad_context(_pipe);
190   struct pipe_context *pipe = glhd_pipe->pipe;
191
192   if (num_samplers > PIPE_MAX_SAMPLERS) {
193      glhd_error("%u fragment samplers requested, "
194         "but only %u are permitted by API",
195         num_samplers, PIPE_MAX_SAMPLERS);
196   }
197
198   pipe->bind_fragment_sampler_states(pipe,
199                                      num_samplers,
200                                      samplers);
201}
202
203static void
204galahad_bind_vertex_sampler_states(struct pipe_context *_pipe,
205                                    unsigned num_samplers,
206                                    void **samplers)
207{
208   struct galahad_context *glhd_pipe = galahad_context(_pipe);
209   struct pipe_context *pipe = glhd_pipe->pipe;
210
211   if (num_samplers > PIPE_MAX_VERTEX_SAMPLERS) {
212      glhd_error("%u vertex samplers requested, "
213         "but only %u are permitted by API",
214         num_samplers, PIPE_MAX_VERTEX_SAMPLERS);
215   }
216
217   pipe->bind_vertex_sampler_states(pipe,
218                                    num_samplers,
219                                    samplers);
220}
221
222static void
223galahad_delete_sampler_state(struct pipe_context *_pipe,
224                              void *sampler)
225{
226   struct galahad_context *glhd_pipe = galahad_context(_pipe);
227   struct pipe_context *pipe = glhd_pipe->pipe;
228
229   pipe->delete_sampler_state(pipe,
230                              sampler);
231}
232
233static void *
234galahad_create_rasterizer_state(struct pipe_context *_pipe,
235                                 const struct pipe_rasterizer_state *rasterizer)
236{
237   struct galahad_context *glhd_pipe = galahad_context(_pipe);
238   struct pipe_context *pipe = glhd_pipe->pipe;
239
240   if (rasterizer->point_quad_rasterization) {
241       if (rasterizer->point_smooth) {
242           glhd_warn("Point smoothing requested but ignored");
243       }
244   } else {
245       if (rasterizer->sprite_coord_enable) {
246           glhd_warn("Point sprites requested but ignored");
247       }
248   }
249
250   return pipe->create_rasterizer_state(pipe,
251                                        rasterizer);
252}
253
254static void
255galahad_bind_rasterizer_state(struct pipe_context *_pipe,
256                               void *rasterizer)
257{
258   struct galahad_context *glhd_pipe = galahad_context(_pipe);
259   struct pipe_context *pipe = glhd_pipe->pipe;
260
261   pipe->bind_rasterizer_state(pipe,
262                               rasterizer);
263}
264
265static void
266galahad_delete_rasterizer_state(struct pipe_context *_pipe,
267                                 void *rasterizer)
268{
269   struct galahad_context *glhd_pipe = galahad_context(_pipe);
270   struct pipe_context *pipe = glhd_pipe->pipe;
271
272   pipe->delete_rasterizer_state(pipe,
273                                 rasterizer);
274}
275
276static void *
277galahad_create_depth_stencil_alpha_state(struct pipe_context *_pipe,
278                                          const struct pipe_depth_stencil_alpha_state *depth_stencil_alpha)
279{
280   struct galahad_context *glhd_pipe = galahad_context(_pipe);
281   struct pipe_context *pipe = glhd_pipe->pipe;
282
283   return pipe->create_depth_stencil_alpha_state(pipe,
284                                                 depth_stencil_alpha);
285}
286
287static void
288galahad_bind_depth_stencil_alpha_state(struct pipe_context *_pipe,
289                                        void *depth_stencil_alpha)
290{
291   struct galahad_context *glhd_pipe = galahad_context(_pipe);
292   struct pipe_context *pipe = glhd_pipe->pipe;
293
294   pipe->bind_depth_stencil_alpha_state(pipe,
295                                        depth_stencil_alpha);
296}
297
298static void
299galahad_delete_depth_stencil_alpha_state(struct pipe_context *_pipe,
300                                          void *depth_stencil_alpha)
301{
302   struct galahad_context *glhd_pipe = galahad_context(_pipe);
303   struct pipe_context *pipe = glhd_pipe->pipe;
304
305   pipe->delete_depth_stencil_alpha_state(pipe,
306                                          depth_stencil_alpha);
307}
308
309static void *
310galahad_create_fs_state(struct pipe_context *_pipe,
311                         const struct pipe_shader_state *fs)
312{
313   struct galahad_context *glhd_pipe = galahad_context(_pipe);
314   struct pipe_context *pipe = glhd_pipe->pipe;
315
316   return pipe->create_fs_state(pipe,
317                                fs);
318}
319
320static void
321galahad_bind_fs_state(struct pipe_context *_pipe,
322                       void *fs)
323{
324   struct galahad_context *glhd_pipe = galahad_context(_pipe);
325   struct pipe_context *pipe = glhd_pipe->pipe;
326
327   pipe->bind_fs_state(pipe,
328                       fs);
329}
330
331static void
332galahad_delete_fs_state(struct pipe_context *_pipe,
333                         void *fs)
334{
335   struct galahad_context *glhd_pipe = galahad_context(_pipe);
336   struct pipe_context *pipe = glhd_pipe->pipe;
337
338   pipe->delete_fs_state(pipe,
339                         fs);
340}
341
342static void *
343galahad_create_vs_state(struct pipe_context *_pipe,
344                         const struct pipe_shader_state *vs)
345{
346   struct galahad_context *glhd_pipe = galahad_context(_pipe);
347   struct pipe_context *pipe = glhd_pipe->pipe;
348
349   return pipe->create_vs_state(pipe,
350                                vs);
351}
352
353static void
354galahad_bind_vs_state(struct pipe_context *_pipe,
355                       void *vs)
356{
357   struct galahad_context *glhd_pipe = galahad_context(_pipe);
358   struct pipe_context *pipe = glhd_pipe->pipe;
359
360   pipe->bind_vs_state(pipe,
361                       vs);
362}
363
364static void
365galahad_delete_vs_state(struct pipe_context *_pipe,
366                         void *vs)
367{
368   struct galahad_context *glhd_pipe = galahad_context(_pipe);
369   struct pipe_context *pipe = glhd_pipe->pipe;
370
371   pipe->delete_vs_state(pipe,
372                         vs);
373}
374
375
376static void *
377galahad_create_vertex_elements_state(struct pipe_context *_pipe,
378                                      unsigned num_elements,
379                                      const struct pipe_vertex_element *vertex_elements)
380{
381   struct galahad_context *glhd_pipe = galahad_context(_pipe);
382   struct pipe_context *pipe = glhd_pipe->pipe;
383
384   return pipe->create_vertex_elements_state(pipe,
385                                             num_elements,
386                                             vertex_elements);
387}
388
389static void
390galahad_bind_vertex_elements_state(struct pipe_context *_pipe,
391                                    void *velems)
392{
393   struct galahad_context *glhd_pipe = galahad_context(_pipe);
394   struct pipe_context *pipe = glhd_pipe->pipe;
395
396   pipe->bind_vertex_elements_state(pipe,
397                                    velems);
398}
399
400static void
401galahad_delete_vertex_elements_state(struct pipe_context *_pipe,
402                                      void *velems)
403{
404   struct galahad_context *glhd_pipe = galahad_context(_pipe);
405   struct pipe_context *pipe = glhd_pipe->pipe;
406
407   pipe->delete_vertex_elements_state(pipe,
408                                      velems);
409}
410
411static void
412galahad_set_blend_color(struct pipe_context *_pipe,
413                         const struct pipe_blend_color *blend_color)
414{
415   struct galahad_context *glhd_pipe = galahad_context(_pipe);
416   struct pipe_context *pipe = glhd_pipe->pipe;
417
418   pipe->set_blend_color(pipe,
419                         blend_color);
420}
421
422static void
423galahad_set_stencil_ref(struct pipe_context *_pipe,
424                         const struct pipe_stencil_ref *stencil_ref)
425{
426   struct galahad_context *glhd_pipe = galahad_context(_pipe);
427   struct pipe_context *pipe = glhd_pipe->pipe;
428
429   pipe->set_stencil_ref(pipe,
430                         stencil_ref);
431}
432
433static void
434galahad_set_clip_state(struct pipe_context *_pipe,
435                        const struct pipe_clip_state *clip)
436{
437   struct galahad_context *glhd_pipe = galahad_context(_pipe);
438   struct pipe_context *pipe = glhd_pipe->pipe;
439
440   pipe->set_clip_state(pipe,
441                        clip);
442}
443
444static void
445galahad_set_sample_mask(struct pipe_context *_pipe,
446                         unsigned sample_mask)
447{
448   struct galahad_context *glhd_pipe = galahad_context(_pipe);
449   struct pipe_context *pipe = glhd_pipe->pipe;
450
451   pipe->set_sample_mask(pipe,
452                         sample_mask);
453}
454
455static void
456galahad_set_constant_buffer(struct pipe_context *_pipe,
457                             uint shader,
458                             uint index,
459                             struct pipe_resource *_resource)
460{
461   struct galahad_context *glhd_pipe = galahad_context(_pipe);
462   struct pipe_context *pipe = glhd_pipe->pipe;
463   struct pipe_resource *unwrapped_resource;
464   struct pipe_resource *resource = NULL;
465
466   if (shader >= PIPE_SHADER_TYPES) {
467      glhd_error("Unknown shader type %u", shader);
468   }
469
470   if (index &&
471      index >=
472         pipe->screen->get_shader_param(pipe->screen, shader, PIPE_SHADER_CAP_MAX_CONST_BUFFERS)) {
473      glhd_error("Access to constant buffer %u requested, "
474         "but only %d are supported",
475         index,
476         pipe->screen->get_shader_param(pipe->screen, shader, PIPE_SHADER_CAP_MAX_CONST_BUFFERS));
477   }
478
479   /* XXX hmm? unwrap the input state */
480   if (_resource) {
481      unwrapped_resource = galahad_resource_unwrap(_resource);
482      resource = unwrapped_resource;
483   }
484
485   pipe->set_constant_buffer(pipe,
486                             shader,
487                             index,
488                             resource);
489}
490
491static void
492galahad_set_framebuffer_state(struct pipe_context *_pipe,
493                               const struct pipe_framebuffer_state *_state)
494{
495   struct galahad_context *glhd_pipe = galahad_context(_pipe);
496   struct pipe_context *pipe = glhd_pipe->pipe;
497   struct pipe_framebuffer_state unwrapped_state;
498   struct pipe_framebuffer_state *state = NULL;
499   unsigned i;
500
501   if (_state->nr_cbufs > PIPE_MAX_COLOR_BUFS) {
502      glhd_error("%d render targets bound, but only %d are permitted by API",
503         _state->nr_cbufs, PIPE_MAX_COLOR_BUFS);
504   } else if (_state->nr_cbufs >
505      pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS)) {
506      glhd_warn("%d render targets bound, but only %d are supported",
507         _state->nr_cbufs,
508         pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS));
509   }
510
511   /* unwrap the input state */
512   if (_state) {
513      memcpy(&unwrapped_state, _state, sizeof(unwrapped_state));
514      for(i = 0; i < _state->nr_cbufs; i++)
515         unwrapped_state.cbufs[i] = galahad_surface_unwrap(_state->cbufs[i]);
516      for (; i < PIPE_MAX_COLOR_BUFS; i++)
517         unwrapped_state.cbufs[i] = NULL;
518      unwrapped_state.zsbuf = galahad_surface_unwrap(_state->zsbuf);
519      state = &unwrapped_state;
520   }
521
522   pipe->set_framebuffer_state(pipe,
523                               state);
524}
525
526static void
527galahad_set_polygon_stipple(struct pipe_context *_pipe,
528                             const struct pipe_poly_stipple *poly_stipple)
529{
530   struct galahad_context *glhd_pipe = galahad_context(_pipe);
531   struct pipe_context *pipe = glhd_pipe->pipe;
532
533   pipe->set_polygon_stipple(pipe,
534                             poly_stipple);
535}
536
537static void
538galahad_set_scissor_state(struct pipe_context *_pipe,
539                           const struct pipe_scissor_state *scissor)
540{
541   struct galahad_context *glhd_pipe = galahad_context(_pipe);
542   struct pipe_context *pipe = glhd_pipe->pipe;
543
544   pipe->set_scissor_state(pipe,
545                           scissor);
546}
547
548static void
549galahad_set_viewport_state(struct pipe_context *_pipe,
550                            const struct pipe_viewport_state *viewport)
551{
552   struct galahad_context *glhd_pipe = galahad_context(_pipe);
553   struct pipe_context *pipe = glhd_pipe->pipe;
554
555   pipe->set_viewport_state(pipe,
556                            viewport);
557}
558
559static void
560galahad_set_fragment_sampler_views(struct pipe_context *_pipe,
561                                    unsigned num,
562                                    struct pipe_sampler_view **_views)
563{
564   struct galahad_context *glhd_pipe = galahad_context(_pipe);
565   struct pipe_context *pipe = glhd_pipe->pipe;
566   struct pipe_sampler_view *unwrapped_views[PIPE_MAX_SAMPLERS];
567   struct pipe_sampler_view **views = NULL;
568   unsigned i;
569
570   if (_views) {
571      for (i = 0; i < num; i++)
572         unwrapped_views[i] = galahad_sampler_view_unwrap(_views[i]);
573      for (; i < PIPE_MAX_SAMPLERS; i++)
574         unwrapped_views[i] = NULL;
575
576      views = unwrapped_views;
577   }
578
579   pipe->set_fragment_sampler_views(pipe, num, views);
580}
581
582static void
583galahad_set_vertex_sampler_views(struct pipe_context *_pipe,
584                                  unsigned num,
585                                  struct pipe_sampler_view **_views)
586{
587   struct galahad_context *glhd_pipe = galahad_context(_pipe);
588   struct pipe_context *pipe = glhd_pipe->pipe;
589   struct pipe_sampler_view *unwrapped_views[PIPE_MAX_VERTEX_SAMPLERS];
590   struct pipe_sampler_view **views = NULL;
591   unsigned i;
592
593   if (_views) {
594      for (i = 0; i < num; i++)
595         unwrapped_views[i] = galahad_sampler_view_unwrap(_views[i]);
596      for (; i < PIPE_MAX_VERTEX_SAMPLERS; i++)
597         unwrapped_views[i] = NULL;
598
599      views = unwrapped_views;
600   }
601
602   pipe->set_vertex_sampler_views(pipe, num, views);
603}
604
605static void
606galahad_set_vertex_buffers(struct pipe_context *_pipe,
607                            unsigned num_buffers,
608                            const struct pipe_vertex_buffer *_buffers)
609{
610   struct galahad_context *glhd_pipe = galahad_context(_pipe);
611   struct pipe_context *pipe = glhd_pipe->pipe;
612   struct pipe_vertex_buffer unwrapped_buffers[PIPE_MAX_SHADER_INPUTS];
613   struct pipe_vertex_buffer *buffers = NULL;
614   unsigned i;
615
616   if (num_buffers) {
617      memcpy(unwrapped_buffers, _buffers, num_buffers * sizeof(*_buffers));
618      for (i = 0; i < num_buffers; i++)
619         unwrapped_buffers[i].buffer = galahad_resource_unwrap(_buffers[i].buffer);
620      buffers = unwrapped_buffers;
621   }
622
623   pipe->set_vertex_buffers(pipe,
624                            num_buffers,
625                            buffers);
626}
627
628static void
629galahad_set_index_buffer(struct pipe_context *_pipe,
630                         const struct pipe_index_buffer *_ib)
631{
632   struct galahad_context *glhd_pipe = galahad_context(_pipe);
633   struct pipe_context *pipe = glhd_pipe->pipe;
634   struct pipe_index_buffer unwrapped_ib, *ib = NULL;
635
636   if (_ib->buffer) {
637      switch (_ib->index_size) {
638      case 1:
639      case 2:
640      case 4:
641         break;
642      default:
643         glhd_warn("index buffer %p has unrecognized index size %d",
644               _ib->buffer, _ib->index_size);
645         break;
646      }
647   }
648   else if (_ib->offset || _ib->index_size) {
649      glhd_warn("non-indexed state with index offset %d and index size %d",
650            _ib->offset, _ib->index_size);
651   }
652
653   if (_ib) {
654      unwrapped_ib = *_ib;
655      unwrapped_ib.buffer = galahad_resource_unwrap(_ib->buffer);
656      ib = &unwrapped_ib;
657   }
658
659   pipe->set_index_buffer(pipe, ib);
660}
661
662static void
663galahad_resource_copy_region(struct pipe_context *_pipe,
664                              struct pipe_resource *_dst,
665                              struct pipe_subresource subdst,
666                              unsigned dstx,
667                              unsigned dsty,
668                              unsigned dstz,
669                              struct pipe_resource *_src,
670                              struct pipe_subresource subsrc,
671                              unsigned srcx,
672                              unsigned srcy,
673                              unsigned srcz,
674                              unsigned width,
675                              unsigned height)
676{
677   struct galahad_context *glhd_pipe = galahad_context(_pipe);
678   struct galahad_resource *glhd_resource_dst = galahad_resource(_dst);
679   struct galahad_resource *glhd_resource_src = galahad_resource(_src);
680   struct pipe_context *pipe = glhd_pipe->pipe;
681   struct pipe_resource *dst = glhd_resource_dst->resource;
682   struct pipe_resource *src = glhd_resource_src->resource;
683
684   if (_dst->format != _src->format) {
685      glhd_warn("Format mismatch: Source is %s, destination is %s",
686         util_format_short_name(_src->format),
687         util_format_short_name(_dst->format));
688   }
689
690   pipe->resource_copy_region(pipe,
691                              dst,
692                              subdst,
693                              dstx,
694                              dsty,
695                              dstz,
696                              src,
697                              subsrc,
698                              srcx,
699                              srcy,
700                              srcz,
701                              width,
702                              height);
703}
704
705static void
706galahad_clear(struct pipe_context *_pipe,
707               unsigned buffers,
708               const float *rgba,
709               double depth,
710               unsigned stencil)
711{
712   struct galahad_context *glhd_pipe = galahad_context(_pipe);
713   struct pipe_context *pipe = glhd_pipe->pipe;
714
715   pipe->clear(pipe,
716               buffers,
717               rgba,
718               depth,
719               stencil);
720}
721
722static void
723galahad_clear_render_target(struct pipe_context *_pipe,
724                             struct pipe_surface *_dst,
725                             const float *rgba,
726                             unsigned dstx, unsigned dsty,
727                             unsigned width, unsigned height)
728{
729   struct galahad_context *glhd_pipe = galahad_context(_pipe);
730   struct galahad_surface *glhd_surface_dst = galahad_surface(_dst);
731   struct pipe_context *pipe = glhd_pipe->pipe;
732   struct pipe_surface *dst = glhd_surface_dst->surface;
733
734   pipe->clear_render_target(pipe,
735                             dst,
736                             rgba,
737                             dstx,
738                             dsty,
739                             width,
740                             height);
741}
742static void
743galahad_clear_depth_stencil(struct pipe_context *_pipe,
744                             struct pipe_surface *_dst,
745                             unsigned clear_flags,
746                             double depth,
747                             unsigned stencil,
748                             unsigned dstx, unsigned dsty,
749                             unsigned width, unsigned height)
750{
751   struct galahad_context *glhd_pipe = galahad_context(_pipe);
752   struct galahad_surface *glhd_surface_dst = galahad_surface(_dst);
753   struct pipe_context *pipe = glhd_pipe->pipe;
754   struct pipe_surface *dst = glhd_surface_dst->surface;
755
756   pipe->clear_depth_stencil(pipe,
757                             dst,
758                             clear_flags,
759                             depth,
760                             stencil,
761                             dstx,
762                             dsty,
763                             width,
764                             height);
765
766}
767
768static void
769galahad_flush(struct pipe_context *_pipe,
770               unsigned flags,
771               struct pipe_fence_handle **fence)
772{
773   struct galahad_context *glhd_pipe = galahad_context(_pipe);
774   struct pipe_context *pipe = glhd_pipe->pipe;
775
776   pipe->flush(pipe,
777               flags,
778               fence);
779}
780
781static unsigned int
782galahad_is_resource_referenced(struct pipe_context *_pipe,
783                                struct pipe_resource *_resource,
784                                unsigned face,
785                                unsigned level)
786{
787   struct galahad_context *glhd_pipe = galahad_context(_pipe);
788   struct galahad_resource *glhd_resource = galahad_resource(_resource);
789   struct pipe_context *pipe = glhd_pipe->pipe;
790   struct pipe_resource *resource = glhd_resource->resource;
791
792   return pipe->is_resource_referenced(pipe,
793                                       resource,
794                                       face,
795                                       level);
796}
797
798static struct pipe_sampler_view *
799galahad_context_create_sampler_view(struct pipe_context *_pipe,
800                                     struct pipe_resource *_resource,
801                                     const struct pipe_sampler_view *templ)
802{
803   struct galahad_context *glhd_context = galahad_context(_pipe);
804   struct galahad_resource *glhd_resource = galahad_resource(_resource);
805   struct pipe_context *pipe = glhd_context->pipe;
806   struct pipe_resource *resource = glhd_resource->resource;
807   struct pipe_sampler_view *result;
808
809   result = pipe->create_sampler_view(pipe,
810                                      resource,
811                                      templ);
812
813   if (result)
814      return galahad_sampler_view_create(glhd_context, glhd_resource, result);
815   return NULL;
816}
817
818static void
819galahad_context_sampler_view_destroy(struct pipe_context *_pipe,
820                                      struct pipe_sampler_view *_view)
821{
822   galahad_sampler_view_destroy(galahad_context(_pipe),
823                                 galahad_sampler_view(_view));
824}
825
826static struct pipe_transfer *
827galahad_context_get_transfer(struct pipe_context *_context,
828                              struct pipe_resource *_resource,
829                              struct pipe_subresource sr,
830                              unsigned usage,
831                              const struct pipe_box *box)
832{
833   struct galahad_context *glhd_context = galahad_context(_context);
834   struct galahad_resource *glhd_resource = galahad_resource(_resource);
835   struct pipe_context *context = glhd_context->pipe;
836   struct pipe_resource *resource = glhd_resource->resource;
837   struct pipe_transfer *result;
838
839   result = context->get_transfer(context,
840                                  resource,
841                                  sr,
842                                  usage,
843                                  box);
844
845   if (result)
846      return galahad_transfer_create(glhd_context, glhd_resource, result);
847   return NULL;
848}
849
850static void
851galahad_context_transfer_destroy(struct pipe_context *_pipe,
852                                  struct pipe_transfer *_transfer)
853{
854   galahad_transfer_destroy(galahad_context(_pipe),
855                             galahad_transfer(_transfer));
856}
857
858static void *
859galahad_context_transfer_map(struct pipe_context *_context,
860                              struct pipe_transfer *_transfer)
861{
862   struct galahad_context *glhd_context = galahad_context(_context);
863   struct galahad_transfer *glhd_transfer = galahad_transfer(_transfer);
864   struct pipe_context *context = glhd_context->pipe;
865   struct pipe_transfer *transfer = glhd_transfer->transfer;
866
867   struct galahad_resource *glhd_resource = galahad_resource(_transfer->resource);
868
869   glhd_resource->map_count++;
870
871   return context->transfer_map(context,
872                                transfer);
873}
874
875
876
877static void
878galahad_context_transfer_flush_region(struct pipe_context *_context,
879                                       struct pipe_transfer *_transfer,
880                                       const struct pipe_box *box)
881{
882   struct galahad_context *glhd_context = galahad_context(_context);
883   struct galahad_transfer *glhd_transfer = galahad_transfer(_transfer);
884   struct pipe_context *context = glhd_context->pipe;
885   struct pipe_transfer *transfer = glhd_transfer->transfer;
886
887   context->transfer_flush_region(context,
888                                  transfer,
889                                  box);
890}
891
892
893static void
894galahad_context_transfer_unmap(struct pipe_context *_context,
895                                struct pipe_transfer *_transfer)
896{
897   struct galahad_context *glhd_context = galahad_context(_context);
898   struct galahad_transfer *glhd_transfer = galahad_transfer(_transfer);
899   struct pipe_context *context = glhd_context->pipe;
900   struct pipe_transfer *transfer = glhd_transfer->transfer;
901   struct galahad_resource *glhd_resource = galahad_resource(_transfer->resource);
902
903   if (glhd_resource->map_count < 1) {
904      glhd_warn("context::transfer_unmap() called too many times"
905                " (count = %d)\n", glhd_resource->map_count);
906   }
907
908   glhd_resource->map_count--;
909
910   context->transfer_unmap(context,
911                           transfer);
912}
913
914
915static void
916galahad_context_transfer_inline_write(struct pipe_context *_context,
917                                       struct pipe_resource *_resource,
918                                       struct pipe_subresource sr,
919                                       unsigned usage,
920                                       const struct pipe_box *box,
921                                       const void *data,
922                                       unsigned stride,
923                                       unsigned slice_stride)
924{
925   struct galahad_context *glhd_context = galahad_context(_context);
926   struct galahad_resource *glhd_resource = galahad_resource(_resource);
927   struct pipe_context *context = glhd_context->pipe;
928   struct pipe_resource *resource = glhd_resource->resource;
929
930   context->transfer_inline_write(context,
931                                  resource,
932                                  sr,
933                                  usage,
934                                  box,
935                                  data,
936                                  stride,
937                                  slice_stride);
938}
939
940
941struct pipe_context *
942galahad_context_create(struct pipe_screen *_screen, struct pipe_context *pipe)
943{
944   struct galahad_context *glhd_pipe;
945   (void)galahad_screen(_screen);
946
947   glhd_pipe = CALLOC_STRUCT(galahad_context);
948   if (!glhd_pipe) {
949      return NULL;
950   }
951
952   glhd_pipe->base.winsys = NULL;
953   glhd_pipe->base.screen = _screen;
954   glhd_pipe->base.priv = pipe->priv; /* expose wrapped data */
955   glhd_pipe->base.draw = NULL;
956
957   glhd_pipe->base.destroy = galahad_destroy;
958   glhd_pipe->base.draw_vbo = galahad_draw_vbo;
959   glhd_pipe->base.create_query = galahad_create_query;
960   glhd_pipe->base.destroy_query = galahad_destroy_query;
961   glhd_pipe->base.begin_query = galahad_begin_query;
962   glhd_pipe->base.end_query = galahad_end_query;
963   glhd_pipe->base.get_query_result = galahad_get_query_result;
964   glhd_pipe->base.create_blend_state = galahad_create_blend_state;
965   glhd_pipe->base.bind_blend_state = galahad_bind_blend_state;
966   glhd_pipe->base.delete_blend_state = galahad_delete_blend_state;
967   glhd_pipe->base.create_sampler_state = galahad_create_sampler_state;
968   glhd_pipe->base.bind_fragment_sampler_states = galahad_bind_fragment_sampler_states;
969   glhd_pipe->base.bind_vertex_sampler_states = galahad_bind_vertex_sampler_states;
970   glhd_pipe->base.delete_sampler_state = galahad_delete_sampler_state;
971   glhd_pipe->base.create_rasterizer_state = galahad_create_rasterizer_state;
972   glhd_pipe->base.bind_rasterizer_state = galahad_bind_rasterizer_state;
973   glhd_pipe->base.delete_rasterizer_state = galahad_delete_rasterizer_state;
974   glhd_pipe->base.create_depth_stencil_alpha_state = galahad_create_depth_stencil_alpha_state;
975   glhd_pipe->base.bind_depth_stencil_alpha_state = galahad_bind_depth_stencil_alpha_state;
976   glhd_pipe->base.delete_depth_stencil_alpha_state = galahad_delete_depth_stencil_alpha_state;
977   glhd_pipe->base.create_fs_state = galahad_create_fs_state;
978   glhd_pipe->base.bind_fs_state = galahad_bind_fs_state;
979   glhd_pipe->base.delete_fs_state = galahad_delete_fs_state;
980   glhd_pipe->base.create_vs_state = galahad_create_vs_state;
981   glhd_pipe->base.bind_vs_state = galahad_bind_vs_state;
982   glhd_pipe->base.delete_vs_state = galahad_delete_vs_state;
983   glhd_pipe->base.create_vertex_elements_state = galahad_create_vertex_elements_state;
984   glhd_pipe->base.bind_vertex_elements_state = galahad_bind_vertex_elements_state;
985   glhd_pipe->base.delete_vertex_elements_state = galahad_delete_vertex_elements_state;
986   glhd_pipe->base.set_blend_color = galahad_set_blend_color;
987   glhd_pipe->base.set_stencil_ref = galahad_set_stencil_ref;
988   glhd_pipe->base.set_clip_state = galahad_set_clip_state;
989   glhd_pipe->base.set_sample_mask = galahad_set_sample_mask;
990   glhd_pipe->base.set_constant_buffer = galahad_set_constant_buffer;
991   glhd_pipe->base.set_framebuffer_state = galahad_set_framebuffer_state;
992   glhd_pipe->base.set_polygon_stipple = galahad_set_polygon_stipple;
993   glhd_pipe->base.set_scissor_state = galahad_set_scissor_state;
994   glhd_pipe->base.set_viewport_state = galahad_set_viewport_state;
995   glhd_pipe->base.set_fragment_sampler_views = galahad_set_fragment_sampler_views;
996   glhd_pipe->base.set_vertex_sampler_views = galahad_set_vertex_sampler_views;
997   glhd_pipe->base.set_vertex_buffers = galahad_set_vertex_buffers;
998   glhd_pipe->base.set_index_buffer = galahad_set_index_buffer;
999   glhd_pipe->base.resource_copy_region = galahad_resource_copy_region;
1000   glhd_pipe->base.clear = galahad_clear;
1001   glhd_pipe->base.clear_render_target = galahad_clear_render_target;
1002   glhd_pipe->base.clear_depth_stencil = galahad_clear_depth_stencil;
1003   glhd_pipe->base.flush = galahad_flush;
1004   glhd_pipe->base.is_resource_referenced = galahad_is_resource_referenced;
1005   glhd_pipe->base.create_sampler_view = galahad_context_create_sampler_view;
1006   glhd_pipe->base.sampler_view_destroy = galahad_context_sampler_view_destroy;
1007   glhd_pipe->base.get_transfer = galahad_context_get_transfer;
1008   glhd_pipe->base.transfer_destroy = galahad_context_transfer_destroy;
1009   glhd_pipe->base.transfer_map = galahad_context_transfer_map;
1010   glhd_pipe->base.transfer_unmap = galahad_context_transfer_unmap;
1011   glhd_pipe->base.transfer_flush_region = galahad_context_transfer_flush_region;
1012   glhd_pipe->base.transfer_inline_write = galahad_context_transfer_inline_write;
1013
1014   glhd_pipe->pipe = pipe;
1015
1016   glhd_warn("Created context %p", glhd_pipe);
1017
1018   return &glhd_pipe->base;
1019}
1020