depthGLSL.cpp

Engine/source/shaderGen/GLSL/depthGLSL.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 "platform/platform.h"
 25#include "shaderGen/GLSL/depthGLSL.h"
 26
 27#include "materials/materialFeatureTypes.h"
 28#include "materials/materialFeatureData.h"
 29#include "terrain/terrFeatureTypes.h"
 30
 31void EyeSpaceDepthOutGLSL::processVert(   Vector<ShaderComponent*> &componentList, 
 32                                          const MaterialFeatureData &fd )
 33{
 34   MultiLine *meta = new MultiLine;
 35   output = meta;
 36
 37   // grab output
 38   ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
 39   Var *outWSEyeVec = connectComp->getElement( RT_TEXCOORD );
 40   outWSEyeVec->setName( "wsEyeVec" );
 41   outWSEyeVec->setStructName( "OUT" );
 42
 43   // grab incoming vert position   
 44   Var *wsPosition = new Var( "depthPos", "float3" );
 45   getWsPosition( componentList, fd.features[MFT_UseInstancing], meta, new DecOp( wsPosition ) );
 46
 47   Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
 48   if( !eyePos )
 49   {
 50      eyePos = new Var;
 51      eyePos->setType("float3");
 52      eyePos->setName("eyePosWorld");
 53      eyePos->uniform = true;
 54      eyePos->constSortPos = cspPass;
 55   }
 56
 57   meta->addStatement( new GenOp( "   @ = float4( @.xyz - @, 1 );\r\n", outWSEyeVec, wsPosition, eyePos ) );
 58}
 59
 60void EyeSpaceDepthOutGLSL::processPix( Vector<ShaderComponent*> &componentList, 
 61                                       const MaterialFeatureData &fd )
 62{      
 63   MultiLine *meta = new MultiLine;
 64
 65   // grab connector position
 66   ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
 67   Var *wsEyeVec = connectComp->getElement( RT_TEXCOORD );
 68   wsEyeVec->setName( "wsEyeVec" );
 69   wsEyeVec->setStructName( "IN" );
 70   wsEyeVec->setType( "float4" );
 71   wsEyeVec->uniform = false;
 72
 73   // get shader constants
 74   Var *vEye = new Var;
 75   vEye->setType("float3");
 76   vEye->setName("vEye");
 77   vEye->uniform = true;
 78   vEye->constSortPos = cspPass;
 79
 80   // Expose the depth to the depth format feature
 81   Var *depthOut = new Var;
 82   depthOut->setType("float");
 83   depthOut->setName(getOutputVarName());
 84
 85   LangElement *depthOutDecl = new DecOp( depthOut );
 86
 87   meta->addStatement( new GenOp( "#ifndef CUBE_SHADOW_MAP\r\n" ) );
 88   
 89   if (fd.features.hasFeature(MFT_TerrainBaseMap))
 90      meta->addStatement(new GenOp("   @ =min(0.9999, dot(@, (@.xyz / @.w)));\r\n", depthOutDecl, vEye, wsEyeVec, wsEyeVec));
 91   else
 92      meta->addStatement(new GenOp("   @ = dot(@, (@.xyz / @.w));\r\n", depthOutDecl, vEye, wsEyeVec, wsEyeVec));
 93      
 94   meta->addStatement( new GenOp( "#else\r\n" ) );
 95
 96   Var *farDist = (Var*)Var::find( "oneOverFarplane" );
 97   if ( !farDist )
 98   {
 99      farDist = new Var;
100      farDist->setType("float4");
101      farDist->setName("oneOverFarplane");
102      farDist->uniform = true;
103      farDist->constSortPos = cspPass;
104   }
105
106   meta->addStatement( new GenOp( "   @ = length( @.xyz / @.w ) * @.x;\r\n", depthOutDecl, wsEyeVec, wsEyeVec, farDist ) );      
107   meta->addStatement( new GenOp( "#endif\r\n" ) );
108
109   // If there isn't an output conditioner for the pre-pass, than just write
110   // out the depth to rgba and return.
111   if( !fd.features[MFT_DeferredConditioner] )
112      meta->addStatement( new GenOp( "   @;\r\n", assignColor( new GenOp( "float4(float3(@),1)", depthOut ), Material::None ) ) );
113   
114   output = meta;
115}
116
117ShaderFeature::Resources EyeSpaceDepthOutGLSL::getResources( const MaterialFeatureData &fd )
118{
119   Resources temp;
120
121   // Passing from VS->PS:
122   // - world space position (wsPos)
123   temp.numTexReg = 1; 
124
125   return temp; 
126}
127
128
129void DepthOutGLSL::processVert(  Vector<ShaderComponent*> &componentList, 
130                                 const MaterialFeatureData &fd )
131{
132   ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
133
134   // Grab the output vert.
135   Var *outPosition = (Var*)LangElement::find( "gl_Position" ); //hpos
136
137   // Grab our output depth.
138   Var *outDepth = connectComp->getElement( RT_TEXCOORD );
139   outDepth->setName( "depth" );
140   outDepth->setStructName( "OUT" );
141   outDepth->setType( "float" );
142
143   output = new GenOp( "   @ = @.z / @.w;\r\n", outDepth, outPosition, outPosition );
144}
145
146void DepthOutGLSL::processPix(   Vector<ShaderComponent*> &componentList, 
147                                 const MaterialFeatureData &fd )
148{
149   ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
150
151   // grab connector position
152   Var *depthVar = connectComp->getElement( RT_TEXCOORD );
153   depthVar->setName( "depth" );
154   depthVar->setStructName( "IN" );
155   depthVar->setType( "float" );
156   depthVar->uniform = false;
157
158   /*
159   // Expose the depth to the depth format feature
160   Var *depthOut = new Var;
161   depthOut->setType("float");
162   depthOut->setName(getOutputVarName());
163   */
164
165   LangElement *depthOut = new GenOp( "float4( @, 0, 0, 1 )", depthVar );
166
167   output = new GenOp( "   @;\r\n", assignColor( depthOut, Material::None ) );
168}
169
170ShaderFeature::Resources DepthOutGLSL::getResources( const MaterialFeatureData &fd )
171{
172   // We pass the depth to the pixel shader.
173   Resources temp;
174   temp.numTexReg = 1; 
175
176   return temp; 
177}
178