processedShaderMaterial.h
Engine/source/materials/processedShaderMaterial.h
Classes:
class
Hold the instancing state data for the material.
class
class
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _MATERIALS_PROCESSEDSHADERMATERIAL_H_ 25#define _MATERIALS_PROCESSEDSHADERMATERIAL_H_ 26 27#ifndef _MATERIALS_PROCESSEDMATERIAL_H_ 28#include "processedMaterial.h" 29#endif 30#ifndef _GFXSHADER_H_ 31#include "gfx/gfxShader.h" 32#endif 33 34class GenericConstBufferLayout; 35class ShaderData; 36class LightInfo; 37class ShaderMaterialParameterHandle; 38class ShaderFeatureConstHandles; 39class CustomMaterial; 40 41 42class ShaderConstHandles 43{ 44public: 45 GFXShaderConstHandle* mDiffuseColorSC; 46 GFXShaderConstHandle* mToneMapTexSC; 47 GFXShaderConstHandle* mTexMatSC; 48 GFXShaderConstHandle* mSpecularColorSC; 49 GFXShaderConstHandle* mSpecularPowerSC; 50 GFXShaderConstHandle* mSpecularStrengthSC; 51 GFXShaderConstHandle* mParallaxInfoSC; 52 GFXShaderConstHandle* mAccuScaleSC; 53 GFXShaderConstHandle* mAccuDirectionSC; 54 GFXShaderConstHandle* mAccuStrengthSC; 55 GFXShaderConstHandle* mAccuCoverageSC; 56 GFXShaderConstHandle* mAccuSpecularSC; 57 GFXShaderConstHandle* mFogDataSC; 58 GFXShaderConstHandle* mFogColorSC; 59 GFXShaderConstHandle* mDetailScaleSC; 60 GFXShaderConstHandle* mVisiblitySC; 61 GFXShaderConstHandle* mColorMultiplySC; 62 GFXShaderConstHandle* mAlphaTestValueSC; 63 GFXShaderConstHandle* mModelViewProjSC; 64 GFXShaderConstHandle* mWorldViewOnlySC; 65 GFXShaderConstHandle* mWorldToCameraSC; 66 GFXShaderConstHandle* mWorldToObjSC; 67 GFXShaderConstHandle* mViewToObjSC; 68 GFXShaderConstHandle* mCubeTransSC; 69 GFXShaderConstHandle* mObjTransSC; 70 GFXShaderConstHandle* mCubeEyePosSC; 71 GFXShaderConstHandle* mEyePosSC; 72 GFXShaderConstHandle* mEyePosWorldSC; 73 GFXShaderConstHandle* m_vEyeSC; 74 GFXShaderConstHandle* mEyeMatSC; 75 GFXShaderConstHandle* mOneOverFarplane; 76 GFXShaderConstHandle* mAccumTimeSC; 77 GFXShaderConstHandle* mMinnaertConstantSC; 78 GFXShaderConstHandle* mSubSurfaceParamsSC; 79 GFXShaderConstHandle* mDiffuseAtlasParamsSC; 80 GFXShaderConstHandle* mBumpAtlasParamsSC; 81 GFXShaderConstHandle* mDiffuseAtlasTileSC; 82 GFXShaderConstHandle* mBumpAtlasTileSC; 83 GFXShaderConstHandle *mRTSizeSC; 84 GFXShaderConstHandle *mOneOverRTSizeSC; 85 GFXShaderConstHandle* mDetailBumpStrength; 86 GFXShaderConstHandle* mViewProjSC; 87 88 GFXShaderConstHandle *mImposterUVs; 89 GFXShaderConstHandle *mImposterLimits; 90 91 // Deferred Shading : Material Info Flags 92 GFXShaderConstHandle* mMatInfoFlagsSC; 93 94 GFXShaderConstHandle* mTexHandlesSC[Material::MAX_TEX_PER_PASS]; 95 GFXShaderConstHandle* mRTParamsSC[TEXTURE_STAGE_COUNT]; 96 97 GFXShaderConstHandle* mNodeTransforms; 98 99 void init( GFXShader* shader, CustomMaterial* mat = NULL ); 100}; 101 102class ShaderRenderPassData : public RenderPassData 103{ 104 typedef RenderPassData Parent; 105 106public: 107 108 virtual ~ShaderRenderPassData() { reset(); } 109 110 GFXShaderRef shader; 111 ShaderConstHandles shaderHandles; 112 Vector<ShaderFeatureConstHandles*> featureShaderHandles; 113 114 virtual void reset(); 115 virtual String describeSelf() const; 116}; 117 118class ProcessedShaderMaterial : public ProcessedMaterial 119{ 120 typedef ProcessedMaterial Parent; 121public: 122 123 ProcessedShaderMaterial(); 124 ProcessedShaderMaterial(Material &mat); 125 ~ProcessedShaderMaterial(); 126 127 // ProcessedMaterial 128 virtual bool init( const FeatureSet &features, 129 const GFXVertexFormat *vertexFormat, 130 const MatFeaturesDelegate &featuresDelegate ); 131 virtual bool setupPass(SceneRenderState *, const SceneData& sgData, U32 pass); 132 virtual void setTextureStages(SceneRenderState *, const SceneData &sgData, U32 pass ); 133 virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass); 134 virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass); 135 virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass); 136 virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer); 137 virtual bool stepInstance(); 138 virtual void dumpMaterialInfo(); 139 virtual MaterialParameters* allocMaterialParameters(); 140 virtual MaterialParameters* getDefaultMaterialParameters() { return mDefaultParameters; } 141 virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name); 142 virtual U32 getNumStages(); 143 144protected: 145 146 Vector<GFXShaderConstDesc> mShaderConstDesc; 147 MaterialParameters* mDefaultParameters; 148 Vector<ShaderMaterialParameterHandle*> mParameterHandles; 149 150 /// Hold the instancing state data for the material. 151 class InstancingState 152 { 153 const static U32 COUNT = 200; 154 155 public: 156 157 InstancingState() 158 : mInstFormat( NULL ), 159 mBuffer( NULL ), 160 mCount( -1 ) 161 { 162 } 163 164 ~InstancingState() 165 { 166 delete [] mBuffer; 167 } 168 169 void setFormat( const GFXVertexFormat *instFormat, const GFXVertexFormat *vertexFormat ) 170 { 171 mInstFormat = instFormat; 172 mDeclFormat.copy( *vertexFormat ); 173 mDeclFormat.append( *mInstFormat, 1 ); 174 // Let the declaration know we have instancing. 175 mDeclFormat.enableInstancing(); 176 mDeclFormat.getDecl(); 177 178 delete [] mBuffer; 179 mBuffer = new U8[ mInstFormat->getSizeInBytes() * COUNT ]; 180 mCount = -1; 181 } 182 183 bool step( U8 **outPtr ) 184 { 185 // Are we starting a new draw call? 186 if ( mCount < 0 ) 187 { 188 *outPtr = mBuffer; 189 mCount = 0; 190 } 191 else 192 { 193 // Increment to the next instance. 194 *outPtr += mInstFormat->getSizeInBytes(); 195 mCount++; 196 } 197 198 return mCount < COUNT; 199 } 200 201 void resetStep() { mCount = -1; } 202 203 U8* getBuffer() const { return mBuffer; } 204 205 S32 getCount() const { return mCount; } 206 207 const GFXVertexFormat* getFormat() const { return mInstFormat; } 208 209 const GFXVertexFormat* getDeclFormat() const { return &mDeclFormat; } 210 211 protected: 212 213 GFXVertexFormat mDeclFormat; 214 const GFXVertexFormat *mInstFormat; 215 U8 *mBuffer; 216 S32 mCount; 217 218 }; 219 220 /// The instancing state if this material 221 /// supports instancing. 222 InstancingState *mInstancingState; 223 224 /// @name Internal functions 225 /// 226 /// @{ 227 228 /// Adds a pass for the given stage. 229 virtual bool _addPass( ShaderRenderPassData &rpd, 230 U32 &texIndex, 231 MaterialFeatureData &fd, 232 U32 stageNum, 233 const FeatureSet &features); 234 235 /// Chooses a blend op for the given pass 236 virtual void _setPassBlendOp( ShaderFeature *sf, 237 ShaderRenderPassData &passData, 238 U32 &texIndex, 239 MaterialFeatureData &stageFeatures, 240 U32 stageNum, 241 const FeatureSet &features); 242 243 /// Creates passes for the given stage 244 virtual bool _createPasses( MaterialFeatureData &fd, U32 stageNum, const FeatureSet &features ); 245 246 /// Fills in the MaterialFeatureData for the given stage 247 virtual void _determineFeatures( U32 stageNum, 248 MaterialFeatureData &fd, 249 const FeatureSet &features ); 250 251 /// Do we have a cubemap on pass? 252 virtual bool _hasCubemap(U32 pass); 253 254 /// Used by setTextureTransforms 255 F32 _getWaveOffset( U32 stage ); 256 257 /// Sets texture transformation matrices for texture animations such as scale and wave 258 virtual void _setTextureTransforms(const U32 pass); 259 260 /// Sets all of the necessary shader constants for the given pass 261 virtual void _setShaderConstants(SceneRenderState *, const SceneData &sgData, U32 pass); 262 263 /// @} 264 265 void _setPrimaryLightConst(const LightInfo* light, const MatrixF& objTrans, const U32 stageNum); 266 267 /// This is here to deal with the differences between ProcessedCustomMaterials and ProcessedShaderMaterials. 268 virtual GFXShaderConstBuffer* _getShaderConstBuffer(const U32 pass); 269 virtual ShaderConstHandles* _getShaderConstHandles(const U32 pass); 270 271 /// 272 virtual void _initMaterialParameters(); 273 274 ShaderRenderPassData* _getRPD(const U32 pass) { return static_cast<ShaderRenderPassData*>(mPasses[pass]); } 275}; 276 277#endif // _MATERIALS_PROCESSEDSHADERMATERIAL_H_ 278
