Path: blob/21.2-virgl/src/gallium/drivers/r300/compiler/radeon_program_pair.c
4574 views
/*1* Copyright (C) 2008-2009 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#include "radeon_program_pair.h"2829#include "radeon_compiler_util.h"3031#include <stdlib.h>3233/**34* Return the source slot where we installed the given register access,35* or -1 if no slot was free anymore.36*/37int rc_pair_alloc_source(struct rc_pair_instruction *pair,38unsigned int rgb, unsigned int alpha,39rc_register_file file, unsigned int index)40{41int candidate = -1;42int candidate_quality = -1;43unsigned int alpha_used = 0;44unsigned int rgb_used = 0;45int i;4647if ((!rgb && !alpha) || file == RC_FILE_NONE)48return 0;4950/* Make sure only one presubtract operation is used per instruction. */51if (file == RC_FILE_PRESUB) {52if (rgb && pair->RGB.Src[RC_PAIR_PRESUB_SRC].Used53&& index != pair->RGB.Src[RC_PAIR_PRESUB_SRC].Index) {54return -1;55}5657if (alpha && pair->Alpha.Src[RC_PAIR_PRESUB_SRC].Used58&& index != pair->Alpha.Src[RC_PAIR_PRESUB_SRC].Index) {59return -1;60}61}6263for(i = 0; i < 3; ++i) {64int q = 0;65if (rgb) {66if (pair->RGB.Src[i].Used) {67if (pair->RGB.Src[i].File != file ||68pair->RGB.Src[i].Index != index) {69rgb_used++;70continue;71}72q++;73}74}75if (alpha) {76if (pair->Alpha.Src[i].Used) {77if (pair->Alpha.Src[i].File != file ||78pair->Alpha.Src[i].Index != index) {79alpha_used++;80continue;81}82q++;83}84}85if (q > candidate_quality) {86candidate_quality = q;87candidate = i;88}89}9091if (file == RC_FILE_PRESUB) {92candidate = RC_PAIR_PRESUB_SRC;93} else if (candidate < 0 || (rgb && rgb_used > 2)94|| (alpha && alpha_used > 2)) {95return -1;96}9798/* candidate >= 0 */99100if (rgb) {101pair->RGB.Src[candidate].Used = 1;102pair->RGB.Src[candidate].File = file;103pair->RGB.Src[candidate].Index = index;104if (candidate == RC_PAIR_PRESUB_SRC) {105/* For registers with the RC_FILE_PRESUB file,106* the index stores the presubtract op. */107int src_regs = rc_presubtract_src_reg_count(index);108for(i = 0; i < src_regs; i++) {109pair->RGB.Src[i].Used = 1;110}111}112}113if (alpha) {114pair->Alpha.Src[candidate].Used = 1;115pair->Alpha.Src[candidate].File = file;116pair->Alpha.Src[candidate].Index = index;117if (candidate == RC_PAIR_PRESUB_SRC) {118/* For registers with the RC_FILE_PRESUB file,119* the index stores the presubtract op. */120int src_regs = rc_presubtract_src_reg_count(index);121for(i=0; i < src_regs; i++) {122pair->Alpha.Src[i].Used = 1;123}124}125}126127return candidate;128}129130static void pair_foreach_source_callback(131struct rc_pair_instruction * pair,132void * data,133rc_pair_foreach_src_fn cb,134unsigned int swz,135unsigned int src)136{137/* swz > 3 means that the swizzle is either not used, or a constant138* swizzle (e.g. 0, 1, 0.5). */139if(swz > 3)140return;141142if(swz == RC_SWIZZLE_W) {143if (src == RC_PAIR_PRESUB_SRC) {144unsigned int i;145unsigned int src_count = rc_presubtract_src_reg_count(146pair->Alpha.Src[RC_PAIR_PRESUB_SRC].Index);147for(i = 0; i < src_count; i++) {148cb(data, &pair->Alpha.Src[i]);149}150} else {151cb(data, &pair->Alpha.Src[src]);152}153} else {154if (src == RC_PAIR_PRESUB_SRC) {155unsigned int i;156unsigned int src_count = rc_presubtract_src_reg_count(157pair->RGB.Src[RC_PAIR_PRESUB_SRC].Index);158for(i = 0; i < src_count; i++) {159cb(data, &pair->RGB.Src[i]);160}161}162else {163cb(data, &pair->RGB.Src[src]);164}165}166}167168void rc_pair_foreach_source_that_alpha_reads(169struct rc_pair_instruction * pair,170void * data,171rc_pair_foreach_src_fn cb)172{173unsigned int i;174const struct rc_opcode_info * info =175rc_get_opcode_info(pair->Alpha.Opcode);176for(i = 0; i < info->NumSrcRegs; i++) {177pair_foreach_source_callback(pair, data, cb,178GET_SWZ(pair->Alpha.Arg[i].Swizzle, 0),179pair->Alpha.Arg[i].Source);180}181}182183void rc_pair_foreach_source_that_rgb_reads(184struct rc_pair_instruction * pair,185void * data,186rc_pair_foreach_src_fn cb)187{188unsigned int i;189const struct rc_opcode_info * info =190rc_get_opcode_info(pair->RGB.Opcode);191for(i = 0; i < info->NumSrcRegs; i++) {192unsigned int chan;193unsigned int swz = RC_SWIZZLE_UNUSED;194/* Find a swizzle that is either X,Y,Z,or W. We assume here195* that if one channel swizzles X,Y, or Z, then none of the196* other channels swizzle W, and vice-versa. */197for(chan = 0; chan < 4; chan++) {198swz = GET_SWZ(pair->RGB.Arg[i].Swizzle, chan);199if(swz == RC_SWIZZLE_X || swz == RC_SWIZZLE_Y200|| swz == RC_SWIZZLE_Z || swz == RC_SWIZZLE_W)201continue;202}203pair_foreach_source_callback(pair, data, cb,204swz,205pair->RGB.Arg[i].Source);206}207}208209struct rc_pair_instruction_source * rc_pair_get_src(210struct rc_pair_instruction * pair_inst,211struct rc_pair_instruction_arg * arg)212{213unsigned int type;214215type = rc_source_type_swz(arg->Swizzle);216217if (type & RC_SOURCE_RGB) {218return &pair_inst->RGB.Src[arg->Source];219} else if (type & RC_SOURCE_ALPHA) {220return &pair_inst->Alpha.Src[arg->Source];221} else {222return NULL;223}224}225226int rc_pair_get_src_index(227struct rc_pair_instruction * pair_inst,228struct rc_pair_instruction_source * src)229{230int i;231for (i = 0; i < 3; i++) {232if (&pair_inst->RGB.Src[i] == src233|| &pair_inst->Alpha.Src[i] == src) {234return i;235}236}237return -1;238}239240241