From a445a366fca194b464c37c179c768b280f1c6d91 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Mon, 13 Jul 2026 07:11:23 -0500 Subject: [PATCH] rebuild iggy's native fill styles --- .../src/reconstructed/CMakeLists.txt | 11 + iggy-decompiled/src/reconstructed/README.md | 7 + iggy-decompiled/src/reconstructed/modules.tsv | 1 + .../src/reconstructed/swf_draw_filltype.c | 533 ++++++++++++++++++ .../src/reconstructed/swf_draw_filltype.h | 81 +++ .../src/reconstructed/swf_draw_renderstate.h | 4 +- .../support/swf_draw_filltype_test_support.c | 181 ++++++ tests/test_reconstructed_swf_draw_filltype.py | 387 +++++++++++++ ...t_reconstructed_swf_draw_filltype_cross.py | 111 ++++ 9 files changed, 1315 insertions(+), 1 deletion(-) create mode 100644 iggy-decompiled/src/reconstructed/swf_draw_filltype.c create mode 100644 iggy-decompiled/src/reconstructed/swf_draw_filltype.h create mode 100644 tests/support/swf_draw_filltype_test_support.c create mode 100644 tests/test_reconstructed_swf_draw_filltype.py create mode 100644 tests/test_reconstructed_swf_draw_filltype_cross.py diff --git a/iggy-decompiled/src/reconstructed/CMakeLists.txt b/iggy-decompiled/src/reconstructed/CMakeLists.txt index e0a2653..1b31cbf 100644 --- a/iggy-decompiled/src/reconstructed/CMakeLists.txt +++ b/iggy-decompiled/src/reconstructed/CMakeLists.txt @@ -159,6 +159,17 @@ foreach(platform IN ITEMS durango orbis ps3 vita) configure_iggy_reconstructed_target(${target} c_std_11) endforeach() +foreach(platform IN ITEMS durango orbis ps3 vita) + string(TOUPPER ${platform} platform_upper) + set(target iggy_reconstructed_swf_draw_filltype_${platform}) + add_library(${target} STATIC swf_draw_filltype.c) + target_compile_definitions( + ${target} + PRIVATE IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_${platform_upper} + ) + configure_iggy_reconstructed_target(${target} c_std_11) +endforeach() + add_library( iggy_reconstructed_as3vm_debugger_interface STATIC diff --git a/iggy-decompiled/src/reconstructed/README.md b/iggy-decompiled/src/reconstructed/README.md index 769b0cc..80d8146 100644 --- a/iggy-decompiled/src/reconstructed/README.md +++ b/iggy-decompiled/src/reconstructed/README.md @@ -92,6 +92,13 @@ exact color normalization, stencil and scissor propagation, cull intersections, target rounding imports, GDraw callbacks, statistics, and telemetry while leaving the SWF/AVM2 runtime unchanged. +`swf_draw_filltype.c` restores the five native fill-style and bitmap render-state +exports plus the smoothing control used by the existing SWF renderer. Its four +variants preserve the 32/64-bit fill and resource layouts, texture lock/rebuild +ownership, solid morph colors, affine gradient and bitmap texgen, spread and +smoothing modes, focal parameters, color transforms, telemetry, and target NaN +behavior without replacing the SWF/AVM2 UI path. + `as3vm_debugger_interface.c` and `as3vm_op_strings.c` preserve the three intentional zero-return stubs in the shipping archives. Every platform object contains the same compiled-out behavior with no debugger metadata, opcode diff --git a/iggy-decompiled/src/reconstructed/modules.tsv b/iggy-decompiled/src/reconstructed/modules.tsv index 319aae2..b937342 100644 --- a/iggy-decompiled/src/reconstructed/modules.tsv +++ b/iggy-decompiled/src/reconstructed/modules.tsv @@ -14,6 +14,7 @@ swf_draw_filter 5 5 durango,orbis,ps3,vita complete iggy_mixer 7 7 durango,orbis,ps3,vita complete swf_draw_lib 16 5 durango,orbis,ps3,vita complete swf_draw_renderstate 8 8 durango,orbis,ps3,vita complete +swf_draw_filltype 11 5 durango,orbis,ps3,vita complete as3vm_debugger_interface 2 2 durango,orbis,ps3,vita complete as3vm_op_strings 1 1 durango,orbis,ps3,vita complete iggy_platform_audio 2 2 durango complete diff --git a/iggy-decompiled/src/reconstructed/swf_draw_filltype.c b/iggy-decompiled/src/reconstructed/swf_draw_filltype.c new file mode 100644 index 0000000..2dfd47a --- /dev/null +++ b/iggy-decompiled/src/reconstructed/swf_draw_filltype.c @@ -0,0 +1,533 @@ +#include "swf_draw_filltype.h" + +#include + +#if (defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_DURANGO) + \ + defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_ORBIS) + \ + defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_PS3) + \ + defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_VITA)) != 1 +#error "select exactly one swf_draw_filltype target" +#endif + +#if UINTPTR_MAX == UINT64_MAX +_Static_assert(offsetof(IggyFillStyle, cached_texture) == 0x08, "fill cache"); +_Static_assert(offsetof(IggyFillStyle, initial) == 0x10, "fill initial"); +_Static_assert(offsetof(IggyFillStyle, morph) == 0x28, "fill morph"); +_Static_assert(offsetof(IggyFillStyle, resource) == 0x40, "fill resource"); +_Static_assert(sizeof(IggyFillStyle) == 0x48, "fill size"); +_Static_assert(offsetof(IggyBitmap, width) == 0x20, "bitmap width"); +_Static_assert(offsetof(IggyBitmap, scale_x) == 0x30, "bitmap scale"); +_Static_assert(offsetof(IggyBitmapResource, cached_texture) == 0x28, "bitmap texture"); +_Static_assert(offsetof(IggyDirectTexture, cached_texture) == 0x18, "direct texture"); +_Static_assert(offsetof(IggyDirectTexture, width) == 0x28, "direct width"); +#else +_Static_assert(offsetof(IggyFillStyle, cached_texture) == 0x04, "fill cache"); +_Static_assert(offsetof(IggyFillStyle, initial) == 0x08, "fill initial"); +_Static_assert(offsetof(IggyFillStyle, morph) == 0x20, "fill morph"); +_Static_assert(offsetof(IggyFillStyle, resource) == 0x38, "fill resource"); +_Static_assert(sizeof(IggyFillStyle) == 0x3c, "fill size"); +_Static_assert(offsetof(IggyBitmap, width) == 0x10, "bitmap width"); +_Static_assert(offsetof(IggyBitmap, scale_x) == 0x1c, "bitmap scale"); +_Static_assert(offsetof(IggyBitmapResource, cached_texture) == 0x18, "bitmap texture"); +_Static_assert(offsetof(IggyDirectTexture, cached_texture) == 0x0c, "direct texture"); +_Static_assert(offsetof(IggyDirectTexture, width) == 0x14, "direct width"); +#endif +_Static_assert( + offsetof(IggyRenderColorTransform, additive) == 0x14, + "color transform additive offset"); +_Static_assert(sizeof(IggyRenderColorTransform) == 0x1c, "color transform size"); + +typedef struct FilltypeTelemetry { + void *reserved[20]; + void (*begin)(struct FilltypeTelemetry *, ...); + void (*end)(struct FilltypeTelemetry *, ...); +} FilltypeTelemetry; + +extern FilltypeTelemetry *iggy_tm_internal; +extern IggyRenderTexture iggy_direct_texture_rebuild( + void *iggy, IggyDirectTexture *texture); +extern void iggy_gdraw_FreeTexture( + void *iggy, IggyRenderTexture texture, IggyRenderTexture *cache); +extern int32_t iggy_gdraw_TryToLockTexture( + void *iggy, IggyRenderTexture texture, IggyRenderTexture *cache); +extern IggyRenderTexture iggy_get_bitmap_texture( + void *iggy, void *bitmap, int32_t type, int32_t *has_alpha, + int32_t *width, int32_t *height, float scale_x, float scale_y); +extern IggyRenderTexture iggy_make_gradient_texture( + void *iggy, IggyRenderTexture *cache, IggyFillStyle *fill_style, + float morph, int32_t linear_rgb); + +int32_t iggy_force_default_smoothing; + +#if !defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_ORBIS) +#if defined(IGGY_SWF_DRAW_FILLTYPE_TEST) +int32_t texgen_debug; +float texgen_debug_s; +float texgen_debug_t; +#else +static int32_t texgen_debug; +static float texgen_debug_s; +static float texgen_debug_t; +#endif +#endif + +static const char source_path[] = + "c:\\projects\\g\\src\\flash\\swf_draw_filltype.c"; +static uint32_t telemetry_format; + +typedef struct InverseMatrix { + float a; + float b; + float c; + float d; + float tx; + float ty; +} InverseMatrix; + +static float interpolate(float initial, float morph_value, float morph) { + return initial + (morph_value - initial) * morph; +} + +static float absolute_float(float value) { + return __builtin_fabsf(value); +} + +static float maximum_by_comparison(float first, float second) { + return first <= second ? second : first; +} + +static uint8_t spread_mode_to_wrap(uint8_t spread_mode) { + if (spread_mode == 2U) return 1U; + if (spread_mode == 1U) return 2U; + return 0U; +} + +static void telemetry_begin(void) { + FilltypeTelemetry *const telemetry = iggy_tm_internal; + if (telemetry == NULL) return; +#if defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_VITA) + telemetry->begin( + telemetry, 0, 0, 0, 0, 0, 0, + source_path, 0x16d, &telemetry_format, "iggy_setup_fill_style"); +#elif defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_PS3) + telemetry->begin( + telemetry, 0, 0, 0, 0, source_path, 0x16d, &telemetry_format); +#else + telemetry->begin( + telemetry, 0, 0, 0, 0, source_path, 0x16d, + &telemetry_format, "iggy_setup_fill_style"); +#endif +} + +static void telemetry_end(void) { + FilltypeTelemetry *const telemetry = iggy_tm_internal; + if (telemetry == NULL) return; +#if defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_VITA) + telemetry->end(telemetry, 0x1ea, 0, 0, 0, source_path, 0x1ea); +#else + telemetry->end(telemetry, 0, 0, source_path, 0x1ea); +#endif +} + +static void apply_initial_transform( + IggyRenderState *state, const IggyRenderColorTransform *transform) { + if (transform != NULL && transform->multiply_enabled != 0) { + for (size_t index = 0; index < 4; ++index) { + state->color[index] = transform->multiply[index]; + } + } + state->color_transform_add = + transform != NULL && transform->additive_enabled != 0 + ? (int16_t *)transform->additive + : NULL; +} + +static void install_additive_transform( + IggyRenderState *state, const IggyRenderColorTransform *transform) { + if (transform != NULL && transform->additive_enabled != 0) { + state->color_transform_add = (int16_t *)transform->additive; + } +} + +static float clamp_rgb(float value) { + return value < 0.0f ? 0.0f : value; +} + +static float clamp_alpha(float value) { + if (value != value) { +#if defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_PS3) + return 1.0f; +#elif defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_VITA) + return value; +#else + return 0.0f; +#endif + } + if (value < 0.0f) return 0.0f; + if (value > 1.0f) return 1.0f; + return value; +} + +static void apply_final_transform( + IggyRenderState *state, const IggyRenderColorTransform *transform) { + if (transform == NULL || transform->multiply_enabled == 0) return; + for (size_t index = 0; index < 3; ++index) { + state->color[index] = + clamp_rgb(state->color[index] * transform->multiply[index]); + } + state->color[3] = + clamp_alpha(state->color[3] * transform->multiply[3]); +} + +static int32_t use_bounds_transform( + const void *iggy, const IggyRenderBounds *new_bounds) { + if (iggy == NULL || new_bounds == NULL) return 0; +#if UINTPTR_MAX == UINT64_MAX + const size_t first_flag = 0x2298U; +#else + const size_t first_flag = 0x1cfcU; +#endif + const int32_t *const flags = + (const int32_t *)((const uint8_t *)iggy + first_flag); + return flags[0] != 0 && flags[1] != 0; +} + +static InverseMatrix inverse_matrix( + const IggyFillMatrix *initial, const IggyFillMatrix *morph_matrix, + float morph, const void *iggy, + const IggyRenderBounds *new_bounds, const IggyRenderBounds *old_bounds) { + float values[6]; + for (size_t index = 0; index < 6; ++index) { + values[index] = initial->values[index]; + if (morph > 0.0f) { + values[index] = interpolate( + values[index], morph_matrix->values[index], morph); + } + } + const float inverse_determinant = + 1.0f / (values[3] * values[0] - values[2] * values[1]); + InverseMatrix result; + result.a = values[3] * inverse_determinant; + result.b = -values[1] * inverse_determinant; + result.c = -values[2] * inverse_determinant; + result.d = values[0] * inverse_determinant; + result.tx = -result.a * values[4] - result.b * values[5]; + result.ty = -result.c * values[4] - result.d * values[5]; + if (use_bounds_transform(iggy, new_bounds)) { + const float scale_x = + (new_bounds->maximum_x - new_bounds->minimum_x) / + (old_bounds->maximum_x - old_bounds->minimum_x); + const float scale_y = + (new_bounds->maximum_y - new_bounds->minimum_y) / + (old_bounds->maximum_y - old_bounds->minimum_y); + const float old_a = result.a; + const float old_b = result.b; + const float old_c = result.c; + const float old_d = result.d; + result.a *= scale_x; + result.b *= scale_y; + result.c *= scale_x; + result.d *= scale_y; + result.tx = result.tx + old_a * new_bounds->minimum_x + + old_b * new_bounds->minimum_y - + result.a * old_bounds->minimum_x - + result.b * old_bounds->minimum_y; + result.ty = result.ty + old_c * new_bounds->minimum_x + + old_d * new_bounds->minimum_y - + result.c * old_bounds->minimum_x - + result.d * old_bounds->minimum_y; + } + return result; +} + +static void set_gradient_texgen( + IggyRenderState *state, const InverseMatrix *matrix, uint8_t type) { + state->texgen0_enabled = 1; + state->texture_s[2] = 0.0f; + if (type == 0x10U) { + const float scale = 0.000030517578125f; + state->texture_s[0] = matrix->a * scale; + state->texture_s[1] = matrix->b * scale; + state->texture_s[3] = matrix->tx * scale + 0.5f; + for (size_t index = 0; index < 4; ++index) state->texture_t[index] = 0.0f; + return; + } + const float scale = 0.00006103515625f; + state->texture_s[0] = matrix->a * scale; + state->texture_s[1] = matrix->b * scale; + state->texture_s[3] = matrix->tx * scale; + state->texture_t[0] = matrix->c * scale; + state->texture_t[1] = matrix->d * scale; + state->texture_t[2] = 0.0f; + state->texture_t[3] = matrix->ty * scale; +} + +static void get_texgen_scale( + const IggyFillStyle *fill_style, float morph, + const IggyFillMatrix *extra_matrix, float *scale_x, float *scale_y) { + float values[4]; + for (size_t index = 0; index < 4; ++index) { + values[index] = fill_style->initial.matrix.values[index]; + if (morph != 0.0f) { + values[index] = interpolate( + values[index], fill_style->morph.matrix.values[index], morph); + } + } + *scale_x = maximum_by_comparison( + absolute_float(values[0]), absolute_float(values[1])); + *scale_y = maximum_by_comparison( + absolute_float(values[2]), absolute_float(values[3])); + if (extra_matrix != NULL) { + *scale_x *= maximum_by_comparison( + absolute_float(extra_matrix->values[0]), + absolute_float(extra_matrix->values[1])); + *scale_y *= maximum_by_comparison( + absolute_float(extra_matrix->values[2]), + absolute_float(extra_matrix->values[3])); + } +} + +void IggyDebugTexgen(float scale_s, float scale_t) { +#if defined(IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_ORBIS) + (void)scale_s; + (void)scale_t; +#else + if (scale_s == 1.0f && scale_t == 1.0f) { + texgen_debug = 0; + return; + } + texgen_debug_s = scale_s; + texgen_debug_t = scale_t; + texgen_debug = 1; +#endif +} + +IggyRenderTexture iggy_get_fill_style_texture( + void *iggy, const IggyFillStyle *fill_style) { + (void)iggy; + if (fill_style->type == 0x10U || fill_style->type == 0x12U || + fill_style->type == 0x13U) { + return fill_style->cached_texture; + } + if (fill_style->type >= 0x40U && fill_style->type <= 0x43U && + fill_style->resource != NULL) { + return ((const IggyBitmapResource *)fill_style->resource)->cached_texture; + } + return 0; +} + +void iggy_setup_bitmap_renderstate( + void *iggy, IggyRenderState *state, IggyBitmap *bitmap, + const IggyRenderColorTransform *transform, + int32_t clamp, int32_t nearest) { + int32_t has_alpha; + int32_t texture_width; + int32_t texture_height; + iggy_clear_renderstate(iggy, state); + apply_initial_transform(state, transform); + const IggyRenderTexture texture = iggy_get_bitmap_texture( + iggy, bitmap, 0x41U, &has_alpha, &texture_width, &texture_height, + 0.25f, 0.25f); + if (texture == 0) return; + (void)texture_width; + (void)texture_height; + state->textures[0] = texture; + state->texture_mode = 1; + state->wrap_mode = clamp == 0; + state->nearest = nearest != 0; + state->blend_mode = has_alpha != 0; + state->texture_s[0] = bitmap->scale_x / (float)bitmap->width; + state->texture_s[1] = 0.0f; + state->texture_s[2] = 0.0f; + state->texture_s[3] = bitmap->offset_x; + state->texture_t[0] = 0.0f; + state->texture_t[1] = bitmap->scale_y / (float)bitmap->height; + state->texture_t[2] = 0.0f; + state->texture_t[3] = bitmap->offset_y; + state->texgen0_enabled = 1; +} + +void iggy_setup_bitmap_texture_renderstate( + void *iggy, IggyRenderState *state, IggyDirectTexture *texture, + const IggyRenderColorTransform *transform, + int32_t clamp, int32_t nearest) { + iggy_clear_renderstate(iggy, state); + apply_initial_transform(state, transform); + IggyRenderTexture handle = texture->cached_texture; + if (handle == 0) { + handle = iggy_direct_texture_rebuild(iggy, texture); + if (handle == 0) return; + if (iggy_gdraw_TryToLockTexture( + iggy, handle, &texture->cached_texture) == 0) { + texture->cached_texture = 0; + return; + } + } else if (iggy_gdraw_TryToLockTexture( + iggy, handle, &texture->cached_texture) == 0) { + texture->cached_texture = 0; + handle = iggy_direct_texture_rebuild(iggy, texture); + if (handle == 0) return; + if (iggy_gdraw_TryToLockTexture( + iggy, handle, &texture->cached_texture) == 0) { + texture->cached_texture = 0; + return; + } + } + state->textures[0] = handle; + state->texture_mode = 1; + state->wrap_mode = clamp == 0; + state->nearest = nearest != 0; + state->blend_mode = 1; + state->texture_s[0] = 1.0f / (float)texture->width; + state->texture_s[1] = 0.0f; + state->texture_s[2] = 0.0f; + state->texture_s[3] = 0.0f; + state->texture_t[0] = 0.0f; + state->texture_t[1] = 1.0f / (float)texture->height; + state->texture_t[2] = 0.0f; + state->texture_t[3] = 0.0f; + state->texgen0_enabled = 1; +} + +static void setup_solid_fill( + IggyRenderState *state, const IggyFillStyle *fill_style, float morph) { + const uint8_t *const initial = fill_style->initial.rgba; + const uint8_t *const final = fill_style->morph.rgba; + iggy_setcolor_4ub( + state, initial[0], initial[1], initial[2], initial[3]); + state->blend_mode = + initial[3] != 255U || (morph > 0.0f && final[3] != 255U); + if (morph > 0.0f) { + const float byte_to_float = 0.003921568859368563f; + float color[4]; + for (size_t index = 0; index < 4; ++index) { + color[index] = + ((float)initial[index] + + (float)((int32_t)final[index] - (int32_t)initial[index]) * morph) * + byte_to_float; + } + iggy_setcolor_4f( + state, color[0], color[1], color[2], color[3]); + } +} + +static void setup_gradient_fill( + void *iggy, IggyRenderState *state, IggyFillStyle *fill_style, + float morph, const IggyRenderBounds *new_bounds, + const IggyRenderBounds *old_bounds) { + IggyRenderTexture texture = fill_style->cached_texture; + if (texture != 0) { + if (morph > 0.0f) { + iggy_gdraw_FreeTexture(iggy, texture, &fill_style->cached_texture); + texture = 0; + } else if (iggy_gdraw_TryToLockTexture( + iggy, texture, &fill_style->cached_texture) == 0) { + fill_style->cached_texture = 0; + texture = 0; + } else { + texture = fill_style->cached_texture; + } + } + const uint8_t *const resource = (const uint8_t *)fill_style->resource; + if (texture == 0) { + texture = iggy_make_gradient_texture( + iggy, &fill_style->cached_texture, fill_style, + morph, resource[1] == 1U); + fill_style->cached_texture = texture; + } + state->textures[0] = texture; + state->wrap_mode = spread_mode_to_wrap(resource[0]); + state->blend_mode = 1; + const InverseMatrix matrix = inverse_matrix( + &fill_style->initial.matrix, &fill_style->morph.matrix, + morph, iggy, new_bounds, old_bounds); + set_gradient_texgen(state, &matrix, fill_style->type); + if (fill_style->type == 0x13U) { + state->texture_mode = 4; + const float focal = *(const float *)(resource + 4) * -0.9990000128746033f; + const float factor = 1.0f / (1.0f - focal * focal); + state->focal_point[0] = focal; + state->focal_point[1] = factor * focal; + state->focal_point[2] = factor; + state->focal_point[3] = 0.0f; + } else { + state->texture_mode = fill_style->type == 0x12U ? 3U : 1U; + } +} + +static int32_t setup_bitmap_fill( + void *iggy, IggyRenderState *state, IggyFillStyle *fill_style, + float morph, const IggyRenderBounds *new_bounds, + const IggyRenderBounds *old_bounds, + const IggyFillMatrix *extra_matrix) { + float requested_scale_x; + float requested_scale_y; + int32_t has_alpha; + int32_t texture_width; + int32_t texture_height; + get_texgen_scale( + fill_style, morph, extra_matrix, + &requested_scale_x, &requested_scale_y); + IggyBitmapResource *const resource = + (IggyBitmapResource *)fill_style->resource; + const IggyRenderTexture texture = iggy_get_bitmap_texture( + iggy, resource, fill_style->type, + &has_alpha, &texture_width, &texture_height, + requested_scale_x, requested_scale_y); + if (texture == 0) return 0; + state->textures[0] = texture; + state->texture_mode = 1; + state->blend_mode = has_alpha != 0; + const InverseMatrix matrix = inverse_matrix( + &fill_style->initial.matrix, &fill_style->morph.matrix, + morph, iggy, new_bounds, old_bounds); + const float width = (float)texture_width; + const float height = (float)texture_height; + state->texture_s[0] = matrix.a / width * resource->scale_x; + state->texture_s[1] = matrix.b / width * resource->scale_x; + state->texture_s[2] = 0.0f; + state->texture_s[3] = + matrix.tx / width * resource->scale_x + resource->offset_x; + state->texture_t[0] = matrix.c / height * resource->scale_y; + state->texture_t[1] = matrix.d / height * resource->scale_y; + state->texture_t[2] = 0.0f; + state->texture_t[3] = + matrix.ty / height * resource->scale_y + resource->offset_y; + state->texgen0_enabled = 1; + if (fill_style->type == 0x40U) { + state->wrap_mode = 1; + state->nearest = 0; + } else if (fill_style->type == 0x41U) { + state->wrap_mode = 0; + state->nearest = 0; + } else { + state->wrap_mode = fill_style->type == 0x42U; + state->nearest = iggy_force_default_smoothing == 0; + } + return 1; +} + +void iggy_setup_fill_style( + void *iggy, IggyRenderState *state, IggyFillStyle *fill_style, + float morph, const IggyRenderColorTransform *transform, + const IggyRenderBounds *new_bounds, const IggyRenderBounds *old_bounds, + const IggyFillMatrix *extra_matrix) { + telemetry_begin(); + iggy_clear_renderstate(iggy, state); + install_additive_transform(state, transform); + if (fill_style->type == 0U) { + setup_solid_fill(state, fill_style, morph); + } else if (fill_style->type == 0x10U || fill_style->type == 0x12U || + fill_style->type == 0x13U) { + setup_gradient_fill( + iggy, state, fill_style, morph, new_bounds, old_bounds); + } else if (fill_style->type >= 0x40U && fill_style->type <= 0x43U) { + if (setup_bitmap_fill( + iggy, state, fill_style, morph, new_bounds, old_bounds, + extra_matrix) == 0) { + return; + } + } + apply_final_transform(state, transform); + telemetry_end(); +} diff --git a/iggy-decompiled/src/reconstructed/swf_draw_filltype.h b/iggy-decompiled/src/reconstructed/swf_draw_filltype.h new file mode 100644 index 0000000..ec2f9e3 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/swf_draw_filltype.h @@ -0,0 +1,81 @@ +#ifndef IGGY_RECONSTRUCTED_SWF_DRAW_FILLTYPE_H +#define IGGY_RECONSTRUCTED_SWF_DRAW_FILLTYPE_H + +#include + +#include "swf_draw_renderstate.h" + +typedef struct IggyFillMatrix { + float values[6]; +} IggyFillMatrix; + +typedef union IggyFillValue { + uint8_t rgba[4]; + IggyFillMatrix matrix; +} IggyFillValue; + +typedef struct IggyFillStyle { + uint8_t type; + uint8_t reserved[sizeof(uintptr_t) - 1U]; + IggyRenderTexture cached_texture; + IggyFillValue initial; + IggyFillValue morph; + void *resource; +} IggyFillStyle; + +typedef struct IggyBitmap { + void *reserved_before_size[4]; + int16_t width; + int16_t height; + uint8_t reserved_before_scale[sizeof(uintptr_t) + 4U]; + float scale_x; + float scale_y; + float offset_x; + float offset_y; +} IggyBitmap; + +typedef struct IggyBitmapResource { + uint8_t reserved[4U * sizeof(uintptr_t) + 8U]; + IggyRenderTexture cached_texture; + float scale_x; + float scale_y; + float offset_x; + float offset_y; +} IggyBitmapResource; + +typedef struct IggyDirectTexture { + void *reserved_before_texture[3]; + IggyRenderTexture cached_texture; + void *reserved_before_size; + int32_t width; + int32_t height; +} IggyDirectTexture; + +extern int32_t iggy_force_default_smoothing; + +#ifdef __cplusplus +extern "C" { +#endif + +void IggyDebugTexgen(float scale_s, float scale_t); +IggyRenderTexture iggy_get_fill_style_texture( + void *iggy, const IggyFillStyle *fill_style); +void iggy_setup_bitmap_renderstate( + void *iggy, IggyRenderState *state, IggyBitmap *bitmap, + const IggyRenderColorTransform *transform, + int32_t clamp, int32_t nearest); +void iggy_setup_bitmap_texture_renderstate( + void *iggy, IggyRenderState *state, IggyDirectTexture *texture, + const IggyRenderColorTransform *transform, + int32_t clamp, int32_t nearest); +void iggy_setup_fill_style( + void *iggy, IggyRenderState *state, IggyFillStyle *fill_style, + float morph, const IggyRenderColorTransform *transform, + const IggyRenderBounds *new_bounds, const IggyRenderBounds *old_bounds, + const IggyFillMatrix *extra_matrix); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/iggy-decompiled/src/reconstructed/swf_draw_renderstate.h b/iggy-decompiled/src/reconstructed/swf_draw_renderstate.h index 019374a..0b7d90e 100644 --- a/iggy-decompiled/src/reconstructed/swf_draw_renderstate.h +++ b/iggy-decompiled/src/reconstructed/swf_draw_renderstate.h @@ -42,10 +42,12 @@ typedef struct IggyRenderState { } IggyRenderState; typedef struct IggyRenderColorTransform { - uint8_t reserved[2]; + uint8_t reserved; + uint8_t additive_enabled; uint8_t multiply_enabled; uint8_t padding; float multiply[4]; + int16_t additive[4]; } IggyRenderColorTransform; #ifdef __cplusplus diff --git a/tests/support/swf_draw_filltype_test_support.c b/tests/support/swf_draw_filltype_test_support.c new file mode 100644 index 0000000..c2ce547 --- /dev/null +++ b/tests/support/swf_draw_filltype_test_support.c @@ -0,0 +1,181 @@ +#include +#include +#include + +typedef struct TestRenderState { + int32_t identifier; + uint32_t flags; + uint8_t texgen0_enabled; + uint8_t texture_mode; + uint8_t wrap_mode; + uint8_t nearest; + uint8_t blend_mode; + uint8_t modes[7]; + int32_t blur_passes; + int16_t *color_transform_add; + uintptr_t textures[3]; + float *edge_matrix; + void *object_to_world; + float color[4]; + int32_t scissor[4]; + float texture_s[4]; + float texture_t[4]; + float focal_point[4]; + float trailing[22]; +} TestRenderState; + +int filltype_test_clear_calls; +int filltype_test_bitmap_calls; +int filltype_test_lock_calls; +int filltype_test_rebuild_calls; +int filltype_test_free_calls; +int filltype_test_gradient_calls; +int filltype_test_tm_begin_calls; +int filltype_test_tm_end_calls; +uintptr_t filltype_test_bitmap_result; +uintptr_t filltype_test_rebuild_result; +uintptr_t filltype_test_gradient_result; +int filltype_test_has_alpha; +int filltype_test_texture_width; +int filltype_test_texture_height; +int filltype_test_lock_results[4]; +int filltype_test_lock_result_count; +float filltype_test_bitmap_scale_x; +float filltype_test_bitmap_scale_y; +uint8_t filltype_test_bitmap_type; +float filltype_test_gradient_morph; +int filltype_test_gradient_linear_rgb; +void *filltype_test_last_bitmap; +void *filltype_test_last_gradient_fill; +void *iggy_tm_internal; + +typedef struct FilltypeTestTelemetry { + void *reserved[20]; + void (*begin)(struct FilltypeTestTelemetry *, ...); + void (*end)(struct FilltypeTestTelemetry *, ...); +} FilltypeTestTelemetry; + +static void filltype_test_tm_begin(FilltypeTestTelemetry *telemetry, ...) { + (void)telemetry; + ++filltype_test_tm_begin_calls; +} + +static void filltype_test_tm_end(FilltypeTestTelemetry *telemetry, ...) { + (void)telemetry; + ++filltype_test_tm_end_calls; +} + +static FilltypeTestTelemetry filltype_test_telemetry = { + {0}, filltype_test_tm_begin, filltype_test_tm_end}; + +void filltype_test_enable_telemetry(void) { + iggy_tm_internal = &filltype_test_telemetry; +} + +void filltype_test_reset(void) { + filltype_test_clear_calls = 0; + filltype_test_bitmap_calls = 0; + filltype_test_lock_calls = 0; + filltype_test_rebuild_calls = 0; + filltype_test_free_calls = 0; + filltype_test_gradient_calls = 0; + filltype_test_tm_begin_calls = 0; + filltype_test_tm_end_calls = 0; + filltype_test_bitmap_result = 0; + filltype_test_rebuild_result = 0; + filltype_test_gradient_result = 0; + filltype_test_has_alpha = 0; + filltype_test_texture_width = 1; + filltype_test_texture_height = 1; + memset(filltype_test_lock_results, 0, sizeof(filltype_test_lock_results)); + filltype_test_lock_result_count = 0; + filltype_test_bitmap_scale_x = 0.0f; + filltype_test_bitmap_scale_y = 0.0f; + filltype_test_bitmap_type = 0; + filltype_test_gradient_morph = 0.0f; + filltype_test_gradient_linear_rgb = 0; + filltype_test_last_bitmap = 0; + filltype_test_last_gradient_fill = 0; + iggy_tm_internal = 0; +} + +void iggy_clear_renderstate(void *iggy, TestRenderState *state) { + (void)iggy; + ++filltype_test_clear_calls; + memset(state, 0, sizeof(*state)); + state->color[0] = 1.0f; + state->color[1] = 1.0f; + state->color[2] = 1.0f; + state->color[3] = 1.0f; +} + +void iggy_setcolor_4f( + TestRenderState *state, float red, float green, float blue, float alpha) { + state->color[0] = red; + state->color[1] = green; + state->color[2] = blue; + state->color[3] = alpha; +} + +void iggy_setcolor_4ub( + TestRenderState *state, uint8_t red, uint8_t green, + uint8_t blue, uint8_t alpha) { + const float scale = 0.003921568859368563f; + state->color[0] = (float)red * scale; + state->color[1] = (float)green * scale; + state->color[2] = (float)blue * scale; + state->color[3] = (float)alpha * scale; +} + +uintptr_t iggy_get_bitmap_texture( + void *iggy, void *bitmap, int32_t type, int32_t *has_alpha, + int32_t *width, int32_t *height, float scale_x, float scale_y) { + (void)iggy; + ++filltype_test_bitmap_calls; + filltype_test_last_bitmap = bitmap; + filltype_test_bitmap_type = (uint8_t)type; + filltype_test_bitmap_scale_x = scale_x; + filltype_test_bitmap_scale_y = scale_y; + *has_alpha = filltype_test_has_alpha; + *width = filltype_test_texture_width; + *height = filltype_test_texture_height; + return filltype_test_bitmap_result; +} + +int iggy_gdraw_TryToLockTexture( + void *iggy, uintptr_t texture, uintptr_t *cache) { + (void)iggy; + (void)texture; + (void)cache; + const int index = filltype_test_lock_calls++; + return index < filltype_test_lock_result_count + ? filltype_test_lock_results[index] : 0; +} + +uintptr_t iggy_direct_texture_rebuild(void *iggy, void *direct_texture) { + (void)iggy; + ++filltype_test_rebuild_calls; + if (filltype_test_rebuild_result != 0) { + *(uintptr_t *)((uint8_t *)direct_texture + 0x18) = filltype_test_rebuild_result; + } + return filltype_test_rebuild_result; +} + +void iggy_gdraw_FreeTexture( + void *iggy, uintptr_t texture, uintptr_t *cache) { + (void)iggy; + (void)texture; + ++filltype_test_free_calls; + *cache = 0; +} + +uintptr_t iggy_make_gradient_texture( + void *iggy, uintptr_t *cache, void *fill, float morph, int linear_rgb) { + (void)iggy; + ++filltype_test_gradient_calls; + filltype_test_last_gradient_fill = fill; + filltype_test_gradient_morph = morph; + filltype_test_gradient_linear_rgb = linear_rgb; + *cache = filltype_test_gradient_result; + return filltype_test_gradient_result; +} diff --git a/tests/test_reconstructed_swf_draw_filltype.py b/tests/test_reconstructed_swf_draw_filltype.py new file mode 100644 index 0000000..b4c6ec8 --- /dev/null +++ b/tests/test_reconstructed_swf_draw_filltype.py @@ -0,0 +1,387 @@ +import ctypes +import math +import os +from pathlib import Path +import shlex +import struct +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed" +SOURCE = RECONSTRUCTED / "swf_draw_filltype.c" +SUPPORT = ROOT / "tests/support/swf_draw_filltype_test_support.c" +VARIANTS = { + name: f"IGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_{name.upper()}" + for name in ("durango", "orbis", "ps3", "vita") +} + + +class RenderState(ctypes.Structure): + _fields_ = [ + ("identifier", ctypes.c_int32), ("flags", ctypes.c_uint32), + ("texgen", ctypes.c_uint8), ("texture_mode", ctypes.c_uint8), + ("wrap", ctypes.c_uint8), ("nearest", ctypes.c_uint8), + ("blend", ctypes.c_uint8), ("modes", ctypes.c_uint8 * 7), + ("blur_passes", ctypes.c_int32), ("cxf_add", ctypes.c_void_p), + ("textures", ctypes.c_void_p * 3), ("edge_matrix", ctypes.c_void_p), + ("object_to_world", ctypes.c_void_p), ("color", ctypes.c_float * 4), + ("scissor", ctypes.c_int32 * 4), ("texture_s", ctypes.c_float * 4), + ("texture_t", ctypes.c_float * 4), ("focal", ctypes.c_float * 4), + ("trailing", ctypes.c_float * 22), + ] + + +class ColorTransform(ctypes.Structure): + _fields_ = [ + ("reserved", ctypes.c_uint8), ("add_enabled", ctypes.c_uint8), + ("multiply_enabled", ctypes.c_uint8), ("padding", ctypes.c_uint8), + ("multiply", ctypes.c_float * 4), ("add", ctypes.c_int16 * 4), + ] + + +class Bounds(ctypes.Structure): + _fields_ = [(name, ctypes.c_float) for name in ("x0", "y0", "x1", "y1")] + + +class FillStyle(ctypes.Structure): + _fields_ = [ + ("type", ctypes.c_uint8), ("padding", ctypes.c_uint8 * 7), + ("cache", ctypes.c_void_p), ("initial", ctypes.c_uint8 * 24), + ("morph", ctypes.c_uint8 * 24), ("resource", ctypes.c_void_p), + ] + + +class Bitmap(ctypes.Structure): + _fields_ = [ + ("padding0", ctypes.c_uint8 * 0x20), + ("width", ctypes.c_int16), ("height", ctypes.c_int16), + ("padding1", ctypes.c_uint8 * 12), + ("scale_x", ctypes.c_float), ("scale_y", ctypes.c_float), + ("offset_x", ctypes.c_float), ("offset_y", ctypes.c_float), + ] + + +class BitmapResource(ctypes.Structure): + _fields_ = [ + ("padding", ctypes.c_uint8 * 0x28), ("cached", ctypes.c_void_p), + ("scale_x", ctypes.c_float), ("scale_y", ctypes.c_float), + ("offset_x", ctypes.c_float), ("offset_y", ctypes.c_float), + ] + + +class DirectTexture(ctypes.Structure): + _fields_ = [ + ("padding0", ctypes.c_uint8 * 0x18), ("cached", ctypes.c_void_p), + ("padding1", ctypes.c_uint8 * 8), + ("width", ctypes.c_int32), ("height", ctypes.c_int32), + ] + + +def write_matrix(buffer, values): + ctypes.memmove(ctypes.addressof(buffer), struct.pack("=6f", *values), 24) + + +class ReconstructedSwfDrawFilltypeTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.temp = tempfile.TemporaryDirectory() + build = Path(os.environ.get("IGGY_TEST_BUILD_DIR", cls.temp.name)) + build.mkdir(parents=True, exist_ok=True) + cls.libraries = {} + for platform, define in VARIANTS.items(): + output = build / f"swf_draw_filltype_{platform}.dylib" + result = subprocess.run([ + "clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra", + "-Werror", "-DIGGY_SWF_DRAW_FILLTYPE_TEST=1", f"-D{define}=1", + "-I", str(RECONSTRUCTED), str(SOURCE), str(SUPPORT), + *shlex.split(os.environ.get("IGGY_TEST_CFLAGS", "")), "-o", str(output), + ], capture_output=True, text=True) + if result.returncode: + raise AssertionError((platform, result.stdout + result.stderr)) + library = ctypes.CDLL(str(output)) + cls.configure(library) + cls.libraries[platform] = library + + @classmethod + def tearDownClass(cls): + cls.temp.cleanup() + + @staticmethod + def configure(library): + library.IggyDebugTexgen.argtypes = [ctypes.c_float, ctypes.c_float] + library.iggy_get_fill_style_texture.argtypes = [ctypes.c_void_p, ctypes.POINTER(FillStyle)] + library.iggy_get_fill_style_texture.restype = ctypes.c_void_p + common = [ctypes.c_void_p, ctypes.POINTER(RenderState)] + library.iggy_setup_bitmap_renderstate.argtypes = common + [ + ctypes.POINTER(Bitmap), ctypes.POINTER(ColorTransform), ctypes.c_int32, ctypes.c_int32] + library.iggy_setup_bitmap_texture_renderstate.argtypes = common + [ + ctypes.POINTER(DirectTexture), ctypes.POINTER(ColorTransform), ctypes.c_int32, ctypes.c_int32] + library.iggy_setup_fill_style.argtypes = common + [ + ctypes.POINTER(FillStyle), ctypes.c_float, ctypes.POINTER(ColorTransform), + ctypes.POINTER(Bounds), ctypes.POINTER(Bounds), ctypes.POINTER(ctypes.c_float)] + + def setUp(self): + for library in self.libraries.values(): + library.filltype_test_reset() + ctypes.c_uint32.in_dll(library, "iggy_force_default_smoothing").value = 0 + + @staticmethod + def assert_floats(actual, expected): + for got, wanted in zip(actual, expected): + unittest.TestCase().assertAlmostEqual(got, wanted, places=6) + + def test_debug_texgen_target_divergence_and_retained_scales(self): + for platform, library in self.libraries.items(): + library.IggyDebugTexgen(2, 3) + if platform == "orbis": + continue + self.assertEqual(ctypes.c_int32.in_dll(library, "texgen_debug").value, 1) + self.assertEqual(ctypes.c_float.in_dll(library, "texgen_debug_s").value, 2) + self.assertEqual(ctypes.c_float.in_dll(library, "texgen_debug_t").value, 3) + library.IggyDebugTexgen(1, 1) + self.assertEqual(ctypes.c_int32.in_dll(library, "texgen_debug").value, 0) + self.assertEqual(ctypes.c_float.in_dll(library, "texgen_debug_s").value, 2) + library.IggyDebugTexgen(math.nan, 1) + self.assertEqual(ctypes.c_int32.in_dll(library, "texgen_debug").value, 1) + + def test_fill_style_texture_dispatch(self): + resource = BitmapResource(cached=0xBEEF) + for platform, library in self.libraries.items(): + fill = FillStyle(cache=0xCAFE, resource=ctypes.addressof(resource)) + for value in range(0x45): + fill.type = value + actual = library.iggy_get_fill_style_texture(None, ctypes.byref(fill)) + expected = 0xCAFE if value in (0x10, 0x12, 0x13) else ( + 0xBEEF if 0x40 <= value <= 0x43 else None) + self.assertEqual(actual, expected, (platform, value)) + fill.resource = None + fill.type = 0x40 + self.assertFalse(library.iggy_get_fill_style_texture(None, ctypes.byref(fill))) + + def test_bitmap_renderstate_success_null_and_cxf(self): + bitmap = Bitmap(width=10, height=20, scale_x=2, scale_y=4, offset_x=.25, offset_y=.5) + transform = ColorTransform(add_enabled=1, multiply_enabled=1) + transform.multiply[:] = (2, 3, 4, 5) + for platform, library in self.libraries.items(): + ctypes.c_size_t.in_dll(library, "filltype_test_bitmap_result").value = 0x1234 + ctypes.c_int.in_dll(library, "filltype_test_has_alpha").value = 1 + state = RenderState() + library.iggy_setup_bitmap_renderstate( + None, ctypes.byref(state), ctypes.byref(bitmap), ctypes.byref(transform), 0, -7) + self.assertEqual((state.textures[0], state.texture_mode, state.wrap, state.nearest, state.blend), + (0x1234, 1, 1, 1, 1), platform) + self.assertEqual(state.cxf_add, ctypes.addressof(transform) + 0x14) + self.assertEqual(tuple(state.color), (2, 3, 4, 5)) + self.assert_floats(state.texture_s, (.2, 0, 0, .25)) + self.assert_floats(state.texture_t, (0, .2, 0, .5)) + self.assertEqual(ctypes.c_float.in_dll(library, "filltype_test_bitmap_scale_x").value, .25) + library.filltype_test_reset() + state = RenderState() + library.iggy_setup_bitmap_renderstate(None, ctypes.byref(state), ctypes.byref(bitmap), None, 1, 0) + self.assertFalse(state.textures[0], platform) + self.assertEqual(tuple(state.color), (1, 1, 1, 1), platform) + + def test_direct_texture_lock_rebuild_paths(self): + for platform, library in self.libraries.items(): + direct = DirectTexture(cached=0x1111, width=8, height=4) + locks = (ctypes.c_int * 4).in_dll(library, "filltype_test_lock_results") + locks[:] = (0, 1, 0, 0) + ctypes.c_int.in_dll(library, "filltype_test_lock_result_count").value = 2 + ctypes.c_size_t.in_dll(library, "filltype_test_rebuild_result").value = 0x2222 + state = RenderState() + library.iggy_setup_bitmap_texture_renderstate( + None, ctypes.byref(state), ctypes.byref(direct), None, 1, 3) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_lock_calls").value, 2) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_rebuild_calls").value, 1) + self.assertEqual((direct.cached, state.textures[0], state.blend), (0x2222, 0x2222, 1)) + self.assert_floats(state.texture_s, (.125, 0, 0, 0)) + self.assert_floats(state.texture_t, (0, .25, 0, 0)) + + def test_direct_texture_terminal_failures_clear_the_cache(self): + for platform, library in self.libraries.items(): + direct = DirectTexture(width=8, height=4) + state = RenderState() + library.iggy_setup_bitmap_texture_renderstate( + None, ctypes.byref(state), ctypes.byref(direct), None, 0, 0) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_rebuild_calls").value, 1) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_lock_calls").value, 0) + self.assertFalse(state.textures[0], platform) + + library.filltype_test_reset() + direct.cached = 0x1111 + locks = (ctypes.c_int * 4).in_dll(library, "filltype_test_lock_results") + locks[:] = (0, 0, 0, 0) + ctypes.c_int.in_dll(library, "filltype_test_lock_result_count").value = 2 + ctypes.c_size_t.in_dll(library, "filltype_test_rebuild_result").value = 0x2222 + library.iggy_setup_bitmap_texture_renderstate( + None, ctypes.byref(state), ctypes.byref(direct), None, 0, 0) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_lock_calls").value, 2) + self.assertFalse(direct.cached, platform) + self.assertFalse(state.textures[0], platform) + + def test_solid_morph_blend_and_common_cxf(self): + for platform, library in self.libraries.items(): + fill = FillStyle(type=0) + fill.initial[:4] = (10, 20, 30, 255) + fill.morph[:4] = (110, 220, 130, 127) + transform = ColorTransform(multiply_enabled=1) + transform.multiply[:] = (-1, .5, 3, 4) + state = RenderState() + library.iggy_setup_fill_style( + None, ctypes.byref(state), ctypes.byref(fill), .5, + ctypes.byref(transform), None, None, None) + self.assertEqual(state.blend, 1, platform) + self.assertEqual(state.color[0], 0, platform) + self.assertAlmostEqual(state.color[1], 120 / 255 * .5, places=6) + self.assertAlmostEqual(state.color[2], 80 / 255 * 3, places=6) + self.assertEqual(state.color[3], 1, platform) + + def test_gradient_texgen_cache_wrap_and_bounds(self): + resource = (ctypes.c_uint8 * 8)() + resource[0], resource[1] = 1, 1 + for platform, library in self.libraries.items(): + fill = FillStyle(type=0x10, resource=ctypes.addressof(resource)) + write_matrix(fill.initial, (1, 0, 0, 1, 0, 0)) + write_matrix(fill.morph, (1, 0, 0, 1, 0, 0)) + ctypes.c_size_t.in_dll(library, "filltype_test_gradient_result").value = 0x7777 + state = RenderState() + library.iggy_setup_fill_style(None, ctypes.byref(state), ctypes.byref(fill), 0, None, None, None, None) + self.assertEqual((state.textures[0], state.wrap, state.blend, state.texgen, state.texture_mode), + (0x7777, 2, 1, 1, 1), platform) + self.assert_floats(state.texture_s, (1 / 32768, 0, 0, .5)) + self.assertEqual(tuple(state.texture_t), (0, 0, 0, 0), platform) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_gradient_linear_rgb").value, 1) + + fill.type = 0x12 + fill.cache = None + state = RenderState() + library.iggy_setup_fill_style(None, ctypes.byref(state), ctypes.byref(fill), 0, None, None, None, None) + self.assert_floats(state.texture_s, (1 / 16384, 0, 0, 0)) + self.assert_floats(state.texture_t, (0, 1 / 16384, 0, 0)) + self.assertEqual(state.texture_mode, 3) + + def test_gradient_cache_lifecycle_and_nontrivial_inverse(self): + resource = (ctypes.c_uint8 * 8)() + for platform, library in self.libraries.items(): + fill = FillStyle(type=0x12, cache=0x1111, resource=ctypes.addressof(resource)) + write_matrix(fill.initial, (2, 1, 3, 4, 5, 6)) + write_matrix(fill.morph, (2, 1, 3, 4, 5, 6)) + locks = (ctypes.c_int * 4).in_dll(library, "filltype_test_lock_results") + locks[0] = 1 + ctypes.c_int.in_dll(library, "filltype_test_lock_result_count").value = 1 + state = RenderState() + library.iggy_setup_fill_style( + None, ctypes.byref(state), ctypes.byref(fill), 0, None, None, None, None) + self.assertEqual(state.textures[0], 0x1111, platform) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_gradient_calls").value, 0) + self.assert_floats( + state.texture_s, (.8 / 16384, -.2 / 16384, 0, -2.8 / 16384)) + self.assert_floats( + state.texture_t, (-.6 / 16384, .4 / 16384, 0, .6 / 16384)) + + library.filltype_test_reset() + ctypes.c_size_t.in_dll(library, "filltype_test_gradient_result").value = 0x3333 + library.iggy_setup_fill_style( + None, ctypes.byref(state), ctypes.byref(fill), .5, None, None, None, None) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_free_calls").value, 1) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_gradient_calls").value, 1) + self.assertEqual(fill.cache, 0x3333, platform) + + def test_bounds_correction_scales_inverse_axes(self): + resource = (ctypes.c_uint8 * 8)() + old = Bounds(0, 0, 10, 10) + new = Bounds(0, 0, 20, 30) + for platform, library in self.libraries.items(): + context = (ctypes.c_uint8 * 0x2400)() + ctypes.c_int32.from_buffer(context, 0x2298).value = 1 + ctypes.c_int32.from_buffer(context, 0x229C).value = 1 + fill = FillStyle(type=0x12, resource=ctypes.addressof(resource)) + write_matrix(fill.initial, (1, 0, 0, 1, 0, 0)) + write_matrix(fill.morph, (1, 0, 0, 1, 0, 0)) + ctypes.c_size_t.in_dll(library, "filltype_test_gradient_result").value = 0x4444 + state = RenderState() + library.iggy_setup_fill_style( + context, ctypes.byref(state), ctypes.byref(fill), 0, + None, ctypes.byref(new), ctypes.byref(old), None) + self.assert_floats(state.texture_s, (2 / 16384, 0, 0, 0)) + self.assert_floats(state.texture_t, (0, 3 / 16384, 0, 0)) + + def test_bitmap_fill_scale_modes_negative_morph_and_null_early_return(self): + resource = BitmapResource(scale_x=2, scale_y=4, offset_x=.25, offset_y=.5) + for platform, library in self.libraries.items(): + fill = FillStyle(type=0x42, resource=ctypes.addressof(resource)) + write_matrix(fill.initial, (1, 0, 0, 1, 0, 0)) + write_matrix(fill.morph, (3, 0, 0, 5, 0, 0)) + ctypes.c_size_t.in_dll(library, "filltype_test_bitmap_result").value = 0x9999 + ctypes.c_int.in_dll(library, "filltype_test_texture_width").value = 10 + ctypes.c_int.in_dll(library, "filltype_test_texture_height").value = 20 + state = RenderState() + library.iggy_setup_fill_style(None, ctypes.byref(state), ctypes.byref(fill), -1, None, None, None, None) + self.assertEqual(ctypes.c_float.in_dll(library, "filltype_test_bitmap_scale_x").value, 1) + self.assertEqual(ctypes.c_float.in_dll(library, "filltype_test_bitmap_scale_y").value, 3) + self.assert_floats(state.texture_s, (.2, 0, 0, .25)) + self.assert_floats(state.texture_t, (0, .2, 0, .5)) + self.assertEqual((state.wrap, state.nearest), (1, 1), platform) + ctypes.c_uint32.in_dll(library, "iggy_force_default_smoothing").value = 1 + state = RenderState() + library.iggy_setup_fill_style(None, ctypes.byref(state), ctypes.byref(fill), 0, None, None, None, None) + self.assertEqual(state.nearest, 0, platform) + + def test_bitmap_mode_table_and_null_texture_early_return(self): + resource = BitmapResource(scale_x=1, scale_y=1) + transform = ColorTransform(multiply_enabled=1) + transform.multiply[:] = (2, 3, 4, 5) + for platform, library in self.libraries.items(): + for fill_type, expected in { + 0x40: (1, 0), 0x41: (0, 0), + 0x42: (1, 1), 0x43: (0, 1)}.items(): + library.filltype_test_reset() + ctypes.c_size_t.in_dll(library, "filltype_test_bitmap_result").value = 0x5555 + ctypes.c_int.in_dll(library, "filltype_test_texture_width").value = 1 + ctypes.c_int.in_dll(library, "filltype_test_texture_height").value = 1 + fill = FillStyle(type=fill_type, resource=ctypes.addressof(resource)) + write_matrix(fill.initial, (1, 0, 0, 1, 0, 0)) + write_matrix(fill.morph, (1, 0, 0, 1, 0, 0)) + state = RenderState() + library.iggy_setup_fill_style( + None, ctypes.byref(state), ctypes.byref(fill), 0, None, None, None, None) + self.assertEqual((state.wrap, state.nearest), expected, (platform, fill_type)) + + library.filltype_test_reset() + library.filltype_test_enable_telemetry() + fill = FillStyle(type=0x40, resource=ctypes.addressof(resource)) + write_matrix(fill.initial, (1, 0, 0, 1, 0, 0)) + write_matrix(fill.morph, (1, 0, 0, 1, 0, 0)) + state = RenderState() + library.iggy_setup_fill_style( + None, ctypes.byref(state), ctypes.byref(fill), 0, + ctypes.byref(transform), None, None, None) + self.assertEqual(tuple(state.color), (1, 1, 1, 1), platform) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_tm_begin_calls").value, 1) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_tm_end_calls").value, 0) + + def test_common_transform_nan_alpha_target_behavior(self): + transform = ColorTransform(multiply_enabled=1) + transform.multiply[:] = (math.nan, math.nan, math.nan, math.nan) + for platform, library in self.libraries.items(): + library.filltype_test_enable_telemetry() + fill = FillStyle(type=0x11) + state = RenderState() + library.iggy_setup_fill_style( + None, ctypes.byref(state), ctypes.byref(fill), 0, + ctypes.byref(transform), None, None, None) + self.assertTrue(all(math.isnan(value) for value in state.color[:3]), platform) + if platform in ("durango", "orbis"): + self.assertEqual(state.color[3], 0, platform) + elif platform == "ps3": + self.assertEqual(state.color[3], 1, platform) + else: + self.assertTrue(math.isnan(state.color[3]), platform) + self.assertEqual(ctypes.c_int.in_dll(library, "filltype_test_tm_end_calls").value, 1) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_reconstructed_swf_draw_filltype_cross.py b/tests/test_reconstructed_swf_draw_filltype_cross.py new file mode 100644 index 0000000..9c7b336 --- /dev/null +++ b/tests/test_reconstructed_swf_draw_filltype_cross.py @@ -0,0 +1,111 @@ +from pathlib import Path +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed" +SOURCE = RECONSTRUCTED / "swf_draw_filltype.c" +HEADER = RECONSTRUCTED / "swf_draw_filltype.h" +LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm") +VARIANTS = { + "durango": ("clang", "x86_64-pc-windows-msvc"), + "orbis": ("clang", "x86_64-unknown-freebsd"), + "vita": ("clang", "armv7a-unknown-linux-gnueabihf"), +} +TARGET_FLAGS = {"vita": ["-mthumb"]} +PS3_CLANG = Path("/opt/homebrew/opt/llvm/bin/clang") +if PS3_CLANG.exists(): + VARIANTS["ps3"] = (str(PS3_CLANG), "powerpc-unknown-linux-gnu") + +EXPECTED_EXPORTS = { + "IggyDebugTexgen", "iggy_force_default_smoothing", + "iggy_get_fill_style_texture", "iggy_setup_bitmap_renderstate", + "iggy_setup_bitmap_texture_renderstate", "iggy_setup_fill_style", +} +EXPECTED_IMPORTS = { + "durango": {"iggy_clear_renderstate", "iggy_direct_texture_rebuild", + "iggy_gdraw_FreeTexture", "iggy_gdraw_TryToLockTexture", + "iggy_get_bitmap_texture", "iggy_make_gradient_texture", + "iggy_setcolor_4f", "iggy_setcolor_4ub", "iggy_tm_internal"}, + "orbis": {"iggy_clear_renderstate", "iggy_direct_texture_rebuild", + "iggy_gdraw_FreeTexture", "iggy_gdraw_TryToLockTexture", + "iggy_get_bitmap_texture", "iggy_make_gradient_texture", + "iggy_setcolor_4f", "iggy_setcolor_4ub", "iggy_tm_internal"}, + "ps3": {"iggy_clear_renderstate", "iggy_direct_texture_rebuild", + "iggy_gdraw_FreeTexture", "iggy_gdraw_TryToLockTexture", + "iggy_get_bitmap_texture", "iggy_make_gradient_texture", + "iggy_setcolor_4f", "iggy_setcolor_4ub", "iggy_tm_internal"}, + "vita": {"iggy_clear_renderstate", + "iggy_direct_texture_rebuild", "iggy_gdraw_FreeTexture", + "iggy_gdraw_TryToLockTexture", "iggy_get_bitmap_texture", + "iggy_make_gradient_texture", "iggy_setcolor_4f", + "iggy_setcolor_4ub", "iggy_tm_internal"}, +} +COMPILER_IMPORTS = { + "durango": {"_fltused"}, + "orbis": set(), + "ps3": set(), + "vita": {"__stack_chk_fail", "__stack_chk_guard"}, +} + + +class ReconstructedSwfDrawFilltypeCrossTests(unittest.TestCase): + def test_target_layout_import_smoke_and_cpp_header(self): + with tempfile.TemporaryDirectory() as directory: + temporary = Path(directory) + for platform, (compiler, target) in VARIANTS.items(): + output = temporary / f"swf_draw_filltype_{platform}.o" + result = subprocess.run([ + compiler, "-target", target, "-ffreestanding", "-c", "-std=c11", + "-O2", "-Wall", "-Wextra", "-Werror", + f"-DIGGY_RECONSTRUCT_SWF_DRAW_FILLTYPE_{platform.upper()}=1", + *TARGET_FLAGS.get(platform, []), + "-I", str(RECONSTRUCTED), str(SOURCE), "-o", str(output), + ], capture_output=True, text=True) + self.assertEqual(result.returncode, 0, (platform, result.stderr)) + if not LLVM_NM.exists(): + continue + defined = subprocess.run([str(LLVM_NM), "--defined-only", str(output)], capture_output=True, text=True) + undefined = subprocess.run([str(LLVM_NM), "--undefined-only", str(output)], capture_output=True, text=True) + self.assertEqual(defined.returncode, 0, defined.stderr) + self.assertEqual(undefined.returncode, 0, undefined.stderr) + names = {line.split()[-1].removeprefix(".") for line in defined.stdout.splitlines() if line.split()} + imports = {line.split()[-1].removeprefix(".") for line in undefined.stdout.splitlines() if line.split()} + self.assertEqual({name for name in names if name.startswith("iggy_") or name == "IggyDebugTexgen"}, EXPECTED_EXPORTS) + self.assertEqual(imports - COMPILER_IMPORTS[platform], EXPECTED_IMPORTS[platform]) + + cpp = temporary / "swf_draw_filltype.cpp" + cpp.write_text( + '#include "swf_draw_filltype.h"\n' + "static_assert(sizeof(IggyFillStyle) == 72);\n" + "void use_fill(IggyFillStyle *fill) { (void)iggy_get_fill_style_texture(nullptr, fill); }\n", + encoding="utf-8", + ) + syntax = subprocess.run([ + "clang++", "-fsyntax-only", "-std=c++17", "-Wall", "-Wextra", "-Werror", + "-I", str(RECONSTRUCTED), str(cpp), + ], capture_output=True, text=True) + self.assertEqual(syntax.returncode, 0, syntax.stderr) + + def test_native_ps3_fill_parameter_register_map_when_object_is_available(self): + original = ROOT / "work/iggy-native-decomp/ps3-objects/swf_draw_filltype.obj" + if not original.exists() or not LLVM_NM.exists(): + self.skipTest("native PS3 object or LLVM tools unavailable") + objdump = LLVM_NM.with_name("llvm-objdump") + result = subprocess.run( + [str(objdump), "-d", "--no-show-raw-insn", str(original)], + capture_output=True, text=True, + ) + self.assertEqual(result.returncode, 0, result.stderr) + start = result.stdout.index("<.iggy_setup_fill_style>:") + prologue = result.stdout[start:start + 1800] + for instruction in ( + "fmr 31, 1", "mr\t20, 3", "mr\t30, 4", "mr\t25, 5", + "mr\t23, 7", "mr\t22, 8", "mr\t19, 9", "mr\t29, 10"): + self.assertIn(instruction, prologue) + + +if __name__ == "__main__": + unittest.main()