processedMaterial.h
Engine/source/materials/processedMaterial.h
Classes:
class
This is an abstract base class which provides the external interface all subclasses must implement.
class
This contains the common data needed to render a pass.
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_PROCESSEDMATERIAL_H_ 25#define _MATERIALS_PROCESSEDMATERIAL_H_ 26 27#ifndef _MATERIALDEFINITION_H_ 28#include "materials/materialDefinition.h" 29#endif 30#ifndef _MATERIALFEATUREDATA_H_ 31#include "materials/materialFeatureData.h" 32#endif 33#ifndef _GFXSTATEBLOCK_H_ 34#include "gfx/gfxStateBlock.h" 35#endif 36#ifndef _MATTEXTURETARGET_H_ 37#include "materials/matTextureTarget.h" 38#endif 39#ifndef _MATSTATEHINT_H_ 40#include "materials/matStateHint.h" 41#endif 42 43class ShaderFeature; 44class MaterialParameters; 45class MaterialParameterHandle; 46class SceneRenderState; 47class GFXVertexBufferHandleBase; 48class GFXPrimitiveBufferHandle; 49class MatrixSet; 50 51 52/// This contains the common data needed to render a pass. 53struct RenderPassData 54{ 55public: 56 57 struct TexSlotT 58 { 59 /// This is the default type of texture which 60 /// is valid with most texture types. 61 /// @see mTexType 62 GFXTexHandle texObject; 63 64 /// Only valid when the texture type is set 65 /// to Material::TexTarget. 66 /// @see mTexType 67 NamedTexTargetRef texTarget; 68 69 } mTexSlot[Material::MAX_TEX_PER_PASS]; 70 71 U32 mTexType[Material::MAX_TEX_PER_PASS]; 72 String mSamplerNames[Material::MAX_TEX_PER_PASS]; 73 74 /// The cubemap to use when the texture type is 75 /// set to Material::Cube. 76 /// @see mTexType 77 GFXCubemapHandle mCubeMap; 78 79 U32 mNumTex; 80 81 U32 mNumTexReg; 82 83 MaterialFeatureData mFeatureData; 84 85 bool mGlow; 86 87 Material::BlendOp mBlendOp; 88 89 U32 mStageNum; 90 91 /// State permutations, used to index into 92 /// the render states array. 93 /// @see mRenderStates 94 enum 95 { 96 STATE_REFLECT = 1, 97 STATE_TRANSLUCENT = 2, 98 STATE_GLOW = 4, 99 STATE_WIREFRAME = 8, 100 STATE_MAX = 16 101 }; 102 103 /// 104 GFXStateBlockRef mRenderStates[STATE_MAX]; 105 106 RenderPassData(); 107 108 virtual ~RenderPassData() { reset(); } 109 110 virtual void reset(); 111 112 /// Creates and returns a unique description string. 113 virtual String describeSelf() const; 114}; 115 116/// This is an abstract base class which provides the external 117/// interface all subclasses must implement. This interface 118/// primarily consists of setting state. Pass creation 119/// is implementation specific, and internal, thus it is 120/// not in this base class. 121class ProcessedMaterial 122{ 123public: 124 ProcessedMaterial(); 125 virtual ~ProcessedMaterial(); 126 127 /// @name State setting functions 128 /// 129 /// @{ 130 131 /// 132 virtual void addStateBlockDesc(const GFXStateBlockDesc& sb); 133 134 /// 135 virtual void updateStateBlocks() { _initRenderPassDataStateBlocks(); } 136 137 /// Set the user defined shader macros. 138 virtual void setShaderMacros( const Vector<GFXShaderMacro> ¯os ) { mUserMacros = macros; } 139 140 /// Sets the textures needed for rendering the current pass 141 virtual void setTextureStages(SceneRenderState *, const SceneData &sgData, U32 pass ) = 0; 142 143 /// Sets the transformation matrix, i.e. Model * View * Projection 144 virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass) = 0; 145 146 /// Sets the node transforms for HW Skinning 147 virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass) = 0; 148 149 /// Sets the scene info like lights for the given pass. 150 virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass) = 0; 151 152 /// Sets the given vertex and primitive buffers so we can render geometry 153 virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer); 154 155 /// @see BaseMatInstance::setUserObject 156 virtual void setUserObject( SimObject *userObject ) { mUserObject = userObject; } 157 158 /// 159 virtual bool stepInstance(); 160 161 /// @} 162 163 /// Initializes us (eg. loads textures, creates passes, generates shaders) 164 virtual bool init( const FeatureSet& features, 165 const GFXVertexFormat *vertexFormat, 166 const MatFeaturesDelegate &featuresDelegate ) = 0; 167 168 /// Returns the state hint which can be used for 169 /// sorting and fast comparisions of the equality 170 /// of a material instance. 171 virtual const MatStateHint& getStateHint() const { return mStateHint; } 172 173 /// Sets up the given pass. Returns true if the pass was set up, false if there was an error or if 174 /// the specified pass is out of bounds. 175 virtual bool setupPass(SceneRenderState *, const SceneData& sgData, U32 pass) = 0; 176 177 // Material parameter methods 178 virtual MaterialParameters* allocMaterialParameters() = 0; 179 virtual MaterialParameters* getDefaultMaterialParameters() = 0; 180 virtual void setMaterialParameters(MaterialParameters* param, S32 pass) { mCurrentParams = param; }; 181 virtual MaterialParameters* getMaterialParameters() { return mCurrentParams; } 182 virtual MaterialParameterHandle* getMaterialParameterHandle(const String& name) = 0; 183 184 /// Returns the pass data for the given pass. 185 RenderPassData* getPass(U32 pass) 186 { 187 if(pass >= mPasses.size()) 188 return NULL; 189 return mPasses[pass]; 190 } 191 192 /// Returns the pass data for the given pass. 193 const RenderPassData* getPass( U32 pass ) const { return mPasses[pass]; } 194 195 /// Returns the number of stages we're rendering (not to be confused with the number of passes). 196 virtual U32 getNumStages() = 0; 197 198 /// Returns the number of passes we are rendering (not to be confused with the number of stages). 199 U32 getNumPasses() const { return mPasses.size(); } 200 201 /// Returns true if any pass glows 202 bool hasGlow() const { return mHasGlow; } 203 204 /// Returns true if any pass accumulates 205 bool hasAccumulation() const { return mHasAccumulation; } 206 207 /// Gets the stage number for a pass 208 U32 getStageFromPass(U32 pass) const 209 { 210 if(pass >= mPasses.size()) 211 return 0; 212 return mPasses[pass]->mStageNum; 213 } 214 215 /// Returns the active features in use by this material. 216 /// @see BaseMatInstance::getFeatures 217 const FeatureSet& getFeatures() const { return mFeatures; } 218 219 /// Dump shader info, or FF texture info? 220 virtual void dumpMaterialInfo() { } 221 222 /// Returns the source material. 223 Material* getMaterial() const { return mMaterial; } 224 225 /// Returns the texture used by a stage 226 GFXTexHandle getStageTexture(U32 stage, const FeatureType &type) 227 { 228 return (stage < Material::MAX_STAGES) ? mStages[stage].getTex(type) : NULL; 229 } 230 231protected: 232 233 /// Our passes. 234 Vector<RenderPassData*> mPasses; 235 236 /// The active features in use by this material. 237 FeatureSet mFeatures; 238 239 /// The material which we are processing. 240 Material* mMaterial; 241 242 MaterialParameters* mCurrentParams; 243 244 /// Material::StageData is used here because the shader 245 /// generator throws a fit if it's passed anything else. 246 Material::StageData mStages[Material::MAX_STAGES]; 247 248 /// If we've already loaded the stage data 249 bool mHasSetStageData; 250 251 /// If we glow 252 bool mHasGlow; 253 254 /// If we have accumulation. 255 bool mHasAccumulation; 256 257 /// Number of stages (not to be confused with number of passes) 258 U32 mMaxStages; 259 260 /// The vertex format on which this material will render. 261 const GFXVertexFormat *mVertexFormat; 262 263 /// Set by addStateBlockDesc, should be considered 264 /// when initPassStateBlock is called. 265 GFXStateBlockDesc mUserDefined; 266 267 /// The user defined macros to pass to the 268 /// shader initialization. 269 Vector<GFXShaderMacro> mUserMacros; 270 271 /// The user defined object to pass to ShaderFeature::createConstHandles. 272 SimObject *mUserObject; 273 274 /// The state hint used for material sorting 275 /// and quick equality comparision. 276 MatStateHint mStateHint; 277 278 /// Loads all the textures for all of the stages in the Material 279 virtual void _setStageData(); 280 281 /// Sets the blend state for rendering 282 void _setBlendState(Material::BlendOp blendOp, GFXStateBlockDesc& desc ); 283 284 /// Returns the path the material will attempt to load for a given texture filename. 285 String _getTexturePath(const String& filename); 286 287 /// Loads the texture located at _getTexturePath(filename) and gives it the specified profile 288 GFXTexHandle _createTexture( const char *filename, GFXTextureProfile *profile ); 289 290 /// @name State blocks 291 /// 292 /// @{ 293 294 /// Creates the default state block templates, used by initStateBlocks. 295 virtual void _initStateBlockTemplates(GFXStateBlockDesc& stateTranslucent, GFXStateBlockDesc& stateGlow, GFXStateBlockDesc& stateReflect); 296 297 /// Does the base render state block setting, normally per pass. 298 virtual void _initPassStateBlock( RenderPassData *rpd, GFXStateBlockDesc& result); 299 300 /// Creates the default state blocks for a list of render states. 301 virtual void _initRenderStateStateBlocks( RenderPassData *rpd ); 302 303 /// Creates the default state blocks for each RenderPassData item. 304 virtual void _initRenderPassDataStateBlocks(); 305 306 /// This returns the index into the renderState array based on the sgData passed in. 307 virtual U32 _getRenderStateIndex( const SceneRenderState *state, 308 const SceneData &sgData ); 309 310 /// Activates the correct mPasses[currPass].renderState based on scene graph info 311 virtual void _setRenderState( const SceneRenderState *state, 312 const SceneData &sgData, 313 U32 pass ); 314 /// @ 315}; 316 317#endif // _MATERIALS_PROCESSEDMATERIAL_H_ 318
