1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vl_compositor.h"
30 #include <pipe/p_context.h>
31 #include <util/u_inlines.h>
32 #include <util/u_memory.h>
33 #include <util/u_keymap.h>
34 #include <util/u_draw.h>
35 #include <util/u_sampler.h>
36 #include <tgsi/tgsi_ureg.h>
39 struct vertex_shader_consts
41 struct vertex4f dst_scale
;
42 struct vertex4f dst_trans
;
43 struct vertex4f src_scale
;
44 struct vertex4f src_trans
;
47 struct fragment_shader_consts
53 u_video_rects_equal(struct pipe_video_rect
*a
, struct pipe_video_rect
*b
)
70 create_vert_shader(struct vl_compositor
*c
)
72 struct ureg_program
*shader
;
73 struct ureg_src vpos
, vtex
;
74 struct ureg_dst o_vpos
, o_vtex
;
76 shader
= ureg_create(TGSI_PROCESSOR_VERTEX
);
80 vpos
= ureg_DECL_vs_input(shader
, 0);
81 vtex
= ureg_DECL_vs_input(shader
, 1);
82 o_vpos
= ureg_DECL_output(shader
, TGSI_SEMANTIC_POSITION
, 0);
83 o_vtex
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, 1);
89 ureg_MOV(shader
, o_vpos
, vpos
);
90 ureg_MOV(shader
, o_vtex
, vtex
);
94 c
->vertex_shader
= ureg_create_shader_and_destroy(shader
, c
->pipe
);
95 if (!c
->vertex_shader
)
102 create_frag_shader_ycbcr_2_rgb(struct vl_compositor
*c
)
104 struct ureg_program
*shader
;
106 struct ureg_src csc
[4];
107 struct ureg_src sampler
;
108 struct ureg_dst texel
;
109 struct ureg_dst fragment
;
112 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
116 tc
= ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, 1, TGSI_INTERPOLATE_LINEAR
);
117 for (i
= 0; i
< 4; ++i
)
118 csc
[i
] = ureg_DECL_constant(shader
, i
);
119 sampler
= ureg_DECL_sampler(shader
, 0);
120 texel
= ureg_DECL_temporary(shader
);
121 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
124 * texel = tex(tc, sampler)
125 * fragment = csc * texel
127 ureg_TEX(shader
, texel
, TGSI_TEXTURE_2D
, tc
, sampler
);
128 for (i
= 0; i
< 4; ++i
)
129 ureg_DP4(shader
, ureg_writemask(fragment
, TGSI_WRITEMASK_X
<< i
), csc
[i
], ureg_src(texel
));
131 ureg_release_temporary(shader
, texel
);
134 c
->fragment_shader
.ycbcr_2_rgb
= ureg_create_shader_and_destroy(shader
, c
->pipe
);
135 if (!c
->fragment_shader
.ycbcr_2_rgb
)
142 create_frag_shader_rgb_2_rgb(struct vl_compositor
*c
)
144 struct ureg_program
*shader
;
146 struct ureg_src sampler
;
147 struct ureg_dst fragment
;
149 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
153 tc
= ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, 1, TGSI_INTERPOLATE_LINEAR
);
154 sampler
= ureg_DECL_sampler(shader
, 0);
155 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
158 * fragment = tex(tc, sampler)
160 ureg_TEX(shader
, fragment
, TGSI_TEXTURE_2D
, tc
, sampler
);
163 c
->fragment_shader
.rgb_2_rgb
= ureg_create_shader_and_destroy(shader
, c
->pipe
);
164 if (!c
->fragment_shader
.rgb_2_rgb
)
171 init_pipe_state(struct vl_compositor
*c
)
173 struct pipe_sampler_state sampler
;
177 c
->fb_state
.nr_cbufs
= 1;
178 c
->fb_state
.zsbuf
= NULL
;
180 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
181 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
182 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
183 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
184 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
185 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
186 sampler
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
187 sampler
.compare_func
= PIPE_FUNC_ALWAYS
;
188 sampler
.normalized_coords
= 1;
189 /*sampler.lod_bias = ;*/
190 /*sampler.min_lod = ;*/
191 /*sampler.max_lod = ;*/
192 /*sampler.border_color[i] = ;*/
193 /*sampler.max_anisotropy = ;*/
194 c
->sampler
= c
->pipe
->create_sampler_state(c
->pipe
, &sampler
);
199 static void cleanup_pipe_state(struct vl_compositor
*c
)
203 c
->pipe
->delete_sampler_state(c
->pipe
, c
->sampler
);
207 init_shaders(struct vl_compositor
*c
)
211 if (!create_vert_shader(c
)) {
212 debug_printf("Unable to create vertex shader.\n");
215 if (!create_frag_shader_ycbcr_2_rgb(c
)) {
216 debug_printf("Unable to create YCbCr-to-RGB fragment shader.\n");
219 if (!create_frag_shader_rgb_2_rgb(c
)) {
220 debug_printf("Unable to create RGB-to-RGB fragment shader.\n");
227 static void cleanup_shaders(struct vl_compositor
*c
)
231 c
->pipe
->delete_vs_state(c
->pipe
, c
->vertex_shader
);
232 c
->pipe
->delete_fs_state(c
->pipe
, c
->fragment_shader
.ycbcr_2_rgb
);
233 c
->pipe
->delete_fs_state(c
->pipe
, c
->fragment_shader
.rgb_2_rgb
);
237 init_buffers(struct vl_compositor
*c
)
239 struct fragment_shader_consts fsc
;
240 struct pipe_vertex_element vertex_elems
[2];
245 * Create our vertex buffer and vertex buffer elements
247 c
->vertex_buf
.stride
= sizeof(struct vertex4f
);
248 c
->vertex_buf
.max_index
= (VL_COMPOSITOR_MAX_LAYERS
+ 2) * 6 - 1;
249 c
->vertex_buf
.buffer_offset
= 0;
250 /* XXX: Create with DYNAMIC or STREAM */
251 c
->vertex_buf
.buffer
= pipe_buffer_create
254 PIPE_BIND_VERTEX_BUFFER
,
255 sizeof(struct vertex4f
) * (VL_COMPOSITOR_MAX_LAYERS
+ 2) * 6
258 vertex_elems
[0].src_offset
= 0;
259 vertex_elems
[0].instance_divisor
= 0;
260 vertex_elems
[0].vertex_buffer_index
= 0;
261 vertex_elems
[0].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
262 vertex_elems
[1].src_offset
= sizeof(struct vertex2f
);
263 vertex_elems
[1].instance_divisor
= 0;
264 vertex_elems
[1].vertex_buffer_index
= 0;
265 vertex_elems
[1].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
266 c
->vertex_elems_state
= c
->pipe
->create_vertex_elements_state(c
->pipe
, 2, vertex_elems
);
269 * Create our fragment shader's constant buffer
270 * Const buffer contains the color conversion matrix and bias vectors
272 /* XXX: Create with IMMUTABLE/STATIC... although it does change every once in a long while... */
273 c
->fs_const_buf
= pipe_buffer_create
276 PIPE_BIND_CONSTANT_BUFFER
,
277 sizeof(struct fragment_shader_consts
)
280 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY
, NULL
, true, fsc
.matrix
);
282 vl_compositor_set_csc_matrix(c
, fsc
.matrix
);
288 cleanup_buffers(struct vl_compositor
*c
)
292 c
->pipe
->delete_vertex_elements_state(c
->pipe
, c
->vertex_elems_state
);
293 pipe_resource_reference(&c
->vertex_buf
.buffer
, NULL
);
294 pipe_resource_reference(&c
->fs_const_buf
, NULL
);
298 texview_map_delete(const struct keymap
*map
,
299 const void *key
, void *data
,
302 struct pipe_sampler_view
*sv
= (struct pipe_sampler_view
*)data
;
309 pipe_sampler_view_reference(&sv
, NULL
);
312 bool vl_compositor_init(struct vl_compositor
*compositor
, struct pipe_context
*pipe
)
318 memset(compositor
, 0, sizeof(struct vl_compositor
));
320 compositor
->pipe
= pipe
;
322 compositor
->texview_map
= util_new_keymap(sizeof(struct pipe_surface
*), -1,
324 if (!compositor
->texview_map
)
327 if (!init_pipe_state(compositor
)) {
328 util_delete_keymap(compositor
->texview_map
, compositor
->pipe
);
331 if (!init_shaders(compositor
)) {
332 util_delete_keymap(compositor
->texview_map
, compositor
->pipe
);
333 cleanup_pipe_state(compositor
);
336 if (!init_buffers(compositor
)) {
337 util_delete_keymap(compositor
->texview_map
, compositor
->pipe
);
338 cleanup_shaders(compositor
);
339 cleanup_pipe_state(compositor
);
343 compositor
->fb_state
.width
= 0;
344 compositor
->fb_state
.height
= 0;
345 compositor
->bg
= NULL
;
346 compositor
->dirty_bg
= false;
347 for (i
= 0; i
< VL_COMPOSITOR_MAX_LAYERS
; ++i
)
348 compositor
->layers
[i
] = NULL
;
349 compositor
->dirty_layers
= 0;
354 void vl_compositor_cleanup(struct vl_compositor
*compositor
)
358 util_delete_keymap(compositor
->texview_map
, compositor
->pipe
);
359 cleanup_buffers(compositor
);
360 cleanup_shaders(compositor
);
361 cleanup_pipe_state(compositor
);
364 void vl_compositor_set_background(struct vl_compositor
*compositor
,
365 struct pipe_surface
*bg
, struct pipe_video_rect
*bg_src_rect
)
368 assert((bg
&& bg_src_rect
) || (!bg
&& !bg_src_rect
));
370 if (compositor
->bg
!= bg
||
371 !u_video_rects_equal(&compositor
->bg_src_rect
, bg_src_rect
)) {
372 pipe_surface_reference(&compositor
->bg
, bg
);
373 /*if (!u_video_rects_equal(&compositor->bg_src_rect, bg_src_rect))*/
374 compositor
->bg_src_rect
= *bg_src_rect
;
375 compositor
->dirty_bg
= true;
379 void vl_compositor_set_layers(struct vl_compositor
*compositor
,
380 struct pipe_surface
*layers
[],
381 struct pipe_video_rect
*src_rects
[],
382 struct pipe_video_rect
*dst_rects
[],
388 assert(num_layers
<= VL_COMPOSITOR_MAX_LAYERS
);
390 for (i
= 0; i
< num_layers
; ++i
)
392 assert((layers
[i
] && src_rects
[i
] && dst_rects
[i
]) ||
393 (!layers
[i
] && !src_rects
[i
] && !dst_rects
[i
]));
395 if (compositor
->layers
[i
] != layers
[i
] ||
396 !u_video_rects_equal(&compositor
->layer_src_rects
[i
], src_rects
[i
]) ||
397 !u_video_rects_equal(&compositor
->layer_dst_rects
[i
], dst_rects
[i
]))
399 pipe_surface_reference(&compositor
->layers
[i
], layers
[i
]);
400 /*if (!u_video_rects_equal(&compositor->layer_src_rects[i], src_rects[i]))*/
401 compositor
->layer_src_rects
[i
] = *src_rects
[i
];
402 /*if (!u_video_rects_equal(&compositor->layer_dst_rects[i], dst_rects[i]))*/
403 compositor
->layer_dst_rects
[i
] = *dst_rects
[i
];
404 compositor
->dirty_layers
|= 1 << i
;
408 compositor
->dirty_layers
|= 1 << i
;
411 for (; i
< VL_COMPOSITOR_MAX_LAYERS
; ++i
)
412 pipe_surface_reference(&compositor
->layers
[i
], NULL
);
415 static void gen_rect_verts(unsigned pos
,
416 struct pipe_video_rect
*src_rect
,
417 struct vertex2f
*src_inv_size
,
418 struct pipe_video_rect
*dst_rect
,
419 struct vertex2f
*dst_inv_size
,
422 assert(pos
< VL_COMPOSITOR_MAX_LAYERS
+ 2);
424 assert(src_inv_size
);
425 assert((dst_rect
&& dst_inv_size
) /*|| (!dst_rect && !dst_inv_size)*/);
428 vb
[pos
* 6 + 0].x
= dst_rect
->x
* dst_inv_size
->x
;
429 vb
[pos
* 6 + 0].y
= dst_rect
->y
* dst_inv_size
->y
;
430 vb
[pos
* 6 + 0].z
= src_rect
->x
* src_inv_size
->x
;
431 vb
[pos
* 6 + 0].w
= src_rect
->y
* src_inv_size
->y
;
433 vb
[pos
* 6 + 1].x
= dst_rect
->x
* dst_inv_size
->x
;
434 vb
[pos
* 6 + 1].y
= (dst_rect
->y
+ dst_rect
->h
) * dst_inv_size
->y
;
435 vb
[pos
* 6 + 1].z
= src_rect
->x
* src_inv_size
->x
;
436 vb
[pos
* 6 + 1].w
= (src_rect
->y
+ src_rect
->h
) * src_inv_size
->y
;
438 vb
[pos
* 6 + 2].x
= (dst_rect
->x
+ dst_rect
->w
) * dst_inv_size
->x
;
439 vb
[pos
* 6 + 2].y
= dst_rect
->y
* dst_inv_size
->y
;
440 vb
[pos
* 6 + 2].z
= (src_rect
->x
+ src_rect
->w
) * src_inv_size
->x
;
441 vb
[pos
* 6 + 2].w
= src_rect
->y
* src_inv_size
->y
;
443 vb
[pos
* 6 + 3].x
= (dst_rect
->x
+ dst_rect
->w
) * dst_inv_size
->x
;
444 vb
[pos
* 6 + 3].y
= dst_rect
->y
* dst_inv_size
->y
;
445 vb
[pos
* 6 + 3].z
= (src_rect
->x
+ src_rect
->w
) * src_inv_size
->x
;
446 vb
[pos
* 6 + 3].w
= src_rect
->y
* src_inv_size
->y
;
448 vb
[pos
* 6 + 4].x
= dst_rect
->x
* dst_inv_size
->x
;
449 vb
[pos
* 6 + 4].y
= (dst_rect
->y
+ dst_rect
->h
) * dst_inv_size
->y
;
450 vb
[pos
* 6 + 4].z
= src_rect
->x
* src_inv_size
->x
;
451 vb
[pos
* 6 + 4].w
= (src_rect
->y
+ src_rect
->h
) * src_inv_size
->y
;
453 vb
[pos
* 6 + 5].x
= (dst_rect
->x
+ dst_rect
->w
) * dst_inv_size
->x
;
454 vb
[pos
* 6 + 5].y
= (dst_rect
->y
+ dst_rect
->h
) * dst_inv_size
->y
;
455 vb
[pos
* 6 + 5].z
= (src_rect
->x
+ src_rect
->w
) * src_inv_size
->x
;
456 vb
[pos
* 6 + 5].w
= (src_rect
->y
+ src_rect
->h
) * src_inv_size
->y
;
459 static unsigned gen_data(struct vl_compositor
*c
,
460 struct pipe_surface
*src_surface
,
461 struct pipe_video_rect
*src_rect
,
462 struct pipe_video_rect
*dst_rect
,
463 struct pipe_surface
**textures
,
467 struct pipe_transfer
*buf_transfer
;
468 unsigned num_rects
= 0;
477 vb
= pipe_buffer_map(c
->pipe
, c
->vertex_buf
.buffer
,
478 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
485 struct vertex2f bg_inv_size
= {1.0f
/ c
->bg
->width
, 1.0f
/ c
->bg
->height
};
486 gen_rect_verts(num_rects
, &c
->bg_src_rect
, &bg_inv_size
, NULL
, NULL
, vb
);
487 textures
[num_rects
] = c
->bg
;
489 frag_shaders
[num_rects
] = c
->fragment_shader
.rgb_2_rgb
;
495 struct vertex2f src_inv_size
= { 1.0f
/ src_surface
->width
, 1.0f
/ src_surface
->height
};
496 gen_rect_verts(num_rects
, src_rect
, &src_inv_size
, dst_rect
, &c
->fb_inv_size
, vb
);
497 textures
[num_rects
] = src_surface
;
498 /* XXX: Hack, sort of */
499 frag_shaders
[num_rects
] = c
->fragment_shader
.ycbcr_2_rgb
;
503 for (i
= 0; c
->dirty_layers
> 0; i
++) {
504 assert(i
< VL_COMPOSITOR_MAX_LAYERS
);
506 if (c
->dirty_layers
& (1 << i
)) {
507 struct vertex2f layer_inv_size
= {1.0f
/ c
->layers
[i
]->width
, 1.0f
/ c
->layers
[i
]->height
};
508 gen_rect_verts(num_rects
, &c
->layer_src_rects
[i
], &layer_inv_size
,
509 &c
->layer_dst_rects
[i
], &c
->fb_inv_size
, vb
);
510 textures
[num_rects
] = c
->layers
[i
];
512 frag_shaders
[num_rects
] = c
->fragment_shader
.rgb_2_rgb
;
514 c
->dirty_layers
&= ~(1 << i
);
518 pipe_buffer_unmap(c
->pipe
, c
->vertex_buf
.buffer
, buf_transfer
);
523 static void draw_layers(struct vl_compositor
*c
,
524 struct pipe_surface
*src_surface
,
525 struct pipe_video_rect
*src_rect
,
526 struct pipe_video_rect
*dst_rect
)
529 struct pipe_surface
*src_surfaces
[VL_COMPOSITOR_MAX_LAYERS
+ 2];
530 void *frag_shaders
[VL_COMPOSITOR_MAX_LAYERS
+ 2];
538 num_rects
= gen_data(c
, src_surface
, src_rect
, dst_rect
, src_surfaces
, frag_shaders
);
540 for (i
= 0; i
< num_rects
; ++i
) {
541 boolean delete_view
= FALSE
;
542 struct pipe_sampler_view
*surface_view
= (struct pipe_sampler_view
*)util_keymap_lookup(c
->texview_map
,
545 struct pipe_sampler_view templat
;
546 u_sampler_view_default_template(&templat
, src_surfaces
[i
]->texture
,
547 src_surfaces
[i
]->texture
->format
);
548 surface_view
= c
->pipe
->create_sampler_view(c
->pipe
, src_surfaces
[i
]->texture
,
553 delete_view
= !util_keymap_insert(c
->texview_map
, &src_surfaces
[i
],
554 surface_view
, c
->pipe
);
557 c
->pipe
->bind_fs_state(c
->pipe
, frag_shaders
[i
]);
558 c
->pipe
->set_fragment_sampler_views(c
->pipe
, 1, &surface_view
);
561 util_draw_arrays(c
->pipe
,PIPE_PRIM_TRIANGLES
,i
* 6,6);
564 pipe_sampler_view_reference(&surface_view
, NULL
);
569 void vl_compositor_render(struct vl_compositor
*compositor
,
570 struct pipe_surface
*src_surface
,
571 enum pipe_mpeg12_picture_type picture_type
,
572 /*unsigned num_past_surfaces,
573 struct pipe_surface *past_surfaces,
574 unsigned num_future_surfaces,
575 struct pipe_surface *future_surfaces,*/
576 struct pipe_video_rect
*src_area
,
577 struct pipe_surface
*dst_surface
,
578 struct pipe_video_rect
*dst_area
,
579 struct pipe_fence_handle
**fence
)
586 assert(picture_type
== PIPE_MPEG12_PICTURE_TYPE_FRAME
);
588 if (compositor
->fb_state
.width
!= dst_surface
->width
) {
589 compositor
->fb_inv_size
.x
= 1.0f
/ dst_surface
->width
;
590 compositor
->fb_state
.width
= dst_surface
->width
;
592 if (compositor
->fb_state
.height
!= dst_surface
->height
) {
593 compositor
->fb_inv_size
.y
= 1.0f
/ dst_surface
->height
;
594 compositor
->fb_state
.height
= dst_surface
->height
;
597 compositor
->fb_state
.cbufs
[0] = dst_surface
;
599 compositor
->viewport
.scale
[0] = compositor
->fb_state
.width
;
600 compositor
->viewport
.scale
[1] = compositor
->fb_state
.height
;
601 compositor
->viewport
.scale
[2] = 1;
602 compositor
->viewport
.scale
[3] = 1;
603 compositor
->viewport
.translate
[0] = 0;
604 compositor
->viewport
.translate
[1] = 0;
605 compositor
->viewport
.translate
[2] = 0;
606 compositor
->viewport
.translate
[3] = 0;
608 compositor
->pipe
->set_framebuffer_state(compositor
->pipe
, &compositor
->fb_state
);
609 compositor
->pipe
->set_viewport_state(compositor
->pipe
, &compositor
->viewport
);
610 compositor
->pipe
->bind_fragment_sampler_states(compositor
->pipe
, 1, &compositor
->sampler
);
611 compositor
->pipe
->bind_vs_state(compositor
->pipe
, compositor
->vertex_shader
);
612 compositor
->pipe
->set_vertex_buffers(compositor
->pipe
, 1, &compositor
->vertex_buf
);
613 compositor
->pipe
->bind_vertex_elements_state(compositor
->pipe
, compositor
->vertex_elems_state
);
614 compositor
->pipe
->set_constant_buffer(compositor
->pipe
, PIPE_SHADER_FRAGMENT
, 0, compositor
->fs_const_buf
);
616 draw_layers(compositor
, src_surface
, src_area
, dst_area
);
618 assert(!compositor
->dirty_bg
&& !compositor
->dirty_layers
);
619 compositor
->pipe
->flush(compositor
->pipe
, PIPE_FLUSH_RENDER_CACHE
, fence
);
622 void vl_compositor_set_csc_matrix(struct vl_compositor
*compositor
, const float *mat
)
624 struct pipe_transfer
*buf_transfer
;
630 pipe_buffer_map(compositor
->pipe
, compositor
->fs_const_buf
,
631 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
634 sizeof(struct fragment_shader_consts
)
637 pipe_buffer_unmap(compositor
->pipe
, compositor
->fs_const_buf
,