gfxStateBlock.h 6.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------

#ifndef _GFXSTATEBLOCK_H_
#define _GFXSTATEBLOCK_H_

#ifndef _REFBASE_H_
#include "core/util/refBase.h"
#endif
#ifndef _GFXENUMS_H_
#include "gfx/gfxEnums.h"
#endif
#ifndef _GFXRESOURCE_H_
#include "gfx/gfxResource.h"
#endif
#ifndef _COLOR_H_
#include "core/color.h"
#endif


struct GFXSamplerStateDesc
{
   GFXTextureAddressMode addressModeU;
   GFXTextureAddressMode addressModeV;
   GFXTextureAddressMode addressModeW;

   GFXTextureFilterType magFilter;
   GFXTextureFilterType minFilter;
   GFXTextureFilterType mipFilter;

   /// The maximum anisotropy used when one of the filter types
   /// is set to anisotropic.
   ///
   /// Defaults to 1.
   ///
   /// @see GFXTextureFilterType
   U32 maxAnisotropy;

   /// Used to offset the mipmap selection by whole or 
   /// fractional amounts either postively or negatively.
   ///
   /// Defaults to zero.
   F32 mipLODBias;

   GFXTextureOp textureColorOp;

   GFXTextureOp alphaOp;
   GFXTextureArgument alphaArg1;
   GFXTextureArgument alphaArg2;
   GFXTextureArgument alphaArg3;

   GFXTextureArgument colorArg1;
   GFXTextureArgument colorArg2;
   GFXTextureArgument colorArg3;

   GFXTextureArgument resultArg;

   GFXTextureTransformFlags textureTransform;

   GFXSamplerStateDesc();

   /// Returns an modulate, wrap, and linear sampled state.
   static GFXSamplerStateDesc getWrapLinear();

   /// Returns an modulate, wrap, and point sampled state.
   static GFXSamplerStateDesc getWrapPoint();

   /// Returns an modulate, clamp, and linear sampled state.
   static GFXSamplerStateDesc getClampLinear();

   /// Returns an modulate, clamp, and point sampled state.
   static GFXSamplerStateDesc getClampPoint();
};

/// GFXStateBlockDesc defines a render state, which is then used to create a GFXStateBlock instance.  
struct GFXStateBlockDesc
{   
   // Blending   
   bool blendDefined;
   bool blendEnable;
   GFXBlend blendSrc;
   GFXBlend blendDest;
   GFXBlendOp blendOp;

   /// @name Separate Alpha Blending
   /// @{
   bool separateAlphaBlendDefined;
   bool separateAlphaBlendEnable;
   GFXBlend separateAlphaBlendSrc;
   GFXBlend separateAlphaBlendDest;
   GFXBlendOp separateAlphaBlendOp;
   /// @}

   // Alpha test
   bool alphaDefined;
   bool alphaTestEnable;   
   S32 alphaTestRef;
   GFXCmpFunc alphaTestFunc;

   // Color Writes
   bool colorWriteDefined;
   bool colorWriteRed;
   bool colorWriteBlue;
   bool colorWriteGreen;
   bool colorWriteAlpha;

   // Rasterizer
   bool cullDefined;
   GFXCullMode cullMode;

   // Depth
   bool zDefined;
   bool zEnable;
   bool zWriteEnable;
   GFXCmpFunc zFunc;
   F32 zBias;
   F32 zSlopeBias;

   // Stencil
   bool stencilDefined;
   bool stencilEnable;
   GFXStencilOp stencilFailOp;
   GFXStencilOp stencilZFailOp;
   GFXStencilOp stencilPassOp;
   GFXCmpFunc  stencilFunc;
   U32 stencilRef;
   U32 stencilMask;
   U32 stencilWriteMask;

   // FF lighting
   bool ffLighting;

   bool vertexColorEnable;

   GFXFillMode fillMode;

   // Sampler states
   bool samplersDefined;
   GFXSamplerStateDesc samplers[TEXTURE_STAGE_COUNT];
   ColorI textureFactor;

   GFXStateBlockDesc();

   /// Returns the hash value of this state description
   U32 getHashValue() const;

   /// Adds data from desc to this description, uses *defined parameters in desc to figure out
   /// what blocks of state to actually copy from desc.
   void addDesc( const GFXStateBlockDesc& desc );

   /// Returns a string that describes the options set (used by GFXStateBlock::describeSelf)
   const String describeSelf() const;

   /// Utility functions to make setting up stateblock descriptions less wordy.
   void setCullMode( GFXCullMode m ); 

   /// Helpers for setting the fill modes.
   void setFillModePoint() { fillMode = GFXFillPoint; }
   void setFillModeWireframe() { fillMode = GFXFillWireframe; }
   void setFillModeSolid() { fillMode = GFXFillSolid; }

   void setZReadWrite( bool read, bool write = true ); 

   void setAlphaTest(   bool enable, 
                        GFXCmpFunc func = GFXCmpGreaterEqual, 
                        S32 alphaRef = 0 );

   void setBlend( bool enable, 
                  GFXBlend src = GFXBlendSrcAlpha, 
                  GFXBlend dest = GFXBlendInvSrcAlpha,
                  GFXBlendOp op = GFXBlendOpAdd );

   void setSeparateAlphaBlend(   bool enable, 
                                 GFXBlend src = GFXBlendOne, 
                                 GFXBlend dest = GFXBlendZero,
                                 GFXBlendOp op = GFXBlendOpAdd );


   ///
   void setColorWrites( bool red, bool green, bool blue, bool alpha );
};

class GFXStateBlock : public StrongRefBase, public GFXResource
{
public:
   virtual ~GFXStateBlock() { }

   /// Returns the hash value of the desc that created this block
   virtual U32 getHashValue() const = 0;

   /// Returns a GFXStateBlockDesc that this block represents
   virtual const GFXStateBlockDesc& getDesc() const = 0;

   /// Default implementation for GFXResource::describeSelf   
   virtual const String describeSelf() const;
};

typedef StrongRefPtr<GFXStateBlock> GFXStateBlockRef;

#endif