WebGLFormats.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. // EXT_texture_sRGB
  99. COMPRESSED_SRGB_S3TC_DXT1_EXT,
  100. COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,
  101. COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,
  102. COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT,
  103. // KHR_texture_compression_astc_ldr
  104. COMPRESSED_RGBA_ASTC_4x4_KHR,
  105. COMPRESSED_RGBA_ASTC_5x4_KHR,
  106. COMPRESSED_RGBA_ASTC_5x5_KHR,
  107. COMPRESSED_RGBA_ASTC_6x5_KHR,
  108. COMPRESSED_RGBA_ASTC_6x6_KHR,
  109. COMPRESSED_RGBA_ASTC_8x5_KHR,
  110. COMPRESSED_RGBA_ASTC_8x6_KHR,
  111. COMPRESSED_RGBA_ASTC_8x8_KHR,
  112. COMPRESSED_RGBA_ASTC_10x5_KHR,
  113. COMPRESSED_RGBA_ASTC_10x6_KHR,
  114. COMPRESSED_RGBA_ASTC_10x8_KHR,
  115. COMPRESSED_RGBA_ASTC_10x10_KHR,
  116. COMPRESSED_RGBA_ASTC_12x10_KHR,
  117. COMPRESSED_RGBA_ASTC_12x12_KHR,
  118. COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
  119. COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
  120. COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
  121. COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
  122. COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
  123. COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
  124. COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
  125. COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
  126. COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
  127. COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
  128. COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
  129. COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
  130. COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
  131. COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
  132. // IMG_texture_compression_pvrtc
  133. COMPRESSED_RGB_PVRTC_4BPPV1,
  134. COMPRESSED_RGBA_PVRTC_4BPPV1,
  135. COMPRESSED_RGB_PVRTC_2BPPV1,
  136. COMPRESSED_RGBA_PVRTC_2BPPV1,
  137. // OES_compressed_ETC1_RGB8_texture
  138. ETC1_RGB8_OES,
  139. ////////////////////////////////////
  140. // GLES 3.0.4, p128, table 3.12.
  141. Luminance8Alpha8,
  142. Luminance8,
  143. Alpha8,
  144. // OES_texture_float
  145. Luminance32FAlpha32F,
  146. Luminance32F,
  147. Alpha32F,
  148. // OES_texture_half_float
  149. Luminance16FAlpha16F,
  150. Luminance16F,
  151. Alpha16F,
  152. MAX,
  153. };
  154. enum class UnsizedFormat : uint8_t {
  155. R,
  156. RG,
  157. RGB,
  158. RGBA,
  159. LA,
  160. L,
  161. A,
  162. D,
  163. S,
  164. DEPTH_STENCIL, // `DS` is a macro on Solaris. (regset.h)
  165. };
  166. // GLES 3.0.4 p114 Table 3.4, p240
  167. enum class ComponentType : uint8_t {
  168. None,
  169. Int, // RGBA32I
  170. UInt, // RGBA32UI, STENCIL_INDEX8
  171. NormInt, // RGBA8_SNORM
  172. NormUInt, // RGBA8, DEPTH_COMPONENT16
  173. Float, // RGBA32F
  174. Special, // DEPTH24_STENCIL8
  175. };
  176. enum class CompressionFamily : uint8_t {
  177. ASTC,
  178. ATC,
  179. ES3, // ETC2 or EAC
  180. ETC1,
  181. PVRTC,
  182. S3TC,
  183. };
  184. ////////////////////////////////////////////////////////////////////////////////
  185. struct CompressedFormatInfo
  186. {
  187. const EffectiveFormat effectiveFormat;
  188. const uint8_t bytesPerBlock;
  189. const uint8_t blockWidth;
  190. const uint8_t blockHeight;
  191. const CompressionFamily family;
  192. };
  193. struct FormatInfo
  194. {
  195. const EffectiveFormat effectiveFormat;
  196. const char* const name;
  197. const GLenum sizedFormat;
  198. const UnsizedFormat unsizedFormat;
  199. const ComponentType componentType;
  200. const bool isSRGB;
  201. const CompressedFormatInfo* const compression;
  202. const uint8_t estimatedBytesPerPixel; // 0 iff bool(compression).
  203. // In bits. Iff bool(compression), active channels are 1.
  204. const uint8_t r;
  205. const uint8_t g;
  206. const uint8_t b;
  207. const uint8_t a;
  208. const uint8_t d;
  209. const uint8_t s;
  210. //////
  211. std::map<UnsizedFormat, const FormatInfo*> copyDecayFormats;
  212. const FormatInfo* GetCopyDecayFormat(UnsizedFormat) const;
  213. bool IsColorFormat() const {
  214. // Alpha is a 'color format' since it's 'color-attachable'.
  215. return bool(compression) ||
  216. bool(r | g | b | a);
  217. }
  218. };
  219. struct PackingInfo
  220. {
  221. GLenum format;
  222. GLenum type;
  223. bool operator <(const PackingInfo& x) const
  224. {
  225. if (format != x.format)
  226. return format < x.format;
  227. return type < x.type;
  228. }
  229. bool operator ==(const PackingInfo& x) const {
  230. return (format == x.format &&
  231. type == x.type);
  232. }
  233. };
  234. struct DriverUnpackInfo
  235. {
  236. GLenum internalFormat;
  237. GLenum unpackFormat;
  238. GLenum unpackType;
  239. PackingInfo ToPacking() const {
  240. return {unpackFormat, unpackType};
  241. }
  242. };
  243. //////////////////////////////////////////////////////////////////////////////////////////
  244. const FormatInfo* GetFormat(EffectiveFormat format);
  245. uint8_t BytesPerPixel(const PackingInfo& packing);
  246. bool GetBytesPerPixel(const PackingInfo& packing, uint8_t* const out_bytes);
  247. /*
  248. GLint ComponentSize(const FormatInfo* format, GLenum component);
  249. GLenum ComponentType(const FormatInfo* format);
  250. */
  251. ////////////////////////////////////////
  252. struct FormatUsageInfo
  253. {
  254. const FormatInfo* const format;
  255. private:
  256. bool isRenderable;
  257. public:
  258. bool isFilterable;
  259. std::map<PackingInfo, DriverUnpackInfo> validUnpacks;
  260. const DriverUnpackInfo* idealUnpack;
  261. const GLint* textureSwizzleRGBA;
  262. bool maxSamplesKnown;
  263. uint32_t maxSamples;
  264. static const GLint kLuminanceSwizzleRGBA[4];
  265. static const GLint kAlphaSwizzleRGBA[4];
  266. static const GLint kLumAlphaSwizzleRGBA[4];
  267. explicit FormatUsageInfo(const FormatInfo* _format)
  268. : format(_format)
  269. , isRenderable(false)
  270. , isFilterable(false)
  271. , idealUnpack(nullptr)
  272. , textureSwizzleRGBA(nullptr)
  273. , maxSamplesKnown(false)
  274. , maxSamples(0)
  275. { }
  276. bool IsRenderable() const { return isRenderable; }
  277. void SetRenderable();
  278. bool IsUnpackValid(const PackingInfo& key,
  279. const DriverUnpackInfo** const out_value) const;
  280. void ResolveMaxSamples(gl::GLContext* gl);
  281. };
  282. class FormatUsageAuthority
  283. {
  284. std::map<EffectiveFormat, FormatUsageInfo> mUsageMap;
  285. std::map<GLenum, const FormatUsageInfo*> mRBFormatMap;
  286. std::map<GLenum, const FormatUsageInfo*> mSizedTexFormatMap;
  287. std::map<PackingInfo, const FormatUsageInfo*> mUnsizedTexFormatMap;
  288. std::set<GLenum> mValidTexInternalFormats;
  289. std::set<GLenum> mValidTexUnpackFormats;
  290. std::set<GLenum> mValidTexUnpackTypes;
  291. public:
  292. static UniquePtr<FormatUsageAuthority> CreateForWebGL1(gl::GLContext* gl);
  293. static UniquePtr<FormatUsageAuthority> CreateForWebGL2(gl::GLContext* gl);
  294. private:
  295. FormatUsageAuthority() { }
  296. public:
  297. FormatUsageInfo* EditUsage(EffectiveFormat format);
  298. const FormatUsageInfo* GetUsage(EffectiveFormat format) const;
  299. void AddTexUnpack(FormatUsageInfo* usage, const PackingInfo& pi,
  300. const DriverUnpackInfo& dui);
  301. bool IsInternalFormatEnumValid(GLenum internalFormat) const;
  302. bool AreUnpackEnumsValid(GLenum unpackFormat, GLenum unpackType) const;
  303. void AllowRBFormat(GLenum sizedFormat, const FormatUsageInfo* usage);
  304. void AllowSizedTexFormat(GLenum sizedFormat, const FormatUsageInfo* usage);
  305. void AllowUnsizedTexFormat(const PackingInfo& pi, const FormatUsageInfo* usage);
  306. const FormatUsageInfo* GetRBUsage(GLenum sizedFormat) const;
  307. const FormatUsageInfo* GetSizedTexUsage(GLenum sizedFormat) const;
  308. const FormatUsageInfo* GetUnsizedTexUsage(const PackingInfo& pi) const;
  309. };
  310. } // namespace webgl
  311. } // namespace mozilla
  312. #endif // WEBGL_FORMATS_H_