BsGLSLParamParser.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLSLParamParser.h"
  4. namespace BansheeEngine
  5. {
  6. INT32 GLSLAttribute::matchesName(const String& name)
  7. {
  8. if (name.length() >= mName.length())
  9. {
  10. if (name.substr(0, mName.length()) == mName)
  11. {
  12. String indexStr = name.substr(mName.length(), name.length());
  13. return parseUINT32(indexStr, 0);
  14. }
  15. }
  16. return -1;
  17. }
  18. List<VertexElement> GLSLParamParser::buildVertexDeclaration(GLuint glProgram)
  19. {
  20. GLint numAttributes = 0;
  21. glGetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &numAttributes);
  22. GLint maxNameSize = 0;
  23. glGetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxNameSize);
  24. GLchar* attributeName = (GLchar*)bs_alloc(sizeof(GLchar)* maxNameSize);
  25. List<VertexElement> elementList;
  26. for (GLint i = 0; i < numAttributes; i++)
  27. {
  28. GLint attribSize = 0;
  29. GLenum attribType = 0;
  30. glGetActiveAttrib(glProgram, i, maxNameSize, nullptr, &attribSize, &attribType, attributeName);
  31. VertexElementSemantic semantic = VES_POSITION;
  32. UINT16 index = 0;
  33. if (attribNameToElementSemantic(attributeName, semantic, index))
  34. {
  35. VertexElementType type = glTypeToAttributeType(attribType);
  36. elementList.push_back(VertexElement(0, i, type, semantic, index));
  37. }
  38. else
  39. {
  40. LOGWRN("Cannot determine vertex input attribute type for attribute: " + String(attributeName));
  41. }
  42. }
  43. bs_free(attributeName);
  44. return elementList;
  45. }
  46. VertexElementType GLSLParamParser::glTypeToAttributeType(GLenum glType)
  47. {
  48. switch (glType)
  49. {
  50. case GL_FLOAT:
  51. return VET_FLOAT1;
  52. case GL_FLOAT_VEC2:
  53. return VET_FLOAT2;
  54. case GL_FLOAT_VEC3:
  55. return VET_FLOAT3;
  56. case GL_FLOAT_VEC4:
  57. return VET_FLOAT4;
  58. default:
  59. BS_EXCEPT(NotImplementedException, "OpenGL render system currently only supports float parameters.");
  60. }
  61. }
  62. bool GLSLParamParser::attribNameToElementSemantic(const String& name, VertexElementSemantic& semantic, UINT16& index)
  63. {
  64. static GLSLAttribute attributes[] =
  65. {
  66. GLSLAttribute("bs_position", VES_POSITION),
  67. GLSLAttribute("bs_normal", VES_NORMAL),
  68. GLSLAttribute("bs_tangent", VES_TANGENT),
  69. GLSLAttribute("bs_bitangent", VES_BITANGENT),
  70. GLSLAttribute("bs_texcoord", VES_TEXCOORD),
  71. GLSLAttribute("bs_color", VES_COLOR),
  72. GLSLAttribute("bs_blendweights", VES_BLEND_WEIGHTS),
  73. GLSLAttribute("bs_blendindices", VES_BLEND_INDICES)
  74. };
  75. static const UINT32 numAttribs = sizeof(attributes) / sizeof(attributes[0]);
  76. for (UINT32 i = 0; i < numAttribs; i++)
  77. {
  78. INT32 attribIndex = attributes[i].matchesName(name);
  79. if (attribIndex != -1)
  80. {
  81. index = attribIndex;
  82. semantic = attributes[i].getSemantic();
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. void GLSLParamParser::buildUniformDescriptions(GLuint glProgram, GpuParamDesc& returnParamDesc)
  89. {
  90. // scan through the active uniforms and add them to the reference list
  91. GLint maxBufferSize = 0;
  92. glGetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxBufferSize);
  93. GLint maxBlockNameBufferSize = 0;
  94. glGetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &maxBlockNameBufferSize);
  95. if (maxBlockNameBufferSize > maxBufferSize)
  96. maxBufferSize = maxBlockNameBufferSize;
  97. GLchar* uniformName = (GLchar*)bs_alloc(sizeof(GLchar)* maxBufferSize);
  98. GpuParamBlockDesc newGlobalBlockDesc;
  99. newGlobalBlockDesc.slot = 0;
  100. newGlobalBlockDesc.name = "BS_INTERNAL_Globals";
  101. newGlobalBlockDesc.blockSize = 0;
  102. newGlobalBlockDesc.isShareable = false;
  103. UINT32 textureSlot = 0;
  104. returnParamDesc.paramBlocks[newGlobalBlockDesc.name] = newGlobalBlockDesc;
  105. GpuParamBlockDesc& globalBlockDesc = returnParamDesc.paramBlocks[newGlobalBlockDesc.name];
  106. GLint uniformBlockCount = 0;
  107. glGetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &uniformBlockCount);
  108. Map<UINT32, String> blockSlotToName;
  109. Set<String> blockNames;
  110. for (GLuint index = 0; index < (GLuint)uniformBlockCount; index++)
  111. {
  112. GLsizei unusedSize = 0;
  113. glGetActiveUniformBlockName(glProgram, index, maxBufferSize, &unusedSize, uniformName);
  114. GpuParamBlockDesc newBlockDesc;
  115. newBlockDesc.slot = index + 1;
  116. newBlockDesc.name = uniformName;
  117. newBlockDesc.blockSize = 0;
  118. newBlockDesc.isShareable = true;
  119. returnParamDesc.paramBlocks[newBlockDesc.name] = newBlockDesc;
  120. blockSlotToName.insert(std::make_pair(newBlockDesc.slot, newBlockDesc.name));
  121. blockNames.insert(newBlockDesc.name);
  122. }
  123. Map<String, UINT32> foundFirstArrayIndex;
  124. Map<String, GpuParamDataDesc> foundStructs;
  125. // get the number of active uniforms
  126. GLint uniformCount = 0;
  127. glGetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &uniformCount);
  128. // Loop over each of the active uniforms, and add them to the reference container
  129. // only do this for user defined uniforms, ignore built in gl state uniforms
  130. for (GLuint index = 0; index < (GLuint)uniformCount; index++)
  131. {
  132. GLsizei arraySize = 0;
  133. glGetActiveUniformName(glProgram, index, maxBufferSize, &arraySize, uniformName);
  134. String paramName = String(uniformName);
  135. // Naming rules and packing rules used here are described in
  136. // OpenGL Core Specification 2.11.4
  137. // Check if parameter is a part of a struct
  138. Vector<String> nameElements = StringUtil::tokenise(paramName, ".");
  139. bool inStruct = false;
  140. String structName;
  141. if (nameElements.size() > 1)
  142. {
  143. auto uniformBlockFind = blockNames.find(nameElements[0]);
  144. // Check if the name is not a struct, and instead a Uniform block namespace
  145. if (uniformBlockFind != blockNames.end())
  146. {
  147. // Possibly it's a struct inside a named uniform block
  148. if (nameElements.size() > 2)
  149. {
  150. inStruct = true;
  151. structName = nameElements[1];
  152. }
  153. }
  154. else
  155. {
  156. inStruct = true;
  157. structName = nameElements[0];
  158. }
  159. }
  160. String cleanParamName = paramName; // Param name without array indexes
  161. // Check if the parameter is in an array
  162. UINT32 arrayIdx = 0;
  163. bool isInArray = false;
  164. if (inStruct)
  165. {
  166. // If the uniform name has a "[" in it then its an array element uniform.
  167. String::size_type arrayStart = structName.find("[");
  168. String::size_type arrayEnd = structName.find("]");
  169. if (arrayStart != String::npos)
  170. {
  171. String strArrIdx = structName.substr(arrayStart + 1, arrayEnd - (arrayStart + 1));
  172. arrayIdx = parseUINT32(strArrIdx, 0);
  173. isInArray = true;
  174. structName = structName.substr(0, arrayStart);
  175. }
  176. }
  177. else
  178. {
  179. // If the uniform name has a "[" in it then its an array element uniform.
  180. String::size_type arrayStart = cleanParamName.find("[");
  181. String::size_type arrayEnd = cleanParamName.find("]");
  182. if (arrayStart != String::npos)
  183. {
  184. String strArrIdx = cleanParamName.substr(arrayStart + 1, arrayEnd - (arrayStart + 1));
  185. arrayIdx = parseUINT32(strArrIdx, 0);
  186. isInArray = true;
  187. cleanParamName = cleanParamName.substr(0, arrayStart);
  188. }
  189. }
  190. if (inStruct)
  191. {
  192. // OpenGL makes struct management really difficult, which is why I have given up on implementing this so far
  193. // Some of the issues I encountered:
  194. // - Elements will be optimized out if they are not used. This makes it hard to determine proper structure size.
  195. // - If struct is within a Uniform buffer block, then it is possible because the element won't be optimized out of the buffer
  196. // - If the struct is within a global buffer, it is impossible to determine actual size, since the element will be optimized out of the buffer too
  197. // - Same issue happens with arrays, as OpenGL will optimize out array elements. With global buffers this makes it impossible to determine
  198. // actual array size (e.g. suppose OpenGL optimized out few last elements)
  199. // - Normal arrays work fine as OpenGL has utilities for reporting their actual size, but those do not work with structs
  200. BS_EXCEPT(NotImplementedException, "Structs are not supported.")
  201. }
  202. // GLSL will optimize out unused array indexes, so there's no guarantee that 0 is the first,
  203. // so we store the first one here
  204. int firstArrayIndex = 0;
  205. if (isInArray)
  206. {
  207. String& nameToSearch = cleanParamName;
  208. if (inStruct)
  209. nameToSearch = structName;
  210. auto arrayIndexFind = foundFirstArrayIndex.find(nameToSearch);
  211. if (arrayIndexFind == foundFirstArrayIndex.end())
  212. {
  213. foundFirstArrayIndex[nameToSearch] = arrayIdx;
  214. }
  215. firstArrayIndex = foundFirstArrayIndex[nameToSearch];
  216. }
  217. GLint uniformType;
  218. glGetActiveUniformsiv(glProgram, 1, &index, GL_UNIFORM_TYPE, &uniformType);
  219. bool isSampler = false;
  220. bool isImage = false;
  221. switch (uniformType)
  222. {
  223. case GL_SAMPLER_1D:
  224. case GL_SAMPLER_2D:
  225. case GL_SAMPLER_3D:
  226. case GL_SAMPLER_2D_MULTISAMPLE:
  227. case GL_SAMPLER_CUBE:
  228. isSampler = true;
  229. break;
  230. case GL_IMAGE_1D:
  231. case GL_IMAGE_2D:
  232. case GL_IMAGE_3D:
  233. case GL_IMAGE_CUBE:
  234. case GL_IMAGE_2D_MULTISAMPLE:
  235. isImage = true;
  236. break;
  237. }
  238. if (isSampler)
  239. {
  240. GpuParamObjectDesc samplerParam;
  241. samplerParam.name = paramName;
  242. samplerParam.slot = glGetUniformLocation(glProgram, uniformName);
  243. GpuParamObjectDesc textureParam;
  244. textureParam.name = paramName;
  245. textureParam.slot = samplerParam.slot;
  246. switch (uniformType)
  247. {
  248. case GL_SAMPLER_1D:
  249. samplerParam.type = GPOT_SAMPLER1D;
  250. textureParam.type = GPOT_TEXTURE1D;
  251. break;
  252. case GL_SAMPLER_2D:
  253. samplerParam.type = GPOT_SAMPLER2D;
  254. textureParam.type = GPOT_TEXTURE2D;
  255. break;
  256. case GL_SAMPLER_3D:
  257. samplerParam.type = GPOT_SAMPLER3D;
  258. textureParam.type = GPOT_TEXTURE3D;
  259. break;
  260. case GL_SAMPLER_CUBE:
  261. samplerParam.type = GPOT_SAMPLERCUBE;
  262. textureParam.type = GPOT_TEXTURECUBE;
  263. break;
  264. case GL_SAMPLER_2D_MULTISAMPLE:
  265. samplerParam.type = GPOT_SAMPLER2DMS;
  266. textureParam.type = GPOT_TEXTURE2DMS;
  267. break;
  268. }
  269. returnParamDesc.samplers.insert(std::make_pair(paramName, samplerParam));
  270. returnParamDesc.textures.insert(std::make_pair(paramName, textureParam));
  271. }
  272. else if (isImage)
  273. {
  274. GpuParamObjectDesc textureParam;
  275. textureParam.name = paramName;
  276. textureParam.slot = glGetUniformLocation(glProgram, uniformName);
  277. switch (uniformType)
  278. {
  279. case GL_IMAGE_1D:
  280. textureParam.type = GPOT_TEXTURE1D;
  281. break;
  282. case GL_IMAGE_2D:
  283. textureParam.type = GPOT_TEXTURE2D;
  284. break;
  285. case GL_IMAGE_3D:
  286. textureParam.type = GPOT_TEXTURE3D;
  287. break;
  288. }
  289. returnParamDesc.textures.insert(std::make_pair(paramName, textureParam));
  290. }
  291. else
  292. {
  293. // If array index is larger than 0 and uniform is not a part of a struct,
  294. // it means we already processed it (struct arrays are processed differently)
  295. if (!inStruct && arrayIdx != 0)
  296. continue;
  297. GLint blockIndex;
  298. glGetActiveUniformsiv(glProgram, 1, &index, GL_UNIFORM_BLOCK_INDEX, &blockIndex);
  299. GpuParamDataDesc gpuParam;
  300. if (isInArray)
  301. gpuParam.name = cleanParamName;
  302. else
  303. gpuParam.name = paramName;
  304. determineParamInfo(gpuParam, paramName, glProgram, index);
  305. if (blockIndex != -1)
  306. {
  307. GLint blockOffset;
  308. glGetActiveUniformsiv(glProgram, 1, &index, GL_UNIFORM_OFFSET, &blockOffset);
  309. blockOffset = blockOffset / 4;
  310. gpuParam.gpuMemOffset = blockOffset;
  311. gpuParam.paramBlockSlot = blockIndex + 1; // 0 is reserved for globals
  312. String& blockName = blockSlotToName[gpuParam.paramBlockSlot];
  313. GpuParamBlockDesc& curBlockDesc = returnParamDesc.paramBlocks[blockName];
  314. gpuParam.cpuMemOffset = blockOffset;
  315. curBlockDesc.blockSize = std::max(curBlockDesc.blockSize, gpuParam.cpuMemOffset + gpuParam.arrayElementStride * gpuParam.arraySize);
  316. }
  317. else
  318. {
  319. gpuParam.gpuMemOffset = glGetUniformLocation(glProgram, uniformName);
  320. gpuParam.paramBlockSlot = 0;
  321. gpuParam.cpuMemOffset = globalBlockDesc.blockSize;
  322. globalBlockDesc.blockSize = std::max(globalBlockDesc.blockSize, gpuParam.cpuMemOffset + gpuParam.arrayElementStride * gpuParam.arraySize);
  323. }
  324. // If parameter is not a part of a struct we're done
  325. if (!inStruct)
  326. {
  327. returnParamDesc.params.insert(std::make_pair(gpuParam.name, gpuParam));
  328. continue;
  329. }
  330. // If the parameter is part of a struct, then we need to update the struct definition
  331. auto findExistingStruct = foundStructs.find(structName);
  332. // Create new definition if one doesn't exist
  333. if (findExistingStruct == foundStructs.end())
  334. {
  335. foundStructs[structName] = GpuParamDataDesc();
  336. GpuParamDataDesc& structDesc = foundStructs[structName];
  337. structDesc.type = GPDT_STRUCT;
  338. structDesc.name = structName;
  339. structDesc.arraySize = 1;
  340. structDesc.elementSize = 0;
  341. structDesc.arrayElementStride = 0;
  342. structDesc.gpuMemOffset = gpuParam.gpuMemOffset;
  343. structDesc.cpuMemOffset = gpuParam.cpuMemOffset;
  344. structDesc.paramBlockSlot = gpuParam.paramBlockSlot;
  345. }
  346. // Update struct with size of the new parameter
  347. GpuParamDataDesc& structDesc = foundStructs[structName];
  348. assert(gpuParam.cpuMemOffset >= structDesc.cpuMemOffset);
  349. if (arrayIdx == firstArrayIndex) // Determine element size only using the first array element
  350. {
  351. structDesc.elementSize = std::max(structDesc.elementSize, (gpuParam.cpuMemOffset - structDesc.cpuMemOffset) + gpuParam.arrayElementStride * gpuParam.arraySize);
  352. structDesc.arrayElementStride = structDesc.elementSize;
  353. }
  354. // New array element reached, determine arrayElementStride
  355. if (arrayIdx != firstArrayIndex)
  356. {
  357. UINT32 numElements = arrayIdx - firstArrayIndex;
  358. structDesc.arrayElementStride = (gpuParam.cpuMemOffset - structDesc.cpuMemOffset) / numElements;
  359. }
  360. structDesc.arraySize = std::max(structDesc.arraySize, arrayIdx + 1);
  361. }
  362. }
  363. for (auto iter = foundStructs.begin(); iter != foundStructs.end(); ++iter)
  364. returnParamDesc.params.insert(std::make_pair(iter->first, iter->second));
  365. // Param blocks always need to be a multiple of 4, so make it so
  366. for (auto iter = returnParamDesc.paramBlocks.begin(); iter != returnParamDesc.paramBlocks.end(); ++iter)
  367. {
  368. GpuParamBlockDesc& blockDesc = iter->second;
  369. if (blockDesc.blockSize % 4 != 0)
  370. blockDesc.blockSize += (4 - (blockDesc.blockSize % 4));
  371. }
  372. #if BS_DEBUG_MODE
  373. // Check if manually calculated and OpenGL buffer sizes match
  374. for (auto iter = returnParamDesc.paramBlocks.begin(); iter != returnParamDesc.paramBlocks.end(); ++iter)
  375. {
  376. if (iter->second.slot == 0)
  377. continue;
  378. GLint blockSize = 0;
  379. glGetActiveUniformBlockiv(glProgram, iter->second.slot - 1, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
  380. assert(blockSize % 4 == 0);
  381. blockSize = blockSize / 4;
  382. if (iter->second.blockSize != blockSize)
  383. BS_EXCEPT(InternalErrorException, "OpenGL specified and manual uniform block buffer sizes don't match!");
  384. }
  385. #endif
  386. bs_free(uniformName);
  387. }
  388. void GLSLParamParser::determineParamInfo(GpuParamDataDesc& desc, const String& paramName, GLuint programHandle, GLuint uniformIndex)
  389. {
  390. GLint arraySize;
  391. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_SIZE, &arraySize);
  392. desc.arraySize = arraySize;
  393. GLint uniformType;
  394. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_TYPE, &uniformType);
  395. switch (uniformType)
  396. {
  397. case GL_BOOL:
  398. desc.type = GPDT_BOOL;
  399. desc.elementSize = 1;
  400. break;
  401. case GL_FLOAT:
  402. desc.type = GPDT_FLOAT1;
  403. desc.elementSize = 1;
  404. break;
  405. case GL_FLOAT_VEC2:
  406. desc.type = GPDT_FLOAT2;
  407. desc.elementSize = 2;
  408. break;
  409. case GL_FLOAT_VEC3:
  410. desc.type = GPDT_FLOAT3;
  411. desc.elementSize = 3;
  412. break;
  413. case GL_FLOAT_VEC4:
  414. desc.type = GPDT_FLOAT4;
  415. desc.elementSize = 4;
  416. break;
  417. case GL_INT:
  418. desc.type = GPDT_INT1;
  419. desc.elementSize = 1;
  420. break;
  421. case GL_INT_VEC2:
  422. desc.type = GPDT_INT2;
  423. desc.elementSize = 2;
  424. break;
  425. case GL_INT_VEC3:
  426. desc.type = GPDT_INT3;
  427. desc.elementSize = 3;
  428. break;
  429. case GL_INT_VEC4:
  430. desc.type = GPDT_INT4;
  431. desc.elementSize = 4;
  432. break;
  433. case GL_FLOAT_MAT2:
  434. desc.type = GPDT_MATRIX_2X2;
  435. desc.elementSize = 4;
  436. break;
  437. case GL_FLOAT_MAT3:
  438. desc.type = GPDT_MATRIX_3X3;
  439. desc.elementSize = 9;
  440. break;
  441. case GL_FLOAT_MAT4:
  442. desc.type = GPDT_MATRIX_4X4;
  443. desc.elementSize = 16;
  444. break;
  445. case GL_FLOAT_MAT2x3:
  446. desc.type = GPDT_MATRIX_2X3;
  447. desc.elementSize = 6;
  448. break;
  449. case GL_FLOAT_MAT3x2:
  450. desc.type = GPDT_MATRIX_3X2;
  451. desc.elementSize = 6;
  452. break;
  453. case GL_FLOAT_MAT2x4:
  454. desc.type = GPDT_MATRIX_2X4;
  455. desc.elementSize = 8;
  456. break;
  457. case GL_FLOAT_MAT4x2:
  458. desc.type = GPDT_MATRIX_4X2;
  459. desc.elementSize = 8;
  460. break;
  461. case GL_FLOAT_MAT3x4:
  462. desc.type = GPDT_MATRIX_3X4;
  463. desc.elementSize = 12;
  464. break;
  465. case GL_FLOAT_MAT4x3:
  466. desc.type = GPDT_MATRIX_4X3;
  467. desc.elementSize = 12;
  468. break;
  469. default:
  470. BS_EXCEPT(InternalErrorException, "Invalid shader parameter type: " + toString(uniformType) + " for parameter " + paramName);
  471. }
  472. if (arraySize > 1)
  473. {
  474. GLint arrayStride;
  475. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE, &arrayStride);
  476. if (arrayStride > 0)
  477. {
  478. assert(arrayStride % 4 == 0);
  479. desc.arrayElementStride = arrayStride / 4;
  480. }
  481. else
  482. desc.arrayElementStride = desc.elementSize;
  483. }
  484. else
  485. desc.arrayElementStride = desc.elementSize;
  486. }
  487. }