rafx_api/types/
misc.rs

1#[cfg(feature = "serde-support")]
2use serde::{Deserialize, Serialize};
3
4use crate::{RafxBuffer, RafxBufferDef, RafxSampler, RafxTexture};
5use rafx_base::DecimalF32;
6use std::hash::{Hash, Hasher};
7
8#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
9pub enum RafxApiType {
10    Vk = 0,
11    Dx12 = 1,
12    Metal = 2,
13    Gles2 = 3,
14    Gles3 = 4,
15    Empty = 5,
16}
17
18// Generally we shouldn't use empty, it's excluded from this list
19pub const RAFX_VALID_API_TYPES: [RafxApiType; 5] = [
20    RafxApiType::Vk,
21    RafxApiType::Dx12,
22    RafxApiType::Metal,
23    RafxApiType::Gles2,
24    RafxApiType::Gles3,
25];
26
27/// Controls if validation is enabled or not. The requirements/behaviors of validation is
28/// API-specific.
29#[derive(Copy, Clone, Debug, PartialEq)]
30pub enum RafxValidationMode {
31    /// Do not enable validation. Even if validation is turned on through external means, do not
32    /// intentionally fail initialization
33    Disabled,
34
35    /// Enable validation if possible. (Details on requirements to enable at runtime are
36    /// API-specific)
37    EnabledIfAvailable,
38
39    /// Enable validation, and fail if we cannot enable it or detect that it is not enabled through
40    /// external means. (Details on this are API-specific)
41    Enabled,
42}
43
44impl Default for RafxValidationMode {
45    fn default() -> Self {
46        #[cfg(debug_assertions)]
47        let validation_mode = RafxValidationMode::EnabledIfAvailable;
48        #[cfg(not(debug_assertions))]
49        let validation_mode = RafxValidationMode::Disabled;
50
51        validation_mode
52    }
53}
54
55/// Information about the device, mostly limits, requirements (like memory alignment), and flags to
56/// indicate whether certain features are supported
57pub struct RafxDeviceInfo {
58    pub supports_multithreaded_usage: bool,
59    pub debug_names_enabled: bool,
60    pub min_uniform_buffer_offset_alignment: u32,
61    pub min_storage_buffer_offset_alignment: u32,
62    pub upload_texture_alignment: u32,
63    pub upload_texture_row_alignment: u32,
64
65    // Requires iOS 14.0, macOS 10.12
66    pub supports_clamp_to_border_color: bool,
67
68    pub max_vertex_attribute_count: u32,
69    //max_vertex_input_binding_count: u32,
70    // max_root_signature_dwords: u32,
71    // wave_lane_count: u32,
72    // wave_ops_support_flags: u32,
73    // gpu_vendor_preset: u32,
74    // metal_argument_buffer_max_textures: u32,
75    // metal_heaps: u32,
76    // metal_placement_heaps: u32,
77    // metal_draw_index_vertex_offset_supported: bool,
78}
79
80/// Used to indicate which type of queue to use. Some operations require certain types of queues.
81#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
82pub enum RafxQueueType {
83    /// Graphics queues generally supports all operations and are a safe default choice
84    Graphics,
85
86    /// Compute queues can be used for compute-based work.
87    Compute,
88
89    /// Transfer queues are generally limited to basic operations like copying data from buffers
90    /// to images.
91    Transfer,
92}
93
94/// The color space an image data is in. The correct color space often varies between texture types
95/// (like normal maps vs. albedo maps).
96#[derive(Copy, Clone, Debug)]
97pub enum RafxColorType {
98    Linear,
99    Srgb,
100}
101
102// /// Texture will allocate its own memory (COMMITTED resource)
103// TEXTURE_CREATION_FLAG_OWN_MEMORY_BIT = 0x01,
104// /// Use on-tile memory to store this texture
105// TEXTURE_CREATION_FLAG_ON_TILE = 0x20,
106// /// Force 2D instead of automatically determining dimension based on width, height, depth
107// TEXTURE_CREATION_FLAG_FORCE_2D = 0x80,
108// /// Force 3D instead of automatically determining dimension based on width, height, depth
109// TEXTURE_CREATION_FLAG_FORCE_3D = 0x100,
110// /// Display target
111// TEXTURE_CREATION_FLAG_ALLOW_DISPLAY_TARGET = 0x200,
112// /// Create an sRGB texture.
113// TEXTURE_CREATION_FLAG_SRGB = 0x400,
114
115bitflags::bitflags! {
116    /// The current state of a resource. When an operation is performed that references a resource,
117    /// it must be in the correct state. Resources are moved between state using barriers.
118    ///
119    /// The implementation of resource_state_to_access_flags() in the vulkan backend gives a more
120    /// thorough explanation for what these states imply about syncrhonization. See also
121    /// determine_pipeline_stage_flags() and resource_state_to_image_layout() in the vulkan backend.
122    pub struct RafxResourceState: u32 {
123        const UNDEFINED = 0;
124        const VERTEX_AND_CONSTANT_BUFFER = 0x1;
125        const INDEX_BUFFER = 0x2;
126        /// Similar to vulkan's COLOR_ATTACHMENT_OPTIMAL image layout
127        const RENDER_TARGET = 0x4;
128        /// Shader read/write
129        const UNORDERED_ACCESS = 0x8;
130        /// Similar to vulkan's DEPTH_STENCIL_ATTACHMENT_OPTIMAL image layout
131        const DEPTH_WRITE = 0x10;
132        const DEPTH_READ = 0x20;
133        const NON_PIXEL_SHADER_RESOURCE = 0x40;
134        const PIXEL_SHADER_RESOURCE = 0x80;
135        /// Similar to vulkan's SHADER_READ_ONLY_OPTIMAL image layout
136        const SHADER_RESOURCE = 0x40 | 0x80;
137        const STREAM_OUT = 0x100;
138        const INDIRECT_ARGUMENT = 0x200;
139        /// Similar to vulkan's TRANSFER_DST_OPTIMAL image layout
140        const COPY_DST = 0x400;
141        /// Similar to vulkan's TRANSFER_SRC_OPTIMAL image layout
142        const COPY_SRC = 0x800;
143        const GENERIC_READ = (((((0x1 | 0x2) | 0x40) | 0x80) | 0x200) | 0x800);
144        /// Similar to vulkan's PRESENT_SRC_KHR image layout
145        const PRESENT = 0x1000;
146        /// Similar to vulkan's COMMON image layout
147        const COMMON = 0x2000;
148    }
149}
150
151/// A 2d size for windows, textures, etc.
152#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, Hash)]
153pub struct RafxExtents2D {
154    pub width: u32,
155    pub height: u32,
156}
157
158/// A 3d size for windows, textures, etc.
159#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, Hash)]
160pub struct RafxExtents3D {
161    pub width: u32,
162    pub height: u32,
163    pub depth: u32,
164}
165
166impl RafxExtents3D {
167    pub fn to_2d(self) -> RafxExtents2D {
168        RafxExtents2D {
169            width: self.width,
170            height: self.height,
171        }
172    }
173}
174
175/// Number of MSAA samples to use. 1xMSAA and 4xMSAA are most broadly supported
176#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
177#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
178pub enum RafxSampleCount {
179    SampleCount1,
180    SampleCount2,
181    SampleCount4,
182    SampleCount8,
183    SampleCount16,
184}
185
186impl Default for RafxSampleCount {
187    fn default() -> Self {
188        RafxSampleCount::SampleCount1
189    }
190}
191
192impl RafxSampleCount {
193    pub fn from_u32(samples: u32) -> Self {
194        match samples {
195            1 => RafxSampleCount::SampleCount1,
196            2 => RafxSampleCount::SampleCount2,
197            4 => RafxSampleCount::SampleCount4,
198            8 => RafxSampleCount::SampleCount8,
199            16 => RafxSampleCount::SampleCount16,
200            _ => unimplemented!(),
201        }
202    }
203
204    pub fn as_u32(self) -> u32 {
205        match self {
206            RafxSampleCount::SampleCount1 => 1,
207            RafxSampleCount::SampleCount2 => 2,
208            RafxSampleCount::SampleCount4 => 4,
209            RafxSampleCount::SampleCount8 => 8,
210            RafxSampleCount::SampleCount16 => 16,
211        }
212    }
213}
214
215bitflags::bitflags! {
216    /// Indicates how a resource will be used. In some cases, multiple flags are allowed.
217    #[derive(Default)]
218    #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
219    pub struct RafxResourceType: u32 {
220        const UNDEFINED = 0;
221        const SAMPLER = 1<<0;
222        /// Similar to DX12 SRV and vulkan SAMPLED image usage flag and SAMPLED_IMAGE descriptor type
223        const TEXTURE = 1<<1;
224        /// Similar to DX12 UAV and vulkan STORAGE image usage flag and STORAGE_IMAGE descriptor type
225        const TEXTURE_READ_WRITE = 1<<2;
226        /// Similar to DX12 SRV and vulkan STORAGE_BUFFER descriptor type
227        const BUFFER = 1<<3;
228        /// Similar to DX12 UAV and vulkan STORAGE_BUFFER descriptor type
229        const BUFFER_READ_WRITE = 1<<5;
230        /// Similar to vulkan UNIFORM_BUFFER descriptor type
231        const UNIFORM_BUFFER = 1<<7;
232        // Push constant / Root constant
233        /// Similar to DX12 root constants and vulkan push constants
234        const ROOT_CONSTANT = 1<<8;
235        // Input assembler
236        /// Similar to vulkan VERTEX_BUFFER buffer usage flag
237        const VERTEX_BUFFER = 1<<9;
238        /// Similar to vulkan INDEX_BUFFER buffer usage flag
239        const INDEX_BUFFER = 1<<10;
240        /// Similar to vulkan INDIRECT_BUFFER buffer usage flag
241        const INDIRECT_BUFFER = 1<<11;
242        // Cubemap SRV
243        /// Similar to vulkan's CUBE_COMPATIBLE image create flag and metal's Cube texture type
244        const TEXTURE_CUBE = 1<<12 | RafxResourceType::TEXTURE.bits();
245        // RTV
246        const RENDER_TARGET_MIP_SLICES = 1<<13;
247        const RENDER_TARGET_ARRAY_SLICES = 1<<14;
248        const RENDER_TARGET_DEPTH_SLICES = 1<<15;
249        // Vulkan-only stuff
250        const INPUT_ATTACHMENT = 1<<16;
251        const TEXEL_BUFFER = 1<<17;
252        const TEXEL_BUFFER_READ_WRITE = 1<<18;
253        const COMBINED_IMAGE_SAMPLER = 1<<19;
254        // Metal-only stuff
255        const ARGUMENT_BUFFER = 1<<20;
256        const INDIRECT_COMMAND_BUFFER = 1<<21;
257        const RENDER_PIPELINE_STATE = 1<<22;
258        // Render target types
259        /// A color attachment in a renderpass
260        const RENDER_TARGET_COLOR = 1<<23;
261        /// A depth/stencil attachment in a renderpass
262        const RENDER_TARGET_DEPTH_STENCIL = 1<<24;
263    }
264}
265
266impl RafxResourceType {
267    pub fn is_uniform_buffer(self) -> bool {
268        self.intersects(RafxResourceType::UNIFORM_BUFFER)
269    }
270
271    pub fn is_storage_buffer(self) -> bool {
272        self.intersects(RafxResourceType::BUFFER | RafxResourceType::BUFFER_READ_WRITE)
273    }
274
275    pub fn is_render_target(self) -> bool {
276        self.intersects(
277            RafxResourceType::RENDER_TARGET_COLOR | RafxResourceType::RENDER_TARGET_DEPTH_STENCIL,
278        )
279    }
280
281    pub fn is_texture(self) -> bool {
282        self.intersects(RafxResourceType::TEXTURE | RafxResourceType::TEXTURE_READ_WRITE)
283    }
284}
285
286bitflags::bitflags! {
287    /// Flags for enabling/disabling color channels, used with `RafxBlendState`
288    #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
289    pub struct RafxColorFlags: u8 {
290        const RED = 1;
291        const GREEN = 2;
292        const BLUE = 4;
293        const ALPHA = 8;
294        const ALL = 0x0F;
295    }
296}
297
298impl Default for RafxColorFlags {
299    fn default() -> Self {
300        RafxColorFlags::ALL
301    }
302}
303
304/// Indicates how the memory will be accessed and affects where in memory it needs to be allocated.
305#[derive(Clone, Copy, PartialEq, Debug)]
306pub enum RafxMemoryUsage {
307    Unknown,
308
309    /// The memory is only accessed by the GPU
310    GpuOnly,
311
312    /// The memory is only accessed by the CPU
313    CpuOnly,
314
315    /// The memory is written by the CPU and read by the GPU
316    CpuToGpu,
317
318    /// The memory is written by the GPU and read by the CPU
319    GpuToCpu,
320}
321
322/// Indicates the result of presenting a swapchain image
323#[derive(Clone, Copy, PartialEq, Debug)]
324pub enum RafxPresentSuccessResult {
325    /// The image was shown and the swapchain can continue to be used.
326    Success,
327
328    /// The image was shown and the swapchain can continue to be used. However, this result also
329    /// hints that there is a more optimal configuration for the swapchain to be in. This is vague
330    /// because the precise meaning varies between platform. For example, windows may return this
331    /// when the application is minimized.
332    SuccessSuboptimal,
333
334    // While this is an "error" being returned as success, it is expected and recoverable while
335    // other errors usually aren't. This way the ? operator can still be used to bail out the
336    // unrecoverable errors and the different flavors of "success" should be explicitly handled
337    // in a match
338    /// Indicates that the swapchain can no longer be used
339    DeviceReset,
340}
341
342/// Indicates the current state of a fence.
343#[derive(Clone, Copy, PartialEq, Debug)]
344pub enum RafxFenceStatus {
345    /// The fence was submitted to the command buffer and signaled as completed by the GPU
346    Complete,
347    /// The fence will be signaled as complete later by the GPU
348    Incomplete,
349    /// The fence was never submitted, or was submitted and already returned complete once, putting
350    /// it back into the unsubmitted state
351    Unsubmitted,
352}
353
354bitflags::bitflags! {
355    /// Indicates what render targets are affected by a blend state
356    #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
357    pub struct RafxBlendStateTargets : u8 {
358        const BLEND_STATE_TARGET_0 = 0x01;
359        const BLEND_STATE_TARGET_1 = 0x02;
360        const BLEND_STATE_TARGET_2 = 0x04;
361        const BLEND_STATE_TARGET_3 = 0x08;
362        const BLEND_STATE_TARGET_4 = 0x10;
363        const BLEND_STATE_TARGET_5 = 0x20;
364        const BLEND_STATE_TARGET_6 = 0x40;
365        const BLEND_STATE_TARGET_7 = 0x80;
366        const BLEND_STATE_TARGET_ALL = 0xFF;
367    }
368}
369
370bitflags::bitflags! {
371    /// Indicates a particular stage of a shader, or set of stages in a shader. Similar to
372    /// VkShaderStageFlagBits
373    #[derive(Default)]
374    #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
375    pub struct RafxShaderStageFlags : u32 {
376        const NONE = 0;
377        const VERTEX = 1;
378        const TESSELLATION_CONTROL = 2;
379        const TESSELLATION_EVALUATION = 4;
380        const GEOMETRY = 8;
381        const FRAGMENT = 16;
382        const COMPUTE = 32;
383        const ALL_GRAPHICS = 0x1F;
384        const ALL = 0x7FFF_FFFF;
385    }
386}
387
388/// Contains all the individual stages
389pub const ALL_SHADER_STAGE_FLAGS: [RafxShaderStageFlags; 6] = [
390    RafxShaderStageFlags::VERTEX,
391    RafxShaderStageFlags::TESSELLATION_CONTROL, // dx12 hull shader
392    RafxShaderStageFlags::TESSELLATION_EVALUATION, // dx12 domain shader
393    RafxShaderStageFlags::GEOMETRY,
394    RafxShaderStageFlags::FRAGMENT,
395    RafxShaderStageFlags::COMPUTE,
396];
397
398/// Indicates the type of pipeline, roughly corresponds with RafxQueueType
399#[derive(Clone, Copy, PartialEq, Debug)]
400pub enum RafxPipelineType {
401    Graphics = 0,
402    Compute = 1,
403}
404
405/// Affects how quickly vertex attributes are consumed from buffers, similar to VkVertexInputRate
406#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
407pub enum RafxVertexAttributeRate {
408    Vertex,
409    Instance,
410}
411
412impl Default for RafxVertexAttributeRate {
413    fn default() -> Self {
414        RafxVertexAttributeRate::Vertex
415    }
416}
417
418/// Determines if the contents of an image attachment in a renderpass begins with its previous
419/// contents, a clear value, or undefined data. Similar to VkAttachmentLoadOp
420#[derive(Copy, Clone, Debug, Hash, PartialEq)]
421pub enum RafxLoadOp {
422    DontCare,
423    Load,
424    Clear,
425}
426
427impl Default for RafxLoadOp {
428    fn default() -> Self {
429        RafxLoadOp::DontCare
430    }
431}
432
433/// Determines if the contents of an image attachment in a rander pass will store the resulting
434/// state for use after the render pass
435#[derive(Copy, Clone, Debug, Hash, PartialEq)]
436pub enum RafxStoreOp {
437    /// Do not store the image, leaving the contents of it undefined
438    DontCare,
439
440    /// Persist the image's content after a render pass completes
441    Store,
442}
443
444impl Default for RafxStoreOp {
445    fn default() -> Self {
446        RafxStoreOp::Store
447    }
448}
449
450/// How to intepret vertex data into a form of geometry. Similar to VkPrimitiveTopology
451#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
452#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
453pub enum RafxPrimitiveTopology {
454    PointList,
455    LineList,
456    LineStrip,
457    TriangleList,
458    TriangleStrip,
459    PatchList,
460}
461
462/// The size of index buffer elements
463#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
464#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
465pub enum RafxIndexType {
466    Uint32,
467    Uint16,
468}
469
470impl Default for RafxIndexType {
471    fn default() -> Self {
472        RafxIndexType::Uint32
473    }
474}
475
476impl RafxIndexType {
477    pub fn size_in_bytes(self) -> usize {
478        match self {
479            RafxIndexType::Uint32 => 4,
480            RafxIndexType::Uint16 => 2,
481        }
482    }
483}
484
485/// Affects blending. Similar to VkBlendFactor
486#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
487#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
488pub enum RafxBlendFactor {
489    Zero,
490    One,
491    SrcColor,
492    OneMinusSrcColor,
493    DstColor,
494    OneMinusDstColor,
495    SrcAlpha,
496    OneMinusSrcAlpha,
497    DstAlpha,
498    OneMinusDstAlpha,
499    SrcAlphaSaturate,
500    ConstantColor,
501    OneMinusConstantColor,
502}
503
504impl Default for RafxBlendFactor {
505    fn default() -> Self {
506        RafxBlendFactor::Zero
507    }
508}
509
510/// Affects blending. Similar to VkBlendOp
511#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
512#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
513pub enum RafxBlendOp {
514    Add,
515    Subtract,
516    ReverseSubtract,
517    Min,
518    Max,
519}
520
521impl Default for RafxBlendOp {
522    fn default() -> Self {
523        RafxBlendOp::Add
524    }
525}
526
527/// Affects depth testing and sampling. Similar to VkCompareOp
528#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
529#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
530pub enum RafxCompareOp {
531    Never,
532    Less,
533    Equal,
534    LessOrEqual,
535    Greater,
536    NotEqual,
537    GreaterOrEqual,
538    Always,
539}
540
541impl Default for RafxCompareOp {
542    fn default() -> Self {
543        RafxCompareOp::Never
544    }
545}
546
547/// Similar to VkStencilOp
548#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
549#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
550pub enum RafxStencilOp {
551    Keep,
552    Zero,
553    Replace,
554    IncrementAndClamp,
555    DecrementAndClamp,
556    Invert,
557    IncrementAndWrap,
558    DecrementAndWrap,
559}
560
561impl Default for RafxStencilOp {
562    fn default() -> Self {
563        RafxStencilOp::Keep
564    }
565}
566
567/// Determines if we cull polygons that are front-facing or back-facing. Facing direction is
568/// determined by RafxFrontFace, sometimes called "winding order". Similar to VkCullModeFlags
569#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
570#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
571pub enum RafxCullMode {
572    None,
573    Back,
574    Front,
575}
576
577impl Default for RafxCullMode {
578    fn default() -> Self {
579        RafxCullMode::None
580    }
581}
582
583/// Determines what winding order is considerered the front face of a polygon. Similar to
584/// VkFrontFace
585#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
586#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
587pub enum RafxFrontFace {
588    CounterClockwise,
589    Clockwise,
590}
591
592impl Default for RafxFrontFace {
593    fn default() -> Self {
594        RafxFrontFace::CounterClockwise
595    }
596}
597
598/// Whether to fill in polygons or not. Similar to VkPolygonMode
599#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
600#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
601pub enum RafxFillMode {
602    Solid,
603    Wireframe,
604}
605
606impl Default for RafxFillMode {
607    fn default() -> Self {
608        RafxFillMode::Solid
609    }
610}
611
612/// Filtering method when sampling. Similar to VkFilter
613#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
614#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
615pub enum RafxFilterType {
616    /// Finds the closest value in the texture and uses it. Commonly used for "pixel-perfect"
617    /// assets.
618    Nearest,
619
620    /// "Averages" color values of the texture. A common choice for most cases but may make some
621    /// "pixel-perfect" assets appear blurry
622    Linear,
623}
624
625impl Default for RafxFilterType {
626    fn default() -> Self {
627        RafxFilterType::Nearest
628    }
629}
630
631/// Affects image sampling, particularly for UV coordinates outside the [0, 1] range. Similar to
632/// VkSamplerAddressMode
633#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
634#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
635pub enum RafxAddressMode {
636    Mirror,
637    Repeat,
638    ClampToEdge,
639    ClampToBorder,
640}
641
642impl Default for RafxAddressMode {
643    fn default() -> Self {
644        RafxAddressMode::Mirror
645    }
646}
647
648/// Similar to VkSamplerMipmapMode
649#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
650#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
651pub enum RafxMipMapMode {
652    Nearest,
653    Linear,
654}
655
656impl Default for RafxMipMapMode {
657    fn default() -> Self {
658        RafxMipMapMode::Nearest
659    }
660}
661
662/// A clear value for color attachments
663#[derive(Copy, Clone, Debug, Default)]
664pub struct RafxColorClearValue(pub [f32; 4]);
665
666impl Hash for RafxColorClearValue {
667    fn hash<H: Hasher>(
668        &self,
669        mut state: &mut H,
670    ) {
671        for &value in &self.0 {
672            DecimalF32(value).hash(&mut state);
673        }
674    }
675}
676
677/// A clear values for depth/stencil attachments. One or both values may be used depending on the
678/// format of the attached image
679#[derive(Clone, Copy, Debug)]
680pub struct RafxDepthStencilClearValue {
681    pub depth: f32,
682    pub stencil: u32,
683}
684
685impl Default for RafxDepthStencilClearValue {
686    fn default() -> Self {
687        RafxDepthStencilClearValue {
688            depth: 0.0,
689            stencil: 0,
690        }
691    }
692}
693
694impl Hash for RafxDepthStencilClearValue {
695    fn hash<H: Hasher>(
696        &self,
697        mut state: &mut H,
698    ) {
699        DecimalF32(self.depth).hash(&mut state);
700        self.stencil.hash(&mut state);
701    }
702}
703
704/// Determines if a barrier is transferring a resource from one queue to another.
705#[derive(Debug, Copy, Clone)]
706pub enum RafxBarrierQueueTransition {
707    /// No queue transition will take place
708    None,
709
710    /// A barrier for the "sending" queue. Contains the "receiving" queue. (the "sending" queue is
711    /// inferred by the queue on which the barrier is submitted)
712    ReleaseTo(RafxQueueType),
713
714    /// A barrier for the "receiving" queue. Contains the "sending" queue. (the "receiving" queue is
715    /// inferred by the queue on which the barrier is submitted)
716    AcquireFrom(RafxQueueType),
717}
718
719impl Default for RafxBarrierQueueTransition {
720    fn default() -> Self {
721        RafxBarrierQueueTransition::None
722    }
723}
724
725/// A memory barrier for buffers. This is used to transition buffers between resource states and
726/// possibly from one queue to another
727pub struct RafxBufferBarrier<'a> {
728    pub buffer: &'a RafxBuffer,
729    pub src_state: RafxResourceState,
730    pub dst_state: RafxResourceState,
731    pub queue_transition: RafxBarrierQueueTransition,
732    pub offset_size: Option<RafxOffsetSize>,
733}
734
735/// A memory barrier for textures. This is used to transition textures between resource states and
736/// possibly from one queue to another.
737pub struct RafxTextureBarrier<'a> {
738    pub texture: &'a RafxTexture,
739    pub src_state: RafxResourceState,
740    pub dst_state: RafxResourceState,
741    pub queue_transition: RafxBarrierQueueTransition,
742    /// If set, only the specified array element is included
743    pub array_slice: Option<u16>,
744    /// If set, only the specified mip level is included
745    pub mip_slice: Option<u8>,
746}
747
748impl<'a> RafxTextureBarrier<'a> {
749    /// Creates a simple state transition
750    pub fn state_transition(
751        texture: &'a RafxTexture,
752        src_state: RafxResourceState,
753        dst_state: RafxResourceState,
754    ) -> RafxTextureBarrier {
755        RafxTextureBarrier {
756            texture,
757            src_state,
758            dst_state,
759            queue_transition: RafxBarrierQueueTransition::None,
760            array_slice: None,
761            mip_slice: None,
762        }
763    }
764}
765
766// Recommended format/color space options for desktop vulkan are:
767// HDR: R16G16B16A16_SFLOAT/EXTENDED_SRGB_LINEAR_EXT (windows only)
768// Non-HDR: B8G8R8A8_SRGB/SRGB_NONLINEAR_KHR
769//
770// Both of these work well on apple/metal
771#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
772#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
773pub enum RafxSwapchainColorSpace {
774    Srgb,
775    SrgbExtended,
776
777    // Only supported on apple/metal, SrgbExtended is recommended for all HDR, including
778    // on apple devices.
779    DisplayP3Extended,
780}
781
782impl RafxSwapchainColorSpace {
783    pub fn is_extended(self) -> bool {
784        match self {
785            RafxSwapchainColorSpace::Srgb => false,
786            RafxSwapchainColorSpace::SrgbExtended => true,
787            RafxSwapchainColorSpace::DisplayP3Extended => true,
788        }
789    }
790
791    pub fn is_srgb(self) -> bool {
792        match self {
793            RafxSwapchainColorSpace::Srgb => true,
794            RafxSwapchainColorSpace::SrgbExtended => true,
795            RafxSwapchainColorSpace::DisplayP3Extended => false,
796        }
797    }
798}
799
800impl Default for RafxSwapchainColorSpace {
801    fn default() -> Self {
802        RafxSwapchainColorSpace::Srgb
803    }
804}
805
806/// Represents an image owned by the swapchain
807#[derive(Clone)]
808pub struct RafxSwapchainImage {
809    pub texture: RafxTexture,
810    pub swapchain_image_index: u32,
811}
812
813/// A color render target bound during a renderpass
814#[derive(Debug)]
815pub struct RafxColorRenderTargetBinding<'a> {
816    pub texture: &'a RafxTexture,
817    pub load_op: RafxLoadOp,
818    pub store_op: RafxStoreOp,
819    pub mip_slice: Option<u8>,
820    pub array_slice: Option<u16>,
821    pub clear_value: RafxColorClearValue,
822    pub resolve_target: Option<&'a RafxTexture>,
823    pub resolve_store_op: RafxStoreOp,
824    pub resolve_mip_slice: Option<u8>,
825    pub resolve_array_slice: Option<u16>,
826}
827
828/// A depth/stencil render target to be bound during a renderpass
829#[derive(Debug)]
830pub struct RafxDepthStencilRenderTargetBinding<'a> {
831    pub texture: &'a RafxTexture,
832    pub depth_load_op: RafxLoadOp,
833    pub stencil_load_op: RafxLoadOp,
834    pub depth_store_op: RafxStoreOp,
835    pub stencil_store_op: RafxStoreOp,
836    pub mip_slice: Option<u8>,
837    pub array_slice: Option<u16>,
838    pub clear_value: RafxDepthStencilClearValue,
839}
840
841/// A vertex buffer to be bound during a renderpass
842pub struct RafxVertexBufferBinding<'a> {
843    pub buffer: &'a RafxBuffer,
844    pub byte_offset: u64,
845}
846
847/// An index buffer to be bound during a renderpass
848pub struct RafxIndexBufferBinding<'a> {
849    pub buffer: &'a RafxBuffer,
850    pub byte_offset: u64,
851    pub index_type: RafxIndexType,
852}
853
854/// Parameters for copying a buffer to a texture
855#[derive(Default, Clone)]
856pub struct RafxCmdCopyBufferToBufferParams {
857    pub src_byte_offset: u64,
858    pub dst_byte_offset: u64,
859    pub size: u64,
860}
861
862impl RafxCmdCopyBufferToBufferParams {
863    pub fn full_copy(
864        src_def: &RafxBufferDef,
865        dst_def: &RafxBufferDef,
866    ) -> Self {
867        assert_eq!(src_def.size, dst_def.size);
868        RafxCmdCopyBufferToBufferParams {
869            src_byte_offset: 0,
870            dst_byte_offset: 0,
871            size: src_def.size,
872        }
873    }
874}
875
876/// Parameters for copying a buffer to a texture
877#[derive(Default, Clone)]
878pub struct RafxCmdCopyBufferToTextureParams {
879    pub buffer_offset: u64,
880    pub array_layer: u16,
881    pub mip_level: u8,
882}
883
884#[derive(Default, Clone)]
885pub struct RafxCmdCopyTextureToTextureParams {
886    pub src_offset: RafxExtents3D,
887    pub dst_offset: RafxExtents3D,
888    pub extents: RafxExtents3D,
889    pub src_mip_level: u8,
890    pub dst_mip_level: u8,
891    // If none, operate on all image slices (we assume images have same number of slices)
892    pub array_slices: Option<[u16; 2]>,
893}
894
895/// Parameters for blitting one image to another (vulkan backend only)
896#[derive(Clone)]
897pub struct RafxCmdBlitParams {
898    pub src_state: RafxResourceState,
899    pub dst_state: RafxResourceState,
900    pub src_extents: [RafxExtents3D; 2],
901    pub dst_extents: [RafxExtents3D; 2],
902    pub src_mip_level: u8,
903    pub dst_mip_level: u8,
904    pub array_slices: Option<[u16; 2]>,
905}
906
907/// A rafx-specific index that refers to a particular binding. Instead of doing name/binding lookups
908/// every frame, query the descriptor index during startup and use it instead. This is a more
909/// efficient way to address descriptors.
910#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
911pub struct RafxDescriptorIndex(pub(crate) u32);
912
913/// Selects a particular descriptor in a descriptor set
914#[derive(Clone, PartialEq, Eq, Hash, Debug)]
915pub enum RafxDescriptorKey<'a> {
916    Undefined,
917    Name(&'a str),
918    Binding(u32),
919    DescriptorIndex(RafxDescriptorIndex),
920}
921
922impl<'a> Default for RafxDescriptorKey<'a> {
923    fn default() -> Self {
924        RafxDescriptorKey::Undefined
925    }
926}
927
928/// Used in various APIs where we supply an offset/size pair
929#[derive(Default, Clone, Copy, Debug)]
930pub struct RafxOffsetSize {
931    pub byte_offset: u64,
932    pub size: u64,
933}
934
935/// Specifies what value to assign to a descriptor set
936#[derive(Default, Debug)]
937pub struct RafxDescriptorElements<'a> {
938    pub textures: Option<&'a [&'a RafxTexture]>,
939    pub samplers: Option<&'a [&'a RafxSampler]>,
940    pub buffers: Option<&'a [&'a RafxBuffer]>,
941    pub buffer_offset_sizes: Option<&'a [RafxOffsetSize]>,
942}
943
944/// Used when binding a texture to select between different ways to bind the texture
945#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
946pub enum RafxTextureBindType {
947    // Color or depth only
948    Srv,
949    // stencil?
950    SrvStencil,
951    // Bind all mip levels of the 0th provided texture
952    UavMipChain,
953    // Bind a particular mip slice of all provided textures
954    UavMipSlice(u32),
955}
956
957/// Describes how to update a single descriptor
958#[derive(Debug)]
959pub struct RafxDescriptorUpdate<'a> {
960    pub array_index: u32,
961    pub descriptor_key: RafxDescriptorKey<'a>,
962    pub elements: RafxDescriptorElements<'a>,
963    pub dst_element_offset: u32,
964    // Srv when read-only, UavMipSlice(0) when read-write
965    pub texture_bind_type: Option<RafxTextureBindType>,
966}
967
968impl<'a> Default for RafxDescriptorUpdate<'a> {
969    fn default() -> Self {
970        RafxDescriptorUpdate {
971            array_index: 0,
972            descriptor_key: RafxDescriptorKey::Undefined,
973            elements: RafxDescriptorElements::default(),
974            dst_element_offset: 0,
975            texture_bind_type: None,
976        }
977    }
978}
979
980// Corresponds 1:1 with VkDrawIndirectCommand, MTLDrawPrimitivesIndirectArguments, D3D12_DRAW_ARGUMENTS
981pub struct RafxDrawIndirectCommand {
982    pub vertex_count: u32,
983    pub instance_count: u32,
984    pub first_vertex: u32,
985    pub first_instance: u32,
986}
987
988// Corresponds 1:1 with VkDrawIndexedIndirectCommand, MTLDrawIndexedPrimitivesIndirectArguments, D3D12_DRAW_INDEXED_ARGUMENTS
989pub struct RafxDrawIndexedIndirectCommand {
990    pub index_count: u32,
991    pub instance_count: u32,
992    pub first_index: u32,
993    pub vertex_offset: i32, // value added to the vertex index before indexing into the vertex buffer
994    pub first_instance: u32,
995}
996
997// Corresponds 1:1 with VkDispatchIndirectCommand, MTLDispatchThreadgroupsIndirectArguments, D3D12_DISPATCH_ARGUMENTS
998pub struct RafxDispatchIndirectCommand {
999    pub group_count_x: u32,
1000    pub group_count_y: u32,
1001    pub group_count_z: u32,
1002}