State.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "State.h"
  21. #include "VertexBuffer.h"
  22. #include "IndexBuffer.h"
  23. #include "Texture2D.h"
  24. #include "Program.h"
  25. RENDERER_BEGIN
  26. State::State()
  27. {
  28. _textureUnits.resize(10);
  29. _vertexBuffers.resize(10);
  30. _vertexBufferOffsets.resize(10);
  31. reset();
  32. }
  33. State::~State()
  34. {
  35. for (auto vertexBuf : _vertexBuffers)
  36. {
  37. RENDERER_SAFE_RELEASE(vertexBuf);
  38. }
  39. RENDERER_SAFE_RELEASE(_indexBuffer);
  40. for (auto texture : _textureUnits)
  41. {
  42. RENDERER_SAFE_RELEASE(texture);
  43. }
  44. RENDERER_SAFE_RELEASE(_program);
  45. }
  46. void State::reset()
  47. {
  48. blend = false;
  49. blendSeparation = false;
  50. blendColor = 0xFFFFFFFF;
  51. blendEq = BlendOp::ADD;
  52. blendAlphaEq = BlendOp::ADD;
  53. blendSrc = BlendFactor::ONE;
  54. blendDst = BlendFactor::ZERO;
  55. blendSrcAlpha = BlendFactor::ONE;
  56. blendDstAlpha = BlendFactor::ZERO;
  57. // depth
  58. depthTest = false;
  59. depthWrite = false;
  60. depthFunc = DepthFunc::LESS;
  61. // stencil
  62. stencilTest = false;
  63. stencilSeparation = false;
  64. stencilFuncFront = StencilFunc::ALWAYS;
  65. stencilRefFront = 0;
  66. stencilMaskFront = 0xFF;
  67. stencilFailOpFront = StencilOp::KEEP;
  68. stencilZFailOpFront = StencilOp::KEEP;
  69. stencilZPassOpFront = StencilOp::KEEP;
  70. stencilWriteMaskFront = 0xFF;
  71. stencilFuncBack = StencilFunc::ALWAYS;
  72. stencilRefBack = 0;
  73. stencilMaskBack = 0xFF;
  74. stencilFailOpBack = StencilOp::KEEP;
  75. stencilZFailOpBack = StencilOp::KEEP;
  76. stencilZPassOpBack = StencilOp::KEEP;
  77. stencilWriteMaskBack = 0xFF;
  78. // cull-mode
  79. cullMode = CullMode::BACK;
  80. // primitive-type
  81. primitiveType = PrimitiveType::TRIANGLES;
  82. // bindings
  83. maxStream = -1;
  84. for (auto i = 0; i < _textureUnits.size(); i++) {
  85. if (_textureUnits[i]) {
  86. RENDERER_SAFE_RELEASE(_textureUnits[i]);
  87. }
  88. _textureUnits[i] = nullptr;
  89. }
  90. for (auto i = 0; i < _vertexBuffers.size(); i++) {
  91. if (_vertexBuffers[i]) {
  92. RENDERER_SAFE_RELEASE(_vertexBuffers[i]);
  93. }
  94. _vertexBuffers[i] = nullptr;
  95. }
  96. if (_indexBuffer)
  97. {
  98. RENDERER_SAFE_RELEASE(_indexBuffer);
  99. }
  100. _indexBuffer = nullptr;
  101. if (_program)
  102. {
  103. RENDERER_SAFE_RELEASE(_program);
  104. }
  105. _program = nullptr;
  106. }
  107. void State::setVertexBuffer(size_t index, VertexBuffer* vertBuf)
  108. {
  109. if (index >= _vertexBuffers.size())
  110. {
  111. _vertexBuffers.resize(index + 1);
  112. }
  113. VertexBuffer* oldBuf = _vertexBuffers[index];
  114. if (oldBuf != vertBuf)
  115. {
  116. RENDERER_SAFE_RELEASE(oldBuf);
  117. _vertexBuffers[index] = vertBuf;
  118. RENDERER_SAFE_RETAIN(vertBuf);
  119. }
  120. }
  121. VertexBuffer* State::getVertexBuffer(size_t index) const
  122. {
  123. if (_vertexBuffers.empty())
  124. return nullptr;
  125. assert(index < _vertexBuffers.size());
  126. return _vertexBuffers[index];
  127. }
  128. void State::setVertexBufferOffset(size_t index, int32_t offset)
  129. {
  130. if (index >= _vertexBufferOffsets.size())
  131. {
  132. _vertexBufferOffsets.resize(index + 1);
  133. }
  134. _vertexBufferOffsets[index] = offset;
  135. }
  136. int32_t State::getVertexBufferOffset(size_t index) const
  137. {
  138. assert(index < _vertexBufferOffsets.size());
  139. return _vertexBufferOffsets[index];
  140. }
  141. void State::setIndexBuffer(IndexBuffer* indexBuf)
  142. {
  143. if (_indexBuffer != indexBuf)
  144. {
  145. RENDERER_SAFE_RELEASE(_indexBuffer);
  146. _indexBuffer = indexBuf;
  147. RENDERER_SAFE_RETAIN(_indexBuffer);
  148. }
  149. }
  150. IndexBuffer* State::getIndexBuffer() const
  151. {
  152. return _indexBuffer;
  153. }
  154. void State::setTexture(size_t index, Texture* texture)
  155. {
  156. if (index >= _textureUnits.size())
  157. {
  158. _textureUnits.resize(index + 1);
  159. }
  160. Texture* oldTexture = _textureUnits[index];
  161. if (oldTexture != texture)
  162. {
  163. RENDERER_SAFE_RELEASE(oldTexture);
  164. _textureUnits[index] = texture;
  165. RENDERER_SAFE_RETAIN(texture);
  166. }
  167. }
  168. Texture* State::getTexture(size_t index) const
  169. {
  170. if (_textureUnits.empty())
  171. return nullptr;
  172. assert(index < _textureUnits.size());
  173. return _textureUnits[index];
  174. }
  175. void State::setProgram(Program* program)
  176. {
  177. assert(program != nullptr);
  178. if (_program != program)
  179. {
  180. RENDERER_SAFE_RELEASE(_program);
  181. _program = program;
  182. RENDERER_SAFE_RETAIN(_program);
  183. }
  184. }
  185. Program* State::getProgram() const
  186. {
  187. return _program;
  188. }
  189. RENDERER_END