| 396 | } |
| 397 | |
| 398 | bool TextureTool::UpdateTexture(GPUContext* context, GPUTexture* texture, int32 arrayIndex, int32 mipIndex, Span<byte> data, uint32 rowPitch, uint32 slicePitch, PixelFormat dataFormat) |
| 399 | { |
| 400 | PROFILE_MEM(GraphicsTextures); |
| 401 | PixelFormat textureFormat = texture->Format(); |
| 402 | |
| 403 | // Basis Universal data transcoded into the runtime GPU format (supercompressed texture) |
| 404 | if (dataFormat == PixelFormat::Basis) |
| 405 | { |
| 406 | #if COMPILE_WITH_BASISU |
| 407 | return UpdateTextureBasisUniversal(context, texture, arrayIndex, mipIndex, data, rowPitch, slicePitch, dataFormat); |
| 408 | #else |
| 409 | LOG(Error, "Loading Basis Universal textures is not supported on this platform."); |
| 410 | #endif |
| 411 | } |
| 412 | |
| 413 | // Try converting texture on the fly (slow) |
| 414 | Array<byte> tempData; |
| 415 | if (textureFormat != dataFormat) |
| 416 | { |
| 417 | PROFILE_CPU_NAMED("ConvertTexture"); |
| 418 | |
| 419 | int32 mipWidth, mipHeight, mipDepth; |
| 420 | texture->GetMipSize(mipIndex, mipWidth, mipHeight, mipDepth); |
| 421 | |
| 422 | auto dataSampler = PixelFormatSampler::Get(dataFormat); |
| 423 | auto textureSampler = PixelFormatSampler::Get(textureFormat); |
| 424 | if (dataSampler && textureSampler) |
| 425 | { |
| 426 | // Conversion with an in-built samplers |
| 427 | auto tempRowPitch = mipWidth * textureSampler->PixelSize; |
| 428 | auto tempSlicePitch = tempRowPitch * mipHeight; |
| 429 | tempData.Resize(tempSlicePitch * mipDepth); |
| 430 | ASSERT(data.Length() / rowPitch >= (uint32)mipHeight); |
| 431 | for (int32 y = 0; y < mipHeight; y++) |
| 432 | { |
| 433 | for (int32 x = 0; x < mipWidth; x++) |
| 434 | { |
| 435 | Color color = dataSampler->SamplePoint(data.Get(), x, y, rowPitch); |
| 436 | textureSampler->Store(tempData.Get(), x, y, tempRowPitch, color); |
| 437 | } |
| 438 | } |
| 439 | data = ToSpan(tempData); |
| 440 | rowPitch = tempRowPitch; |
| 441 | slicePitch = tempSlicePitch; |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | // Conversion with external library |
| 446 | TextureData src, dst; |
| 447 | src.Width = mipWidth; |
| 448 | src.Height = mipHeight; |
| 449 | src.Depth = mipDepth; |
| 450 | src.Format = dataFormat; |
| 451 | auto& srcItem = src.Items.AddOne(); |
| 452 | auto& srcMip = srcItem.Mips.AddOne(); |
| 453 | srcMip.RowPitch = rowPitch; |
| 454 | srcMip.DepthPitch = slicePitch; |
| 455 | srcMip.Lines = slicePitch / rowPitch; |
no test coverage detected