materialParameters.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MATERIALPARAMETERS_H_
  23. #define _MATERIALPARAMETERS_H_
  24. #ifndef _GFXSHADER_H_
  25. #include "gfx/gfxShader.h"
  26. #endif
  27. ///
  28. /// Similar class to GFXShaderConsts, but this is to describe generic material parameters.
  29. ///
  30. class MaterialParameterHandle
  31. {
  32. public:
  33. virtual ~MaterialParameterHandle() {}
  34. virtual const String& getName() const = 0;
  35. // This is similar to GFXShaderConstHandle, but a Material will always return a handle when
  36. // asked for one. Even if it doesn't have that material parameter. This is because after a
  37. // reInitMaterials call, the constants can change underneath the MatInstance.
  38. // Note: GFXShaderConstHandle actually works this way too, now.
  39. virtual bool isValid() const = 0;
  40. /// Returns -1 if this handle does not point to a Sampler.
  41. virtual S32 getSamplerRegister( U32 pass ) const = 0;
  42. };
  43. class MaterialParameters
  44. {
  45. public:
  46. MaterialParameters()
  47. {
  48. VECTOR_SET_ASSOCIATION( mShaderConstDesc );
  49. }
  50. virtual ~MaterialParameters() {}
  51. /// Returns our list of shader constants, the material can get this and just set the constants it knows about
  52. virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const { return mShaderConstDesc; }
  53. /// An inline helper which ensures the handle is valid
  54. /// before the virtual set method is called.
  55. template< typename VALUE >
  56. inline void setSafe( MaterialParameterHandle *handle, const VALUE& v )
  57. {
  58. if ( handle->isValid() )
  59. set( handle, v );
  60. }
  61. /// @name Set shader constant values
  62. /// @{
  63. /// Actually set shader constant values
  64. /// @param name Name of the constant, this should be a name contained in the array returned in getShaderConstDesc,
  65. /// if an invalid name is used, it is ignored.
  66. virtual void set(MaterialParameterHandle* handle, const F32 f) {}
  67. virtual void set(MaterialParameterHandle* handle, const Point2F& fv) {}
  68. virtual void set(MaterialParameterHandle* handle, const Point3F& fv) {}
  69. virtual void set(MaterialParameterHandle* handle, const Point4F& fv) {}
  70. virtual void set(MaterialParameterHandle* handle, const LinearColorF& fv) {}
  71. virtual void set(MaterialParameterHandle* handle, const S32 f) {}
  72. virtual void set(MaterialParameterHandle* handle, const Point2I& fv) {}
  73. virtual void set(MaterialParameterHandle* handle, const Point3I& fv) {}
  74. virtual void set(MaterialParameterHandle* handle, const Point4I& fv) {}
  75. virtual void set(MaterialParameterHandle* handle, const AlignedArray<F32>& fv) {}
  76. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2F>& fv) {}
  77. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3F>& fv) {}
  78. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4F>& fv) {}
  79. virtual void set(MaterialParameterHandle* handle, const AlignedArray<S32>& fv) {}
  80. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point2I>& fv) {}
  81. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point3I>& fv) {}
  82. virtual void set(MaterialParameterHandle* handle, const AlignedArray<Point4I>& fv) {}
  83. virtual void set(MaterialParameterHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType = GFXSCT_Float4x4) {}
  84. virtual void set(MaterialParameterHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType = GFXSCT_Float4x4) {}
  85. /// Returns the alignment value for the constType in bytes.
  86. virtual U32 getAlignmentValue(const GFXShaderConstType constType) { return 0; }
  87. protected:
  88. Vector<GFXShaderConstDesc> mShaderConstDesc;
  89. };
  90. #endif