Path: blob/main_old/src/tests/gl_tests/D3DImageFormatConversionTest.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//5// D3DImageFormatConversionTest:6// Basic tests to validate code relating to D3D Image format conversions.78#include "test_utils/ANGLETest.h"910#include "image_util/imageformats.h"1112using namespace angle;1314namespace15{1617class D3DImageFormatConversionTest : public ANGLETest18{19protected:20D3DImageFormatConversionTest()21{22setWindowWidth(128);23setWindowHeight(128);24setConfigRedBits(8);25setConfigGreenBits(8);26setConfigBlueBits(8);27setConfigAlphaBits(8);28}2930void testSetUp() override31{32constexpr char kVS[] = R"(precision highp float;33attribute vec4 position;34varying vec2 texcoord;3536void main()37{38gl_Position = vec4(position.xy, 0.0, 1.0);39texcoord = (position.xy * 0.5) + 0.5;40})";4142constexpr char kFS[] = R"(precision highp float;43uniform sampler2D tex;44varying vec2 texcoord;4546void main()47{48gl_FragColor = texture2D(tex, texcoord);49})";5051m2DProgram = CompileProgram(kVS, kFS);52mTexture2DUniformLocation = glGetUniformLocation(m2DProgram, "tex");53}5455void testTearDown() override { glDeleteProgram(m2DProgram); }5657// Uses ColorStructType::writeColor to populate initial data for a texture, pass it to58// glTexImage2D, then render with it. The resulting colors should match the colors passed into59// ::writeColor.60template <typename ColorStructType>61void runTest(GLenum tex2DFormat, GLenum tex2DType)62{63gl::ColorF srcColorF[4];64ColorStructType pixels[4];6566GLuint tex = 0;67GLuint fbo = 0;68glGenTextures(1, &tex);69glGenFramebuffers(1, &fbo);70EXPECT_GL_NO_ERROR();7172srcColorF[0].red = 1.0f;73srcColorF[0].green = 0.0f;74srcColorF[0].blue = 0.0f;75srcColorF[0].alpha = 1.0f; // Red76srcColorF[1].red = 0.0f;77srcColorF[1].green = 1.0f;78srcColorF[1].blue = 0.0f;79srcColorF[1].alpha = 1.0f; // Green80srcColorF[2].red = 0.0f;81srcColorF[2].green = 0.0f;82srcColorF[2].blue = 1.0f;83srcColorF[2].alpha = 1.0f; // Blue84srcColorF[3].red = 1.0f;85srcColorF[3].green = 1.0f;86srcColorF[3].blue = 0.0f;87srcColorF[3].alpha = 1.0f; // Red + Green (Yellow)8889// Convert the ColorF into the pixels that will be fed to glTexImage2D90for (unsigned int i = 0; i < 4; i++)91{92ColorStructType::writeColor(&(pixels[i]), &(srcColorF[i]));93}9495// Generate the texture96glBindTexture(GL_TEXTURE_2D, tex);97glTexImage2D(GL_TEXTURE_2D, 0, tex2DFormat, 2, 2, 0, tex2DFormat, tex2DType, pixels);98glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);99glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);100EXPECT_GL_NO_ERROR();101102// Draw a quad using the texture103glClearColor(0, 0, 0, 0);104glClear(GL_COLOR_BUFFER_BIT);105glUseProgram(m2DProgram);106glUniform1i(mTexture2DUniformLocation, 0);107drawQuad(m2DProgram, "position", 0.5f);108EXPECT_GL_NO_ERROR();109110// Check that the pixel colors match srcColorF111EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);112EXPECT_PIXEL_EQ(getWindowHeight() - 1, 0, 0, 255, 0, 255);113EXPECT_PIXEL_EQ(0, getWindowWidth() - 1, 0, 0, 255, 255);114EXPECT_PIXEL_EQ(getWindowHeight() - 1, getWindowWidth() - 1, 255, 255, 0, 255);115swapBuffers();116117glDeleteFramebuffers(1, &fbo);118glDeleteTextures(1, &tex);119}120121GLuint m2DProgram;122GLint mTexture2DUniformLocation;123};124125// Validation test for rx::R4G4B4A4's writeColor functions126TEST_P(D3DImageFormatConversionTest, WriteColorFunctionR4G4B4A4)127{128runTest<R4G4B4A4>(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4);129}130131// Validation test for rx::R5G5B5A1's writeColor functions132TEST_P(D3DImageFormatConversionTest, WriteColorFunctionR5G5B5A1)133{134runTest<R5G5B5A1>(GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1);135}136137// Validation test for rx::R5G6B5's writeColor functions138TEST_P(D3DImageFormatConversionTest, WriteColorFunctionR5G6B5)139{140runTest<R5G6B5>(GL_RGB, GL_UNSIGNED_SHORT_5_6_5);141}142143// Validation test for rx::R8G8B8A8's writeColor functions144TEST_P(D3DImageFormatConversionTest, WriteColorFunctionR8G8B8A8)145{146runTest<R8G8B8A8>(GL_RGBA, GL_UNSIGNED_BYTE);147}148149// Validation test for rx::R8G8B8's writeColor functions150TEST_P(D3DImageFormatConversionTest, WriteColorFunctionR8G8B8)151{152glPixelStorei(GL_UNPACK_ALIGNMENT, 1);153runTest<R8G8B8>(GL_RGB, GL_UNSIGNED_BYTE);154}155156// Use this to select which configurations (e.g. which renderer, which GLES major version) these157// tests should be run against. Even though this test is only run on Windows (since it includes158// imageformats.h from the D3D renderer), we can still run the test against OpenGL. This is159// valuable, since it provides extra validation using a renderer that doesn't use imageformats.h160// itself.161ANGLE_INSTANTIATE_TEST_ES2(D3DImageFormatConversionTest);162163} // namespace164165166