Torque3D Documentation / _generateds / accuFeatureHLSL.cpp

accuFeatureHLSL.cpp

Engine/source/shaderGen/HLSL/accuFeatureHLSL.cpp

More...

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