Hash :
f2aa9d5d
Author :
Date :
2021-04-11T14:55:19
Reland: Metal: Support importing external metal textures This relands I4d4a88cfbb77d8b7508b787c7fec44073d3b11b0. Fixes: - uninstantiated ImageTestMetal error - failed ImageTest.ANGLEExtensionAvailability on ARM mac. Bug: angleproject:5763 Bug: angleproject:5814 Change-Id: I906fe52baefd6be3c6e00f594795bd527df01616 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2820178 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Le Hoang Quyen <le.hoang.q@gmail.com>
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
//
// 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.
//
// ImageTestMetal:
// Tests the correctness of eglImage with native Metal texture extensions.
//
#include "test_utils/ANGLETest.h"
#include "common/mathutil.h"
#include "test_utils/gl_raii.h"
#include "util/EGLWindow.h"
#include <CoreFoundation/CoreFoundation.h>
#include <Metal/Metal.h>
namespace angle
{
namespace
{
constexpr char kOESExt[] = "GL_OES_EGL_image";
constexpr char kBaseExt[] = "EGL_KHR_image_base";
constexpr char kDeviceMtlExt[] = "EGL_ANGLE_device_metal";
constexpr char kEGLMtlImageNativeTextureExt[] = "EGL_ANGLE_metal_texture_client_buffer";
constexpr EGLint kDefaultAttribs[] = {
EGL_NONE,
};
} // anonymous namespace
class ScopeMetalTextureRef : angle::NonCopyable
{
public:
explicit ScopeMetalTextureRef(id<MTLTexture> surface) : mSurface(surface) {}
~ScopeMetalTextureRef()
{
if (mSurface)
{
release();
mSurface = nullptr;
}
}
id<MTLTexture> get() const { return mSurface; }
// auto cast to MTLTexture
operator id<MTLTexture>() const { return mSurface; }
ScopeMetalTextureRef(const ScopeMetalTextureRef &other)
{
if (mSurface)
{
release();
}
mSurface = other.mSurface;
}
explicit ScopeMetalTextureRef(ScopeMetalTextureRef &&other)
{
if (mSurface)
{
release();
}
mSurface = other.mSurface;
other.mSurface = nil;
}
ScopeMetalTextureRef &operator=(ScopeMetalTextureRef &&other)
{
if (mSurface)
{
release();
}
mSurface = other.mSurface;
other.mSurface = nil;
return *this;
}
ScopeMetalTextureRef &operator=(const ScopeMetalTextureRef &other)
{
if (mSurface)
{
release();
}
mSurface = other.mSurface;
return *this;
}
private:
void release()
{
#if !__has_feature(objc_arc)
[mSurface release];
#endif
}
id<MTLTexture> mSurface = nil;
};
ScopeMetalTextureRef CreateMetalTexture2D(id<MTLDevice> deviceMtl,
int width,
int height,
MTLPixelFormat format)
{
@autoreleasepool
{
MTLTextureDescriptor *desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format
width:width
height:width
mipmapped:NO];
desc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
id<MTLTexture> texture = [deviceMtl newTextureWithDescriptor:desc];
ScopeMetalTextureRef re(texture);
return re;
}
}
class ImageTestMetal : public ANGLETest
{
protected:
ImageTestMetal()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
setConfigDepthBits(24);
}
void testSetUp() override
{
constexpr char kVS[] = "precision highp float;\n"
"attribute vec4 position;\n"
"varying vec2 texcoord;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
" texcoord.y = 1.0 - texcoord.y;\n"
"}\n";
constexpr char kTextureFS[] = "precision highp float;\n"
"uniform sampler2D tex;\n"
"varying vec2 texcoord;\n"
"\n"
"void main()\n"
"{\n"
" gl_FragColor = texture2D(tex, texcoord);\n"
"}\n";
mTextureProgram = CompileProgram(kVS, kTextureFS);
if (mTextureProgram == 0)
{
FAIL() << "shader compilation failed.";
}
mTextureUniformLocation = glGetUniformLocation(mTextureProgram, "tex");
ASSERT_GL_NO_ERROR();
}
void testTearDown() override { glDeleteProgram(mTextureProgram); }
id<MTLDevice> getMtlDevice()
{
EGLAttrib angleDevice = 0;
EGLAttrib device = 0;
EXPECT_EGL_TRUE(
eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
EXPECT_EGL_TRUE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
EGL_METAL_DEVICE_ANGLE, &device));
return (__bridge id<MTLDevice>)reinterpret_cast<void *>(device);
}
ScopeMetalTextureRef createMtlTexture2D(int width, int height, MTLPixelFormat format)
{
id<MTLDevice> device = getMtlDevice();
return CreateMetalTexture2D(device, width, height, format);
}
void sourceMetalTarget2D_helper(GLubyte data[4],
const EGLint *attribs,
EGLImageKHR *imageOut,
GLuint *textureOut);
void verifyResultsTexture(GLuint texture,
GLubyte data[4],
GLenum textureTarget,
GLuint program,
GLuint textureUniform)
{
// Draw a quad with the target texture
glUseProgram(program);
glBindTexture(textureTarget, texture);
glUniform1i(textureUniform, 0);
drawQuad(program, "position", 0.5f);
// Expect that the rendered quad has the same color as the source texture
EXPECT_PIXEL_NEAR(0, 0, data[0], data[1], data[2], data[3], 1.0);
}
void verifyResults2D(GLuint texture, GLubyte data[4])
{
verifyResultsTexture(texture, data, GL_TEXTURE_2D, mTextureProgram,
mTextureUniformLocation);
}
template <typename destType, typename sourcetype>
destType reinterpretHelper(sourcetype source)
{
static_assert(sizeof(destType) == sizeof(size_t),
"destType should be the same size as a size_t");
size_t sourceSizeT = static_cast<size_t>(source);
return reinterpret_cast<destType>(sourceSizeT);
}
bool hasImageNativeMetalTextureExt() const
{
if (!IsMetal())
{
return false;
}
EGLAttrib angleDevice = 0;
eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice);
if (!angleDevice)
{
return false;
}
auto extensionString = static_cast<const char *>(
eglQueryDeviceStringEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice), EGL_EXTENSIONS));
if (strstr(extensionString, kDeviceMtlExt) == nullptr)
{
return false;
}
return IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(),
kEGLMtlImageNativeTextureExt);
}
bool hasOESExt() const { return IsGLExtensionEnabled(kOESExt); }
bool hasBaseExt() const
{
return IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(), kBaseExt);
}
GLuint mTextureProgram;
GLint mTextureUniformLocation;
};
void ImageTestMetal::sourceMetalTarget2D_helper(GLubyte data[4],
const EGLint *attribs,
EGLImageKHR *imageOut,
GLuint *textureOut)
{
EGLWindow *window = getEGLWindow();
// Create MTLTexture
ScopeMetalTextureRef textureMtl = createMtlTexture2D(1, 1, MTLPixelFormatRGBA8Unorm);
// Create image
EGLImageKHR image =
eglCreateImageKHR(window->getDisplay(), EGL_NO_CONTEXT, EGL_METAL_TEXTURE_ANGLE,
reinterpret_cast<EGLClientBuffer>(textureMtl.get()), attribs);
ASSERT_EGL_SUCCESS();
// Write the data to the MTLTexture
[textureMtl.get() replaceRegion:MTLRegionMake2D(0, 0, 1, 1)
mipmapLevel:0
slice:0
withBytes:data
bytesPerRow:4
bytesPerImage:0];
// Create a texture target to bind the egl image
GLuint target;
glGenTextures(1, &target);
glBindTexture(GL_TEXTURE_2D, target);
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
*imageOut = image;
*textureOut = target;
}
// Testing source metal EGL image, target 2D texture
TEST_P(ImageTestMetal, SourceMetalTarget2D)
{
ANGLE_SKIP_TEST_IF(!hasOESExt() || !hasBaseExt());
ANGLE_SKIP_TEST_IF(!hasImageNativeMetalTextureExt());
EGLWindow *window = getEGLWindow();
// Create the Image
EGLImageKHR image;
GLuint texTarget;
GLubyte data[4] = {7, 51, 197, 231};
sourceMetalTarget2D_helper(data, kDefaultAttribs, &image, &texTarget);
// Use texture target bound to egl image as source and render to framebuffer
// Verify that data in framebuffer matches that in the egl image
verifyResults2D(texTarget, data);
// Clean up
eglDestroyImageKHR(window->getDisplay(), image);
glDeleteTextures(1, &texTarget);
}
// Create source metal EGL image, target 2D texture, then trigger texture respecification.
TEST_P(ImageTestMetal, SourceMetal2DTargetTextureRespecifySize)
{
ANGLE_SKIP_TEST_IF(!hasOESExt() || !hasBaseExt());
ANGLE_SKIP_TEST_IF(!hasImageNativeMetalTextureExt());
EGLWindow *window = getEGLWindow();
// Create the Image
EGLImageKHR image;
GLuint texTarget;
GLubyte data[4] = {7, 51, 197, 231};
sourceMetalTarget2D_helper(data, kDefaultAttribs, &image, &texTarget);
// Use texture target bound to egl image as source and render to framebuffer
// Verify that data in framebuffer matches that in the egl image
verifyResults2D(texTarget, data);
// Respecify texture size and verify results
std::array<GLubyte, 16> referenceColor;
referenceColor.fill(127);
glBindTexture(GL_TEXTURE_2D, texTarget);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
referenceColor.data());
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
referenceColor.data());
ASSERT_GL_NO_ERROR();
// Expect that the target texture has the reference color values
verifyResults2D(texTarget, referenceColor.data());
// Clean up
eglDestroyImageKHR(window->getDisplay(), image);
glDeleteTextures(1, &texTarget);
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these
// tests should be run against.
ANGLE_INSTANTIATE_TEST(ImageTestMetal, ES2_METAL(), ES3_METAL());
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ImageTestMetal);
} // namespace angle