BsGLSLParamParser.cpp 18 KB

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