2
0

CmMaterial.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmResource.h"
  4. #include "CmGpuParam.h"
  5. #include "CmVector2.h"
  6. #include "CmVector3.h"
  7. #include "CmVector4.h"
  8. #include "CmMatrix3.h"
  9. #include "CmMatrix4.h"
  10. #include "CmCommonEnums.h"
  11. namespace CamelotFramework
  12. {
  13. class CM_EXPORT PassParameters
  14. {
  15. public:
  16. GpuParamsPtr mVertParams;
  17. GpuParamsPtr mFragParams;
  18. GpuParamsPtr mGeomParams;
  19. GpuParamsPtr mHullParams;
  20. GpuParamsPtr mDomainParams;
  21. GpuParamsPtr mComputeParams;
  22. // Helper method
  23. GpuParamsPtr& getParamByIdx(UINT32 idx)
  24. {
  25. GpuParamsPtr* paramArray[] = {&mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams};
  26. return *paramArray[idx];
  27. }
  28. // Helper method
  29. UINT32 getNumParams() const { return 6; }
  30. };
  31. class CM_EXPORT Material : public Resource
  32. {
  33. public:
  34. struct StructData
  35. {
  36. StructData()
  37. :size(0), data(nullptr)
  38. { }
  39. StructData(void* _data, UINT32 _size)
  40. :size(_size)
  41. {
  42. data = std::shared_ptr<void>(cm_alloc<ScratchAlloc>(_size), &cm_free<ScratchAlloc>);
  43. memcpy(data.get(), _data, size);
  44. }
  45. std::shared_ptr<void> data;
  46. UINT32 size;
  47. };
  48. ~Material();
  49. /**
  50. * @brief Sets a shader that will be used by the material.
  51. * Shaders best technique will be retrieved and used in all subsequent Material
  52. * operations.
  53. * You need to call this method before doing any other operations with this class.
  54. * After setting the shader if change any systems that a shader technique is defendant upon (render system, renderer, ...)
  55. * you will need to call this method again on all your Materials to make sure technique used is updated.
  56. */
  57. void setShader(ShaderPtr shader);
  58. ShaderPtr getShader() const { return mShader; }
  59. void setTexture(const String& name, const HTexture& value);
  60. void setSamplerState(const String& name, const HSamplerState& samplerState);
  61. void setFloat(const String& name, float value, UINT32 arrayIdx = 0);
  62. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0);
  63. void setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0);
  64. void setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0);
  65. void setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0);
  66. void setMat3(const String& name, const Matrix3& value, UINT32 arrayIdx = 0);
  67. void setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0);
  68. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0);
  69. void setRenderQueue(INT16 renderQueue) { mRenderQueue = renderQueue; }
  70. //void setParamBlock(const String& name, GpuParamBlockPtr paramBlock);
  71. HTexture getTexture(const String& name) const;
  72. HSamplerState getSamplerState(const String& name) const;
  73. float getFloat(const String& name, UINT32 arrayIdx = 0) const;
  74. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const;
  75. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const;
  76. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const;
  77. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const;
  78. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const;
  79. const StructData& getStructData(const String& name, UINT32 arrayIdx = 0) const;
  80. INT16 getRenderQueue() const { return mRenderQueue; }
  81. UINT32 getNumPasses() const;
  82. PassPtr getPass(UINT32 passIdx) const;
  83. PassParametersPtr getPassParameters(UINT32 passIdx) const;
  84. static HMaterial create();
  85. static HMaterial create(ShaderPtr shader);
  86. public:
  87. /**
  88. * @brief Allows you to retrieve a handle to a parameter that you can then use for quickly
  89. * setting and retrieving parameter data. This allows you to set/get parameter data
  90. * without all the cost of extra lookups otherwise required.
  91. *
  92. * @note All of these handles will be invalidated if material shader ever changes. It is up to the
  93. * caller to keep track of that.
  94. */
  95. template <typename T>
  96. void getParam(const String& name, GpuDataParamBase<T>& output) const
  97. {
  98. auto iterFind = mValidParams.find(name);
  99. if(iterFind == mValidParams.end())
  100. {
  101. LOGWRN("Material doesn't have a parameter named " + name);
  102. return;
  103. }
  104. const String& gpuVarName = iterFind->second;
  105. for(auto iter = mParametersPerPass.begin(); iter != mParametersPerPass.end(); ++iter)
  106. {
  107. PassParametersPtr params = *iter;
  108. for(UINT32 i = 0; i < params->getNumParams(); i++)
  109. {
  110. GpuParamsPtr& paramPtr = params->getParamByIdx(i);
  111. if(paramPtr)
  112. {
  113. if(paramPtr->hasParam(gpuVarName))
  114. {
  115. paramPtr->getParam<T>(gpuVarName, output);
  116. return;
  117. }
  118. }
  119. }
  120. }
  121. CM_EXCEPT(InternalErrorException, "Shader has no parameter with the name: " + gpuVarName);
  122. }
  123. protected:
  124. void destroy_internal();
  125. private:
  126. friend class MaterialManager;
  127. ShaderPtr mShader;
  128. TechniquePtr mBestTechnique;
  129. INT16 mRenderQueue;
  130. Set<String>::type mValidShareableParamBlocks;
  131. Map<String, String>::type mValidParams; // Also maps Shader param name -> gpu variable name
  132. Vector<PassParametersPtr>::type mParametersPerPass;
  133. Vector<GpuParamBlockBufferPtr>::type mParamBuffers;
  134. // These maps aren't necessary as we can read these values from the GpuParams directly
  135. // but they make many things (especially serializing and getting values) so much easier
  136. Map<String, Vector<StructData>::type>::type mStructValues;
  137. Map<String, HTexture>::type mTextureValues;
  138. Map<String, HSamplerState>::type mSamplerValues;
  139. Material();
  140. void throwIfNotInitialized() const;
  141. const Map<String, String>::type& getValidParamNames() const { return mValidParams; }
  142. void initBestTechnique();
  143. Map<String, const GpuParamDataDesc*>::type determineValidDataParameters(const Vector<const GpuParamDesc*>::type& paramDescs) const;
  144. Set<String>::type determineValidObjectParameters(const Vector<const GpuParamDesc*>::type& paramDescs) const;
  145. Set<String>::type determineValidShareableParamBlocks(const Vector<const GpuParamDesc*>::type& paramDescs) const;
  146. Map<String, String>::type determineParameterToBlockMapping(const Vector<const GpuParamDesc*>::type& paramDescs);
  147. bool areParamsEqual(const GpuParamDataDesc& paramA, const GpuParamDataDesc& paramB, bool ignoreBufferOffsets = false) const;
  148. void freeParamBuffers();
  149. /************************************************************************/
  150. /* RTTI */
  151. /************************************************************************/
  152. public:
  153. friend class MaterialRTTI;
  154. static RTTITypeBase* getRTTIStatic();
  155. virtual RTTITypeBase* getRTTI() const;
  156. };
  157. }