1 // Copyright 2017 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef DAWNNATIVE_RENDERPIPELINE_H_ 16 #define DAWNNATIVE_RENDERPIPELINE_H_ 17 18 #include "dawn_native/Pipeline.h" 19 20 #include "dawn_native/dawn_platform.h" 21 22 #include <array> 23 #include <bitset> 24 25 namespace dawn_native { 26 27 struct BeginRenderPassCmd; 28 29 class DeviceBase; 30 31 MaybeError ValidateRenderPipelineDescriptor(const DeviceBase* device, 32 const RenderPipelineDescriptor* descriptor); 33 size_t IndexFormatSize(dawn::IndexFormat format); 34 uint32_t VertexFormatNumComponents(dawn::VertexFormat format); 35 size_t VertexFormatComponentSize(dawn::VertexFormat format); 36 size_t VertexFormatSize(dawn::VertexFormat format); 37 38 bool StencilTestEnabled(const DepthStencilStateDescriptor* mDepthStencilState); 39 bool BlendEnabled(const ColorStateDescriptor* mColorState); 40 41 struct VertexAttributeInfo { 42 uint32_t shaderLocation; 43 uint32_t inputSlot; 44 uint64_t offset; 45 dawn::VertexFormat format; 46 }; 47 48 struct VertexBufferInfo { 49 uint64_t stride; 50 dawn::InputStepMode stepMode; 51 }; 52 53 class RenderPipelineBase : public PipelineBase { 54 public: 55 RenderPipelineBase(DeviceBase* device, 56 const RenderPipelineDescriptor* descriptor, 57 bool blueprint = false); 58 ~RenderPipelineBase() override; 59 60 static RenderPipelineBase* MakeError(DeviceBase* device); 61 62 const VertexInputDescriptor* GetVertexInputDescriptor() const; 63 const std::bitset<kMaxVertexAttributes>& GetAttributesSetMask() const; 64 const VertexAttributeInfo& GetAttribute(uint32_t location) const; 65 const std::bitset<kMaxVertexBuffers>& GetInputsSetMask() const; 66 const VertexBufferInfo& GetInput(uint32_t slot) const; 67 68 const ColorStateDescriptor* GetColorStateDescriptor(uint32_t attachmentSlot) const; 69 const DepthStencilStateDescriptor* GetDepthStencilStateDescriptor() const; 70 dawn::PrimitiveTopology GetPrimitiveTopology() const; 71 dawn::CullMode GetCullMode() const; 72 dawn::FrontFace GetFrontFace() const; 73 74 std::bitset<kMaxColorAttachments> GetColorAttachmentsMask() const; 75 bool HasDepthStencilAttachment() const; 76 dawn::TextureFormat GetColorAttachmentFormat(uint32_t attachment) const; 77 dawn::TextureFormat GetDepthStencilFormat() const; 78 uint32_t GetSampleCount() const; 79 80 // A pipeline can be used in a render pass if its attachment info matches the actual 81 // attachments in the render pass. This returns whether it is the case. 82 MaybeError ValidateCompatibleWith(const BeginRenderPassCmd* renderPassCmd) const; 83 std::bitset<kMaxVertexAttributes> GetAttributesUsingInput(uint32_t slot) const; 84 std::array<std::bitset<kMaxVertexAttributes>, kMaxVertexBuffers> attributesUsingInput; 85 86 // Functors necessary for the unordered_set<RenderPipelineBase*>-based cache. 87 struct HashFunc { 88 size_t operator()(const RenderPipelineBase* pipeline) const; 89 }; 90 struct EqualityFunc { 91 bool operator()(const RenderPipelineBase* a, const RenderPipelineBase* b) const; 92 }; 93 94 private: 95 RenderPipelineBase(DeviceBase* device, ObjectBase::ErrorTag tag); 96 97 // Vertex input 98 VertexInputDescriptor mVertexInput; 99 std::bitset<kMaxVertexAttributes> mAttributesSetMask; 100 std::array<VertexAttributeInfo, kMaxVertexAttributes> mAttributeInfos; 101 std::bitset<kMaxVertexBuffers> mInputsSetMask; 102 std::array<VertexBufferInfo, kMaxVertexBuffers> mInputInfos; 103 104 // Attachments 105 bool mHasDepthStencilAttachment = false; 106 DepthStencilStateDescriptor mDepthStencilState; 107 std::bitset<kMaxColorAttachments> mColorAttachmentsSet; 108 std::array<ColorStateDescriptor, kMaxColorAttachments> mColorStates; 109 110 // Other state 111 dawn::PrimitiveTopology mPrimitiveTopology; 112 RasterizationStateDescriptor mRasterizationState; 113 uint32_t mSampleCount; 114 uint32_t mSampleMask; 115 bool mAlphaToCoverageEnabled; 116 117 // Stage information 118 // TODO(cwallez@chromium.org): Store a crypto hash of the modules instead. 119 Ref<ShaderModuleBase> mVertexModule; 120 std::string mVertexEntryPoint; 121 Ref<ShaderModuleBase> mFragmentModule; 122 std::string mFragmentEntryPoint; 123 124 bool mIsBlueprint = false; 125 }; 126 127 } // namespace dawn_native 128 129 #endif // DAWNNATIVE_RENDERPIPELINE_H_ 130