Skip to content

Commit

Permalink
Merge branch 'main' of github.com:google/filament
Browse files Browse the repository at this point in the history
  • Loading branch information
dazcjones committed Oct 19, 2023
2 parents c6b2605 + b62991d commit ee3a535
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
4 changes: 2 additions & 2 deletions filament/backend/src/vulkan/VulkanTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ VulkanTexture::VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice,
// any kind of attachment (color or depth).
const auto& limits = context.getPhysicalDeviceLimits();
if (imageInfo.usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
samples = reduceSampleCount(samples, isDepthFormat(mVkFormat)
samples = reduceSampleCount(samples, isVkDepthFormat(mVkFormat)
? limits.sampledImageDepthSampleCounts
: limits.sampledImageColorSampleCounts);
}
Expand Down Expand Up @@ -452,7 +452,7 @@ void VulkanTexture::transitionLayout(VkCommandBuffer cmdbuf, const VkImageSubres
<< "," << range.levelCount << ")"
<< " from=" << oldLayout << " to=" << newLayout
<< " format=" << mVkFormat
<< " depth=" << isDepthFormat(mVkFormat)
<< " depth=" << isVkDepthFormat(mVkFormat)
<< " slice-by-slice=" << transitionSliceBySlice
<< utils::io::endl;
#endif
Expand Down
9 changes: 4 additions & 5 deletions filament/backend/src/vulkan/VulkanUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,14 +638,13 @@ VkImageAspectFlags getImageAspect(VkFormat format) {
}
}

bool isDepthFormat(VkFormat format) {
bool isVkDepthFormat(VkFormat format) {
return (getImageAspect(format) & VK_IMAGE_ASPECT_DEPTH_BIT) != 0;
}

// TODO: support stencil attachments
// bool isStencilFormat(VkFormat format) {
// return (getImageAspect(format) & VK_IMAGE_ASPECT_STENCIL_BIT) != 0;
// }
bool isVkStencilFormat(VkFormat format) {
return (getImageAspect(format) & VK_IMAGE_ASPECT_STENCIL_BIT) != 0;
}

static uint32_t mostSignificantBit(uint32_t x) { return 1ul << (31ul - utils::clz(x)); }

Expand Down
6 changes: 2 additions & 4 deletions filament/backend/src/vulkan/VulkanUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ VkShaderStageFlags getShaderStageFlags(ShaderStageFlags stageFlags);

bool equivalent(const VkRect2D& a, const VkRect2D& b);
bool equivalent(const VkExtent2D& a, const VkExtent2D& b);
bool isDepthFormat(VkFormat format);

// TODO: support stencil attachments
// bool isStencilFormat(VkFormat format);
bool isVkDepthFormat(VkFormat format);
bool isVkStencilFormat(VkFormat format);

VkImageAspectFlags getImageAspect(VkFormat format);
uint8_t reduceSampleCount(uint8_t sampleCount, VkSampleCountFlags mask);
Expand Down
5 changes: 2 additions & 3 deletions filament/backend/src/vulkan/platform/VulkanPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,8 @@ VkFormatList findAttachmentDepthFormats(VkPhysicalDevice device) {
VK_FORMAT_D32_SFLOAT,
VK_FORMAT_X8_D24_UNORM_PACK32,

// TODO: support stencil attachments
// VK_FORMAT_D32_SFLOAT_S8_UINT,
// VK_FORMAT_D24_UNORM_S8_UINT,
VK_FORMAT_D32_SFLOAT_S8_UINT,
VK_FORMAT_D24_UNORM_S8_UINT,
};
std::vector<VkFormat> selectedFormats;
for (VkFormat format: formats) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace {

std::tuple<VkImage, VkDeviceMemory> createImageAndMemory(VulkanContext const& context,
VkDevice device, VkExtent2D extent, VkFormat format) {
bool const isDepth = isDepthFormat(format);
bool const isDepth = isVkDepthFormat(format);
// Filament expects blit() to work with any texture, so we almost always set these usage flags.
// TODO: investigate performance implications of setting these flags.
VkImageUsageFlags const blittable
Expand Down Expand Up @@ -76,6 +76,14 @@ std::tuple<VkImage, VkDeviceMemory> createImageAndMemory(VulkanContext const& co
return std::tuple(image, imageMemory);
}

VkFormat selectDepthFormat(VkFormatList const& depthFormats, bool hasStencil) {
auto const formatItr = std::find_if(depthFormats.begin(), depthFormats.end(),
hasStencil ? isVkStencilFormat : isVkDepthFormat);
assert_invariant(
formatItr != depthFormats.end() && "Cannot find suitable swapchain depth format");
return *formatItr;
}

}// anonymous namespace

VulkanPlatformSwapChainImpl::VulkanPlatformSwapChainImpl(VulkanContext const& context,
Expand Down Expand Up @@ -116,7 +124,8 @@ VulkanPlatformSurfaceSwapChain::VulkanPlatformSurfaceSwapChain(VulkanContext con
mPhysicalDevice(physicalDevice),
mSurface(surface),
mFallbackExtent(fallbackExtent),
mUsesRGB((flags & backend::SWAP_CHAIN_CONFIG_SRGB_COLORSPACE) != 0) {
mUsesRGB((flags & backend::SWAP_CHAIN_CONFIG_SRGB_COLORSPACE) != 0),
mHasStencil((flags & backend::SWAP_CHAIN_HAS_STENCIL_BUFFER) != 0) {
assert_invariant(surface);
create();
}
Expand Down Expand Up @@ -152,10 +161,15 @@ VkResult VulkanPlatformSurfaceSwapChain::create() {
// Find a suitable surface format.
FixedCapacityVector<VkSurfaceFormatKHR> const surfaceFormats
= enumerate(vkGetPhysicalDeviceSurfaceFormatsKHR, mPhysicalDevice, mSurface);
FixedCapacityVector<VkFormat> expectedFormats
= {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM};
std::array<VkFormat, 2> expectedFormats = {
VK_FORMAT_R8G8B8A8_UNORM,
VK_FORMAT_B8G8R8A8_UNORM,
};
if (mUsesRGB) {
expectedFormats = {VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_B8G8R8A8_SRGB};
expectedFormats = {
VK_FORMAT_R8G8B8A8_SRGB,
VK_FORMAT_B8G8R8A8_SRGB,
};
}
for (VkSurfaceFormatKHR const& format: surfaceFormats) {
if (std::any_of(expectedFormats.begin(), expectedFormats.end(),
Expand Down Expand Up @@ -228,14 +242,18 @@ VkResult VulkanPlatformSurfaceSwapChain::create() {

mSwapChainBundle.colors = enumerate(vkGetSwapchainImagesKHR, mDevice, mSwapchain);
mSwapChainBundle.colorFormat = surfaceFormat.format;
mSwapChainBundle.depthFormat =
selectDepthFormat(mContext.getAttachmentDepthFormats(), mHasStencil);
mSwapChainBundle.depth = createImage(mSwapChainBundle.extent, mSwapChainBundle.depthFormat);

slog.i << "vkCreateSwapchain"
<< ": " << mSwapChainBundle.extent.width << "x" << mSwapChainBundle.extent.height << ", "
<< surfaceFormat.format << ", " << surfaceFormat.colorSpace << ", "
<< mSwapChainBundle.colors.size() << ", " << caps.currentTransform << io::endl;
<< "swapchain-size=" << mSwapChainBundle.colors.size() << ", "
<< "identity-transform=" << (caps.currentTransform == 1) << ", "
<< "depth=" << mSwapChainBundle.depthFormat
<< io::endl;

auto const& depthFormats = mContext.getAttachmentDepthFormats();
mSwapChainBundle.depthFormat = depthFormats[0];
mSwapChainBundle.depth = createImage(mSwapChainBundle.extent, mSwapChainBundle.depthFormat);
return result;
}

Expand Down Expand Up @@ -310,8 +328,9 @@ VulkanPlatformHeadlessSwapChain::VulkanPlatformHeadlessSwapChain(VulkanContext c
images[i] = createImage(extent, mSwapChainBundle.colorFormat);
}

auto const& depthFormats = mContext.getAttachmentDepthFormats();
mSwapChainBundle.depthFormat = depthFormats[0];
bool const hasStencil = (flags & backend::SWAP_CHAIN_HAS_STENCIL_BUFFER) != 0;
mSwapChainBundle.depthFormat =
selectDepthFormat(mContext.getAttachmentDepthFormats(), hasStencil);
mSwapChainBundle.depth = createImage(extent, mSwapChainBundle.depthFormat);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ struct VulkanPlatformSurfaceSwapChain : public VulkanPlatformSwapChainImpl {
VkExtent2D const mFallbackExtent;

bool mUsesRGB = false;
bool mHasStencil = false;
bool mSuboptimal;
};

Expand Down

0 comments on commit ee3a535

Please sign in to comment.