WebGLExtensionTextureHalfFloat.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "WebGLExtensions.h"
  5. #include "GLContext.h"
  6. #include "mozilla/dom/WebGLRenderingContextBinding.h"
  7. #include "WebGLContext.h"
  8. #include "WebGLFormats.h"
  9. namespace mozilla {
  10. WebGLExtensionTextureHalfFloat::WebGLExtensionTextureHalfFloat(WebGLContext* webgl)
  11. : WebGLExtensionBase(webgl)
  12. {
  13. auto& fua = webgl->mFormatUsage;
  14. gl::GLContext* gl = webgl->GL();
  15. webgl::PackingInfo pi;
  16. webgl::DriverUnpackInfo dui;
  17. const GLint* swizzle = nullptr;
  18. const auto fnAdd = [&fua, &pi, &dui, &swizzle](webgl::EffectiveFormat effFormat)
  19. {
  20. auto usage = fua->EditUsage(effFormat);
  21. usage->textureSwizzleRGBA = swizzle;
  22. fua->AddTexUnpack(usage, pi, dui);
  23. fua->AllowUnsizedTexFormat(pi, usage);
  24. };
  25. const bool needsSwizzle = gl->IsCoreProfile();
  26. MOZ_ASSERT_IF(needsSwizzle, gl->IsSupported(gl::GLFeature::texture_swizzle));
  27. const bool needsSizedFormat = !gl->IsGLES();
  28. GLenum driverUnpackType = LOCAL_GL_HALF_FLOAT;
  29. if (!gl->IsSupported(gl::GLFeature::texture_half_float)) {
  30. MOZ_ASSERT(gl->IsExtensionSupported(gl::GLContext::OES_texture_half_float));
  31. driverUnpackType = LOCAL_GL_HALF_FLOAT_OES;
  32. }
  33. ////////////////
  34. pi = {LOCAL_GL_RGBA, LOCAL_GL_HALF_FLOAT_OES};
  35. dui = {pi.format, pi.format, driverUnpackType};
  36. swizzle = nullptr;
  37. if (needsSizedFormat) {
  38. dui.internalFormat = LOCAL_GL_RGBA16F;
  39. }
  40. fnAdd(webgl::EffectiveFormat::RGBA16F);
  41. //////
  42. pi = {LOCAL_GL_RGB, LOCAL_GL_HALF_FLOAT_OES};
  43. dui = {pi.format, pi.format, driverUnpackType};
  44. swizzle = nullptr;
  45. if (needsSizedFormat) {
  46. dui.internalFormat = LOCAL_GL_RGB16F;
  47. }
  48. fnAdd(webgl::EffectiveFormat::RGB16F);
  49. //////
  50. pi = {LOCAL_GL_LUMINANCE, LOCAL_GL_HALF_FLOAT_OES};
  51. dui = {pi.format, pi.format, driverUnpackType};
  52. swizzle = nullptr;
  53. if (needsSwizzle) {
  54. dui = {LOCAL_GL_R16F, LOCAL_GL_RED, driverUnpackType};
  55. swizzle = webgl::FormatUsageInfo::kLuminanceSwizzleRGBA;
  56. } else if (needsSizedFormat) {
  57. dui.internalFormat = LOCAL_GL_LUMINANCE16F_ARB;
  58. }
  59. fnAdd(webgl::EffectiveFormat::Luminance16F);
  60. //////
  61. pi = {LOCAL_GL_ALPHA, LOCAL_GL_HALF_FLOAT_OES};
  62. dui = {pi.format, pi.format, driverUnpackType};
  63. swizzle = nullptr;
  64. if (needsSwizzle) {
  65. dui = {LOCAL_GL_R16F, LOCAL_GL_RED, driverUnpackType};
  66. swizzle = webgl::FormatUsageInfo::kAlphaSwizzleRGBA;
  67. } else if (needsSizedFormat) {
  68. dui.internalFormat = LOCAL_GL_ALPHA16F_ARB;
  69. }
  70. fnAdd(webgl::EffectiveFormat::Alpha16F);
  71. //////
  72. pi = {LOCAL_GL_LUMINANCE_ALPHA, LOCAL_GL_HALF_FLOAT_OES};
  73. dui = {pi.format, pi.format, driverUnpackType};
  74. swizzle = nullptr;
  75. if (needsSwizzle) {
  76. dui = {LOCAL_GL_RG16F, LOCAL_GL_RG, driverUnpackType};
  77. swizzle = webgl::FormatUsageInfo::kLumAlphaSwizzleRGBA;
  78. } else if (needsSizedFormat) {
  79. dui.internalFormat = LOCAL_GL_LUMINANCE_ALPHA16F_ARB;
  80. }
  81. fnAdd(webgl::EffectiveFormat::Luminance16FAlpha16F);
  82. }
  83. WebGLExtensionTextureHalfFloat::~WebGLExtensionTextureHalfFloat()
  84. {
  85. }
  86. bool
  87. WebGLExtensionTextureHalfFloat::IsSupported(const WebGLContext* webgl)
  88. {
  89. gl::GLContext* gl = webgl->GL();
  90. if (!gl->IsSupported(gl::GLFeature::texture_half_float) &&
  91. !gl->IsExtensionSupported(gl::GLContext::OES_texture_half_float))
  92. {
  93. return false;
  94. }
  95. const bool needsSwizzle = gl->IsCoreProfile();
  96. const bool hasSwizzle = gl->IsSupported(gl::GLFeature::texture_swizzle);
  97. if (needsSwizzle && !hasSwizzle)
  98. return false;
  99. return true;
  100. }
  101. IMPL_WEBGL_EXTENSION_GOOP(WebGLExtensionTextureHalfFloat, OES_texture_half_float)
  102. } // namespace mozilla