102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
primitives.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
16#include "pcdogs/math/types.h"
21#include <math.h>
22#include <stdarg.h>
23#include <string.h>
24
25// Original pcdogs.exe: 0x4145b0
26// Converts source pixels into a work surface and blits fixed loading-screen regions into four textures. Conversion assumes tightly packed destination rows and ignores the locked surface pitch; bottom regions retain their native source-row offsets.
27void Graphics_BlitTextureToQuadrants(IDirectDrawSurface7 **quadrant_surfaces, uint8_t *pixel_data, int32_t pixel_count, int32_t width, int32_t height)
28{
29 DDSURFACEDESC2 work_surface_desc;
30 memset(&work_surface_desc, 0, 0x7c);
31
32 // Zero-initialize DDSURFACEDESC2 structure (124 bytes) before use
33 work_surface_desc.dwSize = 0x7c;
34 work_surface_desc.dwFlags = 0;
35
36 if (!quadrant_surfaces)
37 {
38 Debug_Log("CopyMem_Texture(): lpTex == NULL!", "Error");
39
40 // IDirectDrawSurface7::GetSurfaceDesc(this, pDDSurfaceDesc2) - Vtable offset +0x58
41 return;
42 }
43
44 DDSURFACEDESC2 source_surface_desc;
45 source_surface_desc.dwSize = 0x7c;
46 // Call IDirectDrawSurface7::GetSurfaceDesc (vtable+0x58) to retrieve surface properties into DDSURFACEDESC2
47 // Create work texture surface with specified dimensions for pixel conversion
48 IDirectDrawSurface7 *source_surface = (IDirectDrawSurface7 *)(*(uint32_t *)quadrant_surfaces);
49 // IDirectDrawSurface7::Blt (vtable+0x14) - Blit from work surface to final texture surface. Flag
50 // 0x1000000 = DDBLT_WAIT.
51 source_surface->lpVtbl->GetSurfaceDesc(source_surface, &source_surface_desc);
52 // Lock work surface for write access - retrieves surface pointer and pitch from DDSURFACEDESC2
53 // Check if fog capability bit 0x2000 is set in hardware caps
54 IDirectDrawSurface7 *work_surface =
55 D3D_CreateWorkSurface((IDirectDrawSurface7 *)(*(uint32_t *)quadrant_surfaces), width, height);
56
57 if (!work_surface)
58 // IDirectDrawSurface7::Release (vtable+0x08) - Release work surface reference count.
59 Debug_Log("work surface is NULL!", "Error");
60
61 // IDirectDrawSurface7::GetDC(this, phDC) - Vtable offset at RestoreAllSurfaces position. Gets device context
62 // for GDI operations.
63 HRESULT error_code;
64
65 error_code = work_surface->lpVtbl->Lock(work_surface, NULL, &work_surface_desc, DDLOCK_WAIT, NULL);
66
67 if (error_code < 0)
68 {
69 char *message = "DirectX Error: Lock();";
70 char *description = D3D_FormatDirectXError(error_code, NULL);
71
72 Debug_Log(description, message);
73
74 return;
75 }
76
77 void *lp_surface = work_surface_desc.lpSurface;
78
79 if (!lp_surface)
80 {
81 Debug_Log("Cannot get surface pointer!", "DirectX Error");
82
83 return;
84 }
85
86 uint16_t *dst_pixel = lp_surface;
87 int32_t pixel_index = 0;
88
89 do
90 {
91 uint32_t blue = pixel_data[0] >> (g_blueBitsToDiscard & 31);
92 uint32_t green = pixel_data[1] >> (g_greenBitsToDiscard & 31);
93 uint32_t red = pixel_data[2] >> (g_redBitsToDiscard & 31);
94
95 *dst_pixel++ = (uint16_t)((red << (D3D_CreateTextureSurface_GraphicsPixelRedShift & 31)) + (green << (graphics_pixel_green_shift & 31)) + (blue << (D3D_CreateTextureSurface_GraphicsPixelBlueShift & 31)));
96 pixel_data += 4;
97 } while (++pixel_index < pixel_count);
98
99 work_surface->lpVtbl->Unlock(work_surface, NULL); // Unlock work surface after writing all converted pixel data
100 // IDirectDrawSurface7::Release(this) - Vtable offset +0x08. Decrements reference count.
101 IDirectDrawSurface7 *top_left_surface = (IDirectDrawSurface7 *)(*(uint32_t *)quadrant_surfaces);
102 DDBLTFX *top_left_blit_effects = NULL;
103
104 DWORD top_left_blit_flags = DDBLT_WAIT;
105
106 RECT source_rect;
107 RECT *top_left_source_rect = &source_rect;
108 source_rect = (RECT){
109 .left = 0,
110 .right = 0x140,
111 .top = 0,
112 .bottom = 0xf0};
113
114 // Blt from work surface to final texture surfaces with DDBLT_WAIT (0x1000000) flag
115 top_left_surface->lpVtbl->Blt(top_left_surface, NULL, work_surface, top_left_source_rect, top_left_blit_flags, top_left_blit_effects);
116
117 int32_t *top_right_surface = (int32_t *)(quadrant_surfaces[1]);
118 int32_t top_right_blit_effects = 0;
119 int32_t top_right_blit_flags = DDBLT_WAIT;
120
121 // IDirectDrawSurface7::Lock(this, lpDestRect, lpDDSurfaceDesc, dwFlags=0x1000000, hEvent) - Vtable offset
122 // +0x64
123 RECT *top_right_source_rect = &source_rect;
124 // BUG: Should be 0x140 (320), not 0x141 (321). Off-by-one creates 1px gap at vertical seam.
125 source_rect = (RECT){
126 .left = 0x141,
127 .right = 0x280,
128 .top = 0,
129 .bottom = 0xf0};
130
131 ((IDirectDrawSurface7 *)top_right_surface)->lpVtbl->Blt((IDirectDrawSurface7 *)(top_right_surface), 0, work_surface, top_right_source_rect, top_right_blit_flags, (LPDDBLTFX)(top_right_blit_effects));
132
133 int32_t *bottom_right_surface = (int32_t *)(quadrant_surfaces[2]);
134 int32_t bottom_right_blit_effects = 0;
135 int32_t bottom_right_blit_flags = DDBLT_WAIT;
136
137 // IDirectDrawSurface7::Lock(this, lpDestRect, lpDDSurfaceDesc, dwFlags=0x1000000, hEvent) - Vtable offset
138 // +0x64
139 RECT *bottom_right_source_rect = &source_rect;
140 // BUG: Should be 0x140 (320), not 0x141 (321). Off-by-one creates 1px gap at vertical seam.
141 memcpy(&source_rect,
142 "\x41\x01\x00\x00\xf1\x00\x00\x00\x80\x02\x00\x00\xe0\x01\x00\x00", 0x10);
143 // Release work surface - cleanup temporary surface used for pixel format conversion
144 ((IDirectDrawSurface7 *)bottom_right_surface)->lpVtbl->Blt((IDirectDrawSurface7 *)(bottom_right_surface), 0, work_surface, bottom_right_source_rect, bottom_right_blit_flags, (LPDDBLTFX)(bottom_right_blit_effects));
145
146 int32_t *bottom_left_surface = (int32_t *)(quadrant_surfaces[3]);
147 int32_t bottom_left_blit_effects = 0;
148 int32_t bottom_left_blit_flags = DDBLT_WAIT;
149
150 RECT *bottom_left_source_rect = &source_rect;
151 source_rect = (RECT){
152 .left = 0,
153 // IDirectDrawSurface7::Lock(this, lpDestRect, lpDDSurfaceDesc, dwFlags=0x1000000, hEvent) - Vtable offset
154 // +0x64
155 .right = 0x140,
156 // BUG: Should be 0xF0 (240), not 0xF1 (241). Off-by-one creates 1px gap at horizontal seam.
157 .top = 0xf1,
158 .bottom = 0x1e0};
159
160 // IDirectDrawSurface7::Release(this) - Vtable offset +0x08
161 ((IDirectDrawSurface7 *)bottom_left_surface)->lpVtbl->Blt((IDirectDrawSurface7 *)(bottom_left_surface), 0, work_surface, bottom_left_source_rect, bottom_left_blit_flags, (LPDDBLTFX)(bottom_left_blit_effects));
162 work_surface->lpVtbl->Release(work_surface);
163}
164
165// Original pcdogs.exe: 0x414f60
166// Draws a square textured quad at packed screen coordinates with top and bottom colors, material blending and temporary depth-test suppression.
167void Graphics_RenderTexturedQuad(uint32_t packed_xy, struct Material_TableEntry *material, uint32_t render_flags, uint32_t top_color, uint32_t bottom_color, int32_t quad_size)
168{
169 int32_t x = (int16_t)packed_xy, y = (int16_t)(packed_xy >> 16);
170
172
173 if (!blend_mode)
174 {
175 if (material->flags & MATERIAL_BLEND_ENABLED)
177 }
178 else if (blend_mode >= GRAPHICS_BLEND_ADDITIVE_DIM)
179 blend_mode = 0;
180
181 uint8_t alpha = D3D_SetBlendMode(blend_mode);
182
183 IDirect3DDevice7 *device = g_d3dDevice7;
184
186 device->lpVtbl->SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
187
188 D3D_SetTextureColorOperation(D3DTOP_MODULATE);
189
190 struct Material_RuntimeDescriptor *texture = material->texture_descriptor;
191
192 unsigned width_minus_one = texture->dimensions_minus_1.width;
193 unsigned height_minus_one = texture->dimensions_minus_1.height;
194
195 float reciprocal_height = (float)(1.0L / (height_minus_one + 1));
196 float u = (float)((1.0L / (width_minus_one + 1)) * width_minus_one);
197 float v = (float)((long double)height_minus_one * reciprocal_height);
198
200
201 if (g_currentBoundTextureSurface != surface)
202 {
204 device->lpVtbl->SetTexture(device, 0, surface);
205 }
206
207 uint32_t packed_top_color = (uint32_t)alpha << 24 | (top_color & 0xff) << 16 | (top_color & 0xff00) | ((top_color >> 16) & 0xff);
208 uint32_t packed_bottom_color = (uint32_t)alpha << 24 | (bottom_color & 0xff) << 16 | (bottom_color & 0xff00) | ((bottom_color >> 16) & 0xff);
209
210 struct
211 {
212 float x, y, z, rhw;
213 uint32_t color;
214 float u, v;
215 } vertices[4];
216
217 uint32_t depth_bits = GRAPHICS_SCREEN_QUAD_DEPTH_BITS;
218 float depth;
219
220 memcpy(&depth, &depth_bits, sizeof depth);
221
222 for (int i = 0; i < 4; ++i)
223 {
224 vertices[i].x = (float)(x + ((i & 1) ? quad_size : 0));
225 vertices[i].y = (float)(y + ((i & 2) ? quad_size : 0));
226 vertices[i].z = depth;
227 vertices[i].rhw = depth;
228 vertices[i].color = (i & 2) ? packed_bottom_color : packed_top_color;
229 vertices[i].u = (i & 1) ? u : 0.0f;
230 vertices[i].v = (i & 2) ? v : 0.0f;
231 }
232
233 device->lpVtbl->DrawPrimitive(device, D3DPT_TRIANGLESTRIP, (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1), vertices, 4, 0);
234
236 device->lpVtbl->SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
237}
238
239// Original pcdogs.exe: 0x415200
240// Draws a textured quad sized from the material dimensions, with separate top and bottom colors and blend flags.
241void Graphics_RenderTexturedQuadMaterialSize(uint32_t packed_xy, struct Material_TableEntry *material, uint32_t render_flags, uint32_t top_color, uint32_t bottom_color)
242{
243 int32_t left = (int32_t)(int16_t)packed_xy;
244 // Get glyph dimensions (width at +2, height at +3)
245 int32_t top = (int32_t)(int16_t)(packed_xy >> 0x10);
246 uint32_t texture_descriptor_data;
247
248 if (material)
249 texture_descriptor_data = (uint32_t)(material->texture_descriptor);
250
251 int32_t material_width;
252
253 if (!material || !texture_descriptor_data)
254 material_width = 1;
255 else
256 {
257 int32_t material_width_minus_one = (uint8_t)(((struct Material_RuntimeDescriptor *)(uintptr_t)texture_descriptor_data)->dimensions_minus_1.width);
258
259 material_width = material_width_minus_one + 1;
260 }
261
262 uint32_t texture_descriptor_data_height;
263
264 if (material)
265 texture_descriptor_data_height = (uint32_t)(material->texture_descriptor);
266
267 int32_t material_height;
268
269 if (!material || !texture_descriptor_data_height)
270 material_height = 1;
271 else
272 {
273 int32_t material_height_minus_one = (uint8_t)(((struct Material_RuntimeDescriptor *)(uintptr_t)texture_descriptor_data_height)->dimensions_minus_1.height);
274
275 material_height = material_height_minus_one + 1;
276 }
277
278 // Bind glyph texture (handle at offset +0xC or +0x10)
279 char requested_blend_mode = (char)(render_flags >> SPRITE_RENDER_BLEND_MODE_SHIFT) & (SPRITE_RENDER_BLEND_MODE_MASK >> SPRITE_RENDER_BLEND_MODE_SHIFT);
280 char blend_mode = requested_blend_mode;
281
282 if (!requested_blend_mode)
283 {
284 uint32_t flags = material->flags;
285
286 if ((uint8_t)flags & MATERIAL_BLEND_ENABLED)
288 }
289 else if (requested_blend_mode >= GRAPHICS_BLEND_ADDITIVE_DIM)
290 blend_mode = 0;
291
292 uint8_t blend_alpha = D3D_SetBlendMode((uint32_t)blend_mode);
293
294 // Set up quad with gradient (top vertices use top_color, bottom use bottom_color)
296 {
297 IDirect3DDevice7 *disable_depth_device = g_d3dDevice7;
298 disable_depth_device->lpVtbl->SetRenderState(disable_depth_device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
299 }
300
301 D3D_SetTextureColorOperation(D3DTOP_MODULATE);
302
303 uint32_t texture_descriptor_data_active = (uint32_t)(material->texture_descriptor);
304 int32_t uv_width_minus_one = (uint8_t)(((struct Material_RuntimeDescriptor *)(uintptr_t)texture_descriptor_data_active)->dimensions_minus_1.width);
305 int32_t uv_height_minus_one = (uint8_t)(((struct Material_RuntimeDescriptor *)(uintptr_t)texture_descriptor_data_active)->dimensions_minus_1.height);
306 float v0_u = 0.0f;
307 float v0_v = 0.0f;
308 float v1_v = 0.0f;
309 float v2_u = 0.0f;
310 float reciprocal_texture_height = (float)((long double)1.0f / (long double)(uv_height_minus_one + 1));
311 long double maximum_u = (long double)1.0f / (long double)(uv_width_minus_one + 1) * (long double)uv_width_minus_one;
312 float v1_u = (float)maximum_u;
313 long double maximum_v = (long double)uv_height_minus_one * (long double)reciprocal_texture_height;
314 float v2_v = (float)maximum_v;
315 float v3_u = (float)maximum_u;
316 float v3_v = (float)maximum_v;
317
318 IDirectDrawSurface7 *texture_surface = ((struct Material_RuntimeDescriptor *)(uintptr_t)texture_descriptor_data_active)->texture_handles[~g_graphicsCapabilities >> GRAPHICS_CAP_DEPTH_BUFFER_SHIFT & (GRAPHICS_CAP_DEPTH_BUFFER >> GRAPHICS_CAP_DEPTH_BUFFER_SHIFT)];
319
320 if (g_currentBoundTextureSurface != texture_surface)
321 {
322 IDirect3DDevice7 *texture_device = g_d3dDevice7;
323 g_currentBoundTextureSurface = texture_surface;
324 texture_device->lpVtbl->SetTexture(texture_device, 0, texture_surface);
325 }
326
327 uint32_t alpha_bits = (uint32_t)blend_alpha << 8;
328 int32_t top_blue_byte = (uint8_t)((uint8_t)((uint32_t)top_color >> 16));
329 int32_t packed_top_color =
330 // Pack top_color (arg4) into D3DCOLOR ARGB: alpha from blend mode | R,G,B from arg4.
331 // NO 2x applied - color remains in PS1 scale. Assigned to vertices 0 and 1 (top edge).
332 // Sidecar's DrawPrimitive intercept would apply 2x here via g_dttr_ps1_color_scale.
333 ((uint32_t)(uint8_t)top_color | alpha_bits) << 0x10 | top_blue_byte | (top_color & 0xff00);
334 long double left_precise = (long double)left;
335 uint32_t v0_color = packed_top_color;
336 float v0_x = (float)left_precise;
337 long double top_precise = (long double)top;
338 int32_t bottom_blue_byte = (uint8_t)((uint8_t)((uint32_t)bottom_color >> 16));
339 uint32_t v1_color = packed_top_color;
340 float v0_y = (float)top_precise;
341
342 IDirect3DDevice7 *draw_device = g_d3dDevice7;
343
344 long double right_precise = (long double)(material_width + left);
345 int32_t packed_bottom_color =
346 // Pack bottom_color (arg5) into D3DCOLOR ARGB: same alpha | R,G,B from arg5.
347 // NO 2x applied - color remains in PS1 scale. Assigned to vertices 2 and 3 (bottom edge).
348 // Sidecar's DrawPrimitive intercept would apply 2x here via g_dttr_ps1_color_scale.
349 ((uint32_t)(uint8_t)bottom_color | alpha_bits) << 0x10 | bottom_blue_byte | (bottom_color & 0xff00);
350 float v1_x = (float)right_precise;
351 float v1_y = (float)top_precise;
352 float v3_x = (float)right_precise;
353 float v2_x = (float)left_precise;
354 long double bottom_precise = (long double)(material_height + top);
355 float v0_z = 9.99999997e-07f;
356 float v0_rhw = 9.99999997e-07f;
357 float v1_z = 9.99999997e-07f;
358 float v2_y = (float)bottom_precise;
359 float v3_y = (float)bottom_precise;
360 float v1_rhw = 9.99999997e-07f;
361 float v2_z = 9.99999997e-07f;
362 float v2_rhw = 9.99999997e-07f;
363 uint32_t v2_color = packed_bottom_color;
364 float v3_z = 9.99999997e-07f;
365 float v3_rhw = 9.99999997e-07f;
366 uint32_t v3_color = packed_bottom_color;
367
368 // IDirect3DDevice7::DrawPrimitive(D3DPT_TRIANGLESTRIP, FVF, vertices, 4, 0).
369 // Vertex colors: top 2 have arg4 color, bottom 2 have arg5 color - all in PS1 scale.
370 // No 2x applied here. The sidecar's DrawPrimitive hook applies the 2x when
371 // g_dttr_ps1_color_scale is active (set by the gradient glyph hook wrapper).
372 struct
373 {
374 float x, y, z, rhw;
375 uint32_t color;
376 float u, v;
377 } vertices[4] = {
378 {v0_x, v0_y, v0_z, v0_rhw, v0_color, v0_u, v0_v},
379 {v1_x, v1_y, v1_z, v1_rhw, v1_color, v1_u, v1_v},
380 {v2_x, v2_y, v2_z, v2_rhw, v2_color, v2_u, v2_v},
381 {v3_x, v3_y, v3_z, v3_rhw, v3_color, v3_u, v3_v}};
382
383 draw_device->lpVtbl->DrawPrimitive(draw_device, D3DPT_TRIANGLESTRIP, (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1), vertices, 4, 0);
384
386 {
387 IDirect3DDevice7 *restore_depth_device = g_d3dDevice7;
388 restore_depth_device->lpVtbl->SetRenderState(restore_depth_device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
389 }
390}
391
392static uint32_t Graphics_ClampChannel(uint32_t value)
393{
394 return value > 255 ? 255 : value;
395}
396
397static void Graphics_TintPolygonColors(const struct Graphics_Render_PolygonBatchRecord *polygon, uint32_t tint)
398{
399 for (unsigned i = 0; i < 4; ++i)
400 {
401 uint32_t color = polygon->vertex_colors[i];
402 g_quadVertexColorR[i] = Graphics_ClampChannel(((color & 255) * (tint & 255)) >> 7);
403 g_quadVertexColorG[i] = Graphics_ClampChannel((((color >> 8) & 255) * ((tint >> 8) & 255)) >> 7);
404 g_quadVertexColorB[i] = Graphics_ClampChannel((((color >> 16) & 255) * ((tint >> 16) & 255)) >> 7);
405 }
406}
407
408static unsigned Graphics_StripSourceIndex(unsigned slot, unsigned count)
409{
410 return count == 4 && slot >= 2 ? 5 - slot : slot;
411}
412
413static void Graphics_SetStageAddress(D3DTEXTURESTAGESTATETYPE state, D3DTEXTUREADDRESS address)
414{
415 IDirect3DDevice7 *device = g_d3dDevice7;
416 device->lpVtbl->SetTextureStageState(device, 0, state, address);
417}
418
420 struct Material_RuntimeDescriptor *texture, unsigned lod)
421{
422 if (polygon->texture_repeat_count.u > 1 || polygon->texture_repeat_count.v > 1)
423 {
424 Graphics_SetStageAddress(D3DTSS_ADDRESS, D3DTADDRESS_WRAP);
425 if (polygon->texture_repeat_count.u == 1)
426 Graphics_SetStageAddress(D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
427 if (polygon->texture_repeat_count.v == 1)
428 Graphics_SetStageAddress(D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
429 }
430
431 /* U spills to binary32 before multiplication. V stays in the x87 register. */
432 volatile float u_scale = (float)((long double)polygon->texture_repeat_count.u / texture->dimensions_minus_1.width);
433 long double v_scale = (long double)polygon->texture_repeat_count.v / texture->dimensions_minus_1.height;
434 float inset = (polygon->flags & MATERIAL_DEPTH_SORT) ? 0.005f : 0.0f;
435 struct D3D_TransformedVertex *vertices = (void *)&g_quadVertex0;
436 unsigned count = polygon->screen_vertices[3].packed_screen == GRAPHICS_TRIANGLE_FOURTH_VERTEX ? 3 : 4;
437 for (unsigned slot = 0; slot < count; ++slot)
438 {
439 unsigned i = Graphics_StripSourceIndex(slot, count);
440 vertices[slot].uv.u = (float)((long double)polygon->tex_uvs[i].u * u_scale + inset);
441 vertices[slot].uv.v = (float)((long double)polygon->tex_uvs[i].v * v_scale + inset);
442 }
443
444 IDirectDrawSurface7 *surface = texture->texture_handles[lod];
446 {
447 IDirect3DDevice7 *device = g_d3dDevice7;
449 device->lpVtbl->SetTexture(device, 0, surface);
450 }
451 D3D_SetTextureColorOperation(D3DTOP_MODULATE);
453 g_quadVertexPtrs = (void *)&vertices[0];
454 g_quadVertex1Ptr = (void *)&vertices[1];
455 g_quadVertex2Ptr = (void *)&vertices[2];
456 g_quadVertex3Ptr = (void *)&vertices[3];
457}
458
459static BOOL Graphics_PolygonNeedsClipping(const struct Graphics_Render_PolygonBatchRecord *polygon, unsigned count)
460{
461 for (unsigned i = 0; i < 4; ++i)
462 {
463 /* Unordered depths clip. The sentinel exempts only the fourth slot. */
464 if (!(polygon->view_vertices[i].z > Camera_SetupClipPlanes_NearClipDistanceSource) && i < count)
465 return TRUE;
466 }
467 for (unsigned i = 0; i < count; ++i)
468 {
469 const struct Graphics_Render_ScreenVertexI16Z *vertex = &polygon->screen_vertices[i];
470 if ((unsigned)vertex->screen.x >= GRAPHICS_DEFAULT_WIDTH || (unsigned)vertex->screen.y >= GRAPHICS_DEFAULT_HEIGHT)
471 return TRUE;
472 }
473 return FALSE;
474}
475
477 D3DPRIMITIVETYPE primitive, unsigned count, uint8_t alpha, BOOL textured)
478{
480 float reciprocal_z;
481 memcpy(&reciprocal_z, &g_reciprocalZ, sizeof(reciprocal_z));
482
483 for (unsigned slot = 0; slot < count; ++slot)
484 {
485 unsigned i = Graphics_StripSourceIndex(slot, count);
486 struct D3D_TransformedVertexNoTex *out = vertices[slot];
487 float z = polygon->view_vertices[i].z;
488 out->screen.x = polygon->screen_vertices[i].screen.x;
489 out->screen.y = polygon->screen_vertices[i].screen.y;
490 out->depth = (float)(((long double)polygon->depth_bias + z) / 65536.0L);
491 uint32_t red = g_quadVertexColorR[i], green = g_quadVertexColorG[i], blue = g_quadVertexColorB[i];
492 if (textured)
493 {
494 red = Graphics_ClampChannel(red * 2);
495 green = Graphics_ClampChannel(green * 2);
496 blue = Graphics_ClampChannel(blue * 2);
497 }
498 out->diffuse_color = ((uint32_t)alpha << 24) | (red << 16) | (green << 8) | blue;
499 out->rhw = (float)((long double)reciprocal_z / z);
500 }
501
502 if (primitive == D3DPT_LINESTRIP)
503 {
505 lines[count++] = lines[0];
506 }
508 {
509 float offset = polygon->face_normal_dot < 0 ? -0.0001f : 0.0001f;
510 for (unsigned slot = 0; slot < count; ++slot)
511 vertices[slot]->depth = (float)((long double)offset + vertices[slot]->depth);
512 }
513
514 /* The original clamps all four scratch slots, even for triangles. */
515 for (unsigned slot = 0; slot < 4; ++slot)
516 if (vertices[slot]->depth > 0.99f)
517 vertices[slot]->depth = 0.99f;
518
519 DWORD format = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;
520 if (textured)
521 format |= D3DFVF_TEX1;
522 IDirect3DDevice7 *device = g_d3dDevice7;
523 device->lpVtbl->DrawPrimitive(device, primitive, format, g_quadVertexPtrs, count, 0);
525}
526
527// Original pcdogs.exe: 0x4169b0
529{
530 uint32_t flags = polygon->flags;
532 polygon->depth_bias = 10.0f;
533
534 unsigned blend = 0;
535 if (flags & MATERIAL_BLEND_ENABLED)
536 blend = ((flags & MATERIAL_BLEND_MODE_MASK) >> MATERIAL_BLEND_MODE_SHIFT) + 1;
538 uint8_t alpha = D3D_SetBlendMode(blend);
540 g_quadVertexPtrs = NULL;
541
542 for (unsigned i = 0; i < 4; ++i)
543 {
544 uint32_t color = polygon->vertex_colors[i];
545 g_quadVertexColorR[i] = color & 255;
546 g_quadVertexColorG[i] = (color >> 8) & 255;
547 g_quadVertexColorB[i] = (color >> 16) & 255;
548 }
549
550 BOOL disable_depth = !polygon->texture_desc && g_currentGameMode == GAME_MODE_GAMEPLAY_3D;
551 if (disable_depth)
552 {
553 IDirect3DDevice7 *device = g_d3dDevice7;
554 device->lpVtbl->SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
555 }
556
557 uint32_t render_flags = polygon->render_state_flags;
558 BOOL wireframe = (render_flags & BATCH_RENDER_WIREFRAME) ||
560 struct Material_RuntimeDescriptor *texture = polygon->texture_desc;
561 BOOL textured = FALSE;
562 if (wireframe)
563 {
564 if (render_flags & BATCH_RENDER_STIPPLED)
565 {
566 IDirect3DDevice7 *device = g_d3dDevice7;
567 device->lpVtbl->SetRenderState(device, D3DRENDERSTATE_LINEPATTERN,
569 }
570 }
571 else if (texture)
572 {
573 int32_t lod = Graphics_SelectTextureLOD(polygon);
574 if (lod < 0)
577 {
578 Graphics_PreparePolygonTexture(polygon, texture, lod);
579 textured = TRUE;
580 }
581 }
582 else if (polygon->flags & MATERIAL_NEAR_LOD)
583 Graphics_TintPolygonColors(polygon, polygon->material_tint);
584
585 BOOL reverse_wire_vertices = FALSE;
586 if (!g_quadVertexPtrs)
587 {
588 D3D_SetTextureColorOperation(D3DTOP_DISABLE);
589 struct D3D_TransformedVertexNoTex *vertices = (void *)&g_untexturedVertexBuffer;
590 reverse_wire_vertices = wireframe && polygon->screen_vertices[3].packed_screen != GRAPHICS_TRIANGLE_FOURTH_VERTEX;
592 g_quadVertexPtrs = &vertices[0];
593 g_quadVertex1Ptr = &vertices[1];
594 g_quadVertex2Ptr = &vertices[reverse_wire_vertices ? 3 : 2];
595 g_quadVertex3Ptr = &vertices[reverse_wire_vertices ? 2 : 3];
596 }
598 {
599 for (unsigned i = 0; i < 4; ++i)
600 {
601 uint32_t red = g_quadVertexColorR[i], blue = g_quadVertexColorB[i];
602 if (g_quadVertexColorG[i] > red && g_quadVertexColorG[i] > blue)
603 g_quadVertexColorG[i] = red > blue ? red : blue;
604 }
605 }
606
607 unsigned count = polygon->screen_vertices[3].packed_screen == GRAPHICS_TRIANGLE_FOURTH_VERTEX ? 3 : 4;
608 if (Graphics_PolygonNeedsClipping(polygon, count))
609 Graphics_ClipAndDrawPolygon(polygon, (const void *)&g_quadVertex0, alpha, reverse_wire_vertices, textured);
610 else
611 Graphics_SubmitProjectedPolygon(polygon, wireframe ? D3DPT_LINESTRIP : D3DPT_TRIANGLESTRIP, count, alpha, textured);
612
613 if (polygon->texture_repeat_count.u > 1 || polygon->texture_repeat_count.v > 1)
614 {
615 Graphics_SetStageAddress(D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);
616 Graphics_SetStageAddress(D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
617 Graphics_SetStageAddress(D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
618 }
619 if (disable_depth)
620 {
621 IDirect3DDevice7 *device = g_d3dDevice7;
622 device->lpVtbl->SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
623 }
624 return 1;
625}
626
627// Original pcdogs.exe: 0x417e40
628// Draws a filled rectangle from packed signed coordinates and dimensions, applying blend mode and color intensity while temporarily disabling supported depth testing.
629void Graphics_DrawRectangle(int32_t packed_origin_xy, int32_t packed_size_wh, int32_t blend_flags, uint32_t rgb_color)
630{
631 int32_t left = (int32_t)(int16_t)packed_origin_xy;
632 uint8_t red = (uint8_t)rgb_color;
633 int32_t top = (int32_t)(int16_t)(packed_origin_xy >> 0x10);
634 // Extract X coordinate from packed pos_xy (low 16 bits)
635 int32_t rectangle_width = (int32_t)(int16_t)packed_size_wh;
636 // Extract Y coordinate from packed pos_xy (high 16 bits)
637 int32_t rectangle_height = (int32_t)(int16_t)(packed_size_wh >> 0x10);
638 uint8_t requested_blend_mode = (char)(blend_flags >> SPRITE_RENDER_BLEND_MODE_SHIFT) & (SPRITE_RENDER_BLEND_MODE_MASK >> SPRITE_RENDER_BLEND_MODE_SHIFT);
639 uint8_t green = (uint8_t)(rgb_color >> 8);
640 uint8_t blue = (uint8_t)(rgb_color >> 16);
641
642 if (requested_blend_mode >= GRAPHICS_BLEND_ADDITIVE_DIM)
643 requested_blend_mode = 0;
644
646
647 uint8_t alpha = (uint8_t)D3D_SetBlendMode(requested_blend_mode);
648
649 // Apply 12-bit fixed-point intensity modulation to RGB components
650 int32_t intensity_mode = blend_flags & GRAPHICS_RECT_INTENSITY_MODE_MASK;
651
653
654 if (intensity_mode)
655 {
656 if (intensity_mode != GRAPHICS_RECT_BLACK_ALPHA_MODE)
657 {
658 int32_t intensity_q12 = blend_flags & GRAPHICS_RECT_INTENSITY_VALUE_MASK;
659
660 if (!intensity_q12)
661 {
662 blue = 0;
663 green = 0;
664 // Disable lighting if hardware supports it (D3DRENDERSTATE_LIGHTING = 0)
665 red = 0;
666 }
667 else if (intensity_q12 < MATH_Q12_ONE)
668 {
669 red = ((uint32_t)red * intensity_q12 + 0x800) >> 0xc;
670 green =
671 (char)(((uint32_t)green * intensity_q12 + 0x800) >> 0xc);
672 blue = (char)(((uint32_t)blue * intensity_q12 + 0x800) >> 0xc);
673 }
674 }
675 else
676 {
677 red = 0;
678 blue = 0;
679 green = 0;
680 alpha = 0;
681 }
682 }
683
685 {
686 IDirect3DDevice7 *disable_depth_device = g_d3dDevice7;
687 // Set RHW (reciprocal homogeneous W) to magic constant 0x358637bd for screen-space rendering
688 disable_depth_device->lpVtbl->SetRenderState(disable_depth_device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
689 }
690
691 D3D_SetTextureColorOperation(D3DTOP_DISABLE);
692
693 long double left_precise = (long double)left;
694 float top_left_x = (float)left_precise;
695 long double top_precise = (long double)top;
696
697 // IDirect3DDevice7::DrawPrimitive(D3DPT_TRIANGLESTRIP, FVF=0x44, vertices, 4, 0)
698 float top_left_y = (float)top_precise;
699 long double right_precise = (long double)(rectangle_width + left);
700 float top_right_x = (float)right_precise;
701 float top_right_y = (float)top_precise;
702 float bottom_left_x = (float)left_precise;
703 uint32_t diffuse_color = ((uint32_t)alpha << 24) | ((uint32_t)red << 16) | ((uint32_t)green << 8) | blue;
704 long double bottom_precise = (long double)(rectangle_height + top);
705 float bottom_right_x = (float)right_precise;
706 float bottom_left_y = (float)bottom_precise;
707 float bottom_right_y = (float)bottom_precise;
708
709 IDirect3DDevice7 *draw_device = g_d3dDevice7;
710
711 struct
712 {
713 float x, y, z, rhw;
714 uint32_t color;
715 } vertices[4] = {
716 {top_left_x, top_left_y, 9.99999997e-7f, 9.99999997e-7f, (uint32_t)diffuse_color},
717 {top_right_x, top_right_y, 9.99999997e-7f, 9.99999997e-7f, (uint32_t)diffuse_color},
718 {bottom_left_x, bottom_left_y, 9.99999997e-7f, 9.99999997e-7f, (uint32_t)diffuse_color},
719 {bottom_right_x, bottom_right_y, 9.99999997e-7f, 9.99999997e-7f, (uint32_t)diffuse_color}};
720
721 draw_device->lpVtbl->DrawPrimitive(draw_device, D3DPT_TRIANGLESTRIP, (D3DFVF_XYZRHW | D3DFVF_DIFFUSE), vertices, 4, 0);
722
724 {
725 IDirect3DDevice7 *restore_depth_device = g_d3dDevice7;
726 restore_depth_device->lpVtbl->SetRenderState(restore_depth_device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
727 }
728}
729
730// Original pcdogs.exe: 0x4184a0
731// Draws four textures over the window quadrants, using triangle strips and shared blend and texture state.
733{
734 float half_width = (float)(uint32_t)(Graphics_WindowWidth / 2);
735 float half_height = (float)(uint32_t)(Graphics_WindowHeight / 2);
736 uint32_t depth_bits = 0x3f7fbe77;
737 float depth;
738
739 __builtin_memcpy(&depth, &depth_bits, sizeof(depth));
740
741 struct D3D_TransformedVertex vertices[4] = {
742 {{0.0f, 0.0f}, depth, depth, 0xffffffff, {0.0f, 0.0f}},
743 {{half_width, 0.0f}, depth, depth, 0xffffffff, {1.0f, 0.0f}},
744 {{0.0f, half_height}, depth, depth, 0xffffffff, {0.0f, 1.0f}},
745 {{half_width, half_height}, depth, depth, 0xffffffff, {1.0f, 1.0f}}};
746
747 for (int quadrant = 0; quadrant < 4; ++quadrant)
748 {
749 IDirect3DDevice7 *device = g_d3dDevice7;
750 IDirectDrawSurface7 *texture = texture_set->quadrants[quadrant];
752 device->lpVtbl->SetTexture(device, 0, texture);
753
754 if (quadrant == 0)
755 {
757 D3D_SetTextureColorOperation(D3DTOP_MODULATE);
758 }
759 else
760 {
761 float left = quadrant == 3 ? 0.0f : half_width;
762 float right = quadrant == 3 ? half_width : (float)Graphics_WindowWidth;
763 float top = quadrant == 1 ? 0.0f : half_height;
764 float bottom = quadrant == 1 ? half_height : (float)Graphics_WindowHeight;
765
766 vertices[0].screen.x = vertices[2].screen.x = left;
767 vertices[1].screen.x = vertices[3].screen.x = right;
768 vertices[0].screen.y = vertices[1].screen.y = top;
769 vertices[2].screen.y = vertices[3].screen.y = bottom;
770 }
771
772 device = g_d3dDevice7;
773 device->lpVtbl->DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1, vertices, 4, 0);
774 }
775}
776
777// Original pcdogs.exe: 0x42ccf0
778// Wraps a fullscreen four-corner gradient draw in a rendering pass with supported depth writes temporarily disabled.
779void Graphics_ClearScreenAndRenderRectangle(uint32_t top_left_color, uint32_t top_right_color, uint32_t bottom_left_color, uint32_t bottom_right_color)
780{
784 Graphics_WindowHeight << 0x10 | (uint32_t)(uint16_t)Graphics_WindowWidth, top_left_color,
785 top_right_color, bottom_left_color, bottom_right_color);
787
788 /* tailcall */
789 return Graphics_EndRendering();
790}
#define Camera_SetupClipPlanes_NearClipDistanceSource
Definition globals.h:90
void Debug_Log(char const *category, char const *message)
Definition diagnostics.c:17
@ GAME_MODE_GAMEPLAY_3D
Definition enums.h:38
#define g_currentGameMode
Definition globals.h:74
#define GRAPHICS_CAP_DEPTH_BUFFER
Definition constants.h:16
#define GRAPHICS_STIPPLE_PATTERN
Definition constants.h:56
#define GRAPHICS_TRIANGLE_FOURTH_VERTEX
Definition constants.h:30
#define GRAPHICS_SCREEN_QUAD_DEPTH_BITS
Definition constants.h:77
#define GRAPHICS_DEFAULT_WIDTH
Definition constants.h:25
#define GRAPHICS_RECT_INTENSITY_MODE_MASK
Definition constants.h:62
#define GRAPHICS_RECT_INTENSITY_VALUE_MASK
Definition constants.h:63
#define BATCH_RENDER_FORCE_SOLID
Definition constants.h:55
@ GRAPHICS_VERTEX_LAYOUT_TEXTURED
Definition constants.h:82
@ GRAPHICS_VERTEX_LAYOUT_COLOR
Definition constants.h:81
#define SPRITE_RENDER_BLEND_MODE_SHIFT
Definition constants.h:36
@ GRAPHICS_BLEND_ADDITIVE_DIM
Definition constants.h:47
#define GRAPHICS_CAP_TEXTURE_REPEAT
Definition constants.h:50
#define GRAPHICS_DEFAULT_HEIGHT
Definition constants.h:26
#define BATCH_RENDER_WIREFRAME
Definition constants.h:53
#define SPRITE_RENDER_BLEND_MODE_MASK
Definition constants.h:37
#define GRAPHICS_RECT_BLACK_ALPHA_MODE
Definition constants.h:64
#define GRAPHICS_STIPPLE_PATTERN_SHIFT
Definition constants.h:58
#define GRAPHICS_CAP_WIREFRAME
Definition constants.h:51
#define BATCH_RENDER_STIPPLED
Definition constants.h:54
#define GRAPHICS_STIPPLE_REPEAT
Definition constants.h:57
#define GRAPHICS_CAP_DEPTH_BUFFER_SHIFT
Definition constants.h:17
void Graphics_EndRendering(void)
Definition frame.c:156
void Graphics_DrawFilledRectangleGradient(int32_t packed_origin_xy, int32_t packed_size_wh, uint32_t top_left_rgb, uint32_t top_right_rgb, uint32_t bottom_left_rgb, uint32_t bottom_right_rgb)
Definition effects.c:183
void Graphics_ClipAndDrawPolygon(struct Graphics_Render_PolygonBatchRecord *batch, struct D3D_TransformedVertex const *prepared_vertices, uint8_t alpha_byte, int32_t unused_batch_alias, int32_t brighten_colors)
Definition clipping.c:187
int32_t Graphics_SelectTextureLOD(struct Graphics_Render_PolygonBatchRecord *batch)
Definition textures.c:57
void Graphics_IncrementPassCounter(void)
Definition frame.c:141
#define Graphics_WindowWidth
Definition globals.h:140
#define g_graphicsCapabilities_high_byte
Definition globals.h:335
#define g_quadVertexColorB
Definition globals.h:296
#define graphics_pixel_green_shift
Definition globals.h:326
#define g_quadVertexColorR
Definition globals.h:302
#define g_graphicsCapabilities
Definition globals.h:284
#define g_quadVertexColorG
Definition globals.h:299
#define Graphics_WindowHeight
Definition globals.h:143
#define graphics_text_rendering_mode
Definition globals.h:293
#define g_inputProcessedFlag
Definition globals.h:59
#define MATERIAL_BLEND_MODE_SHIFT
Definition constants.h:50
#define MATERIAL_BLEND_MODE_MASK
Definition constants.h:51
#define MATERIAL_DEPTH_SORT
Definition constants.h:39
#define MATERIAL_BLEND_ENABLED
Definition constants.h:29
#define MATERIAL_NEAR_LOD
Definition constants.h:31
#define MATH_Q12_ONE
Definition constants.h:9
char * D3D_FormatDirectXError(HRESULT error_code, char *out_buffer)
void D3D_SetZWriteEnableIfSupported(BOOL enable)
IDirectDrawSurface7 * D3D_CreateWorkSurface(IDirectDrawSurface7 *source_surface, uint32_t width, uint32_t height)
uint8_t D3D_SetBlendMode(int32_t blend_mode)
void D3D_SetTextureColorOperation(DWORD color_operation)
#define D3D_CreateTextureSurface_GraphicsPixelRedShift
Definition globals.h:234
#define g_quadVertex2Ptr
Definition globals.h:180
#define g_quadVertexPtrs
Definition globals.h:174
#define g_quadVertex3Ptr
Definition globals.h:183
#define g_quadVertex1Ptr
Definition globals.h:177
#define D3D_CreateTextureSurface_GraphicsPixelBlueShift
Definition globals.h:228
static void Graphics_SetStageAddress(D3DTEXTURESTAGESTATETYPE state, D3DTEXTUREADDRESS address)
Definition primitives.c:413
void Graphics_RenderTexturedQuad(uint32_t packed_xy, struct Material_TableEntry *material, uint32_t render_flags, uint32_t top_color, uint32_t bottom_color, int32_t quad_size)
Definition primitives.c:167
static BOOL Graphics_PolygonNeedsClipping(const struct Graphics_Render_PolygonBatchRecord *polygon, unsigned count)
Definition primitives.c:459
static unsigned Graphics_StripSourceIndex(unsigned slot, unsigned count)
Definition primitives.c:408
void Graphics_RenderTexturedQuadMaterialSize(uint32_t packed_xy, struct Material_TableEntry *material, uint32_t render_flags, uint32_t top_color, uint32_t bottom_color)
Definition primitives.c:241
static void Graphics_TintPolygonColors(const struct Graphics_Render_PolygonBatchRecord *polygon, uint32_t tint)
Definition primitives.c:397
void Graphics_ClearScreenAndRenderRectangle(uint32_t top_left_color, uint32_t top_right_color, uint32_t bottom_left_color, uint32_t bottom_right_color)
Definition primitives.c:779
static void Graphics_PreparePolygonTexture(struct Graphics_Render_PolygonBatchRecord *polygon, struct Material_RuntimeDescriptor *texture, unsigned lod)
Definition primitives.c:419
void Graphics_DrawRectangle(int32_t packed_origin_xy, int32_t packed_size_wh, int32_t blend_flags, uint32_t rgb_color)
Definition primitives.c:629
static uint32_t Graphics_ClampChannel(uint32_t value)
Definition primitives.c:392
static void Graphics_SubmitProjectedPolygon(const struct Graphics_Render_PolygonBatchRecord *polygon, D3DPRIMITIVETYPE primitive, unsigned count, uint8_t alpha, BOOL textured)
Definition primitives.c:476
void Graphics_DrawFullscreenTextureQuadrants(struct Material_BlendTextureSet *texture_set)
Definition primitives.c:732
int32_t Graphics_DrawQuad(struct Graphics_Render_PolygonBatchRecord *polygon)
Definition primitives.c:528
void Graphics_BlitTextureToQuadrants(IDirectDrawSurface7 **quadrant_surfaces, uint8_t *pixel_data, int32_t pixel_count, int32_t width, int32_t height)
Definition primitives.c:27
struct D3D_TransformedVertexNoTex::@374050323141060103245254175054163133141064310235 screen
struct D3D_TransformedVertex::@332353021317301242313220124156233255233130311077 screen
struct D3D_TransformedVertex::@022204046230112111334327023223004057003241046046 uv
struct Graphics_Render_ScreenVertexI16Z screen_vertices[0x4]
Definition types.h:512
struct Material_RuntimeDescriptor * texture_desc
Definition types.h:496
struct Graphics_Render_PolygonBatchRecord::@145031042204155336373271307321163127250224235021::@310256361111253022321370021321105232356207344074 texture_repeat_count
struct Math_UV8 tex_uvs[0x4]
Definition types.h:517
struct Math_Vec3F view_vertices[0x4]
Definition types.h:513
struct Graphics_Render_ScreenVertexI16Z::@276010272006271151340046314217100172252315123166::@167176104245357063215260323150214177117323053225 screen
IDirectDrawSurface7 * quadrants[0x4]
Definition types.h:38
uint32_t average_transparent_color
Definition types.h:270
IDirectDrawSurface7 * texture_handles[0x19]
Definition types.h:268
struct Math_SizeU8 dimensions_minus_1
Definition types.h:264
struct Material_RuntimeDescriptor * texture_descriptor
Definition types.h:353
uint32_t flags
Definition types.h:342
uint8_t width
Definition types.h:347
uint8_t height
Definition types.h:348
uint8_t v
Definition types.h:370
uint8_t u
Definition types.h:369
float z
Definition types.h:448
#define g_greenBitsToDiscard
Definition values.h:819
#define g_d3dDevice7
Definition values.h:792
#define g_quadVertex0
Definition values.h:177
#define g_renderingStateFlag
Definition values.h:627
#define g_currentVertexFormat
Definition values.h:795
#define g_reciprocalZ
Definition values.h:813
#define g_blueBitsToDiscard
Definition values.h:789
#define g_redBitsToDiscard
Definition values.h:807
#define g_untexturedVertexBuffer
Definition values.h:270
#define g_renderFrameCount
Definition values.h:552
#define g_currentBoundTextureSurface
Definition values.h:294