Files
o3de/Gems/GradientSignal/Code/Source/ImageAsset.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

212 lines
8.3 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <GradientSignal/ImageAsset.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Math/Vector3.h>
#include <AzCore/RTTI/ReflectContext.h>
#include <AzCore/Debug/Profiler.h>
#include <Atom/ImageProcessing/PixelFormats.h>
#include <GradientSignal/Util.h>
#include <numeric>
namespace
{
template <ImageProcessingAtom::EPixelFormat>
float RetrieveValue(const AZ::u8* mem, size_t index)
{
AZ_Assert(false, "Unimplemented!");
}
template <>
float RetrieveValue<ImageProcessingAtom::EPixelFormat::ePixelFormat_Unknown>([[maybe_unused]] const AZ::u8* mem, [[maybe_unused]] size_t index)
{
return 0.0f;
}
template <>
float RetrieveValue<ImageProcessingAtom::EPixelFormat::ePixelFormat_R8>(const AZ::u8* mem, size_t index)
{
return mem[index] / static_cast<float>(std::numeric_limits<AZ::u8>::max());
}
template <>
float RetrieveValue<ImageProcessingAtom::EPixelFormat::ePixelFormat_R16>(const AZ::u8* mem, size_t index)
{
// 16 bits per channel
auto actualMem = reinterpret_cast<const AZ::u16*>(mem);
actualMem += index;
return *actualMem / static_cast<float>(std::numeric_limits<AZ::u16>::max());
}
template <>
float RetrieveValue<ImageProcessingAtom::EPixelFormat::ePixelFormat_R32>(const AZ::u8* mem, size_t index)
{
// 32 bits per channel
auto actualMem = reinterpret_cast<const AZ::u32*>(mem);
actualMem += index;
return *actualMem / static_cast<float>(std::numeric_limits<AZ::u32>::max());
}
template <>
float RetrieveValue<ImageProcessingAtom::EPixelFormat::ePixelFormat_R32F>(const AZ::u8* mem, size_t index)
{
// 32 bits per channel
auto actualMem = reinterpret_cast<const float*>(mem);
actualMem += index;
return *actualMem;
}
float RetrieveValue(const AZ::u8* mem, size_t index, ImageProcessingAtom::EPixelFormat format)
{
using namespace ImageProcessingAtom;
switch (format)
{
case ePixelFormat_R8:
return RetrieveValue<ePixelFormat_R8>(mem, index);
case ePixelFormat_R16:
return RetrieveValue<ePixelFormat_R16>(mem, index);
case ePixelFormat_R32:
return RetrieveValue<ePixelFormat_R32>(mem, index);
case ePixelFormat_R32F:
return RetrieveValue<ePixelFormat_R32F>(mem, index);
default:
return RetrieveValue<ePixelFormat_Unknown>(mem, index);
}
}
}
namespace GradientSignal
{
void ImageAsset::Reflect(AZ::ReflectContext* context)
{
AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
if (serialize)
{
serialize->Class<ImageAsset, AZ::Data::AssetData>()
->Version(1, &ImageAsset::VersionConverter)
->Field("Width", &ImageAsset::m_imageWidth)
->Field("Height", &ImageAsset::m_imageHeight)
->Field("BytesPerPixel", &ImageAsset::m_bytesPerPixel)
->Field("Format", &ImageAsset::m_imageFormat)
->Field("Data", &ImageAsset::m_imageData)
;
AZ::EditContext* edit = serialize->GetEditContext();
if (edit)
{
edit->Class<ImageAsset>(
"Image Asset", "")
->DataElement(0, &ImageAsset::m_imageWidth, "Width", "Image width.")
->DataElement(0, &ImageAsset::m_imageHeight, "Height", "Image height.")
->DataElement(0, &ImageAsset::m_bytesPerPixel, "BytesPerPixel", "Image bytes per pixel.")
->DataElement(0, &ImageAsset::m_imageFormat, "Format", "Image format.")
->DataElement(0, &ImageAsset::m_imageData, "Data", "Image color data.")
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
->Attribute(AZ::Edit::Attributes::ContainerCanBeModified, true)
;
}
}
}
bool ImageAsset::VersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
{
if (classElement.GetVersion() < 1)
{
int formatIndex = classElement.FindElement(AZ_CRC("Format", 0xdeba72df));
if (formatIndex < 0)
{
return false;
}
AZ::SerializeContext::DataElementNode& format = classElement.GetSubElement(formatIndex);
if (format.Convert<ImageProcessingAtom::EPixelFormat>(context))
{
format.SetData<ImageProcessingAtom::EPixelFormat>(context, ImageProcessingAtom::EPixelFormat::ePixelFormat_R8);
}
int bppIndex = classElement.AddElement<AZ::u8>(context, "BytesPerPixel");
AZ::SerializeContext::DataElementNode& bpp = classElement.GetSubElement(bppIndex);
bpp.SetData<AZ::u8>(context, 1);
}
return true;
}
float GetValueFromImageAsset(const AZ::Data::Asset<ImageAsset>& imageAsset, const AZ::Vector3& uvw, float tilingX, float tilingY, float defaultValue)
{
AZ_PROFILE_FUNCTION(AZ::Debug::ProfileCategory::Entity);
if (imageAsset.IsReady())
{
const auto& image = imageAsset.Get();
AZStd::size_t imageSize = image->m_imageWidth * image->m_imageHeight *
static_cast<AZ::u32>(image->m_bytesPerPixel);
if (image->m_imageWidth > 0 &&
image->m_imageHeight > 0 &&
image->m_imageData.size() == imageSize)
{
// When "rasterizing" from uvs, a range of 0-1 has slightly different meanings depending on the sampler state.
// For repeating states (Unbounded/None, Repeat), a uv value of 1 should wrap around back to our 0th pixel.
// For clamping states (Clamp to Zero, Clamp to Edge), a uv value of 1 should point to the last pixel.
// We assume here that the code handling sampler states has handled this for us in the clamping cases
// by reducing our uv by a small delta value such that anything that wants the last pixel has a value
// just slightly less than 1.
// Keeping that in mind, we scale our uv from 0-1 to 0-image size inclusive. So a 4-pixel image will scale
// uv values of 0-1 to 0-4, not 0-3 as you might expect. This is because we want the following range mappings:
// [0 - 1/4) = pixel 0
// [1/4 - 1/2) = pixel 1
// [1/2 - 3/4) = pixel 2
// [3/4 - 1) = pixel 3
// [1 - 1 1/4) = pixel 0
// ...
// Also, based on our tiling settings, we extend the size of our image virtually by a factor of tilingX and tilingY.
// A 16x16 pixel image and tilingX = tilingY = 1 maps the uv range of 0-1 to 0-16 pixels.
// A 16x16 pixel image and tilingX = tilingY = 1.5 maps the uv range of 0-1 to 0-24 pixels.
const AZ::Vector3 tiledDimensions((image->m_imageWidth * tilingX),
(image->m_imageHeight * tilingY),
0.0f);
// Convert from uv space back to pixel space
AZ::Vector3 pixelLookup = (uvw * tiledDimensions);
// UVs outside the 0-1 range are treated as infinitely tiling, so that we behave the same as the
// other gradient generators. As mentioned above, if clamping is desired, we expect it to be applied
// outside of this function.
size_t x = static_cast<size_t>(pixelLookup.GetX()) % image->m_imageWidth;
size_t y = static_cast<size_t>(pixelLookup.GetY()) % image->m_imageHeight;
// Flip the y because images are stored in reverse of our world axes
size_t index = ((image->m_imageHeight - 1) - y) * image->m_imageWidth + x;
return RetrieveValue(image->m_imageData.data(), index, image->m_imageFormat);
}
}
return defaultValue;
}
}