baseMatInstance.h
Engine/source/materials/baseMatInstance.h
Classes:
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#ifndef _BASEMATINSTANCE_H_ 24#define _BASEMATINSTANCE_H_ 25 26#ifndef _TSIGNAL_H_ 27#include "core/util/tSignal.h" 28#endif 29#ifndef _BASEMATERIALDEFINITION_H_ 30#include "materials/baseMaterialDefinition.h" 31#endif 32#ifndef _MATERIALPARAMETERS_H_ 33#include "materials/materialParameters.h" 34#endif 35#ifndef _MMATRIX_H_ 36#include "math/mMatrix.h" 37#endif 38#ifndef _GFXENUMS_H_ 39#include "gfx/gfxEnums.h" 40#endif 41#ifndef _GFXSHADER_H_ 42#include "gfx/gfxShader.h" 43#endif 44#ifndef _MATERIALFEATUREDATA_H_ 45#include "materials/materialFeatureData.h" 46#endif 47#ifndef _MATINSTANCEHOOK_H_ 48#include "materials/matInstanceHook.h" 49#endif 50#ifndef _MATSTATEHINT_H_ 51#include "materials/matStateHint.h" 52#endif 53 54struct RenderPassData; 55class GFXVertexBufferHandleBase; 56class GFXPrimitiveBufferHandle; 57struct SceneData; 58class SceneRenderState; 59struct GFXStateBlockDesc; 60class GFXVertexFormat; 61class MatrixSet; 62class ProcessedMaterial; 63 64 65/// 66class BaseMatInstance 67{ 68protected: 69 70 /// The array of active material hooks indexed 71 /// by a MatInstanceHookType. 72 Vector<MatInstanceHook*> mHooks; 73 74 /// 75 MatFeaturesDelegate mFeaturesDelegate; 76 77 /// Should be true if init has been called and it succeeded. 78 /// It is up to the derived class to set this variable appropriately. 79 bool mIsValid; 80 81 /// This is set by initialization and used by the prepass. 82 bool mHasNormalMaps; 83 84public: 85 86 virtual ~BaseMatInstance(); 87 88 /// @param features The features you want to allow for this material. 89 /// 90 /// @param vertexFormat The vertex format on which this material will be rendered. 91 /// 92 /// @see GFXVertexFormat 93 /// @see FeatureSet 94 virtual bool init( const FeatureSet &features, 95 const GFXVertexFormat *vertexFormat ) = 0; 96 97 /// Reinitializes the material using the previous 98 /// initialization parameters. 99 /// @see init 100 virtual bool reInit() = 0; 101 102 /// Returns true if init has been successfully called. 103 /// It is up to the derived class to set this value properly. 104 bool isValid() { return mIsValid; } 105 106 /// Adds this stateblock to the base state block 107 /// used during initialization. 108 /// @see init 109 virtual void addStateBlockDesc(const GFXStateBlockDesc& desc) = 0; 110 111 /// Updates the state blocks for this material. 112 virtual void updateStateBlocks() = 0; 113 114 /// Adds a shader macro which will be passed to the shader 115 /// during initialization. 116 /// @see init 117 virtual void addShaderMacro( const String &name, const String &value ) = 0; 118 119 /// Get a MaterialParameters block for this BaseMatInstance, 120 /// caller is responsible for freeing it. 121 virtual MaterialParameters* allocMaterialParameters() = 0; 122 123 /// Set the current parameters for this BaseMatInstance 124 virtual void setMaterialParameters(MaterialParameters* param) = 0; 125 126 /// Get the current parameters for this BaseMatInstance (BaseMatInstances are created with a default active 127 /// MaterialParameters which is managed by BaseMatInstance. 128 virtual MaterialParameters* getMaterialParameters() = 0; 129 130 /// Returns a MaterialParameterHandle for name. 131 virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name) = 0; 132 133 /// Sets up the next rendering pass for this material. It is 134 /// typically called like so... 135 /// 136 ///@code 137 /// while( mat->setupPass( state, sgData ) ) 138 /// { 139 /// mat->setTransforms(...); 140 /// mat->setSceneInfo(...); 141 /// ... 142 /// GFX->drawPrimitive(); 143 /// } 144 ///@endcode 145 /// 146 virtual bool setupPass( SceneRenderState *state, const SceneData &sgData ) = 0; 147 148 /// This initializes the material transforms and should be 149 /// called after setupPass() within the pass loop. 150 /// @see setupPass 151 virtual void setTransforms( const MatrixSet &matrixSet, SceneRenderState *state ) = 0; 152 153 /// This initializes various material scene state settings and 154 /// should be called after setupPass() within the pass loop. 155 /// @see setupPass 156 virtual void setSceneInfo( SceneRenderState *state, const SceneData &sgData ) = 0; 157 158 /// This is normally called from within setupPass() automatically, so its 159 /// unnecessary to do so manually unless a texture stage has changed. If 160 /// so it should be called after setupPass() within the pass loop. 161 /// @see setupPass 162 virtual void setTextureStages(SceneRenderState *, const SceneData &sgData ) = 0; 163 164 /// Sets the vertex and primitive buffers as well as the instancing 165 /// stream buffer for the current material if the material is instanced. 166 virtual void setBuffers( GFXVertexBufferHandleBase *vertBuffer, GFXPrimitiveBufferHandle *primBuffer ) = 0; 167 168 /// Returns true if this material is instanced. 169 virtual bool isInstanced() const = 0; 170 171 /// Used to increment the instance buffer for this material. 172 virtual bool stepInstance() = 0; 173 174 /// Returns true if the material is forward lit and requires 175 /// a list of lights which affect it when rendering. 176 virtual bool isForwardLit() const = 0; 177 178 /// Sets a SimObject which will passed into ShaderFeature::createConstHandles. 179 /// Normal features do not make use of this, it is for special class specific 180 /// or user designed features. 181 virtual void setUserObject( SimObject *userObject ) = 0; 182 virtual SimObject* getUserObject() const = 0; 183 184 /// Returns the material this instance is based on. 185 virtual BaseMaterialDefinition* getMaterial() = 0; 186 187 // BTRTODO: This stuff below should probably not be in BaseMatInstance 188 virtual bool hasGlow() = 0; 189 virtual bool hasAccumulation() = 0; 190 191 virtual U32 getCurPass() = 0; 192 193 virtual U32 getCurStageNum() = 0; 194 195 virtual RenderPassData *getPass(U32 pass) = 0; 196 197 /// Returns the state hint which can be used for 198 /// sorting and fast comparisions of the equality 199 /// of a material instance. 200 virtual const MatStateHint& getStateHint() const = 0; 201 202 /// Returns the active features in use by this material. 203 /// @see getRequestedFeatures 204 virtual const FeatureSet& getFeatures() const = 0; 205 206 /// Returns the features that were requested at material 207 /// creation time which may differ from the active features. 208 /// @see getFeatures 209 virtual const FeatureSet& getRequestedFeatures() const = 0; 210 211 virtual const GFXVertexFormat* getVertexFormat() const = 0; 212 213 virtual void dumpShaderInfo() const = 0; 214 215 /// Fast test for use of normal maps in this material. 216 bool hasNormalMap() const { return mHasNormalMaps; } 217 218 /// 219 MatFeaturesDelegate& getFeaturesDelegate() { return mFeaturesDelegate; } 220 221 /// Returns true if this MatInstance is built from a CustomMaterial. 222 virtual bool isCustomMaterial() const = 0; 223 224 /// @name Material Hook functions 225 /// @{ 226 227 /// 228 void addHook( MatInstanceHook *hook ); 229 230 /// Helper function for getting a hook. 231 /// @see getHook 232 template <class HOOK> 233 inline HOOK* getHook() { return (HOOK*)getHook( HOOK::Type ); } 234 235 /// 236 MatInstanceHook* getHook( const MatInstanceHookType &type ) const; 237 238 /// 239 void deleteHook( const MatInstanceHookType &type ); 240 241 /// 242 U32 deleteAllHooks(); 243 244 /// @} 245 246 virtual const GFXStateBlockDesc &getUserStateBlock() const = 0; 247 248}; 249 250#endif /// _BASEMATINSTANCE_H_ 251 252 253 254 255 256 257
