2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// 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/processedShaderMaterial.h"
# include "core/util/safeDelete.h"
# include "gfx/sim/cubemapData.h"
# include "gfx/gfxShader.h"
# include "gfx/gfxPrimitiveBuffer.h"
# include "scene/sceneRenderState.h"
# include "shaderGen/shaderFeature.h"
# include "shaderGen/shaderGenVars.h"
# include "shaderGen/featureMgr.h"
# include "shaderGen/shaderGen.h"
# include "materials/sceneData.h"
# include "materials/materialFeatureTypes.h"
# include "materials/materialManager.h"
# include "materials/shaderMaterialParameters.h"
# include "materials/matTextureTarget.h"
# include "gfx/util/screenspace.h"
# include "math/util/matrixSet.h"
2019-01-26 08:05:18 +00:00
# include "renderInstance/renderProbeMgr.h"
2018-09-17 03:15:07 +00:00
2017-10-10 20:54:54 +00:00
# include "ts/tsRenderState.h"
2012-09-19 15:15:01 +00:00
// We need to include customMaterialDefinition for ShaderConstHandles::init
# include "materials/customMaterialDefinition.h"
2019-05-13 05:28:23 +00:00
# include "gui/controls/guiTreeViewCtrl.h"
2015-01-10 19:41:25 +00:00
# include "ts/tsShape.h"
2012-09-19 15:15:01 +00:00
///
/// ShaderConstHandles
///
2020-09-19 23:25:10 +00:00
void ShaderConstHandles : : init ( GFXShader * shader , CustomMaterial * mat /*=NULL*/ )
2012-09-19 15:15:01 +00:00
{
mDiffuseColorSC = shader - > getShaderConstHandle ( " $diffuseMaterialColor " ) ;
mTexMatSC = shader - > getShaderConstHandle ( ShaderGenVars : : texMat ) ;
mToneMapTexSC = shader - > getShaderConstHandle ( ShaderGenVars : : toneMap ) ;
2020-09-30 18:51:12 +00:00
mORMConfigSC = shader - > getShaderConstHandle ( ShaderGenVars : : ormConfig ) ;
mRoughnessSC = shader - > getShaderConstHandle ( ShaderGenVars : : roughness ) ;
2018-09-16 01:19:57 +00:00
mMetalnessSC = shader - > getShaderConstHandle ( ShaderGenVars : : metalness ) ;
2019-10-30 23:29:11 +00:00
mGlowMulSC = shader - > getShaderConstHandle ( ShaderGenVars : : glowMul ) ;
2014-12-21 20:07:42 +00:00
mAccuScaleSC = shader - > getShaderConstHandle ( " $accuScale " ) ;
mAccuDirectionSC = shader - > getShaderConstHandle ( " $accuDirection " ) ;
mAccuStrengthSC = shader - > getShaderConstHandle ( " $accuStrength " ) ;
mAccuCoverageSC = shader - > getShaderConstHandle ( " $accuCoverage " ) ;
mAccuSpecularSC = shader - > getShaderConstHandle ( " $accuSpecular " ) ;
2012-09-19 15:15:01 +00:00
mParallaxInfoSC = shader - > getShaderConstHandle ( " $parallaxInfo " ) ;
mFogDataSC = shader - > getShaderConstHandle ( ShaderGenVars : : fogData ) ;
mFogColorSC = shader - > getShaderConstHandle ( ShaderGenVars : : fogColor ) ;
mDetailScaleSC = shader - > getShaderConstHandle ( ShaderGenVars : : detailScale ) ;
mVisiblitySC = shader - > getShaderConstHandle ( ShaderGenVars : : visibility ) ;
mColorMultiplySC = shader - > getShaderConstHandle ( ShaderGenVars : : colorMultiply ) ;
mAlphaTestValueSC = shader - > getShaderConstHandle ( ShaderGenVars : : alphaTestValue ) ;
mModelViewProjSC = shader - > getShaderConstHandle ( ShaderGenVars : : modelview ) ;
mWorldViewOnlySC = shader - > getShaderConstHandle ( ShaderGenVars : : worldViewOnly ) ;
mWorldToCameraSC = shader - > getShaderConstHandle ( ShaderGenVars : : worldToCamera ) ;
2018-10-28 10:42:26 +00:00
mCameraToWorldSC = shader - > getShaderConstHandle ( ShaderGenVars : : cameraToWorld ) ;
2012-09-19 15:15:01 +00:00
mWorldToObjSC = shader - > getShaderConstHandle ( ShaderGenVars : : worldToObj ) ;
mViewToObjSC = shader - > getShaderConstHandle ( ShaderGenVars : : viewToObj ) ;
2019-09-06 19:25:17 +00:00
mInvCameraTransSC = shader - > getShaderConstHandle ( ShaderGenVars : : invCameraTrans ) ;
mCameraToScreenSC = shader - > getShaderConstHandle ( ShaderGenVars : : cameraToScreen ) ;
mScreenToCameraSC = shader - > getShaderConstHandle ( ShaderGenVars : : screenToCamera ) ;
2012-09-19 15:15:01 +00:00
mCubeTransSC = shader - > getShaderConstHandle ( ShaderGenVars : : cubeTrans ) ;
2017-06-23 16:36:20 +00:00
mCubeMipsSC = shader - > getShaderConstHandle ( ShaderGenVars : : cubeMips ) ;
2012-09-19 15:15:01 +00:00
mObjTransSC = shader - > getShaderConstHandle ( ShaderGenVars : : objTrans ) ;
mCubeEyePosSC = shader - > getShaderConstHandle ( ShaderGenVars : : cubeEyePos ) ;
mEyePosSC = shader - > getShaderConstHandle ( ShaderGenVars : : eyePos ) ;
mEyePosWorldSC = shader - > getShaderConstHandle ( ShaderGenVars : : eyePosWorld ) ;
m_vEyeSC = shader - > getShaderConstHandle ( ShaderGenVars : : vEye ) ;
mEyeMatSC = shader - > getShaderConstHandle ( ShaderGenVars : : eyeMat ) ;
mOneOverFarplane = shader - > getShaderConstHandle ( ShaderGenVars : : oneOverFarplane ) ;
mAccumTimeSC = shader - > getShaderConstHandle ( ShaderGenVars : : accumTime ) ;
2022-11-22 03:12:23 +00:00
mDampnessSC = shader - > getShaderConstHandle ( ShaderGenVars : : dampness ) ;
2012-09-19 15:15:01 +00:00
mMinnaertConstantSC = shader - > getShaderConstHandle ( ShaderGenVars : : minnaertConstant ) ;
mSubSurfaceParamsSC = shader - > getShaderConstHandle ( ShaderGenVars : : subSurfaceParams ) ;
mDiffuseAtlasParamsSC = shader - > getShaderConstHandle ( ShaderGenVars : : diffuseAtlasParams ) ;
mDiffuseAtlasTileSC = shader - > getShaderConstHandle ( ShaderGenVars : : diffuseAtlasTileParams ) ;
mBumpAtlasParamsSC = shader - > getShaderConstHandle ( ShaderGenVars : : bumpAtlasParams ) ;
mBumpAtlasTileSC = shader - > getShaderConstHandle ( ShaderGenVars : : bumpAtlasTileParams ) ;
mRTSizeSC = shader - > getShaderConstHandle ( " $targetSize " ) ;
mOneOverRTSizeSC = shader - > getShaderConstHandle ( " $oneOverTargetSize " ) ;
mDetailBumpStrength = shader - > getShaderConstHandle ( " $detailBumpStrength " ) ;
mViewProjSC = shader - > getShaderConstHandle ( " $viewProj " ) ;
// MFT_ImposterVert
mImposterUVs = shader - > getShaderConstHandle ( " $imposterUVs " ) ;
mImposterLimits = shader - > getShaderConstHandle ( " $imposterLimits " ) ;
2021-01-05 02:57:17 +00:00
for ( S32 i = 0 ; i < GFX_TEXTURE_STAGE_COUNT ; + + i )
2012-09-19 15:15:01 +00:00
mRTParamsSC [ i ] = shader - > getShaderConstHandle ( String : : ToString ( " $rtParams%d " , i ) ) ;
2015-01-10 19:41:25 +00:00
// MFT_HardwareSkinning
mNodeTransforms = shader - > getShaderConstHandle ( " $nodeTransforms " ) ;
2024-04-16 18:51:41 +00:00
mIsCapturingSC = shader - > getShaderConstHandle ( ShaderGenVars : : isCapturing ) ;
2012-09-19 15:15:01 +00:00
// Clear any existing texture handles.
dMemset ( mTexHandlesSC , 0 , sizeof ( mTexHandlesSC ) ) ;
if ( mat )
{
for ( S32 i = 0 ; i < Material : : MAX_TEX_PER_PASS ; + + i )
mTexHandlesSC [ i ] = shader - > getShaderConstHandle ( mat - > mSamplerNames [ i ] ) ;
}
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of:
renderPrePassMgr.cpp related:
A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures
B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render
C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages()
D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets)
Reflections:
A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever.
B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected.
C) 2 things best described bluntly as working hacks:
C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly.
C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material.
Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
// Deferred Shading
mMatInfoFlagsSC = shader - > getShaderConstHandle ( ShaderGenVars : : matInfoFlags ) ;
2012-09-19 15:15:01 +00:00
}
///
/// ShaderRenderPassData
///
void ShaderRenderPassData : : reset ( )
{
Parent : : reset ( ) ;
shader = NULL ;
for ( U32 i = 0 ; i < featureShaderHandles . size ( ) ; i + + )
delete featureShaderHandles [ i ] ;
featureShaderHandles . clear ( ) ;
}
String ShaderRenderPassData : : describeSelf ( ) const
{
// First write the shader identification.
String desc = String : : ToString ( " %s \n " , shader - > describeSelf ( ) . c_str ( ) ) ;
// Let the parent get the rest.
desc + = Parent : : describeSelf ( ) ;
return desc ;
}
///
/// ProcessedShaderMaterial
///
ProcessedShaderMaterial : : ProcessedShaderMaterial ( )
: mDefaultParameters ( NULL ) ,
mInstancingState ( NULL )
{
VECTOR_SET_ASSOCIATION ( mShaderConstDesc ) ;
VECTOR_SET_ASSOCIATION ( mParameterHandles ) ;
}
ProcessedShaderMaterial : : ProcessedShaderMaterial ( Material & mat )
: mDefaultParameters ( NULL ) ,
mInstancingState ( NULL )
{
VECTOR_SET_ASSOCIATION ( mShaderConstDesc ) ;
VECTOR_SET_ASSOCIATION ( mParameterHandles ) ;
mMaterial = & mat ;
}
ProcessedShaderMaterial : : ~ ProcessedShaderMaterial ( )
{
SAFE_DELETE ( mInstancingState ) ;
SAFE_DELETE ( mDefaultParameters ) ;
for ( U32 i = 0 ; i < mParameterHandles . size ( ) ; i + + )
SAFE_DELETE ( mParameterHandles [ i ] ) ;
}
//
// Material init
//
bool ProcessedShaderMaterial : : init ( const FeatureSet & features ,
const GFXVertexFormat * vertexFormat ,
const MatFeaturesDelegate & featuresDelegate )
{
// Load our textures
_setStageData ( ) ;
// Determine how many stages we use
mMaxStages = getNumStages ( ) ;
mVertexFormat = vertexFormat ;
mFeatures . clear ( ) ;
mStateHint . clear ( ) ;
SAFE_DELETE ( mInstancingState ) ;
for ( U32 i = 0 ; i < mMaxStages ; i + + )
{
MaterialFeatureData fd ;
// Determine the features of this stage
_determineFeatures ( i , fd , features ) ;
// Let the delegate poke at the features.
if ( featuresDelegate )
featuresDelegate ( this , i , fd , features ) ;
// Create the passes for this stage
if ( fd . features . isNotEmpty ( ) )
if ( ! _createPasses ( fd , i , features ) )
return false ;
}
_initRenderPassDataStateBlocks ( ) ;
_initMaterialParameters ( ) ;
mDefaultParameters = allocMaterialParameters ( ) ;
setMaterialParameters ( mDefaultParameters , 0 ) ;
mStateHint . init ( this ) ;
// Enable instancing if we have it.
if ( mFeatures . hasFeature ( MFT_UseInstancing ) )
{
mInstancingState = new InstancingState ( ) ;
2016-03-20 11:52:11 +00:00
mInstancingState - > setFormat ( _getRPD ( 0 ) - > shader - > getInstancingFormat ( ) , mVertexFormat ) ;
2012-09-19 15:15:01 +00:00
}
2025-03-28 14:05:03 +00:00
if ( mMaterial & & mMaterial - > getDiffuseMapAsset ( 0 ) . notNull ( ) & & mMaterial - > getDiffuseMapAsset ( 0 ) - > isNamedTarget ( ) )
2016-06-07 18:54:30 +00:00
{
2025-03-25 18:22:26 +00:00
NamedTexTarget * texTarget = mMaterial - > getDiffuseMapAsset ( 0 ) - > getNamedTarget ( ) ;
2016-06-07 18:54:30 +00:00
RenderPassData * rpd = getPass ( 0 ) ;
if ( rpd )
{
rpd - > mTexSlot [ 0 ] . texTarget = texTarget ;
rpd - > mTexType [ 0 ] = Material : : TexTarget ;
rpd - > mSamplerNames [ 0 ] = " diffuseMap " ;
}
}
2012-09-19 15:15:01 +00:00
return true ;
}
U32 ProcessedShaderMaterial : : getNumStages ( )
{
// Loops through all stages to determine how many
// stages we actually use.
//
// The first stage is always active else we shouldn't be
// creating the material to begin with.
U32 numStages = 1 ;
U32 i ;
for ( i = 1 ; i < Material : : MAX_STAGES ; i + + )
{
// Assume stage is inactive
bool stageActive = false ;
// Cubemaps only on first stage
if ( i = = 0 )
{
// If we have a cubemap the stage is active
if ( mMaterial - > mCubemapData | | mMaterial - > mDynamicCubemap )
{
numStages + + ;
continue ;
}
}
// If we have a texture for the a feature the
// stage is active.
if ( mStages [ i ] . hasValidTex ( ) )
stageActive = true ;
2019-05-06 01:10:14 +00:00
2012-09-19 15:15:01 +00:00
// If this stage has diffuse color, it's active
if ( mMaterial - > mDiffuse [ i ] . alpha > 0 & &
2017-06-23 16:36:20 +00:00
mMaterial - > mDiffuse [ i ] ! = LinearColorF : : WHITE )
2012-09-19 15:15:01 +00:00
stageActive = true ;
// If we have a Material that is vertex lit
// then it may not have a texture
if ( mMaterial - > mVertLit [ i ] )
stageActive = true ;
// Increment the number of active stages
numStages + = stageActive ;
}
return numStages ;
}
void ProcessedShaderMaterial : : _determineFeatures ( U32 stageNum ,
MaterialFeatureData & fd ,
const FeatureSet & features )
{
2022-10-01 17:44:45 +00:00
if ( GFX - > getAdapterType ( ) = = NullDevice ) return ;
2012-09-19 15:15:01 +00:00
PROFILE_SCOPE ( ProcessedShaderMaterial_DetermineFeatures ) ;
2013-08-04 21:58:59 +00:00
const F32 shaderVersion = GFX - > getPixelShaderVersion ( ) ;
2012-09-19 15:15:01 +00:00
AssertFatal ( shaderVersion > 0.0 , " Cannot create a shader material if we don't support shaders " ) ;
bool lastStage = stageNum = = ( mMaxStages - 1 ) ;
// First we add all the features which the
// material has defined.
2020-09-30 18:51:12 +00:00
if ( mMaterial - > mInvertRoughness [ stageNum ] )
fd . features . addFeature ( MFT_InvertRoughness ) ;
2012-09-19 15:15:01 +00:00
if ( mMaterial - > isTranslucent ( ) )
{
2017-04-11 05:23:14 +00:00
// Note: This is for decal blending into the deferred
2012-09-19 15:15:01 +00:00
// for AL... it probably needs to be made clearer.
if ( mMaterial - > mTranslucentBlendOp = = Material : : LerpAlpha & &
mMaterial - > mTranslucentZWrite )
fd . features . addFeature ( MFT_IsTranslucentZWrite ) ;
else
{
fd . features . addFeature ( MFT_IsTranslucent ) ;
fd . features . addFeature ( MFT_ForwardShading ) ;
}
}
// TODO: This sort of sucks... BL should somehow force this
// feature on from the outside and not this way.
2020-10-03 12:37:55 +00:00
if ( String : : compare ( LIGHTMGR - > getId ( ) , " BLM " ) = = 0 )
Updated path handling for loose asset files for CPP, Image, Level, Material, PostFX, Shape, Terrain, TerrainMat and StateMachine assets to be more predictable in when and how they expando the loose file path into a full, useable path
Fixed loose file bindings for all associated slots in level asset, such as postFX file, decals, etc
Expanded TSStatic onInspect testcase to parse materialSlots and hook-in a specialized material field for editing/quick reference from the inspector
Adjusted expand behavior of guiTree to be more reliable
Added internal name 'stack' to inspectorGroup's stack child objects for easier access to add programatic fields
Removed redundant PreMult translucency type code
Added setting of feature so probes work when in forward/basic lit mode
Corrected indexing error in SQLiteObject class so it properly parses with the updated console API
Tweaked the FOV setting logic in GameConnection::onControlObjectChange to not be spammy
Fixed var when trying to bind the camera to the client
Added project setting field to dictate the default render mode between Forward or Deferred
Integrated MotionBlur PostFX into updated PostFX Editor paradigm and exposed the samples uniform as an editable field
Integrated DOF PostFX into updated PostFX Editor paradigm
Updated setting group name for vignette postFX
Shifted shaderCache to be in data/cache along with other cached files
Added helper function to replace strings in a file
Fixed ExampleCppObject asset to have correct loose file references
Adjusted editor default level logic so it can be modifed and then stored, as well as reset back to the original default
Fixed verve reference to root scene group
Adjusted location of a nonmodal gui profile so it loads at the correct time
Reorganized AssetBrowser loading and refresh logic so it doesn't stack multiple refresh requests back-to-back causing lag
Updated the search behavior to search not just the current address, but all child folders as well, making it far more useful
Initial work into zip and folder drag-and-drop asset importing support
Removed the import config setting for 'always display material maps' as it is redundant with the new importer context menu actions
Updated example asset type file
Ensured all asset types have proper handling for move, rename and delete actions
Fixed double-click behavior on folders in the AB
Fixed CPP asset preview
Added better logic to discern if a top-level folder belongs to a module or not in the AB directory browser
Added ability to convert a non-module top-level folder in the AB into a module
Added initial hooks for being able to generate a new Editor Tool, similar to how the AB can generate modules
Renamed CPP asset template files to have the .template so they aren't accidentally picked up by cmake
Fixed convex editor's material handling to work with AB and reference back properly
Updated AB images for folder up/down navigation buttons, and the breadcrumb divider arrow
Made PostFX Editor properly allow for input pass-through so you can still edit the level with it open
Added some additional common text gui profiles
Disabled calls to old editor settings logic in various editors to remove spam
Added callOnModules call so tools can initialize properly when the world editor is opened
Fixed logic test for visualizers
Added ability for cmake to scan tools directory for any tools that add source files
2020-02-04 07:47:28 +00:00
{
fd . features . addFeature ( MFT_ForwardShading ) ;
2024-04-18 18:57:29 +00:00
if ( ! mMaterial - > mDynamicCubemap )
fd . features . addFeature ( MFT_ReflectionProbes ) ;
Updated path handling for loose asset files for CPP, Image, Level, Material, PostFX, Shape, Terrain, TerrainMat and StateMachine assets to be more predictable in when and how they expando the loose file path into a full, useable path
Fixed loose file bindings for all associated slots in level asset, such as postFX file, decals, etc
Expanded TSStatic onInspect testcase to parse materialSlots and hook-in a specialized material field for editing/quick reference from the inspector
Adjusted expand behavior of guiTree to be more reliable
Added internal name 'stack' to inspectorGroup's stack child objects for easier access to add programatic fields
Removed redundant PreMult translucency type code
Added setting of feature so probes work when in forward/basic lit mode
Corrected indexing error in SQLiteObject class so it properly parses with the updated console API
Tweaked the FOV setting logic in GameConnection::onControlObjectChange to not be spammy
Fixed var when trying to bind the camera to the client
Added project setting field to dictate the default render mode between Forward or Deferred
Integrated MotionBlur PostFX into updated PostFX Editor paradigm and exposed the samples uniform as an editable field
Integrated DOF PostFX into updated PostFX Editor paradigm
Updated setting group name for vignette postFX
Shifted shaderCache to be in data/cache along with other cached files
Added helper function to replace strings in a file
Fixed ExampleCppObject asset to have correct loose file references
Adjusted editor default level logic so it can be modifed and then stored, as well as reset back to the original default
Fixed verve reference to root scene group
Adjusted location of a nonmodal gui profile so it loads at the correct time
Reorganized AssetBrowser loading and refresh logic so it doesn't stack multiple refresh requests back-to-back causing lag
Updated the search behavior to search not just the current address, but all child folders as well, making it far more useful
Initial work into zip and folder drag-and-drop asset importing support
Removed the import config setting for 'always display material maps' as it is redundant with the new importer context menu actions
Updated example asset type file
Ensured all asset types have proper handling for move, rename and delete actions
Fixed double-click behavior on folders in the AB
Fixed CPP asset preview
Added better logic to discern if a top-level folder belongs to a module or not in the AB directory browser
Added ability to convert a non-module top-level folder in the AB into a module
Added initial hooks for being able to generate a new Editor Tool, similar to how the AB can generate modules
Renamed CPP asset template files to have the .template so they aren't accidentally picked up by cmake
Fixed convex editor's material handling to work with AB and reference back properly
Updated AB images for folder up/down navigation buttons, and the breadcrumb divider arrow
Made PostFX Editor properly allow for input pass-through so you can still edit the level with it open
Added some additional common text gui profiles
Disabled calls to old editor settings logic in various editors to remove spam
Added callOnModules call so tools can initialize properly when the world editor is opened
Fixed logic test for visualizers
Added ability for cmake to scan tools directory for any tools that add source files
2020-02-04 07:47:28 +00:00
}
2012-09-19 15:15:01 +00:00
2017-04-11 05:23:14 +00:00
// Disabling the InterlacedDeferred feature for now. It is not ready for prime-time
2012-09-19 15:15:01 +00:00
// and it should not be triggered off of the DoubleSided parameter. [2/5/2010 Pat]
/*if ( mMaterial->isDoubleSided() )
{
2017-04-11 05:23:14 +00:00
fd . features . addFeature ( MFT_InterlacedDeferred ) ;
2012-09-19 15:15:01 +00:00
} */
// Allow instancing if it was requested and the card supports
// SM 3.0 or above.
//
// We also disable instancing for non-single pass materials
// and glowing materials because its untested/unimplemented.
//
if ( features . hasFeature ( MFT_UseInstancing ) & &
mMaxStages = = 1 & &
shaderVersion > = 3.0f )
fd . features . addFeature ( MFT_UseInstancing ) ;
if ( mMaterial - > mAlphaTest )
fd . features . addFeature ( MFT_AlphaTest ) ;
2022-12-29 19:38:30 +00:00
if ( mMaterial - > isTranslucent ( ) )
2018-09-17 03:15:07 +00:00
{
fd . features . addFeature ( MFT_RTLighting ) ;
2024-04-18 18:57:29 +00:00
if ( ! mMaterial - > mDynamicCubemap )
fd . features . addFeature ( MFT_ReflectionProbes ) ;
2018-09-17 03:15:07 +00:00
}
2012-09-19 15:15:01 +00:00
if ( mMaterial - > mAnimFlags [ stageNum ] )
fd . features . addFeature ( MFT_TexAnim ) ;
if ( mMaterial - > mVertLit [ stageNum ] )
fd . features . addFeature ( MFT_VertLit ) ;
// cubemaps only available on stage 0 for now - bramage
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of:
renderPrePassMgr.cpp related:
A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures
B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render
C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages()
D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets)
Reflections:
A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever.
B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected.
C) 2 things best described bluntly as working hacks:
C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly.
C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material.
Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
if ( stageNum < 1 & & mMaterial - > isTranslucent ( ) & &
2012-09-19 15:15:01 +00:00
( ( mMaterial - > mCubemapData & & mMaterial - > mCubemapData - > mCubemap ) | |
2024-04-18 18:57:29 +00:00
mMaterial - > mDynamicCubemap ) /*&& !features.hasFeature(MFT_ReflectionProbes) */ )
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of:
renderPrePassMgr.cpp related:
A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures
B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render
C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages()
D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets)
Reflections:
A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever.
B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected.
C) 2 things best described bluntly as working hacks:
C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly.
C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material.
Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
{
fd . features . addFeature ( MFT_CubeMap ) ;
}
2012-09-19 15:15:01 +00:00
fd . features . addFeature ( MFT_Visibility ) ;
if ( lastStage & &
( ! gClientSceneGraph - > usePostEffectFog ( ) | |
fd . features . hasFeature ( MFT_IsTranslucent ) | |
fd . features . hasFeature ( MFT_ForwardShading ) ) )
fd . features . addFeature ( MFT_Fog ) ;
if ( mMaterial - > mMinnaertConstant [ stageNum ] > 0.0f )
fd . features . addFeature ( MFT_MinnaertShading ) ;
if ( mMaterial - > mSubSurface [ stageNum ] )
fd . features . addFeature ( MFT_SubSurface ) ;
if ( ! mMaterial - > mCellLayout [ stageNum ] . isZero ( ) )
{
fd . features . addFeature ( MFT_DiffuseMapAtlas ) ;
if ( mMaterial - > mNormalMapAtlas )
fd . features . addFeature ( MFT_NormalMapAtlas ) ;
}
2019-11-21 06:48:55 +00:00
if ( ! fd . features . hasFeature ( MFT_ForwardShading ) )
{
fd . features . removeFeature ( MFT_DebugViz ) ;
}
2012-09-19 15:15:01 +00:00
// Grab other features like normal maps, base texture, etc.
FeatureSet mergeFeatures ;
mStages [ stageNum ] . getFeatureSet ( & mergeFeatures ) ;
fd . features . merge ( mergeFeatures ) ;
if ( fd . features [ MFT_NormalMap ] )
{
2017-06-23 16:36:20 +00:00
if ( mStages [ stageNum ] . getTex ( MFT_NormalMap ) - > mFormat = = GFXFormatBC3 & &
2012-09-19 15:15:01 +00:00
! mStages [ stageNum ] . getTex ( MFT_NormalMap ) - > mHasTransparency )
2017-06-23 16:36:20 +00:00
fd . features . addFeature ( MFT_IsBC3nm ) ;
else if ( mStages [ stageNum ] . getTex ( MFT_NormalMap ) - > mFormat = = GFXFormatBC5 & &
! mStages [ stageNum ] . getTex ( MFT_NormalMap ) - > mHasTransparency )
fd . features . addFeature ( MFT_IsBC5nm ) ;
2012-09-19 15:15:01 +00:00
}
// Now for some more advanced features that we
// cannot do on SM 2.0 and below.
if ( shaderVersion > 2.0f )
{
2014-07-03 07:56:47 +00:00
2012-09-19 15:15:01 +00:00
if ( mMaterial - > mParallaxScale [ stageNum ] > 0.0f & &
2014-07-03 07:56:47 +00:00
fd . features [ MFT_NormalMap ] )
2012-09-19 15:15:01 +00:00
fd . features . addFeature ( MFT_Parallax ) ;
}
2019-10-23 19:59:29 +00:00
// Deferred Shading : PBR Config
2020-09-30 18:51:12 +00:00
if ( mStages [ stageNum ] . getTex ( MFT_OrmMap ) )
2019-10-23 19:59:29 +00:00
{
2020-09-30 18:51:12 +00:00
fd . features . addFeature ( MFT_OrmMap ) ;
2019-10-23 19:59:29 +00:00
}
else
2020-09-30 18:51:12 +00:00
fd . features . addFeature ( MFT_ORMConfigVars ) ;
2019-10-23 19:59:29 +00:00
2019-10-24 01:38:13 +00:00
// Deferred Shading : Material Info Flags
fd . features . addFeature ( MFT_MatInfoFlags ) ;
2012-09-19 15:15:01 +00:00
2020-07-14 19:08:12 +00:00
if ( features . hasFeature ( MFT_isBackground ) )
{
fd . features . addFeature ( MFT_isBackground ) ;
}
2019-10-30 03:11:59 +00:00
if ( features . hasFeature ( MFT_SkyBox ) )
2012-09-19 15:15:01 +00:00
{
2019-10-30 03:11:59 +00:00
fd . features . addFeature ( MFT_StaticCubemap ) ;
fd . features . addFeature ( MFT_CubeMap ) ;
fd . features . addFeature ( MFT_SkyBox ) ;
fd . features . removeFeature ( MFT_ReflectionProbes ) ;
2020-09-30 18:51:12 +00:00
fd . features . removeFeature ( MFT_ORMConfigVars ) ;
2019-10-30 03:11:59 +00:00
fd . features . removeFeature ( MFT_MatInfoFlags ) ;
2012-09-19 15:15:01 +00:00
}
2014-12-21 20:07:42 +00:00
if ( mMaterial - > mAccuEnabled [ stageNum ] )
{
mHasAccumulation = true ;
}
// we need both diffuse and normal maps + sm3 to have an accu map
if ( fd . features [ MFT_AccuMap ] & &
( ! fd . features [ MFT_DiffuseMap ] | |
! fd . features [ MFT_NormalMap ] | |
GFX - > getPixelShaderVersion ( ) < 3.0f ) ) {
AssertWarn ( false , " SAHARA: Using an Accu Map requires SM 3.0 and a normal map. " ) ;
fd . features . removeFeature ( MFT_AccuMap ) ;
mHasAccumulation = false ;
}
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of:
renderPrePassMgr.cpp related:
A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures
B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render
C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages()
D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets)
Reflections:
A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever.
B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected.
C) 2 things best described bluntly as working hacks:
C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly.
C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material.
Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
2012-09-19 15:15:01 +00:00
// Without a base texture use the diffuse color
// feature to ensure some sort of output.
if ( ! fd . features [ MFT_DiffuseMap ] )
{
fd . features . addFeature ( MFT_DiffuseColor ) ;
// No texture coords... no overlay.
fd . features . removeFeature ( MFT_OverlayMap ) ;
}
// If we have a diffuse map and the alpha on the diffuse isn't
// zero and the color isn't pure white then multiply the color.
else if ( mMaterial - > mDiffuse [ stageNum ] . alpha > 0.0f & &
2017-06-23 16:36:20 +00:00
mMaterial - > mDiffuse [ stageNum ] ! = LinearColorF : : WHITE )
2012-09-19 15:15:01 +00:00
fd . features . addFeature ( MFT_DiffuseColor ) ;
// If lightmaps or tonemaps are enabled or we
// don't have a second UV set then we cannot
// use the overlay texture.
if ( fd . features [ MFT_LightMap ] | |
fd . features [ MFT_ToneMap ] | |
mVertexFormat - > getTexCoordCount ( ) < 2 )
fd . features . removeFeature ( MFT_OverlayMap ) ;
// If tonemaps are enabled don't use lightmap
if ( fd . features [ MFT_ToneMap ] | | mVertexFormat - > getTexCoordCount ( ) < 2 )
fd . features . removeFeature ( MFT_LightMap ) ;
// Don't allow tonemaps if we don't have a second UV set
if ( mVertexFormat - > getTexCoordCount ( ) < 2 )
fd . features . removeFeature ( MFT_ToneMap ) ;
// Always add the HDR output feature.
//
// It will be filtered out if it was disabled
// for this material creation below.
//
// Also the shader code will evaluate to a nop
// if HDR is not enabled in the scene.
//
fd . features . addFeature ( MFT_HDROut ) ;
2019-11-21 06:48:55 +00:00
fd . features . addFeature ( MFT_DebugViz ) ;
2012-09-19 15:15:01 +00:00
// If vertex color is enabled on the material's stage and
// color is present in vertex format, add diffuse vertex
// color feature.
if ( mMaterial - > mVertColor [ stageNum ] & &
mVertexFormat - > hasColor ( ) )
fd . features . addFeature ( MFT_DiffuseVertColor ) ;
2024-05-30 22:29:42 +00:00
if ( mMaterial - > mGlow [ stageNum ] )
fd . features . addFeature ( MFT_GlowMask ) ;
2012-09-19 15:15:01 +00:00
// Allow features to add themselves.
for ( U32 i = 0 ; i < FEATUREMGR - > getFeatureCount ( ) ; i + + )
{
const FeatureInfo & info = FEATUREMGR - > getAt ( i ) ;
info . feature - > determineFeature ( mMaterial ,
mVertexFormat ,
stageNum ,
* info . type ,
features ,
& fd ) ;
}
2015-01-10 19:41:25 +00:00
// Need to add the Hardware Skinning feature if its used
if ( features . hasFeature ( MFT_HardwareSkinning ) )
{
fd . features . addFeature ( MFT_HardwareSkinning ) ;
}
2012-09-19 15:15:01 +00:00
// Now disable any features that were
// not part of the input feature handle.
fd . features . filter ( features ) ;
}
bool ProcessedShaderMaterial : : _createPasses ( MaterialFeatureData & stageFeatures , U32 stageNum , const FeatureSet & features )
{
// Creates passes for the given stage
ShaderRenderPassData passData ;
U32 texIndex = 0 ;
2018-03-14 02:25:45 +00:00
for ( U32 featureIDx = 0 ; featureIDx < FEATUREMGR - > getFeatureCount ( ) ; featureIDx + + )
2012-09-19 15:15:01 +00:00
{
2018-03-14 02:25:45 +00:00
const FeatureInfo & info = FEATUREMGR - > getAt ( featureIDx ) ;
2012-09-19 15:15:01 +00:00
if ( ! stageFeatures . features . hasFeature ( * info . type ) )
continue ;
U32 numTexReg = info . feature - > getResources ( stageFeatures ) . numTexReg ;
// adds pass if blend op changes for feature
_setPassBlendOp ( info . feature , passData , texIndex , stageFeatures , stageNum , features ) ;
// Add pass if num tex reg is going to be too high
if ( passData . mNumTexReg + numTexReg > GFX - > getNumSamplers ( ) )
{
if ( ! _addPass ( passData , texIndex , stageFeatures , stageNum , features ) )
return false ;
_setPassBlendOp ( info . feature , passData , texIndex , stageFeatures , stageNum , features ) ;
}
passData . mNumTexReg + = numTexReg ;
passData . mFeatureData . features . addFeature ( * info . type ) ;
2014-11-08 00:57:28 +00:00
# if defined(TORQUE_DEBUG) && defined( TORQUE_OPENGL)
U32 oldTexNumber = texIndex ;
# endif
2012-09-19 15:15:01 +00:00
info . feature - > setTexData ( mStages [ stageNum ] , stageFeatures , passData , texIndex ) ;
2014-11-08 00:57:28 +00:00
# if defined(TORQUE_DEBUG) && defined( TORQUE_OPENGL)
if ( oldTexNumber ! = texIndex )
{
2018-03-14 02:25:45 +00:00
for ( int texNum = oldTexNumber ; texNum < texIndex ; texNum + + )
2014-11-08 00:57:28 +00:00
{
AssertFatal ( passData . mSamplerNames [ oldTexNumber ] . isNotEmpty ( ) , avar ( " ERROR: ShaderGen feature %s don't set used sampler name " , info . feature - > getName ( ) . c_str ( ) ) ) ;
}
}
# endif
2012-09-19 15:15:01 +00:00
// Add pass if tex units are maxed out
if ( texIndex > GFX - > getNumSamplers ( ) )
{
if ( ! _addPass ( passData , texIndex , stageFeatures , stageNum , features ) )
return false ;
_setPassBlendOp ( info . feature , passData , texIndex , stageFeatures , stageNum , features ) ;
}
}
2014-11-08 00:57:28 +00:00
# if defined(TORQUE_DEBUG) && defined( TORQUE_OPENGL)
2018-03-14 02:25:45 +00:00
for ( int samplerIDx = 0 ; samplerIDx < texIndex ; samplerIDx + + )
2014-11-08 00:57:28 +00:00
{
2018-03-14 02:25:45 +00:00
AssertFatal ( passData . mSamplerNames [ samplerIDx ] . isNotEmpty ( ) , " " ) ;
2014-11-08 00:57:28 +00:00
}
# endif
2012-09-19 15:15:01 +00:00
const FeatureSet & passFeatures = passData . mFeatureData . codify ( ) ;
if ( passFeatures . isNotEmpty ( ) )
{
mFeatures . merge ( passFeatures ) ;
if ( ! _addPass ( passData , texIndex , stageFeatures , stageNum , features ) )
{
mFeatures . clear ( ) ;
return false ;
}
}
return true ;
}
void ProcessedShaderMaterial : : _initMaterialParameters ( )
{
// Cleanup anything left first.
SAFE_DELETE ( mDefaultParameters ) ;
for ( U32 i = 0 ; i < mParameterHandles . size ( ) ; i + + )
SAFE_DELETE ( mParameterHandles [ i ] ) ;
// Gather the shaders as they all need to be
// passed to the ShaderMaterialParameterHandles.
Vector < GFXShader * > shaders ;
shaders . setSize ( mPasses . size ( ) ) ;
for ( U32 i = 0 ; i < mPasses . size ( ) ; i + + )
shaders [ i ] = _getRPD ( i ) - > shader ;
// Run through each shader and prepare its constants.
for ( U32 i = 0 ; i < mPasses . size ( ) ; i + + )
{
const Vector < GFXShaderConstDesc > & desc = shaders [ i ] - > getShaderConstDesc ( ) ;
Vector < GFXShaderConstDesc > : : const_iterator p = desc . begin ( ) ;
for ( ; p ! = desc . end ( ) ; p + + )
{
// Add this to our list of shader constants
GFXShaderConstDesc d ( * p ) ;
mShaderConstDesc . push_back ( d ) ;
ShaderMaterialParameterHandle * smph = new ShaderMaterialParameterHandle ( d . name , shaders ) ;
mParameterHandles . push_back ( smph ) ;
}
}
}
bool ProcessedShaderMaterial : : _addPass ( ShaderRenderPassData & rpd ,
U32 & texIndex ,
MaterialFeatureData & fd ,
U32 stageNum ,
const FeatureSet & features )
{
// Set number of textures, stage, glow, etc.
rpd . mNumTex = texIndex ;
rpd . mStageNum = stageNum ;
rpd . mGlow | = mMaterial - > mGlow [ stageNum ] ;
// Copy over features
rpd . mFeatureData . materialFeatures = fd . features ;
2014-11-08 00:57:28 +00:00
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 ] ;
}
2012-09-19 15:15:01 +00:00
// Generate shader
GFXShader : : setLogging ( true , true ) ;
2020-09-19 23:25:10 +00:00
rpd . shader = SHADERGEN - > getShader ( rpd . mFeatureData , mVertexFormat , & mUserMacros , samplers ) ;
2012-09-19 15:15:01 +00:00
if ( ! rpd . shader )
return false ;
2020-09-19 23:25:10 +00:00
rpd . shaderHandles . init ( rpd . shader ) ;
2012-09-19 15:15:01 +00:00
// If a pass glows, we glow
if ( rpd . mGlow )
mHasGlow = true ;
ShaderRenderPassData * newPass = new ShaderRenderPassData ( rpd ) ;
mPasses . push_back ( newPass ) ;
2014-11-08 00:57:28 +00:00
//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 , " " ) ;
}
2012-09-19 15:15:01 +00:00
// Give each active feature a chance to create specialized shader consts.
for ( U32 i = 0 ; i < FEATUREMGR - > getFeatureCount ( ) ; i + + )
{
const FeatureInfo & info = FEATUREMGR - > getAt ( i ) ;
if ( ! fd . features . hasFeature ( * info . type ) )
continue ;
ShaderFeatureConstHandles * fh = info . feature - > createConstHandles ( rpd . shader , mUserObject ) ;
if ( fh )
newPass - > featureShaderHandles . push_back ( fh ) ;
}
rpd . reset ( ) ;
texIndex = 0 ;
return true ;
}
void ProcessedShaderMaterial : : _setPassBlendOp ( ShaderFeature * sf ,
ShaderRenderPassData & passData ,
U32 & texIndex ,
MaterialFeatureData & stageFeatures ,
U32 stageNum ,
const FeatureSet & features )
{
if ( sf - > getBlendOp ( ) = = Material : : None )
{
return ;
}
// set up the current blend operation for multi-pass materials
if ( mPasses . size ( ) > 0 )
{
// If passData.numTexReg is 0, this is a brand new pass, so set the
// blend operation to the first feature.
if ( passData . mNumTexReg = = 0 )
{
passData . mBlendOp = sf - > getBlendOp ( ) ;
}
else
{
// numTegReg is more than zero, if this feature
// doesn't have the same blend operation, then
// we need to create yet another pass
if ( sf - > getBlendOp ( ) ! = passData . mBlendOp & & mPasses [ mPasses . size ( ) - 1 ] - > mStageNum = = stageNum )
{
_addPass ( passData , texIndex , stageFeatures , stageNum , features ) ;
passData . mBlendOp = sf - > getBlendOp ( ) ;
}
}
}
}
//
// Runtime / rendering
//
bool ProcessedShaderMaterial : : setupPass ( SceneRenderState * state , const SceneData & sgData , U32 pass )
{
PROFILE_SCOPE ( ProcessedShaderMaterial_SetupPass ) ;
// Make sure we have the pass
if ( pass > = mPasses . size ( ) )
{
// If we were rendering instanced data tell
// the device to reset that vb stream.
if ( mInstancingState )
GFX - > setVertexBuffer ( NULL , 1 ) ;
return false ;
}
_setRenderState ( state , sgData , pass ) ;
// Set shaders
ShaderRenderPassData * rpd = _getRPD ( pass ) ;
if ( rpd - > shader )
{
GFX - > setShader ( rpd - > shader ) ;
GFX - > setShaderConstBuffer ( _getShaderConstBuffer ( pass ) ) ;
_setShaderConstants ( state , sgData , pass ) ;
// If we're instancing then do the initial step to get
// set the vb pointer to the const buffer.
if ( mInstancingState )
stepInstance ( ) ;
}
else
{
2014-04-13 15:55:02 +00:00
GFX - > setupGenericShaders ( ) ;
2012-09-19 15:15:01 +00:00
GFX - > setShaderConstBuffer ( NULL ) ;
}
// Set our textures
setTextureStages ( state , sgData , pass ) ;
_setTextureTransforms ( pass ) ;
return true ;
}
void ProcessedShaderMaterial : : setTextureStages ( SceneRenderState * state , const SceneData & sgData , U32 pass )
{
PROFILE_SCOPE ( ProcessedShaderMaterial_SetTextureStages ) ;
ShaderConstHandles * handles = _getShaderConstHandles ( pass ) ;
2014-11-08 00:57:28 +00:00
AssertFatal ( handles , " " ) ;
2012-09-19 15:15:01 +00:00
// Set all of the textures we need to render the give pass.
# ifdef TORQUE_DEBUG
AssertFatal ( pass < mPasses . size ( ) , " Pass out of bounds " ) ;
# endif
RenderPassData * rpd = mPasses [ pass ] ;
GFXShaderConstBuffer * shaderConsts = _getShaderConstBuffer ( pass ) ;
NamedTexTarget * texTarget ;
GFXTextureObject * texObject ;
for ( U32 i = 0 ; i < rpd - > mNumTex ; i + + )
{
U32 currTexFlag = rpd - > mTexType [ i ] ;
if ( ! LIGHTMGR | | ! LIGHTMGR - > setTextureStage ( sgData , currTexFlag , i , shaderConsts , handles ) )
{
switch ( currTexFlag )
{
// If the flag is unset then assume its just
// a regular texture to set... nothing special.
case 0 :
default :
GFX - > setTexture ( i , rpd - > mTexSlot [ i ] . texObject ) ;
break ;
case Material : : NormalizeCube :
GFX - > setCubeTexture ( i , Material : : GetNormalizeCube ( ) ) ;
break ;
case Material : : Lightmap :
GFX - > setTexture ( i , sgData . lightmap ) ;
break ;
case Material : : ToneMapTex :
shaderConsts - > setSafe ( handles - > mToneMapTexSC , ( S32 ) i ) ;
GFX - > setTexture ( i , rpd - > mTexSlot [ i ] . texObject ) ;
break ;
case Material : : Cube :
GFX - > setCubeTexture ( i , rpd - > mCubeMap ) ;
break ;
case Material : : SGCube :
GFX - > setCubeTexture ( i , sgData . cubemap ) ;
break ;
case Material : : BackBuff :
GFX - > setTexture ( i , sgData . backBuffTex ) ;
break ;
2014-12-21 20:07:42 +00:00
case Material : : AccuMap :
if ( sgData . accuTex )
GFX - > setTexture ( i , sgData . accuTex ) ;
else
GFX - > setTexture ( i , GFXTexHandle : : ZERO ) ;
break ;
2012-09-19 15:15:01 +00:00
case Material : : TexTarget :
{
texTarget = rpd - > mTexSlot [ i ] . texTarget ;
2025-05-28 22:45:02 +00:00
if ( ! mMaterial - > getDiffuseMap ( 0 ) )
2025-03-30 11:22:17 +00:00
{
GFX - > setTexture ( i , NULL ) ;
break ;
}
2025-03-30 10:22:42 +00:00
texObject = mMaterial - > getDiffuseMapAsset ( 0 ) - > getTexture ( & GFXStaticTextureSRGBProfile ) ;
2012-09-19 15:15:01 +00:00
if ( ! texTarget )
{
2025-03-25 18:22:26 +00:00
// try again.
texTarget = mMaterial - > getDiffuseMapAsset ( 0 ) - > getNamedTarget ( ) ;
if ( ! texTarget )
{
2025-03-30 10:22:42 +00:00
GFX - > setTexture ( i , texObject ) ;
2025-03-25 18:22:26 +00:00
break ;
}
else
{
rpd - > mTexSlot [ i ] . texTarget = texTarget ;
}
2012-09-19 15:15:01 +00:00
}
// 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 [ i ] - > isValid ( ) & & texObject )
{
const Point3I & targetSz = texObject - > getSize ( ) ;
const RectI & targetVp = texTarget - > getViewport ( ) ;
Point4F rtParams ;
ScreenSpace : : RenderTargetParameters ( targetSz , targetVp , rtParams ) ;
shaderConsts - > set ( handles - > mRTParamsSC [ i ] , rtParams ) ;
}
GFX - > setTexture ( i , texObject ) ;
break ;
}
}
}
}
}
void ProcessedShaderMaterial : : _setTextureTransforms ( const U32 pass )
{
PROFILE_SCOPE ( ProcessedShaderMaterial_SetTextureTransforms ) ;
ShaderConstHandles * handles = _getShaderConstHandles ( pass ) ;
2025-03-07 01:04:22 +00:00
if ( ! handles - > mTexMatSC - > isValid ( ) )
return ;
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
MatrixF texMat ( true ) ;
mMaterial - > updateTimeBasedParams ( ) ;
F32 waveOffset = _getWaveOffset ( pass ) ; // offset is between 0.0 and 1.0
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
// --- Scroll Animation ---
if ( mMaterial - > mAnimFlags [ pass ] & Material : : Scroll )
{
Point3F offset = ( mMaterial - > mAnimFlags [ pass ] & Material : : Wave )
? Point3F ( mMaterial - > mScrollDir [ pass ] . x * waveOffset ,
mMaterial - > mScrollDir [ pass ] . y * waveOffset , 0.0f )
: Point3F ( mMaterial - > mScrollOffset [ pass ] . x ,
mMaterial - > mScrollOffset [ pass ] . y , 0.0f ) ;
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
MatrixF scrollMat ( true ) ;
scrollMat . setColumn ( 3 , offset ) ;
texMat . mul ( scrollMat ) ;
}
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
// --- Rotation Animation ---
if ( mMaterial - > mAnimFlags [ pass ] & Material : : Rotate )
{
F32 rotationAngle = ( mMaterial - > mAnimFlags [ pass ] & Material : : Wave )
? waveOffset * M_2PI
: mMaterial - > mRotPos [ pass ] ;
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
Point3F pivotPoint (
mMaterial - > mRotPivotOffset [ pass ] . x ,
mMaterial - > mRotPivotOffset [ pass ] . y ,
0.0f ) ;
2025-04-03 04:07:09 +00:00
MatrixF rotationMat = MatrixF ( EulerF ( 0.0 , 0.0 , rotationAngle ) , Point3F ( 0.5 , 0.5 , 0.0 ) ) ;
MatrixF test ( true ) ;
test . setColumn ( 3 , pivotPoint ) ;
rotationMat . mul ( test ) ;
rotationMat . displace ( texMat . getPosition ( ) ) ;
2025-03-07 01:04:22 +00:00
// Apply final rotation matrix
2025-04-03 04:07:09 +00:00
texMat = rotationMat ;
2025-03-07 01:04:22 +00:00
}
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
// --- Scale Animation ---
if ( ( mMaterial - > mAnimFlags [ pass ] & Material : : Scale ) & & ( mMaterial - > mAnimFlags [ pass ] & Material : : Wave ) )
{
F32 scaleFactor = mFabs ( waveOffset ) ;
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
MatrixF scaleMat ( true ) ;
scaleMat . setRow ( 0 , Point3F ( scaleFactor , 0.0f , 0.0f ) ) ;
scaleMat . setRow ( 1 , Point3F ( 0.0f , scaleFactor , 0.0f ) ) ;
scaleMat . setRow ( 2 , Point3F ( 0.0f , 0.0f , scaleFactor ) ) ;
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
// Apply final scale matrix
texMat . mul ( scaleMat ) ;
}
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
// --- Sequence Animation ---
if ( mMaterial - > mAnimFlags [ pass ] & Material : : Sequence )
{
U32 frameNum = static_cast < U32 > ( MATMGR - > getTotalTime ( ) * mMaterial - > mSeqFramePerSec [ pass ] ) ;
F32 offset = frameNum * mMaterial - > mSeqSegSize [ pass ] ;
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
MatrixF sequenceMat ( true ) ;
sequenceMat . setColumn ( 3 , Point3F ( offset , 0.0f , 0.0f ) ) ;
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
if ( mMaterial - > mAnimFlags [ pass ] & Material : : Scale )
sequenceMat . scale ( Point3F ( mMaterial - > mSeqSegSize [ pass ] , 1.0f , 1.0f ) ) ;
2012-09-19 15:15:01 +00:00
2025-03-07 01:04:22 +00:00
texMat . mul ( sequenceMat ) ;
2012-09-19 15:15:01 +00:00
}
2025-03-07 01:04:22 +00:00
GFXShaderConstBuffer * shaderConsts = _getShaderConstBuffer ( pass ) ;
shaderConsts - > setSafe ( handles - > mTexMatSC , texMat ) ;
2012-09-19 15:15:01 +00:00
}
//--------------------------------------------------------------------------
// Get wave offset for texture animations using a wave transform
//--------------------------------------------------------------------------
F32 ProcessedShaderMaterial : : _getWaveOffset ( U32 stage )
{
switch ( mMaterial - > mWaveType [ stage ] )
{
case Material : : Sin :
{
return mMaterial - > mWaveAmp [ stage ] * mSin ( M_2PI * mMaterial - > mWavePos [ stage ] ) ;
break ;
}
case Material : : Triangle :
{
F32 frac = mMaterial - > mWavePos [ stage ] - mFloor ( mMaterial - > mWavePos [ stage ] ) ;
if ( frac > 0.0 & & frac < = 0.25 )
{
return mMaterial - > mWaveAmp [ stage ] * frac * 4.0 ;
}
if ( frac > 0.25 & & frac < = 0.5 )
{
return mMaterial - > mWaveAmp [ stage ] * ( 1.0 - ( ( frac - 0.25 ) * 4.0 ) ) ;
}
if ( frac > 0.5 & & frac < = 0.75 )
{
return mMaterial - > mWaveAmp [ stage ] * ( frac - 0.5 ) * - 4.0 ;
}
if ( frac > 0.75 & & frac < = 1.0 )
{
return - mMaterial - > mWaveAmp [ stage ] * ( 1.0 - ( ( frac - 0.75 ) * 4.0 ) ) ;
}
break ;
}
case Material : : Square :
{
F32 frac = mMaterial - > mWavePos [ stage ] - mFloor ( mMaterial - > mWavePos [ stage ] ) ;
if ( frac > 0.0 & & frac < = 0.5 )
{
return 0.0 ;
}
else
{
return mMaterial - > mWaveAmp [ stage ] ;
}
break ;
}
}
return 0.0 ;
}
void ProcessedShaderMaterial : : _setShaderConstants ( SceneRenderState * state , const SceneData & sgData , U32 pass )
{
PROFILE_SCOPE ( ProcessedShaderMaterial_SetShaderConstants ) ;
GFXShaderConstBuffer * shaderConsts = _getShaderConstBuffer ( pass ) ;
ShaderConstHandles * handles = _getShaderConstHandles ( pass ) ;
U32 stageNum = getStageFromPass ( pass ) ;
// First we do all the constants which are not
// controlled via the material... we have to
// set these all the time as they could change.
if ( handles - > mFogDataSC - > isValid ( ) )
{
Point3F fogData ;
fogData . x = sgData . fogDensity ;
fogData . y = sgData . fogDensityOffset ;
fogData . z = sgData . fogHeightFalloff ;
shaderConsts - > set ( handles - > mFogDataSC , fogData ) ;
}
shaderConsts - > setSafe ( handles - > mFogColorSC , sgData . fogColor ) ;
if ( handles - > mOneOverFarplane - > isValid ( ) )
{
const F32 & invfp = 1.0f / state - > getFarPlane ( ) ;
Point4F oneOverFP ( invfp , invfp , invfp , invfp ) ;
shaderConsts - > set ( handles - > mOneOverFarplane , oneOverFP ) ;
}
shaderConsts - > setSafe ( handles - > mAccumTimeSC , MATMGR - > getTotalTime ( ) ) ;
2022-11-22 03:12:23 +00:00
shaderConsts - > setSafe ( handles - > mDampnessSC , MATMGR - > getDampnessClamped ( ) ) ;
2024-04-16 18:51:41 +00:00
shaderConsts - > setSafe ( handles - > mIsCapturingSC , ( S32 ) state - > isCapturing ( ) ) ;
2012-09-19 15:15:01 +00:00
// If the shader constants have not been lost then
// they contain the content from a previous render pass.
//
// In this case we can skip updating the material constants
// which do not change frame to frame.
//
// NOTE: This assumes we're not animating material parameters
// in a way that doesn't cause a shader reload... this isn't
// being done now, but it could change in the future.
//
if ( ! shaderConsts - > wasLost ( ) )
return ;
2020-09-30 18:51:12 +00:00
shaderConsts - > setSafe ( handles - > mRoughnessSC , mMaterial - > mRoughness [ stageNum ] ) ;
2018-09-16 01:19:57 +00:00
shaderConsts - > setSafe ( handles - > mMetalnessSC , mMaterial - > mMetalness [ stageNum ] ) ;
2019-10-30 23:29:11 +00:00
shaderConsts - > setSafe ( handles - > mGlowMulSC , mMaterial - > mGlowMul [ stageNum ] ) ;
2012-09-19 15:15:01 +00:00
shaderConsts - > setSafe ( handles - > mParallaxInfoSC , mMaterial - > mParallaxScale [ stageNum ] ) ;
shaderConsts - > setSafe ( handles - > mMinnaertConstantSC , mMaterial - > mMinnaertConstant [ stageNum ] ) ;
if ( handles - > mSubSurfaceParamsSC - > isValid ( ) )
{
Point4F subSurfParams ;
2017-06-23 16:36:20 +00:00
dMemcpy ( & subSurfParams , & mMaterial - > mSubSurfaceColor [ stageNum ] , sizeof ( LinearColorF ) ) ;
2012-09-19 15:15:01 +00:00
subSurfParams . w = mMaterial - > mSubSurfaceRolloff [ stageNum ] ;
shaderConsts - > set ( handles - > mSubSurfaceParamsSC , subSurfParams ) ;
}
if ( handles - > mRTSizeSC - > isValid ( ) )
{
const Point2I & resolution = GFX - > getActiveRenderTarget ( ) - > getSize ( ) ;
Point2F pixelShaderConstantData ;
pixelShaderConstantData . x = resolution . x ;
pixelShaderConstantData . y = resolution . y ;
shaderConsts - > set ( handles - > mRTSizeSC , pixelShaderConstantData ) ;
}
if ( handles - > mOneOverRTSizeSC - > isValid ( ) )
{
const Point2I & resolution = GFX - > getActiveRenderTarget ( ) - > getSize ( ) ;
Point2F oneOverTargetSize ( 1.0f / ( F32 ) resolution . x , 1.0f / ( F32 ) resolution . y ) ;
shaderConsts - > set ( handles - > mOneOverRTSizeSC , oneOverTargetSize ) ;
}
// set detail scale
shaderConsts - > setSafe ( handles - > mDetailScaleSC , mMaterial - > mDetailScale [ stageNum ] ) ;
shaderConsts - > setSafe ( handles - > mDetailBumpStrength , mMaterial - > mDetailNormalMapStrength [ stageNum ] ) ;
// MFT_ImposterVert
if ( handles - > mImposterUVs - > isValid ( ) )
{
U32 uvCount = getMin ( mMaterial - > mImposterUVs . size ( ) , 64 ) ; // See imposter.hlsl
AlignedArray < Point4F > imposterUVs ( uvCount , sizeof ( Point4F ) , ( U8 * ) mMaterial - > mImposterUVs . address ( ) , false ) ;
shaderConsts - > set ( handles - > mImposterUVs , imposterUVs ) ;
}
shaderConsts - > setSafe ( handles - > mImposterLimits , mMaterial - > mImposterLimits ) ;
// Diffuse
shaderConsts - > setSafe ( handles - > mDiffuseColorSC , mMaterial - > mDiffuse [ stageNum ] ) ;
shaderConsts - > setSafe ( handles - > mAlphaTestValueSC , mClampF ( ( F32 ) mMaterial - > mAlphaRef / 255.0f , 0.0f , 1.0f ) ) ;
if ( handles - > mDiffuseAtlasParamsSC )
{
Point4F atlasParams ( 1.0f / mMaterial - > mCellLayout [ stageNum ] . x , // 1 / num_horizontal
1.0f / mMaterial - > mCellLayout [ stageNum ] . y , // 1 / num_vertical
mMaterial - > mCellSize [ stageNum ] , // tile size in pixels
getBinLog2 ( mMaterial - > mCellSize [ stageNum ] ) ) ; // pow of 2 of tile size in pixels 2^9 = 512, 2^10=1024 etc
shaderConsts - > setSafe ( handles - > mDiffuseAtlasParamsSC , atlasParams ) ;
}
if ( handles - > mBumpAtlasParamsSC )
{
Point4F atlasParams ( 1.0f / mMaterial - > mCellLayout [ stageNum ] . x , // 1 / num_horizontal
1.0f / mMaterial - > mCellLayout [ stageNum ] . y , // 1 / num_vertical
mMaterial - > mCellSize [ stageNum ] , // tile size in pixels
getBinLog2 ( mMaterial - > mCellSize [ stageNum ] ) ) ; // pow of 2 of tile size in pixels 2^9 = 512, 2^10=1024 etc
shaderConsts - > setSafe ( handles - > mBumpAtlasParamsSC , atlasParams ) ;
}
if ( handles - > mDiffuseAtlasTileSC )
{
// Sanity check the wrap flags
//AssertWarn(mMaterial->mTextureAddressModeU == mMaterial->mTextureAddressModeV, "Addresing mode mismatch, texture atlasing will be confused");
Point4F atlasTileParams ( mMaterial - > mCellIndex [ stageNum ] . x , // Tile co-ordinate, ie: [0, 3]
mMaterial - > mCellIndex [ stageNum ] . y ,
0.0f , 0.0f ) ; // TODO: Wrap mode flags?
shaderConsts - > setSafe ( handles - > mDiffuseAtlasTileSC , atlasTileParams ) ;
}
if ( handles - > mBumpAtlasTileSC )
{
// Sanity check the wrap flags
//AssertWarn(mMaterial->mTextureAddressModeU == mMaterial->mTextureAddressModeV, "Addresing mode mismatch, texture atlasing will be confused");
Point4F atlasTileParams ( mMaterial - > mCellIndex [ stageNum ] . x , // Tile co-ordinate, ie: [0, 3]
mMaterial - > mCellIndex [ stageNum ] . y ,
0.0f , 0.0f ) ; // TODO: Wrap mode flags?
shaderConsts - > setSafe ( handles - > mBumpAtlasTileSC , atlasTileParams ) ;
}
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of:
renderPrePassMgr.cpp related:
A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures
B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render
C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages()
D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets)
Reflections:
A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever.
B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected.
C) 2 things best described bluntly as working hacks:
C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly.
C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material.
Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
// Deferred Shading: Determine Material Info Flags
S32 matInfoFlags =
2022-12-29 19:38:30 +00:00
( mMaterial - > mReceiveShadows [ stageNum ] ? 1 : 0 ) | //ReceiveShadows
2023-02-15 02:57:44 +00:00
( mMaterial - > mSubSurface [ stageNum ] ? 1 < < 2 : 0 ) | //subsurface
( mMaterial - > mIgnoreLighting [ stageNum ] ? 1 < < 3 : 0 ) ; //IgnoreLighting
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of:
renderPrePassMgr.cpp related:
A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures
B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render
C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages()
D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets)
Reflections:
A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever.
B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected.
C) 2 things best described bluntly as working hacks:
C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly.
C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material.
Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
mMaterial - > mMatInfoFlags [ stageNum ] = matInfoFlags / 255.0f ;
shaderConsts - > setSafe ( handles - > mMatInfoFlagsSC , mMaterial - > mMatInfoFlags [ stageNum ] ) ;
2014-12-21 20:07:42 +00:00
if ( handles - > mAccuScaleSC - > isValid ( ) )
shaderConsts - > set ( handles - > mAccuScaleSC , mMaterial - > mAccuScale [ stageNum ] ) ;
if ( handles - > mAccuDirectionSC - > isValid ( ) )
shaderConsts - > set ( handles - > mAccuDirectionSC , mMaterial - > mAccuDirection [ stageNum ] ) ;
if ( handles - > mAccuStrengthSC - > isValid ( ) )
shaderConsts - > set ( handles - > mAccuStrengthSC , mMaterial - > mAccuStrength [ stageNum ] ) ;
if ( handles - > mAccuCoverageSC - > isValid ( ) )
shaderConsts - > set ( handles - > mAccuCoverageSC , mMaterial - > mAccuCoverage [ stageNum ] ) ;
if ( handles - > mAccuSpecularSC - > isValid ( ) )
shaderConsts - > set ( handles - > mAccuSpecularSC , mMaterial - > mAccuSpecular [ stageNum ] ) ;
2012-09-19 15:15:01 +00:00
}
bool ProcessedShaderMaterial : : _hasCubemap ( U32 pass )
{
// Only support cubemap on the first stage
if ( mPasses [ pass ] - > mStageNum > 0 )
return false ;
if ( mPasses [ pass ] - > mCubeMap )
return true ;
return false ;
}
void ProcessedShaderMaterial : : setTransforms ( const MatrixSet & matrixSet , SceneRenderState * state , const U32 pass )
{
PROFILE_SCOPE ( ProcessedShaderMaterial_setTransforms ) ;
GFXShaderConstBuffer * shaderConsts = _getShaderConstBuffer ( pass ) ;
ShaderConstHandles * handles = _getShaderConstHandles ( pass ) ;
// The MatrixSet will lazily generate a matrix under the
// various 'get' methods, so inline the test for a valid
// shader constant handle to avoid that work when we can.
if ( handles - > mModelViewProjSC - > isValid ( ) )
shaderConsts - > set ( handles - > mModelViewProjSC , matrixSet . getWorldViewProjection ( ) ) ;
if ( handles - > mObjTransSC - > isValid ( ) )
shaderConsts - > set ( handles - > mObjTransSC , matrixSet . getObjectToWorld ( ) ) ;
if ( handles - > mWorldToObjSC - > isValid ( ) )
shaderConsts - > set ( handles - > mWorldToObjSC , matrixSet . getWorldToObject ( ) ) ;
if ( handles - > mWorldToCameraSC - > isValid ( ) )
shaderConsts - > set ( handles - > mWorldToCameraSC , matrixSet . getWorldToCamera ( ) ) ;
2018-10-28 10:42:26 +00:00
if ( handles - > mCameraToWorldSC - > isValid ( ) )
shaderConsts - > set ( handles - > mCameraToWorldSC , matrixSet . getCameraToWorld ( ) ) ;
2012-09-19 15:15:01 +00:00
if ( handles - > mWorldViewOnlySC - > isValid ( ) )
shaderConsts - > set ( handles - > mWorldViewOnlySC , matrixSet . getObjectToCamera ( ) ) ;
if ( handles - > mViewToObjSC - > isValid ( ) )
shaderConsts - > set ( handles - > mViewToObjSC , matrixSet . getCameraToObject ( ) ) ;
if ( handles - > mViewProjSC - > isValid ( ) )
shaderConsts - > set ( handles - > mViewProjSC , matrixSet . getWorldToScreen ( ) ) ;
if ( handles - > mCubeTransSC - > isValid ( ) & &
( _hasCubemap ( pass ) | | mMaterial - > mDynamicCubemap ) )
{
// TODO: Could we not remove this constant? Use mObjTransSC and cast to float3x3 instead?
shaderConsts - > set ( handles - > mCubeTransSC , matrixSet . getObjectToWorld ( ) , GFXSCT_Float3x3 ) ;
}
if ( handles - > m_vEyeSC - > isValid ( ) )
shaderConsts - > set ( handles - > m_vEyeSC , state - > getVectorEye ( ) ) ;
}
2015-01-10 19:41:25 +00:00
void ProcessedShaderMaterial : : setNodeTransforms ( const MatrixF * transforms , const U32 transformCount , const U32 pass )
{
PROFILE_SCOPE ( ProcessedShaderMaterial_setNodeTransforms ) ;
GFXShaderConstBuffer * shaderConsts = _getShaderConstBuffer ( pass ) ;
ShaderConstHandles * handles = _getShaderConstHandles ( pass ) ;
if ( handles - > mNodeTransforms - > isValid ( ) )
{
S32 realTransformCount = getMin ( transformCount , TSShape : : smMaxSkinBones ) ;
shaderConsts - > set ( handles - > mNodeTransforms , transforms , realTransformCount , GFXSCT_Float4x3 ) ;
}
}
2017-10-26 22:06:27 +00:00
void ProcessedShaderMaterial : : setCustomShaderData ( Vector < CustomShaderBindingData > & shaderData , const U32 pass )
{
2025-03-30 10:22:42 +00:00
PROFILE_SCOPE ( ProcessedShaderMaterial_setCustomShaderData ) ;
GFXShaderConstBuffer * shaderConsts = _getShaderConstBuffer ( pass ) ;
ShaderConstHandles * handles = _getShaderConstHandles ( pass ) ;
for ( U32 i = 0 ; i < shaderData . size ( ) ; i + + )
{
//roll through and try setting our data!
for ( U32 h = 0 ; h < handles - > mCustomHandles . size ( ) ; + + h )
{
if ( handles - > mCustomHandles [ h ] . handleName = = shaderData [ i ] . getHandleName ( ) )
{
if ( handles - > mCustomHandles [ h ] . handle - > isValid ( ) )
{
CustomShaderBindingData : : UniformType type = shaderData [ i ] . getType ( ) ;
if ( type = = CustomShaderBindingData : : Float )
shaderConsts - > setSafe ( handles - > mCustomHandles [ h ] . handle , shaderData [ i ] . getFloat ( ) ) ;
else if ( type = = CustomShaderBindingData : : Float2 )
shaderConsts - > setSafe ( handles - > mCustomHandles [ h ] . handle , shaderData [ i ] . getFloat2 ( ) ) ;
else if ( type = = CustomShaderBindingData : : Float3 )
shaderConsts - > setSafe ( handles - > mCustomHandles [ h ] . handle , shaderData [ i ] . getFloat3 ( ) ) ;
else if ( type = = CustomShaderBindingData : : Float4 )
shaderConsts - > setSafe ( handles - > mCustomHandles [ h ] . handle , shaderData [ i ] . getFloat4 ( ) ) ;
break ;
}
}
}
}
2017-10-26 22:06:27 +00:00
}
2012-09-19 15:15:01 +00:00
void ProcessedShaderMaterial : : setSceneInfo ( SceneRenderState * state , const SceneData & sgData , U32 pass )
{
2018-09-16 01:19:57 +00:00
PROFILE_SCOPE ( ProcessedShaderMaterial_setSceneInfo ) ;
2012-09-19 15:15:01 +00:00
GFXShaderConstBuffer * shaderConsts = _getShaderConstBuffer ( pass ) ;
ShaderConstHandles * handles = _getShaderConstHandles ( pass ) ;
// Set cubemap stuff here (it's convenient!)
const Point3F & eyePosWorld = state - > getCameraPosition ( ) ;
2018-09-16 01:19:57 +00:00
if ( _hasCubemap ( pass ) | | mMaterial - > mDynamicCubemap )
2012-09-19 15:15:01 +00:00
{
2018-09-16 01:19:57 +00:00
if ( handles - > mCubeEyePosSC - > isValid ( ) )
2012-09-19 15:15:01 +00:00
{
Point3F cubeEyePos = eyePosWorld - sgData . objTrans - > getPosition ( ) ;
2018-09-16 01:19:57 +00:00
shaderConsts - > set ( handles - > mCubeEyePosSC , cubeEyePos ) ;
2012-09-19 15:15:01 +00:00
}
}
2018-09-16 01:19:57 +00:00
if ( sgData . cubemap )
shaderConsts - > setSafe ( handles - > mCubeMipsSC , ( F32 ) sgData . cubemap - > getMipMapLevels ( ) ) ;
else
2025-03-25 19:25:29 +00:00
shaderConsts - > setSafe ( handles - > mCubeMipsSC , ( F32 ) getBinLog2 ( PROBEMGR - > getProbeTexSize ( ) ) ) ;
2012-09-19 15:15:01 +00:00
shaderConsts - > setSafe ( handles - > mVisiblitySC , sgData . visibility ) ;
shaderConsts - > setSafe ( handles - > mEyePosWorldSC , eyePosWorld ) ;
if ( handles - > mEyePosSC - > isValid ( ) )
{
MatrixF tempMat ( * sgData . objTrans ) ;
tempMat . inverse ( ) ;
Point3F eyepos ;
tempMat . mulP ( eyePosWorld , & eyepos ) ;
shaderConsts - > set ( handles - > mEyePosSC , eyepos ) ;
}
2018-09-17 03:15:07 +00:00
shaderConsts - > setSafe ( handles - > mEyeMatSC , state - > getCameraTransform ( ) ) ;
ShaderRenderPassData * rpd = _getRPD ( pass ) ;
for ( U32 i = 0 ; i < rpd - > featureShaderHandles . size ( ) ; i + + )
rpd - > featureShaderHandles [ i ] - > setConsts ( state , sgData , shaderConsts ) ;
2012-09-19 15:15:01 +00:00
2018-09-17 03:15:07 +00:00
LIGHTMGR - > setLightInfo ( this , mMaterial , sgData , state , pass , shaderConsts ) ;
2012-09-19 15:15:01 +00:00
2019-06-27 05:36:56 +00:00
PROBEMGR - > setProbeInfo ( this , mMaterial , sgData , state , pass , shaderConsts ) ;
2012-09-19 15:15:01 +00:00
}
void ProcessedShaderMaterial : : setBuffers ( GFXVertexBufferHandleBase * vertBuffer , GFXPrimitiveBufferHandle * primBuffer )
{
PROFILE_SCOPE ( ProcessedShaderMaterial_setBuffers ) ;
// If we're not instanced then just call the parent.
if ( ! mInstancingState )
{
Parent : : setBuffers ( vertBuffer , primBuffer ) ;
return ;
}
PROFILE_SCOPE ( ProcessedShaderMaterial_setBuffers_instancing ) ;
const S32 instCount = mInstancingState - > getCount ( ) ;
AssertFatal ( instCount > 0 ,
" ProcessedShaderMaterial::setBuffers - No instances rendered! " ) ;
// Nothing special here.
GFX - > setPrimitiveBuffer ( * primBuffer ) ;
// Set the first stream the the normal VB and set the
// correct frequency for the number of instances to render.
GFX - > setVertexBuffer ( * vertBuffer , 0 , instCount ) ;
// Get a volatile VB and fill it with the vertex data.
const GFXVertexFormat * instFormat = mInstancingState - > getFormat ( ) ;
GFXVertexBufferDataHandle instVB ;
instVB . set ( GFX , instFormat - > getSizeInBytes ( ) , instFormat , instCount , GFXBufferTypeVolatile ) ;
U8 * dest = instVB . lock ( ) ;
2013-06-30 15:51:38 +00:00
if ( ! dest ) return ;
2012-09-19 15:15:01 +00:00
dMemcpy ( dest , mInstancingState - > getBuffer ( ) , instFormat - > getSizeInBytes ( ) * instCount ) ;
instVB . unlock ( ) ;
// Set the instance vb for streaming.
GFX - > setVertexBuffer ( instVB , 1 , 1 ) ;
// Finally set the vertex format which defines
// both of the streams.
GFX - > setVertexFormat ( mInstancingState - > getDeclFormat ( ) ) ;
// Done... reset the count.
mInstancingState - > resetStep ( ) ;
}
bool ProcessedShaderMaterial : : stepInstance ( )
{
PROFILE_SCOPE ( ProcessedShaderMaterial_stepInstance ) ;
AssertFatal ( mInstancingState , " ProcessedShaderMaterial::stepInstance - This material isn't instanced! " ) ;
return mInstancingState - > step ( & _getShaderConstBuffer ( 0 ) - > mInstPtr ) ;
}
MaterialParameters * ProcessedShaderMaterial : : allocMaterialParameters ( )
{
ShaderMaterialParameters * smp = new ShaderMaterialParameters ( ) ;
Vector < GFXShaderConstBufferRef > buffers ( __FILE__ , __LINE__ ) ;
buffers . setSize ( mPasses . size ( ) ) ;
for ( U32 i = 0 ; i < mPasses . size ( ) ; i + + )
buffers [ i ] = _getRPD ( i ) - > shader - > allocConstBuffer ( ) ;
// smp now owns these buffers.
smp - > setBuffers ( mShaderConstDesc , buffers ) ;
return smp ;
}
MaterialParameterHandle * ProcessedShaderMaterial : : getMaterialParameterHandle ( const String & name )
{
// Search our list
for ( U32 i = 0 ; i < mParameterHandles . size ( ) ; i + + )
{
if ( mParameterHandles [ i ] - > getName ( ) . equal ( name ) )
return mParameterHandles [ i ] ;
}
// If we didn't find it, we have to add it to support shader reloading.
Vector < GFXShader * > shaders ;
shaders . setSize ( mPasses . size ( ) ) ;
for ( U32 i = 0 ; i < mPasses . size ( ) ; i + + )
shaders [ i ] = _getRPD ( i ) - > shader ;
ShaderMaterialParameterHandle * smph = new ShaderMaterialParameterHandle ( name , shaders ) ;
mParameterHandles . push_back ( smph ) ;
return smph ;
}
/// This is here to deal with the differences between ProcessedCustomMaterials and ProcessedShaderMaterials.
GFXShaderConstBuffer * ProcessedShaderMaterial : : _getShaderConstBuffer ( const U32 pass )
{
2013-06-30 15:47:43 +00:00
if ( mCurrentParams & & pass < mPasses . size ( ) )
2012-09-19 15:15:01 +00:00
{
return static_cast < ShaderMaterialParameters * > ( mCurrentParams ) - > getBuffer ( pass ) ;
}
return NULL ;
}
ShaderConstHandles * ProcessedShaderMaterial : : _getShaderConstHandles ( const U32 pass )
{
if ( pass < mPasses . size ( ) )
{
return & _getRPD ( pass ) - > shaderHandles ;
}
return NULL ;
}
void ProcessedShaderMaterial : : dumpMaterialInfo ( )
{
for ( U32 i = 0 ; i < getNumPasses ( ) ; i + + )
{
const ShaderRenderPassData * passData = _getRPD ( i ) ;
if ( passData = = NULL )
continue ;
const GFXShader * shader = passData - > shader ;
if ( shader = = NULL )
Con : : printf ( " [%i] [NULL shader] " , i ) ;
else
Con : : printf ( " [%i] %s " , i , shader - > describeSelf ( ) . c_str ( ) ) ;
}
}
2019-05-13 05:28:23 +00:00
void ProcessedShaderMaterial : : getMaterialInfo ( GuiTreeViewCtrl * tree , U32 item )
{
for ( U32 i = 0 ; i < getNumPasses ( ) ; i + + )
{
const ShaderRenderPassData * passData = _getRPD ( i ) ;
if ( passData = = NULL )
continue ;
char passStr [ 64 ] ;
dSprintf ( passStr , 64 , " Pass Number: %i " , i ) ;
U32 passItem = tree - > insertItem ( item , passStr ) ;
const GFXShader * shader = passData - > shader ;
if ( shader = = NULL )
tree - > insertItem ( passItem , " [NULL shader] " ) ;
else
tree - > insertItem ( passItem , shader - > describeSelf ( ) . c_str ( ) ) ;
}
}