(Draft) WIP adding pixel retrieval API to StreamingImageAsset

Signed-off-by: Chris Galvan <chgalvan@amazon.com>
This commit is contained in:
Chris Galvan
2022-01-07 14:27:43 -06:00
parent 67a89c6cd0
commit 8510bdbfa7
7 changed files with 157 additions and 16 deletions
@@ -242,6 +242,10 @@ namespace ImageProcessingAtom
}
}
// Should we actually put the GetSubImagePixelValue API in this Utils file instead, since it already has
// some conversion logic, and has the helper method for retrieving an entire image (LoadImageFromImageAsset)
// whereas this is a helper for retrieving a specific pixel from the image?
IImageObjectPtr LoadImageFromImageAsset(const AZ::Data::Asset<AZ::RPI::StreamingImageAsset>& imageAsset)
{
if (!imageAsset.IsReady())
@@ -85,6 +85,10 @@ namespace AZ
//! Get image data for specified mip and slice. It may return empty array if its mipchain assets are not loaded
AZStd::array_view<uint8_t> GetSubImageData(uint32_t mip, uint32_t slice);
//! Get image pixel value for specified mip and slice
template<typename T>
T GetSubImagePixelValue(uint32_t mip, uint32_t slice, uint32_t x, uint32_t y, uint32_t componentIndex = 0);
//! Returns streaming image pool asset id of the pool that will be used to create the streaming image.
const Data::AssetId& GetPoolAssetId() const;
@@ -13,6 +13,95 @@
namespace AZ
{
namespace Internal
{
template <AZ::RHI::Format>
float RetrieveFloatValue(const AZ::u8* mem, size_t index)
{
AZ_Assert(false, "Unsupported pixel format");
}
template <AZ::RHI::Format>
AZ::u32 RetrieveUintValue(const AZ::u8* mem, size_t index)
{
AZ_Assert(false, "Unsupported pixel format");
}
template <>
float RetrieveFloatValue<AZ::RHI::Format::Unknown>([[maybe_unused]] const AZ::u8* mem, [[maybe_unused]] size_t index)
{
return 0.0f;
}
template <>
AZ::u32 RetrieveUintValue<AZ::RHI::Format::Unknown>([[maybe_unused]] const AZ::u8* mem, [[maybe_unused]] size_t index)
{
return 0;
}
template <>
AZ::u32 RetrieveUintValue<AZ::RHI::Format::R8_UNORM>(const AZ::u8* mem, size_t index)
{
return mem[index] / static_cast<AZ::u32>(std::numeric_limits<AZ::u8>::max());
}
template <>
AZ::u32 RetrieveUintValue<AZ::RHI::Format::R16_UNORM>(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<AZ::u32>(std::numeric_limits<AZ::u16>::max());
}
template <>
AZ::u32 RetrieveUintValue<AZ::RHI::Format::R32_UINT>(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<AZ::u32>(std::numeric_limits<AZ::u32>::max());
}
template <>
float RetrieveFloatValue<AZ::RHI::Format::R32_FLOAT>(const AZ::u8* mem, size_t index)
{
// 32 bits per channel
auto actualMem = reinterpret_cast<const float*>(mem);
actualMem += index;
return *actualMem;
}
float RetrieveFloatValue(const AZ::u8* mem, size_t index, AZ::RHI::Format format)
{
switch (format)
{
case AZ::RHI::Format::R32_FLOAT:
return RetrieveFloatValue<AZ::RHI::Format::R32_FLOAT>(mem, index);
default:
return RetrieveFloatValue<AZ::RHI::Format::Unknown>(mem, index);
}
}
AZ::u32 RetrieveUintValue(const AZ::u8* mem, size_t index, AZ::RHI::Format format)
{
switch (format)
{
case AZ::RHI::Format::R8_UNORM:
return RetrieveUintValue<AZ::RHI::Format::R8_UNORM>(mem, index);
case AZ::RHI::Format::R16_UNORM:
return RetrieveUintValue<AZ::RHI::Format::R16_UNORM>(mem, index);
case AZ::RHI::Format::R32_UINT:
return RetrieveUintValue<AZ::RHI::Format::R32_UINT>(mem, index);
default:
return RetrieveUintValue<AZ::RHI::Format::Unknown>(mem, index);
}
}
}
namespace RPI
{
const char* StreamingImageAsset::DisplayName = "StreamingImage";
@@ -124,5 +213,45 @@ namespace AZ
return mipChainAsset->GetSubImageData(mip - mipChain.m_mipOffset, slice);
}
template<>
float StreamingImageAsset::GetSubImagePixelValue<float>(uint32_t mip, uint32_t slice, uint32_t x, uint32_t y, uint32_t componentIndex)
{
// TODO: Use the component index
(void)componentIndex;
auto imageData = GetSubImageData(mip, slice);
if (!imageData.empty())
{
const AZ::RHI::ImageDescriptor imageDescriptor = GetImageDescriptor();
auto width = imageDescriptor.m_size.m_width;
size_t index = (y * width) + x;
return Internal::RetrieveFloatValue(imageData.data(), index, imageDescriptor.m_format);
}
return 0.0f;
}
template<>
AZ::u32 StreamingImageAsset::GetSubImagePixelValue<AZ::u32>(uint32_t mip, uint32_t slice, uint32_t x, uint32_t y, uint32_t componentIndex)
{
// TODO: Use the component index
(void)componentIndex;
auto imageData = GetSubImageData(mip, slice);
if (!imageData.empty())
{
const AZ::RHI::ImageDescriptor imageDescriptor = GetImageDescriptor();
auto width = imageDescriptor.m_size.m_width;
size_t index = (y * width) + x;
return Internal::RetrieveUintValue(imageData.data(), index, imageDescriptor.m_format);
}
return 0;
}
}
}
+1
View File
@@ -21,6 +21,7 @@ ly_add_target(
AZ::AzCore
AZ::AtomCore
AZ::AzFramework
Gem::Atom_RPI.Public
Gem::SurfaceData
Gem::ImageProcessingAtom.Headers
Gem::LmbrCentral
@@ -10,6 +10,9 @@
#include <AzCore/Asset/AssetCommon.h>
#include <AzCore/Component/Component.h>
#include <Atom/RPI.Reflect/Image/StreamingImageAsset.h>
#include <GradientSignal/Ebuses/GradientRequestBus.h>
#include <GradientSignal/Ebuses/GradientTransformRequestBus.h>
#include <GradientSignal/Ebuses/ImageGradientRequestBus.h>
@@ -33,6 +36,7 @@ namespace GradientSignal
AZ_RTTI(ImageGradientConfig, "{1BDB5DA4-A4A8-452B-BE6D-6BD451D4E7CD}", AZ::ComponentConfig);
static void Reflect(AZ::ReflectContext* context);
AZ::Data::Asset<ImageAsset> m_imageAsset = { AZ::Data::AssetLoadBehavior::QueueLoad };
AZ::Data::Asset<AZ::RPI::StreamingImageAsset> m_streamingImageAsset = { AZ::Data::AssetLoadBehavior::QueueLoad };
float m_tilingX = 1.0f;
float m_tilingY = 1.0f;
};
@@ -12,6 +12,7 @@
#include <AzCore/RTTI/ReflectContext.h>
#include <AzFramework/Asset/GenericAssetHandler.h>
#include <Atom/ImageProcessing/PixelFormats.h>
#include <Atom/RPI.Reflect/Image/StreamingImageAsset.h>
namespace AZ
{
@@ -61,6 +62,6 @@ namespace GradientSignal
}
};
float GetValueFromImageAsset(const AZ::Data::Asset<ImageAsset>& imageAsset, const AZ::Vector3& uvw, float tilingX, float tilingY, float defaultValue);
float GetValueFromImageAsset(const AZ::Data::Asset<AZ::RPI::StreamingImageAsset>& imageAsset, const AZ::Vector3& uvw, float tilingX, float tilingY, float defaultValue);
} // namespace GradientSignal
+13 -15
View File
@@ -20,6 +20,7 @@
namespace
{
// Could (should) move these RetrieveValue helper methods over to where our new API lives
template <ImageProcessingAtom::EPixelFormat>
float RetrieveValue(const AZ::u8* mem, size_t index)
{
@@ -151,17 +152,17 @@ namespace GradientSignal
return true;
}
float GetValueFromImageAsset(const AZ::Data::Asset<ImageAsset>& imageAsset, const AZ::Vector3& uvw, float tilingX, float tilingY, float defaultValue)
float GetValueFromImageAsset(const AZ::Data::Asset<AZ::RPI::StreamingImageAsset>& imageAsset, const AZ::Vector3& uvw, float tilingX, float tilingY, float defaultValue)
{
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);
const AZ::RHI::ImageDescriptor imageDescriptor = imageAsset->GetImageDescriptor();
auto width = imageDescriptor.m_size.m_width;
auto height = imageDescriptor.m_size.m_height;
if (image->m_imageWidth > 0 &&
image->m_imageHeight > 0 &&
image->m_imageData.size() == imageSize)
if (width > 0
&& height > 0
)
{
// 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.
@@ -184,8 +185,8 @@ namespace GradientSignal
// 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),
const AZ::Vector3 tiledDimensions((width * tilingX),
(height * tilingY),
0.0f);
// Convert from uv space back to pixel space
@@ -194,13 +195,10 @@ namespace GradientSignal
// 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;
uint32_t x = static_cast<uint32_t>(pixelLookup.GetX()) % width;
uint32_t y = static_cast<uint32_t>(pixelLookup.GetY()) % height;
// 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 imageAsset->GetSubImagePixelValue<float>(0, 0, x, y, 0);
}
}