xa_renderer.c revision dc4c821f0817a3db716f965692fb701079f66340
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(float *matrix)
50{
51    return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
52	&& floatsEqual(matrix[8], 1);
53}
54
55static INLINE void
56map_point(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 struct pipe_resource *
75renderer_buffer_create(struct xa_context *r)
76{
77    struct pipe_resource *buf = pipe_user_buffer_create(r->pipe->screen,
78							r->buffer,
79							sizeof(float) *
80							r->buffer_size,
81							PIPE_BIND_VERTEX_BUFFER);
82
83    r->buffer_size = 0;
84
85    return buf;
86}
87
88static INLINE void
89renderer_draw(struct xa_context *r)
90{
91    struct pipe_context *pipe = r->pipe;
92    struct pipe_resource *buf = 0;
93    int num_verts = r->buffer_size / (r->attrs_per_vertex * NUM_COMPONENTS);
94
95    if (!r->buffer_size)
96	return;
97
98    buf = renderer_buffer_create(r);
99
100    if (buf) {
101	cso_set_vertex_elements(r->cso, r->attrs_per_vertex, r->velems);
102
103	util_draw_vertex_buffer(pipe, r->cso, buf, 0, PIPE_PRIM_QUADS, num_verts,	/* verts */
104				r->attrs_per_vertex);	/* attribs/vert */
105
106	pipe_resource_reference(&buf, NULL);
107    }
108}
109
110static INLINE void
111renderer_draw_conditional(struct xa_context *r, int next_batch)
112{
113    if (r->buffer_size + next_batch >= XA_VB_SIZE ||
114	(next_batch == 0 && r->buffer_size)) {
115	renderer_draw(r);
116    }
117}
118
119void
120renderer_init_state(struct xa_context *r)
121{
122    struct pipe_depth_stencil_alpha_state dsa;
123    struct pipe_rasterizer_state raster;
124    unsigned i;
125
126    /* set common initial clip state */
127    memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
128    cso_set_depth_stencil_alpha(r->cso, &dsa);
129
130    /* XXX: move to renderer_init_state? */
131    memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
132    raster.gl_rasterization_rules = 1;
133    raster.depth_clip = 1;
134    cso_set_rasterizer(r->cso, &raster);
135
136    /* vertex elements state */
137    memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3);
138    for (i = 0; i < 3; i++) {
139	r->velems[i].src_offset = i * 4 * sizeof(float);
140	r->velems[i].instance_divisor = 0;
141	r->velems[i].vertex_buffer_index = 0;
142	r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
143    }
144}
145
146static INLINE void
147add_vertex_color(struct xa_context *r, float x, float y, float color[4])
148{
149    float *vertex = r->buffer + r->buffer_size;
150
151    vertex[0] = x;
152    vertex[1] = y;
153    vertex[2] = 0.f;		/*z */
154    vertex[3] = 1.f;		/*w */
155
156    vertex[4] = color[0];	/*r */
157    vertex[5] = color[1];	/*g */
158    vertex[6] = color[2];	/*b */
159    vertex[7] = color[3];	/*a */
160
161    r->buffer_size += 8;
162}
163
164static INLINE void
165add_vertex_1tex(struct xa_context *r, float x, float y, float s, float t)
166{
167    float *vertex = r->buffer + r->buffer_size;
168
169    vertex[0] = x;
170    vertex[1] = y;
171    vertex[2] = 0.f;		/*z */
172    vertex[3] = 1.f;		/*w */
173
174    vertex[4] = s;		/*s */
175    vertex[5] = t;		/*t */
176    vertex[6] = 0.f;		/*r */
177    vertex[7] = 1.f;		/*q */
178
179    r->buffer_size += 8;
180}
181
182static INLINE void
183add_vertex_2tex(struct xa_context *r,
184		float x, float y, float s0, float t0, float s1, float t1)
185{
186    float *vertex = r->buffer + r->buffer_size;
187
188    vertex[0] = x;
189    vertex[1] = y;
190    vertex[2] = 0.f;		/*z */
191    vertex[3] = 1.f;		/*w */
192
193    vertex[4] = s0;		/*s */
194    vertex[5] = t0;		/*t */
195    vertex[6] = 0.f;		/*r */
196    vertex[7] = 1.f;		/*q */
197
198    vertex[8] = s1;		/*s */
199    vertex[9] = t1;		/*t */
200    vertex[10] = 0.f;		/*r */
201    vertex[11] = 1.f;		/*q */
202
203    r->buffer_size += 12;
204}
205
206static void
207add_vertex_data1(struct xa_context *r,
208                 float srcX, float srcY,  float dstX, float dstY,
209                 float width, float height,
210                 struct pipe_resource *src, const float *src_matrix)
211{
212    float s0, t0, s1, t1, s2, t2, s3, t3;
213    float pt0[2], pt1[2], pt2[2], pt3[2];
214
215    pt0[0] = srcX;
216    pt0[1] = srcY;
217    pt1[0] = (srcX + width);
218    pt1[1] = srcY;
219    pt2[0] = (srcX + width);
220    pt2[1] = (srcY + height);
221    pt3[0] = srcX;
222    pt3[1] = (srcY + height);
223
224    if (src_matrix) {
225	map_point((float *)src_matrix, pt0[0], pt0[1], &pt0[0], &pt0[1]);
226	map_point((float *)src_matrix, pt1[0], pt1[1], &pt1[0], &pt1[1]);
227	map_point((float *)src_matrix, pt2[0], pt2[1], &pt2[0], &pt2[1]);
228	map_point((float *)src_matrix, pt3[0], pt3[1], &pt3[0], &pt3[1]);
229    }
230
231    s0 =  pt0[0] / src->width0;
232    s1 =  pt1[0] / src->width0;
233    s2 =  pt2[0] / src->width0;
234    s3 =  pt3[0] / src->width0;
235    t0 =  pt0[1] / src->height0;
236    t1 =  pt1[1] / src->height0;
237    t2 =  pt2[1] / src->height0;
238    t3 =  pt3[1] / src->height0;
239
240    /* 1st vertex */
241    add_vertex_1tex(r, dstX, dstY, s0, t0);
242    /* 2nd vertex */
243    add_vertex_1tex(r, dstX + width, dstY, s1, t1);
244    /* 3rd vertex */
245    add_vertex_1tex(r, dstX + width, dstY + height, s2, t2);
246    /* 4th vertex */
247    add_vertex_1tex(r, dstX, dstY + height, s3, t3);
248}
249
250static void
251add_vertex_data2(struct xa_context *r,
252                 float srcX, float srcY, float maskX, float maskY,
253                 float dstX, float dstY, float width, float height,
254                 struct pipe_resource *src,
255                 struct pipe_resource *mask,
256                 const float *src_matrix, const float *mask_matrix)
257{
258    float src_s0, src_t0, src_s1, src_t1;
259    float mask_s0, mask_t0, mask_s1, mask_t1;
260    float spt0[2], spt1[2];
261    float mpt0[2], mpt1[2];
262
263    spt0[0] = srcX;
264    spt0[1] = srcY;
265    spt1[0] = srcX + width;
266    spt1[1] = srcY + height;
267
268    mpt0[0] = maskX;
269    mpt0[1] = maskY;
270    mpt1[0] = maskX + width;
271    mpt1[1] = maskY + height;
272
273    if (src_matrix) {
274	map_point((float *)src_matrix, spt0[0], spt0[1], &spt0[0], &spt0[1]);
275	map_point((float *)src_matrix, spt1[0], spt1[1], &spt1[0], &spt1[1]);
276    }
277
278    if (mask_matrix) {
279	map_point((float *)mask_matrix, mpt0[0], mpt0[1], &mpt0[0], &mpt0[1]);
280	map_point((float *)mask_matrix, mpt1[0], mpt1[1], &mpt1[0], &mpt1[1]);
281    }
282
283    src_s0 = spt0[0] / src->width0;
284    src_t0 = spt0[1] / src->height0;
285    src_s1 = spt1[0] / src->width0;
286    src_t1 = spt1[1] / src->height0;
287
288    mask_s0 = mpt0[0] / mask->width0;
289    mask_t0 = mpt0[1] / mask->height0;
290    mask_s1 = mpt1[0] / mask->width0;
291    mask_t1 = mpt1[1] / mask->height0;
292
293    /* 1st vertex */
294    add_vertex_2tex(r, dstX, dstY,
295		    src_s0, src_t0, mask_s0, mask_t0);
296    /* 2nd vertex */
297    add_vertex_2tex(r, dstX + width, dstY,
298		    src_s1, src_t0, mask_s1, mask_t0);
299    /* 3rd vertex */
300    add_vertex_2tex(r, dstX + width, dstY + height,
301		    src_s1, src_t1, mask_s1, mask_t1);
302    /* 4th vertex */
303    add_vertex_2tex(r, dstX, dstY + height,
304		    src_s0, src_t1, mask_s0, mask_t1);
305}
306
307static struct pipe_resource *
308setup_vertex_data_yuv(struct xa_context *r,
309		      float srcX,
310		      float srcY,
311		      float srcW,
312		      float srcH,
313		      float dstX,
314		      float dstY,
315		      float dstW, float dstH, struct xa_surface *srf[])
316{
317    float s0, t0, s1, t1;
318    float spt0[2], spt1[2];
319    struct pipe_resource *tex;
320
321    spt0[0] = srcX;
322    spt0[1] = srcY;
323    spt1[0] = srcX + srcW;
324    spt1[1] = srcY + srcH;
325
326    tex = srf[0]->tex;
327    s0 = spt0[0] / tex->width0;
328    t0 = spt0[1] / tex->height0;
329    s1 = spt1[0] / tex->width0;
330    t1 = spt1[1] / tex->height0;
331
332    /* 1st vertex */
333    add_vertex_1tex(r, dstX, dstY, s0, t0);
334    /* 2nd vertex */
335    add_vertex_1tex(r, dstX + dstW, dstY, s1, t0);
336    /* 3rd vertex */
337    add_vertex_1tex(r, dstX + dstW, dstY + dstH, s1, t1);
338    /* 4th vertex */
339    add_vertex_1tex(r, dstX, dstY + dstH, s0, t1);
340
341    return renderer_buffer_create(r);
342}
343
344/* Set up framebuffer, viewport and vertex shader constant buffer
345 * state for a particular destinaton surface.  In all our rendering,
346 * these concepts are linked.
347 */
348void
349renderer_bind_destination(struct xa_context *r,
350			  struct pipe_surface *surface, int width, int height)
351{
352
353    struct pipe_framebuffer_state fb;
354    struct pipe_viewport_state viewport;
355
356    /* Framebuffer uses actual surface width/height
357     */
358    memset(&fb, 0, sizeof fb);
359    fb.width = surface->width;
360    fb.height = surface->height;
361    fb.nr_cbufs = 1;
362    fb.cbufs[0] = surface;
363    fb.zsbuf = 0;
364
365    /* Viewport just touches the bit we're interested in:
366     */
367    viewport.scale[0] = width / 2.f;
368    viewport.scale[1] = height / 2.f;
369    viewport.scale[2] = 1.0;
370    viewport.scale[3] = 1.0;
371    viewport.translate[0] = width / 2.f;
372    viewport.translate[1] = height / 2.f;
373    viewport.translate[2] = 0.0;
374    viewport.translate[3] = 0.0;
375
376    /* Constant buffer set up to match viewport dimensions:
377     */
378    if (r->fb_width != width || r->fb_height != height) {
379	float vs_consts[8] = {
380	    2.f / width, 2.f / height, 1, 1,
381	    -1, -1, 0, 0
382	};
383
384	r->fb_width = width;
385	r->fb_height = height;
386
387	renderer_set_constants(r, PIPE_SHADER_VERTEX,
388			       vs_consts, sizeof vs_consts);
389    }
390
391    cso_set_framebuffer(r->cso, &fb);
392    cso_set_viewport(r->cso, &viewport);
393}
394
395void
396renderer_set_constants(struct xa_context *r,
397		       int shader_type, const float *params, int param_bytes)
398{
399    struct pipe_resource **cbuf =
400	(shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
401	&r->fs_const_buffer;
402
403    pipe_resource_reference(cbuf, NULL);
404    *cbuf = pipe_buffer_create(r->pipe->screen,
405			       PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STATIC,
406			       param_bytes);
407
408    if (*cbuf) {
409	pipe_buffer_write(r->pipe, *cbuf, 0, param_bytes, params);
410    }
411    r->pipe->set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
412}
413
414void
415renderer_copy_prepare(struct xa_context *r,
416		      struct pipe_surface *dst_surface,
417		      struct pipe_resource *src_texture,
418		      const enum xa_formats src_xa_format,
419		      const enum xa_formats dst_xa_format)
420{
421    struct pipe_context *pipe = r->pipe;
422    struct pipe_screen *screen = pipe->screen;
423    struct xa_shader shader;
424    uint32_t fs_traits = FS_COMPOSITE;
425
426    assert(screen->is_format_supported(screen, dst_surface->format,
427				       PIPE_TEXTURE_2D, 0,
428				       PIPE_BIND_RENDER_TARGET));
429    (void)screen;
430
431    /* set misc state we care about */
432    {
433	struct pipe_blend_state blend;
434
435	memset(&blend, 0, sizeof(blend));
436	blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
437	blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
438	blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
439	blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
440	blend.rt[0].colormask = PIPE_MASK_RGBA;
441	cso_set_blend(r->cso, &blend);
442    }
443
444    /* sampler */
445    {
446	struct pipe_sampler_state sampler;
447
448	memset(&sampler, 0, sizeof(sampler));
449	sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
450	sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
451	sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
452	sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
453	sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
454	sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
455	sampler.normalized_coords = 1;
456	cso_single_sampler(r->cso, 0, &sampler);
457	cso_single_sampler_done(r->cso);
458    }
459
460    renderer_bind_destination(r, dst_surface,
461			      dst_surface->width, dst_surface->height);
462
463    /* texture/sampler view */
464    {
465	struct pipe_sampler_view templ;
466	struct pipe_sampler_view *src_view;
467
468	u_sampler_view_default_template(&templ,
469					src_texture, src_texture->format);
470	src_view = pipe->create_sampler_view(pipe, src_texture, &templ);
471	cso_set_fragment_sampler_views(r->cso, 1, &src_view);
472	pipe_sampler_view_reference(&src_view, NULL);
473    }
474
475    /* shaders */
476    if (src_texture->format == PIPE_FORMAT_L8_UNORM)
477	fs_traits |= FS_SRC_LUMINANCE;
478    if (dst_surface->format == PIPE_FORMAT_L8_UNORM)
479	fs_traits |= FS_DST_LUMINANCE;
480    if (xa_format_a(dst_xa_format) != 0 &&
481	xa_format_a(src_xa_format) == 0)
482	fs_traits |= FS_SRC_SET_ALPHA;
483
484    shader = xa_shaders_get(r->shaders, VS_COMPOSITE, fs_traits);
485    cso_set_vertex_shader_handle(r->cso, shader.vs);
486    cso_set_fragment_shader_handle(r->cso, shader.fs);
487
488    r->buffer_size = 0;
489    r->attrs_per_vertex = 2;
490}
491
492void
493renderer_copy(struct xa_context *r,
494	      int dx,
495	      int dy,
496	      int sx,
497	      int sy,
498	      int width, int height, float src_width, float src_height)
499{
500    float s0, t0, s1, t1;
501    float x0, y0, x1, y1;
502
503    /* XXX: could put the texcoord scaling calculation into the vertex
504     * shader.
505     */
506    s0 = sx / src_width;
507    s1 = (sx + width) / src_width;
508    t0 = sy / src_height;
509    t1 = (sy + height) / src_height;
510
511    x0 = dx;
512    x1 = dx + width;
513    y0 = dy;
514    y1 = dy + height;
515
516    /* draw quad */
517    renderer_draw_conditional(r, 4 * 8);
518    add_vertex_1tex(r, x0, y0, s0, t0);
519    add_vertex_1tex(r, x1, y0, s1, t0);
520    add_vertex_1tex(r, x1, y1, s1, t1);
521    add_vertex_1tex(r, x0, y1, s0, t1);
522}
523
524void
525renderer_draw_yuv(struct xa_context *r,
526		  float src_x,
527		  float src_y,
528		  float src_w,
529		  float src_h,
530		  int dst_x,
531		  int dst_y, int dst_w, int dst_h, struct xa_surface *srf[])
532{
533    struct pipe_context *pipe = r->pipe;
534    struct pipe_resource *buf = 0;
535
536    buf = setup_vertex_data_yuv(r,
537				src_x, src_y, src_w, src_h, dst_x, dst_y, dst_w,
538				dst_h, srf);
539
540    if (buf) {
541	const int num_attribs = 2;	/*pos + tex coord */
542
543	cso_set_vertex_elements(r->cso, num_attribs, r->velems);
544
545	util_draw_vertex_buffer(pipe, r->cso, buf, 0, PIPE_PRIM_QUADS, 4,	/* verts */
546				num_attribs);	/* attribs/vert */
547
548	pipe_resource_reference(&buf, NULL);
549    }
550}
551
552void
553renderer_begin_solid(struct xa_context *r)
554{
555    r->buffer_size = 0;
556    r->attrs_per_vertex = 2;
557}
558
559void
560renderer_solid(struct xa_context *r,
561	       int x0, int y0, int x1, int y1, float *color)
562{
563    /*
564     * debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
565     * x0, y0, x1, y1, color[0], color[1], color[2], color[3]); */
566
567    renderer_draw_conditional(r, 4 * 8);
568
569    /* 1st vertex */
570    add_vertex_color(r, x0, y0, color);
571    /* 2nd vertex */
572    add_vertex_color(r, x1, y0, color);
573    /* 3rd vertex */
574    add_vertex_color(r, x1, y1, color);
575    /* 4th vertex */
576    add_vertex_color(r, x0, y1, color);
577}
578
579void
580renderer_draw_flush(struct xa_context *r)
581{
582    renderer_draw_conditional(r, 0);
583}
584
585void
586renderer_begin_textures(struct xa_context *r)
587{
588    r->attrs_per_vertex = 1 + r->num_bound_samplers;
589    r->buffer_size = 0;
590}
591
592void
593renderer_texture(struct xa_context *r,
594		 int *pos,
595		 int width, int height,
596		 const float *src_matrix,
597		 const float *mask_matrix)
598{
599    struct pipe_sampler_view **sampler_view = r->bound_sampler_views;
600
601#if 0
602    if (src_matrix) {
603	debug_printf("src_matrix = \n");
604	debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
605	debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
606	debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
607    }
608    if (mask_matrix) {
609	debug_printf("mask_matrix = \n");
610	debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
611	debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
612	debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
613    }
614#endif
615
616    switch(r->attrs_per_vertex) {
617    case 2:
618	renderer_draw_conditional(r, 4 * 8);
619	add_vertex_data1(r,
620			 pos[0], pos[1], /* src */
621			 pos[4], pos[5], /* dst */
622			 width, height,
623			 sampler_view[0]->texture, src_matrix);
624	break;
625    case 3:
626	renderer_draw_conditional(r, 4 * 12);
627	add_vertex_data2(r,
628			 pos[0], pos[1], /* src */
629			 pos[2], pos[3], /* mask */
630			 pos[4], pos[5], /* dst */
631			 width, height,
632			 sampler_view[0]->texture, sampler_view[1]->texture,
633			 src_matrix, mask_matrix);
634	break;
635    default:
636	break;
637    }
638}
639