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