Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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#include <type_traits>
13
14#include "flutter/fml/hash_combine.h"
15#include "flutter/fml/logging.h"
16#include "impeller/base/mask.h"
20
21namespace impeller {
22
23enum class WindingOrder {
26};
27
28class Texture;
29
30//------------------------------------------------------------------------------
31/// @brief Specified where the allocation resides and how it is used.
32///
33enum class StorageMode {
34 //----------------------------------------------------------------------------
35 /// Allocations can be mapped onto the hosts address space and also be used by
36 /// the device.
37 ///
39 //----------------------------------------------------------------------------
40 /// Allocations can only be used by the device. This location is optimal for
41 /// use by the device. If the host needs to access these allocations, the
42 /// transfer queue must be used to transfer this allocation onto the a host
43 /// visible buffer.
44 ///
46 //----------------------------------------------------------------------------
47 /// Used by the device for temporary render targets. These allocations cannot
48 /// be transferred from and to other allocations using the transfer queue.
49 /// Render pass cannot initialize the contents of these buffers using load and
50 /// store actions.
51 ///
52 /// These allocations reside in tile memory which has higher bandwidth, lower
53 /// latency and lower power consumption. The total device memory usage is
54 /// also lower as a separate allocation does not need to be created in
55 /// device memory. Prefer using these allocations for intermediates like depth
56 /// and stencil buffers.
57 ///
59};
60
61constexpr const char* StorageModeToString(StorageMode mode) {
62 switch (mode) {
64 return "HostVisible";
66 return "DevicePrivate";
68 return "DeviceTransient";
69 }
71}
72
73//------------------------------------------------------------------------------
74/// @brief The Pixel formats supported by Impeller. The naming convention
75/// denotes the usage of the component, the bit width of that
76/// component, and then one or more qualifiers to its
77/// interpretation.
78///
79/// For instance, `kR8G8B8A8UNormIntSRGB` is a 32 bits-per-pixel
80/// format ordered in RGBA with 8 bits per component with each
81/// component expressed as an unsigned normalized integer and a
82/// conversion from sRGB to linear color space.
83///
84/// Key:
85/// R -> Red Component
86/// G -> Green Component
87/// B -> Blue Component
88/// D -> Depth Component
89/// S -> Stencil Component
90/// U -> Unsigned (Lack of this denotes a signed component)
91/// Norm -> Normalized
92/// SRGB -> sRGB to linear interpretation
93///
94/// While the effective bit width of the pixel can be determined by
95/// adding up the widths of each component, only the non-esoteric
96/// formats are tightly packed. Do not assume tight packing for the
97/// esoteric formats and use blit passes to convert to a
98/// non-esoteric pass.
99///
119
121 switch (format) {
124 return true;
125 default:
126 return false;
127 }
128}
129
131 switch (format) {
135 return true;
136 default:
137 return false;
138 }
139}
140
141constexpr const char* PixelFormatToString(PixelFormat format) {
142 switch (format) {
144 return "Unknown";
146 return "A8UNormInt";
148 return "R8UNormInt";
150 return "R8G8UNormInt";
152 return "R8G8B8A8UNormInt";
154 return "R8G8B8A8UNormIntSRGB";
156 return "B8G8R8A8UNormInt";
158 return "B8G8R8A8UNormIntSRGB";
160 return "R32G32B32A32Float";
162 return "R16G16B16A16Float";
164 return "B10G10R10XR";
166 return "B10G10R10XRSRGB";
168 return "B10G10R10A10XR";
170 return "S8UInt";
172 return "D24UnormS8Uint";
174 return "D32FloatS8UInt";
175 }
177}
178
196
197enum class BlendOperation {
198 kAdd,
199 kSubtract,
201};
202
203enum class LoadAction {
204 kDontCare,
205 kLoad,
206 kClear,
207};
208
215
216constexpr const char* LoadActionToString(LoadAction action) {
217 switch (action) {
219 return "DontCare";
221 return "Load";
223 return "Clear";
224 }
225}
226
227constexpr const char* StoreActionToString(StoreAction action) {
228 switch (action) {
230 return "DontCare";
232 return "Store";
234 return "MultisampleResolve";
236 return "StoreAndMultisampleResolve";
237 }
238}
239
241 switch (action) {
243 return false;
246 return true;
247 }
249}
250
252 switch (action) {
255 return false;
258 return true;
259 }
261}
262
269
270constexpr const char* TextureTypeToString(TextureType type) {
271 switch (type) {
273 return "Texture2D";
275 return "Texture2DMultisample";
277 return "TextureCube";
279 return "TextureExternalOES";
280 }
282}
283
285 switch (type) {
289 return false;
291 return true;
292 }
293 return false;
294}
295
296enum class SampleCount : uint8_t {
297 kCount1 = 1,
298 kCount4 = 4,
299};
300
301enum class TextureUsage {
302 kUnknown = 0,
303 kShaderRead = 1 << 0,
304 kShaderWrite = 1 << 1,
305 kRenderTarget = 1 << 2,
306};
308
310
312 switch (usage) {
314 return "Unknown";
316 return "ShaderRead";
318 return "ShaderWrite";
320 return "RenderTarget";
321 }
323}
324
326
327// Texture coordinate system.
329 // Alternative coordinate system used when uploading texture data from the
330 // host.
331 // (0, 0) is the bottom-left of the image with +Y going up.
333 // Default coordinate system.
334 // (0, 0) is the top-left of the image with +Y going down.
336};
337
338enum class CullMode {
339 kNone,
341 kBackFace,
342};
343
344enum class IndexType {
345 kUnknown,
346 k16bit,
347 k32bit,
348 /// Does not use the index buffer.
349 kNone,
350};
351
352/// Decides how backend draws pixels based on input vertices.
353enum class PrimitiveType : uint8_t {
354 /// Draws a triage for each separate set of three vertices.
355 ///
356 /// Vertices [A, B, C, D, E, F] will produce triages
357 /// [ABC, DEF].
358 kTriangle,
359
360 /// Draws a triage for every adjacent three vertices.
361 ///
362 /// Vertices [A, B, C, D, E, F] will produce triages
363 /// [ABC, BCD, CDE, DEF].
365
366 /// Draws a line for each separate set of two vertices.
367 ///
368 /// Vertices [A, B, C] will produce discontinued line
369 /// [AB, BC].
370 kLine,
371
372 /// Draws a continuous line that connect every input vertices
373 ///
374 /// Vertices [A, B, C] will produce one continuous line
375 /// [ABC].
377
378 /// Draws a point at each input vertex.
379 kPoint,
380 // Triangle fans are implementation dependent and need extra extensions
381 // checks. Hence, they are not supported here.
382};
383
384enum class PolygonMode {
385 kFill,
386 kLine,
387};
388
392
393 constexpr bool operator==(const DepthRange& other) const {
394 return z_near == other.z_near && z_far == other.z_far;
395 }
396};
397
398struct Viewport {
401
402 constexpr bool operator==(const Viewport& other) const {
403 return rect == other.rect && depth_range == other.depth_range;
404 }
405};
406
407enum class MinMagFilter {
408 /// Select nearest to the sample point. Most widely supported.
409 kNearest,
410 /// Select two points and linearly interpolate between them. Some formats
411 /// may not support this.
412 kLinear,
413};
414
415enum class MipFilter {
416 /// Sample from the nearest mip level.
417 kNearest,
418 /// Sample from the two nearest mip levels and linearly interpolate between
419 /// them.
420 kLinear,
421};
422
425 kRepeat,
426 kMirror,
427 // More modes are almost always supported but they are usually behind
428 // extensions checks. The ones current in these structs are safe (always
429 // supported) defaults.
430
431 /// @brief decal sampling mode is only supported on devices that pass
432 /// the `Capabilities.SupportsDecalSamplerAddressMode` check.
433 kDecal,
434};
435
436enum class ColorWriteMaskBits : uint64_t {
437 kNone = 0,
438 kRed = 1 << 0,
439 kGreen = 1 << 1,
440 kBlue = 1 << 2,
441 kAlpha = 1 << 3,
442 kAll = kRed | kGreen | kBlue | kAlpha,
443};
445
447
449 switch (format) {
451 return 0u;
455 return 1u;
457 return 2u;
464 return 4u;
466 return 4u;
468 return 5u;
471 return 8u;
473 return 16u;
474 }
475 return 0u;
476}
477
478//------------------------------------------------------------------------------
479/// @brief Describe the color attachment that will be used with this
480/// pipeline.
481///
482/// Blending at specific color attachments follows the pseudo-code:
483/// ```
484/// if (blending_enabled) {
485/// final_color.rgb = (src_color_blend_factor * new_color.rgb)
486/// <color_blend_op>
487/// (dst_color_blend_factor * old_color.rgb);
488/// final_color.a = (src_alpha_blend_factor * new_color.a)
489/// <alpha_blend_op>
490/// (dst_alpha_blend_factor * old_color.a);
491/// } else {
492/// final_color = new_color;
493/// }
494/// // IMPORTANT: The write mask is applied irrespective of whether
495/// // blending_enabled is set.
496/// final_color = final_color & write_mask;
497/// ```
498///
499/// The default blend mode is 1 - source alpha.
533
534enum class CompareFunction : uint8_t {
535 /// Comparison test never passes.
536 kNever,
537 /// Comparison test passes always passes.
538 kAlways,
539 /// Comparison test passes if new_value < current_value.
540 kLess,
541 /// Comparison test passes if new_value == current_value.
542 kEqual,
543 /// Comparison test passes if new_value <= current_value.
545 /// Comparison test passes if new_value > current_value.
546 kGreater,
547 /// Comparison test passes if new_value != current_value.
548 kNotEqual,
549 /// Comparison test passes if new_value >= current_value.
551};
552
553enum class StencilOperation : uint8_t {
554 /// Don't modify the current stencil value.
555 kKeep,
556 /// Reset the stencil value to zero.
557 kZero,
558 /// Reset the stencil value to the reference value.
560 /// Increment the current stencil value by 1. Clamp it to the maximum.
562 /// Decrement the current stencil value by 1. Clamp it to zero.
564 /// Perform a logical bitwise invert on the current stencil value.
565 kInvert,
566 /// Increment the current stencil value by 1. If at maximum, set to zero.
568 /// Decrement the current stencil value by 1. If at zero, set to maximum.
570};
571
573 //----------------------------------------------------------------------------
574 /// Indicates how to compare the value with that in the depth buffer.
575 ///
577 //----------------------------------------------------------------------------
578 /// Indicates when writes must be performed to the depth buffer.
579 ///
581
582 constexpr bool operator==(const DepthAttachmentDescriptor& o) const {
583 return depth_compare == o.depth_compare &&
585 }
586
587 constexpr size_t GetHash() const {
589 }
590};
591
593 //----------------------------------------------------------------------------
594 /// Indicates the operation to perform between the reference value and the
595 /// value in the stencil buffer. Both values have the read_mask applied to
596 /// them before performing this operation.
597 ///
599 //----------------------------------------------------------------------------
600 /// Indicates what to do when the stencil test has failed.
601 ///
603 //----------------------------------------------------------------------------
604 /// Indicates what to do when the stencil test passes but the depth test
605 /// fails.
606 ///
608 //----------------------------------------------------------------------------
609 /// Indicates what to do when both the stencil and depth tests pass.
610 ///
612
613 //----------------------------------------------------------------------------
614 /// The mask applied to the reference and stencil buffer values before
615 /// performing the stencil_compare operation.
616 ///
617 uint32_t read_mask = ~0;
618 //----------------------------------------------------------------------------
619 /// The mask applied to the new stencil value before it is written into the
620 /// stencil buffer.
621 ///
622 uint32_t write_mask = ~0;
623
631
636};
637
639 std::shared_ptr<Texture> texture;
640 std::shared_ptr<Texture> resolve_texture;
643
644 bool IsValid() const;
645};
646
650
652 double clear_depth = 0.0;
653};
654
656 uint32_t clear_stencil = 0;
657};
658
659std::string AttachmentToString(const Attachment& attachment);
660
662
663std::string DepthAttachmentToString(const DepthAttachment& depth);
664
665std::string StencilAttachmentToString(const StencilAttachment& stencil);
666
667} // namespace impeller
668
669namespace std {
670
671template <>
672struct hash<impeller::DepthAttachmentDescriptor> {
673 constexpr std::size_t operator()(
674 const impeller::DepthAttachmentDescriptor& des) const {
675 return des.GetHash();
676 }
677};
678
679template <>
680struct hash<impeller::StencilAttachmentDescriptor> {
681 constexpr std::size_t operator()(
682 const impeller::StencilAttachmentDescriptor& des) const {
683 return des.GetHash();
684 }
685};
686
687} // namespace std
688
689#endif // FLUTTER_IMPELLER_CORE_FORMATS_H_
SkColor4f color
static uint32_t hash(const SkShaderBase::GradientInfo &v)
uint32_t uint32_t * format
#define FML_UNREACHABLE()
Definition logging.h:109
#define IMPELLER_ENUM_IS_MASK(enum_name)
Declare this in the "impeller" namespace to make the enum maskable.
Definition mask.h:21
constexpr std::size_t HashCombine()
constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format)
Definition formats.h:448
constexpr bool CanClearAttachment(LoadAction action)
Definition formats.h:240
std::string DepthAttachmentToString(const DepthAttachment &depth)
Definition formats.cc:130
std::string ColorAttachmentToString(const ColorAttachment &color)
Definition formats.cc:123
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition formats.h:353
@ kPoint
Draws a point at each input vertex.
float Scalar
Definition scalar.h:18
constexpr bool IsDepthWritable(PixelFormat format)
Definition formats.h:120
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:33
std::string AttachmentToString(const Attachment &attachment)
Definition formats.cc:104
SamplerAddressMode
Definition formats.h:423
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:100
CompareFunction
Definition formats.h:534
@ 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 const char * LoadActionToString(LoadAction action)
Definition formats.h:216
constexpr const char * StoreActionToString(StoreAction action)
Definition formats.h:227
StencilOperation
Definition formats.h:553
@ 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.
TextureCoordinateSystem
Definition formats.h:328
WindingOrder
Definition formats.h:23
constexpr bool IsStencilWritable(PixelFormat format)
Definition formats.h:130
@ kNearest
Select nearest to the sample point. Most widely supported.
constexpr const char * TextureTypeToString(TextureType type)
Definition formats.h:270
constexpr const char * StorageModeToString(StorageMode mode)
Definition formats.h:61
constexpr bool IsMultisampleCapable(TextureType type)
Definition formats.h:284
ColorWriteMaskBits
Definition formats.h:436
constexpr const char * TextureUsageToString(TextureUsage usage)
Definition formats.h:311
Mask< TextureUsage > TextureUsageMask
Definition formats.h:309
constexpr const char * PixelFormatToString(PixelFormat format)
Definition formats.h:141
BlendOperation
Definition formats.h:197
constexpr bool CanDiscardAttachmentWhenDone(StoreAction action)
Definition formats.h:251
Definition ref_ptr.h:256
static void usage(char *argv0)
std::shared_ptr< Texture > resolve_texture
Definition formats.h:640
bool IsValid() const
Definition formats.cc:26
LoadAction load_action
Definition formats.h:641
std::shared_ptr< Texture > texture
Definition formats.h:639
StoreAction store_action
Definition formats.h:642
Describe the color attachment that will be used with this pipeline.
Definition formats.h:500
constexpr size_t Hash() const
Definition formats.h:526
constexpr bool operator==(const ColorAttachmentDescriptor &o) const
Definition formats.h:514
static constexpr Color BlackTransparent()
Definition color.h:262
constexpr bool operator==(const DepthAttachmentDescriptor &o) const
Definition formats.h:582
constexpr size_t GetHash() const
Definition formats.h:587
constexpr bool operator==(const DepthRange &other) const
Definition formats.h:393
constexpr bool operator==(const StencilAttachmentDescriptor &o) const
Definition formats.h:624
constexpr size_t GetHash() const
Definition formats.h:632
constexpr bool operator==(const Viewport &other) const
Definition formats.h:402
DepthRange depth_range
Definition formats.h:400
constexpr std::size_t operator()(const impeller::DepthAttachmentDescriptor &des) const
Definition formats.h:673
constexpr std::size_t operator()(const impeller::StencilAttachmentDescriptor &des) const
Definition formats.h:681