CCTrianglesCommand.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "renderer/CCTrianglesCommand.h"
  22. #include "renderer/ccGLStateCache.h"
  23. #include "renderer/CCGLProgram.h"
  24. #include "renderer/CCGLProgramState.h"
  25. #include "xxhash.h"
  26. #include "renderer/CCRenderer.h"
  27. #include "renderer/CCTexture2D.h"
  28. NS_CC_BEGIN
  29. TrianglesCommand::TrianglesCommand()
  30. :_materialID(0)
  31. ,_textureID(0)
  32. ,_glProgramState(nullptr)
  33. ,_blendType(BlendFunc::DISABLE)
  34. ,_alphaTextureID(0)
  35. {
  36. _type = RenderCommand::Type::TRIANGLES_COMMAND;
  37. }
  38. void TrianglesCommand::init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, const Triangles& triangles,const Mat4& mv, uint32_t flags)
  39. {
  40. CCASSERT(glProgramState, "Invalid GLProgramState");
  41. CCASSERT(glProgramState->getVertexAttribsFlags() == 0, "No custom attributes are supported in QuadCommand");
  42. RenderCommand::init(globalOrder, mv, flags);
  43. _triangles = triangles;
  44. if(_triangles.indexCount % 3 != 0)
  45. {
  46. int count = _triangles.indexCount;
  47. _triangles.indexCount = count / 3 * 3;
  48. CCLOGERROR("Resize indexCount from %zd to %zd, size must be multiple times of 3", count, _triangles.indexCount);
  49. }
  50. _mv = mv;
  51. if( _textureID != textureID || _blendType.src != blendType.src || _blendType.dst != blendType.dst ||
  52. _glProgramState != glProgramState)
  53. {
  54. _textureID = textureID;
  55. _blendType = blendType;
  56. _glProgramState = glProgramState;
  57. generateMaterialID();
  58. }
  59. }
  60. void TrianglesCommand::init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, const Triangles& triangles,const Mat4& mv)
  61. {
  62. init(globalOrder, textureID, glProgramState, blendType, triangles, mv, 0);
  63. }
  64. void TrianglesCommand::init(float globalOrder, Texture2D* texture, GLProgramState* glProgramState, BlendFunc blendType, const Triangles& triangles, const Mat4& mv, uint32_t flags)
  65. {
  66. init(globalOrder, texture->getName(), glProgramState, blendType, triangles, mv, flags);
  67. _alphaTextureID = texture->getAlphaTextureName();
  68. }
  69. TrianglesCommand::~TrianglesCommand()
  70. {
  71. }
  72. void TrianglesCommand::generateMaterialID()
  73. {
  74. // glProgramState is hashed because it contains:
  75. // * uniforms/values
  76. // * glProgram
  77. //
  78. // we safely can when the same glProgramState is being used then they share those states
  79. // if they don't have the same glProgramState, they might still have the same
  80. // uniforms/values and glProgram, but it would be too expensive to check the uniforms.
  81. struct {
  82. void* glProgramState;
  83. GLuint textureId;
  84. GLenum blendSrc;
  85. GLenum blendDst;
  86. } hashMe;
  87. // NOTE: Initialize hashMe struct to make the value of padding bytes be filled with zero.
  88. // It's important since XXH32 below will also consider the padding bytes which probably
  89. // are set to random values by different compilers.
  90. memset(&hashMe, 0, sizeof(hashMe));
  91. hashMe.textureId = _textureID;
  92. hashMe.blendSrc = _blendType.src;
  93. hashMe.blendDst = _blendType.dst;
  94. hashMe.glProgramState = _glProgramState;
  95. _materialID = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
  96. }
  97. void TrianglesCommand::useMaterial() const
  98. {
  99. //Set texture
  100. GL::bindTexture2D(_textureID);
  101. if (_alphaTextureID > 0)
  102. { // ANDROID ETC1 ALPHA supports.
  103. GL::bindTexture2DN(1, _alphaTextureID);
  104. }
  105. //set blend mode
  106. GL::blendFunc(_blendType.src, _blendType.dst);
  107. _glProgramState->apply(_mv);
  108. }
  109. NS_CC_END