| 879 | } |
| 880 | |
| 881 | Result NewDynamicTexture(HScene scene, const dmhash_t path, uint32_t width, uint32_t height, dmImage::Type type, dmImage::CompressionType compression_type, bool flip, const void* buffer, uint32_t buffer_size) |
| 882 | { |
| 883 | if (scene->m_DynamicTextures.Full()) |
| 884 | return RESULT_OUT_OF_RESOURCES; |
| 885 | |
| 886 | if (compression_type == dmImage::COMPRESSION_TYPE_NONE) |
| 887 | { |
| 888 | uint32_t expected_buffer_size = width * height * dmImage::BytesPerPixel(type); |
| 889 | if (buffer_size != expected_buffer_size) |
| 890 | { |
| 891 | dmLogError("Invalid image buffer size. Expected %d, got %d", expected_buffer_size, buffer_size); |
| 892 | return RESULT_INVAL_ERROR; |
| 893 | } |
| 894 | } |
| 895 | else if (compression_type == dmImage::COMPRESSION_TYPE_ASTC) |
| 896 | { |
| 897 | if (flip) // Cannot flip a compressed textures |
| 898 | { |
| 899 | dmLogWarning("Flipping a compressed texture is not supported! '%s'", dmHashReverseSafe64(path)); |
| 900 | } |
| 901 | flip = false; |
| 902 | |
| 903 | uint32_t depth; |
| 904 | if (!dmImage::GetAstcDimensions(buffer, buffer_size, &width, &height, &depth)) |
| 905 | { |
| 906 | dmLogError("Invalid image data. Expected astc format"); |
| 907 | return RESULT_INVAL_ERROR; |
| 908 | } |
| 909 | } |
| 910 | else |
| 911 | { |
| 912 | dmLogError("Invalid image compression type. %d", compression_type); |
| 913 | return RESULT_INVAL_ERROR; |
| 914 | } |
| 915 | |
| 916 | if (scene->m_DynamicTextures.Get(path) != 0x0) |
| 917 | { |
| 918 | return RESULT_TEXTURE_ALREADY_EXISTS; |
| 919 | } |
| 920 | |
| 921 | // Only make a copy if we need to flip the image |
| 922 | void* flipped_data = flip ? MakeDynamicTextureData(width, height, type, flip, buffer, buffer_size) : 0; |
| 923 | const void* data = flip ? flipped_data : buffer; |
| 924 | if (!data) |
| 925 | { |
| 926 | return RESULT_DATA_ERROR; |
| 927 | } |
| 928 | |
| 929 | HTextureSource res = scene->m_NewTextureResourceCallback(scene, path, width, height, type, compression_type, data, buffer_size); |
| 930 | free(flipped_data); |
| 931 | |
| 932 | if (!res) |
| 933 | { |
| 934 | return RESULT_OUT_OF_RESOURCES; |
| 935 | } |
| 936 | |
| 937 | float buffer_size_mb = buffer_size / float(1024 * 1024); |
| 938 | DM_PROPERTY_ADD_F32(rmtp_GuiDynamicTexturesSizeMb, buffer_size_mb); |