Path: blob/main_old/src/tests/gl_tests/CubeMapTextureTest.cpp
1693 views
//1// Copyright 2015 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//56#include "test_utils/ANGLETest.h"7#include "test_utils/gl_raii.h"89using namespace angle;1011class CubeMapTextureTest : public ANGLETest12{13protected:14CubeMapTextureTest()15{16setWindowWidth(256);17setWindowHeight(256);18setConfigRedBits(8);19setConfigGreenBits(8);20setConfigBlueBits(8);21setConfigAlphaBits(8);22}2324void testSetUp() override25{26mProgram = CompileProgram(essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());27if (mProgram == 0)28{29FAIL() << "shader compilation failed.";30}3132mColorLocation = glGetUniformLocation(mProgram, essl1_shaders::ColorUniform());3334glUseProgram(mProgram);3536glClearColor(0, 0, 0, 0);37glClearDepthf(0.0);38glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);3940glEnable(GL_BLEND);41glDisable(GL_DEPTH_TEST);4243ASSERT_GL_NO_ERROR();44}4546void testTearDown() override { glDeleteProgram(mProgram); }4748void runSampleCoordinateTransformTest(const char *shader);4950GLuint mProgram;51GLint mColorLocation;52};5354// Verify that rendering to the faces of a cube map consecutively will correctly render to each55// face.56TEST_P(CubeMapTextureTest, RenderToFacesConsecutively)57{58// TODO: Diagnose and fix. http://anglebug.com/295459ANGLE_SKIP_TEST_IF(IsVulkan() && IsIntel() && IsWindows());6061// http://anglebug.com/314562ANGLE_SKIP_TEST_IF(IsVulkan() && IsIntel() && IsFuchsia());6364// TODO(hqle): Find what wrong with NVIDIA GPU. http://anglebug.com/413865ANGLE_SKIP_TEST_IF(IsNVIDIA() && IsMetal());6667const GLfloat faceColors[] = {681.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,691.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,70};7172GLuint tex = 0;73glGenTextures(1, &tex);74glBindTexture(GL_TEXTURE_CUBE_MAP, tex);75for (GLenum face = 0; face < 6; face++)76{77glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, 1, 1, 0, GL_RGBA,78GL_UNSIGNED_BYTE, nullptr);79}80EXPECT_GL_NO_ERROR();8182GLuint fbo = 0;83glGenFramebuffers(1, &fbo);84glBindFramebuffer(GL_FRAMEBUFFER, fbo);85EXPECT_GL_NO_ERROR();8687for (GLenum face = 0; face < 6; face++)88{89glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,90GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, tex, 0);91EXPECT_GL_NO_ERROR();9293glUseProgram(mProgram);9495const GLfloat *faceColor = faceColors + (face * 4);96glUniform4f(mColorLocation, faceColor[0], faceColor[1], faceColor[2], faceColor[3]);9798drawQuad(mProgram, essl1_shaders::PositionAttrib(), 0.5f);99EXPECT_GL_NO_ERROR();100}101102for (GLenum face = 0; face < 6; face++)103{104glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,105GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, tex, 0);106EXPECT_GL_NO_ERROR();107108const GLfloat *faceColor = faceColors + (face * 4);109EXPECT_PIXEL_EQ(0, 0, faceColor[0] * 255, faceColor[1] * 255, faceColor[2] * 255,110faceColor[3] * 255);111EXPECT_GL_NO_ERROR();112}113114glDeleteFramebuffers(1, &fbo);115glDeleteTextures(1, &tex);116117EXPECT_GL_NO_ERROR();118}119120void CubeMapTextureTest::runSampleCoordinateTransformTest(const char *shader)121{122// Fails to compile the shader. anglebug.com/3776123ANGLE_SKIP_TEST_IF(IsOpenGL() && IsIntel() && IsWindows());124125constexpr GLsizei kCubeFaceCount = 6;126constexpr GLsizei kCubeFaceSectionCount = 4;127constexpr GLsizei kCubeFaceSectionCountSqrt = 2;128129constexpr GLColor faceColors[kCubeFaceCount][kCubeFaceSectionCount] = {130{GLColor(255, 0, 0, 255), GLColor(191, 0, 0, 255), GLColor(127, 0, 0, 255),131GLColor(63, 0, 0, 255)},132{GLColor(0, 255, 0, 255), GLColor(0, 191, 0, 255), GLColor(0, 127, 0, 255),133GLColor(0, 63, 0, 255)},134{GLColor(0, 0, 255, 255), GLColor(0, 0, 191, 255), GLColor(0, 0, 127, 255),135GLColor(0, 0, 63, 255)},136{GLColor(255, 63, 0, 255), GLColor(191, 127, 0, 255), GLColor(127, 191, 0, 255),137GLColor(63, 255, 0, 255)},138{GLColor(0, 255, 63, 255), GLColor(0, 191, 127, 255), GLColor(0, 127, 191, 255),139GLColor(0, 63, 255, 255)},140{GLColor(63, 0, 255, 255), GLColor(127, 0, 191, 255), GLColor(191, 0, 127, 255),141GLColor(255, 0, 63, 255)},142};143144constexpr GLsizei kTextureSize = 32;145146GLTexture tex;147glBindTexture(GL_TEXTURE_CUBE_MAP, tex);148for (GLenum face = 0; face < kCubeFaceCount; face++)149{150std::vector<GLColor> faceData(kTextureSize * kTextureSize);151152// Create the face with four sections, each with a solid color from |faceColors|.153for (size_t row = 0; row < kTextureSize / kCubeFaceSectionCountSqrt; ++row)154{155for (size_t col = 0; col < kTextureSize / kCubeFaceSectionCountSqrt; ++col)156{157for (size_t srow = 0; srow < kCubeFaceSectionCountSqrt; ++srow)158{159for (size_t scol = 0; scol < kCubeFaceSectionCountSqrt; ++scol)160{161size_t r = row + srow * kTextureSize / kCubeFaceSectionCountSqrt;162size_t c = col + scol * kTextureSize / kCubeFaceSectionCountSqrt;163size_t s = srow * kCubeFaceSectionCountSqrt + scol;164faceData[r * kTextureSize + c] = faceColors[face][s];165}166}167}168}169170glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, kTextureSize, kTextureSize,1710, GL_RGBA, GL_UNSIGNED_BYTE, faceData.data());172}173glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);174glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);175EXPECT_GL_NO_ERROR();176177GLTexture fboTex;178glBindTexture(GL_TEXTURE_2D, fboTex);179glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kCubeFaceCount, kCubeFaceSectionCount, 0, GL_RGBA,180GL_UNSIGNED_BYTE, nullptr);181182GLFramebuffer fbo;183glBindFramebuffer(GL_FRAMEBUFFER, fbo);184glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTex, 0);185EXPECT_GL_NO_ERROR();186187ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), shader);188glUseProgram(program);189190GLint texCubeLocation = glGetUniformLocation(program, "texCube");191ASSERT_NE(-1, texCubeLocation);192glUniform1i(texCubeLocation, 0);193194drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);195EXPECT_GL_NO_ERROR();196197for (GLenum face = 0; face < kCubeFaceCount; face++)198{199// The following table defines the translation from textureCube coordinates to coordinates200// in each face. The framebuffer has width 6 and height 4. Every column corresponding to201// an x value represents one cube face. The values in rows are samples from the four202// sections of the face.203//204// Major Axis Direction Target sc tc ma205// +rx TEXTURE_CUBE_MAP_POSITIVE_X −rz −ry rx206// −rx TEXTURE_CUBE_MAP_NEGATIVE_X rz −ry rx207// +ry TEXTURE_CUBE_MAP_POSITIVE_Y rx rz ry208// −ry TEXTURE_CUBE_MAP_NEGATIVE_Y rx −rz ry209// +rz TEXTURE_CUBE_MAP_POSITIVE_Z rx −ry rz210// −rz TEXTURE_CUBE_MAP_NEGATIVE_Z −rx −ry rz211//212// This table is used only to determine the direction of growth for s and t. The shader213// always generates (row,col) coordinates (0, 0), (0, 1), (1, 0), (1, 1) which is the order214// the data is uploaded to the faces, but based on the table above, the sample order would215// be different.216constexpr size_t faceSampledSections[kCubeFaceCount][kCubeFaceSectionCount] = {217{3, 2, 1, 0}, {2, 3, 0, 1}, {0, 1, 2, 3}, {2, 3, 0, 1}, {2, 3, 0, 1}, {3, 2, 1, 0},218};219220for (size_t section = 0; section < kCubeFaceSectionCount; ++section)221{222const GLColor sectionColor = faceColors[face][faceSampledSections[face][section]];223224EXPECT_PIXEL_COLOR_EQ(face, section, sectionColor)225<< "face " << face << ", section " << section;226}227}228EXPECT_GL_NO_ERROR();229}230231// Verify that cube map sampling follows the rules that map cubemap coordinates to coordinates232// within each face. See section 3.7.5 of GLES2.0 (Cube Map Texture Selection).233TEST_P(CubeMapTextureTest, SampleCoordinateTransform)234{235// http://anglebug.com/4092236ANGLE_SKIP_TEST_IF(IsWindows() && IsD3D9());237// Create a program that samples from 6x4 directions of the cubemap, draw and verify that the238// colors match the right color from |faceColors|.239constexpr char kFS[] = R"(precision mediump float;240241uniform samplerCube texCube;242243const mat4 coordInSection = mat4(244vec4(-0.5, -0.5, 0, 0),245vec4( 0.5, -0.5, 0, 0),246vec4(-0.5, 0.5, 0, 0),247vec4( 0.5, 0.5, 0, 0)248);249250void main()251{252vec3 coord;253if (gl_FragCoord.x < 2.0)254{255coord.x = gl_FragCoord.x < 1.0 ? 1.0 : -1.0;256coord.zy = coordInSection[int(gl_FragCoord.y)].xy;257}258else if (gl_FragCoord.x < 4.0)259{260coord.y = gl_FragCoord.x < 3.0 ? 1.0 : -1.0;261coord.xz = coordInSection[int(gl_FragCoord.y)].xy;262}263else264{265coord.z = gl_FragCoord.x < 5.0 ? 1.0 : -1.0;266coord.xy = coordInSection[int(gl_FragCoord.y)].xy;267}268269gl_FragColor = textureCube(texCube, coord);270})";271272runSampleCoordinateTransformTest(kFS);273}274275// On Android Vulkan, unequal x and y derivatives cause this test to fail.276TEST_P(CubeMapTextureTest, SampleCoordinateTransformGrad)277{278ANGLE_SKIP_TEST_IF(IsAndroid() && IsVulkan()); // anglebug.com/3814279ANGLE_SKIP_TEST_IF(IsD3D11()); // anglebug.com/3856280ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_shader_texture_lod"));281// http://anglebug.com/4092282ANGLE_SKIP_TEST_IF(IsWindows() && IsD3D9());283284constexpr char kFS[] = R"(#extension GL_EXT_shader_texture_lod : require285precision mediump float;286287uniform samplerCube texCube;288289const mat4 coordInSection = mat4(290vec4(-0.5, -0.5, 0, 0),291vec4( 0.5, -0.5, 0, 0),292vec4(-0.5, 0.5, 0, 0),293vec4( 0.5, 0.5, 0, 0)294);295296void main()297{298vec3 coord;299if (gl_FragCoord.x < 2.0)300{301coord.x = gl_FragCoord.x < 1.0 ? 1.0 : -1.0;302coord.zy = coordInSection[int(gl_FragCoord.y)].xy;303}304else if (gl_FragCoord.x < 4.0)305{306coord.y = gl_FragCoord.x < 3.0 ? 1.0 : -1.0;307coord.xz = coordInSection[int(gl_FragCoord.y)].xy;308}309else310{311coord.z = gl_FragCoord.x < 5.0 ? 1.0 : -1.0;312coord.xy = coordInSection[int(gl_FragCoord.y)].xy;313}314315gl_FragColor = textureCubeGradEXT(texCube, coord,316vec3(10.0, 10.0, 0.0), vec3(0.0, 10.0, 10.0));317})";318319runSampleCoordinateTransformTest(kFS);320}321322// Use this to select which configurations (e.g. which renderer, which GLES major version) these323// tests should be run against.324ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(CubeMapTextureTest);325326327