DepthStencilTarget.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "Base.h"
  2. #include "DepthStencilTarget.h"
  3. #ifndef GL_DEPTH24_STENCIL8_OES
  4. #define GL_DEPTH24_STENCIL8_OES 0x88F0
  5. #endif
  6. #ifndef GL_DEPTH_COMPONENT24
  7. #define GL_DEPTH_COMPONENT24 GL_DEPTH_COMPONENT24_OES
  8. #endif
  9. namespace gameplay
  10. {
  11. static std::vector<DepthStencilTarget*> __depthStencilTargets;
  12. DepthStencilTarget::DepthStencilTarget(const char* id, Format format, unsigned int width, unsigned int height)
  13. : _id(id ? id : ""), _format(format), _depthBuffer(0), _stencilBuffer(0), _width(width), _height(height), _packed(false)
  14. {
  15. }
  16. DepthStencilTarget::~DepthStencilTarget()
  17. {
  18. // Destroy GL resources.
  19. if (_depthBuffer)
  20. GL_ASSERT( glDeleteRenderbuffers(1, &_depthBuffer) );
  21. if (_stencilBuffer)
  22. GL_ASSERT( glDeleteRenderbuffers(1, &_stencilBuffer) );
  23. // Remove from vector.
  24. std::vector<DepthStencilTarget*>::iterator it = std::find(__depthStencilTargets.begin(), __depthStencilTargets.end(), this);
  25. if (it != __depthStencilTargets.end())
  26. {
  27. __depthStencilTargets.erase(it);
  28. }
  29. }
  30. DepthStencilTarget* DepthStencilTarget::create(const char* id, Format format, unsigned int width, unsigned int height)
  31. {
  32. // Create the depth stencil target.
  33. DepthStencilTarget* depthStencilTarget = new DepthStencilTarget(id, format, width, height);
  34. // Create a render buffer for this new depth+stencil target
  35. GL_ASSERT( glGenRenderbuffers(1, &depthStencilTarget->_depthBuffer) );
  36. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, depthStencilTarget->_depthBuffer) );
  37. // First try to add storage for the most common standard GL_DEPTH24_STENCIL8
  38. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
  39. // Fall back to less common GLES2 extension combination for seperate depth24 + stencil8 or depth16 + stencil8
  40. __gl_error_code = glGetError();
  41. if ( __gl_error_code != GL_NO_ERROR)
  42. {
  43. const char* extString = (const char*)glGetString(GL_EXTENSIONS);
  44. if (strstr(extString, "GL_OES_packed_depth_stencil") != 0)
  45. {
  46. GL_ASSERT( glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, width, height) );
  47. depthStencilTarget->_packed = true;
  48. }
  49. else
  50. {
  51. if (strstr(extString, "GL_OES_depth24") != 0)
  52. {
  53. GL_ASSERT( glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height) );
  54. }
  55. else
  56. {
  57. GL_ASSERT( glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height) );
  58. }
  59. if (format == DepthStencilTarget::DEPTH_STENCIL)
  60. {
  61. GL_ASSERT( glGenRenderbuffers(1, &depthStencilTarget->_stencilBuffer) );
  62. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, depthStencilTarget->_stencilBuffer) );
  63. GL_ASSERT( glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height) );
  64. }
  65. }
  66. }
  67. else
  68. {
  69. // Packed format GL_DEPTH24_STENCIL8 is used mark format as packed.
  70. depthStencilTarget->_packed = true;
  71. }
  72. // Add it to the cache.
  73. __depthStencilTargets.push_back(depthStencilTarget);
  74. return depthStencilTarget;
  75. }
  76. DepthStencilTarget* DepthStencilTarget::getDepthStencilTarget(const char* id)
  77. {
  78. GP_ASSERT(id);
  79. // Search the vector for a matching ID.
  80. std::vector<DepthStencilTarget*>::const_iterator it;
  81. for (it = __depthStencilTargets.begin(); it < __depthStencilTargets.end(); it++)
  82. {
  83. DepthStencilTarget* dst = *it;
  84. GP_ASSERT(dst);
  85. if (strcmp(id, dst->getId()) == 0)
  86. {
  87. return dst;
  88. }
  89. }
  90. return NULL;
  91. }
  92. const char* DepthStencilTarget::getId() const
  93. {
  94. return _id.c_str();
  95. }
  96. DepthStencilTarget::Format DepthStencilTarget::getFormat() const
  97. {
  98. return _format;
  99. }
  100. unsigned int DepthStencilTarget::getWidth() const
  101. {
  102. return _width;
  103. }
  104. unsigned int DepthStencilTarget::getHeight() const
  105. {
  106. return _height;
  107. }
  108. bool DepthStencilTarget::isPacked() const
  109. {
  110. return _packed;
  111. }
  112. }