Line data Source code
1 : /* Copyright (C) 2023 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "Mapping.h"
21 :
22 : #include "lib/code_annotation.h"
23 : #include "lib/config2.h"
24 :
25 : namespace Renderer
26 : {
27 :
28 : namespace Backend
29 : {
30 :
31 : namespace Vulkan
32 : {
33 :
34 : namespace Mapping
35 : {
36 :
37 0 : VkCompareOp FromCompareOp(const CompareOp compareOp)
38 : {
39 0 : VkCompareOp op = VK_COMPARE_OP_NEVER;
40 0 : switch (compareOp)
41 : {
42 : #define CASE(NAME) case CompareOp::NAME: op = VK_COMPARE_OP_##NAME; break
43 0 : CASE(NEVER);
44 0 : CASE(LESS);
45 0 : CASE(EQUAL);
46 0 : CASE(LESS_OR_EQUAL);
47 0 : CASE(GREATER);
48 0 : CASE(NOT_EQUAL);
49 0 : CASE(GREATER_OR_EQUAL);
50 0 : CASE(ALWAYS);
51 : #undef CASE
52 : }
53 0 : return op;
54 : }
55 :
56 0 : VkStencilOp FromStencilOp(const StencilOp stencilOp)
57 : {
58 0 : VkStencilOp op = VK_STENCIL_OP_KEEP;
59 0 : switch (stencilOp)
60 : {
61 : #define CASE(NAME) case StencilOp::NAME: op = VK_STENCIL_OP_##NAME; break
62 0 : CASE(KEEP);
63 0 : CASE(ZERO);
64 0 : CASE(REPLACE);
65 0 : CASE(INCREMENT_AND_CLAMP);
66 0 : CASE(DECREMENT_AND_CLAMP);
67 0 : CASE(INVERT);
68 0 : CASE(INCREMENT_AND_WRAP);
69 0 : CASE(DECREMENT_AND_WRAP);
70 : #undef CASE
71 : }
72 0 : return op;
73 : }
74 :
75 0 : VkBlendFactor FromBlendFactor(const BlendFactor blendFactor)
76 : {
77 0 : VkBlendFactor factor = VK_BLEND_FACTOR_ZERO;
78 0 : switch (blendFactor)
79 : {
80 : #define CASE(NAME) case BlendFactor::NAME: factor = VK_BLEND_FACTOR_##NAME; break
81 0 : CASE(ZERO);
82 0 : CASE(ONE);
83 0 : CASE(SRC_COLOR);
84 0 : CASE(ONE_MINUS_SRC_COLOR);
85 0 : CASE(DST_COLOR);
86 0 : CASE(ONE_MINUS_DST_COLOR);
87 0 : CASE(SRC_ALPHA);
88 0 : CASE(ONE_MINUS_SRC_ALPHA);
89 0 : CASE(DST_ALPHA);
90 0 : CASE(ONE_MINUS_DST_ALPHA);
91 0 : CASE(CONSTANT_COLOR);
92 0 : CASE(ONE_MINUS_CONSTANT_COLOR);
93 0 : CASE(CONSTANT_ALPHA);
94 0 : CASE(ONE_MINUS_CONSTANT_ALPHA);
95 0 : CASE(SRC_ALPHA_SATURATE);
96 :
97 0 : CASE(SRC1_COLOR);
98 0 : CASE(ONE_MINUS_SRC1_COLOR);
99 0 : CASE(SRC1_ALPHA);
100 0 : CASE(ONE_MINUS_SRC1_ALPHA);
101 : #undef CASE
102 : }
103 0 : return factor;
104 : }
105 :
106 0 : VkBlendOp FromBlendOp(const BlendOp blendOp)
107 : {
108 0 : VkBlendOp mode = VK_BLEND_OP_ADD;
109 0 : switch (blendOp)
110 : {
111 0 : case BlendOp::ADD: mode = VK_BLEND_OP_ADD; break;
112 0 : case BlendOp::SUBTRACT: mode = VK_BLEND_OP_SUBTRACT; break;
113 0 : case BlendOp::REVERSE_SUBTRACT: mode = VK_BLEND_OP_REVERSE_SUBTRACT; break;
114 0 : case BlendOp::MIN: mode = VK_BLEND_OP_MIN; break;
115 0 : case BlendOp::MAX: mode = VK_BLEND_OP_MAX; break;
116 : };
117 0 : return mode;
118 : }
119 :
120 0 : VkColorComponentFlags FromColorWriteMask(const uint32_t colorWriteMask)
121 : {
122 0 : VkColorComponentFlags flags = 0;
123 0 : if (colorWriteMask & ColorWriteMask::RED)
124 0 : flags |= VK_COLOR_COMPONENT_R_BIT;
125 0 : if (colorWriteMask & ColorWriteMask::GREEN)
126 0 : flags |= VK_COLOR_COMPONENT_G_BIT;
127 0 : if (colorWriteMask & ColorWriteMask::BLUE)
128 0 : flags |= VK_COLOR_COMPONENT_B_BIT;
129 0 : if (colorWriteMask & ColorWriteMask::ALPHA)
130 0 : flags |= VK_COLOR_COMPONENT_A_BIT;
131 0 : return flags;
132 : }
133 :
134 0 : VkPolygonMode FromPolygonMode(const PolygonMode polygonMode)
135 : {
136 0 : if (polygonMode == PolygonMode::LINE)
137 0 : return VK_POLYGON_MODE_LINE;
138 0 : return VK_POLYGON_MODE_FILL;
139 : }
140 :
141 0 : VkCullModeFlags FromCullMode(const CullMode cullMode)
142 : {
143 0 : VkCullModeFlags flags = VK_CULL_MODE_NONE;
144 0 : switch (cullMode)
145 : {
146 0 : case CullMode::NONE:
147 0 : break;
148 0 : case CullMode::FRONT:
149 0 : flags |= VK_CULL_MODE_FRONT_BIT;
150 0 : break;
151 0 : case CullMode::BACK:
152 0 : flags |= VK_CULL_MODE_BACK_BIT;
153 0 : break;
154 : }
155 0 : return flags;
156 : }
157 :
158 0 : VkFormat FromFormat(const Format format)
159 : {
160 0 : VkFormat resultFormat = VK_FORMAT_UNDEFINED;
161 0 : switch (format)
162 : {
163 : #define CASE(NAME) case Format::NAME: resultFormat = VK_FORMAT_##NAME; break;
164 : #define CASE2(NAME, VK_NAME) case Format::NAME: resultFormat = VK_FORMAT_##VK_NAME; break;
165 :
166 0 : CASE(UNDEFINED)
167 :
168 0 : CASE(R8_UNORM)
169 0 : CASE(R8G8_UNORM)
170 0 : CASE(R8G8_UINT)
171 0 : CASE(R8G8B8A8_UNORM)
172 0 : CASE(R8G8B8A8_UINT)
173 :
174 0 : CASE(R16_UNORM)
175 0 : CASE(R16_UINT)
176 0 : CASE(R16_SINT)
177 0 : CASE(R16G16_UNORM)
178 0 : CASE(R16G16_UINT)
179 0 : CASE(R16G16_SINT)
180 :
181 0 : CASE(R32_SFLOAT)
182 0 : CASE(R32G32_SFLOAT)
183 0 : CASE(R32G32B32_SFLOAT)
184 0 : CASE(R32G32B32A32_SFLOAT)
185 :
186 0 : CASE(D16_UNORM)
187 0 : CASE2(D24_UNORM, X8_D24_UNORM_PACK32)
188 0 : CASE(D24_UNORM_S8_UINT)
189 0 : CASE(D32_SFLOAT_S8_UINT)
190 0 : CASE(D32_SFLOAT)
191 :
192 0 : CASE2(BC1_RGB_UNORM, BC1_RGB_UNORM_BLOCK)
193 0 : CASE2(BC1_RGBA_UNORM, BC1_RGBA_UNORM_BLOCK)
194 0 : CASE2(BC2_UNORM, BC2_UNORM_BLOCK)
195 0 : CASE2(BC3_UNORM, BC3_UNORM_BLOCK)
196 :
197 : #undef CASE
198 : #undef CASE2
199 0 : default:
200 0 : debug_warn("Unsupported format");
201 : }
202 0 : return resultFormat;
203 : }
204 :
205 0 : VkSampleCountFlagBits FromSampleCount(const uint32_t sampleCount)
206 : {
207 0 : VkSampleCountFlagBits flags = VK_SAMPLE_COUNT_1_BIT;
208 0 : switch (sampleCount)
209 : {
210 0 : case 1: flags = VK_SAMPLE_COUNT_1_BIT; break;
211 0 : case 2: flags = VK_SAMPLE_COUNT_2_BIT; break;
212 0 : case 4: flags = VK_SAMPLE_COUNT_4_BIT; break;
213 0 : case 8: flags = VK_SAMPLE_COUNT_8_BIT; break;
214 0 : case 16: flags = VK_SAMPLE_COUNT_16_BIT; break;
215 0 : default:
216 0 : debug_warn("Unsupported number of samples");
217 : }
218 0 : return flags;
219 : }
220 :
221 0 : VkSamplerAddressMode FromAddressMode(const Sampler::AddressMode addressMode)
222 : {
223 0 : VkSamplerAddressMode resultAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT;
224 0 : switch (addressMode)
225 : {
226 0 : case Sampler::AddressMode::REPEAT:
227 0 : resultAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT;
228 0 : break;
229 0 : case Sampler::AddressMode::MIRRORED_REPEAT:
230 0 : resultAddressMode = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
231 0 : break;
232 0 : case Sampler::AddressMode::CLAMP_TO_EDGE:
233 0 : resultAddressMode = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
234 0 : break;
235 0 : case Sampler::AddressMode::CLAMP_TO_BORDER:
236 0 : resultAddressMode = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
237 0 : break;
238 : }
239 0 : return resultAddressMode;
240 : }
241 :
242 0 : VkAttachmentLoadOp FromAttachmentLoadOp(const AttachmentLoadOp loadOp)
243 : {
244 0 : VkAttachmentLoadOp resultLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
245 0 : switch (loadOp)
246 : {
247 0 : case AttachmentLoadOp::LOAD:
248 0 : resultLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
249 0 : break;
250 0 : case AttachmentLoadOp::CLEAR:
251 0 : resultLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
252 0 : break;
253 0 : case AttachmentLoadOp::DONT_CARE:
254 0 : resultLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
255 0 : break;
256 : }
257 0 : return resultLoadOp;
258 : }
259 :
260 0 : VkAttachmentStoreOp FromAttachmentStoreOp(const AttachmentStoreOp storeOp)
261 : {
262 0 : VkAttachmentStoreOp resultStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
263 0 : switch (storeOp)
264 : {
265 0 : case AttachmentStoreOp::STORE:
266 0 : resultStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
267 0 : break;
268 0 : case AttachmentStoreOp::DONT_CARE:
269 0 : resultStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
270 0 : break;
271 : }
272 0 : return resultStoreOp;
273 : }
274 :
275 : } // namespace Mapping
276 :
277 : } // namespace Vulkan
278 :
279 : } // namespace Backend
280 :
281 3 : } // namespace Renderer
|