Hash :
87187835
Author :
Date :
2022-04-07T13:51:10
Metal: For readPixels copy IOSurface to non-IOSurface texture For intel GPU/Drivers, it's faster to copy an IOSurface texture to a non-IOSurface texture and read from the copy than it is to read directly from the IOSurface texture. Bug: angleproject:7117 Change-Id: I786009444480f75be6feb05f09f87fb45a3186b1 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3573078 Reviewed-by: Kenneth Russell <kbr@chromium.org> Reviewed-by: Kyle Piddington <kpiddington@apple.com> Commit-Queue: Gregg Tavares <gman@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// mtl_resources.h:
// Declares wrapper classes for Metal's MTLTexture and MTLBuffer.
//
#ifndef LIBANGLE_RENDERER_METAL_MTL_RESOURCES_H_
#define LIBANGLE_RENDERER_METAL_MTL_RESOURCES_H_
#import <Metal/Metal.h>
#include <atomic>
#include <memory>
#include "common/FastVector.h"
#include "common/MemoryBuffer.h"
#include "common/angleutils.h"
#include "libANGLE/Error.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/renderer/metal/mtl_common.h"
#include "libANGLE/renderer/metal/mtl_format_utils.h"
namespace rx
{
class ContextMtl;
namespace mtl
{
class ContextDevice;
class CommandQueue;
class BlitCommandEncoder;
class Resource;
class Texture;
class Buffer;
using ResourceRef = std::shared_ptr<Resource>;
using TextureRef = std::shared_ptr<Texture>;
using TextureWeakRef = std::weak_ptr<Texture>;
using BufferRef = std::shared_ptr<Buffer>;
using BufferWeakRef = std::weak_ptr<Buffer>;
class Resource : angle::NonCopyable
{
public:
virtual ~Resource() {}
// Check whether the resource still being used by GPU including the pending (uncommitted)
// command buffer.
bool isBeingUsedByGPU(Context *context) const;
// Checks whether the last command buffer that uses the given resource has been committed or not
bool hasPendingWorks(Context *context) const;
void setUsedByCommandBufferWithQueueSerial(uint64_t serial, bool writing);
uint64_t getCommandBufferQueueSerial() const { return mUsageRef->cmdBufferQueueSerial; }
// Flag indicate whether we should synchronize the content to CPU after GPU changed this
// resource's content.
bool isCPUReadMemNeedSync() const { return mUsageRef->cpuReadMemNeedSync; }
void resetCPUReadMemNeedSync() { mUsageRef->cpuReadMemNeedSync = false; }
bool isCPUReadMemSyncPending() const { return mUsageRef->cpuReadMemSyncPending; }
void setCPUReadMemSyncPending(bool value) const { mUsageRef->cpuReadMemSyncPending = value; }
void resetCPUReadMemSyncPending() { mUsageRef->cpuReadMemSyncPending = false; }
bool isCPUReadMemDirty() const { return mUsageRef->cpuReadMemDirty; }
void resetCPUReadMemDirty() { mUsageRef->cpuReadMemDirty = false; }
virtual size_t estimatedByteSize() const = 0;
virtual id getID() const = 0;
protected:
Resource();
// Share the GPU usage ref with other resource
Resource(Resource *other);
void reset();
private:
struct UsageRef
{
// The id of the last command buffer that is using this resource.
uint64_t cmdBufferQueueSerial = 0;
// This flag means the resource was issued to be modified by GPU, if CPU wants to read
// its content, explicit synchronization call must be invoked.
bool cpuReadMemNeedSync = false;
// This flag is set when synchronization for the resource has been
// encoded on the GPU, and a map operation must wait
// until it's completed.
bool cpuReadMemSyncPending = false;
// This flag is useful for BufferMtl to know whether it should update the shadow copy
bool cpuReadMemDirty = false;
};
// One resource object might just be a view of another resource. For example, a texture 2d
// object might be a view of one face of a cube texture object. Another example is one texture
// object of size 2x2 might be a mipmap view of a texture object size 4x4. Thus, if one object
// is being used by a command buffer, it means the other object is being used also. In this
// case, the two objects must share the same UsageRef property.
std::shared_ptr<UsageRef> mUsageRef;
};
class Texture final : public Resource,
public WrappedObject<id<MTLTexture>>,
public std::enable_shared_from_this<Texture>
{
public:
static angle::Result Make2DTexture(ContextMtl *context,
const Format &format,
uint32_t width,
uint32_t height,
uint32_t mips /** use zero to create full mipmaps chain */,
bool renderTargetOnly,
bool allowFormatView,
TextureRef *refOut);
// On macOS, memory will still be allocated for this texture.
static angle::Result MakeMemoryLess2DTexture(ContextMtl *context,
const Format &format,
uint32_t width,
uint32_t height,
TextureRef *refOut);
static angle::Result MakeCubeTexture(ContextMtl *context,
const Format &format,
uint32_t size,
uint32_t mips /** use zero to create full mipmaps chain */,
bool renderTargetOnly,
bool allowFormatView,
TextureRef *refOut);
static angle::Result Make2DMSTexture(ContextMtl *context,
const Format &format,
uint32_t width,
uint32_t height,
uint32_t samples,
bool renderTargetOnly,
bool allowFormatView,
TextureRef *refOut);
static angle::Result Make2DArrayTexture(ContextMtl *context,
const Format &format,
uint32_t width,
uint32_t height,
uint32_t mips,
uint32_t arrayLength,
bool renderTargetOnly,
bool allowFormatView,
TextureRef *refOut);
static angle::Result Make3DTexture(ContextMtl *context,
const Format &format,
uint32_t width,
uint32_t height,
uint32_t depth,
uint32_t mips,
bool renderTargetOnly,
bool allowFormatView,
TextureRef *refOut);
static TextureRef MakeFromMetal(id<MTLTexture> metalTexture);
// Allow CPU to read & write data directly to this texture?
bool isCPUAccessible() const;
// Allow shaders to read/sample this texture?
// Texture created with renderTargetOnly flag won't be readable
bool isShaderReadable() const;
bool supportFormatView() const;
void replace2DRegion(ContextMtl *context,
const MTLRegion ®ion,
const MipmapNativeLevel &mipmapLevel,
uint32_t slice,
const uint8_t *data,
size_t bytesPerRow);
void replaceRegion(ContextMtl *context,
const MTLRegion ®ion,
const MipmapNativeLevel &mipmapLevel,
uint32_t slice,
const uint8_t *data,
size_t bytesPerRow,
size_t bytesPer2DImage);
void getBytes(ContextMtl *context,
size_t bytesPerRow,
size_t bytesPer2DInage,
const MTLRegion ®ion,
const MipmapNativeLevel &mipmapLevel,
uint32_t slice,
uint8_t *dataOut);
// Create 2d view of a cube face which full range of mip levels.
TextureRef createCubeFaceView(uint32_t face);
// Create a view of one slice at a level.
TextureRef createSliceMipView(uint32_t slice, const MipmapNativeLevel &level);
// Create a view of a level.
TextureRef createMipView(const MipmapNativeLevel &level);
// Create a view with different format
TextureRef createViewWithDifferentFormat(MTLPixelFormat format);
// Same as above but the target format must be compatible, for example sRGB to linear. In this
// case texture doesn't need format view usage flag.
TextureRef createViewWithCompatibleFormat(MTLPixelFormat format);
// Create a swizzled view
TextureRef createSwizzleView(const TextureSwizzleChannels &swizzle);
MTLTextureType textureType() const;
MTLPixelFormat pixelFormat() const;
uint32_t mipmapLevels() const;
uint32_t arrayLength() const;
uint32_t cubeFacesOrArrayLength() const;
uint32_t width(const MipmapNativeLevel &level) const;
uint32_t height(const MipmapNativeLevel &level) const;
uint32_t depth(const MipmapNativeLevel &level) const;
gl::Extents size(const MipmapNativeLevel &level) const;
gl::Extents size(const ImageNativeIndex &index) const;
uint32_t widthAt0() const { return width(kZeroNativeMipLevel); }
uint32_t heightAt0() const { return height(kZeroNativeMipLevel); }
uint32_t depthAt0() const { return depth(kZeroNativeMipLevel); }
gl::Extents sizeAt0() const { return size(kZeroNativeMipLevel); }
uint32_t samples() const;
bool hasIOSurface() const;
bool sameTypeAndDimemsionsAs(const TextureRef &other) const;
angle::Result resize(ContextMtl *context, uint32_t width, uint32_t height);
// For render target
MTLColorWriteMask getColorWritableMask() const { return *mColorWritableMask; }
void setColorWritableMask(MTLColorWriteMask mask) { *mColorWritableMask = mask; }
// Get reading copy. Used for reading non-readable texture or reading stencil value from
// packed depth & stencil texture.
// NOTE: this only copies 1 depth slice of the 3D texture.
// The texels will be copied to region(0, 0, 0, areaToCopy.size) of the returned texture.
// The returned pointer will be retained by the original texture object.
// Calling getReadableCopy() will overwrite previously returned texture.
TextureRef getReadableCopy(ContextMtl *context,
mtl::BlitCommandEncoder *encoder,
const uint32_t levelToCopy,
const uint32_t sliceToCopy,
const MTLRegion &areaToCopy);
void releaseReadableCopy();
// Get stencil view
TextureRef getStencilView();
// Get linear color
TextureRef getLinearColorView();
// Change the wrapped metal object. Special case for swapchain image
void set(id<MTLTexture> metalTexture);
// Explicitly sync content between CPU and GPU
void syncContent(ContextMtl *context, mtl::BlitCommandEncoder *encoder);
void setEstimatedByteSize(size_t bytes) { mEstimatedByteSize = bytes; }
size_t estimatedByteSize() const override { return mEstimatedByteSize; }
id getID() const override { return get(); }
private:
using ParentClass = WrappedObject<id<MTLTexture>>;
static angle::Result MakeTexture(ContextMtl *context,
const Format &mtlFormat,
MTLTextureDescriptor *desc,
uint32_t mips,
bool renderTargetOnly,
bool allowFormatView,
TextureRef *refOut);
static angle::Result MakeTexture(ContextMtl *context,
const Format &mtlFormat,
MTLTextureDescriptor *desc,
uint32_t mips,
bool renderTargetOnly,
bool allowFormatView,
bool memoryLess,
TextureRef *refOut);
static angle::Result MakeTexture(ContextMtl *context,
const Format &mtlFormat,
MTLTextureDescriptor *desc,
IOSurfaceRef surfaceRef,
NSUInteger slice,
bool renderTargetOnly,
TextureRef *refOut);
Texture(id<MTLTexture> metalTexture);
Texture(ContextMtl *context,
MTLTextureDescriptor *desc,
uint32_t mips,
bool renderTargetOnly,
bool allowFormatView);
Texture(ContextMtl *context,
MTLTextureDescriptor *desc,
uint32_t mips,
bool renderTargetOnly,
bool allowFormatView,
bool memoryLess);
Texture(ContextMtl *context,
MTLTextureDescriptor *desc,
IOSurfaceRef iosurface,
NSUInteger plane,
bool renderTargetOnly);
// Create a texture view
Texture(Texture *original, MTLPixelFormat format);
Texture(Texture *original, MTLTextureType type, NSRange mipmapLevelRange, NSRange slices);
Texture(Texture *original, const TextureSwizzleChannels &swizzle);
void syncContentIfNeeded(ContextMtl *context);
AutoObjCObj<MTLTextureDescriptor> mCreationDesc;
// This property is shared between this object and its views:
std::shared_ptr<MTLColorWriteMask> mColorWritableMask;
// Linear view of sRGB texture
TextureRef mLinearColorView;
TextureRef mStencilView;
// Readable copy of texture
TextureRef mReadCopy;
size_t mEstimatedByteSize = 0;
};
class Buffer final : public Resource, public WrappedObject<id<MTLBuffer>>
{
public:
static angle::Result MakeBuffer(ContextMtl *context,
size_t size,
const uint8_t *data,
BufferRef *bufferOut);
static angle::Result MakeBufferWithSharedMemOpt(ContextMtl *context,
bool forceUseSharedMem,
size_t size,
const uint8_t *data,
BufferRef *bufferOut);
static angle::Result MakeBufferWithResOpt(ContextMtl *context,
MTLResourceOptions resourceOptions,
size_t size,
const uint8_t *data,
BufferRef *bufferOut);
angle::Result reset(ContextMtl *context, size_t size, const uint8_t *data);
angle::Result resetWithSharedMemOpt(ContextMtl *context,
bool forceUseSharedMem,
size_t size,
const uint8_t *data);
angle::Result resetWithResOpt(ContextMtl *context,
MTLResourceOptions resourceOptions,
size_t size,
const uint8_t *data);
const uint8_t *mapReadOnly(ContextMtl *context);
uint8_t *map(ContextMtl *context);
uint8_t *mapWithOpt(ContextMtl *context, bool readonly, bool noSync);
void unmap(ContextMtl *context);
// Same as unmap but do not do implicit flush()
void unmapNoFlush(ContextMtl *context);
void unmapAndFlushSubset(ContextMtl *context, size_t offsetWritten, size_t sizeWritten);
void flush(ContextMtl *context, size_t offsetWritten, size_t sizeWritten);
size_t size() const;
bool useSharedMem() const;
// Explicitly sync content between CPU and GPU
void syncContent(ContextMtl *context, mtl::BlitCommandEncoder *encoder);
size_t estimatedByteSize() const override { return size(); }
id getID() const override { return get(); }
private:
Buffer(ContextMtl *context, bool forceUseSharedMem, size_t size, const uint8_t *data);
Buffer(ContextMtl *context,
MTLResourceOptions resourceOptions,
size_t size,
const uint8_t *data);
bool mMapReadOnly = true;
};
class NativeTexLevelArray
{
public:
TextureRef &at(const MipmapNativeLevel &level) { return mTexLevels.at(level.get()); }
const TextureRef &at(const MipmapNativeLevel &level) const
{
return mTexLevels.at(level.get());
}
TextureRef &operator[](const MipmapNativeLevel &level) { return at(level); }
const TextureRef &operator[](const MipmapNativeLevel &level) const { return at(level); }
gl::TexLevelArray<TextureRef>::iterator begin() { return mTexLevels.begin(); }
gl::TexLevelArray<TextureRef>::const_iterator begin() const { return mTexLevels.begin(); }
gl::TexLevelArray<TextureRef>::iterator end() { return mTexLevels.end(); }
gl::TexLevelArray<TextureRef>::const_iterator end() const { return mTexLevels.end(); }
private:
gl::TexLevelArray<TextureRef> mTexLevels;
};
} // namespace mtl
} // namespace rx
#endif /* LIBANGLE_RENDERER_METAL_MTL_RESOURCES_H_ */