langElement.cpp
Engine/source/shaderGen/langElement.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "core/strings/stringFunctions.h" 25#include "core/util/str.h" 26#include "gfx/gfxDevice.h" 27#include "langElement.h" 28 29//************************************************************************** 30// Language element 31//************************************************************************** 32Vector<LangElement*> LangElement::elementList( __FILE__, __LINE__ ); 33 34//-------------------------------------------------------------------------- 35// Constructor 36//-------------------------------------------------------------------------- 37LangElement::LangElement() 38{ 39 elementList.push_back( this ); 40 41 static U32 tempNum = 0; 42 dSprintf( (char*)name, sizeof(name), "tempName%d", tempNum++ ); 43} 44 45//-------------------------------------------------------------------------- 46// Find element of specified name 47//-------------------------------------------------------------------------- 48LangElement * LangElement::find( const char *name ) 49{ 50 for( U32 i=0; i<elementList.size(); i++ ) 51 { 52 if( !String::compare( (char*)elementList[i]->name, name ) ) 53 { 54 return elementList[i]; 55 } 56 } 57 58 return NULL; 59} 60 61//-------------------------------------------------------------------------- 62// Delete existing elements 63//-------------------------------------------------------------------------- 64void LangElement::deleteElements() 65{ 66 for( U32 i=0; i<elementList.size(); i++ ) 67 { 68 delete elementList[i]; 69 } 70 71 elementList.setSize( 0 ); 72 73} 74 75//-------------------------------------------------------------------------- 76// Set name 77//-------------------------------------------------------------------------- 78void LangElement::setName(const char* newName ) 79{ 80 dStrncpy( ( char* ) name, newName, sizeof( name ) ); 81 name[ sizeof( name ) - 1 ] = '\0'; 82} 83 84//************************************************************************** 85// Variable 86//************************************************************************** 87U32 Var::texUnitCount = 0; 88 89Var::Var() 90{ 91 dStrcpy( (char*)type, "float4", 32 ); 92 structName[0] = '\0'; 93 connectName[0] = '\0'; 94 constSortPos = cspUninit; 95 constNum = 0; 96 texCoordNum = 0; 97 uniform = false; 98 vertData = false; 99 connector = false; 100 sampler = false; 101 arraySize = 1; 102 texture = false; 103 rank = 0; 104} 105 106Var::Var( const char *inName, const char *inType ) 107{ 108 structName[0] = '\0'; 109 connectName[0] = '\0'; 110 uniform = false; 111 vertData = false; 112 connector = false; 113 sampler = false; 114 texCoordNum = 0; 115 constSortPos = cspUninit; 116 constNum = 0; 117 arraySize = 1; 118 texture = false; 119 rank = 0; 120 121 setName( inName ); 122 setType( inType ); 123} 124 125void Var::setUniform(const String& constType, const String& constName, ConstantSortPosition sortPos) 126{ 127 uniform = true; 128 setType(constType.c_str()); 129 setName(constName.c_str()); 130 constSortPos = cspPass; 131} 132 133//-------------------------------------------------------------------------- 134// Set struct name 135//-------------------------------------------------------------------------- 136void Var::setStructName(const char* newName ) 137{ 138 dStrncpy( ( char* ) structName, newName, sizeof( structName ) ); 139 structName[ sizeof( structName ) - 1 ] = '\0'; 140} 141 142//-------------------------------------------------------------------------- 143// Set connect name 144//-------------------------------------------------------------------------- 145void Var::setConnectName(const char* newName ) 146{ 147 dStrncpy( ( char* ) connectName, newName, sizeof( connectName ) ); 148 connectName[ sizeof( connectName ) - 1 ] = '\0'; 149} 150 151//-------------------------------------------------------------------------- 152// Set type 153//-------------------------------------------------------------------------- 154void Var::setType(const char *newType ) 155{ 156 dStrncpy( ( char* ) type, newType, sizeof( type ) ); 157 type[ sizeof( type ) - 1 ] = '\0'; 158} 159 160//-------------------------------------------------------------------------- 161// print 162//-------------------------------------------------------------------------- 163void Var::print( Stream &stream ) 164{ 165 if( structName[0] != '\0' ) 166 { 167 stream.write( dStrlen((char*)structName), structName ); 168 if(GFX->getAdapterType() == OpenGL) 169 stream.write( 1, "_" ); 170 else 171 stream.write( 1, "." ); 172 } 173 174 stream.write( dStrlen((char*)name), name ); 175} 176 177//-------------------------------------------------------------------------- 178// Get next available texture unit number 179//-------------------------------------------------------------------------- 180U32 Var::getTexUnitNum(U32 numElements) 181{ 182 U32 ret = texUnitCount; 183 texUnitCount += numElements; 184 return ret; 185} 186 187//-------------------------------------------------------------------------- 188// Reset 189//-------------------------------------------------------------------------- 190void Var::reset() 191{ 192 texUnitCount = 0; 193} 194 195//************************************************************************** 196// Multi line statement 197//************************************************************************** 198void MultiLine::addStatement( LangElement *elem ) 199{ 200 AssertFatal( elem, "Attempting to add empty statement" ); 201 202 mStatementList.push_back( elem ); 203} 204 205//-------------------------------------------------------------------------- 206// Print 207//-------------------------------------------------------------------------- 208void MultiLine::print( Stream &stream ) 209{ 210 for( U32 i=0; i<mStatementList.size(); i++ ) 211 { 212 mStatementList[i]->print( stream ); 213 } 214} 215