Add support for memory render targets.

This commit is contained in:
Chinmay Garde
2021-07-05 15:00:22 -07:00
committed by Dan Field
parent ec3d9f6060
commit 98ba1675d3
3 changed files with 64 additions and 5 deletions

View File

@@ -12,9 +12,35 @@
namespace impeller {
//------------------------------------------------------------------------------
/// @brief Specified where the allocation resides and how it is used.
///
enum class StorageMode {
kHostCoherent,
//----------------------------------------------------------------------------
/// Allocations can be mapped onto the hosts address space and also be used by
/// the device.
///
kHostVisible,
//----------------------------------------------------------------------------
/// Allocations can only be used by the device. This location is optimal for
/// use by the device. If the host needs to access these allocations, the
/// transfer queue must be used to transfer this allocation onto the a host
/// visible buffer.
///
kDevicePrivate,
//----------------------------------------------------------------------------
/// Used by the device for temporary render targets. These allocations cannot
/// be transferred from and to other allocations using the transfer queue.
/// Render pass cannot initialize the contents of these buffers using load and
/// store actions.
///
/// These allocations reside in tile memory which has higher bandwidth, lower
/// latency and lower power consumption. The total device memory usage is
/// also lower as a separate allocation does not need to be created in
/// device memory. Prefer using these allocations for intermediates like depth
/// and stencil buffers.
///
kDeviceTransient,
};
class Context;
@@ -31,6 +57,8 @@ class Allocator {
std::shared_ptr<DeviceBuffer> CreateBufferWithCopy(const uint8_t* buffer,
size_t length);
static bool RequiresExplicitHostSynchronization(StorageMode mode);
private:
friend class Context;

View File

@@ -4,6 +4,7 @@
#include "impeller/compositor/allocator.h"
#include "flutter/fml/build_config.h"
#include "flutter/fml/logging.h"
#include "impeller/compositor/buffer.h"
#include "impeller/compositor/device_buffer.h"
@@ -27,17 +28,41 @@ bool Allocator::IsValid() const {
static MTLResourceOptions ResourceOptionsFromStorageType(StorageMode type) {
switch (type) {
case StorageMode::kHostCoherent:
case StorageMode::kHostVisible:
#if OS_IOS
return MTLStorageModeShared;
#else
return MTLResourceStorageModeManaged;
#endif
case StorageMode::kDevicePrivate:
return MTLResourceStorageModePrivate;
case StorageMode::kDeviceTransient:
#if OS_IOS
return MTLStorageModeMemoryless;
#else
return MTLResourceStorageModePrivate;
#endif
}
}
bool Allocator::RequiresExplicitHostSynchronization(StorageMode mode) {
if (mode != StorageMode::kHostVisible) {
return false;
}
#if OS_IOS
// StorageMode::kHostVisible is MTLStorageModeShared already.
return false;
#else
// StorageMode::kHostVisible is MTLResourceStorageModeManaged.
return true;
#endif
}
std::shared_ptr<DeviceBuffer> Allocator::CreateBufferWithCopy(
const uint8_t* buffer,
size_t length) {
auto new_buffer = CreateBuffer(StorageMode::kHostCoherent, length);
auto new_buffer = CreateBuffer(StorageMode::kHostVisible, length);
if (!new_buffer) {
return nullptr;

View File

@@ -49,6 +49,11 @@ std::shared_ptr<Texture> DeviceBuffer::MakeTexture(TextureDescriptor desc,
[[nodiscard]] bool DeviceBuffer::CopyHostBuffer(const uint8_t* source,
Range source_range,
size_t offset) {
if (mode_ != StorageMode::kHostVisible) {
// One of the storage modes where a transfer queue must be used.
return false;
}
if (offset + source_range.length > size_) {
// Out of bounds of this buffer.
return false;
@@ -57,13 +62,14 @@ std::shared_ptr<Texture> DeviceBuffer::MakeTexture(TextureDescriptor desc,
auto dest = static_cast<uint8_t*>(buffer_.contents);
if (!dest) {
// Probably StorageMode::kDevicePrivate.
return false;
}
::memmove(dest + offset, source + source_range.offset, source_range.length);
[buffer_ didModifyRange:NSMakeRange(offset, source_range.length)];
if (Allocator::RequiresExplicitHostSynchronization(mode_)) {
[buffer_ didModifyRange:NSMakeRange(offset, source_range.length)];
}
return true;
}