diff --git a/Gems/Atom/Asset/ImageProcessingAtom/Assets/Config/AlbedoWithGenericAlpha.preset b/Gems/Atom/Asset/ImageProcessingAtom/Assets/Config/AlbedoWithGenericAlpha.preset index c315ede21c..3340fd39e4 100644 --- a/Gems/Atom/Asset/ImageProcessingAtom/Assets/Config/AlbedoWithGenericAlpha.preset +++ b/Gems/Atom/Asset/ImageProcessingAtom/Assets/Config/AlbedoWithGenericAlpha.preset @@ -7,7 +7,7 @@ "UUID": "{5D9ECB52-4CD9-4CB8-80E3-10CAE5EFB8A2}", "Name": "AlbedoWithGenericAlpha", "RGB_Weight": "CIEXYZ", - "PixelFormat": "ASTC_4x4", + "PixelFormat": "BC3", "MipMapSetting": { "MipGenType": "Box" } diff --git a/Gems/Atom/Asset/ImageProcessingAtom/Code/Include/Atom/ImageProcessing/PixelFormats.h b/Gems/Atom/Asset/ImageProcessingAtom/Code/Include/Atom/ImageProcessing/PixelFormats.h index 4da996dbde..1d11a05c17 100644 --- a/Gems/Atom/Asset/ImageProcessingAtom/Code/Include/Atom/ImageProcessing/PixelFormats.h +++ b/Gems/Atom/Asset/ImageProcessingAtom/Code/Include/Atom/ImageProcessing/PixelFormats.h @@ -79,6 +79,7 @@ namespace ImageProcessingAtom }; bool IsASTCFormat(EPixelFormat fmt); + bool IsHDRFormat(EPixelFormat fmt); } // namespace ImageProcessingAtom namespace AZ diff --git a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Compressors/ASTCCompressor.cpp b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Compressors/ASTCCompressor.cpp index 4ef47c043d..91829bb5be 100644 --- a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Compressors/ASTCCompressor.cpp +++ b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Compressors/ASTCCompressor.cpp @@ -78,7 +78,7 @@ namespace ImageProcessingAtom return true; } - astcenc_profile GetAstcProfile(bool isSrgb, EPixelFormat pixelFormat) + astcenc_profile GetAstcProfile(bool isSrgb, bool isHDR) { // select profile depends on LDR or HDR, SRGB or Linear // ASTCENC_PRF_LDR @@ -86,8 +86,6 @@ namespace ImageProcessingAtom // ASTCENC_PRF_HDR_RGB_LDR_A // ASTCENC_PRF_HDR - auto formatInfo = CPixelFormats::GetInstance().GetPixelFormatInfo(pixelFormat); - bool isHDR = formatInfo->eSampleType == ESampleType::eSampleType_Half || formatInfo->eSampleType == ESampleType::eSampleType_Float; astcenc_profile profile; if (isHDR) { @@ -170,7 +168,7 @@ namespace ImageProcessingAtom auto dstFormatInfo = CPixelFormats::GetInstance().GetPixelFormatInfo(fmtDst); const float quality = GetAstcCompressQuality(compressOption->compressQuality); - const astcenc_profile profile = GetAstcProfile(srcImage->HasImageFlags(EIF_SRGBRead), fmtSrc); + const astcenc_profile profile = GetAstcProfile(srcImage->HasImageFlags(EIF_SRGBRead), srcImage->HasImageFlags(EIF_HDR)); astcenc_config config; astcenc_error status; @@ -182,10 +180,12 @@ namespace ImageProcessingAtom // Create a context based on the configuration astcenc_context* context; AZ::u32 blockCount = ((srcImage->GetWidth(0)+ dstFormatInfo->blockWidth-1)/dstFormatInfo->blockWidth) * ((srcImage->GetHeight(0) + dstFormatInfo->blockHeight-1)/dstFormatInfo->blockHeight); - AZ::u32 threadCount = AZStd::min(AZStd::thread::hardware_concurrency(), blockCount); + AZ::u32 threadCount = AZStd::min(AZStd::thread::hardware_concurrency()/2, blockCount); status = astcenc_context_alloc(&config, threadCount, &context); AZ_Assert( status == ASTCENC_SUCCESS, "ERROR: Codec context alloc failed: %s\n", astcenc_get_error_string(status)); + AZ::Job* currentJob = AZ::JobContext::GetGlobalContext()->GetJobManager().GetCurrentJob(); + const astcenc_type dataType =GetAstcDataType(fmtSrc); // Compress the image for each mips @@ -209,29 +209,65 @@ namespace ImageProcessingAtom dstImage->GetImagePointer(mip, dstMem, dstPitch); AZ::u32 dataSize = dstImage->GetMipBufSize(mip); - // Create jobs for each compression thread - auto completionJob = aznew AZ::JobCompletion(); - for (AZ::u32 threadIdx = 0; threadIdx < threadCount; threadIdx++) + if (threadCount == 1) + { + astcenc_error error = astcenc_compress_image(context, &image, &swizzle, dstMem, dataSize, 0); + if (error != ASTCENC_SUCCESS) + { + status = error; + } + } + else { - const auto jobLambda = [&status, context, &image, &swizzle, dstMem, dataSize, threadIdx]() + AZ::JobCompletion* completionJob = nullptr; + if (!currentJob) { + completionJob = aznew AZ::JobCompletion(); + } + // Create jobs for each compression thread + for (AZ::u32 threadIdx = 0; threadIdx < threadCount; threadIdx++) + { + const auto jobLambda = [&status, context, &image, &swizzle, dstMem, dataSize, threadIdx]() + { + astcenc_error error = astcenc_compress_image(context, &image, &swizzle, dstMem, dataSize, threadIdx); + if (error != ASTCENC_SUCCESS) + { + status = error; + } + }; + + AZ::Job* simulationJob = AZ::CreateJobFunction(AZStd::move(jobLambda), true, nullptr); //auto-deletes + + // adds this job as child to current job if there is a current job + // otherwise adds it as a dependent for the complete job + if (currentJob) + { + currentJob->StartAsChild(simulationJob); + } + else + { + simulationJob->SetDependent(completionJob); + simulationJob->Start(); + } + astcenc_error error = astcenc_compress_image(context, &image, &swizzle, dstMem, dataSize, threadIdx); if (error != ASTCENC_SUCCESS) { status = error; } - }; - - AZ::Job* simulationJob = AZ::CreateJobFunction(AZStd::move(jobLambda), true, nullptr); //auto-deletes - simulationJob->SetDependent(completionJob); - simulationJob->Start(); - } + } + + if (currentJob) + { + currentJob->WaitForChildren(); + } - if (completionJob) - { - completionJob->StartAndWaitForCompletion(); - delete completionJob; - completionJob = nullptr; + if (completionJob) + { + completionJob->StartAndWaitForCompletion(); + delete completionJob; + completionJob = nullptr; + } } if (status != ASTCENC_SUCCESS) diff --git a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageBuilderComponent.cpp b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageBuilderComponent.cpp index 57596f4a71..c44cb39452 100644 --- a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageBuilderComponent.cpp +++ b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageBuilderComponent.cpp @@ -221,18 +221,19 @@ namespace ImageProcessingAtom m_isShuttingDown = true; } - PresetName GetImagePreset(const AZStd::string& filepath) + PresetName GetImagePreset(const AZStd::string& imageFileFullPath) { // first let preset from asset info TextureSettings textureSettings; - StringOutcome output = TextureSettings::LoadTextureSetting(filepath, textureSettings); + AZStd::string settingFilePath = imageFileFullPath + TextureSettings::ExtensionName; + TextureSettings::LoadTextureSetting(settingFilePath, textureSettings); if (!textureSettings.m_preset.IsEmpty()) { return textureSettings.m_preset; } - return BuilderSettingManager::Instance()->GetSuggestedPreset(filepath); + return BuilderSettingManager::Instance()->GetSuggestedPreset(imageFileFullPath); } void HandlePresetDependency(PresetName presetName, AZStd::vector& sourceDependencyList) @@ -305,13 +306,14 @@ namespace ImageProcessingAtom // add source dependency for .assetinfo file AssetBuilderSDK::SourceFileDependency sourceFileDependency; sourceFileDependency.m_sourceDependencyType = AssetBuilderSDK::SourceFileDependency::SourceFileDependencyType::Absolute; - sourceFileDependency.m_sourceFileDependencyPath = request.m_sourceFile; - AZ::StringFunc::Path::ReplaceExtension(sourceFileDependency.m_sourceFileDependencyPath, TextureSettings::ExtensionName); + sourceFileDependency.m_sourceFileDependencyPath = request.m_sourceFile + TextureSettings::ExtensionName; response.m_sourceFileDependencyList.push_back(sourceFileDependency); // add source dependencies for .preset files - // Get the preset for this file - auto presetName = GetImagePreset(request.m_sourceFile); + // Get the preset for this file + AZ::IO::FixedMaxPath fullPath(request.m_watchFolder); + fullPath /= request.m_sourceFile; + auto presetName = GetImagePreset(fullPath.c_str()); HandlePresetDependency(presetName, response.m_sourceFileDependencyList); response.m_result = AssetBuilderSDK::CreateJobsResultCode::Success; diff --git a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageLoader/ImageLoaders.cpp b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageLoader/ImageLoaders.cpp index e756174810..74517f5b18 100644 --- a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageLoader/ImageLoaders.cpp +++ b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageLoader/ImageLoaders.cpp @@ -8,6 +8,7 @@ #include +#include #include // warning C4251: class QT_Type needs to have dll-interface to be used by clients of class 'QT_Type' AZ_PUSH_DISABLE_WARNING(4251, "-Wunknown-warning-option") @@ -26,21 +27,31 @@ namespace ImageProcessingAtom return nullptr; } + IImageObject* loadedImage = nullptr; if (TIFFLoader::IsExtensionSupported(ext.toUtf8())) { - return TIFFLoader::LoadImageFromTIFF(filename); + loadedImage = TIFFLoader::LoadImageFromTIFF(filename); } else if (DdsLoader::IsExtensionSupported(ext.toUtf8())) { - return DdsLoader::LoadImageFromFile(filename); + loadedImage = DdsLoader::LoadImageFromFile(filename); } else if (QtImageLoader::IsExtensionSupported(ext.toUtf8())) { - return QtImageLoader::LoadImageFromFile(filename); + loadedImage = QtImageLoader::LoadImageFromFile(filename); } else if (ExrLoader::IsExtensionSupported(ext.toUtf8())) { - return ExrLoader::LoadImageFromFile(filename); + loadedImage = ExrLoader::LoadImageFromFile(filename); + } + + if (loadedImage) + { + if (IsHDRFormat(loadedImage->GetPixelFormat())) + { + loadedImage->AddImageFlags(EIF_HDR); + } + return loadedImage; } AZ_Warning("ImageProcessing", false, "No proper image loader to load file: %s", filename.c_str()); diff --git a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Processing/ImageFlags.h b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Processing/ImageFlags.h index d25533cccd..cefd315448 100644 --- a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Processing/ImageFlags.h +++ b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Processing/ImageFlags.h @@ -19,7 +19,7 @@ namespace ImageProcessingAtom const static AZ::u32 EIF_Decal = 0x4; // this is usually set through the preset const static AZ::u32 EIF_Greyscale = 0x8; // hint for the engine (e.g. greyscale light beams can be applied to shadow mask), can be for DXT1 because compression artfacts don't count as color const static AZ::u32 EIF_SupressEngineReduce = 0x10; // info for the engine: don't reduce texture resolution on this texture - const static AZ::u32 EIF_UNUSED_BIT = 0x40; // Free to use + const static AZ::u32 EIF_HDR = 0x40; // the image contains HDR data const static AZ::u32 EIF_AttachedAlpha = 0x400; // deprecated: info for the engine: it's a texture with attached alpha channel const static AZ::u32 EIF_SRGBRead = 0x800; // info for the engine: if gamma corrected rendering is on, this texture requires SRGBRead (it's not stored in linear) const static AZ::u32 EIF_DontResize = 0x8000; // info for the engine: for dds textures that shouldn't be resized diff --git a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Processing/PixelFormatInfo.cpp b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Processing/PixelFormatInfo.cpp index 8a741d6637..a413a29862 100644 --- a/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Processing/PixelFormatInfo.cpp +++ b/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Processing/PixelFormatInfo.cpp @@ -52,6 +52,24 @@ namespace ImageProcessingAtom return false; } + bool IsHDRFormat(EPixelFormat fmt) + { + switch (fmt) + { + case ePixelFormat_BC6UH: + case ePixelFormat_R9G9B9E5: + case ePixelFormat_R32G32B32A32F: + case ePixelFormat_R32G32F: + case ePixelFormat_R32F: + case ePixelFormat_R16G16B16A16F: + case ePixelFormat_R16G16F: + case ePixelFormat_R16F: + return true; + default: + return false; + } + } + PixelFormatInfo::PixelFormatInfo( uint32_t a_bitsPerPixel, uint32_t a_Channels,