processedFFMaterial.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "materials/processedFFMaterial.h"
  24. #include "gfx/sim/cubemapData.h"
  25. #include "materials/sceneData.h"
  26. #include "materials/customMaterialDefinition.h"
  27. #include "materials/materialFeatureTypes.h"
  28. #include "gfx/sim/gfxStateBlockData.h"
  29. #include "gfx/gfxDevice.h"
  30. #include "gfx/genericConstBuffer.h"
  31. #include "materials/materialParameters.h"
  32. #include "lighting/lightInfo.h"
  33. #include "scene/sceneRenderState.h"
  34. #include "core/util/safeDelete.h"
  35. #include "math/util/matrixSet.h"
  36. class FFMaterialParameterHandle : public MaterialParameterHandle
  37. {
  38. public:
  39. virtual ~FFMaterialParameterHandle() {}
  40. virtual const String& getName() const { return mName; }
  41. virtual bool isValid() const { return false; }
  42. virtual S32 getSamplerRegister( U32 pass ) const { return -1; }
  43. private:
  44. String mName;
  45. };
  46. ProcessedFFMaterial::ProcessedFFMaterial()
  47. {
  48. VECTOR_SET_ASSOCIATION( mParamDesc );
  49. _construct();
  50. }
  51. ProcessedFFMaterial::ProcessedFFMaterial(Material &mat, const bool isLightingMaterial)
  52. {
  53. VECTOR_SET_ASSOCIATION( mParamDesc );
  54. _construct();
  55. mMaterial = &mat;
  56. mIsLightingMaterial = isLightingMaterial;
  57. }
  58. void ProcessedFFMaterial::_construct()
  59. {
  60. mHasSetStageData = false;
  61. mHasGlow = false;
  62. mHasAccumulation = false;
  63. mIsLightingMaterial = false;
  64. mDefaultHandle = new FFMaterialParameterHandle();
  65. mDefaultParameters = new MaterialParameters();
  66. mCurrentParams = mDefaultParameters;
  67. }
  68. ProcessedFFMaterial::~ProcessedFFMaterial()
  69. {
  70. SAFE_DELETE(mDefaultParameters);
  71. SAFE_DELETE( mDefaultHandle );
  72. }
  73. void ProcessedFFMaterial::_createPasses( U32 stageNum, const FeatureSet &features )
  74. {
  75. FixedFuncFeatureData featData;
  76. _determineFeatures(stageNum, featData, features);
  77. // Just create a simple pass!
  78. _addPass(0, featData);
  79. mFeatures.clear();
  80. if ( featData.features[FixedFuncFeatureData::DiffuseMap] )
  81. mFeatures.addFeature( MFT_DiffuseMap );
  82. if ( featData.features[FixedFuncFeatureData::LightMap] )
  83. mFeatures.addFeature( MFT_LightMap );
  84. if ( featData.features[FixedFuncFeatureData::ToneMap] )
  85. mFeatures.addFeature( MFT_ToneMap );
  86. }
  87. void ProcessedFFMaterial::_determineFeatures( U32 stageNum,
  88. FixedFuncFeatureData& featData,
  89. const FeatureSet &features )
  90. {
  91. if ( mStages[stageNum].getTex( MFT_DiffuseMap ) )
  92. featData.features[FixedFuncFeatureData::DiffuseMap] = true;
  93. if ( features.hasFeature( MFT_LightMap ) )
  94. featData.features[FixedFuncFeatureData::LightMap] = true;
  95. if ( features.hasFeature( MFT_ToneMap ))
  96. featData.features[FixedFuncFeatureData::ToneMap] = true;
  97. }
  98. U32 ProcessedFFMaterial::getNumStages()
  99. {
  100. // Loops through all stages to determine how many stages we actually use
  101. U32 numStages = 0;
  102. U32 i;
  103. for( i=0; i<Material::MAX_STAGES; i++ )
  104. {
  105. // Assume stage is inactive
  106. bool stageActive = false;
  107. // Cubemaps only on first stage
  108. if( i == 0 )
  109. {
  110. // If we have a cubemap the stage is active
  111. if( mMaterial->mCubemapData || mMaterial->mDynamicCubemap )
  112. {
  113. numStages++;
  114. continue;
  115. }
  116. }
  117. // If we have a texture for the a feature the
  118. // stage is active.
  119. if ( mStages[i].hasValidTex() )
  120. stageActive = true;
  121. // If we have a Material that is vertex lit
  122. // then it may not have a texture
  123. if( mMaterial->mVertLit[i] )
  124. {
  125. stageActive = true;
  126. }
  127. // Increment the number of active stages
  128. numStages += stageActive;
  129. }
  130. return numStages;
  131. }
  132. bool ProcessedFFMaterial::setupPass( SceneRenderState *state, const SceneData &sgData, U32 pass )
  133. {
  134. PROFILE_SCOPE( ProcessedFFMaterial_SetupPass );
  135. // Make sure we have a pass
  136. if(pass >= mPasses.size())
  137. return false;
  138. _setRenderState( state, sgData, pass );
  139. // Bind our textures
  140. setTextureStages( state, sgData, pass );
  141. return true;
  142. }
  143. void ProcessedFFMaterial::setTextureStages(SceneRenderState * state, const SceneData& sgData, U32 pass)
  144. {
  145. // We may need to do some trickery in here for fixed function, this is just copy/paste from MatInstance
  146. #ifdef TORQUE_DEBUG
  147. AssertFatal( pass<mPasses.size(), "Pass out of bounds" );
  148. #endif
  149. RenderPassData *rpd = mPasses[pass];
  150. for( U32 i=0; i<rpd->mNumTex; i++ )
  151. {
  152. U32 currTexFlag = rpd->mTexType[i];
  153. if (!LIGHTMGR || !LIGHTMGR->setTextureStage(sgData, currTexFlag, i, NULL, NULL))
  154. {
  155. switch( currTexFlag )
  156. {
  157. case Material::NoTexture:
  158. if (rpd->mTexSlot[i].texObject)
  159. GFX->setTexture( i, rpd->mTexSlot[i].texObject );
  160. break;
  161. case Material::NormalizeCube:
  162. GFX->setCubeTexture(i, Material::GetNormalizeCube());
  163. break;
  164. case Material::Lightmap:
  165. GFX->setTexture( i, sgData.lightmap );
  166. break;
  167. case Material::Cube:
  168. // TODO: Is this right?
  169. GFX->setTexture( i, rpd->mTexSlot[0].texObject );
  170. break;
  171. case Material::SGCube:
  172. // No cubemap support just yet
  173. //GFX->setCubeTexture( i, sgData.cubemap );
  174. GFX->setTexture( i, rpd->mTexSlot[0].texObject );
  175. break;
  176. case Material::BackBuff:
  177. GFX->setTexture( i, sgData.backBuffTex );
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. MaterialParameters* ProcessedFFMaterial::allocMaterialParameters()
  184. {
  185. return new MaterialParameters();
  186. }
  187. MaterialParameters* ProcessedFFMaterial::getDefaultMaterialParameters()
  188. {
  189. return mDefaultParameters;
  190. }
  191. MaterialParameterHandle* ProcessedFFMaterial::getMaterialParameterHandle(const String& name)
  192. {
  193. return mDefaultHandle;
  194. }
  195. void ProcessedFFMaterial::setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass)
  196. {
  197. GFX->setWorldMatrix(matrixSet.getObjectToWorld());
  198. GFX->setViewMatrix(matrixSet.getWorldToCamera());
  199. GFX->setProjectionMatrix(matrixSet.getCameraToScreen());
  200. }
  201. void ProcessedFFMaterial::setSceneInfo(SceneRenderState * state, const SceneData& sgData, U32 pass)
  202. {
  203. _setPrimaryLightInfo(*sgData.objTrans, sgData.lights[0], pass);
  204. _setSecondaryLightInfo(*sgData.objTrans, sgData.lights[1]);
  205. }
  206. void ProcessedFFMaterial::_setPrimaryLightInfo(const MatrixF &_objTrans, LightInfo* light, U32 pass)
  207. {
  208. // Just in case
  209. GFX->setGlobalAmbientColor(LinearColorF(0.0f, 0.0f, 0.0f, 1.0f));
  210. if ( light->getType() == LightInfo::Ambient )
  211. {
  212. // Ambient light
  213. GFX->setGlobalAmbientColor( light->getAmbient() );
  214. return;
  215. }
  216. GFX->setLight(0, NULL);
  217. GFX->setLight(1, NULL);
  218. // This is a quick hack that lets us use FF lights
  219. GFXLightMaterial lightMat;
  220. lightMat.ambient = LinearColorF(1.0f, 1.0f, 1.0f, 1.0f);
  221. lightMat.diffuse = LinearColorF(1.0f, 1.0f, 1.0f, 1.0f);
  222. lightMat.emissive = LinearColorF(0.0f, 0.0f, 0.0f, 0.0f);
  223. lightMat.specular = LinearColorF(0.0f, 0.0f, 0.0f, 0.0f);
  224. lightMat.shininess = 128.0f;
  225. GFX->setLightMaterial(lightMat);
  226. // set object transform
  227. MatrixF objTrans = _objTrans;
  228. objTrans.inverse();
  229. // fill in primary light
  230. //-------------------------
  231. GFXLightInfo xlatedLight;
  232. light->setGFXLight(&xlatedLight);
  233. Point3F lightPos = light->getPosition();
  234. Point3F lightDir = light->getDirection();
  235. objTrans.mulP(lightPos);
  236. objTrans.mulV(lightDir);
  237. xlatedLight.mPos = lightPos;
  238. xlatedLight.mDirection = lightDir;
  239. GFX->setLight(0, &xlatedLight);
  240. }
  241. void ProcessedFFMaterial::_setSecondaryLightInfo(const MatrixF &_objTrans, LightInfo* light)
  242. {
  243. // set object transform
  244. MatrixF objTrans = _objTrans;
  245. objTrans.inverse();
  246. // fill in secondary light
  247. //-------------------------
  248. GFXLightInfo xlatedLight;
  249. light->setGFXLight(&xlatedLight);
  250. Point3F lightPos = light->getPosition();
  251. Point3F lightDir = light->getDirection();
  252. objTrans.mulP(lightPos);
  253. objTrans.mulV(lightDir);
  254. xlatedLight.mPos = lightPos;
  255. xlatedLight.mDirection = lightDir;
  256. GFX->setLight(1, &xlatedLight);
  257. }
  258. bool ProcessedFFMaterial::init( const FeatureSet &features,
  259. const GFXVertexFormat *vertexFormat,
  260. const MatFeaturesDelegate &featuresDelegate )
  261. {
  262. TORQUE_UNUSED( vertexFormat );
  263. TORQUE_UNUSED( featuresDelegate );
  264. _setStageData();
  265. // Just create a simple pass
  266. _createPasses(0, features);
  267. _initRenderPassDataStateBlocks();
  268. mStateHint.init( this );
  269. return true;
  270. }
  271. void ProcessedFFMaterial::_addPass(U32 stageNum, FixedFuncFeatureData& featData)
  272. {
  273. U32 numTex = 0;
  274. // Just creates a simple pass, but it can still glow!
  275. RenderPassData rpd;
  276. // Base texture, texunit 0
  277. if(featData.features[FixedFuncFeatureData::DiffuseMap])
  278. {
  279. rpd.mTexSlot[0].texObject = mStages[stageNum].getTex( MFT_DiffuseMap );
  280. rpd.mTexType[0] = Material::NoTexture;
  281. numTex++;
  282. }
  283. // lightmap, texunit 1
  284. if(featData.features[FixedFuncFeatureData::LightMap])
  285. {
  286. rpd.mTexType[1] = Material::Lightmap;
  287. numTex++;
  288. }
  289. rpd.mNumTex = numTex;
  290. rpd.mStageNum = stageNum;
  291. rpd.mGlow = false;
  292. mPasses.push_back( new RenderPassData(rpd) );
  293. }
  294. void ProcessedFFMaterial::_setPassBlendOp()
  295. {
  296. }
  297. void ProcessedFFMaterial::_initPassStateBlock( RenderPassData *rpd, GFXStateBlockDesc &result )
  298. {
  299. Parent::_initPassStateBlock( rpd, result );
  300. if ( mIsLightingMaterial )
  301. {
  302. result.ffLighting = true;
  303. result.blendDefined = true;
  304. result.blendEnable = true;
  305. result.blendSrc = GFXBlendOne;
  306. result.blendDest = GFXBlendZero;
  307. }
  308. // This is here for generic FF shader fallbacks.
  309. CustomMaterial* custmat = dynamic_cast<CustomMaterial*>(mMaterial);
  310. if (custmat && custmat->getStateBlockData() )
  311. result.addDesc(custmat->getStateBlockData()->getState());
  312. }