1 // 2 // Copyright 2024 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // wgpu_format_utils: 7 // Helper for WebGPU format code. 8 9 #ifndef LIBANGLE_RENDERER_WGPU_WGPU_FORMAT_UTILS_H_ 10 #define LIBANGLE_RENDERER_WGPU_WGPU_FORMAT_UTILS_H_ 11 12 #include <dawn/webgpu_cpp.h> 13 14 #include "libANGLE/formatutils.h" 15 #include "libANGLE/renderer/Format.h" 16 #include "libANGLE/renderer/copyvertex.h" 17 18 namespace rx 19 { 20 namespace webgpu 21 { 22 23 struct ImageFormatInitInfo final 24 { 25 angle::FormatID format; 26 InitializeTextureDataFunction initializer; 27 }; 28 29 struct BufferFormatInitInfo final 30 { 31 angle::FormatID format; 32 VertexCopyFunction VertexLoadFunction; 33 bool VertexLoadRequiresConversion; 34 }; 35 36 wgpu::TextureFormat GetWgpuTextureFormatFromFormatID(angle::FormatID formatID); 37 angle::FormatID GetFormatIDFromWgpuTextureFormat(wgpu::TextureFormat wgpuFormat); 38 wgpu::VertexFormat GetWgpuVertexFormatFromFormatID(angle::FormatID formatID); 39 angle::FormatID GetFormatIDFromWgpuBufferFormat(wgpu::VertexFormat wgpuFormat); 40 41 // Describes a WebGPU format. WebGPU has separate formats for images and vertex buffers, this class 42 // describes both. 43 class Format final : private angle::NonCopyable 44 { 45 public: 46 Format(); 47 valid()48 bool valid() const { return mIntendedGLFormat != 0; } 49 50 // The intended format is the front-end format. For Textures this usually correponds to a 51 // GLenum in the headers. Buffer formats don't always have a corresponding GLenum type. 52 // Some Surface formats and unsized types also don't have a corresponding GLenum. getIntendedFormatID()53 angle::FormatID getIntendedFormatID() const { return mIntendedFormatID; } getIntendedFormat()54 const angle::Format &getIntendedFormat() const { return angle::Format::Get(mIntendedFormatID); } 55 56 // The actual Image format is used to implement the front-end format for Texture/Renderbuffers. getActualImageFormat()57 const angle::Format &getActualImageFormat() const 58 { 59 return angle::Format::Get(getActualImageFormatID()); 60 } 61 getTextureLoadFunction(GLenum type)62 LoadImageFunctionInfo getTextureLoadFunction(GLenum type) const 63 { 64 return mTextureLoadFunctions(type); 65 } 66 getActualWgpuTextureFormat()67 wgpu::TextureFormat getActualWgpuTextureFormat() const 68 { 69 return GetWgpuTextureFormatFromFormatID(mActualImageFormatID); 70 } getActualWgpuVertexFormat()71 wgpu::VertexFormat getActualWgpuVertexFormat() const 72 { 73 return GetWgpuVertexFormatFromFormatID(mActualBufferFormatID); 74 } 75 getActualImageFormatID()76 angle::FormatID getActualImageFormatID() const { return mActualImageFormatID; } 77 78 // The actual Buffer format is used to implement the front-end format for Buffers. This format 79 // is used by vertex buffers as well as texture buffers. getActualBufferFormat()80 const angle::Format &getActualBufferFormat() const 81 { 82 return angle::Format::Get(mActualBufferFormatID); 83 } 84 85 // |intendedGLFormat| always correponds to a valid GLenum type. For types that don't have a 86 // corresponding GLenum we do our best to specify a GLenum that is "close". getInternalFormatInfo(GLenum type)87 const gl::InternalFormat &getInternalFormatInfo(GLenum type) const 88 { 89 return gl::GetInternalFormatInfo(mIntendedGLFormat, type); 90 } 91 92 private: 93 friend class FormatTable; 94 // This is an auto-generated method in vk_format_table_autogen.cpp. 95 void initialize(const angle::Format &intendedAngleFormat); 96 97 // These are used in the format table init. 98 void initImageFallback(const ImageFormatInitInfo *info, int numInfo); 99 100 void initBufferFallback(const BufferFormatInitInfo *fallbackInfo, int numInfo); 101 102 angle::FormatID mIntendedFormatID; 103 GLenum mIntendedGLFormat; 104 angle::FormatID mActualImageFormatID; 105 angle::FormatID mActualBufferFormatID; 106 107 InitializeTextureDataFunction mImageInitializerFunction; 108 LoadFunctionMap mTextureLoadFunctions; 109 VertexCopyFunction mVertexLoadFunction; 110 111 bool mVertexLoadRequiresConversion; 112 bool mIsRenderable; 113 }; 114 115 bool operator==(const Format &lhs, const Format &rhs); 116 bool operator!=(const Format &lhs, const Format &rhs); 117 118 class FormatTable final : angle::NonCopyable 119 { 120 public: 121 FormatTable(); 122 ~FormatTable(); 123 124 void initialize(); 125 126 ANGLE_INLINE const Format &operator[](GLenum internalFormat) const 127 { 128 angle::FormatID formatID = angle::Format::InternalFormatToID(internalFormat); 129 return mFormatData[static_cast<size_t>(formatID)]; 130 } 131 132 ANGLE_INLINE const Format &operator[](angle::FormatID formatID) const 133 { 134 return mFormatData[static_cast<size_t>(formatID)]; 135 } 136 137 private: 138 // The table data is indexed by angle::FormatID. 139 std::array<Format, angle::kNumANGLEFormats> mFormatData; 140 }; 141 } // namespace webgpu 142 } // namespace rx 143 144 #endif // LIBANGLE_RENDERER_WGPU_WGPU_FORMAT_UTILS_H_ 145