1// Copyright 2016 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 sw_Context_hpp
16#define sw_Context_hpp
17
18#include "Vulkan/VkConfig.h"
19#include "Vulkan/VkDescriptorSet.hpp"
20#include "Config.hpp"
21#include "Memset.hpp"
22#include "Stream.hpp"
23#include "System/Types.hpp"
24
25namespace vk
26{
27 class ImageView;
28 class PipelineLayout;
29}
30
31namespace sw
32{
33 class SpirvShader;
34
35 struct PushConstantStorage
36 {
37 unsigned char data[vk::MAX_PUSH_CONSTANT_SIZE];
38 };
39
40 struct BlendState : Memset<BlendState>
41 {
42 BlendState() : Memset(this, 0) {}
43
44 BlendState(bool alphaBlendEnable,
45 VkBlendFactor sourceBlendFactor,
46 VkBlendFactor destBlendFactor,
47 VkBlendOp blendOperation,
48 VkBlendFactor sourceBlendFactorAlpha,
49 VkBlendFactor destBlendFactorAlpha,
50 VkBlendOp blendOperationAlpha) :
51 Memset(this, 0),
52 alphaBlendEnable(alphaBlendEnable),
53 sourceBlendFactor(sourceBlendFactor),
54 destBlendFactor(destBlendFactor),
55 blendOperation(blendOperation),
56 sourceBlendFactorAlpha(sourceBlendFactorAlpha),
57 destBlendFactorAlpha(destBlendFactorAlpha),
58 blendOperationAlpha(blendOperationAlpha)
59 {}
60
61 bool alphaBlendEnable;
62 VkBlendFactor sourceBlendFactor;
63 VkBlendFactor destBlendFactor;
64 VkBlendOp blendOperation;
65 VkBlendFactor sourceBlendFactorAlpha;
66 VkBlendFactor destBlendFactorAlpha;
67 VkBlendOp blendOperationAlpha;
68 };
69
70 class Context
71 {
72 public:
73 Context();
74
75 void init();
76
77 bool isDrawPoint(bool polygonModeAware) const;
78 bool isDrawLine(bool polygonModeAware) const;
79 bool isDrawTriangle(bool polygonModeAware) const;
80
81 bool depthWriteActive() const;
82 bool depthBufferActive() const;
83 bool stencilActive() const;
84
85 bool allTargetsColorClamp() const;
86
87 void setBlendState(int index, BlendState state);
88 BlendState getBlendState(int index) const;
89
90 VkPrimitiveTopology topology;
91 VkProvokingVertexModeEXT provokingVertexMode;
92
93 bool stencilEnable;
94 VkStencilOpState frontStencil;
95 VkStencilOpState backStencil;
96
97 // Pixel processor states
98 VkCullModeFlags cullMode;
99 VkFrontFace frontFace;
100 VkPolygonMode polygonMode;
101 VkLineRasterizationModeEXT lineRasterizationMode;
102
103 float depthBias;
104 float slopeDepthBias;
105
106 VkFormat renderTargetInternalFormat(int index) const;
107 int colorWriteActive(int index) const;
108
109 vk::DescriptorSet::Bindings descriptorSets = {};
110 vk::DescriptorSet::DynamicOffsets descriptorDynamicOffsets = {};
111 Stream input[MAX_INTERFACE_COMPONENTS / 4];
112 bool robustBufferAccess;
113
114 vk::ImageView *renderTarget[RENDERTARGETS];
115 vk::ImageView *depthBuffer;
116 vk::ImageView *stencilBuffer;
117
118 vk::PipelineLayout const *pipelineLayout;
119
120 // Shaders
121 const SpirvShader *pixelShader;
122 const SpirvShader *vertexShader;
123
124 bool occlusionEnabled;
125
126 // Pixel processor states
127 bool rasterizerDiscard;
128 bool depthBoundsTestEnable;
129 bool depthBufferEnable;
130 VkCompareOp depthCompareMode;
131 bool depthWriteEnable;
132
133 float lineWidth;
134
135 int colorWriteMask[RENDERTARGETS]; // RGBA
136 unsigned int sampleMask;
137 unsigned int multiSampleMask;
138 int sampleCount;
139 bool alphaToCoverage;
140
141 private:
142 bool colorWriteActive() const;
143 bool colorUsed() const;
144
145 bool alphaBlendActive(int index) const;
146 VkBlendFactor sourceBlendFactor(int index) const;
147 VkBlendFactor destBlendFactor(int index) const;
148 VkBlendOp blendOperation(int index) const;
149
150 VkBlendFactor sourceBlendFactorAlpha(int index) const;
151 VkBlendFactor destBlendFactorAlpha(int index) const;
152 VkBlendOp blendOperationAlpha(int index) const;
153
154 BlendState blendState[RENDERTARGETS];
155 };
156}
157
158#endif // sw_Context_hpp
159