102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
textures.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
12#include <string.h>
13
14// Original pcdogs.exe: 0x413a40
15void Material_ReleaseTextureArray(IDirectDrawSurface7 **texture_handles)
16{
17 for (int i = 0; i < MATERIAL_TEXTURE_HANDLE_COUNT; ++i)
18 {
19 IDirectDrawSurface7 *surface = texture_handles[i];
20 if (surface)
21 {
22 texture_handles[i] = NULL;
24 surface->lpVtbl->Release(surface);
25 }
26 }
28 {
29 IDirect3DDevice7 *device = g_d3dDevice7;
31 device->lpVtbl->SetTexture(device, 0, NULL);
32 }
33}
34
35// Original pcdogs.exe: 0x413aa0
37{
39 {
40 descriptor->flags &= (uint16_t)~MATERIAL_DESCRIPTOR_HAS_SURFACES;
41 memset(descriptor->texture_handles, 0, sizeof(descriptor->texture_handles));
42 }
43 else
45 descriptor->average_transparent_color = UINT32_MAX;
46}
47
48// Original pcdogs.exe: 0x413ae0
49void Material_ReleaseSingleTexture(IDirectDrawSurface7 *surface)
50{
52 surface->lpVtbl->Release(surface);
53}
54
55// Original pcdogs.exe: 0x4140e0
56// Averages the nonblack colors horizontally adjacent to black pixels and writes the three channel means, or zeros when there are no samples. The nested loops require valid positive image dimensions.
57void Material_ComputeAvgTransparentColor(uint8_t *pixel_data, uint32_t width, uint32_t height, uint8_t *out_blue, uint8_t *out_green, uint8_t *out_red)
58{
59 uint32_t sums[3] = {0, 0, 0}, samples = 0, row = 0;
60 uint8_t previous[3] = {0, 0, 0};
61
62 /* Only colors adjacent horizontally to black pixels contribute. */
63 do
64 {
65 uint32_t col = 0;
66
67 do
68 {
69 uint8_t blue = pixel_data[0], green = pixel_data[1], red = pixel_data[2];
70
71 pixel_data += 4;
72
73 if (!(blue | green | red))
74 {
75 if ((int32_t)col > 0 && (previous[0] | previous[1] | previous[2]))
76 {
77 sums[0] += previous[0];
78 sums[1] += previous[1];
79 sums[2] += previous[2];
80 ++samples;
81 }
82
83 if (col < width - 1)
84 {
85 blue = pixel_data[0];
86 green = pixel_data[1];
87 red = pixel_data[2];
88
89 if (blue | green | red)
90 {
91 sums[0] += blue;
92 sums[1] += green;
93 sums[2] += red;
94 ++samples;
95 }
96 }
97 }
98
99 previous[0] = blue;
100 previous[1] = green;
101 previous[2] = red;
102 } while (++col < width);
103 } while (++row < height);
104
105 *out_red = 0;
106 *out_green = 0;
107 *out_blue = 0;
108
109 if (samples)
110 {
111 *out_blue = sums[0] / samples;
112 *out_green = sums[1] / samples;
113 *out_red = sums[2] / samples;
114 }
115}
116
117/* Nonzero RGB channels must survive quantization so only black is keyed. */
118static uint8_t quantize_channel(uint8_t value, uint32_t discard)
119{
120 uint8_t result = value >> (discard & 31);
121 return value && !result ? 1 : result;
122}
123
124static uint32_t pack_channel(uint8_t value, uint32_t shift)
125{
126 return (uint32_t)value << (shift & 31);
127}
128
129// Original pcdogs.exe: 0x414290
130void Material_CopyPixelDataToTexture(IDirectDrawSurface7 *surface, uint8_t *pixels, uint32_t unused_pixel_count, uint32_t width, uint32_t height)
131{
132 (void)unused_pixel_count;
133 DDSURFACEDESC2 desc = {.dwSize = sizeof(desc)};
134 if (!surface)
135 {
136 Debug_Log("CopyMem_Texture(): lpTex == NULL!", "Error");
137 return;
138 }
139
140 IDirectDrawSurface7 *work = D3D_CreateWorkSurface(surface, width, height);
141 if (!work)
142 {
143 Debug_Log("work == NULL.", "Error");
144 return;
145 }
146 HRESULT result = work->lpVtbl->Lock(work, NULL, &desc, DDLOCK_WAIT, NULL);
147 if (result < 0)
148 {
149 D3D_FormatDirectXError(result, NULL);
150 Debug_Log(g_errorMessageBuffer, "DirectX Error: Lock();");
151 return;
152 }
153 if (!desc.lpSurface)
154 {
155 Debug_Log("Cannot get surface pointer!", "DirectX Error");
156 return;
157 }
158
159 uint8_t edge_blue, edge_green, edge_red;
161 Material_ComputeAvgTransparentColor(pixels, width, height, &edge_blue, &edge_green, &edge_red);
162
163 const struct Math_ColorBGRA8 *src = (const struct Math_ColorBGRA8 *)pixels;
164 uint8_t *row = desc.lpSurface;
165 uint32_t y = 0;
166 do
167 {
168 uint16_t *dst = (uint16_t *)row;
169 uint32_t x = 0;
170 do
171 {
172 uint8_t r = quantize_channel(src->r, g_redBitsToDiscard);
173 uint8_t g = quantize_channel(src->g, g_greenBitsToDiscard);
174 uint8_t b = quantize_channel(src->b, g_blueBitsToDiscard);
175 uint8_t a = 0;
177 {
178 if (r | g | b)
179 a = 255u >> (g_alphaBitsToDiscard & 31);
180 else
181 {
182 /* The native keyed edge exchanges the red and blue means. */
183 r = edge_blue >> (g_redBitsToDiscard & 31);
184 g = edge_green >> (g_greenBitsToDiscard & 31);
185 b = edge_red >> (g_blueBitsToDiscard & 31);
186 }
187 }
192 ++src;
193 } while (++x < width);
194 row += desc.lPitch & ~1;
195 } while (++y < height);
196
197 work->lpVtbl->Unlock(work, NULL);
198 uint32_t fill = 0;
201 DDraw_FillSurfaceColor(surface, fill);
202 result = surface->lpVtbl->Blt(surface, NULL, work, NULL, DDBLT_WAIT, NULL);
203 if (result < 0)
204 {
205 D3D_FormatDirectXError(result, NULL);
206 Debug_Log(g_errorMessageBuffer, "DirectXError: Blt();");
207 }
208 work->lpVtbl->Release(work);
209}
210
211// Original pcdogs.exe: 0x414810
212// Reports whether the image contains a pixel with all three color channels zero; the alpha byte is ignored.
213bool Material_CheckHasTransparency(uint8_t *pixel_data, int32_t width, int32_t height)
214{
215 struct Math_ColorBGRA8 const *source_pixel = (struct Math_ColorBGRA8 const *)pixel_data;
216
217 for (int32_t row = 0; row < height; ++row)
218 {
219 for (int32_t column = 0; column < width; ++column)
220 {
221 if (!source_pixel->b && !source_pixel->g && !source_pixel->r)
222 return 1;
223
224 ++source_pixel;
225 }
226 }
227
228 return 0;
229}
230
231static struct Math_ColorBGRA8 expand_bgr555(uint16_t packed)
232{
233 return (struct Math_ColorBGRA8){.b = (packed & 31) << 3, .g = ((packed >> 5) & 31) << 3, .r = ((packed >> 10) & 31) << 3, .a = packed ? 255 : 0};
234}
235
236// Original pcdogs.exe: 0x414860
238{
241 struct Material_RuntimeDescriptor *descriptor = entry->texture_descriptor;
242 descriptor->average_transparent_color = UINT32_MAX;
243 memset(descriptor->texture_handles, 0, sizeof(descriptor->texture_handles));
244 unsigned width = descriptor->dimensions_minus_1.width + 1u;
245 unsigned height = descriptor->dimensions_minus_1.height + 1u;
246 unsigned count = width * height;
247 const uint8_t *data = descriptor->pixel_data;
248
250 {
251 const uint16_t *colors = (const uint16_t *)data;
252 for (unsigned i = 0; i < count; ++i)
253 pixels[i] = expand_bgr555(colors[i]);
254 }
255 else if (!(entry->pixel_format_flags & MATERIAL_PIXEL_PALETTED))
256 {
257 unsigned red = 0, green = 0, blue = 0, samples = 0;
258 for (unsigned i = 0; i < count; ++i)
259 {
260 uint8_t index = data[i];
261 struct Math_ColorBGRA8 color = expand_bgr555(descriptor->palette[index]);
262 if (index || color.a)
263 {
264 color.a = 255;
265 red += color.r;
266 green += color.g;
267 blue += color.b;
268 ++samples;
269 }
270 pixels[i] = color;
271 }
273 {
274 if (samples)
275 {
276 red /= samples;
277 green /= samples;
278 blue /= samples;
279 }
280 entry->material_tint = 0x80000000u | (blue << 16) | (green << 8) | red;
281 }
282 descriptor->average_transparent_color = entry->material_tint;
283 }
284 else
285 {
286 for (unsigned i = 0; i < count; ++i)
287 pixels[i] = (struct Math_ColorBGRA8){data[i], data[i], data[i], data[i]};
288 }
289
290 /* The base slot counts even when creation fails. The second slot counts on success. */
292 graphics_tex_needs_alpha = Material_CheckHasTransparency((uint8_t *)pixels, width, height);
295 for (unsigned slot = 0; slot < 2; ++slot)
296 {
297 IDirectDrawSurface7 *surface = D3D_CreateTextureSurface(width, height);
298 if (surface)
299 {
300 if (slot == 1)
302 Material_CopyPixelDataToTexture(surface, (uint8_t *)pixels, count, width, height);
303 descriptor->texture_handles[slot] = surface;
306 }
307 }
308}
309
310// Original pcdogs.exe: 0x42de20
311void Material_BuildTextureArray(struct Material_SectionHeader *section, struct Material_RefEntry *refs, int32_t material_count, int32_t first_material_index)
312{
314 struct Material_TableEntry *entry = &section->material_entries[first_material_index];
315 for (uint32_t i = 0; i < (uint32_t)material_count; ++i)
316 {
317 refs[i].material_table_entry = entry++;
318 uint32_t words = Material_BuildStructure(section, &refs[i], cursor);
319 cursor = (struct Material_FrameSet *)((uintptr_t)cursor + words * sizeof(uint32_t));
320 }
322}
323
324// Original pcdogs.exe: 0x42de90
325int32_t Material_FindTextureByFrame(struct Material_RefEntry *ref, int32_t frame, struct Graphics_Render_SpriteContext **out_sprite)
326{
327 struct Material_FrameSet *set = ref->frame_set;
328 if (set)
329 {
330 struct Material_FrameData **layer = set->frame_ptr_array;
331 uint32_t remaining = (int32_t)set->frame_count;
332 do
333 {
334 uint32_t index = (*layer++)->material_node_ref_packed;
335 if (index == MATERIAL_NODE_REFERENCE_NONE)
336 continue;
339 {
341 *out_sprite = sprite;
342 /* The matching layer sets the limit. The final layer supplies the table. */
343 set = ref->frame_set;
344 uint32_t last = set->frame_ptr_array[set->frame_count - 1]->material_node_ref_packed;
346 bool in_range = frame <= node->anim_record[5];
347 sprite->textureDescriptor = table[in_range ? frame : 0];
348 return in_range;
349 }
350 } while (--remaining);
351 }
352 *out_sprite = (struct Graphics_Render_SpriteContext *)ref->material_table_entry;
353 return 1;
354}
355
356// Original pcdogs.exe: 0x43caa0
358{
360 {
362 out_ref->material_table_entry = Shared_TextureDataRefsCursor->material_table_entry;
363 out_ref->frame_set = Shared_TextureDataRefsCursor->frame_set;
364 }
365}
366
367// Original pcdogs.exe: 0x43e8c0
369{
370 struct Material_TableEntry *entry = section->material_entries;
371 for (int i = 0; i < section->material_entry_count; ++i, ++entry)
372 {
374 {
377 }
378 }
379 struct Material_Node *node = section->node_table;
380 if (node)
381 {
382 uint32_t remaining = (int32_t)section->node_count;
383 do
384 {
386 {
387 unsigned frame = 0;
388 do
389 {
390 struct Material_RuntimeDescriptor *descriptor = node->frame_table[frame];
391 if (descriptor && (descriptor->flags_low & MATERIAL_DESCRIPTOR_PROCESSED))
392 {
393 Material_ClearTextureCache(descriptor);
394 descriptor->flags_low &= (uint8_t)~MATERIAL_DESCRIPTOR_PROCESSED;
395 }
396 } while (++frame <= node->anim_record[5]);
397 }
398 ++node;
399 } while (--remaining);
400 }
401}
402
403// Original pcdogs.exe: 0x43e950
404// Marks an entry as shared when an earlier flagged entry refers to the same texture descriptor.
405void Material_MarkSharedTextureReference(struct Material_SectionLoadView *section, struct Material_TableEntry *entry, int32_t entry_index)
406{
407 for (int32_t i = 0; i < entry_index; ++i)
408 {
409 const struct Material_TableEntry *prior = &section->material_entries[i];
411 {
413 return;
414 }
415 }
416}
417
418// Original pcdogs.exe: 0x43e9f0
420{
421 for (int i = 0; i < section->material_entry_count; ++i)
422 {
423 /* The native prepass only writes when both table cursors address this same entry. */
424 volatile uint32_t *flags = &section->material_entries[i].flags;
427 }
428 struct Material_Node *node = section->node_table;
429 if (node)
430 {
431 uint32_t remaining = (int32_t)section->node_count;
432 do
433 {
435 {
436 unsigned frame = 0;
437 do
438 {
439 struct Material_RuntimeDescriptor *descriptor = node->frame_table[frame];
440 if (descriptor)
441 Material_MarkReferencedByParent(section, node->material, descriptor, section->material_entry_count);
442 } while (++frame <= node->anim_record[5]);
443 }
444 ++node;
445 } while (--remaining);
446 }
447}
void Debug_Log(char const *category, char const *message)
Definition diagnostics.c:17
#define graphics_tex_needs_alpha
Definition globals.h:236
#define graphics_pixel_alpha_shift
Definition globals.h:320
#define graphics_pixel_green_shift
Definition globals.h:326
#define MATERIAL_NODE_INDEX_MASK
Definition constants.h:17
#define MATERIAL_REFERENCE_SHARED_DESCRIPTOR
Definition constants.h:13
#define MATERIAL_MAX_EXPANDED_PIXEL_BYTES
Definition constants.h:22
#define MATERIAL_DESCRIPTOR_FORMAT_HIGH
Definition constants.h:8
@ MATERIAL_NODE_TEXTURE_FRAMES
Definition constants.h:45
#define MATERIAL_SHARED_TEXTURE
Definition constants.h:14
#define MATERIAL_DESCRIPTOR_FORMAT_LOW
Definition constants.h:7
#define MATERIAL_DESCRIPTOR_HAS_SURFACES
Definition constants.h:9
#define MATERIAL_TINT_PRECOMPUTED
Definition constants.h:16
#define MATERIAL_NODE_REFERENCE_NONE
Definition constants.h:18
#define MATERIAL_DESCRIPTOR_PROCESSED
Definition constants.h:10
#define MATERIAL_TEXTURE_HANDLE_COUNT
Definition constants.h:48
#define MATERIAL_SHARED_TEXTURE_UNSIGNED
Definition constants.h:15
#define MATERIAL_PIXEL_PALETTED
Definition constants.h:11
void Material_MarkReferencedByParent(struct Material_SectionHeader *material_section, struct Material_TableEntry *material_ref, struct Material_RuntimeDescriptor *parent_texture_descriptor, int32_t material_count)
Definition resources.c:97
void Material_ReleaseSingleTexture(IDirectDrawSurface7 *texture_surface)
Definition textures.c:49
int32_t Material_BuildStructure(struct Material_SectionHeader *section, struct Material_RefEntry *ref, struct Material_FrameSet *frame_set)
Definition resources.c:28
static struct Math_ColorBGRA8 expand_bgr555(uint16_t packed)
Definition textures.c:231
void Material_MarkSharedTextureReference(struct Material_SectionLoadView *section, struct Material_TableEntry *entry, int32_t entry_index)
Definition textures.c:405
void Material_ComputeAvgTransparentColor(uint8_t *pixel_data, uint32_t width, uint32_t height, uint8_t *out_blue, uint8_t *out_green, uint8_t *out_red)
Definition textures.c:57
int32_t Material_FindTextureByFrame(struct Material_RefEntry *ref, int32_t frame, struct Graphics_Render_SpriteContext **out_sprite)
Definition textures.c:325
void Material_CopyPixelDataToTexture(IDirectDrawSurface7 *surface, uint8_t *pixels, uint32_t unused_pixel_count, uint32_t width, uint32_t height)
Definition textures.c:130
bool Material_CheckHasTransparency(uint8_t *pixel_data, int32_t width, int32_t height)
Definition textures.c:213
void Material_LoadTexture(struct Material_TableEntry *entry)
Definition textures.c:237
void Material_MarkNodeTextureReferences(struct Material_SectionHeader *section)
Definition textures.c:419
void Material_BuildTextureArray(struct Material_SectionHeader *section, struct Material_RefEntry *refs, int32_t material_count, int32_t first_material_index)
Definition textures.c:311
void Material_GetGlobalTextureRefs(struct Material_RefEntry *out_ref)
Definition textures.c:357
void Material_ClearTextureCache(struct Material_RuntimeDescriptor *descriptor)
Definition textures.c:36
void Material_ReleaseSectionTextures(struct Material_SectionHeader *section)
Definition textures.c:368
static uint8_t quantize_channel(uint8_t value, uint32_t discard)
Definition textures.c:118
static uint32_t pack_channel(uint8_t value, uint32_t shift)
Definition textures.c:124
void Material_ReleaseTextureArray(IDirectDrawSurface7 **texture_handles)
Definition textures.c:15
#define g_sharedMaterialSection
Definition globals.h:108
#define Shared_MaterialFrameSetCursor
Definition globals.h:105
#define Shared_TextureDataRefsCursor
Definition globals.h:114
char * D3D_FormatDirectXError(HRESULT error_code, char *out_buffer)
IDirectDrawSurface7 * D3D_CreateTextureSurface(uint32_t width, uint32_t height)
BOOL DDraw_FillSurfaceColor(IDirectDrawSurface7 *surface, uint32_t fill_color)
IDirectDrawSurface7 * D3D_CreateWorkSurface(IDirectDrawSurface7 *source_surface, uint32_t width, uint32_t height)
#define D3D_CreateTextureSurface_GraphicsPixelRedShift
Definition globals.h:234
#define D3D_CreateTextureSurface_GraphicsPixelBlueShift
Definition globals.h:228
struct Material_RuntimeDescriptor * textureDescriptor
Definition types.h:570
uint32_t material_node_ref_packed
Definition types.h:51
struct Material_FrameData ** frame_ptr_array
Definition types.h:63
int16_t frame_count
Definition types.h:62
uint8_t * anim_record
Definition types.h:91
struct Material_TableEntry * material
Definition types.h:86
uint8_t node_type
Definition types.h:87
struct Material_RuntimeDescriptor ** frame_table
Definition types.h:92
struct Material_SectionHeader * material_section
Definition types.h:134
struct Material_TableEntry * material_table_entry
Definition types.h:135
struct Material_FrameSet * frame_set
Definition types.h:136
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
int16_t material_entry_count
Definition types.h:151
struct Material_Node * node_table
Definition types.h:146
struct Material_TableEntry * material_entries
Definition types.h:149
struct Material_TableEntry * material_entries
Definition types.h:181
uint8_t pixel_format_flags
Definition types.h:348
struct Material_RuntimeDescriptor * texture_descriptor
Definition types.h:353
uint32_t material_tint
Definition types.h:357
uint32_t flags
Definition types.h:342
struct Math_ColorRGBA8 tint_channels
Definition types.h:358
uint8_t texture_reference_flags
Definition types.h:347
uint8_t b
Definition types.h:25
uint8_t g
Definition types.h:26
uint8_t r
Definition types.h:27
uint8_t a
Definition types.h:28
uint8_t a
Definition types.h:53
uint8_t width
Definition types.h:347
uint8_t height
Definition types.h:348
#define g_greenBitsToDiscard
Definition values.h:819
#define g_d3dDevice7
Definition values.h:792
#define g_alphaBitsToDiscard
Definition values.h:816
#define g_blueBitsToDiscard
Definition values.h:789
#define g_redBitsToDiscard
Definition values.h:807
#define g_errorMessageBuffer
Definition values.h:315
#define g_activeTextureCount
Definition values.h:267
#define g_currentBoundTextureSurface
Definition values.h:294