Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
11#include "fml/logging.h"
16#include "vulkan/vulkan_enums.hpp"
17
18namespace impeller {
19
20constexpr std::optional<PixelFormat> VkFormatToImpellerFormat(
21 vk::Format format) {
22 switch (format) {
23 case vk::Format::eR8G8B8A8Unorm:
25 case vk::Format::eB8G8R8A8Unorm:
27 default:
28 return std::nullopt;
29 }
30}
31
32constexpr vk::SampleCountFlagBits ToVKSampleCountFlagBits(SampleCount count) {
33 switch (count) {
35 return vk::SampleCountFlagBits::e1;
37 return vk::SampleCountFlagBits::e4;
38 }
40}
41
42constexpr vk::BlendFactor ToVKBlendFactor(BlendFactor factor) {
43 switch (factor) {
45 return vk::BlendFactor::eZero;
47 return vk::BlendFactor::eOne;
49 return vk::BlendFactor::eSrcColor;
51 return vk::BlendFactor::eOneMinusSrcColor;
53 return vk::BlendFactor::eSrcAlpha;
55 return vk::BlendFactor::eOneMinusSrcAlpha;
57 return vk::BlendFactor::eDstColor;
59 return vk::BlendFactor::eOneMinusDstColor;
61 return vk::BlendFactor::eDstAlpha;
63 return vk::BlendFactor::eOneMinusDstAlpha;
65 return vk::BlendFactor::eSrcAlphaSaturate;
67 return vk::BlendFactor::eConstantColor;
69 return vk::BlendFactor::eOneMinusConstantColor;
71 return vk::BlendFactor::eConstantAlpha;
73 return vk::BlendFactor::eOneMinusConstantAlpha;
74 }
76}
77
78constexpr vk::BlendOp ToVKBlendOp(BlendOperation op) {
79 switch (op) {
81 return vk::BlendOp::eAdd;
83 return vk::BlendOp::eSubtract;
85 return vk::BlendOp::eReverseSubtract;
86 }
88}
89
90constexpr vk::ColorComponentFlags ToVKColorComponentFlags(ColorWriteMask type) {
91 vk::ColorComponentFlags mask;
92
94 mask |= vk::ColorComponentFlagBits::eR;
95 }
96
98 mask |= vk::ColorComponentFlagBits::eG;
99 }
100
102 mask |= vk::ColorComponentFlagBits::eB;
103 }
104
106 mask |= vk::ColorComponentFlagBits::eA;
107 }
108
109 return mask;
110}
111
112constexpr vk::PipelineColorBlendAttachmentState
114 vk::PipelineColorBlendAttachmentState res;
115
116 res.setBlendEnable(desc.blending_enabled);
117
118 res.setSrcColorBlendFactor(ToVKBlendFactor(desc.src_color_blend_factor));
119 res.setColorBlendOp(ToVKBlendOp(desc.color_blend_op));
120 res.setDstColorBlendFactor(ToVKBlendFactor(desc.dst_color_blend_factor));
121
122 res.setSrcAlphaBlendFactor(ToVKBlendFactor(desc.src_alpha_blend_factor));
123 res.setAlphaBlendOp(ToVKBlendOp(desc.alpha_blend_op));
124 res.setDstAlphaBlendFactor(ToVKBlendFactor(desc.dst_alpha_blend_factor));
125
126 res.setColorWriteMask(ToVKColorComponentFlags(desc.write_mask));
127
128 return res;
129}
130
131constexpr std::optional<vk::ShaderStageFlagBits> ToVKShaderStageFlagBits(
132 ShaderStage stage) {
133 switch (stage) {
135 return std::nullopt;
137 return vk::ShaderStageFlagBits::eVertex;
139 return vk::ShaderStageFlagBits::eFragment;
141 return vk::ShaderStageFlagBits::eCompute;
142 }
144}
145
146constexpr vk::Format ToVKImageFormat(PixelFormat format) {
147 switch (format) {
152 return vk::Format::eUndefined;
154 // TODO(csg): This is incorrect. Don't depend on swizzle support for GLES.
155 return vk::Format::eR8Unorm;
157 return vk::Format::eR8G8B8A8Unorm;
159 return vk::Format::eR8G8B8A8Srgb;
161 return vk::Format::eB8G8R8A8Unorm;
163 return vk::Format::eB8G8R8A8Srgb;
165 return vk::Format::eR32G32B32A32Sfloat;
167 return vk::Format::eR16G16B16A16Sfloat;
169 return vk::Format::eS8Uint;
171 return vk::Format::eD24UnormS8Uint;
173 return vk::Format::eD32SfloatS8Uint;
175 return vk::Format::eR8Unorm;
177 return vk::Format::eR8G8Unorm;
179 return vk::Format::eR32Sfloat;
181 return vk::Format::eBc1RgbaUnormBlock;
183 return vk::Format::eBc1RgbaSrgbBlock;
185 return vk::Format::eBc3UnormBlock;
187 return vk::Format::eBc3SrgbBlock;
189 return vk::Format::eBc5UnormBlock;
191 return vk::Format::eBc7UnormBlock;
193 return vk::Format::eBc7SrgbBlock;
195 return vk::Format::eEtc2R8G8B8UnormBlock;
197 return vk::Format::eEtc2R8G8B8SrgbBlock;
199 return vk::Format::eEtc2R8G8B8A8UnormBlock;
201 return vk::Format::eEtc2R8G8B8A8SrgbBlock;
203 return vk::Format::eAstc4x4UnormBlock;
205 return vk::Format::eAstc4x4SrgbBlock;
207 return vk::Format::eAstc8x8UnormBlock;
209 return vk::Format::eAstc8x8SrgbBlock;
211 return vk::Format::eAstc4x4SfloatBlock;
213 return vk::Format::eAstc8x8SfloatBlock;
214 }
215
217}
218
219constexpr PixelFormat ToPixelFormat(vk::Format format) {
220 switch (format) {
221 case vk::Format::eUndefined:
223 case vk::Format::eR8G8B8A8Unorm:
225 case vk::Format::eR8G8B8A8Srgb:
227 case vk::Format::eB8G8R8A8Unorm:
229 case vk::Format::eB8G8R8A8Srgb:
231 case vk::Format::eR32G32B32A32Sfloat:
233 case vk::Format::eR16G16B16A16Sfloat:
235 case vk::Format::eS8Uint:
237 case vk::Format::eD24UnormS8Uint:
239 case vk::Format::eD32SfloatS8Uint:
241 case vk::Format::eR8Unorm:
243 case vk::Format::eR8G8Unorm:
245 default:
247 }
248}
249
250constexpr vk::SampleCountFlagBits ToVKSampleCount(SampleCount sample_count) {
251 switch (sample_count) {
253 return vk::SampleCountFlagBits::e1;
255 return vk::SampleCountFlagBits::e4;
256 }
257
259}
260
261constexpr vk::Filter ToVKSamplerMinMagFilter(MinMagFilter filter) {
262 switch (filter) {
264 return vk::Filter::eNearest;
266 return vk::Filter::eLinear;
267 }
268
270}
271
272constexpr vk::SamplerMipmapMode ToVKSamplerMipmapMode(MipFilter filter) {
273 switch (filter) {
274 case MipFilter::kBase:
276 return vk::SamplerMipmapMode::eNearest;
278 return vk::SamplerMipmapMode::eLinear;
279 }
280
282}
283
284constexpr vk::SamplerAddressMode ToVKSamplerAddressMode(
285 SamplerAddressMode mode) {
286 switch (mode) {
288 return vk::SamplerAddressMode::eRepeat;
290 return vk::SamplerAddressMode::eMirroredRepeat;
292 return vk::SamplerAddressMode::eClampToEdge;
294 return vk::SamplerAddressMode::eClampToBorder;
295 }
296
298}
299
300constexpr vk::ShaderStageFlags ToVkShaderStage(ShaderStage stage) {
301 switch (stage) {
303 return vk::ShaderStageFlagBits::eAll;
305 return vk::ShaderStageFlagBits::eFragment;
307 return vk::ShaderStageFlagBits::eCompute;
309 return vk::ShaderStageFlagBits::eVertex;
310 }
311
313}
314
315static_assert(static_cast<int>(DescriptorType::kSampledImage) ==
316 static_cast<int>(vk::DescriptorType::eCombinedImageSampler));
317static_assert(static_cast<int>(DescriptorType::kUniformBuffer) ==
318 static_cast<int>(vk::DescriptorType::eUniformBuffer));
319static_assert(static_cast<int>(DescriptorType::kStorageBuffer) ==
320 static_cast<int>(vk::DescriptorType::eStorageBuffer));
321static_assert(static_cast<int>(DescriptorType::kImage) ==
322 static_cast<int>(vk::DescriptorType::eSampledImage));
323static_assert(static_cast<int>(DescriptorType::kSampler) ==
324 static_cast<int>(vk::DescriptorType::eSampler));
325static_assert(static_cast<int>(DescriptorType::kInputAttachment) ==
326 static_cast<int>(vk::DescriptorType::eInputAttachment));
327
328constexpr vk::DescriptorType ToVKDescriptorType(DescriptorType type) {
329 return static_cast<vk::DescriptorType>(type);
330}
331
332constexpr vk::DescriptorSetLayoutBinding ToVKDescriptorSetLayoutBinding(
333 const DescriptorSetLayout& layout) {
334 vk::DescriptorSetLayoutBinding binding;
335 binding.binding = layout.binding;
336 binding.descriptorCount = 1u;
337 binding.descriptorType = ToVKDescriptorType(layout.descriptor_type);
338 binding.stageFlags = ToVkShaderStage(layout.shader_stage);
339 return binding;
340}
341
342constexpr vk::AttachmentLoadOp ToVKAttachmentLoadOp(LoadAction load_action) {
343 switch (load_action) {
345 return vk::AttachmentLoadOp::eLoad;
347 return vk::AttachmentLoadOp::eClear;
349 return vk::AttachmentLoadOp::eDontCare;
350 }
351
353}
354
355constexpr vk::AttachmentStoreOp ToVKAttachmentStoreOp(StoreAction store_action,
356 bool is_resolve_texture) {
357 switch (store_action) {
359 // Both MSAA and resolve textures need to be stored. A resolve is NOT
360 // performed.
361 return vk::AttachmentStoreOp::eStore;
363 // Both MSAA and resolve textures can be discarded. A resolve is NOT
364 // performed.
365 return vk::AttachmentStoreOp::eDontCare;
367 // The resolve texture is stored but the MSAA texture can be discarded. A
368 // resolve IS performed.
369 return is_resolve_texture ? vk::AttachmentStoreOp::eStore
370 : vk::AttachmentStoreOp::eDontCare;
372 // Both MSAA and resolve textures need to be stored. A resolve IS
373 // performed.
374 return vk::AttachmentStoreOp::eStore;
375 }
377}
378
379constexpr bool StoreActionPerformsResolve(StoreAction store_action) {
380 switch (store_action) {
383 return false;
386 return true;
387 }
389}
390
391constexpr vk::IndexType ToVKIndexType(IndexType index_type) {
392 switch (index_type) {
394 return vk::IndexType::eUint16;
396 return vk::IndexType::eUint32;
398 return vk::IndexType::eUint32;
399 case IndexType::kNone:
401 }
402
404}
405
406constexpr vk::PolygonMode ToVKPolygonMode(PolygonMode mode) {
407 switch (mode) {
409 return vk::PolygonMode::eFill;
411 return vk::PolygonMode::eLine;
412 }
414}
415
417 PrimitiveType primitive) {
418 switch (primitive) {
423 return true;
426 return false;
427 }
429}
430
431constexpr vk::PrimitiveTopology ToVKPrimitiveTopology(PrimitiveType primitive) {
432 switch (primitive) {
434 return vk::PrimitiveTopology::eTriangleList;
436 return vk::PrimitiveTopology::eTriangleStrip;
438 return vk::PrimitiveTopology::eLineList;
440 return vk::PrimitiveTopology::eLineStrip;
442 return vk::PrimitiveTopology::ePointList;
444 return vk::PrimitiveTopology::eTriangleFan;
445 }
446
448}
449
491
492static constexpr vk::AttachmentReference kUnusedAttachmentReference = {
493 VK_ATTACHMENT_UNUSED, vk::ImageLayout::eUndefined};
494
495constexpr vk::CullModeFlags ToVKCullModeFlags(CullMode mode) {
496 switch (mode) {
497 case CullMode::kNone:
498 return vk::CullModeFlagBits::eNone;
500 return vk::CullModeFlagBits::eFront;
502 return vk::CullModeFlagBits::eBack;
503 }
505}
506
507constexpr vk::CompareOp ToVKCompareOp(CompareFunction op) {
508 switch (op) {
510 return vk::CompareOp::eNever;
512 return vk::CompareOp::eAlways;
514 return vk::CompareOp::eLess;
516 return vk::CompareOp::eEqual;
518 return vk::CompareOp::eLessOrEqual;
520 return vk::CompareOp::eGreater;
522 return vk::CompareOp::eNotEqual;
524 return vk::CompareOp::eGreaterOrEqual;
525 }
527}
528
529constexpr vk::StencilOp ToVKStencilOp(StencilOperation op) {
530 switch (op) {
532 return vk::StencilOp::eKeep;
534 return vk::StencilOp::eZero;
536 return vk::StencilOp::eReplace;
538 return vk::StencilOp::eIncrementAndClamp;
540 return vk::StencilOp::eDecrementAndClamp;
542 return vk::StencilOp::eInvert;
544 return vk::StencilOp::eIncrementAndWrap;
546 return vk::StencilOp::eDecrementAndWrap;
547 break;
548 }
550}
551
552constexpr vk::StencilOpState ToVKStencilOpState(
553 const StencilAttachmentDescriptor& desc) {
554 vk::StencilOpState state;
555 state.failOp = ToVKStencilOp(desc.stencil_failure);
556 state.passOp = ToVKStencilOp(desc.depth_stencil_pass);
557 state.depthFailOp = ToVKStencilOp(desc.depth_failure);
558 state.compareOp = ToVKCompareOp(desc.stencil_compare);
559 state.compareMask = desc.read_mask;
560 state.writeMask = desc.write_mask;
561 // This is irrelevant as the stencil references are always dynamic state and
562 // will be set in the render pass.
563 state.reference = 1988;
564 return state;
565}
566
567constexpr vk::ImageAspectFlags ToVKImageAspectFlags(PixelFormat format) {
568 switch (format) {
600 return vk::ImageAspectFlagBits::eColor;
602 return vk::ImageAspectFlagBits::eStencil;
605 return vk::ImageAspectFlagBits::eDepth |
606 vk::ImageAspectFlagBits::eStencil;
607 }
609}
610
611constexpr uint32_t ToArrayLayerCount(TextureType type) {
612 switch (type) {
615 return 1u;
617 return 6u;
620 << "kTextureExternalOES can not be used with the Vulkan backend.";
621 }
623}
624
625constexpr vk::ImageViewType ToVKImageViewType(TextureType type) {
626 switch (type) {
629 return vk::ImageViewType::e2D;
631 return vk::ImageViewType::eCube;
634 << "kTextureExternalOES can not be used with the Vulkan backend.";
635 }
637}
638
639constexpr vk::ImageCreateFlags ToVKImageCreateFlags(TextureType type) {
640 switch (type) {
643 return {};
645 return vk::ImageCreateFlagBits::eCubeCompatible;
648 << "kTextureExternalOES can not be used with the Vulkan backend.";
649 }
651}
652
653vk::PipelineDepthStencilStateCreateInfo ToVKPipelineDepthStencilStateCreateInfo(
654 std::optional<DepthAttachmentDescriptor> depth,
655 std::optional<StencilAttachmentDescriptor> front,
656 std::optional<StencilAttachmentDescriptor> back);
657
658constexpr vk::ImageAspectFlags ToImageAspectFlags(PixelFormat format) {
659 switch (format) {
661 return {};
692 return vk::ImageAspectFlagBits::eColor;
694 return vk::ImageAspectFlagBits::eStencil;
697 return vk::ImageAspectFlagBits::eDepth |
698 vk::ImageAspectFlagBits::eStencil;
699 }
701}
702
703} // namespace impeller
704
705#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_FORMATS_VK_H_
uint32_t uint32_t * format
#define FML_UNREACHABLE()
Definition logging.h:128
constexpr std::optional< PixelFormat > VkFormatToImpellerFormat(vk::Format format)
Definition formats_vk.h:20
static constexpr vk::AttachmentReference kUnusedAttachmentReference
Definition formats_vk.h:492
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition formats.h:529
@ kPoint
Draws a point at each input vertex.
constexpr vk::SamplerAddressMode ToVKSamplerAddressMode(SamplerAddressMode mode)
Definition formats_vk.h:284
@ kNone
Does not use the index buffer.
constexpr vk::PipelineColorBlendAttachmentState ToVKPipelineColorBlendAttachmentState(const ColorAttachmentDescriptor &desc)
Definition formats_vk.h:113
constexpr PixelFormat ToPixelFormat(vk::Format format)
Definition formats_vk.h:219
constexpr vk::CompareOp ToVKCompareOp(CompareFunction op)
Definition formats_vk.h:507
constexpr vk::SamplerMipmapMode ToVKSamplerMipmapMode(MipFilter filter)
Definition formats_vk.h:272
SamplerAddressMode
Definition formats.h:618
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
constexpr bool StoreActionPerformsResolve(StoreAction store_action)
Definition formats_vk.h:379
constexpr vk::IndexType ToVKIndexType(IndexType index_type)
Definition formats_vk.h:391
constexpr vk::DescriptorSetLayoutBinding ToVKDescriptorSetLayoutBinding(const DescriptorSetLayout &layout)
Definition formats_vk.h:332
constexpr uint32_t ToArrayLayerCount(TextureType type)
Definition formats_vk.h:611
constexpr vk::Filter ToVKSamplerMinMagFilter(MinMagFilter filter)
Definition formats_vk.h:261
constexpr vk::ImageAspectFlags ToImageAspectFlags(PixelFormat format)
Definition formats_vk.h:658
constexpr vk::SampleCountFlagBits ToVKSampleCountFlagBits(SampleCount count)
Definition formats_vk.h:32
constexpr vk::AttachmentLoadOp ToVKAttachmentLoadOp(LoadAction load_action)
Definition formats_vk.h:342
constexpr vk::DescriptorType ToVKDescriptorType(DescriptorType type)
Definition formats_vk.h:328
constexpr vk::PolygonMode ToVKPolygonMode(PolygonMode mode)
Definition formats_vk.h:406
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:99
CompareFunction
Definition formats.h:804
@ 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:355
constexpr vk::CullModeFlags ToVKCullModeFlags(CullMode mode)
Definition formats_vk.h:495
MipFilter
Options for selecting and filtering between mipmap levels.
Definition formats.h:602
@ 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::ColorComponentFlags ToVKColorComponentFlags(ColorWriteMask type)
Definition formats_vk.h:90
StencilOperation
Definition formats.h:823
@ 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:42
constexpr bool PixelFormatIsDepthStencil(PixelFormat format)
Definition formats_vk.h:450
vk::PipelineDepthStencilStateCreateInfo ToVKPipelineDepthStencilStateCreateInfo(std::optional< DepthAttachmentDescriptor > depth, std::optional< StencilAttachmentDescriptor > front, std::optional< StencilAttachmentDescriptor > back)
Definition formats_vk.cc:9
constexpr bool PrimitiveTopologySupportsPrimitiveRestart(PrimitiveType primitive)
Definition formats_vk.h:416
constexpr std::optional< vk::ShaderStageFlagBits > ToVKShaderStageFlagBits(ShaderStage stage)
Definition formats_vk.h:131
constexpr vk::ImageViewType ToVKImageViewType(TextureType type)
Definition formats_vk.h:625
constexpr vk::SampleCountFlagBits ToVKSampleCount(SampleCount sample_count)
Definition formats_vk.h:250
constexpr vk::Format ToVKImageFormat(PixelFormat format)
Definition formats_vk.h:146
constexpr vk::StencilOpState ToVKStencilOpState(const StencilAttachmentDescriptor &desc)
Definition formats_vk.h:552
constexpr vk::ShaderStageFlags ToVkShaderStage(ShaderStage stage)
Definition formats_vk.h:300
constexpr vk::PrimitiveTopology ToVKPrimitiveTopology(PrimitiveType primitive)
Definition formats_vk.h:431
constexpr vk::BlendOp ToVKBlendOp(BlendOperation op)
Definition formats_vk.h:78
constexpr vk::StencilOp ToVKStencilOp(StencilOperation op)
Definition formats_vk.h:529
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition formats.h:592
@ kNearest
Select nearest to the sample point. Most widely supported.
constexpr vk::ImageCreateFlags ToVKImageCreateFlags(TextureType type)
Definition formats_vk.h:639
constexpr vk::ImageAspectFlags ToVKImageAspectFlags(PixelFormat format)
Definition formats_vk.h:567
BlendOperation
Definition formats.h:384
impeller::ShaderType type
Describe the color attachment that will be used with this pipeline.
Definition formats.h:770
#define VALIDATION_LOG
Definition validation.h:91