Hash :
ceffd20c
Author :
Date :
2018-01-08T16:39:45
Set colorspace of D3D pbuffers according to DXGI format When a EGL pbuffer is created based on a D3D SRGB texture using EGL_ANGLE_d3d_texture_client_buffer, SRGB conversions are performed as if it was an SRGB surface. The value of EGL_GL_COLORSPACE now reflects that correctly. If the pbuffer is bound to a texture and used as a framebuffer attachment, querying GL_FRAMEBUFFER_COLOR_ATTACHMENT also reflects that correctly. The behavior is the same on both the D3D backend, where there is no native interop involved, and on the GL backend using WGL_NV_DX_interop(2). There are a few limitations on the GL backend that relies on native interop: 1. SRGB conversion for textures created this way can't be disabled using the GL_FRAMEBUFFER_SRGB_EXT toggle that's exposed in ANGLE through EXT_sRGB_write_control. This is now documented in the EGL_ANGLE_d3d_texture_client_buffer spec. On the D3D backend this is not a problem since EXT_sRGB_write_control is not supported either way. 2. Creating a pbuffer out of a D3D11 texture with the format DXGI_FORMAT_B8G8R8A8_UNORM_SRGB does not work, even though it was listed as one of the supported formats in the EGL_ANGLE_d3d_texture_client_buffer spec. It's now mentioned that support for this format is optional. BUG=angleproject:2300 TEST=angle_white_box_tests Change-Id: I70ee0646680805e4469291a5b2ce59e92fda009e Reviewed-on: https://chromium-review.googlesource.com/866743 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
//
// Copyright 2015 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.
//
// D3DTextureTest:
// Tests of the EGL_ANGLE_d3d_texture_client_buffer extension
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include <d3d11.h>
#include <windows.h>
#include "com_utils.h"
namespace angle
{
class D3DTextureTest : public ANGLETest
{
protected:
D3DTextureTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
setConfigDepthBits(24);
setConfigStencilBits(8);
}
void SetUp() override
{
ANGLETest::SetUp();
const std::string vsSource =
R"(precision highp float;
attribute vec4 position;
varying vec2 texcoord;
void main()
{
gl_Position = position;
texcoord = (position.xy * 0.5) + 0.5;
texcoord.y = 1.0 - texcoord.y;
})";
const std::string textureFSSource =
R"(precision highp float;
uniform sampler2D tex;
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(tex, texcoord);
})";
const std::string textureFSSourceNoSampling =
R"(precision highp float;
void main()
{
gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
})";
mTextureProgram = CompileProgram(vsSource, textureFSSource);
ASSERT_NE(0u, mTextureProgram) << "shader compilation failed.";
mTextureUniformLocation = glGetUniformLocation(mTextureProgram, "tex");
ASSERT_NE(-1, mTextureUniformLocation);
mTextureProgramNoSampling = CompileProgram(vsSource, textureFSSourceNoSampling);
ASSERT_NE(0u, mTextureProgramNoSampling) << "shader compilation failed.";
mD3D11Module = LoadLibrary(TEXT("d3d11.dll"));
ASSERT_NE(nullptr, mD3D11Module);
PFN_D3D11_CREATE_DEVICE createDeviceFunc = reinterpret_cast<PFN_D3D11_CREATE_DEVICE>(
GetProcAddress(mD3D11Module, "D3D11CreateDevice"));
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
if (eglDisplayExtensionEnabled(display, "EGL_EXT_device_query"))
{
PFNEGLQUERYDISPLAYATTRIBEXTPROC eglQueryDisplayAttribEXT =
reinterpret_cast<PFNEGLQUERYDISPLAYATTRIBEXTPROC>(
eglGetProcAddress("eglQueryDisplayAttribEXT"));
PFNEGLQUERYDEVICEATTRIBEXTPROC eglQueryDeviceAttribEXT =
reinterpret_cast<PFNEGLQUERYDEVICEATTRIBEXTPROC>(
eglGetProcAddress("eglQueryDeviceAttribEXT"));
EGLDeviceEXT device = 0;
{
EGLAttrib result = 0;
EXPECT_EGL_TRUE(eglQueryDisplayAttribEXT(display, EGL_DEVICE_EXT, &result));
device = reinterpret_cast<EGLDeviceEXT>(result);
}
if (eglDeviceExtensionEnabled(device, "EGL_ANGLE_device_d3d"))
{
EGLAttrib result = 0;
if (eglQueryDeviceAttribEXT(device, EGL_D3D11_DEVICE_ANGLE, &result))
{
mD3D11Device = reinterpret_cast<ID3D11Device *>(result);
mD3D11Device->AddRef();
}
else if (eglQueryDeviceAttribEXT(device, EGL_D3D9_DEVICE_ANGLE, &result))
{
mD3D9Device = reinterpret_cast<IDirect3DDevice9 *>(result);
mD3D9Device->AddRef();
}
}
}
else
{
ASSERT_TRUE(
SUCCEEDED(createDeviceFunc(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr,
0, D3D11_SDK_VERSION, &mD3D11Device, nullptr, nullptr)));
}
}
void TearDown() override
{
glDeleteProgram(mTextureProgram);
if (mD3D11Device)
{
mD3D11Device->Release();
mD3D11Device = nullptr;
}
FreeLibrary(mD3D11Module);
mD3D11Module = nullptr;
if (mD3D9Device)
{
mD3D9Device->Release();
mD3D9Device = nullptr;
}
ANGLETest::TearDown();
}
EGLSurface createD3D11PBuffer(size_t width,
size_t height,
EGLint eglTextureFormat,
EGLint eglTextureTarget,
UINT sampleCount,
UINT sampleQuality,
UINT bindFlags,
DXGI_FORMAT format)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
EGLConfig config = window->getConfig();
EGLint attribs[] = {
EGL_TEXTURE_FORMAT, eglTextureFormat, EGL_TEXTURE_TARGET,
eglTextureTarget, EGL_NONE, EGL_NONE,
};
ASSERT(mD3D11Device);
ID3D11Texture2D *texture = nullptr;
CD3D11_TEXTURE2D_DESC desc(format, static_cast<UINT>(width), static_cast<UINT>(height), 1,
1, bindFlags);
desc.SampleDesc.Count = sampleCount;
desc.SampleDesc.Quality = sampleQuality;
EXPECT_TRUE(SUCCEEDED(mD3D11Device->CreateTexture2D(&desc, nullptr, &texture)));
EGLSurface pbuffer = eglCreatePbufferFromClientBuffer(display, EGL_D3D_TEXTURE_ANGLE,
texture, config, attribs);
texture->Release();
return pbuffer;
}
EGLSurface createPBuffer(size_t width,
size_t height,
EGLint eglTextureFormat,
EGLint eglTextureTarget,
UINT sampleCount,
UINT sampleQuality)
{
if (mD3D11Device)
{
return createD3D11PBuffer(
width, height, eglTextureFormat, eglTextureTarget, sampleCount, sampleQuality,
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET, DXGI_FORMAT_R8G8B8A8_UNORM);
}
if (mD3D9Device)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
EGLConfig config = window->getConfig();
EGLint attribs[] = {
EGL_TEXTURE_FORMAT, eglTextureFormat, EGL_TEXTURE_TARGET,
eglTextureTarget, EGL_NONE, EGL_NONE,
};
// Multisampled textures are not supported on D3D9.
ASSERT(sampleCount <= 1);
ASSERT(sampleQuality == 0);
IDirect3DTexture9 *texture = nullptr;
EXPECT_TRUE(SUCCEEDED(mD3D9Device->CreateTexture(
static_cast<UINT>(width), static_cast<UINT>(height), 1, D3DUSAGE_RENDERTARGET,
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, nullptr)));
EGLSurface pbuffer = eglCreatePbufferFromClientBuffer(display, EGL_D3D_TEXTURE_ANGLE,
texture, config, attribs);
texture->Release();
return pbuffer;
}
else
{
return EGL_NO_SURFACE;
}
}
bool valid() const
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
if (!eglDisplayExtensionEnabled(display, "EGL_ANGLE_d3d_texture_client_buffer"))
{
std::cout << "Test skipped due to missing EGL_ANGLE_d3d_texture_client_buffer"
<< std::endl;
return false;
}
if (!mD3D11Device && !mD3D9Device)
{
std::cout << "Test skipped due to no D3D devices being available." << std::endl;
return false;
}
if (IsWindows() && IsAMD() && IsOpenGL())
{
std::cout << "Test skipped on Windows AMD OpenGL." << std::endl;
return false;
}
if (IsWindows() && IsIntel() && IsOpenGL())
{
std::cout << "Test skipped on Windows Intel OpenGL." << std::endl;
return false;
}
return true;
}
GLuint mTextureProgram;
GLuint mTextureProgramNoSampling;
GLint mTextureUniformLocation;
HMODULE mD3D11Module = nullptr;
ID3D11Device *mD3D11Device = nullptr;
IDirect3DDevice9 *mD3D9Device = nullptr;
};
// Test creating pbuffer from textures with several different DXGI formats.
TEST_P(D3DTextureTest, TestD3D11SupportedFormatsSurface)
{
bool srgbSupported = extensionEnabled("GL_EXT_sRGB") || getClientMajorVersion() == 3;
ANGLE_SKIP_TEST_IF(!valid() || !mD3D11Device || !srgbSupported);
const DXGI_FORMAT formats[] = {DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM_SRGB};
for (size_t i = 0; i < 4; ++i)
{
if (formats[i] == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB)
{
if (IsOpenGL())
{
// This generates an invalid format error when calling wglDXRegisterObjectNV().
// Reproducible at least on NVIDIA driver 390.65 on Windows 10.
std::cout << "DXGI_FORMAT_B8G8R8A8_UNORM_SRGB subtest skipped: IsOpenGL().\n";
continue;
}
}
EGLSurface pbuffer = createD3D11PBuffer(32, 32, EGL_TEXTURE_RGBA, EGL_TEXTURE_2D, 1, 0,
D3D11_BIND_RENDER_TARGET, formats[i]);
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
EGLint colorspace;
eglQuerySurface(display, pbuffer, EGL_GL_COLORSPACE, &colorspace);
if (formats[i] == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB ||
formats[i] == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB)
{
EXPECT_EQ(EGL_GL_COLORSPACE_SRGB, colorspace);
}
else
{
EXPECT_EQ(EGL_GL_COLORSPACE_LINEAR, colorspace);
}
eglMakeCurrent(display, pbuffer, pbuffer, window->getContext());
ASSERT_EGL_SUCCESS();
window->makeCurrent();
eglDestroySurface(display, pbuffer);
}
}
// Test binding a pbuffer created from a D3D texture as a texture image with several different DXGI
// formats. The test renders to and samples from the pbuffer.
TEST_P(D3DTextureTest, TestD3D11SupportedFormatsTexture)
{
bool srgbSupported = extensionEnabled("GL_EXT_sRGB") || getClientMajorVersion() == 3;
ANGLE_SKIP_TEST_IF(!valid() || !mD3D11Device || !srgbSupported);
bool srgbWriteControlSupported = extensionEnabled("GL_EXT_sRGB_write_control") && !IsOpenGL();
const DXGI_FORMAT formats[] = {DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB};
for (size_t i = 0; i < 4; ++i)
{
if (formats[i] == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB)
{
if (IsOpenGL())
{
// This generates an invalid format error when calling wglDXRegisterObjectNV().
// Reproducible at least on NVIDIA driver 390.65 on Windows 10.
std::cout << "DXGI_FORMAT_B8G8R8A8_UNORM_SRGB subtest skipped: IsOpenGL().\n";
continue;
}
}
SCOPED_TRACE(std::string("Test case:") + std::to_string(i));
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
EGLSurface pbuffer =
createD3D11PBuffer(32, 32, EGL_TEXTURE_RGBA, EGL_TEXTURE_2D, 1, 0,
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE, formats[i]);
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
EGLint colorspace;
eglQuerySurface(display, pbuffer, EGL_GL_COLORSPACE, &colorspace);
GLuint texture = 0u;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
EGLBoolean result = eglBindTexImage(display, pbuffer, EGL_BACK_BUFFER);
ASSERT_EGL_SUCCESS();
ASSERT(result == EGL_TRUE);
GLuint fbo = 0u;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glViewport(0, 0, 32, 32);
// Clear the texture with 50% green and check that the color value written is correct.
glClearColor(0.0f, 0.5f, 0.0f, 1.0f);
GLint colorEncoding = 0;
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT,
&colorEncoding);
if (formats[i] == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB ||
formats[i] == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB)
{
EXPECT_EQ(EGL_GL_COLORSPACE_SRGB, colorspace);
EXPECT_EQ(GL_SRGB_EXT, colorEncoding);
}
else
{
EXPECT_EQ(EGL_GL_COLORSPACE_LINEAR, colorspace);
EXPECT_EQ(GL_LINEAR, colorEncoding);
}
if (colorEncoding == GL_SRGB_EXT)
{
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor(0u, 188u, 0u, 255u), 2);
// Disable SRGB and run the non-sRGB test case.
if (srgbWriteControlSupported)
glDisable(GL_FRAMEBUFFER_SRGB_EXT);
}
if (colorEncoding == GL_LINEAR || srgbWriteControlSupported)
{
glClearColor(0.0f, 0.5f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor(0u, 127u, 0u, 255u), 2);
}
// Draw with the texture to a linear framebuffer and check that the color value written is
// correct.
GLFramebuffer scratchFbo;
glBindFramebuffer(GL_FRAMEBUFFER, scratchFbo);
GLTexture scratchTexture;
glBindTexture(GL_TEXTURE_2D, scratchTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, scratchTexture,
0);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(mTextureProgram);
glUniform1i(mTextureUniformLocation, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
drawQuad(mTextureProgram, "position", 0.5f);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor(0u, 127u, 0u, 255u), 2);
glBindFramebuffer(GL_FRAMEBUFFER, 0u);
glBindTexture(GL_TEXTURE_2D, 0u);
glDeleteTextures(1, &texture);
glDeleteFramebuffers(1, &fbo);
eglDestroySurface(display, pbuffer);
}
}
// Test creating a pbuffer with unnecessary EGL_WIDTH and EGL_HEIGHT attributes because that's what
// Chromium does. This is a regression test for crbug.com/794086
TEST_P(D3DTextureTest, UnnecessaryWidthHeightAttributes)
{
ANGLE_SKIP_TEST_IF(!valid() || !IsD3D11());
ASSERT(mD3D11Device);
ID3D11Texture2D *texture = nullptr;
CD3D11_TEXTURE2D_DESC desc(DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1, 1, 1, D3D11_BIND_RENDER_TARGET);
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
EXPECT_TRUE(SUCCEEDED(mD3D11Device->CreateTexture2D(&desc, nullptr, &texture)));
EGLint attribs[] = {
EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA,
EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
EGL_WIDTH, 1,
EGL_HEIGHT, 1,
EGL_NONE, EGL_NONE,
};
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
EGLConfig config = window->getConfig();
EGLSurface pbuffer =
eglCreatePbufferFromClientBuffer(display, EGL_D3D_TEXTURE_ANGLE, texture, config, attribs);
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
texture->Release();
// Make current with fixture EGL to ensure the Surface can be released immediately.
getEGLWindow()->makeCurrent();
eglDestroySurface(display, pbuffer);
}
// Test creating a pbuffer from a d3d surface and clearing it
TEST_P(D3DTextureTest, Clear)
{
if (!valid())
{
return;
}
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
const size_t bufferSize = 32;
EGLSurface pbuffer =
createPBuffer(bufferSize, bufferSize, EGL_NO_TEXTURE, EGL_NO_TEXTURE, 1, 0);
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
// Apply the Pbuffer and clear it to purple and verify
eglMakeCurrent(display, pbuffer, pbuffer, window->getContext());
ASSERT_EGL_SUCCESS();
glViewport(0, 0, static_cast<GLsizei>(bufferSize), static_cast<GLsizei>(bufferSize));
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(static_cast<GLint>(bufferSize) / 2, static_cast<GLint>(bufferSize) / 2, 255, 0,
255, 255);
// Make current with fixture EGL to ensure the Surface can be released immediately.
getEGLWindow()->makeCurrent();
eglDestroySurface(display, pbuffer);
}
// Test creating a pbuffer with a D3D texture and depth stencil bits in the EGL config creates keeps
// its depth stencil buffer
TEST_P(D3DTextureTest, DepthStencil)
{
if (!valid())
{
return;
}
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
const size_t bufferSize = 32;
EGLSurface pbuffer =
createPBuffer(bufferSize, bufferSize, EGL_NO_TEXTURE, EGL_NO_TEXTURE, 1, 0);
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
// Apply the Pbuffer and clear it to purple and verify
eglMakeCurrent(display, pbuffer, pbuffer, window->getContext());
ASSERT_EGL_SUCCESS();
glViewport(0, 0, static_cast<GLsizei>(bufferSize), static_cast<GLsizei>(bufferSize));
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glClearDepthf(0.5f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glUseProgram(mTextureProgram);
glUniform1i(mTextureUniformLocation, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::green);
// Draw a quad that will fail the depth test and verify that the buffer is unchanged
drawQuad(mTextureProgram, "position", 1.0f);
EXPECT_PIXEL_COLOR_EQ(static_cast<GLint>(bufferSize) / 2, static_cast<GLint>(bufferSize) / 2,
GLColor::cyan);
// Draw a quad that will pass the depth test and verify that the buffer is green
drawQuad(mTextureProgram, "position", -1.0f);
EXPECT_PIXEL_COLOR_EQ(static_cast<GLint>(bufferSize) / 2, static_cast<GLint>(bufferSize) / 2,
GLColor::green);
// Make current with fixture EGL to ensure the Surface can be released immediately.
getEGLWindow()->makeCurrent();
eglDestroySurface(display, pbuffer);
}
// Test creating a pbuffer from a d3d surface and binding it to a texture
TEST_P(D3DTextureTest, BindTexImage)
{
if (!valid())
{
return;
}
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
const size_t bufferSize = 32;
EGLSurface pbuffer =
createPBuffer(bufferSize, bufferSize, EGL_TEXTURE_RGBA, EGL_TEXTURE_2D, 1, 0);
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
// Apply the Pbuffer and clear it to purple
eglMakeCurrent(display, pbuffer, pbuffer, window->getContext());
ASSERT_EGL_SUCCESS();
glViewport(0, 0, static_cast<GLsizei>(bufferSize), static_cast<GLsizei>(bufferSize));
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(static_cast<GLint>(bufferSize) / 2, static_cast<GLint>(bufferSize) / 2, 255, 0,
255, 255);
// Apply the window surface
eglMakeCurrent(display, window->getSurface(), window->getSurface(), window->getContext());
// Create a texture and bind the Pbuffer to it
GLuint texture = 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
EXPECT_GL_NO_ERROR();
eglBindTexImage(display, pbuffer, EGL_BACK_BUFFER);
glViewport(0, 0, getWindowWidth(), getWindowHeight());
ASSERT_EGL_SUCCESS();
// Draw a quad and verify that it is purple
glUseProgram(mTextureProgram);
glUniform1i(mTextureUniformLocation, 0);
drawQuad(mTextureProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
// Unbind the texture
eglReleaseTexImage(display, pbuffer, EGL_BACK_BUFFER);
ASSERT_EGL_SUCCESS();
// Verify that purple was drawn
EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 255, 0, 255, 255);
glDeleteTextures(1, &texture);
// Make current with fixture EGL to ensure the Surface can be released immediately.
getEGLWindow()->makeCurrent();
eglDestroySurface(display, pbuffer);
}
// Verify that creating a pbuffer with a multisampled texture will fail on a non-multisampled
// window.
TEST_P(D3DTextureTest, CheckSampleMismatch)
{
if (!valid())
{
return;
}
// Multisampling is not supported on D3D9 or OpenGL.
if (IsD3D9() || IsOpenGL())
{
return;
}
constexpr size_t bufferSize = 32;
EGLSurface pbuffer = createPBuffer(bufferSize, bufferSize, EGL_NO_TEXTURE, EGL_NO_TEXTURE, 2,
static_cast<UINT>(D3D11_STANDARD_MULTISAMPLE_PATTERN));
EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
EXPECT_EQ(pbuffer, nullptr);
}
// Tests what happens when we make a PBuffer that isn't shader-readable.
TEST_P(D3DTextureTest, NonReadablePBuffer)
{
ANGLE_SKIP_TEST_IF(!valid() || !IsD3D11());
constexpr size_t bufferSize = 32;
EGLSurface pbuffer =
createD3D11PBuffer(bufferSize, bufferSize, EGL_TEXTURE_RGBA, EGL_TEXTURE_2D, 1, 0,
D3D11_BIND_RENDER_TARGET, DXGI_FORMAT_R8G8B8A8_UNORM);
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
eglMakeCurrent(display, pbuffer, pbuffer, window->getContext());
ASSERT_EGL_SUCCESS();
glViewport(0, 0, static_cast<GLsizei>(bufferSize), static_cast<GLsizei>(bufferSize));
// Clear to green.
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
// Copy the green color to a texture.
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, bufferSize, bufferSize, 0);
ASSERT_GL_NO_ERROR();
// Clear to red.
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
// Draw with the texture and expect green.
draw2DTexturedQuad(0.5f, 1.0f, false);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
// Make current with fixture EGL to ensure the Surface can be released immediately.
getEGLWindow()->makeCurrent();
eglDestroySurface(display, pbuffer);
}
class D3DTextureTestMS : public D3DTextureTest
{
protected:
D3DTextureTestMS() : D3DTextureTest()
{
setSamples(4);
setMultisampleEnabled(true);
}
};
// Test creating a pbuffer from a multisampled d3d surface and clearing it.
TEST_P(D3DTextureTestMS, Clear)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
constexpr size_t bufferSize = 32;
constexpr UINT testpoint = bufferSize / 2;
EGLSurface pbuffer = createPBuffer(bufferSize, bufferSize, EGL_NO_TEXTURE, EGL_NO_TEXTURE, 4,
static_cast<UINT>(D3D11_STANDARD_MULTISAMPLE_PATTERN));
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
// Apply the Pbuffer and clear it to magenta and verify
eglMakeCurrent(display, pbuffer, pbuffer, window->getContext());
ASSERT_EGL_SUCCESS();
glViewport(0, 0, static_cast<GLsizei>(bufferSize), static_cast<GLsizei>(bufferSize));
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(testpoint, testpoint, GLColor::magenta);
// Make current with fixture EGL to ensure the Surface can be released immediately.
getEGLWindow()->makeCurrent();
eglDestroySurface(display, pbuffer);
}
// Test creating a pbuffer from a multisampled d3d surface and drawing with a program.
TEST_P(D3DTextureTestMS, DrawProgram)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
constexpr size_t bufferSize = 32;
EGLSurface pbuffer = createPBuffer(bufferSize, bufferSize, EGL_NO_TEXTURE, EGL_NO_TEXTURE, 4,
static_cast<UINT>(D3D11_STANDARD_MULTISAMPLE_PATTERN));
ASSERT_EGL_SUCCESS();
ASSERT_NE(pbuffer, EGL_NO_SURFACE);
// Apply the Pbuffer and clear it to magenta
eglMakeCurrent(display, pbuffer, pbuffer, window->getContext());
ASSERT_EGL_SUCCESS();
glViewport(0, 0, static_cast<GLsizei>(bufferSize), static_cast<GLsizei>(bufferSize));
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
constexpr GLint testPoint = bufferSize / 2;
EXPECT_PIXEL_COLOR_EQ(testPoint, testPoint, GLColor::magenta);
// Apply the window surface
eglMakeCurrent(display, window->getSurface(), window->getSurface(), window->getContext());
ASSERT_EGL_SUCCESS();
glViewport(0, 0, getWindowWidth(), getWindowHeight());
ASSERT_EGL_SUCCESS();
// Draw a quad and verify that it is magenta
glUseProgram(mTextureProgramNoSampling);
EXPECT_GL_NO_ERROR();
drawQuad(mTextureProgramNoSampling, "position", 0.5f);
EXPECT_GL_NO_ERROR();
// Verify that magenta was drawn
EXPECT_PIXEL_COLOR_EQ(getWindowWidth() / 2, getWindowHeight() / 2, GLColor::magenta);
// Make current with fixture EGL to ensure the Surface can be released immediately.
getEGLWindow()->makeCurrent();
eglDestroySurface(display, pbuffer);
}
// Test for failure when creating a pbuffer from a multisampled d3d surface to bind to a texture.
TEST_P(D3DTextureTestMS, BindTexture)
{
constexpr size_t bufferSize = 32;
EGLSurface pbuffer = createPBuffer(bufferSize, bufferSize, EGL_TEXTURE_RGBA, EGL_TEXTURE_2D, 4,
static_cast<UINT>(D3D11_STANDARD_MULTISAMPLE_PATTERN));
EXPECT_EGL_ERROR(EGL_BAD_ATTRIBUTE);
EXPECT_EQ(pbuffer, nullptr);
}
// Verify that creating a pbuffer from a multisampled texture with a multisampled window will fail
// when the sample counts do not match.
TEST_P(D3DTextureTestMS, CheckSampleMismatch)
{
constexpr size_t bufferSize = 32;
EGLSurface pbuffer = createPBuffer(bufferSize, bufferSize, EGL_NO_TEXTURE, EGL_NO_TEXTURE, 2,
static_cast<UINT>(D3D11_STANDARD_MULTISAMPLE_PATTERN));
EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
EXPECT_EQ(pbuffer, nullptr);
}
// Test creating a pbuffer with a D3D texture and depth stencil bits in the EGL config creates keeps
// its depth stencil buffer
TEST_P(D3DTextureTestMS, DepthStencil)
{
EGLWindow *window = getEGLWindow();
EGLDisplay display = window->getDisplay();
const size_t bufferSize = 32;
EGLSurface pbuffer = createPBuffer(bufferSize, bufferSize, EGL_NO_TEXTURE, EGL_NO_TEXTURE, 4,
static_cast<UINT>(D3D11_STANDARD_MULTISAMPLE_PATTERN));
ASSERT_EGL_SUCCESS();
ASSERT_NE(EGL_NO_SURFACE, pbuffer);
// Apply the Pbuffer and clear it to purple and verify
eglMakeCurrent(display, pbuffer, pbuffer, window->getContext());
ASSERT_EGL_SUCCESS();
glViewport(0, 0, static_cast<GLsizei>(bufferSize), static_cast<GLsizei>(bufferSize));
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glClearDepthf(0.5f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glUseProgram(mTextureProgram);
glUniform1i(mTextureUniformLocation, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::green);
// Draw a quad that will fail the depth test and verify that the buffer is unchanged
drawQuad(mTextureProgram, "position", 1.0f);
EXPECT_PIXEL_COLOR_EQ(static_cast<GLint>(bufferSize) / 2, static_cast<GLint>(bufferSize) / 2,
GLColor::cyan);
// Draw a quad that will pass the depth test and verify that the buffer is green
drawQuad(mTextureProgram, "position", -1.0f);
EXPECT_PIXEL_COLOR_EQ(static_cast<GLint>(bufferSize) / 2, static_cast<GLint>(bufferSize) / 2,
GLColor::green);
// Make current with fixture EGL to ensure the Surface can be released immediately.
getEGLWindow()->makeCurrent();
eglDestroySurface(display, pbuffer);
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these
// tests should be run against.
ANGLE_INSTANTIATE_TEST(D3DTextureTest, ES2_D3D9(), ES2_D3D11(), ES2_OPENGL());
ANGLE_INSTANTIATE_TEST(D3DTextureTestMS, ES2_D3D11());
} // namespace