1/**********************************************************
2 * Copyright 2009-2011 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 *********************************************************
25 * Authors:
26 * Zack Rusin <zackr-at-vmware-dot-com>
27 */
28
29#include "xa_context.h"
30#include "xa_priv.h"
31#include <math.h>
32#include "cso_cache/cso_context.h"
33#include "util/u_inlines.h"
34#include "util/u_sampler.h"
35#include "util/u_draw_quad.h"
36
37#define floatsEqual(x, y) (fabs(x - y) <= 0.00001f * MIN2(fabs(x), fabs(y)))
38#define floatIsZero(x) (floatsEqual((x) + 1, 1))
39
40#define NUM_COMPONENTS 4
41
42void
43
44
45renderer_set_constants(struct xa_context *r,
46		       int shader_type, const float *params, int param_bytes);
47
48static inline boolean
49is_affine(const float *matrix)
50{
51    return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
52	&& floatsEqual(matrix[8], 1);
53}
54
55static inline void
56map_point(const float *mat, float x, float y, float *out_x, float *out_y)
57{
58    if (!mat) {
59	*out_x = x;
60	*out_y = y;
61	return;
62    }
63
64    *out_x = mat[0] * x + mat[3] * y + mat[6];
65    *out_y = mat[1] * x + mat[4] * y + mat[7];
66    if (!is_affine(mat)) {
67	float w = 1 / (mat[2] * x + mat[5] * y + mat[8]);
68
69	*out_x *= w;
70	*out_y *= w;
71    }
72}
73
74static inline void
75renderer_draw(struct xa_context *r)
76{
77    int num_verts = r->buffer_size / (r->attrs_per_vertex * NUM_COMPONENTS);
78
79    if (!r->buffer_size)
80	return;
81
82    if (!r->scissor_valid) {
83	r->scissor.minx = 0;
84	r->scissor.miny = 0;
85	r->scissor.maxx = r->dst->tex->width0;
86	r->scissor.maxy = r->dst->tex->height0;
87    }
88
89    r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor);
90
91    cso_set_vertex_elements(r->cso, r->attrs_per_vertex, r->velems);
92    util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,
93                                 num_verts,	/* verts */
94                                 r->attrs_per_vertex);	/* attribs/vert */
95    r->buffer_size = 0;
96
97    xa_scissor_reset(r);
98}
99
100static inline void
101renderer_draw_conditional(struct xa_context *r, int next_batch)
102{
103    if (r->buffer_size + next_batch >= XA_VB_SIZE ||
104	(next_batch == 0 && r->buffer_size)) {
105	renderer_draw(r);
106    }
107}
108
109void
110renderer_init_state(struct xa_context *r)
111{
112    struct pipe_depth_stencil_alpha_state dsa;
113    struct pipe_rasterizer_state raster;
114    unsigned i;
115
116    /* set common initial clip state */
117    memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
118    cso_set_depth_stencil_alpha(r->cso, &dsa);
119
120    /* XXX: move to renderer_init_state? */
121    memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
122    raster.half_pixel_center = 1;
123    raster.bottom_edge_rule = 1;
124    raster.depth_clip_near = 1;
125    raster.depth_clip_far = 1;
126    raster.scissor = 1;
127    cso_set_rasterizer(r->cso, &raster);
128
129    /* vertex elements state */
130    memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3);
131    for (i = 0; i < 3; i++) {
132	r->velems[i].src_offset = i * 4 * sizeof(float);
133	r->velems[i].instance_divisor = 0;
134	r->velems[i].vertex_buffer_index = 0;
135	r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
136    }
137}
138
139static inline void
140add_vertex_none(struct xa_context *r, float x, float y)
141{
142    float *vertex = r->buffer + r->buffer_size;
143
144    vertex[0] = x;
145    vertex[1] = y;
146    vertex[2] = 0.f;		/*z */
147    vertex[3] = 1.f;		/*w */
148
149    r->buffer_size += 4;
150}
151
152static inline void
153add_vertex_1tex(struct xa_context *r, float x, float y, float s, float t)
154{
155    float *vertex = r->buffer + r->buffer_size;
156
157    vertex[0] = x;
158    vertex[1] = y;
159    vertex[2] = 0.f;		/*z */
160    vertex[3] = 1.f;		/*w */
161
162    vertex[4] = s;		/*s */
163    vertex[5] = t;		/*t */
164    vertex[6] = 0.f;		/*r */
165    vertex[7] = 1.f;		/*q */
166
167    r->buffer_size += 8;
168}
169
170static inline void
171add_vertex_2tex(struct xa_context *r,
172		float x, float y, float s0, float t0, float s1, float t1)
173{
174    float *vertex = r->buffer + r->buffer_size;
175
176    vertex[0] = x;
177    vertex[1] = y;
178    vertex[2] = 0.f;		/*z */
179    vertex[3] = 1.f;		/*w */
180
181    vertex[4] = s0;		/*s */
182    vertex[5] = t0;		/*t */
183    vertex[6] = 0.f;		/*r */
184    vertex[7] = 1.f;		/*q */
185
186    vertex[8] = s1;		/*s */
187    vertex[9] = t1;		/*t */
188    vertex[10] = 0.f;		/*r */
189    vertex[11] = 1.f;		/*q */
190
191    r->buffer_size += 12;
192}
193
194static void
195compute_src_coords(float sx, float sy, const struct pipe_resource *src,
196                   const float *src_matrix,
197                   float width, float height,
198                   float tc0[2], float tc1[2], float tc2[2], float tc3[2])
199{
200    tc0[0] = sx;
201    tc0[1] = sy;
202    tc1[0] = sx + width;
203    tc1[1] = sy;
204    tc2[0] = sx + width;
205    tc2[1] = sy + height;
206    tc3[0] = sx;
207    tc3[1] = sy + height;
208
209    if (src_matrix) {
210	map_point(src_matrix, tc0[0], tc0[1], &tc0[0], &tc0[1]);
211	map_point(src_matrix, tc1[0], tc1[1], &tc1[0], &tc1[1]);
212	map_point(src_matrix, tc2[0], tc2[1], &tc2[0], &tc2[1]);
213	map_point(src_matrix, tc3[0], tc3[1], &tc3[0], &tc3[1]);
214    }
215
216    tc0[0] /= src->width0;
217    tc1[0] /= src->width0;
218    tc2[0] /= src->width0;
219    tc3[0] /= src->width0;
220    tc0[1] /= src->height0;
221    tc1[1] /= src->height0;
222    tc2[1] /= src->height0;
223    tc3[1] /= src->height0;
224}
225
226static void
227add_vertex_data1(struct xa_context *r,
228                 float srcX, float srcY,  float dstX, float dstY,
229                 float width, float height,
230                 const struct pipe_resource *src, const float *src_matrix)
231{
232    float tc0[2], tc1[2], tc2[2], tc3[2];
233
234    compute_src_coords(srcX, srcY, src, src_matrix, width, height,
235                       tc0, tc1, tc2, tc3);
236    /* 1st vertex */
237    add_vertex_1tex(r, dstX, dstY, tc0[0], tc0[1]);
238    /* 2nd vertex */
239    add_vertex_1tex(r, dstX + width, dstY, tc1[0], tc1[1]);
240    /* 3rd vertex */
241    add_vertex_1tex(r, dstX + width, dstY + height, tc2[0], tc2[1]);
242    /* 4th vertex */
243    add_vertex_1tex(r, dstX, dstY + height, tc3[0], tc3[1]);
244}
245
246static void
247add_vertex_data2(struct xa_context *r,
248                 float srcX, float srcY, float maskX, float maskY,
249                 float dstX, float dstY, float width, float height,
250                 struct pipe_resource *src,
251                 struct pipe_resource *mask,
252                 const float *src_matrix, const float *mask_matrix)
253{
254    float spt0[2], spt1[2], spt2[2], spt3[2];
255    float mpt0[2], mpt1[2], mpt2[2], mpt3[2];
256
257    compute_src_coords(srcX, srcY, src, src_matrix, width, height,
258                       spt0, spt1, spt2, spt3);
259    compute_src_coords(maskX, maskY, mask, mask_matrix, width, height,
260                       mpt0, mpt1, mpt2, mpt3);
261
262    /* 1st vertex */
263    add_vertex_2tex(r, dstX, dstY,
264		    spt0[0], spt0[1], mpt0[0], mpt0[1]);
265    /* 2nd vertex */
266    add_vertex_2tex(r, dstX + width, dstY,
267		    spt1[0], spt1[1], mpt1[0], mpt1[1]);
268    /* 3rd vertex */
269    add_vertex_2tex(r, dstX + width, dstY + height,
270		    spt2[0], spt2[1], mpt2[0], mpt2[1]);
271    /* 4th vertex */
272    add_vertex_2tex(r, dstX, dstY + height,
273		    spt3[0], spt3[1], mpt3[0], mpt3[1]);
274}
275
276static void
277setup_vertex_data_yuv(struct xa_context *r,
278		      float srcX,
279		      float srcY,
280		      float srcW,
281		      float srcH,
282		      float dstX,
283		      float dstY,
284		      float dstW, float dstH, struct xa_surface *srf[])
285{
286    float s0, t0, s1, t1;
287    float spt0[2], spt1[2];
288    struct pipe_resource *tex;
289
290    spt0[0] = srcX;
291    spt0[1] = srcY;
292    spt1[0] = srcX + srcW;
293    spt1[1] = srcY + srcH;
294
295    tex = srf[0]->tex;
296    s0 = spt0[0] / tex->width0;
297    t0 = spt0[1] / tex->height0;
298    s1 = spt1[0] / tex->width0;
299    t1 = spt1[1] / tex->height0;
300
301    /* 1st vertex */
302    add_vertex_1tex(r, dstX, dstY, s0, t0);
303    /* 2nd vertex */
304    add_vertex_1tex(r, dstX + dstW, dstY, s1, t0);
305    /* 3rd vertex */
306    add_vertex_1tex(r, dstX + dstW, dstY + dstH, s1, t1);
307    /* 4th vertex */
308    add_vertex_1tex(r, dstX, dstY + dstH, s0, t1);
309}
310
311/* Set up framebuffer, viewport and vertex shader constant buffer
312 * state for a particular destinaton surface.  In all our rendering,
313 * these concepts are linked.
314 */
315void
316renderer_bind_destination(struct xa_context *r,
317			  struct pipe_surface *surface)
318{
319    int width = surface->width;
320    int height = surface->height;
321
322    struct pipe_framebuffer_state fb;
323    struct pipe_viewport_state viewport;
324
325    xa_scissor_reset(r);
326
327    /* Framebuffer uses actual surface width/height
328     */
329    memset(&fb, 0, sizeof fb);
330    fb.width = surface->width;
331    fb.height = surface->height;
332    fb.nr_cbufs = 1;
333    fb.cbufs[0] = surface;
334    fb.zsbuf = 0;
335
336    /* Viewport just touches the bit we're interested in:
337     */
338    viewport.scale[0] = width / 2.f;
339    viewport.scale[1] = height / 2.f;
340    viewport.scale[2] = 1.0;
341    viewport.translate[0] = width / 2.f;
342    viewport.translate[1] = height / 2.f;
343    viewport.translate[2] = 0.0;
344
345    /* Constant buffer set up to match viewport dimensions:
346     */
347    if (r->fb_width != width || r->fb_height != height) {
348	float vs_consts[8] = {
349	    2.f / width, 2.f / height, 1, 1,
350	    -1, -1, 0, 0
351	};
352
353	r->fb_width = width;
354	r->fb_height = height;
355
356	renderer_set_constants(r, PIPE_SHADER_VERTEX,
357			       vs_consts, sizeof vs_consts);
358    }
359
360    cso_set_framebuffer(r->cso, &fb);
361    cso_set_viewport(r->cso, &viewport);
362}
363
364void
365renderer_set_constants(struct xa_context *r,
366		       int shader_type, const float *params, int param_bytes)
367{
368    struct pipe_resource **cbuf =
369	(shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
370	&r->fs_const_buffer;
371
372    pipe_resource_reference(cbuf, NULL);
373    *cbuf = pipe_buffer_create_const0(r->pipe->screen,
374                                      PIPE_BIND_CONSTANT_BUFFER,
375                                      PIPE_USAGE_DEFAULT,
376                                      param_bytes);
377
378    if (*cbuf) {
379	pipe_buffer_write(r->pipe, *cbuf, 0, param_bytes, params);
380    }
381    pipe_set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
382}
383
384void
385renderer_copy_prepare(struct xa_context *r,
386		      struct pipe_surface *dst_surface,
387		      struct pipe_resource *src_texture,
388		      const enum xa_formats src_xa_format,
389		      const enum xa_formats dst_xa_format)
390{
391    struct pipe_context *pipe = r->pipe;
392    struct pipe_screen *screen = pipe->screen;
393    struct xa_shader shader;
394    uint32_t fs_traits = FS_COMPOSITE;
395
396    assert(screen->is_format_supported(screen, dst_surface->format,
397				       PIPE_TEXTURE_2D, 0, 0,
398				       PIPE_BIND_RENDER_TARGET));
399    (void)screen;
400
401    renderer_bind_destination(r, dst_surface);
402
403    /* set misc state we care about */
404    {
405	struct pipe_blend_state blend;
406
407	memset(&blend, 0, sizeof(blend));
408	blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
409	blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
410	blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
411	blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
412	blend.rt[0].colormask = PIPE_MASK_RGBA;
413	cso_set_blend(r->cso, &blend);
414    }
415
416    /* sampler */
417    {
418	struct pipe_sampler_state sampler;
419        const struct pipe_sampler_state *p_sampler = &sampler;
420
421	memset(&sampler, 0, sizeof(sampler));
422	sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
423	sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
424	sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
425	sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
426	sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
427	sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
428	sampler.normalized_coords = 1;
429        cso_set_samplers(r->cso, PIPE_SHADER_FRAGMENT, 1, &p_sampler);
430        r->num_bound_samplers = 1;
431    }
432
433    /* texture/sampler view */
434    {
435	struct pipe_sampler_view templ;
436	struct pipe_sampler_view *src_view;
437
438	u_sampler_view_default_template(&templ,
439					src_texture, src_texture->format);
440	src_view = pipe->create_sampler_view(pipe, src_texture, &templ);
441	cso_set_sampler_views(r->cso, PIPE_SHADER_FRAGMENT, 1, &src_view);
442	pipe_sampler_view_reference(&src_view, NULL);
443    }
444
445    /* shaders */
446    if (src_texture->format == PIPE_FORMAT_L8_UNORM ||
447        src_texture->format == PIPE_FORMAT_R8_UNORM)
448	fs_traits |= FS_SRC_LUMINANCE;
449    if (dst_surface->format == PIPE_FORMAT_L8_UNORM ||
450        dst_surface->format == PIPE_FORMAT_R8_UNORM)
451	fs_traits |= FS_DST_LUMINANCE;
452    if (xa_format_a(dst_xa_format) != 0 &&
453	xa_format_a(src_xa_format) == 0)
454	fs_traits |= FS_SRC_SET_ALPHA;
455
456    shader = xa_shaders_get(r->shaders, VS_COMPOSITE, fs_traits);
457    cso_set_vertex_shader_handle(r->cso, shader.vs);
458    cso_set_fragment_shader_handle(r->cso, shader.fs);
459
460    r->buffer_size = 0;
461    r->attrs_per_vertex = 2;
462}
463
464void
465renderer_copy(struct xa_context *r,
466	      int dx,
467	      int dy,
468	      int sx,
469	      int sy,
470	      int width, int height, float src_width, float src_height)
471{
472    float s0, t0, s1, t1;
473    float x0, y0, x1, y1;
474
475    /* XXX: could put the texcoord scaling calculation into the vertex
476     * shader.
477     */
478    s0 = sx / src_width;
479    s1 = (sx + width) / src_width;
480    t0 = sy / src_height;
481    t1 = (sy + height) / src_height;
482
483    x0 = dx;
484    x1 = dx + width;
485    y0 = dy;
486    y1 = dy + height;
487
488    /* draw quad */
489    renderer_draw_conditional(r, 4 * 8);
490    add_vertex_1tex(r, x0, y0, s0, t0);
491    add_vertex_1tex(r, x1, y0, s1, t0);
492    add_vertex_1tex(r, x1, y1, s1, t1);
493    add_vertex_1tex(r, x0, y1, s0, t1);
494}
495
496void
497renderer_draw_yuv(struct xa_context *r,
498		  float src_x,
499		  float src_y,
500		  float src_w,
501		  float src_h,
502		  int dst_x,
503		  int dst_y, int dst_w, int dst_h, struct xa_surface *srf[])
504{
505   const int num_attribs = 2;	/*pos + tex coord */
506
507   setup_vertex_data_yuv(r,
508                         src_x, src_y, src_w, src_h,
509                         dst_x, dst_y, dst_w, dst_h, srf);
510
511   if (!r->scissor_valid) {
512       r->scissor.minx = 0;
513       r->scissor.miny = 0;
514       r->scissor.maxx = r->dst->tex->width0;
515       r->scissor.maxy = r->dst->tex->height0;
516   }
517
518   r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor);
519
520   cso_set_vertex_elements(r->cso, num_attribs, r->velems);
521   util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,
522                                4,	/* verts */
523                                num_attribs);	/* attribs/vert */
524   r->buffer_size = 0;
525
526   xa_scissor_reset(r);
527}
528
529void
530renderer_begin_solid(struct xa_context *r)
531{
532    r->buffer_size = 0;
533    r->attrs_per_vertex = 1;
534    renderer_set_constants(r, PIPE_SHADER_FRAGMENT, r->solid_color,
535                           4 * sizeof(float));
536}
537
538void
539renderer_solid(struct xa_context *r,
540	       int x0, int y0, int x1, int y1)
541{
542    /*
543     * debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
544     * x0, y0, x1, y1, color[0], color[1], color[2], color[3]); */
545
546    renderer_draw_conditional(r, 4 * 4);
547
548    /* 1st vertex */
549    add_vertex_none(r, x0, y0);
550    /* 2nd vertex */
551    add_vertex_none(r, x1, y0);
552    /* 3rd vertex */
553    add_vertex_none(r, x1, y1);
554    /* 4th vertex */
555    add_vertex_none(r, x0, y1);
556}
557
558void
559renderer_draw_flush(struct xa_context *r)
560{
561    renderer_draw_conditional(r, 0);
562}
563
564void
565renderer_begin_textures(struct xa_context *r)
566{
567    r->attrs_per_vertex = 1 + r->num_bound_samplers;
568    r->buffer_size = 0;
569    if (r->has_solid_src || r->has_solid_mask)
570       renderer_set_constants(r, PIPE_SHADER_FRAGMENT, r->solid_color,
571                              4 * sizeof(float));
572}
573
574void
575renderer_texture(struct xa_context *r,
576		 int *pos,
577		 int width, int height,
578		 const float *src_matrix,
579		 const float *mask_matrix)
580{
581    struct pipe_sampler_view **sampler_view = r->bound_sampler_views;
582
583#if 0
584    if (src_matrix) {
585	debug_printf("src_matrix = \n");
586	debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
587	debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
588	debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
589    }
590    if (mask_matrix) {
591	debug_printf("mask_matrix = \n");
592	debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
593	debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
594	debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
595    }
596#endif
597
598    switch(r->attrs_per_vertex) {
599    case 2:
600	renderer_draw_conditional(r, 4 * 8);
601        if (!r->has_solid_src) {
602           add_vertex_data1(r,
603                            pos[0], pos[1], /* src */
604                            pos[4], pos[5], /* dst */
605                            width, height,
606                            sampler_view[0]->texture, src_matrix);
607        } else {
608           add_vertex_data1(r,
609                            pos[2], pos[3], /* mask */
610                            pos[4], pos[5], /* dst */
611                            width, height,
612                            sampler_view[0]->texture, mask_matrix);
613        }
614	break;
615    case 3:
616	renderer_draw_conditional(r, 4 * 12);
617	add_vertex_data2(r,
618			 pos[0], pos[1], /* src */
619			 pos[2], pos[3], /* mask */
620			 pos[4], pos[5], /* dst */
621			 width, height,
622			 sampler_view[0]->texture, sampler_view[1]->texture,
623			 src_matrix, mask_matrix);
624	break;
625    default:
626	break;
627    }
628}
629