BsGLSLParamParser.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. 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 (for example 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_RWTEXTURE1D;
  283. break;
  284. case GL_IMAGE_2D:
  285. textureParam.type = GPOT_RWTEXTURE2D;
  286. break;
  287. case GL_IMAGE_3D:
  288. textureParam.type = GPOT_RWTEXTURE3D;
  289. break;
  290. case GL_IMAGE_2D_MULTISAMPLE:
  291. textureParam.type = GPOT_RWTEXTURE2DMS;
  292. break;
  293. }
  294. returnParamDesc.loadStoreTextures.insert(std::make_pair(paramName, textureParam));
  295. }
  296. else
  297. {
  298. // If array index is larger than 0 and uniform is not a part of a struct,
  299. // it means we already processed it (struct arrays are processed differently)
  300. if (!inStruct && arrayIdx != 0)
  301. continue;
  302. GLint blockIndex;
  303. glGetActiveUniformsiv(glProgram, 1, &index, GL_UNIFORM_BLOCK_INDEX, &blockIndex);
  304. GpuParamDataDesc gpuParam;
  305. if (isInArray)
  306. gpuParam.name = cleanParamName;
  307. else
  308. gpuParam.name = paramName;
  309. determineParamInfo(gpuParam, paramName, glProgram, index);
  310. if (blockIndex != -1)
  311. {
  312. GLint blockOffset;
  313. glGetActiveUniformsiv(glProgram, 1, &index, GL_UNIFORM_OFFSET, &blockOffset);
  314. blockOffset = blockOffset / 4;
  315. gpuParam.gpuMemOffset = blockOffset;
  316. gpuParam.paramBlockSlot = blockIndex + 1; // 0 is reserved for globals
  317. String& blockName = blockSlotToName[gpuParam.paramBlockSlot];
  318. GpuParamBlockDesc& curBlockDesc = returnParamDesc.paramBlocks[blockName];
  319. gpuParam.cpuMemOffset = blockOffset;
  320. curBlockDesc.blockSize = std::max(curBlockDesc.blockSize, gpuParam.cpuMemOffset + gpuParam.arrayElementStride * gpuParam.arraySize);
  321. }
  322. else
  323. {
  324. gpuParam.gpuMemOffset = glGetUniformLocation(glProgram, uniformName);
  325. gpuParam.paramBlockSlot = 0;
  326. gpuParam.cpuMemOffset = globalBlockDesc.blockSize;
  327. globalBlockDesc.blockSize = std::max(globalBlockDesc.blockSize, gpuParam.cpuMemOffset + gpuParam.arrayElementStride * gpuParam.arraySize);
  328. }
  329. // If parameter is not a part of a struct we're done
  330. if (!inStruct)
  331. {
  332. returnParamDesc.params.insert(std::make_pair(gpuParam.name, gpuParam));
  333. continue;
  334. }
  335. // If the parameter is part of a struct, then we need to update the struct definition
  336. auto findExistingStruct = foundStructs.find(structName);
  337. // Create new definition if one doesn't exist
  338. if (findExistingStruct == foundStructs.end())
  339. {
  340. foundStructs[structName] = GpuParamDataDesc();
  341. GpuParamDataDesc& structDesc = foundStructs[structName];
  342. structDesc.type = GPDT_STRUCT;
  343. structDesc.name = structName;
  344. structDesc.arraySize = 1;
  345. structDesc.elementSize = 0;
  346. structDesc.arrayElementStride = 0;
  347. structDesc.gpuMemOffset = gpuParam.gpuMemOffset;
  348. structDesc.cpuMemOffset = gpuParam.cpuMemOffset;
  349. structDesc.paramBlockSlot = gpuParam.paramBlockSlot;
  350. }
  351. // Update struct with size of the new parameter
  352. GpuParamDataDesc& structDesc = foundStructs[structName];
  353. assert(gpuParam.cpuMemOffset >= structDesc.cpuMemOffset);
  354. if (arrayIdx == firstArrayIndex) // Determine element size only using the first array element
  355. {
  356. structDesc.elementSize = std::max(structDesc.elementSize, (gpuParam.cpuMemOffset - structDesc.cpuMemOffset) + gpuParam.arrayElementStride * gpuParam.arraySize);
  357. structDesc.arrayElementStride = structDesc.elementSize;
  358. }
  359. // New array element reached, determine arrayElementStride
  360. if (arrayIdx != firstArrayIndex)
  361. {
  362. UINT32 numElements = arrayIdx - firstArrayIndex;
  363. structDesc.arrayElementStride = (gpuParam.cpuMemOffset - structDesc.cpuMemOffset) / numElements;
  364. }
  365. structDesc.arraySize = std::max(structDesc.arraySize, arrayIdx + 1);
  366. }
  367. }
  368. for (auto iter = foundStructs.begin(); iter != foundStructs.end(); ++iter)
  369. returnParamDesc.params.insert(std::make_pair(iter->first, iter->second));
  370. // Param blocks always need to be a multiple of 4, so make it so
  371. for (auto iter = returnParamDesc.paramBlocks.begin(); iter != returnParamDesc.paramBlocks.end(); ++iter)
  372. {
  373. GpuParamBlockDesc& blockDesc = iter->second;
  374. if (blockDesc.blockSize % 4 != 0)
  375. blockDesc.blockSize += (4 - (blockDesc.blockSize % 4));
  376. }
  377. #if BS_DEBUG_MODE
  378. // Check if manually calculated and OpenGL buffer sizes match
  379. for (auto iter = returnParamDesc.paramBlocks.begin(); iter != returnParamDesc.paramBlocks.end(); ++iter)
  380. {
  381. if (iter->second.slot == 0)
  382. continue;
  383. GLint blockSize = 0;
  384. glGetActiveUniformBlockiv(glProgram, iter->second.slot - 1, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
  385. assert(blockSize % 4 == 0);
  386. blockSize = blockSize / 4;
  387. if (iter->second.blockSize != blockSize)
  388. BS_EXCEPT(InternalErrorException, "OpenGL specified and manual uniform block buffer sizes don't match!");
  389. }
  390. #endif
  391. bs_free(uniformName);
  392. }
  393. void GLSLParamParser::determineParamInfo(GpuParamDataDesc& desc, const String& paramName, GLuint programHandle, GLuint uniformIndex)
  394. {
  395. GLint arraySize;
  396. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_SIZE, &arraySize);
  397. desc.arraySize = arraySize;
  398. GLint uniformType;
  399. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_TYPE, &uniformType);
  400. switch (uniformType)
  401. {
  402. case GL_BOOL:
  403. desc.type = GPDT_BOOL;
  404. desc.elementSize = 1;
  405. break;
  406. case GL_FLOAT:
  407. desc.type = GPDT_FLOAT1;
  408. desc.elementSize = 1;
  409. break;
  410. case GL_FLOAT_VEC2:
  411. desc.type = GPDT_FLOAT2;
  412. desc.elementSize = 2;
  413. break;
  414. case GL_FLOAT_VEC3:
  415. desc.type = GPDT_FLOAT3;
  416. desc.elementSize = 3;
  417. break;
  418. case GL_FLOAT_VEC4:
  419. desc.type = GPDT_FLOAT4;
  420. desc.elementSize = 4;
  421. break;
  422. case GL_INT:
  423. desc.type = GPDT_INT1;
  424. desc.elementSize = 1;
  425. break;
  426. case GL_INT_VEC2:
  427. desc.type = GPDT_INT2;
  428. desc.elementSize = 2;
  429. break;
  430. case GL_INT_VEC3:
  431. desc.type = GPDT_INT3;
  432. desc.elementSize = 3;
  433. break;
  434. case GL_INT_VEC4:
  435. desc.type = GPDT_INT4;
  436. desc.elementSize = 4;
  437. break;
  438. case GL_FLOAT_MAT2:
  439. desc.type = GPDT_MATRIX_2X2;
  440. desc.elementSize = 4;
  441. break;
  442. case GL_FLOAT_MAT3:
  443. desc.type = GPDT_MATRIX_3X3;
  444. desc.elementSize = 9;
  445. break;
  446. case GL_FLOAT_MAT4:
  447. desc.type = GPDT_MATRIX_4X4;
  448. desc.elementSize = 16;
  449. break;
  450. case GL_FLOAT_MAT2x3:
  451. desc.type = GPDT_MATRIX_2X3;
  452. desc.elementSize = 6;
  453. break;
  454. case GL_FLOAT_MAT3x2:
  455. desc.type = GPDT_MATRIX_3X2;
  456. desc.elementSize = 6;
  457. break;
  458. case GL_FLOAT_MAT2x4:
  459. desc.type = GPDT_MATRIX_2X4;
  460. desc.elementSize = 8;
  461. break;
  462. case GL_FLOAT_MAT4x2:
  463. desc.type = GPDT_MATRIX_4X2;
  464. desc.elementSize = 8;
  465. break;
  466. case GL_FLOAT_MAT3x4:
  467. desc.type = GPDT_MATRIX_3X4;
  468. desc.elementSize = 12;
  469. break;
  470. case GL_FLOAT_MAT4x3:
  471. desc.type = GPDT_MATRIX_4X3;
  472. desc.elementSize = 12;
  473. break;
  474. default:
  475. BS_EXCEPT(InternalErrorException, "Invalid shader parameter type: " + toString(uniformType) + " for parameter " + paramName);
  476. }
  477. if (arraySize > 1)
  478. {
  479. GLint arrayStride;
  480. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE, &arrayStride);
  481. if (arrayStride > 0)
  482. {
  483. assert(arrayStride % 4 == 0);
  484. desc.arrayElementStride = arrayStride / 4;
  485. }
  486. else
  487. desc.arrayElementStride = desc.elementSize;
  488. }
  489. else
  490. desc.arrayElementStride = desc.elementSize;
  491. }
  492. }