shaderOp.cpp
Engine/source/shaderGen/shaderOp.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 <stdarg.h> 26 27 28#include "shaderOp.h" 29 30//************************************************************************** 31// Shader Operations 32//************************************************************************** 33ShaderOp::ShaderOp( LangElement *in1, LangElement *in2 ) 34{ 35 mInput[0] = in1; 36 mInput[1] = in2; 37} 38 39//************************************************************************** 40// Declaration Operation - for variables 41//************************************************************************** 42DecOp::DecOp( Var *in1 ) : Parent( in1, NULL ) 43{ 44 mInput[0] = in1; 45} 46 47//-------------------------------------------------------------------------- 48// Print 49//-------------------------------------------------------------------------- 50void DecOp::print( Stream &stream ) 51{ 52 Var *var = dynamic_cast<Var*>( mInput[0] ); 53 54 WRITESTR( (char*)var->type ); 55 WRITESTR( " " ); 56 57 mInput[0]->print( stream ); 58} 59 60//************************************************************************** 61// Echo operation - deletes incoming statement! 62//************************************************************************** 63EchoOp::EchoOp( const char * statement ) : Parent( NULL, NULL ) 64{ 65 mStatement = statement; 66} 67 68//-------------------------------------------------------------------------- 69// Destructor 70//-------------------------------------------------------------------------- 71EchoOp::~EchoOp() 72{ 73 delete [] mStatement; 74} 75 76//-------------------------------------------------------------------------- 77// Print 78//-------------------------------------------------------------------------- 79void EchoOp::print( Stream &stream ) 80{ 81 WRITESTR( mStatement ); 82} 83 84//************************************************************************** 85// Index operation 86//************************************************************************** 87IndexOp::IndexOp( Var* var, U32 index ) : Parent( NULL, NULL ) 88{ 89 mInput[0] = var; 90 mIndex = index; 91} 92 93//-------------------------------------------------------------------------- 94// Print 95//-------------------------------------------------------------------------- 96void IndexOp::print( Stream &stream ) 97{ 98 Var* var = dynamic_cast<Var*>(mInput[0]); 99 100 mInput[0]->print(stream); 101 if (var->arraySize > 1) 102 { 103 WRITESTR(String::ToString("[%d]", mIndex)); 104 } 105} 106 107 108//************************************************************************** 109// General operation 110//************************************************************************** 111GenOp::GenOp( const char * statement, ... ) : Parent( NULL, NULL ) 112{ 113 VECTOR_SET_ASSOCIATION( mElemList ); 114 115 va_list args; 116 va_start(args, statement); 117 118 char* lastEntry = (char*)statement; 119 120 while( 1 ) 121 { 122 // search 'statement' for @ symbol 123 char * str = dStrstr( lastEntry, (char *)"@" ); 124 125 if( !str ) 126 { 127 // not found, handle end of line 128 str = (char*)&statement[ dStrlen( (char*)statement ) ]; 129 130 U32 diff = str - lastEntry + 1; 131 if( diff == 1 ) break; 132 133 char * newStr = new char[diff]; 134 135 dMemcpy( (void*)newStr, lastEntry, diff ); 136 137 mElemList.push_back( new EchoOp( newStr ) ); 138 139 break; 140 } 141 142 // create and store statement fragment 143 U32 diff = str - lastEntry + 1; 144 145 if( diff == 1 ) 146 { 147 // store langElement 148 LangElement *elem = va_arg(args, LangElement* ); 149 AssertFatal( elem, "NULL arguement." ); 150 mElemList.push_back( elem ); 151 lastEntry++; 152 continue; 153 } 154 155 char * newStr = new char[diff]; 156 157 dMemcpy( (void*)newStr, lastEntry, diff ); 158 newStr[diff-1] = '\0'; 159 160 lastEntry = str + 1; 161 162 mElemList.push_back( new EchoOp( newStr ) ); 163 164 // store langElement 165 LangElement *elem = va_arg(args, LangElement* ); 166 AssertFatal( elem, "NULL argument." ); 167 mElemList.push_back( elem ); 168 } 169 170 va_end( args ); 171} 172 173//-------------------------------------------------------------------------- 174// Print 175//-------------------------------------------------------------------------- 176void GenOp::print( Stream &stream ) 177{ 178 for( U32 i=0; i<mElemList.size(); i++ ) 179 { 180 mElemList[i]->print( stream ); 181 } 182} 183