CmMaterial.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 CamelotFramework
  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_alloc<ScratchAlloc>(_size), &cm_free<ScratchAlloc>);
  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, const 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 setRenderQueue(INT16 renderQueue) { mRenderQueue = renderQueue; }
  69. //void setParamBlock(const String& name, GpuParamBlockPtr paramBlock);
  70. HTexture getTexture(const String& name) const;
  71. HSamplerState getSamplerState(const String& name) const;
  72. float getFloat(const String& name, UINT32 arrayIdx = 0) const;
  73. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const;
  74. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const;
  75. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const;
  76. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const;
  77. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const;
  78. const StructData& getStructData(const String& name, UINT32 arrayIdx = 0) const;
  79. INT16 getRenderQueue() const { return mRenderQueue; }
  80. UINT32 getNumPasses() const;
  81. PassPtr getPass(UINT32 passIdx) const;
  82. PassParametersPtr getPassParameters(UINT32 passIdx) const;
  83. static HMaterial create();
  84. static HMaterial create(ShaderPtr shader);
  85. protected:
  86. void destroy_internal();
  87. private:
  88. friend class MaterialManager;
  89. ShaderPtr mShader;
  90. TechniquePtr mBestTechnique;
  91. INT16 mRenderQueue;
  92. Set<String>::type mValidShareableParamBlocks;
  93. Map<String, String>::type mValidParams; // Also maps Shader param name -> gpu variable name
  94. Vector<PassParametersPtr>::type mParametersPerPass;
  95. Vector<GpuParamBlockBufferPtr>::type mParamBuffers;
  96. // These maps aren't necessary as we can read these values from the GpuParams directly
  97. // but they make many things (especially serializing and getting values) so much easier
  98. Map<String, Vector<float>::type>::type mFloatValues;
  99. Map<String, Vector<Vector2>::type>::type mVec2Values;
  100. Map<String, Vector<Vector3>::type>::type mVec3Values;
  101. Map<String, Vector<Vector4>::type>::type mVec4Values;
  102. Map<String, Vector<Matrix3>::type>::type mMat3Values;
  103. Map<String, Vector<Matrix4>::type>::type mMat4Values;
  104. Map<String, Vector<StructData>::type>::type mStructValues;
  105. Map<String, HTexture>::type mTextureValues;
  106. Map<String, HSamplerState>::type mSamplerValues;
  107. Material();
  108. void throwIfNotInitialized() const;
  109. template <typename T>
  110. void setParam(const String& name, T& value, UINT32 arrayIdx)
  111. {
  112. for(auto iter = mParametersPerPass.begin(); iter != mParametersPerPass.end(); ++iter)
  113. {
  114. PassParametersPtr params = *iter;
  115. for(UINT32 i = 0; i < params->getNumParams(); i++)
  116. {
  117. GpuParamsPtr& paramPtr = params->getParamByIdx(i);
  118. if(paramPtr)
  119. {
  120. if(paramPtr->hasParam(name))
  121. paramPtr->setParam(name, value, arrayIdx);
  122. }
  123. }
  124. }
  125. }
  126. const Map<String, String>::type& getValidParamNames() const { return mValidParams; }
  127. void initBestTechnique();
  128. Map<String, const GpuParamDataDesc*>::type determineValidDataParameters(const Vector<const GpuParamDesc*>::type& paramDescs) const;
  129. Set<String>::type determineValidObjectParameters(const Vector<const GpuParamDesc*>::type& paramDescs) const;
  130. Set<String>::type determineValidShareableParamBlocks(const Vector<const GpuParamDesc*>::type& paramDescs) const;
  131. Map<String, String>::type determineParameterToBlockMapping(const Vector<const GpuParamDesc*>::type& paramDescs);
  132. bool areParamsEqual(const GpuParamDataDesc& paramA, const GpuParamDataDesc& paramB, bool ignoreBufferOffsets = false) const;
  133. void freeParamBuffers();
  134. /************************************************************************/
  135. /* RTTI */
  136. /************************************************************************/
  137. public:
  138. friend class MaterialRTTI;
  139. static RTTITypeBase* getRTTIStatic();
  140. virtual RTTITypeBase* getRTTI() const;
  141. };
  142. }