Changeset 273516 in webkit


Ignore:
Timestamp:
Feb 25, 2021, 3:25:29 PM (4 years ago)
Author:
dino@apple.com
Message:

Fix crashes in fast/canvas/webgl/texImage video tests
Refactor IOSurfaceSurfaceMtl to match open source implementation.
Additional cleanup

https://bugs.webkit.org/show_bug.cgi?id=222331

Patch by Kyle Piddington <Kyle Piddington> on 2021-02-25
Reviewed by Dean Jackson.

  • src/libANGLE/renderer/metal/ContextMtl.h:
  • src/libANGLE/renderer/metal/ContextMtl.mm:

(rx::ContextMtl::onBackbufferResized):

  • src/libANGLE/renderer/metal/DisplayMtl.mm:

(rx::DisplayMtl::createPbufferSurface):
(rx::DisplayMtl::createPbufferFromClientBuffer):

  • src/libANGLE/renderer/metal/FrameBufferMtl.h:

(rx::FramebufferMtl::getAttachedBackbuffer const):

  • src/libANGLE/renderer/metal/FrameBufferMtl.mm:

(rx::FramebufferMtl::FramebufferMtl):

  • src/libANGLE/renderer/metal/IOSurfaceSurfaceMtl.h:
  • src/libANGLE/renderer/metal/IOSurfaceSurfaceMtl.mm:

(rx::IOSurfaceSurfaceMtl::IOSurfaceSurfaceMtl):
(rx::IOSurfaceSurfaceMtl::~IOSurfaceSurfaceMtl):
(rx::IOSurfaceSurfaceMtl::bindTexImage):
(rx::IOSurfaceSurfaceMtl::releaseTexImage):
(rx::IOSurfaceSurfaceMtl::getAttachmentRenderTarget):
(rx::IOSurfaceSurfaceMtl::ensureColorTextureCreated):
(rx::IOSurfaceSurfaceMtl::ValidateAttributes):

  • src/libANGLE/renderer/metal/SurfaceMtl.h:

(rx::SurfaceMtl::getColorTexture):
(rx::SurfaceMtl::getSamples const):
(rx::SurfaceMtl::hasRobustResourceInit const):
(rx::WindowSurfaceMtl::preserveBuffer const):

  • src/libANGLE/renderer/metal/SurfaceMtl.mm:

(rx::SurfaceMtl::SurfaceMtl):

  • src/libANGLE/renderer/metal/TextureMtl.h:
  • src/libANGLE/renderer/metal/TextureMtl.mm:

(rx::TextureMtl::ensureTextureCreated):
(rx::TextureMtl::ensureSamplerStateCreated):
(rx::TextureMtl::bindTexImage):
(rx::TextureMtl::releaseTexImage):
(rx::TextureMtl::getAttachmentRenderTarget):

  • src/libANGLE/renderer/metal/mtl_common.h:
Location:
trunk/Source/ThirdParty/ANGLE
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/ThirdParty/ANGLE/ChangeLog

    r273200 r273516  
     12021-02-25  Kyle Piddington  <kpiddington@apple.com>
     2
     3        Fix crashes in fast/canvas/webgl/texImage video tests
     4        Refactor IOSurfaceSurfaceMtl to match open source implementation.
     5        Additional cleanup
     6
     7        https://bugs.webkit.org/show_bug.cgi?id=222331
     8
     9        Reviewed by Dean Jackson.
     10
     11
     12        * src/libANGLE/renderer/metal/ContextMtl.h:
     13        * src/libANGLE/renderer/metal/ContextMtl.mm:
     14        (rx::ContextMtl::onBackbufferResized):
     15        * src/libANGLE/renderer/metal/DisplayMtl.mm:
     16        (rx::DisplayMtl::createPbufferSurface):
     17        (rx::DisplayMtl::createPbufferFromClientBuffer):
     18        * src/libANGLE/renderer/metal/FrameBufferMtl.h:
     19        (rx::FramebufferMtl::getAttachedBackbuffer const):
     20        * src/libANGLE/renderer/metal/FrameBufferMtl.mm:
     21        (rx::FramebufferMtl::FramebufferMtl):
     22        * src/libANGLE/renderer/metal/IOSurfaceSurfaceMtl.h:
     23        * src/libANGLE/renderer/metal/IOSurfaceSurfaceMtl.mm:
     24        (rx::IOSurfaceSurfaceMtl::IOSurfaceSurfaceMtl):
     25        (rx::IOSurfaceSurfaceMtl::~IOSurfaceSurfaceMtl):
     26        (rx::IOSurfaceSurfaceMtl::bindTexImage):
     27        (rx::IOSurfaceSurfaceMtl::releaseTexImage):
     28        (rx::IOSurfaceSurfaceMtl::getAttachmentRenderTarget):
     29        (rx::IOSurfaceSurfaceMtl::ensureColorTextureCreated):
     30        (rx::IOSurfaceSurfaceMtl::ValidateAttributes):
     31        * src/libANGLE/renderer/metal/SurfaceMtl.h:
     32        (rx::SurfaceMtl::getColorTexture):
     33        (rx::SurfaceMtl::getSamples const):
     34        (rx::SurfaceMtl::hasRobustResourceInit const):
     35        (rx::WindowSurfaceMtl::preserveBuffer const):
     36        * src/libANGLE/renderer/metal/SurfaceMtl.mm:
     37        (rx::SurfaceMtl::SurfaceMtl):
     38        * src/libANGLE/renderer/metal/TextureMtl.h:
     39        * src/libANGLE/renderer/metal/TextureMtl.mm:
     40        (rx::TextureMtl::ensureTextureCreated):
     41        (rx::TextureMtl::ensureSamplerStateCreated):
     42        (rx::TextureMtl::bindTexImage):
     43        (rx::TextureMtl::releaseTexImage):
     44        (rx::TextureMtl::getAttachmentRenderTarget):
     45        * src/libANGLE/renderer/metal/mtl_common.h:
     46
    1472021-02-19  Kyle Piddington  <kpiddington@apple.com>
    248
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/ContextMtl.h

    r270733 r273516  
    3030class ProgramMtl;
    3131class RenderTargetMtl;
    32 class SurfaceMtlProtocol;
    3332class BufferMtl;
     33class WindowSurfaceMtl;
    3434
    3535class ContextMtl : public ContextImpl, public mtl::Context
     
    293293                                       FramebufferMtl *framebuffer,
    294294                                       bool renderPassChanged);
    295     void onBackbufferResized(const gl::Context *context, SurfaceMtlProtocol *backbuffer);
     295    void onBackbufferResized(const gl::Context *context, WindowSurfaceMtl *backbuffer);
    296296
    297297    // Invoke by QueryMtl
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/ContextMtl.mm

    r273200 r273516  
    19331933}
    19341934
    1935 void ContextMtl::onBackbufferResized(const gl::Context *context, SurfaceMtlProtocol *backbuffer)
     1935void ContextMtl::onBackbufferResized(const gl::Context *context, WindowSurfaceMtl *backbuffer)
    19361936{
    19371937    const gl::State &glState    = getState();
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/DisplayMtl.mm

    r273032 r273516  
    271271                                              const egl::AttributeMap &attribs)
    272272{
    273     UNIMPLEMENTED();
    274     return static_cast<SurfaceImpl *>(0);
     273    return new PBufferSurfaceMtl(this, state, attribs);
    275274}
    276275
     
    280279                                                       const egl::AttributeMap &attribs)
    281280{
    282     return new IOSurfaceSurfaceMtl(this, state, clientBuffer, attribs);
     281    switch (buftype)
     282    {
     283        case EGL_IOSURFACE_ANGLE:
     284            return new IOSurfaceSurfaceMtl(this, state, clientBuffer, attribs);
     285            break;
     286        default:
     287            UNREACHABLE();
     288            return nullptr;
     289    }
    283290}
    284291
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/FrameBufferMtl.h

    r270733 r273516  
    2424}  // namespace mtl
    2525class ContextMtl;
    26 class SurfaceMtl;
     26class WindowSurfaceMtl;
    2727
    2828class FramebufferMtl : public FramebufferImpl
     
    3131    explicit FramebufferMtl(const gl::FramebufferState &state,
    3232                            bool flipY,
    33                             SurfaceMtlProtocol *backbuffer);
     33                            WindowSurfaceMtl *backbuffer);
    3434    ~FramebufferMtl() override;
    3535    void destroy(const gl::Context *context) override;
     
    100100    gl::Rectangle getCompleteRenderArea() const;
    101101    int getSamples() const;
    102     SurfaceMtlProtocol *getAttachedBackbuffer() const { return mBackbuffer; }
     102    WindowSurfaceMtl *getAttachedBackbuffer() const { return mBackbuffer; }
    103103
    104104    bool renderPassHasStarted(ContextMtl *contextMtl) const;
     
    222222    bool mRenderPassCleanStart = false;
    223223
    224     SurfaceMtlProtocol *mBackbuffer = nullptr;
     224    WindowSurfaceMtl *mBackbuffer = nullptr;
    225225    const bool mFlipY               = false;
    226226
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/FrameBufferMtl.mm

    r271080 r273516  
    5757FramebufferMtl::FramebufferMtl(const gl::FramebufferState &state,
    5858                               bool flipY,
    59                                SurfaceMtlProtocol *backbuffer)
     59                               WindowSurfaceMtl *backbuffer)
    6060    : FramebufferImpl(state), mBackbuffer(backbuffer), mFlipY(flipY)
    6161{
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/IOSurfaceSurfaceMtl.h

    r273200 r273516  
    2323class DisplayMTL;
    2424
    25 class IOSurfaceSurfaceMtl : public SurfaceMtlProtocol
     25// Offscreen created from IOSurface
     26class IOSurfaceSurfaceMtl : public OffscreenSurfaceMtl
    2627{
    2728  public:
     
    3233    ~IOSurfaceSurfaceMtl() override;
    3334
    34     void destroy(const egl::Display *display) override;
    35 
    36     egl::Error initialize(const egl::Display *display) override;
    37     FramebufferImpl *createDefaultFramebuffer(const gl::Context *context,
    38                                               const gl::FramebufferState &state) override;
    39 
    40     egl::Error makeCurrent(const gl::Context *context) override;
    41     egl::Error unMakeCurrent(const gl::Context *context) override;
    42     egl::Error swap(const gl::Context *context) override;
    43     egl::Error postSubBuffer(const gl::Context *context,
    44                              EGLint x,
    45                              EGLint y,
    46                              EGLint width,
    47                              EGLint height) override;
    48 
    49     egl::Error querySurfacePointerANGLE(EGLint attribute, void **value) override;
    5035    egl::Error bindTexImage(const gl::Context *context,
    5136                            gl::Texture *texture,
    5237                            EGLint buffer) override;
    5338    egl::Error releaseTexImage(const gl::Context *context, EGLint buffer) override;
    54     egl::Error getSyncValues(EGLuint64KHR *ust, EGLuint64KHR *msc, EGLuint64KHR *sbc) override;
    55     egl::Error getMscRate(EGLint *numerator, EGLint *denominator) override;
    56     void setSwapInterval(EGLint interval) override;
    57     void setFixedWidth(EGLint width) override;
    58     void setFixedHeight(EGLint height) override;
    59 
    60     // Width can change when the client window resizes.
    61     EGLint getWidth() const override;
    62     // Height can change when the client window resizes.
    63     EGLint getHeight() const override;
    64 
    65     EGLint isPostSubBufferSupported() const override;
    66     EGLint getSwapBehavior() const override;
    6739
    6840    angle::Result getAttachmentRenderTarget(const gl::Context *context,
     
    7244                                            FramebufferAttachmentRenderTarget **rtOut) override;
    7345
    74     angle::Result ensureCurrentDrawableObtained(const gl::Context *context) override;
    75     angle::Result ensureCurrentDrawableObtained(const gl::Context *context,
    76                                                 bool *newDrawableOut) override;
    77     int getSamples() const override { return 1; }
    78     bool preserveBuffer() const override { return false; }
    79     mtl::TextureRef getTexture(const gl::Context *context);
    80     bool hasRobustResourceInit() const override { return false; }
    81     angle::Result ensureColorTextureReadyForReadPixels(const gl::Context *context) override
    82     {
    83         return angle::Result::Continue;
    84     }
    85     const mtl::TextureRef &getColorTexture() override { return mIOSurfaceTexture; }
     46    static bool ValidateAttributes(EGLClientBuffer buffer, const egl::AttributeMap &attribs);
    8647
    8748  private:
    88     angle::Result ensureTextureCreated(const gl::Context *context);
    89     angle::Result createBackingTexture(const gl::Context *context);
    90     angle::Result createTexture(const gl::Context *context,
    91                                 const mtl::Format &format,
    92                                 uint32_t width,
    93                                 uint32_t height,
    94                                 bool renderTargetOnly,
    95                                 mtl::TextureRef *textureOut);
     49    angle::Result ensureColorTextureCreated(const gl::Context *context);
    9650
    97     angle::Result initializeAlphaChannel(const gl::Context *context, GLuint texture);
    98 
    99 #if defined(ANGLE_PLATFORM_IOS_SIMULATOR)
    100     IOSurfaceLockOptions getIOSurfaceLockOptions() const;
    101 #endif
    102 
    103     ContextMtl *contextMTL;
    10451    IOSurfaceRef mIOSurface;
    105     GLint mGLInternalFormat;
    106     mtl::TextureRef mIOSurfaceTexture;
    107     mtl::TextureRef mIOSurfaceTextureView;
    108     mtl::Format mFormat;
    109     mtl::Format mInternalFormat;
    110     RenderTargetMtl mRenderTarget;
    111     bool mIOSurfaceTextureCreated;
    112     int mWidth;
    113     int mHeight;
    114     int mPlane;
    115     int mFormatIndex;
    116     int mRowStrideInPixels;
    117 
    118 #if defined(ANGLE_PLATFORM_IOS_SIMULATOR)
    119     GLuint mBoundTextureID;
    120     bool mUploadFromIOSurface;
    121     bool mReadbackToIOSurface;
    122 #endif
     52    NSUInteger mIOSurfacePlane;
     53    int mIOSurfaceFormatIdx;
    12354};
    12455
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/IOSurfaceSurfaceMtl.mm

    r273200 r273516  
    3636    GLenum internalFormat;
    3737    GLenum type;
    38     angle::FormatID pixelFormat;
    39     angle::FormatID internalPixelFormat;
     38    size_t componentBytes;
     39
     40    angle::FormatID nativeAngleFormatId;
    4041};
    4142
    42 static const IOSurfaceFormatInfo kIOSurfaceFormats[] = {
    43     {GL_RED, GL_UNSIGNED_BYTE, angle::FormatID::R8_UNORM, angle::FormatID::R8_UNORM},
    44     {GL_R16UI, GL_UNSIGNED_SHORT, angle::FormatID::R16G16_UINT, angle::FormatID::R16G16_UINT},
    45     {GL_RG, GL_UNSIGNED_BYTE, angle::FormatID::R8G8_UNORM, angle::FormatID::R8G8_UNORM},
    46     {GL_RGB, GL_UNSIGNED_BYTE, angle::FormatID::R8G8B8A8_UNORM, angle::FormatID::B8G8R8A8_UNORM},
    47     {GL_BGRA_EXT, GL_UNSIGNED_BYTE, angle::FormatID::B8G8R8A8_UNORM,
    48      angle::FormatID::B8G8R8A8_UNORM},
    49     {GL_RGB10_A2, GL_UNSIGNED_INT_2_10_10_10_REV, angle::FormatID::R10G10B10A2_UNORM,
    50      angle::FormatID::R10G10B10A2_UNORM},
    51     {GL_RGBA, GL_HALF_FLOAT, angle::FormatID::R16G16B16A16_FLOAT,
    52      angle::FormatID::R16G16B16A16_FLOAT},
    53 };
     43// clang-format off
     44// NOTE(hqle): Support R16_UINT once GLES3 is complete.
     45constexpr std::array<IOSurfaceFormatInfo, 8> kIOSurfaceFormats = {{
     46    {GL_RED,      GL_UNSIGNED_BYTE,               1, angle::FormatID::R8_UNORM},
     47    {GL_RED,      GL_UNSIGNED_SHORT,              2, angle::FormatID::R16_UNORM},
     48    {GL_RG,       GL_UNSIGNED_BYTE,               2, angle::FormatID::R8G8_UNORM},
     49    {GL_RG,       GL_UNSIGNED_SHORT,              4, angle::FormatID::R16G16_UNORM},
     50    {GL_RGB,      GL_UNSIGNED_BYTE,               4, angle::FormatID::B8G8R8A8_UNORM},
     51    {GL_BGRA_EXT, GL_UNSIGNED_BYTE,               4, angle::FormatID::B8G8R8A8_UNORM},
     52    {GL_RGBA,     GL_HALF_FLOAT,                  8, angle::FormatID::R16G16B16A16_FLOAT},
     53    {GL_RGB10_A2, GL_UNSIGNED_INT_2_10_10_10_REV, 4, angle::FormatID::B10G10R10A2_UNORM},
     54}};
     55// clang-format on
    5456
    5557int FindIOSurfaceFormatIndex(GLenum internalFormat, GLenum type)
    5658{
    57     for (size_t i = 0; i < static_cast<size_t>(ArraySize(kIOSurfaceFormats)); ++i)
     59    for (int i = 0; i < static_cast<int>(kIOSurfaceFormats.size()); ++i)
    5860    {
    5961        const auto &formatInfo = kIOSurfaceFormats[i];
    6062        if (formatInfo.internalFormat == internalFormat && formatInfo.type == type)
    6163        {
    62             return static_cast<int>(i);
     64            return i;
    6365        }
    6466    }
    6567    return -1;
    6668}
    67 
    68 // TODO(jcunningham) : refactor this and surfacemtl to reduce copy+pasted code
    6969
    7070ANGLE_MTL_UNUSED
     
    231231}  // anonymous namespace
    232232
     233// IOSurfaceSurfaceMtl implementation.
    233234IOSurfaceSurfaceMtl::IOSurfaceSurfaceMtl(DisplayMtl *display,
    234235                                         const egl::SurfaceState &state,
    235236                                         EGLClientBuffer buffer,
    236237                                         const egl::AttributeMap &attribs)
    237     : SurfaceMtlProtocol(state)
    238 {
    239     // Keep reference to the IOSurface so it doesn't get deleted while the pbuffer exists.
    240     mIOSurface = reinterpret_cast<IOSurfaceRef>(buffer);
     238    : OffscreenSurfaceMtl(display, state, attribs), mIOSurface((__bridge IOSurfaceRef)(buffer))
     239{
    241240    CFRetain(mIOSurface);
    242241
    243     // Extract attribs useful for the call to EAGLTexImageIOSurface2D
    244     mWidth  = static_cast<int>(attribs.get(EGL_WIDTH));
    245     mHeight = static_cast<int>(attribs.get(EGL_HEIGHT));
    246     mPlane  = static_cast<int>(attribs.get(EGL_IOSURFACE_PLANE_ANGLE));
    247     // Hopefully the number of bytes per row is always an integer number of pixels. We use
    248     // glReadPixels to fill the IOSurface in the simulator and it can only support strides that are
    249     // an integer number of pixels.
    250     ASSERT(IOSurfaceGetBytesPerRowOfPlane(mIOSurface, mPlane) %
    251                IOSurfaceGetBytesPerElementOfPlane(mIOSurface, mPlane) ==
    252            0);
    253     mRowStrideInPixels = static_cast<int>(IOSurfaceGetBytesPerRowOfPlane(mIOSurface, mPlane) /
    254                                           IOSurfaceGetBytesPerElementOfPlane(mIOSurface, mPlane));
     242    mIOSurfacePlane = static_cast<int>(attribs.get(EGL_IOSURFACE_PLANE_ANGLE));
    255243
    256244    EGLAttrib internalFormat = attribs.get(EGL_TEXTURE_INTERNAL_FORMAT_ANGLE);
    257245    EGLAttrib type           = attribs.get(EGL_TEXTURE_TYPE_ANGLE);
    258     mFormatIndex =
     246    mIOSurfaceFormatIdx =
    259247        FindIOSurfaceFormatIndex(static_cast<GLenum>(internalFormat), static_cast<GLenum>(type));
    260     ASSERT(mFormatIndex >= 0);
    261     mFormat         = display->getPixelFormat(kIOSurfaceFormats[mFormatIndex].pixelFormat);
    262     mInternalFormat = display->getPixelFormat(kIOSurfaceFormats[mFormatIndex].internalPixelFormat);
    263     mGLInternalFormat = kIOSurfaceFormats[mFormatIndex].internalFormat;
    264 }
    265 
     248    ASSERT(mIOSurfaceFormatIdx >= 0);
     249
     250    mColorFormat =
     251        display->getPixelFormat(kIOSurfaceFormats[mIOSurfaceFormatIdx].nativeAngleFormatId);
     252}
    266253IOSurfaceSurfaceMtl::~IOSurfaceSurfaceMtl()
    267254{
    268     StopFrameCapture();
    269255    if (mIOSurface != nullptr)
    270256    {
     
    272258        mIOSurface = nullptr;
    273259    }
    274 }
    275 
    276 void IOSurfaceSurfaceMtl::destroy(const egl::Display *display) {}
    277 
    278 egl::Error IOSurfaceSurfaceMtl::initialize(const egl::Display *display)
    279 {
    280     DisplayMtl *displayMtl    = mtl::GetImpl(display);
    281     id<MTLDevice> metalDevice = displayMtl->getMetalDevice();
    282 
    283     StartFrameCapture(metalDevice, displayMtl->cmdQueue().get());
    284     return egl::NoError();
    285 }
    286 
    287 FramebufferImpl *IOSurfaceSurfaceMtl::createDefaultFramebuffer(const gl::Context *context,
    288                                                                const gl::FramebufferState &state)
    289 {
    290     auto fbo = new FramebufferMtl(state, /* flipY */ true, /* backbuffer */ this);
    291     return fbo;
    292 }
    293 
    294 egl::Error IOSurfaceSurfaceMtl::makeCurrent(const gl::Context *context)
    295 {
    296     ContextMtl *contextMtl = mtl::GetImpl(context);
    297     StartFrameCapture(contextMtl);
    298     return egl::NoError();
    299 }
    300 
    301 egl::Error IOSurfaceSurfaceMtl::unMakeCurrent(const gl::Context *context)
    302 {
    303     StopFrameCapture();
    304     return egl::NoError();
    305 }
    306 
    307 egl::Error IOSurfaceSurfaceMtl::swap(const gl::Context *context)
    308 {
    309     StopFrameCapture();
    310     ContextMtl *contextMtl = mtl::GetImpl(context);
    311     StartFrameCapture(contextMtl);
    312     return egl::NoError();
    313 }
    314 
    315 egl::Error IOSurfaceSurfaceMtl::postSubBuffer(const gl::Context *context,
    316                                               EGLint x,
    317                                               EGLint y,
    318                                               EGLint width,
    319                                               EGLint height)
    320 {
    321     UNIMPLEMENTED();
    322     return egl::EglBadAccess();
    323 }
    324 
    325 egl::Error IOSurfaceSurfaceMtl::querySurfacePointerANGLE(EGLint attribute, void **value)
    326 {
    327     UNIMPLEMENTED();
    328     return egl::EglBadAccess();
    329260}
    330261
     
    333264                                             EGLint buffer)
    334265{
    335 
    336     angle::Result res = createBackingTexture(context);
    337     if (res == angle::Result::Continue)
    338         return egl::NoError();
    339     else
    340         return egl::EglBadAccess();
     266    ContextMtl *contextMtl = mtl::GetImpl(context);
     267    StartFrameCapture(contextMtl);
     268
     269    // Initialize offscreen texture if needed:
     270    ANGLE_TO_EGL_TRY(ensureColorTextureCreated(context));
     271
     272    return OffscreenSurfaceMtl::bindTexImage(context, texture, buffer);
    341273}
    342274
    343275egl::Error IOSurfaceSurfaceMtl::releaseTexImage(const gl::Context *context, EGLint buffer)
    344276{
    345     // This will need implementation
    346     ContextMtl *contextMtl = mtl::GetImpl(context);
    347     if (contextMtl->flush(context) == angle::Result::Continue)
    348     {
    349         mIOSurfaceTextureCreated = false;
    350         return egl::NoError();
    351     }
    352     return egl::EglBadAccess();
    353 }
    354 
    355 egl::Error IOSurfaceSurfaceMtl::getSyncValues(EGLuint64KHR *ust,
    356                                               EGLuint64KHR *msc,
    357                                               EGLuint64KHR *sbc)
    358 {
    359     UNIMPLEMENTED();
    360     return egl::EglBadAccess();
    361 }
    362 
    363 egl::Error IOSurfaceSurfaceMtl::getMscRate(EGLint *numerator, EGLint *denominator)
    364 {
    365     UNIMPLEMENTED();
    366     return egl::EglBadAccess();
    367 }
    368 
    369 void IOSurfaceSurfaceMtl::setSwapInterval(EGLint interval)
    370 {
    371     UNIMPLEMENTED();
    372 }
    373 
    374 void IOSurfaceSurfaceMtl::setFixedWidth(EGLint width)
    375 {
    376     UNIMPLEMENTED();
    377 }
    378 
    379 void IOSurfaceSurfaceMtl::setFixedHeight(EGLint height)
    380 {
    381     UNIMPLEMENTED();
    382 }
    383 
    384 // width and height can change with client window resizing
    385 EGLint IOSurfaceSurfaceMtl::getWidth() const
    386 {
    387     return static_cast<EGLint>(mWidth);
    388 }
    389 
    390 EGLint IOSurfaceSurfaceMtl::getHeight() const
    391 {
    392     return static_cast<EGLint>(mHeight);
    393 }
    394 
    395 EGLint IOSurfaceSurfaceMtl::isPostSubBufferSupported() const
    396 {
    397     return EGL_FALSE;
    398 }
    399 
    400 EGLint IOSurfaceSurfaceMtl::getSwapBehavior() const
    401 {
    402     return EGL_BUFFER_DESTROYED;
     277    egl::Error re = OffscreenSurfaceMtl::releaseTexImage(context, buffer);
     278    StopFrameCapture();
     279    return re;
    403280}
    404281
     
    410287    FramebufferAttachmentRenderTarget **rtOut)
    411288{
    412     ANGLE_TRY(ensureTextureCreated(context));
    413     *rtOut = &mRenderTarget;
     289    // Initialize offscreen texture if needed:
     290    ANGLE_TRY(ensureColorTextureCreated(context));
     291
     292    return OffscreenSurfaceMtl::getAttachmentRenderTarget(context, binding, imageIndex, samples,
     293                                                          rtOut);
     294}
     295
     296angle::Result IOSurfaceSurfaceMtl::ensureColorTextureCreated(const gl::Context *context)
     297{
     298    if (mColorTexture)
     299    {
     300        return angle::Result::Continue;
     301    }
     302    ContextMtl *contextMtl = mtl::GetImpl(context);
     303    ANGLE_MTL_OBJC_SCOPE
     304    {
     305        auto texDesc =
     306            [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:mColorFormat.metalFormat
     307                                                               width:mSize.width
     308                                                              height:mSize.height
     309                                                           mipmapped:NO];
     310
     311        texDesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
     312
     313        id<MTLTexture> texture =
     314            [contextMtl->getMetalDevice() newTextureWithDescriptor:texDesc
     315                                                         iosurface:mIOSurface
     316                                                             plane:mIOSurfacePlane];
     317
     318        mColorTexture = mtl::Texture::MakeFromMetal([texture ANGLE_MTL_AUTORELEASE]);
     319    }
     320
     321    mColorRenderTarget.set(mColorTexture, mtl::kZeroNativeMipLevel, 0, mColorFormat);
     322
     323    if (kIOSurfaceFormats[mIOSurfaceFormatIdx].internalFormat == GL_RGB)
     324    {
     325        // This format has emulated alpha channel. Initialize texture's alpha channel to 1.0.
     326        const mtl::Format &rgbClearFormat =
     327            contextMtl->getPixelFormat(angle::FormatID::R8G8B8_UNORM);
     328        ANGLE_TRY(mtl::InitializeTextureContentsGPU(
     329            context, mColorTexture, rgbClearFormat,
     330            mtl::ImageNativeIndex::FromBaseZeroGLIndex(gl::ImageIndex::Make2D(0)),
     331            MTLColorWriteMaskAlpha));
     332
     333        // Disable subsequent rendering to alpha channel.
     334        mColorTexture->setColorWritableMask(MTLColorWriteMaskAll & (~MTLColorWriteMaskAlpha));
     335    }
     336
    414337    return angle::Result::Continue;
    415338}
    416339
    417 angle::Result IOSurfaceSurfaceMtl::ensureCurrentDrawableObtained(const gl::Context *context, bool *newDrawableOut)
    418 {
    419     return angle::Result::Continue;
    420 }
    421 angle::Result IOSurfaceSurfaceMtl::ensureCurrentDrawableObtained(const gl::Context *context)
    422 {
    423     return angle::Result::Continue;
    424 }
    425 
    426 angle::Result IOSurfaceSurfaceMtl::createBackingTexture(const gl::Context *context)
    427 {
    428     mtl::TextureRef mTemporarySurfaceRef;
    429     ANGLE_TRY(createTexture(context, mFormat, mWidth, mHeight, false, &mTemporarySurfaceRef));
    430     // Create texture view around the base format of the texture.
    431     mIOSurfaceTexture =
    432         mTemporarySurfaceRef->createViewWithDifferentFormat(MTLPixelFormatBGRA8Unorm);
    433     mRenderTarget.set(mIOSurfaceTexture, mtl::kZeroNativeMipLevel, 0, mInternalFormat);
    434 
    435     if (mGLInternalFormat == GL_RGB)
    436     {
    437         // Disable subsequent rendering to alpha channel.
    438         // TODO: Investigate if this allows the higher level alpha masks can
    439         // be disabled once this backend is live.
    440         mIOSurfaceTexture->setColorWritableMask(MTLColorWriteMaskAll & (~MTLColorWriteMaskAlpha));
    441     }
    442 
    443     mIOSurfaceTextureCreated = true;
    444     return angle::Result::Continue;
    445 }
    446 
    447 angle::Result IOSurfaceSurfaceMtl::ensureTextureCreated(const gl::Context *context)
    448 {
    449     if (!mIOSurfaceTextureCreated)
    450     {
    451         return createBackingTexture(context);
    452     }
    453     return angle::Result::Continue;
    454 }
    455 
    456 mtl::TextureRef IOSurfaceSurfaceMtl::getTexture(const gl::Context *context)
    457 {
    458     if (ensureCurrentDrawableObtained(context) == angle::Result::Continue)
    459     {
    460         return mIOSurfaceTexture;
    461     }
    462     return nullptr;
    463 }
    464 
    465 angle::Result IOSurfaceSurfaceMtl::createTexture(const gl::Context *context,
    466                                                  const mtl::Format &format,
    467                                                  uint32_t width,
    468                                                  uint32_t height,
    469                                                  bool renderTargetOnly,
    470                                                  mtl::TextureRef *textureOut)
    471 {
    472     ContextMtl *contextMtl = mtl::GetImpl(context);
    473     ANGLE_TRY(mtl::Texture::MakeIOSurfaceTexture(contextMtl, format, width, height, mIOSurface,
    474                                                  mPlane, textureOut));
    475     return angle::Result::Continue;
    476 }
    477 }
     340// static
     341bool IOSurfaceSurfaceMtl::ValidateAttributes(EGLClientBuffer buffer,
     342                                             const egl::AttributeMap &attribs)
     343{
     344    IOSurfaceRef ioSurface = (__bridge IOSurfaceRef)(buffer);
     345
     346    // The plane must exist for this IOSurface. IOSurfaceGetPlaneCount can return 0 for non-planar
     347    // ioSurfaces but we will treat non-planar like it is a single plane.
     348    size_t surfacePlaneCount = std::max(size_t(1), IOSurfaceGetPlaneCount(ioSurface));
     349    EGLAttrib plane          = attribs.get(EGL_IOSURFACE_PLANE_ANGLE);
     350    if (plane < 0 || static_cast<size_t>(plane) >= surfacePlaneCount)
     351    {
     352        return false;
     353    }
     354
     355    // The width height specified must be at least (1, 1) and at most the plane size
     356    EGLAttrib width  = attribs.get(EGL_WIDTH);
     357    EGLAttrib height = attribs.get(EGL_HEIGHT);
     358    if (width <= 0 || static_cast<size_t>(width) > IOSurfaceGetWidthOfPlane(ioSurface, plane) ||
     359        height <= 0 || static_cast<size_t>(height) > IOSurfaceGetHeightOfPlane(ioSurface, plane))
     360    {
     361        return false;
     362    }
     363
     364    // Find this IOSurface format
     365    EGLAttrib internalFormat = attribs.get(EGL_TEXTURE_INTERNAL_FORMAT_ANGLE);
     366    EGLAttrib type           = attribs.get(EGL_TEXTURE_TYPE_ANGLE);
     367
     368    int formatIndex =
     369        FindIOSurfaceFormatIndex(static_cast<GLenum>(internalFormat), static_cast<GLenum>(type));
     370
     371    if (formatIndex < 0)
     372    {
     373        return false;
     374    }
     375
     376    // Check that the format matches this IOSurface plane
     377    if (IOSurfaceGetBytesPerElementOfPlane(ioSurface, plane) !=
     378        kIOSurfaceFormats[formatIndex].componentBytes)
     379    {
     380        return false;
     381    }
     382
     383    return true;
     384}}
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/SurfaceMtl.h

    r270733 r273516  
    2525class DisplayMtl;
    2626
    27 class SurfaceMtlProtocol : public SurfaceImpl
    28 {
    29   public:
    30     SurfaceMtlProtocol(const egl::SurfaceState &state) : SurfaceImpl(state) {}
    31     virtual int getSamples() const      = 0;
    32     virtual bool preserveBuffer() const = 0;
    33 
    34     virtual angle::Result getAttachmentRenderTarget(const gl::Context *context,
    35                                                     GLenum binding,
    36                                                     const gl::ImageIndex &imageIndex,
    37                                                     GLsizei samples,
    38                                                     FramebufferAttachmentRenderTarget **rtOut) override = 0;
    39 
    40     virtual bool hasRobustResourceInit() const = 0;
    41     virtual angle::Result ensureCurrentDrawableObtained(const gl::Context *context) = 0;
    42     virtual angle::Result ensureCurrentDrawableObtained(const gl::Context *context, bool *newDrawableOut) = 0;
    43     virtual angle::Result ensureColorTextureReadyForReadPixels(const gl::Context *context) = 0;
    44     virtual const mtl::TextureRef & getColorTexture() = 0;
    45 };
    46 
    47 class SurfaceMtl : public SurfaceMtlProtocol
     27#define ANGLE_TO_EGL_TRY(EXPR)                                 \
     28    do                                                         \
     29    {                                                          \
     30        if (ANGLE_UNLIKELY((EXPR) != angle::Result::Continue)) \
     31        {                                                      \
     32            return egl::EglBadSurface();                       \
     33        }                                                      \
     34    } while (0)
     35
     36class SurfaceMtl : public SurfaceImpl
    4837{
    4938  public:
     
    8877                                     const gl::ImageIndex &imageIndex) override;
    8978
    90     const mtl::TextureRef &getColorTexture() override { return mColorTexture; }
     79    const mtl::TextureRef &getColorTexture() { return mColorTexture; }
    9180    const mtl::Format &getColorFormat() const { return mColorFormat; }
    92     int getSamples() const override { return mSamples; }
    93 
    94     bool hasRobustResourceInit() const override { return mRobustResourceInit; }
     81    int getSamples() const { return mSamples; }
     82
     83    bool hasRobustResourceInit() const { return mRobustResourceInit; }
    9584
    9685    angle::Result getAttachmentRenderTarget(const gl::Context *context,
     
    165154                                            FramebufferAttachmentRenderTarget **rtOut) override;
    166155
    167     angle::Result ensureCurrentDrawableObtained(const gl::Context *context) override;
     156    angle::Result ensureCurrentDrawableObtained(const gl::Context *context);
    168157    angle::Result ensureCurrentDrawableObtained(const gl::Context *context,
    169                                                 bool *newDrawableOut /** nullable */) override;
     158                                                bool *newDrawableOut /** nullable */);
    170159
    171160    // Ensure the the texture returned from getColorTexture() is ready for glReadPixels(). This
    172161    // implicitly calls ensureCurrentDrawableObtained().
    173     angle::Result ensureColorTextureReadyForReadPixels(const gl::Context *context) override;
    174     bool preserveBuffer() const override { return mRetainBuffer; }
     162    angle::Result ensureColorTextureReadyForReadPixels(const gl::Context *context);
     163    bool preserveBuffer() const { return mRetainBuffer; }
    175164  private:
    176165    angle::Result swapImpl(const gl::Context *context);
     
    194183};
    195184
    196 // Offscreen surface, base class of PBuffer, IOSurface.
     185// Offscreen surface, base class of PBuffer.
    197186class OffscreenSurfaceMtl : public SurfaceMtl
    198187{
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/SurfaceMtl.mm

    r270733 r273516  
    242242                       const egl::SurfaceState &state,
    243243                       const egl::AttributeMap &attribs)
    244     : SurfaceMtlProtocol(state)
     244    : SurfaceImpl(state)
    245245{
    246246    mRobustResourceInit =
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/TextureMtl.h

    r270733 r273516  
    316316
    317317    mtl::Format mFormat;
    318     SurfaceMtlProtocol *mBoundSurface = nil;
     318    SurfaceMtl *mBoundSurface = nil;
    319319    // The real texture used by Metal draw calls.
    320320    mtl::TextureRef mNativeTexture         = nil;
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/TextureMtl.mm

    r270733 r273516  
    2121#include "libANGLE/renderer/metal/DisplayMtl.h"
    2222#include "libANGLE/renderer/metal/FrameBufferMtl.h"
    23 #include "libANGLE/renderer/metal/IOSurfaceSurfaceMtl.h"
    2423#include "libANGLE/renderer/metal/SamplerMtl.h"
     24#include "libANGLE/renderer/metal/SurfaceMtl.h"
    2525#include "libANGLE/renderer/metal/mtl_common.h"
    2626#include "libANGLE/renderer/metal/mtl_format_utils.h"
     
    535535angle::Result TextureMtl::ensureTextureCreated(const gl::Context *context)
    536536{
    537     if (mBoundSurface)
    538     {
    539         return angle::Result::Continue;
    540     }
    541537    if (mNativeTexture)
    542538    {
     
    669665        samplerDesc.maxAnisotropy = 1;
    670666    }
    671 
     667    if(mState.getType() == gl::TextureType::Rectangle)
     668    {
     669        samplerDesc.normalizedCoordinates = NO;
     670    }
     671    else
     672    {
     673        samplerDesc.normalizedCoordinates = YES;
     674    }
    672675    mMetalSamplerState =
    673676        displayMtl->getStateCache().getSamplerState(displayMtl->getMetalDevice(), samplerDesc);
     
    12121215angle::Result TextureMtl::bindTexImage(const gl::Context *context, egl::Surface *surface)
    12131216{
    1214     IOSurfaceSurfaceMtl *surfaceMtl = (IOSurfaceSurfaceMtl *)mtl::GetImpl(surface);
    1215     mBoundSurface                   = surfaceMtl;
     1217    releaseTexture(true);
     1218
     1219    auto pBuffer     = GetImplAs<OffscreenSurfaceMtl>(surface);
     1220    mNativeTexture   = pBuffer->getColorTexture();
     1221    mFormat          = pBuffer->getColorFormat();
     1222    gl::Extents size = mNativeTexture->sizeAt0();
     1223    mIsPow2          = gl::isPow2(size.width) && gl::isPow2(size.height) && gl::isPow2(size.depth);
     1224    ANGLE_TRY(ensureSamplerStateCreated(context));
     1225
     1226    // Tell context to rebind textures
     1227    ContextMtl *contextMtl = mtl::GetImpl(context);
     1228    contextMtl->invalidateCurrentTextures();
     1229
    12161230    return angle::Result::Continue;
    12171231}
     
    12191233angle::Result TextureMtl::releaseTexImage(const gl::Context *context)
    12201234{
    1221     mBoundSurface = nil;
    1222 
     1235    releaseTexture(true);
    12231236    return angle::Result::Continue;
    12241237}
     
    12301243                                                    FramebufferAttachmentRenderTarget **rtOut)
    12311244{
    1232     if (mBoundSurface)
    1233     {
    1234         ANGLE_TRY(
    1235             mBoundSurface->getAttachmentRenderTarget(context, binding, imageIndex, samples, rtOut));
    1236         return angle::Result::Continue;
    1237     }
    12381245    ANGLE_TRY(ensureTextureCreated(context));
    12391246
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/mtl_common.h

    r271334 r273516  
    114114class SamplerMtl;
    115115class TransformFeedbackMtl;
    116 class SurfaceMtlProtocol;
    117116
    118117ANGLE_GL_OBJECTS_X(ANGLE_PRE_DECLARE_MTL_OBJECT)
     
    170169{
    171170    using ImplType = DisplayMtl;
    172 };
    173 template <>
    174 struct ImplTypeHelper<egl::Surface>
    175 {
    176     using ImplType = SurfaceMtlProtocol;
    177171};
    178172template <typename T>
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/mtl_state_cache.h

    r270733 r273516  
    114114    uint8_t magFilter : 1;
    115115    uint8_t mipFilter : 2;
     116    uint8_t normalizedCoordinates: 1;
    116117
    117118    uint8_t maxAnisotropy : 5;
  • trunk/Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/mtl_state_cache.mm

    r270733 r273516  
    7777    ANGLE_OBJC_CP_PROPERTY(objCDesc, desc, maxAnisotropy);
    7878    ANGLE_OBJC_CP_PROPERTY(objCDesc, desc, compareFunction);
     79    ANGLE_OBJC_CP_PROPERTY(objCDesc, desc, normalizedCoordinates);
    7980
    8081    return [objCDesc ANGLE_MTL_AUTORELEASE];
     
    464465
    465466    compareFunction = GetCompareFunc(glState.getCompareFunc());
     467   
     468    normalizedCoordinates = YES;
    466469}
    467470
     
    481484    magFilter = MTLSamplerMinMagFilterNearest;
    482485    mipFilter = MTLSamplerMipFilterNearest;
    483 
     486   
    484487    maxAnisotropy = 1;
    485488
    486489    compareFunction = MTLCompareFunctionNever;
     490   
     491    normalizedCoordinates = YES;
    487492}
    488493
Note: See TracChangeset for help on using the changeset viewer.