Path: blob/main_old/src/tests/gl_tests/ExternalWrapTest.cpp
1693 views
//1// Copyright 2020 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// ExternalWrapTest:6// Tests EXT_EGL_image_external_wrap_modes7//89#include "test_utils/ANGLETest.h"10#include "test_utils/gl_raii.h"11#include "util/EGLWindow.h"1213constexpr int kPixelThreshold = 1;1415namespace angle16{17class ExternalWrapTest : public ANGLETest18{19protected:20ExternalWrapTest() : mProgram(0), mSourceTexture(0), mExternalImage(0), mExternalTexture(0)21{22setWindowWidth(128);23setWindowHeight(128);24setConfigRedBits(8);25setConfigGreenBits(8);26setConfigBlueBits(8);27setConfigAlphaBits(8);28setConfigDepthBits(24);29}3031void testSetUp() override32{33if (!IsGLExtensionEnabled("GL_OES_EGL_image_external"))34{35return;36}3738const char *vertSrc = R"(precision highp float;39attribute vec4 a_position;40varying vec2 v_texcoord;4142uniform vec2 u_offset;4344void main()45{46gl_Position = a_position;47v_texcoord = (a_position.xy * 0.5) + 0.5 + u_offset;48}49)";50const char *fragSrc = R"(#extension GL_OES_EGL_image_external : require51precision highp float;52uniform samplerExternalOES s_tex;53varying vec2 v_texcoord;5455void main()56{57gl_FragColor = texture2D(s_tex, v_texcoord);58}59)";6061mProgram = CompileProgram(vertSrc, fragSrc);62ASSERT_GL_NO_ERROR();63ASSERT_NE(mProgram, 0u);6465constexpr GLsizei texSize = 32;66GLubyte data[texSize * texSize * 4];6768for (int y = 0; y < texSize; y++)69{70float green = static_cast<float>(y) / texSize;71for (int x = 0; x < texSize; x++)72{73float red = static_cast<float>(x) / texSize;7475data[(y * texSize + x) * 4 + 0] = static_cast<GLubyte>(red * 255);76data[(y * texSize + x) * 4 + 1] = static_cast<GLubyte>(green * 255);7778data[(y * texSize + x) * 4 + 2] = 0;79data[(y * texSize + x) * 4 + 3] = 255;80}81}8283glGenTextures(1, &mSourceTexture);84glBindTexture(GL_TEXTURE_2D, mSourceTexture);85glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSize, texSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,86data);8788glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);89glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);90ASSERT_GL_NO_ERROR();9192glGenTextures(1, &mExternalTexture);9394ASSERT_GL_NO_ERROR();95}9697void testTearDown() override98{99if (mProgram != 0)100{101glDeleteProgram(mProgram);102}103if (mExternalTexture != 0)104{105glDeleteTextures(1, &mExternalTexture);106}107if (mSourceTexture != 0)108{109glDeleteTextures(1, &mSourceTexture);110}111}112113void createExternalTexture()114{115ASSERT_TRUE(IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(),116"EGL_KHR_gl_texture_2D_image"));117ASSERT_TRUE(IsGLExtensionEnabled("GL_OES_EGL_image_external"));118119EGLWindow *window = getEGLWindow();120EGLint attribs[] = {121EGL_IMAGE_PRESERVED,122EGL_TRUE,123EGL_NONE,124};125EGLImageKHR image =126eglCreateImageKHR(window->getDisplay(), window->getContext(), EGL_GL_TEXTURE_2D_KHR,127reinterpret_cast<EGLClientBuffer>(mSourceTexture), attribs);128ASSERT_EGL_SUCCESS();129130glBindTexture(GL_TEXTURE_EXTERNAL_OES, mExternalTexture);131glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image);132133glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);134glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);135136ASSERT_GL_NO_ERROR();137}138139GLuint mProgram;140GLuint mTextureUniformLocation;141GLuint mOffsetUniformLocation;142143GLuint mSourceTexture;144EGLImageKHR mExternalImage;145GLuint mExternalTexture;146};147148// Test the default sampling behavior of an external texture149TEST_P(ExternalWrapTest, NoWrap)150{151ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_EGL_image_external"));152ANGLE_SKIP_TEST_IF(153!IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(), "EGL_KHR_gl_texture_2D_image"));154155// Ozone only supports external target for images created with EGL_EXT_image_dma_buf_import156ANGLE_SKIP_TEST_IF(IsOzone());157158createExternalTexture();159160ASSERT_NE(mProgram, 0u);161glUseProgram(mProgram);162glBindTexture(GL_TEXTURE_EXTERNAL_OES, mExternalTexture);163glUniform2f(glGetUniformLocation(mProgram, "u_offset"), 0.0, 0.0);164drawQuad(mProgram, "a_position", 0);165EXPECT_GL_NO_ERROR();166167EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor(0, 0, 0, 255), kPixelThreshold);168EXPECT_PIXEL_COLOR_NEAR(127, 0, GLColor(247, 0, 0, 255), kPixelThreshold);169EXPECT_PIXEL_COLOR_NEAR(0, 127, GLColor(0, 247, 0, 255), kPixelThreshold);170EXPECT_PIXEL_COLOR_NEAR(127, 127, GLColor(247, 247, 0, 255), kPixelThreshold);171}172173// Test that external textures are sampled correctly when used with GL_CLAMP_TO_EDGE174TEST_P(ExternalWrapTest, ClampToEdge)175{176ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_EGL_image_external"));177ANGLE_SKIP_TEST_IF(178!IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(), "EGL_KHR_gl_texture_2D_image"));179180// Ozone only supports external target for images created with EGL_EXT_image_dma_buf_import181ANGLE_SKIP_TEST_IF(IsOzone());182183createExternalTexture();184185ASSERT_NE(mProgram, 0u);186glUseProgram(mProgram);187glBindTexture(GL_TEXTURE_EXTERNAL_OES, mExternalTexture);188glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);189glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);190glUniform2f(glGetUniformLocation(mProgram, "u_offset"), 0.5, 0.5);191drawQuad(mProgram, "a_position", 0);192EXPECT_GL_NO_ERROR();193194EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor(127, 127, 0, 255), kPixelThreshold);195EXPECT_PIXEL_COLOR_NEAR(127, 0, GLColor(247, 127, 0, 255), kPixelThreshold);196EXPECT_PIXEL_COLOR_NEAR(0, 127, GLColor(127, 247, 0, 255), kPixelThreshold);197EXPECT_PIXEL_COLOR_NEAR(127, 127, GLColor(247, 247, 0, 255), kPixelThreshold);198}199200// Test that external textures are sampled correctly when used with GL_REPEAT201TEST_P(ExternalWrapTest, Repeat)202{203ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_EGL_image_external"));204ANGLE_SKIP_TEST_IF(205!IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(), "EGL_KHR_gl_texture_2D_image"));206ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_EGL_image_external_wrap_modes"));207208// Ozone only supports external target for images created with EGL_EXT_image_dma_buf_import209ANGLE_SKIP_TEST_IF(IsOzone());210211createExternalTexture();212213ASSERT_NE(mProgram, 0u);214glUseProgram(mProgram);215glBindTexture(GL_TEXTURE_EXTERNAL_OES, mExternalTexture);216glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_REPEAT);217glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_REPEAT);218glUniform2f(glGetUniformLocation(mProgram, "u_offset"), 0.5, 0.5);219220drawQuad(mProgram, "a_position", 0);221EXPECT_GL_NO_ERROR();222223EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor(127, 127, 0, 255), kPixelThreshold);224EXPECT_PIXEL_COLOR_NEAR(127, 0, GLColor(119, 127, 0, 255), kPixelThreshold);225EXPECT_PIXEL_COLOR_NEAR(0, 127, GLColor(127, 119, 0, 255), kPixelThreshold);226EXPECT_PIXEL_COLOR_NEAR(127, 127, GLColor(119, 119, 0, 255), kPixelThreshold);227228EXPECT_PIXEL_COLOR_NEAR(63, 63, GLColor(247, 247, 0, 255), kPixelThreshold);229EXPECT_PIXEL_COLOR_NEAR(64, 63, GLColor(0, 247, 0, 255), kPixelThreshold);230}231232// Test that external textures are sampled correctly when used with GL_MIRRORED_REPEAT233TEST_P(ExternalWrapTest, MirroredRepeat)234{235ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_EGL_image_external"));236ANGLE_SKIP_TEST_IF(237!IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(), "EGL_KHR_gl_texture_2D_image"));238ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_EGL_image_external_wrap_modes"));239240// Ozone only supports external target for images created with EGL_EXT_image_dma_buf_import241ANGLE_SKIP_TEST_IF(IsOzone());242243createExternalTexture();244245ASSERT_NE(mProgram, 0u);246glUseProgram(mProgram);247glBindTexture(GL_TEXTURE_EXTERNAL_OES, mExternalTexture);248glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);249glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);250glUniform2f(glGetUniformLocation(mProgram, "u_offset"), 0.5, 0.5);251252drawQuad(mProgram, "a_position", 0);253EXPECT_GL_NO_ERROR();254255EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor(127, 127, 0, 255), kPixelThreshold);256EXPECT_PIXEL_COLOR_NEAR(127, 0, GLColor(127, 127, 0, 255), kPixelThreshold);257EXPECT_PIXEL_COLOR_NEAR(0, 127, GLColor(127, 127, 0, 255), kPixelThreshold);258EXPECT_PIXEL_COLOR_NEAR(127, 127, GLColor(127, 127, 0, 255), kPixelThreshold);259260EXPECT_PIXEL_COLOR_NEAR(63, 63, GLColor(247, 247, 0, 255), kPixelThreshold);261EXPECT_PIXEL_COLOR_NEAR(64, 63, GLColor(247, 247, 0, 255), kPixelThreshold);262}263264// Use this to select which configurations (e.g. which renderer, which GLES major version) these265// tests should be run against.266ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(ExternalWrapTest);267} // namespace angle268269270