CmMaterial.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmResource.h"
  4. #include "CmVector2.h"
  5. #include "CmVector3.h"
  6. #include "CmVector4.h"
  7. #include "CmMatrix3.h"
  8. #include "CmMatrix4.h"
  9. #include "CmCommonEnums.h"
  10. namespace CamelotEngine
  11. {
  12. class CM_EXPORT PassParameters
  13. {
  14. public:
  15. GpuParamsPtr mVertParams;
  16. GpuParamsPtr mFragParams;
  17. GpuParamsPtr mGeomParams;
  18. GpuParamsPtr mHullParams;
  19. GpuParamsPtr mDomainParams;
  20. GpuParamsPtr mComputeParams;
  21. // Helper method
  22. GpuParamsPtr& getParamByIdx(UINT32 idx)
  23. {
  24. GpuParamsPtr* paramArray[] = {&mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams};
  25. return *paramArray[idx];
  26. }
  27. // Helper method
  28. UINT32 getNumParams() const { return 6; }
  29. };
  30. class CM_EXPORT Material : public Resource
  31. {
  32. public:
  33. struct StructData
  34. {
  35. StructData()
  36. :size(0), data(nullptr)
  37. { }
  38. StructData(void* _data, UINT32 _size)
  39. :size(_size)
  40. {
  41. data = std::shared_ptr<void>(CM_NEW_BYTES(_size, ScratchAlloc), &MemAllocBytesDeleter<ScratchAlloc>::deleter);
  42. memcpy(data.get(), _data, size);
  43. }
  44. std::shared_ptr<void> data;
  45. UINT32 size;
  46. };
  47. ~Material();
  48. /**
  49. * @brief Sets a shader that will be used by the material.
  50. * Shaders best technique will be retrieved and used in all subsequent Material
  51. * operations.
  52. * You need to call this method before doing any other operations with this class.
  53. * After setting the shader if change any systems that a shader technique is defendant upon (render system, renderer, ...)
  54. * you will need to call this method again on all your Materials to make sure technique used is updated.
  55. */
  56. void setShader(ShaderPtr shader);
  57. ShaderPtr getShader() const { return mShader; }
  58. void setTexture(const String& name, const HTexture& value);
  59. void setSamplerState(const String& name, HSamplerState& samplerState);
  60. void setFloat(const String& name, float value, UINT32 arrayIdx = 0);
  61. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0);
  62. void setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0);
  63. void setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0);
  64. void setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0);
  65. void setMat3(const String& name, const Matrix3& value, UINT32 arrayIdx = 0);
  66. void setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0);
  67. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0);
  68. void setParamBlock(const String& name, GpuParamBlockPtr paramBlock);
  69. HTexture getTexture(const String& name) const;
  70. HSamplerState getSamplerState(const String& name) const;
  71. float getFloat(const String& name, UINT32 arrayIdx = 0) const;
  72. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const;
  73. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const;
  74. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const;
  75. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const;
  76. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const;
  77. const StructData& getStructData(const String& name, UINT32 arrayIdx = 0) const;
  78. UINT32 getNumPasses() const;
  79. PassPtr getPass(UINT32 passIdx) const;
  80. PassParametersPtr getPassParameters(UINT32 passIdx) const;
  81. static HMaterial create();
  82. static HMaterial create(ShaderPtr shader);
  83. private:
  84. friend class MaterialManager;
  85. ShaderPtr mShader;
  86. TechniquePtr mBestTechnique;
  87. set<String>::type mValidShareableParamBlocks;
  88. map<String, String>::type mValidParams; // Also maps Shader param name -> gpu variable name
  89. vector<PassParametersPtr>::type mParametersPerPass;
  90. // These maps aren't necessary as we can read these values from the GpuParams directly
  91. // but they make many things (especially serializing and getting values) so much easier
  92. map<String, vector<float>::type>::type mFloatValues;
  93. map<String, vector<Vector2>::type>::type mVec2Values;
  94. map<String, vector<Vector3>::type>::type mVec3Values;
  95. map<String, vector<Vector4>::type>::type mVec4Values;
  96. map<String, vector<Matrix3>::type>::type mMat3Values;
  97. map<String, vector<Matrix4>::type>::type mMat4Values;
  98. map<String, vector<StructData>::type>::type mStructValues;
  99. map<String, HTexture>::type mTextureValues;
  100. map<String, HSamplerState>::type mSamplerValues;
  101. Material();
  102. void throwIfNotInitialized() const;
  103. template <typename T>
  104. void setParam(const String& name, T& value, UINT32 arrayIdx)
  105. {
  106. for(auto iter = mParametersPerPass.begin(); iter != mParametersPerPass.end(); ++iter)
  107. {
  108. PassParametersPtr params = *iter;
  109. for(UINT32 i = 0; i < params->getNumParams(); i++)
  110. {
  111. GpuParamsPtr& paramPtr = params->getParamByIdx(i);
  112. if(paramPtr)
  113. {
  114. if(paramPtr->hasParam(name))
  115. paramPtr->setParam(name, value, arrayIdx);
  116. }
  117. }
  118. }
  119. }
  120. const map<String, String>::type& getValidParamNames() const { return mValidParams; }
  121. void initBestTechnique();
  122. map<String, const GpuParamDataDesc*>::type determineValidDataParameters(const vector<const GpuParamDesc*>::type& paramDescs) const;
  123. set<String>::type determineValidObjectParameters(const vector<const GpuParamDesc*>::type& paramDescs) const;
  124. set<String>::type determineValidShareableParamBlocks(const vector<const GpuParamDesc*>::type& paramDescs) const;
  125. map<String, String>::type determineParameterToBlockMapping(const vector<const GpuParamDesc*>::type& paramDescs);
  126. bool areParamsEqual(const GpuParamDataDesc& paramA, const GpuParamDataDesc& paramB, bool ignoreBufferOffsets = false) const;
  127. /************************************************************************/
  128. /* RTTI */
  129. /************************************************************************/
  130. public:
  131. friend class MaterialRTTI;
  132. static RTTITypeBase* getRTTIStatic();
  133. virtual RTTITypeBase* getRTTI() const;
  134. };
  135. }