CmMaterial.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 BansheeEngine
  12. {
  13. /**
  14. * @brief Helper class containing parameters for all types
  15. * of GPU programs used in a pass.
  16. */
  17. class CM_EXPORT PassParameters
  18. {
  19. public:
  20. GpuParamsPtr mVertParams;
  21. GpuParamsPtr mFragParams;
  22. GpuParamsPtr mGeomParams;
  23. GpuParamsPtr mHullParams;
  24. GpuParamsPtr mDomainParams;
  25. GpuParamsPtr mComputeParams;
  26. /**
  27. * @brief Returns a set of GPU parameters based on an index.
  28. *
  29. * @note Useful when needing to iterate over all sets of GPU parameters.
  30. */
  31. GpuParamsPtr& getParamByIdx(UINT32 idx)
  32. {
  33. GpuParamsPtr* paramArray[] = {&mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams};
  34. return *paramArray[idx];
  35. }
  36. /**
  37. * @brief Returns the total number of stored sets of
  38. * GPU parameters in this object.
  39. */
  40. UINT32 getNumParams() const { return 6; }
  41. };
  42. /**
  43. * @brief Material represents a shader and parameters used to set up
  44. * that shader. It provides a simple interface for manipulating the
  45. * parameters.
  46. */
  47. class CM_EXPORT Material : public Resource
  48. {
  49. public:
  50. /**
  51. * @brief Data used to described a structure defined within a shader.
  52. */
  53. struct StructData
  54. {
  55. StructData()
  56. :size(0), data(nullptr)
  57. { }
  58. StructData(UINT32 _size)
  59. :size(_size)
  60. {
  61. data = std::shared_ptr<void>(cm_alloc<ScratchAlloc>(_size), &cm_free<ScratchAlloc>);
  62. }
  63. /**
  64. * @brief Writes the specified data to the internal buffer. Caller
  65. * must ensure size of the provided buffer is correct.
  66. */
  67. void write(void* _data)
  68. {
  69. memcpy(data.get(), _data, size);
  70. }
  71. std::shared_ptr<void> data;
  72. UINT32 size;
  73. };
  74. ~Material();
  75. /**
  76. * @brief Sets a shader that will be used by the material. Best technique within the
  77. * provided shader will be used for the material.
  78. *
  79. * @note Shader must be set before doing any other operations with the material.
  80. *
  81. * After setting the shader if change any systems that a shader technique is
  82. * dependent upon (render system, renderer, etc), you will need to call this
  83. * method again on all your Materials to make sure technique used is updated.
  84. */
  85. void setShader(ShaderPtr shader);
  86. /**
  87. * @brief Returns the currently active shader.
  88. */
  89. ShaderPtr getShader() const { return mShader; }
  90. /** @brief Assigns a texture to the shader parameter with the specified name. */
  91. void setTexture(const String& name, const HTexture& value) { return getParamTexture(name).set(value); }
  92. /** @brief Assigns a sampler state to the shader parameter with the specified name. */
  93. void setSamplerState(const String& name, const HSamplerState& value) { return getParamSamplerState(name).set(value); }
  94. /**
  95. * @brief Assigns a float value to the shader parameter with the specified name.
  96. *
  97. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  98. */
  99. void setFloat(const String& name, float value, UINT32 arrayIdx = 0) { return getParamFloat(name).set(value, arrayIdx); }
  100. /**
  101. * @brief Assigns a color to the shader parameter with the specified name.
  102. *
  103. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  104. */
  105. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0);
  106. /**
  107. * @brief Assigns a 2D vector to the shader parameter with the specified name.
  108. *
  109. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  110. */
  111. void setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0) { return getParamVec2(name).set(value, arrayIdx); }
  112. /**
  113. * @brief Assigns a 3D vector to the shader parameter with the specified name.
  114. *
  115. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  116. */
  117. void setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0) { return getParamVec3(name).set(value, arrayIdx); }
  118. /**
  119. * @brief Assigns a 4D vector to the shader parameter with the specified name.
  120. *
  121. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  122. */
  123. void setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0) { return getParamVec4(name).set(value, arrayIdx); }
  124. /**
  125. * @brief Assigns a 3x3 matrix to the shader parameter with the specified name.
  126. *
  127. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  128. */
  129. void setMat3(const String& name, const Matrix3& value, UINT32 arrayIdx = 0) { return getParamMat3(name).set(value, arrayIdx); }
  130. /**
  131. * @brief Assigns a 4x4 matrix to the shader parameter with the specified name.
  132. *
  133. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  134. */
  135. void setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0) { return getParamMat4(name).set(value, arrayIdx); }
  136. /**
  137. * @brief Assigns a structure to the shader parameter with the specified name.
  138. *
  139. * Structure is provided as a raw buffer and caller must ensure structure in buffer
  140. * matches what the shader expects.
  141. *
  142. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  143. */
  144. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0) { return getParamStruct(name).set(value, size, arrayIdx); }
  145. /**
  146. * @brief Sets a render queue in which will objects with this material be rendered in. Objects
  147. * with smaller render queue number will get rendered before objects with a large one.
  148. *
  149. * @note This flag can be differently interpreted by renderers but normally you want to separate out
  150. * opaque and transparent objects as they usually require slightly different render ordering.
  151. */
  152. void setRenderQueue(INT16 renderQueue) { mRenderQueue = renderQueue; }
  153. /**
  154. * @brief Assign a parameter block buffer with the specified name.
  155. *
  156. * @note Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or
  157. * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters
  158. * in the buffers need only be set once and then reused multiple times.
  159. */
  160. void setParamBlockBuffer(const String& name, const GpuParamBlockBufferPtr& paramBlock);
  161. /** @brief Returns a texture assigned with the parameter with the specified name. */
  162. HTexture getTexture(const String& name) const { return getParamTexture(name).get(); }
  163. /** @brief Returns a sampler state assigned with the parameter with the specified name. */
  164. HSamplerState getSamplerState(const String& name) const { return getParamSamplerState(name).get(); }
  165. /**
  166. * @brief Returns a float value assigned with the parameter with the specified name.
  167. *
  168. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  169. */
  170. float getFloat(const String& name, UINT32 arrayIdx = 0) const { return getParamFloat(name).get(arrayIdx); }
  171. /**
  172. * @brief Returns a 2D vector assigned with the parameter with the specified name.
  173. *
  174. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  175. */
  176. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const { return getParamVec2(name).get(arrayIdx); }
  177. /**
  178. * @brief Returns a 3D vector assigned with the parameter with the specified name.
  179. *
  180. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  181. */
  182. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const { return getParamVec3(name).get(arrayIdx); }
  183. /**
  184. * @brief Returns a 4D vector assigned with the parameter with the specified name.
  185. *
  186. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  187. */
  188. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const { return getParamVec4(name).get(arrayIdx); }
  189. /**
  190. * @brief Returns a 3x3 matrix assigned with the parameter with the specified name.
  191. *
  192. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  193. */
  194. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const { return getParamMat3(name).get(arrayIdx); }
  195. /**
  196. * @brief Returns a 4x4 matrix assigned with the parameter with the specified name.
  197. *
  198. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  199. */
  200. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const { return getParamMat4(name).get(arrayIdx); }
  201. /**
  202. * @brief Returns a buffer representing a structure assigned to the parameter with the specified name.
  203. *
  204. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  205. */
  206. StructData getStructData(const String& name, UINT32 arrayIdx = 0) const;
  207. /**
  208. * @brief Returns a float GPU parameter. This parameter may be used for
  209. * more efficiently getting/setting GPU parameter values than calling
  210. * Material::get* / Material::set* methods.
  211. *
  212. * @note Expected behavior is that you would retrieve this parameter when
  213. * initially constructing the material, and then use it throughout material
  214. * lifetime to assign and retrieve parameter values.
  215. *
  216. * If material shader changes this handle will be invalidated.
  217. */
  218. GpuParamFloat getParamFloat(const String& name) const;
  219. /**
  220. * @brief Returns a 2D vector GPU parameter. This parameter may be used for
  221. * more efficiently getting/setting GPU parameter values than calling
  222. * Material::get* / Material::set* methods.
  223. *
  224. * @note Expected behavior is that you would retrieve this parameter when
  225. * initially constructing the material, and then use it throughout material
  226. * lifetime to assign and retrieve parameter values.
  227. *
  228. * If material shader changes this handle will be invalidated.
  229. */
  230. GpuParamVec2 getParamVec2(const String& name) const;
  231. /**
  232. * @brief Returns a 3D vector GPU parameter. This parameter may be used for
  233. * more efficiently getting/setting GPU parameter values than calling
  234. * Material::get* / Material::set* methods.
  235. *
  236. * @note Expected behavior is that you would retrieve this parameter when
  237. * initially constructing the material, and then use it throughout material
  238. * lifetime to assign and retrieve parameter values.
  239. *
  240. * If material shader changes this handle will be invalidated.
  241. */
  242. GpuParamVec3 getParamVec3(const String& name) const;
  243. /**
  244. * @brief Returns a 4D vector GPU parameter. This parameter may be used for
  245. * more efficiently getting/setting GPU parameter values than calling
  246. * Material::get* / Material::set* methods.
  247. *
  248. * @note Expected behavior is that you would retrieve this parameter when
  249. * initially constructing the material, and then use it throughout material
  250. * lifetime to assign and retrieve parameter values.
  251. *
  252. * If material shader changes this handle will be invalidated.
  253. */
  254. GpuParamVec4 getParamVec4(const String& name) const;
  255. /**
  256. * @brief Returns a 3x3 matrix GPU parameter. This parameter may be used for
  257. * more efficiently getting/setting GPU parameter values than calling
  258. * Material::get* / Material::set* methods.
  259. *
  260. * @note Expected behavior is that you would retrieve this parameter when
  261. * initially constructing the material, and then use it throughout material
  262. * lifetime to assign and retrieve parameter values.
  263. *
  264. * If material shader changes this handle will be invalidated.
  265. */
  266. GpuParamMat3 getParamMat3(const String& name) const;
  267. /**
  268. * @brief Returns a 4x4 matrix GPU parameter. This parameter may be used for
  269. * more efficiently getting/setting GPU parameter values than calling
  270. * Material::get* / Material::set* methods.
  271. *
  272. * @note Expected behavior is that you would retrieve this parameter when
  273. * initially constructing the material, and then use it throughout material
  274. * lifetime to assign and retrieve parameter values.
  275. *
  276. * If material shader changes this handle will be invalidated.
  277. */
  278. GpuParamMat4 getParamMat4(const String& name) const;
  279. /**
  280. * @brief Returns a structure GPU parameter. This parameter may be used for
  281. * more efficiently getting/setting GPU parameter values than calling
  282. * Material::get* / Material::set* methods.
  283. *
  284. * @note Expected behavior is that you would retrieve this parameter when
  285. * initially constructing the material, and then use it throughout material
  286. * lifetime to assign and retrieve parameter values.
  287. *
  288. * If material shader changes this handle will be invalidated.
  289. */
  290. GpuParamStruct getParamStruct(const String& name) const;
  291. /**
  292. * @brief Returns a texture GPU parameter. This parameter may be used for
  293. * more efficiently getting/setting GPU parameter values than calling
  294. * Material::get* / Material::set* methods.
  295. *
  296. * @note Expected behavior is that you would retrieve this parameter when
  297. * initially constructing the material, and then use it throughout material
  298. * lifetime to assign and retrieve parameter values.
  299. *
  300. * If material shader changes this handle will be invalidated.
  301. */
  302. GpuParamTexture getParamTexture(const String& name) const;
  303. /**
  304. * @brief Returns a sampler state GPU parameter. This parameter may be used for
  305. * more efficiently getting/setting GPU parameter values than calling
  306. * Material::get* / Material::set* methods.
  307. *
  308. * @note Expected behavior is that you would retrieve this parameter when
  309. * initially constructing the material, and then use it throughout material
  310. * lifetime to assign and retrieve parameter values.
  311. *
  312. * If material shader changes this handle will be invalidated.
  313. */
  314. GpuParamSampState getParamSamplerState(const String& name) const;
  315. /**
  316. * @brief Returns the currently set render queue.
  317. *
  318. * @see Material::setRenderQueue
  319. */
  320. INT16 getRenderQueue() const { return mRenderQueue; }
  321. /**
  322. * @brief Returns the number of passes that are used
  323. * by the shader contained in the material.
  324. */
  325. UINT32 getNumPasses() const;
  326. /**
  327. * @brief Retrieves a specific shader pass.
  328. */
  329. PassPtr getPass(UINT32 passIdx) const;
  330. /**
  331. * @brief Returns a set of parameters for all GPU programs
  332. * in the specified shader pass.
  333. */
  334. PassParametersPtr getPassParameters(UINT32 passIdx) const;
  335. /**
  336. * @brief Creates a new empty material.
  337. *
  338. * @note Make sure you call Material::setShader before using it.
  339. */
  340. static HMaterial create();
  341. /**
  342. * @brief Creates a new material with the specified shader.
  343. */
  344. static HMaterial create(ShaderPtr shader);
  345. protected:
  346. /**
  347. * @copydoc Resource::destroy_internal
  348. */
  349. void destroy_internal();
  350. /**
  351. * @brief Allows you to retrieve a handle to a parameter that you can then use for quickly
  352. * setting and retrieving parameter data. This allows you to set/get parameter data
  353. * without all the cost of extra lookups otherwise required.
  354. *
  355. * @note All of these handles will be invalidated if material shader ever changes. It is up to the
  356. * caller to keep track of that.
  357. */
  358. template <typename T>
  359. void getParam(const String& name, GpuDataParamBase<T>& output) const
  360. {
  361. throwIfNotInitialized();
  362. auto iterFind = mValidParams.find(name);
  363. if(iterFind == mValidParams.end())
  364. {
  365. LOGWRN("Material doesn't have a parameter named " + name);
  366. return;
  367. }
  368. const String& gpuVarName = iterFind->second;
  369. GpuParamsPtr params = findParamsWithName(gpuVarName);
  370. params->getParam<T>(gpuVarName, output);
  371. }
  372. private:
  373. friend class MaterialManager;
  374. ShaderPtr mShader;
  375. TechniquePtr mBestTechnique;
  376. INT16 mRenderQueue;
  377. Set<String> mValidShareableParamBlocks;
  378. Map<String, String> mValidParams; // Also maps Shader param name -> gpu variable name
  379. Vector<PassParametersPtr> mParametersPerPass;
  380. Vector<GpuParamBlockBufferPtr> mParamBuffers;
  381. Material();
  382. /**
  383. * @brief Throw an exception if no shader is set, or no acceptable
  384. * technique was found.
  385. */
  386. void throwIfNotInitialized() const;
  387. /**
  388. * @brief Retrieves a list of all shader GPU parameters, and the GPU program variable names they map to.
  389. */
  390. const Map<String, String>& getValidParamNames() const { return mValidParams; }
  391. /**
  392. * @brief Initializes the material by using the best technique from the currently set shader. Shader
  393. * must contain the technique that matches the current renderer and render system.
  394. */
  395. void initBestTechnique();
  396. /**
  397. * @brief Constructs a map containing all data parameters (e.g. float, vector3, color).
  398. * Map contains parameter names and descriptions.
  399. */
  400. Map<String, const GpuParamDataDesc*> determineValidDataParameters(const Vector<const GpuParamDesc*>& paramDescs) const;
  401. /**
  402. * @brief Constructs a list containing all object parameter (e.g. texture, sampler state) names.
  403. */
  404. Set<String> determineValidObjectParameters(const Vector<const GpuParamDesc*>& paramDescs) const;
  405. /**
  406. * @brief Constructs a list containing all shareable parameter block names. Shareable blocks may be shared between
  407. * different GPU programs, passes or even materials.
  408. */
  409. Set<String> determineValidShareableParamBlocks(const Vector<const GpuParamDesc*>& paramDescs) const;
  410. /**
  411. * @brief Constructs a map that maps parameter names to a parameter block.
  412. */
  413. Map<String, String> determineParameterToBlockMapping(const Vector<const GpuParamDesc*>& paramDescs);
  414. /**
  415. * @brief Checks are the specified two parameters equal
  416. *
  417. * @param paramA The parameter a to compare.
  418. * @param paramB The parameter b to compare.
  419. * @param ignoreBufferOffsets (optional) If true, parameter offsets into the parameter buffer will be ignored
  420. * when comparing.
  421. */
  422. bool areParamsEqual(const GpuParamDataDesc& paramA, const GpuParamDataDesc& paramB, bool ignoreBufferOffsets = false) const;
  423. /**
  424. * @brief Frees all parameter block buffers.
  425. */
  426. void freeParamBuffers();
  427. /**
  428. * @brief Finds a set of GPU parameters containing a data (e.g. float, vector2) parameter with the provided name.
  429. */
  430. GpuParamsPtr findParamsWithName(const String& name) const;
  431. /**
  432. * @brief Finds a set of GPU parameters containing a texture parameter with the provided name.
  433. */
  434. GpuParamsPtr findTexWithName(const String& name) const;
  435. /**
  436. * @brief Finds a set of GPU parameters containing a sampler state parameter with the provided name.
  437. */
  438. GpuParamsPtr findSamplerStateWithName(const String& name) const;
  439. /************************************************************************/
  440. /* RTTI */
  441. /************************************************************************/
  442. public:
  443. friend class MaterialRTTI;
  444. static RTTITypeBase* getRTTIStatic();
  445. virtual RTTITypeBase* getRTTI() const;
  446. };
  447. }