BsMaterialParams.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include "BsMaterialParams.h"
  2. namespace BansheeEngine
  3. {
  4. __MaterialParams::__MaterialParams(const HShader& shader)
  5. {
  6. GpuParamDataType dataTypes[] = { GPDT_FLOAT1, GPDT_FLOAT2, GPDT_FLOAT3, GPDT_FLOAT4, GPDT_INT1, GPDT_INT2, GPDT_INT3,
  7. GPDT_INT4, GPDT_MATRIX_2X2, GPDT_MATRIX_2X3, GPDT_MATRIX_2X4, GPDT_MATRIX_3X3, GPDT_MATRIX_3X2, GPDT_MATRIX_3X4,
  8. GPDT_MATRIX_4X4, GPDT_MATRIX_4X2, GPDT_MATRIX_4X3, GPDT_COLOR, GPDT_STRUCT };
  9. UINT32* dataCounts[] = { &mNumFloatParams, &mNumVec2Params, &mNumVec3Params, &mNumVec4Params, &mNumIntParams,
  10. &mNumVec2IParams, &mNumVec3IParams, &mNumVec4IParams, &mNumMat2Params, &mNumMat2x3Params, &mNumMat2x4Params,
  11. &mNumMat3Params, &mNumMat3x2Params, &mNumMat3x4Params, &mNumMat4Params, &mNumMat4x2Params, &mNumMat4x3Params,
  12. &mNumColorParams, &mNumStructParams };
  13. auto& dataParams = shader->getDataParams();
  14. for (auto& param : dataParams)
  15. {
  16. for (UINT32 i = 0; i < sizeof(dataTypes); i++)
  17. {
  18. if (param.second.type == dataTypes[i])
  19. {
  20. UINT32 count = param.second.arraySize > 1 ? param.second.arraySize : 1;
  21. *dataCounts[i] += count;
  22. break;
  23. }
  24. }
  25. }
  26. auto& textureParams = shader->getTextureParams();
  27. auto& samplerParams = shader->getSamplerParams();
  28. mNumTextureParams = (UINT32)textureParams.size();
  29. mNumSamplerParams = (UINT32)samplerParams.size();
  30. mFloatParams = mAlloc.construct<float>(mNumFloatParams);
  31. mVec2Params = mAlloc.construct<Vector2>(mNumVec2Params);
  32. mVec3Params = mAlloc.construct<Vector3>(mNumVec3Params);
  33. mVec4Params = mAlloc.construct<Vector4>(mNumVec4Params);
  34. mIntParams = mAlloc.construct<int>(mNumIntParams);
  35. mVec2IParams = mAlloc.construct<Vector2I>(mNumVec2IParams);
  36. mVec3IParams = mAlloc.construct<Vector3I>(mNumVec3IParams);
  37. mVec4IParams = mAlloc.construct<Vector4I>(mNumVec4IParams);
  38. mMat2Params = mAlloc.construct<Matrix2>(mNumMat2Params);
  39. mMat2x3Params = mAlloc.construct<Matrix2x3>(mNumMat2x3Params);
  40. mMat2x4Params = mAlloc.construct<Matrix2x4>(mNumMat2x4Params);
  41. mMat3Params = mAlloc.construct<Matrix3>(mNumMat3Params);
  42. mMat3x2Params = mAlloc.construct<Matrix3x2>(mNumMat3x2Params);
  43. mMat3x4Params = mAlloc.construct<Matrix3x4>(mNumMat3x4Params);
  44. mMat4Params = mAlloc.construct<Matrix4>(mNumMat4Params);
  45. mMat4x2Params = mAlloc.construct<Matrix4x2>(mNumMat4x2Params);
  46. mMat4x3Params = mAlloc.construct<Matrix4x3>(mNumMat4x3Params);
  47. mColorParams = mAlloc.construct<Color>(mNumColorParams);
  48. mStructParams = mAlloc.construct<StructParamData>(mNumStructParams);
  49. mTextureParams = mAlloc.construct<TextureParamData>(mNumTextureParams);
  50. mSamplerStateParams = mAlloc.construct<SamplerStatePtr>(mNumSamplerParams);
  51. UINT32 mFloatIdx = 0;
  52. UINT32 mVec2Idx = 0;
  53. UINT32 mVec3Idx = 0;
  54. UINT32 mVec4Idx = 0;
  55. UINT32 mIntIdx = 0;
  56. UINT32 mVec2IIdx = 0;
  57. UINT32 mVec3IIdx = 0;
  58. UINT32 mVec4IIdx = 0;
  59. UINT32 mMat2Idx = 0;
  60. UINT32 mMat2x3Idx = 0;
  61. UINT32 mMat2x4Idx = 0;
  62. UINT32 mMat3Idx = 0;
  63. UINT32 mMat3x2Idx = 0;
  64. UINT32 mMat3x4Idx = 0;
  65. UINT32 mMat4Idx = 0;
  66. UINT32 mMat4x2Idx = 0;
  67. UINT32 mMat4x3Idx = 0;
  68. UINT32 mColorIdx = 0;
  69. UINT32 mStructIdx = 0;
  70. UINT32 mTextureIdx = 0;
  71. UINT32 mSamplerIdx = 0;
  72. for (auto& entry : dataParams)
  73. {
  74. ParamData& dataParam = mParams[entry.first];
  75. UINT32 arraySize = entry.second.arraySize > 1 ? entry.second.arraySize : 1;
  76. dataParam.arraySize = arraySize;
  77. dataParam.type = ParamType::Data;
  78. dataParam.dataType = entry.second.type;
  79. dataParam.index = 0;
  80. switch (entry.second.type)
  81. {
  82. case GPDT_FLOAT1:
  83. for (UINT32 i = 0; i < arraySize; i++)
  84. mFloatParams[mFloatIdx + i] = 0.0f;
  85. dataParam.index = mFloatIdx;
  86. mFloatIdx += arraySize;
  87. break;
  88. case GPDT_FLOAT2:
  89. dataParam.index = mVec2Idx;
  90. mVec2Idx += arraySize;
  91. break;
  92. case GPDT_FLOAT3:
  93. dataParam.index = mVec3Idx;
  94. mVec3Idx += arraySize;
  95. break;
  96. case GPDT_FLOAT4:
  97. dataParam.index = mVec4Idx;
  98. mVec4Idx += arraySize;
  99. break;
  100. case GPDT_INT1:
  101. dataParam.index = mIntIdx;
  102. mIntIdx += arraySize;
  103. break;
  104. case GPDT_INT2:
  105. dataParam.index = mVec2IIdx;
  106. mVec2IIdx += arraySize;
  107. break;
  108. case GPDT_INT3:
  109. dataParam.index = mVec3IIdx;
  110. mVec3IIdx += arraySize;
  111. break;
  112. case GPDT_INT4:
  113. dataParam.index = mVec4IIdx;
  114. mVec4IIdx += arraySize;
  115. break;
  116. case GPDT_MATRIX_2X2:
  117. dataParam.index = mMat2Idx;
  118. mMat2Idx += arraySize;
  119. break;
  120. case GPDT_MATRIX_2X3:
  121. dataParam.index = mMat2x3Idx;
  122. mMat2x3Idx += arraySize;
  123. break;
  124. case GPDT_MATRIX_2X4:
  125. dataParam.index = mMat2x4Idx;
  126. mMat2x4Idx += arraySize;
  127. break;
  128. case GPDT_MATRIX_3X3:
  129. dataParam.index = mMat3Idx;
  130. mMat3Idx += arraySize;
  131. break;
  132. case GPDT_MATRIX_3X2:
  133. dataParam.index = mMat3x2Idx;
  134. mMat3x2Idx += arraySize;
  135. break;
  136. case GPDT_MATRIX_3X4:
  137. dataParam.index = mMat3x4Idx;
  138. mMat3x4Idx += arraySize;
  139. break;
  140. case GPDT_MATRIX_4X4:
  141. dataParam.index = mMat4Idx;
  142. mMat4Idx += arraySize;
  143. break;
  144. case GPDT_MATRIX_4X2:
  145. dataParam.index = mMat4x2Idx;
  146. mMat4x2Idx += arraySize;
  147. break;
  148. case GPDT_MATRIX_4X3:
  149. dataParam.index = mMat4x3Idx;
  150. mMat4x3Idx += arraySize;
  151. break;
  152. case GPDT_COLOR:
  153. dataParam.index = mColorIdx;
  154. mColorIdx += arraySize;
  155. break;
  156. case GPDT_STRUCT:
  157. {
  158. StructParamData& param = mStructParams[mStructIdx];
  159. param.dataSize = entry.second.elementSize;
  160. param.data = mAlloc.alloc(param.dataSize);
  161. dataParam.index = mStructIdx;
  162. mStructIdx += arraySize;
  163. }
  164. break;
  165. }
  166. }
  167. for (auto& entry : textureParams)
  168. {
  169. ParamData& dataParam = mParams[entry.first];
  170. dataParam.arraySize = 1;
  171. dataParam.type = ParamType::Texture;
  172. dataParam.dataType = GPDT_UNKNOWN;
  173. dataParam.index = mTextureIdx;
  174. TextureParamData& param = mTextureParams[mTextureIdx];
  175. param.isLoadStore = false;
  176. mTextureIdx++;
  177. }
  178. for (auto& entry : samplerParams)
  179. {
  180. ParamData& dataParam = mParams[entry.first];
  181. dataParam.arraySize = 1;
  182. dataParam.type = ParamType::Sampler;
  183. dataParam.dataType = GPDT_UNKNOWN;
  184. dataParam.index = mSamplerIdx;
  185. mSamplerIdx++;
  186. }
  187. }
  188. __MaterialParams::~__MaterialParams()
  189. {
  190. if (mStructParams != nullptr)
  191. {
  192. for (UINT32 i = 0; mNumStructParams; i++)
  193. mAlloc.free(mStructParams[i].data);
  194. }
  195. mAlloc.destruct(mFloatParams, mNumFloatParams);
  196. mAlloc.destruct(mVec2Params, mNumVec2Params);
  197. mAlloc.destruct(mVec3Params, mNumVec3Params);
  198. mAlloc.destruct(mVec4Params, mNumVec4Params);
  199. mAlloc.destruct(mIntParams, mNumIntParams);
  200. mAlloc.destruct(mVec2IParams, mNumVec2IParams);
  201. mAlloc.destruct(mVec3IParams, mNumVec3IParams);
  202. mAlloc.destruct(mVec4IParams, mNumVec4IParams);
  203. mAlloc.destruct(mMat2Params, mNumMat2Params);
  204. mAlloc.destruct(mMat2x3Params, mNumMat2x3Params);
  205. mAlloc.destruct(mMat2x4Params, mNumMat2x4Params);
  206. mAlloc.destruct(mMat3Params, mNumMat3Params);
  207. mAlloc.destruct(mMat3x2Params, mNumMat3x2Params);
  208. mAlloc.destruct(mMat3x4Params, mNumMat3x4Params);
  209. mAlloc.destruct(mMat4Params, mNumMat4Params);
  210. mAlloc.destruct(mMat4x2Params, mNumMat4x2Params);
  211. mAlloc.destruct(mMat4x3Params, mNumMat4x3Params);
  212. mAlloc.destruct(mColorParams, mNumColorParams);
  213. mAlloc.destruct(mStructParams, mNumStructParams);
  214. mAlloc.destruct(mTextureParams, mNumTextureParams);
  215. mAlloc.destruct(mSamplerStateParams, mNumSamplerParams);
  216. mAlloc.clear();
  217. }
  218. void __MaterialParams::getStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx)
  219. {
  220. const ParamData* param = getParamData(name, ParamType::Data, GPDT_STRUCT, arrayIdx);
  221. if (param == nullptr)
  222. return;
  223. getStructData(param->index + arrayIdx, value, size);
  224. }
  225. void __MaterialParams::setStructData(const String& name, const void* value, UINT32 size, UINT32 arrayIdx)
  226. {
  227. const ParamData* param = getParamData(name, ParamType::Data, GPDT_STRUCT, arrayIdx);
  228. if (param == nullptr)
  229. return;
  230. setStructData(param->index + arrayIdx, value, size);
  231. }
  232. void __MaterialParams::getTexture(const String& name, HTexture& value)
  233. {
  234. const ParamData* param = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0);
  235. if (param == nullptr)
  236. return;
  237. getTexture(param->index, value);
  238. }
  239. void __MaterialParams::setTexture(const String& name, const HTexture& value)
  240. {
  241. const ParamData* param = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0);
  242. if (param == nullptr)
  243. return;
  244. setTexture(param->index, value);
  245. }
  246. void __MaterialParams::getLoadStoreTexture(const String& name, HTexture& value, TextureSurface& surface)
  247. {
  248. const ParamData* param = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0);
  249. if (param == nullptr)
  250. return;
  251. getLoadStoreTexture(param->index, value, surface);
  252. }
  253. void __MaterialParams::setLoadStoreTexture(const String& name, const HTexture& value, const TextureSurface& surface)
  254. {
  255. const ParamData* param = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0);
  256. if (param == nullptr)
  257. return;
  258. setLoadStoreTexture(param->index, value, surface);
  259. }
  260. void __MaterialParams::getSamplerState(const String& name, SamplerStatePtr& value)
  261. {
  262. const ParamData* param = getParamData(name, ParamType::Sampler, GPDT_UNKNOWN, 0);
  263. if (param == nullptr)
  264. return;
  265. getSamplerState(param->index, value);
  266. }
  267. void __MaterialParams::setSamplerState(const String& name, const SamplerStatePtr& value)
  268. {
  269. const ParamData* param = getParamData(name, ParamType::Sampler, GPDT_UNKNOWN, 0);
  270. if (param == nullptr)
  271. return;
  272. setSamplerState(param->index, value);
  273. }
  274. const __MaterialParams::ParamData* __MaterialParams::getParamData(const String& name, ParamType type, GpuParamDataType dataType,
  275. UINT32 arrayIdx)
  276. {
  277. auto iterFind = mParams.find(name);
  278. if (iterFind == mParams.end())
  279. {
  280. LOGWRN("Material doesn't have a parameter named " + name + ".");
  281. return nullptr;
  282. }
  283. const ParamData& param = iterFind->second;
  284. if (param.type != type || (type == ParamType::Data && param.dataType != dataType))
  285. {
  286. LOGWRN("Parameter \"" + name + "\" is not of the requested type.");
  287. return nullptr;
  288. }
  289. if (arrayIdx >= param.arraySize)
  290. {
  291. LOGWRN("Parameter \"" + name + "\" array index " + toString(arrayIdx) + " out of range. Array length is " +
  292. toString(param.arraySize) + ".");
  293. return nullptr;
  294. }
  295. return &param;
  296. }
  297. void __MaterialParams::getStructData(UINT32 index, void* value, UINT32 size)
  298. {
  299. const StructParamData& structParam = mStructParams[index];
  300. if (structParam.dataSize != size)
  301. {
  302. LOGWRN("Size mismatch when writing to a struct. Provided size was " + toString(size) + " bytes but the "
  303. "struct size is" + toString(structParam.dataSize) + " bytes");
  304. return;
  305. }
  306. memcpy(value, structParam.data, structParam.dataSize);
  307. }
  308. void __MaterialParams::setStructData(UINT32 index, const void* value, UINT32 size)
  309. {
  310. const StructParamData& structParam = mStructParams[index];
  311. if (structParam.dataSize != size)
  312. {
  313. LOGWRN("Size mismatch when writing to a struct. Provided size was " + toString(size) + " bytes but the "
  314. "struct size is" + toString(structParam.dataSize) + " bytes");
  315. return;
  316. }
  317. memcpy(structParam.data, value, structParam.dataSize);
  318. }
  319. void __MaterialParams::getTexture(UINT32 index, HTexture& value)
  320. {
  321. TextureParamData& textureParam = mTextureParams[index];
  322. value = textureParam.value;
  323. }
  324. void __MaterialParams::setTexture(UINT32 index, const HTexture& value)
  325. {
  326. TextureParamData& textureParam = mTextureParams[index];
  327. textureParam.value = value;
  328. textureParam.isLoadStore = false;
  329. }
  330. void __MaterialParams::getLoadStoreTexture(UINT32 index, HTexture& value, TextureSurface& surface)
  331. {
  332. TextureParamData& textureParam = mTextureParams[index];
  333. value = textureParam.value;
  334. surface = textureParam.surface;
  335. }
  336. void __MaterialParams::setLoadStoreTexture(UINT32 index, const HTexture& value, const TextureSurface& surface)
  337. {
  338. TextureParamData& textureParam = mTextureParams[index];
  339. textureParam.value = value;
  340. textureParam.isLoadStore = true;
  341. textureParam.surface = surface;
  342. }
  343. void __MaterialParams::getSamplerState(UINT32 index, SamplerStatePtr& value)
  344. {
  345. value = mSamplerStateParams[index];
  346. }
  347. void __MaterialParams::setSamplerState(UINT32 index, const SamplerStatePtr& value)
  348. {
  349. mSamplerStateParams[index] = value;
  350. }
  351. }