| 112 | } |
| 113 | |
| 114 | static Texture2D* createTexture(bx::AllocatorI* allocator, bimg::ImageContainer* image, String filename) { |
| 115 | if (!image) { |
| 116 | return nullptr; |
| 117 | } |
| 118 | bimg::ImageContainer* dstImage = image; |
| 119 | bool isPNG = Path::getExt(filename) == "png"_slice; |
| 120 | if (isPNG) { |
| 121 | if (image->m_format == bimg::TextureFormat::RG8) { |
| 122 | dstImage = bimg::imageAlloc(allocator, bimg::TextureFormat::RGBA8, uint16_t(image->m_width), uint16_t(image->m_height), uint16_t(image->m_depth), image->m_numLayers, image->m_cubeMap, false); |
| 123 | auto unpack = [](float* dst, const void* src) { |
| 124 | const uint8_t* _src = (const uint8_t*)src; |
| 125 | dst[0] = dst[1] = dst[2] = bx::fromUnorm(_src[0], 255.0f); |
| 126 | dst[3] = bx::fromUnorm(_src[1], 255.0f); |
| 127 | }; |
| 128 | const auto srcbpp = 16; |
| 129 | const auto dstbpp = 32; |
| 130 | bimg::imageConvert(dstImage->m_data, dstbpp, bx::packRgba8, |
| 131 | image->m_data, srcbpp, unpack, |
| 132 | image->m_width, image->m_height, image->m_depth, |
| 133 | image->m_width * (srcbpp / 8), image->m_width * (dstbpp / 8)); |
| 134 | } else if (image->m_format != bimg::TextureFormat::RGBA8) { |
| 135 | dstImage = bimg::imageConvert(allocator, bimg::TextureFormat::RGBA8, *image, false); |
| 136 | bimg::imageFree(image); |
| 137 | } |
| 138 | } |
| 139 | uint64_t flags = BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP; |
| 140 | const bgfx::Memory* mem = bgfx::makeRef( |
| 141 | dstImage->m_data, dstImage->m_size, |
| 142 | releaseImage, dstImage); |
| 143 | bgfx::TextureHandle handle = bgfx::createTexture2D( |
| 144 | s_cast<uint16_t>(dstImage->m_width), |
| 145 | s_cast<uint16_t>(dstImage->m_height), |
| 146 | dstImage->m_numMips > 1, |
| 147 | dstImage->m_numLayers, |
| 148 | s_cast<bgfx::TextureFormat::Enum>(dstImage->m_format), |
| 149 | flags, |
| 150 | mem); |
| 151 | bgfx::TextureInfo info; |
| 152 | bgfx::calcTextureSize(info, |
| 153 | s_cast<uint16_t>(dstImage->m_width), |
| 154 | s_cast<uint16_t>(dstImage->m_height), |
| 155 | s_cast<uint16_t>(dstImage->m_depth), |
| 156 | dstImage->m_cubeMap, |
| 157 | dstImage->m_numMips > 1, |
| 158 | dstImage->m_numLayers, |
| 159 | s_cast<bgfx::TextureFormat::Enum>(dstImage->m_format)); |
| 160 | Texture2D* texture = Texture2D::create(handle, info, flags); |
| 161 | return texture; |
| 162 | } |
| 163 | |
| 164 | Texture2D* TextureCache::update(String filename, const uint8_t* data, int64_t size) { |
| 165 | AssertUnless(data && size > 0, "add invalid data to texture cache."); |
no test coverage detected