Skip to content

Commit

Permalink
Mipmap Support (#892)
Browse files Browse the repository at this point in the history
* Added mipmapping support

* Cleanup
  • Loading branch information
jaremieromer authored Feb 12, 2025
1 parent 7a9a710 commit 2d8402d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ auto ToTextureFormat(nc::asset::asset_flags_type flags) -> Diligent::TEXTURE_FOR
: Diligent::TEX_FORMAT_RGBA8_UNORM_SRGB;
}

auto ToTextureDesc(const nc::asset::Texture& texture, Diligent::TEXTURE_FORMAT format) -> Diligent::TextureDesc
auto ToTextureDesc(const nc::asset::Texture& texture, Diligent::TEXTURE_FORMAT format, uint32_t mipLevels) -> Diligent::TextureDesc
{
/** @todo 750 Add mipmaps */
auto texDesc = Diligent::TextureDesc{
Expand All @@ -103,7 +103,12 @@ auto ToTextureDesc(const nc::asset::Texture& texture, Diligent::TEXTURE_FORMAT f
format
};

texDesc.MipLevels = mipLevels;
texDesc.BindFlags = Diligent::BIND_FLAGS::BIND_SHADER_RESOURCE;
if (mipLevels > 1)
{
texDesc.MiscFlags = Diligent::MISC_TEXTURE_FLAG_GENERATE_MIPS;
}
return texDesc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ auto GetVariable(const ResourceDesc& desc, Diligent::IShaderResourceBinding* srb
}

auto ToTextureFormat(nc::asset::asset_flags_type flags) -> Diligent::TEXTURE_FORMAT;
auto ToTextureDesc(const nc::asset::Texture& texture, Diligent::TEXTURE_FORMAT format) -> Diligent::TextureDesc;
auto ToTextureDesc(const nc::asset::Texture& texture, Diligent::TEXTURE_FORMAT format, uint32_t mipLevels = 1u) -> Diligent::TextureDesc;
auto ToTextureSubResData(const nc::asset::Texture& texture) -> Diligent::TextureSubResData;
void SetArrayRegion(Diligent::IShaderResourceVariable* variable, std::span<Diligent::IDeviceObject*> views, size_t offset, size_t count);
void InitializeArray(Diligent::IDeviceContext& context, Diligent::IRenderDevice& device, Diligent::IShaderResourceVariable* variable, uint32_t arraySize, bool transition = true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ void TextureBufferResource::Load(std::span<const asset::TextureWithId> textures,

for (const auto& [texture, id, flags] : textures)
{
auto subResource = ToTextureSubResData(texture);
auto texData = Diligent::TextureData{&subResource, 1, &context};
auto desc = ToTextureDesc(texture, ToTextureFormat(flags));
auto mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(texture.width, texture.height)))) + 1;
auto mipSubResources = std::vector<Diligent::TextureSubResData>{mipLevels};

for (auto& mipLevel : mipSubResources)
{
mipLevel = ToTextureSubResData(texture);
}

auto texData = Diligent::TextureData{mipSubResources.data(), mipLevels, &context};
auto desc = ToTextureDesc(texture, ToTextureFormat(flags), mipLevels);
auto& textureHandle = m_textures.emplace_back();
device.CreateTexture(desc, &texData, &textureHandle);
if (!textureHandle)
Expand All @@ -67,6 +74,11 @@ void TextureBufferResource::Load(std::span<const asset::TextureWithId> textures,
Diligent::RESOURCE_STATE_SHADER_RESOURCE,
Diligent::STATE_TRANSITION_FLAG_UPDATE_STATE
);

if (mipLevels > 1)
{
context.GenerateMips(textureHandle->GetDefaultView(Diligent::TEXTURE_VIEW_SHADER_RESOURCE));
}
}

context.TransitionResourceStates(static_cast<uint32_t>(barriers.size()), barriers.data());
Expand Down

0 comments on commit 2d8402d

Please sign in to comment.