Skip to content

Commit

Permalink
Merge branch 'main' into pf/bluegl-osmesa-explicit-link
Browse files Browse the repository at this point in the history
  • Loading branch information
poweifeng authored Dec 12, 2024
2 parents fdff028 + 6a772bf commit 7201fcb
Show file tree
Hide file tree
Showing 32 changed files with 507 additions and 61 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ repositories {
}
dependencies {
implementation 'com.google.android.filament:filament-android:1.56.2'
implementation 'com.google.android.filament:filament-android:1.56.3'
}
```

Expand All @@ -51,7 +51,7 @@ Here are all the libraries available in the group `com.google.android.filament`:
iOS projects can use CocoaPods to install the latest release:

```shell
pod 'Filament', '~> 1.56.2'
pod 'Filament', '~> 1.56.3'
```

## Documentation
Expand Down
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ A new header is inserted each time a *tag* is created.
Instead, if you are authoring a PR for the main branch, add your release note to
[NEW_RELEASE_NOTES.md](./NEW_RELEASE_NOTES.md).

## v1.56.4


## v1.56.3


Expand Down
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.google.android.filament
VERSION_NAME=1.56.2
VERSION_NAME=1.56.3

POM_DESCRIPTION=Real-time physically based rendering engine for Android.

Expand Down
6 changes: 4 additions & 2 deletions filament/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,12 @@ if (FILAMENT_SUPPORTS_VULKAN)
src/vulkan/VulkanUtility.cpp
src/vulkan/VulkanUtility.h
)
if (ANDROID OR LINUX OR WIN32)
list(APPEND SRCS src/vulkan/platform/VulkanPlatformAndroidLinuxWindows.cpp)
if (LINUX OR WIN32)
list(APPEND SRCS src/vulkan/platform/VulkanPlatformLinuxWindows.cpp)
elseif (APPLE OR IOS)
list(APPEND SRCS src/vulkan/platform/VulkanPlatformApple.mm)
elseif (ANDROID)
list(APPEND SRCS src/vulkan/platform/VulkanPlatformAndroid.cpp)
endif()
endif()

Expand Down
56 changes: 56 additions & 0 deletions filament/backend/include/backend/platforms/VulkanPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,64 @@ class VulkanPlatform : public Platform, utils::PrivateImplementation<VulkanPlatf
*/
VkQueue getProtectedGraphicsQueue() const noexcept;

struct ExternalImageMetadata {
/**
* The width of the external image
*/
uint32_t width;

/**
* The height of the external image
*/
uint32_t height;

/**
* The layer count of the external image
*/
uint32_t layers;

/**
* The format of the external image
*/
VkFormat format;

/**
* An external buffer can be protected. This tells you if it is.
*/
bool isProtected;

/**
* The type of external format (opaque int) if used.
*/
uint64_t externalFormat;

/**
* Image usage
*/
VkImageUsageFlags usage;

/**
* Allocation size
*/
VkDeviceSize allocationSize;

/**
* Heap information
*/
uint32_t memoryTypeBits;
};
virtual ExternalImageMetadata getExternalImageMetadata(void* externalImage);

using ImageData = std::pair<VkImage, VkDeviceMemory>;
virtual ImageData createExternalImage(void* externalImage,
const ExternalImageMetadata& metadata);

private:
static ExtensionSet getSwapchainInstanceExtensions();
static ExternalImageMetadata getExternalImageMetadataImpl(void* externalImage,
VkDevice device);
static ImageData createExternalImageImpl(void* externalImage, VkDevice device,
const VkAllocationCallbacks* allocator, const ExternalImageMetadata& metadata);

// Platform dependent helper methods
using SurfaceBundle = std::tuple<VkSurfaceKHR, VkExtent2D>;
Expand Down
1 change: 1 addition & 0 deletions filament/backend/src/metal/MetalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ struct MetalContext {
bool supportsTextureSwizzling = false;
bool supportsAutoDepthResolve = false;
bool supportsMemorylessRenderTargets = false;
bool supportsDepthClamp = false;
uint8_t maxColorRenderTargets = 4;
struct {
uint8_t common;
Expand Down
18 changes: 13 additions & 5 deletions filament/backend/src/metal/MetalDriver.mm
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
mContext->highestSupportedGpuFamily.mac >= 2; // newer macOS GPUs
}

mContext->supportsDepthClamp = false;
if (@available(macOS 10.11, iOS 11.0, *)) {
mContext->supportsDepthClamp = true;
}

// In order to support resolve store action on depth attachment, the GPU needs to support it.
// Note that support for depth resolve implies support for stencil resolve using .sample0 resolve filter.
// (Other resolve filters are supported starting .apple5 and .mac2 families).
Expand Down Expand Up @@ -1063,7 +1068,7 @@
}

bool MetalDriver::isDepthClampSupported() {
return true;
return mContext->supportsDepthClamp;
}

bool MetalDriver::isWorkaroundNeeded(Workaround workaround) {
Expand Down Expand Up @@ -1258,6 +1263,7 @@
mContext->cullModeState.invalidate();
mContext->windingState.invalidate();
mContext->scissorRectState.invalidate();
mContext->depthClampState.invalidate();
mContext->currentPolygonOffset = {0.0f, 0.0f};

mContext->finalizedDescriptorSets.clear();
Expand Down Expand Up @@ -1729,10 +1735,12 @@
}

// depth clip mode
MTLDepthClipMode depthClipMode = rs.depthClamp ? MTLDepthClipModeClamp : MTLDepthClipModeClip;
mContext->depthClampState.updateState(depthClipMode);
if (mContext->depthClampState.stateChanged()) {
[mContext->currentRenderPassEncoder setDepthClipMode:depthClipMode];
if (mContext->supportsDepthClamp) {
MTLDepthClipMode depthClipMode = rs.depthClamp ? MTLDepthClipModeClamp : MTLDepthClipModeClip;
mContext->depthClampState.updateState(depthClipMode);
if (mContext->depthClampState.stateChanged()) {
[mContext->currentRenderPassEncoder setDepthClipMode:depthClipMode];
}
}

// Set the depth-stencil state, if a state change is needed.
Expand Down
8 changes: 5 additions & 3 deletions filament/backend/src/metal/MetalShaderCompiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ bool isReady() const noexcept {
CompilerPriorityQueue const priorityQueue = program.getPriorityQueue();
mCompilerThreadPool.queue(priorityQueue, token,
[this, name, device = mDevice, program = std::move(program), token]() {
MetalFunctionBundle compiledProgram = compileProgram(program, device);
token->set(compiledProgram);
mCallbackManager.put(token->handle);
@autoreleasepool {
MetalFunctionBundle compiledProgram = compileProgram(program, device);
token->set(compiledProgram);
mCallbackManager.put(token->handle);
}
});

break;
Expand Down
21 changes: 20 additions & 1 deletion filament/backend/src/vulkan/VulkanDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,25 @@ void VulkanDriver::createTextureViewSwizzleR(Handle<HwTexture> th, Handle<HwText
}

void VulkanDriver::createTextureExternalImageR(Handle<HwTexture> th, backend::TextureFormat format,
uint32_t width, uint32_t height, backend::TextureUsage usage, void* image) {
uint32_t width, uint32_t height, backend::TextureUsage usage, void* externalImage) {
FVK_SYSTRACE_SCOPE();

const auto& metadata = mPlatform->getExternalImageMetadata(externalImage);
if (metadata.isProtected) {
usage |= backend::TextureUsage::PROTECTED;
}

assert_invariant(width == metadata.width);
assert_invariant(height == metadata.height);
assert_invariant(getVkFormat(format) == metadata.format);

const auto& data = mPlatform->createExternalImage(externalImage, metadata);

auto texture = resource_ptr<VulkanTexture>::make(&mResourceManager, th, mPlatform->getDevice(),
mAllocator, &mResourceManager, &mCommands, data.first, data.second, metadata.format,
1, metadata.width, metadata.height, usage, mStagePool);

texture.inc();
}

void VulkanDriver::createTextureExternalImagePlaneR(Handle<HwTexture> th,
Expand Down Expand Up @@ -1172,6 +1190,7 @@ TimerQueryResult VulkanDriver::getTimerQueryValue(Handle<HwTimerQuery> tqh, uint
}

void VulkanDriver::setExternalImage(Handle<HwTexture> th, void* image) {

}

void VulkanDriver::setExternalImagePlane(Handle<HwTexture> th, void* image, uint32_t plane) {
Expand Down
11 changes: 6 additions & 5 deletions filament/backend/src/vulkan/VulkanSwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ void VulkanSwapChain::update() {
}
for (auto const color: bundle.colors) {
auto colorTexture = fvkmemory::resource_ptr<VulkanTexture>::construct(mResourceManager,
device, mAllocator, mResourceManager, mCommands, color, bundle.colorFormat, 1,
bundle.extent.width, bundle.extent.height, TextureUsage::COLOR_ATTACHMENT,
device, mAllocator, mResourceManager, mCommands, color, VK_NULL_HANDLE,
bundle.colorFormat, 1, bundle.extent.width, bundle.extent.height, colorUsage,
mStagePool);
mColors.push_back(colorTexture);
}

mDepth = fvkmemory::resource_ptr<VulkanTexture>::construct(mResourceManager, device, mAllocator,
mResourceManager, mCommands, bundle.depth, bundle.depthFormat, 1, bundle.extent.width,
bundle.extent.height, TextureUsage::DEPTH_ATTACHMENT, mStagePool);
mDepth = fvkmemory::resource_ptr<VulkanTexture>::construct(mResourceManager, device,
mAllocator, mResourceManager, mCommands, bundle.depth, VK_NULL_HANDLE,
bundle.depthFormat, 1, bundle.extent.width, bundle.extent.height, depthUsage,
mStagePool);

mExtent = bundle.extent;
}
Expand Down
5 changes: 3 additions & 2 deletions filament/backend/src/vulkan/VulkanTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,15 @@ VulkanTextureState::VulkanTextureState(VkDevice device, VmaAllocator allocator,
// Constructor for internally passed VkImage
VulkanTexture::VulkanTexture(VkDevice device, VmaAllocator allocator,
fvkmemory::ResourceManager* resourceManager, VulkanCommands* commands, VkImage image,
VkFormat format, uint8_t samples, uint32_t width, uint32_t height, TextureUsage tusage,
VulkanStagePool& stagePool)
VkDeviceMemory memory, VkFormat format, uint8_t samples, uint32_t width,
uint32_t height, TextureUsage tusage, VulkanStagePool& stagePool)
: HwTexture(SamplerType::SAMPLER_2D, 1, samples, width, height, 1, TextureFormat::UNUSED,
tusage),
mState(fvkmemory::resource_ptr<VulkanTextureState>::construct(resourceManager, device,
allocator, commands, stagePool, format, imgutil::getViewType(SamplerType::SAMPLER_2D),
1, 1, getDefaultLayoutImpl(tusage), any(usage & TextureUsage::PROTECTED))) {
mState->mTextureImage = image;
mState->mTextureImageMemory = memory;
mPrimaryViewRange = mState->mFullViewRange;
}

Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/vulkan/VulkanTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ struct VulkanTexture : public HwTexture, fvkmemory::Resource {
// The texture will never destroy the given VkImage, but it does manages its subresources.
VulkanTexture(VkDevice device, VmaAllocator allocator,
fvkmemory::ResourceManager* resourceManager, VulkanCommands* commands, VkImage image,
VkFormat format, uint8_t samples, uint32_t width, uint32_t height, TextureUsage tusage,
VulkanStagePool& stagePool);
VkDeviceMemory memory, VkFormat format, uint8_t samples, uint32_t width, uint32_t height,
TextureUsage tusage, VulkanStagePool& stagePool);

// Constructor for creating a texture view for wrt specific mip range
VulkanTexture(VkDevice device, VkPhysicalDevice physicalDevice, VulkanContext const& context,
Expand Down
33 changes: 29 additions & 4 deletions filament/backend/src/vulkan/memory/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ struct Resource {
: resManager(nullptr),
id(HandleBase::nullid),
mCount(0),
restype(ResourceType::UNDEFINED_TYPE) {}
restype(ResourceType::UNDEFINED_TYPE),
mHandleConsideredDestroyed(false) {}

private:
inline void inc() noexcept {
Expand All @@ -80,6 +81,16 @@ struct Resource {
}
}

// To be able to detect use-after-free, we need a bit to signify if the handle should be
// consider destroyed (from Filament's perspective).
inline void setHandleConsiderDestroyed() noexcept {
mHandleConsideredDestroyed = true;
}

inline bool isHandleConsideredDestroyed() const {
return mHandleConsideredDestroyed;
}

template <typename T>
inline void init(HandleId id, ResourceManager* resManager) {
this->id = id;
Expand All @@ -92,7 +103,9 @@ struct Resource {
ResourceManager* resManager; // 8
HandleId id; // 4
uint32_t mCount : 24;
ResourceType restype : 6; // restype + mCount is 4 bytes.
ResourceType restype : 7;
bool mHandleConsideredDestroyed : 1; // restype + mCount + mHandleConsideredDestroyed
// is 4 bytes.

friend class ResourceManager;

Expand All @@ -105,7 +118,8 @@ struct ThreadSafeResource {
: resManager(nullptr),
id(HandleBase::nullid),
mCount(0),
restype(ResourceType::UNDEFINED_TYPE) {}
restype(ResourceType::UNDEFINED_TYPE),
mHandleConsideredDestroyed(false) {}

private:
inline void inc() noexcept {
Expand All @@ -118,6 +132,16 @@ struct ThreadSafeResource {
}
}

// To be able to detect use-after-free, we need a bit to signify if the handle should be
// consider destroyed (from Filament's perspective).
inline void setHandleConsiderDestroyed() noexcept {
mHandleConsideredDestroyed = true;
}

inline bool isHandleConsideredDestroyed() const {
return mHandleConsideredDestroyed;
}

template <typename T>
inline void init(HandleId id, ResourceManager* resManager) {
this->id = id;
Expand All @@ -130,7 +154,8 @@ struct ThreadSafeResource {
ResourceManager* resManager; // 8
HandleId id; // 4
std::atomic<uint32_t> mCount; // 4
ResourceType restype; // 1
ResourceType restype : 7;
bool mHandleConsideredDestroyed : 1; // restype + mHandleConsideredDestroyed is 1 byte

friend class ResourceManager;

Expand Down
7 changes: 7 additions & 0 deletions filament/backend/src/vulkan/memory/ResourcePointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "vulkan/memory/ResourceManager.h"

#include <backend/Handle.h>
#include <utils/compiler.h>

#include <utility>

Expand Down Expand Up @@ -59,6 +60,10 @@ struct resource_ptr {
static enabled_resource_ptr<B> cast(ResourceManager* resManager,
Handle<B> const& handle) noexcept {
D* ptr = resManager->handle_cast<D*, B>(handle);
FILAMENT_CHECK_PRECONDITION(!ptr->isHandleConsideredDestroyed())
<< "Handle id=" << ptr->id << " (" << getTypeStr(ptr->restype)
<< ") is being used after it has been freed";

return {ptr};
}

Expand Down Expand Up @@ -156,6 +161,8 @@ struct resource_ptr {
// only be used from VulkanDriver.
inline void dec() {
assert_invariant(mRef);
assert_invariant(!mRef->isHandleConsideredDestroyed());
mRef->setHandleConsiderDestroyed();
mRef->dec();
}

Expand Down
Loading

0 comments on commit 7201fcb

Please sign in to comment.