Path: blob/21.2-virgl/src/gallium/drivers/r300/compiler/r300_fragprog_swizzle.c
4574 views
/*1* Copyright (C) 2008 Nicolai Haehnle.2*3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining6* a 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, sublicense, 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 substantial15* portions of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,18* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE21* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION22* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION23* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25*/2627/**28* @file29* Utilities to deal with the somewhat odd restriction on R300 fragment30* program swizzles.31*/3233#include "r300_fragprog_swizzle.h"3435#include <stdio.h>3637#include "util/macros.h"3839#include "r300_reg.h"40#include "radeon_compiler.h"4142#define MAKE_SWZ3(x, y, z) (RC_MAKE_SWIZZLE(RC_SWIZZLE_##x, RC_SWIZZLE_##y, RC_SWIZZLE_##z, RC_SWIZZLE_ZERO))4344struct swizzle_data {45unsigned int hash; /**< swizzle value this matches */46unsigned int base; /**< base value for hw swizzle */47unsigned int stride; /**< difference in base between arg0/1/2 */48unsigned int srcp_stride; /**< difference in base between arg0/scrp */49};5051static const struct swizzle_data native_swizzles[] = {52{MAKE_SWZ3(X, Y, Z), R300_ALU_ARGC_SRC0C_XYZ, 4, 15},53{MAKE_SWZ3(X, X, X), R300_ALU_ARGC_SRC0C_XXX, 4, 15},54{MAKE_SWZ3(Y, Y, Y), R300_ALU_ARGC_SRC0C_YYY, 4, 15},55{MAKE_SWZ3(Z, Z, Z), R300_ALU_ARGC_SRC0C_ZZZ, 4, 15},56{MAKE_SWZ3(W, W, W), R300_ALU_ARGC_SRC0A, 1, 7},57{MAKE_SWZ3(Y, Z, X), R300_ALU_ARGC_SRC0C_YZX, 1, 0},58{MAKE_SWZ3(Z, X, Y), R300_ALU_ARGC_SRC0C_ZXY, 1, 0},59{MAKE_SWZ3(W, Z, Y), R300_ALU_ARGC_SRC0CA_WZY, 1, 0},60{MAKE_SWZ3(ONE, ONE, ONE), R300_ALU_ARGC_ONE, 0, 0},61{MAKE_SWZ3(ZERO, ZERO, ZERO), R300_ALU_ARGC_ZERO, 0, 0},62{MAKE_SWZ3(HALF, HALF, HALF), R300_ALU_ARGC_HALF, 0, 0}63};6465static const int num_native_swizzles = ARRAY_SIZE(native_swizzles);6667/**68* Find a native RGB swizzle that matches the given swizzle.69* Returns 0 if none found.70*/71static const struct swizzle_data* lookup_native_swizzle(unsigned int swizzle)72{73int i, comp;7475for(i = 0; i < num_native_swizzles; ++i) {76const struct swizzle_data* sd = &native_swizzles[i];77for(comp = 0; comp < 3; ++comp) {78unsigned int swz = GET_SWZ(swizzle, comp);79if (swz == RC_SWIZZLE_UNUSED)80continue;81if (swz != GET_SWZ(sd->hash, comp))82break;83}84if (comp == 3)85return sd;86}8788return 0;89}9091/**92* Determines if the given swizzle is valid for r300/r400. In most situations93* it is better to use r300_swizzle_is_native() which can be accessed via94* struct radeon_compiler *c; c->SwizzleCaps->IsNative().95*/96int r300_swizzle_is_native_basic(unsigned int swizzle)97{98if(lookup_native_swizzle(swizzle))99return 1;100else101return 0;102}103104/**105* Check whether the given instruction supports the swizzle and negate106* combinations in the given source register.107*/108static int r300_swizzle_is_native(rc_opcode opcode, struct rc_src_register reg)109{110const struct swizzle_data* sd;111unsigned int relevant;112int j;113114if (opcode == RC_OPCODE_KIL ||115opcode == RC_OPCODE_TEX ||116opcode == RC_OPCODE_TXB ||117opcode == RC_OPCODE_TXP) {118if (reg.Abs || reg.Negate)119return 0;120121for(j = 0; j < 4; ++j) {122unsigned int swz = GET_SWZ(reg.Swizzle, j);123if (swz == RC_SWIZZLE_UNUSED)124continue;125if (swz != j)126return 0;127}128129return 1;130}131132relevant = 0;133134for(j = 0; j < 3; ++j)135if (GET_SWZ(reg.Swizzle, j) != RC_SWIZZLE_UNUSED)136relevant |= 1 << j;137138if ((reg.Negate & relevant) && ((reg.Negate & relevant) != relevant))139return 0;140141sd = lookup_native_swizzle(reg.Swizzle);142if (!sd || (reg.File == RC_FILE_PRESUB && sd->srcp_stride == 0))143return 0;144145return 1;146}147148149static void r300_swizzle_split(150struct rc_src_register src, unsigned int mask,151struct rc_swizzle_split * split)152{153split->NumPhases = 0;154155while(mask) {156unsigned int best_matchcount = 0;157unsigned int best_matchmask = 0;158int i, comp;159160for(i = 0; i < num_native_swizzles; ++i) {161const struct swizzle_data *sd = &native_swizzles[i];162unsigned int matchcount = 0;163unsigned int matchmask = 0;164for(comp = 0; comp < 3; ++comp) {165unsigned int swz;166if (!GET_BIT(mask, comp))167continue;168swz = GET_SWZ(src.Swizzle, comp);169if (swz == RC_SWIZZLE_UNUSED)170continue;171if (swz == GET_SWZ(sd->hash, comp)) {172/* check if the negate bit of current component173* is the same for already matched components */174if (matchmask && (!!(src.Negate & matchmask) != !!(src.Negate & (1 << comp))))175continue;176177matchcount++;178matchmask |= 1 << comp;179}180}181if (matchcount > best_matchcount) {182best_matchcount = matchcount;183best_matchmask = matchmask;184if (matchmask == (mask & RC_MASK_XYZ))185break;186}187}188189if (mask & RC_MASK_W)190best_matchmask |= RC_MASK_W;191192split->Phase[split->NumPhases++] = best_matchmask;193mask &= ~best_matchmask;194}195}196197struct rc_swizzle_caps r300_swizzle_caps = {198.IsNative = r300_swizzle_is_native,199.Split = r300_swizzle_split200};201202203/**204* Translate an RGB (XYZ) swizzle into the hardware code for the given205* instruction source.206*/207unsigned int r300FPTranslateRGBSwizzle(unsigned int src, unsigned int swizzle)208{209const struct swizzle_data* sd = lookup_native_swizzle(swizzle);210211if (!sd || (src == RC_PAIR_PRESUB_SRC && sd->srcp_stride == 0)) {212fprintf(stderr, "Not a native swizzle: %08x\n", swizzle);213return 0;214}215216if (src == RC_PAIR_PRESUB_SRC) {217return sd->base + sd->srcp_stride;218} else {219return sd->base + src*sd->stride;220}221}222223224/**225* Translate an Alpha (W) swizzle into the hardware code for the given226* instruction source.227*/228unsigned int r300FPTranslateAlphaSwizzle(unsigned int src, unsigned int swizzle)229{230unsigned int swz = GET_SWZ(swizzle, 0);231if (src == RC_PAIR_PRESUB_SRC) {232return R300_ALU_ARGA_SRCP_X + swz;233}234if (swz < 3)235return swz + 3*src;236237switch(swz) {238case RC_SWIZZLE_W: return R300_ALU_ARGA_SRC0A + src;239case RC_SWIZZLE_ONE: return R300_ALU_ARGA_ONE;240case RC_SWIZZLE_ZERO: return R300_ALU_ARGA_ZERO;241case RC_SWIZZLE_HALF: return R300_ALU_ARGA_HALF;242default: return R300_ALU_ARGA_ONE;243}244}245246247