Skip to content

Commit

Permalink
Hydrogent: enabled texture compression
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 9, 2024
1 parent a82d0db commit 3e4dd9b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Hydrogent/interface/HnRenderDelegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate
bool UseIndexPool = false;
bool EnableShadows = false;

TEXTURE_LOAD_COMPRESS_MODE TextureCompressMode = TEXTURE_LOAD_COMPRESS_MODE_NONE;

/// Whether to allow hot shader reload.
///
/// \remarks When hot shader reload is enabled, the renderer will need
Expand Down
13 changes: 8 additions & 5 deletions Hydrogent/interface/HnTextureRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ struct HnTextureIdentifier;
class HnTextureRegistry final
{
public:
HnTextureRegistry(IRenderDevice* pDevice,
GLTF::ResourceManager* pResourceManager);
HnTextureRegistry(IRenderDevice* pDevice,
GLTF::ResourceManager* pResourceManager,
TEXTURE_LOAD_COMPRESS_MODE CompressMode);
~HnTextureRegistry();

void Commit(IDeviceContext* pContext);
Expand Down Expand Up @@ -106,6 +107,8 @@ class HnTextureRegistry final
m_Cache.ProcessElements(Handler);
}

TEXTURE_LOAD_COMPRESS_MODE GetCompressMode() const { return m_CompressMode; }

private:
void InitializeHandle(IRenderDevice* pDevice,
IDeviceContext* pContext,
Expand All @@ -114,9 +117,9 @@ class HnTextureRegistry final
TextureHandle& Handle);

private:
RefCntAutoPtr<IRenderDevice> m_pDevice;

GLTF::ResourceManager* const m_pResourceManager;
RefCntAutoPtr<IRenderDevice> m_pDevice;
GLTF::ResourceManager* const m_pResourceManager;
const TEXTURE_LOAD_COMPRESS_MODE m_CompressMode;

ObjectsRegistry<pxr::TfToken, TextureHandleSharedPtr, pxr::TfToken::HashFunctor> m_Cache;

Expand Down
3 changes: 2 additions & 1 deletion Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ HnTextureRegistry::TextureHandleSharedPtr HnMaterial::GetDefaultTexture(HnTextur
[&]() {
RefCntAutoPtr<Image> pImage = CreateDefaultImage(DefaultTexName);

TextureLoadInfo LoadInfo{Name.GetText()};
TextureLoadInfo LoadInfo{Name.GetText()};
LoadInfo.CompressMode = TexRegistry.GetCompressMode();
RefCntAutoPtr<ITextureLoader> pLoader;
CreateTextureLoaderFromImage(pImage, LoadInfo, &pLoader);
VERIFY_EXPR(pLoader);
Expand Down
20 changes: 19 additions & 1 deletion Hydrogent/src/HnRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,25 @@ HnRenderDelegate::HnRenderDelegate(const CreateInfo& CI) :
m_PrimitiveAttribsCB{CreatePrimitiveAttribsCB(CI.pDevice)},
m_MaterialSRBCache{HnMaterial::CreateSRBCache()},
m_USDRenderer{CreateUSDRenderer(CI, m_PrimitiveAttribsCB, m_MaterialSRBCache)},
m_TextureRegistry{CI.pDevice, CI.TextureAtlasDim != 0 ? m_ResourceMgr : RefCntAutoPtr<GLTF::ResourceManager>{}},
m_TextureRegistry{
CI.pDevice,
CI.TextureAtlasDim != 0 ? m_ResourceMgr : RefCntAutoPtr<GLTF::ResourceManager>{},
[](IRenderDevice* pDevice, TEXTURE_LOAD_COMPRESS_MODE CompressMode) {
switch (CompressMode)
{
case TEXTURE_LOAD_COMPRESS_MODE_NONE:
return TEXTURE_LOAD_COMPRESS_MODE_NONE;

case TEXTURE_LOAD_COMPRESS_MODE_BC:
case TEXTURE_LOAD_COMPRESS_MODE_BC_HIGH_QUAL:
return pDevice->GetDeviceInfo().Features.TextureCompressionBC ? CompressMode : TEXTURE_LOAD_COMPRESS_MODE_NONE;

default:
UNEXPECTED("Unexpected compress mode");
return TEXTURE_LOAD_COMPRESS_MODE_NONE;
}
}(CI.pDevice, CI.TextureCompressMode),
},
m_GeometryPool{CI.pDevice, *m_ResourceMgr, CI.UseVertexPool, CI.UseIndexPool},
m_RenderParam{
std::make_unique<HnRenderParam>(
Expand Down
34 changes: 19 additions & 15 deletions Hydrogent/src/HnTextureRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ namespace Diligent
namespace USD
{

HnTextureRegistry::HnTextureRegistry(IRenderDevice* pDevice,
GLTF::ResourceManager* pResourceManager) :
HnTextureRegistry::HnTextureRegistry(IRenderDevice* pDevice,
GLTF::ResourceManager* pResourceManager,
TEXTURE_LOAD_COMPRESS_MODE CompressMode) :
m_pDevice{pDevice},
m_pResourceManager{pResourceManager}
m_pResourceManager{pResourceManager},
m_CompressMode{CompressMode}
{
}

Expand All @@ -61,13 +63,14 @@ void HnTextureRegistry::InitializeHandle(IRenderDevice* pDevice,
{
VERIFY_EXPR(pContext != nullptr);

IDynamicTextureAtlas* pAtlas = Handle.pAtlasSuballocation->GetAtlas();
ITexture* pDstTex = pAtlas->GetTexture();
const TextureDesc& AtlasDesc = pAtlas->GetAtlasDesc();
const TextureData UploadData = pLoader->GetTextureData();
const TextureDesc& SrcDataDesc = pLoader->GetTextureDesc();
const uint2& Origin = Handle.pAtlasSuballocation->GetOrigin();
const Uint32 Slice = Handle.pAtlasSuballocation->GetSlice();
IDynamicTextureAtlas* pAtlas = Handle.pAtlasSuballocation->GetAtlas();
ITexture* pDstTex = pAtlas->GetTexture();
const TextureDesc& AtlasDesc = pAtlas->GetAtlasDesc();
const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs(AtlasDesc.Format);
const TextureData UploadData = pLoader->GetTextureData();
const TextureDesc& SrcDataDesc = pLoader->GetTextureDesc();
const uint2& Origin = Handle.pAtlasSuballocation->GetOrigin();
const Uint32 Slice = Handle.pAtlasSuballocation->GetSlice();

const Uint32 MipsToUpload = std::min(UploadData.NumSubresources, AtlasDesc.MipLevels);
for (Uint32 mip = 0; mip < MipsToUpload; ++mip)
Expand All @@ -76,10 +79,10 @@ void HnTextureRegistry::InitializeHandle(IRenderDevice* pDevice,
const MipLevelProperties MipProps = GetMipLevelProperties(SrcDataDesc, mip);

Box UpdateBox;
UpdateBox.MinX = Origin.x >> mip;
UpdateBox.MaxX = UpdateBox.MinX + MipProps.LogicalWidth;
UpdateBox.MinY = Origin.y >> mip;
UpdateBox.MaxY = UpdateBox.MinY + MipProps.LogicalHeight;
UpdateBox.MinX = AlignDown(Origin.x >> mip, FmtAttribs.BlockWidth);
UpdateBox.MaxX = AlignUp(UpdateBox.MinX + MipProps.LogicalWidth, FmtAttribs.BlockWidth);
UpdateBox.MinY = AlignDown(Origin.y >> mip, FmtAttribs.BlockHeight);
UpdateBox.MaxY = AlignUp(UpdateBox.MinY + MipProps.LogicalHeight, FmtAttribs.BlockHeight);
pContext->UpdateTexture(pDstTex, mip, Slice, UpdateBox, LevelData, RESOURCE_STATE_TRANSITION_MODE_NONE, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
}
}
Expand Down Expand Up @@ -206,7 +209,7 @@ HnTextureRegistry::TextureHandleSharedPtr HnTextureRegistry::Allocate(const HnTe
}

return Allocate(TexId.FilePath, TexId.SubtextureId.Swizzle, SamplerParams,
[&TexId, Format]() {
[&TexId, Format, CompressMode = m_CompressMode]() {
TextureLoadInfo LoadInfo;
LoadInfo.Name = TexId.FilePath.GetText();
LoadInfo.Format = Format;
Expand All @@ -216,6 +219,7 @@ HnTextureRegistry::TextureHandleSharedPtr HnTextureRegistry::Allocate(const HnTe
LoadInfo.IsSRGB = TexId.SubtextureId.IsSRGB;
LoadInfo.PermultiplyAlpha = TexId.SubtextureId.PremultiplyAlpha;
LoadInfo.Swizzle = TexId.SubtextureId.Swizzle;
LoadInfo.CompressMode = CompressMode;

return CreateTextureLoaderFromSdfPath(TexId.FilePath.GetText(), LoadInfo);
});
Expand Down

0 comments on commit 3e4dd9b

Please sign in to comment.