BsMaterial.h 19 KB

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