Skip to content

Commit

Permalink
refactor TextureAspect (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
skallweitNV authored Sep 30, 2024
1 parent 510f19f commit d0795a9
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 147 deletions.
21 changes: 6 additions & 15 deletions include/slang-rhi.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,35 +570,26 @@ enum class TextureType

enum class TextureAspect : uint32_t
{
Default = 0,
Color = 0x00000001,
Depth = 0x00000002,
Stencil = 0x00000004,
MetaData = 0x00000008,
Plane0 = 0x00000010,
Plane1 = 0x00000020,
Plane2 = 0x00000040,

DepthStencil = Depth | Stencil,
All = 0,
DepthOnly = 1,
StencilOnly = 2,
};

struct SubresourceRange
{
TextureAspect aspectMask;
GfxIndex mipLevel;
GfxCount mipLevelCount;
GfxIndex baseArrayLayer; // For Texture3D, this is WSlice.
GfxCount layerCount; // For cube maps, this is a multiple of 6.
bool operator==(const SubresourceRange& other) const
{
return aspectMask == other.aspectMask && mipLevel == other.mipLevel && mipLevelCount == other.mipLevelCount &&
return mipLevel == other.mipLevel && mipLevelCount == other.mipLevelCount &&
baseArrayLayer == other.baseArrayLayer && layerCount == other.layerCount;
}
bool operator!=(const SubresourceRange& other) const { return !(*this == other); }
};

static const SubresourceRange kEntireTexture =
SubresourceRange{TextureAspect::Default, 0l, 0x7fffffffl, 0l, 0x7fffffffl};
static const SubresourceRange kEntireTexture = SubresourceRange{0l, 0x7fffffffl, 0l, 0x7fffffffl};

/// Data for a single subresource of a texture.
///
Expand Down Expand Up @@ -709,7 +700,7 @@ class ITexture : public IResource
struct TextureViewDesc
{
Format format = Format::Unknown;
TextureAspect aspect = TextureAspect::Default;
TextureAspect aspect = TextureAspect::All;
SubresourceRange subresourceRange = kEntireTexture;
const char* label = nullptr;
};
Expand Down
14 changes: 3 additions & 11 deletions src/d3d/d3d-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,10 @@ uint32_t D3DUtil::getPlaneSlice(DXGI_FORMAT format, TextureAspect aspect)
{
switch (aspect)
{
case TextureAspect::Default:
case TextureAspect::Color:
case TextureAspect::All:
case TextureAspect::DepthOnly:
return 0;
case TextureAspect::Depth:
return 0;
case TextureAspect::Stencil:
case TextureAspect::StencilOnly:
switch (format)
{
case DXGI_FORMAT_D24_UNORM_S8_UINT:
Expand All @@ -778,12 +776,6 @@ uint32_t D3DUtil::getPlaneSlice(DXGI_FORMAT format, TextureAspect aspect)
default:
return 0;
}
case TextureAspect::Plane0:
return 0;
case TextureAspect::Plane1:
return 1;
case TextureAspect::Plane2:
return 2;
default:
SLANG_RHI_ASSERT_FAILURE("Unknown texture aspect.");
return 0;
Expand Down
1 change: 0 additions & 1 deletion src/d3d11/d3d11-texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class TextureImpl : public Texture
{
size_t hash = 0;
hash_combine(hash, key.format);
hash_combine(hash, key.range.aspectMask);
hash_combine(hash, key.range.baseArrayLayer);
hash_combine(hash, key.range.layerCount);
hash_combine(hash, key.range.mipLevel);
Expand Down
9 changes: 3 additions & 6 deletions src/d3d12/d3d12-command-buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,14 @@ void CommandBufferImpl::commitBarriers()
uint32_t mipLevelCount = texture->m_desc.mipLevelCount;
uint32_t arrayLayerCount =
texture->m_desc.arrayLength * (texture->m_desc.type == TextureType::TextureCube ? 6 : 1);
DXGI_FORMAT d3dFormat = D3DUtil::getMapFormat(texture->m_desc.format);
uint32_t planeCount = D3DUtil::getPlaneSliceCount(d3dFormat);
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Transition.pResource = texture->m_resource;
barrier.Transition.StateBefore = D3DUtil::getResourceState(textureBarrier.stateBefore);
barrier.Transition.StateAfter = D3DUtil::getResourceState(textureBarrier.stateAfter);
auto d3dFormat = D3DUtil::getMapFormat(texture->m_desc.format);
auto aspectMask = (int32_t)TextureAspect::Color;
while (aspectMask)
for (uint32_t planeIndex = 0; planeIndex < planeCount; ++planeIndex)
{
auto aspect = math::getLowestBit((int32_t)aspectMask);
aspectMask &= ~aspect;
auto planeIndex = D3DUtil::getPlaneSlice(d3dFormat, (TextureAspect)aspect);
barrier.Transition.Subresource = D3DUtil::getSubresourceIndex(
textureBarrier.mipLevel,
textureBarrier.arrayLayer,
Expand Down
11 changes: 3 additions & 8 deletions src/d3d12/d3d12-command-encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,10 @@ void ResourceCommandEncoderImpl::copyTexture(
return;
}

auto d3dFormat = D3DUtil::getMapFormat(dstTexture->m_desc.format);
auto aspectMask = (int32_t)dstSubresource.aspectMask;
if (dstSubresource.aspectMask == TextureAspect::Default)
aspectMask = (int32_t)TextureAspect::Color;
while (aspectMask)
DXGI_FORMAT d3dFormat = D3DUtil::getMapFormat(dstTexture->m_desc.format);
uint32_t planeCount = D3DUtil::getPlaneSliceCount(d3dFormat);
for (GfxIndex planeIndex = 0; planeIndex < planeCount; planeIndex++)
{
auto aspect = math::getLowestBit((int32_t)aspectMask);
aspectMask &= ~aspect;
auto planeIndex = D3DUtil::getPlaneSlice(d3dFormat, (TextureAspect)aspect);
for (GfxIndex layer = 0; layer < dstSubresource.layerCount; layer++)
{
for (GfxIndex mipLevel = 0; mipLevel < dstSubresource.mipLevelCount; mipLevel++)
Expand Down
8 changes: 4 additions & 4 deletions src/d3d12/d3d12-texture-view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ Result TextureViewImpl::getNativeHandle(NativeHandle* outHandle)
D3D12Descriptor TextureViewImpl::getSRV()
{
if (!m_srv)
m_srv = m_texture->getSRV(m_desc.format, m_texture->m_desc.type, m_desc.subresourceRange);
m_srv = m_texture->getSRV(m_desc.format, m_texture->m_desc.type, m_desc.aspect, m_desc.subresourceRange);
return m_srv;
}

D3D12Descriptor TextureViewImpl::getUAV()
{
if (!m_uav)
m_uav = m_texture->getUAV(m_desc.format, m_texture->m_desc.type, m_desc.subresourceRange);
m_uav = m_texture->getUAV(m_desc.format, m_texture->m_desc.type, m_desc.aspect, m_desc.subresourceRange);
return m_uav;
}

D3D12Descriptor TextureViewImpl::getRTV()
{
if (!m_rtv)
m_rtv = m_texture->getRTV(m_desc.format, m_texture->m_desc.type, m_desc.subresourceRange);
m_rtv = m_texture->getRTV(m_desc.format, m_texture->m_desc.type, m_desc.aspect, m_desc.subresourceRange);
return m_rtv;
}

D3D12Descriptor TextureViewImpl::getDSV()
{
if (!m_dsv)
m_dsv = m_texture->getDSV(m_desc.format, m_texture->m_desc.type, m_desc.subresourceRange);
m_dsv = m_texture->getDSV(m_desc.format, m_texture->m_desc.type, m_desc.aspect, m_desc.subresourceRange);
return m_dsv;
}

Expand Down
52 changes: 36 additions & 16 deletions src/d3d12/d3d12-texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ Result TextureImpl::getSharedHandle(NativeHandle* outHandle)
#endif
}

D3D12Descriptor TextureImpl::getSRV(Format format, TextureType type, const SubresourceRange& range)
D3D12Descriptor TextureImpl::getSRV(
Format format,
TextureType type,
TextureAspect aspect,
const SubresourceRange& range
)
{
ViewKey key = {format, type, range};
ViewKey key = {format, type, aspect, range};
D3D12Descriptor& descriptor = m_srvs[key];
if (descriptor)
return descriptor;
Expand Down Expand Up @@ -126,7 +131,7 @@ D3D12Descriptor TextureImpl::getSRV(Format format, TextureType type, const Subre
viewDesc.Texture2DArray.MipLevels = range.mipLevelCount;
viewDesc.Texture2DArray.FirstArraySlice = range.baseArrayLayer;
viewDesc.Texture2DArray.ArraySize = range.layerCount;
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, range.aspectMask);
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, aspect);
}
}
else
Expand All @@ -140,7 +145,7 @@ D3D12Descriptor TextureImpl::getSRV(Format format, TextureType type, const Subre
viewDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
viewDesc.Texture2D.MostDetailedMip = range.mipLevel;
viewDesc.Texture2D.MipLevels = range.mipLevelCount;
viewDesc.Texture2D.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, range.aspectMask);
viewDesc.Texture2D.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, aspect);
}
}
break;
Expand Down Expand Up @@ -173,9 +178,14 @@ D3D12Descriptor TextureImpl::getSRV(Format format, TextureType type, const Subre
return descriptor;
}

D3D12Descriptor TextureImpl::getUAV(Format format, TextureType type, const SubresourceRange& range)
D3D12Descriptor TextureImpl::getUAV(
Format format,
TextureType type,
TextureAspect aspect,
const SubresourceRange& range
)
{
ViewKey key = {format, type, range};
ViewKey key = {format, type, aspect, range};
D3D12Descriptor& descriptor = m_uavs[key];
if (descriptor)
return descriptor;
Expand Down Expand Up @@ -207,12 +217,12 @@ D3D12Descriptor TextureImpl::getUAV(Format format, TextureType type, const Subre
viewDesc.Texture2DArray.MipSlice = range.mipLevel;
viewDesc.Texture2DArray.ArraySize = range.layerCount;
viewDesc.Texture2DArray.FirstArraySlice = range.baseArrayLayer;
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, range.aspectMask);
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, aspect);
}
else
{
viewDesc.Texture2D.MipSlice = range.mipLevel;
viewDesc.Texture2D.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, range.aspectMask);
viewDesc.Texture2D.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, aspect);
}
break;
case TextureType::Texture3D:
Expand All @@ -226,7 +236,7 @@ D3D12Descriptor TextureImpl::getUAV(Format format, TextureType type, const Subre
viewDesc.Texture2DArray.MipSlice = range.mipLevel;
viewDesc.Texture2DArray.ArraySize = range.layerCount;
viewDesc.Texture2DArray.FirstArraySlice = range.baseArrayLayer;
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, range.aspectMask);
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, aspect);
break;
}

Expand All @@ -236,9 +246,14 @@ D3D12Descriptor TextureImpl::getUAV(Format format, TextureType type, const Subre
return descriptor;
}

D3D12Descriptor TextureImpl::getRTV(Format format, TextureType type, const SubresourceRange& range)
D3D12Descriptor TextureImpl::getRTV(
Format format,
TextureType type,
TextureAspect aspect,
const SubresourceRange& range
)
{
ViewKey key = {format, type, range};
ViewKey key = {format, type, aspect, range};
D3D12Descriptor& descriptor = m_rtvs[key];
if (descriptor)
return descriptor;
Expand Down Expand Up @@ -280,12 +295,12 @@ D3D12Descriptor TextureImpl::getRTV(Format format, TextureType type, const Subre
viewDesc.Texture2DArray.MipSlice = range.mipLevel;
viewDesc.Texture2DArray.ArraySize = range.layerCount;
viewDesc.Texture2DArray.FirstArraySlice = range.baseArrayLayer;
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, range.aspectMask);
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, aspect);
}
else
{
viewDesc.Texture2D.MipSlice = range.mipLevel;
viewDesc.Texture2D.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, range.aspectMask);
viewDesc.Texture2D.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, aspect);
}
}
break;
Expand All @@ -300,7 +315,7 @@ D3D12Descriptor TextureImpl::getRTV(Format format, TextureType type, const Subre
viewDesc.Texture2DArray.MipSlice = range.mipLevel;
viewDesc.Texture2DArray.ArraySize = range.layerCount;
viewDesc.Texture2DArray.FirstArraySlice = range.baseArrayLayer;
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, range.aspectMask);
viewDesc.Texture2DArray.PlaneSlice = D3DUtil::getPlaneSlice(viewDesc.Format, aspect);
break;
}

Expand All @@ -310,9 +325,14 @@ D3D12Descriptor TextureImpl::getRTV(Format format, TextureType type, const Subre
return descriptor;
}

D3D12Descriptor TextureImpl::getDSV(Format format, TextureType type, const SubresourceRange& range)
D3D12Descriptor TextureImpl::getDSV(
Format format,
TextureType type,
TextureAspect aspect,
const SubresourceRange& range
)
{
ViewKey key = {format, type, range};
ViewKey key = {format, type, aspect, range};
D3D12Descriptor& descriptor = m_dsvs[key];
if (descriptor)
return descriptor;
Expand Down
16 changes: 10 additions & 6 deletions src/d3d12/d3d12-texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ class TextureImpl : public Texture
{
Format format;
TextureType type;
TextureAspect aspect;
SubresourceRange range;
bool operator==(const ViewKey& other) const { return format == other.format && range == other.range; }
bool operator==(const ViewKey& other) const
{
return format == other.format && type == other.type && aspect == other.aspect && range == other.range;
}
};

struct ViewKeyHasher
Expand All @@ -35,7 +39,7 @@ class TextureImpl : public Texture
size_t hash = 0;
hash_combine(hash, key.format);
hash_combine(hash, key.type);
hash_combine(hash, key.range.aspectMask);
hash_combine(hash, key.aspect);
hash_combine(hash, key.range.baseArrayLayer);
hash_combine(hash, key.range.layerCount);
hash_combine(hash, key.range.mipLevel);
Expand All @@ -49,10 +53,10 @@ class TextureImpl : public Texture
std::unordered_map<ViewKey, D3D12Descriptor, ViewKeyHasher> m_rtvs;
std::unordered_map<ViewKey, D3D12Descriptor, ViewKeyHasher> m_dsvs;

D3D12Descriptor getSRV(Format format, TextureType type, const SubresourceRange& range);
D3D12Descriptor getUAV(Format format, TextureType type, const SubresourceRange& range);
D3D12Descriptor getRTV(Format format, TextureType type, const SubresourceRange& range);
D3D12Descriptor getDSV(Format format, TextureType type, const SubresourceRange& range);
D3D12Descriptor getSRV(Format format, TextureType type, TextureAspect aspect, const SubresourceRange& range);
D3D12Descriptor getUAV(Format format, TextureType type, TextureAspect aspect, const SubresourceRange& range);
D3D12Descriptor getRTV(Format format, TextureType type, TextureAspect aspect, const SubresourceRange& range);
D3D12Descriptor getDSV(Format format, TextureType type, TextureAspect aspect, const SubresourceRange& range);
};

} // namespace rhi::d3d12
6 changes: 3 additions & 3 deletions src/vulkan/vk-command-encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,12 @@ void ResourceCommandEncoderImpl::copyTexture(
srcSubresource.mipLevelCount = dstDesc.mipLevelCount;
}
VkImageCopy region = {};
region.srcSubresource.aspectMask = VulkanUtil::getAspectMask(srcSubresource.aspectMask, srcTexture->m_vkformat);
region.srcSubresource.aspectMask = VulkanUtil::getAspectMask(TextureAspect::All, srcTexture->m_vkformat);
region.srcSubresource.baseArrayLayer = srcSubresource.baseArrayLayer;
region.srcSubresource.mipLevel = srcSubresource.mipLevel;
region.srcSubresource.layerCount = srcSubresource.layerCount;
region.srcOffset = {(int32_t)srcOffset.x, (int32_t)srcOffset.y, (int32_t)srcOffset.z};
region.dstSubresource.aspectMask = VulkanUtil::getAspectMask(dstSubresource.aspectMask, dstTexture->m_vkformat);
region.dstSubresource.aspectMask = VulkanUtil::getAspectMask(TextureAspect::All, dstTexture->m_vkformat);
region.dstSubresource.baseArrayLayer = dstSubresource.baseArrayLayer;
region.dstSubresource.mipLevel = dstSubresource.mipLevel;
region.dstSubresource.layerCount = dstSubresource.layerCount;
Expand Down Expand Up @@ -813,7 +813,7 @@ void ResourceCommandEncoderImpl::copyTextureToBuffer(
region.bufferOffset = dstOffset;
region.bufferRowLength = 0;
region.bufferImageHeight = 0;
region.imageSubresource.aspectMask = VulkanUtil::getAspectMask(srcSubresource.aspectMask, srcTexture->m_vkformat);
region.imageSubresource.aspectMask = VulkanUtil::getAspectMask(TextureAspect::All, srcTexture->m_vkformat);
region.imageSubresource.mipLevel = srcSubresource.mipLevel;
region.imageSubresource.baseArrayLayer = srcSubresource.baseArrayLayer;
region.imageSubresource.layerCount = srcSubresource.layerCount;
Expand Down
30 changes: 20 additions & 10 deletions src/vulkan/vk-helper-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,19 +391,29 @@ VkPipelineStageFlags calcPipelineStageFlagsFromImageLayout(VkImageLayout layout)
}
}

VkImageAspectFlags getAspectMaskFromFormat(VkFormat format)
VkImageAspectFlags getAspectMaskFromFormat(VkFormat format, TextureAspect aspect)
{
switch (format)
switch (aspect)
{
case VK_FORMAT_D16_UNORM_S8_UINT:
case VK_FORMAT_D24_UNORM_S8_UINT:
case VK_FORMAT_D32_SFLOAT_S8_UINT:
return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
case VK_FORMAT_D16_UNORM:
case VK_FORMAT_D32_SFLOAT:
case VK_FORMAT_X8_D24_UNORM_PACK32:
case TextureAspect::All:
switch (format)
{
case VK_FORMAT_D16_UNORM_S8_UINT:
case VK_FORMAT_D24_UNORM_S8_UINT:
case VK_FORMAT_D32_SFLOAT_S8_UINT:
return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
case VK_FORMAT_D16_UNORM:
case VK_FORMAT_D32_SFLOAT:
case VK_FORMAT_X8_D24_UNORM_PACK32:
return VK_IMAGE_ASPECT_DEPTH_BIT;
case VK_FORMAT_S8_UINT:
return VK_IMAGE_ASPECT_STENCIL_BIT;
default:
return VK_IMAGE_ASPECT_COLOR_BIT;
}
case TextureAspect::DepthOnly:
return VK_IMAGE_ASPECT_DEPTH_BIT;
case VK_FORMAT_S8_UINT:
case TextureAspect::StencilOnly:
return VK_IMAGE_ASPECT_STENCIL_BIT;
default:
return VK_IMAGE_ASPECT_COLOR_BIT;
Expand Down
Loading

0 comments on commit d0795a9

Please sign in to comment.