xorg_renderer.c revision 124ae596806f1a77af46f1f0e446d448da6e953a
1#include "xorg_exa.h"
2#include "xorg_renderer.h"
3
4#include "xorg_exa_tgsi.h"
5
6#include "cso_cache/cso_context.h"
7#include "util/u_draw_quad.h"
8#include "util/u_math.h"
9#include "util/u_memory.h"
10#include "util/u_rect.h"
11
12#include "pipe/p_inlines.h"
13
14#include <math.h>
15
16#define floatsEqual(x, y) (fabs(x - y) <= 0.00001f * MIN2(fabs(x), fabs(y)))
17#define floatIsZero(x) (floatsEqual((x) + 1, 1))
18
19#define NUM_COMPONENTS 4
20
21static INLINE boolean is_affine(float *matrix)
22{
23   return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
24      && floatsEqual(matrix[8], 1);
25}
26static INLINE void map_point(float *mat, float x, float y,
27                             float *out_x, float *out_y)
28{
29   if (!mat) {
30      *out_x = x;
31      *out_y = y;
32      return;
33   }
34
35   *out_x = mat[0]*x + mat[3]*y + mat[6];
36   *out_y = mat[1]*x + mat[4]*y + mat[7];
37   if (!is_affine(mat)) {
38      float w = 1/(mat[2]*x + mat[5]*y + mat[8]);
39      *out_x *= w;
40      *out_y *= w;
41   }
42}
43
44static INLINE struct pipe_buffer *
45renderer_buffer_create(struct xorg_renderer *r)
46{
47   struct pipe_buffer *buf =
48      pipe_user_buffer_create(r->pipe->screen,
49                              r->buffer,
50                              sizeof(float)*
51                              r->buffer_size);
52   r->buffer_size = 0;
53
54   return buf;
55}
56
57static INLINE void
58renderer_draw(struct xorg_renderer *r)
59{
60   struct pipe_context *pipe = r->pipe;
61   struct pipe_buffer *buf = 0;
62   int num_verts = r->buffer_size/(r->attrs_per_vertex * NUM_COMPONENTS);
63
64   if (!r->buffer_size)
65      return;
66
67   buf = renderer_buffer_create(r);
68
69
70   if (buf) {
71      util_draw_vertex_buffer(pipe, buf, 0,
72                              PIPE_PRIM_QUADS,
73                              num_verts,  /* verts */
74                              r->attrs_per_vertex); /* attribs/vert */
75
76      pipe_buffer_reference(&buf, NULL);
77   }
78}
79
80static INLINE void
81renderer_draw_conditional(struct xorg_renderer *r,
82                          int next_batch)
83{
84   if (r->buffer_size + next_batch >= BUF_SIZE ||
85       (next_batch == 0 && r->buffer_size)) {
86      renderer_draw(r);
87   }
88}
89
90static void
91renderer_init_state(struct xorg_renderer *r)
92{
93   struct pipe_depth_stencil_alpha_state dsa;
94   struct pipe_rasterizer_state raster;
95
96   /* set common initial clip state */
97   memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
98   cso_set_depth_stencil_alpha(r->cso, &dsa);
99
100
101   /* XXX: move to renderer_init_state? */
102   memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
103   raster.gl_rasterization_rules = 1;
104   cso_set_rasterizer(r->cso, &raster);
105
106}
107
108
109static INLINE void
110add_vertex_color(struct xorg_renderer *r,
111                 float x, float y,
112                 float color[4])
113{
114   float *vertex = r->buffer + r->buffer_size;
115
116   vertex[0] = x;
117   vertex[1] = y;
118   vertex[2] = 0.f; /*z*/
119   vertex[3] = 1.f; /*w*/
120
121   vertex[4] = color[0]; /*r*/
122   vertex[5] = color[1]; /*g*/
123   vertex[6] = color[2]; /*b*/
124   vertex[7] = color[3]; /*a*/
125
126   r->buffer_size += 8;
127}
128
129static INLINE void
130add_vertex_1tex(struct xorg_renderer *r,
131                float x, float y, float s, float t)
132{
133   float *vertex = r->buffer + r->buffer_size;
134
135   vertex[0] = x;
136   vertex[1] = y;
137   vertex[2] = 0.f; /*z*/
138   vertex[3] = 1.f; /*w*/
139
140   vertex[4] = s;   /*s*/
141   vertex[5] = t;   /*t*/
142   vertex[6] = 0.f; /*r*/
143   vertex[7] = 1.f; /*q*/
144
145   r->buffer_size += 8;
146}
147
148static void
149add_vertex_data1(struct xorg_renderer *r,
150                 float srcX, float srcY,  float dstX, float dstY,
151                 float width, float height,
152                 struct pipe_texture *src, float *src_matrix)
153{
154   float s0, t0, s1, t1, s2, t2, s3, t3;
155   float pt0[2], pt1[2], pt2[2], pt3[2];
156
157   pt0[0] = srcX;
158   pt0[1] = srcY;
159   pt1[0] = (srcX + width);
160   pt1[1] = srcY;
161   pt2[0] = (srcX + width);
162   pt2[1] = (srcY + height);
163   pt3[0] = srcX;
164   pt3[1] = (srcY + height);
165
166   if (src_matrix) {
167      map_point(src_matrix, pt0[0], pt0[1], &pt0[0], &pt0[1]);
168      map_point(src_matrix, pt1[0], pt1[1], &pt1[0], &pt1[1]);
169      map_point(src_matrix, pt2[0], pt2[1], &pt2[0], &pt2[1]);
170      map_point(src_matrix, pt3[0], pt3[1], &pt3[0], &pt3[1]);
171   }
172
173   s0 =  pt0[0] / src->width[0];
174   s1 =  pt1[0] / src->width[0];
175   s2 =  pt2[0] / src->width[0];
176   s3 =  pt3[0] / src->width[0];
177   t0 =  pt0[1] / src->height[0];
178   t1 =  pt1[1] / src->height[0];
179   t2 =  pt2[1] / src->height[0];
180   t3 =  pt3[1] / src->height[0];
181
182   /* 1st vertex */
183   add_vertex_1tex(r, dstX, dstY, s0, t0);
184   /* 2nd vertex */
185   add_vertex_1tex(r, dstX + width, dstY, s1, t1);
186   /* 3rd vertex */
187   add_vertex_1tex(r, dstX + width, dstY + height, s2, t2);
188   /* 4th vertex */
189   add_vertex_1tex(r, dstX, dstY + height, s3, t3);
190}
191
192
193static INLINE void
194add_vertex_2tex(struct xorg_renderer *r,
195                float x, float y,
196                float s0, float t0, float s1, float t1)
197{
198   float *vertex = r->buffer + r->buffer_size;
199
200   vertex[0] = x;
201   vertex[1] = y;
202   vertex[2] = 0.f; /*z*/
203   vertex[3] = 1.f; /*w*/
204
205   vertex[4] = s0;  /*s*/
206   vertex[5] = t0;  /*t*/
207   vertex[6] = 0.f; /*r*/
208   vertex[7] = 1.f; /*q*/
209
210   vertex[8] = s1;  /*s*/
211   vertex[9] = t1;  /*t*/
212   vertex[10] = 0.f; /*r*/
213   vertex[11] = 1.f; /*q*/
214
215   r->buffer_size += 12;
216}
217
218static void
219add_vertex_data2(struct xorg_renderer *r,
220                 float srcX, float srcY, float maskX, float maskY,
221                 float dstX, float dstY, float width, float height,
222                 struct pipe_texture *src,
223                 struct pipe_texture *mask,
224                 float *src_matrix, float *mask_matrix)
225{
226   float src_s0, src_t0, src_s1, src_t1;
227   float mask_s0, mask_t0, mask_s1, mask_t1;
228   float spt0[2], spt1[2];
229   float mpt0[2], mpt1[2];
230
231   spt0[0] = srcX;
232   spt0[1] = srcY;
233   spt1[0] = srcX + width;
234   spt1[1] = srcY + height;
235
236   mpt0[0] = maskX;
237   mpt0[1] = maskY;
238   mpt1[0] = maskX + width;
239   mpt1[1] = maskY + height;
240
241   if (src_matrix) {
242      map_point(src_matrix, spt0[0], spt0[1], &spt0[0], &spt0[1]);
243      map_point(src_matrix, spt1[0], spt1[1], &spt1[0], &spt1[1]);
244   }
245
246   if (mask_matrix) {
247      map_point(mask_matrix, mpt0[0], mpt0[1], &mpt0[0], &mpt0[1]);
248      map_point(mask_matrix, mpt1[0], mpt1[1], &mpt1[0], &mpt1[1]);
249   }
250
251   src_s0 = spt0[0] / src->width[0];
252   src_t0 = spt0[1] / src->height[0];
253   src_s1 = spt1[0] / src->width[0];
254   src_t1 = spt1[1] / src->height[0];
255
256   mask_s0 = mpt0[0] / mask->width[0];
257   mask_t0 = mpt0[1] / mask->height[0];
258   mask_s1 = mpt1[0] / mask->width[0];
259   mask_t1 = mpt1[1] / mask->height[0];
260
261   /* 1st vertex */
262   add_vertex_2tex(r, dstX, dstY,
263                   src_s0, src_t0, mask_s0, mask_t0);
264   /* 2nd vertex */
265   add_vertex_2tex(r, dstX + width, dstY,
266                   src_s1, src_t0, mask_s1, mask_t0);
267   /* 3rd vertex */
268   add_vertex_2tex(r, dstX + width, dstY + height,
269                   src_s1, src_t1, mask_s1, mask_t1);
270   /* 4th vertex */
271   add_vertex_2tex(r, dstX, dstY + height,
272                   src_s0, src_t1, mask_s0, mask_t1);
273}
274
275static struct pipe_buffer *
276setup_vertex_data_yuv(struct xorg_renderer *r,
277                      float srcX, float srcY, float srcW, float srcH,
278                      float dstX, float dstY, float dstW, float dstH,
279                      struct pipe_texture **tex)
280{
281   float s0, t0, s1, t1;
282   float spt0[2], spt1[2];
283
284   spt0[0] = srcX;
285   spt0[1] = srcY;
286   spt1[0] = srcX + srcW;
287   spt1[1] = srcY + srcH;
288
289   s0 = spt0[0] / tex[0]->width[0];
290   t0 = spt0[1] / tex[0]->height[0];
291   s1 = spt1[0] / tex[0]->width[0];
292   t1 = spt1[1] / tex[0]->height[0];
293
294   /* 1st vertex */
295   add_vertex_1tex(r, dstX, dstY, s0, t0);
296   /* 2nd vertex */
297   add_vertex_1tex(r, dstX + dstW, dstY,
298                   s1, t0);
299   /* 3rd vertex */
300   add_vertex_1tex(r, dstX + dstW, dstY + dstH,
301                   s1, t1);
302   /* 4th vertex */
303   add_vertex_1tex(r, dstX, dstY + dstH,
304                   s0, t1);
305
306   return renderer_buffer_create(r);
307}
308
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 renderer_bind_destination(struct xorg_renderer *r,
316                               struct pipe_surface *surface,
317                               int width,
318                               int height )
319{
320
321   struct pipe_framebuffer_state fb;
322   struct pipe_viewport_state viewport;
323
324   /* Framebuffer uses actual surface width/height
325    */
326   memset(&fb, 0, sizeof fb);
327   fb.width  = surface->width;
328   fb.height = surface->height;
329   fb.nr_cbufs = 1;
330   fb.cbufs[0] = surface;
331   fb.zsbuf = 0;
332
333   /* Viewport sets us up to just touch the bit we're interested in:
334    */
335   viewport.scale[0] =  width / 2.f;
336   viewport.scale[1] =  height / 2.f;
337   viewport.scale[2] =  1.0;
338   viewport.scale[3] =  1.0;
339   viewport.translate[0] = width / 2.f;
340   viewport.translate[1] = height / 2.f;
341   viewport.translate[2] = 0.0;
342   viewport.translate[3] = 0.0;
343
344   /* Constant buffer set up to match viewport dimensions:
345    */
346   if (r->fb_width != width ||
347       r->fb_height != height)
348   {
349      float vs_consts[8] = {
350         2.f/width, 2.f/height, 1, 1,
351         -1, -1, 0, 0
352      };
353
354      r->fb_width = width;
355      r->fb_height = height;
356
357      renderer_set_constants(r, PIPE_SHADER_VERTEX,
358                             vs_consts, sizeof vs_consts);
359   }
360
361   cso_set_framebuffer(r->cso, &fb);
362   cso_set_viewport(r->cso, &viewport);
363}
364
365
366struct xorg_renderer * renderer_create(struct pipe_context *pipe)
367{
368   struct xorg_renderer *renderer = CALLOC_STRUCT(xorg_renderer);
369
370   renderer->pipe = pipe;
371   renderer->cso = cso_create_context(pipe);
372   renderer->shaders = xorg_shaders_create(renderer);
373
374   renderer_init_state(renderer);
375
376   return renderer;
377}
378
379void renderer_destroy(struct xorg_renderer *r)
380{
381   struct pipe_constant_buffer *vsbuf = &r->vs_const_buffer;
382   struct pipe_constant_buffer *fsbuf = &r->fs_const_buffer;
383
384   if (vsbuf && vsbuf->buffer)
385      pipe_buffer_reference(&vsbuf->buffer, NULL);
386
387   if (fsbuf && fsbuf->buffer)
388      pipe_buffer_reference(&fsbuf->buffer, NULL);
389
390   if (r->shaders) {
391      xorg_shaders_destroy(r->shaders);
392      r->shaders = NULL;
393   }
394
395   if (r->cso) {
396      cso_release_all(r->cso);
397      cso_destroy_context(r->cso);
398      r->cso = NULL;
399   }
400}
401
402
403
404
405
406void renderer_set_constants(struct xorg_renderer *r,
407                            int shader_type,
408                            const float *params,
409                            int param_bytes)
410{
411   struct pipe_constant_buffer *cbuf =
412      (shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
413      &r->fs_const_buffer;
414
415   pipe_buffer_reference(&cbuf->buffer, NULL);
416   cbuf->buffer = pipe_buffer_create(r->pipe->screen, 16,
417                                     PIPE_BUFFER_USAGE_CONSTANT,
418                                     param_bytes);
419
420   if (cbuf->buffer) {
421      pipe_buffer_write(r->pipe->screen, cbuf->buffer,
422                        0, param_bytes, params);
423   }
424   r->pipe->set_constant_buffer(r->pipe, shader_type, 0, cbuf);
425}
426
427
428void renderer_copy_prepare(struct xorg_renderer *r,
429                           struct pipe_surface *dst_surface,
430                           struct pipe_texture *src_texture)
431{
432   struct pipe_context *pipe = r->pipe;
433   struct pipe_screen *screen = pipe->screen;
434   struct xorg_shader shader;
435
436   assert(screen->is_format_supported(screen, dst_surface->format,
437                                      PIPE_TEXTURE_2D,
438                                      PIPE_TEXTURE_USAGE_RENDER_TARGET,
439                                      0));
440
441
442   /* set misc state we care about */
443   {
444      struct pipe_blend_state blend;
445      memset(&blend, 0, sizeof(blend));
446      blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
447      blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
448      blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
449      blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
450      blend.colormask = PIPE_MASK_RGBA;
451      cso_set_blend(r->cso, &blend);
452   }
453
454   /* sampler */
455   {
456      struct pipe_sampler_state sampler;
457      memset(&sampler, 0, sizeof(sampler));
458      sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
459      sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
460      sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
461      sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
462      sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
463      sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
464      sampler.normalized_coords = 1;
465      cso_single_sampler(r->cso, 0, &sampler);
466      cso_single_sampler_done(r->cso);
467   }
468
469   renderer_bind_destination(r, dst_surface);
470
471   /* texture */
472   cso_set_sampler_textures(r->cso, 1, &src_texture);
473
474   /* shaders */
475   shader = xorg_shaders_get(r->shaders,
476                             VS_COMPOSITE,
477                             FS_COMPOSITE);
478   cso_set_vertex_shader_handle(r->cso, shader.vs);
479   cso_set_fragment_shader_handle(r->cso, shader.fs);
480
481   r->buffer_size = 0;
482   r->attrs_per_vertex = 2;
483}
484
485struct pipe_texture *
486renderer_clone_texture(struct xorg_renderer *r,
487                       struct pipe_texture *src)
488{
489   enum pipe_format format;
490   struct pipe_context *pipe = r->pipe;
491   struct pipe_screen *screen = pipe->screen;
492   struct pipe_texture *pt;
493   struct pipe_texture templ;
494
495   if (pipe->is_texture_referenced(pipe, src, 0, 0) &
496       PIPE_REFERENCED_FOR_WRITE)
497      pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
498
499   /* the coming in texture should already have that invariance */
500   debug_assert(screen->is_format_supported(screen, src->format,
501                                            PIPE_TEXTURE_2D,
502                                            PIPE_TEXTURE_USAGE_SAMPLER, 0));
503
504   format = src->format;
505
506   memset(&templ, 0, sizeof(templ));
507   templ.target = PIPE_TEXTURE_2D;
508   templ.format = format;
509   templ.last_level = 0;
510   templ.width[0] = src->width[0];
511   templ.height[0] = src->height[0];
512   templ.depth[0] = 1;
513   pf_get_block(format, &templ.block);
514   templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
515
516   pt = screen->texture_create(screen, &templ);
517
518   debug_assert(!pt || pipe_is_referenced(&pt->reference));
519
520   if (!pt)
521      return NULL;
522
523   {
524      /* copy source framebuffer surface into texture */
525      struct pipe_surface *ps_read = screen->get_tex_surface(
526         screen, src, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ);
527      struct pipe_surface *ps_tex = screen->get_tex_surface(
528         screen, pt, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE );
529      if (pipe->surface_copy) {
530         pipe->surface_copy(pipe,
531                ps_tex, /* dest */
532                0, 0, /* destx/y */
533                ps_read,
534                0, 0, src->width[0], src->height[0]);
535      } else {
536          util_surface_copy(pipe, FALSE,
537                ps_tex, /* dest */
538                0, 0, /* destx/y */
539                ps_read,
540                0, 0, src->width[0], src->height[0]);
541      }
542      pipe_surface_reference(&ps_read, NULL);
543      pipe_surface_reference(&ps_tex, NULL);
544   }
545
546   return pt;
547}
548
549
550void renderer_copy_pixmap(struct xorg_renderer *r,
551                          int dx, int dy,
552                          int sx, int sy,
553                          int width, int height,
554                          float src_width,
555                          float src_height)
556{
557   float s0, t0, s1, t1;
558   float x0, y0, x1, y1;
559
560
561   /* XXX: could put the texcoord scaling calculation into the vertex
562    * shader.
563    */
564   s0 = sx            / src_width;
565   s1 = (sx + width)  / src_width;
566   t0 = sy            / src_height;
567   t1 = (sy + height) / src_height;
568
569   x0 = dx;
570   x1 = dx + width;
571   y0 = dy;
572   y1 = dy + height;
573
574   /* draw quad */
575   renderer_draw_conditional(r, 4*8);
576   add_vertex_1tex(r, x0, y0, s0, t0);
577   add_vertex_1tex(r, x1, y0, s1, t0);
578   add_vertex_1tex(r, x1, y1, s1, t1);
579   add_vertex_1tex(r, x0, y1, s0, t1);
580}
581
582
583
584
585void renderer_draw_yuv(struct xorg_renderer *r,
586                       int src_x, int src_y, int src_w, int src_h,
587                       int dst_x, int dst_y, int dst_w, int dst_h,
588                       struct pipe_texture **textures)
589{
590   struct pipe_context *pipe = r->pipe;
591   struct pipe_buffer *buf = 0;
592
593   buf = setup_vertex_data_yuv(r,
594                               src_x, src_y, src_w, src_h,
595                               dst_x, dst_y, dst_w, dst_h,
596                               textures);
597
598   if (buf) {
599      const int num_attribs = 2; /*pos + tex coord*/
600
601      util_draw_vertex_buffer(pipe, buf, 0,
602                              PIPE_PRIM_QUADS,
603                              4,  /* verts */
604                              num_attribs); /* attribs/vert */
605
606      pipe_buffer_reference(&buf, NULL);
607   }
608}
609
610void renderer_begin_solid(struct xorg_renderer *r)
611{
612   r->buffer_size = 0;
613   r->attrs_per_vertex = 2;
614}
615
616void renderer_solid(struct xorg_renderer *r,
617                    int x0, int y0,
618                    int x1, int y1,
619                    float *color)
620{
621   /*
622   debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
623   x0, y0, x1, y1, color[0], color[1], color[2], color[3]);*/
624
625   renderer_draw_conditional(r, 4 * 8);
626
627   /* 1st vertex */
628   add_vertex_color(r, x0, y0, color);
629   /* 2nd vertex */
630   add_vertex_color(r, x1, y0, color);
631   /* 3rd vertex */
632   add_vertex_color(r, x1, y1, color);
633   /* 4th vertex */
634   add_vertex_color(r, x0, y1, color);
635}
636
637void renderer_draw_flush(struct xorg_renderer *r)
638{
639   renderer_draw_conditional(r, 0);
640}
641
642void renderer_begin_textures(struct xorg_renderer *r,
643                             struct pipe_texture **textures,
644                             int num_textures)
645{
646   r->attrs_per_vertex = 1 + num_textures;
647   r->buffer_size = 0;
648}
649
650void renderer_texture(struct xorg_renderer *r,
651                      int *pos,
652                      int width, int height,
653                      struct pipe_texture **textures,
654                      int num_textures,
655                      float *src_matrix,
656                      float *mask_matrix)
657{
658
659#if 0
660   if (src_matrix) {
661      debug_printf("src_matrix = \n");
662      debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
663      debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
664      debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
665   }
666   if (mask_matrix) {
667      debug_printf("mask_matrix = \n");
668      debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
669      debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
670      debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
671   }
672#endif
673
674   switch(r->attrs_per_vertex) {
675   case 2:
676      renderer_draw_conditional(r, 4 * 8);
677      add_vertex_data1(r,
678                       pos[0], pos[1], /* src */
679                       pos[4], pos[5], /* dst */
680                       width, height,
681                       textures[0], src_matrix);
682      break;
683   case 3:
684      renderer_draw_conditional(r, 4 * 12);
685      add_vertex_data2(r,
686                       pos[0], pos[1], /* src */
687                       pos[2], pos[3], /* mask */
688                       pos[4], pos[5], /* dst */
689                       width, height,
690                       textures[0], textures[1],
691                       src_matrix, mask_matrix);
692      break;
693   default:
694      debug_assert(!"Unsupported number of textures");
695      break;
696   }
697}
698