Skip to content

Commit

Permalink
Initial implementation of texture views
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Oct 22, 2024
1 parent 0c837b6 commit 06bc885
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lvk/LVK.h
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,15 @@ struct TextureDesc {
const char* debugName = "";
};

struct TextureViewDesc {
TextureType type = TextureType_2D;
uint32_t layer = 0;
uint32_t numLayers = 1;
uint32_t mipLevel = 0;
uint32_t numMipLevels = 1;
ComponentMapping swizzle = {};
};

enum AccelStructType : uint8_t {
AccelStructType_Invalid = 0,
AccelStructType_TLAS = 1,
Expand Down Expand Up @@ -995,6 +1004,10 @@ class IContext {
[[nodiscard]] virtual Holder<TextureHandle> createTexture(const TextureDesc& desc,
const char* debugName = nullptr,
Result* outResult = nullptr) = 0;
[[nodiscard]] virtual Holder<TextureHandle> createTextureView(TextureHandle texture,
const TextureViewDesc& desc,
const char* debugName = nullptr,
Result* outResult = nullptr) = 0;
[[nodiscard]] virtual Holder<ComputePipelineHandle> createComputePipeline(const ComputePipelineDesc& desc,
Result* outResult = nullptr) = 0;
[[nodiscard]] virtual Holder<RenderPipelineHandle> createRenderPipeline(const RenderPipelineDesc& desc, Result* outResult = nullptr) = 0;
Expand Down
99 changes: 97 additions & 2 deletions lvk/vulkan/VulkanClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3852,8 +3852,7 @@ lvk::Holder<lvk::TextureHandle> lvk::VulkanContext::createTexture(const TextureD
vkDevice_, vkImageViewType, vkFormat, aspect, 0, VK_REMAINING_MIP_LEVELS, 0, numLayers, mapping, ycbcrInfo, debugNameImageView);

if (image.vkUsageFlags_ & VK_IMAGE_USAGE_STORAGE_BIT) {
if (desc.swizzle.r != Swizzle_Default || desc.swizzle.g != Swizzle_Default || desc.swizzle.b != Swizzle_Default ||
desc.swizzle.a != Swizzle_Default) {
if (!desc.swizzle.identity()) {
// use identity swizzle for storage images
image.imageViewStorage_ = image.createImageView(
vkDevice_, vkImageViewType, vkFormat, aspect, 0, VK_REMAINING_MIP_LEVELS, 0, numLayers, {}, ycbcrInfo, debugNameImageView);
Expand Down Expand Up @@ -3889,6 +3888,102 @@ lvk::Holder<lvk::TextureHandle> lvk::VulkanContext::createTexture(const TextureD
return {this, handle};
}

lvk::Holder<lvk::TextureHandle> lvk::VulkanContext::createTextureView(lvk::TextureHandle texture,
const TextureViewDesc& desc,
const char* debugName,
Result* outResult) {
LVK_ASSERT(texture.valid());

// TODO: Texture views are still work-in-progress. Beware!

const VulkanImage* baseImage = texturesPool_.get(texture);

VulkanImage newImage = *baseImage;

// drop old existing views - the baseImage owns them
memset(&newImage.imageViewStorage_, 0, sizeof(newImage.imageViewStorage_));
memset(&newImage.imageViewForFramebuffer_, 0, sizeof(newImage.imageViewForFramebuffer_));

newImage.isSwapchainImage_ = true; // TODO: rename this to `isExternallyManaged` etc

VkImageAspectFlags aspect = 0;
if (newImage.isDepthFormat_ || newImage.isStencilFormat_) {
if (newImage.isDepthFormat_) {
aspect |= VK_IMAGE_ASPECT_DEPTH_BIT;
} else if (newImage.isStencilFormat_) {
aspect |= VK_IMAGE_ASPECT_STENCIL_BIT;
}
} else {
aspect = VK_IMAGE_ASPECT_COLOR_BIT;
}

VkImageViewType vkImageViewType = VK_IMAGE_VIEW_TYPE_MAX_ENUM;
switch (desc.type) {
case TextureType_2D:
vkImageViewType = desc.numLayers > 1 ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D;
break;
case TextureType_3D:
vkImageViewType = VK_IMAGE_VIEW_TYPE_3D;
break;
case TextureType_Cube:
vkImageViewType = desc.numLayers > 1 ? VK_IMAGE_VIEW_TYPE_CUBE_ARRAY : VK_IMAGE_VIEW_TYPE_CUBE;
break;
default:
LVK_ASSERT_MSG(false, "Code should NOT be reached");
Result::setResult(outResult, Result::Code::RuntimeError, "Unsupported texture view type");
return {};
}

const VkComponentMapping mapping = {
.r = VkComponentSwizzle(desc.swizzle.r),
.g = VkComponentSwizzle(desc.swizzle.g),
.b = VkComponentSwizzle(desc.swizzle.b),
.a = VkComponentSwizzle(desc.swizzle.a),
};

newImage.imageView_ = newImage.createImageView(vkDevice_,
vkImageViewType,
newImage.vkImageFormat_,
aspect,
desc.mipLevel,
desc.numMipLevels,
desc.layer,
desc.numLayers,
mapping,
nullptr,
debugName);

if (!LVK_VERIFY(newImage.imageView_ != VK_NULL_HANDLE)) {
Result::setResult(outResult, Result::Code::RuntimeError, "Cannot create VkImageView");
return {};
}

if (newImage.vkUsageFlags_ & VK_IMAGE_USAGE_STORAGE_BIT) {
if (!desc.swizzle.identity()) {
// use identity swizzle for storage images
newImage.imageViewStorage_ = newImage.createImageView(vkDevice_,
vkImageViewType,
newImage.vkImageFormat_,
aspect,
0,
VK_REMAINING_MIP_LEVELS,
0,
desc.numLayers,
{},
nullptr,
debugName);
LVK_ASSERT(newImage.imageViewStorage_ != VK_NULL_HANDLE);
}
}

TextureHandle handle = texturesPool_.create(std::move(newImage));

awaitingCreation_ = true;

return {this, handle};
}


lvk::AccelStructHandle lvk::VulkanContext::createBLAS(const AccelStructDesc& desc, Result* outResult) {
LVK_ASSERT(desc.type == AccelStructType_BLAS);
LVK_ASSERT(desc.geometryType == AccelStructGeomType_Triangles);
Expand Down
4 changes: 4 additions & 0 deletions lvk/vulkan/VulkanClasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ class VulkanContext final : public IContext {
Holder<BufferHandle> createBuffer(const BufferDesc& desc, Result* outResult) override;
Holder<SamplerHandle> createSampler(const SamplerStateDesc& desc, Result* outResult) override;
Holder<TextureHandle> createTexture(const TextureDesc& desc, const char* debugName, Result* outResult) override;
Holder<TextureHandle> createTextureView(TextureHandle texture,
const TextureViewDesc& desc,
const char* debugName,
Result* outResult) override;

Holder<ComputePipelineHandle> createComputePipeline(const ComputePipelineDesc& desc, Result* outResult) override;
Holder<RenderPipelineHandle> createRenderPipeline(const RenderPipelineDesc& desc, Result* outResult) override;
Expand Down

0 comments on commit 06bc885

Please sign in to comment.