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 *renderTarget[sw::RENDERTARGETS] = {}; 55 ImageView *depthBuffer = nullptr; 56 ImageView *stencilBuffer = nullptr; 57 58 bool isColorClamped(int index) const; 59 VkFormat renderTargetInternalFormat(int index) const; 60 }; 61 62 struct Inputs 63 { 64 Inputs(const VkPipelineVertexInputStateCreateInfo *vertexInputState); 65 66 void updateDescriptorSets(const DescriptorSet::Array &dso, 67 const DescriptorSet::Bindings &ds, 68 const DescriptorSet::DynamicOffsets &ddo); getDescriptorSetObjectsvk::Inputs69 inline const DescriptorSet::Array &getDescriptorSetObjects() const { return descriptorSetObjects; } getDescriptorSetsvk::Inputs70 inline const DescriptorSet::Bindings &getDescriptorSets() const { return descriptorSets; } getDescriptorDynamicOffsetsvk::Inputs71 inline const DescriptorSet::DynamicOffsets &getDescriptorDynamicOffsets() const { return descriptorDynamicOffsets; } getStreamvk::Inputs72 inline const sw::Stream &getStream(uint32_t i) const { return stream[i]; } 73 74 void bindVertexInputs(int firstInstance); 75 void setVertexInputBinding(const VertexInputBinding vertexInputBindings[]); 76 void advanceInstanceAttributes(); 77 78 private: 79 VertexInputBinding vertexInputBindings[MAX_VERTEX_INPUT_BINDINGS] = {}; 80 DescriptorSet::Array descriptorSetObjects = {}; 81 DescriptorSet::Bindings descriptorSets = {}; 82 DescriptorSet::DynamicOffsets descriptorDynamicOffsets = {}; 83 sw::Stream stream[sw::MAX_INTERFACE_COMPONENTS / 4]; 84 }; 85 86 struct BlendState : sw::Memset<BlendState> 87 { BlendStatevk::BlendState88 BlendState() 89 : Memset(this, 0) 90 {} 91 BlendStatevk::BlendState92 BlendState(bool alphaBlendEnable, 93 VkBlendFactor sourceBlendFactor, 94 VkBlendFactor destBlendFactor, 95 VkBlendOp blendOperation, 96 VkBlendFactor sourceBlendFactorAlpha, 97 VkBlendFactor destBlendFactorAlpha, 98 VkBlendOp blendOperationAlpha) 99 : Memset(this, 0) 100 , alphaBlendEnable(alphaBlendEnable) 101 , sourceBlendFactor(sourceBlendFactor) 102 , destBlendFactor(destBlendFactor) 103 , blendOperation(blendOperation) 104 , sourceBlendFactorAlpha(sourceBlendFactorAlpha) 105 , destBlendFactorAlpha(destBlendFactorAlpha) 106 , blendOperationAlpha(blendOperationAlpha) 107 {} 108 109 bool alphaBlendEnable; 110 VkBlendFactor sourceBlendFactor; 111 VkBlendFactor destBlendFactor; 112 VkBlendOp blendOperation; 113 VkBlendFactor sourceBlendFactorAlpha; 114 VkBlendFactor destBlendFactorAlpha; 115 VkBlendOp blendOperationAlpha; 116 }; 117 118 struct DynamicState 119 { 120 VkViewport viewport; 121 VkRect2D scissor; 122 sw::float4 blendConstants; 123 float depthBiasConstantFactor = 0.0f; 124 float depthBiasClamp = 0.0f; 125 float depthBiasSlopeFactor = 0.0f; 126 float minDepthBounds = 0.0f; 127 float maxDepthBounds = 0.0f; 128 129 uint32_t compareMask[2] = { 0 }; 130 uint32_t writeMask[2] = { 0 }; 131 uint32_t reference[2] = { 0 }; 132 }; 133 134 struct GraphicsState 135 { 136 GraphicsState(const Device *device, const VkGraphicsPipelineCreateInfo *pCreateInfo, const PipelineLayout *layout, bool robustBufferAccess); 137 138 const GraphicsState combineStates(const DynamicState &dynamicState) const; 139 getPipelineLayoutvk::GraphicsState140 inline const PipelineLayout *getPipelineLayout() const { return pipelineLayout; } getRobustBufferAccessvk::GraphicsState141 inline bool getRobustBufferAccess() const { return robustBufferAccess; } getTopologyvk::GraphicsState142 inline VkPrimitiveTopology getTopology() const { return topology; } 143 getProvokingVertexModevk::GraphicsState144 inline VkProvokingVertexModeEXT getProvokingVertexMode() const { return provokingVertexMode; } 145 getFrontStencilvk::GraphicsState146 inline VkStencilOpState getFrontStencil() const { return frontStencil; } getBackStencilvk::GraphicsState147 inline VkStencilOpState getBackStencil() const { return backStencil; } 148 149 // Pixel processor states getCullModevk::GraphicsState150 inline VkCullModeFlags getCullMode() const { return cullMode; } getFrontFacevk::GraphicsState151 inline VkFrontFace getFrontFace() const { return frontFace; } getPolygonModevk::GraphicsState152 inline VkPolygonMode getPolygonMode() const { return polygonMode; } getLineRasterizationModevk::GraphicsState153 inline VkLineRasterizationModeEXT getLineRasterizationMode() const { return lineRasterizationMode; } 154 getConstantDepthBiasvk::GraphicsState155 inline float getConstantDepthBias() const { return constantDepthBias; } getSlopeDepthBiasvk::GraphicsState156 inline float getSlopeDepthBias() const { return slopeDepthBias; } getDepthBiasClampvk::GraphicsState157 inline float getDepthBiasClamp() const { return depthBiasClamp; } hasDepthRangeUnrestrictedvk::GraphicsState158 inline bool hasDepthRangeUnrestricted() const { return depthRangeUnrestricted; } 159 160 // Pixel processor states hasRasterizerDiscardvk::GraphicsState161 inline bool hasRasterizerDiscard() const { return rasterizerDiscard; } getDepthCompareModevk::GraphicsState162 inline VkCompareOp getDepthCompareMode() const { return depthCompareMode; } 163 getLineWidthvk::GraphicsState164 inline float getLineWidth() const { return lineWidth; } 165 getMultiSampleMaskvk::GraphicsState166 inline unsigned int getMultiSampleMask() const { return multiSampleMask; } getSampleCountvk::GraphicsState167 inline int getSampleCount() const { return sampleCount; } hasSampleShadingEnabledvk::GraphicsState168 inline bool hasSampleShadingEnabled() const { return sampleShadingEnable; } getMinSampleShadingvk::GraphicsState169 inline float getMinSampleShading() const { return minSampleShading; } hasAlphaToCoveragevk::GraphicsState170 inline bool hasAlphaToCoverage() const { return alphaToCoverage; } 171 hasPrimitiveRestartEnablevk::GraphicsState172 inline bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; } getScissorvk::GraphicsState173 inline const VkRect2D &getScissor() const { return scissor; } getViewportvk::GraphicsState174 inline const VkViewport &getViewport() const { return viewport; } getBlendConstantsvk::GraphicsState175 inline const sw::float4 &getBlendConstants() const { return blendConstants; } 176 177 bool isDrawPoint(bool polygonModeAware) const; 178 bool isDrawLine(bool polygonModeAware) const; 179 bool isDrawTriangle(bool polygonModeAware) const; 180 181 BlendState getBlendState(int index, const Attachments &attachments, bool fragmentContainsKill) const; 182 183 int colorWriteActive(int index, const Attachments &attachments) const; 184 bool depthWriteActive(const Attachments &attachments) const; 185 bool depthBufferActive(const Attachments &attachments) const; 186 bool stencilActive(const Attachments &attachments) const; 187 188 private: hasDynamicStatevk::GraphicsState189 inline bool hasDynamicState(VkDynamicState dynamicState) const { return (dynamicStateFlags & (1 << dynamicState)) != 0; } 190 191 VkBlendFactor sourceBlendFactor(int index) const; 192 VkBlendFactor destBlendFactor(int index) const; 193 VkBlendOp blendOperation(int index, const Attachments &attachments) const; 194 195 VkBlendFactor sourceBlendFactorAlpha(int index) const; 196 VkBlendFactor destBlendFactorAlpha(int index) const; 197 VkBlendOp blendOperationAlpha(int index, const Attachments &attachments) const; 198 199 bool alphaBlendActive(int index, const Attachments &attachments, bool fragmentContainsKill) const; 200 bool colorWriteActive(const Attachments &attachments) const; 201 202 const PipelineLayout *pipelineLayout; 203 const bool robustBufferAccess = true; 204 uint32_t dynamicStateFlags = 0; 205 VkPrimitiveTopology topology; 206 207 VkProvokingVertexModeEXT provokingVertexMode; 208 209 bool stencilEnable; 210 VkStencilOpState frontStencil; 211 VkStencilOpState backStencil; 212 213 // Pixel processor states 214 VkCullModeFlags cullMode; 215 VkFrontFace frontFace; 216 VkPolygonMode polygonMode; 217 VkLineRasterizationModeEXT lineRasterizationMode; 218 219 float constantDepthBias; 220 float slopeDepthBias; 221 float depthBiasClamp; 222 bool depthRangeUnrestricted; 223 224 // Pixel processor states 225 bool rasterizerDiscard; 226 bool depthBoundsTestEnable; 227 bool depthBufferEnable; 228 VkCompareOp depthCompareMode; 229 bool depthWriteEnable; 230 231 float lineWidth; 232 233 int colorWriteMask[sw::RENDERTARGETS]; // RGBA 234 unsigned int multiSampleMask; 235 int sampleCount; 236 bool alphaToCoverage; 237 238 bool sampleShadingEnable = false; 239 float minSampleShading = 0.0f; 240 241 bool primitiveRestartEnable = false; 242 VkRect2D scissor; 243 VkViewport viewport; 244 sw::float4 blendConstants; 245 246 BlendState blendState[sw::RENDERTARGETS]; 247 }; 248 249 } // namespace vk 250 251 #endif // vk_Context_hpp 252