Skip to content

Commit

Permalink
Support external semaphore imported from MTLSharedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
kakashidinho committed Jul 13, 2021
1 parent 90c66ff commit eb455ff
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 95 deletions.
5 changes: 5 additions & 0 deletions include/GLES2/gl2ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -3612,6 +3612,11 @@ GL_APICALL void GL_APIENTRY glEndTilingQCOM (GLbitfield preserveMask);
#define GL_SHADER_BINARY_VIV 0x8FC4
#endif /* GL_VIV_shader_binary */

#ifndef GL_MGL_timeline_semaphore
#define GL_MGL_timeline_semaphore 1
#define GL_TIMELINE_SEMAPHORE_VALUE_MGL 0x9595
#endif

/* ANGLE GLES2 extensions */
#include "gl2ext_angle.h"

Expand Down
20 changes: 18 additions & 2 deletions ios/xcode/OpenGLES.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 52;
objects = {

/* Begin PBXAggregateTarget section */
Expand Down Expand Up @@ -2288,6 +2288,10 @@
0AC8B2F0241E9761003916D1 /* QueryMtl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0AC8B2EB241E9761003916D1 /* QueryMtl.mm */; };
0AC8B2F1241E9761003916D1 /* QueryMtl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0AC8B2EB241E9761003916D1 /* QueryMtl.mm */; };
0AC8B2F2241E9761003916D1 /* QueryMtl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0AC8B2EB241E9761003916D1 /* QueryMtl.mm */; };
0AE8E090269F6E9800070E82 /* SemaphoreMtl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0AE8E067269F6E9600070E82 /* SemaphoreMtl.mm */; };
0AE8E091269F6E9800070E82 /* SemaphoreMtl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0AE8E067269F6E9600070E82 /* SemaphoreMtl.mm */; };
0AE8E092269F6E9800070E82 /* SemaphoreMtl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0AE8E067269F6E9600070E82 /* SemaphoreMtl.mm */; };
0AE8E093269F6E9800070E82 /* SemaphoreMtl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0AE8E067269F6E9600070E82 /* SemaphoreMtl.mm */; };
0AF956AD244C7BF300F59740 /* json_reader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A9B83F6234CD0C2008BF16F /* json_reader.cpp */; };
0AF956AE244C7BF300F59740 /* json_value.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A9B83F7234CD0C2008BF16F /* json_value.cpp */; };
0AF956AF244C7BF300F59740 /* json_writer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0AA2FA9C2346F86700E0B98C /* json_writer.cpp */; };
Expand Down Expand Up @@ -5044,6 +5048,8 @@
0AC8B2EA241E9761003916D1 /* mtl_occlusion_query_pool.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = mtl_occlusion_query_pool.mm; path = ../../src/libANGLE/renderer/metal/mtl_occlusion_query_pool.mm; sourceTree = "<group>"; };
0AC8B2EB241E9761003916D1 /* QueryMtl.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = QueryMtl.mm; path = ../../src/libANGLE/renderer/metal/QueryMtl.mm; sourceTree = "<group>"; };
0AC8B2EC241E9761003916D1 /* mtl_occlusion_query_pool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = mtl_occlusion_query_pool.h; path = ../../src/libANGLE/renderer/metal/mtl_occlusion_query_pool.h; sourceTree = "<group>"; };
0AE8E067269F6E9600070E82 /* SemaphoreMtl.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = SemaphoreMtl.mm; path = ../../src/libANGLE/renderer/metal/SemaphoreMtl.mm; sourceTree = "<group>"; };
0AE8E068269F6E9600070E82 /* SemaphoreMtl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SemaphoreMtl.h; path = ../../src/libANGLE/renderer/metal/SemaphoreMtl.h; sourceTree = "<group>"; };
0AF956B5244C7BF300F59740 /* libjsoncpp_tvos.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libjsoncpp_tvos.a; sourceTree = BUILT_PRODUCTS_DIR; };
0AF956C6244C7C5100F59740 /* libspirv-cross_tvos.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libspirv-cross_tvos.a"; sourceTree = BUILT_PRODUCTS_DIR; };
0AF956F8244C7C8700F59740 /* libglslang_tvos.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libglslang_tvos.a; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -6114,6 +6120,9 @@
0A6055CE234655FB005CEA98 /* RenderTargetMtl.mm */,
0A9E0A7A242D060000688588 /* SamplerMtl.h */,
0A9E0A7B242D060000688588 /* SamplerMtl.mm */,
0AE8E068269F6E9600070E82 /* SemaphoreMtl.h */,
0AE8E067269F6E9600070E82 /* SemaphoreMtl.mm */,
0A9A6AC423911CAD006A152A /* RendererGL.cpp */,
0A605604234655FE005CEA98 /* ShaderMtl.h */,
0A6055E5234655FD005CEA98 /* ShaderMtl.mm */,
0A0546CD242C1DD200FC05D0 /* TransformFeedbackMtl.h */,
Expand Down Expand Up @@ -6629,7 +6638,6 @@
0A9A6B5823911CB3006A152A /* RenderbufferGL.h */,
0A9A6ABE23911CAD006A152A /* renderergl_utils.cpp */,
0A9A6B0823911CB1006A152A /* renderergl_utils.h */,
0A9A6AC423911CAD006A152A /* RendererGL.cpp */,
0A9A6B4623911CB3006A152A /* RendererGL.h */,
0A9A6AF623911CB0006A152A /* SamplerGL.cpp */,
0A9A6B2023911CB2006A152A /* SamplerGL.h */,
Expand Down Expand Up @@ -10280,6 +10288,7 @@
0A60565123465667005CEA98 /* mtl_format_utils.mm in Sources */,
0A60564923465667005CEA98 /* ContextMtl.mm in Sources */,
0A60564C23465667005CEA98 /* mtl_glslang_utils.mm in Sources */,
0AE8E090269F6E9800070E82 /* SemaphoreMtl.mm in Sources */,
0A60564823465667005CEA98 /* CompilerMtl.mm in Sources */,
0AFFDCB624D42F7F0015E16A /* ImageMtl.mm in Sources */,
0A60565C23465667005CEA98 /* TextureMtl.mm in Sources */,
Expand Down Expand Up @@ -10507,6 +10516,7 @@
0A90F76624065B06005BA9A8 /* mtl_format_utils.mm in Sources */,
0A90F76724065B06005BA9A8 /* ContextMtl.mm in Sources */,
0A90F76824065B06005BA9A8 /* mtl_glslang_utils.mm in Sources */,
0AE8E091269F6E9800070E82 /* SemaphoreMtl.mm in Sources */,
0A90F76924065B06005BA9A8 /* CompilerMtl.mm in Sources */,
0AFFDCB724D42F820015E16A /* ImageMtl.mm in Sources */,
0A90F76A24065B06005BA9A8 /* TextureMtl.mm in Sources */,
Expand Down Expand Up @@ -11668,6 +11678,7 @@
0AA2FFBD234727A400E0B98C /* mtl_format_utils.mm in Sources */,
0AA2FFBE234727A400E0B98C /* ContextMtl.mm in Sources */,
0AA2FFBF234727A400E0B98C /* mtl_glslang_utils.mm in Sources */,
0AE8E093269F6E9800070E82 /* SemaphoreMtl.mm in Sources */,
0AA2FFC0234727A400E0B98C /* CompilerMtl.mm in Sources */,
0AFFDCB924D42F880015E16A /* ImageMtl.mm in Sources */,
0AA2FFC1234727A400E0B98C /* TextureMtl.mm in Sources */,
Expand Down Expand Up @@ -11861,6 +11872,7 @@
0AF9573F244C7CC300F59740 /* mtl_format_utils.mm in Sources */,
0AF95740244C7CC300F59740 /* ContextMtl.mm in Sources */,
0AF95741244C7CC300F59740 /* mtl_glslang_utils.mm in Sources */,
0AE8E092269F6E9800070E82 /* SemaphoreMtl.mm in Sources */,
0AF95742244C7CC300F59740 /* CompilerMtl.mm in Sources */,
0AFFDCB824D42F860015E16A /* ImageMtl.mm in Sources */,
0AF95743244C7CC300F59740 /* TextureMtl.mm in Sources */,
Expand Down Expand Up @@ -12566,6 +12578,8 @@
OTHER_CFLAGS = "-Wno-shorten-64-to-32";
SDKROOT = iphoneos;
TVOS_DEPLOYMENT_TARGET = 9.0;
USER_HEADER_SEARCH_PATHS = Yes;
USE_HEADERMAP = NO;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down Expand Up @@ -12652,6 +12666,8 @@
OTHER_CFLAGS = "-Wno-shorten-64-to-32";
SDKROOT = iphoneos;
TVOS_DEPLOYMENT_TARGET = 9.0;
USER_HEADER_SEARCH_PATHS = Yes;
USE_HEADERMAP = NO;
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down
1 change: 1 addition & 0 deletions src/libANGLE/Caps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ const ExtensionInfoMap &GetExtensionInfoMap()
map["GL_ANGLE_texture_external_update"] = enableableExtension(&Extensions::textureExternalUpdateANGLE);
map["GL_ANGLE_base_vertex_base_instance"] = enableableExtension(&Extensions::baseVertexBaseInstance);
map["GL_APPLE_clip_distance"] = enableableExtension(&Extensions::clipDistanceAPPLE);
map["GL_MGL_timeline_semaphore"] = enableableExtension(&Extensions::timelineSemaphoreMGL);
// GLES1 extensinos
map["GL_OES_point_size_array"] = enableableExtension(&Extensions::pointSizeArray);
map["GL_OES_texture_cube_map"] = enableableExtension(&Extensions::textureCubeMap);
Expand Down
3 changes: 3 additions & 0 deletions src/libANGLE/Caps.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ struct Extensions

// GL_APPLE_clip_distance
bool clipDistanceAPPLE = false;

// GL_MGL_timeline_semaphore
bool timelineSemaphoreMGL = false;
};

struct ExtensionInfo
Expand Down
18 changes: 14 additions & 4 deletions src/libANGLE/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7861,14 +7861,24 @@ GLboolean Context::isSemaphore(SemaphoreID semaphore)
return ConvertToGLBoolean(getSemaphore(semaphore));
}

void Context::semaphoreParameterui64v(SemaphoreID semaphore, GLenum pname, const GLuint64 *params)
void Context::semaphoreParameterui64v(SemaphoreID semaphoreHandle,
GLenum pname,
const GLuint64 *params)
{
UNIMPLEMENTED();
Semaphore *semaphore = getSemaphore(semaphoreHandle);
ASSERT(semaphore);

semaphore->parameterui64v(pname, params);
}

void Context::getSemaphoreParameterui64v(SemaphoreID semaphore, GLenum pname, GLuint64 *params)
void Context::getSemaphoreParameterui64v(SemaphoreID semaphoreHandle,
GLenum pname,
GLuint64 *params)
{
UNIMPLEMENTED();
Semaphore *semaphore = getSemaphore(semaphoreHandle);
ASSERT(semaphore);

semaphore->getParameterui64v(pname, params);
}

void Context::waitSemaphore(SemaphoreID semaphoreHandle,
Expand Down
9 changes: 9 additions & 0 deletions src/libANGLE/Semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ angle::Result Semaphore::signal(Context *context,
return mImplementation->signal(context, bufferBarriers, textureBarriers);
}

void Semaphore::parameterui64v(GLenum pname, const GLuint64 *params)
{
mImplementation->parameterui64v(pname, params);
}
void Semaphore::getParameterui64v(GLenum pname, GLuint64 *params)
{
mImplementation->getParameterui64v(pname, params);
}

} // namespace gl
3 changes: 3 additions & 0 deletions src/libANGLE/Semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Semaphore final : public RefCountObject<SemaphoreID>
const BufferBarrierVector &bufferBarriers,
const TextureBarrierVector &textureBarriers);

void parameterui64v(GLenum pname, const GLuint64 *params);
void getParameterui64v(GLenum pname, GLuint64 *params);

private:
std::unique_ptr<rx::SemaphoreImpl> mImplementation;
};
Expand Down
3 changes: 3 additions & 0 deletions src/libANGLE/renderer/SemaphoreImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class SemaphoreImpl : angle::NonCopyable
virtual angle::Result signal(gl::Context *context,
const gl::BufferBarrierVector &bufferBarriers,
const gl::TextureBarrierVector &textureBarriers) = 0;

virtual void parameterui64v(GLenum pname, const GLuint64 *params) {}
virtual void getParameterui64v(GLenum pname, GLuint64 *params) {}
};

} // namespace rx
Expand Down
2 changes: 2 additions & 0 deletions src/libANGLE/renderer/metal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ _metal_backend_sources = [
"RenderTargetMtl.mm",
"SamplerMtl.h",
"SamplerMtl.mm",
"SemaphoreMtl.h",
"SemaphoreMtl.mm",
"ShaderMtl.h",
"ShaderMtl.mm",
"SyncMtl.h",
Expand Down
4 changes: 2 additions & 2 deletions src/libANGLE/renderer/metal/ContextMtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "libANGLE/renderer/metal/QueryMtl.h"
#include "libANGLE/renderer/metal/RenderBufferMtl.h"
#include "libANGLE/renderer/metal/SamplerMtl.h"
#include "libANGLE/renderer/metal/SemaphoreMtl.h"
#include "libANGLE/renderer/metal/ShaderMtl.h"
#include "libANGLE/renderer/metal/SyncMtl.h"
#include "libANGLE/renderer/metal/TextureMtl.h"
Expand Down Expand Up @@ -1182,8 +1183,7 @@ bool IsTransformFeedbackOnly(const gl::State &glState)
// Semaphore creation.
SemaphoreImpl *ContextMtl::createSemaphore()
{
UNIMPLEMENTED();
return nullptr;
return new SemaphoreMtl();
}

OverlayImpl *ContextMtl::createOverlay(const gl::OverlayState &state)
Expand Down
9 changes: 9 additions & 0 deletions src/libANGLE/renderer/metal/DisplayMtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,15 @@ void generateExtensions(egl::DeviceExtensions *outExtensions) const override

// GL_OES_EGL_sync
mNativeExtensions.eglSync = true;

// GL_EXT_semaphore
mNativeExtensions.semaphore = true;

// GL_EXT_semaphore_fd
mNativeExtensions.semaphoreFd = true;

// GL_MGL_timeline_semaphore
mNativeExtensions.timelineSemaphoreMGL = true;
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/libANGLE/renderer/metal/SemaphoreMtl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021 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.
//
// SemaphoreMtl.h: Defines the class interface for SemaphoreMtl,
// implementing SemaphoreImpl.

#ifndef LIBANGLE_RENDERER_METAL_SEMAPHOREMTL_H_
#define LIBANGLE_RENDERER_METAL_SEMAPHOREMTL_H_

#include "libANGLE/renderer/SemaphoreImpl.h"
#include "libANGLE/renderer/metal/mtl_common.h"

namespace rx
{

class SemaphoreMtl : public SemaphoreImpl
{
public:
SemaphoreMtl();
~SemaphoreMtl() override;

void onDestroy(const gl::Context *context) override;

angle::Result importFd(gl::Context *context, gl::HandleType handleType, GLint fd) override;

angle::Result wait(gl::Context *context,
const gl::BufferBarrierVector &bufferBarriers,
const gl::TextureBarrierVector &textureBarriers) override;

angle::Result signal(gl::Context *context,
const gl::BufferBarrierVector &bufferBarriers,
const gl::TextureBarrierVector &textureBarriers) override;

void parameterui64v(GLenum pname, const GLuint64 *params) override;
void getParameterui64v(GLenum pname, GLuint64 *params) override;
private:
angle::Result importOpaqueFd(gl::Context *context, GLint fd);

mtl::SharedEventRef mMetalSharedEvent;
GLuint64 mTimelineValue = 0;
};

} // namespace rx

#endif // LIBANGLE_RENDERER_METAL_SEMAPHOREMTL_H_
114 changes: 114 additions & 0 deletions src/libANGLE/renderer/metal/SemaphoreMtl.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2021 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.
//
// SemaphoreMtl.cpp: Defines the class interface for SemaphoreMtl, implementing
// SemaphoreImpl.

#include "libANGLE/renderer/metal/SemaphoreMtl.h"

#include "common/debug.h"
#include "libANGLE/Context.h"
#include "libANGLE/renderer/metal/ContextMtl.h"

namespace rx
{

SemaphoreMtl::SemaphoreMtl() = default;

SemaphoreMtl::~SemaphoreMtl() = default;

void SemaphoreMtl::onDestroy(const gl::Context *context)
{
mMetalSharedEvent = nil;
}

angle::Result SemaphoreMtl::importFd(gl::Context *context, gl::HandleType handleType, GLint fd)
{
switch (handleType)
{
case gl::HandleType::OpaqueFd:
return importOpaqueFd(context, fd);

default:
UNREACHABLE();
return angle::Result::Stop;
}
}

angle::Result SemaphoreMtl::wait(gl::Context *context,
const gl::BufferBarrierVector &bufferBarriers,
const gl::TextureBarrierVector &textureBarriers)
{
// bufferBarriers & textureBarriers are unused for now because metal always end
// current render pass when there is a wait command.

#if ANGLE_MTL_EVENT_AVAILABLE
ContextMtl *contextMtl = mtl::GetImpl(context);
contextMtl->serverWaitEvent(mMetalSharedEvent, mTimelineValue);
#endif // ANGLE_MTL_EVENT_AVAILABLE

return angle::Result::Continue;
}

angle::Result SemaphoreMtl::signal(gl::Context *context,
const gl::BufferBarrierVector &bufferBarriers,
const gl::TextureBarrierVector &textureBarriers)
{
#if ANGLE_MTL_EVENT_AVAILABLE
ContextMtl *contextMtl = mtl::GetImpl(context);
contextMtl->queueEventSignal(mMetalSharedEvent, mTimelineValue);
#endif // ANGLE_MTL_EVENT_AVAILABLE

contextMtl->flushCommandBufer();
return angle::Result::Continue;
}

void SemaphoreMtl::parameterui64v(GLenum pname, const GLuint64 *params)
{
switch (pname)
{
case GL_TIMELINE_SEMAPHORE_VALUE_MGL:
mTimelineValue = *params;
break;
default:
UNREACHABLE();
}
}

void SemaphoreMtl::getParameterui64v(GLenum pname, GLuint64 *params)
{
switch (pname)
{
case GL_TIMELINE_SEMAPHORE_VALUE_MGL:
*params = mTimelineValue;
break;
default:
UNREACHABLE();
}
}

angle::Result SemaphoreMtl::importOpaqueFd(gl::Context *context, GLint fd)
{
#if !ANGLE_MTL_EVENT_AVAILABLE
UNREACHABLE();
#else
ContextMtl *contextMtl = mtl::GetImpl(context);

// NOTE(hqle): This import assumes the address of MTLSharedEvent is stored in the file.
void* sharedEventPtr = nullptr;
ANGLE_MTL_TRY(contextMtl, read(fd, &sharedEventPtr, sizeof(sharedEventPtr)));
ANGLE_MTL_TRY(contextMtl, sharedEventPtr);

// The ownership of this fd belongs to Semaphore now, so we can close it here.
close(fd);

auto sharedEvent = (__bridge id<MTLSharedEvent>)sharedEventPtr;

mMetalSharedEvent = std::move(sharedEvent);
#endif // ANGLE_MTL_EVENT_AVAILABLE

return angle::Result::Continue;
}

} // namespace rx
Loading

0 comments on commit eb455ff

Please sign in to comment.