BsMaterialParams.cpp 10 KB

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