Revert "mesa: Remove pointless comparison of unsigned integer with a negative constant."
[mesa.git] / src / gallium / auxiliary / vl / vl_compositor.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
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 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.
25 *
26 **************************************************************************/
27
28 #include "vl_compositor.h"
29 #include <assert.h>
30 #include <pipe/p_context.h>
31 #include <util/u_inlines.h>
32 #include <tgsi/tgsi_parse.h>
33 #include <tgsi/tgsi_build.h>
34 #include <util/u_memory.h>
35 #include "vl_csc.h"
36 #include "vl_shader_build.h"
37
38 struct vertex2f
39 {
40 float x, y;
41 };
42
43 struct vertex4f
44 {
45 float x, y, z, w;
46 };
47
48 struct vertex_shader_consts
49 {
50 struct vertex4f dst_scale;
51 struct vertex4f dst_trans;
52 struct vertex4f src_scale;
53 struct vertex4f src_trans;
54 };
55
56 struct fragment_shader_consts
57 {
58 float matrix[16];
59 };
60
61 /*
62 * Represents 2 triangles in a strip in normalized coords.
63 * Used to render the surface onto the frame buffer.
64 */
65 static const struct vertex2f surface_verts[4] =
66 {
67 {0.0f, 0.0f},
68 {0.0f, 1.0f},
69 {1.0f, 0.0f},
70 {1.0f, 1.0f}
71 };
72
73 /*
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.
76 */
77 static const struct vertex2f *surface_texcoords = surface_verts;
78
79 static void
80 create_vert_shader(struct vl_compositor *c)
81 {
82 const unsigned max_tokens = 50;
83
84 struct pipe_shader_state vs;
85 struct tgsi_token *tokens;
86 struct tgsi_header *header;
87
88 struct tgsi_full_declaration decl;
89 struct tgsi_full_instruction inst;
90
91 unsigned ti;
92
93 unsigned i;
94
95 assert(c);
96
97 tokens = (struct tgsi_token*)MALLOC(max_tokens * sizeof(struct tgsi_token));
98 header = (struct tgsi_header*)&tokens[0];
99 *header = tgsi_build_header();
100 *(struct tgsi_processor*)&tokens[1] = tgsi_build_processor(TGSI_PROCESSOR_VERTEX, header);
101
102 ti = 2;
103
104 /*
105 * decl i0 ; Vertex pos
106 * decl i1 ; Vertex texcoords
107 */
108 for (i = 0; i < 2; i++) {
109 decl = vl_decl_input(i == 0 ? TGSI_SEMANTIC_POSITION : TGSI_SEMANTIC_GENERIC, i, i, i);
110 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
111 }
112
113 /*
114 * decl c0 ; Scaling vector to scale vertex pos rect to destination size
115 * decl c1 ; Translation vector to move vertex pos rect into position
116 * decl c2 ; Scaling vector to scale texcoord rect to source size
117 * decl c3 ; Translation vector to move texcoord rect into position
118 */
119 decl = vl_decl_constants(TGSI_SEMANTIC_GENERIC, 0, 0, 3);
120 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
121
122 /*
123 * decl o0 ; Vertex pos
124 * decl o1 ; Vertex texcoords
125 */
126 for (i = 0; i < 2; i++) {
127 decl = vl_decl_output(i == 0 ? TGSI_SEMANTIC_POSITION : TGSI_SEMANTIC_GENERIC, i, i, i);
128 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
129 }
130
131 /* decl t0, t1 */
132 decl = vl_decl_temps(0, 1);
133 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
134
135 /*
136 * mad o0, i0, c0, c1 ; Scale and translate unit output rect to destination size and pos
137 * mad o1, i1, c2, c3 ; Scale and translate unit texcoord rect to source size and pos
138 */
139 for (i = 0; i < 2; ++i) {
140 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);
141 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
142 }
143
144 /* end */
145 inst = vl_end();
146 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
147
148 assert(ti <= max_tokens);
149
150 vs.tokens = tokens;
151 c->vertex_shader = c->pipe->create_vs_state(c->pipe, &vs);
152 FREE(tokens);
153 }
154
155 static void
156 create_frag_shader(struct vl_compositor *c)
157 {
158 const unsigned max_tokens = 50;
159
160 struct pipe_shader_state fs;
161 struct tgsi_token *tokens;
162 struct tgsi_header *header;
163
164 struct tgsi_full_declaration decl;
165 struct tgsi_full_instruction inst;
166
167 unsigned ti;
168
169 unsigned i;
170
171 assert(c);
172
173 tokens = (struct tgsi_token*)MALLOC(max_tokens * sizeof(struct tgsi_token));
174 header = (struct tgsi_header*)&tokens[0];
175 *header = tgsi_build_header();
176 *(struct tgsi_processor*)&tokens[1] = tgsi_build_processor(TGSI_PROCESSOR_FRAGMENT, header);
177
178 ti = 2;
179
180 /* decl i0 ; Texcoords for s0 */
181 decl = vl_decl_interpolated_input(TGSI_SEMANTIC_GENERIC, 1, 0, 0, TGSI_INTERPOLATE_LINEAR);
182 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
183
184 /*
185 * decl c0-c3 ; CSC matrix c0-c3
186 */
187 decl = vl_decl_constants(TGSI_SEMANTIC_GENERIC, 0, 0, 3);
188 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
189
190 /* decl o0 ; Fragment color */
191 decl = vl_decl_output(TGSI_SEMANTIC_COLOR, 0, 0, 0);
192 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
193
194 /* decl t0 */
195 decl = vl_decl_temps(0, 0);
196 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
197
198 /* decl s0 ; Sampler for tex containing picture to display */
199 decl = vl_decl_samplers(0, 0);
200 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
201
202 /* tex2d t0, i0, s0 ; Read src pixel */
203 inst = vl_tex(TGSI_TEXTURE_2D, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_INPUT, 0, TGSI_FILE_SAMPLER, 0);
204 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
205
206 /*
207 * dp4 o0.x, t0, c0 ; Multiply pixel by the color conversion matrix
208 * dp4 o0.y, t0, c1
209 * dp4 o0.z, t0, c2
210 * dp4 o0.w, t0, c3
211 */
212 for (i = 0; i < 4; ++i) {
213 inst = vl_inst3(TGSI_OPCODE_DP4, TGSI_FILE_OUTPUT, 0, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_CONSTANT, i);
214 inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X << i;
215 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
216 }
217
218 /* end */
219 inst = vl_end();
220 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
221
222 assert(ti <= max_tokens);
223
224 fs.tokens = tokens;
225 c->fragment_shader = c->pipe->create_fs_state(c->pipe, &fs);
226 FREE(tokens);
227 }
228
229 static bool
230 init_pipe_state(struct vl_compositor *c)
231 {
232 struct pipe_sampler_state sampler;
233
234 assert(c);
235
236 c->fb_state.nr_cbufs = 1;
237 c->fb_state.zsbuf = NULL;
238
239 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
240 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
241 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
242 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
243 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
244 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
245 sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
246 sampler.compare_func = PIPE_FUNC_ALWAYS;
247 sampler.normalized_coords = 1;
248 /*sampler.lod_bias = ;*/
249 /*sampler.min_lod = ;*/
250 /*sampler.max_lod = ;*/
251 /*sampler.border_color[i] = ;*/
252 /*sampler.max_anisotropy = ;*/
253 c->sampler = c->pipe->create_sampler_state(c->pipe, &sampler);
254
255 return true;
256 }
257
258 static void cleanup_pipe_state(struct vl_compositor *c)
259 {
260 assert(c);
261
262 c->pipe->delete_sampler_state(c->pipe, c->sampler);
263 }
264
265 static bool
266 init_shaders(struct vl_compositor *c)
267 {
268 assert(c);
269
270 create_vert_shader(c);
271 create_frag_shader(c);
272
273 return true;
274 }
275
276 static void cleanup_shaders(struct vl_compositor *c)
277 {
278 assert(c);
279
280 c->pipe->delete_vs_state(c->pipe, c->vertex_shader);
281 c->pipe->delete_fs_state(c->pipe, c->fragment_shader);
282 }
283
284 static bool
285 init_buffers(struct vl_compositor *c)
286 {
287 struct fragment_shader_consts fsc;
288
289 assert(c);
290
291 /*
292 * Create our vertex buffer and vertex buffer element
293 * VB contains 4 vertices that render a quad covering the entire window
294 * to display a rendered surface
295 * Quad is rendered as a tri strip
296 */
297 c->vertex_bufs[0].stride = sizeof(struct vertex2f);
298 c->vertex_bufs[0].max_index = 3;
299 c->vertex_bufs[0].buffer_offset = 0;
300 c->vertex_bufs[0].buffer = pipe_buffer_create
301 (
302 c->pipe->screen,
303 1,
304 PIPE_BUFFER_USAGE_VERTEX,
305 sizeof(struct vertex2f) * 4
306 );
307
308 memcpy
309 (
310 pipe_buffer_map(c->pipe->screen, c->vertex_bufs[0].buffer, PIPE_BUFFER_USAGE_CPU_WRITE),
311 surface_verts,
312 sizeof(struct vertex2f) * 4
313 );
314
315 pipe_buffer_unmap(c->pipe->screen, c->vertex_bufs[0].buffer);
316
317 c->vertex_elems[0].src_offset = 0;
318 c->vertex_elems[0].instance_divisor = 0;
319 c->vertex_elems[0].vertex_buffer_index = 0;
320 c->vertex_elems[0].nr_components = 2;
321 c->vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT;
322
323 /*
324 * Create our texcoord buffer and texcoord buffer element
325 * Texcoord buffer contains the TCs for mapping the rendered surface to the 4 vertices
326 */
327 c->vertex_bufs[1].stride = sizeof(struct vertex2f);
328 c->vertex_bufs[1].max_index = 3;
329 c->vertex_bufs[1].buffer_offset = 0;
330 c->vertex_bufs[1].buffer = pipe_buffer_create
331 (
332 c->pipe->screen,
333 1,
334 PIPE_BUFFER_USAGE_VERTEX,
335 sizeof(struct vertex2f) * 4
336 );
337
338 memcpy
339 (
340 pipe_buffer_map(c->pipe->screen, c->vertex_bufs[1].buffer, PIPE_BUFFER_USAGE_CPU_WRITE),
341 surface_texcoords,
342 sizeof(struct vertex2f) * 4
343 );
344
345 pipe_buffer_unmap(c->pipe->screen, c->vertex_bufs[1].buffer);
346
347 c->vertex_elems[1].src_offset = 0;
348 c->vertex_elems[1].instance_divisor = 0;
349 c->vertex_elems[1].vertex_buffer_index = 1;
350 c->vertex_elems[1].nr_components = 2;
351 c->vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT;
352
353 /*
354 * Create our vertex shader's constant buffer
355 * Const buffer contains scaling and translation vectors
356 */
357 c->vs_const_buf = pipe_buffer_create
358 (
359 c->pipe->screen,
360 1,
361 PIPE_BUFFER_USAGE_CONSTANT | PIPE_BUFFER_USAGE_DISCARD,
362 sizeof(struct vertex_shader_consts)
363 );
364
365 /*
366 * Create our fragment shader's constant buffer
367 * Const buffer contains the color conversion matrix and bias vectors
368 */
369 c->fs_const_buf = pipe_buffer_create
370 (
371 c->pipe->screen,
372 1,
373 PIPE_BUFFER_USAGE_CONSTANT,
374 sizeof(struct fragment_shader_consts)
375 );
376
377 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, fsc.matrix);
378
379 vl_compositor_set_csc_matrix(c, fsc.matrix);
380
381 return true;
382 }
383
384 static void
385 cleanup_buffers(struct vl_compositor *c)
386 {
387 unsigned i;
388
389 assert(c);
390
391 for (i = 0; i < 2; ++i)
392 pipe_buffer_reference(&c->vertex_bufs[i].buffer, NULL);
393
394 pipe_buffer_reference(&c->vs_const_buf, NULL);
395 pipe_buffer_reference(&c->fs_const_buf, NULL);
396 }
397
398 bool vl_compositor_init(struct vl_compositor *compositor, struct pipe_context *pipe)
399 {
400 assert(compositor);
401
402 memset(compositor, 0, sizeof(struct vl_compositor));
403
404 compositor->pipe = pipe;
405
406 if (!init_pipe_state(compositor))
407 return false;
408 if (!init_shaders(compositor)) {
409 cleanup_pipe_state(compositor);
410 return false;
411 }
412 if (!init_buffers(compositor)) {
413 cleanup_shaders(compositor);
414 cleanup_pipe_state(compositor);
415 return false;
416 }
417
418 return true;
419 }
420
421 void vl_compositor_cleanup(struct vl_compositor *compositor)
422 {
423 assert(compositor);
424
425 cleanup_buffers(compositor);
426 cleanup_shaders(compositor);
427 cleanup_pipe_state(compositor);
428 }
429
430 void vl_compositor_render(struct vl_compositor *compositor,
431 /*struct pipe_texture *backround,
432 struct pipe_video_rect *backround_area,*/
433 struct pipe_texture *src_surface,
434 enum pipe_mpeg12_picture_type picture_type,
435 /*unsigned num_past_surfaces,
436 struct pipe_texture *past_surfaces,
437 unsigned num_future_surfaces,
438 struct pipe_texture *future_surfaces,*/
439 struct pipe_video_rect *src_area,
440 struct pipe_texture *dst_surface,
441 struct pipe_video_rect *dst_area,
442 /*unsigned num_layers,
443 struct pipe_texture *layers,
444 struct pipe_video_rect *layer_src_areas,
445 struct pipe_video_rect *layer_dst_areas*/
446 struct pipe_fence_handle **fence)
447 {
448 struct vertex_shader_consts *vs_consts;
449
450 assert(compositor);
451 assert(src_surface);
452 assert(src_area);
453 assert(dst_surface);
454 assert(dst_area);
455 assert(picture_type == PIPE_MPEG12_PICTURE_TYPE_FRAME);
456
457 compositor->fb_state.width = dst_surface->width0;
458 compositor->fb_state.height = dst_surface->height0;
459 compositor->fb_state.cbufs[0] = compositor->pipe->screen->get_tex_surface
460 (
461 compositor->pipe->screen,
462 dst_surface,
463 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE
464 );
465
466 compositor->viewport.scale[0] = compositor->fb_state.width;
467 compositor->viewport.scale[1] = compositor->fb_state.height;
468 compositor->viewport.scale[2] = 1;
469 compositor->viewport.scale[3] = 1;
470 compositor->viewport.translate[0] = 0;
471 compositor->viewport.translate[1] = 0;
472 compositor->viewport.translate[2] = 0;
473 compositor->viewport.translate[3] = 0;
474
475 compositor->scissor.maxx = compositor->fb_state.width;
476 compositor->scissor.maxy = compositor->fb_state.height;
477
478 compositor->pipe->set_framebuffer_state(compositor->pipe, &compositor->fb_state);
479 compositor->pipe->set_viewport_state(compositor->pipe, &compositor->viewport);
480 compositor->pipe->set_scissor_state(compositor->pipe, &compositor->scissor);
481 compositor->pipe->bind_fragment_sampler_states(compositor->pipe, 1, &compositor->sampler);
482 compositor->pipe->set_fragment_sampler_textures(compositor->pipe, 1, &src_surface);
483 compositor->pipe->bind_vs_state(compositor->pipe, compositor->vertex_shader);
484 compositor->pipe->bind_fs_state(compositor->pipe, compositor->fragment_shader);
485 compositor->pipe->set_vertex_buffers(compositor->pipe, 2, compositor->vertex_bufs);
486 compositor->pipe->set_vertex_elements(compositor->pipe, 2, compositor->vertex_elems);
487 compositor->pipe->set_constant_buffer(compositor->pipe, PIPE_SHADER_VERTEX, 0, compositor->vs_const_buf);
488 compositor->pipe->set_constant_buffer(compositor->pipe, PIPE_SHADER_FRAGMENT, 0, compositor->fs_const_buf);
489
490 vs_consts = pipe_buffer_map
491 (
492 compositor->pipe->screen,
493 compositor->vs_const_buf,
494 PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD
495 );
496
497 vs_consts->dst_scale.x = dst_area->w / (float)compositor->fb_state.cbufs[0]->width;
498 vs_consts->dst_scale.y = dst_area->h / (float)compositor->fb_state.cbufs[0]->height;
499 vs_consts->dst_scale.z = 1;
500 vs_consts->dst_scale.w = 1;
501 vs_consts->dst_trans.x = dst_area->x / (float)compositor->fb_state.cbufs[0]->width;
502 vs_consts->dst_trans.y = dst_area->y / (float)compositor->fb_state.cbufs[0]->height;
503 vs_consts->dst_trans.z = 0;
504 vs_consts->dst_trans.w = 0;
505
506 vs_consts->src_scale.x = src_area->w / (float)src_surface->width0;
507 vs_consts->src_scale.y = src_area->h / (float)src_surface->height0;
508 vs_consts->src_scale.z = 1;
509 vs_consts->src_scale.w = 1;
510 vs_consts->src_trans.x = src_area->x / (float)src_surface->width0;
511 vs_consts->src_trans.y = src_area->y / (float)src_surface->height0;
512 vs_consts->src_trans.z = 0;
513 vs_consts->src_trans.w = 0;
514
515 pipe_buffer_unmap(compositor->pipe->screen, compositor->vs_const_buf);
516
517 compositor->pipe->draw_arrays(compositor->pipe, PIPE_PRIM_TRIANGLE_STRIP, 0, 4);
518 compositor->pipe->flush(compositor->pipe, PIPE_FLUSH_RENDER_CACHE, fence);
519
520 pipe_surface_reference(&compositor->fb_state.cbufs[0], NULL);
521 }
522
523 void vl_compositor_set_csc_matrix(struct vl_compositor *compositor, const float *mat)
524 {
525 assert(compositor);
526
527 memcpy
528 (
529 pipe_buffer_map(compositor->pipe->screen, compositor->fs_const_buf, PIPE_BUFFER_USAGE_CPU_WRITE),
530 mat,
531 sizeof(struct fragment_shader_consts)
532 );
533
534 pipe_buffer_unmap(compositor->pipe->screen, compositor->fs_const_buf);
535 }