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 <pipe/p_inlines.h>
32 #include <tgsi/tgsi_parse.h>
33 #include <tgsi/tgsi_build.h>
34 #include <util/u_memory.h>
36 #include "vl_shader_build.h"
48 struct vertex_shader_consts
50 struct vertex4f dst_scale
;
51 struct vertex4f dst_trans
;
52 struct vertex4f src_scale
;
53 struct vertex4f src_trans
;
56 struct fragment_shader_consts
62 * Represents 2 triangles in a strip in normalized coords.
63 * Used to render the surface onto the frame buffer.
65 static const struct vertex2f surface_verts
[4] =
74 * Represents texcoords for the above. We can use the position values directly.
75 * TODO: Duplicate these in the shader, no need to create a buffer.
77 static const struct vertex2f
*surface_texcoords
= surface_verts
;
80 create_vert_shader(struct vl_compositor
*c
)
82 const unsigned max_tokens
= 50;
84 struct pipe_shader_state vs
;
85 struct tgsi_token
*tokens
;
86 struct tgsi_header
*header
;
88 struct tgsi_full_declaration decl
;
89 struct tgsi_full_instruction inst
;
97 tokens
= (struct tgsi_token
*)MALLOC(max_tokens
* sizeof(struct tgsi_token
));
98 *(struct tgsi_version
*)&tokens
[0] = tgsi_build_version();
99 header
= (struct tgsi_header
*)&tokens
[1];
100 *header
= tgsi_build_header();
101 *(struct tgsi_processor
*)&tokens
[2] = tgsi_build_processor(TGSI_PROCESSOR_VERTEX
, header
);
106 * decl i0 ; Vertex pos
107 * decl i1 ; Vertex texcoords
109 for (i
= 0; i
< 2; i
++) {
110 decl
= vl_decl_input(i
== 0 ? TGSI_SEMANTIC_POSITION
: TGSI_SEMANTIC_GENERIC
, i
, i
, i
);
111 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
115 * decl c0 ; Scaling vector to scale vertex pos rect to destination size
116 * decl c1 ; Translation vector to move vertex pos rect into position
117 * decl c2 ; Scaling vector to scale texcoord rect to source size
118 * decl c3 ; Translation vector to move texcoord rect into position
120 decl
= vl_decl_constants(TGSI_SEMANTIC_GENERIC
, 0, 0, 3);
121 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
124 * decl o0 ; Vertex pos
125 * decl o1 ; Vertex texcoords
127 for (i
= 0; i
< 2; i
++) {
128 decl
= vl_decl_output(i
== 0 ? TGSI_SEMANTIC_POSITION
: TGSI_SEMANTIC_GENERIC
, i
, i
, i
);
129 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
133 decl
= vl_decl_temps(0, 1);
134 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
137 * mad o0, i0, c0, c1 ; Scale and translate unit output rect to destination size and pos
138 * mad o1, i1, c2, c3 ; Scale and translate unit texcoord rect to source size and pos
140 for (i
= 0; i
< 2; ++i
) {
141 inst
= vl_inst4(TGSI_OPCODE_MAD
, TGSI_FILE_OUTPUT
, i
, TGSI_FILE_INPUT
, i
, TGSI_FILE_CONSTANT
, i
* 2, TGSI_FILE_CONSTANT
, i
* 2 + 1);
142 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
147 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
149 assert(ti
<= max_tokens
);
152 c
->vertex_shader
= c
->pipe
->create_vs_state(c
->pipe
, &vs
);
157 create_frag_shader(struct vl_compositor
*c
)
159 const unsigned max_tokens
= 50;
161 struct pipe_shader_state fs
;
162 struct tgsi_token
*tokens
;
163 struct tgsi_header
*header
;
165 struct tgsi_full_declaration decl
;
166 struct tgsi_full_instruction inst
;
174 tokens
= (struct tgsi_token
*)MALLOC(max_tokens
* sizeof(struct tgsi_token
));
175 *(struct tgsi_version
*)&tokens
[0] = tgsi_build_version();
176 header
= (struct tgsi_header
*)&tokens
[1];
177 *header
= tgsi_build_header();
178 *(struct tgsi_processor
*)&tokens
[2] = tgsi_build_processor(TGSI_PROCESSOR_FRAGMENT
, header
);
182 /* decl i0 ; Texcoords for s0 */
183 decl
= vl_decl_interpolated_input(TGSI_SEMANTIC_GENERIC
, 1, 0, 0, TGSI_INTERPOLATE_LINEAR
);
184 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
187 * decl c0-c3 ; CSC matrix c0-c3
189 decl
= vl_decl_constants(TGSI_SEMANTIC_GENERIC
, 0, 0, 3);
190 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
192 /* decl o0 ; Fragment color */
193 decl
= vl_decl_output(TGSI_SEMANTIC_COLOR
, 0, 0, 0);
194 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
197 decl
= vl_decl_temps(0, 0);
198 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
200 /* decl s0 ; Sampler for tex containing picture to display */
201 decl
= vl_decl_samplers(0, 0);
202 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
204 /* tex2d t0, i0, s0 ; Read src pixel */
205 inst
= vl_tex(TGSI_TEXTURE_2D
, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_INPUT
, 0, TGSI_FILE_SAMPLER
, 0);
206 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
209 * dp4 o0.x, t0, c0 ; Multiply pixel by the color conversion matrix
214 for (i
= 0; i
< 4; ++i
) {
215 inst
= vl_inst3(TGSI_OPCODE_DP4
, TGSI_FILE_OUTPUT
, 0, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_CONSTANT
, i
);
216 inst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
<< i
;
217 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
222 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
224 assert(ti
<= max_tokens
);
227 c
->fragment_shader
= c
->pipe
->create_fs_state(c
->pipe
, &fs
);
232 init_pipe_state(struct vl_compositor
*c
)
234 struct pipe_sampler_state sampler
;
238 c
->fb_state
.nr_cbufs
= 1;
239 c
->fb_state
.zsbuf
= NULL
;
241 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
242 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
243 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
244 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
245 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
246 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
247 sampler
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
248 sampler
.compare_func
= PIPE_FUNC_ALWAYS
;
249 sampler
.normalized_coords
= 1;
250 /*sampler.prefilter = ;*/
251 /*sampler.lod_bias = ;*/
252 /*sampler.min_lod = ;*/
253 /*sampler.max_lod = ;*/
254 /*sampler.border_color[i] = ;*/
255 /*sampler.max_anisotropy = ;*/
256 c
->sampler
= c
->pipe
->create_sampler_state(c
->pipe
, &sampler
);
261 static void cleanup_pipe_state(struct vl_compositor
*c
)
265 c
->pipe
->delete_sampler_state(c
->pipe
, c
->sampler
);
269 init_shaders(struct vl_compositor
*c
)
273 create_vert_shader(c
);
274 create_frag_shader(c
);
279 static void cleanup_shaders(struct vl_compositor
*c
)
283 c
->pipe
->delete_vs_state(c
->pipe
, c
->vertex_shader
);
284 c
->pipe
->delete_fs_state(c
->pipe
, c
->fragment_shader
);
288 init_buffers(struct vl_compositor
*c
)
290 struct fragment_shader_consts fsc
;
295 * Create our vertex buffer and vertex buffer element
296 * VB contains 4 vertices that render a quad covering the entire window
297 * to display a rendered surface
298 * Quad is rendered as a tri strip
300 c
->vertex_bufs
[0].stride
= sizeof(struct vertex2f
);
301 c
->vertex_bufs
[0].max_index
= 3;
302 c
->vertex_bufs
[0].buffer_offset
= 0;
303 c
->vertex_bufs
[0].buffer
= pipe_buffer_create
307 PIPE_BUFFER_USAGE_VERTEX
,
308 sizeof(struct vertex2f
) * 4
313 pipe_buffer_map(c
->pipe
->screen
, c
->vertex_bufs
[0].buffer
, PIPE_BUFFER_USAGE_CPU_WRITE
),
315 sizeof(struct vertex2f
) * 4
318 pipe_buffer_unmap(c
->pipe
->screen
, c
->vertex_bufs
[0].buffer
);
320 c
->vertex_elems
[0].src_offset
= 0;
321 c
->vertex_elems
[0].vertex_buffer_index
= 0;
322 c
->vertex_elems
[0].nr_components
= 2;
323 c
->vertex_elems
[0].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
326 * Create our texcoord buffer and texcoord buffer element
327 * Texcoord buffer contains the TCs for mapping the rendered surface to the 4 vertices
329 c
->vertex_bufs
[1].stride
= sizeof(struct vertex2f
);
330 c
->vertex_bufs
[1].max_index
= 3;
331 c
->vertex_bufs
[1].buffer_offset
= 0;
332 c
->vertex_bufs
[1].buffer
= pipe_buffer_create
336 PIPE_BUFFER_USAGE_VERTEX
,
337 sizeof(struct vertex2f
) * 4
342 pipe_buffer_map(c
->pipe
->screen
, c
->vertex_bufs
[1].buffer
, PIPE_BUFFER_USAGE_CPU_WRITE
),
344 sizeof(struct vertex2f
) * 4
347 pipe_buffer_unmap(c
->pipe
->screen
, c
->vertex_bufs
[1].buffer
);
349 c
->vertex_elems
[1].src_offset
= 0;
350 c
->vertex_elems
[1].vertex_buffer_index
= 1;
351 c
->vertex_elems
[1].nr_components
= 2;
352 c
->vertex_elems
[1].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
355 * Create our vertex shader's constant buffer
356 * Const buffer contains scaling and translation vectors
358 c
->vs_const_buf
.buffer
= pipe_buffer_create
362 PIPE_BUFFER_USAGE_CONSTANT
| PIPE_BUFFER_USAGE_DISCARD
,
363 sizeof(struct vertex_shader_consts
)
367 * Create our fragment shader's constant buffer
368 * Const buffer contains the color conversion matrix and bias vectors
370 c
->fs_const_buf
.buffer
= pipe_buffer_create
374 PIPE_BUFFER_USAGE_CONSTANT
,
375 sizeof(struct fragment_shader_consts
)
378 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY
, NULL
, true, fsc
.matrix
);
380 vl_compositor_set_csc_matrix(c
, fsc
.matrix
);
386 cleanup_buffers(struct vl_compositor
*c
)
392 for (i
= 0; i
< 2; ++i
)
393 pipe_buffer_reference(&c
->vertex_bufs
[i
].buffer
, NULL
);
395 pipe_buffer_reference(&c
->vs_const_buf
.buffer
, NULL
);
396 pipe_buffer_reference(&c
->fs_const_buf
.buffer
, NULL
);
399 bool vl_compositor_init(struct vl_compositor
*compositor
, struct pipe_context
*pipe
)
403 memset(compositor
, 0, sizeof(struct vl_compositor
));
405 compositor
->pipe
= pipe
;
407 if (!init_pipe_state(compositor
))
409 if (!init_shaders(compositor
)) {
410 cleanup_pipe_state(compositor
);
413 if (!init_buffers(compositor
)) {
414 cleanup_shaders(compositor
);
415 cleanup_pipe_state(compositor
);
422 void vl_compositor_cleanup(struct vl_compositor
*compositor
)
426 cleanup_buffers(compositor
);
427 cleanup_shaders(compositor
);
428 cleanup_pipe_state(compositor
);
431 void vl_compositor_render(struct vl_compositor
*compositor
,
432 /*struct pipe_texture *backround,
433 struct pipe_video_rect *backround_area,*/
434 struct pipe_texture
*src_surface
,
435 enum pipe_mpeg12_picture_type picture_type
,
436 /*unsigned num_past_surfaces,
437 struct pipe_texture *past_surfaces,
438 unsigned num_future_surfaces,
439 struct pipe_texture *future_surfaces,*/
440 struct pipe_video_rect
*src_area
,
441 struct pipe_texture
*dst_surface
,
442 struct pipe_video_rect
*dst_area
,
443 /*unsigned num_layers,
444 struct pipe_texture *layers,
445 struct pipe_video_rect *layer_src_areas,
446 struct pipe_video_rect *layer_dst_areas*/
447 struct pipe_fence_handle
**fence
)
449 struct vertex_shader_consts
*vs_consts
;
456 assert(picture_type
== PIPE_MPEG12_PICTURE_TYPE_FRAME
);
458 compositor
->fb_state
.width
= dst_surface
->width0
;
459 compositor
->fb_state
.height
= dst_surface
->height0
;
460 compositor
->fb_state
.cbufs
[0] = compositor
->pipe
->screen
->get_tex_surface
462 compositor
->pipe
->screen
,
464 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ
| PIPE_BUFFER_USAGE_GPU_WRITE
467 compositor
->viewport
.scale
[0] = compositor
->fb_state
.width
;
468 compositor
->viewport
.scale
[1] = compositor
->fb_state
.height
;
469 compositor
->viewport
.scale
[2] = 1;
470 compositor
->viewport
.scale
[3] = 1;
471 compositor
->viewport
.translate
[0] = 0;
472 compositor
->viewport
.translate
[1] = 0;
473 compositor
->viewport
.translate
[2] = 0;
474 compositor
->viewport
.translate
[3] = 0;
476 compositor
->scissor
.maxx
= compositor
->fb_state
.width
;
477 compositor
->scissor
.maxy
= compositor
->fb_state
.height
;
479 compositor
->pipe
->set_framebuffer_state(compositor
->pipe
, &compositor
->fb_state
);
480 compositor
->pipe
->set_viewport_state(compositor
->pipe
, &compositor
->viewport
);
481 compositor
->pipe
->set_scissor_state(compositor
->pipe
, &compositor
->scissor
);
482 compositor
->pipe
->bind_sampler_states(compositor
->pipe
, 1, &compositor
->sampler
);
483 compositor
->pipe
->set_sampler_textures(compositor
->pipe
, 1, &src_surface
);
484 compositor
->pipe
->bind_vs_state(compositor
->pipe
, compositor
->vertex_shader
);
485 compositor
->pipe
->bind_fs_state(compositor
->pipe
, compositor
->fragment_shader
);
486 compositor
->pipe
->set_vertex_buffers(compositor
->pipe
, 2, compositor
->vertex_bufs
);
487 compositor
->pipe
->set_vertex_elements(compositor
->pipe
, 2, compositor
->vertex_elems
);
488 compositor
->pipe
->set_constant_buffer(compositor
->pipe
, PIPE_SHADER_VERTEX
, 0, &compositor
->vs_const_buf
);
489 compositor
->pipe
->set_constant_buffer(compositor
->pipe
, PIPE_SHADER_FRAGMENT
, 0, &compositor
->fs_const_buf
);
491 vs_consts
= pipe_buffer_map
493 compositor
->pipe
->screen
,
494 compositor
->vs_const_buf
.buffer
,
495 PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_DISCARD
498 vs_consts
->dst_scale
.x
= dst_area
->w
/ (float)compositor
->fb_state
.cbufs
[0]->width
;
499 vs_consts
->dst_scale
.y
= dst_area
->h
/ (float)compositor
->fb_state
.cbufs
[0]->height
;
500 vs_consts
->dst_scale
.z
= 1;
501 vs_consts
->dst_scale
.w
= 1;
502 vs_consts
->dst_trans
.x
= dst_area
->x
/ (float)compositor
->fb_state
.cbufs
[0]->width
;
503 vs_consts
->dst_trans
.y
= dst_area
->y
/ (float)compositor
->fb_state
.cbufs
[0]->height
;
504 vs_consts
->dst_trans
.z
= 0;
505 vs_consts
->dst_trans
.w
= 0;
507 vs_consts
->src_scale
.x
= src_area
->w
/ (float)src_surface
->width0
;
508 vs_consts
->src_scale
.y
= src_area
->h
/ (float)src_surface
->height0
;
509 vs_consts
->src_scale
.z
= 1;
510 vs_consts
->src_scale
.w
= 1;
511 vs_consts
->src_trans
.x
= src_area
->x
/ (float)src_surface
->width0
;
512 vs_consts
->src_trans
.y
= src_area
->y
/ (float)src_surface
->height0
;
513 vs_consts
->src_trans
.z
= 0;
514 vs_consts
->src_trans
.w
= 0;
516 pipe_buffer_unmap(compositor
->pipe
->screen
, compositor
->vs_const_buf
.buffer
);
518 compositor
->pipe
->draw_arrays(compositor
->pipe
, PIPE_PRIM_TRIANGLE_STRIP
, 0, 4);
519 compositor
->pipe
->flush(compositor
->pipe
, PIPE_FLUSH_RENDER_CACHE
, fence
);
521 pipe_surface_reference(&compositor
->fb_state
.cbufs
[0], NULL
);
524 void vl_compositor_set_csc_matrix(struct vl_compositor
*compositor
, const float *mat
)
530 pipe_buffer_map(compositor
->pipe
->screen
, compositor
->fs_const_buf
.buffer
, PIPE_BUFFER_USAGE_CPU_WRITE
),
532 sizeof(struct fragment_shader_consts
)
535 pipe_buffer_unmap(compositor
->pipe
->screen
, compositor
->fs_const_buf
.buffer
);