gfxStateBlock.cpp
Engine/source/gfx/gfxStateBlock.cpp
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#include "gfx/gfxStateBlock.h" 24#include "core/crc.h" 25#include "gfx/gfxDevice.h" 26#include "core/strings/stringFunctions.h" 27#include "gfx/gfxStringEnumTranslate.h" 28 29/// 30/// GFXStateBlock 31/// 32const String GFXStateBlock::describeSelf() const 33{ 34 return String::ToString("hashvalue: 0x%x", getDesc().getHashValue()); 35} 36 37/// 38/// GFXStateBlockDesc 39/// 40GFXStateBlockDesc::GFXStateBlockDesc() 41{ 42 // Alpha blending 43 blendDefined = false; 44 blendEnable = false; 45 blendSrc = GFXBlendOne; 46 blendDest = GFXBlendZero; 47 blendOp = GFXBlendOpAdd; 48 49 // Separate alpha blending 50 separateAlphaBlendDefined = false; 51 separateAlphaBlendEnable = false; 52 separateAlphaBlendSrc = GFXBlendOne; 53 separateAlphaBlendDest = GFXBlendZero; 54 separateAlphaBlendOp = GFXBlendOpAdd; 55 56 // Alpha test 57 alphaDefined = false; 58 alphaTestEnable = false; 59 alphaTestRef = 0; 60 alphaTestFunc = GFXCmpGreaterEqual; 61 62 // Color Writes 63 colorWriteDefined = false; 64 colorWriteRed = true; 65 colorWriteBlue = true; 66 colorWriteGreen = true; 67 colorWriteAlpha = true; 68 69 // Rasterizer 70 cullDefined = false; 71 cullMode = GFXCullCCW; 72 73 // Depth 74 zDefined = false; 75 zEnable = true; 76 zWriteEnable = true; 77 zFunc = GFXCmpLessEqual; 78 zBias = 0; 79 zSlopeBias = 0; 80 81 // Stencil 82 stencilDefined = false; 83 stencilEnable = false; 84 stencilFailOp = GFXStencilOpKeep; 85 stencilZFailOp = GFXStencilOpKeep; 86 stencilPassOp = GFXStencilOpKeep; 87 stencilFunc = GFXCmpNever; 88 stencilRef = 0; 89 stencilMask = 0xFFFFFFFF; 90 stencilWriteMask = 0xFFFFFFFF; 91 92 vertexColorEnable = false; 93 94 fillMode = GFXFillSolid; 95 96 samplersDefined = false; 97 textureFactor.set( 255, 255, 255, 255 ); 98} 99 100// This method just needs to return a unique value based on its contents. 101U32 GFXStateBlockDesc::getHashValue() const 102{ 103 return CRC::calculateCRC(this, sizeof(GFXStateBlockDesc)); 104} 105 106/// Adds data from desc to this description, uses *defined parameters in desc to figure out 107/// what blocks of state to actually copy from desc. 108void GFXStateBlockDesc::addDesc(const GFXStateBlockDesc& desc) 109{ 110 // Alpha blending 111 if (desc.blendDefined) 112 { 113 blendDefined = true; 114 blendEnable = desc.blendEnable; 115 blendSrc = desc.blendSrc; 116 blendDest = desc.blendDest; 117 blendOp = desc.blendOp; 118 } 119 120 // Separate alpha blending 121 if ( desc.separateAlphaBlendDefined ) 122 { 123 separateAlphaBlendDefined = true; 124 separateAlphaBlendEnable = desc.separateAlphaBlendEnable; 125 separateAlphaBlendSrc = desc.separateAlphaBlendSrc; 126 separateAlphaBlendDest = desc.separateAlphaBlendDest; 127 separateAlphaBlendOp = desc.separateAlphaBlendOp; 128 } 129 130 // Alpha test 131 if (desc.alphaDefined) 132 { 133 alphaDefined = true; 134 alphaTestEnable = desc.alphaTestEnable; 135 alphaTestRef = desc.alphaTestRef; 136 alphaTestFunc = desc.alphaTestFunc; 137 } 138 139 // Color Writes 140 if (desc.colorWriteDefined) 141 { 142 colorWriteDefined = true; 143 colorWriteRed = desc.colorWriteRed; 144 colorWriteBlue = desc.colorWriteBlue; 145 colorWriteGreen = desc.colorWriteGreen; 146 colorWriteAlpha = desc.colorWriteAlpha; 147 } 148 149 // Rasterizer 150 if (desc.cullDefined) 151 { 152 cullDefined = true; 153 cullMode = desc.cullMode; 154 } 155 156 // Depth 157 if (desc.zDefined) 158 { 159 zDefined = true; 160 zEnable = desc.zEnable; 161 zWriteEnable = desc.zWriteEnable; 162 zFunc = desc.zFunc; 163 zBias = desc.zBias; 164 zSlopeBias = desc.zSlopeBias; 165 } 166 167 // Stencil 168 if (desc.stencilDefined) 169 { 170 stencilDefined = true; 171 stencilEnable = desc.stencilEnable; 172 stencilFailOp = desc.stencilFailOp; 173 stencilZFailOp = desc.stencilZFailOp; 174 stencilPassOp = desc.stencilPassOp; 175 stencilFunc = desc.stencilFunc; 176 stencilRef = desc.stencilRef; 177 stencilMask = desc.stencilMask; 178 stencilWriteMask = desc.stencilWriteMask; 179 } 180 181 if (desc.samplersDefined) 182 { 183 samplersDefined = true; 184 for (U32 i = 0; i < GFX_TEXTURE_STAGE_COUNT; i++) 185 { 186 samplers[i] = desc.samplers[i]; 187 } 188 textureFactor = desc.textureFactor; 189 } 190 191 vertexColorEnable = desc.vertexColorEnable; 192 fillMode = desc.fillMode; 193} 194 195/// Returns a string that describes the options set (used by GFXStateBlock::describeSelf) 196const String GFXStateBlockDesc::describeSelf() const 197{ 198 GFXStringEnumTranslate::init(); 199 200 String ret; 201 ret = String::ToString(" AlphaBlend: %d, BlendSrc: %s, BlendDest: %s, BlendOp: %s\n", 202 blendEnable, GFXStringBlend[blendSrc], GFXStringBlend[blendDest], GFXStringBlendOp[blendOp]); 203 ret += String::ToString(" SeparateAlphaBlend: %d, SeparateAlphaBlendSrc: %s, SeparateAlphaBlendDest: %s, SeparateAlphaBlendOp: %s\n", 204 separateAlphaBlendEnable, GFXStringBlend[separateAlphaBlendSrc], GFXStringBlend[separateAlphaBlendDest], GFXStringBlendOp[separateAlphaBlendOp]); 205 ret += String::ToString(" AlphaTest: %d, AlphaTestFunc: %s, AlphaTestRef: %d\n", 206 alphaTestEnable, GFXStringCmpFunc[alphaTestFunc], alphaTestRef); 207 ret += String::ToString(" ColorWrites: r: %d g: %d b: %d a: %d", 208 colorWriteRed, colorWriteGreen, colorWriteBlue, colorWriteAlpha); 209 ret += String::ToString(" CullMode: %s\n", GFXStringCullMode[cullMode]); 210 ret += String::ToString(" ZEnable: %d, ZWriteEnable: %d, ZFunc: %s, ZBias: %f, ZSlopeBias: %f\n", 211 zEnable, zWriteEnable, GFXStringCmpFunc[zFunc], zBias, zSlopeBias); 212 ret += String::ToString(" Stencil: %d, StencilFailOp: %s, StencilZFailOp: %s, StencilPassOp: %s, \n stencilFunc: %s, stencilRef: %d, stencilMask: 0x%x, stencilWriteMask: 0x%x\n", 213 stencilEnable, GFXStringCmpFunc[stencilFailOp], GFXStringCmpFunc[stencilZFailOp], GFXStringCmpFunc[stencilPassOp], 214 GFXStringCmpFunc[stencilFunc], stencilRef, stencilMask, stencilWriteMask); 215 216 return ret; 217} 218 219// 220// Utility functions 221// 222 223void GFXStateBlockDesc::setCullMode( GFXCullMode m ) 224{ 225 cullDefined = true; 226 cullMode = m; 227} 228 229void GFXStateBlockDesc::setZReadWrite( bool read, bool write ) 230{ 231 zDefined = true; 232 zEnable = read; 233 zWriteEnable = write; 234} 235 236void GFXStateBlockDesc::setAlphaTest( bool enable, GFXCmpFunc func, S32 alphaRef ) 237{ 238 alphaDefined = true; 239 alphaTestEnable = enable; 240 alphaTestFunc = func; 241 alphaTestRef = alphaRef; 242} 243 244void GFXStateBlockDesc::setBlend( bool enable, GFXBlend src, GFXBlend dest, GFXBlendOp op ) 245{ 246 blendDefined = true; 247 blendEnable = enable; 248 blendSrc = src; 249 blendDest = dest; 250 blendOp = op; 251} 252 253void GFXStateBlockDesc::setSeparateAlphaBlend( bool enable, GFXBlend src, GFXBlend dest, GFXBlendOp op ) 254{ 255 separateAlphaBlendDefined = true; 256 separateAlphaBlendEnable = enable; 257 separateAlphaBlendSrc = src; 258 separateAlphaBlendDest = dest; 259 separateAlphaBlendOp = op; 260} 261 262void GFXStateBlockDesc::setColorWrites( bool red, bool green, bool blue, bool alpha ) 263{ 264 colorWriteDefined = true; 265 colorWriteRed = red; 266 colorWriteGreen = green; 267 colorWriteBlue = blue; 268 colorWriteAlpha = alpha; 269} 270 271GFXSamplerStateDesc::GFXSamplerStateDesc() 272{ 273 addressModeU = GFXAddressWrap; 274 addressModeV = GFXAddressWrap; 275 addressModeW = GFXAddressWrap; 276 magFilter = GFXTextureFilterLinear; 277 minFilter = GFXTextureFilterLinear; 278 mipFilter = GFXTextureFilterLinear; 279 samplerFunc = GFXCmpNever; 280 maxAnisotropy = 1; 281 mipLODBias = 0.0f; 282} 283 284GFXSamplerStateDesc GFXSamplerStateDesc::getWrapLinear() 285{ 286 // Linear with wrapping is already the default 287 GFXSamplerStateDesc ssd; 288 return ssd; 289} 290 291GFXSamplerStateDesc GFXSamplerStateDesc::getWrapPoint() 292{ 293 GFXSamplerStateDesc ssd; 294 ssd.magFilter = GFXTextureFilterPoint; 295 ssd.minFilter = GFXTextureFilterPoint; 296 ssd.mipFilter = GFXTextureFilterPoint; 297 return ssd; 298} 299 300GFXSamplerStateDesc GFXSamplerStateDesc::getClampLinear() 301{ 302 GFXSamplerStateDesc ssd; 303 ssd.addressModeU = GFXAddressClamp; 304 ssd.addressModeV = GFXAddressClamp; 305 ssd.addressModeW = GFXAddressClamp; 306 return ssd; 307} 308 309GFXSamplerStateDesc GFXSamplerStateDesc::getClampPoint() 310{ 311 GFXSamplerStateDesc ssd; 312 ssd.addressModeU = GFXAddressClamp; 313 ssd.addressModeV = GFXAddressClamp; 314 ssd.addressModeW = GFXAddressClamp; 315 ssd.magFilter = GFXTextureFilterPoint; 316 ssd.minFilter = GFXTextureFilterPoint; 317 ssd.mipFilter = GFXTextureFilterPoint; 318 return ssd; 319} 320