Flutter Engine
The Flutter Engine
formats_vk.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_FORMATS_VK_H_
6#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_FORMATS_VK_H_
7
8#include <cstdint>
9#include <ostream>
10
15#include "vulkan/vulkan_enums.hpp"
16
17namespace impeller {
18
19constexpr vk::SampleCountFlagBits ToVKSampleCountFlagBits(SampleCount count) {
20 switch (count) {
24 return vk::SampleCountFlagBits::e4;
25 }
27}
28
30 switch (factor) {
32 return vk::BlendFactor::eZero;
34 return vk::BlendFactor::eOne;
36 return vk::BlendFactor::eSrcColor;
38 return vk::BlendFactor::eOneMinusSrcColor;
40 return vk::BlendFactor::eSrcAlpha;
42 return vk::BlendFactor::eOneMinusSrcAlpha;
44 return vk::BlendFactor::eDstColor;
46 return vk::BlendFactor::eOneMinusDstColor;
48 return vk::BlendFactor::eDstAlpha;
50 return vk::BlendFactor::eOneMinusDstAlpha;
52 return vk::BlendFactor::eSrcAlphaSaturate;
54 return vk::BlendFactor::eConstantColor;
56 return vk::BlendFactor::eOneMinusConstantColor;
58 return vk::BlendFactor::eConstantAlpha;
60 return vk::BlendFactor::eOneMinusConstantAlpha;
61 }
63}
64
65constexpr vk::BlendOp ToVKBlendOp(BlendOperation op) {
66 switch (op) {
68 return vk::BlendOp::eAdd;
70 return vk::BlendOp::eSubtract;
72 return vk::BlendOp::eReverseSubtract;
73 }
75}
76
77constexpr vk::ColorComponentFlags ToVKColorComponentFlags(ColorWriteMask type) {
78 vk::ColorComponentFlags mask;
79
81 mask |= vk::ColorComponentFlagBits::eR;
82 }
83
85 mask |= vk::ColorComponentFlagBits::eG;
86 }
87
89 mask |= vk::ColorComponentFlagBits::eB;
90 }
91
93 mask |= vk::ColorComponentFlagBits::eA;
94 }
95
96 return mask;
97}
98
99constexpr vk::PipelineColorBlendAttachmentState
101 vk::PipelineColorBlendAttachmentState res;
102
103 res.setBlendEnable(desc.blending_enabled);
104
105 res.setSrcColorBlendFactor(ToVKBlendFactor(desc.src_color_blend_factor));
106 res.setColorBlendOp(ToVKBlendOp(desc.color_blend_op));
107 res.setDstColorBlendFactor(ToVKBlendFactor(desc.dst_color_blend_factor));
108
109 res.setSrcAlphaBlendFactor(ToVKBlendFactor(desc.src_alpha_blend_factor));
110 res.setAlphaBlendOp(ToVKBlendOp(desc.alpha_blend_op));
111 res.setDstAlphaBlendFactor(ToVKBlendFactor(desc.dst_alpha_blend_factor));
112
113 res.setColorWriteMask(ToVKColorComponentFlags(desc.write_mask));
114
115 return res;
116}
117
118constexpr std::optional<vk::ShaderStageFlagBits> ToVKShaderStageFlagBits(
119 ShaderStage stage) {
120 switch (stage) {
122 return std::nullopt;
124 return vk::ShaderStageFlagBits::eVertex;
126 return vk::ShaderStageFlagBits::eFragment;
128 return vk::ShaderStageFlagBits::eCompute;
129 }
131}
132
134 switch (format) {
139 return vk::Format::eUndefined;
141 // TODO(csg): This is incorrect. Don't depend on swizzle support for GLES.
142 return vk::Format::eR8Unorm;
144 return vk::Format::eR8G8B8A8Unorm;
146 return vk::Format::eR8G8B8A8Srgb;
148 return vk::Format::eB8G8R8A8Unorm;
150 return vk::Format::eB8G8R8A8Srgb;
152 return vk::Format::eR32G32B32A32Sfloat;
154 return vk::Format::eR16G16B16A16Sfloat;
156 return vk::Format::eS8Uint;
158 return vk::Format::eD24UnormS8Uint;
160 return vk::Format::eD32SfloatS8Uint;
162 return vk::Format::eR8Unorm;
164 return vk::Format::eR8G8Unorm;
165 }
166
168}
169
171 switch (format) {
172 case vk::Format::eUndefined:
174 case vk::Format::eR8G8B8A8Unorm:
176 case vk::Format::eR8G8B8A8Srgb:
178 case vk::Format::eB8G8R8A8Unorm:
180 case vk::Format::eB8G8R8A8Srgb:
182 case vk::Format::eR32G32B32A32Sfloat:
184 case vk::Format::eR16G16B16A16Sfloat:
186 case vk::Format::eS8Uint:
188 case vk::Format::eD24UnormS8Uint:
190 case vk::Format::eD32SfloatS8Uint:
192 case vk::Format::eR8Unorm:
194 case vk::Format::eR8G8Unorm:
196 default:
198 }
199}
200
201constexpr vk::SampleCountFlagBits ToVKSampleCount(SampleCount sample_count) {
202 switch (sample_count) {
206 return vk::SampleCountFlagBits::e4;
207 }
208
210}
211
213 switch (filter) {
215 return vk::Filter::eNearest;
217 return vk::Filter::eLinear;
218 }
219
221}
222
223constexpr vk::SamplerMipmapMode ToVKSamplerMipmapMode(MipFilter filter) {
224 switch (filter) {
225 case MipFilter::kBase:
227 return vk::SamplerMipmapMode::eNearest;
229 return vk::SamplerMipmapMode::eLinear;
230 }
231
233}
234
237 switch (mode) {
239 return vk::SamplerAddressMode::eRepeat;
241 return vk::SamplerAddressMode::eMirroredRepeat;
243 return vk::SamplerAddressMode::eClampToEdge;
245 return vk::SamplerAddressMode::eClampToBorder;
246 }
247
249}
250
251constexpr vk::ShaderStageFlags ToVkShaderStage(ShaderStage stage) {
252 switch (stage) {
254 return vk::ShaderStageFlagBits::eAll;
256 return vk::ShaderStageFlagBits::eFragment;
258 return vk::ShaderStageFlagBits::eCompute;
260 return vk::ShaderStageFlagBits::eVertex;
261 }
262
264}
265
267 switch (type) {
269 return vk::DescriptorType::eCombinedImageSampler;
270 break;
272 return vk::DescriptorType::eUniformBuffer;
273 break;
275 return vk::DescriptorType::eStorageBuffer;
276 break;
278 return vk::DescriptorType::eSampledImage;
279 break;
281 return vk::DescriptorType::eSampler;
282 break;
284 return vk::DescriptorType::eInputAttachment;
285 }
286
288}
289
290constexpr vk::DescriptorSetLayoutBinding ToVKDescriptorSetLayoutBinding(
291 const DescriptorSetLayout& layout) {
292 vk::DescriptorSetLayoutBinding binding;
293 binding.binding = layout.binding;
294 binding.descriptorCount = 1u;
295 binding.descriptorType = ToVKDescriptorType(layout.descriptor_type);
296 binding.stageFlags = ToVkShaderStage(layout.shader_stage);
297 return binding;
298}
299
300constexpr vk::AttachmentLoadOp ToVKAttachmentLoadOp(LoadAction load_action) {
301 switch (load_action) {
303 return vk::AttachmentLoadOp::eLoad;
305 return vk::AttachmentLoadOp::eClear;
307 return vk::AttachmentLoadOp::eDontCare;
308 }
309
311}
312
313constexpr vk::AttachmentStoreOp ToVKAttachmentStoreOp(StoreAction store_action,
314 bool is_resolve_texture) {
315 switch (store_action) {
317 // Both MSAA and resolve textures need to be stored. A resolve is NOT
318 // performed.
319 return vk::AttachmentStoreOp::eStore;
321 // Both MSAA and resolve textures can be discarded. A resolve is NOT
322 // performed.
323 return vk::AttachmentStoreOp::eDontCare;
325 // The resolve texture is stored but the MSAA texture can be discarded. A
326 // resolve IS performed.
327 return is_resolve_texture ? vk::AttachmentStoreOp::eStore
328 : vk::AttachmentStoreOp::eDontCare;
330 // Both MSAA and resolve textures need to be stored. A resolve IS
331 // performed.
332 return vk::AttachmentStoreOp::eStore;
333 }
335}
336
337constexpr bool StoreActionPerformsResolve(StoreAction store_action) {
338 switch (store_action) {
341 return false;
344 return true;
345 }
347}
348
349constexpr vk::IndexType ToVKIndexType(IndexType index_type) {
350 switch (index_type) {
352 return vk::IndexType::eUint16;
354 return vk::IndexType::eUint32;
356 return vk::IndexType::eUint32;
357 case IndexType::kNone:
359 }
360
362}
363
365 switch (mode) {
367 return vk::PolygonMode::eFill;
369 return vk::PolygonMode::eLine;
370 }
372}
373
374constexpr vk::PrimitiveTopology ToVKPrimitiveTopology(PrimitiveType primitive) {
375 switch (primitive) {
377 return vk::PrimitiveTopology::eTriangleList;
379 return vk::PrimitiveTopology::eTriangleStrip;
381 return vk::PrimitiveTopology::eLineList;
383 return vk::PrimitiveTopology::eLineStrip;
385 return vk::PrimitiveTopology::ePointList;
386 }
387
389}
390
392 switch (format) {
406 return false;
410 return true;
411 }
412 return false;
413}
414
415static constexpr vk::AttachmentReference kUnusedAttachmentReference = {
416 VK_ATTACHMENT_UNUSED, vk::ImageLayout::eUndefined};
417
418constexpr vk::CullModeFlags ToVKCullModeFlags(CullMode mode) {
419 switch (mode) {
420 case CullMode::kNone:
421 return vk::CullModeFlagBits::eNone;
423 return vk::CullModeFlagBits::eFront;
425 return vk::CullModeFlagBits::eBack;
426 }
428}
429
431 switch (op) {
433 return vk::CompareOp::eNever;
435 return vk::CompareOp::eAlways;
437 return vk::CompareOp::eLess;
439 return vk::CompareOp::eEqual;
441 return vk::CompareOp::eLessOrEqual;
443 return vk::CompareOp::eGreater;
445 return vk::CompareOp::eNotEqual;
447 return vk::CompareOp::eGreaterOrEqual;
448 }
450}
451
453 switch (op) {
455 return vk::StencilOp::eKeep;
457 return vk::StencilOp::eZero;
459 return vk::StencilOp::eReplace;
461 return vk::StencilOp::eIncrementAndClamp;
463 return vk::StencilOp::eDecrementAndClamp;
465 return vk::StencilOp::eInvert;
467 return vk::StencilOp::eIncrementAndWrap;
469 return vk::StencilOp::eDecrementAndWrap;
470 break;
471 }
473}
474
475constexpr vk::StencilOpState ToVKStencilOpState(
477 vk::StencilOpState state;
478 state.failOp = ToVKStencilOp(desc.stencil_failure);
479 state.passOp = ToVKStencilOp(desc.depth_stencil_pass);
480 state.depthFailOp = ToVKStencilOp(desc.depth_failure);
481 state.compareOp = ToVKCompareOp(desc.stencil_compare);
482 state.compareMask = desc.read_mask;
483 state.writeMask = desc.write_mask;
484 // This is irrelevant as the stencil references are always dynamic state and
485 // will be set in the render pass.
486 state.reference = 1988;
487 return state;
488}
489
490constexpr vk::ImageAspectFlags ToVKImageAspectFlags(PixelFormat format) {
491 switch (format) {
505 return vk::ImageAspectFlagBits::eColor;
507 return vk::ImageAspectFlagBits::eStencil;
510 return vk::ImageAspectFlagBits::eDepth |
511 vk::ImageAspectFlagBits::eStencil;
512 }
514}
515
516constexpr uint32_t ToArrayLayerCount(TextureType type) {
517 switch (type) {
520 return 1u;
522 return 6u;
525 << "kTextureExternalOES can not be used with the Vulkan backend.";
526 }
528}
529
530constexpr vk::ImageViewType ToVKImageViewType(TextureType type) {
531 switch (type) {
534 return vk::ImageViewType::e2D;
536 return vk::ImageViewType::eCube;
539 << "kTextureExternalOES can not be used with the Vulkan backend.";
540 }
542}
543
544constexpr vk::ImageCreateFlags ToVKImageCreateFlags(TextureType type) {
545 switch (type) {
548 return {};
550 return vk::ImageCreateFlagBits::eCubeCompatible;
553 << "kTextureExternalOES can not be used with the Vulkan backend.";
554 }
556}
557
558vk::PipelineDepthStencilStateCreateInfo ToVKPipelineDepthStencilStateCreateInfo(
559 std::optional<DepthAttachmentDescriptor> depth,
560 std::optional<StencilAttachmentDescriptor> front,
561 std::optional<StencilAttachmentDescriptor> back);
562
563constexpr vk::ImageAspectFlags ToImageAspectFlags(PixelFormat format) {
564 switch (format) {
566 return {};
579 return vk::ImageAspectFlagBits::eColor;
581 return vk::ImageAspectFlagBits::eStencil;
584 return vk::ImageAspectFlagBits::eDepth |
585 vk::ImageAspectFlagBits::eStencil;
586 }
588}
589
590} // namespace impeller
591
592#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_FORMATS_VK_H_
int count
Definition: FontMgrTest.cpp:50
float e1
GLenum type
AtkStateType state
uint32_t uint32_t * format
#define FML_UNREACHABLE()
Definition: logging.h:109
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
@ kNone
Does not use the index buffer.
static constexpr vk::AttachmentReference kUnusedAttachmentReference
Definition: formats_vk.h:415
BlendFactor
Definition: formats.h:178
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:352
@ kPoint
Draws a point at each input vertex.
constexpr vk::SamplerAddressMode ToVKSamplerAddressMode(SamplerAddressMode mode)
Definition: formats_vk.h:235
constexpr vk::PipelineColorBlendAttachmentState ToVKPipelineColorBlendAttachmentState(const ColorAttachmentDescriptor &desc)
Definition: formats_vk.h:100
constexpr vk::CompareOp ToVKCompareOp(CompareFunction op)
Definition: formats_vk.h:430
constexpr vk::SamplerMipmapMode ToVKSamplerMipmapMode(MipFilter filter)
Definition: formats_vk.h:223
constexpr bool StoreActionPerformsResolve(StoreAction store_action)
Definition: formats_vk.h:337
MipFilter
Options for selecting and filtering between mipmap levels.
Definition: formats.h:419
@ kLinear
Sample from the two nearest mip levels and linearly interpolate.
@ kBase
The texture is sampled as if it only had a single mipmap level.
@ kNearest
The nearst mipmap level is selected.
constexpr vk::IndexType ToVKIndexType(IndexType index_type)
Definition: formats_vk.h:349
constexpr vk::DescriptorSetLayoutBinding ToVKDescriptorSetLayoutBinding(const DescriptorSetLayout &layout)
Definition: formats_vk.h:290
constexpr uint32_t ToArrayLayerCount(TextureType type)
Definition: formats_vk.h:516
constexpr vk::Filter ToVKSamplerMinMagFilter(MinMagFilter filter)
Definition: formats_vk.h:212
constexpr vk::ImageAspectFlags ToImageAspectFlags(PixelFormat format)
Definition: formats_vk.h:563
constexpr vk::SampleCountFlagBits ToVKSampleCountFlagBits(SampleCount count)
Definition: formats_vk.h:19
constexpr vk::AttachmentLoadOp ToVKAttachmentLoadOp(LoadAction load_action)
Definition: formats_vk.h:300
constexpr vk::DescriptorType ToVKDescriptorType(DescriptorType type)
Definition: formats_vk.h:266
SamplerAddressMode
Definition: formats.h:435
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
constexpr vk::PolygonMode ToVKPolygonMode(PolygonMode mode)
Definition: formats_vk.h:364
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
CompareFunction
Definition: formats.h:546
@ kEqual
Comparison test passes if new_value == current_value.
@ kLessEqual
Comparison test passes if new_value <= current_value.
@ kGreaterEqual
Comparison test passes if new_value >= current_value.
@ kAlways
Comparison test passes always passes.
@ kLess
Comparison test passes if new_value < current_value.
@ kGreater
Comparison test passes if new_value > current_value.
@ kNotEqual
Comparison test passes if new_value != current_value.
@ kNever
Comparison test never passes.
constexpr vk::AttachmentStoreOp ToVKAttachmentStoreOp(StoreAction store_action, bool is_resolve_texture)
Definition: formats_vk.h:313
constexpr vk::CullModeFlags ToVKCullModeFlags(CullMode mode)
Definition: formats_vk.h:418
constexpr vk::ColorComponentFlags ToVKColorComponentFlags(ColorWriteMask type)
Definition: formats_vk.h:77
StencilOperation
Definition: formats.h:565
@ kDecrementWrap
Decrement the current stencil value by 1. If at zero, set to maximum.
@ kSetToReferenceValue
Reset the stencil value to the reference value.
@ kDecrementClamp
Decrement the current stencil value by 1. Clamp it to zero.
@ kZero
Reset the stencil value to zero.
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
@ kIncrementWrap
Increment the current stencil value by 1. If at maximum, set to zero.
@ kInvert
Perform a logical bitwise invert on the current stencil value.
@ kKeep
Don't modify the current stencil value.
constexpr vk::BlendFactor ToVKBlendFactor(BlendFactor factor)
Definition: formats_vk.h:29
TextureType
Definition: formats.h:262
constexpr bool PixelFormatIsDepthStencil(PixelFormat format)
Definition: formats_vk.h:391
vk::PipelineDepthStencilStateCreateInfo ToVKPipelineDepthStencilStateCreateInfo(std::optional< DepthAttachmentDescriptor > depth, std::optional< StencilAttachmentDescriptor > front, std::optional< StencilAttachmentDescriptor > back)
Definition: formats_vk.cc:9
constexpr std::optional< vk::ShaderStageFlagBits > ToVKShaderStageFlagBits(ShaderStage stage)
Definition: formats_vk.h:118
constexpr vk::ImageViewType ToVKImageViewType(TextureType type)
Definition: formats_vk.h:530
LoadAction
Definition: formats.h:202
StoreAction
Definition: formats.h:208
constexpr vk::SampleCountFlagBits ToVKSampleCount(SampleCount sample_count)
Definition: formats_vk.h:201
constexpr vk::Format ToVKImageFormat(PixelFormat format)
Definition: formats_vk.h:133
constexpr vk::StencilOpState ToVKStencilOpState(const StencilAttachmentDescriptor &desc)
Definition: formats_vk.h:475
constexpr vk::ShaderStageFlags ToVkShaderStage(ShaderStage stage)
Definition: formats_vk.h:251
constexpr vk::PrimitiveTopology ToVKPrimitiveTopology(PrimitiveType primitive)
Definition: formats_vk.h:374
PolygonMode
Definition: formats.h:383
constexpr vk::BlendOp ToVKBlendOp(BlendOperation op)
Definition: formats_vk.h:65
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition: formats.h:409
@ kNearest
Select nearest to the sample point. Most widely supported.
constexpr vk::StencilOp ToVKStencilOp(StencilOperation op)
Definition: formats_vk.h:452
constexpr vk::ImageCreateFlags ToVKImageCreateFlags(TextureType type)
Definition: formats_vk.h:544
constexpr vk::ImageAspectFlags ToVKImageAspectFlags(PixelFormat format)
Definition: formats_vk.h:490
static PixelFormat ToPixelFormat(AHardwareBuffer_Format format)
BlendOperation
Definition: formats.h:196
SampleCount
Definition: formats.h:295
def Format(template, **parameters)
Definition: emitter.py:13
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:512
GrSamplerState::Filter Filter
Definition: texelsubset.cpp:39
#define VALIDATION_LOG
Definition: validation.h:73
#define VK_ATTACHMENT_UNUSED
Definition: vulkan_core.h:124