From 626eb938ccfd193995e9c61717c9160f3068666c Mon Sep 17 00:00:00 2001 From: Sergey Kosarevsky Date: Tue, 31 Oct 2023 22:52:52 -0700 Subject: [PATCH] igl | Implement IBuffer::getBufferType() Summary: Implemented `IBuffer::getBufferType()`. Reviewed By: mmaurer Differential Revision: D50860844 fbshipit-source-id: 2ee71dba4681ce56c00f475531fa4e0478080301 --- IGLU/sentinel/Buffer.cpp | 5 +++++ IGLU/sentinel/Buffer.h | 1 + src/igl/Buffer.h | 11 +++++++++-- src/igl/metal/Buffer.h | 11 +++++++++-- src/igl/metal/Buffer.mm | 11 +++++++---- src/igl/metal/Device.mm | 8 ++++---- src/igl/opengl/Buffer.cpp | 6 ++++-- src/igl/opengl/Buffer.h | 21 ++++++++++++++++----- src/igl/opengl/Device.cpp | 6 +++--- src/igl/opengl/UniformBuffer.cpp | 6 ++++-- src/igl/opengl/UniformBuffer.h | 4 +++- src/igl/vulkan/Buffer.h | 5 +++-- 12 files changed, 68 insertions(+), 27 deletions(-) diff --git a/IGLU/sentinel/Buffer.cpp b/IGLU/sentinel/Buffer.cpp index 00cc407008..fdaf42b928 100644 --- a/IGLU/sentinel/Buffer.cpp +++ b/IGLU/sentinel/Buffer.cpp @@ -54,4 +54,9 @@ uint64_t Buffer::gpuAddress(size_t /*offset*/) const { return 0; } +igl::BufferDesc::BufferType Buffer::getBufferType() const { + IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_); + return 0; +} + } // namespace iglu::sentinel diff --git a/IGLU/sentinel/Buffer.h b/IGLU/sentinel/Buffer.h index e0c4ed476f..dca951aefc 100644 --- a/IGLU/sentinel/Buffer.h +++ b/IGLU/sentinel/Buffer.h @@ -30,6 +30,7 @@ class Buffer : public igl::IBuffer { [[nodiscard]] igl::ResourceStorage storage() const noexcept final; [[nodiscard]] size_t getSizeInBytes() const final; [[nodiscard]] uint64_t gpuAddress(size_t offset = 0) const final; + [[nodiscard]] igl::BufferDesc::BufferType getBufferType() const final; private: size_t size_; diff --git a/src/igl/Buffer.h b/src/igl/Buffer.h index 7afc88c8a2..b951617109 100644 --- a/src/igl/Buffer.h +++ b/src/igl/Buffer.h @@ -79,10 +79,10 @@ struct BufferDesc { */ BufferAPIHint hint = 0; /** - * @brief GLES only. Target binding point for this IBuffer + * @brief A bit mask of BufferTypeBits. All usage types must be specified. * @See igl::BufferType */ - BufferType type; // GLES only + BufferType type = 0; /** @brief Identifier used for debugging */ std::string debugName; @@ -171,6 +171,13 @@ class IBuffer : public ITrackedResource { */ virtual uint64_t gpuAddress(size_t offset = 0) const = 0; + /** + * @brief Returns the underlying buffer type which is a mask of igl::BufferDesc::BufferTypeBits + * + * @return igl::BufferDesc::BufferType + */ + virtual BufferDesc::BufferType getBufferType() const = 0; + protected: IBuffer() = default; }; diff --git a/src/igl/metal/Buffer.h b/src/igl/metal/Buffer.h index 53a53db160..d444c6e773 100644 --- a/src/igl/metal/Buffer.h +++ b/src/igl/metal/Buffer.h @@ -21,7 +21,8 @@ class Buffer : public igl::IBuffer { Buffer(id value, MTLResourceOptions options, BufferDesc::BufferAPIHint requestedApiHints, - BufferDesc::BufferAPIHint acceptedApiHints); + BufferDesc::BufferAPIHint acceptedApiHints, + BufferDesc::BufferType bufferType); Result upload(const void* data, const BufferRange& range) override; @@ -35,6 +36,10 @@ class Buffer : public igl::IBuffer { size_t getSizeInBytes() const override; [[nodiscard]] uint64_t gpuAddress(size_t offset) const override; + [[nodiscard]] BufferDesc::BufferType getBufferType() const override { + return bufferType_; + } + IGL_INLINE virtual id get() { return mtlBuffers_[0]; } @@ -44,6 +49,7 @@ class Buffer : public igl::IBuffer { std::vector> mtlBuffers_; BufferDesc::BufferAPIHint requestedApiHints_; BufferDesc::BufferAPIHint acceptedApiHints_; + BufferDesc::BufferType bufferType_; }; // Manages a ring of buffers. @@ -54,7 +60,8 @@ class RingBuffer final : public Buffer { RingBuffer(std::vector> ringBuffers, MTLResourceOptions options, std::shared_ptr syncManager, - BufferDesc::BufferAPIHint requestedApiHints); + BufferDesc::BufferAPIHint requestedApiHints, + BufferDesc::BufferType bufferType); Result upload(const void* data, const BufferRange& range) override; diff --git a/src/igl/metal/Buffer.mm b/src/igl/metal/Buffer.mm index 0d38ad925b..ded6d8f148 100644 --- a/src/igl/metal/Buffer.mm +++ b/src/igl/metal/Buffer.mm @@ -96,10 +96,12 @@ Buffer::Buffer(id value, MTLResourceOptions options, BufferDesc::BufferAPIHint requestedApiHints, - BufferDesc::BufferAPIHint acceptedApiHints) : + BufferDesc::BufferAPIHint acceptedApiHints, + BufferDesc::BufferType bufferType) : resourceOptions_(options), requestedApiHints_(requestedApiHints), - acceptedApiHints_(acceptedApiHints) { + acceptedApiHints_(acceptedApiHints), + bufferType_(bufferType) { mtlBuffers_.push_back(value); } @@ -152,8 +154,9 @@ RingBuffer::RingBuffer(std::vector> ringBuffers, MTLResourceOptions options, std::shared_ptr syncManager, - BufferDesc::BufferAPIHint requestedApiHints) : - Buffer(nil, options, requestedApiHints, BufferDesc::BufferAPIHintBits::Ring), + BufferDesc::BufferAPIHint requestedApiHints, + BufferDesc::BufferType bufferType) : + Buffer(nil, options, requestedApiHints, BufferDesc::BufferAPIHintBits::Ring, bufferType), syncManager_(std::move(syncManager)) { mtlBuffers_ = std::move(ringBuffers); } diff --git a/src/igl/metal/Device.mm b/src/igl/metal/Device.mm index 355697985e..19b7dbdf71 100644 --- a/src/igl/metal/Device.mm +++ b/src/igl/metal/Device.mm @@ -75,7 +75,7 @@ id metalObject = createMetalBuffer(device_, desc, options); std::unique_ptr resource = std::make_unique( - std::move(metalObject), options, desc.hint, 0 /* No accepted hints */); + std::move(metalObject), options, desc.hint, 0 /* No accepted hints */, desc.type); if (getResourceTracker()) { resource->initResourceTracker(getResourceTracker()); } @@ -94,8 +94,8 @@ id metalObject = createMetalBuffer(device_, desc, options); bufferRing.push_back(metalObject); } - std::unique_ptr resource = - std::make_unique(std::move(bufferRing), options, bufferSyncManager_, desc.hint); + std::unique_ptr resource = std::make_unique( + std::move(bufferRing), options, bufferSyncManager_, desc.hint, desc.type); if (getResourceTracker()) { resource->initResourceTracker(getResourceTracker()); @@ -117,7 +117,7 @@ deallocator:deallocator]; std::unique_ptr resource = std::make_unique( - metalObject, options, desc.hint, BufferDesc::BufferAPIHintBits::NoCopy); + metalObject, options, desc.hint, BufferDesc::BufferAPIHintBits::NoCopy, desc.type); if (getResourceTracker()) { resource->initResourceTracker(getResourceTracker()); } diff --git a/src/igl/opengl/Buffer.cpp b/src/igl/opengl/Buffer.cpp index 691c096817..6a33347927 100644 --- a/src/igl/opengl/Buffer.cpp +++ b/src/igl/opengl/Buffer.cpp @@ -18,8 +18,10 @@ namespace opengl { // **** ArrayBuffer // ******************************** // the base buffer object -ArrayBuffer::ArrayBuffer(IContext& context, BufferDesc::BufferAPIHint requestedApiHints) : - Buffer(context, requestedApiHints) { +ArrayBuffer::ArrayBuffer(IContext& context, + BufferDesc::BufferAPIHint requestedApiHints, + BufferDesc::BufferType bufferType) : + Buffer(context, requestedApiHints, bufferType) { iD_ = 0; size_ = 0; isDynamic_ = false; diff --git a/src/igl/opengl/Buffer.h b/src/igl/opengl/Buffer.h index de0b97d039..995a38a554 100644 --- a/src/igl/opengl/Buffer.h +++ b/src/igl/opengl/Buffer.h @@ -22,8 +22,10 @@ class Buffer : public WithContext, public IBuffer { public: enum class Type : uint8_t { Attribute, Uniform, UniformBlock }; - Buffer(IContext& context, BufferDesc::BufferAPIHint requestedApiHints) : - WithContext(context), requestedApiHints_(requestedApiHints) {} + Buffer(IContext& context, + BufferDesc::BufferAPIHint requestedApiHints, + BufferDesc::BufferType bufferType) : + WithContext(context), requestedApiHints_(requestedApiHints), bufferType_(bufferType) {} uint64_t gpuAddress(size_t) const override { IGL_ASSERT_NOT_IMPLEMENTED(); return 0; @@ -35,13 +37,20 @@ class Buffer : public WithContext, public IBuffer { return requestedApiHints_; } + [[nodiscard]] BufferDesc::BufferType getBufferType() const override { + return bufferType_; + } + private: BufferDesc::BufferAPIHint requestedApiHints_; + BufferDesc::BufferType bufferType_ = 0; }; class ArrayBuffer : public Buffer { public: - ArrayBuffer(IContext& context, BufferDesc::BufferAPIHint requestedApiHints); + ArrayBuffer(IContext& context, + BufferDesc::BufferAPIHint requestedApiHints, + BufferDesc::BufferType bufferType); ~ArrayBuffer() override; Result upload(const void* data, const BufferRange& range) override; @@ -98,8 +107,10 @@ class ArrayBuffer : public Buffer { class UniformBlockBuffer : public ArrayBuffer { public: - UniformBlockBuffer(IContext& context, BufferDesc::BufferAPIHint requestedApiHints) : - ArrayBuffer(context, requestedApiHints) {} + UniformBlockBuffer(IContext& context, + BufferDesc::BufferAPIHint requestedApiHints, + BufferDesc::BufferType bufferType) : + ArrayBuffer(context, requestedApiHints, bufferType) {} Type getType() const noexcept override { return Type::UniformBlock; diff --git a/src/igl/opengl/Device.cpp b/src/igl/opengl/Device.cpp index 428747653e..04a837f08e 100644 --- a/src/igl/opengl/Device.cpp +++ b/src/igl/opengl/Device.cpp @@ -38,12 +38,12 @@ std::unique_ptr allocateBuffer(BufferDesc::BufferType bufferType, if ((bufferType & BufferDesc::BufferTypeBits::Index) || (bufferType & BufferDesc::BufferTypeBits::Vertex) || (bufferType & BufferDesc::BufferTypeBits::Storage)) { - resource = std::make_unique(context, requestedApiHints); + resource = std::make_unique(context, requestedApiHints, bufferType); } else if (bufferType & BufferDesc::BufferTypeBits::Uniform) { if (requestedApiHints & BufferDesc::BufferAPIHintBits::UniformBlock) { - resource = std::make_unique(context, requestedApiHints); + resource = std::make_unique(context, requestedApiHints, bufferType); } else { - resource = std::make_unique(context, requestedApiHints); + resource = std::make_unique(context, requestedApiHints, bufferType); } } else { diff --git a/src/igl/opengl/UniformBuffer.cpp b/src/igl/opengl/UniformBuffer.cpp index ec3561a134..f7a9f53819 100644 --- a/src/igl/opengl/UniformBuffer.cpp +++ b/src/igl/opengl/UniformBuffer.cpp @@ -27,8 +27,10 @@ enum class UniformBaseType { Invalid = 0, Boolean, Int, Float, FloatMatrix }; // **** Buffer // ******************************** // the base buffer object -UniformBuffer::UniformBuffer(IContext& context, BufferDesc::BufferAPIHint requestedApiHints) : - Buffer(context, requestedApiHints) { +UniformBuffer::UniformBuffer(IContext& context, + BufferDesc::BufferAPIHint requestedApiHints, + BufferDesc::BufferType bufferType) : + Buffer(context, requestedApiHints, bufferType) { isDynamic_ = false; } diff --git a/src/igl/opengl/UniformBuffer.h b/src/igl/opengl/UniformBuffer.h index d39e6afe02..a7ad132f45 100644 --- a/src/igl/opengl/UniformBuffer.h +++ b/src/igl/opengl/UniformBuffer.h @@ -20,7 +20,9 @@ class RenderPipelineState; class UniformBuffer final : public Buffer { public: - UniformBuffer(IContext& context, BufferDesc::BufferAPIHint requestedApiHints); + UniformBuffer(IContext& context, + BufferDesc::BufferAPIHint requestedApiHints, + BufferDesc::BufferType bufferType); ~UniformBuffer() override; Result upload(const void* data, const BufferRange& range) override; diff --git a/src/igl/vulkan/Buffer.h b/src/igl/vulkan/Buffer.h index 9fb05c0efe..bc8ad98cb2 100644 --- a/src/igl/vulkan/Buffer.h +++ b/src/igl/vulkan/Buffer.h @@ -36,11 +36,12 @@ class Buffer final : public igl::IBuffer { size_t getSizeInBytes() const override; uint64_t gpuAddress(size_t offset) const override; - VkBuffer getVkBuffer() const; - BufferDesc::BufferType getBufferType() const { + BufferDesc::BufferType getBufferType() const override { return desc_.type; } + VkBuffer getVkBuffer() const; + private: Result create(const BufferDesc& desc); [[nodiscard]] const std::shared_ptr& currentVulkanBuffer() const;