1 // Copyright 2020 The SwiftShader Authors. All Rights Reserved. 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 vk_Context_hpp 16 #define vk_Context_hpp 17 18 #include "Config.hpp" 19 #include "Memset.hpp" 20 #include "Stream.hpp" 21 #include "System/Types.hpp" 22 #include "Vulkan/VkDescriptorSet.hpp" 23 24 #include <vector> 25 26 namespace vk { 27 28 class Buffer; 29 class Device; 30 class ImageView; 31 class PipelineLayout; 32 33 struct VertexInputBinding 34 { 35 Buffer *buffer; 36 VkDeviceSize offset; 37 }; 38 39 struct IndexBuffer 40 { getIndexTypevk::IndexBuffer41 inline VkIndexType getIndexType() const { return indexType; } 42 void setIndexBufferBinding(const VertexInputBinding &indexBufferBinding, VkIndexType type); 43 void getIndexBuffers(VkPrimitiveTopology topology, uint32_t count, uint32_t first, bool indexed, bool hasPrimitiveRestartEnable, std::vector<std::pair<uint32_t, void *>> *indexBuffers) const; 44 45 private: 46 int bytesPerIndex() const; 47 48 VertexInputBinding binding; 49 VkIndexType indexType; 50 }; 51 52 struct Attachments 53 { 54 ImageView *colorBuffer[sw::MAX_COLOR_BUFFERS] = {}; 55 ImageView *depthBuffer = nullptr; 56 ImageView *stencilBuffer = nullptr; 57 58 bool isColorClamped(int index) const; 59 VkFormat colorFormat(int index) const; 60 VkFormat depthFormat() const; 61 }; 62 63 struct Inputs 64 { 65 Inputs(const VkPipelineVertexInputStateCreateInfo *vertexInputState); 66 67 void updateDescriptorSets(const DescriptorSet::Array &dso, 68 const DescriptorSet::Bindings &ds, 69 const DescriptorSet::DynamicOffsets &ddo); getDescriptorSetObjectsvk::Inputs70 inline const DescriptorSet::Array &getDescriptorSetObjects() const { return descriptorSetObjects; } getDescriptorSetsvk::Inputs71 inline const DescriptorSet::Bindings &getDescriptorSets() const { return descriptorSets; } getDescriptorDynamicOffsetsvk::Inputs72 inline const DescriptorSet::DynamicOffsets &getDescriptorDynamicOffsets() const { return descriptorDynamicOffsets; } getStreamvk::Inputs73 inline const sw::Stream &getStream(uint32_t i) const { return stream[i]; } 74 75 void bindVertexInputs(int firstInstance); 76 void setVertexInputBinding(const VertexInputBinding vertexInputBindings[]); 77 void advanceInstanceAttributes(); 78 79 private: 80 VertexInputBinding vertexInputBindings[MAX_VERTEX_INPUT_BINDINGS] = {}; 81 DescriptorSet::Array descriptorSetObjects = {}; 82 DescriptorSet::Bindings descriptorSets = {}; 83 DescriptorSet::DynamicOffsets descriptorDynamicOffsets = {}; 84 sw::Stream stream[sw::MAX_INTERFACE_COMPONENTS / 4]; 85 }; 86 87 struct BlendState : sw::Memset<BlendState> 88 { BlendStatevk::BlendState89 BlendState() 90 : Memset(this, 0) 91 {} 92 BlendStatevk::BlendState93 BlendState(bool alphaBlendEnable, 94 VkBlendFactor sourceBlendFactor, 95 VkBlendFactor destBlendFactor, 96 VkBlendOp blendOperation, 97 VkBlendFactor sourceBlendFactorAlpha, 98 VkBlendFactor destBlendFactorAlpha, 99 VkBlendOp blendOperationAlpha) 100 : Memset(this, 0) 101 , alphaBlendEnable(alphaBlendEnable) 102 , sourceBlendFactor(sourceBlendFactor) 103 , destBlendFactor(destBlendFactor) 104 , blendOperation(blendOperation) 105 , sourceBlendFactorAlpha(sourceBlendFactorAlpha) 106 , destBlendFactorAlpha(destBlendFactorAlpha) 107 , blendOperationAlpha(blendOperationAlpha) 108 {} 109 110 bool alphaBlendEnable; 111 VkBlendFactor sourceBlendFactor; 112 VkBlendFactor destBlendFactor; 113 VkBlendOp blendOperation; 114 VkBlendFactor sourceBlendFactorAlpha; 115 VkBlendFactor destBlendFactorAlpha; 116 VkBlendOp blendOperationAlpha; 117 }; 118 119 struct DynamicState 120 { 121 VkViewport viewport; 122 VkRect2D scissor; 123 sw::float4 blendConstants; 124 float depthBiasConstantFactor = 0.0f; 125 float depthBiasClamp = 0.0f; 126 float depthBiasSlopeFactor = 0.0f; 127 float minDepthBounds = 0.0f; 128 float maxDepthBounds = 0.0f; 129 130 uint32_t compareMask[2] = { 0 }; 131 uint32_t writeMask[2] = { 0 }; 132 uint32_t reference[2] = { 0 }; 133 }; 134 135 struct GraphicsState 136 { 137 GraphicsState(const Device *device, const VkGraphicsPipelineCreateInfo *pCreateInfo, const PipelineLayout *layout, bool robustBufferAccess); 138 139 const GraphicsState combineStates(const DynamicState &dynamicState) const; 140 getPipelineLayoutvk::GraphicsState141 inline const PipelineLayout *getPipelineLayout() const { return pipelineLayout; } getRobustBufferAccessvk::GraphicsState142 inline bool getRobustBufferAccess() const { return robustBufferAccess; } getTopologyvk::GraphicsState143 inline VkPrimitiveTopology getTopology() const { return topology; } 144 getProvokingVertexModevk::GraphicsState145 inline VkProvokingVertexModeEXT getProvokingVertexMode() const { return provokingVertexMode; } 146 getFrontStencilvk::GraphicsState147 inline VkStencilOpState getFrontStencil() const { return frontStencil; } getBackStencilvk::GraphicsState148 inline VkStencilOpState getBackStencil() const { return backStencil; } 149 150 // Pixel processor states getCullModevk::GraphicsState151 inline VkCullModeFlags getCullMode() const { return cullMode; } getFrontFacevk::GraphicsState152 inline VkFrontFace getFrontFace() const { return frontFace; } getPolygonModevk::GraphicsState153 inline VkPolygonMode getPolygonMode() const { return polygonMode; } getLineRasterizationModevk::GraphicsState154 inline VkLineRasterizationModeEXT getLineRasterizationMode() const { return lineRasterizationMode; } 155 getConstantDepthBiasvk::GraphicsState156 inline float getConstantDepthBias() const { return constantDepthBias; } getSlopeDepthBiasvk::GraphicsState157 inline float getSlopeDepthBias() const { return slopeDepthBias; } getDepthBiasClampvk::GraphicsState158 inline float getDepthBiasClamp() const { return depthBiasClamp; } getMinDepthBoundsvk::GraphicsState159 inline float getMinDepthBounds() const { return minDepthBounds; } getMaxDepthBoundsvk::GraphicsState160 inline float getMaxDepthBounds() const { return maxDepthBounds; } hasDepthRangeUnrestrictedvk::GraphicsState161 inline bool hasDepthRangeUnrestricted() const { return depthRangeUnrestricted; } getDepthClampEnablevk::GraphicsState162 inline bool getDepthClampEnable() const { return depthClampEnable; } getDepthClipEnablevk::GraphicsState163 inline bool getDepthClipEnable() const { return depthClipEnable; } 164 165 // Pixel processor states hasRasterizerDiscardvk::GraphicsState166 inline bool hasRasterizerDiscard() const { return rasterizerDiscard; } getDepthCompareModevk::GraphicsState167 inline VkCompareOp getDepthCompareMode() const { return depthCompareMode; } 168 getLineWidthvk::GraphicsState169 inline float getLineWidth() const { return lineWidth; } 170 getMultiSampleMaskvk::GraphicsState171 inline unsigned int getMultiSampleMask() const { return multiSampleMask; } getSampleCountvk::GraphicsState172 inline int getSampleCount() const { return sampleCount; } hasSampleShadingEnabledvk::GraphicsState173 inline bool hasSampleShadingEnabled() const { return sampleShadingEnable; } getMinSampleShadingvk::GraphicsState174 inline float getMinSampleShading() const { return minSampleShading; } hasAlphaToCoveragevk::GraphicsState175 inline bool hasAlphaToCoverage() const { return alphaToCoverage; } 176 hasPrimitiveRestartEnablevk::GraphicsState177 inline bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; } getScissorvk::GraphicsState178 inline const VkRect2D &getScissor() const { return scissor; } getViewportvk::GraphicsState179 inline const VkViewport &getViewport() const { return viewport; } getBlendConstantsvk::GraphicsState180 inline const sw::float4 &getBlendConstants() const { return blendConstants; } 181 182 bool isDrawPoint(bool polygonModeAware) const; 183 bool isDrawLine(bool polygonModeAware) const; 184 bool isDrawTriangle(bool polygonModeAware) const; 185 186 BlendState getBlendState(int index, const Attachments &attachments, bool fragmentContainsKill) const; 187 188 int colorWriteActive(int index, const Attachments &attachments) const; 189 bool depthWriteActive(const Attachments &attachments) const; 190 bool depthTestActive(const Attachments &attachments) const; 191 bool stencilActive(const Attachments &attachments) const; 192 bool depthBoundsTestActive(const Attachments &attachments) const; 193 194 private: hasDynamicStatevk::GraphicsState195 inline bool hasDynamicState(VkDynamicState dynamicState) const { return (dynamicStateFlags & (1 << dynamicState)) != 0; } 196 197 VkBlendFactor sourceBlendFactor(int index) const; 198 VkBlendFactor destBlendFactor(int index) const; 199 VkBlendOp blendOperation(int index, const Attachments &attachments) const; 200 201 VkBlendFactor sourceBlendFactorAlpha(int index) const; 202 VkBlendFactor destBlendFactorAlpha(int index) const; 203 VkBlendOp blendOperationAlpha(int index, const Attachments &attachments) const; 204 205 bool alphaBlendActive(int index, const Attachments &attachments, bool fragmentContainsKill) const; 206 bool colorWriteActive(const Attachments &attachments) const; 207 208 const PipelineLayout *pipelineLayout = nullptr; 209 const bool robustBufferAccess = false; 210 uint32_t dynamicStateFlags = 0; 211 VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; 212 213 VkProvokingVertexModeEXT provokingVertexMode = VK_PROVOKING_VERTEX_MODE_FIRST_VERTEX_EXT; 214 215 bool stencilEnable = false; 216 VkStencilOpState frontStencil = {}; 217 VkStencilOpState backStencil = {}; 218 219 // Pixel processor states 220 VkCullModeFlags cullMode = 0; 221 VkFrontFace frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; 222 VkPolygonMode polygonMode = VK_POLYGON_MODE_FILL; 223 VkLineRasterizationModeEXT lineRasterizationMode = VK_LINE_RASTERIZATION_MODE_DEFAULT_EXT; 224 225 float constantDepthBias = 0.0f; 226 float slopeDepthBias = 0.0f; 227 float depthBiasClamp = 0.0f; 228 float minDepthBounds = 0.0f; 229 float maxDepthBounds = 0.0f; 230 bool depthRangeUnrestricted = false; 231 232 // Pixel processor states 233 bool rasterizerDiscard = false; 234 bool depthBoundsTestEnable = false; 235 bool depthTestEnable = false; 236 VkCompareOp depthCompareMode = VK_COMPARE_OP_NEVER; 237 bool depthWriteEnable = false; 238 bool depthClampEnable = false; 239 bool depthClipEnable = false; 240 241 float lineWidth = 0.0f; 242 243 int colorWriteMask[sw::MAX_COLOR_BUFFERS] = {}; // RGBA 244 unsigned int multiSampleMask = 0; 245 int sampleCount = 0; 246 bool alphaToCoverage = false; 247 248 bool sampleShadingEnable = false; 249 float minSampleShading = 0.0f; 250 251 bool primitiveRestartEnable = false; 252 VkRect2D scissor = {}; 253 VkViewport viewport = {}; 254 sw::float4 blendConstants = {}; 255 256 BlendState blendState[sw::MAX_COLOR_BUFFERS] = {}; 257 }; 258 259 } // namespace vk 260 261 #endif // vk_Context_hpp 262