BsMaterialParams.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 "BsIReflectable.h"
  6. #include "BsStaticAlloc.h"
  7. #include "BsVector2.h"
  8. #include "BsGpuParams.h"
  9. namespace BansheeEngine
  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 MaterialParamsCore. */
  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 UINT32 dirtyFlags;
  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 index
  154. * directly, avoiding the name lookup. Caller must guarantee the index is valid.
  155. */
  156. template <typename T>
  157. void getDataParam(UINT32 index, UINT32 arrayIdx, T& output) const
  158. {
  159. GpuParamDataType dataType = (GpuParamDataType)TGpuDataParamInfo<T>::TypeId;
  160. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[dataType];
  161. UINT32 paramTypeSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
  162. assert(sizeof(output) == paramTypeSize);
  163. memcpy(&output, &mDataParamsBuffer[index + arrayIdx * paramTypeSize], paramTypeSize);
  164. }
  165. /**
  166. * Equivalent to setDataParam(const String&, UINT32, T&) except it uses the internal parameter index
  167. * directly, avoiding the name lookup. Caller must guarantee the index is valid.
  168. */
  169. template <typename T>
  170. void setDataParam(UINT32 index, UINT32 arrayIdx, const T& input) const
  171. {
  172. GpuParamDataType dataType = (GpuParamDataType)TGpuDataParamInfo<T>::TypeId;
  173. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[dataType];
  174. UINT32 paramTypeSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
  175. assert(sizeof(input) == paramTypeSize);
  176. memcpy(&mDataParamsBuffer[index + arrayIdx * paramTypeSize], &input, paramTypeSize);
  177. }
  178. /** Returns pointer to the internal data buffer for a data parameter at the specified index. */
  179. UINT8* getData(UINT32 index) const
  180. {
  181. return &mDataParamsBuffer[index];
  182. }
  183. /**
  184. * Clears dirty flags for all parameters, for the specified index.
  185. *
  186. * @param[in] index Index of the bit to clear. Must be in range [0-31]
  187. */
  188. void clearDirtyFlags(UINT32 index);
  189. protected:
  190. const static UINT32 STATIC_BUFFER_SIZE = 256;
  191. UnorderedMap<String, UINT32> mParamLookup;
  192. Vector<ParamData> mParams;
  193. UINT8* mDataParamsBuffer = nullptr;
  194. UINT32 mDataSize = 0;
  195. UINT32 mNumStructParams = 0;
  196. UINT32 mNumTextureParams = 0;
  197. UINT32 mNumBufferParams = 0;
  198. UINT32 mNumSamplerParams = 0;
  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<TextureCore> 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<GpuBufferCore> 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<SamplerStateCore> 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 GpuParamsCore GpuParamsType;
  279. typedef SPtr<TextureCore> TextureType;
  280. typedef SPtr<GpuBufferCore> BufferType;
  281. typedef SPtr<SamplerStateCore> SamplerType;
  282. typedef SPtr<GpuParamBlockBufferCore> ParamsBufferType;
  283. typedef MaterialParamStructDataCore StructParamDataType;
  284. typedef MaterialParamTextureDataCore TextureParamDataType;
  285. typedef MaterialParamBufferDataCore BufferParamDataType;
  286. typedef MaterialParamSamplerStateDataCore SamplerStateParamDataType;
  287. typedef SPtr<ShaderCore> ShaderType;
  288. };
  289. /** Common code that may be specialized for both MaterialParams and MaterialParamsCore. */
  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. */
  335. void getTexture(const String& name, TextureType& value) const;
  336. /**
  337. * Sets the value of a shader texture parameter with the specified name. If the parameter name or type is not
  338. * valid a warning will be logged and output value will not be set.
  339. *
  340. * @param[in] name Name of the shader parameter.
  341. * @param[in] value New value of the parameter.
  342. */
  343. void setTexture(const String& name, const TextureType& value);
  344. /**
  345. * Returns the value of a shader load/store texture parameter with the specified name. If the parameter name or
  346. * type is not valid a warning will be logged and output value will not be retrieved.
  347. *
  348. * @param[in] name Name of the shader parameter.
  349. * @param[out] value Output value of the parameter.
  350. * @param[out] surface Surface describing which part of the texture is being accessed.
  351. */
  352. void getLoadStoreTexture(const String& name, TextureType& value, TextureSurface& surface) const;
  353. /**
  354. * Sets the value of a shader load/store texture parameter with the specified name. If the parameter name or
  355. * type is not valid a warning will be logged and the value will not be set.
  356. *
  357. * @param[in] name Name of the shader parameter.
  358. * @param[in] value New value of the parameter.
  359. * @param[in] surface Surface describing which part of the texture is being accessed.
  360. */
  361. void setLoadStoreTexture(const String& name, const TextureType& value, const TextureSurface& surface);
  362. /**
  363. * Returns the value of a shader buffer parameter with the specified name. If the parameter name or type is not
  364. * valid a warning will be logged and output value will not be retrieved.
  365. *
  366. * @param[in] name Name of the shader parameter.
  367. * @param[out] value Output value of the parameter.
  368. */
  369. void getBuffer(const String& name, BufferType& value) const;
  370. /**
  371. * Sets the value of a shader buffer parameter with the specified name. If the parameter name or type is not
  372. * valid a warning will be logged and output value will not be set.
  373. *
  374. * @param[in] name Name of the shader parameter.
  375. * @param[in] value New value of the parameter.
  376. */
  377. void setBuffer(const String& name, const BufferType& value);
  378. /**
  379. * Sets the value of a shader sampler state parameter with the specified name. If the parameter name or type is not
  380. * valid a warning will be logged and output value will not be set.
  381. *
  382. * @param[in] name Name of the shader parameter.
  383. * @param[out] value Output value of the parameter.
  384. */
  385. void getSamplerState(const String& name, SamplerType& value) const;
  386. /**
  387. * Sets the value of a shader sampler state parameter with the specified name. If the parameter name or type is not
  388. * valid a warning will be logged and output value will not be set.
  389. *
  390. * @param[in] name Name of the shader parameter.
  391. * @param[in] value New value of the parameter.
  392. */
  393. void setSamplerState(const String& name, const SamplerType& value);
  394. /**
  395. * Equivalent to getStructData(const String&, UINT32, void*, UINT32) except it uses the internal parameter index
  396. * directly, avoiding the name lookup. Caller must guarantee the index is valid.
  397. */
  398. void getStructData(UINT32 index, void* value, UINT32 size) const;
  399. /**
  400. * Equivalent to setStructData(const String&, UINT32, void*, UINT32) except it uses the internal parameter index
  401. * directly, avoiding the name lookup. Caller must guarantee the index is valid.
  402. */
  403. void setStructData(UINT32 index, const void* value, UINT32 size);
  404. /**
  405. * Returns a size of a struct parameter in bytes, using the internal parameter index. Caller must guarantee the
  406. * index is valid.
  407. */
  408. UINT32 getStructSize(UINT32 index) const;
  409. /**
  410. * Equivalent to getTexture(const String&, HTexture&) except it uses the internal parameter index directly,
  411. * avoiding the name lookup. Caller must guarantee the index is valid.
  412. */
  413. void getTexture(UINT32 index, TextureType& value) const;
  414. /**
  415. * Equivalent to setTexture(const String&, HTexture&) except it uses the internal parameter index directly,
  416. * avoiding the name lookup. Caller must guarantee the index is valid.
  417. */
  418. void setTexture(UINT32 index, const TextureType& value);
  419. /**
  420. * Equivalent to getBuffer(const String&, SPtr<GpuBuffer>&) except it uses the internal parameter index directly,
  421. * avoiding the name lookup. Caller must guarantee the index is valid.
  422. */
  423. void getBuffer(UINT32 index, BufferType& value) const;
  424. /**
  425. * Equivalent to setBuffer(const String&, SPtr<GpuBuffer>&) except it uses the internal parameter index directly,
  426. * avoiding the name lookup. Caller must guarantee the index is valid.
  427. */
  428. void setBuffer(UINT32 index, const BufferType& value);
  429. /**
  430. * Equivalent to getLoadStoreTexture(const String&, HTexture&, TextureSurface&) except it uses the internal
  431. * parameter index directly, avoiding the name lookup. Caller must guarantee the index is valid.
  432. */
  433. void getLoadStoreTexture(UINT32 index, TextureType& value, TextureSurface& surface) const;
  434. /**
  435. * Equivalent to setLoadStoreTexture(const String&, HTexture&, TextureSurface&) except it uses the internal
  436. * parameter index directly, avoiding the name lookup. Caller must guarantee the index is valid.
  437. */
  438. void setLoadStoreTexture(UINT32 index, const TextureType& value, const TextureSurface& surface);
  439. /**
  440. * Checks is a texture with the specified index a load/store texture or a normal one. Caller must guarantee the
  441. * index is valid.
  442. */
  443. bool getIsTextureLoadStore(UINT32 index) const;
  444. /**
  445. * Equivalent to getSamplerState(const String&, SPtr<SamplerState>&) except it uses the internal parameter index
  446. * directly, avoiding the name lookup. Caller must guarantee the index is valid.
  447. */
  448. void getSamplerState(UINT32 index, SamplerType& value) const;
  449. /**
  450. * Equivalent to setSamplerState(const String&, SPtr<SamplerState>&) except it uses the internal parameter index
  451. * directly, avoiding the name lookup. Caller must guarantee the index is valid.
  452. */
  453. void setSamplerState(UINT32 index, const SamplerType& value);
  454. /**
  455. * Returns the default texture (one assigned when no other is provided), if available for the specified index.
  456. * Index is the internal parameter index and the caller must guarantee the index is valid.
  457. */
  458. void getDefaultTexture(UINT32 index, TextureType& value) const;
  459. /**
  460. * Returns the default sampler state (one assigned when no other is provided), if available for the specified index.
  461. * Index is the internal parameter index and the caller must guarantee the index is valid.
  462. */
  463. void getDefaultSamplerState(UINT32 index, SamplerType& value) const;
  464. protected:
  465. ParamStructDataType* mStructParams = nullptr;
  466. ParamTextureDataType* mTextureParams = nullptr;
  467. ParamBufferDataType* mBufferParams = nullptr;
  468. ParamSamplerStateDataType* mSamplerStateParams = nullptr;
  469. TextureType* mDefaultTextureParams = nullptr;
  470. SamplerType* mDefaultSamplerStateParams = nullptr;
  471. };
  472. class MaterialParams;
  473. /** Core thread version of MaterialParams. */
  474. class BS_CORE_EXPORT MaterialParamsCore : public TMaterialParams<true>
  475. {
  476. public:
  477. /** Initializes the core thread version of MaterialParams from its sim thread counterpart. */
  478. MaterialParamsCore(const SPtr<ShaderCore>& shader, const SPtr<MaterialParams>& params);
  479. /** @copydoc TMaterialParams<Core>::TMaterialParams */
  480. MaterialParamsCore(const SPtr<ShaderCore>& shader);
  481. /**
  482. * Updates the stored parameters from the provided buffer, allowing changes to be transfered between the sim and
  483. * core thread material param objects. Buffer must be retrieved from MaterialParams::getSyncData.
  484. *
  485. * @param[in] buffer Buffer containing the dirty data.
  486. * @param[in, out] size Size of the provided buffer.
  487. */
  488. void setSyncData(UINT8* buffer, UINT32 size);
  489. };
  490. /**
  491. * Contains all parameter values set in a Material. This is similar to GpuParams which also stores parameter values,
  492. * however GpuParams are built for use on the GPU-side and don't store parameters that don't exist in a compiled GPU
  493. * program. This object on the other hand stores all parameters defined in a shader, regardless or not if they actually
  494. * exist in the GPU program. Additionally GpuParams are defined per-program (for example vertex, fragment) while this
  495. * object exists for the entire material.
  496. *
  497. * @note
  498. * This introduces redundancy as parameters stored by GpuParams and this object are duplicated. If this is an issue the
  499. * implementation can be modified to only store parameters not included in GpuParams.
  500. * @note
  501. * The reason why parameters in this class and GpuParams differ is most often compiler optimizations. If a compiler
  502. * optimizes out a variable in a GPU program we should still be able to store it, either for later when the variable
  503. * will be introduced, or for other techniques that might have that variable implemented.
  504. */
  505. class BS_CORE_EXPORT MaterialParams : public IReflectable, public TMaterialParams<false>
  506. {
  507. public:
  508. /** @copydoc TMaterialParams<Core>::TMaterialParams */
  509. MaterialParams(const HShader& shader);
  510. /**
  511. * Populates the provided buffer with parameters that can be used for syncing this object with its core-thread
  512. * counterpart. Can be applied by calling MaterialParamsCore::setSyncData.
  513. *
  514. * @param[in] buffer Pre-allocated buffer to store the sync data in. Set to null to calculate the size
  515. * of the required buffer.
  516. * @param[in, out] size Size of the provided allocated buffer. Or if the buffer is null, this parameter will
  517. * contain the required buffer size when the method executes.
  518. */
  519. void getSyncData(UINT8* buffer, UINT32& size);
  520. private:
  521. friend class MaterialParamsCore;
  522. /************************************************************************/
  523. /* RTTI */
  524. /************************************************************************/
  525. public:
  526. MaterialParams() { } // Only for serialization
  527. friend class MaterialParamsRTTI;
  528. static RTTITypeBase* getRTTIStatic();
  529. RTTITypeBase* getRTTI() const override;
  530. };
  531. /** @} */
  532. }