WebGLFormats.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef WEBGL_FORMATS_H_
  6. #define WEBGL_FORMATS_H_
  7. #include <map>
  8. #include <set>
  9. #include "mozilla/UniquePtr.h"
  10. #include "WebGLTypes.h"
  11. namespace mozilla {
  12. namespace webgl {
  13. typedef uint8_t EffectiveFormatValueT;
  14. enum class EffectiveFormat : EffectiveFormatValueT {
  15. // GLES 3.0.4, p128-129, "Required Texture Formats"
  16. // "Texture and renderbuffer color formats"
  17. RGBA32I,
  18. RGBA32UI,
  19. RGBA16I,
  20. RGBA16UI,
  21. RGBA8,
  22. RGBA8I,
  23. RGBA8UI,
  24. SRGB8_ALPHA8,
  25. RGB10_A2,
  26. RGB10_A2UI,
  27. RGBA4,
  28. RGB5_A1,
  29. RGB8,
  30. RGB565,
  31. RG32I,
  32. RG32UI,
  33. RG16I,
  34. RG16UI,
  35. RG8,
  36. RG8I,
  37. RG8UI,
  38. R32I,
  39. R32UI,
  40. R16I,
  41. R16UI,
  42. R8,
  43. R8I,
  44. R8UI,
  45. // "Texture-only color formats"
  46. RGBA32F,
  47. RGBA16F,
  48. RGBA8_SNORM,
  49. RGB32F,
  50. RGB32I,
  51. RGB32UI,
  52. RGB16F,
  53. RGB16I,
  54. RGB16UI,
  55. RGB8_SNORM,
  56. RGB8I,
  57. RGB8UI,
  58. SRGB8,
  59. R11F_G11F_B10F,
  60. RGB9_E5,
  61. RG32F,
  62. RG16F,
  63. RG8_SNORM,
  64. R32F,
  65. R16F,
  66. R8_SNORM,
  67. // "Depth formats"
  68. DEPTH_COMPONENT32F,
  69. DEPTH_COMPONENT24,
  70. DEPTH_COMPONENT16,
  71. // "Combined depth+stencil formats"
  72. DEPTH32F_STENCIL8,
  73. DEPTH24_STENCIL8,
  74. // GLES 3.0.4, p205-206, "Required Renderbuffer Formats"
  75. STENCIL_INDEX8,
  76. ////////////////////////////////////
  77. // GLES 3.0.4, p147, table 3.19
  78. // GLES 3.0.4, p286+, $C.1 "ETC Compressed Texture Image Formats"
  79. COMPRESSED_R11_EAC,
  80. COMPRESSED_SIGNED_R11_EAC,
  81. COMPRESSED_RG11_EAC,
  82. COMPRESSED_SIGNED_RG11_EAC,
  83. COMPRESSED_RGB8_ETC2,
  84. COMPRESSED_SRGB8_ETC2,
  85. COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
  86. COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
  87. COMPRESSED_RGBA8_ETC2_EAC,
  88. COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
  89. // AMD_compressed_ATC_texture
  90. ATC_RGB_AMD,
  91. ATC_RGBA_EXPLICIT_ALPHA_AMD,
  92. ATC_RGBA_INTERPOLATED_ALPHA_AMD,
  93. // EXT_texture_compression_s3tc
  94. COMPRESSED_RGB_S3TC_DXT1_EXT,
  95. COMPRESSED_RGBA_S3TC_DXT1_EXT,
  96. COMPRESSED_RGBA_S3TC_DXT3_EXT,
  97. COMPRESSED_RGBA_S3TC_DXT5_EXT,
  98. // IMG_texture_compression_pvrtc
  99. COMPRESSED_RGB_PVRTC_4BPPV1,
  100. COMPRESSED_RGBA_PVRTC_4BPPV1,
  101. COMPRESSED_RGB_PVRTC_2BPPV1,
  102. COMPRESSED_RGBA_PVRTC_2BPPV1,
  103. // OES_compressed_ETC1_RGB8_texture
  104. ETC1_RGB8_OES,
  105. ////////////////////////////////////
  106. // GLES 3.0.4, p128, table 3.12.
  107. Luminance8Alpha8,
  108. Luminance8,
  109. Alpha8,
  110. // OES_texture_float
  111. Luminance32FAlpha32F,
  112. Luminance32F,
  113. Alpha32F,
  114. // OES_texture_half_float
  115. Luminance16FAlpha16F,
  116. Luminance16F,
  117. Alpha16F,
  118. MAX,
  119. };
  120. enum class UnsizedFormat : uint8_t {
  121. R,
  122. RG,
  123. RGB,
  124. RGBA,
  125. LA,
  126. L,
  127. A,
  128. D,
  129. S,
  130. DEPTH_STENCIL, // `DS` is a macro on Solaris. (regset.h)
  131. };
  132. // GLES 3.0.4 p114 Table 3.4, p240
  133. enum class ComponentType : uint8_t {
  134. None,
  135. Int, // RGBA32I
  136. UInt, // RGBA32UI, STENCIL_INDEX8
  137. NormInt, // RGBA8_SNORM
  138. NormUInt, // RGBA8, DEPTH_COMPONENT16
  139. Float, // RGBA32F
  140. Special, // DEPTH24_STENCIL8
  141. };
  142. enum class CompressionFamily : uint8_t {
  143. ETC1,
  144. ES3, // ETC2 or EAC
  145. ATC,
  146. S3TC,
  147. PVRTC,
  148. };
  149. ////////////////////////////////////////////////////////////////////////////////
  150. struct CompressedFormatInfo
  151. {
  152. const EffectiveFormat effectiveFormat;
  153. const uint8_t bytesPerBlock;
  154. const uint8_t blockWidth;
  155. const uint8_t blockHeight;
  156. const CompressionFamily family;
  157. };
  158. struct FormatInfo
  159. {
  160. const EffectiveFormat effectiveFormat;
  161. const char* const name;
  162. const GLenum sizedFormat;
  163. const UnsizedFormat unsizedFormat;
  164. const ComponentType componentType;
  165. const bool isSRGB;
  166. const CompressedFormatInfo* const compression;
  167. const uint8_t estimatedBytesPerPixel; // 0 iff bool(compression).
  168. // In bits. Iff bool(compression), active channels are 1.
  169. const uint8_t r;
  170. const uint8_t g;
  171. const uint8_t b;
  172. const uint8_t a;
  173. const uint8_t d;
  174. const uint8_t s;
  175. //////
  176. std::map<UnsizedFormat, const FormatInfo*> copyDecayFormats;
  177. const FormatInfo* GetCopyDecayFormat(UnsizedFormat) const;
  178. bool IsColorFormat() const {
  179. // Alpha is a 'color format' since it's 'color-attachable'.
  180. return bool(compression) ||
  181. bool(r | g | b | a);
  182. }
  183. };
  184. struct PackingInfo
  185. {
  186. GLenum format;
  187. GLenum type;
  188. bool operator <(const PackingInfo& x) const
  189. {
  190. if (format != x.format)
  191. return format < x.format;
  192. return type < x.type;
  193. }
  194. bool operator ==(const PackingInfo& x) const {
  195. return (format == x.format &&
  196. type == x.type);
  197. }
  198. };
  199. struct DriverUnpackInfo
  200. {
  201. GLenum internalFormat;
  202. GLenum unpackFormat;
  203. GLenum unpackType;
  204. PackingInfo ToPacking() const {
  205. return {unpackFormat, unpackType};
  206. }
  207. };
  208. //////////////////////////////////////////////////////////////////////////////////////////
  209. const FormatInfo* GetFormat(EffectiveFormat format);
  210. uint8_t BytesPerPixel(const PackingInfo& packing);
  211. bool GetBytesPerPixel(const PackingInfo& packing, uint8_t* const out_bytes);
  212. /*
  213. GLint ComponentSize(const FormatInfo* format, GLenum component);
  214. GLenum ComponentType(const FormatInfo* format);
  215. */
  216. ////////////////////////////////////////
  217. struct FormatUsageInfo
  218. {
  219. const FormatInfo* const format;
  220. private:
  221. bool isRenderable;
  222. public:
  223. bool isFilterable;
  224. std::map<PackingInfo, DriverUnpackInfo> validUnpacks;
  225. const DriverUnpackInfo* idealUnpack;
  226. const GLint* textureSwizzleRGBA;
  227. bool maxSamplesKnown;
  228. uint32_t maxSamples;
  229. static const GLint kLuminanceSwizzleRGBA[4];
  230. static const GLint kAlphaSwizzleRGBA[4];
  231. static const GLint kLumAlphaSwizzleRGBA[4];
  232. explicit FormatUsageInfo(const FormatInfo* _format)
  233. : format(_format)
  234. , isRenderable(false)
  235. , isFilterable(false)
  236. , idealUnpack(nullptr)
  237. , textureSwizzleRGBA(nullptr)
  238. , maxSamplesKnown(false)
  239. , maxSamples(0)
  240. { }
  241. bool IsRenderable() const { return isRenderable; }
  242. void SetRenderable();
  243. bool IsUnpackValid(const PackingInfo& key,
  244. const DriverUnpackInfo** const out_value) const;
  245. void ResolveMaxSamples(gl::GLContext* gl);
  246. };
  247. class FormatUsageAuthority
  248. {
  249. std::map<EffectiveFormat, FormatUsageInfo> mUsageMap;
  250. std::map<GLenum, const FormatUsageInfo*> mRBFormatMap;
  251. std::map<GLenum, const FormatUsageInfo*> mSizedTexFormatMap;
  252. std::map<PackingInfo, const FormatUsageInfo*> mUnsizedTexFormatMap;
  253. std::set<GLenum> mValidTexInternalFormats;
  254. std::set<GLenum> mValidTexUnpackFormats;
  255. std::set<GLenum> mValidTexUnpackTypes;
  256. public:
  257. static UniquePtr<FormatUsageAuthority> CreateForWebGL1(gl::GLContext* gl);
  258. static UniquePtr<FormatUsageAuthority> CreateForWebGL2(gl::GLContext* gl);
  259. private:
  260. FormatUsageAuthority() { }
  261. public:
  262. FormatUsageInfo* EditUsage(EffectiveFormat format);
  263. const FormatUsageInfo* GetUsage(EffectiveFormat format) const;
  264. void AddTexUnpack(FormatUsageInfo* usage, const PackingInfo& pi,
  265. const DriverUnpackInfo& dui);
  266. bool IsInternalFormatEnumValid(GLenum internalFormat) const;
  267. bool AreUnpackEnumsValid(GLenum unpackFormat, GLenum unpackType) const;
  268. void AllowRBFormat(GLenum sizedFormat, const FormatUsageInfo* usage);
  269. void AllowSizedTexFormat(GLenum sizedFormat, const FormatUsageInfo* usage);
  270. void AllowUnsizedTexFormat(const PackingInfo& pi, const FormatUsageInfo* usage);
  271. const FormatUsageInfo* GetRBUsage(GLenum sizedFormat) const;
  272. const FormatUsageInfo* GetSizedTexUsage(GLenum sizedFormat) const;
  273. const FormatUsageInfo* GetUnsizedTexUsage(const PackingInfo& pi) const;
  274. };
  275. } // namespace webgl
  276. } // namespace mozilla
  277. #endif // WEBGL_FORMATS_H_