BsMaterialParams.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Reflection/BsIReflectable.h"
  6. #include "Allocators/BsStaticAlloc.h"
  7. #include "Math/BsVector2.h"
  8. #include "RenderAPI/BsGpuParams.h"
  9. namespace bs
  10. {
  11. /** @addtogroup Material-Internal
  12. * @{
  13. */
  14. struct SHADER_DATA_PARAM_DESC;
  15. struct SHADER_OBJECT_PARAM_DESC;
  16. /** Common functionality for MaterialParams and ct::MaterialParams. */
  17. class BS_CORE_EXPORT MaterialParamsBase
  18. {
  19. public:
  20. /** Type of material parameter. */
  21. enum class ParamType
  22. {
  23. Data, Texture, Sampler, Buffer
  24. };
  25. /** Result codes for getParam method. */
  26. enum class GetParamResult
  27. {
  28. Success,
  29. NotFound,
  30. InvalidType,
  31. IndexOutOfBounds
  32. };
  33. /** Meta-data about a parameter. */
  34. struct ParamData
  35. {
  36. ParamType type;
  37. GpuParamDataType dataType;
  38. UINT32 index;
  39. UINT32 arraySize;
  40. mutable UINT64 version;
  41. };
  42. /**
  43. * Creates a new material params object and initializes enough room for parameters from the provided parameter data.
  44. */
  45. MaterialParamsBase(
  46. const Map<String, SHADER_DATA_PARAM_DESC>& dataParams,
  47. const Map<String, SHADER_OBJECT_PARAM_DESC>& textureParams,
  48. const Map<String, SHADER_OBJECT_PARAM_DESC>& bufferParams,
  49. const Map<String, SHADER_OBJECT_PARAM_DESC>& samplerParams);
  50. /** Constructor for serialization use only. */
  51. MaterialParamsBase() { }
  52. virtual ~MaterialParamsBase();
  53. /**
  54. * Returns the value of a shader data parameter with the specified name at the specified array index. If the
  55. * parameter name, index or type is not valid a warning will be logged and output value will not be retrieved.
  56. *
  57. * @param[in] name Name of the shader parameter.
  58. * @param[in] arrayIdx If the parameter is an array, index of the entry to access.
  59. * @param[out] output If successful, value of the parameter.
  60. *
  61. * @tparam T Native type of the parameter.
  62. */
  63. template <typename T>
  64. void getDataParam(const String& name, UINT32 arrayIdx, T& output) const
  65. {
  66. GpuParamDataType dataType = TGpuDataParamInfo<T>::TypeId;
  67. const ParamData* param = nullptr;
  68. auto result = getParamData(name, ParamType::Data, dataType, arrayIdx, &param);
  69. if (result != GetParamResult::Success)
  70. return;
  71. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[dataType];
  72. UINT32 paramTypeSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
  73. output = *(T*)(mDataParamsBuffer[param->index + arrayIdx * paramTypeSize]);
  74. memcpy(output, &mDataParamsBuffer[param->index + arrayIdx * paramTypeSize], sizeof(paramTypeSize));
  75. }
  76. /**
  77. * Sets the value of a shader data parameter with the specified name at the specified array index. If the
  78. * parameter name, index or type is not valid a warning will be logged and output value will not be set.
  79. *
  80. * @param[in] name Name of the shader parameter.
  81. * @param[in] arrayIdx If the parameter is an array, index of the entry to access.
  82. * @param[in] input New value of the parameter.
  83. *
  84. * @tparam T Native type of the parameter.
  85. */
  86. template <typename T>
  87. void setDataParam(const String& name, UINT32 arrayIdx, const T& input) const
  88. {
  89. GpuParamDataType dataType = TGpuDataParamInfo<T>::TypeId;
  90. const ParamData* param = nullptr;
  91. auto result = getParamData(name, ParamType::Data, dataType, arrayIdx, &param);
  92. if (result != GetParamResult::Success)
  93. return;
  94. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[dataType];
  95. UINT32 paramTypeSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
  96. memcpy(&mDataParamsBuffer[param->index + arrayIdx * paramTypeSize], input, sizeof(paramTypeSize));
  97. }
  98. /**
  99. * Returns an index of the parameter with the specified name. Index can be used in a call to getParamData(UINT32) to
  100. * get the actual parameter data.
  101. *
  102. * @param[in] name Name of the shader parameter.
  103. * @return Index of the parameter, or -1 if not found.
  104. */
  105. UINT32 getParamIndex(const String& name) const;
  106. /**
  107. * Returns an index of the parameter with the specified name. Index can be used in a call to getParamData(UINT32) to
  108. * get the actual parameter data.
  109. *
  110. * @param[in] name Name of the shader parameter.
  111. * @param[in] type Type of the parameter retrieve. Error will be logged if actual type of the parameter
  112. * doesn't match.
  113. * @param[in] dataType Only relevant if the parameter is a data type. Determines exact data type of the parameter
  114. * to retrieve.
  115. * @param[in] arrayIdx Array index of the entry to retrieve.
  116. * @param[out] output Index of the requested parameter, only valid if success is returned.
  117. * @return Success or error state of the request.
  118. */
  119. GetParamResult getParamIndex(const String& name, ParamType type, GpuParamDataType dataType, UINT32 arrayIdx,
  120. UINT32& output) const;
  121. /**
  122. * Returns data about a parameter and reports an error if there is a type or size mismatch, or if the parameter
  123. * does exist.
  124. *
  125. * @param[in] name Name of the shader parameter.
  126. * @param[in] type Type of the parameter retrieve. Error will be logged if actual type of the parameter
  127. * doesn't match.
  128. * @param[in] dataType Only relevant if the parameter is a data type. Determines exact data type of the parameter
  129. * to retrieve.
  130. * @param[in] arrayIdx Array index of the entry to retrieve.
  131. * @param[out] output Object describing the parameter with an index to its data. If the parameter was not found
  132. * this value is undefined. This value will still be valid if parameter was found but
  133. * some other error was reported.
  134. * @return Success or error state of the request.
  135. */
  136. GetParamResult getParamData(const String& name, ParamType type, GpuParamDataType dataType, UINT32 arrayIdx,
  137. const ParamData** output) const;
  138. /**
  139. * Returns information about a parameter at the specified global index, as retrieved by getParamIndex().
  140. */
  141. const ParamData* getParamData(UINT32 index) const { return &mParams[index]; }
  142. /** Returns the total number of parameters managed by this object. */
  143. UINT32 getNumParams() const { return (UINT32)mParams.size(); }
  144. /**
  145. * Logs an error that was reported by getParamData().
  146. *
  147. * @param[in] errorCode Information about the error.
  148. * @param[in] name Name of the shader parameter for which the error occurred.
  149. * @param[in] arrayIdx Array index for which the error occurred.
  150. */
  151. void reportGetParamError(GetParamResult errorCode, const String& name, UINT32 arrayIdx) const;
  152. /**
  153. * Equivalent to getDataParam(const String&, UINT32, T&) except it uses the internal parameter reference
  154. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  155. * object.
  156. */
  157. template <typename T>
  158. void getDataParam(const ParamData& param, UINT32 arrayIdx, T& output) const
  159. {
  160. GpuParamDataType dataType = (GpuParamDataType)TGpuDataParamInfo<T>::TypeId;
  161. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[dataType];
  162. UINT32 paramTypeSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
  163. assert(sizeof(output) == paramTypeSize);
  164. memcpy(&output, &mDataParamsBuffer[param.index + arrayIdx * paramTypeSize], paramTypeSize);
  165. }
  166. /**
  167. * Equivalent to setDataParam(const String&, UINT32, T&) except it uses the internal parameter reference
  168. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  169. * object.
  170. */
  171. template <typename T>
  172. void setDataParam(const ParamData& param, UINT32 arrayIdx, const T& input) const
  173. {
  174. GpuParamDataType dataType = (GpuParamDataType)TGpuDataParamInfo<T>::TypeId;
  175. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[dataType];
  176. UINT32 paramTypeSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
  177. assert(sizeof(input) == paramTypeSize);
  178. memcpy(&mDataParamsBuffer[param.index + arrayIdx * paramTypeSize], &input, paramTypeSize);
  179. param.version = ++mParamVersion;
  180. }
  181. /** Returns pointer to the internal data buffer for a data parameter at the specified index. */
  182. UINT8* getData(UINT32 index) const
  183. {
  184. return &mDataParamsBuffer[index];
  185. }
  186. /** Returns a counter that gets incremented whenever a parameter gets updated. */
  187. UINT64 getParamVersion() const { return mParamVersion; }
  188. protected:
  189. const static UINT32 STATIC_BUFFER_SIZE = 256;
  190. UnorderedMap<String, UINT32> mParamLookup;
  191. Vector<ParamData> mParams;
  192. UINT8* mDataParamsBuffer = nullptr;
  193. UINT32 mDataSize = 0;
  194. UINT32 mNumStructParams = 0;
  195. UINT32 mNumTextureParams = 0;
  196. UINT32 mNumBufferParams = 0;
  197. UINT32 mNumSamplerParams = 0;
  198. mutable UINT64 mParamVersion = 1;
  199. mutable StaticAlloc<STATIC_BUFFER_SIZE, STATIC_BUFFER_SIZE> mAlloc;
  200. };
  201. /** Raw data for a single structure parameter. */
  202. class BS_CORE_EXPORT MaterialParamStructDataCore
  203. {
  204. public:
  205. UINT8* data;
  206. UINT32 dataSize;
  207. };
  208. /** Raw data for a single structure parameter. */
  209. class BS_CORE_EXPORT MaterialParamStructData : public IReflectable
  210. {
  211. public:
  212. UINT8* data;
  213. UINT32 dataSize;
  214. friend class MaterialParamStructDataRTTI;
  215. static RTTITypeBase* getRTTIStatic();
  216. RTTITypeBase* getRTTI() const override;
  217. };
  218. /** Data for a single texture parameter. */
  219. class BS_CORE_EXPORT MaterialParamTextureDataCore
  220. {
  221. public:
  222. SPtr<ct::Texture> value;
  223. bool isLoadStore;
  224. TextureSurface surface;
  225. };
  226. /** Data for a single texture parameter. */
  227. class BS_CORE_EXPORT MaterialParamTextureData : public IReflectable
  228. {
  229. public:
  230. HTexture value;
  231. bool isLoadStore;
  232. TextureSurface surface;
  233. friend class MaterialParamTextureDataRTTI;
  234. static RTTITypeBase* getRTTIStatic();
  235. RTTITypeBase* getRTTI() const override;
  236. };
  237. /** Data for a single buffer parameter. */
  238. class BS_CORE_EXPORT MaterialParamBufferDataCore
  239. {
  240. public:
  241. SPtr<ct::GpuBuffer> value;
  242. };
  243. /** Data for a single buffer parameter. */
  244. class BS_CORE_EXPORT MaterialParamBufferData
  245. {
  246. public:
  247. SPtr<GpuBuffer> value;
  248. };
  249. /** Data for a single sampler state parameter. */
  250. class BS_CORE_EXPORT MaterialParamSamplerStateDataCore
  251. {
  252. public:
  253. SPtr<ct::SamplerState> value;
  254. };
  255. /** Data for a single sampler state parameter. */
  256. class BS_CORE_EXPORT MaterialParamSamplerStateData
  257. {
  258. public:
  259. SPtr<SamplerState> value;
  260. };
  261. /** Helper typedefs that reference types used by either core or sim thread implementation of TMaterialParams<Core>. */
  262. template<bool Core> struct TMaterialParamsTypes { };
  263. template<> struct TMaterialParamsTypes < false >
  264. {
  265. typedef GpuParams GpuParamsType;
  266. typedef HTexture TextureType;
  267. typedef SPtr<GpuBuffer> BufferType;
  268. typedef SPtr<SamplerState> SamplerType;
  269. typedef SPtr<GpuParamBlockBuffer> ParamsBufferType;
  270. typedef MaterialParamStructData StructParamDataType;
  271. typedef MaterialParamTextureData TextureParamDataType;
  272. typedef MaterialParamBufferData BufferParamDataType;
  273. typedef MaterialParamSamplerStateData SamplerStateParamDataType;
  274. typedef HShader ShaderType;
  275. };
  276. template<> struct TMaterialParamsTypes < true >
  277. {
  278. typedef ct::GpuParams GpuParamsType;
  279. typedef SPtr<ct::Texture> TextureType;
  280. typedef SPtr<ct::GpuBuffer> BufferType;
  281. typedef SPtr<ct::SamplerState> SamplerType;
  282. typedef SPtr<ct::GpuParamBlockBuffer> ParamsBufferType;
  283. typedef MaterialParamStructDataCore StructParamDataType;
  284. typedef MaterialParamTextureDataCore TextureParamDataType;
  285. typedef MaterialParamBufferDataCore BufferParamDataType;
  286. typedef MaterialParamSamplerStateDataCore SamplerStateParamDataType;
  287. typedef SPtr<ct::Shader> ShaderType;
  288. };
  289. /** Common code that may be specialized for both MaterialParams and ct::MaterialParams. */
  290. template<bool Core>
  291. class BS_CORE_EXPORT TMaterialParams : public MaterialParamsBase
  292. {
  293. public:
  294. typedef typename TMaterialParamsTypes<Core>::GpuParamsType GpuParamsType;
  295. typedef typename TMaterialParamsTypes<Core>::TextureType TextureType;
  296. typedef typename TMaterialParamsTypes<Core>::BufferType BufferType;
  297. typedef typename TMaterialParamsTypes<Core>::SamplerType SamplerType;
  298. typedef typename TMaterialParamsTypes<Core>::ShaderType ShaderType;
  299. typedef typename TMaterialParamsTypes<Core>::StructParamDataType ParamStructDataType;
  300. typedef typename TMaterialParamsTypes<Core>::TextureParamDataType ParamTextureDataType;
  301. typedef typename TMaterialParamsTypes<Core>::BufferParamDataType ParamBufferDataType;
  302. typedef typename TMaterialParamsTypes<Core>::SamplerStateParamDataType ParamSamplerStateDataType;
  303. /** Creates a new material params object and initializes enough room for parameters from the provided shader. */
  304. TMaterialParams(const ShaderType& shader);
  305. /** Constructor for serialization use only. */
  306. TMaterialParams() { }
  307. virtual ~TMaterialParams();
  308. /**
  309. * Returns the value of a shader structure parameter with the specified name at the specified array index. If the
  310. * parameter name, index or type is not valid a warning will be logged and output value will not be retrieved.
  311. *
  312. * @param[in] name Name of the shader parameter.
  313. * @param[out] value Pre-allocated buffer of @p size bytes where the value will be retrieved.
  314. * @param[in] size Size of the buffer into which to write the value. Must match parameter struct's size.
  315. * @param[in] arrayIdx If the parameter is an array, index of the entry to access.
  316. */
  317. void getStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx) const;
  318. /**
  319. * Sets the value of a shader structure parameter with the specified name at the specified array index. If the
  320. * parameter name, index or type is not valid a warning will be logged and output value will not be retrieved.
  321. *
  322. * @param[in] name Name of the shader parameter.
  323. * @param[in] value Buffer of @p size bytes containing the new value of the structure.
  324. * @param[in] size Size of the buffer from which to retrieve the value. Must match parameter struct's size.
  325. * @param[in] arrayIdx If the parameter is an array, index of the entry to access.
  326. */
  327. void setStructData(const String& name, const void* value, UINT32 size, UINT32 arrayIdx);
  328. /**
  329. * Returns the value of a shader texture parameter with the specified name. If the parameter name or type is not
  330. * valid a warning will be logged and output value will not be retrieved.
  331. *
  332. * @param[in] name Name of the shader parameter.
  333. * @param[out] value Output value of the parameter.
  334. * @param[out] surface Surface describing which part of the texture is being accessed.
  335. */
  336. void getTexture(const String& name, TextureType& value, TextureSurface& surface) const;
  337. /**
  338. * Sets the value of a shader texture parameter with the specified name. If the parameter name or type is not
  339. * valid a warning will be logged and output value will not be set.
  340. *
  341. * @param[in] name Name of the shader parameter.
  342. * @param[in] value New value of the parameter.
  343. * @param[in] surface Surface describing which part of the texture is being accessed.
  344. */
  345. void setTexture(const String& name, const TextureType& value,
  346. const TextureSurface& surface = TextureSurface::COMPLETE);
  347. /**
  348. * Returns the value of a shader load/store texture parameter with the specified name. If the parameter name or
  349. * type is not valid a warning will be logged and output value will not be retrieved.
  350. *
  351. * @param[in] name Name of the shader parameter.
  352. * @param[out] value Output value of the parameter.
  353. * @param[out] surface Surface describing which part of the texture is being accessed.
  354. */
  355. void getLoadStoreTexture(const String& name, TextureType& value, TextureSurface& surface) const;
  356. /**
  357. * Sets the value of a shader load/store texture parameter with the specified name. If the parameter name or
  358. * type is not valid a warning will be logged and the value will not be set.
  359. *
  360. * @param[in] name Name of the shader parameter.
  361. * @param[in] value New value of the parameter.
  362. * @param[in] surface Surface describing which part of the texture is being accessed.
  363. */
  364. void setLoadStoreTexture(const String& name, const TextureType& value, const TextureSurface& surface);
  365. /**
  366. * Returns the value of a shader buffer parameter with the specified name. If the parameter name or type is not
  367. * valid a warning will be logged and output value will not be retrieved.
  368. *
  369. * @param[in] name Name of the shader parameter.
  370. * @param[out] value Output value of the parameter.
  371. */
  372. void getBuffer(const String& name, BufferType& value) const;
  373. /**
  374. * Sets the value of a shader buffer parameter with the specified name. If the parameter name or type is not
  375. * valid a warning will be logged and output value will not be set.
  376. *
  377. * @param[in] name Name of the shader parameter.
  378. * @param[in] value New value of the parameter.
  379. */
  380. void setBuffer(const String& name, const BufferType& value);
  381. /**
  382. * Sets the value of a shader sampler state parameter with the specified name. If the parameter name or type is not
  383. * valid a warning will be logged and output value will not be set.
  384. *
  385. * @param[in] name Name of the shader parameter.
  386. * @param[out] value Output value of the parameter.
  387. */
  388. void getSamplerState(const String& name, SamplerType& value) const;
  389. /**
  390. * Sets the value of a shader sampler state parameter with the specified name. If the parameter name or type is not
  391. * valid a warning will be logged and output value will not be set.
  392. *
  393. * @param[in] name Name of the shader parameter.
  394. * @param[in] value New value of the parameter.
  395. */
  396. void setSamplerState(const String& name, const SamplerType& value);
  397. /**
  398. * Equivalent to getStructData(const String&, UINT32, void*, UINT32) except it uses the internal parameter reference
  399. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  400. * object.
  401. */
  402. void getStructData(const ParamData& param, void* value, UINT32 size, UINT32 arrayIdx) const;
  403. /**
  404. * Equivalent to setStructData(const String&, UINT32, void*, UINT32) except it uses the internal parameter reference
  405. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  406. * object.
  407. */
  408. void setStructData(const ParamData& param, const void* value, UINT32 size, UINT32 arrayIdx);
  409. /**
  410. * Returns a size of a struct parameter in bytes, using the internal parameter reference. Caller must guarantee the
  411. * parameter reference is valid and belongs to this object.
  412. */
  413. UINT32 getStructSize(const ParamData& param) const;
  414. /**
  415. * Equivalent to getTexture(const String&, HTexture&) except it uses the internal parameter reference directly,
  416. * avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this object.
  417. */
  418. void getTexture(const ParamData& param, TextureType& value, TextureSurface& surface) const;
  419. /**
  420. * Equivalent to setTexture(const String&, HTexture&) except it uses the internal parameter reference directly,
  421. * avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this object.
  422. */
  423. void setTexture(const ParamData& param, const TextureType& value,
  424. const TextureSurface& surface = TextureSurface::COMPLETE);
  425. /**
  426. * Equivalent to getBuffer(const String&, SPtr<GpuBuffer>&) except it uses the internal parameter reference
  427. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  428. * object.
  429. */
  430. void getBuffer(const ParamData& param, BufferType& value) const;
  431. /**
  432. * Equivalent to setBuffer(const String&, SPtr<GpuBuffer>&) except it uses the internal parameter reference
  433. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  434. * object.
  435. */
  436. void setBuffer(const ParamData& param, const BufferType& value);
  437. /**
  438. * Equivalent to getLoadStoreTexture(const String&, HTexture&, TextureSurface&) except it uses the internal
  439. * parameter reference directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid
  440. * and belongs to this object.
  441. */
  442. void getLoadStoreTexture(const ParamData& param, TextureType& value, TextureSurface& surface) const;
  443. /**
  444. * Equivalent to setLoadStoreTexture(const String&, HTexture&, TextureSurface&) except it uses the internal
  445. * parameter reference directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid
  446. * and belongs to this object.
  447. */
  448. void setLoadStoreTexture(const ParamData& param, const TextureType& value, const TextureSurface& surface);
  449. /**
  450. * Checks is a texture with the specified parameter reference a load/store texture or a normal one. Caller must
  451. * guarantee the parameter reference is valid and belongs to this object.
  452. */
  453. bool getIsTextureLoadStore(const ParamData& param) const;
  454. /**
  455. * Equivalent to getSamplerState(const String&, SPtr<SamplerState>&) except it uses the internal parameter reference
  456. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  457. * object.
  458. */
  459. void getSamplerState(const ParamData& param, SamplerType& value) const;
  460. /**
  461. * Equivalent to setSamplerState(const String&, SPtr<SamplerState>&) except it uses the internal parameter reference
  462. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  463. * object.
  464. */
  465. void setSamplerState(const ParamData& param, const SamplerType& value);
  466. /**
  467. * Returns the default texture (one assigned when no other is provided), if available for the specified parameter.
  468. * Parameter is represented using the internal parameter reference and the caller must guarantee the parameter
  469. * eference is valid and belongs to this object.
  470. */
  471. void getDefaultTexture(const ParamData& param, TextureType& value) const;
  472. /**
  473. * Returns the default sampler state (one assigned when no other is provided), if available for the specified
  474. * parameter. Parameter is represented using the internal parameter reference and the caller must guarantee the
  475. * parameter reference is valid and belongs to this object.
  476. */
  477. void getDefaultSamplerState(const ParamData& param, SamplerType& value) const;
  478. protected:
  479. ParamStructDataType* mStructParams = nullptr;
  480. ParamTextureDataType* mTextureParams = nullptr;
  481. ParamBufferDataType* mBufferParams = nullptr;
  482. ParamSamplerStateDataType* mSamplerStateParams = nullptr;
  483. TextureType* mDefaultTextureParams = nullptr;
  484. SamplerType* mDefaultSamplerStateParams = nullptr;
  485. };
  486. /**
  487. * Contains all parameter values set in a Material. This is similar to GpuParams which also stores parameter values,
  488. * however GpuParams are built for use on the GPU-side and don't store parameters that don't exist in a compiled GPU
  489. * program. This object on the other hand stores all parameters defined in a shader, regardless or not if they actually
  490. * exist in the GPU program. Additionally GpuParams are defined per-program (for example vertex, fragment) while this
  491. * object exists for the entire material.
  492. *
  493. * @note
  494. * This introduces redundancy as parameters stored by GpuParams and this object are duplicated. If this is an issue the
  495. * implementation can be modified to only store parameters not included in GpuParams.
  496. * @note
  497. * The reason why parameters in this class and GpuParams differ is most often compiler optimizations. If a compiler
  498. * optimizes out a variable in a GPU program we should still be able to store it, either for later when the variable
  499. * will be introduced, or for other techniques that might have that variable implemented.
  500. */
  501. class BS_CORE_EXPORT MaterialParams : public IReflectable, public TMaterialParams<false>
  502. {
  503. public:
  504. /** @copydoc TMaterialParams::TMaterialParams(const ShaderType&) */
  505. MaterialParams(const HShader& shader);
  506. /**
  507. * Populates the provided buffer with parameters that can be used for syncing this object with its core-thread
  508. * counterpart. Can be applied by calling ct::MaterialParams::setSyncData.
  509. *
  510. * @param[in] buffer Pre-allocated buffer to store the sync data in. Set to null to calculate the size
  511. * of the required buffer.
  512. * @param[in, out] size Size of the provided allocated buffer. Or if the buffer is null, this parameter will
  513. * contain the required buffer size when the method executes.
  514. * @param[in] forceAll If false, only the parameters that were changed since the last call will be synced.
  515. * Otherwise all parameters will be synced.
  516. */
  517. void getSyncData(UINT8* buffer, UINT32& size, bool forceAll);
  518. /** Appends any resources stored by this object to the provided vector. */
  519. void getResourceDependencies(Vector<HResource>& resources);
  520. /** Appends any core objects stored by this object to the provided vector. */
  521. void getCoreObjectDependencies(Vector<CoreObject*>& coreObjects);
  522. private:
  523. friend class ct::MaterialParams;
  524. UINT64 mLastSyncVersion;
  525. /************************************************************************/
  526. /* RTTI */
  527. /************************************************************************/
  528. public:
  529. MaterialParams() { } // Only for serialization
  530. friend class MaterialParamsRTTI;
  531. static RTTITypeBase* getRTTIStatic();
  532. RTTITypeBase* getRTTI() const override;
  533. };
  534. namespace ct
  535. {
  536. /** Core thread version of MaterialParams. */
  537. class BS_CORE_EXPORT MaterialParams : public TMaterialParams<true>
  538. {
  539. public:
  540. /** Initializes the core thread version of MaterialParams from its sim thread counterpart. */
  541. MaterialParams(const SPtr<Shader>& shader, const SPtr<bs::MaterialParams>& params);
  542. /** @copydoc TMaterialParams::TMaterialParams(const ShaderType&) */
  543. MaterialParams(const SPtr<Shader>& shader);
  544. /**
  545. * Updates the stored parameters from the provided buffer, allowing changes to be transfered between the sim and
  546. * core thread material param objects. Buffer must be retrieved from bs::MaterialParams::getSyncData.
  547. *
  548. * @param[in] buffer Buffer containing the dirty data.
  549. * @param[in, out] size Size of the provided buffer.
  550. */
  551. void setSyncData(UINT8* buffer, UINT32 size);
  552. };
  553. }
  554. /** @} */
  555. }