From c1400f0d144b2b5e7c4476f0f141b472fa26a553 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Fri, 24 Nov 2017 13:59:42 -0500 Subject: [PATCH] Format: src/backend --- src/backend/BindGroup.cpp | 33 +- src/backend/BindGroup.h | 92 +-- src/backend/BindGroupLayout.cpp | 21 +- src/backend/BindGroupLayout.h | 57 +- src/backend/BlendState.cpp | 14 +- src/backend/BlendState.h | 65 +- src/backend/Buffer.cpp | 28 +- src/backend/Buffer.h | 148 ++-- src/backend/Builder.cpp | 17 +- src/backend/Builder.h | 101 ++- src/backend/CommandAllocator.cpp | 57 +- src/backend/CommandAllocator.h | 134 ++-- src/backend/CommandBuffer.cpp | 796 +++++++++++----------- src/backend/CommandBuffer.h | 150 ++-- src/backend/CommandBufferStateTracker.cpp | 128 ++-- src/backend/CommandBufferStateTracker.h | 148 ++-- src/backend/Commands.h | 19 +- src/backend/ComputePipeline.cpp | 2 +- src/backend/ComputePipeline.h | 17 +- src/backend/DepthStencilState.cpp | 28 +- src/backend/DepthStencilState.h | 86 +-- src/backend/Device.cpp | 12 +- src/backend/Device.h | 150 ++-- src/backend/Forward.h | 8 +- src/backend/Framebuffer.cpp | 31 +- src/backend/Framebuffer.h | 92 +-- src/backend/InputState.cpp | 13 +- src/backend/InputState.h | 75 +- src/backend/PerStage.cpp | 2 +- src/backend/PerStage.h | 63 +- src/backend/Pipeline.cpp | 16 +- src/backend/Pipeline.h | 66 +- src/backend/PipelineLayout.cpp | 14 +- src/backend/PipelineLayout.h | 49 +- src/backend/Queue.cpp | 4 +- src/backend/Queue.h | 45 +- src/backend/RefCounted.cpp | 10 +- src/backend/RefCounted.h | 163 ++--- src/backend/RenderPass.cpp | 32 +- src/backend/RenderPass.h | 102 +-- src/backend/RenderPipeline.cpp | 24 +- src/backend/RenderPipeline.h | 81 +-- src/backend/Sampler.cpp | 6 +- src/backend/Sampler.h | 40 +- src/backend/ShaderModule.cpp | 53 +- src/backend/ShaderModule.h | 85 +-- src/backend/SwapChain.cpp | 23 +- src/backend/SwapChain.h | 65 +- src/backend/Texture.cpp | 27 +- src/backend/Texture.h | 127 ++-- src/backend/ToBackend.h | 66 +- 51 files changed, 1925 insertions(+), 1760 deletions(-) diff --git a/src/backend/BindGroup.cpp b/src/backend/BindGroup.cpp index fe4e07d564..a92f1a75b7 100644 --- a/src/backend/BindGroup.cpp +++ b/src/backend/BindGroup.cpp @@ -26,7 +26,9 @@ namespace backend { // BindGroup BindGroupBase::BindGroupBase(BindGroupBuilder* builder) - : mLayout(std::move(builder->mLayout)), mUsage(builder->mUsage), mBindings(std::move(builder->mBindings)) { + : mLayout(std::move(builder->mLayout)), + mUsage(builder->mUsage), + mBindings(std::move(builder->mBindings)) { } const BindGroupLayoutBase* BindGroupBase::GetLayout() const { @@ -41,7 +43,7 @@ namespace backend { ASSERT(binding < kMaxBindingsPerGroup); ASSERT(mLayout->GetBindingInfo().mask[binding]); ASSERT(mLayout->GetBindingInfo().types[binding] == nxt::BindingType::UniformBuffer || - mLayout->GetBindingInfo().types[binding] == nxt::BindingType::StorageBuffer); + mLayout->GetBindingInfo().types[binding] == nxt::BindingType::StorageBuffer); return reinterpret_cast(mBindings[binding].Get()); } @@ -104,7 +106,9 @@ namespace backend { mPropertiesSet |= BINDGROUP_PROPERTY_USAGE; } - void BindGroupBuilder::SetBufferViews(uint32_t start, uint32_t count, BufferViewBase* const * bufferViews) { + void BindGroupBuilder::SetBufferViews(uint32_t start, + uint32_t count, + BufferViewBase* const* bufferViews) { if (!SetBindingsValidationBase(start, count)) { return; } @@ -138,10 +142,12 @@ namespace backend { } } - SetBindingsBase(start, count, reinterpret_cast(bufferViews)); + SetBindingsBase(start, count, reinterpret_cast(bufferViews)); } - void BindGroupBuilder::SetSamplers(uint32_t start, uint32_t count, SamplerBase* const * samplers) { + void BindGroupBuilder::SetSamplers(uint32_t start, + uint32_t count, + SamplerBase* const* samplers) { if (!SetBindingsValidationBase(start, count)) { return; } @@ -154,10 +160,12 @@ namespace backend { } } - SetBindingsBase(start, count, reinterpret_cast(samplers)); + SetBindingsBase(start, count, reinterpret_cast(samplers)); } - void BindGroupBuilder::SetTextureViews(uint32_t start, uint32_t count, TextureViewBase* const * textureViews) { + void BindGroupBuilder::SetTextureViews(uint32_t start, + uint32_t count, + TextureViewBase* const* textureViews) { if (!SetBindingsValidationBase(start, count)) { return; } @@ -169,16 +177,19 @@ namespace backend { return; } - if (!(textureViews[j]->GetTexture()->GetAllowedUsage() & nxt::TextureUsageBit::Sampled)) { + if (!(textureViews[j]->GetTexture()->GetAllowedUsage() & + nxt::TextureUsageBit::Sampled)) { HandleError("Texture needs to allow the sampled usage bit"); return; } } - SetBindingsBase(start, count, reinterpret_cast(textureViews)); + SetBindingsBase(start, count, reinterpret_cast(textureViews)); } - void BindGroupBuilder::SetBindingsBase(uint32_t start, uint32_t count, RefCounted* const * objects) { + void BindGroupBuilder::SetBindingsBase(uint32_t start, + uint32_t count, + RefCounted* const* objects) { for (size_t i = start, j = 0; i < start + count; ++i, ++j) { mSetMask.set(i); mBindings[i] = objects[j]; @@ -211,4 +222,4 @@ namespace backend { return true; } -} +} // namespace backend diff --git a/src/backend/BindGroup.h b/src/backend/BindGroup.h index fceb81a8e3..26e4f1096d 100644 --- a/src/backend/BindGroup.h +++ b/src/backend/BindGroup.h @@ -29,65 +29,65 @@ namespace backend { class BindGroupBase : public RefCounted { - public: - BindGroupBase(BindGroupBuilder* builder); + public: + BindGroupBase(BindGroupBuilder* builder); - const BindGroupLayoutBase* GetLayout() const; - nxt::BindGroupUsage GetUsage() const; - BufferViewBase* GetBindingAsBufferView(size_t binding); - SamplerBase* GetBindingAsSampler(size_t binding); - TextureViewBase* GetBindingAsTextureView(size_t binding); + const BindGroupLayoutBase* GetLayout() const; + nxt::BindGroupUsage GetUsage() const; + BufferViewBase* GetBindingAsBufferView(size_t binding); + SamplerBase* GetBindingAsSampler(size_t binding); + TextureViewBase* GetBindingAsTextureView(size_t binding); - private: - Ref mLayout; - nxt::BindGroupUsage mUsage; - std::array, kMaxBindingsPerGroup> mBindings; + private: + Ref mLayout; + nxt::BindGroupUsage mUsage; + std::array, kMaxBindingsPerGroup> mBindings; }; class BindGroupBuilder : public Builder { - public: - BindGroupBuilder(DeviceBase* device); + public: + BindGroupBuilder(DeviceBase* device); - // NXT API - void SetLayout(BindGroupLayoutBase* layout); - void SetUsage(nxt::BindGroupUsage usage); + // NXT API + void SetLayout(BindGroupLayoutBase* layout); + void SetUsage(nxt::BindGroupUsage usage); - template - void SetBufferViews(uint32_t start, uint32_t count, T* const* bufferViews) { - static_assert(std::is_base_of::value, ""); - SetBufferViews(start, count, reinterpret_cast(bufferViews)); - } - void SetBufferViews(uint32_t start, uint32_t count, BufferViewBase* const * bufferViews); + template + void SetBufferViews(uint32_t start, uint32_t count, T* const* bufferViews) { + static_assert(std::is_base_of::value, ""); + SetBufferViews(start, count, reinterpret_cast(bufferViews)); + } + void SetBufferViews(uint32_t start, uint32_t count, BufferViewBase* const* bufferViews); - template - void SetSamplers(uint32_t start, uint32_t count, T* const* samplers) { - static_assert(std::is_base_of::value, ""); - SetSamplers(start, count, reinterpret_cast(samplers)); - } - void SetSamplers(uint32_t start, uint32_t count, SamplerBase* const * samplers); + template + void SetSamplers(uint32_t start, uint32_t count, T* const* samplers) { + static_assert(std::is_base_of::value, ""); + SetSamplers(start, count, reinterpret_cast(samplers)); + } + void SetSamplers(uint32_t start, uint32_t count, SamplerBase* const* samplers); - template - void SetTextureViews(uint32_t start, uint32_t count, T* const* textureViews) { - static_assert(std::is_base_of::value, ""); - SetTextureViews(start, count, reinterpret_cast(textureViews)); - } - void SetTextureViews(uint32_t start, uint32_t count, TextureViewBase* const * textureViews); + template + void SetTextureViews(uint32_t start, uint32_t count, T* const* textureViews) { + static_assert(std::is_base_of::value, ""); + SetTextureViews(start, count, reinterpret_cast(textureViews)); + } + void SetTextureViews(uint32_t start, uint32_t count, TextureViewBase* const* textureViews); - private: - friend class BindGroupBase; + private: + friend class BindGroupBase; - BindGroupBase* GetResultImpl() override; - void SetBindingsBase(uint32_t start, uint32_t count, RefCounted* const * objects); - bool SetBindingsValidationBase(uint32_t start, uint32_t count); + BindGroupBase* GetResultImpl() override; + void SetBindingsBase(uint32_t start, uint32_t count, RefCounted* const* objects); + bool SetBindingsValidationBase(uint32_t start, uint32_t count); - std::bitset mSetMask; - int mPropertiesSet = 0; + std::bitset mSetMask; + int mPropertiesSet = 0; - Ref mLayout; - nxt::BindGroupUsage mUsage; - std::array, kMaxBindingsPerGroup> mBindings; + Ref mLayout; + nxt::BindGroupUsage mUsage; + std::array, kMaxBindingsPerGroup> mBindings; }; -} +} // namespace backend -#endif // BACKEND_BINDGROUP_H_ +#endif // BACKEND_BINDGROUP_H_ diff --git a/src/backend/BindGroupLayout.cpp b/src/backend/BindGroupLayout.cpp index 6a56bdc8c4..fcf173faf7 100644 --- a/src/backend/BindGroupLayout.cpp +++ b/src/backend/BindGroupLayout.cpp @@ -23,13 +23,13 @@ namespace backend { namespace { // Workaround for Chrome's stdlib having a broken std::hash for enums and bitsets - template + template typename std::enable_if::value, size_t>::type Hash(T value) { using Integral = typename nxt::UnderlyingType::type; return std::hash()(static_cast(value)); } - template + template size_t Hash(const std::bitset& value) { static_assert(N <= sizeof(unsigned long long) * 8, ""); return std::hash()(value.to_ullong()); @@ -54,7 +54,8 @@ namespace backend { return hash; } - bool operator== (const BindGroupLayoutBase::LayoutBindingInfo& a, const BindGroupLayoutBase::LayoutBindingInfo& b) { + bool operator==(const BindGroupLayoutBase::LayoutBindingInfo& a, + const BindGroupLayoutBase::LayoutBindingInfo& b) { if (a.mask != b.mask) { return false; } @@ -72,7 +73,7 @@ namespace backend { return true; } - } + } // namespace // BindGroupLayoutBase @@ -108,7 +109,10 @@ namespace backend { return result; } - void BindGroupLayoutBuilder::SetBindingsType(nxt::ShaderStageBit visibility, nxt::BindingType bindingType, uint32_t start, uint32_t count) { + void BindGroupLayoutBuilder::SetBindingsType(nxt::ShaderStageBit visibility, + nxt::BindingType bindingType, + uint32_t start, + uint32_t count) { if (start + count > kMaxBindingsPerGroup) { HandleError("Setting bindings type over maximum number of bindings"); return; @@ -129,12 +133,13 @@ namespace backend { // BindGroupLayoutCacheFuncs - size_t BindGroupLayoutCacheFuncs::operator() (const BindGroupLayoutBase* bgl) const { + size_t BindGroupLayoutCacheFuncs::operator()(const BindGroupLayoutBase* bgl) const { return HashBindingInfo(bgl->GetBindingInfo()); } - bool BindGroupLayoutCacheFuncs::operator() (const BindGroupLayoutBase* a, const BindGroupLayoutBase* b) const { + bool BindGroupLayoutCacheFuncs::operator()(const BindGroupLayoutBase* a, + const BindGroupLayoutBase* b) const { return a->GetBindingInfo() == b->GetBindingInfo(); } -} +} // namespace backend diff --git a/src/backend/BindGroupLayout.h b/src/backend/BindGroupLayout.h index 204be631ae..1b6c590b02 100644 --- a/src/backend/BindGroupLayout.h +++ b/src/backend/BindGroupLayout.h @@ -15,8 +15,8 @@ #ifndef BACKEND_BINDGROUPLAYOUT_H_ #define BACKEND_BINDGROUPLAYOUT_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "common/Constants.h" @@ -28,49 +28,52 @@ namespace backend { class BindGroupLayoutBase : public RefCounted { - public: - BindGroupLayoutBase(BindGroupLayoutBuilder* builder, bool blueprint = false); - ~BindGroupLayoutBase() override; + public: + BindGroupLayoutBase(BindGroupLayoutBuilder* builder, bool blueprint = false); + ~BindGroupLayoutBase() override; - struct LayoutBindingInfo { - std::array visibilities; - std::array types; - std::bitset mask; - }; - const LayoutBindingInfo& GetBindingInfo() const; + struct LayoutBindingInfo { + std::array visibilities; + std::array types; + std::bitset mask; + }; + const LayoutBindingInfo& GetBindingInfo() const; - private: - DeviceBase* mDevice; - LayoutBindingInfo mBindingInfo; - bool mIsBlueprint = false; + private: + DeviceBase* mDevice; + LayoutBindingInfo mBindingInfo; + bool mIsBlueprint = false; }; class BindGroupLayoutBuilder : public Builder { - public: - BindGroupLayoutBuilder(DeviceBase* device); + public: + BindGroupLayoutBuilder(DeviceBase* device); - const BindGroupLayoutBase::LayoutBindingInfo& GetBindingInfo() const; + const BindGroupLayoutBase::LayoutBindingInfo& GetBindingInfo() const; - // NXT API - void SetBindingsType(nxt::ShaderStageBit visibility, nxt::BindingType bindingType, uint32_t start, uint32_t count); + // NXT API + void SetBindingsType(nxt::ShaderStageBit visibility, + nxt::BindingType bindingType, + uint32_t start, + uint32_t count); - private: - friend class BindGroupLayoutBase; + private: + friend class BindGroupLayoutBase; - BindGroupLayoutBase* GetResultImpl() override; + BindGroupLayoutBase* GetResultImpl() override; - BindGroupLayoutBase::LayoutBindingInfo mBindingInfo; + BindGroupLayoutBase::LayoutBindingInfo mBindingInfo; }; // Implements the functors necessary for the unordered_set-based cache. struct BindGroupLayoutCacheFuncs { // The hash function - size_t operator() (const BindGroupLayoutBase* bgl) const; + size_t operator()(const BindGroupLayoutBase* bgl) const; // The equality predicate - bool operator() (const BindGroupLayoutBase* a, const BindGroupLayoutBase* b) const; + bool operator()(const BindGroupLayoutBase* a, const BindGroupLayoutBase* b) const; }; -} +} // namespace backend -#endif // BACKEND_BINDGROUPLAYOUT_H_ +#endif // BACKEND_BINDGROUPLAYOUT_H_ diff --git a/src/backend/BlendState.cpp b/src/backend/BlendState.cpp index 00831c9255..0e44c1fbee 100644 --- a/src/backend/BlendState.cpp +++ b/src/backend/BlendState.cpp @@ -54,7 +54,9 @@ namespace backend { mBlendInfo.blendEnabled = blendEnabled; } - void BlendStateBuilder::SetAlphaBlend(nxt::BlendOperation blendOperation, nxt::BlendFactor srcFactor, nxt::BlendFactor dstFactor) { + void BlendStateBuilder::SetAlphaBlend(nxt::BlendOperation blendOperation, + nxt::BlendFactor srcFactor, + nxt::BlendFactor dstFactor) { if ((mPropertiesSet & BLEND_STATE_PROPERTY_ALPHA_BLEND) != 0) { HandleError("Alpha blend property set multiple times"); return; @@ -62,10 +64,12 @@ namespace backend { mPropertiesSet |= BLEND_STATE_PROPERTY_ALPHA_BLEND; - mBlendInfo.alphaBlend = { blendOperation, srcFactor, dstFactor }; + mBlendInfo.alphaBlend = {blendOperation, srcFactor, dstFactor}; } - void BlendStateBuilder::SetColorBlend(nxt::BlendOperation blendOperation, nxt::BlendFactor srcFactor, nxt::BlendFactor dstFactor) { + void BlendStateBuilder::SetColorBlend(nxt::BlendOperation blendOperation, + nxt::BlendFactor srcFactor, + nxt::BlendFactor dstFactor) { if ((mPropertiesSet & BLEND_STATE_PROPERTY_COLOR_BLEND) != 0) { HandleError("Color blend property set multiple times"); return; @@ -73,7 +77,7 @@ namespace backend { mPropertiesSet |= BLEND_STATE_PROPERTY_COLOR_BLEND; - mBlendInfo.colorBlend = { blendOperation, srcFactor, dstFactor }; + mBlendInfo.colorBlend = {blendOperation, srcFactor, dstFactor}; } void BlendStateBuilder::SetColorWriteMask(nxt::ColorWriteMask colorWriteMask) { @@ -86,4 +90,4 @@ namespace backend { mBlendInfo.colorWriteMask = colorWriteMask; } -} +} // namespace backend diff --git a/src/backend/BlendState.h b/src/backend/BlendState.h index 9851c6f9ef..42da02e41f 100644 --- a/src/backend/BlendState.h +++ b/src/backend/BlendState.h @@ -15,8 +15,8 @@ #ifndef BACKEND_BLENDSTATE_H_ #define BACKEND_BLENDSTATE_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "nxt/nxtcpp.h" @@ -24,49 +24,52 @@ namespace backend { class BlendStateBase : public RefCounted { - public: - BlendStateBase(BlendStateBuilder* builder); + public: + BlendStateBase(BlendStateBuilder* builder); - struct BlendInfo { - struct BlendOpFactor { - nxt::BlendOperation operation = nxt::BlendOperation::Add; - nxt::BlendFactor srcFactor = nxt::BlendFactor::One; - nxt::BlendFactor dstFactor = nxt::BlendFactor::Zero; - }; - - bool blendEnabled = false; - BlendOpFactor alphaBlend; - BlendOpFactor colorBlend; - nxt::ColorWriteMask colorWriteMask = nxt::ColorWriteMask::All; + struct BlendInfo { + struct BlendOpFactor { + nxt::BlendOperation operation = nxt::BlendOperation::Add; + nxt::BlendFactor srcFactor = nxt::BlendFactor::One; + nxt::BlendFactor dstFactor = nxt::BlendFactor::Zero; }; - const BlendInfo& GetBlendInfo() const; + bool blendEnabled = false; + BlendOpFactor alphaBlend; + BlendOpFactor colorBlend; + nxt::ColorWriteMask colorWriteMask = nxt::ColorWriteMask::All; + }; - private: - BlendInfo mBlendInfo; + const BlendInfo& GetBlendInfo() const; + private: + BlendInfo mBlendInfo; }; class BlendStateBuilder : public Builder { - public: - BlendStateBuilder(DeviceBase* device); + public: + BlendStateBuilder(DeviceBase* device); - // NXT API - void SetBlendEnabled(bool blendEnabled); - void SetAlphaBlend(nxt::BlendOperation blendOperation, nxt::BlendFactor srcFactor, nxt::BlendFactor dstFactor); - void SetColorBlend(nxt::BlendOperation blendOperation, nxt::BlendFactor srcFactor, nxt::BlendFactor dstFactor); - void SetColorWriteMask(nxt::ColorWriteMask colorWriteMask); + // NXT API + void SetBlendEnabled(bool blendEnabled); + void SetAlphaBlend(nxt::BlendOperation blendOperation, + nxt::BlendFactor srcFactor, + nxt::BlendFactor dstFactor); + void SetColorBlend(nxt::BlendOperation blendOperation, + nxt::BlendFactor srcFactor, + nxt::BlendFactor dstFactor); + void SetColorWriteMask(nxt::ColorWriteMask colorWriteMask); - private: - friend class BlendStateBase; + private: + friend class BlendStateBase; - BlendStateBase* GetResultImpl() override; + BlendStateBase* GetResultImpl() override; - int mPropertiesSet = 0; + int mPropertiesSet = 0; - BlendStateBase::BlendInfo mBlendInfo; + BlendStateBase::BlendInfo mBlendInfo; }; -} +} // namespace backend -#endif // BACKEND_BLENDSTATE_H_ +#endif // BACKEND_BLENDSTATE_H_ diff --git a/src/backend/Buffer.cpp b/src/backend/Buffer.cpp index 20920da841..a16cad4dca 100644 --- a/src/backend/Buffer.cpp +++ b/src/backend/Buffer.cpp @@ -17,8 +17,8 @@ #include "backend/Device.h" #include "common/Assert.h" -#include #include +#include namespace backend { @@ -57,7 +57,9 @@ namespace backend { return mCurrentUsage; } - void BufferBase::CallMapReadCallback(uint32_t serial, nxtBufferMapReadStatus status, const void* pointer) { + void BufferBase::CallMapReadCallback(uint32_t serial, + nxtBufferMapReadStatus status, + const void* pointer) { if (mMapReadCallback && serial == mMapReadSerial) { mMapReadCallback(status, pointer, mMapReadUserdata); mMapReadCallback = nullptr; @@ -78,7 +80,10 @@ namespace backend { SetSubDataImpl(start, count, data); } - void BufferBase::MapReadAsync(uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata) { + void BufferBase::MapReadAsync(uint32_t start, + uint32_t size, + nxtBufferMapReadCallback callback, + nxtCallbackUserdata userdata) { if (start + size > GetSize()) { mDevice->HandleError("Buffer map read out of range"); callback(NXT_BUFFER_MAP_READ_STATUS_ERROR, nullptr, userdata); @@ -98,7 +103,7 @@ namespace backend { } // TODO(cwallez@chromium.org): what to do on wraparound? Could cause crashes. - mMapReadSerial ++; + mMapReadSerial++; mMapReadCallback = callback; mMapReadUserdata = userdata; MapReadAsyncImpl(mMapReadSerial, start, size); @@ -128,11 +133,8 @@ namespace backend { bool BufferBase::IsUsagePossible(nxt::BufferUsageBit allowedUsage, nxt::BufferUsageBit usage) { const nxt::BufferUsageBit allReadBits = - nxt::BufferUsageBit::MapRead | - nxt::BufferUsageBit::TransferSrc | - nxt::BufferUsageBit::Index | - nxt::BufferUsageBit::Vertex | - nxt::BufferUsageBit::Uniform; + nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::TransferSrc | + nxt::BufferUsageBit::Index | nxt::BufferUsageBit::Vertex | nxt::BufferUsageBit::Uniform; bool allowed = (usage & allowedUsage) == usage; bool readOnly = (usage & allReadBits) == usage; bool singleUse = nxt::HasZeroOrOneBits(usage); @@ -189,14 +191,16 @@ namespace backend { return nullptr; } - const nxt::BufferUsageBit kMapWriteAllowedUsages = nxt::BufferUsageBit::MapWrite | nxt::BufferUsageBit::TransferSrc; + const nxt::BufferUsageBit kMapWriteAllowedUsages = + nxt::BufferUsageBit::MapWrite | nxt::BufferUsageBit::TransferSrc; if (mAllowedUsage & nxt::BufferUsageBit::MapWrite && (mAllowedUsage & kMapWriteAllowedUsages) != mAllowedUsage) { HandleError("Only TransferSrc is allowed with MapWrite"); return nullptr; } - const nxt::BufferUsageBit kMapReadAllowedUsages = nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::TransferDst; + const nxt::BufferUsageBit kMapReadAllowedUsages = + nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::TransferDst; if (mAllowedUsage & nxt::BufferUsageBit::MapRead && (mAllowedUsage & kMapReadAllowedUsages) != mAllowedUsage) { HandleError("Only TransferDst is allowed with MapRead"); @@ -296,4 +300,4 @@ namespace backend { mPropertiesSet |= BUFFER_VIEW_PROPERTY_EXTENT; } -} +} // namespace backend diff --git a/src/backend/Buffer.h b/src/backend/Buffer.h index 5b2cf8207c..ea9f2a60d3 100644 --- a/src/backend/Buffer.h +++ b/src/backend/Buffer.h @@ -15,8 +15,8 @@ #ifndef BACKEND_BUFFER_H_ #define BACKEND_BUFFER_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "nxt/nxtcpp.h" @@ -24,103 +24,109 @@ namespace backend { class BufferBase : public RefCounted { - public: - BufferBase(BufferBuilder* builder); - ~BufferBase(); + public: + BufferBase(BufferBuilder* builder); + ~BufferBase(); - uint32_t GetSize() const; - nxt::BufferUsageBit GetAllowedUsage() const; - nxt::BufferUsageBit GetUsage() const; - static bool IsUsagePossible(nxt::BufferUsageBit allowedUsage, nxt::BufferUsageBit usage); - bool IsTransitionPossible(nxt::BufferUsageBit usage) const; - bool IsFrozen() const; - bool HasFrozenUsage(nxt::BufferUsageBit usage) const; - void UpdateUsageInternal(nxt::BufferUsageBit usage); + uint32_t GetSize() const; + nxt::BufferUsageBit GetAllowedUsage() const; + nxt::BufferUsageBit GetUsage() const; + static bool IsUsagePossible(nxt::BufferUsageBit allowedUsage, nxt::BufferUsageBit usage); + bool IsTransitionPossible(nxt::BufferUsageBit usage) const; + bool IsFrozen() const; + bool HasFrozenUsage(nxt::BufferUsageBit usage) const; + void UpdateUsageInternal(nxt::BufferUsageBit usage); - DeviceBase* GetDevice(); + DeviceBase* GetDevice(); - // NXT API - BufferViewBuilder* CreateBufferViewBuilder(); - void SetSubData(uint32_t start, uint32_t count, const uint32_t* data); - void MapReadAsync(uint32_t start, uint32_t size, nxtBufferMapReadCallback callback, nxtCallbackUserdata userdata); - void Unmap(); - void TransitionUsage(nxt::BufferUsageBit usage); - void FreezeUsage(nxt::BufferUsageBit usage); + // NXT API + BufferViewBuilder* CreateBufferViewBuilder(); + void SetSubData(uint32_t start, uint32_t count, const uint32_t* data); + void MapReadAsync(uint32_t start, + uint32_t size, + nxtBufferMapReadCallback callback, + nxtCallbackUserdata userdata); + void Unmap(); + void TransitionUsage(nxt::BufferUsageBit usage); + void FreezeUsage(nxt::BufferUsageBit usage); - protected: - void CallMapReadCallback(uint32_t serial, nxtBufferMapReadStatus status, const void* pointer); + protected: + void CallMapReadCallback(uint32_t serial, + nxtBufferMapReadStatus status, + const void* pointer); - private: - virtual void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) = 0; - virtual void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) = 0; - virtual void UnmapImpl() = 0; - virtual void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage) = 0; + private: + virtual void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) = 0; + virtual void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) = 0; + virtual void UnmapImpl() = 0; + virtual void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, + nxt::BufferUsageBit targetUsage) = 0; - DeviceBase* mDevice; - uint32_t mSize; - nxt::BufferUsageBit mAllowedUsage = nxt::BufferUsageBit::None; - nxt::BufferUsageBit mCurrentUsage = nxt::BufferUsageBit::None; + DeviceBase* mDevice; + uint32_t mSize; + nxt::BufferUsageBit mAllowedUsage = nxt::BufferUsageBit::None; + nxt::BufferUsageBit mCurrentUsage = nxt::BufferUsageBit::None; - nxtBufferMapReadCallback mMapReadCallback = nullptr; - nxtCallbackUserdata mMapReadUserdata = 0; - uint32_t mMapReadSerial = 0; + nxtBufferMapReadCallback mMapReadCallback = nullptr; + nxtCallbackUserdata mMapReadUserdata = 0; + uint32_t mMapReadSerial = 0; - bool mIsFrozen = false; - bool mIsMapped = false; + bool mIsFrozen = false; + bool mIsMapped = false; }; class BufferBuilder : public Builder { - public: - BufferBuilder(DeviceBase* device); + public: + BufferBuilder(DeviceBase* device); - // NXT API - void SetAllowedUsage(nxt::BufferUsageBit usage); - void SetInitialUsage(nxt::BufferUsageBit usage); - void SetSize(uint32_t size); + // NXT API + void SetAllowedUsage(nxt::BufferUsageBit usage); + void SetInitialUsage(nxt::BufferUsageBit usage); + void SetSize(uint32_t size); - private: - friend class BufferBase; + private: + friend class BufferBase; - BufferBase* GetResultImpl() override; + BufferBase* GetResultImpl() override; - uint32_t mSize; - nxt::BufferUsageBit mAllowedUsage = nxt::BufferUsageBit::None; - nxt::BufferUsageBit mCurrentUsage = nxt::BufferUsageBit::None; - int mPropertiesSet = 0; + uint32_t mSize; + nxt::BufferUsageBit mAllowedUsage = nxt::BufferUsageBit::None; + nxt::BufferUsageBit mCurrentUsage = nxt::BufferUsageBit::None; + int mPropertiesSet = 0; }; class BufferViewBase : public RefCounted { - public: - BufferViewBase(BufferViewBuilder* builder); + public: + BufferViewBase(BufferViewBuilder* builder); - BufferBase* GetBuffer(); - uint32_t GetSize() const; - uint32_t GetOffset() const; + BufferBase* GetBuffer(); + uint32_t GetSize() const; + uint32_t GetOffset() const; - private: - Ref mBuffer; - uint32_t mSize; - uint32_t mOffset; + private: + Ref mBuffer; + uint32_t mSize; + uint32_t mOffset; }; class BufferViewBuilder : public Builder { - public: - BufferViewBuilder(DeviceBase* device, BufferBase* buffer); + public: + BufferViewBuilder(DeviceBase* device, BufferBase* buffer); - // NXT API - void SetExtent(uint32_t offset, uint32_t size); + // NXT API + void SetExtent(uint32_t offset, uint32_t size); - private: - friend class BufferViewBase; + private: + friend class BufferViewBase; - BufferViewBase* GetResultImpl() override; + BufferViewBase* GetResultImpl() override; - Ref mBuffer; - uint32_t mOffset = 0; - uint32_t mSize = 0; - int mPropertiesSet = 0; + Ref mBuffer; + uint32_t mOffset = 0; + uint32_t mSize = 0; + int mPropertiesSet = 0; }; -} +} // namespace backend -#endif // BACKEND_BUFFER_H_ +#endif // BACKEND_BUFFER_H_ diff --git a/src/backend/Builder.cpp b/src/backend/Builder.cpp index 79fc579065..1a668e6b11 100644 --- a/src/backend/Builder.cpp +++ b/src/backend/Builder.cpp @@ -32,8 +32,8 @@ namespace backend { } void BuilderBase::SetErrorCallback(nxt::BuilderErrorCallback callback, - nxt::CallbackUserdata userdata1, - nxt::CallbackUserdata userdata2) { + nxt::CallbackUserdata userdata1, + nxt::CallbackUserdata userdata2) { mCallback = callback; mUserdata1 = userdata1; mUserdata2 = userdata2; @@ -44,14 +44,15 @@ namespace backend { BuilderBase::~BuilderBase() { if (!mIsConsumed && mCallback != nullptr) { - mCallback(NXT_BUILDER_ERROR_STATUS_UNKNOWN, "Builder destroyed before GetResult", mUserdata1, mUserdata2); + mCallback(NXT_BUILDER_ERROR_STATUS_UNKNOWN, "Builder destroyed before GetResult", + mUserdata1, mUserdata2); } } void BuilderBase::SetStatus(nxt::BuilderErrorStatus status, const char* message) { ASSERT(status != nxt::BuilderErrorStatus::Success); ASSERT(status != nxt::BuilderErrorStatus::Unknown); - ASSERT(!mGotStatus); // This is not strictly necessary but something to strive for. + ASSERT(!mGotStatus); // This is not strictly necessary but something to strive for. mGotStatus = true; mStoredStatus = status; @@ -78,17 +79,19 @@ namespace backend { } // Unhandled builder errors are promoted to device errors - if (!mCallback) mDevice->HandleError(("Unhandled builder error: " + mStoredMessage).c_str()); + if (!mCallback) + mDevice->HandleError(("Unhandled builder error: " + mStoredMessage).c_str()); } else { ASSERT(mStoredStatus == nxt::BuilderErrorStatus::Success); ASSERT(mStoredMessage.empty()); } if (mCallback != nullptr) { - mCallback(static_cast(mStoredStatus), mStoredMessage.c_str(), mUserdata1, mUserdata2); + mCallback(static_cast(mStoredStatus), mStoredMessage.c_str(), + mUserdata1, mUserdata2); } return result != nullptr; } -} +} // namespace backend diff --git a/src/backend/Builder.h b/src/backend/Builder.h index 350d8baac9..9a85ce2b4f 100644 --- a/src/backend/Builder.h +++ b/src/backend/Builder.h @@ -25,79 +25,76 @@ namespace backend { // This class implements behavior shared by all builders: - // - Tracking whether GetResult has been called already, needed by the - // autogenerated code to prevent operations on "consumed" builders. - // - The error status callback of the API. The callback is guaranteed to be - // called exactly once with an error, a success, or "unknown" if the - // builder is destroyed; also the builder callback cannot be called before - // either the object is destroyed or GetResult is called. + // - Tracking whether GetResult has been called already, needed by the autogenerated code to + // prevent operations on "consumed" builders. + // - The error status callback of the API. The callback is guaranteed to be called exactly once + // with an error, a success, or "unknown" if the builder is destroyed; also the builder + // callback cannot be called before either the object is destroyed or GetResult is called. // - // It is possible for error to be generated before the error callback is - // registered when a builder "set" function performance validation inline. - // Because of this we have to store the status in the builder and defer - // calling the callback to GetResult. + // It is possible for error to be generated before the error callback is registered when a + // builder "set" function performance validation inline. Because of this we have to store the + // status in the builder and defer calling the callback to GetResult. class BuilderBase : public RefCounted { - public: - // Used by the auto-generated validation to prevent usage of the builder - // after GetResult or an error. - bool CanBeUsed() const; - DeviceBase* GetDevice(); + public: + // Used by the auto-generated validation to prevent usage of the builder + // after GetResult or an error. + bool CanBeUsed() const; + DeviceBase* GetDevice(); - // Set the status of the builder to an error. - void HandleError(const char* message); + // Set the status of the builder to an error. + void HandleError(const char* message); - // Internal API, to be used by builder and BackendProcTable only. - // rReturns true for success cases, and calls the callback with appropriate status. - bool HandleResult(RefCounted* result); + // Internal API, to be used by builder and BackendProcTable only. + // Returns true for success cases, and calls the callback with appropriate status. + bool HandleResult(RefCounted* result); - // NXT API - void SetErrorCallback(nxt::BuilderErrorCallback callback, - nxt::CallbackUserdata userdata1, - nxt::CallbackUserdata userdata2); + // NXT API + void SetErrorCallback(nxt::BuilderErrorCallback callback, + nxt::CallbackUserdata userdata1, + nxt::CallbackUserdata userdata2); - protected: - BuilderBase(DeviceBase* device); - ~BuilderBase(); + protected: + BuilderBase(DeviceBase* device); + ~BuilderBase(); - DeviceBase* const mDevice; - bool mGotStatus = false; + DeviceBase* const mDevice; + bool mGotStatus = false; - private: - void SetStatus(nxt::BuilderErrorStatus status, const char* message); + private: + void SetStatus(nxt::BuilderErrorStatus status, const char* message); - nxt::BuilderErrorCallback mCallback = nullptr; - nxt::CallbackUserdata mUserdata1 = 0; - nxt::CallbackUserdata mUserdata2 = 0; + nxt::BuilderErrorCallback mCallback = nullptr; + nxt::CallbackUserdata mUserdata1 = 0; + nxt::CallbackUserdata mUserdata2 = 0; - nxt::BuilderErrorStatus mStoredStatus = nxt::BuilderErrorStatus::Success; - std::string mStoredMessage; + nxt::BuilderErrorStatus mStoredStatus = nxt::BuilderErrorStatus::Success; + std::string mStoredMessage; - bool mIsConsumed = false; + bool mIsConsumed = false; }; - // This builder base class is used to capture the calls to GetResult and make sure - // that either: + // This builder base class is used to capture the calls to GetResult and make sure that either: // - There was an error, callback is called with an error and nullptr is returned. // - There was no error, callback is called with success and a non-null T* is returned. - template + template class Builder : public BuilderBase { - public: - // NXT API - T* GetResult(); + public: + // NXT API + T* GetResult(); - protected: - using BuilderBase::BuilderBase; + protected: + using BuilderBase::BuilderBase; - private: - virtual T* GetResultImpl() = 0; + private: + virtual T* GetResultImpl() = 0; }; - template + template T* Builder::GetResult() { T* result = GetResultImpl(); - // An object can have been returned but failed its initialization, so if an error - // happened, return nullptr instead of result. + // An object can have been returned but failed its initialization, so if an error happened, + // return nullptr instead of result. if (HandleResult(result)) { return result; } else { @@ -105,6 +102,6 @@ namespace backend { } } -} +} // namespace backend -#endif // BACKEND_BUILDER_H_ +#endif // BACKEND_BUILDER_H_ diff --git a/src/backend/CommandAllocator.cpp b/src/backend/CommandAllocator.cpp index ffb2ccd9ee..101ed0a777 100644 --- a/src/backend/CommandAllocator.cpp +++ b/src/backend/CommandAllocator.cpp @@ -23,13 +23,12 @@ namespace backend { - constexpr uint32_t EndOfBlock = UINT_MAX;//std::numeric_limits::max(); - constexpr uint32_t AdditionalData = UINT_MAX - 1;//std::numeric_limits::max(); + constexpr uint32_t EndOfBlock = UINT_MAX; // std::numeric_limits::max(); + constexpr uint32_t AdditionalData = UINT_MAX - 1; // std::numeric_limits::max() - 1; // TODO(cwallez@chromium.org): figure out a way to have more type safety for the iterator - CommandIterator::CommandIterator() - : mEndOfBlock(EndOfBlock) { + CommandIterator::CommandIterator() : mEndOfBlock(EndOfBlock) { Reset(); } @@ -43,8 +42,7 @@ namespace backend { } } - CommandIterator::CommandIterator(CommandIterator&& other) - : mEndOfBlock(EndOfBlock) { + CommandIterator::CommandIterator(CommandIterator&& other) : mEndOfBlock(EndOfBlock) { if (!other.IsEmpty()) { mBlocks = std::move(other.mBlocks); other.Reset(); @@ -80,9 +78,8 @@ namespace backend { mCurrentBlock = 0; if (mBlocks.empty()) { - // This will case the first NextCommandId call to try to move to the next - // block and stop the iteration immediately, without special casing the - // initialization. + // This will case the first NextCommandId call to try to move to the next block and stop + // the iteration immediately, without special casing the initialization. mCurrentPtr = reinterpret_cast(&mEndOfBlock); mBlocks.emplace_back(); mBlocks[0].size = sizeof(mEndOfBlock); @@ -102,7 +99,8 @@ namespace backend { bool CommandIterator::NextCommandId(uint32_t* commandId) { uint8_t* idPtr = AlignPtr(mCurrentPtr, alignof(uint32_t)); - ASSERT(idPtr + sizeof(uint32_t) <= mBlocks[mCurrentBlock].block + mBlocks[mCurrentBlock].size); + ASSERT(idPtr + sizeof(uint32_t) <= + mBlocks[mCurrentBlock].block + mBlocks[mCurrentBlock].size); uint32_t id = *reinterpret_cast(idPtr); @@ -124,7 +122,8 @@ namespace backend { void* CommandIterator::NextCommand(size_t commandSize, size_t commandAlignment) { uint8_t* commandPtr = AlignPtr(mCurrentPtr, commandAlignment); - ASSERT(commandPtr + sizeof(commandSize) <= mBlocks[mCurrentBlock].block + mBlocks[mCurrentBlock].size); + ASSERT(commandPtr + sizeof(commandSize) <= + mBlocks[mCurrentBlock].block + mBlocks[mCurrentBlock].size); mCurrentPtr = commandPtr + commandSize; return commandPtr; @@ -140,13 +139,18 @@ namespace backend { } // Potential TODO(cwallez@chromium.org): - // - Host the size and pointer to next block in the block itself to avoid having an allocation in the vector - // - Assume T's alignof is, say 64bits, static assert it, and make commandAlignment a constant in Allocate - // - Be able to optimize allocation to one block, for command buffers expected to live long to avoid cache misses - // - Better block allocation, maybe have NXT API to say command buffer is going to have size close to another + // - Host the size and pointer to next block in the block itself to avoid having an allocation + // in the vector + // - Assume T's alignof is, say 64bits, static assert it, and make commandAlignment a constant + // in Allocate + // - Be able to optimize allocation to one block, for command buffers expected to live long to + // avoid cache misses + // - Better block allocation, maybe have NXT API to say command buffer is going to have size + // close to another CommandAllocator::CommandAllocator() - : mCurrentPtr(reinterpret_cast(&mDummyEnum[0])), mEndPtr(reinterpret_cast(&mDummyEnum[1])) { + : mCurrentPtr(reinterpret_cast(&mDummyEnum[0])), + mEndPtr(reinterpret_cast(&mDummyEnum[1])) { } CommandAllocator::~CommandAllocator() { @@ -164,7 +168,9 @@ namespace backend { return std::move(mBlocks); } - uint8_t* CommandAllocator::Allocate(uint32_t commandId, size_t commandSize, size_t commandAlignment) { + uint8_t* CommandAllocator::Allocate(uint32_t commandId, + size_t commandSize, + size_t commandAlignment) { ASSERT(mCurrentPtr != nullptr); ASSERT(mEndPtr != nullptr); ASSERT(commandId != EndOfBlock); @@ -180,15 +186,15 @@ namespace backend { // When there is not enough space, we signal the EndOfBlock, so that the iterator nows to // move to the next one. EndOfBlock on the last block means the end of the commands. if (nextPtr + sizeof(uint32_t) > mEndPtr) { - - // Even if we are not able to get another block, the list of commands will be well-formed - // and iterable as this block will be that last one. + // Even if we are not able to get another block, the list of commands will be + // well-formed and iterable as this block will be that last one. *idAlloc = EndOfBlock; - // Make sure we have space for current allocation, plus end of block and alignment padding - // for the first id. + // Make sure we have space for current allocation, plus end of block and alignment + // padding for the first id. ASSERT(nextPtr > mCurrentPtr); - if (!GetNewBlock(static_cast(nextPtr - mCurrentPtr) + sizeof(uint32_t) + alignof(uint32_t))) { + if (!GetNewBlock(static_cast(nextPtr - mCurrentPtr) + sizeof(uint32_t) + + alignof(uint32_t))) { return nullptr; } return Allocate(commandId, commandSize, commandAlignment); @@ -205,7 +211,8 @@ namespace backend { bool CommandAllocator::GetNewBlock(size_t minimumSize) { // Allocate blocks doubling sizes each time, to a maximum of 16k (or at least minimumSize). - mLastAllocationSize = std::max(minimumSize, std::min(mLastAllocationSize * 2, size_t(16384))); + mLastAllocationSize = + std::max(minimumSize, std::min(mLastAllocationSize * 2, size_t(16384))); uint8_t* block = reinterpret_cast(malloc(mLastAllocationSize)); if (block == nullptr) { @@ -218,4 +225,4 @@ namespace backend { return true; } -} +} // namespace backend diff --git a/src/backend/CommandAllocator.h b/src/backend/CommandAllocator.h index cebf1faa4c..d9c12eea66 100644 --- a/src/backend/CommandAllocator.h +++ b/src/backend/CommandAllocator.h @@ -15,8 +15,8 @@ #ifndef BACKEND_COMMAND_ALLOCATOR_H_ #define BACKEND_COMMAND_ALLOCATOR_H_ -#include #include +#include #include namespace backend { @@ -61,90 +61,90 @@ namespace backend { // TODO(cwallez@chromium.org): prevent copy for both iterator and allocator class CommandIterator { - public: - CommandIterator(); - ~CommandIterator(); + public: + CommandIterator(); + ~CommandIterator(); - CommandIterator(CommandIterator&& other); - CommandIterator& operator=(CommandIterator&& other); + CommandIterator(CommandIterator&& other); + CommandIterator& operator=(CommandIterator&& other); - CommandIterator(CommandAllocator&& allocator); - CommandIterator& operator=(CommandAllocator&& allocator); + CommandIterator(CommandAllocator&& allocator); + CommandIterator& operator=(CommandAllocator&& allocator); - template - bool NextCommandId(E* commandId) { - return NextCommandId(reinterpret_cast(commandId)); - } - template - T* NextCommand() { - return reinterpret_cast(NextCommand(sizeof(T), alignof(T))); - } - template - T* NextData(size_t count) { - return reinterpret_cast(NextData(sizeof(T) * count, alignof(T))); - } + template + bool NextCommandId(E* commandId) { + return NextCommandId(reinterpret_cast(commandId)); + } + template + T* NextCommand() { + return reinterpret_cast(NextCommand(sizeof(T), alignof(T))); + } + template + T* NextData(size_t count) { + return reinterpret_cast(NextData(sizeof(T) * count, alignof(T))); + } - // Needs to be called if iteration was stopped early. - void Reset(); + // Needs to be called if iteration was stopped early. + void Reset(); - void DataWasDestroyed(); + void DataWasDestroyed(); - private: - bool IsEmpty() const; + private: + bool IsEmpty() const; - bool NextCommandId(uint32_t* commandId); - void* NextCommand(size_t commandSize, size_t commandAlignment); - void* NextData(size_t dataSize, size_t dataAlignment); + bool NextCommandId(uint32_t* commandId); + void* NextCommand(size_t commandSize, size_t commandAlignment); + void* NextData(size_t dataSize, size_t dataAlignment); - CommandBlocks mBlocks; - uint8_t* mCurrentPtr = nullptr; - size_t mCurrentBlock = 0; - // Used to avoid a special case for empty iterators. - uint32_t mEndOfBlock; - bool mDataWasDestroyed = false; + CommandBlocks mBlocks; + uint8_t* mCurrentPtr = nullptr; + size_t mCurrentBlock = 0; + // Used to avoid a special case for empty iterators. + uint32_t mEndOfBlock; + bool mDataWasDestroyed = false; }; class CommandAllocator { - public: - CommandAllocator(); - ~CommandAllocator(); + public: + CommandAllocator(); + ~CommandAllocator(); - template - T* Allocate(E commandId) { - static_assert(sizeof(E) == sizeof(uint32_t), ""); - static_assert(alignof(E) == alignof(uint32_t), ""); - return reinterpret_cast(Allocate(static_cast(commandId), sizeof(T), alignof(T))); - } + template + T* Allocate(E commandId) { + static_assert(sizeof(E) == sizeof(uint32_t), ""); + static_assert(alignof(E) == alignof(uint32_t), ""); + return reinterpret_cast( + Allocate(static_cast(commandId), sizeof(T), alignof(T))); + } - template - T* AllocateData(size_t count) { - return reinterpret_cast(AllocateData(sizeof(T) * count, alignof(T))); - } + template + T* AllocateData(size_t count) { + return reinterpret_cast(AllocateData(sizeof(T) * count, alignof(T))); + } - private: - friend CommandIterator; - CommandBlocks&& AcquireBlocks(); + private: + friend CommandIterator; + CommandBlocks&& AcquireBlocks(); - uint8_t* Allocate(uint32_t commandId, size_t commandSize, size_t commandAlignment); - uint8_t* AllocateData(size_t dataSize, size_t dataAlignment); - bool GetNewBlock(size_t minimumSize); + uint8_t* Allocate(uint32_t commandId, size_t commandSize, size_t commandAlignment); + uint8_t* AllocateData(size_t dataSize, size_t dataAlignment); + bool GetNewBlock(size_t minimumSize); - CommandBlocks mBlocks; - size_t mLastAllocationSize = 2048; + CommandBlocks mBlocks; + size_t mLastAllocationSize = 2048; - // Pointers to the current range of allocation in the block. Guaranteed to allow - // for at least one uint32_t is not nullptr, so that the special EndOfBlock command id - // can always be written. - // Nullptr iff the blocks were moved out. - uint8_t* mCurrentPtr = nullptr; - uint8_t* mEndPtr = nullptr; + // Pointers to the current range of allocation in the block. Guaranteed to allow for at + // least one uint32_t is not nullptr, so that the special EndOfBlock command id can always + // be written. Nullptr iff the blocks were moved out. + uint8_t* mCurrentPtr = nullptr; + uint8_t* mEndPtr = nullptr; - // Data used for the block range at initialization so that the first call to Allocate - // sees there is not enough space and calls GetNewBlock. This avoids having to special - // case the initialization in Allocate. - uint32_t mDummyEnum[1] = {0}; + // Data used for the block range at initialization so that the first call to Allocate sees + // there is not enough space and calls GetNewBlock. This avoids having to special case the + // initialization in Allocate. + uint32_t mDummyEnum[1] = {0}; }; -} +} // namespace backend -#endif // BACKEND_COMMAND_ALLOCATOR_H_ +#endif // BACKEND_COMMAND_ALLOCATOR_H_ diff --git a/src/backend/CommandBuffer.cpp b/src/backend/CommandBuffer.cpp index b10ddb0c38..79a99b4fdb 100644 --- a/src/backend/CommandBuffer.cpp +++ b/src/backend/CommandBuffer.cpp @@ -16,8 +16,8 @@ #include "backend/BindGroup.h" #include "backend/Buffer.h" -#include "backend/Commands.h" #include "backend/CommandBufferStateTracker.h" +#include "backend/Commands.h" #include "backend/ComputePipeline.h" #include "backend/Device.h" #include "backend/InputState.h" @@ -32,22 +32,27 @@ namespace backend { namespace { - bool ValidateCopyLocationFitsInTexture(CommandBufferBuilder* builder, const TextureCopyLocation& location) { + bool ValidateCopyLocationFitsInTexture(CommandBufferBuilder* builder, + const TextureCopyLocation& location) { const TextureBase* texture = location.texture.Get(); if (location.level >= texture->GetNumMipLevels()) { builder->HandleError("Copy mip-level out of range"); return false; } - // All texture dimensions are in uint32_t so by doing checks in uint64_t we avoid overflows. + // All texture dimensions are in uint32_t so by doing checks in uint64_t we avoid + // overflows. uint64_t level = location.level; - if (uint64_t(location.x) + uint64_t(location.width) > (static_cast(texture->GetWidth()) >> level) || - uint64_t(location.y) + uint64_t(location.height) > (static_cast(texture->GetHeight()) >> level)) { + if (uint64_t(location.x) + uint64_t(location.width) > + (static_cast(texture->GetWidth()) >> level) || + uint64_t(location.y) + uint64_t(location.height) > + (static_cast(texture->GetHeight()) >> level)) { builder->HandleError("Copy would touch outside of the texture"); return false; } - // TODO(cwallez@chromium.org): Check the depth bound differently for 2D arrays and 3D textures + // TODO(cwallez@chromium.org): Check the depth bound differently for 2D arrays and 3D + // textures if (location.z != 0 || location.depth != 1) { builder->HandleError("No support for z != 0 and depth != 1 for now"); return false; @@ -61,7 +66,9 @@ namespace backend { return offset <= bufferSize && (size <= (bufferSize - offset)); } - bool ValidateCopySizeFitsInBuffer(CommandBufferBuilder* builder, const BufferCopyLocation& location, uint32_t dataSize) { + bool ValidateCopySizeFitsInBuffer(CommandBufferBuilder* builder, + const BufferCopyLocation& location, + uint32_t dataSize) { if (!FitsInBuffer(location.buffer.Get(), location.offset, dataSize)) { builder->HandleError("Copy would overflow the buffer"); return false; @@ -70,8 +77,11 @@ namespace backend { return true; } - bool ValidateTexelBufferOffset(CommandBufferBuilder* builder, TextureBase* texture, const BufferCopyLocation& location) { - uint32_t texelSize = static_cast(TextureFormatPixelSize(texture->GetFormat())); + bool ValidateTexelBufferOffset(CommandBufferBuilder* builder, + TextureBase* texture, + const BufferCopyLocation& location) { + uint32_t texelSize = + static_cast(TextureFormatPixelSize(texture->GetFormat())); if (location.offset % texelSize != 0) { builder->HandleError("Buffer offset must be a multiple of the texel size"); return false; @@ -80,7 +90,10 @@ namespace backend { return true; } - bool ComputeTextureCopyBufferSize(CommandBufferBuilder*, const TextureCopyLocation& location, uint32_t rowPitch, uint32_t* bufferSize) { + bool ComputeTextureCopyBufferSize(CommandBufferBuilder*, + const TextureCopyLocation& location, + uint32_t rowPitch, + uint32_t* bufferSize) { // TODO(cwallez@chromium.org): check for overflows *bufferSize = (rowPitch * (location.height - 1) + location.width) * location.depth; @@ -92,7 +105,9 @@ namespace backend { return texelSize * width; } - bool ValidateRowPitch(CommandBufferBuilder* builder, const TextureCopyLocation& location, uint32_t rowPitch) { + bool ValidateRowPitch(CommandBufferBuilder* builder, + const TextureCopyLocation& location, + uint32_t rowPitch) { if (rowPitch % kTextureRowPitchAlignment != 0) { builder->HandleError("Row pitch must be a multiple of 256"); return false; @@ -107,7 +122,7 @@ namespace backend { return true; } - } + } // namespace CommandBufferBase::CommandBufferBase(CommandBufferBuilder* builder) : mDevice(builder->mDevice), @@ -137,146 +152,104 @@ namespace backend { void FreeCommands(CommandIterator* commands) { Command type; - while(commands->NextCommandId(&type)) { + while (commands->NextCommandId(&type)) { switch (type) { - case Command::BeginComputePass: - { - BeginComputePassCmd* begin = commands->NextCommand(); - begin->~BeginComputePassCmd(); + case Command::BeginComputePass: { + BeginComputePassCmd* begin = commands->NextCommand(); + begin->~BeginComputePassCmd(); + } break; + case Command::BeginRenderPass: { + BeginRenderPassCmd* begin = commands->NextCommand(); + begin->~BeginRenderPassCmd(); + } break; + case Command::BeginRenderSubpass: { + BeginRenderSubpassCmd* begin = commands->NextCommand(); + begin->~BeginRenderSubpassCmd(); + } break; + case Command::CopyBufferToBuffer: { + CopyBufferToBufferCmd* copy = commands->NextCommand(); + copy->~CopyBufferToBufferCmd(); + } break; + case Command::CopyBufferToTexture: { + CopyBufferToTextureCmd* copy = commands->NextCommand(); + copy->~CopyBufferToTextureCmd(); + } break; + case Command::CopyTextureToBuffer: { + CopyTextureToBufferCmd* copy = commands->NextCommand(); + copy->~CopyTextureToBufferCmd(); + } break; + case Command::Dispatch: { + DispatchCmd* dispatch = commands->NextCommand(); + dispatch->~DispatchCmd(); + } break; + case Command::DrawArrays: { + DrawArraysCmd* draw = commands->NextCommand(); + draw->~DrawArraysCmd(); + } break; + case Command::DrawElements: { + DrawElementsCmd* draw = commands->NextCommand(); + draw->~DrawElementsCmd(); + } break; + case Command::EndComputePass: { + EndComputePassCmd* cmd = commands->NextCommand(); + cmd->~EndComputePassCmd(); + } break; + case Command::EndRenderPass: { + EndRenderPassCmd* cmd = commands->NextCommand(); + cmd->~EndRenderPassCmd(); + } break; + case Command::EndRenderSubpass: { + EndRenderSubpassCmd* cmd = commands->NextCommand(); + cmd->~EndRenderSubpassCmd(); + } break; + case Command::SetComputePipeline: { + SetComputePipelineCmd* cmd = commands->NextCommand(); + cmd->~SetComputePipelineCmd(); + } break; + case Command::SetRenderPipeline: { + SetRenderPipelineCmd* cmd = commands->NextCommand(); + cmd->~SetRenderPipelineCmd(); + } break; + case Command::SetPushConstants: { + SetPushConstantsCmd* cmd = commands->NextCommand(); + commands->NextData(cmd->count); + cmd->~SetPushConstantsCmd(); + } break; + case Command::SetStencilReference: { + SetStencilReferenceCmd* cmd = commands->NextCommand(); + cmd->~SetStencilReferenceCmd(); + } break; + case Command::SetBlendColor: { + SetBlendColorCmd* cmd = commands->NextCommand(); + cmd->~SetBlendColorCmd(); + } break; + case Command::SetBindGroup: { + SetBindGroupCmd* cmd = commands->NextCommand(); + cmd->~SetBindGroupCmd(); + } break; + case Command::SetIndexBuffer: { + SetIndexBufferCmd* cmd = commands->NextCommand(); + cmd->~SetIndexBufferCmd(); + } break; + case Command::SetVertexBuffers: { + SetVertexBuffersCmd* cmd = commands->NextCommand(); + auto buffers = commands->NextData>(cmd->count); + for (size_t i = 0; i < cmd->count; ++i) { + (&buffers[i])->~Ref(); } - break; - case Command::BeginRenderPass: - { - BeginRenderPassCmd* begin = commands->NextCommand(); - begin->~BeginRenderPassCmd(); - } - break; - case Command::BeginRenderSubpass: - { - BeginRenderSubpassCmd* begin = commands->NextCommand(); - begin->~BeginRenderSubpassCmd(); - } - break; - case Command::CopyBufferToBuffer: - { - CopyBufferToBufferCmd* copy = commands->NextCommand(); - copy->~CopyBufferToBufferCmd(); - } - break; - case Command::CopyBufferToTexture: - { - CopyBufferToTextureCmd* copy = commands->NextCommand(); - copy->~CopyBufferToTextureCmd(); - } - break; - case Command::CopyTextureToBuffer: - { - CopyTextureToBufferCmd* copy = commands->NextCommand(); - copy->~CopyTextureToBufferCmd(); - } - break; - case Command::Dispatch: - { - DispatchCmd* dispatch = commands->NextCommand(); - dispatch->~DispatchCmd(); - } - break; - case Command::DrawArrays: - { - DrawArraysCmd* draw = commands->NextCommand(); - draw->~DrawArraysCmd(); - } - break; - case Command::DrawElements: - { - DrawElementsCmd* draw = commands->NextCommand(); - draw->~DrawElementsCmd(); - } - break; - case Command::EndComputePass: - { - EndComputePassCmd* cmd = commands->NextCommand(); - cmd->~EndComputePassCmd(); - } - break; - case Command::EndRenderPass: - { - EndRenderPassCmd* cmd = commands->NextCommand(); - cmd->~EndRenderPassCmd(); - } - break; - case Command::EndRenderSubpass: - { - EndRenderSubpassCmd* cmd = commands->NextCommand(); - cmd->~EndRenderSubpassCmd(); - } - break; - case Command::SetComputePipeline: - { - SetComputePipelineCmd* cmd = commands->NextCommand(); - cmd->~SetComputePipelineCmd(); - } - break; - case Command::SetRenderPipeline: - { - SetRenderPipelineCmd* cmd = commands->NextCommand(); - cmd->~SetRenderPipelineCmd(); - } - break; - case Command::SetPushConstants: - { - SetPushConstantsCmd* cmd = commands->NextCommand(); - commands->NextData(cmd->count); - cmd->~SetPushConstantsCmd(); - } - break; - case Command::SetStencilReference: - { - SetStencilReferenceCmd* cmd = commands->NextCommand(); - cmd->~SetStencilReferenceCmd(); - } - break; - case Command::SetBlendColor: - { - SetBlendColorCmd* cmd = commands->NextCommand(); - cmd->~SetBlendColorCmd(); - } - break; - case Command::SetBindGroup: - { - SetBindGroupCmd* cmd = commands->NextCommand(); - cmd->~SetBindGroupCmd(); - } - break; - case Command::SetIndexBuffer: - { - SetIndexBufferCmd* cmd = commands->NextCommand(); - cmd->~SetIndexBufferCmd(); - } - break; - case Command::SetVertexBuffers: - { - SetVertexBuffersCmd* cmd = commands->NextCommand(); - auto buffers = commands->NextData>(cmd->count); - for (size_t i = 0; i < cmd->count; ++i) { - (&buffers[i])->~Ref(); - } - commands->NextData(cmd->count); - cmd->~SetVertexBuffersCmd(); - } - break; - case Command::TransitionBufferUsage: - { - TransitionBufferUsageCmd* cmd = commands->NextCommand(); - cmd->~TransitionBufferUsageCmd(); - } - break; - case Command::TransitionTextureUsage: - { - TransitionTextureUsageCmd* cmd = commands->NextCommand(); - cmd->~TransitionTextureUsageCmd(); - } - break; + commands->NextData(cmd->count); + cmd->~SetVertexBuffersCmd(); + } break; + case Command::TransitionBufferUsage: { + TransitionBufferUsageCmd* cmd = + commands->NextCommand(); + cmd->~TransitionBufferUsageCmd(); + } break; + case Command::TransitionTextureUsage: { + TransitionTextureUsageCmd* cmd = + commands->NextCommand(); + cmd->~TransitionTextureUsageCmd(); + } break; } } commands->DataWasDestroyed(); @@ -340,12 +313,10 @@ namespace backend { commands->NextCommand(); break; - case Command::SetPushConstants: - { - auto* cmd = commands->NextCommand(); - commands->NextData(cmd->count); - } - break; + case Command::SetPushConstants: { + auto* cmd = commands->NextCommand(); + commands->NextData(cmd->count); + } break; case Command::SetStencilReference: commands->NextCommand(); @@ -363,13 +334,11 @@ namespace backend { commands->NextCommand(); break; - case Command::SetVertexBuffers: - { - auto* cmd = commands->NextCommand(); - commands->NextData>(cmd->count); - commands->NextData(cmd->count); - } - break; + case Command::SetVertexBuffers: { + auto* cmd = commands->NextCommand(); + commands->NextData>(cmd->count); + commands->NextData(cmd->count); + } break; case Command::TransitionBufferUsage: commands->NextCommand(); @@ -381,7 +350,8 @@ namespace backend { } } - CommandBufferBuilder::CommandBufferBuilder(DeviceBase* device) : Builder(device), mState(std::make_unique(this)) { + CommandBufferBuilder::CommandBufferBuilder(DeviceBase* device) + : Builder(device), mState(std::make_unique(this)) { } CommandBufferBuilder::~CommandBufferBuilder() { @@ -397,247 +367,216 @@ namespace backend { Command type; while (mIterator.NextCommandId(&type)) { switch (type) { - case Command::BeginComputePass: - { - mIterator.NextCommand(); - if (!mState->BeginComputePass()) { - return false; - } + case Command::BeginComputePass: { + mIterator.NextCommand(); + if (!mState->BeginComputePass()) { + return false; } - break; + } break; - case Command::BeginRenderPass: - { - BeginRenderPassCmd* cmd = mIterator.NextCommand(); - auto* renderPass = cmd->renderPass.Get(); - auto* framebuffer = cmd->framebuffer.Get(); - // TODO(kainino@chromium.org): null checks should not be necessary - if (renderPass == nullptr) { - HandleError("Render pass is invalid"); - return false; - } - if (framebuffer == nullptr) { - HandleError("Framebuffer is invalid"); - return false; - } - if (!mState->BeginRenderPass(renderPass, framebuffer)) { - return false; - } + case Command::BeginRenderPass: { + BeginRenderPassCmd* cmd = mIterator.NextCommand(); + auto* renderPass = cmd->renderPass.Get(); + auto* framebuffer = cmd->framebuffer.Get(); + // TODO(kainino@chromium.org): null checks should not be necessary + if (renderPass == nullptr) { + HandleError("Render pass is invalid"); + return false; } - break; - - case Command::BeginRenderSubpass: - { - mIterator.NextCommand(); - if (!mState->BeginSubpass()) { - return false; - } + if (framebuffer == nullptr) { + HandleError("Framebuffer is invalid"); + return false; } - break; - - case Command::CopyBufferToBuffer: - { - CopyBufferToBufferCmd* copy = mIterator.NextCommand(); - if (!ValidateCopySizeFitsInBuffer(this, copy->source, copy->size) || - !ValidateCopySizeFitsInBuffer(this, copy->destination, copy->size) || - !mState->ValidateCanCopy() || - !mState->ValidateCanUseBufferAs(copy->source.buffer.Get(), nxt::BufferUsageBit::TransferSrc) || - !mState->ValidateCanUseBufferAs(copy->destination.buffer.Get(), nxt::BufferUsageBit::TransferDst)) { - return false; - } + if (!mState->BeginRenderPass(renderPass, framebuffer)) { + return false; } - break; + } break; - case Command::CopyBufferToTexture: - { - CopyBufferToTextureCmd* copy = mIterator.NextCommand(); - - uint32_t bufferCopySize = 0; - if (!ValidateRowPitch(this, copy->destination, copy->rowPitch) || - !ComputeTextureCopyBufferSize(this, copy->destination, copy->rowPitch, &bufferCopySize) || - !ValidateCopyLocationFitsInTexture(this, copy->destination) || - !ValidateCopySizeFitsInBuffer(this, copy->source, bufferCopySize) || - !ValidateTexelBufferOffset(this, copy->destination.texture.Get(), copy->source) || - !mState->ValidateCanCopy() || - !mState->ValidateCanUseBufferAs(copy->source.buffer.Get(), nxt::BufferUsageBit::TransferSrc) || - !mState->ValidateCanUseTextureAs(copy->destination.texture.Get(), nxt::TextureUsageBit::TransferDst)) { - return false; - } + case Command::BeginRenderSubpass: { + mIterator.NextCommand(); + if (!mState->BeginSubpass()) { + return false; } - break; + } break; - case Command::CopyTextureToBuffer: - { - CopyTextureToBufferCmd* copy = mIterator.NextCommand(); - - uint32_t bufferCopySize = 0; - if (!ValidateRowPitch(this, copy->source, copy->rowPitch) || - !ComputeTextureCopyBufferSize(this, copy->source, copy->rowPitch, &bufferCopySize) || - !ValidateCopyLocationFitsInTexture(this, copy->source) || - !ValidateCopySizeFitsInBuffer(this, copy->destination, bufferCopySize) || - !ValidateTexelBufferOffset(this, copy->source.texture.Get(), copy->destination) || - !mState->ValidateCanCopy() || - !mState->ValidateCanUseTextureAs(copy->source.texture.Get(), nxt::TextureUsageBit::TransferSrc) || - !mState->ValidateCanUseBufferAs(copy->destination.buffer.Get(), nxt::BufferUsageBit::TransferDst)) { - return false; - } + case Command::CopyBufferToBuffer: { + CopyBufferToBufferCmd* copy = mIterator.NextCommand(); + if (!ValidateCopySizeFitsInBuffer(this, copy->source, copy->size) || + !ValidateCopySizeFitsInBuffer(this, copy->destination, copy->size) || + !mState->ValidateCanCopy() || + !mState->ValidateCanUseBufferAs(copy->source.buffer.Get(), + nxt::BufferUsageBit::TransferSrc) || + !mState->ValidateCanUseBufferAs(copy->destination.buffer.Get(), + nxt::BufferUsageBit::TransferDst)) { + return false; } - break; + } break; - case Command::Dispatch: - { - mIterator.NextCommand(); - if (!mState->ValidateCanDispatch()) { - return false; - } + case Command::CopyBufferToTexture: { + CopyBufferToTextureCmd* copy = mIterator.NextCommand(); + + uint32_t bufferCopySize = 0; + if (!ValidateRowPitch(this, copy->destination, copy->rowPitch) || + !ComputeTextureCopyBufferSize(this, copy->destination, copy->rowPitch, + &bufferCopySize) || + !ValidateCopyLocationFitsInTexture(this, copy->destination) || + !ValidateCopySizeFitsInBuffer(this, copy->source, bufferCopySize) || + !ValidateTexelBufferOffset(this, copy->destination.texture.Get(), + copy->source) || + !mState->ValidateCanCopy() || + !mState->ValidateCanUseBufferAs(copy->source.buffer.Get(), + nxt::BufferUsageBit::TransferSrc) || + !mState->ValidateCanUseTextureAs(copy->destination.texture.Get(), + nxt::TextureUsageBit::TransferDst)) { + return false; } - break; + } break; - case Command::DrawArrays: - { - mIterator.NextCommand(); - if (!mState->ValidateCanDrawArrays()) { - return false; - } + case Command::CopyTextureToBuffer: { + CopyTextureToBufferCmd* copy = mIterator.NextCommand(); + + uint32_t bufferCopySize = 0; + if (!ValidateRowPitch(this, copy->source, copy->rowPitch) || + !ComputeTextureCopyBufferSize(this, copy->source, copy->rowPitch, + &bufferCopySize) || + !ValidateCopyLocationFitsInTexture(this, copy->source) || + !ValidateCopySizeFitsInBuffer(this, copy->destination, bufferCopySize) || + !ValidateTexelBufferOffset(this, copy->source.texture.Get(), + copy->destination) || + !mState->ValidateCanCopy() || + !mState->ValidateCanUseTextureAs(copy->source.texture.Get(), + nxt::TextureUsageBit::TransferSrc) || + !mState->ValidateCanUseBufferAs(copy->destination.buffer.Get(), + nxt::BufferUsageBit::TransferDst)) { + return false; } - break; + } break; - case Command::DrawElements: - { - mIterator.NextCommand(); - if (!mState->ValidateCanDrawElements()) { - return false; - } + case Command::Dispatch: { + mIterator.NextCommand(); + if (!mState->ValidateCanDispatch()) { + return false; } - break; + } break; - case Command::EndComputePass: - { - mIterator.NextCommand(); - if (!mState->EndComputePass()) { - return false; - } + case Command::DrawArrays: { + mIterator.NextCommand(); + if (!mState->ValidateCanDrawArrays()) { + return false; } - break; + } break; - case Command::EndRenderPass: - { - mIterator.NextCommand(); - if (!mState->EndRenderPass()) { - return false; - } + case Command::DrawElements: { + mIterator.NextCommand(); + if (!mState->ValidateCanDrawElements()) { + return false; } - break; + } break; - case Command::EndRenderSubpass: - { - mIterator.NextCommand(); - if (!mState->EndSubpass()) { - return false; - } + case Command::EndComputePass: { + mIterator.NextCommand(); + if (!mState->EndComputePass()) { + return false; } - break; + } break; - case Command::SetComputePipeline: - { - SetComputePipelineCmd* cmd = mIterator.NextCommand(); - ComputePipelineBase* pipeline = cmd->pipeline.Get(); - if (!mState->SetComputePipeline(pipeline)) { - return false; - } + case Command::EndRenderPass: { + mIterator.NextCommand(); + if (!mState->EndRenderPass()) { + return false; } - break; + } break; - case Command::SetRenderPipeline: - { - SetRenderPipelineCmd* cmd = mIterator.NextCommand(); - RenderPipelineBase* pipeline = cmd->pipeline.Get(); - if (!mState->SetRenderPipeline(pipeline)) { - return false; - } + case Command::EndRenderSubpass: { + mIterator.NextCommand(); + if (!mState->EndSubpass()) { + return false; } - break; + } break; - case Command::SetPushConstants: - { - SetPushConstantsCmd* cmd = mIterator.NextCommand(); - mIterator.NextData(cmd->count); - // Validation of count and offset has already been done when the command was recorded - // because it impacts the size of an allocation in the CommandAllocator. - if (!mState->ValidateSetPushConstants(cmd->stages)) { - return false; - } + case Command::SetComputePipeline: { + SetComputePipelineCmd* cmd = mIterator.NextCommand(); + ComputePipelineBase* pipeline = cmd->pipeline.Get(); + if (!mState->SetComputePipeline(pipeline)) { + return false; } - break; + } break; - case Command::SetStencilReference: - { - mIterator.NextCommand(); - if (!mState->HaveRenderSubpass()) { - HandleError("Can't set stencil reference without an active render subpass"); - return false; - } + case Command::SetRenderPipeline: { + SetRenderPipelineCmd* cmd = mIterator.NextCommand(); + RenderPipelineBase* pipeline = cmd->pipeline.Get(); + if (!mState->SetRenderPipeline(pipeline)) { + return false; } - break; + } break; - case Command::SetBlendColor: - { - mIterator.NextCommand(); - if (!mState->HaveRenderSubpass()) { - HandleError("Can't set blend color without an active render subpass"); - return false; - } + case Command::SetPushConstants: { + SetPushConstantsCmd* cmd = mIterator.NextCommand(); + mIterator.NextData(cmd->count); + // Validation of count and offset has already been done when the command was + // recorded because it impacts the size of an allocation in the + // CommandAllocator. + if (!mState->ValidateSetPushConstants(cmd->stages)) { + return false; } - break; + } break; - case Command::SetBindGroup: - { - SetBindGroupCmd* cmd = mIterator.NextCommand(); - if (!mState->SetBindGroup(cmd->index, cmd->group.Get())) { - return false; - } + case Command::SetStencilReference: { + mIterator.NextCommand(); + if (!mState->HaveRenderSubpass()) { + HandleError("Can't set stencil reference without an active render subpass"); + return false; } - break; + } break; - case Command::SetIndexBuffer: - { - SetIndexBufferCmd* cmd = mIterator.NextCommand(); - if (!mState->SetIndexBuffer(cmd->buffer.Get())) { - return false; - } + case Command::SetBlendColor: { + mIterator.NextCommand(); + if (!mState->HaveRenderSubpass()) { + HandleError("Can't set blend color without an active render subpass"); + return false; } - break; + } break; - case Command::SetVertexBuffers: - { - SetVertexBuffersCmd* cmd = mIterator.NextCommand(); - auto buffers = mIterator.NextData>(cmd->count); - mIterator.NextData(cmd->count); - - for (uint32_t i = 0; i < cmd->count; ++i) { - mState->SetVertexBuffer(cmd->startSlot + i, buffers[i].Get()); - } + case Command::SetBindGroup: { + SetBindGroupCmd* cmd = mIterator.NextCommand(); + if (!mState->SetBindGroup(cmd->index, cmd->group.Get())) { + return false; } - break; + } break; - case Command::TransitionBufferUsage: - { - TransitionBufferUsageCmd* cmd = mIterator.NextCommand(); - if (!mState->TransitionBufferUsage(cmd->buffer.Get(), cmd->usage)) { - return false; - } + case Command::SetIndexBuffer: { + SetIndexBufferCmd* cmd = mIterator.NextCommand(); + if (!mState->SetIndexBuffer(cmd->buffer.Get())) { + return false; } - break; + } break; - case Command::TransitionTextureUsage: - { - TransitionTextureUsageCmd* cmd = mIterator.NextCommand(); - if (!mState->TransitionTextureUsage(cmd->texture.Get(), cmd->usage)) { - return false; - } + case Command::SetVertexBuffers: { + SetVertexBuffersCmd* cmd = mIterator.NextCommand(); + auto buffers = mIterator.NextData>(cmd->count); + mIterator.NextData(cmd->count); + for (uint32_t i = 0; i < cmd->count; ++i) { + mState->SetVertexBuffer(cmd->startSlot + i, buffers[i].Get()); } - break; + } break; + + case Command::TransitionBufferUsage: { + TransitionBufferUsageCmd* cmd = + mIterator.NextCommand(); + if (!mState->TransitionBufferUsage(cmd->buffer.Get(), cmd->usage)) { + return false; + } + } break; + + case Command::TransitionTextureUsage: { + TransitionTextureUsageCmd* cmd = + mIterator.NextCommand(); + if (!mState->TransitionTextureUsage(cmd->texture.Get(), cmd->usage)) { + return false; + } + + } break; } } @@ -663,9 +602,10 @@ namespace backend { mAllocator.Allocate(Command::BeginComputePass); } - void CommandBufferBuilder::BeginRenderPass(RenderPassBase* renderPass, FramebufferBase* framebuffer) { + void CommandBufferBuilder::BeginRenderPass(RenderPassBase* renderPass, + FramebufferBase* framebuffer) { BeginRenderPassCmd* cmd = mAllocator.Allocate(Command::BeginRenderPass); - new(cmd) BeginRenderPassCmd; + new (cmd) BeginRenderPassCmd; cmd->renderPass = renderPass; cmd->framebuffer = framebuffer; } @@ -674,9 +614,14 @@ namespace backend { mAllocator.Allocate(Command::BeginRenderSubpass); } - void CommandBufferBuilder::CopyBufferToBuffer(BufferBase* source, uint32_t sourceOffset, BufferBase* destination, uint32_t destinationOffset, uint32_t size) { - CopyBufferToBufferCmd* copy = mAllocator.Allocate(Command::CopyBufferToBuffer); - new(copy) CopyBufferToBufferCmd; + void CommandBufferBuilder::CopyBufferToBuffer(BufferBase* source, + uint32_t sourceOffset, + BufferBase* destination, + uint32_t destinationOffset, + uint32_t size) { + CopyBufferToBufferCmd* copy = + mAllocator.Allocate(Command::CopyBufferToBuffer); + new (copy) CopyBufferToBufferCmd; copy->source.buffer = source; copy->source.offset = sourceOffset; copy->destination.buffer = destination; @@ -684,14 +629,23 @@ namespace backend { copy->size = size; } - void CommandBufferBuilder::CopyBufferToTexture(BufferBase* buffer, uint32_t bufferOffset, uint32_t rowPitch, - TextureBase* texture, uint32_t x, uint32_t y, uint32_t z, - uint32_t width, uint32_t height, uint32_t depth, uint32_t level) { + void CommandBufferBuilder::CopyBufferToTexture(BufferBase* buffer, + uint32_t bufferOffset, + uint32_t rowPitch, + TextureBase* texture, + uint32_t x, + uint32_t y, + uint32_t z, + uint32_t width, + uint32_t height, + uint32_t depth, + uint32_t level) { if (rowPitch == 0) { rowPitch = ComputeDefaultRowPitch(texture, width); } - CopyBufferToTextureCmd* copy = mAllocator.Allocate(Command::CopyBufferToTexture); - new(copy) CopyBufferToTextureCmd; + CopyBufferToTextureCmd* copy = + mAllocator.Allocate(Command::CopyBufferToTexture); + new (copy) CopyBufferToTextureCmd; copy->source.buffer = buffer; copy->source.offset = bufferOffset; copy->destination.texture = texture; @@ -705,14 +659,23 @@ namespace backend { copy->rowPitch = rowPitch; } - void CommandBufferBuilder::CopyTextureToBuffer(TextureBase* texture, uint32_t x, uint32_t y, uint32_t z, - uint32_t width, uint32_t height, uint32_t depth, uint32_t level, - BufferBase* buffer, uint32_t bufferOffset, uint32_t rowPitch) { + void CommandBufferBuilder::CopyTextureToBuffer(TextureBase* texture, + uint32_t x, + uint32_t y, + uint32_t z, + uint32_t width, + uint32_t height, + uint32_t depth, + uint32_t level, + BufferBase* buffer, + uint32_t bufferOffset, + uint32_t rowPitch) { if (rowPitch == 0) { rowPitch = ComputeDefaultRowPitch(texture, width); } - CopyTextureToBufferCmd* copy = mAllocator.Allocate(Command::CopyTextureToBuffer); - new(copy) CopyTextureToBufferCmd; + CopyTextureToBufferCmd* copy = + mAllocator.Allocate(Command::CopyTextureToBuffer); + new (copy) CopyTextureToBufferCmd; copy->source.texture = texture; copy->source.x = x; copy->source.y = y; @@ -728,24 +691,30 @@ namespace backend { void CommandBufferBuilder::Dispatch(uint32_t x, uint32_t y, uint32_t z) { DispatchCmd* dispatch = mAllocator.Allocate(Command::Dispatch); - new(dispatch) DispatchCmd; + new (dispatch) DispatchCmd; dispatch->x = x; dispatch->y = y; dispatch->z = z; } - void CommandBufferBuilder::DrawArrays(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) { + void CommandBufferBuilder::DrawArrays(uint32_t vertexCount, + uint32_t instanceCount, + uint32_t firstVertex, + uint32_t firstInstance) { DrawArraysCmd* draw = mAllocator.Allocate(Command::DrawArrays); - new(draw) DrawArraysCmd; + new (draw) DrawArraysCmd; draw->vertexCount = vertexCount; draw->instanceCount = instanceCount; draw->firstVertex = firstVertex; draw->firstInstance = firstInstance; } - void CommandBufferBuilder::DrawElements(uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, uint32_t firstInstance) { + void CommandBufferBuilder::DrawElements(uint32_t indexCount, + uint32_t instanceCount, + uint32_t firstIndex, + uint32_t firstInstance) { DrawElementsCmd* draw = mAllocator.Allocate(Command::DrawElements); - new(draw) DrawElementsCmd; + new (draw) DrawElementsCmd; draw->indexCount = indexCount; draw->instanceCount = instanceCount; draw->firstIndex = firstIndex; @@ -765,26 +734,32 @@ namespace backend { } void CommandBufferBuilder::SetComputePipeline(ComputePipelineBase* pipeline) { - SetComputePipelineCmd* cmd = mAllocator.Allocate(Command::SetComputePipeline); - new(cmd) SetComputePipelineCmd; + SetComputePipelineCmd* cmd = + mAllocator.Allocate(Command::SetComputePipeline); + new (cmd) SetComputePipelineCmd; cmd->pipeline = pipeline; } void CommandBufferBuilder::SetRenderPipeline(RenderPipelineBase* pipeline) { - SetRenderPipelineCmd* cmd = mAllocator.Allocate(Command::SetRenderPipeline); - new(cmd) SetRenderPipelineCmd; + SetRenderPipelineCmd* cmd = + mAllocator.Allocate(Command::SetRenderPipeline); + new (cmd) SetRenderPipelineCmd; cmd->pipeline = pipeline; } - void CommandBufferBuilder::SetPushConstants(nxt::ShaderStageBit stages, uint32_t offset, uint32_t count, const void* data) { + void CommandBufferBuilder::SetPushConstants(nxt::ShaderStageBit stages, + uint32_t offset, + uint32_t count, + const void* data) { // TODO(cwallez@chromium.org): check for overflows if (offset + count > kMaxPushConstants) { HandleError("Setting too many push constants"); return; } - SetPushConstantsCmd* cmd = mAllocator.Allocate(Command::SetPushConstants); - new(cmd) SetPushConstantsCmd; + SetPushConstantsCmd* cmd = + mAllocator.Allocate(Command::SetPushConstants); + new (cmd) SetPushConstantsCmd; cmd->stages = stages; cmd->offset = offset; cmd->count = count; @@ -794,14 +769,15 @@ namespace backend { } void CommandBufferBuilder::SetStencilReference(uint32_t reference) { - SetStencilReferenceCmd* cmd = mAllocator.Allocate(Command::SetStencilReference); - new(cmd) SetStencilReferenceCmd; + SetStencilReferenceCmd* cmd = + mAllocator.Allocate(Command::SetStencilReference); + new (cmd) SetStencilReferenceCmd; cmd->reference = reference; } void CommandBufferBuilder::SetBlendColor(float r, float g, float b, float a) { SetBlendColorCmd* cmd = mAllocator.Allocate(Command::SetBlendColor); - new(cmd) SetBlendColorCmd; + new (cmd) SetBlendColorCmd; cmd->r = r; cmd->g = g; cmd->b = b; @@ -815,7 +791,7 @@ namespace backend { } SetBindGroupCmd* cmd = mAllocator.Allocate(Command::SetBindGroup); - new(cmd) SetBindGroupCmd; + new (cmd) SetBindGroupCmd; cmd->index = groupIndex; cmd->group = group; } @@ -824,38 +800,46 @@ namespace backend { // TODO(kainino@chromium.org): validation SetIndexBufferCmd* cmd = mAllocator.Allocate(Command::SetIndexBuffer); - new(cmd) SetIndexBufferCmd; + new (cmd) SetIndexBufferCmd; cmd->buffer = buffer; cmd->offset = offset; } - void CommandBufferBuilder::SetVertexBuffers(uint32_t startSlot, uint32_t count, BufferBase* const* buffers, uint32_t const* offsets){ + void CommandBufferBuilder::SetVertexBuffers(uint32_t startSlot, + uint32_t count, + BufferBase* const* buffers, + uint32_t const* offsets) { // TODO(kainino@chromium.org): validation - SetVertexBuffersCmd* cmd = mAllocator.Allocate(Command::SetVertexBuffers); - new(cmd) SetVertexBuffersCmd; + SetVertexBuffersCmd* cmd = + mAllocator.Allocate(Command::SetVertexBuffers); + new (cmd) SetVertexBuffersCmd; cmd->startSlot = startSlot; cmd->count = count; Ref* cmdBuffers = mAllocator.AllocateData>(count); for (size_t i = 0; i < count; ++i) { - new(&cmdBuffers[i]) Ref(buffers[i]); + new (&cmdBuffers[i]) Ref(buffers[i]); } uint32_t* cmdOffsets = mAllocator.AllocateData(count); memcpy(cmdOffsets, offsets, count * sizeof(uint32_t)); } - void CommandBufferBuilder::TransitionBufferUsage(BufferBase* buffer, nxt::BufferUsageBit usage) { - TransitionBufferUsageCmd* cmd = mAllocator.Allocate(Command::TransitionBufferUsage); - new(cmd) TransitionBufferUsageCmd; + void CommandBufferBuilder::TransitionBufferUsage(BufferBase* buffer, + nxt::BufferUsageBit usage) { + TransitionBufferUsageCmd* cmd = + mAllocator.Allocate(Command::TransitionBufferUsage); + new (cmd) TransitionBufferUsageCmd; cmd->buffer = buffer; cmd->usage = usage; } - void CommandBufferBuilder::TransitionTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage) { - TransitionTextureUsageCmd* cmd = mAllocator.Allocate(Command::TransitionTextureUsage); - new(cmd) TransitionTextureUsageCmd; + void CommandBufferBuilder::TransitionTextureUsage(TextureBase* texture, + nxt::TextureUsageBit usage) { + TransitionTextureUsageCmd* cmd = + mAllocator.Allocate(Command::TransitionTextureUsage); + new (cmd) TransitionTextureUsageCmd; cmd->texture = texture; cmd->usage = usage; } @@ -867,4 +851,4 @@ namespace backend { } } -} +} // namespace backend diff --git a/src/backend/CommandBuffer.h b/src/backend/CommandBuffer.h index ab3ebedb81..62f477d8ff 100644 --- a/src/backend/CommandBuffer.h +++ b/src/backend/CommandBuffer.h @@ -17,8 +17,8 @@ #include "nxt/nxtcpp.h" -#include "backend/CommandAllocator.h" #include "backend/Builder.h" +#include "backend/CommandAllocator.h" #include "backend/RefCounted.h" #include @@ -39,75 +39,111 @@ namespace backend { class CommandBufferBuilder; class CommandBufferBase : public RefCounted { - public: - CommandBufferBase(CommandBufferBuilder* builder); - bool ValidateResourceUsagesImmediate(); + public: + CommandBufferBase(CommandBufferBuilder* builder); + bool ValidateResourceUsagesImmediate(); - DeviceBase* GetDevice(); + DeviceBase* GetDevice(); - private: - DeviceBase* mDevice; - std::set mBuffersTransitioned; - std::set mTexturesTransitioned; + private: + DeviceBase* mDevice; + std::set mBuffersTransitioned; + std::set mTexturesTransitioned; }; class CommandBufferBuilder : public Builder { - public: - CommandBufferBuilder(DeviceBase* device); - ~CommandBufferBuilder(); + public: + CommandBufferBuilder(DeviceBase* device); + ~CommandBufferBuilder(); - bool ValidateGetResult(); + bool ValidateGetResult(); - CommandIterator AcquireCommands(); + CommandIterator AcquireCommands(); - // NXT API - void BeginComputePass(); - void BeginRenderPass(RenderPassBase* renderPass, FramebufferBase* framebuffer); - void BeginRenderSubpass(); - void CopyBufferToBuffer(BufferBase* source, uint32_t sourceOffset, BufferBase* destination, uint32_t destinationOffset, uint32_t size); - void CopyBufferToTexture(BufferBase* buffer, uint32_t bufferOffset, uint32_t rowPitch, - TextureBase* texture, uint32_t x, uint32_t y, uint32_t z, - uint32_t width, uint32_t height, uint32_t depth, uint32_t level); - void CopyTextureToBuffer(TextureBase* texture, uint32_t x, uint32_t y, uint32_t z, - uint32_t width, uint32_t height, uint32_t depth, uint32_t level, - BufferBase* buffer, uint32_t bufferOffset, uint32_t rowPitch); - void Dispatch(uint32_t x, uint32_t y, uint32_t z); - void DrawArrays(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance); - void DrawElements(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstIndex, uint32_t firstInstance); - void EndComputePass(); - void EndRenderPass(); - void EndRenderSubpass(); - void SetPushConstants(nxt::ShaderStageBit stages, uint32_t offset, uint32_t count, const void* data); - void SetComputePipeline(ComputePipelineBase* pipeline); - void SetRenderPipeline(RenderPipelineBase* pipeline); - void SetStencilReference(uint32_t reference); - void SetBlendColor(float r, float g, float b, float a); - void SetBindGroup(uint32_t groupIndex, BindGroupBase* group); - void SetIndexBuffer(BufferBase* buffer, uint32_t offset); + // NXT API + void BeginComputePass(); + void BeginRenderPass(RenderPassBase* renderPass, FramebufferBase* framebuffer); + void BeginRenderSubpass(); + void CopyBufferToBuffer(BufferBase* source, + uint32_t sourceOffset, + BufferBase* destination, + uint32_t destinationOffset, + uint32_t size); + void CopyBufferToTexture(BufferBase* buffer, + uint32_t bufferOffset, + uint32_t rowPitch, + TextureBase* texture, + uint32_t x, + uint32_t y, + uint32_t z, + uint32_t width, + uint32_t height, + uint32_t depth, + uint32_t level); + void CopyTextureToBuffer(TextureBase* texture, + uint32_t x, + uint32_t y, + uint32_t z, + uint32_t width, + uint32_t height, + uint32_t depth, + uint32_t level, + BufferBase* buffer, + uint32_t bufferOffset, + uint32_t rowPitch); + void Dispatch(uint32_t x, uint32_t y, uint32_t z); + void DrawArrays(uint32_t vertexCount, + uint32_t instanceCount, + uint32_t firstVertex, + uint32_t firstInstance); + void DrawElements(uint32_t vertexCount, + uint32_t instanceCount, + uint32_t firstIndex, + uint32_t firstInstance); + void EndComputePass(); + void EndRenderPass(); + void EndRenderSubpass(); + void SetPushConstants(nxt::ShaderStageBit stages, + uint32_t offset, + uint32_t count, + const void* data); + void SetComputePipeline(ComputePipelineBase* pipeline); + void SetRenderPipeline(RenderPipelineBase* pipeline); + void SetStencilReference(uint32_t reference); + void SetBlendColor(float r, float g, float b, float a); + void SetBindGroup(uint32_t groupIndex, BindGroupBase* group); + void SetIndexBuffer(BufferBase* buffer, uint32_t offset); - template - void SetVertexBuffers(uint32_t startSlot, uint32_t count, T* const* buffers, uint32_t const* offsets) { - static_assert(std::is_base_of::value, ""); - SetVertexBuffers(startSlot, count, reinterpret_cast(buffers), offsets); - } - void SetVertexBuffers(uint32_t startSlot, uint32_t count, BufferBase* const* buffers, uint32_t const* offsets); + template + void SetVertexBuffers(uint32_t startSlot, + uint32_t count, + T* const* buffers, + uint32_t const* offsets) { + static_assert(std::is_base_of::value, ""); + SetVertexBuffers(startSlot, count, reinterpret_cast(buffers), + offsets); + } + void SetVertexBuffers(uint32_t startSlot, + uint32_t count, + BufferBase* const* buffers, + uint32_t const* offsets); - void TransitionBufferUsage(BufferBase* buffer, nxt::BufferUsageBit usage); - void TransitionTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage); + void TransitionBufferUsage(BufferBase* buffer, nxt::BufferUsageBit usage); + void TransitionTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage); - private: - friend class CommandBufferBase; + private: + friend class CommandBufferBase; - CommandBufferBase* GetResultImpl() override; - void MoveToIterator(); + CommandBufferBase* GetResultImpl() override; + void MoveToIterator(); - std::unique_ptr mState; - CommandAllocator mAllocator; - CommandIterator mIterator; - bool mWasMovedToIterator = false; - bool mWereCommandsAcquired = false; + std::unique_ptr mState; + CommandAllocator mAllocator; + CommandIterator mIterator; + bool mWasMovedToIterator = false; + bool mWereCommandsAcquired = false; }; -} +} // namespace backend -#endif // BACKEND_COMMANDBUFFER_H_ +#endif // BACKEND_COMMANDBUFFER_H_ diff --git a/src/backend/CommandBufferStateTracker.cpp b/src/backend/CommandBufferStateTracker.cpp index 9bbe0b6539..ca375aa281 100644 --- a/src/backend/CommandBufferStateTracker.cpp +++ b/src/backend/CommandBufferStateTracker.cpp @@ -14,11 +14,11 @@ #include "backend/CommandBufferStateTracker.h" -#include "backend/Forward.h" #include "backend/BindGroup.h" #include "backend/BindGroupLayout.h" #include "backend/Buffer.h" #include "backend/ComputePipeline.h" +#include "backend/Forward.h" #include "backend/Framebuffer.h" #include "backend/InputState.h" #include "backend/PipelineLayout.h" @@ -49,7 +49,8 @@ namespace backend { return true; } - bool CommandBufferStateTracker::ValidateCanUseBufferAs(BufferBase* buffer, nxt::BufferUsageBit usage) const { + bool CommandBufferStateTracker::ValidateCanUseBufferAs(BufferBase* buffer, + nxt::BufferUsageBit usage) const { if (!BufferHasGuaranteedUsageBit(buffer, usage)) { mBuilder->HandleError("Buffer is not in the necessary usage"); return false; @@ -57,7 +58,8 @@ namespace backend { return true; } - bool CommandBufferStateTracker::ValidateCanUseTextureAs(TextureBase* texture, nxt::TextureUsageBit usage) const { + bool CommandBufferStateTracker::ValidateCanUseTextureAs(TextureBase* texture, + nxt::TextureUsageBit usage) const { if (!TextureHasGuaranteedUsageBit(texture, usage)) { mBuilder->HandleError("Texture is not in the necessary usage"); return false; @@ -67,7 +69,7 @@ namespace backend { bool CommandBufferStateTracker::ValidateCanDispatch() { constexpr ValidationAspects requiredAspects = - 1 << VALIDATION_ASPECT_COMPUTE_PIPELINE | // implicitly requires COMPUTE_PASS + 1 << VALIDATION_ASPECT_COMPUTE_PIPELINE | // implicitly requires COMPUTE_PASS 1 << VALIDATION_ASPECT_BIND_GROUPS; if ((requiredAspects & ~mAspects).none()) { // Fast return-true path if everything is good @@ -89,9 +91,8 @@ namespace backend { bool CommandBufferStateTracker::ValidateCanDrawArrays() { // TODO(kainino@chromium.org): Check for a current render pass constexpr ValidationAspects requiredAspects = - 1 << VALIDATION_ASPECT_RENDER_PIPELINE | // implicitly requires RENDER_SUBPASS - 1 << VALIDATION_ASPECT_BIND_GROUPS | - 1 << VALIDATION_ASPECT_VERTEX_BUFFERS; + 1 << VALIDATION_ASPECT_RENDER_PIPELINE | // implicitly requires RENDER_SUBPASS + 1 << VALIDATION_ASPECT_BIND_GROUPS | 1 << VALIDATION_ASPECT_VERTEX_BUFFERS; if ((requiredAspects & ~mAspects).none()) { // Fast return-true path if everything is good return true; @@ -103,10 +104,8 @@ namespace backend { bool CommandBufferStateTracker::ValidateCanDrawElements() { // TODO(kainino@chromium.org): Check for a current render pass constexpr ValidationAspects requiredAspects = - 1 << VALIDATION_ASPECT_RENDER_PIPELINE | - 1 << VALIDATION_ASPECT_BIND_GROUPS | - 1 << VALIDATION_ASPECT_VERTEX_BUFFERS | - 1 << VALIDATION_ASPECT_INDEX_BUFFER; + 1 << VALIDATION_ASPECT_RENDER_PIPELINE | 1 << VALIDATION_ASPECT_BIND_GROUPS | + 1 << VALIDATION_ASPECT_VERTEX_BUFFERS | 1 << VALIDATION_ASPECT_INDEX_BUFFER; if ((requiredAspects & ~mAspects).none()) { // Fast return-true path if everything is good return true; @@ -134,16 +133,19 @@ namespace backend { bool CommandBufferStateTracker::ValidateSetPushConstants(nxt::ShaderStageBit stages) { if (mAspects[VALIDATION_ASPECT_COMPUTE_PASS]) { if (stages & ~nxt::ShaderStageBit::Compute) { - mBuilder->HandleError("SetPushConstants stage must be compute or 0 in compute passes"); + mBuilder->HandleError( + "SetPushConstants stage must be compute or 0 in compute passes"); return false; } } else if (mAspects[VALIDATION_ASPECT_RENDER_SUBPASS]) { if (stages & ~(nxt::ShaderStageBit::Vertex | nxt::ShaderStageBit::Fragment)) { - mBuilder->HandleError("SetPushConstants stage must be a subset if (vertex|fragment) in subpasses"); + mBuilder->HandleError( + "SetPushConstants stage must be a subset if (vertex|fragment) in subpasses"); return false; } } else { - mBuilder->HandleError("PushConstants must be set in either compute passes or subpasses"); + mBuilder->HandleError( + "PushConstants must be set in either compute passes or subpasses"); return false; } return true; @@ -224,7 +226,8 @@ namespace backend { return true; } - bool CommandBufferStateTracker::BeginRenderPass(RenderPassBase* renderPass, FramebufferBase* framebuffer) { + bool CommandBufferStateTracker::BeginRenderPass(RenderPassBase* renderPass, + FramebufferBase* framebuffer) { if (mAspects[VALIDATION_ASPECT_COMPUTE_PASS]) { mBuilder->HandleError("Cannot begin a render pass while a compute pass is active"); return false; @@ -338,7 +341,8 @@ namespace backend { return true; } - bool CommandBufferStateTracker::TransitionBufferUsage(BufferBase* buffer, nxt::BufferUsageBit usage) { + bool CommandBufferStateTracker::TransitionBufferUsage(BufferBase* buffer, + nxt::BufferUsageBit usage) { if (!buffer->IsTransitionPossible(usage)) { if (buffer->IsFrozen()) { mBuilder->HandleError("Buffer transition not possible (usage is frozen)"); @@ -355,14 +359,17 @@ namespace backend { return true; } - bool CommandBufferStateTracker::TransitionTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage) { + bool CommandBufferStateTracker::TransitionTextureUsage(TextureBase* texture, + nxt::TextureUsageBit usage) { if (!IsExplicitTextureTransitionPossible(texture, usage)) { if (texture->IsFrozen()) { mBuilder->HandleError("Texture transition not possible (usage is frozen)"); } else if (!TextureBase::IsUsagePossible(texture->GetAllowedUsage(), usage)) { mBuilder->HandleError("Texture transition not possible (usage not allowed)"); } else if (mTexturesAttached.find(texture) != mTexturesAttached.end()) { - mBuilder->HandleError("Texture transition not possible (texture is in use as a framebuffer attachment)"); + mBuilder->HandleError( + "Texture transition not possible (texture is in use as a framebuffer " + "attachment)"); } else { mBuilder->HandleError("Texture transition not possible"); } @@ -374,7 +381,8 @@ namespace backend { return true; } - bool CommandBufferStateTracker::EnsureTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage) { + bool CommandBufferStateTracker::EnsureTextureUsage(TextureBase* texture, + nxt::TextureUsageBit usage) { if (texture->HasFrozenUsage(usage)) { return true; } @@ -386,7 +394,8 @@ namespace backend { return true; } - bool CommandBufferStateTracker::BufferHasGuaranteedUsageBit(BufferBase* buffer, nxt::BufferUsageBit usage) const { + bool CommandBufferStateTracker::BufferHasGuaranteedUsageBit(BufferBase* buffer, + nxt::BufferUsageBit usage) const { ASSERT(usage != nxt::BufferUsageBit::None && nxt::HasZeroOrOneBits(usage)); if (buffer->HasFrozenUsage(usage)) { return true; @@ -395,7 +404,8 @@ namespace backend { return it != mMostRecentBufferUsages.end() && (it->second & usage); } - bool CommandBufferStateTracker::TextureHasGuaranteedUsageBit(TextureBase* texture, nxt::TextureUsageBit usage) const { + bool CommandBufferStateTracker::TextureHasGuaranteedUsageBit(TextureBase* texture, + nxt::TextureUsageBit usage) const { ASSERT(usage != nxt::TextureUsageBit::None && nxt::HasZeroOrOneBits(usage)); if (texture->HasFrozenUsage(usage)) { return true; @@ -404,7 +414,9 @@ namespace backend { return it != mMostRecentTextureUsages.end() && (it->second & usage); } - bool CommandBufferStateTracker::IsInternalTextureTransitionPossible(TextureBase* texture, nxt::TextureUsageBit usage) const { + bool CommandBufferStateTracker::IsInternalTextureTransitionPossible( + TextureBase* texture, + nxt::TextureUsageBit usage) const { ASSERT(usage != nxt::TextureUsageBit::None && nxt::HasZeroOrOneBits(usage)); if (mTexturesAttached.find(texture) != mTexturesAttached.end()) { return false; @@ -412,9 +424,10 @@ namespace backend { return texture->IsTransitionPossible(usage); } - bool CommandBufferStateTracker::IsExplicitTextureTransitionPossible(TextureBase* texture, nxt::TextureUsageBit usage) const { - const nxt::TextureUsageBit attachmentUsages = - nxt::TextureUsageBit::OutputAttachment; + bool CommandBufferStateTracker::IsExplicitTextureTransitionPossible( + TextureBase* texture, + nxt::TextureUsageBit usage) const { + const nxt::TextureUsageBit attachmentUsages = nxt::TextureUsageBit::OutputAttachment; if (usage & attachmentUsages) { return false; } @@ -456,8 +469,7 @@ namespace backend { bool CommandBufferStateTracker::HavePipeline() const { constexpr ValidationAspects pipelineAspects = - 1 << VALIDATION_ASPECT_COMPUTE_PIPELINE | - 1 << VALIDATION_ASPECT_RENDER_PIPELINE; + 1 << VALIDATION_ASPECT_COMPUTE_PIPELINE | 1 << VALIDATION_ASPECT_RENDER_PIPELINE; return (mAspects & pipelineAspects).any(); } @@ -471,40 +483,36 @@ namespace backend { nxt::BindingType type = layoutInfo.types[i]; switch (type) { case nxt::BindingType::UniformBuffer: - case nxt::BindingType::StorageBuffer: - { - nxt::BufferUsageBit requiredUsage = nxt::BufferUsageBit::None; - switch (type) { - case nxt::BindingType::UniformBuffer: - requiredUsage = nxt::BufferUsageBit::Uniform; - break; + case nxt::BindingType::StorageBuffer: { + nxt::BufferUsageBit requiredUsage = nxt::BufferUsageBit::None; + switch (type) { + case nxt::BindingType::UniformBuffer: + requiredUsage = nxt::BufferUsageBit::Uniform; + break; - case nxt::BindingType::StorageBuffer: - requiredUsage = nxt::BufferUsageBit::Storage; - break; + case nxt::BindingType::StorageBuffer: + requiredUsage = nxt::BufferUsageBit::Storage; + break; - default: - UNREACHABLE(); - } - - auto buffer = group->GetBindingAsBufferView(i)->GetBuffer(); - if (!BufferHasGuaranteedUsageBit(buffer, requiredUsage)) { - mBuilder->HandleError("Can't guarantee buffer usage needed by bind group"); - return false; - } + default: + UNREACHABLE(); } - break; - case nxt::BindingType::SampledTexture: - { - auto requiredUsage = nxt::TextureUsageBit::Sampled; - auto texture = group->GetBindingAsTextureView(i)->GetTexture(); - if (!TextureHasGuaranteedUsageBit(texture, requiredUsage)) { - mBuilder->HandleError("Can't guarantee texture usage needed by bind group"); - return false; - } + auto buffer = group->GetBindingAsBufferView(i)->GetBuffer(); + if (!BufferHasGuaranteedUsageBit(buffer, requiredUsage)) { + mBuilder->HandleError("Can't guarantee buffer usage needed by bind group"); + return false; } - break; + } break; + case nxt::BindingType::SampledTexture: { + auto requiredUsage = nxt::TextureUsageBit::Sampled; + + auto texture = group->GetBindingAsTextureView(i)->GetTexture(); + if (!TextureHasGuaranteedUsageBit(texture, requiredUsage)) { + mBuilder->HandleError("Can't guarantee texture usage needed by bind group"); + return false; + } + } break; case nxt::BindingType::Sampler: continue; } @@ -547,12 +555,10 @@ namespace backend { void CommandBufferStateTracker::UnsetPipeline() { constexpr ValidationAspects pipelineDependentAspects = - 1 << VALIDATION_ASPECT_RENDER_PIPELINE | - 1 << VALIDATION_ASPECT_COMPUTE_PIPELINE | - 1 << VALIDATION_ASPECT_BIND_GROUPS | - 1 << VALIDATION_ASPECT_VERTEX_BUFFERS | + 1 << VALIDATION_ASPECT_RENDER_PIPELINE | 1 << VALIDATION_ASPECT_COMPUTE_PIPELINE | + 1 << VALIDATION_ASPECT_BIND_GROUPS | 1 << VALIDATION_ASPECT_VERTEX_BUFFERS | 1 << VALIDATION_ASPECT_INDEX_BUFFER; mAspects &= ~pipelineDependentAspects; mBindgroups.fill(nullptr); } -} +} // namespace backend diff --git a/src/backend/CommandBufferStateTracker.h b/src/backend/CommandBufferStateTracker.h index fabcf871ed..584026e0d1 100644 --- a/src/backend/CommandBufferStateTracker.h +++ b/src/backend/CommandBufferStateTracker.h @@ -25,92 +25,94 @@ namespace backend { class CommandBufferStateTracker { - public: - explicit CommandBufferStateTracker(CommandBufferBuilder* builder); + public: + explicit CommandBufferStateTracker(CommandBufferBuilder* builder); - // Non-state-modifying validation functions - bool HaveRenderPass() const; - bool HaveRenderSubpass() const; - bool ValidateCanCopy() const; - bool ValidateCanUseBufferAs(BufferBase* buffer, nxt::BufferUsageBit usage) const; - bool ValidateCanUseTextureAs(TextureBase* texture, nxt::TextureUsageBit usage) const; - bool ValidateCanDispatch(); - bool ValidateCanDrawArrays(); - bool ValidateCanDrawElements(); - bool ValidateEndCommandBuffer() const; - bool ValidateSetPushConstants(nxt::ShaderStageBit stages); + // Non-state-modifying validation functions + bool HaveRenderPass() const; + bool HaveRenderSubpass() const; + bool ValidateCanCopy() const; + bool ValidateCanUseBufferAs(BufferBase* buffer, nxt::BufferUsageBit usage) const; + bool ValidateCanUseTextureAs(TextureBase* texture, nxt::TextureUsageBit usage) const; + bool ValidateCanDispatch(); + bool ValidateCanDrawArrays(); + bool ValidateCanDrawElements(); + bool ValidateEndCommandBuffer() const; + bool ValidateSetPushConstants(nxt::ShaderStageBit stages); - // State-modifying methods - bool BeginComputePass(); - bool EndComputePass(); - bool BeginSubpass(); - bool EndSubpass(); - bool BeginRenderPass(RenderPassBase* renderPass, FramebufferBase* framebuffer); - bool EndRenderPass(); - bool SetComputePipeline(ComputePipelineBase* pipeline); - bool SetRenderPipeline(RenderPipelineBase* pipeline); - bool SetBindGroup(uint32_t index, BindGroupBase* bindgroup); - bool SetIndexBuffer(BufferBase* buffer); - bool SetVertexBuffer(uint32_t index, BufferBase* buffer); - bool TransitionBufferUsage(BufferBase* buffer, nxt::BufferUsageBit usage); - bool TransitionTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage); - bool EnsureTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage); + // State-modifying methods + bool BeginComputePass(); + bool EndComputePass(); + bool BeginSubpass(); + bool EndSubpass(); + bool BeginRenderPass(RenderPassBase* renderPass, FramebufferBase* framebuffer); + bool EndRenderPass(); + bool SetComputePipeline(ComputePipelineBase* pipeline); + bool SetRenderPipeline(RenderPipelineBase* pipeline); + bool SetBindGroup(uint32_t index, BindGroupBase* bindgroup); + bool SetIndexBuffer(BufferBase* buffer); + bool SetVertexBuffer(uint32_t index, BufferBase* buffer); + bool TransitionBufferUsage(BufferBase* buffer, nxt::BufferUsageBit usage); + bool TransitionTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage); + bool EnsureTextureUsage(TextureBase* texture, nxt::TextureUsageBit usage); - // These collections are copied to the CommandBuffer at build time. - // These pointers will remain valid since they are referenced by - // the bind groups which are referenced by this command buffer. - std::set mBuffersTransitioned; - std::set mTexturesTransitioned; - std::set mTexturesAttached; + // These collections are copied to the CommandBuffer at build time. These pointers will + // remain valid since they are referenced by the bind groups which are referenced by this + // command buffer. + std::set mBuffersTransitioned; + std::set mTexturesTransitioned; + std::set mTexturesAttached; - private: - enum ValidationAspect { - VALIDATION_ASPECT_RENDER_PIPELINE, - VALIDATION_ASPECT_COMPUTE_PIPELINE, - VALIDATION_ASPECT_BIND_GROUPS, - VALIDATION_ASPECT_VERTEX_BUFFERS, - VALIDATION_ASPECT_INDEX_BUFFER, - VALIDATION_ASPECT_RENDER_SUBPASS, - VALIDATION_ASPECT_COMPUTE_PASS, + private: + enum ValidationAspect { + VALIDATION_ASPECT_RENDER_PIPELINE, + VALIDATION_ASPECT_COMPUTE_PIPELINE, + VALIDATION_ASPECT_BIND_GROUPS, + VALIDATION_ASPECT_VERTEX_BUFFERS, + VALIDATION_ASPECT_INDEX_BUFFER, + VALIDATION_ASPECT_RENDER_SUBPASS, + VALIDATION_ASPECT_COMPUTE_PASS, - VALIDATION_ASPECT_COUNT - }; - using ValidationAspects = std::bitset; + VALIDATION_ASPECT_COUNT + }; + using ValidationAspects = std::bitset; - // Usage helper functions - bool BufferHasGuaranteedUsageBit(BufferBase* buffer, nxt::BufferUsageBit usage) const; - bool TextureHasGuaranteedUsageBit(TextureBase* texture, nxt::TextureUsageBit usage) const; - bool IsInternalTextureTransitionPossible(TextureBase* texture, nxt::TextureUsageBit usage) const; - bool IsExplicitTextureTransitionPossible(TextureBase* texture, nxt::TextureUsageBit usage) const; + // Usage helper functions + bool BufferHasGuaranteedUsageBit(BufferBase* buffer, nxt::BufferUsageBit usage) const; + bool TextureHasGuaranteedUsageBit(TextureBase* texture, nxt::TextureUsageBit usage) const; + bool IsInternalTextureTransitionPossible(TextureBase* texture, + nxt::TextureUsageBit usage) const; + bool IsExplicitTextureTransitionPossible(TextureBase* texture, + nxt::TextureUsageBit usage) const; - // Queries for lazily evaluated aspects - bool RecomputeHaveAspectBindGroups(); - bool RecomputeHaveAspectVertexBuffers(); + // Queries for lazily evaluated aspects + bool RecomputeHaveAspectBindGroups(); + bool RecomputeHaveAspectVertexBuffers(); - bool HavePipeline() const; - bool ValidateBindGroupUsages(BindGroupBase* group) const; - bool RevalidateCanDraw(); + bool HavePipeline() const; + bool ValidateBindGroupUsages(BindGroupBase* group) const; + bool RevalidateCanDraw(); - void SetPipelineCommon(PipelineBase* pipeline); - void UnsetPipeline(); + void SetPipelineCommon(PipelineBase* pipeline); + void UnsetPipeline(); - CommandBufferBuilder* mBuilder; + CommandBufferBuilder* mBuilder; - ValidationAspects mAspects; + ValidationAspects mAspects; - std::bitset mBindgroupsSet; - std::array mBindgroups = {}; - std::bitset mInputsSet; - PipelineBase* mLastPipeline = nullptr; - RenderPipelineBase* mLastRenderPipeline = nullptr; + std::bitset mBindgroupsSet; + std::array mBindgroups = {}; + std::bitset mInputsSet; + PipelineBase* mLastPipeline = nullptr; + RenderPipelineBase* mLastRenderPipeline = nullptr; - std::map mMostRecentBufferUsages; - std::map mMostRecentTextureUsages; + std::map mMostRecentBufferUsages; + std::map mMostRecentTextureUsages; - RenderPassBase* mCurrentRenderPass = nullptr; - FramebufferBase* mCurrentFramebuffer = nullptr; - uint32_t mCurrentSubpass = 0; + RenderPassBase* mCurrentRenderPass = nullptr; + FramebufferBase* mCurrentFramebuffer = nullptr; + uint32_t mCurrentSubpass = 0; }; -} +} // namespace backend -#endif // BACKEND_COMMANDBUFFERSTATETRACKER_H +#endif // BACKEND_COMMANDBUFFERSTATETRACKER_H diff --git a/src/backend/Commands.h b/src/backend/Commands.h index 607ee0834c..18a7d6e2ea 100644 --- a/src/backend/Commands.h +++ b/src/backend/Commands.h @@ -52,16 +52,14 @@ namespace backend { TransitionTextureUsage, }; - struct BeginComputePassCmd { - }; + struct BeginComputePassCmd {}; struct BeginRenderPassCmd { Ref renderPass; Ref framebuffer; }; - struct BeginRenderSubpassCmd { - }; + struct BeginRenderSubpassCmd {}; struct BufferCopyLocation { Ref buffer; @@ -113,14 +111,11 @@ namespace backend { uint32_t firstInstance; }; - struct EndComputePassCmd { - }; + struct EndComputePassCmd {}; - struct EndRenderPassCmd { - }; + struct EndRenderPassCmd {}; - struct EndRenderSubpassCmd { - }; + struct EndRenderSubpassCmd {}; struct SetComputePipelineCmd { Ref pipeline; @@ -176,6 +171,6 @@ namespace backend { void FreeCommands(CommandIterator* commands); void SkipCommand(CommandIterator* commands, Command type); -} +} // namespace backend -#endif // BACKEND_COMMANDS_H_ +#endif // BACKEND_COMMANDS_H_ diff --git a/src/backend/ComputePipeline.cpp b/src/backend/ComputePipeline.cpp index 1a2f28f932..e6e466b0b7 100644 --- a/src/backend/ComputePipeline.cpp +++ b/src/backend/ComputePipeline.cpp @@ -38,4 +38,4 @@ namespace backend { return mDevice->CreateComputePipeline(this); } -} +} // namespace backend diff --git a/src/backend/ComputePipeline.h b/src/backend/ComputePipeline.h index 92a7947317..0f55d726bc 100644 --- a/src/backend/ComputePipeline.h +++ b/src/backend/ComputePipeline.h @@ -20,19 +20,18 @@ namespace backend { class ComputePipelineBase : public RefCounted, public PipelineBase { - public: - ComputePipelineBase(ComputePipelineBuilder* builder); - + public: + ComputePipelineBase(ComputePipelineBuilder* builder); }; class ComputePipelineBuilder : public Builder, public PipelineBuilder { - public: - ComputePipelineBuilder(DeviceBase* device); + public: + ComputePipelineBuilder(DeviceBase* device); - private: - ComputePipelineBase* GetResultImpl() override; + private: + ComputePipelineBase* GetResultImpl() override; }; -} +} // namespace backend -#endif // BACKEND_COMPUTEPIPELINE_H_ +#endif // BACKEND_COMPUTEPIPELINE_H_ diff --git a/src/backend/DepthStencilState.cpp b/src/backend/DepthStencilState.cpp index 509037cec8..1052446265 100644 --- a/src/backend/DepthStencilState.cpp +++ b/src/backend/DepthStencilState.cpp @@ -26,13 +26,13 @@ namespace backend { bool DepthStencilStateBase::StencilTestEnabled() const { return mStencilInfo.back.compareFunction != nxt::CompareFunction::Always || - mStencilInfo.back.stencilFail != nxt::StencilOperation::Keep || - mStencilInfo.back.depthFail != nxt::StencilOperation::Keep || - mStencilInfo.back.depthStencilPass != nxt::StencilOperation::Keep || - mStencilInfo.front.compareFunction != nxt::CompareFunction::Always || - mStencilInfo.front.stencilFail != nxt::StencilOperation::Keep || - mStencilInfo.front.depthFail != nxt::StencilOperation::Keep || - mStencilInfo.front.depthStencilPass != nxt::StencilOperation::Keep; + mStencilInfo.back.stencilFail != nxt::StencilOperation::Keep || + mStencilInfo.back.depthFail != nxt::StencilOperation::Keep || + mStencilInfo.back.depthStencilPass != nxt::StencilOperation::Keep || + mStencilInfo.front.compareFunction != nxt::CompareFunction::Always || + mStencilInfo.front.stencilFail != nxt::StencilOperation::Keep || + mStencilInfo.front.depthFail != nxt::StencilOperation::Keep || + mStencilInfo.front.depthStencilPass != nxt::StencilOperation::Keep; } const DepthStencilStateBase::DepthInfo& DepthStencilStateBase::GetDepth() const { @@ -60,7 +60,8 @@ namespace backend { return mDevice->CreateDepthStencilState(this); } - void DepthStencilStateBuilder::SetDepthCompareFunction(nxt::CompareFunction depthCompareFunction) { + void DepthStencilStateBuilder::SetDepthCompareFunction( + nxt::CompareFunction depthCompareFunction) { if ((mPropertiesSet & DEPTH_STENCIL_STATE_PROPERTY_DEPTH_COMPARE_FUNCTION) != 0) { HandleError("Depth compare property set multiple times"); return; @@ -82,8 +83,11 @@ namespace backend { mDepthInfo.depthWriteEnabled = enabled; } - void DepthStencilStateBuilder::SetStencilFunction(nxt::Face face, nxt::CompareFunction stencilCompareFunction, - nxt::StencilOperation stencilFail, nxt::StencilOperation depthFail, nxt::StencilOperation depthStencilPass) {\ + void DepthStencilStateBuilder::SetStencilFunction(nxt::Face face, + nxt::CompareFunction stencilCompareFunction, + nxt::StencilOperation stencilFail, + nxt::StencilOperation depthFail, + nxt::StencilOperation depthStencilPass) { if (face == nxt::Face::None) { HandleError("Can't set stencil function of None face"); return; @@ -118,7 +122,7 @@ namespace backend { } void DepthStencilStateBuilder::SetStencilMask(uint32_t readMask, uint32_t writeMask) { - if ((mPropertiesSet & DEPTH_STENCIL_STATE_PROPERTY_STENCIL_MASK) != 0) { + if ((mPropertiesSet & DEPTH_STENCIL_STATE_PROPERTY_STENCIL_MASK) != 0) { HandleError("Stencilmask property set multiple times"); return; } @@ -128,4 +132,4 @@ namespace backend { mStencilInfo.writeMask = writeMask; } -} +} // namespace backend diff --git a/src/backend/DepthStencilState.h b/src/backend/DepthStencilState.h index cce5e72c32..33d08b6a7f 100644 --- a/src/backend/DepthStencilState.h +++ b/src/backend/DepthStencilState.h @@ -15,69 +15,71 @@ #ifndef BACKEND_DEPTHSTENCILSTATE_H_ #define BACKEND_DEPTHSTENCILSTATE_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "nxt/nxtcpp.h" namespace backend { - class DepthStencilStateBase : public RefCounted { - public: - DepthStencilStateBase(DepthStencilStateBuilder* builder); + public: + DepthStencilStateBase(DepthStencilStateBuilder* builder); - struct DepthInfo { - nxt::CompareFunction compareFunction = nxt::CompareFunction::Always; - bool depthWriteEnabled = false; - }; + struct DepthInfo { + nxt::CompareFunction compareFunction = nxt::CompareFunction::Always; + bool depthWriteEnabled = false; + }; - struct StencilFaceInfo { - nxt::CompareFunction compareFunction = nxt::CompareFunction::Always; - nxt::StencilOperation stencilFail = nxt::StencilOperation::Keep; - nxt::StencilOperation depthFail = nxt::StencilOperation::Keep; - nxt::StencilOperation depthStencilPass = nxt::StencilOperation::Keep; - }; + struct StencilFaceInfo { + nxt::CompareFunction compareFunction = nxt::CompareFunction::Always; + nxt::StencilOperation stencilFail = nxt::StencilOperation::Keep; + nxt::StencilOperation depthFail = nxt::StencilOperation::Keep; + nxt::StencilOperation depthStencilPass = nxt::StencilOperation::Keep; + }; - struct StencilInfo { - StencilFaceInfo back; - StencilFaceInfo front; - uint32_t readMask = 0xff; - uint32_t writeMask = 0xff; - }; + struct StencilInfo { + StencilFaceInfo back; + StencilFaceInfo front; + uint32_t readMask = 0xff; + uint32_t writeMask = 0xff; + }; - bool StencilTestEnabled() const; - const DepthInfo& GetDepth() const; - const StencilInfo& GetStencil() const; + bool StencilTestEnabled() const; + const DepthInfo& GetDepth() const; + const StencilInfo& GetStencil() const; - private: - DepthInfo mDepthInfo; - StencilInfo mStencilInfo; + private: + DepthInfo mDepthInfo; + StencilInfo mStencilInfo; }; class DepthStencilStateBuilder : public Builder { - public: - DepthStencilStateBuilder(DeviceBase* device); + public: + DepthStencilStateBuilder(DeviceBase* device); - // NXT API - void SetDepthCompareFunction(nxt::CompareFunction depthCompareFunction); - void SetDepthWriteEnabled(bool enabled); - void SetStencilFunction(nxt::Face face, nxt::CompareFunction stencilCompareFunction, - nxt::StencilOperation stencilFail, nxt::StencilOperation depthFail, nxt::StencilOperation depthStencilPass); - void SetStencilMask(uint32_t readMask, uint32_t writeMask); + // NXT API + void SetDepthCompareFunction(nxt::CompareFunction depthCompareFunction); + void SetDepthWriteEnabled(bool enabled); + void SetStencilFunction(nxt::Face face, + nxt::CompareFunction stencilCompareFunction, + nxt::StencilOperation stencilFail, + nxt::StencilOperation depthFail, + nxt::StencilOperation depthStencilPass); + void SetStencilMask(uint32_t readMask, uint32_t writeMask); - private: - friend class DepthStencilStateBase; + private: + friend class DepthStencilStateBase; - DepthStencilStateBase* GetResultImpl() override; + DepthStencilStateBase* GetResultImpl() override; - int mPropertiesSet = 0; + int mPropertiesSet = 0; - DepthStencilStateBase::DepthInfo mDepthInfo; - DepthStencilStateBase::StencilInfo mStencilInfo; + DepthStencilStateBase::DepthInfo mDepthInfo; + DepthStencilStateBase::StencilInfo mStencilInfo; }; -} +} // namespace backend -#endif // BACKEND_DEPTHSTENCILSTATE_H_ +#endif // BACKEND_DEPTHSTENCILSTATE_H_ diff --git a/src/backend/Device.cpp b/src/backend/Device.cpp index 20d26b69d7..8bce2e6ed4 100644 --- a/src/backend/Device.cpp +++ b/src/backend/Device.cpp @@ -40,7 +40,8 @@ namespace backend { // The caches are unordered_sets of pointers with special hash and compare functions // to compare the value of the objects, instead of the pointers. - using BindGroupLayoutCache = std::unordered_set; + using BindGroupLayoutCache = std:: + unordered_set; struct DeviceBase::Caches { BindGroupLayoutCache bindGroupLayouts; @@ -62,7 +63,8 @@ namespace backend { } } - void DeviceBase::SetErrorCallback(nxt::DeviceErrorCallback callback, nxt::CallbackUserdata userdata) { + void DeviceBase::SetErrorCallback(nxt::DeviceErrorCallback callback, + nxt::CallbackUserdata userdata) { mErrorCallback = callback; mErrorUserdata = userdata; } @@ -71,7 +73,9 @@ namespace backend { return this; } - BindGroupLayoutBase* DeviceBase::GetOrCreateBindGroupLayout(const BindGroupLayoutBase* blueprint, BindGroupLayoutBuilder* builder) { + BindGroupLayoutBase* DeviceBase::GetOrCreateBindGroupLayout( + const BindGroupLayoutBase* blueprint, + BindGroupLayoutBuilder* builder) { // The blueprint is only used to search in the cache and is not modified. However cached // objects can be modified, and unordered_set cannot search for a const pointer in a non // const pointer set. That's why we do a const_cast here, but the blueprint won't be @@ -159,4 +163,4 @@ namespace backend { } } -} +} // namespace backend diff --git a/src/backend/Device.h b/src/backend/Device.h index 5608e2f426..6b7e37d7ad 100644 --- a/src/backend/Device.h +++ b/src/backend/Device.h @@ -25,89 +25,91 @@ namespace backend { using ErrorCallback = void (*)(const char* errorMessage, void* userData); class DeviceBase { - public: - DeviceBase(); - virtual ~DeviceBase(); + public: + DeviceBase(); + virtual ~DeviceBase(); - void HandleError(const char* message); + void HandleError(const char* message); - // Used by autogenerated code, returns itself - DeviceBase* GetDevice(); + // Used by autogenerated code, returns itself + DeviceBase* GetDevice(); - virtual BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) = 0; - virtual BindGroupLayoutBase* CreateBindGroupLayout(BindGroupLayoutBuilder* builder) = 0; - virtual BlendStateBase* CreateBlendState(BlendStateBuilder* builder) = 0; - virtual BufferBase* CreateBuffer(BufferBuilder* builder) = 0; - virtual BufferViewBase* CreateBufferView(BufferViewBuilder* builder) = 0; - virtual CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) = 0; - virtual ComputePipelineBase* CreateComputePipeline(ComputePipelineBuilder* builder) = 0; - virtual DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) = 0; - virtual FramebufferBase* CreateFramebuffer(FramebufferBuilder* builder) = 0; - virtual InputStateBase* CreateInputState(InputStateBuilder* builder) = 0; - virtual PipelineLayoutBase* CreatePipelineLayout(PipelineLayoutBuilder* builder) = 0; - virtual QueueBase* CreateQueue(QueueBuilder* builder) = 0; - virtual RenderPassBase* CreateRenderPass(RenderPassBuilder* builder) = 0; - virtual RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) = 0; - virtual SamplerBase* CreateSampler(SamplerBuilder* builder) = 0; - virtual ShaderModuleBase* CreateShaderModule(ShaderModuleBuilder* builder) = 0; - virtual SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) = 0; - virtual TextureBase* CreateTexture(TextureBuilder* builder) = 0; - virtual TextureViewBase* CreateTextureView(TextureViewBuilder* builder) = 0; + virtual BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) = 0; + virtual BindGroupLayoutBase* CreateBindGroupLayout(BindGroupLayoutBuilder* builder) = 0; + virtual BlendStateBase* CreateBlendState(BlendStateBuilder* builder) = 0; + virtual BufferBase* CreateBuffer(BufferBuilder* builder) = 0; + virtual BufferViewBase* CreateBufferView(BufferViewBuilder* builder) = 0; + virtual CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) = 0; + virtual ComputePipelineBase* CreateComputePipeline(ComputePipelineBuilder* builder) = 0; + virtual DepthStencilStateBase* CreateDepthStencilState( + DepthStencilStateBuilder* builder) = 0; + virtual FramebufferBase* CreateFramebuffer(FramebufferBuilder* builder) = 0; + virtual InputStateBase* CreateInputState(InputStateBuilder* builder) = 0; + virtual PipelineLayoutBase* CreatePipelineLayout(PipelineLayoutBuilder* builder) = 0; + virtual QueueBase* CreateQueue(QueueBuilder* builder) = 0; + virtual RenderPassBase* CreateRenderPass(RenderPassBuilder* builder) = 0; + virtual RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) = 0; + virtual SamplerBase* CreateSampler(SamplerBuilder* builder) = 0; + virtual ShaderModuleBase* CreateShaderModule(ShaderModuleBuilder* builder) = 0; + virtual SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) = 0; + virtual TextureBase* CreateTexture(TextureBuilder* builder) = 0; + virtual TextureViewBase* CreateTextureView(TextureViewBuilder* builder) = 0; - virtual void TickImpl() = 0; + virtual void TickImpl() = 0; - // Many NXT objects are completely immutable once created which means that if two - // builders are given the same arguments, they can return the same object. Reusing - // objects will help make comparisons between objects by a single pointer comparison. - // - // Technically no object is immutable as they have a reference count, and an - // application with reference-counting issues could "see" that objects are reused. - // This is solved by automatic-reference counting, and also the fact that when using - // the client-server wire every creation will get a different proxy object, with a - // different reference count. - // - // When trying to create an object, we give both the builder and an example of what - // the built object will be, the "blueprint". The blueprint is just a FooBase object - // instead of a backend Foo object. If the blueprint doesn't match an object in the - // cache, then the builder is used to make a new object. - BindGroupLayoutBase* GetOrCreateBindGroupLayout(const BindGroupLayoutBase* blueprint, BindGroupLayoutBuilder* builder); - void UncacheBindGroupLayout(BindGroupLayoutBase* obj); + // Many NXT objects are completely immutable once created which means that if two + // builders are given the same arguments, they can return the same object. Reusing + // objects will help make comparisons between objects by a single pointer comparison. + // + // Technically no object is immutable as they have a reference count, and an + // application with reference-counting issues could "see" that objects are reused. + // This is solved by automatic-reference counting, and also the fact that when using + // the client-server wire every creation will get a different proxy object, with a + // different reference count. + // + // When trying to create an object, we give both the builder and an example of what + // the built object will be, the "blueprint". The blueprint is just a FooBase object + // instead of a backend Foo object. If the blueprint doesn't match an object in the + // cache, then the builder is used to make a new object. + BindGroupLayoutBase* GetOrCreateBindGroupLayout(const BindGroupLayoutBase* blueprint, + BindGroupLayoutBuilder* builder); + void UncacheBindGroupLayout(BindGroupLayoutBase* obj); - // NXT API - BindGroupBuilder* CreateBindGroupBuilder(); - BindGroupLayoutBuilder* CreateBindGroupLayoutBuilder(); - BlendStateBuilder* CreateBlendStateBuilder(); - BufferBuilder* CreateBufferBuilder(); - CommandBufferBuilder* CreateCommandBufferBuilder(); - ComputePipelineBuilder* CreateComputePipelineBuilder(); - DepthStencilStateBuilder* CreateDepthStencilStateBuilder(); - FramebufferBuilder* CreateFramebufferBuilder(); - InputStateBuilder* CreateInputStateBuilder(); - PipelineLayoutBuilder* CreatePipelineLayoutBuilder(); - QueueBuilder* CreateQueueBuilder(); - RenderPassBuilder* CreateRenderPassBuilder(); - RenderPipelineBuilder* CreateRenderPipelineBuilder(); - SamplerBuilder* CreateSamplerBuilder(); - ShaderModuleBuilder* CreateShaderModuleBuilder(); - SwapChainBuilder* CreateSwapChainBuilder(); - TextureBuilder* CreateTextureBuilder(); + // NXT API + BindGroupBuilder* CreateBindGroupBuilder(); + BindGroupLayoutBuilder* CreateBindGroupLayoutBuilder(); + BlendStateBuilder* CreateBlendStateBuilder(); + BufferBuilder* CreateBufferBuilder(); + CommandBufferBuilder* CreateCommandBufferBuilder(); + ComputePipelineBuilder* CreateComputePipelineBuilder(); + DepthStencilStateBuilder* CreateDepthStencilStateBuilder(); + FramebufferBuilder* CreateFramebufferBuilder(); + InputStateBuilder* CreateInputStateBuilder(); + PipelineLayoutBuilder* CreatePipelineLayoutBuilder(); + QueueBuilder* CreateQueueBuilder(); + RenderPassBuilder* CreateRenderPassBuilder(); + RenderPipelineBuilder* CreateRenderPipelineBuilder(); + SamplerBuilder* CreateSamplerBuilder(); + ShaderModuleBuilder* CreateShaderModuleBuilder(); + SwapChainBuilder* CreateSwapChainBuilder(); + TextureBuilder* CreateTextureBuilder(); - void Tick(); - void SetErrorCallback(nxt::DeviceErrorCallback callback, nxt::CallbackUserdata userdata); - void Reference(); - void Release(); + void Tick(); + void SetErrorCallback(nxt::DeviceErrorCallback callback, nxt::CallbackUserdata userdata); + void Reference(); + void Release(); - private: - // The object caches aren't exposed in the header as they would require a lot of - // additional includes. - struct Caches; - Caches* mCaches = nullptr; + private: + // The object caches aren't exposed in the header as they would require a lot of + // additional includes. + struct Caches; + Caches* mCaches = nullptr; - nxt::DeviceErrorCallback mErrorCallback = nullptr; - nxt::CallbackUserdata mErrorUserdata = 0; - uint32_t mRefCount = 1; + nxt::DeviceErrorCallback mErrorCallback = nullptr; + nxt::CallbackUserdata mErrorUserdata = 0; + uint32_t mRefCount = 1; }; -} +} // namespace backend -#endif // BACKEND_DEVICEBASE_H_ +#endif // BACKEND_DEVICEBASE_H_ diff --git a/src/backend/Forward.h b/src/backend/Forward.h index 134f93ff2b..7c9cd67df1 100644 --- a/src/backend/Forward.h +++ b/src/backend/Forward.h @@ -60,13 +60,13 @@ namespace backend { class DeviceBase; - template + template class Ref; - template + template class PerStage; enum PushConstantType : uint8_t; -} +} // namespace backend -#endif // BACKEND_FORWARD_H_ +#endif // BACKEND_FORWARD_H_ diff --git a/src/backend/Framebuffer.cpp b/src/backend/Framebuffer.cpp index b60f228147..53383924bf 100644 --- a/src/backend/Framebuffer.cpp +++ b/src/backend/Framebuffer.cpp @@ -25,9 +25,13 @@ namespace backend { // Framebuffer FramebufferBase::FramebufferBase(FramebufferBuilder* builder) - : mDevice(builder->mDevice), mRenderPass(std::move(builder->mRenderPass)), - mWidth(builder->mWidth), mHeight(builder->mHeight), mTextureViews(std::move(builder->mTextureViews)), - mClearColors(mTextureViews.size()), mClearDepthStencils(mTextureViews.size()) { + : mDevice(builder->mDevice), + mRenderPass(std::move(builder->mRenderPass)), + mWidth(builder->mWidth), + mHeight(builder->mHeight), + mTextureViews(std::move(builder->mTextureViews)), + mClearColors(mTextureViews.size()), + mClearDepthStencils(mTextureViews.size()) { } DeviceBase* FramebufferBase::GetDevice() { @@ -48,7 +52,8 @@ namespace backend { return mClearColors[attachmentSlot]; } - FramebufferBase::ClearDepthStencil FramebufferBase::GetClearDepthStencil(uint32_t attachmentSlot) { + FramebufferBase::ClearDepthStencil FramebufferBase::GetClearDepthStencil( + uint32_t attachmentSlot) { ASSERT(attachmentSlot < mClearDepthStencils.size()); return mClearDepthStencils[attachmentSlot]; } @@ -61,7 +66,11 @@ namespace backend { return mHeight; } - void FramebufferBase::AttachmentSetClearColor(uint32_t attachmentSlot, float clearR, float clearG, float clearB, float clearA) { + void FramebufferBase::AttachmentSetClearColor(uint32_t attachmentSlot, + float clearR, + float clearG, + float clearB, + float clearA) { if (attachmentSlot >= mRenderPass->GetAttachmentCount()) { mDevice->HandleError("Framebuffer attachment out of bounds"); return; @@ -74,7 +83,9 @@ namespace backend { c.color[3] = clearA; } - void FramebufferBase::AttachmentSetClearDepthStencil(uint32_t attachmentSlot, float clearDepth, uint32_t clearStencil) { + void FramebufferBase::AttachmentSetClearDepthStencil(uint32_t attachmentSlot, + float clearDepth, + uint32_t clearStencil) { if (attachmentSlot >= mRenderPass->GetAttachmentCount()) { mDevice->HandleError("Framebuffer attachment out of bounds"); return; @@ -92,12 +103,12 @@ namespace backend { FRAMEBUFFER_PROPERTY_DIMENSIONS = 0x2, }; - FramebufferBuilder::FramebufferBuilder(DeviceBase* device) - : Builder(device) { + FramebufferBuilder::FramebufferBuilder(DeviceBase* device) : Builder(device) { } FramebufferBase* FramebufferBuilder::GetResultImpl() { - constexpr int requiredProperties = FRAMEBUFFER_PROPERTY_RENDER_PASS | FRAMEBUFFER_PROPERTY_DIMENSIONS; + constexpr int requiredProperties = + FRAMEBUFFER_PROPERTY_RENDER_PASS | FRAMEBUFFER_PROPERTY_DIMENSIONS; if ((mPropertiesSet & requiredProperties) != requiredProperties) { HandleError("Framebuffer missing properties"); return nullptr; @@ -170,4 +181,4 @@ namespace backend { mTextureViews[attachmentSlot] = textureView; } -} +} // namespace backend diff --git a/src/backend/Framebuffer.h b/src/backend/Framebuffer.h index 60ff577ae9..2c858002a4 100644 --- a/src/backend/Framebuffer.h +++ b/src/backend/Framebuffer.h @@ -27,60 +27,66 @@ namespace backend { class FramebufferBase : public RefCounted { - public: - struct ClearColor { - float color[4] = {}; - }; + public: + struct ClearColor { + float color[4] = {}; + }; - struct ClearDepthStencil { - float depth = 1.0f; - uint32_t stencil = 0; - }; + struct ClearDepthStencil { + float depth = 1.0f; + uint32_t stencil = 0; + }; - FramebufferBase(FramebufferBuilder* builder); + FramebufferBase(FramebufferBuilder* builder); - DeviceBase* GetDevice(); - RenderPassBase* GetRenderPass(); - TextureViewBase* GetTextureView(uint32_t attachmentSlot); - ClearColor GetClearColor(uint32_t attachmentSlot); - ClearDepthStencil GetClearDepthStencil(uint32_t attachmentSlot); - uint32_t GetWidth() const; - uint32_t GetHeight() const; + DeviceBase* GetDevice(); + RenderPassBase* GetRenderPass(); + TextureViewBase* GetTextureView(uint32_t attachmentSlot); + ClearColor GetClearColor(uint32_t attachmentSlot); + ClearDepthStencil GetClearDepthStencil(uint32_t attachmentSlot); + uint32_t GetWidth() const; + uint32_t GetHeight() const; - // NXT API - void AttachmentSetClearColor(uint32_t attachmentSlot, float clearR, float clearG, float clearB, float clearA); - void AttachmentSetClearDepthStencil(uint32_t attachmentSlot, float clearDepth, uint32_t clearStencil); + // NXT API + void AttachmentSetClearColor(uint32_t attachmentSlot, + float clearR, + float clearG, + float clearB, + float clearA); + void AttachmentSetClearDepthStencil(uint32_t attachmentSlot, + float clearDepth, + uint32_t clearStencil); - private: - DeviceBase* mDevice; - Ref mRenderPass; - uint32_t mWidth = 0; - uint32_t mHeight = 0; - std::vector> mTextureViews; - std::vector mClearColors; - std::vector mClearDepthStencils; + private: + DeviceBase* mDevice; + Ref mRenderPass; + uint32_t mWidth = 0; + uint32_t mHeight = 0; + std::vector> mTextureViews; + std::vector mClearColors; + std::vector mClearDepthStencils; }; class FramebufferBuilder : public Builder { - public: - FramebufferBuilder(DeviceBase* device); + public: + FramebufferBuilder(DeviceBase* device); - // NXT API - FramebufferBase* GetResultImpl() override; - void SetRenderPass(RenderPassBase* renderPass); - void SetDimensions(uint32_t width, uint32_t height); - void SetAttachment(uint32_t attachmentSlot, TextureViewBase* textureView); + // NXT API + FramebufferBase* GetResultImpl() override; + void SetRenderPass(RenderPassBase* renderPass); + void SetDimensions(uint32_t width, uint32_t height); + void SetAttachment(uint32_t attachmentSlot, TextureViewBase* textureView); - private: - friend class FramebufferBase; + private: + friend class FramebufferBase; - Ref mRenderPass; - uint32_t mWidth = 0; - uint32_t mHeight = 0; - std::vector> mTextureViews; - int mPropertiesSet = 0; + Ref mRenderPass; + uint32_t mWidth = 0; + uint32_t mHeight = 0; + std::vector> mTextureViews; + int mPropertiesSet = 0; }; -} +} // namespace backend -#endif // BACKEND_FRAMEBUFFER_H_ +#endif // BACKEND_FRAMEBUFFER_H_ diff --git a/src/backend/InputState.cpp b/src/backend/InputState.cpp index ba251a0fc2..7180c90142 100644 --- a/src/backend/InputState.cpp +++ b/src/backend/InputState.cpp @@ -94,7 +94,7 @@ namespace backend { InputStateBase* InputStateBuilder::GetResultImpl() { for (uint32_t location = 0; location < kMaxVertexAttributes; ++location) { if (mAttributesSetMask[location] && - !mInputsSetMask[mAttributeInfos[location].bindingSlot]) { + !mInputsSetMask[mAttributeInfos[location].bindingSlot]) { HandleError("Attribute uses unset input"); return nullptr; } @@ -104,7 +104,9 @@ namespace backend { } void InputStateBuilder::SetAttribute(uint32_t shaderLocation, - uint32_t bindingSlot, nxt::VertexFormat format, uint32_t offset) { + uint32_t bindingSlot, + nxt::VertexFormat format, + uint32_t offset) { if (shaderLocation >= kMaxVertexAttributes) { HandleError("Setting attribute out of bounds"); return; @@ -125,8 +127,9 @@ namespace backend { info.offset = offset; } - void InputStateBuilder::SetInput(uint32_t bindingSlot, uint32_t stride, - nxt::InputStepMode stepMode) { + void InputStateBuilder::SetInput(uint32_t bindingSlot, + uint32_t stride, + nxt::InputStepMode stepMode) { if (bindingSlot >= kMaxVertexInputs) { HandleError("Setting input out of bounds"); return; @@ -142,4 +145,4 @@ namespace backend { info.stepMode = stepMode; } -} +} // namespace backend diff --git a/src/backend/InputState.h b/src/backend/InputState.h index 5e192a3dd9..7a62b4ffbd 100644 --- a/src/backend/InputState.h +++ b/src/backend/InputState.h @@ -15,8 +15,8 @@ #ifndef BACKEND_INPUTSTATE_H_ #define BACKEND_INPUTSTATE_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "common/Constants.h" @@ -32,53 +32,54 @@ namespace backend { size_t VertexFormatSize(nxt::VertexFormat format); class InputStateBase : public RefCounted { - public: - InputStateBase(InputStateBuilder* builder); + public: + InputStateBase(InputStateBuilder* builder); - struct AttributeInfo { - uint32_t bindingSlot; - nxt::VertexFormat format; - uint32_t offset; - }; + struct AttributeInfo { + uint32_t bindingSlot; + nxt::VertexFormat format; + uint32_t offset; + }; - struct InputInfo { - uint32_t stride; - nxt::InputStepMode stepMode; - }; + struct InputInfo { + uint32_t stride; + nxt::InputStepMode stepMode; + }; - const std::bitset& GetAttributesSetMask() const; - const AttributeInfo& GetAttribute(uint32_t location) const; - const std::bitset& GetInputsSetMask() const; - const InputInfo& GetInput(uint32_t slot) const; + const std::bitset& GetAttributesSetMask() const; + const AttributeInfo& GetAttribute(uint32_t location) const; + const std::bitset& GetInputsSetMask() const; + const InputInfo& GetInput(uint32_t slot) const; - private: - std::bitset mAttributesSetMask; - std::array mAttributeInfos; - std::bitset mInputsSetMask; - std::array mInputInfos; + private: + std::bitset mAttributesSetMask; + std::array mAttributeInfos; + std::bitset mInputsSetMask; + std::array mInputInfos; }; class InputStateBuilder : public Builder { - public: - InputStateBuilder(DeviceBase* device); + public: + InputStateBuilder(DeviceBase* device); - // NXT API - void SetAttribute(uint32_t shaderLocation, uint32_t bindingSlot, - nxt::VertexFormat format, uint32_t offset); - void SetInput(uint32_t bindingSlot, uint32_t stride, - nxt::InputStepMode stepMode); + // NXT API + void SetAttribute(uint32_t shaderLocation, + uint32_t bindingSlot, + nxt::VertexFormat format, + uint32_t offset); + void SetInput(uint32_t bindingSlot, uint32_t stride, nxt::InputStepMode stepMode); - private: - friend class InputStateBase; + private: + friend class InputStateBase; - InputStateBase* GetResultImpl() override; + InputStateBase* GetResultImpl() override; - std::bitset mAttributesSetMask; - std::array mAttributeInfos; - std::bitset mInputsSetMask; - std::array mInputInfos; + std::bitset mAttributesSetMask; + std::array mAttributeInfos; + std::bitset mInputsSetMask; + std::array mInputInfos; }; -} +} // namespace backend -#endif // BACKEND_INPUTSTATE_H_ +#endif // BACKEND_INPUTSTATE_H_ diff --git a/src/backend/PerStage.cpp b/src/backend/PerStage.cpp index 8cf48b7e57..dbb4ef0399 100644 --- a/src/backend/PerStage.cpp +++ b/src/backend/PerStage.cpp @@ -26,4 +26,4 @@ namespace backend { return static_cast(1 << static_cast(stage)); } -} +} // namespace backend diff --git a/src/backend/PerStage.h b/src/backend/PerStage.h index 10c044bc39..be8eef103d 100644 --- a/src/backend/PerStage.h +++ b/src/backend/PerStage.h @@ -29,42 +29,49 @@ namespace backend { static_assert(static_cast(nxt::ShaderStage::Fragment) < kNumStages, ""); static_assert(static_cast(nxt::ShaderStage::Compute) < kNumStages, ""); - static_assert(static_cast(nxt::ShaderStageBit::Vertex) == (1 << static_cast(nxt::ShaderStage::Vertex)), ""); - static_assert(static_cast(nxt::ShaderStageBit::Fragment) == (1 << static_cast(nxt::ShaderStage::Fragment)), ""); - static_assert(static_cast(nxt::ShaderStageBit::Compute) == (1 << static_cast(nxt::ShaderStage::Compute)), ""); + static_assert(static_cast(nxt::ShaderStageBit::Vertex) == + (1 << static_cast(nxt::ShaderStage::Vertex)), + ""); + static_assert(static_cast(nxt::ShaderStageBit::Fragment) == + (1 << static_cast(nxt::ShaderStage::Fragment)), + ""); + static_assert(static_cast(nxt::ShaderStageBit::Compute) == + (1 << static_cast(nxt::ShaderStage::Compute)), + ""); BitSetIterator IterateStages(nxt::ShaderStageBit stages); nxt::ShaderStageBit StageBit(nxt::ShaderStage stage); - static constexpr nxt::ShaderStageBit kAllStages = static_cast((1 << kNumStages) - 1); + static constexpr nxt::ShaderStageBit kAllStages = + static_cast((1 << kNumStages) - 1); - template + template class PerStage { - public: - T& operator[](nxt::ShaderStage stage) { - NXT_ASSERT(static_cast(stage) < kNumStages); - return mData[static_cast(stage)]; - } - const T& operator[](nxt::ShaderStage stage) const { - NXT_ASSERT(static_cast(stage) < kNumStages); - return mData[static_cast(stage)]; - } + public: + T& operator[](nxt::ShaderStage stage) { + NXT_ASSERT(static_cast(stage) < kNumStages); + return mData[static_cast(stage)]; + } + const T& operator[](nxt::ShaderStage stage) const { + NXT_ASSERT(static_cast(stage) < kNumStages); + return mData[static_cast(stage)]; + } - T& operator[](nxt::ShaderStageBit stageBit) { - uint32_t bit = static_cast(stageBit); - NXT_ASSERT(bit != 0 && IsPowerOfTwo(bit) && bit <= (1 << kNumStages)); - return mData[Log2(bit)]; - } - const T& operator[](nxt::ShaderStageBit stageBit) const { - uint32_t bit = static_cast(stageBit); - NXT_ASSERT(bit != 0 && IsPowerOfTwo(bit) && bit <= (1 << kNumStages)); - return mData[Log2(bit)]; - } + T& operator[](nxt::ShaderStageBit stageBit) { + uint32_t bit = static_cast(stageBit); + NXT_ASSERT(bit != 0 && IsPowerOfTwo(bit) && bit <= (1 << kNumStages)); + return mData[Log2(bit)]; + } + const T& operator[](nxt::ShaderStageBit stageBit) const { + uint32_t bit = static_cast(stageBit); + NXT_ASSERT(bit != 0 && IsPowerOfTwo(bit) && bit <= (1 << kNumStages)); + return mData[Log2(bit)]; + } - private: - std::array mData; + private: + std::array mData; }; -} +} // namespace backend -#endif // BACKEND_PERSTAGE_H_ +#endif // BACKEND_PERSTAGE_H_ diff --git a/src/backend/Pipeline.cpp b/src/backend/Pipeline.cpp index 7cade25218..67c1c19a1f 100644 --- a/src/backend/Pipeline.cpp +++ b/src/backend/Pipeline.cpp @@ -14,8 +14,8 @@ #include "backend/Pipeline.h" -#include "backend/Device.h" #include "backend/DepthStencilState.h" +#include "backend/Device.h" #include "backend/InputState.h" #include "backend/PipelineLayout.h" #include "backend/RenderPass.h" @@ -28,7 +28,10 @@ namespace backend { PipelineBase::PipelineBase(PipelineBuilder* builder) : mStageMask(builder->mStageMask), mLayout(std::move(builder->mLayout)) { if (!mLayout) { - mLayout = builder->GetParentBuilder()->GetDevice()->CreatePipelineLayoutBuilder()->GetResult(); + mLayout = builder->GetParentBuilder() + ->GetDevice() + ->CreatePipelineLayoutBuilder() + ->GetResult(); } auto FillPushConstants = [](const ShaderModuleBase* module, PushConstantInfo* info) { @@ -58,7 +61,8 @@ namespace backend { } } - const PipelineBase::PushConstantInfo& PipelineBase::GetPushConstants(nxt::ShaderStage stage) const { + const PipelineBase::PushConstantInfo& PipelineBase::GetPushConstants( + nxt::ShaderStage stage) const { return mPushConstants[stage]; } @@ -89,7 +93,9 @@ namespace backend { mLayout = layout; } - void PipelineBuilder::SetStage(nxt::ShaderStage stage, ShaderModuleBase* module, const char* entryPoint) { + void PipelineBuilder::SetStage(nxt::ShaderStage stage, + ShaderModuleBase* module, + const char* entryPoint) { if (entryPoint != std::string("main")) { mParentBuilder->HandleError("Currently the entry point has to be main()"); return; @@ -111,4 +117,4 @@ namespace backend { mStages[stage].entryPoint = entryPoint; } -} +} // namespace backend diff --git a/src/backend/Pipeline.h b/src/backend/Pipeline.h index 197434d5c5..308c00e966 100644 --- a/src/backend/Pipeline.h +++ b/src/backend/Pipeline.h @@ -15,8 +15,8 @@ #ifndef BACKEND_PIPELINE_H_ #define BACKEND_PIPELINE_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/PerStage.h" #include "backend/PipelineLayout.h" #include "backend/RefCounted.h" @@ -38,48 +38,48 @@ namespace backend { class PipelineBuilder; class PipelineBase { - public: - PipelineBase(PipelineBuilder* builder); + public: + PipelineBase(PipelineBuilder* builder); - struct PushConstantInfo { - std::bitset mask; - std::array types; - }; - const PushConstantInfo& GetPushConstants(nxt::ShaderStage stage) const; - nxt::ShaderStageBit GetStageMask() const; + struct PushConstantInfo { + std::bitset mask; + std::array types; + }; + const PushConstantInfo& GetPushConstants(nxt::ShaderStage stage) const; + nxt::ShaderStageBit GetStageMask() const; - PipelineLayoutBase* GetLayout(); + PipelineLayoutBase* GetLayout(); - private: - nxt::ShaderStageBit mStageMask; - Ref mLayout; - PerStage mPushConstants; + private: + nxt::ShaderStageBit mStageMask; + Ref mLayout; + PerStage mPushConstants; }; class PipelineBuilder { - public: - PipelineBuilder(BuilderBase* parentBuilder); + public: + PipelineBuilder(BuilderBase* parentBuilder); - struct StageInfo { - std::string entryPoint; - Ref module; - }; - const StageInfo& GetStageInfo(nxt::ShaderStage stage) const; - BuilderBase* GetParentBuilder() const; + struct StageInfo { + std::string entryPoint; + Ref module; + }; + const StageInfo& GetStageInfo(nxt::ShaderStage stage) const; + BuilderBase* GetParentBuilder() const; - // NXT API - void SetLayout(PipelineLayoutBase* layout); - void SetStage(nxt::ShaderStage stage, ShaderModuleBase* module, const char* entryPoint); + // NXT API + void SetLayout(PipelineLayoutBase* layout); + void SetStage(nxt::ShaderStage stage, ShaderModuleBase* module, const char* entryPoint); - private: - friend class PipelineBase; + private: + friend class PipelineBase; - BuilderBase* mParentBuilder; - Ref mLayout; - nxt::ShaderStageBit mStageMask; - PerStage mStages; + BuilderBase* mParentBuilder; + Ref mLayout; + nxt::ShaderStageBit mStageMask; + PerStage mStages; }; -} +} // namespace backend -#endif // BACKEND_PIPELINE_H_ +#endif // BACKEND_PIPELINE_H_ diff --git a/src/backend/PipelineLayout.cpp b/src/backend/PipelineLayout.cpp index e45dacaccc..6adca64f29 100644 --- a/src/backend/PipelineLayout.cpp +++ b/src/backend/PipelineLayout.cpp @@ -35,8 +35,9 @@ namespace backend { return mMask; } - std::bitset PipelineLayoutBase::InheritedGroupsMask(const PipelineLayoutBase* other) const { - return { GroupsInheritUpTo(other) - 1 }; + std::bitset PipelineLayoutBase::InheritedGroupsMask( + const PipelineLayoutBase* other) const { + return {GroupsInheritUpTo(other) - 1}; } uint32_t PipelineLayoutBase::GroupsInheritUpTo(const PipelineLayoutBase* other) const { @@ -54,8 +55,8 @@ namespace backend { } PipelineLayoutBase* PipelineLayoutBuilder::GetResultImpl() { - // TODO(cwallez@chromium.org): this is a hack, have the null bind group layout somewhere in the device - // once we have a cache of BGL + // TODO(cwallez@chromium.org): this is a hack, have the null bind group layout somewhere in + // the device once we have a cache of BGL for (size_t group = 0; group < kMaxBindGroups; ++group) { if (!mBindGroupLayouts[group]) { mBindGroupLayouts[group] = mDevice->CreateBindGroupLayoutBuilder()->GetResult(); @@ -65,7 +66,8 @@ namespace backend { return mDevice->CreatePipelineLayout(this); } - void PipelineLayoutBuilder::SetBindGroupLayout(uint32_t groupIndex, BindGroupLayoutBase* layout) { + void PipelineLayoutBuilder::SetBindGroupLayout(uint32_t groupIndex, + BindGroupLayoutBase* layout) { if (groupIndex >= kMaxBindGroups) { HandleError("groupIndex is over the maximum allowed"); return; @@ -79,4 +81,4 @@ namespace backend { mMask.set(groupIndex); } -} +} // namespace backend diff --git a/src/backend/PipelineLayout.h b/src/backend/PipelineLayout.h index a3fe86e52d..19062e9dc3 100644 --- a/src/backend/PipelineLayout.h +++ b/src/backend/PipelineLayout.h @@ -15,8 +15,8 @@ #ifndef BACKEND_PIPELINELAYOUT_H_ #define BACKEND_PIPELINELAYOUT_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "common/Constants.h" @@ -30,40 +30,41 @@ namespace backend { using BindGroupLayoutArray = std::array, kMaxBindGroups>; class PipelineLayoutBase : public RefCounted { - public: - PipelineLayoutBase(PipelineLayoutBuilder* builder); + public: + PipelineLayoutBase(PipelineLayoutBuilder* builder); - const BindGroupLayoutBase* GetBindGroupLayout(size_t group) const; - const std::bitset GetBindGroupsLayoutMask() const; + const BindGroupLayoutBase* GetBindGroupLayout(size_t group) const; + const std::bitset GetBindGroupsLayoutMask() const; - // Utility functions to compute inherited bind groups. - // Returns the inherited bind groups as a mask - std::bitset InheritedGroupsMask(const PipelineLayoutBase* other) const; + // Utility functions to compute inherited bind groups. + // Returns the inherited bind groups as a mask. + std::bitset InheritedGroupsMask(const PipelineLayoutBase* other) const; - // Returns the index of the first incompatible bind group (in the range [1, kMaxBindGroups + 1]) - uint32_t GroupsInheritUpTo(const PipelineLayoutBase* other) const; + // Returns the index of the first incompatible bind group in the range + // [1, kMaxBindGroups + 1] + uint32_t GroupsInheritUpTo(const PipelineLayoutBase* other) const; - protected: - BindGroupLayoutArray mBindGroupLayouts; - std::bitset mMask; + protected: + BindGroupLayoutArray mBindGroupLayouts; + std::bitset mMask; }; class PipelineLayoutBuilder : public Builder { - public: - PipelineLayoutBuilder(DeviceBase* device); + public: + PipelineLayoutBuilder(DeviceBase* device); - // NXT API - void SetBindGroupLayout(uint32_t groupIndex, BindGroupLayoutBase* layout); + // NXT API + void SetBindGroupLayout(uint32_t groupIndex, BindGroupLayoutBase* layout); - private: - friend class PipelineLayoutBase; + private: + friend class PipelineLayoutBase; - PipelineLayoutBase* GetResultImpl() override; + PipelineLayoutBase* GetResultImpl() override; - BindGroupLayoutArray mBindGroupLayouts; - std::bitset mMask; + BindGroupLayoutArray mBindGroupLayouts; + std::bitset mMask; }; -} +} // namespace backend -#endif // BACKEND_PIPELINELAYOUT_H_ +#endif // BACKEND_PIPELINELAYOUT_H_ diff --git a/src/backend/Queue.cpp b/src/backend/Queue.cpp index fd012451f4..8a8ef7b5b8 100644 --- a/src/backend/Queue.cpp +++ b/src/backend/Queue.cpp @@ -14,8 +14,8 @@ #include "backend/Queue.h" -#include "backend/Device.h" #include "backend/CommandBuffer.h" +#include "backend/Device.h" namespace backend { @@ -41,4 +41,4 @@ namespace backend { return mDevice->CreateQueue(this); } -} +} // namespace backend diff --git a/src/backend/Queue.h b/src/backend/Queue.h index bd9c2a35ca..daa04ab434 100644 --- a/src/backend/Queue.h +++ b/src/backend/Queue.h @@ -15,8 +15,8 @@ #ifndef BACKEND_QUEUE_H_ #define BACKEND_QUEUE_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "nxt/nxtcpp.h" @@ -24,38 +24,39 @@ namespace backend { class QueueBase : public RefCounted { - public: - QueueBase(QueueBuilder* builder); + public: + QueueBase(QueueBuilder* builder); - DeviceBase* GetDevice(); + DeviceBase* GetDevice(); - template - bool ValidateSubmit(uint32_t numCommands, T* const * commands) { - static_assert(std::is_base_of::value, "invalid command buffer type"); + template + bool ValidateSubmit(uint32_t numCommands, T* const* commands) { + static_assert(std::is_base_of::value, + "invalid command buffer type"); - for (uint32_t i = 0; i < numCommands; ++i) { - if (!ValidateSubmitCommand(commands[i])) { - return false; - } + for (uint32_t i = 0; i < numCommands; ++i) { + if (!ValidateSubmitCommand(commands[i])) { + return false; } - return true; } + return true; + } - private: - bool ValidateSubmitCommand(CommandBufferBase* command); + private: + bool ValidateSubmitCommand(CommandBufferBase* command); - DeviceBase* mDevice; + DeviceBase* mDevice; }; class QueueBuilder : public Builder { - public: - QueueBuilder(DeviceBase* device); + public: + QueueBuilder(DeviceBase* device); - private: - friend class QueueBase; - QueueBase* GetResultImpl() override; + private: + friend class QueueBase; + QueueBase* GetResultImpl() override; }; -} +} // namespace backend -#endif // BACKEND_QUEUE_H_ +#endif // BACKEND_QUEUE_H_ diff --git a/src/backend/RefCounted.cpp b/src/backend/RefCounted.cpp index 7665a6efbc..c5a56c1f6c 100644 --- a/src/backend/RefCounted.cpp +++ b/src/backend/RefCounted.cpp @@ -27,12 +27,12 @@ namespace backend { void RefCounted::ReferenceInternal() { ASSERT(mInternalRefs != 0); // TODO(cwallez@chromium.org): what to do on overflow? - mInternalRefs ++; + mInternalRefs++; } void RefCounted::ReleaseInternal() { ASSERT(mInternalRefs != 0); - mInternalRefs --; + mInternalRefs--; if (mInternalRefs == 0) { ASSERT(mExternalRefs == 0); // TODO(cwallez@chromium.org): would this work with custom allocators? @@ -51,15 +51,15 @@ namespace backend { void RefCounted::Reference() { ASSERT(mExternalRefs != 0); // TODO(cwallez@chromium.org): what to do on overflow? - mExternalRefs ++; + mExternalRefs++; } void RefCounted::Release() { ASSERT(mExternalRefs != 0); - mExternalRefs --; + mExternalRefs--; if (mExternalRefs == 0) { ReleaseInternal(); } } -} +} // namespace backend diff --git a/src/backend/RefCounted.h b/src/backend/RefCounted.h index 04061d991c..fa751588c5 100644 --- a/src/backend/RefCounted.h +++ b/src/backend/RefCounted.h @@ -20,107 +20,110 @@ namespace backend { class RefCounted { - public: - RefCounted(); - virtual ~RefCounted(); + public: + RefCounted(); + virtual ~RefCounted(); - void ReferenceInternal(); - void ReleaseInternal(); + void ReferenceInternal(); + void ReleaseInternal(); - uint32_t GetExternalRefs() const; - uint32_t GetInternalRefs() const; + uint32_t GetExternalRefs() const; + uint32_t GetInternalRefs() const; - // NXT API - void Reference(); - void Release(); + // NXT API + void Reference(); + void Release(); - protected: - uint32_t mExternalRefs = 1; - uint32_t mInternalRefs = 1; + protected: + uint32_t mExternalRefs = 1; + uint32_t mInternalRefs = 1; }; - template + template class Ref { - public: - Ref() {} + public: + Ref() { + } - Ref(T* p): mPointee(p) { - Reference(); - } - - Ref(const Ref& other): mPointee(other.mPointee) { - Reference(); - } - Ref& operator=(const Ref& other) { - if (&other == this) return *this; - - other.Reference(); - Release(); - mPointee = other.mPointee; + Ref(T* p) : mPointee(p) { + Reference(); + } + Ref(const Ref& other) : mPointee(other.mPointee) { + Reference(); + } + Ref& operator=(const Ref& other) { + if (&other == this) return *this; - } - Ref(Ref&& other) { - mPointee = other.mPointee; - other.mPointee = nullptr; - } - Ref& operator=(Ref&& other) { - if (&other == this) return *this; + other.Reference(); + Release(); + mPointee = other.mPointee; - Release(); - mPointee = other.mPointee; - other.mPointee = nullptr; + return *this; + } + Ref(Ref&& other) { + mPointee = other.mPointee; + other.mPointee = nullptr; + } + Ref& operator=(Ref&& other) { + if (&other == this) return *this; - } - ~Ref() { - Release(); - mPointee = nullptr; - } + Release(); + mPointee = other.mPointee; + other.mPointee = nullptr; - operator bool() { - return mPointee != nullptr; - } + return *this; + } - const T& operator*() const { - return *mPointee; - } - T& operator*() { - return *mPointee; - } + ~Ref() { + Release(); + mPointee = nullptr; + } - const T* operator->() const { - return mPointee; - } - T* operator->() { - return mPointee; - } + operator bool() { + return mPointee != nullptr; + } - const T* Get() const { - return mPointee; - } - T* Get() { - return mPointee; - } + const T& operator*() const { + return *mPointee; + } + T& operator*() { + return *mPointee; + } - private: - void Reference() const { - if (mPointee != nullptr) { - mPointee->ReferenceInternal(); - } - } - void Release() const { - if (mPointee != nullptr) { - mPointee->ReleaseInternal(); - } - } + const T* operator->() const { + return mPointee; + } + T* operator->() { + return mPointee; + } - //static_assert(std::is_base_of::value, ""); - T* mPointee = nullptr; + const T* Get() const { + return mPointee; + } + T* Get() { + return mPointee; + } + + private: + void Reference() const { + if (mPointee != nullptr) { + mPointee->ReferenceInternal(); + } + } + void Release() const { + if (mPointee != nullptr) { + mPointee->ReleaseInternal(); + } + } + + // static_assert(std::is_base_of::value, ""); + T* mPointee = nullptr; }; -} +} // namespace backend -#endif // BACKEND_REFCOUNTED_H_ +#endif // BACKEND_REFCOUNTED_H_ diff --git a/src/backend/RenderPass.cpp b/src/backend/RenderPass.cpp index bd82b6893b..d4e0c953e9 100644 --- a/src/backend/RenderPass.cpp +++ b/src/backend/RenderPass.cpp @@ -25,7 +25,8 @@ namespace backend { // RenderPass RenderPassBase::RenderPassBase(RenderPassBuilder* builder) - : mAttachments(std::move(builder->mAttachments)), mSubpasses(std::move(builder->mSubpasses)) { + : mAttachments(std::move(builder->mAttachments)), + mSubpasses(std::move(builder->mSubpasses)) { for (uint32_t s = 0; s < GetSubpassCount(); ++s) { const auto& subpass = GetSubpassInfo(s); for (auto location : IterateBitSet(subpass.colorAttachmentsSet)) { @@ -49,7 +50,8 @@ namespace backend { return static_cast(mAttachments.size()); } - const RenderPassBase::AttachmentInfo& RenderPassBase::GetAttachmentInfo(uint32_t attachment) const { + const RenderPassBase::AttachmentInfo& RenderPassBase::GetAttachmentInfo( + uint32_t attachment) const { ASSERT(attachment < mAttachments.size()); return mAttachments[attachment]; } @@ -76,12 +78,12 @@ namespace backend { RENDERPASS_PROPERTY_SUBPASS_COUNT = 0x2, }; - RenderPassBuilder::RenderPassBuilder(DeviceBase* device) - : Builder(device), mSubpasses(1) { + RenderPassBuilder::RenderPassBuilder(DeviceBase* device) : Builder(device), mSubpasses(1) { } RenderPassBase* RenderPassBuilder::GetResultImpl() { - constexpr int requiredProperties = RENDERPASS_PROPERTY_ATTACHMENT_COUNT | RENDERPASS_PROPERTY_SUBPASS_COUNT; + constexpr int requiredProperties = + RENDERPASS_PROPERTY_ATTACHMENT_COUNT | RENDERPASS_PROPERTY_SUBPASS_COUNT; if ((mPropertiesSet & requiredProperties) != requiredProperties) { HandleError("Render pass missing properties"); return nullptr; @@ -105,7 +107,8 @@ namespace backend { if (subpass.depthStencilAttachmentSet) { uint32_t slot = subpass.depthStencilAttachment; if (!TextureFormatHasDepthOrStencil(mAttachments[slot].format)) { - HandleError("Render pass depth/stencil attachment is not of a depth/stencil format"); + HandleError( + "Render pass depth/stencil attachment is not of a depth/stencil format"); return nullptr; } } @@ -125,7 +128,8 @@ namespace backend { mPropertiesSet |= RENDERPASS_PROPERTY_ATTACHMENT_COUNT; } - void RenderPassBuilder::AttachmentSetFormat(uint32_t attachmentSlot, nxt::TextureFormat format) { + void RenderPassBuilder::AttachmentSetFormat(uint32_t attachmentSlot, + nxt::TextureFormat format) { if ((mPropertiesSet & RENDERPASS_PROPERTY_ATTACHMENT_COUNT) == 0) { HandleError("Render pass attachment count not set yet"); return; @@ -156,7 +160,9 @@ namespace backend { mAttachments[attachmentSlot].colorLoadOp = op; } - void RenderPassBuilder::AttachmentSetDepthStencilLoadOps(uint32_t attachmentSlot, nxt::LoadOp depthOp, nxt::LoadOp stencilOp) { + void RenderPassBuilder::AttachmentSetDepthStencilLoadOps(uint32_t attachmentSlot, + nxt::LoadOp depthOp, + nxt::LoadOp stencilOp) { if ((mPropertiesSet & RENDERPASS_PROPERTY_ATTACHMENT_COUNT) == 0) { HandleError("Render pass attachment count not set yet"); return; @@ -170,7 +176,6 @@ namespace backend { mAttachments[attachmentSlot].stencilLoadOp = stencilOp; } - void RenderPassBuilder::SetSubpassCount(uint32_t subpassCount) { if ((mPropertiesSet & RENDERPASS_PROPERTY_SUBPASS_COUNT) != 0) { HandleError("Render pass subpass count property set multiple times"); @@ -185,7 +190,9 @@ namespace backend { mPropertiesSet |= RENDERPASS_PROPERTY_SUBPASS_COUNT; } - void RenderPassBuilder::SubpassSetColorAttachment(uint32_t subpass, uint32_t outputAttachmentLocation, uint32_t attachmentSlot) { + void RenderPassBuilder::SubpassSetColorAttachment(uint32_t subpass, + uint32_t outputAttachmentLocation, + uint32_t attachmentSlot) { if ((mPropertiesSet & RENDERPASS_PROPERTY_SUBPASS_COUNT) == 0) { HandleError("Render pass subpass count not set yet"); return; @@ -215,7 +222,8 @@ namespace backend { mSubpasses[subpass].colorAttachments[outputAttachmentLocation] = attachmentSlot; } - void RenderPassBuilder::SubpassSetDepthStencilAttachment(uint32_t subpass, uint32_t attachmentSlot) { + void RenderPassBuilder::SubpassSetDepthStencilAttachment(uint32_t subpass, + uint32_t attachmentSlot) { if ((mPropertiesSet & RENDERPASS_PROPERTY_SUBPASS_COUNT) == 0) { HandleError("Render pass subpass count not set yet"); return; @@ -241,4 +249,4 @@ namespace backend { mSubpasses[subpass].depthStencilAttachment = attachmentSlot; } -} +} // namespace backend diff --git a/src/backend/RenderPass.h b/src/backend/RenderPass.h index a9ded00ee2..94de20fc0e 100644 --- a/src/backend/RenderPass.h +++ b/src/backend/RenderPass.h @@ -29,68 +29,68 @@ namespace backend { class RenderPassBase : public RefCounted { - public: - RenderPassBase(RenderPassBuilder* builder); + public: + RenderPassBase(RenderPassBuilder* builder); - struct AttachmentInfo { - nxt::TextureFormat format; - nxt::LoadOp colorLoadOp = nxt::LoadOp::Load; - nxt::LoadOp depthLoadOp = nxt::LoadOp::Load; - nxt::LoadOp stencilLoadOp = nxt::LoadOp::Load; - // The first subpass that this attachment is used in. - // This is used to determine, for each subpass, whether each - // of its attachments is being used for the first time. - uint32_t firstSubpass = UINT32_MAX; - }; + struct AttachmentInfo { + nxt::TextureFormat format; + nxt::LoadOp colorLoadOp = nxt::LoadOp::Load; + nxt::LoadOp depthLoadOp = nxt::LoadOp::Load; + nxt::LoadOp stencilLoadOp = nxt::LoadOp::Load; + // The first subpass that this attachment is used in. This is used to determine, for + // each subpass, whether each of its attachments is being used for the first time. + uint32_t firstSubpass = UINT32_MAX; + }; - struct SubpassInfo { - // Set of locations which are set - std::bitset colorAttachmentsSet; - // Mapping from location to attachment slot - std::array colorAttachments; - bool depthStencilAttachmentSet = false; - uint32_t depthStencilAttachment = 0; - }; + struct SubpassInfo { + // Set of locations which are set + std::bitset colorAttachmentsSet; + // Mapping from location to attachment slot + std::array colorAttachments; + bool depthStencilAttachmentSet = false; + uint32_t depthStencilAttachment = 0; + }; - uint32_t GetAttachmentCount() const; - const AttachmentInfo& GetAttachmentInfo(uint32_t attachment) const; - uint32_t GetSubpassCount() const; - const SubpassInfo& GetSubpassInfo(uint32_t subpass) const; - bool IsCompatibleWith(const RenderPassBase* other) const; + uint32_t GetAttachmentCount() const; + const AttachmentInfo& GetAttachmentInfo(uint32_t attachment) const; + uint32_t GetSubpassCount() const; + const SubpassInfo& GetSubpassInfo(uint32_t subpass) const; + bool IsCompatibleWith(const RenderPassBase* other) const; - private: - std::vector mAttachments; - std::vector mSubpasses; + private: + std::vector mAttachments; + std::vector mSubpasses; }; class RenderPassBuilder : public Builder { - public: - RenderPassBuilder(DeviceBase* device); + public: + RenderPassBuilder(DeviceBase* device); - // NXT API - RenderPassBase* GetResultImpl() override; - void SetAttachmentCount(uint32_t attachmentCount); - void AttachmentSetFormat(uint32_t attachmentSlot, nxt::TextureFormat format); - void AttachmentSetColorLoadOp(uint32_t attachmentSlot, nxt::LoadOp op); - void AttachmentSetDepthStencilLoadOps(uint32_t attachmentSlot, nxt::LoadOp depthOp, nxt::LoadOp stencilOp); - void SetSubpassCount(uint32_t subpassCount); - void SubpassSetColorAttachment(uint32_t subpass, uint32_t outputAttachmentLocation, uint32_t attachmentSlot); - void SubpassSetDepthStencilAttachment(uint32_t subpass, uint32_t attachmentSlot); + // NXT API + RenderPassBase* GetResultImpl() override; + void SetAttachmentCount(uint32_t attachmentCount); + void AttachmentSetFormat(uint32_t attachmentSlot, nxt::TextureFormat format); + void AttachmentSetColorLoadOp(uint32_t attachmentSlot, nxt::LoadOp op); + void AttachmentSetDepthStencilLoadOps(uint32_t attachmentSlot, + nxt::LoadOp depthOp, + nxt::LoadOp stencilOp); + void SetSubpassCount(uint32_t subpassCount); + void SubpassSetColorAttachment(uint32_t subpass, + uint32_t outputAttachmentLocation, + uint32_t attachmentSlot); + void SubpassSetDepthStencilAttachment(uint32_t subpass, uint32_t attachmentSlot); - private: - friend class RenderPassBase; + private: + friend class RenderPassBase; - enum AttachmentProperty { - ATTACHMENT_PROPERTY_FORMAT, - ATTACHMENT_PROPERTY_COUNT - }; + enum AttachmentProperty { ATTACHMENT_PROPERTY_FORMAT, ATTACHMENT_PROPERTY_COUNT }; - std::vector> mAttachmentProperties; - std::vector mAttachments; - std::vector mSubpasses; - int mPropertiesSet = 0; + std::vector> mAttachmentProperties; + std::vector mAttachments; + std::vector mSubpasses; + int mPropertiesSet = 0; }; -} +} // namespace backend -#endif // BACKEND_RENDERPASS_H_ +#endif // BACKEND_RENDERPASS_H_ diff --git a/src/backend/RenderPipeline.cpp b/src/backend/RenderPipeline.cpp index 7d6d7af5bf..a0aecca4ee 100644 --- a/src/backend/RenderPipeline.cpp +++ b/src/backend/RenderPipeline.cpp @@ -15,8 +15,8 @@ #include "backend/RenderPipeline.h" #include "backend/BlendState.h" -#include "backend/Device.h" #include "backend/DepthStencilState.h" +#include "backend/Device.h" #include "backend/InputState.h" #include "backend/RenderPass.h" #include "common/BitSetIterator.h" @@ -32,8 +32,8 @@ namespace backend { mInputState(std::move(builder->mInputState)), mPrimitiveTopology(builder->mPrimitiveTopology), mBlendStates(builder->mBlendStates), - mRenderPass(std::move(builder->mRenderPass)), mSubpass(builder->mSubpass) { - + mRenderPass(std::move(builder->mRenderPass)), + mSubpass(builder->mSubpass) { if (GetStageMask() != (nxt::ShaderStageBit::Vertex | nxt::ShaderStageBit::Fragment)) { builder->HandleError("Render pipeline should have exactly a vertex and fragment stage"); return; @@ -41,7 +41,9 @@ namespace backend { // TODO(kainino@chromium.org): Need to verify the pipeline against its render subpass. - if ((builder->GetStageInfo(nxt::ShaderStage::Vertex).module->GetUsedVertexAttributes() & ~mInputState->GetAttributesSetMask()).any()) { + if ((builder->GetStageInfo(nxt::ShaderStage::Vertex).module->GetUsedVertexAttributes() & + ~mInputState->GetAttributesSetMask()) + .any()) { builder->HandleError("Pipeline vertex stage uses inputs not in the input state"); return; } @@ -83,7 +85,8 @@ namespace backend { } RenderPipelineBase* RenderPipelineBuilder::GetResultImpl() { - // TODO(cwallez@chromium.org): the layout should be required, and put the default objects in the device + // TODO(cwallez@chromium.org): the layout should be required, and put the default objects in + // the device if (!mInputState) { mInputState = mDevice->CreateInputStateBuilder()->GetResult(); } @@ -95,21 +98,24 @@ namespace backend { return nullptr; } const auto& subpassInfo = mRenderPass->GetSubpassInfo(mSubpass); - if ((mBlendStatesSet | subpassInfo.colorAttachmentsSet) != subpassInfo.colorAttachmentsSet) { + if ((mBlendStatesSet | subpassInfo.colorAttachmentsSet) != + subpassInfo.colorAttachmentsSet) { HandleError("Blend state set on unset color attachment"); return nullptr; } // Assign all color attachments without a blend state the default state // TODO(enga@google.com): Put the default objects in the device - for (uint32_t attachmentSlot : IterateBitSet(subpassInfo.colorAttachmentsSet & ~mBlendStatesSet)) { + for (uint32_t attachmentSlot : + IterateBitSet(subpassInfo.colorAttachmentsSet & ~mBlendStatesSet)) { mBlendStates[attachmentSlot] = mDevice->CreateBlendStateBuilder()->GetResult(); } return mDevice->CreateRenderPipeline(this); } - void RenderPipelineBuilder::SetColorAttachmentBlendState(uint32_t attachmentSlot, BlendStateBase* blendState) { + void RenderPipelineBuilder::SetColorAttachmentBlendState(uint32_t attachmentSlot, + BlendStateBase* blendState) { if (attachmentSlot > mBlendStates.size()) { HandleError("Attachment index out of bounds"); return; @@ -144,4 +150,4 @@ namespace backend { mSubpass = subpass; } -} +} // namespace backend diff --git a/src/backend/RenderPipeline.h b/src/backend/RenderPipeline.h index 859bb601ca..6d4ac64526 100644 --- a/src/backend/RenderPipeline.h +++ b/src/backend/RenderPipeline.h @@ -25,55 +25,56 @@ namespace backend { class RenderPipelineBase : public RefCounted, public PipelineBase { - public: - RenderPipelineBase(RenderPipelineBuilder* builder); + public: + RenderPipelineBase(RenderPipelineBuilder* builder); - BlendStateBase* GetBlendState(uint32_t attachmentSlot); - DepthStencilStateBase* GetDepthStencilState(); - nxt::IndexFormat GetIndexFormat() const; - InputStateBase* GetInputState(); - nxt::PrimitiveTopology GetPrimitiveTopology() const; - RenderPassBase* GetRenderPass(); - uint32_t GetSubPass(); + BlendStateBase* GetBlendState(uint32_t attachmentSlot); + DepthStencilStateBase* GetDepthStencilState(); + nxt::IndexFormat GetIndexFormat() const; + InputStateBase* GetInputState(); + nxt::PrimitiveTopology GetPrimitiveTopology() const; + RenderPassBase* GetRenderPass(); + uint32_t GetSubPass(); - private: - Ref mDepthStencilState; - nxt::IndexFormat mIndexFormat; - Ref mInputState; - nxt::PrimitiveTopology mPrimitiveTopology; - std::array, kMaxColorAttachments> mBlendStates; - Ref mRenderPass; - uint32_t mSubpass; + private: + Ref mDepthStencilState; + nxt::IndexFormat mIndexFormat; + Ref mInputState; + nxt::PrimitiveTopology mPrimitiveTopology; + std::array, kMaxColorAttachments> mBlendStates; + Ref mRenderPass; + uint32_t mSubpass; }; class RenderPipelineBuilder : public Builder, public PipelineBuilder { - public: - RenderPipelineBuilder(DeviceBase* device); + public: + RenderPipelineBuilder(DeviceBase* device); - // NXT API - void SetColorAttachmentBlendState(uint32_t attachmentSlot, BlendStateBase* blendState); - void SetDepthStencilState(DepthStencilStateBase* depthStencilState); - void SetPrimitiveTopology(nxt::PrimitiveTopology primitiveTopology); - void SetIndexFormat(nxt::IndexFormat format); - void SetInputState(InputStateBase* inputState); - void SetSubpass(RenderPassBase* renderPass, uint32_t subpass); + // NXT API + void SetColorAttachmentBlendState(uint32_t attachmentSlot, BlendStateBase* blendState); + void SetDepthStencilState(DepthStencilStateBase* depthStencilState); + void SetPrimitiveTopology(nxt::PrimitiveTopology primitiveTopology); + void SetIndexFormat(nxt::IndexFormat format); + void SetInputState(InputStateBase* inputState); + void SetSubpass(RenderPassBase* renderPass, uint32_t subpass); - private: - friend class RenderPipelineBase; + private: + friend class RenderPipelineBase; - RenderPipelineBase* GetResultImpl() override; + RenderPipelineBase* GetResultImpl() override; - Ref mDepthStencilState; - Ref mInputState; - // TODO(enga@google.com): Remove default when we validate that all required properties are set - nxt::PrimitiveTopology mPrimitiveTopology = nxt::PrimitiveTopology::TriangleList; - nxt::IndexFormat mIndexFormat = nxt::IndexFormat::Uint32; - std::bitset mBlendStatesSet; - std::array, kMaxColorAttachments> mBlendStates; - Ref mRenderPass; - uint32_t mSubpass; + Ref mDepthStencilState; + Ref mInputState; + // TODO(enga@google.com): Remove default when we validate that all required properties are + // set + nxt::PrimitiveTopology mPrimitiveTopology = nxt::PrimitiveTopology::TriangleList; + nxt::IndexFormat mIndexFormat = nxt::IndexFormat::Uint32; + std::bitset mBlendStatesSet; + std::array, kMaxColorAttachments> mBlendStates; + Ref mRenderPass; + uint32_t mSubpass; }; -} +} // namespace backend -#endif // BACKEND_RENDERPIPELINE_H_ +#endif // BACKEND_RENDERPIPELINE_H_ diff --git a/src/backend/Sampler.cpp b/src/backend/Sampler.cpp index 3cc3c3c61c..4f95dc7199 100644 --- a/src/backend/Sampler.cpp +++ b/src/backend/Sampler.cpp @@ -43,7 +43,9 @@ namespace backend { return mMipMapFilter; } - void SamplerBuilder::SetFilterMode(nxt::FilterMode magFilter, nxt::FilterMode minFilter, nxt::FilterMode mipMapFilter) { + void SamplerBuilder::SetFilterMode(nxt::FilterMode magFilter, + nxt::FilterMode minFilter, + nxt::FilterMode mipMapFilter) { if ((mPropertiesSet & SAMPLER_PROPERTY_FILTER) != 0) { HandleError("Sampler filter property set multiple times"); return; @@ -59,4 +61,4 @@ namespace backend { return mDevice->CreateSampler(this); } -} +} // namespace backend diff --git a/src/backend/Sampler.h b/src/backend/Sampler.h index 181be6ecdf..bd1e1c7f60 100644 --- a/src/backend/Sampler.h +++ b/src/backend/Sampler.h @@ -15,8 +15,8 @@ #ifndef BACKEND_SAMPLER_H_ #define BACKEND_SAMPLER_H_ -#include "backend/Forward.h" #include "backend/Buffer.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "nxt/nxtcpp.h" @@ -24,33 +24,35 @@ namespace backend { class SamplerBase : public RefCounted { - public: - SamplerBase(SamplerBuilder* builder); + public: + SamplerBase(SamplerBuilder* builder); }; class SamplerBuilder : public Builder { - public: - SamplerBuilder(DeviceBase* device); + public: + SamplerBuilder(DeviceBase* device); - nxt::FilterMode GetMagFilter() const; - nxt::FilterMode GetMinFilter() const; - nxt::FilterMode GetMipMapFilter() const; + nxt::FilterMode GetMagFilter() const; + nxt::FilterMode GetMinFilter() const; + nxt::FilterMode GetMipMapFilter() const; - // NXT API - void SetFilterMode(nxt::FilterMode magFilter, nxt::FilterMode minFilter, nxt::FilterMode mipMapFilter); + // NXT API + void SetFilterMode(nxt::FilterMode magFilter, + nxt::FilterMode minFilter, + nxt::FilterMode mipMapFilter); - private: - friend class SamplerBase; + private: + friend class SamplerBase; - SamplerBase* GetResultImpl() override; + SamplerBase* GetResultImpl() override; - int mPropertiesSet = 0; + int mPropertiesSet = 0; - nxt::FilterMode mMagFilter = nxt::FilterMode::Nearest; - nxt::FilterMode mMinFilter = nxt::FilterMode::Nearest; - nxt::FilterMode mMipMapFilter = nxt::FilterMode::Nearest; + nxt::FilterMode mMagFilter = nxt::FilterMode::Nearest; + nxt::FilterMode mMinFilter = nxt::FilterMode::Nearest; + nxt::FilterMode mMipMapFilter = nxt::FilterMode::Nearest; }; -} +} // namespace backend -#endif // BACKEND_SAMPLER_H_ +#endif // BACKEND_SAMPLER_H_ diff --git a/src/backend/ShaderModule.cpp b/src/backend/ShaderModule.cpp index d06923feef..9bb1b73ad4 100644 --- a/src/backend/ShaderModule.cpp +++ b/src/backend/ShaderModule.cpp @@ -23,8 +23,7 @@ namespace backend { - ShaderModuleBase::ShaderModuleBase(ShaderModuleBuilder* builder) - : mDevice(builder->mDevice) { + ShaderModuleBase::ShaderModuleBase(ShaderModuleBuilder* builder) : mDevice(builder->mDevice) { } DeviceBase* ShaderModuleBase::GetDevice() const { @@ -62,8 +61,10 @@ namespace backend { ASSERT(blockType.basetype == spirv_cross::SPIRType::Struct); for (uint32_t i = 0; i < blockType.member_types.size(); i++) { - ASSERT(compiler.get_member_decoration_mask(blockType.self, i) & 1ull << spv::DecorationOffset); - uint32_t offset = compiler.get_member_decoration(blockType.self, i, spv::DecorationOffset); + ASSERT(compiler.get_member_decoration_mask(blockType.self, i) & + 1ull << spv::DecorationOffset); + uint32_t offset = + compiler.get_member_decoration(blockType.self, i, spv::DecorationOffset); ASSERT(offset % 4 == 0); offset /= 4; @@ -78,8 +79,8 @@ namespace backend { constantType = PushConstantType::Float; } - // TODO(cwallez@chromium.org): check for overflows and make the logic better take into account - // things like the array of types with padding. + // TODO(cwallez@chromium.org): check for overflows and make the logic better take + // into account things like the array of types with padding. uint32_t size = memberType.vecsize * memberType.columns; // Handle unidimensional arrays if (!memberType.array.empty()) { @@ -92,7 +93,8 @@ namespace backend { } mPushConstants.mask.set(offset); - mPushConstants.names[offset] = interfaceBlock.name + "." + compiler.get_member_name(blockType.self, i); + mPushConstants.names[offset] = + interfaceBlock.name + "." + compiler.get_member_name(blockType.self, i); mPushConstants.sizes[offset] = size; mPushConstants.types[offset] = constantType; } @@ -100,11 +102,14 @@ namespace backend { // Fill in bindingInfo with the SPIRV bindings auto ExtractResourcesBinding = [this](const std::vector& resources, - const spirv_cross::Compiler& compiler, nxt::BindingType bindingType) { - constexpr uint64_t requiredBindingDecorationMask = (1ull << spv::DecorationBinding) | (1ull << spv::DecorationDescriptorSet); + const spirv_cross::Compiler& compiler, + nxt::BindingType bindingType) { + constexpr uint64_t requiredBindingDecorationMask = + (1ull << spv::DecorationBinding) | (1ull << spv::DecorationDescriptorSet); for (const auto& resource : resources) { - ASSERT((compiler.get_decoration_mask(resource.id) & requiredBindingDecorationMask) == requiredBindingDecorationMask); + ASSERT((compiler.get_decoration_mask(resource.id) & + requiredBindingDecorationMask) == requiredBindingDecorationMask); uint32_t binding = compiler.get_decoration(resource.id, spv::DecorationBinding); uint32_t set = compiler.get_decoration(resource.id, spv::DecorationDescriptorSet); @@ -121,10 +126,13 @@ namespace backend { } }; - ExtractResourcesBinding(resources.uniform_buffers, compiler, nxt::BindingType::UniformBuffer); - ExtractResourcesBinding(resources.separate_images, compiler, nxt::BindingType::SampledTexture); + ExtractResourcesBinding(resources.uniform_buffers, compiler, + nxt::BindingType::UniformBuffer); + ExtractResourcesBinding(resources.separate_images, compiler, + nxt::BindingType::SampledTexture); ExtractResourcesBinding(resources.separate_samplers, compiler, nxt::BindingType::Sampler); - ExtractResourcesBinding(resources.storage_buffers, compiler, nxt::BindingType::StorageBuffer); + ExtractResourcesBinding(resources.storage_buffers, compiler, + nxt::BindingType::StorageBuffer); // Extract the vertex attributes if (mExecutionModel == nxt::ShaderStage::Vertex) { @@ -140,10 +148,11 @@ namespace backend { mUsedVertexAttributes.set(location); } - // Without a location qualifier on vertex outputs, spirv_cross::CompilerMSL gives them all - // the location 0, causing a compile error. + // Without a location qualifier on vertex outputs, spirv_cross::CompilerMSL gives them + // all the location 0, causing a compile error. for (const auto& attrib : resources.stage_outputs) { - if (!(compiler.get_decoration_mask(attrib.id) & (1ull << spv::DecorationLocation))) { + if (!(compiler.get_decoration_mask(attrib.id) & + (1ull << spv::DecorationLocation))) { mDevice->HandleError("Need location qualifier on vertex output"); return; } @@ -151,10 +160,11 @@ namespace backend { } if (mExecutionModel == nxt::ShaderStage::Fragment) { - // Without a location qualifier on vertex inputs, spirv_cross::CompilerMSL gives them all - // the location 0, causing a compile error. + // Without a location qualifier on vertex inputs, spirv_cross::CompilerMSL gives them + // all the location 0, causing a compile error. for (const auto& attrib : resources.stage_inputs) { - if (!(compiler.get_decoration_mask(attrib.id) & (1ull << spv::DecorationLocation))) { + if (!(compiler.get_decoration_mask(attrib.id) & + (1ull << spv::DecorationLocation))) { mDevice->HandleError("Need location qualifier on fragment input"); return; } @@ -187,7 +197,8 @@ namespace backend { return true; } - bool ShaderModuleBase::IsCompatibleWithBindGroupLayout(size_t group, const BindGroupLayoutBase* layout) { + bool ShaderModuleBase::IsCompatibleWithBindGroupLayout(size_t group, + const BindGroupLayoutBase* layout) { const auto& layoutInfo = layout->GetBindingInfo(); for (size_t i = 0; i < kMaxBindingsPerGroup; ++i) { const auto& moduleInfo = mBindingInfo[group][i]; @@ -227,4 +238,4 @@ namespace backend { mSpirv.assign(code, code + codeSize); } -} +} // namespace backend diff --git a/src/backend/ShaderModule.h b/src/backend/ShaderModule.h index 034d737ca8..c81cad9082 100644 --- a/src/backend/ShaderModule.h +++ b/src/backend/ShaderModule.h @@ -15,8 +15,8 @@ #ifndef BACKEND_SHADERMODULE_H_ #define BACKEND_SHADERMODULE_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "common/Constants.h" @@ -33,64 +33,65 @@ namespace spirv_cross { namespace backend { class ShaderModuleBase : public RefCounted { - public: - ShaderModuleBase(ShaderModuleBuilder* builder); + public: + ShaderModuleBase(ShaderModuleBuilder* builder); - DeviceBase* GetDevice() const; + DeviceBase* GetDevice() const; - void ExtractSpirvInfo(const spirv_cross::Compiler& compiler); + void ExtractSpirvInfo(const spirv_cross::Compiler& compiler); - struct PushConstantInfo { - std::bitset mask; + struct PushConstantInfo { + std::bitset mask; - std::array names; - std::array sizes; - std::array types; - }; + std::array names; + std::array sizes; + std::array types; + }; - struct BindingInfo { - // The SPIRV ID of the resource. - uint32_t id; - uint32_t base_type_id; - nxt::BindingType type; - bool used = false; - }; - using ModuleBindingInfo = std::array, kMaxBindGroups>; + struct BindingInfo { + // The SPIRV ID of the resource. + uint32_t id; + uint32_t base_type_id; + nxt::BindingType type; + bool used = false; + }; + using ModuleBindingInfo = + std::array, kMaxBindGroups>; - const PushConstantInfo& GetPushConstants() const; - const ModuleBindingInfo& GetBindingInfo() const; - const std::bitset& GetUsedVertexAttributes() const; - nxt::ShaderStage GetExecutionModel() const; + const PushConstantInfo& GetPushConstants() const; + const ModuleBindingInfo& GetBindingInfo() const; + const std::bitset& GetUsedVertexAttributes() const; + nxt::ShaderStage GetExecutionModel() const; - bool IsCompatibleWithPipelineLayout(const PipelineLayoutBase* layout); + bool IsCompatibleWithPipelineLayout(const PipelineLayoutBase* layout); - private: - bool IsCompatibleWithBindGroupLayout(size_t group, const BindGroupLayoutBase* layout); + private: + bool IsCompatibleWithBindGroupLayout(size_t group, const BindGroupLayoutBase* layout); - DeviceBase* mDevice; - PushConstantInfo mPushConstants = {}; - ModuleBindingInfo mBindingInfo; - std::bitset mUsedVertexAttributes; - nxt::ShaderStage mExecutionModel; + DeviceBase* mDevice; + PushConstantInfo mPushConstants = {}; + ModuleBindingInfo mBindingInfo; + std::bitset mUsedVertexAttributes; + nxt::ShaderStage mExecutionModel; }; class ShaderModuleBuilder : public Builder { - public: - ShaderModuleBuilder(DeviceBase* device); + public: + ShaderModuleBuilder(DeviceBase* device); - std::vector AcquireSpirv(); + std::vector AcquireSpirv(); - // NXT API - void SetSource(uint32_t codeSize, const uint32_t* code); + // NXT API + void SetSource(uint32_t codeSize, const uint32_t* code); - private: - friend class ShaderModuleBase; + private: + friend class ShaderModuleBase; - ShaderModuleBase* GetResultImpl() override; + ShaderModuleBase* GetResultImpl() override; - std::vector mSpirv; + std::vector mSpirv; }; -} +} // namespace backend -#endif // BACKEND_SHADERMODULE_H_ +#endif // BACKEND_SHADERMODULE_H_ diff --git a/src/backend/SwapChain.cpp b/src/backend/SwapChain.cpp index acd621b2a4..2eaf7b6609 100644 --- a/src/backend/SwapChain.cpp +++ b/src/backend/SwapChain.cpp @@ -22,7 +22,7 @@ namespace backend { // SwapChain SwapChainBase::SwapChainBase(SwapChainBuilder* builder) - : mDevice(builder->mDevice), mImplementation(builder->mImplementation) { + : mDevice(builder->mDevice), mImplementation(builder->mImplementation) { } SwapChainBase::~SwapChainBase() { @@ -34,7 +34,10 @@ namespace backend { return mDevice; } - void SwapChainBase::Configure(nxt::TextureFormat format, nxt::TextureUsageBit allowedUsage, uint32_t width, uint32_t height) { + void SwapChainBase::Configure(nxt::TextureFormat format, + nxt::TextureUsageBit allowedUsage, + uint32_t width, + uint32_t height) { if (width == 0 || height == 0) { mDevice->HandleError("Swap chain cannot be configured to zero size"); return; @@ -45,8 +48,8 @@ namespace backend { mAllowedUsage = allowedUsage; mWidth = width; mHeight = height; - mImplementation.Configure(mImplementation.userData, - static_cast(format), static_cast(allowedUsage), width, height); + mImplementation.Configure(mImplementation.userData, static_cast(format), + static_cast(allowedUsage), width, height); } TextureBase* SwapChainBase::GetNextTexture() { @@ -87,8 +90,7 @@ namespace backend { // SwapChain Builder - SwapChainBuilder::SwapChainBuilder(DeviceBase* device) - : Builder(device) { + SwapChainBuilder::SwapChainBuilder(DeviceBase* device) : Builder(device) { } SwapChainBase* SwapChainBuilder::GetResultImpl() { @@ -105,14 +107,15 @@ namespace backend { return; } - nxtSwapChainImplementation& impl = *reinterpret_cast(implementation); + nxtSwapChainImplementation& impl = + *reinterpret_cast(implementation); - if (!impl.Init || !impl.Destroy || !impl.Configure || - !impl.GetNextTexture || !impl.Present) { + if (!impl.Init || !impl.Destroy || !impl.Configure || !impl.GetNextTexture || + !impl.Present) { HandleError("Implementation is incomplete"); return; } mImplementation = impl; } -} +} // namespace backend diff --git a/src/backend/SwapChain.h b/src/backend/SwapChain.h index 9a0ef22685..0110af4f3e 100644 --- a/src/backend/SwapChain.h +++ b/src/backend/SwapChain.h @@ -15,55 +15,58 @@ #ifndef BACKEND_SWAPCHAIN_H_ #define BACKEND_SWAPCHAIN_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" -#include "nxt/nxtcpp.h" #include "nxt/nxt_wsi.h" +#include "nxt/nxtcpp.h" namespace backend { class SwapChainBase : public RefCounted { - public: - SwapChainBase(SwapChainBuilder* builder); - ~SwapChainBase(); + public: + SwapChainBase(SwapChainBuilder* builder); + ~SwapChainBase(); - DeviceBase* GetDevice(); + DeviceBase* GetDevice(); - // NXT API - void Configure(nxt::TextureFormat format, nxt::TextureUsageBit allowedUsage, uint32_t width, uint32_t height); - TextureBase* GetNextTexture(); - void Present(TextureBase* texture); + // NXT API + void Configure(nxt::TextureFormat format, + nxt::TextureUsageBit allowedUsage, + uint32_t width, + uint32_t height); + TextureBase* GetNextTexture(); + void Present(TextureBase* texture); - protected: - const nxtSwapChainImplementation& GetImplementation(); - virtual TextureBase* GetNextTextureImpl(TextureBuilder* builder) = 0; + protected: + const nxtSwapChainImplementation& GetImplementation(); + virtual TextureBase* GetNextTextureImpl(TextureBuilder* builder) = 0; - private: - DeviceBase* mDevice = nullptr; - nxtSwapChainImplementation mImplementation = {}; - nxt::TextureFormat mFormat = {}; - nxt::TextureUsageBit mAllowedUsage; - uint32_t mWidth = 0; - uint32_t mHeight = 0; - TextureBase* mLastNextTexture = nullptr; + private: + DeviceBase* mDevice = nullptr; + nxtSwapChainImplementation mImplementation = {}; + nxt::TextureFormat mFormat = {}; + nxt::TextureUsageBit mAllowedUsage; + uint32_t mWidth = 0; + uint32_t mHeight = 0; + TextureBase* mLastNextTexture = nullptr; }; class SwapChainBuilder : public Builder { - public: - SwapChainBuilder(DeviceBase* device); + public: + SwapChainBuilder(DeviceBase* device); - // NXT API - SwapChainBase* GetResultImpl() override; - void SetImplementation(uint64_t implementation); + // NXT API + SwapChainBase* GetResultImpl() override; + void SetImplementation(uint64_t implementation); - private: - friend class SwapChainBase; + private: + friend class SwapChainBase; - nxtSwapChainImplementation mImplementation = {}; + nxtSwapChainImplementation mImplementation = {}; }; -} +} // namespace backend -#endif // BACKEND_SWAPCHAIN_H_ +#endif // BACKEND_SWAPCHAIN_H_ diff --git a/src/backend/Texture.cpp b/src/backend/Texture.cpp index de437d5c58..4d8ee12f0f 100644 --- a/src/backend/Texture.cpp +++ b/src/backend/Texture.cpp @@ -71,13 +71,18 @@ namespace backend { } } - // TextureBase TextureBase::TextureBase(TextureBuilder* builder) - : mDevice(builder->mDevice), mDimension(builder->mDimension), mFormat(builder->mFormat), mWidth(builder->mWidth), - mHeight(builder->mHeight), mDepth(builder->mDepth), mNumMipLevels(builder->mNumMipLevels), - mAllowedUsage(builder->mAllowedUsage), mCurrentUsage(builder->mCurrentUsage) { + : mDevice(builder->mDevice), + mDimension(builder->mDimension), + mFormat(builder->mFormat), + mWidth(builder->mWidth), + mHeight(builder->mHeight), + mDepth(builder->mDepth), + mNumMipLevels(builder->mNumMipLevels), + mAllowedUsage(builder->mAllowedUsage), + mCurrentUsage(builder->mCurrentUsage) { } DeviceBase* TextureBase::GetDevice() { @@ -121,7 +126,8 @@ namespace backend { return mIsFrozen && (usage & mAllowedUsage); } - bool TextureBase::IsUsagePossible(nxt::TextureUsageBit allowedUsage, nxt::TextureUsageBit usage) { + bool TextureBase::IsUsagePossible(nxt::TextureUsageBit allowedUsage, + nxt::TextureUsageBit usage) { bool allowed = (usage & allowedUsage) == usage; bool singleUse = nxt::HasZeroOrOneBits(usage); return allowed && singleUse; @@ -173,13 +179,13 @@ namespace backend { TEXTURE_PROPERTY_INITIAL_USAGE = 0x20, }; - TextureBuilder::TextureBuilder(DeviceBase* device) - : Builder(device) { + TextureBuilder::TextureBuilder(DeviceBase* device) : Builder(device) { } TextureBase* TextureBuilder::GetResultImpl() { constexpr int allProperties = TEXTURE_PROPERTY_DIMENSION | TEXTURE_PROPERTY_EXTENT | - TEXTURE_PROPERTY_FORMAT | TEXTURE_PROPERTY_MIP_LEVELS | TEXTURE_PROPERTY_ALLOWED_USAGE; + TEXTURE_PROPERTY_FORMAT | TEXTURE_PROPERTY_MIP_LEVELS | + TEXTURE_PROPERTY_ALLOWED_USAGE; if ((mPropertiesSet & allProperties) != allProperties) { HandleError("Texture missing properties"); return nullptr; @@ -264,8 +270,7 @@ namespace backend { // TextureViewBase - TextureViewBase::TextureViewBase(TextureViewBuilder* builder) - : mTexture(builder->mTexture) { + TextureViewBase::TextureViewBase(TextureViewBuilder* builder) : mTexture(builder->mTexture) { } TextureBase* TextureViewBase::GetTexture() { @@ -282,4 +287,4 @@ namespace backend { return mDevice->CreateTextureView(this); } -} +} // namespace backend diff --git a/src/backend/Texture.h b/src/backend/Texture.h index 5775c73862..2f380f979d 100644 --- a/src/backend/Texture.h +++ b/src/backend/Texture.h @@ -15,8 +15,8 @@ #ifndef BACKEND_TEXTURE_H_ #define BACKEND_TEXTURE_H_ -#include "backend/Forward.h" #include "backend/Builder.h" +#include "backend/Forward.h" #include "backend/RefCounted.h" #include "nxt/nxtcpp.h" @@ -29,93 +29,94 @@ namespace backend { bool TextureFormatHasDepthOrStencil(nxt::TextureFormat format); class TextureBase : public RefCounted { - public: - TextureBase(TextureBuilder* builder); + public: + TextureBase(TextureBuilder* builder); - nxt::TextureDimension GetDimension() const; - nxt::TextureFormat GetFormat() const; - uint32_t GetWidth() const; - uint32_t GetHeight() const; - uint32_t GetDepth() const; - uint32_t GetNumMipLevels() const; - nxt::TextureUsageBit GetAllowedUsage() const; - nxt::TextureUsageBit GetUsage() const; - bool IsFrozen() const; - bool HasFrozenUsage(nxt::TextureUsageBit usage) const; - static bool IsUsagePossible(nxt::TextureUsageBit allowedUsage, nxt::TextureUsageBit usage); - bool IsTransitionPossible(nxt::TextureUsageBit usage) const; - void UpdateUsageInternal(nxt::TextureUsageBit usage); + nxt::TextureDimension GetDimension() const; + nxt::TextureFormat GetFormat() const; + uint32_t GetWidth() const; + uint32_t GetHeight() const; + uint32_t GetDepth() const; + uint32_t GetNumMipLevels() const; + nxt::TextureUsageBit GetAllowedUsage() const; + nxt::TextureUsageBit GetUsage() const; + bool IsFrozen() const; + bool HasFrozenUsage(nxt::TextureUsageBit usage) const; + static bool IsUsagePossible(nxt::TextureUsageBit allowedUsage, nxt::TextureUsageBit usage); + bool IsTransitionPossible(nxt::TextureUsageBit usage) const; + void UpdateUsageInternal(nxt::TextureUsageBit usage); - DeviceBase* GetDevice(); + DeviceBase* GetDevice(); - // NXT API - TextureViewBuilder* CreateTextureViewBuilder(); - void TransitionUsage(nxt::TextureUsageBit usage); - void FreezeUsage(nxt::TextureUsageBit usage); + // NXT API + TextureViewBuilder* CreateTextureViewBuilder(); + void TransitionUsage(nxt::TextureUsageBit usage); + void FreezeUsage(nxt::TextureUsageBit usage); - virtual void TransitionUsageImpl(nxt::TextureUsageBit currentUsage, nxt::TextureUsageBit targetUsage) = 0; + virtual void TransitionUsageImpl(nxt::TextureUsageBit currentUsage, + nxt::TextureUsageBit targetUsage) = 0; - private: - DeviceBase* mDevice; + private: + DeviceBase* mDevice; - nxt::TextureDimension mDimension; - nxt::TextureFormat mFormat; - uint32_t mWidth, mHeight, mDepth; - uint32_t mNumMipLevels; - nxt::TextureUsageBit mAllowedUsage = nxt::TextureUsageBit::None; - nxt::TextureUsageBit mCurrentUsage = nxt::TextureUsageBit::None; - bool mIsFrozen = false; + nxt::TextureDimension mDimension; + nxt::TextureFormat mFormat; + uint32_t mWidth, mHeight, mDepth; + uint32_t mNumMipLevels; + nxt::TextureUsageBit mAllowedUsage = nxt::TextureUsageBit::None; + nxt::TextureUsageBit mCurrentUsage = nxt::TextureUsageBit::None; + bool mIsFrozen = false; }; class TextureBuilder : public Builder { - public: - TextureBuilder(DeviceBase* device); + public: + TextureBuilder(DeviceBase* device); - // NXT API - void SetDimension(nxt::TextureDimension dimension); - void SetExtent(uint32_t width, uint32_t height, uint32_t depth); - void SetFormat(nxt::TextureFormat format); - void SetMipLevels(uint32_t numMipLevels); - void SetAllowedUsage(nxt::TextureUsageBit usage); - void SetInitialUsage(nxt::TextureUsageBit usage); + // NXT API + void SetDimension(nxt::TextureDimension dimension); + void SetExtent(uint32_t width, uint32_t height, uint32_t depth); + void SetFormat(nxt::TextureFormat format); + void SetMipLevels(uint32_t numMipLevels); + void SetAllowedUsage(nxt::TextureUsageBit usage); + void SetInitialUsage(nxt::TextureUsageBit usage); - private: - friend class TextureBase; + private: + friend class TextureBase; - TextureBase* GetResultImpl() override; + TextureBase* GetResultImpl() override; - int mPropertiesSet = 0; + int mPropertiesSet = 0; - nxt::TextureDimension mDimension; - uint32_t mWidth, mHeight, mDepth; - nxt::TextureFormat mFormat; - uint32_t mNumMipLevels; - nxt::TextureUsageBit mAllowedUsage = nxt::TextureUsageBit::None; - nxt::TextureUsageBit mCurrentUsage = nxt::TextureUsageBit::None; + nxt::TextureDimension mDimension; + uint32_t mWidth, mHeight, mDepth; + nxt::TextureFormat mFormat; + uint32_t mNumMipLevels; + nxt::TextureUsageBit mAllowedUsage = nxt::TextureUsageBit::None; + nxt::TextureUsageBit mCurrentUsage = nxt::TextureUsageBit::None; }; class TextureViewBase : public RefCounted { - public: - TextureViewBase(TextureViewBuilder* builder); + public: + TextureViewBase(TextureViewBuilder* builder); - TextureBase* GetTexture(); + TextureBase* GetTexture(); - private: - Ref mTexture; + private: + Ref mTexture; }; class TextureViewBuilder : public Builder { - public: - TextureViewBuilder(DeviceBase* device, TextureBase* texture); + public: + TextureViewBuilder(DeviceBase* device, TextureBase* texture); - private: - friend class TextureViewBase; + private: + friend class TextureViewBase; - TextureViewBase* GetResultImpl() override; + TextureViewBase* GetResultImpl() override; - Ref mTexture; + Ref mTexture; }; -} +} // namespace backend -#endif // BACKEND_TEXTURE_H_ +#endif // BACKEND_TEXTURE_H_ diff --git a/src/backend/ToBackend.h b/src/backend/ToBackend.h index 07aacf8ad2..b8ed161260 100644 --- a/src/backend/ToBackend.h +++ b/src/backend/ToBackend.h @@ -20,105 +20,105 @@ namespace backend { // ToBackendTraits implements the mapping from base type to member type of BackendTraits - template + template struct ToBackendTraits; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::BindGroupType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::BindGroupLayoutType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::BlendStateType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::BufferType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::BufferViewType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::CommandBufferType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::ComputePipelineType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::DepthStencilStateType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::DeviceType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::FramebufferType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::InputStateType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::PipelineLayoutType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::QueueType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::RenderPassType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::RenderPipelineType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::SamplerType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::ShaderModuleType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::TextureType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::SwapChainType; }; - template + template struct ToBackendTraits { using BackendType = typename BackendTraits::TextureViewType; }; @@ -130,26 +130,30 @@ namespace backend { // return ToBackendBase(common); // } - template + template Ref::BackendType>& ToBackendBase(Ref& common) { - return reinterpret_cast::BackendType>&>(common); + return reinterpret_cast::BackendType>&>( + common); } - template - const Ref::BackendType>& ToBackendBase(const Ref& common) { - return reinterpret_cast::BackendType>&>(common); + template + const Ref::BackendType>& ToBackendBase( + const Ref& common) { + return reinterpret_cast< + const Ref::BackendType>&>(common); } - template + template typename ToBackendTraits::BackendType* ToBackendBase(T* common) { return reinterpret_cast::BackendType*>(common); } - template + template const typename ToBackendTraits::BackendType* ToBackendBase(const T* common) { - return reinterpret_cast::BackendType*>(common); + return reinterpret_cast::BackendType*>( + common); } -} +} // namespace backend -#endif // BACKEND_TOBACKEND_H_ +#endif // BACKEND_TOBACKEND_H_