PageRenderTime 154ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/ThirdParty/ANGLE/src/libANGLE/renderer/gl/ProgramGL.cpp

https://gitlab.com/paretje/qtwebkit
C++ | 419 lines | 319 code | 77 blank | 23 comment | 13 complexity | 7edae952da5ef5ecf18756130dd9f515 MD5 | raw file
  1. //
  2. // Copyright 2015 The ANGLE Project Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style license that can be
  4. // found in the LICENSE file.
  5. //
  6. // ProgramGL.cpp: Implements the class methods for ProgramGL.
  7. #include "libANGLE/renderer/gl/ProgramGL.h"
  8. #include "common/debug.h"
  9. #include "common/utilities.h"
  10. #include "libANGLE/renderer/gl/FunctionsGL.h"
  11. #include "libANGLE/renderer/gl/ShaderGL.h"
  12. #include "libANGLE/renderer/gl/StateManagerGL.h"
  13. namespace rx
  14. {
  15. ProgramGL::ProgramGL(const FunctionsGL *functions, StateManagerGL *stateManager)
  16. : ProgramImpl(),
  17. mFunctions(functions),
  18. mStateManager(stateManager),
  19. mProgramID(0)
  20. {
  21. ASSERT(mFunctions);
  22. ASSERT(mStateManager);
  23. }
  24. ProgramGL::~ProgramGL()
  25. {
  26. if (mProgramID != 0)
  27. {
  28. mFunctions->deleteProgram(mProgramID);
  29. mProgramID = 0;
  30. }
  31. }
  32. bool ProgramGL::usesPointSize() const
  33. {
  34. UNIMPLEMENTED();
  35. return bool();
  36. }
  37. int ProgramGL::getShaderVersion() const
  38. {
  39. UNIMPLEMENTED();
  40. return int();
  41. }
  42. GLenum ProgramGL::getTransformFeedbackBufferMode() const
  43. {
  44. UNIMPLEMENTED();
  45. return GLenum();
  46. }
  47. GLenum ProgramGL::getBinaryFormat()
  48. {
  49. UNIMPLEMENTED();
  50. return GLenum();
  51. }
  52. LinkResult ProgramGL::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
  53. {
  54. UNIMPLEMENTED();
  55. return LinkResult(false, gl::Error(GL_INVALID_OPERATION));
  56. }
  57. gl::Error ProgramGL::save(gl::BinaryOutputStream *stream)
  58. {
  59. UNIMPLEMENTED();
  60. return gl::Error(GL_INVALID_OPERATION);
  61. }
  62. LinkResult ProgramGL::link(const gl::Data &data, gl::InfoLog &infoLog,
  63. gl::Shader *fragmentShader, gl::Shader *vertexShader,
  64. const std::vector<std::string> &transformFeedbackVaryings,
  65. GLenum transformFeedbackBufferMode,
  66. int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
  67. std::map<int, gl::VariableLocation> *outputVariables)
  68. {
  69. // Reset the program state, delete the current program if one exists
  70. reset();
  71. ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(vertexShader);
  72. ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(fragmentShader);
  73. // Generate a new program, make sure one doesn't already exist
  74. ASSERT(mProgramID == 0);
  75. mProgramID = mFunctions->createProgram();
  76. // Attach the shaders
  77. mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID());
  78. mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID());
  79. // TODO: bind attribute locations?
  80. // Link and verify
  81. mFunctions->linkProgram(mProgramID);
  82. GLint linkStatus = GL_FALSE;
  83. mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
  84. ASSERT(linkStatus == GL_TRUE);
  85. if (linkStatus == GL_FALSE)
  86. {
  87. // Linking failed, put the error into the info log
  88. GLint infoLogLength = 0;
  89. mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
  90. std::vector<char> buf(infoLogLength);
  91. mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]);
  92. mFunctions->deleteProgram(mProgramID);
  93. mProgramID = 0;
  94. infoLog.append(&buf[0]);
  95. TRACE("\n%s", &buf[0]);
  96. // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case
  97. return LinkResult(false, gl::Error(GL_NO_ERROR));
  98. }
  99. // Query the uniform information
  100. // TODO: A lot of this logic should be done at the gl::Program level
  101. GLint activeUniformMaxLength = 0;
  102. mFunctions->getProgramiv(mProgramID, GL_ACTIVE_UNIFORM_MAX_LENGTH, &activeUniformMaxLength);
  103. std::vector<GLchar> uniformNameBuffer(activeUniformMaxLength);
  104. GLint uniformCount = 0;
  105. mFunctions->getProgramiv(mProgramID, GL_ACTIVE_UNIFORMS, &uniformCount);
  106. for (GLint i = 0; i < uniformCount; i++)
  107. {
  108. GLsizei uniformNameLength = 0;
  109. GLint uniformSize = 0;
  110. GLenum uniformType = GL_NONE;
  111. mFunctions->getActiveUniform(mProgramID, i, uniformNameBuffer.size(), &uniformNameLength, &uniformSize, &uniformType, &uniformNameBuffer[0]);
  112. std::string uniformName = gl::ParseUniformName(std::string(&uniformNameBuffer[0], uniformNameLength), nullptr);
  113. for (size_t arrayIndex = 0; arrayIndex < static_cast<size_t>(uniformSize); arrayIndex++)
  114. {
  115. std::string locationName = uniformName;
  116. if (uniformSize > 1)
  117. {
  118. locationName += "[" + Str(arrayIndex) + "]";
  119. }
  120. GLint location = mFunctions->getUniformLocation(mProgramID, locationName.c_str());
  121. if (location >= 0)
  122. {
  123. // Make sure the uniform index array is large enough
  124. if (static_cast<size_t>(location) >= mUniformIndex.size())
  125. {
  126. mUniformIndex.resize(location + 1);
  127. }
  128. mUniformIndex[location] = gl::VariableLocation(uniformName, arrayIndex, static_cast<unsigned int>(mUniforms.size()));
  129. }
  130. }
  131. // ANGLE uses 0 to identify an non-array uniform.
  132. unsigned int arraySize = (uniformSize > 1) ? static_cast<unsigned int>(uniformSize) : 0;
  133. // TODO: determine uniform precision
  134. mUniforms.push_back(new gl::LinkedUniform(uniformType, GL_NONE, uniformName, arraySize, -1, sh::BlockMemberInfo::getDefaultBlockInfo()));
  135. }
  136. // Query the attribute information
  137. GLint activeAttributeMaxLength = 0;
  138. mFunctions->getProgramiv(mProgramID, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &activeAttributeMaxLength);
  139. std::vector<GLchar> attributeNameBuffer(activeAttributeMaxLength);
  140. GLint attributeCount = 0;
  141. mFunctions->getProgramiv(mProgramID, GL_ACTIVE_ATTRIBUTES, &attributeCount);
  142. for (GLint i = 0; i < attributeCount; i++)
  143. {
  144. GLsizei attributeNameLength = 0;
  145. GLint attributeSize = 0;
  146. GLenum attributeType = GL_NONE;
  147. mFunctions->getActiveAttrib(mProgramID, i, attributeNameBuffer.size(), &attributeNameLength, &attributeSize, &attributeType, &attributeNameBuffer[0]);
  148. std::string attributeName(&attributeNameBuffer[0], attributeNameLength);
  149. // TODO: determine attribute precision
  150. setShaderAttribute(static_cast<size_t>(i), attributeType, GL_NONE, attributeName, attributeSize, i);
  151. }
  152. return LinkResult(true, gl::Error(GL_NO_ERROR));
  153. }
  154. void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
  155. {
  156. mStateManager->useProgram(mProgramID);
  157. mFunctions->uniform1fv(location, count, v);
  158. }
  159. void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
  160. {
  161. mStateManager->useProgram(mProgramID);
  162. mFunctions->uniform2fv(location, count, v);
  163. }
  164. void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
  165. {
  166. mStateManager->useProgram(mProgramID);
  167. mFunctions->uniform3fv(location, count, v);
  168. }
  169. void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
  170. {
  171. mStateManager->useProgram(mProgramID);
  172. mFunctions->uniform4fv(location, count, v);
  173. }
  174. void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v)
  175. {
  176. mStateManager->useProgram(mProgramID);
  177. mFunctions->uniform1iv(location, count, v);
  178. }
  179. void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *v)
  180. {
  181. mStateManager->useProgram(mProgramID);
  182. mFunctions->uniform2iv(location, count, v);
  183. }
  184. void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v)
  185. {
  186. mStateManager->useProgram(mProgramID);
  187. mFunctions->uniform3iv(location, count, v);
  188. }
  189. void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v)
  190. {
  191. mStateManager->useProgram(mProgramID);
  192. mFunctions->uniform4iv(location, count, v);
  193. }
  194. void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
  195. {
  196. mStateManager->useProgram(mProgramID);
  197. mFunctions->uniform1uiv(location, count, v);
  198. }
  199. void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
  200. {
  201. mStateManager->useProgram(mProgramID);
  202. mFunctions->uniform2uiv(location, count, v);
  203. }
  204. void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
  205. {
  206. mStateManager->useProgram(mProgramID);
  207. mFunctions->uniform3uiv(location, count, v);
  208. }
  209. void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
  210. {
  211. mStateManager->useProgram(mProgramID);
  212. mFunctions->uniform4uiv(location, count, v);
  213. }
  214. void ProgramGL::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  215. {
  216. mStateManager->useProgram(mProgramID);
  217. mFunctions->uniformMatrix2fv(location, count, transpose, value);
  218. }
  219. void ProgramGL::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  220. {
  221. mStateManager->useProgram(mProgramID);
  222. mFunctions->uniformMatrix3fv(location, count, transpose, value);
  223. }
  224. void ProgramGL::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  225. {
  226. mStateManager->useProgram(mProgramID);
  227. mFunctions->uniformMatrix4fv(location, count, transpose, value);
  228. }
  229. void ProgramGL::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  230. {
  231. mStateManager->useProgram(mProgramID);
  232. mFunctions->uniformMatrix2x3fv(location, count, transpose, value);
  233. }
  234. void ProgramGL::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  235. {
  236. mStateManager->useProgram(mProgramID);
  237. mFunctions->uniformMatrix3x2fv(location, count, transpose, value);
  238. }
  239. void ProgramGL::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  240. {
  241. mStateManager->useProgram(mProgramID);
  242. mFunctions->uniformMatrix2x4fv(location, count, transpose, value);
  243. }
  244. void ProgramGL::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  245. {
  246. mStateManager->useProgram(mProgramID);
  247. mFunctions->uniformMatrix4x2fv(location, count, transpose, value);
  248. }
  249. void ProgramGL::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  250. {
  251. mStateManager->useProgram(mProgramID);
  252. mFunctions->uniformMatrix3x4fv(location, count, transpose, value);
  253. }
  254. void ProgramGL::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
  255. {
  256. mStateManager->useProgram(mProgramID);
  257. mFunctions->uniformMatrix4x3fv(location, count, transpose, value);
  258. }
  259. void ProgramGL::getUniformfv(GLint location, GLfloat *params)
  260. {
  261. mFunctions->getUniformfv(mProgramID, location, params);
  262. }
  263. void ProgramGL::getUniformiv(GLint location, GLint *params)
  264. {
  265. mFunctions->getUniformiv(mProgramID, location, params);
  266. }
  267. void ProgramGL::getUniformuiv(GLint location, GLuint *params)
  268. {
  269. mFunctions->getUniformuiv(mProgramID, location, params);
  270. }
  271. GLint ProgramGL::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
  272. {
  273. UNIMPLEMENTED();
  274. return GLint();
  275. }
  276. GLenum ProgramGL::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
  277. {
  278. UNIMPLEMENTED();
  279. return GLenum();
  280. }
  281. GLint ProgramGL::getUsedSamplerRange(gl::SamplerType type) const
  282. {
  283. UNIMPLEMENTED();
  284. return GLint();
  285. }
  286. void ProgramGL::updateSamplerMapping()
  287. {
  288. UNIMPLEMENTED();
  289. }
  290. bool ProgramGL::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
  291. {
  292. //UNIMPLEMENTED();
  293. return true;
  294. }
  295. LinkResult ProgramGL::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
  296. int registers)
  297. {
  298. //UNIMPLEMENTED();
  299. return LinkResult(true, gl::Error(GL_NO_ERROR));
  300. }
  301. bool ProgramGL::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
  302. const gl::Caps &caps)
  303. {
  304. //UNIMPLEMENTED();
  305. return true;
  306. }
  307. bool ProgramGL::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
  308. const gl::Caps &caps)
  309. {
  310. UNIMPLEMENTED();
  311. return bool();
  312. }
  313. gl::Error ProgramGL::applyUniforms()
  314. {
  315. UNIMPLEMENTED();
  316. return gl::Error(GL_INVALID_OPERATION);
  317. }
  318. gl::Error ProgramGL::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
  319. {
  320. UNIMPLEMENTED();
  321. return gl::Error(GL_INVALID_OPERATION);
  322. }
  323. bool ProgramGL::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
  324. unsigned int registerIndex, const gl::Caps &caps)
  325. {
  326. UNIMPLEMENTED();
  327. return bool();
  328. }
  329. void ProgramGL::reset()
  330. {
  331. ProgramImpl::reset();
  332. if (mProgramID)
  333. {
  334. mFunctions->deleteProgram(mProgramID);
  335. mProgramID = 0;
  336. }
  337. }
  338. GLuint ProgramGL::getProgramID() const
  339. {
  340. return mProgramID;
  341. }
  342. }