gallium: fix type of flags in pipe_context::flush()
[mesa.git] / src / gallium / drivers / galahad / glhd_context.c
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
39 static void
40 galahad_context_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
50 static void
51 galahad_context_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
64 static struct pipe_query *
65 galahad_context_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_QUERY_TIME_ELAPSED)) {
78 glhd_error("Timer query requested but not supported");
79 }
80
81 return pipe->create_query(pipe,
82 query_type);
83 }
84
85 static void
86 galahad_context_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
96 static void
97 galahad_context_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
107 static void
108 galahad_context_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
118 static boolean
119 galahad_context_get_query_result(struct pipe_context *_pipe,
120 struct pipe_query *query,
121 boolean wait,
122 union pipe_query_result *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
133 static void *
134 galahad_context_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
151 static void
152 galahad_context_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
162 static void
163 galahad_context_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
173 static void *
174 galahad_context_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
184 static void
185 galahad_context_bind_sampler_states(struct pipe_context *_pipe,
186 unsigned shader,
187 unsigned start,
188 unsigned num_samplers,
189 void **samplers)
190 {
191 struct galahad_context *glhd_pipe = galahad_context(_pipe);
192 struct pipe_context *pipe = glhd_pipe->pipe;
193
194 if (num_samplers > PIPE_MAX_SAMPLERS) {
195 glhd_error("%u samplers requested, "
196 "but only %u are permitted by API",
197 num_samplers, PIPE_MAX_SAMPLERS);
198 }
199
200 switch (shader) {
201 case PIPE_SHADER_VERTEX:
202 pipe->bind_vertex_sampler_states(pipe, num_samplers, samplers);
203 break;
204 case PIPE_SHADER_FRAGMENT:
205 pipe->bind_fragment_sampler_states(pipe, num_samplers, samplers);
206 break;
207 case PIPE_SHADER_GEOMETRY:
208 pipe->bind_geometry_sampler_states(pipe, num_samplers, samplers);
209 break;
210 default:
211 assert(0);
212 }
213 }
214
215 static void
216 galahad_context_bind_vertex_sampler_states(struct pipe_context *_pipe,
217 unsigned num_samplers,
218 void **samplers)
219 {
220 galahad_context_bind_sampler_states(_pipe, PIPE_SHADER_VERTEX,
221 0, num_samplers, samplers);
222 }
223
224 static void
225 galahad_context_bind_fragment_sampler_states(struct pipe_context *_pipe,
226 unsigned num_samplers,
227 void **samplers)
228 {
229 galahad_context_bind_sampler_states(_pipe, PIPE_SHADER_FRAGMENT,
230 0, num_samplers, samplers);
231 }
232
233 static void
234 galahad_context_bind_geometry_sampler_states(struct pipe_context *_pipe,
235 unsigned num_samplers,
236 void **samplers)
237 {
238 galahad_context_bind_sampler_states(_pipe, PIPE_SHADER_GEOMETRY,
239 0, num_samplers, samplers);
240 }
241
242
243 static void
244 galahad_context_delete_sampler_state(struct pipe_context *_pipe,
245 void *sampler)
246 {
247 struct galahad_context *glhd_pipe = galahad_context(_pipe);
248 struct pipe_context *pipe = glhd_pipe->pipe;
249
250 pipe->delete_sampler_state(pipe,
251 sampler);
252 }
253
254 static void *
255 galahad_context_create_rasterizer_state(struct pipe_context *_pipe,
256 const struct pipe_rasterizer_state *rasterizer)
257 {
258 struct galahad_context *glhd_pipe = galahad_context(_pipe);
259 struct pipe_context *pipe = glhd_pipe->pipe;
260
261 if (rasterizer->point_quad_rasterization) {
262 if (rasterizer->point_smooth) {
263 glhd_warn("Point smoothing requested but ignored");
264 }
265 } else {
266 if (rasterizer->sprite_coord_enable) {
267 glhd_warn("Point sprites requested but ignored");
268 }
269 }
270
271 return pipe->create_rasterizer_state(pipe,
272 rasterizer);
273 }
274
275 static void
276 galahad_context_bind_rasterizer_state(struct pipe_context *_pipe,
277 void *rasterizer)
278 {
279 struct galahad_context *glhd_pipe = galahad_context(_pipe);
280 struct pipe_context *pipe = glhd_pipe->pipe;
281
282 pipe->bind_rasterizer_state(pipe,
283 rasterizer);
284 }
285
286 static void
287 galahad_context_delete_rasterizer_state(struct pipe_context *_pipe,
288 void *rasterizer)
289 {
290 struct galahad_context *glhd_pipe = galahad_context(_pipe);
291 struct pipe_context *pipe = glhd_pipe->pipe;
292
293 pipe->delete_rasterizer_state(pipe,
294 rasterizer);
295 }
296
297 static void *
298 galahad_context_create_depth_stencil_alpha_state(struct pipe_context *_pipe,
299 const struct pipe_depth_stencil_alpha_state *depth_stencil_alpha)
300 {
301 struct galahad_context *glhd_pipe = galahad_context(_pipe);
302 struct pipe_context *pipe = glhd_pipe->pipe;
303
304 return pipe->create_depth_stencil_alpha_state(pipe,
305 depth_stencil_alpha);
306 }
307
308 static void
309 galahad_context_bind_depth_stencil_alpha_state(struct pipe_context *_pipe,
310 void *depth_stencil_alpha)
311 {
312 struct galahad_context *glhd_pipe = galahad_context(_pipe);
313 struct pipe_context *pipe = glhd_pipe->pipe;
314
315 pipe->bind_depth_stencil_alpha_state(pipe,
316 depth_stencil_alpha);
317 }
318
319 static void
320 galahad_context_delete_depth_stencil_alpha_state(struct pipe_context *_pipe,
321 void *depth_stencil_alpha)
322 {
323 struct galahad_context *glhd_pipe = galahad_context(_pipe);
324 struct pipe_context *pipe = glhd_pipe->pipe;
325
326 pipe->delete_depth_stencil_alpha_state(pipe,
327 depth_stencil_alpha);
328 }
329
330 #define GLHD_SHADER_STATE(shader_type) \
331 static void * \
332 galahad_context_create_##shader_type##_state(struct pipe_context *_pipe, \
333 const struct pipe_shader_state *state) \
334 { \
335 struct galahad_context *glhd_pipe = galahad_context(_pipe); \
336 struct pipe_context *pipe = glhd_pipe->pipe; \
337 return pipe->create_##shader_type##_state(pipe, state); \
338 } \
339 \
340 static void \
341 galahad_context_bind_##shader_type##_state(struct pipe_context *_pipe, \
342 void *state) \
343 { \
344 struct galahad_context *glhd_pipe = galahad_context(_pipe); \
345 struct pipe_context *pipe = glhd_pipe->pipe; \
346 pipe->bind_##shader_type##_state(pipe, state); \
347 } \
348 \
349 static void \
350 galahad_context_delete_##shader_type##_state(struct pipe_context *_pipe, \
351 void *state) \
352 { \
353 struct galahad_context *glhd_pipe = galahad_context(_pipe); \
354 struct pipe_context *pipe = glhd_pipe->pipe; \
355 pipe->delete_##shader_type##_state(pipe, state); \
356 }
357
358 GLHD_SHADER_STATE(fs)
359 GLHD_SHADER_STATE(vs)
360 GLHD_SHADER_STATE(gs)
361
362 #undef GLHD_SHADER_STATE
363
364 static void *
365 galahad_context_create_vertex_elements_state(struct pipe_context *_pipe,
366 unsigned num_elements,
367 const struct pipe_vertex_element *vertex_elements)
368 {
369 struct galahad_context *glhd_pipe = galahad_context(_pipe);
370 struct pipe_context *pipe = glhd_pipe->pipe;
371
372 /* XXX check if stride lines up with element size, at least for floats */
373
374 return pipe->create_vertex_elements_state(pipe,
375 num_elements,
376 vertex_elements);
377 }
378
379 static void
380 galahad_context_bind_vertex_elements_state(struct pipe_context *_pipe,
381 void *velems)
382 {
383 struct galahad_context *glhd_pipe = galahad_context(_pipe);
384 struct pipe_context *pipe = glhd_pipe->pipe;
385
386 pipe->bind_vertex_elements_state(pipe,
387 velems);
388 }
389
390 static void
391 galahad_context_delete_vertex_elements_state(struct pipe_context *_pipe,
392 void *velems)
393 {
394 struct galahad_context *glhd_pipe = galahad_context(_pipe);
395 struct pipe_context *pipe = glhd_pipe->pipe;
396
397 pipe->delete_vertex_elements_state(pipe,
398 velems);
399 }
400
401 static void
402 galahad_context_set_blend_color(struct pipe_context *_pipe,
403 const struct pipe_blend_color *blend_color)
404 {
405 struct galahad_context *glhd_pipe = galahad_context(_pipe);
406 struct pipe_context *pipe = glhd_pipe->pipe;
407
408 pipe->set_blend_color(pipe,
409 blend_color);
410 }
411
412 static void
413 galahad_context_set_stencil_ref(struct pipe_context *_pipe,
414 const struct pipe_stencil_ref *stencil_ref)
415 {
416 struct galahad_context *glhd_pipe = galahad_context(_pipe);
417 struct pipe_context *pipe = glhd_pipe->pipe;
418
419 pipe->set_stencil_ref(pipe,
420 stencil_ref);
421 }
422
423 static void
424 galahad_context_set_clip_state(struct pipe_context *_pipe,
425 const struct pipe_clip_state *clip)
426 {
427 struct galahad_context *glhd_pipe = galahad_context(_pipe);
428 struct pipe_context *pipe = glhd_pipe->pipe;
429
430 pipe->set_clip_state(pipe,
431 clip);
432 }
433
434 static void
435 galahad_context_set_sample_mask(struct pipe_context *_pipe,
436 unsigned sample_mask)
437 {
438 struct galahad_context *glhd_pipe = galahad_context(_pipe);
439 struct pipe_context *pipe = glhd_pipe->pipe;
440
441 pipe->set_sample_mask(pipe,
442 sample_mask);
443 }
444
445 static void
446 galahad_context_set_constant_buffer(struct pipe_context *_pipe,
447 uint shader,
448 uint index,
449 struct pipe_constant_buffer *_cb)
450 {
451 struct galahad_context *glhd_pipe = galahad_context(_pipe);
452 struct pipe_context *pipe = glhd_pipe->pipe;
453 struct pipe_constant_buffer cb;
454
455 if (shader >= PIPE_SHADER_TYPES) {
456 glhd_error("Unknown shader type %u", shader);
457 }
458
459 if (index &&
460 index >=
461 pipe->screen->get_shader_param(pipe->screen, shader, PIPE_SHADER_CAP_MAX_CONST_BUFFERS)) {
462 glhd_error("Access to constant buffer %u requested, "
463 "but only %d are supported",
464 index,
465 pipe->screen->get_shader_param(pipe->screen, shader, PIPE_SHADER_CAP_MAX_CONST_BUFFERS));
466 }
467
468 /* XXX hmm? unwrap the input state */
469 if (_cb) {
470 cb = *_cb;
471 cb.buffer = galahad_resource_unwrap(_cb->buffer);
472 }
473
474 pipe->set_constant_buffer(pipe,
475 shader,
476 index,
477 _cb ? &cb : NULL);
478 }
479
480 static void
481 galahad_context_set_framebuffer_state(struct pipe_context *_pipe,
482 const struct pipe_framebuffer_state *_state)
483 {
484 struct galahad_context *glhd_pipe = galahad_context(_pipe);
485 struct pipe_context *pipe = glhd_pipe->pipe;
486 struct pipe_framebuffer_state unwrapped_state;
487 struct pipe_framebuffer_state *state = NULL;
488 unsigned i;
489
490 if (_state->nr_cbufs > PIPE_MAX_COLOR_BUFS) {
491 glhd_error("%d render targets bound, but only %d are permitted by API",
492 _state->nr_cbufs, PIPE_MAX_COLOR_BUFS);
493 } else if (_state->nr_cbufs >
494 pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS)) {
495 glhd_warn("%d render targets bound, but only %d are supported",
496 _state->nr_cbufs,
497 pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_RENDER_TARGETS));
498 }
499
500 /* unwrap the input state */
501 if (_state) {
502 memcpy(&unwrapped_state, _state, sizeof(unwrapped_state));
503 for(i = 0; i < _state->nr_cbufs; i++)
504 unwrapped_state.cbufs[i] = galahad_surface_unwrap(_state->cbufs[i]);
505 for (; i < PIPE_MAX_COLOR_BUFS; i++)
506 unwrapped_state.cbufs[i] = NULL;
507 unwrapped_state.zsbuf = galahad_surface_unwrap(_state->zsbuf);
508 state = &unwrapped_state;
509 }
510
511 pipe->set_framebuffer_state(pipe,
512 state);
513 }
514
515 static void
516 galahad_context_set_polygon_stipple(struct pipe_context *_pipe,
517 const struct pipe_poly_stipple *poly_stipple)
518 {
519 struct galahad_context *glhd_pipe = galahad_context(_pipe);
520 struct pipe_context *pipe = glhd_pipe->pipe;
521
522 pipe->set_polygon_stipple(pipe,
523 poly_stipple);
524 }
525
526 static void
527 galahad_context_set_scissor_state(struct pipe_context *_pipe,
528 const struct pipe_scissor_state *scissor)
529 {
530 struct galahad_context *glhd_pipe = galahad_context(_pipe);
531 struct pipe_context *pipe = glhd_pipe->pipe;
532
533 pipe->set_scissor_state(pipe,
534 scissor);
535 }
536
537 static void
538 galahad_context_set_viewport_state(struct pipe_context *_pipe,
539 const struct pipe_viewport_state *viewport)
540 {
541 struct galahad_context *glhd_pipe = galahad_context(_pipe);
542 struct pipe_context *pipe = glhd_pipe->pipe;
543
544 pipe->set_viewport_state(pipe,
545 viewport);
546 }
547
548 static void
549 galahad_context_set_sampler_views(struct pipe_context *_pipe,
550 unsigned shader,
551 unsigned start,
552 unsigned num,
553 struct pipe_sampler_view **_views)
554 {
555 struct galahad_context *glhd_pipe = galahad_context(_pipe);
556 struct pipe_context *pipe = glhd_pipe->pipe;
557 struct pipe_sampler_view *unwrapped_views[PIPE_MAX_SAMPLERS];
558 struct pipe_sampler_view **views = NULL;
559 unsigned i;
560
561 if (_views) {
562 for (i = 0; i < num; i++)
563 unwrapped_views[i] = galahad_sampler_view_unwrap(_views[i]);
564 for (; i < PIPE_MAX_SAMPLERS; i++)
565 unwrapped_views[i] = NULL;
566
567 views = unwrapped_views;
568 }
569
570 switch (shader) {
571 case PIPE_SHADER_VERTEX:
572 pipe->set_vertex_sampler_views(pipe, num, views);
573 break;
574 case PIPE_SHADER_FRAGMENT:
575 pipe->set_fragment_sampler_views(pipe, num, views);
576 break;
577 case PIPE_SHADER_GEOMETRY:
578 pipe->set_geometry_sampler_views(pipe, num, views);
579 break;
580 default:
581 assert(0);
582 }
583 }
584
585 static void
586 galahad_context_set_vertex_sampler_views(struct pipe_context *_pipe,
587 unsigned num,
588 struct pipe_sampler_view **_views)
589 {
590 galahad_context_set_sampler_views(_pipe, PIPE_SHADER_VERTEX,
591 0, num, _views);
592 }
593
594 static void
595 galahad_context_set_fragment_sampler_views(struct pipe_context *_pipe,
596 unsigned num,
597 struct pipe_sampler_view **_views)
598 {
599 galahad_context_set_sampler_views(_pipe, PIPE_SHADER_FRAGMENT,
600 0, num, _views);
601 }
602
603 static void
604 galahad_context_set_geometry_sampler_views(struct pipe_context *_pipe,
605 unsigned num,
606 struct pipe_sampler_view **_views)
607 {
608 galahad_context_set_sampler_views(_pipe, PIPE_SHADER_GEOMETRY,
609 0, num, _views);
610 }
611
612 static void
613 galahad_context_set_vertex_buffers(struct pipe_context *_pipe,
614 unsigned start_slot, unsigned num_buffers,
615 const struct pipe_vertex_buffer *_buffers)
616 {
617 struct galahad_context *glhd_pipe = galahad_context(_pipe);
618 struct pipe_context *pipe = glhd_pipe->pipe;
619 struct pipe_vertex_buffer unwrapped_buffers[PIPE_MAX_SHADER_INPUTS];
620 struct pipe_vertex_buffer *buffers = NULL;
621 unsigned i;
622
623 if (num_buffers && _buffers) {
624 memcpy(unwrapped_buffers, _buffers, num_buffers * sizeof(*_buffers));
625 for (i = 0; i < num_buffers; i++)
626 unwrapped_buffers[i].buffer = galahad_resource_unwrap(_buffers[i].buffer);
627 buffers = unwrapped_buffers;
628 }
629
630 pipe->set_vertex_buffers(pipe,
631 start_slot, num_buffers,
632 buffers);
633 }
634
635 static void
636 galahad_context_set_index_buffer(struct pipe_context *_pipe,
637 const struct pipe_index_buffer *_ib)
638 {
639 struct galahad_context *glhd_pipe = galahad_context(_pipe);
640 struct pipe_context *pipe = glhd_pipe->pipe;
641 struct pipe_index_buffer unwrapped_ib, *ib = NULL;
642
643 if (_ib) {
644 if (_ib->buffer || _ib->user_buffer) {
645 switch (_ib->index_size) {
646 case 1:
647 case 2:
648 case 4:
649 break;
650 default:
651 glhd_warn("unrecognized index size %d", _ib->index_size);
652 break;
653 }
654 }
655 else if (_ib->offset || _ib->index_size) {
656 glhd_warn("non-indexed state with index offset %d and index size %d",
657 _ib->offset, _ib->index_size);
658 }
659
660 unwrapped_ib = *_ib;
661 unwrapped_ib.buffer = galahad_resource_unwrap(_ib->buffer);
662 ib = &unwrapped_ib;
663 }
664
665 pipe->set_index_buffer(pipe, ib);
666 }
667
668 static INLINE struct pipe_stream_output_target *
669 galahad_context_create_stream_output_target(struct pipe_context *_pipe,
670 struct pipe_resource *_res,
671 unsigned buffer_offset,
672 unsigned buffer_size)
673 {
674 struct galahad_context *glhd_pipe = galahad_context(_pipe);
675 struct galahad_resource *glhd_resource_res = galahad_resource(_res);
676 struct pipe_context *pipe = glhd_pipe->pipe;
677 struct pipe_resource *res = glhd_resource_res->resource;
678
679 return pipe->create_stream_output_target(pipe,
680 res, buffer_offset, buffer_size);
681 }
682
683 static INLINE void
684 galahad_context_stream_output_target_destroy(
685 struct pipe_context *_pipe,
686 struct pipe_stream_output_target *target)
687 {
688 struct galahad_context *glhd_pipe = galahad_context(_pipe);
689 struct pipe_context *pipe = glhd_pipe->pipe;
690
691 pipe->stream_output_target_destroy(pipe, target);
692 }
693
694 static INLINE void
695 galahad_context_set_stream_output_targets(struct pipe_context *_pipe,
696 unsigned num_targets,
697 struct pipe_stream_output_target **tgs,
698 unsigned append_bitmask)
699 {
700 struct galahad_context *glhd_pipe = galahad_context(_pipe);
701 struct pipe_context *pipe = glhd_pipe->pipe;
702
703 pipe->set_stream_output_targets(pipe, num_targets, tgs, append_bitmask);
704 }
705
706 static void
707 galahad_context_resource_copy_region(struct pipe_context *_pipe,
708 struct pipe_resource *_dst,
709 unsigned dst_level,
710 unsigned dstx,
711 unsigned dsty,
712 unsigned dstz,
713 struct pipe_resource *_src,
714 unsigned src_level,
715 const struct pipe_box *src_box)
716 {
717 struct galahad_context *glhd_pipe = galahad_context(_pipe);
718 struct galahad_resource *glhd_resource_dst = galahad_resource(_dst);
719 struct galahad_resource *glhd_resource_src = galahad_resource(_src);
720 struct pipe_context *pipe = glhd_pipe->pipe;
721 struct pipe_resource *dst = glhd_resource_dst->resource;
722 struct pipe_resource *src = glhd_resource_src->resource;
723
724 if (_dst->format != _src->format) {
725 const struct util_format_description *src_desc =
726 util_format_description(_src->format);
727 const struct util_format_description *dst_desc =
728 util_format_description(_dst->format);
729 if (!util_is_format_compatible(src_desc, dst_desc))
730 glhd_warn("Format mismatch: Source is %s, destination is %s",
731 src_desc->short_name,
732 dst_desc->short_name);
733 }
734
735 if ((_src->target == PIPE_BUFFER && _dst->target != PIPE_BUFFER) ||
736 (_src->target != PIPE_BUFFER && _dst->target == PIPE_BUFFER)) {
737 glhd_warn("Resource target mismatch: Source is %i, destination is %i",
738 _src->target, _dst->target);
739 }
740
741 pipe->resource_copy_region(pipe,
742 dst,
743 dst_level,
744 dstx,
745 dsty,
746 dstz,
747 src,
748 src_level,
749 src_box);
750 }
751
752 static void
753 galahad_context_blit(struct pipe_context *_pipe,
754 const struct pipe_blit_info *_info)
755 {
756 struct galahad_context *glhd_pipe = galahad_context(_pipe);
757 struct pipe_context *pipe = glhd_pipe->pipe;
758 struct pipe_blit_info info = *_info;
759
760 info.dst.resource = galahad_resource_unwrap(info.dst.resource);
761 info.src.resource = galahad_resource_unwrap(info.src.resource);
762
763 if (info.dst.box.width < 0 ||
764 info.dst.box.height < 0)
765 glhd_error("Destination dimensions are negative");
766
767 if (info.filter != PIPE_TEX_FILTER_NEAREST &&
768 info.src.resource->target != PIPE_TEXTURE_3D &&
769 info.dst.box.depth != info.src.box.depth)
770 glhd_error("Filtering in z-direction on non-3D texture");
771
772 if (util_format_is_depth_or_stencil(info.dst.format) !=
773 util_format_is_depth_or_stencil(info.src.format))
774 glhd_error("Invalid format conversion: %s <- %s\n",
775 util_format_name(info.dst.format),
776 util_format_name(info.src.format));
777
778 pipe->blit(pipe, &info);
779 }
780
781 static void
782 galahad_context_clear(struct pipe_context *_pipe,
783 unsigned buffers,
784 const union pipe_color_union *color,
785 double depth,
786 unsigned stencil)
787 {
788 struct galahad_context *glhd_pipe = galahad_context(_pipe);
789 struct pipe_context *pipe = glhd_pipe->pipe;
790
791 pipe->clear(pipe,
792 buffers,
793 color,
794 depth,
795 stencil);
796 }
797
798 static void
799 galahad_context_clear_render_target(struct pipe_context *_pipe,
800 struct pipe_surface *_dst,
801 const union pipe_color_union *color,
802 unsigned dstx, unsigned dsty,
803 unsigned width, unsigned height)
804 {
805 struct galahad_context *glhd_pipe = galahad_context(_pipe);
806 struct galahad_surface *glhd_surface_dst = galahad_surface(_dst);
807 struct pipe_context *pipe = glhd_pipe->pipe;
808 struct pipe_surface *dst = glhd_surface_dst->surface;
809
810 pipe->clear_render_target(pipe,
811 dst,
812 color,
813 dstx,
814 dsty,
815 width,
816 height);
817 }
818 static void
819 galahad_context_clear_depth_stencil(struct pipe_context *_pipe,
820 struct pipe_surface *_dst,
821 unsigned clear_flags,
822 double depth,
823 unsigned stencil,
824 unsigned dstx, unsigned dsty,
825 unsigned width, unsigned height)
826 {
827 struct galahad_context *glhd_pipe = galahad_context(_pipe);
828 struct galahad_surface *glhd_surface_dst = galahad_surface(_dst);
829 struct pipe_context *pipe = glhd_pipe->pipe;
830 struct pipe_surface *dst = glhd_surface_dst->surface;
831
832 pipe->clear_depth_stencil(pipe,
833 dst,
834 clear_flags,
835 depth,
836 stencil,
837 dstx,
838 dsty,
839 width,
840 height);
841
842 }
843
844 static void
845 galahad_context_flush(struct pipe_context *_pipe,
846 struct pipe_fence_handle **fence,
847 unsigned flags)
848 {
849 struct galahad_context *glhd_pipe = galahad_context(_pipe);
850 struct pipe_context *pipe = glhd_pipe->pipe;
851
852 pipe->flush(pipe, fence, flags);
853 }
854
855 static struct pipe_sampler_view *
856 galahad_context_create_sampler_view(struct pipe_context *_pipe,
857 struct pipe_resource *_resource,
858 const struct pipe_sampler_view *templ)
859 {
860 struct galahad_context *glhd_context = galahad_context(_pipe);
861 struct galahad_resource *glhd_resource = galahad_resource(_resource);
862 struct pipe_context *pipe = glhd_context->pipe;
863 struct pipe_resource *resource = glhd_resource->resource;
864 struct pipe_sampler_view *result;
865
866 result = pipe->create_sampler_view(pipe,
867 resource,
868 templ);
869
870 if (result)
871 return galahad_sampler_view_create(glhd_context, glhd_resource, result);
872 return NULL;
873 }
874
875 static void
876 galahad_context_sampler_view_destroy(struct pipe_context *_pipe,
877 struct pipe_sampler_view *_view)
878 {
879 galahad_sampler_view_destroy(galahad_context(_pipe),
880 galahad_sampler_view(_view));
881 }
882
883 static struct pipe_surface *
884 galahad_context_create_surface(struct pipe_context *_pipe,
885 struct pipe_resource *_resource,
886 const struct pipe_surface *templ)
887 {
888 struct galahad_context *glhd_context = galahad_context(_pipe);
889 struct galahad_resource *glhd_resource = galahad_resource(_resource);
890 struct pipe_context *pipe = glhd_context->pipe;
891 struct pipe_resource *resource = glhd_resource->resource;
892 struct pipe_surface *result;
893
894 result = pipe->create_surface(pipe,
895 resource,
896 templ);
897
898 if (result)
899 return galahad_surface_create(glhd_context, glhd_resource, result);
900 return NULL;
901 }
902
903 static void
904 galahad_context_surface_destroy(struct pipe_context *_pipe,
905 struct pipe_surface *_surface)
906 {
907 galahad_surface_destroy(galahad_context(_pipe),
908 galahad_surface(_surface));
909 }
910
911
912 static void *
913 galahad_context_transfer_map(struct pipe_context *_context,
914 struct pipe_resource *_resource,
915 unsigned level,
916 unsigned usage,
917 const struct pipe_box *box,
918 struct pipe_transfer **transfer)
919 {
920 struct galahad_context *glhd_context = galahad_context(_context);
921 struct galahad_resource *glhd_resource = galahad_resource(_resource);
922 struct pipe_context *context = glhd_context->pipe;
923 struct pipe_resource *resource = glhd_resource->resource;
924 struct pipe_transfer *result;
925 void *map;
926
927 map = context->transfer_map(context,
928 resource,
929 level,
930 usage,
931 box, &result);
932 if (!map)
933 return NULL;
934
935 glhd_resource->map_count++;
936
937 *transfer = galahad_transfer_create(glhd_context, glhd_resource, result);
938 return *transfer ? map : NULL;
939 }
940
941 static void
942 galahad_context_transfer_flush_region(struct pipe_context *_context,
943 struct pipe_transfer *_transfer,
944 const struct pipe_box *box)
945 {
946 struct galahad_context *glhd_context = galahad_context(_context);
947 struct galahad_transfer *glhd_transfer = galahad_transfer(_transfer);
948 struct pipe_context *context = glhd_context->pipe;
949 struct pipe_transfer *transfer = glhd_transfer->transfer;
950
951 context->transfer_flush_region(context,
952 transfer,
953 box);
954 }
955
956 static void
957 galahad_context_transfer_unmap(struct pipe_context *_context,
958 struct pipe_transfer *_transfer)
959 {
960 struct galahad_context *glhd_context = galahad_context(_context);
961 struct galahad_transfer *glhd_transfer = galahad_transfer(_transfer);
962 struct pipe_context *context = glhd_context->pipe;
963 struct pipe_transfer *transfer = glhd_transfer->transfer;
964 struct galahad_resource *glhd_resource = galahad_resource(_transfer->resource);
965
966 if (glhd_resource->map_count < 1) {
967 glhd_warn("context::transfer_unmap() called too many times"
968 " (count = %d)\n", glhd_resource->map_count);
969 }
970
971 glhd_resource->map_count--;
972
973 context->transfer_unmap(context,
974 transfer);
975
976 galahad_transfer_destroy(galahad_context(_context),
977 galahad_transfer(_transfer));
978 }
979
980
981 static void
982 galahad_context_transfer_inline_write(struct pipe_context *_context,
983 struct pipe_resource *_resource,
984 unsigned level,
985 unsigned usage,
986 const struct pipe_box *box,
987 const void *data,
988 unsigned stride,
989 unsigned slice_stride)
990 {
991 struct galahad_context *glhd_context = galahad_context(_context);
992 struct galahad_resource *glhd_resource = galahad_resource(_resource);
993 struct pipe_context *context = glhd_context->pipe;
994 struct pipe_resource *resource = glhd_resource->resource;
995
996 context->transfer_inline_write(context,
997 resource,
998 level,
999 usage,
1000 box,
1001 data,
1002 stride,
1003 slice_stride);
1004 }
1005
1006
1007 static void
1008 galahad_context_render_condition(struct pipe_context *_context,
1009 struct pipe_query *query,
1010 uint mode)
1011 {
1012 struct galahad_context *glhd_context = galahad_context(_context);
1013 struct pipe_context *context = glhd_context->pipe;
1014
1015 context->render_condition(context, query, mode);
1016 }
1017
1018
1019 struct pipe_context *
1020 galahad_context_create(struct pipe_screen *_screen, struct pipe_context *pipe)
1021 {
1022 struct galahad_context *glhd_pipe;
1023 (void)galahad_screen(_screen);
1024
1025 glhd_pipe = CALLOC_STRUCT(galahad_context);
1026 if (!glhd_pipe) {
1027 return NULL;
1028 }
1029
1030 glhd_pipe->base.screen = _screen;
1031 glhd_pipe->base.priv = pipe->priv; /* expose wrapped data */
1032 glhd_pipe->base.draw = NULL;
1033
1034 glhd_pipe->base.destroy = galahad_context_destroy;
1035
1036 #define GLHD_PIPE_INIT(_member) \
1037 glhd_pipe->base . _member = pipe -> _member ? galahad_context_ ## _member : NULL
1038
1039 GLHD_PIPE_INIT(draw_vbo);
1040 GLHD_PIPE_INIT(render_condition);
1041 GLHD_PIPE_INIT(create_query);
1042 GLHD_PIPE_INIT(destroy_query);
1043 GLHD_PIPE_INIT(begin_query);
1044 GLHD_PIPE_INIT(end_query);
1045 GLHD_PIPE_INIT(get_query_result);
1046 GLHD_PIPE_INIT(create_blend_state);
1047 GLHD_PIPE_INIT(bind_blend_state);
1048 GLHD_PIPE_INIT(delete_blend_state);
1049 GLHD_PIPE_INIT(create_sampler_state);
1050 GLHD_PIPE_INIT(bind_fragment_sampler_states);
1051 GLHD_PIPE_INIT(bind_vertex_sampler_states);
1052 GLHD_PIPE_INIT(bind_geometry_sampler_states);
1053 //GLHD_PIPE_INIT(bind_compute_sampler_states);
1054 GLHD_PIPE_INIT(delete_sampler_state);
1055 GLHD_PIPE_INIT(create_rasterizer_state);
1056 GLHD_PIPE_INIT(bind_rasterizer_state);
1057 GLHD_PIPE_INIT(delete_rasterizer_state);
1058 GLHD_PIPE_INIT(create_depth_stencil_alpha_state);
1059 GLHD_PIPE_INIT(bind_depth_stencil_alpha_state);
1060 GLHD_PIPE_INIT(delete_depth_stencil_alpha_state);
1061 GLHD_PIPE_INIT(create_fs_state);
1062 GLHD_PIPE_INIT(bind_fs_state);
1063 GLHD_PIPE_INIT(delete_fs_state);
1064 GLHD_PIPE_INIT(create_vs_state);
1065 GLHD_PIPE_INIT(bind_vs_state);
1066 GLHD_PIPE_INIT(delete_vs_state);
1067 GLHD_PIPE_INIT(create_gs_state);
1068 GLHD_PIPE_INIT(bind_gs_state);
1069 GLHD_PIPE_INIT(delete_gs_state);
1070 GLHD_PIPE_INIT(create_vertex_elements_state);
1071 GLHD_PIPE_INIT(bind_vertex_elements_state);
1072 GLHD_PIPE_INIT(delete_vertex_elements_state);
1073 GLHD_PIPE_INIT(set_blend_color);
1074 GLHD_PIPE_INIT(set_stencil_ref);
1075 GLHD_PIPE_INIT(set_sample_mask);
1076 GLHD_PIPE_INIT(set_clip_state);
1077 GLHD_PIPE_INIT(set_constant_buffer);
1078 GLHD_PIPE_INIT(set_framebuffer_state);
1079 GLHD_PIPE_INIT(set_polygon_stipple);
1080 GLHD_PIPE_INIT(set_scissor_state);
1081 GLHD_PIPE_INIT(set_viewport_state);
1082 GLHD_PIPE_INIT(set_fragment_sampler_views);
1083 GLHD_PIPE_INIT(set_vertex_sampler_views);
1084 GLHD_PIPE_INIT(set_geometry_sampler_views);
1085 //GLHD_PIPE_INIT(set_compute_sampler_views);
1086 //GLHD_PIPE_INIT(set_shader_resources);
1087 GLHD_PIPE_INIT(set_vertex_buffers);
1088 GLHD_PIPE_INIT(set_index_buffer);
1089 GLHD_PIPE_INIT(create_stream_output_target);
1090 GLHD_PIPE_INIT(stream_output_target_destroy);
1091 GLHD_PIPE_INIT(set_stream_output_targets);
1092 GLHD_PIPE_INIT(resource_copy_region);
1093 GLHD_PIPE_INIT(blit);
1094 GLHD_PIPE_INIT(clear);
1095 GLHD_PIPE_INIT(clear_render_target);
1096 GLHD_PIPE_INIT(clear_depth_stencil);
1097 GLHD_PIPE_INIT(flush);
1098 GLHD_PIPE_INIT(create_sampler_view);
1099 GLHD_PIPE_INIT(sampler_view_destroy);
1100 GLHD_PIPE_INIT(create_surface);
1101 GLHD_PIPE_INIT(surface_destroy);
1102 GLHD_PIPE_INIT(transfer_map);
1103 GLHD_PIPE_INIT(transfer_flush_region);
1104 GLHD_PIPE_INIT(transfer_unmap);
1105 GLHD_PIPE_INIT(transfer_inline_write);
1106 //GLHD_PIPE_INIT(texture_barrier);
1107 //GLHD_PIPE_INIT(create_video_decoder);
1108 //GLHD_PIPE_INIT(create_video_buffer);
1109 //GLHD_PIPE_INIT(create_compute_state);
1110 //GLHD_PIPE_INIT(bind_compute_state);
1111 //GLHD_PIPE_INIT(delete_compute_state);
1112 //GLHD_PIPE_INIT(set_compute_resources);
1113 //GLHD_PIPE_INIT(set_global_binding);
1114 //GLHD_PIPE_INIT(launch_grid);
1115
1116 #undef GLHD_PIPE_INIT
1117
1118 glhd_pipe->pipe = pipe;
1119
1120 return &glhd_pipe->base;
1121 }