1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#[cfg(any(
    feature = "rafx-empty",
    not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
))]
use crate::empty::RafxCommandPoolEmpty;
#[cfg(feature = "rafx-metal")]
use crate::metal::RafxCommandPoolMetal;
#[cfg(feature = "rafx-vulkan")]
use crate::vulkan::RafxCommandPoolVulkan;
use crate::{RafxCommandBuffer, RafxCommandBufferDef, RafxDeviceContext, RafxResult};

/// A pool of command buffers. A command pool is necessary to create a command buffer.
///
/// A command pool cannot be modified (including allocating from it) if one of its command buffers
/// is being modified or in-use by the GPU.
///
/// Resetting a command pool clears all of the command buffers allocated from it, but the command
/// buffers remain allocated.
///
/// The command pool must not be dropped while any of its command buffers are in use. However, it
/// is ok to drop a command pool while command buffers are allocated, as long as those command
/// buffers are never used again. (The command pool owns the memory the command buffer points to)
pub enum RafxCommandPool {
    #[cfg(feature = "rafx-vulkan")]
    Vk(RafxCommandPoolVulkan),
    #[cfg(feature = "rafx-metal")]
    Metal(RafxCommandPoolMetal),
    #[cfg(any(
        feature = "rafx-empty",
        not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
    ))]
    Empty(RafxCommandPoolEmpty),
}

impl RafxCommandPool {
    pub fn device_context(&self) -> RafxDeviceContext {
        match self {
            #[cfg(feature = "rafx-vulkan")]
            RafxCommandPool::Vk(inner) => RafxDeviceContext::Vk(inner.device_context().clone()),
            #[cfg(feature = "rafx-metal")]
            RafxCommandPool::Metal(inner) => {
                RafxDeviceContext::Metal(inner.device_context().clone())
            }
            #[cfg(any(
                feature = "rafx-empty",
                not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
            ))]
            RafxCommandPool::Empty(inner) => {
                RafxDeviceContext::Empty(inner.device_context().clone())
            }
        }
    }

    /// Allocate a command buffer from the pool. This must not be called if a command buffer from
    /// this pool is being written or is in-use by the GPU.
    pub fn create_command_buffer(
        &mut self,
        command_buffer_def: &RafxCommandBufferDef,
    ) -> RafxResult<RafxCommandBuffer> {
        Ok(match self {
            #[cfg(feature = "rafx-vulkan")]
            RafxCommandPool::Vk(inner) => {
                RafxCommandBuffer::Vk(inner.create_command_buffer(command_buffer_def)?)
            }
            #[cfg(feature = "rafx-metal")]
            RafxCommandPool::Metal(inner) => {
                RafxCommandBuffer::Metal(inner.create_command_buffer(command_buffer_def)?)
            }
            #[cfg(any(
                feature = "rafx-empty",
                not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
            ))]
            RafxCommandPool::Empty(inner) => {
                RafxCommandBuffer::Empty(inner.create_command_buffer(command_buffer_def)?)
            }
        })
    }

    /// Reset all command buffers to an "unwritten" state. This must not be called if any command
    /// buffers allocated from this pool are in use by the GPU.
    ///
    /// This does not "free" allocated command buffers for reallocation.
    pub fn reset_command_pool(&mut self) -> RafxResult<()> {
        match self {
            #[cfg(feature = "rafx-vulkan")]
            RafxCommandPool::Vk(inner) => inner.reset_command_pool(),
            // metal does not have the concept of command buffer pools in the API
            #[cfg(feature = "rafx-metal")]
            RafxCommandPool::Metal(inner) => inner.reset_command_pool(),
            #[cfg(any(
                feature = "rafx-empty",
                not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
            ))]
            RafxCommandPool::Empty(inner) => inner.reset_command_pool(),
        }
    }

    /// Get the underlying vulkan API object. This provides access to any internally created
    /// vulkan objects.
    #[cfg(feature = "rafx-vulkan")]
    pub fn vk_command_pool(&self) -> Option<&RafxCommandPoolVulkan> {
        match self {
            #[cfg(feature = "rafx-vulkan")]
            RafxCommandPool::Vk(inner) => Some(inner),
            #[cfg(feature = "rafx-metal")]
            RafxCommandPool::Metal(_inner) => None,
            #[cfg(any(
                feature = "rafx-empty",
                not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
            ))]
            RafxCommandPool::Empty(_inner) => None,
        }
    }

    /// Get the underlying metal API object. This provides access to any internally created
    /// metal objects.
    #[cfg(feature = "rafx-metal")]
    pub fn metal_command_pool(&self) -> Option<&RafxCommandPoolMetal> {
        match self {
            #[cfg(feature = "rafx-vulkan")]
            RafxCommandPool::Vk(_inner) => None,
            #[cfg(feature = "rafx-metal")]
            RafxCommandPool::Metal(inner) => Some(inner),
            #[cfg(any(
                feature = "rafx-empty",
                not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
            ))]
            RafxCommandPool::Empty(_inner) => None,
        }
    }

    #[cfg(any(
        feature = "rafx-empty",
        not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
    ))]
    pub fn empty_command_pool(&self) -> Option<&RafxCommandPoolEmpty> {
        match self {
            #[cfg(feature = "rafx-vulkan")]
            RafxCommandPool::Vk(_inner) => None,
            #[cfg(feature = "rafx-metal")]
            RafxCommandPool::Metal(_inner) => None,
            #[cfg(any(
                feature = "rafx-empty",
                not(any(feature = "rafx-metal", feature = "rafx-vulkan"))
            ))]
            RafxCommandPool::Empty(inner) => Some(inner),
        }
    }
}