| 1 |
/* |
| 2 |
* Copyright © Microsoft Corporation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <gtest/gtest.h> |
| 25 |
|
| 26 |
#include <windows.h> |
| 27 |
#include <unknwn.h> |
| 28 |
#include <GL/gl.h> |
| 29 |
|
| 30 |
#include "util/os_misc.h" |
| 31 |
|
| 32 |
#undef GetMessage |
| 33 |
|
| 34 |
class window |
| 35 |
{ |
| 36 |
public: |
| 37 |
window(UINT width = 64, UINT height = 64); |
| 38 |
~window(); |
| 39 |
|
| 40 |
HWND get_hwnd() const { return _window; }; |
| 41 |
HDC get_hdc() const { return _hdc; }; |
| 42 |
bool valid() const { return _window && _hdc && _hglrc; } |
| 43 |
void show() { |
| 44 |
ShowWindow(_window, SW_SHOW); |
| 45 |
} |
| 46 |
|
| 47 |
void recreate_attribs(const int *attribList); |
| 48 |
|
| 49 |
private: |
| 50 |
HWND _window = nullptr; |
| 51 |
HDC _hdc = nullptr; |
| 52 |
HGLRC _hglrc = nullptr; |
| 53 |
}; |
| 54 |
|
| 55 |
window::window(uint32_t width, uint32_t height) |
| 56 |
{ |
| 57 |
_window = CreateWindowW( |
| 58 |
L"STATIC", |
| 59 |
L"OpenGLTestWindow", |
| 60 |
WS_OVERLAPPEDWINDOW, |
| 61 |
0, |
| 62 |
0, |
| 63 |
width, |
| 64 |
height, |
| 65 |
NULL, |
| 66 |
NULL, |
| 67 |
NULL, |
| 68 |
NULL |
| 69 |
); |
| 70 |
|
| 71 |
if (_window == nullptr) |
| 72 |
return; |
| 73 |
|
| 74 |
_hdc = ::GetDC(_window); |
| 75 |
|
| 76 |
os_set_option("D3D12_DEBUG", "singleton,debuglayer", true); |
| 77 |
|
| 78 |
PIXELFORMATDESCRIPTOR pfd = { |
| 79 |
sizeof(PIXELFORMATDESCRIPTOR), /* size */ |
| 80 |
1, /* version */ |
| 81 |
PFD_SUPPORT_OPENGL | |
| 82 |
PFD_DRAW_TO_WINDOW | |
| 83 |
PFD_DOUBLEBUFFER, /* support double-buffering */ |
| 84 |
PFD_TYPE_RGBA, /* color type */ |
| 85 |
32, /* preferred color depth */ |
| 86 |
0, 0, 0, 0, 0, 0, /* color bits (ignored) */ |
| 87 |
0, /* no alpha buffer */ |
| 88 |
0, /* alpha bits (ignored) */ |
| 89 |
0, /* no accumulation buffer */ |
| 90 |
0, 0, 0, 0, /* accum bits (ignored) */ |
| 91 |
32, /* depth buffer */ |
| 92 |
0, /* no stencil buffer */ |
| 93 |
0, /* no auxiliary buffers */ |
| 94 |
PFD_MAIN_PLANE, /* main layer */ |
| 95 |
0, /* reserved */ |
| 96 |
0, 0, 0, /* no layer, visible, damage masks */ |
| 97 |
}; |
| 98 |
int pixel_format = ChoosePixelFormat(_hdc, &pfd); |
| 99 |
if (pixel_format == 0) |
| 100 |
return; |
| 101 |
if (!SetPixelFormat(_hdc, pixel_format, &pfd)) |
| 102 |
return; |
| 103 |
|
| 104 |
_hglrc = wglCreateContext(_hdc); |
| 105 |
if (!_hglrc) |
| 106 |
return; |
| 107 |
|
| 108 |
wglMakeCurrent(_hdc, _hglrc); |
| 109 |
} |
| 110 |
|
| 111 |
void window::recreate_attribs(const int *attribs) |
| 112 |
{ |
| 113 |
using pwglCreateContextAttribsARB = HGLRC(WINAPI*)(HDC, HGLRC, const int *); |
| 114 |
auto wglCreateContextAttribsARB = (pwglCreateContextAttribsARB)wglGetProcAddress("wglCreateContextAttribsARB"); |
| 115 |
if (!wglCreateContextAttribsARB) |
| 116 |
GTEST_FAIL() << "failed to get wglCreateContextAttribsARB"; |
| 117 |
|
| 118 |
wglMakeCurrent(nullptr, nullptr); |
| 119 |
wglDeleteContext(_hglrc); |
| 120 |
_hglrc = wglCreateContextAttribsARB(_hdc, nullptr, attribs); |
| 121 |
if (!_hglrc) |
| 122 |
return; |
| 123 |
|
| 124 |
wglMakeCurrent(_hdc, _hglrc); |
| 125 |
} |
| 126 |
|
| 127 |
window::~window() |
| 128 |
{ |
| 129 |
if (_hglrc) { |
| 130 |
wglMakeCurrent(NULL, NULL); |
| 131 |
wglDeleteContext(_hglrc); |
| 132 |
} |
| 133 |
if (_hdc) |
| 134 |
ReleaseDC(_window, _hdc); |
| 135 |
if (_window) |
| 136 |
DestroyWindow(_window); |
| 137 |
} |
| 138 |
|
| 139 |
TEST(wgl, basic_create) |
| 140 |
{ |
| 141 |
window wnd; |
| 142 |
ASSERT_TRUE(wnd.valid()); |
| 143 |
|
| 144 |
const char *version = (const char *)glGetString(GL_VERSION); |
| 145 |
ASSERT_NE(strstr(version, "Mesa"), nullptr); |
| 146 |
} |
| 147 |
|
| 148 |
#ifdef GALLIUM_D3D12 |
| 149 |
/* Fixture for tests for the d3d12 backend. Will be skipped if |
| 150 |
* the environment isn't set up to run them. |
| 151 |
*/ |
| 152 |
#include <directx/d3d12.h> |
| 153 |
#include <dxguids/dxguids.h> |
| 154 |
#include <wrl/client.h> |
| 155 |
#include <memory> |
| 156 |
using Microsoft::WRL::ComPtr; |
| 157 |
|
| 158 |
class d3d12 : public ::testing::Test |
| 159 |
{ |
| 160 |
void SetUp() override; |
| 161 |
}; |
| 162 |
|
| 163 |
void d3d12::SetUp() |
| 164 |
{ |
| 165 |
window wnd; |
| 166 |
ASSERT_TRUE(wnd.valid()); |
| 167 |
|
| 168 |
const char *renderer = (const char *)glGetString(GL_RENDERER); |
| 169 |
if (!strstr(renderer, "D3D12")) |
| 170 |
GTEST_SKIP(); |
| 171 |
} |
| 172 |
|
| 173 |
static bool |
| 174 |
info_queue_has_swapchain(ID3D12DebugDevice *debug_device, ID3D12InfoQueue *info_queue) |
| 175 |
{ |
| 176 |
info_queue->PushEmptyStorageFilter(); |
| 177 |
|
| 178 |
debug_device->ReportLiveDeviceObjects(D3D12_RLDO_DETAIL); |
| 179 |
|
| 180 |
uint32_t num_messages = info_queue->GetNumStoredMessages(); |
| 181 |
for (uint32_t i = 0; i < num_messages; ++i) { |
| 182 |
SIZE_T message_size = 0; |
| 183 |
info_queue->GetMessage(i, nullptr, &message_size); |
| 184 |
EXPECT_GT(message_size, 0); |
| 185 |
|
| 186 |
std::unique_ptr<byte[]> message_bytes(new byte[message_size]); |
| 187 |
D3D12_MESSAGE *message = (D3D12_MESSAGE *)message_bytes.get(); |
| 188 |
info_queue->GetMessage(i, message, &message_size); |
| 189 |
|
| 190 |
if (strstr(message->pDescription, "SwapChain")) { |
| 191 |
info_queue->ClearStoredMessages(); |
| 192 |
info_queue->PopStorageFilter(); |
| 193 |
return true; |
| 194 |
} |
| 195 |
} |
| 196 |
info_queue->ClearStoredMessages(); |
| 197 |
info_queue->PopStorageFilter(); |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
TEST_F(d3d12, swapchain_cleanup) |
| 202 |
{ |
| 203 |
ComPtr<ID3D12InfoQueue> info_queue; |
| 204 |
ComPtr<ID3D12DebugDevice> debug_device; |
| 205 |
if (FAILED(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&info_queue))) || |
| 206 |
FAILED(info_queue.As(&debug_device))) |
| 207 |
GTEST_SKIP(); |
| 208 |
|
| 209 |
ASSERT_FALSE(info_queue_has_swapchain(debug_device.Get(), info_queue.Get())); |
| 210 |
|
| 211 |
{ |
| 212 |
window wnd; |
| 213 |
wnd.show(); |
| 214 |
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); |
| 215 |
glClear(GL_COLOR_BUFFER_BIT); |
| 216 |
SwapBuffers(wnd.get_hdc()); |
| 217 |
|
| 218 |
ASSERT_TRUE(info_queue_has_swapchain(debug_device.Get(), info_queue.Get())); |
| 219 |
} |
| 220 |
|
| 221 |
ASSERT_FALSE(info_queue_has_swapchain(debug_device.Get(), info_queue.Get())); |
| 222 |
} |
| 223 |
|
| 224 |
#define WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB 0x8256 |
| 225 |
#define WGL_LOSE_CONTEXT_ON_RESET_ARB 0x8252 |
| 226 |
using pglGetGraphicsResetStatusARB = GLenum(APIENTRY*)(); |
| 227 |
TEST_F(d3d12, context_reset) |
| 228 |
{ |
| 229 |
ComPtr<ID3D12Device5> device; |
| 230 |
if (FAILED(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device)))) |
| 231 |
GTEST_SKIP(); |
| 232 |
|
| 233 |
const int attribs[] = { WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB, WGL_LOSE_CONTEXT_ON_RESET_ARB, 0 }; |
| 234 |
|
| 235 |
{ |
| 236 |
window wnd; |
| 237 |
wnd.recreate_attribs(attribs); |
| 238 |
EXPECT_TRUE(wnd.valid()); |
| 239 |
|
| 240 |
wnd.show(); |
| 241 |
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); |
| 242 |
glClear(GL_COLOR_BUFFER_BIT); |
| 243 |
SwapBuffers(wnd.get_hdc()); |
| 244 |
|
| 245 |
auto glGetGraphicsResetStatusARB = (pglGetGraphicsResetStatusARB)wglGetProcAddress("glGetGraphicsResetStatusARB"); |
| 246 |
if (!glGetGraphicsResetStatusARB) |
| 247 |
GTEST_FAIL() << "Couldn't get reset function"; |
| 248 |
|
| 249 |
EXPECT_EQ(glGetGraphicsResetStatusARB(), NO_ERROR); |
| 250 |
|
| 251 |
device->RemoveDevice(); |
| 252 |
device.Reset(); |
| 253 |
|
| 254 |
EXPECT_NE(glGetGraphicsResetStatusARB(), NO_ERROR); |
| 255 |
} |
| 256 |
|
| 257 |
{ |
| 258 |
window wnd; |
| 259 |
EXPECT_TRUE(wnd.valid()); |
| 260 |
|
| 261 |
wnd.recreate_attribs(attribs); |
| 262 |
EXPECT_TRUE(wnd.valid()); |
| 263 |
|
| 264 |
wnd.show(); |
| 265 |
auto glGetGraphicsResetStatusARB = (pglGetGraphicsResetStatusARB)wglGetProcAddress("glGetGraphicsResetStatusARB"); |
| 266 |
EXPECT_EQ(glGetGraphicsResetStatusARB(), NO_ERROR); |
| 267 |
|
| 268 |
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); |
| 269 |
glClear(GL_COLOR_BUFFER_BIT); |
| 270 |
SwapBuffers(wnd.get_hdc()); |
| 271 |
} |
| 272 |
} |
| 273 |
#endif |