Path: blob/21.2-virgl/src/gallium/auxiliary/tgsi/tgsi_build.c
4565 views
/**************************************************************************1*2* Copyright 2007 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#include "util/u_debug.h"28#include "pipe/p_format.h"29#include "pipe/p_shader_tokens.h"30#include "tgsi_build.h"31#include "tgsi_parse.h"323334/*35* header36*/3738struct tgsi_header39tgsi_build_header( void )40{41struct tgsi_header header;4243header.HeaderSize = 1;44header.BodySize = 0;4546return header;47}4849static void50header_headersize_grow( struct tgsi_header *header )51{52assert( header->HeaderSize < 0xFF );53assert( header->BodySize == 0 );5455header->HeaderSize++;56}5758static void59header_bodysize_grow( struct tgsi_header *header )60{61assert( header->BodySize < 0xFFFFFF );6263header->BodySize++;64}6566struct tgsi_processor67tgsi_build_processor(68unsigned type,69struct tgsi_header *header )70{71struct tgsi_processor processor;7273processor.Processor = type;74processor.Padding = 0;7576header_headersize_grow( header );7778return processor;79}8081/*82* declaration83*/8485static void86declaration_grow(87struct tgsi_declaration *declaration,88struct tgsi_header *header )89{90assert( declaration->NrTokens < 0xFF );9192declaration->NrTokens++;9394header_bodysize_grow( header );95}9697static struct tgsi_declaration98tgsi_default_declaration( void )99{100struct tgsi_declaration declaration;101102declaration.Type = TGSI_TOKEN_TYPE_DECLARATION;103declaration.NrTokens = 1;104declaration.File = TGSI_FILE_NULL;105declaration.UsageMask = TGSI_WRITEMASK_XYZW;106declaration.Interpolate = 0;107declaration.Dimension = 0;108declaration.Semantic = 0;109declaration.Invariant = 0;110declaration.Local = 0;111declaration.Array = 0;112declaration.Atomic = 0;113declaration.MemType = TGSI_MEMORY_TYPE_GLOBAL;114declaration.Padding = 0;115116return declaration;117}118119static struct tgsi_declaration120tgsi_build_declaration(121unsigned file,122unsigned usage_mask,123unsigned interpolate,124unsigned dimension,125unsigned semantic,126unsigned invariant,127unsigned local,128unsigned array,129unsigned atomic,130unsigned mem_type,131struct tgsi_header *header )132{133struct tgsi_declaration declaration;134135assert( file < TGSI_FILE_COUNT );136assert( interpolate < TGSI_INTERPOLATE_COUNT );137138declaration = tgsi_default_declaration();139declaration.File = file;140declaration.UsageMask = usage_mask;141declaration.Interpolate = interpolate;142declaration.Dimension = dimension;143declaration.Semantic = semantic;144declaration.Invariant = invariant;145declaration.Local = local;146declaration.Array = array;147declaration.Atomic = atomic;148declaration.MemType = mem_type;149header_bodysize_grow( header );150151return declaration;152}153154static struct tgsi_declaration_range155tgsi_default_declaration_range( void )156{157struct tgsi_declaration_range dr;158159dr.First = 0;160dr.Last = 0;161162return dr;163}164165static struct tgsi_declaration_dimension166tgsi_default_declaration_dimension()167{168struct tgsi_declaration_dimension dim;169170dim.Index2D = 0;171dim.Padding = 0;172173return dim;174}175176static struct tgsi_declaration_range177tgsi_build_declaration_range(178unsigned first,179unsigned last,180struct tgsi_declaration *declaration,181struct tgsi_header *header )182{183struct tgsi_declaration_range declaration_range;184185assert( last >= first );186assert( last <= 0xFFFF );187188declaration_range.First = first;189declaration_range.Last = last;190191declaration_grow( declaration, header );192193return declaration_range;194}195196static struct tgsi_declaration_dimension197tgsi_build_declaration_dimension(unsigned index_2d,198struct tgsi_declaration *declaration,199struct tgsi_header *header)200{201struct tgsi_declaration_dimension dd;202203assert(index_2d <= 0xFFFF);204205dd.Index2D = index_2d;206dd.Padding = 0;207208declaration_grow(declaration, header);209210return dd;211}212213static struct tgsi_declaration_interp214tgsi_default_declaration_interp( void )215{216struct tgsi_declaration_interp di;217218di.Interpolate = TGSI_INTERPOLATE_CONSTANT;219di.Location = TGSI_INTERPOLATE_LOC_CENTER;220di.CylindricalWrap = 0;221di.Padding = 0;222223return di;224}225226static struct tgsi_declaration_interp227tgsi_build_declaration_interp(unsigned interpolate,228unsigned interpolate_location,229unsigned cylindrical_wrap,230struct tgsi_declaration *declaration,231struct tgsi_header *header)232{233struct tgsi_declaration_interp di;234235di.Interpolate = interpolate;236di.Location = interpolate_location;237di.CylindricalWrap = cylindrical_wrap;238di.Padding = 0;239240declaration_grow(declaration, header);241242return di;243}244245static struct tgsi_declaration_semantic246tgsi_default_declaration_semantic( void )247{248struct tgsi_declaration_semantic ds;249250ds.Name = TGSI_SEMANTIC_POSITION;251ds.Index = 0;252ds.StreamX = 0;253ds.StreamY = 0;254ds.StreamZ = 0;255ds.StreamW = 0;256257return ds;258}259260static struct tgsi_declaration_semantic261tgsi_build_declaration_semantic(262unsigned semantic_name,263unsigned semantic_index,264unsigned streamx,265unsigned streamy,266unsigned streamz,267unsigned streamw,268struct tgsi_declaration *declaration,269struct tgsi_header *header )270{271struct tgsi_declaration_semantic ds;272273assert( semantic_name <= TGSI_SEMANTIC_COUNT );274assert( semantic_index <= 0xFFFF );275276ds.Name = semantic_name;277ds.Index = semantic_index;278ds.StreamX = streamx;279ds.StreamY = streamy;280ds.StreamZ = streamz;281ds.StreamW = streamw;282283declaration_grow( declaration, header );284285return ds;286}287288static struct tgsi_declaration_image289tgsi_default_declaration_image(void)290{291struct tgsi_declaration_image di;292293di.Resource = TGSI_TEXTURE_BUFFER;294di.Raw = 0;295di.Writable = 0;296di.Format = 0;297di.Padding = 0;298299return di;300}301302static struct tgsi_declaration_image303tgsi_build_declaration_image(unsigned texture,304unsigned format,305unsigned raw,306unsigned writable,307struct tgsi_declaration *declaration,308struct tgsi_header *header)309{310struct tgsi_declaration_image di;311312di = tgsi_default_declaration_image();313di.Resource = texture;314di.Format = format;315di.Raw = raw;316di.Writable = writable;317318declaration_grow(declaration, header);319320return di;321}322323static struct tgsi_declaration_sampler_view324tgsi_default_declaration_sampler_view(void)325{326struct tgsi_declaration_sampler_view dsv;327328dsv.Resource = TGSI_TEXTURE_BUFFER;329dsv.ReturnTypeX = TGSI_RETURN_TYPE_UNORM;330dsv.ReturnTypeY = TGSI_RETURN_TYPE_UNORM;331dsv.ReturnTypeZ = TGSI_RETURN_TYPE_UNORM;332dsv.ReturnTypeW = TGSI_RETURN_TYPE_UNORM;333334return dsv;335}336337static struct tgsi_declaration_sampler_view338tgsi_build_declaration_sampler_view(unsigned texture,339unsigned return_type_x,340unsigned return_type_y,341unsigned return_type_z,342unsigned return_type_w,343struct tgsi_declaration *declaration,344struct tgsi_header *header)345{346struct tgsi_declaration_sampler_view dsv;347348dsv = tgsi_default_declaration_sampler_view();349dsv.Resource = texture;350dsv.ReturnTypeX = return_type_x;351dsv.ReturnTypeY = return_type_y;352dsv.ReturnTypeZ = return_type_z;353dsv.ReturnTypeW = return_type_w;354355declaration_grow(declaration, header);356357return dsv;358}359360361static struct tgsi_declaration_array362tgsi_default_declaration_array( void )363{364struct tgsi_declaration_array a;365366a.ArrayID = 0;367a.Padding = 0;368369return a;370}371372static struct tgsi_declaration_array373tgsi_build_declaration_array(unsigned arrayid,374struct tgsi_declaration *declaration,375struct tgsi_header *header)376{377struct tgsi_declaration_array da;378379da = tgsi_default_declaration_array();380da.ArrayID = arrayid;381382declaration_grow(declaration, header);383384return da;385}386387struct tgsi_full_declaration388tgsi_default_full_declaration( void )389{390struct tgsi_full_declaration full_declaration;391392full_declaration.Declaration = tgsi_default_declaration();393full_declaration.Range = tgsi_default_declaration_range();394full_declaration.Dim = tgsi_default_declaration_dimension();395full_declaration.Semantic = tgsi_default_declaration_semantic();396full_declaration.Interp = tgsi_default_declaration_interp();397full_declaration.Image = tgsi_default_declaration_image();398full_declaration.SamplerView = tgsi_default_declaration_sampler_view();399full_declaration.Array = tgsi_default_declaration_array();400401return full_declaration;402}403404unsigned405tgsi_build_full_declaration(406const struct tgsi_full_declaration *full_decl,407struct tgsi_token *tokens,408struct tgsi_header *header,409unsigned maxsize )410{411unsigned size = 0;412struct tgsi_declaration *declaration;413struct tgsi_declaration_range *dr;414415if( maxsize <= size )416return 0;417declaration = (struct tgsi_declaration *) &tokens[size];418size++;419420*declaration = tgsi_build_declaration(421full_decl->Declaration.File,422full_decl->Declaration.UsageMask,423full_decl->Declaration.Interpolate,424full_decl->Declaration.Dimension,425full_decl->Declaration.Semantic,426full_decl->Declaration.Invariant,427full_decl->Declaration.Local,428full_decl->Declaration.Array,429full_decl->Declaration.Atomic,430full_decl->Declaration.MemType,431header );432433if (maxsize <= size)434return 0;435dr = (struct tgsi_declaration_range *) &tokens[size];436size++;437438*dr = tgsi_build_declaration_range(439full_decl->Range.First,440full_decl->Range.Last,441declaration,442header );443444if (full_decl->Declaration.Dimension) {445struct tgsi_declaration_dimension *dd;446447if (maxsize <= size) {448return 0;449}450dd = (struct tgsi_declaration_dimension *)&tokens[size];451size++;452453*dd = tgsi_build_declaration_dimension(full_decl->Dim.Index2D,454declaration,455header);456}457458if (full_decl->Declaration.Interpolate) {459struct tgsi_declaration_interp *di;460461if (maxsize <= size) {462return 0;463}464di = (struct tgsi_declaration_interp *)&tokens[size];465size++;466467*di = tgsi_build_declaration_interp(full_decl->Interp.Interpolate,468full_decl->Interp.Location,469full_decl->Interp.CylindricalWrap,470declaration,471header);472}473474if( full_decl->Declaration.Semantic ) {475struct tgsi_declaration_semantic *ds;476477if( maxsize <= size )478return 0;479ds = (struct tgsi_declaration_semantic *) &tokens[size];480size++;481482*ds = tgsi_build_declaration_semantic(483full_decl->Semantic.Name,484full_decl->Semantic.Index,485full_decl->Semantic.StreamX,486full_decl->Semantic.StreamY,487full_decl->Semantic.StreamZ,488full_decl->Semantic.StreamW,489declaration,490header );491}492493if (full_decl->Declaration.File == TGSI_FILE_IMAGE) {494struct tgsi_declaration_image *di;495496if (maxsize <= size) {497return 0;498}499di = (struct tgsi_declaration_image *)&tokens[size];500size++;501502*di = tgsi_build_declaration_image(full_decl->Image.Resource,503full_decl->Image.Format,504full_decl->Image.Raw,505full_decl->Image.Writable,506declaration,507header);508}509510if (full_decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {511struct tgsi_declaration_sampler_view *dsv;512513if (maxsize <= size) {514return 0;515}516dsv = (struct tgsi_declaration_sampler_view *)&tokens[size];517size++;518519*dsv = tgsi_build_declaration_sampler_view(520full_decl->SamplerView.Resource,521full_decl->SamplerView.ReturnTypeX,522full_decl->SamplerView.ReturnTypeY,523full_decl->SamplerView.ReturnTypeZ,524full_decl->SamplerView.ReturnTypeW,525declaration,526header);527}528529if (full_decl->Declaration.Array) {530struct tgsi_declaration_array *da;531532if (maxsize <= size) {533return 0;534}535da = (struct tgsi_declaration_array *)&tokens[size];536size++;537*da = tgsi_build_declaration_array(538full_decl->Array.ArrayID,539declaration,540header);541}542return size;543}544545/*546* immediate547*/548549static struct tgsi_immediate550tgsi_default_immediate( void )551{552struct tgsi_immediate immediate;553554immediate.Type = TGSI_TOKEN_TYPE_IMMEDIATE;555immediate.NrTokens = 1;556immediate.DataType = TGSI_IMM_FLOAT32;557immediate.Padding = 0;558559return immediate;560}561562static struct tgsi_immediate563tgsi_build_immediate(564struct tgsi_header *header,565unsigned type )566{567struct tgsi_immediate immediate;568569immediate = tgsi_default_immediate();570immediate.DataType = type;571572header_bodysize_grow( header );573574return immediate;575}576577struct tgsi_full_immediate578tgsi_default_full_immediate( void )579{580struct tgsi_full_immediate fullimm;581582fullimm.Immediate = tgsi_default_immediate();583fullimm.u[0].Float = 0.0f;584fullimm.u[1].Float = 0.0f;585fullimm.u[2].Float = 0.0f;586fullimm.u[3].Float = 0.0f;587588return fullimm;589}590591static void592immediate_grow(593struct tgsi_immediate *immediate,594struct tgsi_header *header )595{596assert( immediate->NrTokens < 0xFF );597598immediate->NrTokens++;599600header_bodysize_grow( header );601}602603unsigned604tgsi_build_full_immediate(605const struct tgsi_full_immediate *full_imm,606struct tgsi_token *tokens,607struct tgsi_header *header,608unsigned maxsize )609{610unsigned size = 0;611int i;612struct tgsi_immediate *immediate;613614if( maxsize <= size )615return 0;616immediate = (struct tgsi_immediate *) &tokens[size];617size++;618619*immediate = tgsi_build_immediate( header, full_imm->Immediate.DataType );620621assert( full_imm->Immediate.NrTokens <= 4 + 1 );622623for( i = 0; i < full_imm->Immediate.NrTokens - 1; i++ ) {624union tgsi_immediate_data *data;625626if( maxsize <= size )627return 0;628629data = (union tgsi_immediate_data *) &tokens[size];630*data = full_imm->u[i];631632immediate_grow( immediate, header );633size++;634}635636return size;637}638639/*640* instruction641*/642643struct tgsi_instruction644tgsi_default_instruction( void )645{646struct tgsi_instruction instruction;647648instruction.Type = TGSI_TOKEN_TYPE_INSTRUCTION;649instruction.NrTokens = 0;650instruction.Opcode = TGSI_OPCODE_MOV;651instruction.Saturate = 0;652instruction.NumDstRegs = 1;653instruction.NumSrcRegs = 1;654instruction.Label = 0;655instruction.Texture = 0;656instruction.Memory = 0;657instruction.Precise = 0;658instruction.Padding = 0;659660return instruction;661}662663static struct tgsi_instruction664tgsi_build_instruction(enum tgsi_opcode opcode,665unsigned saturate,666unsigned precise,667unsigned num_dst_regs,668unsigned num_src_regs,669struct tgsi_header *header)670{671struct tgsi_instruction instruction;672673assert (opcode <= TGSI_OPCODE_LAST);674assert (saturate <= 1);675assert (num_dst_regs <= 3);676assert (num_src_regs <= 15);677678instruction = tgsi_default_instruction();679instruction.Opcode = opcode;680instruction.Saturate = saturate;681instruction.Precise = precise;682instruction.NumDstRegs = num_dst_regs;683instruction.NumSrcRegs = num_src_regs;684685header_bodysize_grow( header );686687return instruction;688}689690static void691instruction_grow(692struct tgsi_instruction *instruction,693struct tgsi_header *header )694{695assert (instruction->NrTokens < 0xFF);696697instruction->NrTokens++;698699header_bodysize_grow( header );700}701702static struct tgsi_instruction_label703tgsi_default_instruction_label( void )704{705struct tgsi_instruction_label instruction_label;706707instruction_label.Label = 0;708instruction_label.Padding = 0;709710return instruction_label;711}712713static struct tgsi_instruction_label714tgsi_build_instruction_label(715unsigned label,716struct tgsi_instruction *instruction,717struct tgsi_header *header )718{719struct tgsi_instruction_label instruction_label;720721instruction_label.Label = label;722instruction_label.Padding = 0;723instruction->Label = 1;724725instruction_grow( instruction, header );726727return instruction_label;728}729730static struct tgsi_instruction_texture731tgsi_default_instruction_texture( void )732{733struct tgsi_instruction_texture instruction_texture;734735instruction_texture.Texture = TGSI_TEXTURE_UNKNOWN;736instruction_texture.NumOffsets = 0;737instruction_texture.ReturnType = TGSI_RETURN_TYPE_UNKNOWN;738instruction_texture.Padding = 0;739740return instruction_texture;741}742743static struct tgsi_instruction_texture744tgsi_build_instruction_texture(745unsigned texture,746unsigned num_offsets,747unsigned return_type,748struct tgsi_instruction *instruction,749struct tgsi_header *header )750{751struct tgsi_instruction_texture instruction_texture;752753instruction_texture.Texture = texture;754instruction_texture.NumOffsets = num_offsets;755instruction_texture.ReturnType = return_type;756instruction_texture.Padding = 0;757instruction->Texture = 1;758759instruction_grow( instruction, header );760761return instruction_texture;762}763764static struct tgsi_instruction_memory765tgsi_default_instruction_memory( void )766{767struct tgsi_instruction_memory instruction_memory;768769instruction_memory.Qualifier = 0;770instruction_memory.Texture = 0;771instruction_memory.Format = 0;772instruction_memory.Padding = 0;773774return instruction_memory;775}776777static struct tgsi_instruction_memory778tgsi_build_instruction_memory(779unsigned qualifier,780unsigned texture,781unsigned format,782struct tgsi_instruction *instruction,783struct tgsi_header *header )784{785struct tgsi_instruction_memory instruction_memory;786787instruction_memory.Qualifier = qualifier;788instruction_memory.Texture = texture;789instruction_memory.Format = format;790instruction_memory.Padding = 0;791instruction->Memory = 1;792793instruction_grow( instruction, header );794795return instruction_memory;796}797798static struct tgsi_texture_offset799tgsi_default_texture_offset( void )800{801struct tgsi_texture_offset texture_offset;802803texture_offset.Index = 0;804texture_offset.File = 0;805texture_offset.SwizzleX = 0;806texture_offset.SwizzleY = 0;807texture_offset.SwizzleZ = 0;808texture_offset.Padding = 0;809810return texture_offset;811}812813static struct tgsi_texture_offset814tgsi_build_texture_offset(815int index, int file, int swizzle_x, int swizzle_y, int swizzle_z,816struct tgsi_instruction *instruction,817struct tgsi_header *header )818{819struct tgsi_texture_offset texture_offset;820821texture_offset.Index = index;822texture_offset.File = file;823texture_offset.SwizzleX = swizzle_x;824texture_offset.SwizzleY = swizzle_y;825texture_offset.SwizzleZ = swizzle_z;826texture_offset.Padding = 0;827828instruction_grow( instruction, header );829830return texture_offset;831}832833static struct tgsi_src_register834tgsi_default_src_register( void )835{836struct tgsi_src_register src_register;837838src_register.File = TGSI_FILE_NULL;839src_register.SwizzleX = TGSI_SWIZZLE_X;840src_register.SwizzleY = TGSI_SWIZZLE_Y;841src_register.SwizzleZ = TGSI_SWIZZLE_Z;842src_register.SwizzleW = TGSI_SWIZZLE_W;843src_register.Negate = 0;844src_register.Absolute = 0;845src_register.Indirect = 0;846src_register.Dimension = 0;847src_register.Index = 0;848849return src_register;850}851852static struct tgsi_src_register853tgsi_build_src_register(854unsigned file,855unsigned swizzle_x,856unsigned swizzle_y,857unsigned swizzle_z,858unsigned swizzle_w,859unsigned negate,860unsigned absolute,861unsigned indirect,862unsigned dimension,863int index,864struct tgsi_instruction *instruction,865struct tgsi_header *header )866{867struct tgsi_src_register src_register;868869assert( file < TGSI_FILE_COUNT );870assert( swizzle_x <= TGSI_SWIZZLE_W );871assert( swizzle_y <= TGSI_SWIZZLE_W );872assert( swizzle_z <= TGSI_SWIZZLE_W );873assert( swizzle_w <= TGSI_SWIZZLE_W );874assert( negate <= 1 );875assert( index >= -0x8000 && index <= 0x7FFF );876877src_register.File = file;878src_register.SwizzleX = swizzle_x;879src_register.SwizzleY = swizzle_y;880src_register.SwizzleZ = swizzle_z;881src_register.SwizzleW = swizzle_w;882src_register.Negate = negate;883src_register.Absolute = absolute;884src_register.Indirect = indirect;885src_register.Dimension = dimension;886src_register.Index = index;887888instruction_grow( instruction, header );889890return src_register;891}892893static struct tgsi_ind_register894tgsi_default_ind_register( void )895{896struct tgsi_ind_register ind_register;897898ind_register.File = TGSI_FILE_NULL;899ind_register.Index = 0;900ind_register.Swizzle = TGSI_SWIZZLE_X;901ind_register.ArrayID = 0;902903return ind_register;904}905906static struct tgsi_ind_register907tgsi_build_ind_register(908unsigned file,909unsigned swizzle,910int index,911unsigned arrayid,912struct tgsi_instruction *instruction,913struct tgsi_header *header )914{915struct tgsi_ind_register ind_register;916917assert( file < TGSI_FILE_COUNT );918assert( swizzle <= TGSI_SWIZZLE_W );919assert( index >= -0x8000 && index <= 0x7FFF );920921ind_register.File = file;922ind_register.Swizzle = swizzle;923ind_register.Index = index;924ind_register.ArrayID = arrayid;925926instruction_grow( instruction, header );927928return ind_register;929}930931static struct tgsi_dimension932tgsi_default_dimension( void )933{934struct tgsi_dimension dimension;935936dimension.Indirect = 0;937dimension.Dimension = 0;938dimension.Padding = 0;939dimension.Index = 0;940941return dimension;942}943944static struct tgsi_full_src_register945tgsi_default_full_src_register( void )946{947struct tgsi_full_src_register full_src_register;948949full_src_register.Register = tgsi_default_src_register();950full_src_register.Indirect = tgsi_default_ind_register();951full_src_register.Dimension = tgsi_default_dimension();952full_src_register.DimIndirect = tgsi_default_ind_register();953954return full_src_register;955}956957static struct tgsi_dimension958tgsi_build_dimension(959unsigned indirect,960unsigned index,961struct tgsi_instruction *instruction,962struct tgsi_header *header )963{964struct tgsi_dimension dimension;965966dimension.Indirect = indirect;967dimension.Dimension = 0;968dimension.Padding = 0;969dimension.Index = index;970971instruction_grow( instruction, header );972973return dimension;974}975976static struct tgsi_dst_register977tgsi_default_dst_register( void )978{979struct tgsi_dst_register dst_register;980981dst_register.File = TGSI_FILE_NULL;982dst_register.WriteMask = TGSI_WRITEMASK_XYZW;983dst_register.Indirect = 0;984dst_register.Dimension = 0;985dst_register.Index = 0;986dst_register.Padding = 0;987988return dst_register;989}990991static struct tgsi_dst_register992tgsi_build_dst_register(993unsigned file,994unsigned mask,995unsigned indirect,996unsigned dimension,997int index,998struct tgsi_instruction *instruction,999struct tgsi_header *header )1000{1001struct tgsi_dst_register dst_register;10021003assert( file < TGSI_FILE_COUNT );1004assert( mask <= TGSI_WRITEMASK_XYZW );1005assert( index >= -32768 && index <= 32767 );10061007dst_register.File = file;1008dst_register.WriteMask = mask;1009dst_register.Indirect = indirect;1010dst_register.Dimension = dimension;1011dst_register.Index = index;1012dst_register.Padding = 0;10131014instruction_grow( instruction, header );10151016return dst_register;1017}10181019static struct tgsi_full_dst_register1020tgsi_default_full_dst_register( void )1021{1022struct tgsi_full_dst_register full_dst_register;10231024full_dst_register.Register = tgsi_default_dst_register();1025full_dst_register.Indirect = tgsi_default_ind_register();1026full_dst_register.Dimension = tgsi_default_dimension();1027full_dst_register.DimIndirect = tgsi_default_ind_register();10281029return full_dst_register;1030}10311032struct tgsi_full_instruction1033tgsi_default_full_instruction( void )1034{1035struct tgsi_full_instruction full_instruction;1036unsigned i;10371038full_instruction.Instruction = tgsi_default_instruction();1039full_instruction.Label = tgsi_default_instruction_label();1040full_instruction.Texture = tgsi_default_instruction_texture();1041full_instruction.Memory = tgsi_default_instruction_memory();1042for( i = 0; i < TGSI_FULL_MAX_TEX_OFFSETS; i++ ) {1043full_instruction.TexOffsets[i] = tgsi_default_texture_offset();1044}1045for( i = 0; i < TGSI_FULL_MAX_DST_REGISTERS; i++ ) {1046full_instruction.Dst[i] = tgsi_default_full_dst_register();1047}1048for( i = 0; i < TGSI_FULL_MAX_SRC_REGISTERS; i++ ) {1049full_instruction.Src[i] = tgsi_default_full_src_register();1050}10511052return full_instruction;1053}10541055unsigned1056tgsi_build_full_instruction(1057const struct tgsi_full_instruction *full_inst,1058struct tgsi_token *tokens,1059struct tgsi_header *header,1060unsigned maxsize )1061{1062unsigned size = 0;1063unsigned i;1064struct tgsi_instruction *instruction;10651066if( maxsize <= size )1067return 0;1068instruction = (struct tgsi_instruction *) &tokens[size];1069size++;10701071*instruction = tgsi_build_instruction(full_inst->Instruction.Opcode,1072full_inst->Instruction.Saturate,1073full_inst->Instruction.Precise,1074full_inst->Instruction.NumDstRegs,1075full_inst->Instruction.NumSrcRegs,1076header);10771078if (full_inst->Instruction.Label) {1079struct tgsi_instruction_label *instruction_label;10801081if( maxsize <= size )1082return 0;1083instruction_label =1084(struct tgsi_instruction_label *) &tokens[size];1085size++;10861087*instruction_label = tgsi_build_instruction_label(1088full_inst->Label.Label,1089instruction,1090header );1091}10921093if (full_inst->Instruction.Texture) {1094struct tgsi_instruction_texture *instruction_texture;10951096if( maxsize <= size )1097return 0;1098instruction_texture =1099(struct tgsi_instruction_texture *) &tokens[size];1100size++;11011102*instruction_texture = tgsi_build_instruction_texture(1103full_inst->Texture.Texture,1104full_inst->Texture.NumOffsets,1105full_inst->Texture.ReturnType,1106instruction,1107header );11081109for (i = 0; i < full_inst->Texture.NumOffsets; i++) {1110struct tgsi_texture_offset *texture_offset;11111112if ( maxsize <= size )1113return 0;1114texture_offset = (struct tgsi_texture_offset *)&tokens[size];1115size++;1116*texture_offset = tgsi_build_texture_offset(1117full_inst->TexOffsets[i].Index,1118full_inst->TexOffsets[i].File,1119full_inst->TexOffsets[i].SwizzleX,1120full_inst->TexOffsets[i].SwizzleY,1121full_inst->TexOffsets[i].SwizzleZ,1122instruction,1123header);1124}1125}11261127if (full_inst->Instruction.Memory) {1128struct tgsi_instruction_memory *instruction_memory;11291130if( maxsize <= size )1131return 0;1132instruction_memory =1133(struct tgsi_instruction_memory *) &tokens[size];1134size++;11351136*instruction_memory = tgsi_build_instruction_memory(1137full_inst->Memory.Qualifier,1138full_inst->Memory.Texture,1139full_inst->Memory.Format,1140instruction,1141header );1142}11431144for( i = 0; i < full_inst->Instruction.NumDstRegs; i++ ) {1145const struct tgsi_full_dst_register *reg = &full_inst->Dst[i];1146struct tgsi_dst_register *dst_register;11471148if( maxsize <= size )1149return 0;1150dst_register = (struct tgsi_dst_register *) &tokens[size];1151size++;11521153*dst_register = tgsi_build_dst_register(1154reg->Register.File,1155reg->Register.WriteMask,1156reg->Register.Indirect,1157reg->Register.Dimension,1158reg->Register.Index,1159instruction,1160header );11611162if( reg->Register.Indirect ) {1163struct tgsi_ind_register *ind;11641165if( maxsize <= size )1166return 0;1167ind = (struct tgsi_ind_register *) &tokens[size];1168size++;11691170*ind = tgsi_build_ind_register(1171reg->Indirect.File,1172reg->Indirect.Swizzle,1173reg->Indirect.Index,1174reg->Indirect.ArrayID,1175instruction,1176header );1177}11781179if( reg->Register.Dimension ) {1180struct tgsi_dimension *dim;11811182assert( !reg->Dimension.Dimension );11831184if( maxsize <= size )1185return 0;1186dim = (struct tgsi_dimension *) &tokens[size];1187size++;11881189*dim = tgsi_build_dimension(1190reg->Dimension.Indirect,1191reg->Dimension.Index,1192instruction,1193header );11941195if( reg->Dimension.Indirect ) {1196struct tgsi_ind_register *ind;11971198if( maxsize <= size )1199return 0;1200ind = (struct tgsi_ind_register *) &tokens[size];1201size++;12021203*ind = tgsi_build_ind_register(1204reg->DimIndirect.File,1205reg->DimIndirect.Swizzle,1206reg->DimIndirect.Index,1207reg->DimIndirect.ArrayID,1208instruction,1209header );1210}1211}1212}12131214for( i = 0; i < full_inst->Instruction.NumSrcRegs; i++ ) {1215const struct tgsi_full_src_register *reg = &full_inst->Src[i];1216struct tgsi_src_register *src_register;12171218if( maxsize <= size )1219return 0;1220src_register = (struct tgsi_src_register *) &tokens[size];1221size++;12221223*src_register = tgsi_build_src_register(1224reg->Register.File,1225reg->Register.SwizzleX,1226reg->Register.SwizzleY,1227reg->Register.SwizzleZ,1228reg->Register.SwizzleW,1229reg->Register.Negate,1230reg->Register.Absolute,1231reg->Register.Indirect,1232reg->Register.Dimension,1233reg->Register.Index,1234instruction,1235header );12361237if( reg->Register.Indirect ) {1238struct tgsi_ind_register *ind;12391240if( maxsize <= size )1241return 0;1242ind = (struct tgsi_ind_register *) &tokens[size];1243size++;12441245*ind = tgsi_build_ind_register(1246reg->Indirect.File,1247reg->Indirect.Swizzle,1248reg->Indirect.Index,1249reg->Indirect.ArrayID,1250instruction,1251header );1252}12531254if( reg->Register.Dimension ) {1255struct tgsi_dimension *dim;12561257assert( !reg->Dimension.Dimension );12581259if( maxsize <= size )1260return 0;1261dim = (struct tgsi_dimension *) &tokens[size];1262size++;12631264*dim = tgsi_build_dimension(1265reg->Dimension.Indirect,1266reg->Dimension.Index,1267instruction,1268header );12691270if( reg->Dimension.Indirect ) {1271struct tgsi_ind_register *ind;12721273if( maxsize <= size )1274return 0;1275ind = (struct tgsi_ind_register *) &tokens[size];1276size++;12771278*ind = tgsi_build_ind_register(1279reg->DimIndirect.File,1280reg->DimIndirect.Swizzle,1281reg->DimIndirect.Index,1282reg->DimIndirect.ArrayID,1283instruction,1284header );1285}1286}1287}12881289return size;1290}12911292static struct tgsi_property1293tgsi_default_property( void )1294{1295struct tgsi_property property;12961297property.Type = TGSI_TOKEN_TYPE_PROPERTY;1298property.NrTokens = 1;1299property.PropertyName = TGSI_PROPERTY_GS_INPUT_PRIM;1300property.Padding = 0;13011302return property;1303}13041305static struct tgsi_property1306tgsi_build_property(unsigned property_name,1307struct tgsi_header *header)1308{1309struct tgsi_property property;13101311property = tgsi_default_property();1312property.PropertyName = property_name;13131314header_bodysize_grow( header );13151316return property;1317}131813191320struct tgsi_full_property1321tgsi_default_full_property( void )1322{1323struct tgsi_full_property full_property;13241325full_property.Property = tgsi_default_property();1326memset(full_property.u, 0,1327sizeof(struct tgsi_property_data) * 8);13281329return full_property;1330}13311332static void1333property_grow(1334struct tgsi_property *property,1335struct tgsi_header *header )1336{1337assert( property->NrTokens < 0xFF );13381339property->NrTokens++;13401341header_bodysize_grow( header );1342}13431344static struct tgsi_property_data1345tgsi_build_property_data(1346unsigned value,1347struct tgsi_property *property,1348struct tgsi_header *header )1349{1350struct tgsi_property_data property_data;13511352property_data.Data = value;13531354property_grow( property, header );13551356return property_data;1357}13581359unsigned1360tgsi_build_full_property(1361const struct tgsi_full_property *full_prop,1362struct tgsi_token *tokens,1363struct tgsi_header *header,1364unsigned maxsize )1365{1366unsigned size = 0;1367int i;1368struct tgsi_property *property;13691370if( maxsize <= size )1371return 0;1372property = (struct tgsi_property *) &tokens[size];1373size++;13741375*property = tgsi_build_property(1376full_prop->Property.PropertyName,1377header );13781379assert( full_prop->Property.NrTokens <= 8 + 1 );13801381for( i = 0; i < full_prop->Property.NrTokens - 1; i++ ) {1382struct tgsi_property_data *data;13831384if( maxsize <= size )1385return 0;1386data = (struct tgsi_property_data *) &tokens[size];1387size++;13881389*data = tgsi_build_property_data(1390full_prop->u[i].Data,1391property,1392header );1393}13941395return size;1396}13971398struct tgsi_full_src_register1399tgsi_full_src_register_from_dst(const struct tgsi_full_dst_register *dst)1400{1401struct tgsi_full_src_register src;1402src.Register = tgsi_default_src_register();1403src.Register.File = dst->Register.File;1404src.Register.Indirect = dst->Register.Indirect;1405src.Register.Dimension = dst->Register.Dimension;1406src.Register.Index = dst->Register.Index;1407src.Indirect = dst->Indirect;1408src.Dimension = dst->Dimension;1409src.DimIndirect = dst->DimIndirect;1410return src;1411}141214131414