client/tests/graphics.c (33653B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <string.h> 5 #ifdef _WIN32 6 #include <windows.h> 7 #include <float.h> 8 #include <ddraw.h> 9 #include <d3d.h> 10 #include <stddef.h> 11 _Static_assert(sizeof(DDDEVICEIDENTIFIER2) == 1072, "guest device identifier size"); 12 _Static_assert(offsetof(DDDEVICEIDENTIFIER2, guidDeviceIdentifier) == 1048, "guest identifier offset"); 13 _Static_assert(offsetof(DDDEVICEIDENTIFIER2, dwWHQLLevel) == 1064, "guest certification offset"); 14 _Static_assert(sizeof(DDCAPS_DX7) == 380, "guest capabilities size"); 15 _Static_assert(sizeof(DDSURFACEDESC2) == 124, "guest mode description size"); 16 _Static_assert(offsetof(DDSURFACEDESC2, ddpfPixelFormat) == 72, "guest pixel format offset"); 17 _Static_assert(sizeof(D3DDEVICEDESC7) == 236, "guest Direct3D device description size"); 18 _Static_assert(offsetof(D3DDEVICEDESC7, deviceGUID) == 196, "guest device GUID offset"); 19 static uint32_t scratch[512], create_address, create_ex_address, enumerate_address; 20 #else 21 #include "linux_graphics.h" 22 #include "gl_renderer.h" 23 #include "guest_memory.h" 24 #include <SDL2/SDL.h> 25 #include <SDL2/SDL_opengl.h> 26 static uint32_t enumerate_address = 1; 27 static void enumeration_callback(void); 28 static void mode_callback(void); 29 static void device_callback(void); 30 recomp_func_t recomp_lookup(uint32_t address) 31 { 32 if (address == 0x11110004u) return mode_callback; 33 if (address == 0x11110008u) return device_callback; 34 assert(address == 0x11110000u); 35 return enumeration_callback; 36 } 37 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } 38 #endif 39 40 static uint32_t workspace(void) 41 { 42 #ifdef _WIN32 43 return (uintptr_t)scratch; 44 #else 45 return 0x50001000u; 46 #endif 47 } 48 49 static uint32_t *memory(uint32_t address) 50 { 51 #ifdef _WIN32 52 return (void *)(uintptr_t)address; 53 #else 54 return (void *)ADDR(address); 55 #endif 56 } 57 58 static uint32_t invoke(uint32_t address, unsigned count, uint32_t a, uint32_t b, uint32_t c) 59 { 60 #ifdef _WIN32 61 if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))(uintptr_t)address)(a); 62 if (count == 2) return ((uint32_t (WINAPI *)(uint32_t, uint32_t))(uintptr_t)address)(a, b); 63 return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t))(uintptr_t)address)(a, b, c); 64 #else 65 uint32_t stack = g_esp, caller = g_cur_func; 66 g_esp -= (count + 1) * 4; 67 MEM32(g_esp) = RECOMP_RETADDR; 68 MEM32(g_esp + 4) = a; 69 if (count >= 2) MEM32(g_esp + 8) = b; 70 if (count == 3) MEM32(g_esp + 12) = c; 71 g_ebx = 0x12345678u; g_esi = 0x23456789u; 72 g_edi = 0x3456789au; g_ebp = 0x456789abu; 73 recomp_func_t function; 74 if (!address) function = linux_graphics_lookup("DirectDrawCreate"); 75 else if (address == 1) function = linux_graphics_lookup("DirectDrawEnumerateExA"); 76 else function = linux_graphics_virtual(address); 77 assert(function); 78 function(); 79 assert(g_esp == stack && g_cur_func == caller); 80 assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); 81 assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); 82 return g_eax; 83 #endif 84 } 85 86 static uint32_t create(void) 87 { 88 #ifdef _WIN32 89 assert(!invoke(create_address, 3, 0, workspace(), 0)); 90 #else 91 assert(!invoke(0, 3, 0, workspace(), 0)); 92 #endif 93 return *memory(workspace()); 94 } 95 96 static uint32_t query(uint32_t object, const uint32_t *iid) 97 { 98 memcpy(memory(workspace() + 4), iid, 16); 99 *memory(workspace()) = 0x12345678u; 100 return invoke(*memory(*memory(object)), 3, object, workspace() + 4, workspace()); 101 } 102 103 static uint32_t ref(uint32_t object, unsigned slot) 104 { 105 return invoke(memory(*memory(object))[slot], 1, object, 0, 0); 106 } 107 108 static uint32_t create_ex(uint32_t iid, uint32_t output, uint32_t outer) 109 { 110 #ifdef _WIN32 111 return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)create_ex_address)(0, output, iid, outer); 112 #else 113 uint32_t stack = g_esp, caller = g_cur_func; 114 PUSH32(g_esp, outer); 115 PUSH32(g_esp, iid); 116 PUSH32(g_esp, output); 117 PUSH32(g_esp, 0); 118 PUSH32(g_esp, RECOMP_RETADDR); 119 linux_graphics_lookup("DirectDrawCreateEx")(); 120 assert(g_esp == stack && g_cur_func == caller); 121 return g_eax; 122 #endif 123 } 124 125 static void direct_draw_seven_creation_and_windowed_mode(void) 126 { 127 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 128 const uint32_t draw1[] = {0x6c14db80, 0x11cea733, 0x200021a5, 0x60e50baf}; 129 memcpy(memory(workspace() + 4), draw7, 16); 130 131 assert(create_ex(workspace() + 4, workspace(), 0) == 0); 132 uint32_t object = *memory(workspace()); 133 uint32_t result = invoke(memory(*memory(object))[20], 3, object, 0, 8); 134 135 assert(result == 0); 136 assert(!invoke(memory(*memory(object))[19], 1, object, 0, 0)); 137 assert(!invoke(memory(*memory(object))[26], 1, object, 0, 0)); 138 #ifdef _WIN32 139 unsigned control = _control87(0, 0); 140 #else 141 unsigned control = g_fpu_cw; 142 #endif 143 assert(!invoke(memory(*memory(object))[20], 3, object, 0, 0x808)); 144 assert(!invoke(memory(*memory(object))[26], 1, object, 0, 0)); 145 #ifdef _WIN32 146 assert(_control87(0, 0) == control); 147 #else 148 assert(g_fpu_cw == control); 149 #endif 150 #ifndef _WIN32 151 assert(!(SDL_GetWindowFlags(SDL_GL_GetCurrentWindow()) & SDL_WINDOW_FULLSCREEN)); 152 #endif 153 assert(query(object, draw7) == 0 && *memory(workspace()) == object); 154 assert(ref(object, 2) == 1); 155 assert(query(object, draw1) == 0 && *memory(workspace()) != object); 156 assert(ref(*memory(workspace()), 2) == 0); 157 assert(ref(object, 2) == 0); 158 memcpy(memory(workspace() + 4), draw7, 16); 159 assert(create_ex(workspace() + 4, 0, 0) == 0x80070057u); 160 result = create_ex(workspace() + 4, workspace(), 1); 161 assert(result == 0x80040110u); 162 memcpy(memory(workspace() + 4), draw1, 16); 163 assert(create_ex(workspace() + 4, workspace(), 0) == 0x80070057u); 164 } 165 166 static void vertical_blank_wait_preserves_guest_state(void) 167 { 168 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 169 memcpy(memory(workspace() + 4), draw7, 16); 170 assert(!create_ex(workspace() + 4, workspace(), 0)); 171 uint32_t object = *memory(workspace()); 172 assert(!invoke(memory(*memory(object))[20], 3, object, 0, 8)); 173 uint32_t wait = memory(*memory(object))[22]; 174 #ifndef _WIN32 175 SDL_DisplayMode mode; 176 assert(!SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(SDL_GL_GetCurrentWindow()), &mode)); 177 if (!mode.refresh_rate) mode.refresh_rate = 60; 178 uint64_t start = SDL_GetPerformanceCounter(); 179 #endif 180 181 for (unsigned i = 0; i < 12; ++i) assert(!invoke(wait, 3, object, 1, 0)); 182 183 #ifndef _WIN32 184 double elapsed = (double)(SDL_GetPerformanceCounter() - start) / SDL_GetPerformanceFrequency(); 185 assert(elapsed >= 10.0 / mode.refresh_rate && elapsed < 5); 186 fprintf(stderr, "Native refresh clock: 12 waits in %.3f seconds at %d Hz\n", elapsed, mode.refresh_rate); 187 #endif 188 assert(invoke(wait, 3, object, 0, 0) == 0x80070057u); 189 assert(!ref(object, 2)); 190 } 191 192 static void device_information_has_stable_identity_and_preserves_padding(void) 193 { 194 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 195 memcpy(memory(workspace() + 4), draw7, 16); 196 assert(create_ex(workspace() + 4, workspace(), 0) == 0); 197 uint32_t object = *memory(workspace()), output = workspace() + 64; 198 uint32_t method = memory(*memory(object))[27]; 199 memset(memory(output), 0xcc, 1072); 200 201 uint32_t result = invoke(method, 3, object, output, 0); 202 203 assert(result == 0 && *memory(output + 1068) == 0xccccccccu); 204 assert(*(char *)memory(output) && memchr(memory(output), 0, 512)); 205 assert(*(char *)memory(output + 512) && memchr(memory(output + 512), 0, 512)); 206 assert(*memory(output + 1048) || *memory(output + 1052) || *memory(output + 1056) || *memory(output + 1060)); 207 #ifndef _WIN32 208 assert(!strcmp((char *)memory(output), "ao-sdl-opengl")); 209 assert(!strcmp((char *)memory(output + 512), (const char *)glGetString(GL_RENDERER))); 210 for (unsigned offset = 1024; offset < 1048; offset += 4) assert(*memory(output + offset) == 0); 211 assert(*memory(output + 1064) == 0); /* No Windows driver certification. */ 212 #endif 213 unsigned char first[1072]; 214 memcpy(first, memory(output), sizeof(first)); 215 assert(invoke(method, 3, object, output, 1) == 0); 216 assert(!memcmp(first, memory(output), sizeof(first))); 217 assert(ref(object, 2) == 0); 218 assert(create_ex(workspace() + 4, workspace(), 0) == 0); 219 object = *memory(workspace()); 220 method = memory(*memory(object))[27]; 221 assert(invoke(method, 3, object, output, 0) == 0); 222 assert(!memcmp(first, memory(output), sizeof(first))); 223 assert(invoke(method, 3, object, output, 2) == 0x80070057u); 224 for (unsigned offset = 0; offset < 1064; offset += 4) assert(*memory(output + offset) == 0); 225 assert(*memory(output + 1068) == 0xccccccccu); 226 assert(invoke(method, 3, object, 0, 0) == 0x80070057u); 227 assert(ref(object, 2) == 0); 228 } 229 230 static void capability_queries_respect_structure_sizes(void) 231 { 232 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 233 uint32_t original = create(); 234 assert(query(original, draw7) == 0); 235 uint32_t object = *memory(workspace()), output = workspace() + 64; 236 uint32_t method = memory(*memory(object))[11]; 237 const unsigned sizes[] = {172, 316, 364, 380}; 238 239 for (unsigned i = 0; i < 4; ++i) { 240 memset(memory(output), 0xcc, 384); 241 *memory(output) = sizes[i]; 242 uint32_t result = invoke(method, 3, object, output, 0); 243 244 assert(result == 0 && *memory(output) == sizes[i]); 245 assert(*memory(output + sizes[i]) == 0xccccccccu); 246 #ifndef _WIN32 247 assert(*memory(output + 4) == 1); 248 assert(*memory(output + 8) == 0x80000u); 249 for (unsigned offset = 4; offset < sizes[i]; offset += 4) 250 if (offset == 132 || offset == 364) assert(*memory(output + offset) == 0x401008); 251 else if (offset != 4 && offset != 8) assert(*memory(output + offset) == 0); 252 #endif 253 } 254 assert(invoke(method, 3, object, 0, output) == 0); 255 #ifndef _WIN32 256 for (unsigned offset = 4; offset < 380; offset += 4) assert(*memory(output + offset) == 0); 257 #endif 258 *memory(output) = 4; 259 *memory(output + 4) = 0xccccccccu; 260 assert(invoke(method, 3, object, output, 0) == 0x80070057u); 261 assert(*memory(output) == 4 && *memory(output + 4) == 0xccccccccu); 262 assert(invoke(method, 3, object, 0, 0) == 0x80070057u); 263 assert(ref(object, 2) == 0 && ref(original, 2) == 0); 264 } 265 266 static unsigned mode_count, mode_flags, cancel_modes; 267 static uint32_t seen_modes[1024][4]; 268 static int inspect_mode(uint32_t description, uint32_t data) 269 { 270 assert(data == 0x76543210u && *memory(description) == 124); 271 assert(*memory(description + 4) == 0x4100eu); 272 uint32_t width = *memory(description + 12), height = *memory(description + 8); 273 uint32_t bits = *memory(description + 84), refresh = *memory(description + 24); 274 assert(width && height && bits && *memory(description + 72) == 32); 275 assert(*memory(description + 76) & 0x40); 276 if (!(*memory(description + 76) & 0x20)) { 277 uint32_t red = *memory(description + 88), green = *memory(description + 92), blue = *memory(description + 96); 278 assert(red && green && blue && !(red & green) && !(green & blue) && !(red & blue)); 279 } 280 assert(*memory(description + 16) >= width * bits / 8); 281 if (!(mode_flags & 1)) assert(refresh == 0); 282 for (unsigned i = 0; i < mode_count; ++i) 283 assert(seen_modes[i][0] != width || seen_modes[i][1] != height || seen_modes[i][2] != bits || seen_modes[i][3] != refresh); 284 #ifndef _WIN32 285 int display = SDL_GetWindowDisplayIndex(SDL_GL_GetCurrentWindow()), found = 0; 286 for (int i = 0; i < SDL_GetNumDisplayModes(display); ++i) { 287 SDL_DisplayMode mode; 288 assert(SDL_GetDisplayMode(display, i, &mode) == 0); 289 if (mode.w == width && mode.h == height && SDL_BYTESPERPIXEL(mode.format) * 8 == bits && 290 (!(mode_flags & 1) || mode.refresh_rate == refresh)) found = 1; 291 } 292 assert(found); /* Every reported mode must exist in the native display API. */ 293 #endif 294 assert(mode_count < 1024); 295 seen_modes[mode_count][0] = width; seen_modes[mode_count][1] = height; 296 seen_modes[mode_count][2] = bits; seen_modes[mode_count][3] = refresh; 297 ++mode_count; 298 uint32_t object = create(); 299 assert(ref(object, 2) == 0); 300 return !cancel_modes; 301 } 302 303 #ifdef _WIN32 304 static HRESULT WINAPI mode_callback(DDSURFACEDESC2 *description, void *data) 305 { 306 return inspect_mode((uintptr_t)description, (uintptr_t)data); 307 } 308 #else 309 static void mode_callback(void) 310 { 311 g_cur_func = 0x11110004u; 312 g_eax = inspect_mode(MEM32(g_esp + 4), MEM32(g_esp + 8)); 313 g_esp += 12; 314 } 315 #endif 316 317 static uint32_t enumerate_modes(uint32_t object, uint32_t callback) 318 { 319 uint32_t method = memory(*memory(object))[8]; 320 #ifdef _WIN32 321 return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)method) 322 (object, mode_flags, 0, 0x76543210u, callback); 323 #else 324 uint32_t stack = g_esp, caller = g_cur_func; 325 g_ebx = 0x12345678u; g_esi = 0x23456789u; 326 g_edi = 0x3456789au; g_ebp = 0x456789abu; 327 PUSH32(g_esp, callback); 328 PUSH32(g_esp, 0x76543210u); 329 PUSH32(g_esp, 0); 330 PUSH32(g_esp, mode_flags); 331 PUSH32(g_esp, object); 332 PUSH32(g_esp, RECOMP_RETADDR); 333 linux_graphics_virtual(method)(); 334 assert(g_esp == stack && g_cur_func == caller); 335 assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); 336 assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); 337 return g_eax; 338 #endif 339 } 340 341 static void display_modes_describe_the_native_monitor(void) 342 { 343 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 344 memcpy(memory(workspace() + 4), draw7, 16); 345 assert(create_ex(workspace() + 4, workspace(), 0) == 0); 346 uint32_t object = *memory(workspace()); 347 #ifdef _WIN32 348 uint32_t callback = (uintptr_t)mode_callback; 349 #else 350 uint32_t callback = 0x11110004u; 351 #endif 352 mode_flags = 0; mode_count = 0; cancel_modes = 0; 353 354 uint32_t result = enumerate_modes(object, callback); 355 unsigned resolutions = mode_count; 356 mode_flags = 1; mode_count = 0; 357 uint32_t refresh_result = enumerate_modes(object, callback); 358 359 assert(result == 0 && refresh_result == 0 && resolutions > 0 && mode_count >= resolutions); 360 fprintf(stderr, "Display modes: %u resolutions, %u refresh variants\n", resolutions, mode_count); 361 #ifndef _WIN32 362 /* Follow the documented cancel contract. This Windows driver continues 363 * callbacks after DDENUMRET_CANCEL, so cancellation is tested natively. */ 364 mode_count = 0; cancel_modes = 1; 365 assert(enumerate_modes(object, callback) == 0 && mode_count == 1); 366 #endif 367 mode_flags = 4; mode_count = 0; 368 assert(enumerate_modes(object, callback) == 0x80070057u && mode_count == 0); 369 mode_flags = 0; 370 assert(enumerate_modes(object, 0) == 0x80070057u); 371 assert(ref(object, 2) == 0); 372 } 373 374 static void current_display_mode_preserves_buffer_bounds(void) 375 { 376 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 377 memcpy(memory(workspace() + 4), draw7, 16); 378 assert(!create_ex(workspace() + 4, workspace(), 0)); 379 uint32_t object = *memory(workspace()), output = workspace() + 128; 380 uint32_t method = memory(*memory(object))[12]; 381 #ifdef _WIN32 382 DEVMODEA mode = {0}; 383 mode.dmSize = sizeof(mode); 384 assert(EnumDisplaySettingsA(NULL, ENUM_CURRENT_SETTINGS, &mode)); 385 uint32_t width = mode.dmPelsWidth, height = mode.dmPelsHeight, refresh = mode.dmDisplayFrequency; 386 assert(mode.dmBitsPerPel == 32); 387 #else 388 SDL_DisplayMode mode; 389 assert(!SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(SDL_GL_GetCurrentWindow()), &mode)); 390 uint32_t width = mode.w, height = mode.h, refresh = mode.refresh_rate; 391 assert(SDL_BYTESPERPIXEL(mode.format) == 4); 392 #endif 393 const uint32_t sizes[] = {0, 4, 108, 124, 128}; 394 for (unsigned i = 0; i < 5; ++i) { 395 memset(memory(output), 0xa5, 132); 396 *memory(output) = sizes[i]; 397 uint32_t expected[33]; 398 memcpy(expected, memory(output), sizeof(expected)); 399 if (sizes[i] == 108 || sizes[i] == 124) { 400 memset(expected, 0, sizes[i]); 401 expected[0] = 124; expected[1] = 0x4100e; 402 expected[2] = height; expected[3] = width; expected[4] = width * 4; 403 expected[6] = refresh; expected[18] = 32; expected[19] = 0x40; 404 expected[21] = 32; expected[22] = 0xff0000; expected[23] = 0xff00; expected[24] = 0xff; 405 } 406 407 uint32_t result = invoke(method, 2, object, output, 0); 408 409 assert(result == ((sizes[i] == 108 || sizes[i] == 124) ? 0 : 0x80070057u)); 410 assert(!memcmp(memory(output), expected, sizeof(expected))); 411 } 412 assert(invoke(method, 2, object, 0, 0) == 0x80070057u); 413 assert(!ref(object, 2)); 414 } 415 416 static unsigned callback_count, continue_enumeration; 417 static int inspect_adapter(uint32_t guid, uint32_t description, uint32_t name, uint32_t data, uint32_t monitor) 418 { 419 assert(!guid && !monitor && data == 0x12345678u); 420 assert(*(const char *)memory(description) && *(const char *)memory(name)); 421 #ifndef _WIN32 422 assert(!strcmp((const char *)memory(description), (const char *)glGetString(GL_RENDERER))); 423 assert(!strcmp((const char *)memory(name), SDL_GetDisplayName(SDL_GetWindowDisplayIndex(SDL_GL_GetCurrentWindow())))); 424 #endif 425 ++callback_count; 426 uint32_t object = create(); 427 assert(ref(object, 2) == 0); /* Reentrant calls must leave the callback stack intact. */ 428 return continue_enumeration; 429 } 430 431 #ifdef _WIN32 432 static BOOL WINAPI enumeration_callback(GUID *guid, char *description, char *name, void *data, HMONITOR monitor) 433 { 434 return inspect_adapter((uintptr_t)guid, (uintptr_t)description, (uintptr_t)name, (uintptr_t)data, (uintptr_t)monitor); 435 } 436 #else 437 static void enumeration_callback(void) 438 { 439 g_cur_func = 0x11110000u; 440 g_eax = inspect_adapter(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16), MEM32(g_esp + 20)); 441 g_esp += 24; 442 } 443 #endif 444 445 static void adapter_enumeration_invokes_guest_callbacks(void) 446 { 447 #ifdef _WIN32 448 uint32_t callback = (uintptr_t)enumeration_callback; 449 #else 450 uint32_t callback = 0x11110000u; 451 #endif 452 callback_count = 0; 453 continue_enumeration = 1; 454 455 uint32_t result = invoke(enumerate_address, 3, callback, 0x12345678u, 0); 456 457 assert(result == 0 && callback_count == 1); 458 callback_count = 0; 459 continue_enumeration = 0; 460 assert(invoke(enumerate_address, 3, callback, 0x12345678u, 7) == 0 && callback_count == 1); 461 callback_count = 0; 462 assert(invoke(enumerate_address, 3, callback, 0x12345678u, 8) == 0x80070057u && callback_count == 0); 463 assert(invoke(enumerate_address, 3, 0, 0, 0) == 0x80070057u); 464 } 465 466 static void independent_interface_counts_share_object_lifetime(void) 467 { 468 uint32_t original = create(); 469 uint32_t original_vtable = *memory(original); 470 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 471 const uint32_t draw1[] = {0x6c14db80, 0x11cea733, 0x200021a5, 0x60e50baf}; 472 const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; 473 const uint32_t unsupported[] = {0x12345678, 0, 0, 0}; 474 475 assert(query(original, draw7) == 0); 476 uint32_t version7 = *memory(workspace()); 477 assert(version7 != original && *memory(version7) != original_vtable); 478 assert(*memory(original) == original_vtable); 479 assert(ref(original, 1) == 2 && ref(original, 2) == 1); 480 assert(ref(version7, 1) == 2 && ref(version7, 2) == 1); 481 assert(query(version7, unknown) == 0 && *memory(workspace()) == original); 482 assert(ref(original, 2) == 1); 483 assert(query(version7, draw1) == 0 && *memory(workspace()) == original); 484 assert(ref(original, 2) == 1); 485 assert(query(version7, unsupported) == 0x80004002u && *memory(workspace()) == 0); 486 assert(ref(original, 2) == 0); 487 assert(query(version7, unknown) == 0); 488 uint32_t identity = *memory(workspace()); 489 490 assert(query(version7, unknown) == 0 && *memory(workspace()) == identity); 491 assert(ref(identity, 2) == 1 && ref(identity, 2) == 0); 492 assert(ref(version7, 2) == 0); 493 for (unsigned i = 0; i < 32; ++i) { 494 uint32_t first = create(), second = create(); 495 assert(first != second && ref(first, 2) == 0 && ref(second, 2) == 0); 496 } 497 } 498 499 static void direct3d_shares_the_directdraw_seven_reference_count(void) 500 { 501 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 502 const uint32_t d3d7[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9}; 503 const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; 504 memcpy(memory(workspace() + 4), draw7, 16); 505 assert(create_ex(workspace() + 4, workspace(), 0) == 0); 506 uint32_t draw = *memory(workspace()); 507 508 assert(query(draw, d3d7) == 0); 509 uint32_t d3d = *memory(workspace()); 510 511 assert(draw != d3d && *memory(draw) != *memory(d3d)); 512 assert(ref(draw, 1) == 3 && ref(draw, 2) == 2); 513 assert(ref(d3d, 1) == 3 && ref(d3d, 2) == 2); 514 assert(query(d3d, d3d7) == 0 && *memory(workspace()) == d3d); 515 assert(ref(d3d, 2) == 2); 516 assert(query(d3d, unknown) == 0); 517 uint32_t identity = *memory(workspace()); 518 assert(identity != d3d && identity != draw); 519 assert(ref(identity, 2) == 0); 520 assert(ref(draw, 2) == 1); 521 assert(query(d3d, draw7) == 0 && *memory(workspace()) == draw); 522 assert(ref(draw, 2) == 1 && ref(d3d, 2) == 0); 523 } 524 525 static unsigned device_count; 526 static int inspect_device(uint32_t description, uint32_t name, uint32_t caps, uint32_t data) 527 { 528 assert(data == 0x12345678u); 529 assert(*(char *)memory(description) && *(char *)memory(name)); 530 assert(*memory(caps + 4) == 56 && *memory(caps + 60) == 56); 531 assert(*memory(caps + 116) & 0x100); /* 32-bit color */ 532 #ifndef _WIN32 533 assert(*memory(caps + 120) == 0x600); /* Native 16/24-bit depth buffers. */ 534 assert(!strcmp((char *)memory(description), (const char *)glGetString(GL_RENDERER))); 535 assert(!strcmp((char *)memory(name), "Native OpenGL")); 536 assert(*memory(caps) == 0x80200u); 537 assert(*memory(caps + 64) == 0x10 && *memory(caps + 68) == 0x10); 538 assert(*memory(caps + 72) == 8 && *memory(caps + 88) == 8); 539 assert(*memory(caps + 184) == 0x00020002); /* Two textures and two blend stages. */ 540 assert(*memory(caps + 92) == 0x45 && *memory(caps + 96) == 0x03010317); 541 assert(*memory(caps + 104) == 0x15 && *memory(caps + 180) == 0x8004b); 542 const uint32_t hal[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; 543 assert(!memcmp(memory(caps + 196), hal, 16)); 544 #endif 545 ++device_count; 546 uint32_t object = create(); 547 assert(ref(object, 2) == 0); 548 return 1; 549 } 550 551 #ifdef _WIN32 552 static HRESULT WINAPI device_callback(char *description, char *name, D3DDEVICEDESC7 *caps, void *data) 553 { 554 return inspect_device((uintptr_t)description, (uintptr_t)name, (uintptr_t)caps, (uintptr_t)data); 555 } 556 #else 557 static void device_callback(void) 558 { 559 g_cur_func = 0x11110008u; 560 g_eax = inspect_device(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16)); 561 g_esp += 20; 562 } 563 #endif 564 565 static void direct3d_enumerates_the_native_renderer(void) 566 { 567 const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; 568 const uint32_t d3d7[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9}; 569 memcpy(memory(workspace() + 4), draw7, 16); 570 assert(create_ex(workspace() + 4, workspace(), 0) == 0); 571 uint32_t draw = *memory(workspace()); 572 assert(query(draw, d3d7) == 0); 573 uint32_t d3d = *memory(workspace()), method = memory(*memory(d3d))[3]; 574 #ifdef _WIN32 575 uint32_t callback = (uintptr_t)device_callback; 576 #else 577 uint32_t callback = 0x11110008u; 578 #endif 579 device_count = 0; 580 581 uint32_t result = invoke(method, 3, d3d, callback, 0x12345678u); 582 583 assert(result == 0 && device_count > 0); 584 #ifndef _WIN32 585 assert(device_count == 1); 586 #endif 587 assert(invoke(method, 3, d3d, 0, 0) == 0x80070057u); 588 assert(ref(draw, 2) == 1 && ref(d3d, 2) == 0); 589 } 590 591 #ifndef _WIN32 592 static void attached_depth_is_shared_and_retained(void) 593 { 594 struct gl_target *first = gl_target_create(32, 32, 1), *second = gl_target_create(32, 32, 1); 595 struct gl_depth *depth = gl_depth_create(32, 32, 16); 596 assert(first && second && depth); 597 gl_target_set_depth(first, depth); 598 gl_target_set_depth(second, depth); 599 gl_depth_destroy(depth); 600 gl_target_clear(first, 0xff000000, 1.0f); 601 gl_target_clear(second, 0xff0000ff, 1.0f); 602 struct gl_vertex vertices[] = { 603 {{-0.75f, -0.75f, 0.25f, 1}, {1, 1, 1, 1}}, 604 {{0.75f, -0.75f, 0.25f, 1}, {1, 1, 1, 1}}, 605 {{0, 0.75f, 0.25f, 1}, {1, 1, 1, 1}} 606 }; 607 const uint16_t indices[] = {0, 1, 2}; 608 unsigned char pixels[32 * 32 * 4]; 609 610 gl_target_draw(first, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32}); 611 gl_target_set_depth(first, NULL); 612 gl_target_destroy(first); 613 for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = 0.75f; 614 gl_target_draw(second, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32}); 615 gl_target_read(second, pixels); 616 617 unsigned center = (16 * 32 + 16) * 4; 618 assert(pixels[center] == 0 && pixels[center + 1] == 0 && pixels[center + 2] == 255); 619 gl_target_set_depth(second, NULL); 620 gl_target_draw(second, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32}); 621 gl_target_read(second, pixels); 622 assert(pixels[center] == 255 && pixels[center + 1] == 255 && pixels[center + 2] == 255); 623 gl_target_destroy(second); 624 } 625 626 static void uploads_preserve_depth_and_top_down_pixels(void) 627 { 628 struct gl_target *target = gl_target_create(2, 2, 1); 629 assert(target); 630 gl_target_clear(target, 0, 0.0f); 631 const unsigned char input[] = {10, 20, 30, 40, 50, 60, 70, 80, 632 90, 100, 110, 120, 130, 140, 150, 160}; 633 struct gl_vertex vertices[] = { 634 {{-1, -1, 0.5f, 1}, {1, 1, 1, 1}}, 635 {{3, -1, 0.5f, 1}, {1, 1, 1, 1}}, 636 {{-1, 3, 0.5f, 1}, {1, 1, 1, 1}} 637 }; 638 const uint16_t indices[] = {0, 1, 2}; 639 unsigned char output[16]; 640 641 gl_target_write(target, input); 642 gl_target_draw(target, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 2, 2}); 643 gl_target_read(target, output); 644 645 assert(!memcmp(input, output, sizeof(input))); 646 gl_target_destroy(target); 647 } 648 649 static void indexed_triangles_preserve_depth_and_target_contents(void) 650 { 651 struct gl_target *first = gl_target_create(32, 32, 1), *second = gl_target_create(32, 32, 1); 652 assert(first && second); 653 struct gl_vertex vertices[] = { 654 {{-0.75f, -0.75f, 0.25f, 1}, {0, 1, 0, 1}}, 655 {{0.75f, -0.75f, 0.25f, 1}, {0, 1, 0, 1}}, 656 {{0, 0.75f, 0.25f, 1}, {0, 1, 0, 1}} 657 }; 658 const uint16_t indices[] = {0, 1, 2}; 659 unsigned char pixels[32 * 32 * 4]; 660 661 gl_target_clear(first, 0xff102030u, 1.0f); 662 gl_target_draw(first, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32}); 663 gl_target_clear(second, 0xff405060u, 1.0f); 664 for (unsigned i = 0; i < 3; ++i) { 665 vertices[i].position[2] = 0.75f; 666 vertices[i].color[0] = 1; 667 vertices[i].color[1] = 0; 668 } 669 gl_target_draw(first, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32}); 670 gl_target_read(first, pixels); 671 672 const unsigned center = (16 * 32 + 16) * 4; 673 assert(pixels[center] == 0 && pixels[center + 1] == 255 && pixels[center + 2] == 0 && pixels[center + 3] == 255); 674 assert(pixels[0] == 16 && pixels[1] == 32 && pixels[2] == 48 && pixels[3] == 255); 675 /* Top-down readback places the narrow apex above the broad triangle base. */ 676 assert(pixels[(8 * 32 + 8) * 4] == 16); 677 assert(pixels[(24 * 32 + 8) * 4 + 1] == 255); 678 gl_target_read(second, pixels); 679 assert(pixels[center] == 64 && pixels[center + 1] == 80 && pixels[center + 2] == 96 && pixels[center + 3] == 255); 680 gl_target_clear(first, 0xff102030u, 0.0f); 681 gl_target_draw(first, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32}); 682 gl_target_read(first, pixels); 683 assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48); 684 gl_target_clear(first, 0xff102030u, 1.0f); 685 for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = -0.25f; 686 gl_target_draw(first, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32}); 687 gl_target_read(first, pixels); 688 assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48); 689 struct gl_vertex gradient[] = { 690 {{-1, -1, 0.25f, 1}, {1, 0, 0, 1}}, 691 {{1, -1, 0.25f, 1}, {0, 1, 0, 1}}, 692 {{-1, 1, 0.25f, 1}, {0, 0, 1, 1}} 693 }; 694 const uint16_t reversed[] = {0, 2, 1}; 695 gl_target_draw(first, 4, gradient, 3, reversed, 3, (const int32_t[]){0, 0, 32, 32}); 696 gl_target_read(first, pixels); 697 unsigned sample = (24 * 32 + 8) * 4; 698 assert((pixels[sample] == 127 || pixels[sample] == 128) && pixels[sample + 1] == 68 && pixels[sample + 2] == 60); 699 for (unsigned i = 0; i < 4; ++i) gradient[1].position[i] *= 2; 700 gl_target_draw(first, 4, gradient, 3, reversed, 3, (const int32_t[]){0, 0, 32, 32}); 701 gl_target_read(first, pixels); 702 assert(pixels[sample] == 147 && pixels[sample + 1] == 39 && pixels[sample + 2] == 69); 703 gl_target_clear(first, 0xff102030u, 1.0f); 704 for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = 1.25f; 705 gl_target_draw(first, 4, vertices, 3, indices, 3, (const int32_t[]){0, 0, 32, 32}); 706 gl_target_read(first, pixels); 707 assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48); 708 gl_target_destroy(first); 709 gl_target_destroy(second); 710 fprintf(stderr, "Native renderer: indexed triangles, depth, clipping, independent targets and top-down readback passed\n"); 711 } 712 713 static void presentation_flips_guest_rows_into_window_coordinates(void) 714 { 715 struct gl_target *target = gl_target_create(4, 4, 1); 716 unsigned char input[64] = {0}, output[32 * 32 * 4]; 717 const int32_t rectangle[] = {1, 1, 3, 3}; 718 input[(1 * 4 + 1) * 4] = 255; 719 input[(1 * 4 + 2) * 4 + 1] = 255; 720 input[(2 * 4 + 1) * 4 + 2] = 255; 721 input[(2 * 4 + 2) * 4] = 255; 722 input[(2 * 4 + 2) * 4 + 1] = 255; 723 gl_target_write(target, input); 724 725 gl_target_present(target, rectangle, 32, 32); 726 glReadBuffer(GL_BACK); 727 glReadPixels(0, 0, 32, 32, GL_RGBA, GL_UNSIGNED_BYTE, output); 728 729 assert(glGetError() == GL_NO_ERROR); 730 for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) { 731 unsigned source = ((y < 16 ? 2 : 1) * 4 + (x < 16 ? 1 : 2)) * 4; 732 assert(!memcmp(output + (y * 32 + x) * 4, input + source, 3)); 733 } 734 gl_target_destroy(target); 735 } 736 737 static void eviction_releases_gpu_storage_and_restores_pixels(void) 738 { 739 struct gl_target *texture = gl_target_create(2, 2, 0); 740 const unsigned char input[] = {1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16}; 741 unsigned char output[16]; 742 gl_target_set_depth(texture, NULL); 743 gl_target_write(texture, input); 744 GLint name; 745 glGetIntegerv(GL_TEXTURE_BINDING_2D, &name); 746 assert(glIsTexture(name)); 747 748 gl_target_evict(texture, NULL); 749 gl_target_evict(texture, NULL); 750 751 assert(!glIsTexture(name)); 752 gl_target_read(texture, output); 753 assert(!memcmp(output, input, 16)); 754 struct gl_texture_state state = {.image = texture}; 755 gl_renderer_set_texture(0, &state); 756 glGetIntegerv(GL_TEXTURE_BINDING_2D, &name); 757 assert(glIsTexture(name)); 758 gl_target_evict(texture, input); 759 gl_target_destroy(texture); 760 assert(!glIsTexture(name) && glGetError() == GL_NO_ERROR); 761 } 762 763 static void native_context_renders_and_reads_pixels(void) 764 { 765 assert(SDL_GL_GetCurrentContext()); 766 unsigned char pixel[4] = {0}; 767 glClearColor(0.25f, 0.5f, 0.75f, 1.0f); 768 769 glClear(GL_COLOR_BUFFER_BIT); 770 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); 771 772 assert(glGetError() == GL_NO_ERROR); 773 fprintf(stderr, "Native graphics pixel: %u %u %u %u\n", pixel[0], pixel[1], pixel[2], pixel[3]); 774 /* Half intensity lies between two representable 8-bit channel values. */ 775 assert(pixel[0] == 64 && (pixel[1] == 127 || pixel[1] == 128) && pixel[2] == 191 && pixel[3] == 255); 776 } 777 #endif 778 779 int main(void) 780 { 781 #ifdef _WIN32 782 create_address = (uintptr_t)GetProcAddress(LoadLibraryA("ddraw.dll"), "DirectDrawCreate"); 783 enumerate_address = (uintptr_t)GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawEnumerateExA"); 784 create_ex_address = (uintptr_t)GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"); 785 assert(create_address && create_ex_address && enumerate_address); 786 #else 787 assert(guest_map("graphics test", 0x50000000u, 0x10000u)); 788 g_esp = 0x5000ff00u; 789 #endif 790 adapter_enumeration_invokes_guest_callbacks(); 791 direct_draw_seven_creation_and_windowed_mode(); 792 vertical_blank_wait_preserves_guest_state(); 793 device_information_has_stable_identity_and_preserves_padding(); 794 capability_queries_respect_structure_sizes(); 795 display_modes_describe_the_native_monitor(); 796 current_display_mode_preserves_buffer_bounds(); 797 independent_interface_counts_share_object_lifetime(); 798 direct3d_shares_the_directdraw_seven_reference_count(); 799 direct3d_enumerates_the_native_renderer(); 800 #ifndef _WIN32 801 indexed_triangles_preserve_depth_and_target_contents(); 802 uploads_preserve_depth_and_top_down_pixels(); 803 attached_depth_is_shared_and_retained(); 804 presentation_flips_guest_rows_into_window_coordinates(); 805 eviction_releases_gpu_storage_and_restores_pixels(); 806 native_context_renders_and_reads_pixels(); 807 #endif 808 puts("DirectDraw: adapter/mode callbacks, reentrancy, capabilities, DirectDrawCreateEx, windowed mode, device information, DirectDraw/Direct3D interface lifetime and ABI passed"); 809 return 0; 810 }