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