25 D3DFMT_X8R8G8B8, 32, FALSE,
26 D3DFMT_R5G6B5, 16, FALSE,
27 D3DFMT_X1R5G5B5, 16, FALSE,
29 D3DFMT_A2R10G10B10, 32, TRUE,
32 D3DFMT_UNKNOWN, 0, FALSE,
35 typedef BOOL (WINAPI *GlobalMemoryStatusExType) (LPMEMORYSTATUSEX lpBuffer);
37 static int d3d_format_to_bits_per_pixel (D3DFORMAT d3d_format) {
43 while (display_format_array [format_index].d3d_format != D3DFMT_UNKNOWN) {
44 if (d3d_format == display_format_array [format_index].d3d_format) {
45 bits_per_pixel = display_format_array [format_index].bits_per_pixel;
52 return bits_per_pixel;
55 static DWORD _GetLastError (
char *message_prefix) {
60 error = GetLastError ( );
62 FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM,
63 nullptr, error, MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ),
64 (LPTSTR)&ptr,0,
nullptr)) {
65 std::cout <<
"ERROR: "<< message_prefix <<
" result = " << (
char*) ptr <<
"\n";
72 static LRESULT CALLBACK window_procedure (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
73 return DefWindowProc(hwnd, msg, wparam, lparam);
76 static DWORD print_GetLastError (
char *message_prefix)
82 error = GetLastError ( );
83 if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
84 FORMAT_MESSAGE_FROM_SYSTEM,
87 MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ),
91 std::cout <<
"ERROR: "<< message_prefix <<
" result = " << (
char*) ptr <<
"\n";
103 DisplayInformation::DetectionState state;
104 int get_adapter_display_mode_state;
105 int get_device_caps_state;
107 GraphicsStateGuardian::ShaderModel shader_model;
112 int minimum_bits_per_pixel;
113 int maximum_bits_per_pixel;
120 int window_bits_per_pixel;
121 int total_display_modes;
124 uint64_t physical_memory;
125 uint64_t available_physical_memory;
142 window_bits_per_pixel = 0;
143 total_display_modes = 0;
144 display_mode_array =
nullptr;
146 minimum_width = display_search_parameters._minimum_width;
147 minimum_height = display_search_parameters._minimum_height;
148 maximum_width = display_search_parameters._maximum_width;
149 maximum_height = display_search_parameters._maximum_height;
150 minimum_bits_per_pixel = display_search_parameters._minimum_bits_per_pixel;
151 maximum_bits_per_pixel = display_search_parameters._maximum_bits_per_pixel;
153 shader_model = GraphicsStateGuardian::SM_00;
157 state = DisplayInformation::DS_unknown;
158 get_adapter_display_mode_state =
false;
159 get_device_caps_state =
false;
162 available_physical_memory = 0;
177 DIRECT_3D_CREATE Direct3DCreate;
179 d3d_dll = LoadLibrary (d3d_dll_name);
181 Direct3DCreate = (DIRECT_3D_CREATE) GetProcAddress (d3d_dll, direct_3d_create_function_name);
182 if (Direct3DCreate) {
185 direct_3d = Direct3DCreate (D3D_SDK_VERSION);
186 if (direct_3d !=
nullptr) {
189 D3DDEVTYPE device_type;
190 D3DDISPLAYMODE current_d3d_display_mode;
191 D3DADAPTER_IDENTIFIER d3d_adapter_identifier;
194 adapter = D3DADAPTER_DEFAULT;
195 device_type = D3DDEVTYPE_HAL;
198 if (direct_3d -> GetAdapterDisplayMode (adapter, ¤t_d3d_display_mode) == D3D_OK) {
200 printf (
"current mode w = %d h = %d r = %d f = %d \n",
201 current_d3d_display_mode.Width,
202 current_d3d_display_mode.Height,
203 current_d3d_display_mode.RefreshRate,
204 current_d3d_display_mode.Format);
207 window_width = current_d3d_display_mode.Width;
208 window_height = current_d3d_display_mode.Height;
209 window_bits_per_pixel = d3d_format_to_bits_per_pixel (current_d3d_display_mode.Format);
211 get_adapter_display_mode_state =
true;
214 get_adapter_display_mode_state =
false;
218 if (direct_3d -> GetAdapterIdentifier (adapter, flags, &d3d_adapter_identifier) == D3D_OK) {
220 d3d_adapter_identifier.Driver;
221 d3d_adapter_identifier.Description;
222 d3d_adapter_identifier.DeviceName;
223 d3d_adapter_identifier.DriverVersion;
224 d3d_adapter_identifier.VendorId;
225 d3d_adapter_identifier.DeviceId;
226 d3d_adapter_identifier.SubSysId;
227 d3d_adapter_identifier.Revision;
228 d3d_adapter_identifier.DeviceIdentifier;
229 d3d_adapter_identifier.WHQLLevel;
232 printf (
"Driver: %s\n", d3d_adapter_identifier.Driver);
233 printf (
"Description: %s\n", d3d_adapter_identifier.Description);
236 char system_directory [MAX_PATH];
237 char dll_file_path [MAX_PATH];
241 if (GetSystemDirectory (system_directory, MAX_PATH) > 0) {
243 printf (
"system_directory = %s \n", system_directory);
245 sprintf (dll_file_path,
"%s\\%s", system_directory, d3d_adapter_identifier.Driver);
248 struct _finddata_t find_data;
250 find = _findfirst (dll_file_path, &find_data);
254 dll_time = localtime(&find_data.time_write);
256 month = dll_time -> tm_mon + 1;
257 day = dll_time -> tm_mday;
258 year = dll_time -> tm_year + 1900;
261 printf (
"Driver Date: %d/%d/%d\n", month, day, year);
297 printf (
"VendorId = 0x%x\n", d3d_adapter_identifier.VendorId);
298 printf (
"DeviceId = 0x%x\n", d3d_adapter_identifier.DeviceId);
301 vendor_id = d3d_adapter_identifier.VendorId;
302 device_id = d3d_adapter_identifier.DeviceId;
304 product = HIWORD(d3d_adapter_identifier.DriverVersion.HighPart);
305 version = LOWORD(d3d_adapter_identifier.DriverVersion.HighPart);
306 sub_version = HIWORD(d3d_adapter_identifier.DriverVersion.LowPart);
307 build = LOWORD(d3d_adapter_identifier.DriverVersion.LowPart);
310 printf (
"DRIVER VERSION: %d.%d.%d.%d \n", product, version, sub_version, build);
314 if (direct_3d -> GetDeviceCaps (adapter, device_type, &d3d_caps) == D3D_OK) {
316 int vertex_shader_version_major;
317 int vertex_shader_version_minor;
318 int pixel_shader_version_major;
319 int pixel_shader_version_minor;
321 vertex_shader_version_major = D3DSHADER_VERSION_MAJOR (d3d_caps.VertexShaderVersion);
322 vertex_shader_version_minor = D3DSHADER_VERSION_MINOR (d3d_caps.VertexShaderVersion);
323 pixel_shader_version_major = D3DSHADER_VERSION_MAJOR (d3d_caps.PixelShaderVersion);
324 pixel_shader_version_minor = D3DSHADER_VERSION_MINOR (d3d_caps.PixelShaderVersion);
326 switch (pixel_shader_version_major)
329 shader_model = GraphicsStateGuardian::SM_00;
332 shader_model = GraphicsStateGuardian::SM_11;
337 shader_model = GraphicsStateGuardian::SM_20;
338 if (d3d_caps.PS20Caps.NumInstructionSlots >= 512) {
339 shader_model = GraphicsStateGuardian::SM_2X;
343 shader_model = GraphicsStateGuardian::SM_30;
347 shader_model = GraphicsStateGuardian::SM_40;
352 printf (
"shader_model = %d \n", shader_model);
354 get_device_caps_state =
true;
357 get_device_caps_state =
false;
362 int maximum_display_modes;
363 UINT display_mode_count;
364 D3DFORMAT d3d_format;
367 maximum_display_modes = 0;
369 while (display_format_array [format_index].d3d_format != D3DFMT_UNKNOWN) {
370 d3d_format = display_format_array [format_index].d3d_format;
372 display_mode_count = direct_3d -> GetAdapterModeCount (adapter, d3d_format);
373 if (display_mode_count > 0) {
375 D3DDISPLAYMODE d3d_display_mode;
377 for (mode_index = 0; mode_index < display_mode_count; mode_index++) {
378 if (direct_3d -> EnumAdapterModes (adapter, d3d_format, mode_index, &d3d_display_mode) == D3D_OK) {
379 if (d3d_display_mode.Width >= minimum_width && d3d_display_mode.Height >= minimum_height &&
380 d3d_display_mode.Width <= maximum_width && d3d_display_mode.Height <= maximum_height) {
381 if (display_format_array [format_index].bits_per_pixel >= minimum_bits_per_pixel &&
382 display_format_array [format_index].bits_per_pixel <= maximum_bits_per_pixel) {
383 if (d3d_format == d3d_display_mode.Format) {
384 maximum_display_modes++;
396 printf (
"maximum_display_modes %d \n", maximum_display_modes);
399 display_mode_array =
new DisplayMode [maximum_display_modes];
402 while (display_format_array [format_index].d3d_format != D3DFMT_UNKNOWN) {
403 d3d_format = display_format_array [format_index].d3d_format;
404 display_mode_count = direct_3d -> GetAdapterModeCount (adapter, d3d_format);
405 if (display_mode_count > 0) {
407 D3DDISPLAYMODE d3d_display_mode;
409 for (mode_index = 0; mode_index < display_mode_count; mode_index++) {
410 if (direct_3d -> EnumAdapterModes (adapter, d3d_format, mode_index, &d3d_display_mode) == D3D_OK) {
411 if (d3d_display_mode.Width >= minimum_width && d3d_display_mode.Height >= minimum_height &&
412 d3d_display_mode.Width <= maximum_width && d3d_display_mode.Height <= maximum_height) {
413 if (display_format_array [format_index].bits_per_pixel >= minimum_bits_per_pixel &&
414 display_format_array [format_index].bits_per_pixel <= maximum_bits_per_pixel) {
416 printf (
"w = %d h = %d r = %d f = %d \n",
417 d3d_display_mode.Width,
418 d3d_display_mode.Height,
419 d3d_display_mode.RefreshRate,
420 d3d_display_mode.Format);
423 if (d3d_format == d3d_display_mode.Format) {
426 display_mode = &display_mode_array [total_display_modes];
427 display_mode -> width = d3d_display_mode.Width;
428 display_mode -> height = d3d_display_mode.Height;
429 display_mode -> bits_per_pixel = display_format_array [format_index].bits_per_pixel;
430 display_mode -> refresh_rate = d3d_display_mode.RefreshRate;
431 display_mode -> fullscreen_only = display_format_array [format_index].fullscreen_only;
433 total_display_modes++;
451 WNDCLASSEX window_class =
453 sizeof (WNDCLASSEX), CS_CLASSDC, window_procedure, 0L, 0L,
454 GetModuleHandle(
nullptr),
nullptr,
nullptr,
nullptr,
nullptr,
455 "class_name",
nullptr 457 RegisterClassEx (&window_class);
461 window_handle = CreateWindow (
"class_name",
"window_name", WS_DISABLED, 0, 0, width, height, (HWND)
nullptr, (HMENU)
nullptr, window_class.hInstance,
nullptr);
462 if (window_handle !=
nullptr) {
463 ShowWindow (window_handle, SW_HIDE);
465 DIRECT_3D_DEVICE direct_3d_device;
466 D3DPRESENT_PARAMETERS present_parameters;
467 DWORD behavior_flags;
469 direct_3d_device = 0;
470 memset (&present_parameters, 0,
sizeof (D3DPRESENT_PARAMETERS));
472 present_parameters.BackBufferWidth = width;
473 present_parameters.BackBufferHeight = height;
474 present_parameters.BackBufferFormat = D3DFMT_X8R8G8B8;
475 present_parameters.BackBufferCount = 1;
477 present_parameters.SwapEffect = D3DSWAPEFFECT_FLIP;
478 present_parameters.hDeviceWindow = window_handle;
480 present_parameters.Windowed =
true;
481 present_parameters.EnableAutoDepthStencil =
true;
482 present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
484 present_parameters.FullScreen_RefreshRateInHz;
485 present_parameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
487 if (d3d_caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) {
488 behavior_flags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
491 behavior_flags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
495 behavior_flags |= D3DCREATE_FPU_PRESERVE;
499 result = direct_3d -> CreateDevice (adapter, device_type, window_handle, behavior_flags, &present_parameters, &direct_3d_device);
500 if (result == D3D_OK) {
504 #define MAXIMUM_TEXTURES (2048 - 1) 507 HRESULT texture_result;
508 IDirect3DTexture9 *texture_array [MAXIMUM_TEXTURES];
511 while (total_textures < MAXIMUM_TEXTURES) {
513 texture_result = direct_3d_device -> CreateTexture (
517 D3DUSAGE_RENDERTARGET,
520 &texture_array [total_textures],
522 if (texture_result == D3D_OK) {
526 if (texture_result == D3DERR_OUTOFVIDEOMEMORY) {
528 printf (
"D3DERR_OUTOFVIDEOMEMORY \n");
537 for (index = 0; index < total_textures; index++) {
538 texture_array [index] -> Release ( );
541 video_memory = (total_textures * 1024 * 1024);
544 printf (
"video_memory = %d \n", video_memory);
547 texture_memory = direct_3d_device -> GetAvailableTextureMem ( );
549 printf (
"texture_memory = %d \n", texture_memory);
552 direct_3d_device -> Release ( );
554 state = DisplayInformation::DS_success;
560 printf (
"CreateDevice failed.\n");
563 state = DisplayInformation::DS_create_device_error;
567 DestroyWindow (window_handle);
570 _GetLastError (
"CreateWindow");
571 state = DisplayInformation::DS_create_window_error;
574 direct_3d -> Release ( );
577 state = DisplayInformation::DS_direct_3d_create_error;
581 state = DisplayInformation::DS_direct_3d_create_error;
584 FreeLibrary (d3d_dll);
587 state = DisplayInformation::DS_direct_3d_create_error;
591 display_information -> _state = state;
592 display_information -> _get_adapter_display_mode_state = get_adapter_display_mode_state;
593 display_information -> _get_device_caps_state = get_device_caps_state;
594 display_information -> _maximum_window_width = window_width;
595 display_information -> _maximum_window_height = window_height;
596 display_information -> _window_bits_per_pixel = window_bits_per_pixel;
597 display_information -> _total_display_modes = total_display_modes;
598 display_information -> _display_mode_array = display_mode_array;
599 display_information -> _shader_model = shader_model;
600 display_information -> _video_memory = video_memory;
601 display_information -> _texture_memory = texture_memory;
602 display_information -> _vendor_id = vendor_id;
603 display_information -> _device_id = device_id;
604 display_information -> _driver_product = product;
605 display_information -> _driver_version = version;
606 display_information -> _driver_sub_version = sub_version;
607 display_information -> _driver_build = build;
609 display_information -> _driver_date_month = month;
610 display_information -> _driver_date_day = day;
611 display_information -> _driver_date_year = year;
616 HMODULE kernel32_dll;
618 memory_state =
false;
619 kernel32_dll = LoadLibrary (
"kernel32.dll");
621 GlobalMemoryStatusExType GlobalMemoryStatusExFunction;
623 GlobalMemoryStatusExFunction = (GlobalMemoryStatusExType) GetProcAddress (kernel32_dll,
"GlobalMemoryStatusEx");
624 if (GlobalMemoryStatusExFunction) {
625 MEMORYSTATUSEX memory_status;
627 memory_status.dwLength =
sizeof (MEMORYSTATUSEX);
628 if (GlobalMemoryStatusExFunction (&memory_status)) {
629 physical_memory = memory_status.ullTotalPhys;
630 available_physical_memory = memory_status.ullAvailPhys;
634 FreeLibrary (kernel32_dll);
636 if (memory_state ==
false) {
637 MEMORYSTATUS memory_status;
639 memory_status.dwLength =
sizeof (MEMORYSTATUS);
640 GlobalMemoryStatus (&memory_status);
642 physical_memory = memory_status.dwTotalPhys;
643 available_physical_memory = memory_status.dwAvailPhys;
647 printf (
"physical_memory %I64d \n", physical_memory);
648 printf (
"available_physical_memory %I64d \n", available_physical_memory);
651 display_information -> _physical_memory = physical_memory;
652 display_information -> _available_physical_memory = available_physical_memory;
Parameters used for searching display capabilities.