Changes for get necesary sampler names for OpenGL shaders.

This commit is contained in:
LuisAntonRebollo 2014-11-08 01:57:28 +01:00
parent 79e158d528
commit 1ac8fab884
11 changed files with 155 additions and 23 deletions

View file

@ -138,9 +138,22 @@ bool CustomMaterial::onAdd()
return false;
}
mSamplerNames[i] = entry->slotName + dStrlen(samplerDecl);
mSamplerNames[i].insert(0, '$');
mTexFilename[i] = entry->value;
// Assert sampler names are defined on ShaderData
S32 pos = -1;
String samplerName = entry->slotName + dStrlen(samplerDecl);
samplerName.insert(0, '$');
mShaderData->hasSamplerDef(samplerName, pos);
if(pos == -1)
{
const char *error = (avar("CustomMaterial(%s) bind sampler[%s] and is not present on ShaderData(%s)",
getName(), samplerName.c_str(), mShaderDataName.c_str() ));
Con::errorf(error);
GFXAssertFatal(0, error);
continue;
}
mSamplerNames[pos] = samplerName;
mTexFilename[pos] = entry->value;
++i;
}
}

View file

@ -79,6 +79,7 @@ void ProcessedCustomMaterial::_setStageData()
if(filename.equal(String("$dynamiclight"), String::NoCase))
{
rpd->mTexType[i] = Material::DynamicLight;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
continue;
}
@ -86,6 +87,7 @@ void ProcessedCustomMaterial::_setStageData()
if(filename.equal(String("$dynamiclightmask"), String::NoCase))
{
rpd->mTexType[i] = Material::DynamicLightMask;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
continue;
}
@ -93,6 +95,7 @@ void ProcessedCustomMaterial::_setStageData()
if(filename.equal(String("$lightmap"), String::NoCase))
{
rpd->mTexType[i] = Material::Lightmap;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
continue;
}
@ -102,6 +105,7 @@ void ProcessedCustomMaterial::_setStageData()
if( mCustomMaterial->mCubemapData )
{
rpd->mTexType[i] = Material::Cube;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
}
else
@ -114,6 +118,7 @@ void ProcessedCustomMaterial::_setStageData()
if(filename.equal(String("$dynamicCubemap"), String::NoCase))
{
rpd->mTexType[i] = Material::SGCube;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
continue;
}
@ -121,6 +126,7 @@ void ProcessedCustomMaterial::_setStageData()
if(filename.equal(String("$backbuff"), String::NoCase))
{
rpd->mTexType[i] = Material::BackBuff;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
continue;
}
@ -128,6 +134,7 @@ void ProcessedCustomMaterial::_setStageData()
if(filename.equal(String("$reflectbuff"), String::NoCase))
{
rpd->mTexType[i] = Material::ReflectBuff;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
continue;
}
@ -135,6 +142,7 @@ void ProcessedCustomMaterial::_setStageData()
if(filename.equal(String("$miscbuff"), String::NoCase))
{
rpd->mTexType[i] = Material::Misc;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
continue;
}
@ -151,6 +159,7 @@ void ProcessedCustomMaterial::_setStageData()
texTarget->getShaderMacros( &mConditionerMacros );
rpd->mTexType[i] = Material::TexTarget;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
continue;
}
@ -162,6 +171,7 @@ void ProcessedCustomMaterial::_setStageData()
continue;
}
rpd->mTexType[i] = Material::Standard;
rpd->mSamplerNames[i] = mCustomMaterial->mSamplerNames[i];
mMaxTex = i+1;
}
@ -232,6 +242,20 @@ bool ProcessedCustomMaterial::init( const FeatureSet &features,
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;
}
@ -384,14 +408,14 @@ void ProcessedCustomMaterial::setTextureStages( SceneRenderState *state, const S
if ( !texObject )
texObject = GFXTexHandle::ZERO;
if ( handles->mRTParamsSC[samplerRegister]->isValid() && texObject )
if ( handles->mRTParamsSC[i]->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);
shaderConsts->set(handles->mRTParamsSC[i], rtParams);
}
GFX->setTexture( samplerRegister, texObject );

View file

@ -221,6 +221,7 @@ bool ProcessedShaderMaterial::init( const FeatureSet &features,
{
rpd->mTexSlot[0].texTarget = texTarget;
rpd->mTexType[0] = Material::TexTarget;
rpd->mSamplerNames[0] = "diffuseMap";
}
}
@ -516,8 +517,23 @@ bool ProcessedShaderMaterial::_createPasses( MaterialFeatureData &stageFeatures,
passData.mNumTexReg += numTexReg;
passData.mFeatureData.features.addFeature( *info.type );
#if defined(TORQUE_DEBUG) && defined( TORQUE_OPENGL)
U32 oldTexNumber = texIndex;
#endif
info.feature->setTexData( mStages[stageNum], stageFeatures, passData, texIndex );
#if defined(TORQUE_DEBUG) && defined( TORQUE_OPENGL)
if(oldTexNumber != texIndex)
{
for(int i = oldTexNumber; i < texIndex; i++)
{
AssertFatal(passData.mSamplerNames[ oldTexNumber ].isNotEmpty(), avar( "ERROR: ShaderGen feature %s don't set used sampler name", info.feature->getName().c_str()) );
}
}
#endif
// Add pass if tex units are maxed out
if( texIndex > GFX->getNumSamplers() )
{
@ -527,6 +543,13 @@ bool ProcessedShaderMaterial::_createPasses( MaterialFeatureData &stageFeatures,
}
}
#if defined(TORQUE_DEBUG) && defined( TORQUE_OPENGL)
for(int i = 0; i < texIndex; i++)
{
AssertFatal(passData.mSamplerNames[ i ].isNotEmpty(),"");
}
#endif
const FeatureSet &passFeatures = passData.mFeatureData.codify();
if ( passFeatures.isNotEmpty() )
{
@ -587,9 +610,16 @@ bool ProcessedShaderMaterial::_addPass( ShaderRenderPassData &rpd,
// Copy over features
rpd.mFeatureData.materialFeatures = fd.features;
Vector<String> samplers;
samplers.setSize(Material::MAX_TEX_PER_PASS);
for(int i = 0; i < Material::MAX_TEX_PER_PASS; ++i)
{
samplers[i] = (rpd.mSamplerNames[i].isEmpty() || rpd.mSamplerNames[i][0] == '$') ? rpd.mSamplerNames[i] : "$" + rpd.mSamplerNames[i];
}
// Generate shader
GFXShader::setLogging( true, true );
rpd.shader = SHADERGEN->getShader( rpd.mFeatureData, mVertexFormat, &mUserMacros );
rpd.shader = SHADERGEN->getShader( rpd.mFeatureData, mVertexFormat, &mUserMacros, samplers );
if( !rpd.shader )
return false;
rpd.shaderHandles.init( rpd.shader );
@ -601,6 +631,30 @@ bool ProcessedShaderMaterial::_addPass( ShaderRenderPassData &rpd,
ShaderRenderPassData *newPass = new ShaderRenderPassData( rpd );
mPasses.push_back( newPass );
//initSamplerHandles
ShaderConstHandles *handles = _getShaderConstHandles( mPasses.size()-1 );
AssertFatal(handles,"");
for(int i = 0; i < rpd.mNumTex; i++)
{
if(rpd.mSamplerNames[i].isEmpty())
{
handles->mTexHandlesSC[i] = newPass->shader->getShaderConstHandle( String::EmptyString );
handles->mRTParamsSC[i] = newPass->shader->getShaderConstHandle( String::EmptyString );
continue;
}
String samplerName = rpd.mSamplerNames[i];
if( !samplerName.startsWith("$"))
samplerName.insert(0, "$");
GFXShaderConstHandle *handle = newPass->shader->getShaderConstHandle( samplerName );
handles->mTexHandlesSC[i] = handle;
handles->mRTParamsSC[i] = newPass->shader->getShaderConstHandle( String::ToString( "$rtParams%s", samplerName.c_str()+1 ) );
AssertFatal( handle,"");
}
// Give each active feature a chance to create specialized shader consts.
for( U32 i=0; i < FEATUREMGR->getFeatureCount(); i++ )
{
@ -705,6 +759,7 @@ void ProcessedShaderMaterial::setTextureStages( SceneRenderState *state, const S
PROFILE_SCOPE( ProcessedShaderMaterial_SetTextureStages );
ShaderConstHandles *handles = _getShaderConstHandles(pass);
AssertFatal(handles,"");
// Set all of the textures we need to render the give pass.
#ifdef TORQUE_DEBUG