| 34 | } |
| 35 | |
| 36 | bool load_texture_image(const resource_desc &desc, subresource_data &data, std::vector<std::vector<uint8_t>> &data_to_delete) |
| 37 | { |
| 38 | #if RESHADE_ADDON_TEXTURE_LOAD_HASH_TEXMOD |
| 39 | // Behavior of the original TexMod (see https://github.com/codemasher/texmod/blob/master/uMod_DX9/uMod_TextureFunction.cpp#L41) |
| 40 | const uint32_t hash = ~compute_crc32( |
| 41 | static_cast<const uint8_t *>(data.data), |
| 42 | desc.texture.height * static_cast<size_t>( |
| 43 | (desc.texture.format >= format::bc1_typeless && desc.texture.format <= format::bc1_unorm_srgb) || (desc.texture.format >= format::bc4_typeless && desc.texture.format <= format::bc4_snorm) ? (desc.texture.width * 4) / 8 : |
| 44 | (desc.texture.format >= format::bc2_typeless && desc.texture.format <= format::bc2_unorm_srgb) || (desc.texture.format >= format::bc3_typeless && desc.texture.format <= format::bc3_unorm_srgb) || (desc.texture.format >= format::bc5_typeless && desc.texture.format <= format::bc7_unorm_srgb) ? desc.texture.width : |
| 45 | format_row_pitch(desc.texture.format, desc.texture.width))); |
| 46 | #else |
| 47 | // Correct hash calculation using entire resource data |
| 48 | const uint32_t hash = compute_crc32( |
| 49 | static_cast<const uint8_t *>(data.data), |
| 50 | format_slice_pitch(desc.texture.format, data.row_pitch, desc.texture.height)); |
| 51 | #endif |
| 52 | |
| 53 | const std::filesystem::path file_path = make_texture_file_path(hash); |
| 54 | |
| 55 | // Check if a replacement file for this texture hash exists and if so, overwrite the texture data with its contents |
| 56 | if (!std::filesystem::exists(file_path)) |
| 57 | return false; |
| 58 | |
| 59 | int width = 0, height = 0, channels = 0; |
| 60 | stbi_uc *const rgba_pixel_data_p = stbi_load(file_path.u8string().c_str(), &width, &height, &channels, STBI_rgb_alpha); |
| 61 | if (rgba_pixel_data_p == nullptr) |
| 62 | return false; |
| 63 | |
| 64 | std::vector<uint8_t> pixel_data(rgba_pixel_data_p, rgba_pixel_data_p + static_cast<size_t>(width) * static_cast<size_t>(height) * 4); |
| 65 | |
| 66 | stbi_image_free(rgba_pixel_data_p); |
| 67 | |
| 68 | // Only support changing pixel data, but not texture dimensions |
| 69 | if (desc.texture.width != static_cast<uint32_t>(width) || |
| 70 | desc.texture.height != static_cast<uint32_t>(height)) |
| 71 | { |
| 72 | reshade::log::message(reshade::log::level::error, "Failed to replace texture data because dimensions do not match!"); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | switch (desc.texture.format) |
| 77 | { |
| 78 | case format::l8_unorm: |
| 79 | case format::a8_unorm: |
| 80 | case format::r8_typeless: |
| 81 | case format::r8_unorm: |
| 82 | case format::r8_snorm: |
| 83 | for (size_t y = 0; y < static_cast<size_t>(height); ++y) |
| 84 | { |
| 85 | for (size_t x = 0; x < static_cast<size_t>(width); ++x) |
| 86 | { |
| 87 | const uint8_t *const src = pixel_data.data() + (y * width + x) * 4; |
| 88 | uint8_t *const dst = pixel_data.data() + (y * width + x); |
| 89 | |
| 90 | dst[0] = src[0]; |
| 91 | } |
| 92 | } |
| 93 | pixel_data.resize(static_cast<size_t>(width) * static_cast<size_t>(height)); |
no test coverage detected