Path: blob/21.2-virgl/src/gallium/drivers/llvmpipe/lp_rast_tri_tmp.h
4570 views
/**************************************************************************1*2* Copyright 2007-2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/*28* Rasterization for binned triangles within a tile29*/30313233/**34* Prototype for a 8 plane rasterizer function. Will codegenerate35* several of these.36*37* XXX: Varients for more/fewer planes.38* XXX: Need ways of dropping planes as we descend.39* XXX: SIMD40*/41static void42TAG(do_block_4)(struct lp_rasterizer_task *task,43const struct lp_rast_triangle *tri,44const struct lp_rast_plane *plane,45int x, int y,46const int64_t *c)47{48int j;49#ifndef MULTISAMPLE50unsigned mask = 0xffff;51#else52uint64_t mask = UINT64_MAX;53#endif5455for (j = 0; j < NR_PLANES; j++) {56#ifndef MULTISAMPLE57#ifdef RASTER_6458mask &= ~BUILD_MASK_LINEAR(((c[j] - 1) >> (int64_t)FIXED_ORDER),59-plane[j].dcdx >> FIXED_ORDER,60plane[j].dcdy >> FIXED_ORDER);61#else62mask &= ~BUILD_MASK_LINEAR((c[j] - 1),63-plane[j].dcdx,64plane[j].dcdy);65#endif66#else67for (unsigned s = 0; s < 4; s++) {68int64_t new_c = (c[j]) + ((IMUL64(task->scene->fixed_sample_pos[s][1], plane[j].dcdy) + IMUL64(task->scene->fixed_sample_pos[s][0], -plane[j].dcdx)) >> FIXED_ORDER);69uint32_t build_mask;70#ifdef RASTER_6471build_mask = BUILD_MASK_LINEAR((int32_t)((new_c - 1) >> (int64_t)FIXED_ORDER),72-plane[j].dcdx >> FIXED_ORDER,73plane[j].dcdy >> FIXED_ORDER);74#else75build_mask = BUILD_MASK_LINEAR((new_c - 1),76-plane[j].dcdx,77plane[j].dcdy);78#endif79mask &= ~((uint64_t)build_mask << (s * 16));80}81#endif82}8384/* Now pass to the shader:85*/86if (mask)87lp_rast_shade_quads_mask_sample(task, &tri->inputs, x, y, mask);88}8990/**91* Evaluate a 16x16 block of pixels to determine which 4x4 subblocks are in/out92* of the triangle's bounds.93*/94static void95TAG(do_block_16)(struct lp_rasterizer_task *task,96const struct lp_rast_triangle *tri,97const struct lp_rast_plane *plane,98int x, int y,99const int64_t *c)100{101unsigned outmask, inmask, partmask, partial_mask;102unsigned j;103104outmask = 0; /* outside one or more trivial reject planes */105partmask = 0; /* outside one or more trivial accept planes */106107for (j = 0; j < NR_PLANES; j++) {108#ifdef RASTER_64109int32_t dcdx = -plane[j].dcdx >> FIXED_ORDER;110int32_t dcdy = plane[j].dcdy >> FIXED_ORDER;111const int32_t cox = plane[j].eo >> FIXED_ORDER;112const int32_t ei = (dcdy + dcdx - cox) << 2;113const int32_t cox_s = cox << 2;114const int32_t co = (int32_t)(c[j] >> (int64_t)FIXED_ORDER) + cox_s;115int32_t cdiff;116cdiff = ei - cox_s + ((int32_t)((c[j] - 1) >> (int64_t)FIXED_ORDER) -117(int32_t)(c[j] >> (int64_t)FIXED_ORDER));118dcdx <<= 2;119dcdy <<= 2;120#else121const int64_t dcdx = -IMUL64(plane[j].dcdx, 4);122const int64_t dcdy = IMUL64(plane[j].dcdy, 4);123const int64_t cox = IMUL64(plane[j].eo, 4);124const int32_t ei = plane[j].dcdy - plane[j].dcdx - (int64_t)plane[j].eo;125const int64_t cio = IMUL64(ei, 4) - 1;126int32_t co, cdiff;127co = c[j] + cox;128cdiff = cio - cox;129#endif130131BUILD_MASKS(co, cdiff,132dcdx, dcdy,133&outmask, /* sign bits from c[i][0..15] + cox */134&partmask); /* sign bits from c[i][0..15] + cio */135}136137if (outmask == 0xffff)138return;139140/* Mask of sub-blocks which are inside all trivial accept planes:141*/142inmask = ~partmask & 0xffff;143144/* Mask of sub-blocks which are inside all trivial reject planes,145* but outside at least one trivial accept plane:146*/147partial_mask = partmask & ~outmask;148149assert((partial_mask & inmask) == 0);150151LP_COUNT_ADD(nr_empty_4, util_bitcount(0xffff & ~(partial_mask | inmask)));152153/* Iterate over partials:154*/155while (partial_mask) {156int i = ffs(partial_mask) - 1;157int ix = (i & 3) * 4;158int iy = (i >> 2) * 4;159int px = x + ix;160int py = y + iy;161int64_t cx[NR_PLANES];162163partial_mask &= ~(1 << i);164165LP_COUNT(nr_partially_covered_4);166167for (j = 0; j < NR_PLANES; j++)168cx[j] = (c[j]169- IMUL64(plane[j].dcdx, ix)170+ IMUL64(plane[j].dcdy, iy));171172TAG(do_block_4)(task, tri, plane, px, py, cx);173}174175/* Iterate over fulls:176*/177while (inmask) {178int i = ffs(inmask) - 1;179int ix = (i & 3) * 4;180int iy = (i >> 2) * 4;181int px = x + ix;182int py = y + iy;183184inmask &= ~(1 << i);185186LP_COUNT(nr_fully_covered_4);187block_full_4(task, tri, px, py);188}189}190191192/**193* Scan the tile in chunks and figure out which pixels to rasterize194* for this triangle.195*/196void197TAG(lp_rast_triangle)(struct lp_rasterizer_task *task,198const union lp_rast_cmd_arg arg)199{200const struct lp_rast_triangle *tri = arg.triangle.tri;201unsigned plane_mask = arg.triangle.plane_mask;202const struct lp_rast_plane *tri_plane = GET_PLANES(tri);203const int x = task->x, y = task->y;204struct lp_rast_plane plane[NR_PLANES];205int64_t c[NR_PLANES];206unsigned outmask, inmask, partmask, partial_mask;207unsigned j = 0;208209if (tri->inputs.disable) {210/* This triangle was partially binned and has been disabled */211return;212}213214outmask = 0; /* outside one or more trivial reject planes */215partmask = 0; /* outside one or more trivial accept planes */216217while (plane_mask) {218int i = ffs(plane_mask) - 1;219plane[j] = tri_plane[i];220plane_mask &= ~(1 << i);221c[j] = plane[j].c + IMUL64(plane[j].dcdy, y) - IMUL64(plane[j].dcdx, x);222223{224#ifdef RASTER_64225/*226* Strip off lower FIXED_ORDER bits. Note that those bits from227* dcdx, dcdy, eo are always 0 (by definition).228* c values, however, are not. This means that for every229* addition of the form c + n*dcdx the lower FIXED_ORDER bits will230* NOT change. And those bits are not relevant to the sign bit (which231* is only what we need!) that is,232* sign(c + n*dcdx) == sign((c >> FIXED_ORDER) + n*(dcdx >> FIXED_ORDER))233* This means we can get away with using 32bit math for the most part.234* Only tricky part is the -1 adjustment for cdiff.235*/236int32_t dcdx = -plane[j].dcdx >> FIXED_ORDER;237int32_t dcdy = plane[j].dcdy >> FIXED_ORDER;238const int32_t cox = plane[j].eo >> FIXED_ORDER;239const int32_t ei = (dcdy + dcdx - cox) << 4;240const int32_t cox_s = cox << 4;241const int32_t co = (int32_t)(c[j] >> (int64_t)FIXED_ORDER) + cox_s;242int32_t cdiff;243/*244* Plausibility check to ensure the 32bit math works.245* Note that within a tile, the max we can move the edge function246* is essentially dcdx * TILE_SIZE + dcdy * TILE_SIZE.247* TILE_SIZE is 64, dcdx/dcdy are nominally 21 bit (for 8192 max size248* and 8 subpixel bits), I'd be happy with 2 bits more too (1 for249* increasing fb size to 16384, the required d3d11 value, another one250* because I'm not quite sure we can't be _just_ above the max value251* here). This gives us 30 bits max - hence if c would exceed that here252* that means the plane is either trivial reject for the whole tile253* (in which case the tri will not get binned), or trivial accept for254* the whole tile (in which case plane_mask will not include it).255*/256assert((c[j] >> (int64_t)FIXED_ORDER) > (int32_t)0xb0000000 &&257(c[j] >> (int64_t)FIXED_ORDER) < (int32_t)0x3fffffff);258/*259* Note the fixup part is constant throughout the tile - thus could260* just calculate this and avoid _all_ 64bit math in rasterization261* (except exactly this fixup calc).262* In fact theoretically could move that even to setup, albeit that263* seems tricky (pre-bin certainly can have values larger than 32bit,264* and would need to communicate that fixup value through).265* And if we want to support msaa, we'd probably don't want to do the266* downscaling in setup in any case...267*/268cdiff = ei - cox_s + ((int32_t)((c[j] - 1) >> (int64_t)FIXED_ORDER) -269(int32_t)(c[j] >> (int64_t)FIXED_ORDER));270dcdx <<= 4;271dcdy <<= 4;272#else273const int32_t dcdx = -plane[j].dcdx << 4;274const int32_t dcdy = plane[j].dcdy << 4;275const int32_t cox = plane[j].eo << 4;276const int32_t ei = plane[j].dcdy - plane[j].dcdx - (int32_t)plane[j].eo;277const int32_t cio = (ei << 4) - 1;278int32_t co, cdiff;279co = c[j] + cox;280cdiff = cio - cox;281#endif282BUILD_MASKS(co, cdiff,283dcdx, dcdy,284&outmask, /* sign bits from c[i][0..15] + cox */285&partmask); /* sign bits from c[i][0..15] + cio */286}287288j++;289}290291if (outmask == 0xffff)292return;293294/* Mask of sub-blocks which are inside all trivial accept planes:295*/296inmask = ~partmask & 0xffff;297298/* Mask of sub-blocks which are inside all trivial reject planes,299* but outside at least one trivial accept plane:300*/301partial_mask = partmask & ~outmask;302303assert((partial_mask & inmask) == 0);304305LP_COUNT_ADD(nr_empty_16, util_bitcount(0xffff & ~(partial_mask | inmask)));306307/* Iterate over partials:308*/309while (partial_mask) {310int i = ffs(partial_mask) - 1;311int ix = (i & 3) * 16;312int iy = (i >> 2) * 16;313int px = x + ix;314int py = y + iy;315int64_t cx[NR_PLANES];316317for (j = 0; j < NR_PLANES; j++)318cx[j] = (c[j]319- IMUL64(plane[j].dcdx, ix)320+ IMUL64(plane[j].dcdy, iy));321322partial_mask &= ~(1 << i);323324LP_COUNT(nr_partially_covered_16);325TAG(do_block_16)(task, tri, plane, px, py, cx);326}327328/* Iterate over fulls:329*/330while (inmask) {331int i = ffs(inmask) - 1;332int ix = (i & 3) * 16;333int iy = (i >> 2) * 16;334int px = x + ix;335int py = y + iy;336337inmask &= ~(1 << i);338339LP_COUNT(nr_fully_covered_16);340block_full_16(task, tri, px, py);341}342}343344#if defined(PIPE_ARCH_SSE) && defined(TRI_16)345/* XXX: special case this when intersection is not required.346* - tile completely within bbox,347* - bbox completely within tile.348*/349void350TRI_16(struct lp_rasterizer_task *task,351const union lp_rast_cmd_arg arg)352{353const struct lp_rast_triangle *tri = arg.triangle.tri;354const struct lp_rast_plane *plane = GET_PLANES(tri);355unsigned mask = arg.triangle.plane_mask;356unsigned outmask, partial_mask;357unsigned j;358__m128i cstep4[NR_PLANES][4];359360int x = (mask & 0xff);361int y = (mask >> 8);362363outmask = 0; /* outside one or more trivial reject planes */364365x += task->x;366y += task->y;367368for (j = 0; j < NR_PLANES; j++) {369const int dcdx = -plane[j].dcdx * 4;370const int dcdy = plane[j].dcdy * 4;371__m128i xdcdy = _mm_set1_epi32(dcdy);372373cstep4[j][0] = _mm_setr_epi32(0, dcdx, dcdx*2, dcdx*3);374cstep4[j][1] = _mm_add_epi32(cstep4[j][0], xdcdy);375cstep4[j][2] = _mm_add_epi32(cstep4[j][1], xdcdy);376cstep4[j][3] = _mm_add_epi32(cstep4[j][2], xdcdy);377378{379const int c = plane[j].c + plane[j].dcdy * y - plane[j].dcdx * x;380const int cox = plane[j].eo * 4;381382outmask |= sign_bits4(cstep4[j], c + cox);383}384}385386if (outmask == 0xffff)387return;388389390/* Mask of sub-blocks which are inside all trivial reject planes,391* but outside at least one trivial accept plane:392*/393partial_mask = 0xffff & ~outmask;394395/* Iterate over partials:396*/397while (partial_mask) {398int i = ffs(partial_mask) - 1;399int ix = (i & 3) * 4;400int iy = (i >> 2) * 4;401int px = x + ix;402int py = y + iy;403unsigned mask = 0xffff;404405partial_mask &= ~(1 << i);406407for (j = 0; j < NR_PLANES; j++) {408const int cx = (plane[j].c - 1409- plane[j].dcdx * px410+ plane[j].dcdy * py) * 4;411412mask &= ~sign_bits4(cstep4[j], cx);413}414415if (mask)416lp_rast_shade_quads_mask(task, &tri->inputs, px, py, mask);417}418}419#endif420421#if defined(PIPE_ARCH_SSE) && defined(TRI_4)422void423TRI_4(struct lp_rasterizer_task *task,424const union lp_rast_cmd_arg arg)425{426const struct lp_rast_triangle *tri = arg.triangle.tri;427const struct lp_rast_plane *plane = GET_PLANES(tri);428unsigned mask = arg.triangle.plane_mask;429const int x = task->x + (mask & 0xff);430const int y = task->y + (mask >> 8);431unsigned j;432433/* Iterate over partials:434*/435{436unsigned mask = 0xffff;437438for (j = 0; j < NR_PLANES; j++) {439const int cx = (plane[j].c440- plane[j].dcdx * x441+ plane[j].dcdy * y);442443const int dcdx = -plane[j].dcdx;444const int dcdy = plane[j].dcdy;445__m128i xdcdy = _mm_set1_epi32(dcdy);446447__m128i cstep0 = _mm_setr_epi32(cx, cx + dcdx, cx + dcdx*2, cx + dcdx*3);448__m128i cstep1 = _mm_add_epi32(cstep0, xdcdy);449__m128i cstep2 = _mm_add_epi32(cstep1, xdcdy);450__m128i cstep3 = _mm_add_epi32(cstep2, xdcdy);451452__m128i cstep01 = _mm_packs_epi32(cstep0, cstep1);453__m128i cstep23 = _mm_packs_epi32(cstep2, cstep3);454__m128i result = _mm_packs_epi16(cstep01, cstep23);455456/* Extract the sign bits457*/458mask &= ~_mm_movemask_epi8(result);459}460461if (mask)462lp_rast_shade_quads_mask(task, &tri->inputs, x, y, mask);463}464}465#endif466467468469#undef TAG470#undef TRI_4471#undef TRI_16472#undef NR_PLANES473474475476