BsMaterialParams.cpp 12 KB

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