BsMaterialParams.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "BsMaterialParams.h"
  2. #include "BsMaterialParamsRTTI.h"
  3. #include "BsProfilerCPU.h"
  4. #include "BsShader.h"
  5. namespace BansheeEngine
  6. {
  7. MaterialParams::MaterialParams(const HShader& shader)
  8. {
  9. gProfilerCPU().beginSample("Create material params");
  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. gProfilerCPU().endSample("Create material params");
  82. }
  83. MaterialParams::~MaterialParams()
  84. {
  85. if (mStructParams != nullptr)
  86. {
  87. for (UINT32 i = 0; mNumStructParams; i++)
  88. mAlloc.free(mStructParams[i].data);
  89. }
  90. mAlloc.free(mDataParamsBuffer);
  91. mAlloc.destruct(mStructParams, mNumStructParams);
  92. mAlloc.destruct(mTextureParams, mNumTextureParams);
  93. mAlloc.destruct(mSamplerStateParams, mNumSamplerParams);
  94. if(mDefaultTextureParams != nullptr)
  95. mAlloc.destruct(mDefaultTextureParams, mNumTextureParams);
  96. if (mDefaultSamplerStateParams != nullptr)
  97. mAlloc.destruct(mDefaultSamplerStateParams, mNumSamplerParams);
  98. mAlloc.clear();
  99. }
  100. void MaterialParams::getStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx) const
  101. {
  102. const ParamData* param = nullptr;
  103. GetParamResult result = getParamData(name, ParamType::Data, GPDT_STRUCT, arrayIdx, &param);
  104. if (result != GetParamResult::Success)
  105. {
  106. reportGetParamError(result, name, arrayIdx);
  107. return;
  108. }
  109. getStructData(param->index + arrayIdx, value, size);
  110. }
  111. void MaterialParams::setStructData(const String& name, const void* value, UINT32 size, UINT32 arrayIdx)
  112. {
  113. const ParamData* param = nullptr;
  114. GetParamResult result = getParamData(name, ParamType::Data, GPDT_STRUCT, arrayIdx, &param);
  115. if (result != GetParamResult::Success)
  116. {
  117. reportGetParamError(result, name, arrayIdx);
  118. return;
  119. }
  120. setStructData(param->index + arrayIdx, value, size);
  121. }
  122. void MaterialParams::getTexture(const String& name, HTexture& value) const
  123. {
  124. const ParamData* param = nullptr;
  125. GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, &param);
  126. if (result != GetParamResult::Success)
  127. {
  128. reportGetParamError(result, name, 0);
  129. return;
  130. }
  131. getTexture(param->index, value);
  132. }
  133. void MaterialParams::setTexture(const String& name, const HTexture& value)
  134. {
  135. const ParamData* param = nullptr;
  136. GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, &param);
  137. if (result != GetParamResult::Success)
  138. {
  139. reportGetParamError(result, name, 0);
  140. return;
  141. }
  142. setTexture(param->index, value);
  143. }
  144. void MaterialParams::getLoadStoreTexture(const String& name, HTexture& value, TextureSurface& surface) const
  145. {
  146. const ParamData* param = nullptr;
  147. GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, &param);
  148. if (result != GetParamResult::Success)
  149. {
  150. reportGetParamError(result, name, 0);
  151. return;
  152. }
  153. getLoadStoreTexture(param->index, value, surface);
  154. }
  155. void MaterialParams::setLoadStoreTexture(const String& name, const HTexture& value, const TextureSurface& surface)
  156. {
  157. const ParamData* param = nullptr;
  158. GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, &param);
  159. if (result != GetParamResult::Success)
  160. {
  161. reportGetParamError(result, name, 0);
  162. return;
  163. }
  164. setLoadStoreTexture(param->index, value, surface);
  165. }
  166. void MaterialParams::getSamplerState(const String& name, SamplerStatePtr& value) const
  167. {
  168. const ParamData* param = nullptr;
  169. GetParamResult result = getParamData(name, ParamType::Sampler, GPDT_UNKNOWN, 0, &param);
  170. if (result != GetParamResult::Success)
  171. {
  172. reportGetParamError(result, name, 0);
  173. return;
  174. }
  175. getSamplerState(param->index, value);
  176. }
  177. void MaterialParams::setSamplerState(const String& name, const SamplerStatePtr& value)
  178. {
  179. const ParamData* param = nullptr;
  180. GetParamResult result = getParamData(name, ParamType::Sampler, GPDT_UNKNOWN, 0, &param);
  181. if(result != GetParamResult::Success)
  182. {
  183. reportGetParamError(result, name, 0);
  184. return;
  185. }
  186. setSamplerState(param->index, value);
  187. }
  188. MaterialParams::GetParamResult MaterialParams::getParamData(const String& name, ParamType type, GpuParamDataType dataType,
  189. UINT32 arrayIdx, const ParamData** output) const
  190. {
  191. auto iterFind = mParams.find(name);
  192. if (iterFind == mParams.end())
  193. return GetParamResult::NotFound;
  194. const ParamData& param = iterFind->second;
  195. *output = &param;
  196. if (param.type != type || (type == ParamType::Data && param.dataType != dataType))
  197. return GetParamResult::InvalidType;
  198. if (arrayIdx >= param.arraySize)
  199. return GetParamResult::IndexOutOfBounds;
  200. return GetParamResult::Success;
  201. }
  202. void MaterialParams::reportGetParamError(GetParamResult errorCode, const String& name, UINT32 arrayIdx) const
  203. {
  204. switch(errorCode)
  205. {
  206. case GetParamResult::NotFound:
  207. LOGWRN("Material doesn't have a parameter named " + name + ".");
  208. break;
  209. case GetParamResult::InvalidType:
  210. LOGWRN("Parameter \"" + name + "\" is not of the requested type.");
  211. break;
  212. case GetParamResult::IndexOutOfBounds:
  213. LOGWRN("Parameter \"" + name + "\" array index " + toString(arrayIdx) + " out of range.");
  214. break;
  215. }
  216. }
  217. void MaterialParams::getStructData(UINT32 index, void* value, UINT32 size) const
  218. {
  219. const StructParamData& structParam = mStructParams[index];
  220. if (structParam.dataSize != size)
  221. {
  222. LOGWRN("Size mismatch when writing to a struct. Provided size was " + toString(size) + " bytes but the "
  223. "struct size is" + toString(structParam.dataSize) + " bytes");
  224. return;
  225. }
  226. memcpy(value, structParam.data, structParam.dataSize);
  227. }
  228. void MaterialParams::setStructData(UINT32 index, const void* value, UINT32 size)
  229. {
  230. const StructParamData& structParam = mStructParams[index];
  231. if (structParam.dataSize != size)
  232. {
  233. LOGWRN("Size mismatch when writing to a struct. Provided size was " + toString(size) + " bytes but the "
  234. "struct size is" + toString(structParam.dataSize) + " bytes");
  235. return;
  236. }
  237. memcpy(structParam.data, value, structParam.dataSize);
  238. }
  239. UINT32 MaterialParams::getStructSize(UINT32 index) const
  240. {
  241. const StructParamData& structParam = mStructParams[index];
  242. return structParam.dataSize;
  243. }
  244. void MaterialParams::getTexture(UINT32 index, HTexture& value) const
  245. {
  246. TextureParamData& textureParam = mTextureParams[index];
  247. value = textureParam.value;
  248. }
  249. void MaterialParams::setTexture(UINT32 index, const HTexture& value)
  250. {
  251. TextureParamData& textureParam = mTextureParams[index];
  252. textureParam.value = value;
  253. textureParam.isLoadStore = false;
  254. }
  255. void MaterialParams::getLoadStoreTexture(UINT32 index, HTexture& value, TextureSurface& surface) const
  256. {
  257. TextureParamData& textureParam = mTextureParams[index];
  258. value = textureParam.value;
  259. surface = textureParam.surface;
  260. }
  261. void MaterialParams::setLoadStoreTexture(UINT32 index, const HTexture& value, const TextureSurface& surface)
  262. {
  263. TextureParamData& textureParam = mTextureParams[index];
  264. textureParam.value = value;
  265. textureParam.isLoadStore = true;
  266. textureParam.surface = surface;
  267. }
  268. bool MaterialParams::getIsTextureLoadStore(UINT32 index) const
  269. {
  270. return mTextureParams[index].isLoadStore;
  271. }
  272. void MaterialParams::getSamplerState(UINT32 index, SamplerStatePtr& value) const
  273. {
  274. value = mSamplerStateParams[index];
  275. }
  276. void MaterialParams::setSamplerState(UINT32 index, const SamplerStatePtr& value)
  277. {
  278. mSamplerStateParams[index] = value;
  279. }
  280. void MaterialParams::getDefaultTexture(UINT32 index, HTexture& value) const
  281. {
  282. value = mDefaultTextureParams[index];
  283. }
  284. void MaterialParams::getDefaultSamplerState(UINT32 index, SamplerStatePtr& value) const
  285. {
  286. value = mDefaultSamplerStateParams[index];
  287. }
  288. RTTITypeBase* MaterialParams::TextureParamData::getRTTIStatic()
  289. {
  290. return TextureParamDataRTTI::instance();
  291. }
  292. RTTITypeBase* MaterialParams::TextureParamData::getRTTI() const
  293. {
  294. return getRTTIStatic();
  295. }
  296. RTTITypeBase* MaterialParams::StructParamData::getRTTIStatic()
  297. {
  298. return StructParamDataRTTI::instance();
  299. }
  300. RTTITypeBase* MaterialParams::StructParamData::getRTTI() const
  301. {
  302. return getRTTIStatic();
  303. }
  304. RTTITypeBase* MaterialParams::getRTTIStatic()
  305. {
  306. return MaterialParamsRTTI::instance();
  307. }
  308. RTTITypeBase* MaterialParams::getRTTI() const
  309. {
  310. return MaterialParams::getRTTIStatic();
  311. }
  312. }