BsMaterialParams.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 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. */
  335. void getTexture(const String& name, TextureType& value, TextureSurface& surface) 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. const TextureSurface& surface = TextureSurface::COMPLETE);
  345. /**
  346. * Returns the value of a shader load/store texture parameter with the specified name. If the parameter name or
  347. * type is not valid a warning will be logged and output value will not be retrieved.
  348. *
  349. * @param[in] name Name of the shader parameter.
  350. * @param[out] value Output value of the parameter.
  351. * @param[out] surface Surface describing which part of the texture is being accessed.
  352. */
  353. void getLoadStoreTexture(const String& name, TextureType& value, TextureSurface& surface) const;
  354. /**
  355. * Sets the value of a shader load/store texture parameter with the specified name. If the parameter name or
  356. * type is not valid a warning will be logged and the value will not be set.
  357. *
  358. * @param[in] name Name of the shader parameter.
  359. * @param[in] value New value of the parameter.
  360. * @param[in] surface Surface describing which part of the texture is being accessed.
  361. */
  362. void setLoadStoreTexture(const String& name, const TextureType& value, const TextureSurface& surface);
  363. /**
  364. * Returns the value of a shader buffer parameter with the specified name. If the parameter name or type is not
  365. * valid a warning will be logged and output value will not be retrieved.
  366. *
  367. * @param[in] name Name of the shader parameter.
  368. * @param[out] value Output value of the parameter.
  369. */
  370. void getBuffer(const String& name, BufferType& value) const;
  371. /**
  372. * Sets the value of a shader buffer parameter with the specified name. If the parameter name or type is not
  373. * valid a warning will be logged and output value will not be set.
  374. *
  375. * @param[in] name Name of the shader parameter.
  376. * @param[in] value New value of the parameter.
  377. */
  378. void setBuffer(const String& name, const BufferType& value);
  379. /**
  380. * Sets the value of a shader sampler state parameter with the specified name. If the parameter name or type is not
  381. * valid a warning will be logged and output value will not be set.
  382. *
  383. * @param[in] name Name of the shader parameter.
  384. * @param[out] value Output value of the parameter.
  385. */
  386. void getSamplerState(const String& name, SamplerType& value) const;
  387. /**
  388. * Sets the value of a shader sampler state parameter with the specified name. If the parameter name or type is not
  389. * valid a warning will be logged and output value will not be set.
  390. *
  391. * @param[in] name Name of the shader parameter.
  392. * @param[in] value New value of the parameter.
  393. */
  394. void setSamplerState(const String& name, const SamplerType& value);
  395. /**
  396. * Equivalent to getStructData(const String&, UINT32, void*, UINT32) except it uses the internal parameter reference
  397. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  398. * object.
  399. */
  400. void getStructData(const ParamData& param, void* value, UINT32 size, UINT32 arrayIdx) const;
  401. /**
  402. * Equivalent to setStructData(const String&, UINT32, void*, UINT32) except it uses the internal parameter reference
  403. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  404. * object.
  405. */
  406. void setStructData(const ParamData& param, const void* value, UINT32 size, UINT32 arrayIdx);
  407. /**
  408. * Returns a size of a struct parameter in bytes, using the internal parameter reference. Caller must guarantee the
  409. * parameter reference is valid and belongs to this object.
  410. */
  411. UINT32 getStructSize(const ParamData& param) const;
  412. /**
  413. * Equivalent to getTexture(const String&, HTexture&) except it uses the internal parameter reference directly,
  414. * avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this object.
  415. */
  416. void getTexture(const ParamData& param, TextureType& value, TextureSurface& surface) const;
  417. /**
  418. * Equivalent to setTexture(const String&, HTexture&) except it uses the internal parameter reference directly,
  419. * avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this object.
  420. */
  421. void setTexture(const ParamData& param, const TextureType& value,
  422. const TextureSurface& surface = TextureSurface::COMPLETE);
  423. /**
  424. * Equivalent to getBuffer(const String&, SPtr<GpuBuffer>&) except it uses the internal parameter reference
  425. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  426. * object.
  427. */
  428. void getBuffer(const ParamData& param, BufferType& value) const;
  429. /**
  430. * Equivalent to setBuffer(const String&, SPtr<GpuBuffer>&) except it uses the internal parameter reference
  431. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  432. * object.
  433. */
  434. void setBuffer(const ParamData& param, const BufferType& value);
  435. /**
  436. * Equivalent to getLoadStoreTexture(const String&, HTexture&, TextureSurface&) except it uses the internal
  437. * parameter reference directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid
  438. * and belongs to this object.
  439. */
  440. void getLoadStoreTexture(const ParamData& param, TextureType& value, TextureSurface& surface) const;
  441. /**
  442. * Equivalent to setLoadStoreTexture(const String&, HTexture&, TextureSurface&) except it uses the internal
  443. * parameter reference directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid
  444. * and belongs to this object.
  445. */
  446. void setLoadStoreTexture(const ParamData& param, const TextureType& value, const TextureSurface& surface);
  447. /**
  448. * Checks is a texture with the specified parameter reference a load/store texture or a normal one. Caller must
  449. * guarantee the parameter reference is valid and belongs to this object.
  450. */
  451. bool getIsTextureLoadStore(const ParamData& param) const;
  452. /**
  453. * Equivalent to getSamplerState(const String&, SPtr<SamplerState>&) except it uses the internal parameter reference
  454. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  455. * object.
  456. */
  457. void getSamplerState(const ParamData& param, SamplerType& value) const;
  458. /**
  459. * Equivalent to setSamplerState(const String&, SPtr<SamplerState>&) except it uses the internal parameter reference
  460. * directly, avoiding the name lookup. Caller must guarantee the parameter reference is valid and belongs to this
  461. * object.
  462. */
  463. void setSamplerState(const ParamData& param, const SamplerType& value);
  464. /**
  465. * Returns the default texture (one assigned when no other is provided), if available for the specified parameter.
  466. * Parameter is represented using the internal parameter reference and the caller must guarantee the parameter
  467. * eference is valid and belongs to this object.
  468. */
  469. void getDefaultTexture(const ParamData& param, TextureType& value) const;
  470. /**
  471. * Returns the default sampler state (one assigned when no other is provided), if available for the specified
  472. * parameter. Parameter is represented using the internal parameter reference and the caller must guarantee the
  473. * parameter reference is valid and belongs to this object.
  474. */
  475. void getDefaultSamplerState(const ParamData& param, SamplerType& value) const;
  476. protected:
  477. ParamStructDataType* mStructParams = nullptr;
  478. ParamTextureDataType* mTextureParams = nullptr;
  479. ParamBufferDataType* mBufferParams = nullptr;
  480. ParamSamplerStateDataType* mSamplerStateParams = nullptr;
  481. TextureType* mDefaultTextureParams = nullptr;
  482. SamplerType* mDefaultSamplerStateParams = nullptr;
  483. };
  484. /**
  485. * Contains all parameter values set in a Material. This is similar to GpuParams which also stores parameter values,
  486. * however GpuParams are built for use on the GPU-side and don't store parameters that don't exist in a compiled GPU
  487. * program. This object on the other hand stores all parameters defined in a shader, regardless or not if they actually
  488. * exist in the GPU program. Additionally GpuParams are defined per-program (for example vertex, fragment) while this
  489. * object exists for the entire material.
  490. *
  491. * @note
  492. * This introduces redundancy as parameters stored by GpuParams and this object are duplicated. If this is an issue the
  493. * implementation can be modified to only store parameters not included in GpuParams.
  494. * @note
  495. * The reason why parameters in this class and GpuParams differ is most often compiler optimizations. If a compiler
  496. * optimizes out a variable in a GPU program we should still be able to store it, either for later when the variable
  497. * will be introduced, or for other techniques that might have that variable implemented.
  498. */
  499. class BS_CORE_EXPORT MaterialParams : public IReflectable, public TMaterialParams<false>
  500. {
  501. public:
  502. /** @copydoc TMaterialParams::TMaterialParams(const ShaderType&) */
  503. MaterialParams(const HShader& shader);
  504. /**
  505. * Populates the provided buffer with parameters that can be used for syncing this object with its core-thread
  506. * counterpart. Can be applied by calling ct::MaterialParams::setSyncData.
  507. *
  508. * @param[in] buffer Pre-allocated buffer to store the sync data in. Set to null to calculate the size
  509. * of the required buffer.
  510. * @param[in, out] size Size of the provided allocated buffer. Or if the buffer is null, this parameter will
  511. * contain the required buffer size when the method executes.
  512. */
  513. void getSyncData(UINT8* buffer, UINT32& size);
  514. private:
  515. friend class ct::MaterialParams;
  516. UINT64 mLastSyncVersion;
  517. /************************************************************************/
  518. /* RTTI */
  519. /************************************************************************/
  520. public:
  521. MaterialParams() { } // Only for serialization
  522. friend class MaterialParamsRTTI;
  523. static RTTITypeBase* getRTTIStatic();
  524. RTTITypeBase* getRTTI() const override;
  525. };
  526. namespace ct
  527. {
  528. /** Core thread version of MaterialParams. */
  529. class BS_CORE_EXPORT MaterialParams : public TMaterialParams<true>
  530. {
  531. public:
  532. /** Initializes the core thread version of MaterialParams from its sim thread counterpart. */
  533. MaterialParams(const SPtr<Shader>& shader, const SPtr<bs::MaterialParams>& params);
  534. /** @copydoc TMaterialParams::TMaterialParams(const ShaderType&) */
  535. MaterialParams(const SPtr<Shader>& shader);
  536. /**
  537. * Updates the stored parameters from the provided buffer, allowing changes to be transfered between the sim and
  538. * core thread material param objects. Buffer must be retrieved from bs::MaterialParams::getSyncData.
  539. *
  540. * @param[in] buffer Buffer containing the dirty data.
  541. * @param[in, out] size Size of the provided buffer.
  542. */
  543. void setSyncData(UINT8* buffer, UINT32 size);
  544. };
  545. }
  546. /** @} */
  547. }