customFeatureGLSL.cpp
Engine/source/shaderGen/GLSL/customFeatureGLSL.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 "customFeatureGLSL.h" 25#include "shaderGen/shaderFeature.h" 26#include "shaderGen/shaderOp.h" 27#include "shaderGen/featureMgr.h" 28//#include "materials/materialFeatureTypes.h" 29//#include "gfx/gfxDevice.h" 30//#include "materials/processedMaterial.h" 31 32//**************************************************************************** 33// Accu Texture 34//**************************************************************************** 35void CustomFeatureGLSL::processVert(Vector<ShaderComponent*>& componentList, 36 const MaterialFeatureData& fd) 37{ 38 meta = new MultiLine; 39 40 mFeatureData = fd; 41 mComponentList = componentList; 42 43 mOutputState = VertexOutput; 44 45 if (mOwner->isMethod("processVertGLSL")) 46 Con::executef(mOwner, "processVertGLSL"); 47 48 output = meta; 49} 50 51void CustomFeatureGLSL::processPix(Vector<ShaderComponent*>& componentList, 52 const MaterialFeatureData& fd) 53{ 54 meta = new MultiLine; 55 56 mFeatureData = fd; 57 mComponentList = componentList; 58 59 mOutputState = PixelOutput; 60 61 if (mOwner->isMethod("processPixelGLSL")) 62 Con::executef(mOwner, "processPixelGLSL"); 63 64 output = meta; 65} 66 67void CustomFeatureGLSL::setTexData(Material::StageData& stageDat, 68 const MaterialFeatureData& fd, 69 RenderPassData& passData, 70 U32& texIndex) 71{ 72 73 if (mOwner->isMethod("setTextureData")) 74 Con::executef(mOwner, "setTextureData"); 75} 76 77void CustomFeatureGLSL::addUniform(String name, String type, String defaultValue, U32 arraySize) 78{ 79 //do the var/arg fetching here 80 Var* newVar = (Var*)LangElement::find(name.c_str()); 81 if (!newVar) 82 { 83 VarHolder newVarHolder(name, type, ""); 84 newVarHolder.arraySize = arraySize; 85 newVarHolder.sampler = false; 86 newVarHolder.uniform = true; 87 newVarHolder.constSortPos = cspPotentialPrimitive; 88 89 mVars.push_back(newVarHolder); 90 91 mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str())); 92 } 93} 94 95void CustomFeatureGLSL::addSampler(String name, String type, U32 arraySize) 96{ 97 //do the var/arg fetching here 98 Var* newVar = (Var*)LangElement::find(name.c_str()); 99 if (!newVar) 100 { 101 //As far as I know, it's always SamplerState regardless of the texture's type 102 VarHolder newVarHolder(name, "SamplerState", ""); 103 newVarHolder.arraySize = arraySize; 104 newVarHolder.sampler = true; 105 newVarHolder.uniform = true; 106 newVarHolder.constNum = Var::getTexUnitNum(); // used as texture unit num here 107 108 mVars.push_back(newVarHolder); 109 110 mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str())); 111 } 112} 113 114void CustomFeatureGLSL::addTexture(String name, String type, String samplerState, U32 arraySize) 115{ 116 //do the var/arg fetching here 117 Var* newVar = (Var*)LangElement::find(name.c_str()); 118 if (!newVar) 119 { 120 //go find our sampler state var 121 U32 constNum = 0; 122 123 Var* samplerStateVar = (Var*)LangElement::find(samplerState.c_str()); 124 if (!samplerStateVar) 125 { 126 //check our holder vars 127 bool foundHolder = false; 128 for (U32 v = 0; v < mVars.size(); v++) 129 { 130 if (mVars[v].varName == samplerState) 131 { 132 constNum = mVars[v].constNum; 133 foundHolder = true; 134 break; 135 } 136 } 137 138 if (!foundHolder) 139 { 140 Con::errorf("CustomShaderFeature::addTexture: Unable to find texture's sampler state!"); 141 return; 142 } 143 } 144 else 145 { 146 constNum = samplerStateVar->constNum; 147 } 148 149 VarHolder newVarHolder(name, type, ""); 150 newVarHolder.arraySize = arraySize; 151 newVarHolder.texture = true; 152 newVarHolder.uniform = true; 153 newVarHolder.constNum = constNum; // used as texture unit num here 154 155 mVars.push_back(newVarHolder); 156 157 mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str())); 158 } 159} 160 161void CustomFeatureGLSL::addVariable(String name, String type, String defaultValue) 162{ 163 //do the var/arg fetching here 164 Var* newVar = (Var*)LangElement::find(name.c_str()); 165 if (!newVar) 166 { 167 if (!defaultValue.isEmpty()) 168 { 169 char declareStatement[128]; 170 dSprintf(declareStatement, 128, " @ = %s;\n", defaultValue.c_str()); 171 172 newVar = new Var(name, type); 173 LangElement* newVarDecl = new DecOp(newVar); 174 meta->addStatement(new GenOp(declareStatement, newVarDecl)); 175 } 176 else 177 { 178 VarHolder newVarHolder(name, type, defaultValue); 179 180 mVars.push_back(newVarHolder); 181 } 182 } 183} 184 185void CustomFeatureGLSL::addConnector(String name, String type, String elementName) 186{ 187 // grab connector texcoord register 188 ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(mComponentList[C_CONNECTOR]); 189 190 //Get element 191 S32 element = -1; 192 193 if (elementName == String("RT_POSITION")) 194 element = RT_POSITION; 195 else if (elementName == String("RT_NORMAL")) 196 element = RT_NORMAL; 197 else if (elementName == String("RT_BINORMAL")) 198 element = RT_BINORMAL; 199 else if (elementName == String("RT_TANGENT")) 200 element = RT_TANGENT; 201 else if (elementName == String("RT_TANGENTW")) 202 element = RT_TANGENTW; 203 else if (elementName == String("RT_COLOR")) 204 element = RT_COLOR; 205 else if (elementName == String("RT_TEXCOORD")) 206 element = RT_TEXCOORD; 207 else if (elementName == String("RT_VPOS")) 208 element = RT_VPOS; 209 else if (elementName == String("RT_SVPOSITION")) 210 element = RT_SVPOSITION; 211 else if (elementName == String("RT_BLENDINDICES")) 212 element = RT_BLENDINDICES; 213 else if (elementName == String("RT_BLENDWEIGHT")) 214 element = RT_BLENDWEIGHT; 215 216 if (element == -1) 217 { 218 Con::errorf("CustomShaderFeatureHLSL::addConnector - Invalid element type %s", elementName.c_str()); 219 return; 220 } 221 222 VarHolder newVarHolder(name, type, ""); 223 224 newVarHolder.elementId = element; 225 226 if (mOutputState == VertexOutput) 227 newVarHolder.structName = "OUT"; 228 else if (mOutputState == PixelOutput) 229 newVarHolder.structName = "IN"; 230 231 mVars.push_back(newVarHolder); 232} 233 234void CustomFeatureGLSL::addVertTexCoord(String name) 235{ 236 VarHolder newVarHolder(name, "", ""); 237 newVarHolder.texCoord = true; 238 239 mVars.push_back(newVarHolder); 240} 241 242void CustomFeatureGLSL::writeLine(String format, S32 argc, ConsoleValueRef * argv) 243{ 244 //do the var/arg fetching here 245 Vector<Var*> varList; 246 bool declarationStatement = false; 247 248 for (U32 i = 0; i < argc; i++) 249 { 250 String varName = argv[i].getStringValue(); 251 Var* newVar = (Var*)LangElement::find(varName.c_str()); 252 if (!newVar) 253 { 254 //ok, check our existing var holders, see if we just haven't utilized it yet 255 for (U32 v = 0; v < mVars.size(); v++) 256 { 257 if (mVars[v].varName == varName) 258 { 259 if (!mVars[v].texCoord) 260 { 261 if (mVars[v].elementId != -1) 262 { 263 ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(mComponentList[C_CONNECTOR]); 264 Var* newDeclVar = connectComp->getElement((RegisterType)mVars[v].elementId); 265 newDeclVar->setName(mVars[v].varName); 266 newDeclVar->setStructName(mVars[v].structName); 267 newDeclVar->setType(mVars[v].type); 268 269 newVar = newDeclVar; 270 } 271 else 272 { 273 Var* newDeclVar = new Var(mVars[v].varName, mVars[v].type); 274 275 newDeclVar->arraySize = mVars[v].arraySize; 276 newDeclVar->uniform = mVars[v].uniform; 277 newDeclVar->sampler = mVars[v].sampler; 278 newDeclVar->texture = mVars[v].texture; 279 newDeclVar->constNum = mVars[v].constNum; 280 newDeclVar->constSortPos = mVars[v].constSortPos; 281 282 if (!newDeclVar->uniform) 283 { 284 LangElement* newVarDecl = new DecOp(newDeclVar); 285 newVar = (Var*)newVarDecl; 286 287 declarationStatement = true; 288 } 289 else 290 { 291 newVar = newDeclVar; 292 } 293 } 294 } 295 else 296 { 297 newVar = getVertTexCoord(mVars[v].varName); 298 } 299 300 mVars.erase(v); 301 break; 302 } 303 } 304 305 if (!newVar) 306 { 307 //couldn't find that variable, bail out 308 Con::errorf("CustomShaderFeature::writeLine: unable to find variable %s, meaning it was not declared before being used!", argv[i].getStringValue()); 309 return; 310 } 311 } 312 313 varList.push_back(newVar); 314 } 315 316 //not happy about it, but do a trampoline here to pass along the args 317 318 switch (varList.size()) 319 { 320 case 0: 321 meta->addStatement(new GenOp(format + "\n")); 322 break; 323 case 1: 324 meta->addStatement(new GenOp(format + "\n", varList[0])); 325 break; 326 case 2: 327 meta->addStatement(new GenOp(format + "\n", varList[0], varList[1])); 328 break; 329 case 3: 330 meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2])); 331 break; 332 case 4: 333 meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3])); 334 break; 335 case 5: 336 meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3], varList[4])); 337 break; 338 } 339} 340 341bool CustomFeatureGLSL::hasFeature(String name) 342{ 343 for (U32 i = 0; i < mFeatureData.materialFeatures.getCount(); i++) 344 { 345 String featureName = mFeatureData.materialFeatures.getAt(i).getName(); 346 if (name == featureName) 347 return true; 348 } 349 350 return false; 351} 352