Flutter Engine
The Flutter Engine
formats.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_CORE_FORMATS_H_
6#define FLUTTER_IMPELLER_CORE_FORMATS_H_
7
8#include <cstdint>
9#include <functional>
10#include <memory>
11#include <string>
12
13#include "flutter/fml/hash_combine.h"
14#include "flutter/fml/logging.h"
15#include "impeller/base/mask.h"
19
20namespace impeller {
21
22enum class WindingOrder {
25};
26
27class Texture;
28
29//------------------------------------------------------------------------------
30/// @brief Specified where the allocation resides and how it is used.
31///
32enum class StorageMode {
33 //----------------------------------------------------------------------------
34 /// Allocations can be mapped onto the hosts address space and also be used by
35 /// the device.
36 ///
38 //----------------------------------------------------------------------------
39 /// Allocations can only be used by the device. This location is optimal for
40 /// use by the device. If the host needs to access these allocations, the
41 /// transfer queue must be used to transfer this allocation onto the a host
42 /// visible buffer.
43 ///
45 //----------------------------------------------------------------------------
46 /// Used by the device for temporary render targets. These allocations cannot
47 /// be transferred from and to other allocations using the transfer queue.
48 /// Render pass cannot initialize the contents of these buffers using load and
49 /// store actions.
50 ///
51 /// These allocations reside in tile memory which has higher bandwidth, lower
52 /// latency and lower power consumption. The total device memory usage is
53 /// also lower as a separate allocation does not need to be created in
54 /// device memory. Prefer using these allocations for intermediates like depth
55 /// and stencil buffers.
56 ///
58};
59
60constexpr const char* StorageModeToString(StorageMode mode) {
61 switch (mode) {
63 return "HostVisible";
65 return "DevicePrivate";
67 return "DeviceTransient";
68 }
70}
71
72//------------------------------------------------------------------------------
73/// @brief The Pixel formats supported by Impeller. The naming convention
74/// denotes the usage of the component, the bit width of that
75/// component, and then one or more qualifiers to its
76/// interpretation.
77///
78/// For instance, `kR8G8B8A8UNormIntSRGB` is a 32 bits-per-pixel
79/// format ordered in RGBA with 8 bits per component with each
80/// component expressed as an unsigned normalized integer and a
81/// conversion from sRGB to linear color space.
82///
83/// Key:
84/// R -> Red Component
85/// G -> Green Component
86/// B -> Blue Component
87/// D -> Depth Component
88/// S -> Stencil Component
89/// U -> Unsigned (Lack of this denotes a signed component)
90/// Norm -> Normalized
91/// SRGB -> sRGB to linear interpretation
92///
93/// While the effective bit width of the pixel can be determined by
94/// adding up the widths of each component, only the non-esoteric
95/// formats are tightly packed. Do not assume tight packing for the
96/// esoteric formats and use blit passes to convert to a
97/// non-esoteric pass.
98///
99enum class PixelFormat : uint8_t {
100 kUnknown,
113 // Depth and stencil formats.
114 kS8UInt,
117};
118
120 switch (format) {
123 return true;
124 default:
125 return false;
126 }
127}
128
130 switch (format) {
134 return true;
135 default:
136 return false;
137 }
138}
139
140constexpr const char* PixelFormatToString(PixelFormat format) {
141 switch (format) {
143 return "Unknown";
145 return "A8UNormInt";
147 return "R8UNormInt";
149 return "R8G8UNormInt";
151 return "R8G8B8A8UNormInt";
153 return "R8G8B8A8UNormIntSRGB";
155 return "B8G8R8A8UNormInt";
157 return "B8G8R8A8UNormIntSRGB";
159 return "R32G32B32A32Float";
161 return "R16G16B16A16Float";
163 return "B10G10R10XR";
165 return "B10G10R10XRSRGB";
167 return "B10G10R10A10XR";
169 return "S8UInt";
171 return "D24UnormS8Uint";
173 return "D32FloatS8UInt";
174 }
176}
177
178enum class BlendFactor {
179 kZero,
180 kOne,
194};
195
196enum class BlendOperation {
197 kAdd,
198 kSubtract,
200};
201
202enum class LoadAction {
203 kDontCare,
204 kLoad,
205 kClear,
206};
207
208enum class StoreAction {
209 kDontCare,
210 kStore,
213};
214
215constexpr const char* LoadActionToString(LoadAction action) {
216 switch (action) {
218 return "DontCare";
220 return "Load";
222 return "Clear";
223 }
224}
225
226constexpr const char* StoreActionToString(StoreAction action) {
227 switch (action) {
229 return "DontCare";
231 return "Store";
233 return "MultisampleResolve";
235 return "StoreAndMultisampleResolve";
236 }
237}
238
240 switch (action) {
242 return false;
245 return true;
246 }
248}
249
251 switch (action) {
254 return false;
257 return true;
258 }
260}
261
262enum class TextureType {
267};
268
269constexpr const char* TextureTypeToString(TextureType type) {
270 switch (type) {
272 return "Texture2D";
274 return "Texture2DMultisample";
276 return "TextureCube";
278 return "TextureExternalOES";
279 }
281}
282
284 switch (type) {
288 return false;
290 return true;
291 }
292 return false;
293}
294
295enum class SampleCount : uint8_t {
296 kCount1 = 1,
297 kCount4 = 4,
298};
299
300enum class TextureUsage {
301 kUnknown = 0,
302 kShaderRead = 1 << 0,
303 kShaderWrite = 1 << 1,
304 kRenderTarget = 1 << 2,
305};
307
309
311 switch (usage) {
313 return "Unknown";
315 return "ShaderRead";
317 return "ShaderWrite";
319 return "RenderTarget";
320 }
322}
323
325
326// Texture coordinate system.
328 // Alternative coordinate system used when uploading texture data from the
329 // host.
330 // (0, 0) is the bottom-left of the image with +Y going up.
332 // Default coordinate system.
333 // (0, 0) is the top-left of the image with +Y going down.
335};
336
337enum class CullMode {
338 kNone,
340 kBackFace,
341};
342
343enum class IndexType {
344 kUnknown,
345 k16bit,
346 k32bit,
347 /// Does not use the index buffer.
348 kNone,
349};
350
351/// Decides how backend draws pixels based on input vertices.
352enum class PrimitiveType : uint8_t {
353 /// Draws a triage for each separate set of three vertices.
354 ///
355 /// Vertices [A, B, C, D, E, F] will produce triages
356 /// [ABC, DEF].
357 kTriangle,
358
359 /// Draws a triage for every adjacent three vertices.
360 ///
361 /// Vertices [A, B, C, D, E, F] will produce triages
362 /// [ABC, BCD, CDE, DEF].
364
365 /// Draws a line for each separate set of two vertices.
366 ///
367 /// Vertices [A, B, C] will produce discontinued line
368 /// [AB, BC].
369 kLine,
370
371 /// Draws a continuous line that connect every input vertices
372 ///
373 /// Vertices [A, B, C] will produce one continuous line
374 /// [ABC].
376
377 /// Draws a point at each input vertex.
378 kPoint,
379 // Triangle fans are implementation dependent and need extra extensions
380 // checks. Hence, they are not supported here.
381};
382
383enum class PolygonMode {
384 kFill,
385 kLine,
386};
387
391
392 constexpr bool operator==(const DepthRange& other) const {
393 return z_near == other.z_near && z_far == other.z_far;
394 }
395};
396
397struct Viewport {
400
401 constexpr bool operator==(const Viewport& other) const {
402 return rect == other.rect && depth_range == other.depth_range;
403 }
404};
405
406/// @brief Describes how the texture should be sampled when the texture
407/// is being shrunk (minified) or expanded (magnified) to fit to
408/// the sample point.
409enum class MinMagFilter {
410 /// Select nearest to the sample point. Most widely supported.
411 kNearest,
412
413 /// Select two points and linearly interpolate between them. Some formats
414 /// may not support this.
415 kLinear,
416};
417
418/// @brief Options for selecting and filtering between mipmap levels.
419enum class MipFilter {
420 /// @brief The texture is sampled as if it only had a single mipmap level.
421 ///
422 /// All samples are read from level 0.
423 kBase,
424
425 /// @brief The nearst mipmap level is selected.
426 kNearest,
427
428 /// @brief Sample from the two nearest mip levels and linearly interpolate.
429 ///
430 /// If the filter falls between levels, both levels are sampled, and
431 /// their results linearly interpolated between levels.
432 kLinear,
433};
434
437 kRepeat,
438 kMirror,
439 // More modes are almost always supported but they are usually behind
440 // extensions checks. The ones current in these structs are safe (always
441 // supported) defaults.
442
443 /// @brief decal sampling mode is only supported on devices that pass
444 /// the `Capabilities.SupportsDecalSamplerAddressMode` check.
445 kDecal,
446};
447
448enum class ColorWriteMaskBits : uint64_t {
449 kNone = 0,
450 kRed = 1 << 0,
451 kGreen = 1 << 1,
452 kBlue = 1 << 2,
453 kAlpha = 1 << 3,
454 kAll = kRed | kGreen | kBlue | kAlpha,
455};
457
459
461 switch (format) {
463 return 0u;
467 return 1u;
469 return 2u;
476 return 4u;
478 return 4u;
480 return 5u;
483 return 8u;
485 return 16u;
486 }
487 return 0u;
488}
489
490//------------------------------------------------------------------------------
491/// @brief Describe the color attachment that will be used with this
492/// pipeline.
493///
494/// Blending at specific color attachments follows the pseudo-code:
495/// ```
496/// if (blending_enabled) {
497/// final_color.rgb = (src_color_blend_factor * new_color.rgb)
498/// <color_blend_op>
499/// (dst_color_blend_factor * old_color.rgb);
500/// final_color.a = (src_alpha_blend_factor * new_color.a)
501/// <alpha_blend_op>
502/// (dst_alpha_blend_factor * old_color.a);
503/// } else {
504/// final_color = new_color;
505/// }
506/// // IMPORTANT: The write mask is applied irrespective of whether
507/// // blending_enabled is set.
508/// final_color = final_color & write_mask;
509/// ```
510///
511/// The default blend mode is 1 - source alpha.
514 bool blending_enabled = false;
515
519
523
525
526 constexpr bool operator==(const ColorAttachmentDescriptor& o) const {
527 return format == o.format && //
536 }
537
538 constexpr size_t Hash() const {
539 return fml::HashCombine(
542 dst_alpha_blend_factor, static_cast<uint64_t>(write_mask));
543 }
544};
545
546enum class CompareFunction : uint8_t {
547 /// Comparison test never passes.
548 kNever,
549 /// Comparison test passes always passes.
550 kAlways,
551 /// Comparison test passes if new_value < current_value.
552 kLess,
553 /// Comparison test passes if new_value == current_value.
554 kEqual,
555 /// Comparison test passes if new_value <= current_value.
557 /// Comparison test passes if new_value > current_value.
558 kGreater,
559 /// Comparison test passes if new_value != current_value.
560 kNotEqual,
561 /// Comparison test passes if new_value >= current_value.
563};
564
565enum class StencilOperation : uint8_t {
566 /// Don't modify the current stencil value.
567 kKeep,
568 /// Reset the stencil value to zero.
569 kZero,
570 /// Reset the stencil value to the reference value.
572 /// Increment the current stencil value by 1. Clamp it to the maximum.
574 /// Decrement the current stencil value by 1. Clamp it to zero.
576 /// Perform a logical bitwise invert on the current stencil value.
577 kInvert,
578 /// Increment the current stencil value by 1. If at maximum, set to zero.
580 /// Decrement the current stencil value by 1. If at zero, set to maximum.
582};
583
585 //----------------------------------------------------------------------------
586 /// Indicates how to compare the value with that in the depth buffer.
587 ///
589 //----------------------------------------------------------------------------
590 /// Indicates when writes must be performed to the depth buffer.
591 ///
593
594 constexpr bool operator==(const DepthAttachmentDescriptor& o) const {
595 return depth_compare == o.depth_compare &&
597 }
598
599 constexpr size_t GetHash() const {
601 }
602};
603
605 //----------------------------------------------------------------------------
606 /// Indicates the operation to perform between the reference value and the
607 /// value in the stencil buffer. Both values have the read_mask applied to
608 /// them before performing this operation.
609 ///
611 //----------------------------------------------------------------------------
612 /// Indicates what to do when the stencil test has failed.
613 ///
615 //----------------------------------------------------------------------------
616 /// Indicates what to do when the stencil test passes but the depth test
617 /// fails.
618 ///
620 //----------------------------------------------------------------------------
621 /// Indicates what to do when both the stencil and depth tests pass.
622 ///
624
625 //----------------------------------------------------------------------------
626 /// The mask applied to the reference and stencil buffer values before
627 /// performing the stencil_compare operation.
628 ///
629 uint32_t read_mask = ~0;
630 //----------------------------------------------------------------------------
631 /// The mask applied to the new stencil value before it is written into the
632 /// stencil buffer.
633 ///
634 uint32_t write_mask = ~0;
635
636 constexpr bool operator==(const StencilAttachmentDescriptor& o) const {
637 return stencil_compare == o.stencil_compare &&
642 }
643
644 constexpr size_t GetHash() const {
647 }
648};
649
651 std::shared_ptr<Texture> texture;
652 std::shared_ptr<Texture> resolve_texture;
655
656 bool IsValid() const;
657};
658
661};
662
664 double clear_depth = 0.0;
665};
666
668 uint32_t clear_stencil = 0;
669};
670
671std::string AttachmentToString(const Attachment& attachment);
672
674
675std::string DepthAttachmentToString(const DepthAttachment& depth);
676
677std::string StencilAttachmentToString(const StencilAttachment& stencil);
678
679} // namespace impeller
680
681namespace std {
682
683template <>
684struct hash<impeller::DepthAttachmentDescriptor> {
685 constexpr std::size_t operator()(
686 const impeller::DepthAttachmentDescriptor& des) const {
687 return des.GetHash();
688 }
689};
690
691template <>
692struct hash<impeller::StencilAttachmentDescriptor> {
693 constexpr std::size_t operator()(
694 const impeller::StencilAttachmentDescriptor& des) const {
695 return des.GetHash();
696 }
697};
698
699} // namespace std
700
701#endif // FLUTTER_IMPELLER_CORE_FORMATS_H_
@ kClear
r = 0
static uint32_t hash(const SkShaderBase::GradientInfo &v)
@ kLine
SkPath::RawIter returns 2 points.
constexpr auto kNever
Definition: SkSLTest.cpp:963
GLenum type
DlColor color
@ kAdd
Definition: embedder.h:990
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
constexpr std::size_t HashCombine()
Definition: hash_combine.h:25
constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format)
Definition: formats.h:460
constexpr bool CanClearAttachment(LoadAction action)
Definition: formats.h:239
std::string DepthAttachmentToString(const DepthAttachment &depth)
Definition: formats.cc:130
std::string ColorAttachmentToString(const ColorAttachment &color)
Definition: formats.cc:123
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.
float Scalar
Definition: scalar.h:18
MipFilter
Options for selecting and filtering between mipmap levels.
Definition: formats.h:419
@ kBase
The texture is sampled as if it only had a single mipmap level.
constexpr bool IsDepthWritable(PixelFormat format)
Definition: formats.h:119
std::string StencilAttachmentToString(const StencilAttachment &stencil)
Definition: formats.cc:137
std::string TextureUsageMaskToString(TextureUsageMask mask)
Definition: formats.cc:81
StorageMode
Specified where the allocation resides and how it is used.
Definition: formats.h:32
std::string AttachmentToString(const Attachment &attachment)
Definition: formats.cc:104
SamplerAddressMode
Definition: formats.h:435
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
@ 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.
constexpr const char * LoadActionToString(LoadAction action)
Definition: formats.h:215
constexpr const char * StoreActionToString(StoreAction action)
Definition: formats.h:226
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.
@ 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.
TextureType
Definition: formats.h:262
TextureCoordinateSystem
Definition: formats.h:327
WindingOrder
Definition: formats.h:22
IMPELLER_ENUM_IS_MASK(MyMaskBits)
LoadAction
Definition: formats.h:202
StoreAction
Definition: formats.h:208
constexpr bool IsStencilWritable(PixelFormat format)
Definition: formats.h:129
PolygonMode
Definition: formats.h:383
MinMagFilter
Describes how the texture should be sampled when the texture is being shrunk (minified) or expanded (...
Definition: formats.h:409
constexpr const char * TextureTypeToString(TextureType type)
Definition: formats.h:269
constexpr const char * StorageModeToString(StorageMode mode)
Definition: formats.h:60
constexpr bool IsMultisampleCapable(TextureType type)
Definition: formats.h:283
ColorWriteMaskBits
Definition: formats.h:448
constexpr const char * TextureUsageToString(TextureUsage usage)
Definition: formats.h:310
Mask< TextureUsage > TextureUsageMask
Definition: formats.h:308
constexpr const char * PixelFormatToString(PixelFormat format)
Definition: formats.h:140
BlendOperation
Definition: formats.h:196
TextureUsage
Definition: formats.h:300
SampleCount
Definition: formats.h:295
constexpr bool CanDiscardAttachmentWhenDone(StoreAction action)
Definition: formats.h:250
Definition: ref_ptr.h:256
static void usage(char *argv0)
std::shared_ptr< Texture > resolve_texture
Definition: formats.h:652
bool IsValid() const
Definition: formats.cc:26
LoadAction load_action
Definition: formats.h:653
std::shared_ptr< Texture > texture
Definition: formats.h:651
StoreAction store_action
Definition: formats.h:654
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:512
constexpr size_t Hash() const
Definition: formats.h:538
constexpr bool operator==(const ColorAttachmentDescriptor &o) const
Definition: formats.h:526
static constexpr Color BlackTransparent()
Definition: color.h:272
constexpr bool operator==(const DepthAttachmentDescriptor &o) const
Definition: formats.h:594
constexpr size_t GetHash() const
Definition: formats.h:599
constexpr bool operator==(const DepthRange &other) const
Definition: formats.h:392
constexpr bool operator==(const StencilAttachmentDescriptor &o) const
Definition: formats.h:636
constexpr size_t GetHash() const
Definition: formats.h:644
StencilOperation depth_stencil_pass
Definition: formats.h:623
constexpr bool operator==(const Viewport &other) const
Definition: formats.h:401
DepthRange depth_range
Definition: formats.h:399
constexpr std::size_t operator()(const impeller::DepthAttachmentDescriptor &des) const
Definition: formats.h:685
constexpr std::size_t operator()(const impeller::StencilAttachmentDescriptor &des) const
Definition: formats.h:693