BsMaterialParams.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsMaterialParams.h"
  4. #include "BsMaterialParamsRTTI.h"
  5. #include "BsShader.h"
  6. namespace BansheeEngine
  7. {
  8. MaterialParams::MaterialParams(const HShader& shader)
  9. {
  10. mDataSize = 0;
  11. auto& dataParams = shader->getDataParams();
  12. for (auto& param : dataParams)
  13. {
  14. UINT32 arraySize = param.second.arraySize > 1 ? param.second.arraySize : 1;
  15. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[(int)param.second.type];
  16. UINT32 paramSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
  17. mDataSize += arraySize * paramSize;
  18. }
  19. auto& textureParams = shader->getTextureParams();
  20. auto& samplerParams = shader->getSamplerParams();
  21. mNumTextureParams = (UINT32)textureParams.size();
  22. mNumSamplerParams = (UINT32)samplerParams.size();
  23. mDataParamsBuffer = mAlloc.alloc(mDataSize);
  24. memset(mDataParamsBuffer, 0, mDataSize);
  25. mStructParams = mAlloc.construct<StructParamData>(mNumStructParams);
  26. mTextureParams = mAlloc.construct<TextureParamData>(mNumTextureParams);
  27. mSamplerStateParams = mAlloc.construct<SamplerStatePtr>(mNumSamplerParams);
  28. mDefaultTextureParams = mAlloc.construct<HTexture>(mNumTextureParams);
  29. mDefaultSamplerStateParams = mAlloc.construct<SamplerStatePtr>(mNumSamplerParams);
  30. UINT32 mDataBufferIdx = 0;
  31. UINT32 mStructIdx = 0;
  32. UINT32 mTextureIdx = 0;
  33. UINT32 mSamplerIdx = 0;
  34. for (auto& entry : dataParams)
  35. {
  36. ParamData& dataParam = mParams[entry.first];
  37. UINT32 arraySize = entry.second.arraySize > 1 ? entry.second.arraySize : 1;
  38. dataParam.arraySize = arraySize;
  39. dataParam.type = ParamType::Data;
  40. dataParam.dataType = entry.second.type;
  41. const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[(int)dataParam.dataType];
  42. UINT32 paramSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
  43. dataParam.index = mDataBufferIdx;
  44. mDataBufferIdx += arraySize * paramSize;
  45. if(entry.second.type == GPDT_STRUCT)
  46. {
  47. for (UINT32 i = 0; i < arraySize; i++)
  48. {
  49. StructParamData& param = mStructParams[mStructIdx];
  50. param.dataSize = entry.second.elementSize;
  51. param.data = mAlloc.alloc(param.dataSize);
  52. dataParam.index = mStructIdx;
  53. mStructIdx++;
  54. }
  55. }
  56. }
  57. for (auto& entry : textureParams)
  58. {
  59. ParamData& dataParam = mParams[entry.first];
  60. dataParam.arraySize = 1;
  61. dataParam.type = ParamType::Texture;
  62. dataParam.dataType = GPDT_UNKNOWN;
  63. dataParam.index = mTextureIdx;
  64. TextureParamData& param = mTextureParams[mTextureIdx];
  65. param.isLoadStore = false;
  66. if (entry.second.defaultValueIdx != (UINT32)-1)
  67. mDefaultTextureParams[mTextureIdx] = shader->getDefaultTexture(entry.second.defaultValueIdx);
  68. mTextureIdx++;
  69. }
  70. for (auto& entry : samplerParams)
  71. {
  72. ParamData& dataParam = mParams[entry.first];
  73. dataParam.arraySize = 1;
  74. dataParam.type = ParamType::Sampler;
  75. dataParam.dataType = GPDT_UNKNOWN;
  76. dataParam.index = mSamplerIdx;
  77. if (entry.second.defaultValueIdx != (UINT32)-1)
  78. mDefaultSamplerStateParams[mTextureIdx] = shader->getDefaultSampler(entry.second.defaultValueIdx);
  79. mSamplerIdx++;
  80. }
  81. }
  82. MaterialParams::~MaterialParams()
  83. {
  84. if (mStructParams != nullptr)
  85. {
  86. for (UINT32 i = 0; mNumStructParams; i++)
  87. mAlloc.free(mStructParams[i].data);
  88. }
  89. mAlloc.free(mDataParamsBuffer);
  90. mAlloc.destruct(mStructParams, mNumStructParams);
  91. mAlloc.destruct(mTextureParams, mNumTextureParams);
  92. mAlloc.destruct(mSamplerStateParams, mNumSamplerParams);
  93. if(mDefaultTextureParams != nullptr)
  94. mAlloc.destruct(mDefaultTextureParams, mNumTextureParams);
  95. if (mDefaultSamplerStateParams != nullptr)
  96. mAlloc.destruct(mDefaultSamplerStateParams, mNumSamplerParams);
  97. mAlloc.clear();
  98. }
  99. void MaterialParams::getStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx) const
  100. {
  101. const ParamData* param = nullptr;
  102. GetParamResult result = getParamData(name, ParamType::Data, GPDT_STRUCT, arrayIdx, &param);
  103. if (result != GetParamResult::Success)
  104. {
  105. reportGetParamError(result, name, arrayIdx);
  106. return;
  107. }
  108. getStructData(param->index + arrayIdx, value, size);
  109. }
  110. void MaterialParams::setStructData(const String& name, const void* value, UINT32 size, UINT32 arrayIdx)
  111. {
  112. const ParamData* param = nullptr;
  113. GetParamResult result = getParamData(name, ParamType::Data, GPDT_STRUCT, arrayIdx, &param);
  114. if (result != GetParamResult::Success)
  115. {
  116. reportGetParamError(result, name, arrayIdx);
  117. return;
  118. }
  119. setStructData(param->index + arrayIdx, value, size);
  120. }
  121. void MaterialParams::getTexture(const String& name, HTexture& value) const
  122. {
  123. const ParamData* param = nullptr;
  124. GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, &param);
  125. if (result != GetParamResult::Success)
  126. {
  127. reportGetParamError(result, name, 0);
  128. return;
  129. }
  130. getTexture(param->index, value);
  131. }
  132. void MaterialParams::setTexture(const String& name, const HTexture& value)
  133. {
  134. const ParamData* param = nullptr;
  135. GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, &param);
  136. if (result != GetParamResult::Success)
  137. {
  138. reportGetParamError(result, name, 0);
  139. return;
  140. }
  141. setTexture(param->index, value);
  142. }
  143. void MaterialParams::getLoadStoreTexture(const String& name, HTexture& value, TextureSurface& surface) const
  144. {
  145. const ParamData* param = nullptr;
  146. GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, &param);
  147. if (result != GetParamResult::Success)
  148. {
  149. reportGetParamError(result, name, 0);
  150. return;
  151. }
  152. getLoadStoreTexture(param->index, value, surface);
  153. }
  154. void MaterialParams::setLoadStoreTexture(const String& name, const HTexture& value, const TextureSurface& surface)
  155. {
  156. const ParamData* param = nullptr;
  157. GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, &param);
  158. if (result != GetParamResult::Success)
  159. {
  160. reportGetParamError(result, name, 0);
  161. return;
  162. }
  163. setLoadStoreTexture(param->index, value, surface);
  164. }
  165. void MaterialParams::getSamplerState(const String& name, SamplerStatePtr& value) const
  166. {
  167. const ParamData* param = nullptr;
  168. GetParamResult result = getParamData(name, ParamType::Sampler, GPDT_UNKNOWN, 0, &param);
  169. if (result != GetParamResult::Success)
  170. {
  171. reportGetParamError(result, name, 0);
  172. return;
  173. }
  174. getSamplerState(param->index, value);
  175. }
  176. void MaterialParams::setSamplerState(const String& name, const SamplerStatePtr& value)
  177. {
  178. const ParamData* param = nullptr;
  179. GetParamResult result = getParamData(name, ParamType::Sampler, GPDT_UNKNOWN, 0, &param);
  180. if(result != GetParamResult::Success)
  181. {
  182. reportGetParamError(result, name, 0);
  183. return;
  184. }
  185. setSamplerState(param->index, value);
  186. }
  187. MaterialParams::GetParamResult MaterialParams::getParamData(const String& name, ParamType type, GpuParamDataType dataType,
  188. UINT32 arrayIdx, const ParamData** output) const
  189. {
  190. auto iterFind = mParams.find(name);
  191. if (iterFind == mParams.end())
  192. return GetParamResult::NotFound;
  193. const ParamData& param = iterFind->second;
  194. *output = &param;
  195. if (param.type != type || (type == ParamType::Data && param.dataType != dataType))
  196. return GetParamResult::InvalidType;
  197. if (arrayIdx >= param.arraySize)
  198. return GetParamResult::IndexOutOfBounds;
  199. return GetParamResult::Success;
  200. }
  201. void MaterialParams::reportGetParamError(GetParamResult errorCode, const String& name, UINT32 arrayIdx) const
  202. {
  203. switch(errorCode)
  204. {
  205. case GetParamResult::NotFound:
  206. LOGWRN("Material doesn't have a parameter named " + name + ".");
  207. break;
  208. case GetParamResult::InvalidType:
  209. LOGWRN("Parameter \"" + name + "\" is not of the requested type.");
  210. break;
  211. case GetParamResult::IndexOutOfBounds:
  212. LOGWRN("Parameter \"" + name + "\" array index " + toString(arrayIdx) + " out of range.");
  213. break;
  214. }
  215. }
  216. void MaterialParams::getStructData(UINT32 index, void* value, UINT32 size) const
  217. {
  218. const StructParamData& structParam = mStructParams[index];
  219. if (structParam.dataSize != size)
  220. {
  221. LOGWRN("Size mismatch when writing to a struct. Provided size was " + toString(size) + " bytes but the "
  222. "struct size is" + toString(structParam.dataSize) + " bytes");
  223. return;
  224. }
  225. memcpy(value, structParam.data, structParam.dataSize);
  226. }
  227. void MaterialParams::setStructData(UINT32 index, const void* value, UINT32 size)
  228. {
  229. const StructParamData& structParam = mStructParams[index];
  230. if (structParam.dataSize != size)
  231. {
  232. LOGWRN("Size mismatch when writing to a struct. Provided size was " + toString(size) + " bytes but the "
  233. "struct size is" + toString(structParam.dataSize) + " bytes");
  234. return;
  235. }
  236. memcpy(structParam.data, value, structParam.dataSize);
  237. }
  238. UINT32 MaterialParams::getStructSize(UINT32 index) const
  239. {
  240. const StructParamData& structParam = mStructParams[index];
  241. return structParam.dataSize;
  242. }
  243. void MaterialParams::getTexture(UINT32 index, HTexture& value) const
  244. {
  245. TextureParamData& textureParam = mTextureParams[index];
  246. value = textureParam.value;
  247. }
  248. void MaterialParams::setTexture(UINT32 index, const HTexture& value)
  249. {
  250. TextureParamData& textureParam = mTextureParams[index];
  251. textureParam.value = value;
  252. textureParam.isLoadStore = false;
  253. }
  254. void MaterialParams::getLoadStoreTexture(UINT32 index, HTexture& value, TextureSurface& surface) const
  255. {
  256. TextureParamData& textureParam = mTextureParams[index];
  257. value = textureParam.value;
  258. surface = textureParam.surface;
  259. }
  260. void MaterialParams::setLoadStoreTexture(UINT32 index, const HTexture& value, const TextureSurface& surface)
  261. {
  262. TextureParamData& textureParam = mTextureParams[index];
  263. textureParam.value = value;
  264. textureParam.isLoadStore = true;
  265. textureParam.surface = surface;
  266. }
  267. bool MaterialParams::getIsTextureLoadStore(UINT32 index) const
  268. {
  269. return mTextureParams[index].isLoadStore;
  270. }
  271. void MaterialParams::getSamplerState(UINT32 index, SamplerStatePtr& value) const
  272. {
  273. value = mSamplerStateParams[index];
  274. }
  275. void MaterialParams::setSamplerState(UINT32 index, const SamplerStatePtr& value)
  276. {
  277. mSamplerStateParams[index] = value;
  278. }
  279. void MaterialParams::getDefaultTexture(UINT32 index, HTexture& value) const
  280. {
  281. value = mDefaultTextureParams[index];
  282. }
  283. void MaterialParams::getDefaultSamplerState(UINT32 index, SamplerStatePtr& value) const
  284. {
  285. value = mDefaultSamplerStateParams[index];
  286. }
  287. RTTITypeBase* MaterialParams::TextureParamData::getRTTIStatic()
  288. {
  289. return TextureParamDataRTTI::instance();
  290. }
  291. RTTITypeBase* MaterialParams::TextureParamData::getRTTI() const
  292. {
  293. return getRTTIStatic();
  294. }
  295. RTTITypeBase* MaterialParams::StructParamData::getRTTIStatic()
  296. {
  297. return StructParamDataRTTI::instance();
  298. }
  299. RTTITypeBase* MaterialParams::StructParamData::getRTTI() const
  300. {
  301. return getRTTIStatic();
  302. }
  303. RTTITypeBase* MaterialParams::getRTTIStatic()
  304. {
  305. return MaterialParamsRTTI::instance();
  306. }
  307. RTTITypeBase* MaterialParams::getRTTI() const
  308. {
  309. return MaterialParams::getRTTIStatic();
  310. }
  311. }