| 170 | // Load texture data from binary asset content, and place it on the GPU |
| 171 | // |
| 172 | bool TextureAsset::LoadFromMemory(std::vector<uint8> const& data) |
| 173 | { |
| 174 | // check image format |
| 175 | |
| 176 | stbi_set_flip_vertically_on_load(false); |
| 177 | int32 width = 0; |
| 178 | int32 height = 0; |
| 179 | int32 channels = 0; |
| 180 | |
| 181 | static int32 const s_TargetNumChannels = 3; // for now we only support opaque textures |
| 182 | |
| 183 | // option to load 16 bit texture |
| 184 | uint8* bits = stbi_load_from_memory(data.data(), static_cast<int32>(data.size()), &width, &height, &channels, s_TargetNumChannels); |
| 185 | |
| 186 | if (bits == nullptr) |
| 187 | { |
| 188 | LOG("TextureAsset::LoadFromMemory > Failed to load texture bytes from data!", core::LogLevel::Warning); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | if ((width == 0) || (height == 0)) |
| 193 | { |
| 194 | LOG("TextureAsset::LoadFromMemory > Image is too small to display!", core::LogLevel::Warning); |
| 195 | stbi_image_free(bits); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | render::GraphicsSettings const& graphicsSettings = RenderingSystems::Instance()->GetGraphicsSettings(); |
| 200 | if (!math::nearEquals(graphicsSettings.TextureScaleFactor, 1.f)) |
| 201 | { |
| 202 | if (!m_ForceResolution) |
| 203 | { |
| 204 | // resize |
| 205 | int32 const outWidth = static_cast<int32>(static_cast<float>(width) * graphicsSettings.TextureScaleFactor); |
| 206 | int32 const outHeight = static_cast<int32>(static_cast<float>(height) * graphicsSettings.TextureScaleFactor); |
| 207 | uint8* outBits = new uint8[outWidth * outHeight * channels]; |
| 208 | |
| 209 | stbir_resize_uint8(bits, width, height, 0, outBits, outWidth, outHeight, 0, channels); |
| 210 | |
| 211 | stbi_image_free(bits); |
| 212 | bits = outBits; |
| 213 | width = outWidth; |
| 214 | height = outHeight; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // convert data type |
| 219 | // check number of channels |
| 220 | |
| 221 | //Upload to GPU |
| 222 | m_Data = new TextureData(ivec2(width, height), (m_UseSrgb ? E_ColorFormat::SRGB : E_ColorFormat::RGB), E_ColorFormat::RGB, E_DataType::UByte); |
| 223 | m_Data->Build((void*)bits); |
| 224 | m_Data->SetParameters(m_Parameters); |
| 225 | |
| 226 | m_Data->CreateHandle(); |
| 227 | |
| 228 | stbi_image_free(bits); |
| 229 | bits = nullptr; |
nothing calls this directly
no test coverage detected