accuFeatureGLSL.cpp
Engine/source/shaderGen/GLSL/accuFeatureGLSL.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 "shaderGen/GLSL/accuFeatureGLSL.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 AccuTexFeatGLSL::processVert(Vector<ShaderComponent*> &componentList, 36 const MaterialFeatureData &fd ) 37{ 38 MultiLine *meta = new MultiLine; 39 getOutTexCoord( "texCoord", 40 "vec2", 41 false, 42 meta, 43 componentList ); 44 45 getOutObjToTangentSpace( componentList, meta, fd ); 46 addOutAccuVec( componentList, meta ); 47 48 output = meta; 49} 50 51void AccuTexFeatGLSL::processPix(Vector<ShaderComponent*> &componentList, 52 const MaterialFeatureData &fd ) 53{ 54 MultiLine *meta = new MultiLine; 55 56 output = meta; 57 58 // OUT.col 59 Var *color = (Var*) LangElement::find( "col1" ); 60 if (!color) 61 { 62 output = new GenOp(" //NULL COLOR!"); 63 return; 64 } 65 66 // accu map 67 Var *accuMap = new Var; 68 accuMap->setType( "sampler2D" ); 69 accuMap->setName( "accuMap" ); 70 accuMap->uniform = true; 71 accuMap->sampler = true; 72 accuMap->constNum = Var::getTexUnitNum(); // used as texture unit num here 73 74 // accuColor var 75 Var *accuColor = new Var; 76 accuColor->setType( "vec4" ); 77 accuColor->setName( "accuColor" ); 78 LangElement *colorAccuDecl = new DecOp( accuColor ); 79 80 // plc (placement) 81 Var *accuPlc = new Var; 82 accuPlc->setType( "vec4" ); 83 accuPlc->setName( "plc" ); 84 LangElement *plcAccu = new DecOp( accuPlc ); 85 86 // accu constants 87 Var *accuScale = (Var*)LangElement::find( "accuScale" ); 88 if ( !accuScale ) { 89 accuScale = new Var; 90 accuScale->setType( "float" ); 91 accuScale->setName( "accuScale" ); 92 accuScale->uniform = true; 93 accuScale->sampler = false; 94 accuScale->constSortPos = cspPotentialPrimitive; 95 } 96 Var *accuDirection = (Var*)LangElement::find( "accuDirection" ); 97 if ( !accuDirection ) { 98 accuDirection = new Var; 99 accuDirection->setType( "float" ); 100 accuDirection->setName( "accuDirection" ); 101 accuDirection->uniform = true; 102 accuDirection->sampler = false; 103 accuDirection->constSortPos = cspPotentialPrimitive; 104 } 105 Var *accuStrength = (Var*)LangElement::find( "accuStrength" ); 106 if ( !accuStrength ) { 107 accuStrength = new Var; 108 accuStrength->setType( "float" ); 109 accuStrength->setName( "accuStrength" ); 110 accuStrength->uniform = true; 111 accuStrength->sampler = false; 112 accuStrength->constSortPos = cspPotentialPrimitive; 113 } 114 Var *accuCoverage = (Var*)LangElement::find( "accuCoverage" ); 115 if ( !accuCoverage ) { 116 accuCoverage = new Var; 117 accuCoverage->setType( "float" ); 118 accuCoverage->setName( "accuCoverage" ); 119 accuCoverage->uniform = true; 120 accuCoverage->sampler = false; 121 accuCoverage->constSortPos = cspPotentialPrimitive; 122 } 123 Var *accuSpecular = (Var*)LangElement::find( "accuSpecular" ); 124 if ( !accuSpecular ) { 125 accuSpecular = new Var; 126 accuSpecular->setType( "float" ); 127 accuSpecular->setName( "accuSpecular" ); 128 accuSpecular->uniform = true; 129 accuSpecular->sampler = false; 130 accuSpecular->constSortPos = cspPotentialPrimitive; 131 } 132 133 Var *inTex = getInTexCoord( "texCoord", "vec2", componentList ); 134 Var *accuVec = getInTexCoord( "accuVec", "vec3", componentList ); 135 Var *bumpNorm = (Var *)LangElement::find( "bumpSample" ); 136 if( bumpNorm == NULL ) { 137 bumpNorm = (Var *)LangElement::find( "bumpNormal" ); 138 if (!bumpNorm) 139 { 140 output = new GenOp(" //NULL bumpNormal!"); 141 return; 142 } 143 } 144 145 // get the accu pixel color 146 meta->addStatement( new GenOp( " @ = tex2D(@, @ * @);\r\n", colorAccuDecl, accuMap, inTex, accuScale ) ); 147 148 // scale up normals 149 meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 0.5;\r\n", bumpNorm, bumpNorm ) ); 150 151 // assign direction 152 meta->addStatement( new GenOp( " @.z *= @*2.0;\r\n", accuVec, accuDirection ) ); 153 154 // saturate based on strength 155 meta->addStatement( new GenOp( " @ = saturate( float4(dot( float3(@), @.xyz * pow(@, 5) ) ));\r\n", plcAccu, bumpNorm, accuVec, accuStrength ) ); 156 157 // add coverage 158 meta->addStatement( new GenOp( " @.a += (2 * pow(@/2, 5)) - 0.5;\r\n", accuPlc, accuCoverage ) ); 159 160 // clamp to a sensible value 161 meta->addStatement( new GenOp( " @.a = clamp(@.a, 0, 1);\r\n", accuPlc, accuPlc ) ); 162 163 // light 164 Var *lightColor = (Var*) LangElement::find( "d_lightcolor" ); 165 if(lightColor != NULL) 166 meta->addStatement( new GenOp( " @ *= float4(@, 1.0);\r\n\r\n", accuColor, lightColor ) ); 167 168 // lerp with current pixel - use the accu alpha as well 169 meta->addStatement( new GenOp( " @ = mix( @, @, @.a * @.a);\r\n", color, color, accuColor, accuPlc, accuColor ) ); 170 171 // the result should always be opaque 172 meta->addStatement( new GenOp( " @.a = 1.0;\r\n", color ) ); 173} 174 175void AccuTexFeatGLSL::setTexData(Material::StageData &stageDat, 176 const MaterialFeatureData &fd, 177 RenderPassData &passData, 178 U32 &texIndex ) 179{ 180 //GFXTextureObject *tex = stageDat.getTex( MFT_AccuMap ); 181 //if ( tex ) 182 //{ 183 passData.mSamplerNames[texIndex] = "accuMap"; 184 passData.mTexType[ texIndex++ ] = Material::AccuMap; 185 //passData.mTexSlot[ texIndex++ ].texObject = tex; 186 //} 187} 188 189 190void AccuTexFeatGLSL::getAccuVec(MultiLine *meta, LangElement *accuVec) 191{ 192 // Get the transform to world space. 193 Var *objTrans = (Var*)LangElement::find( "objTrans" ); 194 if ( !objTrans ) 195 { 196 objTrans = new Var; 197 objTrans->setType( "float4x4" ); 198 objTrans->setName( "objTrans" ); 199 objTrans->uniform = true; 200 objTrans->constSortPos = cspPrimitive; 201 } 202 203 // accu obj trans 204 Var *aobjTrans = new Var; 205 aobjTrans->setType( "float4x4" ); 206 aobjTrans->setName( "accuObjTrans" ); 207 LangElement *accuObjTransDecl = new DecOp( aobjTrans ); 208 209 Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" ); 210 211 Var *tav = new Var; 212 tav->setType( "float4" ); 213 tav->setName( "tAccuVec" ); 214 LangElement *tavDecl = new DecOp( tav ); 215 216 meta->addStatement( new GenOp( " @ = float4(0,0,1,0);\r\n", tavDecl ) ); 217 meta->addStatement( new GenOp( " @ = transpose(@);\r\n", accuObjTransDecl, objTrans ) ); 218 meta->addStatement( new GenOp( " @ = tMul(@, @);\r\n", tav, aobjTrans, tav ) ); 219 meta->addStatement( new GenOp( " @.xyz = tMul(@, @.xyz);\r\n", tav, outObjToTangentSpace, tav ) ); 220 meta->addStatement( new GenOp( " @.y *= -1;\r\n", tav ) ); 221 meta->addStatement( new GenOp( " @ = @.xyz;\r\n", accuVec, tav ) ); 222} 223 224Var* AccuTexFeatGLSL::addOutAccuVec(Vector<ShaderComponent*> &componentList, MultiLine *meta) 225{ 226 Var *outAccuVec = (Var*)LangElement::find( "accuVec" ); 227 if ( !outAccuVec ) 228 { 229 // Setup the connector. 230 ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); 231 outAccuVec = connectComp->getElement( RT_TEXCOORD ); 232 outAccuVec->setName( "accuVec" ); 233 outAccuVec->setStructName( "OUT" ); 234 outAccuVec->setType( "float3" ); 235 236 getAccuVec( meta, outAccuVec ); 237 } 238 239 return outAccuVec; 240} 241