Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Renaming MemoryPool to MemoryHeap and setting up resource tracking.
Browse files Browse the repository at this point in the history
This will make it easier to implement against mtl/vk/d3d12 and free up the
name for an actual pooling type to be created in future CLs.
  • Loading branch information
benvanik committed Jul 17, 2017
1 parent 4f97cd6 commit fd6adff
Show file tree
Hide file tree
Showing 20 changed files with 317 additions and 199 deletions.
31 changes: 15 additions & 16 deletions xrtl/examples/triangle_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using gfx::Context;
using gfx::ContextFactory;
using gfx::Image;
using gfx::ImageView;
using gfx::MemoryPool;
using gfx::MemoryHeap;
using gfx::RenderPass;
using gfx::RenderPipeline;
using gfx::RenderState;
Expand Down Expand Up @@ -121,12 +121,12 @@ class TriangleExample : private Control::Listener {
return false;
}

// Allocate a memory pool to allocate buffers and textures.
memory_pool_ = context_->CreateMemoryPool(
// Allocate a memory heap to allocate buffers and textures.
memory_heap_ = context_->CreateMemoryHeap(
gfx::MemoryType::kHostVisible | gfx::MemoryType::kHostCoherent,
1 * 1024 * 1024);
if (!memory_pool_) {
LOG(ERROR) << "Unable to create memory pool";
16 * 1024 * 1024);
if (!memory_heap_) {
LOG(ERROR) << "Unable to create memory heap";
return false;
}

Expand All @@ -148,10 +148,10 @@ class TriangleExample : private Control::Listener {
};

// Allocate a buffer for the geometry.
auto allocation_result = memory_pool_->AllocateBuffer(
auto allocation_result = memory_heap_->AllocateBuffer(
sizeof(kVertexData), Buffer::Usage::kVertexBuffer, &triangle_buffer_);
switch (allocation_result) {
case MemoryPool::AllocationResult::kSuccess:
case MemoryHeap::AllocationResult::kSuccess:
break;
default:
LOG(ERROR) << "Failed to allocate geometry buffer";
Expand Down Expand Up @@ -187,13 +187,12 @@ class TriangleExample : private Control::Listener {
create_params.format = gfx::PixelFormats::kR8G8B8A8UNorm;
create_params.tiling_mode = Image::TilingMode::kLinear;
create_params.size = {kWidth, kHeight};
create_params.usage_mask = Image::Usage::kSampled;
create_params.initial_layout = Image::Layout::kPreinitialized;

auto allocation_result =
memory_pool_->AllocateImage(create_params, &grid_image_);
auto allocation_result = memory_heap_->AllocateImage(
create_params, Image::Usage::kSampled, &grid_image_);
switch (allocation_result) {
case MemoryPool::AllocationResult::kSuccess:
case MemoryHeap::AllocationResult::kSuccess:
break;
default:
LOG(ERROR) << "Failed to allocate texture image";
Expand Down Expand Up @@ -359,10 +358,10 @@ void main() {
}

// Allocate the uniform buffer.
auto allocation_result = memory_pool_->AllocateBuffer(
auto allocation_result = memory_heap_->AllocateBuffer(
sizeof(UniformBlock), Buffer::Usage::kUniformBuffer, &uniform_buffer_);
switch (allocation_result) {
case MemoryPool::AllocationResult::kSuccess:
case MemoryHeap::AllocationResult::kSuccess:
break;
default:
LOG(ERROR) << "Failed to allocate uniform buffer";
Expand Down Expand Up @@ -527,7 +526,7 @@ void main() {
resource_set_.reset();
render_pipeline_.reset();
render_pass_.reset();
memory_pool_.reset();
memory_heap_.reset();
swap_chain_.reset();
context_.reset();
}
Expand Down Expand Up @@ -565,7 +564,7 @@ void main() {
ref_ptr<RenderPipeline> render_pipeline_;
ref_ptr<ResourceSet> resource_set_;

ref_ptr<MemoryPool> memory_pool_;
ref_ptr<MemoryHeap> memory_heap_;
ref_ptr<Buffer> triangle_buffer_;
ref_ptr<Image> grid_image_;
ref_ptr<ImageView> grid_image_view_;
Expand Down
6 changes: 3 additions & 3 deletions xrtl/gfx/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ cc_library(
":device",
":framebuffer",
":image_view",
":memory_pool",
":memory_heap",
":pipeline",
":pipeline_layout",
":pixel_format",
Expand Down Expand Up @@ -199,8 +199,8 @@ cc_library(
)

cc_library(
name = "memory_pool",
hdrs = ["memory_pool.h"],
name = "memory_heap",
hdrs = ["memory_heap.h"],
deps = [
":buffer",
":image",
Expand Down
16 changes: 8 additions & 8 deletions xrtl/gfx/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "xrtl/gfx/device.h"
#include "xrtl/gfx/framebuffer.h"
#include "xrtl/gfx/image_view.h"
#include "xrtl/gfx/memory_pool.h"
#include "xrtl/gfx/memory_heap.h"
#include "xrtl/gfx/pipeline.h"
#include "xrtl/gfx/pipeline_layout.h"
#include "xrtl/gfx/pixel_format.h"
Expand Down Expand Up @@ -146,20 +146,20 @@ class Context : public RefObject<Context> {
ref_ptr<ui::Control> control, SwapChain::PresentMode present_mode,
int image_count, ArrayView<PixelFormat> pixel_formats) = 0;

// Creates a new resource memory pool.
// The pool can be used to create images and buffers of the given memory
// Creates a new resource memory heap.
// The heap can be used to create images and buffers of the given memory
// type.
//
// The memory pool will request hardware resources in the provided chunk size
// and then dole out images and buffers from those chunks. Chunk sizes
// The memory heap will request hardware resources in the provided heap size
// and then dole out images and buffers from that allocation. Heap sizes
// should be sufficiently large to prevent frequent exhaustion but not so
// large as to potentially run out of device memory. 64-128MB is often a good
// size to start with. The provided chunk size may be rounded up to alignment
// size to start with. The provided heap size may be rounded up to alignment
// restrictions of the device.
//
// Returns nullptr if the memory type mask is invalid.
virtual ref_ptr<MemoryPool> CreateMemoryPool(MemoryType memory_type_mask,
size_t chunk_size) = 0;
virtual ref_ptr<MemoryHeap> CreateMemoryHeap(MemoryType memory_type_mask,
size_t heap_size) = 0;

// Creates a new image sampler.
virtual ref_ptr<Sampler> CreateSampler(Sampler::Params params) = 0;
Expand Down
15 changes: 9 additions & 6 deletions xrtl/gfx/es3/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cc_library(
":es3_common",
":es3_platform_context",
"//xrtl/gfx:buffer",
"//xrtl/gfx:memory_heap",
],
)

Expand Down Expand Up @@ -70,7 +71,7 @@ cc_library(
":es3_common",
":es3_framebuffer",
":es3_image_view",
":es3_memory_pool",
":es3_memory_heap",
":es3_pipeline",
":es3_pipeline_layout",
":es3_platform_context",
Expand Down Expand Up @@ -149,6 +150,7 @@ cc_library(
":es3_pixel_format",
":es3_platform_context",
"//xrtl/gfx:image",
"//xrtl/gfx:memory_heap",
],
)

Expand All @@ -162,17 +164,18 @@ cc_library(
)

cc_library(
name = "es3_memory_pool",
srcs = ["es3_memory_pool.cc"],
hdrs = ["es3_memory_pool.h"],
name = "es3_memory_heap",
srcs = ["es3_memory_heap.cc"],
hdrs = ["es3_memory_heap.h"],
deps = [
":es3_buffer",
":es3_common",
":es3_image",
":es3_pixel_format",
":es3_platform_context",
"//xrtl/base:math",
"//xrtl/base:tracing",
"//xrtl/gfx:memory_pool",
"//xrtl/gfx:memory_heap",
],
)

Expand Down Expand Up @@ -379,7 +382,7 @@ cc_library(
"//xrtl/base/threading:event",
"//xrtl/base/threading:semaphore",
"//xrtl/base/threading:thread",
"//xrtl/gfx:memory_pool",
"//xrtl/gfx:memory_heap",
"//xrtl/gfx:swap_chain",
"//xrtl/ui:control",
],
Expand Down
17 changes: 12 additions & 5 deletions xrtl/gfx/es3/es3_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

#include <utility>

#include "xrtl/gfx/memory_heap.h"

namespace xrtl {
namespace gfx {
namespace es3 {

ES3Buffer::ES3Buffer(ref_ptr<ES3PlatformContext> platform_context,
MemoryType memory_type_mask, size_t allocation_size,
ref_ptr<MemoryHeap> memory_heap, size_t allocation_size,
Usage usage_mask)
: Buffer(allocation_size, usage_mask),
platform_context_(std::move(platform_context)),
memory_type_mask_(memory_type_mask) {
memory_heap_(std::move(memory_heap)) {
auto context_lock =
ES3PlatformContext::LockTransientContext(platform_context_);

Expand Down Expand Up @@ -65,6 +67,10 @@ ES3Buffer::~ES3Buffer() {
glDeleteBuffers(1, &buffer_id_);
}

ref_ptr<MemoryHeap> ES3Buffer::memory_heap() const { return memory_heap_; }

void ES3Buffer::Release() { memory_heap_->ReleaseBuffer(this); }

bool ES3Buffer::ReadData(size_t source_offset, void* data, size_t data_length) {
DCHECK_LE(source_offset + data_length, allocation_size());
// TODO(benvanik): buffer.
Expand All @@ -91,7 +97,8 @@ bool ES3Buffer::MapMemory(MemoryAccess memory_access, size_t* byte_offset,
*out_data = nullptr;

// Must be mappable.
bool is_mappable = any(memory_type_mask_ & MemoryType::kHostVisible);
bool is_mappable =
any(memory_heap_->memory_type_mask() & MemoryType::kHostVisible);
DCHECK(is_mappable);
if (!is_mappable) {
LOG(ERROR) << "Attempting to map a non-host-visible memory buffer";
Expand Down Expand Up @@ -123,7 +130,7 @@ bool ES3Buffer::MapMemory(MemoryAccess memory_access, size_t* byte_offset,

if (access & GL_MAP_WRITE_BIT) {
// Non-host-coherent memory requires explicit flushes.
if (!any(memory_type_mask_ & MemoryType::kHostCoherent)) {
if (!any(memory_heap_->memory_type_mask() & MemoryType::kHostCoherent)) {
access |= GL_MAP_UNSYNCHRONIZED_BIT;
access |= GL_MAP_FLUSH_EXPLICIT_BIT;
}
Expand Down Expand Up @@ -164,7 +171,7 @@ void ES3Buffer::InvalidateMappedMemory(size_t byte_offset, size_t byte_length) {

void ES3Buffer::FlushMappedMemory(size_t byte_offset, size_t byte_length) {
// Flushes are ignored with kHostCoherent memory.
if (any(memory_type_mask_ & MemoryType::kHostCoherent)) {
if (any(memory_heap_->memory_type_mask() & MemoryType::kHostCoherent)) {
return;
}

Expand Down
10 changes: 8 additions & 2 deletions xrtl/gfx/es3/es3_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef XRTL_GFX_ES3_ES3_BUFFER_H_
#define XRTL_GFX_ES3_ES3_BUFFER_H_

#include <functional>

#include "xrtl/gfx/buffer.h"
#include "xrtl/gfx/es3/es3_common.h"
#include "xrtl/gfx/es3/es3_platform_context.h"
Expand All @@ -26,10 +28,12 @@ namespace es3 {
class ES3Buffer : public Buffer {
public:
ES3Buffer(ref_ptr<ES3PlatformContext> platform_context,
MemoryType memory_type_mask, size_t allocation_size,
ref_ptr<MemoryHeap> memory_heap, size_t allocation_size,
Usage usage_mask);
~ES3Buffer() override;

ref_ptr<MemoryHeap> memory_heap() const override;

GLenum target() const { return target_; }
GLuint buffer_id() const { return buffer_id_; }

Expand All @@ -42,13 +46,15 @@ class ES3Buffer : public Buffer {

void FlushMappedMemory(size_t byte_offset, size_t byte_length) override;

void Release() override;

private:
bool MapMemory(MemoryAccess memory_access, size_t* byte_offset,
size_t* byte_length, void** out_data) override;
void UnmapMemory(size_t byte_offset, size_t byte_length, void* data) override;

ref_ptr<ES3PlatformContext> platform_context_;
MemoryType memory_type_mask_ = MemoryType::kDeviceLocal;
ref_ptr<MemoryHeap> memory_heap_;

GLenum target_ = GL_COPY_READ_BUFFER;
GLuint buffer_id_ = 0;
Expand Down
17 changes: 9 additions & 8 deletions xrtl/gfx/es3/es3_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "xrtl/gfx/es3/es3_command_fence.h"
#include "xrtl/gfx/es3/es3_framebuffer.h"
#include "xrtl/gfx/es3/es3_image_view.h"
#include "xrtl/gfx/es3/es3_memory_pool.h"
#include "xrtl/gfx/es3/es3_memory_heap.h"
#include "xrtl/gfx/es3/es3_pipeline.h"
#include "xrtl/gfx/es3/es3_pipeline_layout.h"
#include "xrtl/gfx/es3/es3_program.h"
Expand Down Expand Up @@ -215,18 +215,19 @@ ref_ptr<SwapChain> ES3Context::CreateSwapChain(
int image_count, ArrayView<PixelFormat> pixel_formats) {
// Shared memory pool for all frame buffer images.
// TODO(benvanik): pool across swap chains.
auto memory_pool = CreateMemoryPool(MemoryType::kDeviceLocal, 0);
DCHECK(memory_pool);
auto memory_heap =
CreateMemoryHeap(MemoryType::kDeviceLocal, 64 * 1024 * 1024);
DCHECK(memory_heap);

return ES3SwapChain::Create(platform_context_, presentation_queue_.get(),
std::move(memory_pool), std::move(control),
std::move(memory_heap), std::move(control),
present_mode, image_count, pixel_formats);
}

ref_ptr<MemoryPool> ES3Context::CreateMemoryPool(MemoryType memory_type_mask,
size_t chunk_size) {
return make_ref<ES3MemoryPool>(platform_context_, memory_type_mask,
chunk_size);
ref_ptr<MemoryHeap> ES3Context::CreateMemoryHeap(MemoryType memory_type_mask,
size_t heap_size) {
return make_ref<ES3MemoryHeap>(platform_context_, memory_type_mask,
heap_size);
}

ref_ptr<Sampler> ES3Context::CreateSampler(Sampler::Params params) {
Expand Down
4 changes: 2 additions & 2 deletions xrtl/gfx/es3/es3_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class ES3Context : public Context {
ref_ptr<ui::Control> control, SwapChain::PresentMode present_mode,
int image_count, ArrayView<PixelFormat> pixel_formats) override;

ref_ptr<MemoryPool> CreateMemoryPool(MemoryType memory_type_mask,
size_t chunk_size) override;
ref_ptr<MemoryHeap> CreateMemoryHeap(MemoryType memory_type_mask,
size_t heap_size) override;

ref_ptr<Sampler> CreateSampler(Sampler::Params params) override;

Expand Down
7 changes: 7 additions & 0 deletions xrtl/gfx/es3/es3_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <utility>

#include "xrtl/gfx/es3/es3_image_view.h"
#include "xrtl/gfx/memory_heap.h"

namespace xrtl {
namespace gfx {
Expand Down Expand Up @@ -47,10 +48,12 @@ size_t ES3Image::ComputeAllocationSize(
}

ES3Image::ES3Image(ref_ptr<ES3PlatformContext> platform_context,
ref_ptr<MemoryHeap> memory_heap,
ES3TextureParams texture_params, size_t allocation_size,
CreateParams create_params)
: Image(allocation_size, create_params),
platform_context_(std::move(platform_context)),
memory_heap_(std::move(memory_heap)),
texture_params_(texture_params) {
auto context_lock =
ES3PlatformContext::LockTransientContext(platform_context_);
Expand Down Expand Up @@ -104,6 +107,10 @@ ES3Image::~ES3Image() {
glDeleteTextures(1, &texture_id_);
}

ref_ptr<MemoryHeap> ES3Image::memory_heap() const { return memory_heap_; }

void ES3Image::Release() { memory_heap_->ReleaseImage(this); }

ref_ptr<ImageView> ES3Image::CreateView() {
return CreateView(create_params_.type, create_params_.format, entire_range());
}
Expand Down
Loading

0 comments on commit fd6adff

Please sign in to comment.