BsGLSLParamParser.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLSLParamParser.h"
  4. namespace bs { namespace ct
  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. UINT32 slot = glGetAttribLocation(glProgram, attributeName);
  37. elementList.push_back(VertexElement(0, slot, type, semantic, index));
  38. }
  39. else
  40. {
  41. // Ignore built-in attributes
  42. if(memcmp(attributeName, "gl_", 3) != 0)
  43. LOGWRN("Cannot determine vertex input attribute type for attribute: " + String(attributeName));
  44. }
  45. }
  46. bs_free(attributeName);
  47. return elementList;
  48. }
  49. VertexElementType GLSLParamParser::glTypeToAttributeType(GLenum glType)
  50. {
  51. switch (glType)
  52. {
  53. case GL_FLOAT:
  54. return VET_FLOAT1;
  55. case GL_FLOAT_VEC2:
  56. return VET_FLOAT2;
  57. case GL_FLOAT_VEC3:
  58. return VET_FLOAT3;
  59. case GL_FLOAT_VEC4:
  60. return VET_FLOAT4;
  61. case GL_INT:
  62. return VET_INT1;
  63. case GL_INT_VEC2:
  64. return VET_INT2;
  65. case GL_INT_VEC3:
  66. return VET_INT3;
  67. case GL_INT_VEC4:
  68. return VET_INT4;
  69. case GL_UNSIGNED_INT:
  70. return VET_UINT1;
  71. case GL_UNSIGNED_INT_VEC2:
  72. return VET_UINT2;
  73. case GL_UNSIGNED_INT_VEC3:
  74. return VET_UINT3;
  75. case GL_UNSIGNED_INT_VEC4:
  76. return VET_UINT4;
  77. default:
  78. BS_EXCEPT(NotImplementedException, "Unsupported vertex attribute type.");
  79. }
  80. return VET_FLOAT4;
  81. }
  82. bool GLSLParamParser::attribNameToElementSemantic(const String& name, VertexElementSemantic& semantic, UINT16& index)
  83. {
  84. static GLSLAttribute attributes[] =
  85. {
  86. GLSLAttribute("bs_position", VES_POSITION),
  87. GLSLAttribute("bs_normal", VES_NORMAL),
  88. GLSLAttribute("bs_tangent", VES_TANGENT),
  89. GLSLAttribute("bs_bitangent", VES_BITANGENT),
  90. GLSLAttribute("bs_texcoord", VES_TEXCOORD),
  91. GLSLAttribute("bs_color", VES_COLOR),
  92. GLSLAttribute("bs_blendweights", VES_BLEND_WEIGHTS),
  93. GLSLAttribute("bs_blendindices", VES_BLEND_INDICES)
  94. };
  95. static const UINT32 numAttribs = sizeof(attributes) / sizeof(attributes[0]);
  96. for (UINT32 i = 0; i < numAttribs; i++)
  97. {
  98. INT32 attribIndex = attributes[i].matchesName(name);
  99. if (attribIndex != -1)
  100. {
  101. index = attribIndex;
  102. semantic = attributes[i].getSemantic();
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. void GLSLParamParser::buildUniformDescriptions(GLuint glProgram, GpuProgramType type, GpuParamDesc& returnParamDesc)
  109. {
  110. // Scan through the active uniform blocks
  111. GLint maxBufferSize = 0;
  112. glGetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxBufferSize);
  113. GLint maxBlockNameBufferSize = 0;
  114. glGetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &maxBlockNameBufferSize);
  115. GLint maxStorageBlockNameBufferSize = 0;
  116. glGetProgramInterfaceiv(glProgram, GL_SHADER_STORAGE_BLOCK, GL_MAX_NAME_LENGTH, &maxStorageBlockNameBufferSize);
  117. maxBufferSize = std::max(maxBufferSize, maxBlockNameBufferSize);
  118. maxBufferSize = std::max(maxBufferSize, maxStorageBlockNameBufferSize);
  119. GLchar* uniformName = (GLchar*)bs_alloc(sizeof(GLchar)* maxBufferSize);
  120. GpuParamBlockDesc newGlobalBlockDesc;
  121. newGlobalBlockDesc.slot = 0;
  122. newGlobalBlockDesc.set = mapParameterToSet(type, ParamType::UniformBlock);
  123. newGlobalBlockDesc.name = "BS_INTERNAL_Globals";
  124. newGlobalBlockDesc.blockSize = 0;
  125. newGlobalBlockDesc.isShareable = false;
  126. returnParamDesc.paramBlocks[newGlobalBlockDesc.name] = newGlobalBlockDesc;
  127. GpuParamBlockDesc& globalBlockDesc = returnParamDesc.paramBlocks[newGlobalBlockDesc.name];
  128. GLint uniformBlockCount = 0;
  129. glGetProgramInterfaceiv(glProgram, GL_UNIFORM_BLOCK, GL_ACTIVE_RESOURCES, &uniformBlockCount);
  130. Map<UINT32, String> blockSlotToName;
  131. Set<String> blockNames;
  132. for (GLuint index = 0; index < (GLuint)uniformBlockCount; index++)
  133. {
  134. GLsizei unusedSize = 0;
  135. glGetProgramResourceName(glProgram, GL_UNIFORM_BLOCK, index, maxBufferSize, &unusedSize, uniformName);
  136. GpuParamBlockDesc newBlockDesc;
  137. newBlockDesc.slot = index + 1;
  138. newBlockDesc.set = mapParameterToSet(type, ParamType::UniformBlock);
  139. newBlockDesc.name = uniformName;
  140. newBlockDesc.blockSize = 0;
  141. newBlockDesc.isShareable = true;
  142. returnParamDesc.paramBlocks[newBlockDesc.name] = newBlockDesc;
  143. blockSlotToName.insert(std::make_pair(index + 1, newBlockDesc.name));
  144. blockNames.insert(newBlockDesc.name);
  145. }
  146. // Scan through the shared storage blocks
  147. GLint storageBlockCount = 0;
  148. glGetProgramInterfaceiv(glProgram, GL_SHADER_STORAGE_BLOCK, GL_ACTIVE_RESOURCES, &storageBlockCount);
  149. for (GLuint index = 0; index < (GLuint)storageBlockCount; index++)
  150. {
  151. GLsizei unusedSize = 0;
  152. glGetProgramResourceName(glProgram, GL_SHADER_STORAGE_BLOCK, index, maxBufferSize, &unusedSize, uniformName);
  153. GpuParamObjectDesc bufferParam;
  154. bufferParam.name = uniformName;
  155. bufferParam.slot = index;
  156. bufferParam.type = GPOT_RWSTRUCTURED_BUFFER;
  157. bufferParam.set = mapParameterToSet(type, ParamType::StorageBlock);
  158. returnParamDesc.buffers.insert(std::make_pair(uniformName, bufferParam));
  159. }
  160. Map<String, UINT32> foundFirstArrayIndex;
  161. Map<String, GpuParamDataDesc> foundStructs;
  162. // Get the number of active uniforms
  163. GLint uniformCount = 0;
  164. glGetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &uniformCount);
  165. // Loop over each of the active uniforms, and add them to the reference container
  166. // only do this for user defined uniforms, ignore built in gl state uniforms
  167. for (GLuint index = 0; index < (GLuint)uniformCount; index++)
  168. {
  169. GLsizei arraySize = 0;
  170. glGetActiveUniformName(glProgram, index, maxBufferSize, &arraySize, uniformName);
  171. String paramName = String(uniformName);
  172. // Naming rules and packing rules used here are described in
  173. // OpenGL Core Specification 2.11.4
  174. // Check if parameter is a part of a struct
  175. Vector<String> nameElements = StringUtil::tokenise(paramName, ".");
  176. bool inStruct = false;
  177. String structName;
  178. if (nameElements.size() > 1)
  179. {
  180. auto uniformBlockFind = blockNames.find(nameElements[0]);
  181. // Check if the name is not a struct, and instead a Uniform block namespace
  182. if (uniformBlockFind != blockNames.end())
  183. {
  184. // Possibly it's a struct inside a named uniform block
  185. if (nameElements.size() > 2)
  186. {
  187. inStruct = true;
  188. structName = nameElements[1];
  189. }
  190. }
  191. else
  192. {
  193. inStruct = true;
  194. structName = nameElements[0];
  195. }
  196. }
  197. String cleanParamName = paramName; // Param name without array indexes
  198. // Check if the parameter is in an array
  199. UINT32 arrayIdx = 0;
  200. bool isInArray = false;
  201. if (inStruct)
  202. {
  203. // If the uniform name has a "[" in it then its an array element uniform.
  204. String::size_type arrayStart = structName.find("[");
  205. String::size_type arrayEnd = structName.find("]");
  206. if (arrayStart != String::npos)
  207. {
  208. String strArrIdx = structName.substr(arrayStart + 1, arrayEnd - (arrayStart + 1));
  209. arrayIdx = parseUINT32(strArrIdx, 0);
  210. isInArray = true;
  211. structName = structName.substr(0, arrayStart);
  212. }
  213. }
  214. else
  215. {
  216. // If the uniform name has a "[" in it then its an array element uniform.
  217. String::size_type arrayStart = cleanParamName.find("[");
  218. String::size_type arrayEnd = cleanParamName.find("]");
  219. if (arrayStart != String::npos)
  220. {
  221. String strArrIdx = cleanParamName.substr(arrayStart + 1, arrayEnd - (arrayStart + 1));
  222. arrayIdx = parseUINT32(strArrIdx, 0);
  223. isInArray = true;
  224. cleanParamName = cleanParamName.substr(0, arrayStart);
  225. }
  226. }
  227. if (inStruct)
  228. {
  229. // OpenGL makes struct management really difficult, which is why I have given up on implementing this so far
  230. // Some of the issues I encountered:
  231. // - Elements will be optimized out if they are not used. This makes it hard to determine proper structure size.
  232. // - If struct is within a Uniform buffer block, then it is possible because the element won't be optimized out of the buffer
  233. // - 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
  234. // - Same issue happens with arrays, as OpenGL will optimize out array elements. With global buffers this makes it impossible to determine
  235. // actual array size (for example suppose OpenGL optimized out few last elements)
  236. // - Normal arrays work fine as OpenGL has utilities for reporting their actual size, but those do not work with structs
  237. BS_EXCEPT(NotImplementedException, "Structs are not supported.")
  238. }
  239. // GLSL will optimize out unused array indexes, so there's no guarantee that 0 is the first,
  240. // so we store the first one here
  241. int firstArrayIndex = 0;
  242. if (isInArray)
  243. {
  244. String& nameToSearch = cleanParamName;
  245. if (inStruct)
  246. nameToSearch = structName;
  247. auto arrayIndexFind = foundFirstArrayIndex.find(nameToSearch);
  248. if (arrayIndexFind == foundFirstArrayIndex.end())
  249. {
  250. foundFirstArrayIndex[nameToSearch] = arrayIdx;
  251. }
  252. firstArrayIndex = foundFirstArrayIndex[nameToSearch];
  253. }
  254. GLint uniformType;
  255. glGetActiveUniformsiv(glProgram, 1, &index, GL_UNIFORM_TYPE, &uniformType);
  256. GpuParamObjectType samplerType = GPOT_UNKNOWN;
  257. GpuParamObjectType textureType = GPOT_UNKNOWN;
  258. bool isSampler = false;
  259. bool isImage = false;
  260. bool isBuffer = false;
  261. bool isRWBuffer = false;
  262. switch (uniformType)
  263. {
  264. case GL_SAMPLER_1D:
  265. case GL_SAMPLER_1D_SHADOW:
  266. case GL_SAMPLER_1D_ARRAY:
  267. case GL_SAMPLER_1D_ARRAY_SHADOW:
  268. case GL_UNSIGNED_INT_SAMPLER_1D:
  269. case GL_INT_SAMPLER_1D:
  270. case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY:
  271. case GL_INT_SAMPLER_1D_ARRAY:
  272. samplerType = GPOT_SAMPLER1D;
  273. textureType = GPOT_TEXTURE1D;
  274. isSampler = true;
  275. break;
  276. case GL_SAMPLER_2D:
  277. case GL_SAMPLER_2D_SHADOW:
  278. case GL_SAMPLER_2D_ARRAY:
  279. case GL_SAMPLER_2D_ARRAY_SHADOW:
  280. case GL_UNSIGNED_INT_SAMPLER_2D:
  281. case GL_INT_SAMPLER_2D:
  282. case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
  283. case GL_INT_SAMPLER_2D_ARRAY:
  284. samplerType = GPOT_SAMPLER2D;
  285. textureType = GPOT_TEXTURE2D;
  286. isSampler = true;
  287. break;
  288. case GL_SAMPLER_2D_MULTISAMPLE:
  289. case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
  290. case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
  291. case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
  292. case GL_INT_SAMPLER_2D_MULTISAMPLE:
  293. case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
  294. samplerType = GPOT_SAMPLER2DMS;
  295. textureType = GPOT_TEXTURE2DMS;
  296. isSampler = true;
  297. break;
  298. case GL_SAMPLER_3D:
  299. case GL_UNSIGNED_INT_SAMPLER_3D:
  300. case GL_INT_SAMPLER_3D:
  301. samplerType = GPOT_SAMPLER3D;
  302. textureType = GPOT_TEXTURE3D;
  303. isSampler = true;
  304. break;
  305. case GL_SAMPLER_CUBE:
  306. case GL_SAMPLER_CUBE_MAP_ARRAY:
  307. case GL_SAMPLER_CUBE_SHADOW:
  308. case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
  309. case GL_UNSIGNED_INT_SAMPLER_CUBE:
  310. case GL_INT_SAMPLER_CUBE:
  311. case GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY:
  312. case GL_INT_SAMPLER_CUBE_MAP_ARRAY:
  313. samplerType = GPOT_SAMPLERCUBE;
  314. textureType = GPOT_TEXTURECUBE;
  315. isSampler = true;
  316. break;
  317. case GL_IMAGE_1D:
  318. case GL_UNSIGNED_INT_IMAGE_1D:
  319. case GL_INT_IMAGE_1D:
  320. case GL_IMAGE_1D_ARRAY:
  321. case GL_UNSIGNED_INT_IMAGE_1D_ARRAY:
  322. case GL_INT_IMAGE_1D_ARRAY:
  323. textureType = GPOT_RWTEXTURE1D;
  324. isImage = true;
  325. break;
  326. case GL_IMAGE_2D:
  327. case GL_UNSIGNED_INT_IMAGE_2D:
  328. case GL_INT_IMAGE_2D:
  329. case GL_IMAGE_2D_ARRAY:
  330. case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
  331. case GL_INT_IMAGE_2D_ARRAY:
  332. textureType = GPOT_RWTEXTURE2D;
  333. isImage = true;
  334. break;
  335. case GL_IMAGE_2D_MULTISAMPLE:
  336. case GL_IMAGE_2D_MULTISAMPLE_ARRAY:
  337. case GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE:
  338. case GL_INT_IMAGE_2D_MULTISAMPLE:
  339. textureType = GPOT_RWTEXTURE2DMS;
  340. isImage = true;
  341. break;
  342. case GL_IMAGE_3D:
  343. case GL_UNSIGNED_INT_IMAGE_3D:
  344. case GL_INT_IMAGE_3D:
  345. textureType = GPOT_RWTEXTURE3D;
  346. isImage = true;
  347. break;
  348. case GL_SAMPLER_BUFFER:
  349. case GL_UNSIGNED_INT_SAMPLER_BUFFER:
  350. case GL_INT_SAMPLER_BUFFER:
  351. isBuffer = true;
  352. break;
  353. case GL_IMAGE_BUFFER:
  354. case GL_UNSIGNED_INT_IMAGE_BUFFER:
  355. case GL_INT_IMAGE_BUFFER:
  356. isRWBuffer = true;
  357. break;
  358. }
  359. if (isSampler)
  360. {
  361. GpuParamObjectDesc samplerParam;
  362. samplerParam.name = paramName;
  363. samplerParam.type = samplerType;
  364. samplerParam.slot = glGetUniformLocation(glProgram, uniformName);
  365. samplerParam.set = mapParameterToSet(type, ParamType::Sampler);
  366. GpuParamObjectDesc textureParam;
  367. textureParam.name = paramName;
  368. textureParam.type = textureType;
  369. textureParam.slot = samplerParam.slot;
  370. textureParam.set = mapParameterToSet(type, ParamType::Texture);
  371. returnParamDesc.samplers.insert(std::make_pair(paramName, samplerParam));
  372. returnParamDesc.textures.insert(std::make_pair(paramName, textureParam));
  373. }
  374. else if (isImage)
  375. {
  376. GpuParamObjectDesc textureParam;
  377. textureParam.name = paramName;
  378. textureParam.type = textureType;
  379. textureParam.slot = glGetUniformLocation(glProgram, uniformName);
  380. textureParam.set = mapParameterToSet(type, ParamType::Image);
  381. returnParamDesc.loadStoreTextures.insert(std::make_pair(paramName, textureParam));
  382. }
  383. else if (isBuffer)
  384. {
  385. GpuParamObjectDesc bufferParam;
  386. bufferParam.name = paramName;
  387. bufferParam.type = GPOT_BYTE_BUFFER;
  388. bufferParam.slot = glGetUniformLocation(glProgram, uniformName);
  389. bufferParam.set = mapParameterToSet(type, ParamType::Texture);
  390. returnParamDesc.buffers.insert(std::make_pair(paramName, bufferParam));
  391. }
  392. else if(isRWBuffer)
  393. {
  394. GpuParamObjectDesc bufferParam;
  395. bufferParam.name = paramName;
  396. bufferParam.type = GPOT_RWBYTE_BUFFER;
  397. bufferParam.slot = glGetUniformLocation(glProgram, uniformName);
  398. bufferParam.set = mapParameterToSet(type, ParamType::Image);
  399. returnParamDesc.buffers.insert(std::make_pair(paramName, bufferParam));
  400. }
  401. else
  402. {
  403. // If array index is larger than 0 and uniform is not a part of a struct,
  404. // it means we already processed it (struct arrays are processed differently)
  405. if (!inStruct && arrayIdx != 0)
  406. continue;
  407. GLint blockIndex;
  408. glGetActiveUniformsiv(glProgram, 1, &index, GL_UNIFORM_BLOCK_INDEX, &blockIndex);
  409. GpuParamDataDesc gpuParam;
  410. if (isInArray)
  411. gpuParam.name = cleanParamName;
  412. else
  413. gpuParam.name = paramName;
  414. determineParamInfo(gpuParam, paramName, glProgram, index);
  415. if (blockIndex != -1)
  416. {
  417. GLint blockOffset;
  418. glGetActiveUniformsiv(glProgram, 1, &index, GL_UNIFORM_OFFSET, &blockOffset);
  419. blockOffset = blockOffset / 4;
  420. gpuParam.gpuMemOffset = blockOffset;
  421. String& blockName = blockSlotToName[blockIndex + 1];
  422. GpuParamBlockDesc& curBlockDesc = returnParamDesc.paramBlocks[blockName];
  423. gpuParam.paramBlockSlot = curBlockDesc.slot;
  424. gpuParam.paramBlockSet = mapParameterToSet(type, ParamType::UniformBlock);
  425. gpuParam.cpuMemOffset = blockOffset;
  426. curBlockDesc.blockSize = std::max(curBlockDesc.blockSize, gpuParam.cpuMemOffset + gpuParam.arrayElementStride * gpuParam.arraySize);
  427. }
  428. else
  429. {
  430. gpuParam.gpuMemOffset = glGetUniformLocation(glProgram, uniformName);
  431. gpuParam.paramBlockSlot = 0;
  432. gpuParam.paramBlockSet = mapParameterToSet(type, ParamType::UniformBlock);
  433. gpuParam.cpuMemOffset = globalBlockDesc.blockSize;
  434. globalBlockDesc.blockSize = std::max(globalBlockDesc.blockSize, gpuParam.cpuMemOffset + gpuParam.arrayElementStride * gpuParam.arraySize);
  435. }
  436. // If parameter is not a part of a struct we're done
  437. if (!inStruct)
  438. {
  439. returnParamDesc.params.insert(std::make_pair(gpuParam.name, gpuParam));
  440. continue;
  441. }
  442. // If the parameter is part of a struct, then we need to update the struct definition
  443. auto findExistingStruct = foundStructs.find(structName);
  444. // Create new definition if one doesn't exist
  445. if (findExistingStruct == foundStructs.end())
  446. {
  447. foundStructs[structName] = GpuParamDataDesc();
  448. GpuParamDataDesc& structDesc = foundStructs[structName];
  449. structDesc.type = GPDT_STRUCT;
  450. structDesc.name = structName;
  451. structDesc.arraySize = 1;
  452. structDesc.elementSize = 0;
  453. structDesc.arrayElementStride = 0;
  454. structDesc.gpuMemOffset = gpuParam.gpuMemOffset;
  455. structDesc.cpuMemOffset = gpuParam.cpuMemOffset;
  456. structDesc.paramBlockSlot = gpuParam.paramBlockSlot;
  457. structDesc.paramBlockSet = gpuParam.paramBlockSet;
  458. }
  459. // Update struct with size of the new parameter
  460. GpuParamDataDesc& structDesc = foundStructs[structName];
  461. assert(gpuParam.cpuMemOffset >= structDesc.cpuMemOffset);
  462. if (arrayIdx == firstArrayIndex) // Determine element size only using the first array element
  463. {
  464. structDesc.elementSize = std::max(structDesc.elementSize, (gpuParam.cpuMemOffset - structDesc.cpuMemOffset) + gpuParam.arrayElementStride * gpuParam.arraySize);
  465. structDesc.arrayElementStride = structDesc.elementSize;
  466. }
  467. // New array element reached, determine arrayElementStride
  468. if (arrayIdx != firstArrayIndex)
  469. {
  470. UINT32 numElements = arrayIdx - firstArrayIndex;
  471. structDesc.arrayElementStride = (gpuParam.cpuMemOffset - structDesc.cpuMemOffset) / numElements;
  472. }
  473. structDesc.arraySize = std::max(structDesc.arraySize, arrayIdx + 1);
  474. }
  475. }
  476. for (auto iter = foundStructs.begin(); iter != foundStructs.end(); ++iter)
  477. returnParamDesc.params.insert(std::make_pair(iter->first, iter->second));
  478. // Param blocks always need to be a multiple of 4, so make it so
  479. for (auto iter = returnParamDesc.paramBlocks.begin(); iter != returnParamDesc.paramBlocks.end(); ++iter)
  480. {
  481. GpuParamBlockDesc& blockDesc = iter->second;
  482. if (blockDesc.blockSize % 4 != 0)
  483. blockDesc.blockSize += (4 - (blockDesc.blockSize % 4));
  484. }
  485. #if BS_DEBUG_MODE
  486. // Check if manually calculated and OpenGL buffer sizes match
  487. UINT32 blockIdx = 0;
  488. for (auto iter = returnParamDesc.paramBlocks.begin(); iter != returnParamDesc.paramBlocks.end(); ++iter)
  489. {
  490. if (iter->second.slot == 0)
  491. continue;
  492. GLint blockSize = 0;
  493. glGetActiveUniformBlockiv(glProgram, blockIdx, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
  494. assert(blockSize % 4 == 0);
  495. blockSize = blockSize / 4;
  496. if (iter->second.blockSize != blockSize)
  497. BS_EXCEPT(InternalErrorException, "OpenGL specified and manual uniform block buffer sizes don't match!");
  498. blockIdx++;
  499. }
  500. #endif
  501. bs_free(uniformName);
  502. }
  503. void GLSLParamParser::determineParamInfo(GpuParamDataDesc& desc, const String& paramName, GLuint programHandle, GLuint uniformIndex)
  504. {
  505. GLint arraySize;
  506. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_SIZE, &arraySize);
  507. desc.arraySize = arraySize;
  508. GLint uniformType;
  509. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_TYPE, &uniformType);
  510. switch (uniformType)
  511. {
  512. case GL_BOOL:
  513. desc.type = GPDT_BOOL;
  514. desc.elementSize = 1;
  515. break;
  516. case GL_FLOAT:
  517. desc.type = GPDT_FLOAT1;
  518. desc.elementSize = 1;
  519. break;
  520. case GL_FLOAT_VEC2:
  521. desc.type = GPDT_FLOAT2;
  522. desc.elementSize = 2;
  523. break;
  524. case GL_FLOAT_VEC3:
  525. desc.type = GPDT_FLOAT3;
  526. desc.elementSize = 3;
  527. break;
  528. case GL_FLOAT_VEC4:
  529. desc.type = GPDT_FLOAT4;
  530. desc.elementSize = 4;
  531. break;
  532. case GL_INT:
  533. case GL_UNSIGNED_INT:
  534. desc.type = GPDT_INT1;
  535. desc.elementSize = 1;
  536. break;
  537. case GL_INT_VEC2:
  538. case GL_UNSIGNED_INT_VEC2:
  539. desc.type = GPDT_INT2;
  540. desc.elementSize = 2;
  541. break;
  542. case GL_INT_VEC3:
  543. case GL_UNSIGNED_INT_VEC3:
  544. desc.type = GPDT_INT3;
  545. desc.elementSize = 3;
  546. break;
  547. case GL_INT_VEC4:
  548. case GL_UNSIGNED_INT_VEC4:
  549. desc.type = GPDT_INT4;
  550. desc.elementSize = 4;
  551. break;
  552. case GL_FLOAT_MAT2:
  553. desc.type = GPDT_MATRIX_2X2;
  554. desc.elementSize = 4;
  555. break;
  556. case GL_FLOAT_MAT3:
  557. desc.type = GPDT_MATRIX_3X3;
  558. desc.elementSize = 9;
  559. break;
  560. case GL_FLOAT_MAT4:
  561. desc.type = GPDT_MATRIX_4X4;
  562. desc.elementSize = 16;
  563. break;
  564. case GL_FLOAT_MAT2x3:
  565. desc.type = GPDT_MATRIX_2X3;
  566. desc.elementSize = 6;
  567. break;
  568. case GL_FLOAT_MAT3x2:
  569. desc.type = GPDT_MATRIX_3X2;
  570. desc.elementSize = 6;
  571. break;
  572. case GL_FLOAT_MAT2x4:
  573. desc.type = GPDT_MATRIX_2X4;
  574. desc.elementSize = 8;
  575. break;
  576. case GL_FLOAT_MAT4x2:
  577. desc.type = GPDT_MATRIX_4X2;
  578. desc.elementSize = 8;
  579. break;
  580. case GL_FLOAT_MAT3x4:
  581. desc.type = GPDT_MATRIX_3X4;
  582. desc.elementSize = 12;
  583. break;
  584. case GL_FLOAT_MAT4x3:
  585. desc.type = GPDT_MATRIX_4X3;
  586. desc.elementSize = 12;
  587. break;
  588. default:
  589. BS_EXCEPT(InternalErrorException, "Invalid shader parameter type: " + toString(uniformType) + " for parameter " + paramName);
  590. }
  591. if (arraySize > 1)
  592. {
  593. GLint arrayStride;
  594. glGetActiveUniformsiv(programHandle, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE, &arrayStride);
  595. if (arrayStride > 0)
  596. {
  597. assert(arrayStride % 4 == 0);
  598. desc.arrayElementStride = arrayStride / 4;
  599. }
  600. else
  601. desc.arrayElementStride = desc.elementSize;
  602. }
  603. else
  604. desc.arrayElementStride = desc.elementSize;
  605. }
  606. UINT32 GLSLParamParser::mapParameterToSet(GpuProgramType progType, ParamType paramType)
  607. {
  608. UINT32 progTypeIdx = (UINT32)progType;
  609. UINT32 paramTypeIdx = (UINT32)paramType;
  610. return progTypeIdx * (UINT32)ParamType::Count + paramTypeIdx;
  611. }
  612. }}