29 unsigned int month : 8;
30 unsigned int year : 16;
37 D3DFMT_X8R8G8B8, 32, FALSE,
38 D3DFMT_R5G6B5, 16, FALSE,
39 D3DFMT_X1R5G5B5, 16, FALSE,
41 D3DFMT_A2R10G10B10, 32, TRUE,
44 D3DFMT_UNKNOWN, 0, FALSE,
47 typedef BOOL (WINAPI *GlobalMemoryStatusExType) (LPMEMORYSTATUSEX lpBuffer);
49 static int d3d_format_to_bits_per_pixel (D3DFORMAT d3d_format) {
55 while (display_format_array [format_index].d3d_format != D3DFMT_UNKNOWN) {
56 if (d3d_format == display_format_array [format_index].d3d_format) {
57 bits_per_pixel = display_format_array [format_index].bits_per_pixel;
64 return bits_per_pixel;
67 static DWORD _GetLastError (
char *message_prefix) {
72 error = GetLastError ( );
74 FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM,
75 NULL, error, MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ),
76 (LPTSTR)&ptr,0, NULL)) {
77 cout <<
"ERROR: "<< message_prefix <<
" result = " << (
char*) ptr <<
"\n";
84 static LRESULT CALLBACK window_procedure (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
85 return DefWindowProc(hwnd, msg, wparam, lparam);
88 static DWORD print_GetLastError (
char *message_prefix)
94 error = GetLastError ( );
95 if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
96 FORMAT_MESSAGE_FROM_SYSTEM,
99 MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ),
103 cout <<
"ERROR: "<< message_prefix <<
" result = " << (
char*) ptr <<
"\n";
115 DisplayInformation::DetectionState state;
116 int get_adapter_display_mode_state;
117 int get_device_caps_state;
124 int minimum_bits_per_pixel;
125 int maximum_bits_per_pixel;
132 int window_bits_per_pixel;
133 int total_display_modes;
136 PN_uint64 physical_memory;
137 PN_uint64 available_physical_memory;
154 window_bits_per_pixel = 0;
155 total_display_modes = 0;
156 display_mode_array = NULL;
158 minimum_width = display_search_parameters._minimum_width;
159 minimum_height = display_search_parameters._minimum_height;
160 maximum_width = display_search_parameters._maximum_width;
161 maximum_height = display_search_parameters._maximum_height;
162 minimum_bits_per_pixel = display_search_parameters._minimum_bits_per_pixel;
163 maximum_bits_per_pixel = display_search_parameters._maximum_bits_per_pixel;
165 shader_model = GraphicsStateGuardian::SM_00;
169 state = DisplayInformation::DS_unknown;
170 get_adapter_display_mode_state =
false;
171 get_device_caps_state =
false;
174 available_physical_memory = 0;
189 DIRECT_3D_CREATE Direct3DCreate;
191 d3d_dll = LoadLibrary (d3d_dll_name);
193 Direct3DCreate = (DIRECT_3D_CREATE) GetProcAddress (d3d_dll, direct_3d_create_function_name);
194 if (Direct3DCreate) {
197 direct_3d = Direct3DCreate (D3D_SDK_VERSION);
198 if (direct_3d != NULL) {
201 D3DDEVTYPE device_type;
202 D3DDISPLAYMODE current_d3d_display_mode;
203 D3DADAPTER_IDENTIFIER d3d_adapter_identifier;
206 adapter = D3DADAPTER_DEFAULT;
207 device_type = D3DDEVTYPE_HAL;
210 if (direct_3d -> GetAdapterDisplayMode (adapter, ¤t_d3d_display_mode) == D3D_OK) {
212 printf (
"current mode w = %d h = %d r = %d f = %d \n",
213 current_d3d_display_mode.Width,
214 current_d3d_display_mode.Height,
215 current_d3d_display_mode.RefreshRate,
216 current_d3d_display_mode.Format);
219 window_width = current_d3d_display_mode.Width;
220 window_height = current_d3d_display_mode.Height;
221 window_bits_per_pixel = d3d_format_to_bits_per_pixel (current_d3d_display_mode.Format);
223 get_adapter_display_mode_state =
true;
226 get_adapter_display_mode_state =
false;
230 if (direct_3d -> GetAdapterIdentifier (adapter, flags, &d3d_adapter_identifier) == D3D_OK) {
232 d3d_adapter_identifier.Driver;
233 d3d_adapter_identifier.Description;
234 d3d_adapter_identifier.DeviceName;
235 d3d_adapter_identifier.DriverVersion;
236 d3d_adapter_identifier.VendorId;
237 d3d_adapter_identifier.DeviceId;
238 d3d_adapter_identifier.SubSysId;
239 d3d_adapter_identifier.Revision;
240 d3d_adapter_identifier.DeviceIdentifier;
241 d3d_adapter_identifier.WHQLLevel;
244 printf (
"Driver: %s\n", d3d_adapter_identifier.Driver);
245 printf (
"Description: %s\n", d3d_adapter_identifier.Description);
248 char system_directory [MAX_PATH];
249 char dll_file_path [MAX_PATH];
252 if (GetSystemDirectory (system_directory, MAX_PATH) > 0) {
254 printf (
"system_directory = %s \n", system_directory);
256 sprintf (dll_file_path,
"%s\\%s", system_directory, d3d_adapter_identifier.Driver);
259 struct _finddata_t find_data;
261 find = _findfirst (dll_file_path, &find_data);
265 dll_time = localtime(&find_data.time_write);
267 month = dll_time -> tm_mon + 1;
268 day = dll_time -> tm_mday;
269 year = dll_time -> tm_year + 1900;
272 printf (
"Driver Date: %d/%d/%d\n", month, day, year);
308 printf (
"VendorId = 0x%x\n", d3d_adapter_identifier.VendorId);
309 printf (
"DeviceId = 0x%x\n", d3d_adapter_identifier.DeviceId);
312 vendor_id = d3d_adapter_identifier.VendorId;
313 device_id = d3d_adapter_identifier.DeviceId;
315 product = HIWORD(d3d_adapter_identifier.DriverVersion.HighPart);
316 version = LOWORD(d3d_adapter_identifier.DriverVersion.HighPart);
317 sub_version = HIWORD(d3d_adapter_identifier.DriverVersion.LowPart);
318 build = LOWORD(d3d_adapter_identifier.DriverVersion.LowPart);
321 printf (
"DRIVER VERSION: %d.%d.%d.%d \n", product, version, sub_version, build);
326 whql.whql= d3d_adapter_identifier.WHQLLevel;
329 printf (
"WHQL: %d %d %d \n", whql.day, whql.day, whql.year);
333 if (direct_3d -> GetDeviceCaps (adapter, device_type, &d3d_caps) == D3D_OK) {
335 int vertex_shader_version_major;
336 int vertex_shader_version_minor;
337 int pixel_shader_version_major;
338 int pixel_shader_version_minor;
340 vertex_shader_version_major = D3DSHADER_VERSION_MAJOR (d3d_caps.VertexShaderVersion);
341 vertex_shader_version_minor = D3DSHADER_VERSION_MINOR (d3d_caps.VertexShaderVersion);
342 pixel_shader_version_major = D3DSHADER_VERSION_MAJOR (d3d_caps.PixelShaderVersion);
343 pixel_shader_version_minor = D3DSHADER_VERSION_MINOR (d3d_caps.PixelShaderVersion);
345 switch (pixel_shader_version_major)
348 shader_model = GraphicsStateGuardian::SM_00;
351 shader_model = GraphicsStateGuardian::SM_11;
355 shader_model = GraphicsStateGuardian::SM_20;
356 if (d3d_caps.PS20Caps.NumInstructionSlots >= 512) {
357 shader_model = GraphicsStateGuardian::SM_2X;
361 shader_model = GraphicsStateGuardian::SM_30;
365 shader_model = GraphicsStateGuardian::SM_40;
370 printf (
"shader_model = %d \n", shader_model);
372 get_device_caps_state =
true;
375 get_device_caps_state =
false;
380 int maximum_display_modes;
381 UINT display_mode_count;
382 D3DFORMAT d3d_format;
385 maximum_display_modes = 0;
387 while (display_format_array [format_index].d3d_format != D3DFMT_UNKNOWN) {
388 d3d_format = display_format_array [format_index].d3d_format;
390 display_mode_count = direct_3d -> GetAdapterModeCount (adapter, d3d_format);
391 if (display_mode_count > 0) {
393 D3DDISPLAYMODE d3d_display_mode;
395 for (mode_index = 0; mode_index < display_mode_count; mode_index++) {
396 if (direct_3d -> EnumAdapterModes (adapter, d3d_format, mode_index, &d3d_display_mode) == D3D_OK) {
397 if (d3d_display_mode.Width >= minimum_width && d3d_display_mode.Height >= minimum_height &&
398 d3d_display_mode.Width <= maximum_width && d3d_display_mode.Height <= maximum_height) {
399 if (display_format_array [format_index].bits_per_pixel >= minimum_bits_per_pixel &&
400 display_format_array [format_index].bits_per_pixel <= maximum_bits_per_pixel) {
401 if (d3d_format == d3d_display_mode.Format) {
402 maximum_display_modes++;
414 printf (
"maximum_display_modes %d \n", maximum_display_modes);
417 display_mode_array =
new DisplayMode [maximum_display_modes];
420 while (display_format_array [format_index].d3d_format != D3DFMT_UNKNOWN) {
421 d3d_format = display_format_array [format_index].d3d_format;
422 display_mode_count = direct_3d -> GetAdapterModeCount (adapter, d3d_format);
423 if (display_mode_count > 0) {
425 D3DDISPLAYMODE d3d_display_mode;
427 for (mode_index = 0; mode_index < display_mode_count; mode_index++) {
428 if (direct_3d -> EnumAdapterModes (adapter, d3d_format, mode_index, &d3d_display_mode) == D3D_OK) {
429 if (d3d_display_mode.Width >= minimum_width && d3d_display_mode.Height >= minimum_height &&
430 d3d_display_mode.Width <= maximum_width && d3d_display_mode.Height <= maximum_height) {
431 if (display_format_array [format_index].bits_per_pixel >= minimum_bits_per_pixel &&
432 display_format_array [format_index].bits_per_pixel <= maximum_bits_per_pixel) {
434 printf (
"w = %d h = %d r = %d f = %d \n",
435 d3d_display_mode.Width,
436 d3d_display_mode.Height,
437 d3d_display_mode.RefreshRate,
438 d3d_display_mode.Format);
441 if (d3d_format == d3d_display_mode.Format) {
444 display_mode = &display_mode_array [total_display_modes];
445 display_mode -> width = d3d_display_mode.Width;
446 display_mode -> height = d3d_display_mode.Height;
447 display_mode -> bits_per_pixel = display_format_array [format_index].bits_per_pixel;
448 display_mode -> refresh_rate = d3d_display_mode.RefreshRate;
449 display_mode -> fullscreen_only = display_format_array [format_index].fullscreen_only;
451 total_display_modes++;
469 WNDCLASSEX window_class =
471 sizeof (WNDCLASSEX), CS_CLASSDC, window_procedure, 0L, 0L,
472 GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
475 RegisterClassEx (&window_class);
479 window_handle = CreateWindow (
"class_name",
"window_name", WS_DISABLED, 0, 0, width, height, (HWND) NULL, (HMENU) NULL, window_class.hInstance, NULL);
480 if (window_handle != NULL) {
481 ShowWindow (window_handle, SW_HIDE);
483 DIRECT_3D_DEVICE direct_3d_device;
484 D3DPRESENT_PARAMETERS present_parameters;
485 DWORD behavior_flags;
487 direct_3d_device = 0;
488 memset (&present_parameters, 0,
sizeof (D3DPRESENT_PARAMETERS));
490 present_parameters.BackBufferWidth = width;
491 present_parameters.BackBufferHeight = height;
492 present_parameters.BackBufferFormat = D3DFMT_X8R8G8B8;
493 present_parameters.BackBufferCount = 1;
495 present_parameters.SwapEffect = D3DSWAPEFFECT_FLIP;
496 present_parameters.hDeviceWindow = window_handle;
498 present_parameters.Windowed =
true;
499 present_parameters.EnableAutoDepthStencil =
true;
500 present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
502 present_parameters.FullScreen_RefreshRateInHz;
503 present_parameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
505 if (d3d_caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) {
506 behavior_flags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
509 behavior_flags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
512 behavior_flags |= D3DCREATE_FPU_PRESERVE;
516 result = direct_3d -> CreateDevice (adapter, device_type, window_handle, behavior_flags, &present_parameters, &direct_3d_device);
517 if (result == D3D_OK) {
520 #define MAXIMUM_TEXTURES (2048 - 1) 523 HRESULT texture_result;
524 IDirect3DTexture9 *texture_array [MAXIMUM_TEXTURES];
527 while (total_textures < MAXIMUM_TEXTURES) {
529 texture_result = direct_3d_device -> CreateTexture (
533 D3DUSAGE_RENDERTARGET,
536 &texture_array [total_textures],
538 if (texture_result == D3D_OK) {
542 if (texture_result == D3DERR_OUTOFVIDEOMEMORY) {
544 printf (
"D3DERR_OUTOFVIDEOMEMORY \n");
553 for (index = 0; index < total_textures; index++) {
554 texture_array [index] -> Release ( );
557 video_memory = (total_textures * 1024 * 1024);
560 printf (
"video_memory = %d \n", video_memory);
563 texture_memory = direct_3d_device -> GetAvailableTextureMem ( );
565 printf (
"texture_memory = %d \n", texture_memory);
568 direct_3d_device -> Release ( );
570 state = DisplayInformation::DS_success;
576 printf (
"CreateDevice failed.\n");
579 state = DisplayInformation::DS_create_device_error;
583 DestroyWindow (window_handle);
586 _GetLastError (
"CreateWindow");
587 state = DisplayInformation::DS_create_window_error;
590 direct_3d -> Release ( );
593 state = DisplayInformation::DS_direct_3d_create_error;
597 state = DisplayInformation::DS_direct_3d_create_error;
600 FreeLibrary (d3d_dll);
603 state = DisplayInformation::DS_direct_3d_create_error;
607 display_information -> _state = state;
608 display_information -> _get_adapter_display_mode_state = get_adapter_display_mode_state;
609 display_information -> _get_device_caps_state = get_device_caps_state;
610 display_information -> _maximum_window_width = window_width;
611 display_information -> _maximum_window_height = window_height;
612 display_information -> _window_bits_per_pixel = window_bits_per_pixel;
613 display_information -> _total_display_modes = total_display_modes;
614 display_information -> _display_mode_array = display_mode_array;
615 display_information -> _shader_model = shader_model;
616 display_information -> _video_memory = video_memory;
617 display_information -> _texture_memory = texture_memory;
618 display_information -> _vendor_id = vendor_id;
619 display_information -> _device_id = device_id;
620 display_information -> _driver_product = product;
621 display_information -> _driver_version = version;
622 display_information -> _driver_sub_version = sub_version;
623 display_information -> _driver_build = build;
625 display_information -> _driver_date_month = month;
626 display_information -> _driver_date_day = day;
627 display_information -> _driver_date_year = year;
632 HMODULE kernel32_dll;
634 memory_state =
false;
635 kernel32_dll = LoadLibrary (
"kernel32.dll");
637 GlobalMemoryStatusExType GlobalMemoryStatusExFunction;
639 GlobalMemoryStatusExFunction = (GlobalMemoryStatusExType) GetProcAddress (kernel32_dll,
"GlobalMemoryStatusEx");
640 if (GlobalMemoryStatusExFunction) {
641 MEMORYSTATUSEX memory_status;
643 memory_status.dwLength =
sizeof (MEMORYSTATUSEX);
644 if (GlobalMemoryStatusExFunction (&memory_status)) {
645 physical_memory = memory_status.ullTotalPhys;
646 available_physical_memory = memory_status.ullAvailPhys;
650 FreeLibrary (kernel32_dll);
652 if (memory_state ==
false) {
653 MEMORYSTATUS memory_status;
655 memory_status.dwLength =
sizeof (MEMORYSTATUS);
656 GlobalMemoryStatus (&memory_status);
658 physical_memory = memory_status.dwTotalPhys;
659 available_physical_memory = memory_status.dwAvailPhys;
663 printf (
"physical_memory %I64d \n", physical_memory);
664 printf (
"available_physical_memory %I64d \n", available_physical_memory);
667 display_information -> _physical_memory = physical_memory;
668 display_information -> _available_physical_memory = available_physical_memory;
Parameters used for searching display capabilities.