102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
graphics_devices.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include <math.h>
11#include <stdarg.h>
12#include <string.h>
13
14// Original pcdogs.exe: 0x417d70
15// Accepts only a pixel format whose flags equal 0x400, copies its eight DWORDs forward and stops enumeration. Other formats continue enumeration; overlapping buffers retain the forward-copy behavior.
16HRESULT __stdcall D3D_EnumZBufferFormatCallback(DDPIXELFORMAT *pixel_format, void *selected_format_context)
17{
18 if (pixel_format->dwFlags != DDPF_ZBUFFER) // Copy 8 DWORDs (32 bytes) from source pixel format to context buffer
19 return 1;
20
21 // Return 0 on success (format accepted), 1 on failure (format not valid)
22 /* REP MOVSD copies forwards one dword at a time, including overlap. */
23 volatile uint32_t *destination = selected_format_context;
24 const volatile uint32_t *source = (const volatile uint32_t *)pixel_format;
25
26 for (unsigned i = 0; i < 8; ++i)
27 destination[i] = source[i];
28
29 return 0;
30}
31
32// Original pcdogs.exe: 0x418f70
33// Enumerates DirectDraw devices and reports whether any devices were enumerated and retained. It neither resets the counters nor enforces the 20-device capacity, and the stored acceptance callback is not invoked.
34HRESULT D3D_EnumerateDirectDrawDevices(D3D_DeviceAcceptCallback unused_device_accept_callback)
35{
36 g_driverAcceptCallback = (global_type_g_driverAcceptCallback)(unused_device_accept_callback);
37
38 HRESULT error_code = DDraw_EnumerateExA(D3D_EnumDriverCallback, NULL, DDENUM_ATTACHEDSECONDARYDEVICES | DDENUM_DETACHEDSECONDARYDEVICES | DDENUM_NONDISPLAYDEVICES);
39
40 // Check g_enumDeviceCount at 0x004943d4 - total number of enumerated devices
41 // Check if any devices were enumerated
43 {
44 D3D_FormatDirectXError(error_code, NULL);
45 // Check if any enumerated devices are compatible
46 Debug_Log(g_errorMessageBuffer, "No devices and/or modes were enumerated!");
47
48 return 0x81000002;
49 }
50
51 // Check g_acceptedDeviceCount at 0x004943d8 - number of compatible devices accepted by callback
53 return 0;
54
55 D3D_FormatDirectXError(error_code, NULL);
56 Debug_Log(g_errorMessageBuffer, "No enumerated devices were accepted!");
57 D3D_FormatDirectXError(error_code, NULL);
58 Debug_Log(g_errorMessageBuffer, "Try enabling the D3D Reference Rasterizer.");
59
60 return 0x81000003;
61}
62
63// Original pcdogs.exe: 0x419000
64// Creates temporary DirectDraw and Direct3D interfaces, gathers capabilities and enumerates modes and devices before releasing them. Capability and enumeration HRESULTs are ignored, so failed capability queries can leave incomplete or zero capabilities.
65BOOL __stdcall D3D_EnumDriverCallback(GUID *guid, char *driver_description, char *driver_name, void *context, HMONITOR monitor)
66{
67 (void)driver_name;
68 (void)context;
69 (void)monitor;
70
71 IDirectDraw7 *direct_draw;
72 HRESULT result = DDraw_CreateEx(guid, (void **)&direct_draw, (GUID *)&IID_IDirectDraw7, NULL);
73 if (FAILED(result))
74 {
75 Debug_Log(D3D_FormatDirectXError(result, NULL), "Can't create DDraw during enumeration!");
76 return DDENUMRET_OK;
77 }
78
79 IDirect3D7 *direct_3d;
80 result = direct_draw->lpVtbl->QueryInterface(direct_draw, &data_44d0e8, (void **)&direct_3d);
81 if (FAILED(result))
82 {
83 direct_draw->lpVtbl->Release(direct_draw);
84 Debug_Log(D3D_FormatDirectXError(result, NULL), "Can't query IDirect3D7 during enumeration!");
85 return DDENUMRET_OK;
86 }
87
88 struct D3D_DriverInfo driver = {0};
89 lstrcpynA(driver.driverName, driver_description, sizeof(driver.driverName));
90 driver.driverCaps.dwSize = sizeof(driver.driverCaps);
91 driver.helCaps.dwSize = sizeof(driver.helCaps);
92 direct_draw->lpVtbl->GetCaps(direct_draw, &driver.driverCaps, &driver.helCaps);
93
94 if (guid)
95 {
96 driver.driverGuid = *guid;
97 driver.lpGuid = &driver.driverGuid;
98 }
99 driver.desktop_compatible = !guid && (driver.driverCaps.dwCaps2 & DDCAPS2_CANRENDERWINDOWED);
100
101 direct_draw->lpVtbl->EnumDisplayModes(direct_draw, 0, NULL, &driver, D3D_EnumDisplayModeCallback);
102 CRT_qsort(driver.displayModes, driver.modeCount, sizeof(driver.displayModes[0]), DDraw_CompareDisplayModes);
103 direct_3d->lpVtbl->EnumDevices(direct_3d, D3D_EnumDeviceCallback, &driver);
104 direct_3d->lpVtbl->Release(direct_3d);
105 direct_draw->lpVtbl->Release(direct_draw);
106
107 return 1;
108}
109
110// Original pcdogs.exe: 0x4191a0
111// Appends a display-mode descriptor and increments the staged mode count, cancelling enumeration once the new count reaches 80. The caller must provide space for the current index before entry.
112HRESULT __stdcall D3D_EnumDisplayModeCallback(DDSURFACEDESC2 *surface_desc, void *context)
113{
114 struct D3D_DriverInfo *parent_driver_info = context;
115
116 __builtin_memcpy(&parent_driver_info->displayModes[parent_driver_info->modeCount], surface_desc,
117 0x7c);
118
119 uint32_t new_mode_count = parent_driver_info->modeCount + 1;
120
121 parent_driver_info->modeCount = new_mode_count;
122
123 return ((0x4f < new_mode_count) ? 0L : 1L);
124}
125
126// Original pcdogs.exe: 0x4191f0
127// Copies and filters a Direct3D device into the shared enumeration table and retains its supported display modes. The accepted-device index has no capacity guard, so repeated enumeration or more than 20 accepted devices can overrun the table.
128HRESULT __stdcall D3D_EnumDeviceCallback(char *device_description, char *device_name, D3DDEVICEDESC7 *device_desc, void *context)
129{
130 struct D3D_DriverInfo *parent_driver_info = context;
131
132 (void)device_description;
133
134 /* Native indexing uses the byte stride 0x2b8c. The original has no bounds guard. */
136
138 memset(record, 0, sizeof *record);
139 record->has_hardware_accel = device_desc->dwDevCaps & D3DDEVCAPS_HWRASTERIZATION;
140 record->device_capabilities = *device_desc;
141 record->desktop_compatible = parent_driver_info->desktop_compatible;
142 record->driverCaps = parent_driver_info->driverCaps;
143 record->helCaps = parent_driver_info->helCaps;
144 record->deviceGuid = device_desc->deviceGUID;
145 record->device_guid_ptr = &record->deviceGuid;
146
147 const char *name = device_name;
148
149 if (parent_driver_info->lpGuid)
150 {
151 record->driverGuid = parent_driver_info->driverGuid;
152 record->lpGuid = &record->driverGuid;
153 name = parent_driver_info->driverName;
154 }
155
156 lstrcpynA(record->driverName, name, 0x27);
157
158 if (record->lpGuid && !record->has_hardware_accel)
159 return 1;
160
161 for (uint32_t i = 0; i < parent_driver_info->modeCount; ++i)
162 {
163 const DDSURFACEDESC2 *mode = &parent_driver_info->displayModes[i];
164
165 uint32_t bits = mode->ddpfPixelFormat.dwRGBBitCount;
166 uint32_t supported = record->device_capabilities.dwDeviceRenderBitDepth;
167
168 if ((bits == 32 && (supported & DDBD_32)) || (bits == 24 && (supported & DDBD_24)) || (bits == 16 && (supported & DDBD_16)))
169 {
170 record->displayModes[record->modeCount++] = *mode;
171
172 if (mode->ddsCaps.dwCaps2 & DDSCAPS2_STEREOSURFACELEFT)
173 record->has_stereo_modes = 1;
174 }
175 }
176
177 if (record->modeCount)
178 {
179 for (uint32_t i = 0; i < record->modeCount; ++i)
180 {
181 const DDSURFACEDESC2 *mode = &record->displayModes[i];
182
183 if (mode->dwWidth == 640 && mode->dwHeight == 480 && mode->ddpfPixelFormat.dwRGBBitCount == 16)
184 {
185 record->preferredMode = *mode;
186 record->preferredModeIdx = i;
187 }
188 }
189
190 record->selected_marker = 0;
192 }
193
194 return 1;
195}
196
197// Original pcdogs.exe: 0x419440
198// Returns the shared device-list pointer and accepted count through independently optional outputs. The count is not clamped to the 20-record capacity.
199void D3D_GetEnumeratedDevices(struct D3D_DriverInfo **device_list_out, uint32_t *device_count_out)
200{
201 if (device_list_out)
202 *device_list_out = graphics_ddraw_enum_device_list;
203
204 if (device_count_out)
205 *device_count_out = g_acceptedDeviceCount;
206}
207
208// Original pcdogs.exe: 0x419460
209// Selects the last device in the best available category: hardware TnL, hardware, software, then reference. Software-only mode skips hardware categories; the accepted count is trusted and desktop compatibility is not checked.
210HRESULT D3D_SelectDefaultDevice(struct D3D_DriverInfo **selected_device_out, uint32_t flags)
211{
212 if (!selected_device_out)
213 return E_INVALIDARG;
214
215 struct D3D_DriverInfo *devices;
216 uint32_t count;
217 D3D_GetEnumeratedDevices(&devices, &count);
218
219 struct D3D_DriverInfo *tnl = NULL;
220 struct D3D_DriverInfo *hardware = NULL;
221 struct D3D_DriverInfo *software = NULL;
222 struct D3D_DriverInfo *reference = NULL;
223
224 for (uint32_t i = 0; i < count; ++i)
225 {
226 struct D3D_DriverInfo *device = &devices[i];
227 if (device->has_hardware_accel)
228 {
229 if (IsEqualGUID(device->device_guid_ptr, &D3D_IID_IDirect3DTnLHalDevice))
230 tnl = device;
231 else
232 hardware = device;
233 }
234 else if (IsEqualGUID(device->device_guid_ptr, &D3D_IID_IDirect3DRefDevice))
235 reference = device;
236 else
237 software = device;
238 }
239
240 struct D3D_DriverInfo *selected = software ? software : reference;
241 if (!(flags & GRAPHICS_DEVICE_SOFTWARE_ONLY) && (tnl || hardware))
242 selected = tnl ? tnl : hardware;
243
244 if (!selected)
245 return 0x81000004;
246
247 *selected_device_out = selected;
248 selected->selected_marker = 0;
249 return DD_OK;
250}
251
252// Original pcdogs.exe: 0x419590
253// Returns true for any device and ignores both capability arguments; the stored policy hook is unused by this enumerator.
254HRESULT D3D_AcceptAnyDevice(DDCAPS *driver_caps, D3DDEVICEDESC7 *device_desc)
255{
256 (void)driver_caps;
257 (void)device_desc;
258 return 1;
259}
260
261// Original pcdogs.exe: 0x4195a0
262// Enumerates devices and returns the default selection while ignoring both status codes. Selection failure leaves the returned local pointer indeterminate.
264{
265 /* Native leaves the output slot unwritten when selection fails. */
266 struct D3D_DriverInfo *selected_device_out;
267
269 D3D_SelectDefaultDevice(&selected_device_out, 0);
270
271 return selected_device_out;
272}
273
274// Original pcdogs.exe: 0x445126
275// Forwards the callback, context and flags to the DirectDrawEnumerateExA import and returns its HRESULT.
276HRESULT __stdcall DDraw_EnumerateExA(LPDDENUMCALLBACKEXA lp_callback, LPVOID lp_context, DWORD dw_flags)
277{
278 /* tailcall */
279 return DirectDrawEnumerateExA(lp_callback, lp_context, dw_flags);
280}
HRESULT(* D3D_DeviceAcceptCallback)(DDCAPS *driver_caps, D3DDEVICEDESC7 *device_desc)
Definition common.h:11
void Debug_Log(char const *category, char const *message)
Definition diagnostics.c:17
#define D3D_IID_IDirect3DRefDevice
#define D3D_IID_IDirect3DTnLHalDevice
#define GRAPHICS_DEVICE_SOFTWARE_ONLY
Definition constants.h:85
#define graphics_ddraw_enum_device_list
Definition globals.h:239
BOOL D3D_EnumDriverCallback(GUID *guid, char *driver_description, char *driver_name, void *context, HMONITOR monitor)
HRESULT D3D_EnumDisplayModeCallback(DDSURFACEDESC2 *surface_desc, void *context)
struct D3D_DriverInfo * D3D_EnumerateAndSelectDefaultDevice()
HRESULT D3D_EnumerateDirectDrawDevices(D3D_DeviceAcceptCallback unused_device_accept_callback)
HRESULT D3D_AcceptAnyDevice(DDCAPS *driver_caps, D3DDEVICEDESC7 *device_desc)
HRESULT D3D_EnumZBufferFormatCallback(DDPIXELFORMAT *pixel_format, void *selected_format_context)
HRESULT DDraw_EnumerateExA(LPDDENUMCALLBACKEXA lp_callback, LPVOID lp_context, DWORD dw_flags)
HRESULT D3D_SelectDefaultDevice(struct D3D_DriverInfo **selected_device_out, uint32_t flags)
HRESULT D3D_EnumDeviceCallback(char *device_description, char *device_name, D3DDEVICEDESC7 *device_desc, void *context)
void D3D_GetEnumeratedDevices(struct D3D_DriverInfo **device_list_out, uint32_t *device_count_out)
int32_t(* global_type_g_driverAcceptCallback)()
#define g_driverAcceptCallback
#define data_44d0e8
Definition opaque_data.h:21
BOOL D3D_EnumDriverCallback(GUID *guid, char *driver_description, char *driver_name, void *context, HMONITOR monitor)
HRESULT DDraw_CreateEx(GUID *lp_guid, void **lplp_dd, GUID *iid, IUnknown *p_unk_outer)
HRESULT D3D_EnumDisplayModeCallback(DDSURFACEDESC2 *surface_desc, void *context)
char * D3D_FormatDirectXError(HRESULT error_code, char *out_buffer)
HRESULT D3D_AcceptAnyDevice(DDCAPS *driver_caps, D3DDEVICEDESC7 *device_desc)
int32_t DDraw_CompareDisplayModes(const void *left_record, const void *right_record)
HRESULT D3D_EnumDeviceCallback(char *device_description, char *device_name, D3DDEVICEDESC7 *device_desc, void *context)
void CRT_qsort(void *base, uint32_t count, uint32_t element_size, int32_t(*compare)(void const *, void const *))
Definition sorting.c:11
DDSURFACEDESC2 displayModes[0x50]
Definition types.h:42
D3DDEVICEDESC7 device_capabilities
Definition types.h:28
struct _GUID driverGuid
Definition types.h:40
struct _GUID * device_guid_ptr
Definition types.h:26
uint32_t has_stereo_modes
Definition types.h:47
struct _GUID * lpGuid
Definition types.h:31
uint32_t modeCount
Definition types.h:44
struct _GUID deviceGuid
Definition types.h:39
int32_t preferredModeIdx
Definition types.h:45
int32_t desktop_compatible
Definition types.h:46
DDCAPS helCaps
Definition types.h:34
uint32_t selected_marker
Definition types.h:37
DDCAPS driverCaps
Definition types.h:33
DDSURFACEDESC2 preferredMode
Definition types.h:35
char driverName[0x27]
Definition types.h:24
uint32_t has_hardware_accel
Definition types.h:30
#define g_enumDeviceCount
Definition values.h:309
#define g_errorMessageBuffer
Definition values.h:315
#define g_acceptedDeviceCount
Definition values.h:312