2 #include "xorg_renderer.h"
4 #include "xorg_exa_tgsi.h"
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 #include "util/u_sampler.h"
13 #include "util/u_inlines.h"
17 #define floatsEqual(x, y) (fabs(x - y) <= 0.00001f * MIN2(fabs(x), fabs(y)))
18 #define floatIsZero(x) (floatsEqual((x) + 1, 1))
20 #define NUM_COMPONENTS 4
22 static INLINE boolean is_affine(float *matrix)
24 return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
25 && floatsEqual(matrix[8], 1);
27 static INLINE void map_point(float *mat, float x, float y,
28 float *out_x, float *out_y)
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]);
45 static INLINE struct pipe_resource *
46 renderer_buffer_create(struct xorg_renderer *r)
48 struct pipe_resource *buf =
49 pipe_user_buffer_create(r->pipe->screen,
53 /* XXX was: PIPE_BUFFER_USAGE_PIXEL/PIPE_BUFFER_USAGE_GPU_WRITE even though this is a vertex buffer??? */
54 PIPE_BIND_VERTEX_BUFFER);
61 renderer_draw(struct xorg_renderer *r)
63 struct pipe_context *pipe = r->pipe;
64 struct pipe_resource *buf = 0;
65 int num_verts = r->buffer_size/(r->attrs_per_vertex * NUM_COMPONENTS);
70 buf = renderer_buffer_create(r);
74 cso_set_vertex_elements(r->cso, r->attrs_per_vertex, r->velems);
76 util_draw_vertex_buffer(pipe, buf, 0,
78 num_verts, /* verts */
79 r->attrs_per_vertex); /* attribs/vert */
81 pipe_resource_reference(&buf, NULL);
86 renderer_draw_conditional(struct xorg_renderer *r,
89 if (r->buffer_size + next_batch >= BUF_SIZE ||
90 (next_batch == 0 && r->buffer_size)) {
96 renderer_init_state(struct xorg_renderer *r)
98 struct pipe_depth_stencil_alpha_state dsa;
99 struct pipe_rasterizer_state raster;
102 /* set common initial clip state */
103 memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
104 cso_set_depth_stencil_alpha(r->cso, &dsa);
107 /* XXX: move to renderer_init_state? */
108 memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
109 raster.gl_rasterization_rules = 1;
110 cso_set_rasterizer(r->cso, &raster);
112 /* vertex elements state */
113 memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3);
114 for (i = 0; i < 3; i++) {
115 r->velems[i].src_offset = i * 4 * sizeof(float);
116 r->velems[i].instance_divisor = 0;
117 r->velems[i].vertex_buffer_index = 0;
118 r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
124 add_vertex_color(struct xorg_renderer *r,
128 float *vertex = r->buffer + r->buffer_size;
132 vertex[2] = 0.f; /*z*/
133 vertex[3] = 1.f; /*w*/
135 vertex[4] = color[0]; /*r*/
136 vertex[5] = color[1]; /*g*/
137 vertex[6] = color[2]; /*b*/
138 vertex[7] = color[3]; /*a*/
144 add_vertex_1tex(struct xorg_renderer *r,
145 float x, float y, float s, float t)
147 float *vertex = r->buffer + r->buffer_size;
151 vertex[2] = 0.f; /*z*/
152 vertex[3] = 1.f; /*w*/
156 vertex[6] = 0.f; /*r*/
157 vertex[7] = 1.f; /*q*/
163 add_vertex_data1(struct xorg_renderer *r,
164 float srcX, float srcY, float dstX, float dstY,
165 float width, float height,
166 struct pipe_resource *src, float *src_matrix)
168 float s0, t0, s1, t1, s2, t2, s3, t3;
169 float pt0[2], pt1[2], pt2[2], pt3[2];
173 pt1[0] = (srcX + width);
175 pt2[0] = (srcX + width);
176 pt2[1] = (srcY + height);
178 pt3[1] = (srcY + height);
181 map_point(src_matrix, pt0[0], pt0[1], &pt0[0], &pt0[1]);
182 map_point(src_matrix, pt1[0], pt1[1], &pt1[0], &pt1[1]);
183 map_point(src_matrix, pt2[0], pt2[1], &pt2[0], &pt2[1]);
184 map_point(src_matrix, pt3[0], pt3[1], &pt3[0], &pt3[1]);
187 s0 = pt0[0] / src->width0;
188 s1 = pt1[0] / src->width0;
189 s2 = pt2[0] / src->width0;
190 s3 = pt3[0] / src->width0;
191 t0 = pt0[1] / src->height0;
192 t1 = pt1[1] / src->height0;
193 t2 = pt2[1] / src->height0;
194 t3 = pt3[1] / src->height0;
197 add_vertex_1tex(r, dstX, dstY, s0, t0);
199 add_vertex_1tex(r, dstX + width, dstY, s1, t1);
201 add_vertex_1tex(r, dstX + width, dstY + height, s2, t2);
203 add_vertex_1tex(r, dstX, dstY + height, s3, t3);
208 add_vertex_2tex(struct xorg_renderer *r,
210 float s0, float t0, float s1, float t1)
212 float *vertex = r->buffer + r->buffer_size;
216 vertex[2] = 0.f; /*z*/
217 vertex[3] = 1.f; /*w*/
219 vertex[4] = s0; /*s*/
220 vertex[5] = t0; /*t*/
221 vertex[6] = 0.f; /*r*/
222 vertex[7] = 1.f; /*q*/
224 vertex[8] = s1; /*s*/
225 vertex[9] = t1; /*t*/
226 vertex[10] = 0.f; /*r*/
227 vertex[11] = 1.f; /*q*/
229 r->buffer_size += 12;
233 add_vertex_data2(struct xorg_renderer *r,
234 float srcX, float srcY, float maskX, float maskY,
235 float dstX, float dstY, float width, float height,
236 struct pipe_resource *src,
237 struct pipe_resource *mask,
238 float *src_matrix, float *mask_matrix)
240 float src_s0, src_t0, src_s1, src_t1;
241 float mask_s0, mask_t0, mask_s1, mask_t1;
242 float spt0[2], spt1[2];
243 float mpt0[2], mpt1[2];
247 spt1[0] = srcX + width;
248 spt1[1] = srcY + height;
252 mpt1[0] = maskX + width;
253 mpt1[1] = maskY + height;
256 map_point(src_matrix, spt0[0], spt0[1], &spt0[0], &spt0[1]);
257 map_point(src_matrix, spt1[0], spt1[1], &spt1[0], &spt1[1]);
261 map_point(mask_matrix, mpt0[0], mpt0[1], &mpt0[0], &mpt0[1]);
262 map_point(mask_matrix, mpt1[0], mpt1[1], &mpt1[0], &mpt1[1]);
265 src_s0 = spt0[0] / src->width0;
266 src_t0 = spt0[1] / src->height0;
267 src_s1 = spt1[0] / src->width0;
268 src_t1 = spt1[1] / src->height0;
270 mask_s0 = mpt0[0] / mask->width0;
271 mask_t0 = mpt0[1] / mask->height0;
272 mask_s1 = mpt1[0] / mask->width0;
273 mask_t1 = mpt1[1] / mask->height0;
276 add_vertex_2tex(r, dstX, dstY,
277 src_s0, src_t0, mask_s0, mask_t0);
279 add_vertex_2tex(r, dstX + width, dstY,
280 src_s1, src_t0, mask_s1, mask_t0);
282 add_vertex_2tex(r, dstX + width, dstY + height,
283 src_s1, src_t1, mask_s1, mask_t1);
285 add_vertex_2tex(r, dstX, dstY + height,
286 src_s0, src_t1, mask_s0, mask_t1);
289 static struct pipe_resource *
290 setup_vertex_data_yuv(struct xorg_renderer *r,
291 float srcX, float srcY, float srcW, float srcH,
292 float dstX, float dstY, float dstW, float dstH,
293 struct pipe_resource **tex)
295 float s0, t0, s1, t1;
296 float spt0[2], spt1[2];
300 spt1[0] = srcX + srcW;
301 spt1[1] = srcY + srcH;
303 s0 = spt0[0] / tex[0]->width0;
304 t0 = spt0[1] / tex[0]->height0;
305 s1 = spt1[0] / tex[0]->width0;
306 t1 = spt1[1] / tex[0]->height0;
309 add_vertex_1tex(r, dstX, dstY, s0, t0);
311 add_vertex_1tex(r, dstX + dstW, dstY,
314 add_vertex_1tex(r, dstX + dstW, dstY + dstH,
317 add_vertex_1tex(r, dstX, dstY + dstH,
320 return renderer_buffer_create(r);
325 /* Set up framebuffer, viewport and vertex shader constant buffer
326 * state for a particular destinaton surface. In all our rendering,
327 * these concepts are linked.
329 void renderer_bind_destination(struct xorg_renderer *r,
330 struct pipe_surface *surface,
335 struct pipe_framebuffer_state fb;
336 struct pipe_viewport_state viewport;
338 /* Framebuffer uses actual surface width/height
340 memset(&fb, 0, sizeof fb);
341 fb.width = surface->width;
342 fb.height = surface->height;
344 fb.cbufs[0] = surface;
347 /* Viewport just touches the bit we're interested in:
349 viewport.scale[0] = width / 2.f;
350 viewport.scale[1] = height / 2.f;
351 viewport.scale[2] = 1.0;
352 viewport.scale[3] = 1.0;
353 viewport.translate[0] = width / 2.f;
354 viewport.translate[1] = height / 2.f;
355 viewport.translate[2] = 0.0;
356 viewport.translate[3] = 0.0;
358 /* Constant buffer set up to match viewport dimensions:
360 if (r->fb_width != width ||
361 r->fb_height != height)
363 float vs_consts[8] = {
364 2.f/width, 2.f/height, 1, 1,
369 r->fb_height = height;
371 renderer_set_constants(r, PIPE_SHADER_VERTEX,
372 vs_consts, sizeof vs_consts);
375 cso_set_framebuffer(r->cso, &fb);
376 cso_set_viewport(r->cso, &viewport);
380 struct xorg_renderer * renderer_create(struct pipe_context *pipe)
382 struct xorg_renderer *renderer = CALLOC_STRUCT(xorg_renderer);
384 renderer->pipe = pipe;
385 renderer->cso = cso_create_context(pipe);
386 renderer->shaders = xorg_shaders_create(renderer);
388 renderer_init_state(renderer);
393 void renderer_destroy(struct xorg_renderer *r)
395 struct pipe_resource **vsbuf = &r->vs_const_buffer;
396 struct pipe_resource **fsbuf = &r->fs_const_buffer;
399 pipe_resource_reference(vsbuf, NULL);
402 pipe_resource_reference(fsbuf, NULL);
405 xorg_shaders_destroy(r->shaders);
410 cso_release_all(r->cso);
411 cso_destroy_context(r->cso);
420 void renderer_set_constants(struct xorg_renderer *r,
425 struct pipe_resource **cbuf =
426 (shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
429 pipe_resource_reference(cbuf, NULL);
430 *cbuf = pipe_buffer_create(r->pipe->screen,
431 PIPE_BIND_CONSTANT_BUFFER,
435 pipe_buffer_write(r->pipe, *cbuf,
436 0, param_bytes, params);
438 r->pipe->set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
442 void renderer_copy_prepare(struct xorg_renderer *r,
443 struct pipe_surface *dst_surface,
444 struct pipe_resource *src_texture)
446 struct pipe_context *pipe = r->pipe;
447 struct pipe_screen *screen = pipe->screen;
448 struct xorg_shader shader;
450 assert(screen->is_format_supported(screen, dst_surface->format,
452 PIPE_BIND_RENDER_TARGET,
457 /* set misc state we care about */
459 struct pipe_blend_state blend;
460 memset(&blend, 0, sizeof(blend));
461 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
462 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
463 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
464 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
465 blend.rt[0].colormask = PIPE_MASK_RGBA;
466 cso_set_blend(r->cso, &blend);
471 struct pipe_sampler_state sampler;
472 memset(&sampler, 0, sizeof(sampler));
473 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
474 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
475 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
476 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
477 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
478 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
479 sampler.normalized_coords = 1;
480 cso_single_sampler(r->cso, 0, &sampler);
481 cso_single_sampler_done(r->cso);
484 renderer_bind_destination(r, dst_surface,
486 dst_surface->height);
488 /* texture/sampler view */
490 struct pipe_sampler_view templ;
491 struct pipe_sampler_view *src_view;
492 u_sampler_view_default_template(&templ,
494 src_texture->format);
495 src_view = pipe->create_sampler_view(pipe, src_texture, &templ);
496 cso_set_fragment_sampler_views(r->cso, 1, &src_view);
497 pipe_sampler_view_reference(&src_view, NULL);
501 shader = xorg_shaders_get(r->shaders,
504 cso_set_vertex_shader_handle(r->cso, shader.vs);
505 cso_set_fragment_shader_handle(r->cso, shader.fs);
508 r->attrs_per_vertex = 2;
511 struct pipe_resource *
512 renderer_clone_texture(struct xorg_renderer *r,
513 struct pipe_resource *src)
515 enum pipe_format format;
516 struct pipe_context *pipe = r->pipe;
517 struct pipe_screen *screen = pipe->screen;
518 struct pipe_resource *pt;
519 struct pipe_resource templ;
521 if (pipe->is_resource_referenced(pipe, src, 0, 0) &
522 PIPE_REFERENCED_FOR_WRITE)
523 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
525 /* the coming in texture should already have that invariance */
526 debug_assert(screen->is_format_supported(screen, src->format,
528 PIPE_BIND_SAMPLER_VIEW, 0));
530 format = src->format;
532 memset(&templ, 0, sizeof(templ));
533 templ.target = PIPE_TEXTURE_2D;
534 templ.format = format;
535 templ.last_level = 0;
536 templ.width0 = src->width0;
537 templ.height0 = src->height0;
539 templ.bind = PIPE_BIND_SAMPLER_VIEW;
541 pt = screen->resource_create(screen, &templ);
543 debug_assert(!pt || pipe_is_referenced(&pt->reference));
549 /* copy source framebuffer surface into texture */
550 struct pipe_surface *ps_read = screen->get_tex_surface(
551 screen, src, 0, 0, 0, PIPE_BIND_BLIT_SOURCE);
552 struct pipe_surface *ps_tex = screen->get_tex_surface(
553 screen, pt, 0, 0, 0, PIPE_BIND_BLIT_DESTINATION );
554 if (pipe->surface_copy) {
555 pipe->surface_copy(pipe,
559 0, 0, src->width0, src->height0);
561 util_surface_copy(pipe, FALSE,
565 0, 0, src->width0, src->height0);
567 pipe_surface_reference(&ps_read, NULL);
568 pipe_surface_reference(&ps_tex, NULL);
575 void renderer_copy_pixmap(struct xorg_renderer *r,
578 int width, int height,
582 float s0, t0, s1, t1;
583 float x0, y0, x1, y1;
586 /* XXX: could put the texcoord scaling calculation into the vertex
590 s1 = (sx + width) / src_width;
591 t0 = sy / src_height;
592 t1 = (sy + height) / src_height;
600 renderer_draw_conditional(r, 4*8);
601 add_vertex_1tex(r, x0, y0, s0, t0);
602 add_vertex_1tex(r, x1, y0, s1, t0);
603 add_vertex_1tex(r, x1, y1, s1, t1);
604 add_vertex_1tex(r, x0, y1, s0, t1);
610 void renderer_draw_yuv(struct xorg_renderer *r,
611 int src_x, int src_y, int src_w, int src_h,
612 int dst_x, int dst_y, int dst_w, int dst_h,
613 struct pipe_resource **textures)
615 struct pipe_context *pipe = r->pipe;
616 struct pipe_resource *buf = 0;
618 buf = setup_vertex_data_yuv(r,
619 src_x, src_y, src_w, src_h,
620 dst_x, dst_y, dst_w, dst_h,
624 const int num_attribs = 2; /*pos + tex coord*/
626 cso_set_vertex_elements(r->cso, num_attribs, r->velems);
628 util_draw_vertex_buffer(pipe, buf, 0,
631 num_attribs); /* attribs/vert */
633 pipe_resource_reference(&buf, NULL);
637 void renderer_begin_solid(struct xorg_renderer *r)
640 r->attrs_per_vertex = 2;
643 void renderer_solid(struct xorg_renderer *r,
649 debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
650 x0, y0, x1, y1, color[0], color[1], color[2], color[3]);*/
652 renderer_draw_conditional(r, 4 * 8);
655 add_vertex_color(r, x0, y0, color);
657 add_vertex_color(r, x1, y0, color);
659 add_vertex_color(r, x1, y1, color);
661 add_vertex_color(r, x0, y1, color);
664 void renderer_draw_flush(struct xorg_renderer *r)
666 renderer_draw_conditional(r, 0);
669 void renderer_begin_textures(struct xorg_renderer *r,
672 r->attrs_per_vertex = 1 + num_textures;
676 void renderer_texture(struct xorg_renderer *r,
678 int width, int height,
679 struct pipe_sampler_view **sampler_view,
687 debug_printf("src_matrix = \n");
688 debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
689 debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
690 debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
693 debug_printf("mask_matrix = \n");
694 debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
695 debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
696 debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
700 switch(r->attrs_per_vertex) {
702 renderer_draw_conditional(r, 4 * 8);
704 pos[0], pos[1], /* src */
705 pos[4], pos[5], /* dst */
707 sampler_view[0]->texture, src_matrix);
710 renderer_draw_conditional(r, 4 * 12);
712 pos[0], pos[1], /* src */
713 pos[2], pos[3], /* mask */
714 pos[4], pos[5], /* dst */
716 sampler_view[0]->texture, sampler_view[1]->texture,
717 src_matrix, mask_matrix);
720 debug_assert(!"Unsupported number of textures");