tgsi_util.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  * 
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  * 
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  * 
00026  **************************************************************************/
00027 
00028 #include "pipe/p_debug.h"
00029 #include "pipe/p_shader_tokens.h"
00030 #include "tgsi_parse.h"
00031 #include "tgsi_build.h"
00032 #include "tgsi_util.h"
00033 
00034 union pointer_hack
00035 {
00036    void *pointer;
00037    uint64_t uint64;
00038 };
00039 
00040 void *
00041 tgsi_align_128bit(
00042    void *unaligned )
00043 {
00044    union pointer_hack ph;
00045 
00046    ph.uint64 = 0;
00047    ph.pointer = unaligned;
00048    ph.uint64 = (ph.uint64 + 15) & ~15;
00049    return ph.pointer;
00050 }
00051 
00052 unsigned
00053 tgsi_util_get_src_register_swizzle(
00054    const struct tgsi_src_register *reg,
00055    unsigned component )
00056 {
00057    switch( component ) {
00058    case 0:
00059       return reg->SwizzleX;
00060    case 1:
00061       return reg->SwizzleY;
00062    case 2:
00063       return reg->SwizzleZ;
00064    case 3:
00065       return reg->SwizzleW;
00066    default:
00067       assert( 0 );
00068    }
00069    return 0;
00070 }
00071 
00072 unsigned
00073 tgsi_util_get_src_register_extswizzle(
00074    const struct tgsi_src_register_ext_swz *reg,
00075    unsigned component )
00076 {
00077    switch( component ) {
00078    case 0:
00079       return reg->ExtSwizzleX;
00080    case 1:
00081       return reg->ExtSwizzleY;
00082    case 2:
00083       return reg->ExtSwizzleZ;
00084    case 3:
00085       return reg->ExtSwizzleW;
00086    default:
00087       assert( 0 );
00088    }
00089    return 0;
00090 }
00091 
00092 unsigned
00093 tgsi_util_get_full_src_register_extswizzle(
00094    const struct tgsi_full_src_register  *reg,
00095    unsigned component )
00096 {
00097    unsigned swizzle;
00098 
00099    /*
00100     * First, calculate  the   extended swizzle for a given channel. This will give
00101     * us either a channel index into the simple swizzle or  a constant 1 or   0.
00102     */
00103    swizzle = tgsi_util_get_src_register_extswizzle(
00104       &reg->SrcRegisterExtSwz,
00105       component );
00106 
00107    assert (TGSI_SWIZZLE_X == TGSI_EXTSWIZZLE_X);
00108    assert (TGSI_SWIZZLE_Y == TGSI_EXTSWIZZLE_Y);
00109    assert (TGSI_SWIZZLE_Z == TGSI_EXTSWIZZLE_Z);
00110    assert (TGSI_SWIZZLE_W == TGSI_EXTSWIZZLE_W);
00111    assert (TGSI_EXTSWIZZLE_ZERO > TGSI_SWIZZLE_W);
00112    assert (TGSI_EXTSWIZZLE_ONE > TGSI_SWIZZLE_W);
00113 
00114    /*
00115     * Second, calculate the simple  swizzle  for   the   unswizzled channel index.
00116     * Leave the constants intact, they are   not   affected by the   simple swizzle.
00117     */
00118    if( swizzle <= TGSI_SWIZZLE_W ) {
00119       swizzle = tgsi_util_get_src_register_swizzle(
00120          &reg->SrcRegister,
00121          swizzle );
00122    }
00123 
00124    return swizzle;
00125 }
00126 
00127 void
00128 tgsi_util_set_src_register_swizzle(
00129    struct tgsi_src_register *reg,
00130    unsigned swizzle,
00131    unsigned component )
00132 {
00133    switch( component ) {
00134    case 0:
00135       reg->SwizzleX = swizzle;
00136       break;
00137    case 1:
00138       reg->SwizzleY = swizzle;
00139       break;
00140    case 2:
00141       reg->SwizzleZ = swizzle;
00142       break;
00143    case 3:
00144       reg->SwizzleW = swizzle;
00145       break;
00146    default:
00147       assert( 0 );
00148    }
00149 }
00150 
00151 void
00152 tgsi_util_set_src_register_extswizzle(
00153    struct tgsi_src_register_ext_swz *reg,
00154    unsigned swizzle,
00155    unsigned component )
00156 {
00157    switch( component ) {
00158    case 0:
00159       reg->ExtSwizzleX = swizzle;
00160       break;
00161    case 1:
00162       reg->ExtSwizzleY = swizzle;
00163       break;
00164    case 2:
00165       reg->ExtSwizzleZ = swizzle;
00166       break;
00167    case 3:
00168       reg->ExtSwizzleW = swizzle;
00169       break;
00170    default:
00171       assert( 0 );
00172    }
00173 }
00174 
00175 unsigned
00176 tgsi_util_get_src_register_extnegate(
00177    const  struct tgsi_src_register_ext_swz *reg,
00178    unsigned component )
00179 {
00180    switch( component ) {
00181    case 0:
00182       return reg->NegateX;
00183    case 1:
00184       return reg->NegateY;
00185    case 2:
00186       return reg->NegateZ;
00187    case 3:
00188       return reg->NegateW;
00189    default:
00190       assert( 0 );
00191    }
00192    return 0;
00193 }
00194 
00195 void
00196 tgsi_util_set_src_register_extnegate(
00197    struct tgsi_src_register_ext_swz *reg,
00198    unsigned negate,
00199    unsigned component )
00200 {
00201    switch( component ) {
00202    case 0:
00203       reg->NegateX = negate;
00204       break;
00205    case 1:
00206       reg->NegateY = negate;
00207       break;
00208    case 2:
00209       reg->NegateZ = negate;
00210       break;
00211    case 3:
00212       reg->NegateW = negate;
00213       break;
00214    default:
00215       assert( 0 );
00216    }
00217 }
00218 
00219 unsigned
00220 tgsi_util_get_full_src_register_sign_mode(
00221    const struct  tgsi_full_src_register *reg,
00222    unsigned component )
00223 {
00224    unsigned sign_mode;
00225 
00226    if( reg->SrcRegisterExtMod.Absolute ) {
00227       /* Consider only the post-abs negation. */
00228 
00229       if( reg->SrcRegisterExtMod.Negate ) {
00230          sign_mode = TGSI_UTIL_SIGN_SET;
00231       }
00232       else {
00233          sign_mode = TGSI_UTIL_SIGN_CLEAR;
00234       }
00235    }
00236    else {
00237       /* Accumulate the three negations. */
00238 
00239       unsigned negate;
00240 
00241       negate = reg->SrcRegister.Negate;
00242       if( tgsi_util_get_src_register_extnegate( &reg->SrcRegisterExtSwz, component ) ) {
00243          negate = !negate;
00244       }
00245       if( reg->SrcRegisterExtMod.Negate ) {
00246          negate = !negate;
00247       }
00248 
00249       if( negate ) {
00250          sign_mode = TGSI_UTIL_SIGN_TOGGLE;
00251       }
00252       else {
00253          sign_mode = TGSI_UTIL_SIGN_KEEP;
00254       }
00255    }
00256 
00257    return sign_mode;
00258 }
00259 
00260 void
00261 tgsi_util_set_full_src_register_sign_mode(
00262    struct tgsi_full_src_register *reg,
00263    unsigned sign_mode )
00264 {
00265    reg->SrcRegisterExtSwz.NegateX = 0;
00266    reg->SrcRegisterExtSwz.NegateY = 0;
00267    reg->SrcRegisterExtSwz.NegateZ = 0;
00268    reg->SrcRegisterExtSwz.NegateW = 0;
00269 
00270    switch (sign_mode)
00271    {
00272    case TGSI_UTIL_SIGN_CLEAR:
00273       reg->SrcRegister.Negate = 0;
00274       reg->SrcRegisterExtMod.Absolute = 1;
00275       reg->SrcRegisterExtMod.Negate = 0;
00276       break;
00277 
00278    case TGSI_UTIL_SIGN_SET:
00279       reg->SrcRegister.Negate = 0;
00280       reg->SrcRegisterExtMod.Absolute = 1;
00281       reg->SrcRegisterExtMod.Negate = 1;
00282       break;
00283 
00284    case TGSI_UTIL_SIGN_TOGGLE:
00285       reg->SrcRegister.Negate = 1;
00286       reg->SrcRegisterExtMod.Absolute = 0;
00287       reg->SrcRegisterExtMod.Negate = 0;
00288       break;
00289 
00290    case TGSI_UTIL_SIGN_KEEP:
00291       reg->SrcRegister.Negate = 0;
00292       reg->SrcRegisterExtMod.Absolute = 0;
00293       reg->SrcRegisterExtMod.Negate = 0;
00294       break;
00295 
00296    default:
00297       assert( 0 );
00298    }
00299 }

Generated on Tue Sep 29 06:25:15 2009 for Gallium3D by  doxygen 1.5.4