xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/metal/TextureMtl.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // TextureMtl.h:
7*8975f5c5SAndroid Build Coastguard Worker //    Defines the class interface for TextureMtl, implementing TextureImpl.
8*8975f5c5SAndroid Build Coastguard Worker //
9*8975f5c5SAndroid Build Coastguard Worker 
10*8975f5c5SAndroid Build Coastguard Worker #ifndef LIBANGLE_RENDERER_METAL_TEXTUREMTL_H_
11*8975f5c5SAndroid Build Coastguard Worker #define LIBANGLE_RENDERER_METAL_TEXTUREMTL_H_
12*8975f5c5SAndroid Build Coastguard Worker 
13*8975f5c5SAndroid Build Coastguard Worker #include <map>
14*8975f5c5SAndroid Build Coastguard Worker 
15*8975f5c5SAndroid Build Coastguard Worker #include "common/PackedEnums.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/TextureImpl.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/metal/RenderTargetMtl.h"
18*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/metal/SurfaceMtl.h"
19*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/metal/mtl_command_buffer.h"
20*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/metal/mtl_context_device.h"
21*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/metal/mtl_resources.h"
22*8975f5c5SAndroid Build Coastguard Worker namespace rx
23*8975f5c5SAndroid Build Coastguard Worker {
24*8975f5c5SAndroid Build Coastguard Worker 
25*8975f5c5SAndroid Build Coastguard Worker // structure represents one image definition of a texture created by glTexImage* call.
26*8975f5c5SAndroid Build Coastguard Worker struct ImageDefinitionMtl
27*8975f5c5SAndroid Build Coastguard Worker {
28*8975f5c5SAndroid Build Coastguard Worker     mtl::TextureRef image;
29*8975f5c5SAndroid Build Coastguard Worker     angle::FormatID formatID = angle::FormatID::NONE;
30*8975f5c5SAndroid Build Coastguard Worker };
31*8975f5c5SAndroid Build Coastguard Worker 
32*8975f5c5SAndroid Build Coastguard Worker class TextureMtl : public TextureImpl
33*8975f5c5SAndroid Build Coastguard Worker {
34*8975f5c5SAndroid Build Coastguard Worker   public:
35*8975f5c5SAndroid Build Coastguard Worker     using TextureViewVector           = std::vector<mtl::TextureRef>;
36*8975f5c5SAndroid Build Coastguard Worker     using LayerLevelTextureViewVector = std::vector<TextureViewVector>;
37*8975f5c5SAndroid Build Coastguard Worker 
38*8975f5c5SAndroid Build Coastguard Worker     TextureMtl(const gl::TextureState &state);
39*8975f5c5SAndroid Build Coastguard Worker     // Texture  view
40*8975f5c5SAndroid Build Coastguard Worker     TextureMtl(const TextureMtl &mtl, GLenum format);
41*8975f5c5SAndroid Build Coastguard Worker     ~TextureMtl() override;
42*8975f5c5SAndroid Build Coastguard Worker     void onDestroy(const gl::Context *context) override;
43*8975f5c5SAndroid Build Coastguard Worker 
44*8975f5c5SAndroid Build Coastguard Worker     angle::Result setImage(const gl::Context *context,
45*8975f5c5SAndroid Build Coastguard Worker                            const gl::ImageIndex &index,
46*8975f5c5SAndroid Build Coastguard Worker                            GLenum internalFormat,
47*8975f5c5SAndroid Build Coastguard Worker                            const gl::Extents &size,
48*8975f5c5SAndroid Build Coastguard Worker                            GLenum format,
49*8975f5c5SAndroid Build Coastguard Worker                            GLenum type,
50*8975f5c5SAndroid Build Coastguard Worker                            const gl::PixelUnpackState &unpack,
51*8975f5c5SAndroid Build Coastguard Worker                            gl::Buffer *unpackBuffer,
52*8975f5c5SAndroid Build Coastguard Worker                            const uint8_t *pixels) override;
53*8975f5c5SAndroid Build Coastguard Worker     angle::Result setSubImage(const gl::Context *context,
54*8975f5c5SAndroid Build Coastguard Worker                               const gl::ImageIndex &index,
55*8975f5c5SAndroid Build Coastguard Worker                               const gl::Box &area,
56*8975f5c5SAndroid Build Coastguard Worker                               GLenum format,
57*8975f5c5SAndroid Build Coastguard Worker                               GLenum type,
58*8975f5c5SAndroid Build Coastguard Worker                               const gl::PixelUnpackState &unpack,
59*8975f5c5SAndroid Build Coastguard Worker                               gl::Buffer *unpackBuffer,
60*8975f5c5SAndroid Build Coastguard Worker                               const uint8_t *pixels) override;
61*8975f5c5SAndroid Build Coastguard Worker 
62*8975f5c5SAndroid Build Coastguard Worker     angle::Result setCompressedImage(const gl::Context *context,
63*8975f5c5SAndroid Build Coastguard Worker                                      const gl::ImageIndex &index,
64*8975f5c5SAndroid Build Coastguard Worker                                      GLenum internalFormat,
65*8975f5c5SAndroid Build Coastguard Worker                                      const gl::Extents &size,
66*8975f5c5SAndroid Build Coastguard Worker                                      const gl::PixelUnpackState &unpack,
67*8975f5c5SAndroid Build Coastguard Worker                                      size_t imageSize,
68*8975f5c5SAndroid Build Coastguard Worker                                      const uint8_t *pixels) override;
69*8975f5c5SAndroid Build Coastguard Worker     angle::Result setCompressedSubImage(const gl::Context *context,
70*8975f5c5SAndroid Build Coastguard Worker                                         const gl::ImageIndex &index,
71*8975f5c5SAndroid Build Coastguard Worker                                         const gl::Box &area,
72*8975f5c5SAndroid Build Coastguard Worker                                         GLenum format,
73*8975f5c5SAndroid Build Coastguard Worker                                         const gl::PixelUnpackState &unpack,
74*8975f5c5SAndroid Build Coastguard Worker                                         size_t imageSize,
75*8975f5c5SAndroid Build Coastguard Worker                                         const uint8_t *pixels) override;
76*8975f5c5SAndroid Build Coastguard Worker 
77*8975f5c5SAndroid Build Coastguard Worker     angle::Result copyImage(const gl::Context *context,
78*8975f5c5SAndroid Build Coastguard Worker                             const gl::ImageIndex &index,
79*8975f5c5SAndroid Build Coastguard Worker                             const gl::Rectangle &sourceArea,
80*8975f5c5SAndroid Build Coastguard Worker                             GLenum internalFormat,
81*8975f5c5SAndroid Build Coastguard Worker                             gl::Framebuffer *source) override;
82*8975f5c5SAndroid Build Coastguard Worker     angle::Result copySubImage(const gl::Context *context,
83*8975f5c5SAndroid Build Coastguard Worker                                const gl::ImageIndex &index,
84*8975f5c5SAndroid Build Coastguard Worker                                const gl::Offset &destOffset,
85*8975f5c5SAndroid Build Coastguard Worker                                const gl::Rectangle &sourceArea,
86*8975f5c5SAndroid Build Coastguard Worker                                gl::Framebuffer *source) override;
87*8975f5c5SAndroid Build Coastguard Worker 
88*8975f5c5SAndroid Build Coastguard Worker     angle::Result copyTexture(const gl::Context *context,
89*8975f5c5SAndroid Build Coastguard Worker                               const gl::ImageIndex &index,
90*8975f5c5SAndroid Build Coastguard Worker                               GLenum internalFormat,
91*8975f5c5SAndroid Build Coastguard Worker                               GLenum type,
92*8975f5c5SAndroid Build Coastguard Worker                               GLint sourceLevel,
93*8975f5c5SAndroid Build Coastguard Worker                               bool unpackFlipY,
94*8975f5c5SAndroid Build Coastguard Worker                               bool unpackPremultiplyAlpha,
95*8975f5c5SAndroid Build Coastguard Worker                               bool unpackUnmultiplyAlpha,
96*8975f5c5SAndroid Build Coastguard Worker                               const gl::Texture *source) override;
97*8975f5c5SAndroid Build Coastguard Worker     angle::Result copySubTexture(const gl::Context *context,
98*8975f5c5SAndroid Build Coastguard Worker                                  const gl::ImageIndex &index,
99*8975f5c5SAndroid Build Coastguard Worker                                  const gl::Offset &destOffset,
100*8975f5c5SAndroid Build Coastguard Worker                                  GLint sourceLevel,
101*8975f5c5SAndroid Build Coastguard Worker                                  const gl::Box &sourceBox,
102*8975f5c5SAndroid Build Coastguard Worker                                  bool unpackFlipY,
103*8975f5c5SAndroid Build Coastguard Worker                                  bool unpackPremultiplyAlpha,
104*8975f5c5SAndroid Build Coastguard Worker                                  bool unpackUnmultiplyAlpha,
105*8975f5c5SAndroid Build Coastguard Worker                                  const gl::Texture *source) override;
106*8975f5c5SAndroid Build Coastguard Worker 
107*8975f5c5SAndroid Build Coastguard Worker     angle::Result copyCompressedTexture(const gl::Context *context,
108*8975f5c5SAndroid Build Coastguard Worker                                         const gl::Texture *source) override;
109*8975f5c5SAndroid Build Coastguard Worker 
110*8975f5c5SAndroid Build Coastguard Worker     angle::Result setStorage(const gl::Context *context,
111*8975f5c5SAndroid Build Coastguard Worker                              gl::TextureType type,
112*8975f5c5SAndroid Build Coastguard Worker                              size_t levels,
113*8975f5c5SAndroid Build Coastguard Worker                              GLenum internalFormat,
114*8975f5c5SAndroid Build Coastguard Worker                              const gl::Extents &size) override;
115*8975f5c5SAndroid Build Coastguard Worker 
116*8975f5c5SAndroid Build Coastguard Worker     angle::Result setStorageExternalMemory(const gl::Context *context,
117*8975f5c5SAndroid Build Coastguard Worker                                            gl::TextureType type,
118*8975f5c5SAndroid Build Coastguard Worker                                            size_t levels,
119*8975f5c5SAndroid Build Coastguard Worker                                            GLenum internalFormat,
120*8975f5c5SAndroid Build Coastguard Worker                                            const gl::Extents &size,
121*8975f5c5SAndroid Build Coastguard Worker                                            gl::MemoryObject *memoryObject,
122*8975f5c5SAndroid Build Coastguard Worker                                            GLuint64 offset,
123*8975f5c5SAndroid Build Coastguard Worker                                            GLbitfield createFlags,
124*8975f5c5SAndroid Build Coastguard Worker                                            GLbitfield usageFlags,
125*8975f5c5SAndroid Build Coastguard Worker                                            const void *imageCreateInfoPNext) override;
126*8975f5c5SAndroid Build Coastguard Worker 
127*8975f5c5SAndroid Build Coastguard Worker     angle::Result setStorageMultisample(const gl::Context *context,
128*8975f5c5SAndroid Build Coastguard Worker                                         gl::TextureType type,
129*8975f5c5SAndroid Build Coastguard Worker                                         GLsizei samples,
130*8975f5c5SAndroid Build Coastguard Worker                                         GLint internalFormat,
131*8975f5c5SAndroid Build Coastguard Worker                                         const gl::Extents &size,
132*8975f5c5SAndroid Build Coastguard Worker                                         bool fixedSampleLocations) override;
133*8975f5c5SAndroid Build Coastguard Worker 
134*8975f5c5SAndroid Build Coastguard Worker     angle::Result setEGLImageTarget(const gl::Context *context,
135*8975f5c5SAndroid Build Coastguard Worker                                     gl::TextureType type,
136*8975f5c5SAndroid Build Coastguard Worker                                     egl::Image *image) override;
137*8975f5c5SAndroid Build Coastguard Worker 
138*8975f5c5SAndroid Build Coastguard Worker     angle::Result setImageExternal(const gl::Context *context,
139*8975f5c5SAndroid Build Coastguard Worker                                    gl::TextureType type,
140*8975f5c5SAndroid Build Coastguard Worker                                    egl::Stream *stream,
141*8975f5c5SAndroid Build Coastguard Worker                                    const egl::Stream::GLTextureDescription &desc) override;
142*8975f5c5SAndroid Build Coastguard Worker 
143*8975f5c5SAndroid Build Coastguard Worker     angle::Result generateMipmap(const gl::Context *context) override;
144*8975f5c5SAndroid Build Coastguard Worker 
145*8975f5c5SAndroid Build Coastguard Worker     angle::Result setBaseLevel(const gl::Context *context, GLuint baseLevel) override;
146*8975f5c5SAndroid Build Coastguard Worker 
147*8975f5c5SAndroid Build Coastguard Worker     angle::Result bindTexImage(const gl::Context *context, egl::Surface *surface) override;
148*8975f5c5SAndroid Build Coastguard Worker     angle::Result releaseTexImage(const gl::Context *context) override;
149*8975f5c5SAndroid Build Coastguard Worker 
150*8975f5c5SAndroid Build Coastguard Worker     angle::Result getAttachmentRenderTarget(const gl::Context *context,
151*8975f5c5SAndroid Build Coastguard Worker                                             GLenum binding,
152*8975f5c5SAndroid Build Coastguard Worker                                             const gl::ImageIndex &imageIndex,
153*8975f5c5SAndroid Build Coastguard Worker                                             GLsizei samples,
154*8975f5c5SAndroid Build Coastguard Worker                                             FramebufferAttachmentRenderTarget **rtOut) override;
155*8975f5c5SAndroid Build Coastguard Worker 
156*8975f5c5SAndroid Build Coastguard Worker     angle::Result syncState(const gl::Context *context,
157*8975f5c5SAndroid Build Coastguard Worker                             const gl::Texture::DirtyBits &dirtyBits,
158*8975f5c5SAndroid Build Coastguard Worker                             gl::Command source) override;
159*8975f5c5SAndroid Build Coastguard Worker 
160*8975f5c5SAndroid Build Coastguard Worker     angle::Result initializeContents(const gl::Context *context,
161*8975f5c5SAndroid Build Coastguard Worker                                      GLenum binding,
162*8975f5c5SAndroid Build Coastguard Worker                                      const gl::ImageIndex &imageIndex) override;
163*8975f5c5SAndroid Build Coastguard Worker 
164*8975f5c5SAndroid Build Coastguard Worker     // The texture's data is initially initialized and stored in an array
165*8975f5c5SAndroid Build Coastguard Worker     // of images through glTexImage*/glCopyTex* calls. During draw calls, the caller must make sure
166*8975f5c5SAndroid Build Coastguard Worker     // the actual texture is created by calling this method to transfer the stored images data
167*8975f5c5SAndroid Build Coastguard Worker     // to the actual texture.
168*8975f5c5SAndroid Build Coastguard Worker     angle::Result ensureNativeStorageCreated(const gl::Context *context);
169*8975f5c5SAndroid Build Coastguard Worker 
170*8975f5c5SAndroid Build Coastguard Worker     angle::Result bindToShader(const gl::Context *context,
171*8975f5c5SAndroid Build Coastguard Worker                                mtl::RenderCommandEncoder *cmdEncoder,
172*8975f5c5SAndroid Build Coastguard Worker                                gl::ShaderType shaderType,
173*8975f5c5SAndroid Build Coastguard Worker                                gl::Sampler *sampler, /** nullable */
174*8975f5c5SAndroid Build Coastguard Worker                                int textureSlotIndex,
175*8975f5c5SAndroid Build Coastguard Worker                                int samplerSlotIndex);
176*8975f5c5SAndroid Build Coastguard Worker 
177*8975f5c5SAndroid Build Coastguard Worker     angle::Result bindToShaderImage(const gl::Context *context,
178*8975f5c5SAndroid Build Coastguard Worker                                     mtl::RenderCommandEncoder *cmdEncoder,
179*8975f5c5SAndroid Build Coastguard Worker                                     gl::ShaderType shaderType,
180*8975f5c5SAndroid Build Coastguard Worker                                     int textureSlotIndex,
181*8975f5c5SAndroid Build Coastguard Worker                                     int level,
182*8975f5c5SAndroid Build Coastguard Worker                                     int layer,
183*8975f5c5SAndroid Build Coastguard Worker                                     GLenum format);
184*8975f5c5SAndroid Build Coastguard Worker 
getFormat()185*8975f5c5SAndroid Build Coastguard Worker     const mtl::Format &getFormat() const { return mFormat; }
186*8975f5c5SAndroid Build Coastguard Worker 
187*8975f5c5SAndroid Build Coastguard Worker   private:
188*8975f5c5SAndroid Build Coastguard Worker     void deallocateNativeStorage(bool keepImages, bool keepSamplerStateAndFormat = false);
189*8975f5c5SAndroid Build Coastguard Worker     angle::Result createNativeStorage(const gl::Context *context,
190*8975f5c5SAndroid Build Coastguard Worker                                       gl::TextureType type,
191*8975f5c5SAndroid Build Coastguard Worker                                       GLuint mips,
192*8975f5c5SAndroid Build Coastguard Worker                                       GLuint samples,
193*8975f5c5SAndroid Build Coastguard Worker                                       const gl::Extents &size);
194*8975f5c5SAndroid Build Coastguard Worker     angle::Result onBaseMaxLevelsChanged(const gl::Context *context);
195*8975f5c5SAndroid Build Coastguard Worker     angle::Result ensureSamplerStateCreated(const gl::Context *context);
196*8975f5c5SAndroid Build Coastguard Worker     // Ensure image at given index is created:
197*8975f5c5SAndroid Build Coastguard Worker     angle::Result ensureImageCreated(const gl::Context *context, const gl::ImageIndex &index);
198*8975f5c5SAndroid Build Coastguard Worker     // Ensure all image views at all faces/levels are retained.
199*8975f5c5SAndroid Build Coastguard Worker     void retainImageDefinitions();
200*8975f5c5SAndroid Build Coastguard Worker     mtl::TextureRef createImageViewFromTextureStorage(GLuint cubeFaceOrZero, GLuint glLevel);
201*8975f5c5SAndroid Build Coastguard Worker     angle::Result createViewFromBaseToMaxLevel();
202*8975f5c5SAndroid Build Coastguard Worker     angle::Result ensureLevelViewsWithinBaseMaxCreated();
203*8975f5c5SAndroid Build Coastguard Worker     angle::Result checkForEmulatedChannels(const gl::Context *context,
204*8975f5c5SAndroid Build Coastguard Worker                                            const mtl::Format &mtlFormat,
205*8975f5c5SAndroid Build Coastguard Worker                                            const mtl::TextureRef &texture);
206*8975f5c5SAndroid Build Coastguard Worker     mtl::TextureRef &getImage(const gl::ImageIndex &imageIndex);
207*8975f5c5SAndroid Build Coastguard Worker     ImageDefinitionMtl &getImageDefinition(const gl::ImageIndex &imageIndex);
208*8975f5c5SAndroid Build Coastguard Worker     angle::Result getRenderTarget(ContextMtl *context,
209*8975f5c5SAndroid Build Coastguard Worker                                   const gl::ImageIndex &imageIndex,
210*8975f5c5SAndroid Build Coastguard Worker                                   GLsizei implicitSamples,
211*8975f5c5SAndroid Build Coastguard Worker                                   RenderTargetMtl **renderTargetOut);
212*8975f5c5SAndroid Build Coastguard Worker     mtl::TextureRef &getImplicitMSTexture(const gl::ImageIndex &imageIndex);
213*8975f5c5SAndroid Build Coastguard Worker 
214*8975f5c5SAndroid Build Coastguard Worker     angle::Result setStorageImpl(const gl::Context *context,
215*8975f5c5SAndroid Build Coastguard Worker                                  gl::TextureType type,
216*8975f5c5SAndroid Build Coastguard Worker                                  GLuint mips,
217*8975f5c5SAndroid Build Coastguard Worker                                  GLuint samples,
218*8975f5c5SAndroid Build Coastguard Worker                                  const mtl::Format &mtlFormat,
219*8975f5c5SAndroid Build Coastguard Worker                                  const gl::Extents &size);
220*8975f5c5SAndroid Build Coastguard Worker 
221*8975f5c5SAndroid Build Coastguard Worker     angle::Result redefineImage(const gl::Context *context,
222*8975f5c5SAndroid Build Coastguard Worker                                 const gl::ImageIndex &index,
223*8975f5c5SAndroid Build Coastguard Worker                                 const mtl::Format &mtlFormat,
224*8975f5c5SAndroid Build Coastguard Worker                                 const gl::Extents &size);
225*8975f5c5SAndroid Build Coastguard Worker 
226*8975f5c5SAndroid Build Coastguard Worker     angle::Result setImageImpl(const gl::Context *context,
227*8975f5c5SAndroid Build Coastguard Worker                                const gl::ImageIndex &index,
228*8975f5c5SAndroid Build Coastguard Worker                                const gl::InternalFormat &dstFormatInfo,
229*8975f5c5SAndroid Build Coastguard Worker                                const gl::Extents &size,
230*8975f5c5SAndroid Build Coastguard Worker                                GLenum srcFormat,
231*8975f5c5SAndroid Build Coastguard Worker                                GLenum srcType,
232*8975f5c5SAndroid Build Coastguard Worker                                const gl::PixelUnpackState &unpack,
233*8975f5c5SAndroid Build Coastguard Worker                                gl::Buffer *unpackBuffer,
234*8975f5c5SAndroid Build Coastguard Worker                                const uint8_t *pixels);
235*8975f5c5SAndroid Build Coastguard Worker     angle::Result setSubImageImpl(const gl::Context *context,
236*8975f5c5SAndroid Build Coastguard Worker                                   const gl::ImageIndex &index,
237*8975f5c5SAndroid Build Coastguard Worker                                   const gl::Box &area,
238*8975f5c5SAndroid Build Coastguard Worker                                   const gl::InternalFormat &formatInfo,
239*8975f5c5SAndroid Build Coastguard Worker                                   GLenum type,
240*8975f5c5SAndroid Build Coastguard Worker                                   const gl::PixelUnpackState &unpack,
241*8975f5c5SAndroid Build Coastguard Worker                                   gl::Buffer *unpackBuffer,
242*8975f5c5SAndroid Build Coastguard Worker                                   const uint8_t *pixels);
243*8975f5c5SAndroid Build Coastguard Worker 
244*8975f5c5SAndroid Build Coastguard Worker     angle::Result copySubImageImpl(const gl::Context *context,
245*8975f5c5SAndroid Build Coastguard Worker                                    const gl::ImageIndex &index,
246*8975f5c5SAndroid Build Coastguard Worker                                    const gl::Offset &destOffset,
247*8975f5c5SAndroid Build Coastguard Worker                                    const gl::Rectangle &sourceArea,
248*8975f5c5SAndroid Build Coastguard Worker                                    const gl::InternalFormat &internalFormat,
249*8975f5c5SAndroid Build Coastguard Worker                                    const FramebufferMtl *source,
250*8975f5c5SAndroid Build Coastguard Worker                                    const RenderTargetMtl *sourceRtt);
251*8975f5c5SAndroid Build Coastguard Worker     angle::Result copySubImageWithDraw(const gl::Context *context,
252*8975f5c5SAndroid Build Coastguard Worker                                        const gl::ImageIndex &index,
253*8975f5c5SAndroid Build Coastguard Worker                                        const gl::Offset &destOffset,
254*8975f5c5SAndroid Build Coastguard Worker                                        const gl::Rectangle &sourceArea,
255*8975f5c5SAndroid Build Coastguard Worker                                        const gl::InternalFormat &internalFormat,
256*8975f5c5SAndroid Build Coastguard Worker                                        const FramebufferMtl *source,
257*8975f5c5SAndroid Build Coastguard Worker                                        const RenderTargetMtl *sourceRtt);
258*8975f5c5SAndroid Build Coastguard Worker     angle::Result copySubImageCPU(const gl::Context *context,
259*8975f5c5SAndroid Build Coastguard Worker                                   const gl::ImageIndex &index,
260*8975f5c5SAndroid Build Coastguard Worker                                   const gl::Offset &destOffset,
261*8975f5c5SAndroid Build Coastguard Worker                                   const gl::Rectangle &sourceArea,
262*8975f5c5SAndroid Build Coastguard Worker                                   const gl::InternalFormat &internalFormat,
263*8975f5c5SAndroid Build Coastguard Worker                                   const FramebufferMtl *source,
264*8975f5c5SAndroid Build Coastguard Worker                                   const RenderTargetMtl *sourceRtt);
265*8975f5c5SAndroid Build Coastguard Worker 
266*8975f5c5SAndroid Build Coastguard Worker     angle::Result copySubTextureImpl(const gl::Context *context,
267*8975f5c5SAndroid Build Coastguard Worker                                      const gl::ImageIndex &index,
268*8975f5c5SAndroid Build Coastguard Worker                                      const gl::Offset &destOffset,
269*8975f5c5SAndroid Build Coastguard Worker                                      const gl::InternalFormat &internalFormat,
270*8975f5c5SAndroid Build Coastguard Worker                                      GLint sourceLevel,
271*8975f5c5SAndroid Build Coastguard Worker                                      const gl::Box &sourceBox,
272*8975f5c5SAndroid Build Coastguard Worker                                      bool unpackFlipY,
273*8975f5c5SAndroid Build Coastguard Worker                                      bool unpackPremultiplyAlpha,
274*8975f5c5SAndroid Build Coastguard Worker                                      bool unpackUnmultiplyAlpha,
275*8975f5c5SAndroid Build Coastguard Worker                                      const gl::Texture *source);
276*8975f5c5SAndroid Build Coastguard Worker 
277*8975f5c5SAndroid Build Coastguard Worker     angle::Result copySubTextureWithDraw(const gl::Context *context,
278*8975f5c5SAndroid Build Coastguard Worker                                          const gl::ImageIndex &index,
279*8975f5c5SAndroid Build Coastguard Worker                                          const gl::Offset &destOffset,
280*8975f5c5SAndroid Build Coastguard Worker                                          const gl::InternalFormat &internalFormat,
281*8975f5c5SAndroid Build Coastguard Worker                                          const mtl::MipmapNativeLevel &sourceNativeLevel,
282*8975f5c5SAndroid Build Coastguard Worker                                          const gl::Box &sourceBox,
283*8975f5c5SAndroid Build Coastguard Worker                                          const angle::Format &sourceAngleFormat,
284*8975f5c5SAndroid Build Coastguard Worker                                          bool unpackFlipY,
285*8975f5c5SAndroid Build Coastguard Worker                                          bool unpackPremultiplyAlpha,
286*8975f5c5SAndroid Build Coastguard Worker                                          bool unpackUnmultiplyAlpha,
287*8975f5c5SAndroid Build Coastguard Worker                                          const mtl::TextureRef &sourceTexture);
288*8975f5c5SAndroid Build Coastguard Worker 
289*8975f5c5SAndroid Build Coastguard Worker     angle::Result copySubTextureCPU(const gl::Context *context,
290*8975f5c5SAndroid Build Coastguard Worker                                     const gl::ImageIndex &index,
291*8975f5c5SAndroid Build Coastguard Worker                                     const gl::Offset &destOffset,
292*8975f5c5SAndroid Build Coastguard Worker                                     const gl::InternalFormat &internalFormat,
293*8975f5c5SAndroid Build Coastguard Worker                                     const mtl::MipmapNativeLevel &sourceNativeLevel,
294*8975f5c5SAndroid Build Coastguard Worker                                     const gl::Box &sourceBox,
295*8975f5c5SAndroid Build Coastguard Worker                                     const angle::Format &sourceAngleFormat,
296*8975f5c5SAndroid Build Coastguard Worker                                     bool unpackFlipY,
297*8975f5c5SAndroid Build Coastguard Worker                                     bool unpackPremultiplyAlpha,
298*8975f5c5SAndroid Build Coastguard Worker                                     bool unpackUnmultiplyAlpha,
299*8975f5c5SAndroid Build Coastguard Worker                                     const mtl::TextureRef &sourceTexture);
300*8975f5c5SAndroid Build Coastguard Worker 
301*8975f5c5SAndroid Build Coastguard Worker     // Copy data to texture's per array's slice/cube's face. NOTE: This function doesn't upload
302*8975f5c5SAndroid Build Coastguard Worker     // data to 3D texture's z layer. Metal treats 3D texture's z layer & array texture's slice
303*8975f5c5SAndroid Build Coastguard Worker     // differently. For array/cube texture, it is only possible to upload to one slice at a time.
304*8975f5c5SAndroid Build Coastguard Worker     angle::Result setPerSliceSubImage(const gl::Context *context,
305*8975f5c5SAndroid Build Coastguard Worker                                       int slice,
306*8975f5c5SAndroid Build Coastguard Worker                                       const MTLRegion &mtlArea,
307*8975f5c5SAndroid Build Coastguard Worker                                       const gl::InternalFormat &internalFormat,
308*8975f5c5SAndroid Build Coastguard Worker                                       GLenum type,
309*8975f5c5SAndroid Build Coastguard Worker                                       const angle::Format &pixelsAngleFormat,
310*8975f5c5SAndroid Build Coastguard Worker                                       size_t pixelsRowPitch,
311*8975f5c5SAndroid Build Coastguard Worker                                       size_t pixelsDepthPitch,
312*8975f5c5SAndroid Build Coastguard Worker                                       gl::Buffer *unpackBuffer,
313*8975f5c5SAndroid Build Coastguard Worker                                       const uint8_t *pixels,
314*8975f5c5SAndroid Build Coastguard Worker                                       const mtl::TextureRef &image);
315*8975f5c5SAndroid Build Coastguard Worker 
316*8975f5c5SAndroid Build Coastguard Worker     // Convert pixels to suported format before uploading to texture
317*8975f5c5SAndroid Build Coastguard Worker     angle::Result convertAndSetPerSliceSubImage(const gl::Context *context,
318*8975f5c5SAndroid Build Coastguard Worker                                                 int slice,
319*8975f5c5SAndroid Build Coastguard Worker                                                 const MTLRegion &mtlArea,
320*8975f5c5SAndroid Build Coastguard Worker                                                 const gl::InternalFormat &internalFormat,
321*8975f5c5SAndroid Build Coastguard Worker                                                 GLenum type,
322*8975f5c5SAndroid Build Coastguard Worker                                                 const angle::Format &pixelsAngleFormat,
323*8975f5c5SAndroid Build Coastguard Worker                                                 size_t pixelsRowPitch,
324*8975f5c5SAndroid Build Coastguard Worker                                                 size_t pixelsDepthPitch,
325*8975f5c5SAndroid Build Coastguard Worker                                                 gl::Buffer *unpackBuffer,
326*8975f5c5SAndroid Build Coastguard Worker                                                 const uint8_t *pixels,
327*8975f5c5SAndroid Build Coastguard Worker                                                 const mtl::TextureRef &image);
328*8975f5c5SAndroid Build Coastguard Worker 
329*8975f5c5SAndroid Build Coastguard Worker     angle::Result generateMipmapCPU(const gl::Context *context);
330*8975f5c5SAndroid Build Coastguard Worker 
331*8975f5c5SAndroid Build Coastguard Worker     bool needsFormatViewForPixelLocalStorage(const ShPixelLocalStorageOptions &) const;
332*8975f5c5SAndroid Build Coastguard Worker     bool isImmutableOrPBuffer() const;
333*8975f5c5SAndroid Build Coastguard Worker 
334*8975f5c5SAndroid Build Coastguard Worker     mtl::Format mFormat;
335*8975f5c5SAndroid Build Coastguard Worker     egl::Surface *mBoundSurface = nullptr;
336*8975f5c5SAndroid Build Coastguard Worker     class NativeTextureWrapper;
337*8975f5c5SAndroid Build Coastguard Worker     class NativeTextureWrapperWithViewSupport;
338*8975f5c5SAndroid Build Coastguard Worker     // The real texture used by Metal.
339*8975f5c5SAndroid Build Coastguard Worker     // For non-immutable texture, this usually contains levels from (GL base level -> GL max level).
340*8975f5c5SAndroid Build Coastguard Worker     // For immutable texture, this contains levels allocated with glTexStorage which could be
341*8975f5c5SAndroid Build Coastguard Worker     // outside (GL base level -> GL max level) range.
342*8975f5c5SAndroid Build Coastguard Worker     std::unique_ptr<NativeTextureWrapperWithViewSupport> mNativeTextureStorage;
343*8975f5c5SAndroid Build Coastguard Worker     // The view of mNativeTextureStorage from (GL base level -> GL max level)
344*8975f5c5SAndroid Build Coastguard Worker     std::unique_ptr<NativeTextureWrapper> mViewFromBaseToMaxLevel;
345*8975f5c5SAndroid Build Coastguard Worker     id<MTLSamplerState> mMetalSamplerState = nil;
346*8975f5c5SAndroid Build Coastguard Worker 
347*8975f5c5SAndroid Build Coastguard Worker     // Number of slices
348*8975f5c5SAndroid Build Coastguard Worker     uint32_t mSlices = 1;
349*8975f5c5SAndroid Build Coastguard Worker 
350*8975f5c5SAndroid Build Coastguard Worker     // Stored images array defined by glTexImage/glCopy*.
351*8975f5c5SAndroid Build Coastguard Worker     // Once the images array is complete, they will be transferred to real texture object.
352*8975f5c5SAndroid Build Coastguard Worker     // NOTE:
353*8975f5c5SAndroid Build Coastguard Worker     //  - For Cube map, there will be at most 6 entries in the map table, one for each face. This is
354*8975f5c5SAndroid Build Coastguard Worker     //  because the Cube map's image is defined per face & per level.
355*8975f5c5SAndroid Build Coastguard Worker     //  - For other texture types, there will be only one entry in the map table. All other textures
356*8975f5c5SAndroid Build Coastguard Worker     //  except Cube map has texture image defined per level (all slices included).
357*8975f5c5SAndroid Build Coastguard Worker     //  - The second dimension is indexed by GL level.
358*8975f5c5SAndroid Build Coastguard Worker     std::map<int, gl::TexLevelArray<ImageDefinitionMtl>> mTexImageDefs;
359*8975f5c5SAndroid Build Coastguard Worker     // 1st index = image index, 2nd index = samples count.
360*8975f5c5SAndroid Build Coastguard Worker     std::map<gl::ImageIndex, gl::RenderToTextureImageMap<RenderTargetMtl>> mRenderTargets;
361*8975f5c5SAndroid Build Coastguard Worker     std::map<gl::ImageIndex, gl::RenderToTextureImageMap<mtl::TextureRef>> mImplicitMSTextures;
362*8975f5c5SAndroid Build Coastguard Worker 
363*8975f5c5SAndroid Build Coastguard Worker     // Lazily populated 2D views for shader storage images.
364*8975f5c5SAndroid Build Coastguard Worker     // May have different formats than the original texture.
365*8975f5c5SAndroid Build Coastguard Worker     // Indexed by format, then layer, then level.
366*8975f5c5SAndroid Build Coastguard Worker     std::map<MTLPixelFormat, LayerLevelTextureViewVector> mShaderImageViews;
367*8975f5c5SAndroid Build Coastguard Worker 
368*8975f5c5SAndroid Build Coastguard Worker     // Mipmap views are indexed from (base GL level -> max GL level):
369*8975f5c5SAndroid Build Coastguard Worker     mtl::NativeTexLevelArray mLevelViewsWithinBaseMax;
370*8975f5c5SAndroid Build Coastguard Worker 
371*8975f5c5SAndroid Build Coastguard Worker     // The swizzled or stencil view used for shader sampling.
372*8975f5c5SAndroid Build Coastguard Worker     mtl::TextureRef mSwizzleStencilSamplingView;
373*8975f5c5SAndroid Build Coastguard Worker };
374*8975f5c5SAndroid Build Coastguard Worker 
375*8975f5c5SAndroid Build Coastguard Worker }  // namespace rx
376*8975f5c5SAndroid Build Coastguard Worker 
377*8975f5c5SAndroid Build Coastguard Worker #endif /* LIBANGLE_RENDERER_METAL_TEXTUREMTL_H_ */
378