BsGLSLParamParser.cpp 18 KB

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