forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorBlendState.cpp
129 lines (104 loc) · 6.66 KB
/
ColorBlendState.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/core/compare.h>
#include <vsg/io/Options.h>
#include <vsg/state/ColorBlendState.h>
#include <vsg/vk/Context.h>
using namespace vsg;
ColorBlendState::ColorBlendState()
{
VkPipelineColorBlendAttachmentState colorBlendAttachment = {
VK_FALSE, // blendEnable
VK_BLEND_FACTOR_ZERO, // srcColorBlendFactor
VK_BLEND_FACTOR_ZERO, // dstColorBlendFactor
VK_BLEND_OP_ADD, // colorBlendOp
VK_BLEND_FACTOR_ZERO, // srcAlphaBlendFactor
VK_BLEND_FACTOR_ZERO, // dstAlphaBlendFactor
VK_BLEND_OP_ADD, // alphaBlendOp
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT // colorWriteMask
};
attachments.push_back(colorBlendAttachment);
}
ColorBlendState::ColorBlendState(const ColorBlendState& cbs) :
Inherit(cbs),
logicOpEnable(cbs.logicOpEnable),
logicOp(cbs.logicOp),
attachments(cbs.attachments),
blendConstants{cbs.blendConstants[0], cbs.blendConstants[1], cbs.blendConstants[2], cbs.blendConstants[3]}
{
}
ColorBlendState::ColorBlendState(const ColorBlendAttachments& colorBlendAttachments) :
attachments(colorBlendAttachments)
{
}
ColorBlendState::~ColorBlendState()
{
}
int ColorBlendState::compare(const Object& rhs_object) const
{
int result = Object::compare(rhs_object);
if (result != 0) return result;
auto& rhs = static_cast<decltype(*this)>(rhs_object);
if ((result = compare_value(logicOpEnable, rhs.logicOpEnable))) return result;
if ((result = compare_value(logicOp, rhs.logicOp))) return result;
if ((result = compare_value_container(attachments, rhs.attachments))) return result;
return compare_values(blendConstants, rhs.blendConstants, 3);
}
void ColorBlendState::read(Input& input)
{
Object::read(input);
input.readValue<uint32_t>("logicOp", logicOp);
input.readValue<uint32_t>("logicOpEnable", logicOpEnable);
attachments.resize(input.readValue<uint32_t>("NumColorBlendAttachments")); // TODO review capitalization
for (auto& colorBlendAttachment : attachments)
{
input.readValue<uint32_t>("blendEnable", colorBlendAttachment.blendEnable);
input.readValue<uint32_t>("srcColorBlendFactor", colorBlendAttachment.srcColorBlendFactor);
input.readValue<uint32_t>("dstColorBlendFactor", colorBlendAttachment.dstColorBlendFactor);
input.readValue<uint32_t>("colorBlendOp", colorBlendAttachment.colorBlendOp);
input.readValue<uint32_t>("srcAlphaBlendFactor", colorBlendAttachment.srcAlphaBlendFactor);
input.readValue<uint32_t>("dstAlphaBlendFactor", colorBlendAttachment.dstAlphaBlendFactor);
input.readValue<uint32_t>("alphaBlendOp", colorBlendAttachment.alphaBlendOp);
input.readValue<uint32_t>("colorWriteMask", colorBlendAttachment.colorWriteMask);
}
input.read("blendConstants", blendConstants[0], blendConstants[1], blendConstants[2], blendConstants[3]);
}
void ColorBlendState::write(Output& output) const
{
Object::write(output);
output.writeValue<uint32_t>("logicOp", logicOp);
output.writeValue<uint32_t>("logicOpEnable", logicOpEnable);
output.writeValue<uint32_t>("NumColorBlendAttachments", attachments.size()); // TODO review capitalization
for (auto& colorBlendAttachment : attachments)
{
output.writeValue<uint32_t>("blendEnable", colorBlendAttachment.blendEnable);
output.writeValue<uint32_t>("srcColorBlendFactor", colorBlendAttachment.srcColorBlendFactor);
output.writeValue<uint32_t>("dstColorBlendFactor", colorBlendAttachment.dstColorBlendFactor);
output.writeValue<uint32_t>("colorBlendOp", colorBlendAttachment.colorBlendOp);
output.writeValue<uint32_t>("srcAlphaBlendFactor", colorBlendAttachment.srcAlphaBlendFactor);
output.writeValue<uint32_t>("dstAlphaBlendFactor", colorBlendAttachment.dstAlphaBlendFactor);
output.writeValue<uint32_t>("alphaBlendOp", colorBlendAttachment.alphaBlendOp);
output.writeValue<uint32_t>("colorWriteMask", colorBlendAttachment.colorWriteMask);
}
output.write("blendConstants", blendConstants[0], blendConstants[1], blendConstants[2], blendConstants[3]);
}
void ColorBlendState::apply(Context& context, VkGraphicsPipelineCreateInfo& pipelineInfo) const
{
auto colorBlendState = context.scratchMemory->allocate<VkPipelineColorBlendStateCreateInfo>();
colorBlendState->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlendState->pNext = nullptr;
colorBlendState->flags = 0;
colorBlendState->logicOpEnable = logicOpEnable;
colorBlendState->logicOp = logicOp;
colorBlendState->attachmentCount = static_cast<uint32_t>(attachments.size());
colorBlendState->pAttachments = attachments.data();
colorBlendState->blendConstants[0] = blendConstants[0];
colorBlendState->blendConstants[1] = blendConstants[1];
colorBlendState->blendConstants[2] = blendConstants[2];
colorBlendState->blendConstants[3] = blendConstants[3];
pipelineInfo.pColorBlendState = colorBlendState;
}