Atom/mriegger/decalnormalmaps (#4302)

* Adding normal maps to decals initial commit

Signed-off-by: mrieggeramzn <mriegger@amazon.com>

* Adding checks for normal maps not present

Signed-off-by: mrieggeramzn <mriegger@amazon.com>

* Readding the proper switch statement

Signed-off-by: mrieggeramzn <mriegger@amazon.com>

* Addressing PR feedback, adding brackets

Signed-off-by: mrieggeramzn <mriegger@amazon.com>

* Adding comments

Signed-off-by: mrieggeramzn <mriegger@amazon.com>

* fixing compile issue

Signed-off-by: mrieggeramzn <mriegger@amazon.com>
This commit is contained in:
mrieggeramzn
2021-09-30 15:34:12 -07:00
committed by GitHub
parent 8a1af282c4
commit 60a0d2ba01
7 changed files with 181 additions and 83 deletions
@@ -16,6 +16,7 @@
#include <Atom/Features/MatrixUtility.azsli> #include <Atom/Features/MatrixUtility.azsli>
#include <Atom/Features/Decals/DecalTextureUtil.azsli> #include <Atom/Features/Decals/DecalTextureUtil.azsli>
#include <Atom/Features/LightCulling/LightCullingTileIterator.azsli> #include <Atom/Features/LightCulling/LightCullingTileIterator.azsli>
#include <Atom/RPI/TangentSpace.azsli>
void ApplyDecal(uint currDecalIndex, inout Surface surface); void ApplyDecal(uint currDecalIndex, inout Surface surface);
@@ -47,9 +48,10 @@ void ApplyDecal(uint currDecalIndex, inout Surface surface)
ViewSrg::Decal decal = ViewSrg::m_decals[currDecalIndex]; ViewSrg::Decal decal = ViewSrg::m_decals[currDecalIndex];
float3x3 decalRot = MatrixFromQuaternion(decal.m_quaternion); float3x3 decalRot = MatrixFromQuaternion(decal.m_quaternion);
decalRot = transpose(decalRot);
float3 localPos = surface.position - decal.m_position; float3 localPos = surface.position - decal.m_position;
localPos = mul(localPos, decalRot); localPos = mul(decalRot, localPos);
float3 decalUVW = localPos * rcp(decal.m_halfSize); float3 decalUVW = localPos * rcp(decal.m_halfSize);
if(decalUVW.x >= -1.0f && decalUVW.x <= 1.0f && if(decalUVW.x >= -1.0f && decalUVW.x <= 1.0f &&
@@ -63,30 +65,39 @@ void ApplyDecal(uint currDecalIndex, inout Surface surface)
decalUVW.y *= -1; decalUVW.y *= -1;
float3 decalUV = float3(decalUVW.xy * 0.5f + 0.5f, textureIndex); float3 decalUV = float3(decalUVW.xy * 0.5f + 0.5f, textureIndex);
float3 decalSample; float3 decalSample;
float4 baseMap = 0; float4 baseMap = 0;
float2 normalMap = 0;
switch(textureArrayIndex) switch(textureArrayIndex)
{ {
case 0: case 0:
baseMap = ViewSrg::m_decalTextureArray0.Sample(PassSrg::LinearSampler, decalUV); baseMap = ViewSrg::m_decalTextureArrayDiffuse0.Sample(PassSrg::LinearSampler, decalUV);
normalMap = ViewSrg::m_decalTextureArrayNormalMaps0.Sample(PassSrg::LinearSampler, decalUV);
break; break;
case 1: case 1:
baseMap = ViewSrg::m_decalTextureArray1.Sample(PassSrg::LinearSampler, decalUV); baseMap = ViewSrg::m_decalTextureArrayDiffuse1.Sample(PassSrg::LinearSampler, decalUV);
normalMap = ViewSrg::m_decalTextureArrayNormalMaps1.Sample(PassSrg::LinearSampler, decalUV);
break; break;
case 2: case 2:
baseMap = ViewSrg::m_decalTextureArray2.Sample(PassSrg::LinearSampler, decalUV); baseMap = ViewSrg::m_decalTextureArrayDiffuse2.Sample(PassSrg::LinearSampler, decalUV);
normalMap = ViewSrg::m_decalTextureArrayNormalMaps2.Sample(PassSrg::LinearSampler, decalUV);
break; break;
case 3: case 3:
baseMap = ViewSrg::m_decalTextureArray3.Sample(PassSrg::LinearSampler, decalUV); baseMap = ViewSrg::m_decalTextureArrayDiffuse3.Sample(PassSrg::LinearSampler, decalUV);
normalMap = ViewSrg::m_decalTextureArrayNormalMaps3.Sample(PassSrg::LinearSampler, decalUV);
break; break;
case 4: case 4:
baseMap = ViewSrg::m_decalTextureArray4.Sample(PassSrg::LinearSampler, decalUV); baseMap = ViewSrg::m_decalTextureArrayDiffuse4.Sample(PassSrg::LinearSampler, decalUV);
break; normalMap = ViewSrg::m_decalTextureArrayNormalMaps4.Sample(PassSrg::LinearSampler, decalUV);
break;
} }
float opacity = baseMap.a * decal.m_opacity * GetDecalAttenuation(surface.normal, decalRot[2], decal.m_angleAttenuation); float opacity = baseMap.a * decal.m_opacity * GetDecalAttenuation(surface.normal, decalRot[2], decal.m_angleAttenuation);
surface.albedo = lerp(surface.albedo, baseMap.rgb, opacity); surface.albedo = lerp(surface.albedo, baseMap.rgb, opacity);
float3 normalMapWS = GetWorldSpaceNormal(normalMap, decalRot[2], decalRot[0], decalRot[1], 1.0f);
surface.normal = normalize(lerp(surface.normal, normalMapWS, opacity));
} }
} }
@@ -31,12 +31,18 @@ partial ShaderResourceGroup ViewSrg
// e.g. m_decalTextureArray0 might store 24 textures @128x128, // e.g. m_decalTextureArray0 might store 24 textures @128x128,
// m_decalTextureArray1 might store 16 * 256x256 // m_decalTextureArray1 might store 16 * 256x256
// and m_decalTextureArray2 might store 4 @ 512x512 // and m_decalTextureArray2 might store 4 @ 512x512
// This must match the variable NumTextureArrays in DecalTextureArrayFeatureProcessor.h
Texture2DArray<float4> m_decalTextureArray0; Texture2DArray<float4> m_decalTextureArrayDiffuse0;
Texture2DArray<float4> m_decalTextureArray1; Texture2DArray<float4> m_decalTextureArrayDiffuse1;
Texture2DArray<float4> m_decalTextureArray2; Texture2DArray<float4> m_decalTextureArrayDiffuse2;
Texture2DArray<float4> m_decalTextureArray3; Texture2DArray<float4> m_decalTextureArrayDiffuse3;
Texture2DArray<float4> m_decalTextureArray4; Texture2DArray<float4> m_decalTextureArrayDiffuse4;
Texture2DArray<float4> m_decalTextureArrayNormalMaps0;
Texture2DArray<float4> m_decalTextureArrayNormalMaps1;
Texture2DArray<float4> m_decalTextureArrayNormalMaps2;
Texture2DArray<float4> m_decalTextureArrayNormalMaps3;
Texture2DArray<float4> m_decalTextureArrayNormalMaps4;
uint m_decalCount; uint m_decalCount;
} }
@@ -5,7 +5,6 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT * SPDX-License-Identifier: Apache-2.0 OR MIT
* *
*/ */
#include "DecalTextureArray.h" #include "DecalTextureArray.h"
#include <Atom/RPI.Public/Image/ImageSystemInterface.h> #include <Atom/RPI.Public/Image/ImageSystemInterface.h>
#include <Atom/RPI.Reflect/Image/StreamingImageAssetCreator.h> #include <Atom/RPI.Reflect/Image/StreamingImageAssetCreator.h>
@@ -24,7 +23,16 @@ namespace AZ
{ {
namespace namespace
{ {
static const char* BaseColorTextureMapName = "baseColor.textureMap"; static AZ::Name GetMapName(const DecalMapType mapType)
{
// Using local static to avoid cost of creating AZ::Name. Also so that this can be called from other static functions
static AZStd::array<AZ::Name, DecalMapType_Num> mapNames =
{
AZ::Name("baseColor.textureMap"),
AZ::Name("normal.textureMap")
};
return mapNames[mapType];
}
static AZ::Data::AssetId GetImagePoolId() static AZ::Data::AssetId GetImagePoolId()
{ {
@@ -40,6 +48,7 @@ namespace AZ
return asset; return asset;
} }
// Extract exactly which texture asset we need to load from the given material and map type (diffuse, normal, etc).
static AZ::Data::Asset<AZ::RPI::StreamingImageAsset> GetStreamingImageAsset(const AZ::RPI::MaterialAsset& materialAsset, const AZ::Name& propertyName) static AZ::Data::Asset<AZ::RPI::StreamingImageAsset> GetStreamingImageAsset(const AZ::RPI::MaterialAsset& materialAsset, const AZ::Name& propertyName)
{ {
if (!materialAsset.IsReady()) if (!materialAsset.IsReady())
@@ -78,11 +87,6 @@ namespace AZ
const AZ::RPI::MaterialAsset* materialAsset = materialAssetData.GetAs<AZ::RPI::MaterialAsset>(); const AZ::RPI::MaterialAsset* materialAsset = materialAssetData.GetAs<AZ::RPI::MaterialAsset>();
return GetStreamingImageAsset(*materialAsset, propertyName); return GetStreamingImageAsset(*materialAsset, propertyName);
} }
AZ::Data::Asset<AZ::RPI::StreamingImageAsset> GetBaseColorImageAsset(const AZ::Data::Asset<Data::AssetData> materialAssetData)
{
return GetStreamingImageAsset(materialAssetData, AZ::Name(BaseColorTextureMapName));
}
} }
int DecalTextureArray::FindMaterial(const AZ::Data::AssetId materialAssetId) const int DecalTextureArray::FindMaterial(const AZ::Data::AssetId materialAssetId) const
@@ -103,7 +107,7 @@ namespace AZ
{ {
AZ_Error("DecalTextureArray", FindMaterial(materialAssetId) == -1, "Adding material when it already exists in the array"); AZ_Error("DecalTextureArray", FindMaterial(materialAssetId) == -1, "Adding material when it already exists in the array");
// Invalidate the existing texture array, as we need to repack it taking into account the new material. // Invalidate the existing texture array, as we need to repack it taking into account the new material.
m_textureArrayPacked = nullptr; AZStd::fill(m_textureArrayPacked.begin(), m_textureArrayPacked.end(), nullptr);
MaterialData materialData; MaterialData materialData;
materialData.m_materialAssetId = materialAssetId; materialData.m_materialAssetId = materialAssetId;
@@ -122,42 +126,42 @@ namespace AZ
return m_materials[index].m_materialAssetId; return m_materials[index].m_materialAssetId;
} }
RHI::Size DecalTextureArray::GetImageDimensions() const RHI::Size DecalTextureArray::GetImageDimensions(const DecalMapType mapType) const
{ {
AZ_Assert(m_materials.size() > 0, "GetImageDimensions() cannot be called until at least one material has been added"); AZ_Assert(m_materials.size() > 0, "GetImageDimensions() cannot be called until at least one material has been added");
const int iter = m_materials.begin(); const int iter = m_materials.begin();
// All textures in a texture array must have the same size, so just pick the first // All textures in a texture array must have the same size, so just pick the first
const MaterialData& firstMaterial = m_materials[iter]; const MaterialData& firstMaterial = m_materials[iter];
const auto& baseColorAsset = GetBaseColorImageAsset(firstMaterial.m_materialAssetData); const auto& baseColorAsset = GetStreamingImageAsset(firstMaterial.m_materialAssetData, GetMapName(mapType));
return baseColorAsset->GetImageDescriptor().m_size; return baseColorAsset->GetImageDescriptor().m_size;
} }
const AZ::Data::Instance<AZ::RPI::StreamingImage>& DecalTextureArray::GetPackedTexture() const const AZ::Data::Instance<AZ::RPI::StreamingImage>& DecalTextureArray::GetPackedTexture(const DecalMapType mapType) const
{ {
return m_textureArrayPacked; return m_textureArrayPacked[mapType];
} }
bool DecalTextureArray::IsValidDecalMaterial(const AZ::RPI::MaterialAsset& materialAsset) bool DecalTextureArray::IsValidDecalMaterial(const AZ::RPI::MaterialAsset& materialAsset)
{ {
return GetStreamingImageAsset(materialAsset, AZ::Name(BaseColorTextureMapName)).IsReady(); return GetStreamingImageAsset(materialAsset, GetMapName(DecalMapType_Diffuse)).IsReady();
} }
AZ::Data::Asset<AZ::RPI::ImageMipChainAsset> DecalTextureArray::BuildPackedMipChainAsset(const size_t numTexturesToCreate) AZ::Data::Asset<AZ::RPI::ImageMipChainAsset> DecalTextureArray::BuildPackedMipChainAsset(const DecalMapType mapType, const size_t numTexturesToCreate)
{ {
RPI::ImageMipChainAssetCreator assetCreator; RPI::ImageMipChainAssetCreator assetCreator;
const uint32_t mipLevels = GetNumMipLevels(); const uint32_t mipLevels = GetNumMipLevels(mapType);
assetCreator.Begin(Data::AssetId(AZ::Uuid::CreateRandom()), static_cast<uint16_t>(mipLevels), aznumeric_cast<uint16_t>(numTexturesToCreate)); assetCreator.Begin(Data::AssetId(AZ::Uuid::CreateRandom()), aznumeric_cast<uint16_t>(mipLevels), aznumeric_cast<uint16_t>(numTexturesToCreate));
for (uint32_t mipLevel = 0; mipLevel < mipLevels; ++mipLevel) for (uint32_t mipLevel = 0; mipLevel < mipLevels; ++mipLevel)
{ {
const auto& layout = GetLayout(mipLevel); const auto& layout = GetLayout(mapType, mipLevel);
assetCreator.BeginMip(layout); assetCreator.BeginMip(layout);
for (int i = 0; i < m_materials.array_size(); ++i) for (int i = 0; i < m_materials.array_size(); ++i)
{ {
const auto rawData = GetRawImageData(i, mipLevel); const auto imageData = GetRawImageData(GetMapName(mapType), i, mipLevel);
assetCreator.AddSubImage(rawData.data(), rawData.size()); assetCreator.AddSubImage(imageData.data(), imageData.size());
} }
assetCreator.EndMip(); assetCreator.EndMip();
@@ -169,10 +173,12 @@ namespace AZ
return AZStd::move(asset); return AZStd::move(asset);
} }
RHI::ImageDescriptor DecalTextureArray::CreatePackedImageDescriptor(const uint16_t arraySize, const uint16_t mipLevels) const RHI::ImageDescriptor DecalTextureArray::CreatePackedImageDescriptor(
const DecalMapType mapType, const uint16_t arraySize, const uint16_t mipLevels) const
{ {
const RHI::Size imageDimensions = GetImageDimensions(); const RHI::Size imageDimensions = GetImageDimensions(mapType);
RHI::ImageDescriptor imageDescriptor = RHI::ImageDescriptor::Create2DArray(RHI::ImageBindFlags::ShaderRead, imageDimensions.m_width, imageDimensions.m_height, arraySize, GetFormat()); RHI::ImageDescriptor imageDescriptor = RHI::ImageDescriptor::Create2DArray(
RHI::ImageBindFlags::ShaderRead, imageDimensions.m_width, imageDimensions.m_height, arraySize, GetFormat(mapType));
imageDescriptor.m_mipLevels = mipLevels; imageDescriptor.m_mipLevels = mipLevels;
return imageDescriptor; return imageDescriptor;
} }
@@ -189,21 +195,34 @@ namespace AZ
} }
const size_t numTexturesToCreate = m_materials.array_size(); const size_t numTexturesToCreate = m_materials.array_size();
const auto mipChainAsset = BuildPackedMipChainAsset(numTexturesToCreate); for (int i = 0; i < DecalMapType_Num; ++i)
RHI::ImageViewDescriptor imageViewDescriptor; {
imageViewDescriptor.m_isArray = true; const DecalMapType mapType = aznumeric_cast<DecalMapType>(i);
if (!AreAllTextureMapsPresent(mapType))
{
AZ_Warning("DecalTextureArray", true, "Missing decal texture maps for %s. Please make sure all maps of this type are present.\n", GetMapName(mapType).GetCStr());
m_textureArrayPacked[i] = nullptr;
continue;
}
RPI::StreamingImageAssetCreator assetCreator; const auto mipChainAsset = BuildPackedMipChainAsset(mapType, numTexturesToCreate);
assetCreator.Begin(Data::AssetId(Uuid::CreateRandom())); RHI::ImageViewDescriptor imageViewDescriptor;
assetCreator.SetPoolAssetId(GetImagePoolId()); imageViewDescriptor.m_isArray = true;
assetCreator.SetFlags(RPI::StreamingImageFlags::None);
assetCreator.SetImageDescriptor(CreatePackedImageDescriptor(aznumeric_cast<uint16_t>(numTexturesToCreate), GetNumMipLevels())); RPI::StreamingImageAssetCreator assetCreator;
assetCreator.SetImageViewDescriptor(imageViewDescriptor); assetCreator.Begin(Data::AssetId(Uuid::CreateRandom()));
assetCreator.AddMipChainAsset(*mipChainAsset); assetCreator.SetPoolAssetId(GetImagePoolId());
Data::Asset<RPI::StreamingImageAsset> packedAsset; assetCreator.SetFlags(RPI::StreamingImageFlags::None);
const bool createdOk = assetCreator.End(packedAsset); assetCreator.SetImageDescriptor(
AZ_Error("TextureArrayData", createdOk, "Pack() call failed."); CreatePackedImageDescriptor(mapType, aznumeric_cast<uint16_t>(numTexturesToCreate), GetNumMipLevels(mapType)));
m_textureArrayPacked = createdOk ? RPI::StreamingImage::FindOrCreate(packedAsset) : nullptr; assetCreator.SetImageViewDescriptor(imageViewDescriptor);
assetCreator.AddMipChainAsset(*mipChainAsset);
Data::Asset<RPI::StreamingImageAsset> packedAsset;
const bool createdOk = assetCreator.End(packedAsset);
AZ_Error("TextureArrayData", createdOk, "Pack() call failed.");
m_textureArrayPacked[i] = createdOk ? RPI::StreamingImage::FindOrCreate(packedAsset) : nullptr;
}
// Free unused memory // Free unused memory
ClearAssets(); ClearAssets();
@@ -225,29 +244,30 @@ namespace AZ
} }
} }
uint16_t DecalTextureArray::GetNumMipLevels() const uint16_t DecalTextureArray::GetNumMipLevels(const DecalMapType mapType) const
{ {
AZ_Assert(m_materials.size() > 0, "GetNumMipLevels() cannot be called until at least one material has been added"); AZ_Assert(m_materials.size() > 0, "GetNumMipLevels() cannot be called until at least one material has been added");
// All decals in a texture array must have the same number of mips, so just pick the first // All decals in a texture array must have the same number of mips, so just pick the first
const int iter = m_materials.begin(); const int iter = m_materials.begin();
const MaterialData& firstMaterial = m_materials[iter]; const MaterialData& firstMaterial = m_materials[iter];
const auto& baseColorAsset = GetBaseColorImageAsset(firstMaterial.m_materialAssetData); const auto& imageAsset = GetStreamingImageAsset(firstMaterial.m_materialAssetData, GetMapName(mapType));
return baseColorAsset->GetImageDescriptor().m_mipLevels; return imageAsset->GetImageDescriptor().m_mipLevels;
} }
RHI::ImageSubresourceLayout DecalTextureArray::GetLayout(int mip) const RHI::ImageSubresourceLayout DecalTextureArray::GetLayout(const DecalMapType mapType, int mip) const
{ {
AZ_Assert(m_materials.size() > 0, "GetLayout() cannot be called unless at least one material has been added"); AZ_Assert(m_materials.size() > 0, "GetLayout() cannot be called unless at least one material has been added");
const int iter = m_materials.begin(); const int iter = m_materials.begin();
const auto& descriptor = GetBaseColorImageAsset(m_materials[iter].m_materialAssetData)->GetImageDescriptor(); const auto& descriptor =
GetStreamingImageAsset(m_materials[iter].m_materialAssetData, GetMapName(mapType))->GetImageDescriptor();
RHI::Size mipSize = descriptor.m_size; RHI::Size mipSize = descriptor.m_size;
mipSize.m_width >>= mip; mipSize.m_width >>= mip;
mipSize.m_height >>= mip; mipSize.m_height >>= mip;
return AZ::RHI::GetImageSubresourceLayout(mipSize, descriptor.m_format); return AZ::RHI::GetImageSubresourceLayout(mipSize, descriptor.m_format);
} }
AZStd::array_view<uint8_t> DecalTextureArray::GetRawImageData(int arrayLevel, const int mip) const AZStd::array_view<uint8_t> DecalTextureArray::GetRawImageData(const AZ::Name& mapName, int arrayLevel, const int mip) const
{ {
// We always want to provide valid data to the AssetCreator for each texture. // We always want to provide valid data to the AssetCreator for each texture.
// If this spot in the array is empty, just provide some random image as filler. // If this spot in the array is empty, just provide some random image as filler.
@@ -257,17 +277,20 @@ namespace AZ
{ {
arrayLevel = m_materials.begin(); arrayLevel = m_materials.begin();
} }
const auto image = GetStreamingImageAsset(m_materials[arrayLevel].m_materialAssetData, mapName);
const auto image = GetBaseColorImageAsset(m_materials[arrayLevel].m_materialAssetData); if (!image)
{
return {};
}
const auto srcData = image->GetSubImageData(mip, 0); const auto srcData = image->GetSubImageData(mip, 0);
return srcData; return srcData;
} }
AZ::RHI::Format DecalTextureArray::GetFormat() const AZ::RHI::Format DecalTextureArray::GetFormat(const DecalMapType mapType) const
{ {
AZ_Assert(m_materials.size() > 0, "GetFormat() can only be called after at least one material has been added."); AZ_Assert(m_materials.size() > 0, "GetFormat() can only be called after at least one material has been added.");
const int iter = m_materials.begin(); const int iter = m_materials.begin();
const auto& baseColorAsset = GetBaseColorImageAsset(m_materials[iter].m_materialAssetData); const auto& baseColorAsset = GetStreamingImageAsset(m_materials[iter].m_materialAssetData, GetMapName(mapType));
return baseColorAsset->GetImageDescriptor().m_format; return baseColorAsset->GetImageDescriptor().m_format;
} }
@@ -290,6 +313,25 @@ namespace AZ
return id.IsValid() && materialData.m_materialAssetData.IsReady(); return id.IsValid() && materialData.m_materialAssetData.IsReady();
} }
bool DecalTextureArray::AreAllTextureMapsPresent(const DecalMapType mapType) const
{
int iter = m_materials.begin();
while (iter != -1)
{
if (!IsTextureMapPresentInMaterial(m_materials[iter], mapType))
{
return false;
}
iter = m_materials.next(iter);
}
return true;
}
bool DecalTextureArray::IsTextureMapPresentInMaterial(const MaterialData& materialData, const DecalMapType mapType) const
{
return GetStreamingImageAsset(materialData.m_materialAssetData, GetMapName(mapType)).IsReady();
}
void DecalTextureArray::ClearAssets() void DecalTextureArray::ClearAssets()
{ {
int iter = m_materials.begin(); int iter = m_materials.begin();
@@ -330,7 +372,8 @@ namespace AZ
if (m_materials.size() == 0) if (m_materials.size() == 0)
return false; return false;
return m_textureArrayPacked == nullptr; // We pack all diffuse/normal/etc in one go, so just check to see if the diffusemaps need packing
return m_textureArrayPacked[DecalMapType_Diffuse] == nullptr;
} }
} }
@@ -28,8 +28,18 @@ namespace AZ
namespace Render namespace Render
{ {
enum DecalMapType : uint32_t
{
DecalMapType_Diffuse,
DecalMapType_Normal,
DecalMapType_Num
};
//! Helper class used by DecalTextureArrayFeatureProcessor. //! Helper class used by DecalTextureArrayFeatureProcessor.
//! Given a set of images (all with the same dimensions and format), it can pack them together into a single textureArray that can be sent to the GPU. //! Given a set of images (all with the same dimensions and format), it can pack them together into a single textureArray that can be sent to the GPU.
//! Note that once textures are packed, this class will release any material references
//! This might free memory if nothing else is holding onto them
//! The class DOES keep note of which material asset ids were added, so it can load them again if necessary if the whole thing needs to be repacked
class DecalTextureArray : public Data::AssetBus::MultiHandler class DecalTextureArray : public Data::AssetBus::MultiHandler
{ {
public: public:
@@ -40,8 +50,12 @@ namespace AZ
AZ::Data::AssetId GetMaterialAssetId(const int index) const; AZ::Data::AssetId GetMaterialAssetId(const int index) const;
// Packs all the added materials into one texture array per DecalMapType.
void Pack(); void Pack();
const Data::Instance<RPI::StreamingImage>& GetPackedTexture() const;
// Note that we pack each type into a separate texture array. This is because formats are
// often different (BC5 for normals, BC7 for diffuse, etc)
const Data::Instance<RPI::StreamingImage>& GetPackedTexture(const DecalMapType mapType) const;
static bool IsValidDecalMaterial(const RPI::MaterialAsset& materialAsset); static bool IsValidDecalMaterial(const RPI::MaterialAsset& materialAsset);
@@ -56,22 +70,25 @@ namespace AZ
void OnAssetReady(Data::Asset<Data::AssetData> asset) override; void OnAssetReady(Data::Asset<Data::AssetData> asset) override;
// Returns the index of the material in the m_materials container. -1 if not present.
int FindMaterial(const AZ::Data::AssetId materialAssetId) const; int FindMaterial(const AZ::Data::AssetId materialAssetId) const;
// packs the contents of the source images into a texture array readable by the GPU and returns it // packs the contents of the source images into a texture array readable by the GPU and returns it
AZ::Data::Asset<AZ::RPI::ImageMipChainAsset> BuildPackedMipChainAsset(const size_t numTexturesToCreate); AZ::Data::Asset<AZ::RPI::ImageMipChainAsset> BuildPackedMipChainAsset(const DecalMapType mapType, const size_t numTexturesToCreate);
RHI::ImageDescriptor CreatePackedImageDescriptor(const DecalMapType mapType, const uint16_t arraySize, const uint16_t mipLevels) const;
RHI::ImageDescriptor CreatePackedImageDescriptor(const uint16_t arraySize, const uint16_t mipLevels) const; uint16_t GetNumMipLevels(const DecalMapType mapType) const;
RHI::Size GetImageDimensions(const DecalMapType mapType) const;
uint16_t GetNumMipLevels() const; RHI::Format GetFormat(const DecalMapType mapType) const;
RHI::Size GetImageDimensions() const; RHI::ImageSubresourceLayout GetLayout(const DecalMapType mapType, int mip) const;
RHI::Format GetFormat() const; AZStd::array_view<uint8_t> GetRawImageData(const AZ::Name& mapName, int arrayLevel, int mip) const;
RHI::ImageSubresourceLayout GetLayout(int mip) const;
AZStd::array_view<uint8_t> GetRawImageData(int arrayLevel, int mip) const;
bool AreAllAssetsReady() const; bool AreAllAssetsReady() const;
bool IsAssetReady(const MaterialData& materialData) const; bool IsAssetReady(const MaterialData& materialData) const;
bool AreAllTextureMapsPresent(const DecalMapType mapType) const;
bool IsTextureMapPresentInMaterial(const MaterialData& materialData, const DecalMapType mapType) const;
void ClearAssets(); void ClearAssets();
void ClearAsset(MaterialData& materialData); void ClearAsset(MaterialData& materialData);
@@ -81,7 +98,7 @@ namespace AZ
bool NeedsPacking() const; bool NeedsPacking() const;
IndexableList<MaterialData> m_materials; IndexableList<MaterialData> m_materials;
Data::Instance<RPI::StreamingImage> m_textureArrayPacked; AZStd::array<Data::Instance<RPI::StreamingImage>, DecalMapType_Num> m_textureArrayPacked;
AZStd::unordered_set<AZ::Data::AssetId> m_assetsCurrentlyLoading; AZStd::unordered_set<AZ::Data::AssetId> m_assetsCurrentlyLoading;
}; };
@@ -322,13 +322,30 @@ namespace AZ
void DecalTextureArrayFeatureProcessor::CacheShaderIndices() void DecalTextureArrayFeatureProcessor::CacheShaderIndices()
{ {
for (int i = 0; i < NumTextureArrays; ++i) // The azsl shader should define several texture arrays such as:
{ // Texture2DArray<float4> m_decalTextureArrayDiffuse0;
const RHI::ShaderResourceGroupLayout* viewSrgLayout = RPI::RPISystemInterface::Get()->GetViewSrgLayout().get(); // Texture2DArray<float4> m_decalTextureArrayDiffuse1;
const AZStd::string baseName = "m_decalTextureArray" + AZStd::to_string(i); // Texture2DArray<float4> m_decalTextureArrayDiffuse2;
// and
// Texture2DArray<float2> m_decalTextureArrayNormalMaps0;
// Texture2DArray<float2> m_decalTextureArrayNormalMaps1;
// Texture2DArray<float2> m_decalTextureArrayNormalMaps2;
static const AZStd::array<AZStd::string, DecalMapType_Num> ShaderNames = { "m_decalTextureArrayDiffuse",
"m_decalTextureArrayNormalMaps" };
m_decalTextureArrayIndices[i] = viewSrgLayout->FindShaderInputImageIndex(Name(baseName.c_str())); for (int mapType = 0; mapType < DecalMapType_Num; ++mapType)
AZ_Warning("DecalTextureArrayFeatureProcessor", m_decalTextureArrayIndices[i].IsValid(), "Unable to find %s in decal shader.", baseName.c_str()); {
for (int texArrayIdx = 0; texArrayIdx < NumTextureArrays; ++texArrayIdx)
{
const RHI::ShaderResourceGroupLayout* viewSrgLayout = RPI::RPISystemInterface::Get()->GetViewSrgLayout().get();
const AZStd::string baseName = ShaderNames[mapType] + AZStd::to_string(texArrayIdx);
m_decalTextureArrayIndices[texArrayIdx][mapType] = viewSrgLayout->FindShaderInputImageIndex(Name(baseName.c_str()));
AZ_Warning(
"DecalTextureArrayFeatureProcessor", m_decalTextureArrayIndices[texArrayIdx][mapType].IsValid(),
"Unable to find %s in decal shader.",
baseName.c_str());
}
} }
} }
@@ -411,8 +428,11 @@ namespace AZ
int iter = m_textureArrayList.begin(); int iter = m_textureArrayList.begin();
while (iter != -1) while (iter != -1)
{ {
const auto& packedTexture = m_textureArrayList[iter].second.GetPackedTexture(); for (int mapType = 0 ; mapType < DecalMapType_Num ; ++mapType)
view->GetShaderResourceGroup()->SetImage(m_decalTextureArrayIndices[iter], packedTexture); {
const auto& packedTexture = m_textureArrayList[iter].second.GetPackedTexture(aznumeric_cast<DecalMapType>(mapType));
view->GetShaderResourceGroup()->SetImage(m_decalTextureArrayIndices[iter][mapType], packedTexture);
}
iter = m_textureArrayList.next(iter); iter = m_textureArrayList.next(iter);
} }
} }
@@ -89,6 +89,7 @@ namespace AZ
private: private:
// Number of size and format permutations // Number of size and format permutations
// This number should match the number of texture arrays in Decals/ViewSrg.azsli
static constexpr int NumTextureArrays = 5; static constexpr int NumTextureArrays = 5;
static constexpr const char* FeatureProcessorName = "DecalTextureArrayFeatureProcessor"; static constexpr const char* FeatureProcessorName = "DecalTextureArrayFeatureProcessor";
@@ -128,7 +129,7 @@ namespace AZ
// 4 textures @ 512x512 // 4 textures @ 512x512
IndexableList < AZStd::pair < AZ::RHI::Size, DecalTextureArray>> m_textureArrayList; IndexableList < AZStd::pair < AZ::RHI::Size, DecalTextureArray>> m_textureArrayList;
AZStd::array<RHI::ShaderInputImageIndex, NumTextureArrays> m_decalTextureArrayIndices; AZStd::array<AZStd::array<RHI::ShaderInputImageIndex, DecalMapType_Num>, NumTextureArrays> m_decalTextureArrayIndices;
GpuBufferHandler m_decalBufferHandler; GpuBufferHandler m_decalBufferHandler;
AsyncLoadTracker<DecalHandle> m_materialLoadTracker; AsyncLoadTracker<DecalHandle> m_materialLoadTracker;
@@ -42,7 +42,7 @@ namespace UnitTest
{ {
AZ::Render::DecalTextureArray decalTextureArray; AZ::Render::DecalTextureArray decalTextureArray;
decalTextureArray.Pack(); decalTextureArray.Pack();
auto nothing = decalTextureArray.GetPackedTexture(); auto nothing = decalTextureArray.GetPackedTexture(AZ::Render::DecalMapType_Diffuse);
EXPECT_EQ(nothing, nullptr); EXPECT_EQ(nothing, nullptr);
} }