Path: blob/21.2-virgl/src/gallium/frontends/xa/xa_renderer.c
4561 views
/**********************************************************1* Copyright 2009-2011 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23*********************************************************24* Authors:25* Zack Rusin <zackr-at-vmware-dot-com>26*/2728#include "xa_context.h"29#include "xa_priv.h"30#include <math.h>31#include "cso_cache/cso_context.h"32#include "util/u_inlines.h"33#include "util/u_sampler.h"34#include "util/u_draw_quad.h"3536#define floatsEqual(x, y) (fabsf(x - y) <= 0.00001f * MIN2(fabsf(x), fabsf(y)))37#define floatIsZero(x) (floatsEqual((x) + 1, 1))3839#define NUM_COMPONENTS 44041void424344renderer_set_constants(struct xa_context *r,45int shader_type, const float *params, int param_bytes);4647static inline boolean48is_affine(const float *matrix)49{50return floatIsZero(matrix[2]) && floatIsZero(matrix[5])51&& floatsEqual(matrix[8], 1);52}5354static inline void55map_point(const float *mat, float x, float y, float *out_x, float *out_y)56{57if (!mat) {58*out_x = x;59*out_y = y;60return;61}6263*out_x = mat[0] * x + mat[3] * y + mat[6];64*out_y = mat[1] * x + mat[4] * y + mat[7];65if (!is_affine(mat)) {66float w = 1 / (mat[2] * x + mat[5] * y + mat[8]);6768*out_x *= w;69*out_y *= w;70}71}7273static inline void74renderer_draw(struct xa_context *r)75{76int num_verts = r->buffer_size / (r->attrs_per_vertex * NUM_COMPONENTS);7778if (!r->buffer_size)79return;8081if (!r->scissor_valid) {82r->scissor.minx = 0;83r->scissor.miny = 0;84r->scissor.maxx = r->dst->tex->width0;85r->scissor.maxy = r->dst->tex->height0;86}8788r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor);8990struct cso_velems_state velems;91velems.count = r->attrs_per_vertex;92memcpy(velems.velems, r->velems, sizeof(r->velems[0]) * velems.count);9394cso_set_vertex_elements(r->cso, &velems);95util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,96num_verts, /* verts */97r->attrs_per_vertex); /* attribs/vert */98r->buffer_size = 0;99100xa_scissor_reset(r);101}102103static inline void104renderer_draw_conditional(struct xa_context *r, int next_batch)105{106if (r->buffer_size + next_batch >= XA_VB_SIZE ||107(next_batch == 0 && r->buffer_size)) {108renderer_draw(r);109}110}111112void113renderer_init_state(struct xa_context *r)114{115struct pipe_depth_stencil_alpha_state dsa;116struct pipe_rasterizer_state raster;117unsigned i;118119/* set common initial clip state */120memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));121cso_set_depth_stencil_alpha(r->cso, &dsa);122123/* XXX: move to renderer_init_state? */124memset(&raster, 0, sizeof(struct pipe_rasterizer_state));125raster.half_pixel_center = 1;126raster.bottom_edge_rule = 1;127raster.depth_clip_near = 1;128raster.depth_clip_far = 1;129raster.scissor = 1;130cso_set_rasterizer(r->cso, &raster);131132/* vertex elements state */133memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3);134for (i = 0; i < 3; i++) {135r->velems[i].src_offset = i * 4 * sizeof(float);136r->velems[i].instance_divisor = 0;137r->velems[i].vertex_buffer_index = 0;138r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;139}140}141142static inline void143add_vertex_none(struct xa_context *r, float x, float y)144{145float *vertex = r->buffer + r->buffer_size;146147vertex[0] = x;148vertex[1] = y;149vertex[2] = 0.f; /*z */150vertex[3] = 1.f; /*w */151152r->buffer_size += 4;153}154155static inline void156add_vertex_1tex(struct xa_context *r, float x, float y, float s, float t)157{158float *vertex = r->buffer + r->buffer_size;159160vertex[0] = x;161vertex[1] = y;162vertex[2] = 0.f; /*z */163vertex[3] = 1.f; /*w */164165vertex[4] = s; /*s */166vertex[5] = t; /*t */167vertex[6] = 0.f; /*r */168vertex[7] = 1.f; /*q */169170r->buffer_size += 8;171}172173static inline void174add_vertex_2tex(struct xa_context *r,175float x, float y, float s0, float t0, float s1, float t1)176{177float *vertex = r->buffer + r->buffer_size;178179vertex[0] = x;180vertex[1] = y;181vertex[2] = 0.f; /*z */182vertex[3] = 1.f; /*w */183184vertex[4] = s0; /*s */185vertex[5] = t0; /*t */186vertex[6] = 0.f; /*r */187vertex[7] = 1.f; /*q */188189vertex[8] = s1; /*s */190vertex[9] = t1; /*t */191vertex[10] = 0.f; /*r */192vertex[11] = 1.f; /*q */193194r->buffer_size += 12;195}196197static void198compute_src_coords(float sx, float sy, const struct pipe_resource *src,199const float *src_matrix,200float width, float height,201float tc0[2], float tc1[2], float tc2[2], float tc3[2])202{203tc0[0] = sx;204tc0[1] = sy;205tc1[0] = sx + width;206tc1[1] = sy;207tc2[0] = sx + width;208tc2[1] = sy + height;209tc3[0] = sx;210tc3[1] = sy + height;211212if (src_matrix) {213map_point(src_matrix, tc0[0], tc0[1], &tc0[0], &tc0[1]);214map_point(src_matrix, tc1[0], tc1[1], &tc1[0], &tc1[1]);215map_point(src_matrix, tc2[0], tc2[1], &tc2[0], &tc2[1]);216map_point(src_matrix, tc3[0], tc3[1], &tc3[0], &tc3[1]);217}218219tc0[0] /= src->width0;220tc1[0] /= src->width0;221tc2[0] /= src->width0;222tc3[0] /= src->width0;223tc0[1] /= src->height0;224tc1[1] /= src->height0;225tc2[1] /= src->height0;226tc3[1] /= src->height0;227}228229static void230add_vertex_data1(struct xa_context *r,231float srcX, float srcY, float dstX, float dstY,232float width, float height,233const struct pipe_resource *src, const float *src_matrix)234{235float tc0[2], tc1[2], tc2[2], tc3[2];236237compute_src_coords(srcX, srcY, src, src_matrix, width, height,238tc0, tc1, tc2, tc3);239/* 1st vertex */240add_vertex_1tex(r, dstX, dstY, tc0[0], tc0[1]);241/* 2nd vertex */242add_vertex_1tex(r, dstX + width, dstY, tc1[0], tc1[1]);243/* 3rd vertex */244add_vertex_1tex(r, dstX + width, dstY + height, tc2[0], tc2[1]);245/* 4th vertex */246add_vertex_1tex(r, dstX, dstY + height, tc3[0], tc3[1]);247}248249static void250add_vertex_data2(struct xa_context *r,251float srcX, float srcY, float maskX, float maskY,252float dstX, float dstY, float width, float height,253struct pipe_resource *src,254struct pipe_resource *mask,255const float *src_matrix, const float *mask_matrix)256{257float spt0[2], spt1[2], spt2[2], spt3[2];258float mpt0[2], mpt1[2], mpt2[2], mpt3[2];259260compute_src_coords(srcX, srcY, src, src_matrix, width, height,261spt0, spt1, spt2, spt3);262compute_src_coords(maskX, maskY, mask, mask_matrix, width, height,263mpt0, mpt1, mpt2, mpt3);264265/* 1st vertex */266add_vertex_2tex(r, dstX, dstY,267spt0[0], spt0[1], mpt0[0], mpt0[1]);268/* 2nd vertex */269add_vertex_2tex(r, dstX + width, dstY,270spt1[0], spt1[1], mpt1[0], mpt1[1]);271/* 3rd vertex */272add_vertex_2tex(r, dstX + width, dstY + height,273spt2[0], spt2[1], mpt2[0], mpt2[1]);274/* 4th vertex */275add_vertex_2tex(r, dstX, dstY + height,276spt3[0], spt3[1], mpt3[0], mpt3[1]);277}278279static void280setup_vertex_data_yuv(struct xa_context *r,281float srcX,282float srcY,283float srcW,284float srcH,285float dstX,286float dstY,287float dstW, float dstH, struct xa_surface *srf[])288{289float s0, t0, s1, t1;290float spt0[2], spt1[2];291struct pipe_resource *tex;292293spt0[0] = srcX;294spt0[1] = srcY;295spt1[0] = srcX + srcW;296spt1[1] = srcY + srcH;297298tex = srf[0]->tex;299s0 = spt0[0] / tex->width0;300t0 = spt0[1] / tex->height0;301s1 = spt1[0] / tex->width0;302t1 = spt1[1] / tex->height0;303304/* 1st vertex */305add_vertex_1tex(r, dstX, dstY, s0, t0);306/* 2nd vertex */307add_vertex_1tex(r, dstX + dstW, dstY, s1, t0);308/* 3rd vertex */309add_vertex_1tex(r, dstX + dstW, dstY + dstH, s1, t1);310/* 4th vertex */311add_vertex_1tex(r, dstX, dstY + dstH, s0, t1);312}313314/* Set up framebuffer, viewport and vertex shader constant buffer315* state for a particular destinaton surface. In all our rendering,316* these concepts are linked.317*/318void319renderer_bind_destination(struct xa_context *r,320struct pipe_surface *surface)321{322int width = surface->width;323int height = surface->height;324325struct pipe_framebuffer_state fb;326struct pipe_viewport_state viewport;327328xa_scissor_reset(r);329330/* Framebuffer uses actual surface width/height331*/332memset(&fb, 0, sizeof fb);333fb.width = surface->width;334fb.height = surface->height;335fb.nr_cbufs = 1;336fb.cbufs[0] = surface;337fb.zsbuf = 0;338339/* Viewport just touches the bit we're interested in:340*/341viewport.scale[0] = width / 2.f;342viewport.scale[1] = height / 2.f;343viewport.scale[2] = 1.0;344viewport.translate[0] = width / 2.f;345viewport.translate[1] = height / 2.f;346viewport.translate[2] = 0.0;347viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;348viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;349viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;350viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;351352/* Constant buffer set up to match viewport dimensions:353*/354if (r->fb_width != width || r->fb_height != height) {355float vs_consts[8] = {3562.f / width, 2.f / height, 1, 1,357-1, -1, 0, 0358};359360r->fb_width = width;361r->fb_height = height;362363renderer_set_constants(r, PIPE_SHADER_VERTEX,364vs_consts, sizeof vs_consts);365}366367cso_set_framebuffer(r->cso, &fb);368cso_set_viewport(r->cso, &viewport);369}370371void372renderer_set_constants(struct xa_context *r,373int shader_type, const float *params, int param_bytes)374{375struct pipe_resource **cbuf =376(shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :377&r->fs_const_buffer;378379pipe_resource_reference(cbuf, NULL);380*cbuf = pipe_buffer_create_const0(r->pipe->screen,381PIPE_BIND_CONSTANT_BUFFER,382PIPE_USAGE_DEFAULT,383param_bytes);384385if (*cbuf) {386pipe_buffer_write(r->pipe, *cbuf, 0, param_bytes, params);387}388pipe_set_constant_buffer(r->pipe, shader_type, 0, *cbuf);389}390391void392renderer_copy_prepare(struct xa_context *r,393struct pipe_surface *dst_surface,394struct pipe_resource *src_texture,395const enum xa_formats src_xa_format,396const enum xa_formats dst_xa_format)397{398struct pipe_context *pipe = r->pipe;399struct pipe_screen *screen = pipe->screen;400struct xa_shader shader;401uint32_t fs_traits = FS_COMPOSITE;402403assert(screen->is_format_supported(screen, dst_surface->format,404PIPE_TEXTURE_2D, 0, 0,405PIPE_BIND_RENDER_TARGET));406(void)screen;407408renderer_bind_destination(r, dst_surface);409410/* set misc state we care about */411{412struct pipe_blend_state blend;413414memset(&blend, 0, sizeof(blend));415blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;416blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;417blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;418blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;419blend.rt[0].colormask = PIPE_MASK_RGBA;420cso_set_blend(r->cso, &blend);421}422423/* sampler */424{425struct pipe_sampler_state sampler;426const struct pipe_sampler_state *p_sampler = &sampler;427428memset(&sampler, 0, sizeof(sampler));429sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;430sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;431sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;432sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;433sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;434sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;435sampler.normalized_coords = 1;436cso_set_samplers(r->cso, PIPE_SHADER_FRAGMENT, 1, &p_sampler);437r->num_bound_samplers = 1;438}439440/* texture/sampler view */441{442struct pipe_sampler_view templ;443struct pipe_sampler_view *src_view;444445u_sampler_view_default_template(&templ,446src_texture, src_texture->format);447src_view = pipe->create_sampler_view(pipe, src_texture, &templ);448pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, &src_view);449pipe_sampler_view_reference(&src_view, NULL);450}451452/* shaders */453if (src_texture->format == PIPE_FORMAT_L8_UNORM ||454src_texture->format == PIPE_FORMAT_R8_UNORM)455fs_traits |= FS_SRC_LUMINANCE;456if (dst_surface->format == PIPE_FORMAT_L8_UNORM ||457dst_surface->format == PIPE_FORMAT_R8_UNORM)458fs_traits |= FS_DST_LUMINANCE;459if (xa_format_a(dst_xa_format) != 0 &&460xa_format_a(src_xa_format) == 0)461fs_traits |= FS_SRC_SET_ALPHA;462463shader = xa_shaders_get(r->shaders, VS_COMPOSITE, fs_traits);464cso_set_vertex_shader_handle(r->cso, shader.vs);465cso_set_fragment_shader_handle(r->cso, shader.fs);466467r->buffer_size = 0;468r->attrs_per_vertex = 2;469}470471void472renderer_copy(struct xa_context *r,473int dx,474int dy,475int sx,476int sy,477int width, int height, float src_width, float src_height)478{479float s0, t0, s1, t1;480float x0, y0, x1, y1;481482/* XXX: could put the texcoord scaling calculation into the vertex483* shader.484*/485s0 = sx / src_width;486s1 = (sx + width) / src_width;487t0 = sy / src_height;488t1 = (sy + height) / src_height;489490x0 = dx;491x1 = dx + width;492y0 = dy;493y1 = dy + height;494495/* draw quad */496renderer_draw_conditional(r, 4 * 8);497add_vertex_1tex(r, x0, y0, s0, t0);498add_vertex_1tex(r, x1, y0, s1, t0);499add_vertex_1tex(r, x1, y1, s1, t1);500add_vertex_1tex(r, x0, y1, s0, t1);501}502503void504renderer_draw_yuv(struct xa_context *r,505float src_x,506float src_y,507float src_w,508float src_h,509int dst_x,510int dst_y, int dst_w, int dst_h, struct xa_surface *srf[])511{512const int num_attribs = 2; /*pos + tex coord */513514setup_vertex_data_yuv(r,515src_x, src_y, src_w, src_h,516dst_x, dst_y, dst_w, dst_h, srf);517518if (!r->scissor_valid) {519r->scissor.minx = 0;520r->scissor.miny = 0;521r->scissor.maxx = r->dst->tex->width0;522r->scissor.maxy = r->dst->tex->height0;523}524525r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor);526527struct cso_velems_state velems;528velems.count = num_attribs;529memcpy(velems.velems, r->velems, sizeof(r->velems[0]) * velems.count);530531cso_set_vertex_elements(r->cso, &velems);532util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,5334, /* verts */534num_attribs); /* attribs/vert */535r->buffer_size = 0;536537xa_scissor_reset(r);538}539540void541renderer_begin_solid(struct xa_context *r)542{543r->buffer_size = 0;544r->attrs_per_vertex = 1;545renderer_set_constants(r, PIPE_SHADER_FRAGMENT, r->solid_color,5464 * sizeof(float));547}548549void550renderer_solid(struct xa_context *r,551int x0, int y0, int x1, int y1)552{553/*554* debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",555* x0, y0, x1, y1, color[0], color[1], color[2], color[3]); */556557renderer_draw_conditional(r, 4 * 4);558559/* 1st vertex */560add_vertex_none(r, x0, y0);561/* 2nd vertex */562add_vertex_none(r, x1, y0);563/* 3rd vertex */564add_vertex_none(r, x1, y1);565/* 4th vertex */566add_vertex_none(r, x0, y1);567}568569void570renderer_draw_flush(struct xa_context *r)571{572renderer_draw_conditional(r, 0);573}574575void576renderer_begin_textures(struct xa_context *r)577{578r->attrs_per_vertex = 1 + r->num_bound_samplers;579r->buffer_size = 0;580if (r->has_solid_src || r->has_solid_mask)581renderer_set_constants(r, PIPE_SHADER_FRAGMENT, r->solid_color,5824 * sizeof(float));583}584585void586renderer_texture(struct xa_context *r,587int *pos,588int width, int height,589const float *src_matrix,590const float *mask_matrix)591{592struct pipe_sampler_view **sampler_view = r->bound_sampler_views;593594#if 0595if (src_matrix) {596debug_printf("src_matrix = \n");597debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);598debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);599debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);600}601if (mask_matrix) {602debug_printf("mask_matrix = \n");603debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);604debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);605debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);606}607#endif608609switch(r->attrs_per_vertex) {610case 2:611renderer_draw_conditional(r, 4 * 8);612if (!r->has_solid_src) {613add_vertex_data1(r,614pos[0], pos[1], /* src */615pos[4], pos[5], /* dst */616width, height,617sampler_view[0]->texture, src_matrix);618} else {619add_vertex_data1(r,620pos[2], pos[3], /* mask */621pos[4], pos[5], /* dst */622width, height,623sampler_view[0]->texture, mask_matrix);624}625break;626case 3:627renderer_draw_conditional(r, 4 * 12);628add_vertex_data2(r,629pos[0], pos[1], /* src */630pos[2], pos[3], /* mask */631pos[4], pos[5], /* dst */632width, height,633sampler_view[0]->texture, sampler_view[1]->texture,634src_matrix, mask_matrix);635break;636default:637break;638}639}640641642