123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "materials/processedCustomMaterial.h"
- #include "gfx/sim/cubemapData.h"
- #include "materials/sceneData.h"
- #include "shaderGen/shaderGenVars.h"
- #include "scene/sceneRenderState.h"
- #include "materials/customMaterialDefinition.h"
- #include "materials/shaderData.h"
- #include "materials/materialManager.h"
- #include "materials/matTextureTarget.h"
- #include "materials/materialFeatureTypes.h"
- #include "materials/materialParameters.h"
- #include "gfx/sim/gfxStateBlockData.h"
- #include "core/util/safeDelete.h"
- #include "gfx/genericConstBuffer.h"
- #include "console/simFieldDictionary.h"
- #include "console/propertyParsing.h"
- #include "gfx/util/screenspace.h"
- #include "scene/reflectionManager.h"
- #include "renderInstance/renderProbeMgr.h"
- ProcessedCustomMaterial::ProcessedCustomMaterial(Material &mat)
- {
- mMaterial = &mat;
- AssertFatal(dynamic_cast<CustomMaterial*>(mMaterial), "Incompatible Material type!");
- mCustomMaterial = static_cast<CustomMaterial*>(mMaterial);
- mHasSetStageData = false;
- mHasGlow = false;
- mHasAccumulation = false;
- mMaxStages = 0;
- mMaxTex = 0;
- }
- ProcessedCustomMaterial::~ProcessedCustomMaterial()
- {
- }
- void ProcessedCustomMaterial::_setStageData()
- {
- // Only do this once
- if ( mHasSetStageData )
- return;
- mHasSetStageData = true;
- ShaderRenderPassData* rpd = _getRPD(0);
- mConditionerMacros.clear();
- // Loop through all the possible textures, set the right flags, and load them if needed
- for(U32 i=0; i<CustomMaterial::MAX_TEX_PER_PASS; i++ )
- {
- rpd->mTexType[i] = Material::NoTexture; // Set none as the default in case none of the cases below catch it.
- String filename = mCustomMaterial->mTexFilename[i];
- if(filename.isEmpty())
- continue;
- if(filename.equal(String("$dynamiclight"), String::NoCase))
- {
- rpd->mTexType[i] = Material::DynamicLight;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- continue;
- }
- if(filename.equal(String("$dynamiclightmask"), String::NoCase))
- {
- rpd->mTexType[i] = Material::DynamicLightMask;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- continue;
- }
- if(filename.equal(String("$lightmap"), String::NoCase))
- {
- rpd->mTexType[i] = Material::Lightmap;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- continue;
- }
- if(filename.equal(String("$cubemap"), String::NoCase))
- {
- if( mCustomMaterial->mCubemapData )
- {
- rpd->mTexType[i] = Material::Cube;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- }
- else
- {
- mCustomMaterial->logError( "Could not find CubemapData - %s", mCustomMaterial->mCubemapName.c_str());
- }
- continue;
- }
- if(filename.equal(String("$dynamicCubemap"), String::NoCase))
- {
- rpd->mTexType[i] = Material::SGCube;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- continue;
- }
- if(filename.equal(String("$backbuff"), String::NoCase))
- {
- rpd->mTexType[i] = Material::BackBuff;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- continue;
- }
- if(filename.equal(String("$reflectbuff"), String::NoCase))
- {
- rpd->mTexType[i] = Material::ReflectBuff;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- continue;
- }
- if(filename.equal(String("$miscbuff"), String::NoCase))
- {
- rpd->mTexType[i] = Material::Misc;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- continue;
- }
- // Check for a RenderTexTargetBin assignment
- if (filename.substr( 0, 1 ).equal("#"))
- {
- String texTargetBufferName = filename.substr(1, filename.length() - 1);
- NamedTexTarget *texTarget = NamedTexTarget::find( texTargetBufferName );
- rpd->mTexSlot[i].texTarget = texTarget;
- // Get the conditioner macros.
- if ( texTarget )
- texTarget->getShaderMacros( &mConditionerMacros );
- rpd->mTexType[i] = Material::TexTarget;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- continue;
- }
- rpd->mTexSlot[i].texObject = _createTexture( filename, &GFXStaticTextureSRGBProfile );
- if ( !rpd->mTexSlot[i].texObject )
- {
- mMaterial->logError("Failed to load texture %s", _getTexturePath(filename).c_str());
- continue;
- }
- rpd->mTexType[i] = Material::Standard;
- rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
- mMaxTex = i+1;
- }
- // We only get one cubemap
- if( mCustomMaterial->mCubemapData )
- {
- mCustomMaterial->mCubemapData->createMap();
- rpd->mCubeMap = mMaterial->mCubemapData->mCubemap; // BTRTODO ?
- if ( !rpd->mCubeMap )
- mMaterial->logError("Failed to load cubemap");
- }
- // If this has a output target defined, it may be writing
- // to a tex target bin with a conditioner, so search for
- // one and add its macros.
- if ( mCustomMaterial->mOutputTarget.isNotEmpty() )
- {
- NamedTexTarget *texTarget = NamedTexTarget::find( mCustomMaterial->mOutputTarget );
- if ( texTarget )
- texTarget->getShaderMacros( &mConditionerMacros );
- }
- // Copy the glow state over.
- mHasGlow = mCustomMaterial->mGlow[0];
- }
- bool ProcessedCustomMaterial::init( const FeatureSet &features,
- const GFXVertexFormat *vertexFormat,
- const MatFeaturesDelegate &featuresDelegate )
- {
- // If we don't have a shader data... we have nothing to do.
- if ( !mCustomMaterial->mShaderData )
- return true;
- // Custom materials only do one pass at the moment... so
- // add one for the stage data to fill in.
- ShaderRenderPassData *rpd = new ShaderRenderPassData();
- mPasses.push_back( rpd );
- _setStageData();
- _initPassStateBlocks();
- mStateHint.clear();
- // Note: We don't use the vertex format in a custom
- // material at all right now.
- //
- // Maybe we can add some required semantics and
- // validate that the format fits the shader?
- // Build a composite list of shader macros from
- // the conditioner and the user defined lists.
- Vector<GFXShaderMacro> macros;
- macros.merge( mConditionerMacros );
- macros.merge( mUserMacros );
- // Ask the shader data to give us a shader instance.
- rpd->shader = mCustomMaterial->mShaderData->getShader( macros );
- if ( !rpd->shader )
- {
- delete rpd;
- mPasses.clear();
- return false;
- }
- rpd->shaderHandles.init( rpd->shader, mCustomMaterial );
- _initMaterialParameters();
- mDefaultParameters = allocMaterialParameters();
- setMaterialParameters( mDefaultParameters, 0 );
- mStateHint.init( this );
-
- for(int i = 0; i < mMaxTex; i++)
- {
- ShaderConstHandles *handles = _getShaderConstHandles( mPasses.size()-1 );
- AssertFatal(handles,"");
- if(rpd->mSamplerNames[i].isEmpty())
- continue;
- String samplerName = rpd->mSamplerNames[i].startsWith("$") ? rpd->mSamplerNames[i] : String("$") + rpd->mSamplerNames[i];
- GFXShaderConstHandle *handle = rpd->shader->getShaderConstHandle( samplerName );
- AssertFatal(handle,"");
- handles->mTexHandlesSC[i] = handle;
- }
-
- return true;
- }
- void ProcessedCustomMaterial::_initPassStateBlock( RenderPassData *rpd, GFXStateBlockDesc &result )
- {
- Parent::_initPassStateBlock( rpd, result );
- if (mCustomMaterial->getStateBlockData())
- result.addDesc(mCustomMaterial->getStateBlockData()->getState());
- }
- void ProcessedCustomMaterial::_initPassStateBlocks()
- {
- AssertFatal(mHasSetStageData, "State data must be set before initializing state block!");
- ShaderRenderPassData* rpd = _getRPD(0);
- _initRenderStateStateBlocks( rpd );
- }
- bool ProcessedCustomMaterial::_hasCubemap(U32 pass)
- {
- // If the material doesn't have a cubemap, we don't
- if( mMaterial->mCubemapData ) return true;
- else return false;
- }
- bool ProcessedCustomMaterial::setupPass( SceneRenderState *state, const SceneData& sgData, U32 pass )
- {
- PROFILE_SCOPE( ProcessedCustomMaterial_SetupPass );
- // Make sure we have a pass.
- if ( pass >= mPasses.size() )
- return false;
- ShaderRenderPassData* rpd = _getRPD( pass );
- U32 currState = _getRenderStateIndex( state, sgData );
- GFX->setStateBlock(rpd->mRenderStates[currState]);
- // activate shader
- if ( rpd->shader )
- GFX->setShader( rpd->shader );
- else
- GFX->setupGenericShaders();
- // Set our textures
- setTextureStages( state, sgData, pass );
-
- GFXShaderConstBuffer* shaderConsts = _getShaderConstBuffer(pass);
- GFX->setShaderConstBuffer(shaderConsts);
-
- // Set our shader constants.
- _setTextureTransforms(pass);
- _setShaderConstants(state, sgData, pass);
- LightManager* lm = state ? LIGHTMGR : NULL;
- if (lm)
- lm->setLightInfo(this, NULL, sgData, state, pass, shaderConsts);
- RenderProbeMgr* pm = state ? PROBEMGR : NULL;
- if (pm)
- pm->setProbeInfo(this, NULL, sgData, state, pass, shaderConsts);
- shaderConsts->setSafe(rpd->shaderHandles.mAccumTimeSC, MATMGR->getTotalTime());
- shaderConsts->setSafe(rpd->shaderHandles.mDampnessSC, MATMGR->getDampnessClamped());
-
- return true;
- }
- void ProcessedCustomMaterial::setTextureStages( SceneRenderState *state, const SceneData &sgData, U32 pass )
- {
- LightManager* lm = state ? LIGHTMGR : NULL;
- ShaderRenderPassData* rpd = _getRPD(pass);
- ShaderConstHandles* handles = _getShaderConstHandles(pass);
- GFXShaderConstBuffer* shaderConsts = _getShaderConstBuffer(pass);
- const NamedTexTarget *texTarget;
- GFXTextureObject *texObject;
-
- for( U32 i=0; i<mMaxTex; i++ )
- {
- U32 currTexFlag = rpd->mTexType[i];
- if ( !lm || !lm->setTextureStage(sgData, currTexFlag, i, shaderConsts, handles ) )
- {
- GFXShaderConstHandle* handle = handles->mTexHandlesSC[i];
- if ( !handle->isValid() )
- continue;
- S32 samplerRegister = handle->getSamplerRegister();
-
- switch( currTexFlag )
- {
- case 0:
- default:
- break;
- case Material::Mask:
- case Material::Standard:
- case Material::Bump:
- case Material::Detail:
- {
- GFX->setTexture( samplerRegister, rpd->mTexSlot[i].texObject );
- break;
- }
- case Material::Lightmap:
- {
- GFX->setTexture( samplerRegister, sgData.lightmap );
- break;
- }
- case Material::Cube:
- {
- GFX->setCubeTexture( samplerRegister, rpd->mCubeMap );
- break;
- }
- case Material::SGCube:
- {
- GFX->setCubeTexture( samplerRegister, sgData.cubemap );
- break;
- }
- case Material::BackBuff:
- {
- GFX->setTexture( samplerRegister, sgData.backBuffTex );
- //if ( sgData.reflectTex )
- // GFX->setTexture( samplerRegister, sgData.reflectTex );
- //else
- //{
- // GFXTextureObject *refractTex = REFLECTMGR->getRefractTex( true );
- // GFX->setTexture( samplerRegister, refractTex );
- //}
- break;
- }
- case Material::ReflectBuff:
- {
- GFX->setTexture( samplerRegister, sgData.reflectTex );
- break;
- }
- case Material::Misc:
- {
- GFX->setTexture( samplerRegister, sgData.miscTex );
- break;
- }
- case Material::TexTarget:
- {
- texTarget = rpd->mTexSlot[i].texTarget;
- if ( !texTarget )
- {
- GFX->setTexture( samplerRegister, NULL );
- break;
- }
-
- texObject = texTarget->getTexture();
- // If no texture is available then map the default 2x2
- // black texture to it. This at least will ensure that
- // we get consistant behavior across GPUs and platforms.
- if ( !texObject )
- texObject = GFXTexHandle::ZERO;
- if ( handles->mRTParamsSC[samplerRegister]->isValid() && texObject )
- {
- const Point3I &targetSz = texObject->getSize();
- const RectI &targetVp = texTarget->getViewport();
- Point4F rtParams;
- ScreenSpace::RenderTargetParameters(targetSz, targetVp, rtParams);
- shaderConsts->set(handles->mRTParamsSC[samplerRegister], rtParams);
- }
-
- GFX->setTexture( samplerRegister, texObject );
- break;
- }
- }
- }
- }
- }
- template <typename T>
- void ProcessedCustomMaterial::setMaterialParameter(MaterialParameters* param,
- MaterialParameterHandle* handle,
- const String& value)
- {
- T typedValue;
- if (PropertyInfo::default_scan(value, typedValue))
- {
- param->set(handle, typedValue);
- } else {
- Con::errorf("Error setting %s, parse error: %s", handle->getName().c_str(), value.c_str());
- }
- }
- void ProcessedCustomMaterial::setMatrixParameter(MaterialParameters* param,
- MaterialParameterHandle* handle,
- const String& value, GFXShaderConstType matrixType)
- {
- MatrixF result(true);
- F32* m = result;
- switch (matrixType)
- {
- case GFXSCT_Float2x2 :
- dSscanf(value.c_str(),"%g %g %g %g",
- m[result.idx(0,0)], m[result.idx(0,1)],
- m[result.idx(1,0)], m[result.idx(1,1)]);
- break;
- case GFXSCT_Float3x3 :
- dSscanf(value.c_str(),"%g %g %g %g %g %g %g %g %g",
- m[result.idx(0,0)], m[result.idx(0,1)], m[result.idx(0,2)],
- m[result.idx(1,0)], m[result.idx(1,1)], m[result.idx(1,2)],
- m[result.idx(2,0)], m[result.idx(2,1)], m[result.idx(2,2)]);
- break;
- default:
- AssertFatal(false, "Invalid type!");
- break;
- }
- }
- // BTRTODO: Support arrays!?
- MaterialParameters* ProcessedCustomMaterial::allocMaterialParameters()
- {
- MaterialParameters* ret = Parent::allocMaterialParameters();
- // See if any of the dynamic fields match up with shader constants we have.
- SimFieldDictionary* fields = mMaterial->getFieldDictionary();
- if (!fields || fields->getNumFields() == 0)
- return ret;
- const Vector<GFXShaderConstDesc>& consts = ret->getShaderConstDesc();
- for (U32 i = 0; i < consts.size(); i++)
- {
- // strip the dollar sign from the front.
- String stripped(consts[i].name);
- stripped.erase(0, 1);
- SimFieldDictionary::Entry* field = fields->findDynamicField(stripped);
- if (field)
- {
- MaterialParameterHandle* handle = getMaterialParameterHandle(consts[i].name);
- switch (consts[i].constType)
- {
- case GFXSCT_Float :
- setMaterialParameter<F32>(ret, handle, field->value);
- break;
- case GFXSCT_Float2:
- setMaterialParameter<Point2F>(ret, handle, field->value);
- break;
- case GFXSCT_Float3:
- setMaterialParameter<Point3F>(ret, handle, field->value);
- break;
- case GFXSCT_Float4:
- setMaterialParameter<Point4F>(ret, handle, field->value);
- break;
- case GFXSCT_Float2x2:
- case GFXSCT_Float3x3:
- setMatrixParameter(ret, handle, field->value, consts[i].constType);
- break;
- case GFXSCT_Float4x4:
- setMaterialParameter<MatrixF>(ret, handle, field->value);
- break;
- case GFXSCT_Int:
- setMaterialParameter<S32>(ret, handle, field->value);
- break;
- case GFXSCT_Int2:
- setMaterialParameter<Point2I>(ret, handle, field->value);
- break;
- case GFXSCT_Int3:
- setMaterialParameter<Point3I>(ret, handle, field->value);
- break;
- case GFXSCT_Int4:
- setMaterialParameter<Point4I>(ret, handle, field->value);
- break;
- // Do we want to ignore these?
- case GFXSCT_Sampler:
- case GFXSCT_SamplerCube:
- default:
- break;
- }
- }
- }
- return ret;
- }
|