| 65 | } |
| 66 | |
| 67 | bool STextureCooker::Cook(SCookContext *ctx) |
| 68 | { |
| 69 | const auto outputPath = ctx->GetOutputPath(); |
| 70 | auto uncompressed = ctx->Import<skr_uncompressed_render_texture_t>(); |
| 71 | SKR_DEFER({ ctx->Destroy(uncompressed); }); |
| 72 | |
| 73 | // try decode texture & calculate compressed format |
| 74 | const auto decoder = uncompressed->decoder; |
| 75 | const auto format = decoder->get_color_format(); |
| 76 | ECGPUFormat compressed_format = CGPU_FORMAT_UNDEFINED; |
| 77 | switch (format) // TODO: format shuffle |
| 78 | { |
| 79 | case IMAGE_CODER_COLOR_FORMAT_Gray: |
| 80 | case IMAGE_CODER_COLOR_FORMAT_GrayF: |
| 81 | compressed_format = CGPU_FORMAT_DXBC4_UNORM; |
| 82 | break; |
| 83 | case IMAGE_CODER_COLOR_FORMAT_RGBA: |
| 84 | default: |
| 85 | compressed_format = CGPU_FORMAT_DXBC3_UNORM; |
| 86 | break; |
| 87 | } |
| 88 | // DXT |
| 89 | skr::Vector<uint8_t> compressed_data; |
| 90 | { |
| 91 | SkrZoneScopedN("DXTCompress"); |
| 92 | compressed_data = Util_DXTCompressWithImageCoder(decoder, compressed_format); |
| 93 | } |
| 94 | // TODO: ASTC |
| 95 | // write texture resource |
| 96 | skr_texture_resource_t resource; |
| 97 | resource.format = compressed_format; |
| 98 | resource.mips_count = 1; |
| 99 | resource.data_size = compressed_data.size(); |
| 100 | resource.height = decoder->get_height(); |
| 101 | resource.width = decoder->get_width(); |
| 102 | resource.depth = 1; |
| 103 | { |
| 104 | SkrZoneScopedN("SaveToCtx"); |
| 105 | if(!ctx->Save(resource)) |
| 106 | return false; |
| 107 | } |
| 108 | // write compressed files |
| 109 | { |
| 110 | SkrZoneScopedN("SaveToDisk"); |
| 111 | |
| 112 | auto extension = Util_CompressedTypeString(compressed_format); |
| 113 | auto compressed_path = outputPath; |
| 114 | compressed_path.replace_extension(extension.c_str()); |
| 115 | auto compressed_pathstr = compressed_path.string(); |
| 116 | auto compressed_file = fopen(compressed_pathstr.c_str(), "wb"); |
| 117 | SKR_DEFER({ fclose(compressed_file); }); |
| 118 | fwrite(compressed_data.data(), compressed_data.size(), 1, compressed_file); |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | uint32_t STextureCooker::Version() |
| 124 | { |
nothing calls this directly
no test coverage detected