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
//! Renderer error types.

use std::error::Error as StdError;
use std::fmt::Result as FmtResult;
use std::fmt::{Display, Formatter};
use std::result::Result as StdResult;

use amethyst_core;
use gfx;
use gfx_core;

/// Renderer result type.
pub type Result<T> = StdResult<T, Error>;

/// Common renderer error type.
#[derive(Debug)]
pub enum Error {
    /// Failed to create a buffer.
    BufferCreation(gfx::buffer::CreationError),
    /// A render target with the given name does not exist.
    NoSuchTarget(String),
    /// Failed to initialize a render pass.
    PassInit(gfx::PipelineStateError<String>),
    /// Failed to create a pipeline state object (PSO).
    PipelineCreation(gfx_core::pso::CreationError),
    /// Failed to create thread pool.
    PoolCreation(String),
    /// Failed to create and link a shader program.
    ProgramCreation(gfx::shade::ProgramError),
    /// Failed to create a resource view.
    ResViewCreation(gfx::ResourceViewError),
    /// Failed to interact with the ECS.
    SpecsError(amethyst_core::specs::error::Error),
    /// Failed to create a render target.
    TargetCreation(gfx::CombinedError),
    /// Failed to create a texture resource.
    TextureCreation(gfx::texture::CreationError),
    /// The window handle associated with the renderer has been destroyed.
    WindowDestroyed,
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::BufferCreation(_) => "Failed to create buffer!",
            Error::NoSuchTarget(_) => "Target with this name does not exist!",
            Error::PassInit(_) => "Failed to initialize render pass!",
            Error::PipelineCreation(_) => "Failed to create PSO!",
            Error::PoolCreation(_) => "Failed to create thread pool!",
            Error::ProgramCreation(_) => "Failed to create shader program!",
            Error::ResViewCreation(_) => "Failed to create resource view!",
            Error::SpecsError(_) => "Failed to interact with the ECS!",
            Error::TargetCreation(_) => "Failed to create render target!",
            Error::TextureCreation(_) => "Failed to create texture!",
            Error::WindowDestroyed => "Window has been destroyed!",
        }
    }

    fn cause(&self) -> Option<&StdError> {
        match *self {
            Error::BufferCreation(ref e) => Some(e),
            Error::PassInit(ref e) => Some(e),
            Error::PipelineCreation(ref e) => Some(e),
            Error::ProgramCreation(ref e) => Some(e),
            Error::ResViewCreation(ref e) => Some(e),
            Error::SpecsError(ref e) => Some(e),
            Error::TargetCreation(ref e) => Some(e),
            Error::TextureCreation(ref e) => Some(e),
            _ => None,
        }
    }
}

impl Display for Error {
    fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
        match *self {
            Error::BufferCreation(ref e) => write!(fmt, "Buffer creation failed: {}", e),
            Error::NoSuchTarget(ref e) => write!(fmt, "Nonexistent target: {}", e),
            Error::PassInit(ref e) => write!(fmt, "Pass initialization failed: {}", e),
            Error::PipelineCreation(ref e) => write!(fmt, "PSO creation failed: {}", e),
            Error::PoolCreation(ref e) => write!(fmt, "Thread pool creation failed: {}", e),
            Error::ProgramCreation(ref e) => write!(fmt, "Program compilation failed: {}", e),
            Error::ResViewCreation(ref e) => write!(fmt, "Resource view creation failed: {}", e),
            Error::SpecsError(ref e) => write!(fmt, "Interaction with ECS failed: {}", e),
            Error::TargetCreation(ref e) => write!(fmt, "Target creation failed: {}", e),
            Error::TextureCreation(ref e) => write!(fmt, "Texture creation failed: {}", e),
            Error::WindowDestroyed => write!(fmt, "Window has been destroyed"),
        }
    }
}

impl From<gfx::CombinedError> for Error {
    fn from(e: gfx::CombinedError) -> Error {
        Error::TargetCreation(e)
    }
}

impl From<gfx::PipelineStateError<String>> for Error {
    fn from(e: gfx::PipelineStateError<String>) -> Error {
        Error::PassInit(e)
    }
}

impl From<gfx::ResourceViewError> for Error {
    fn from(e: gfx::ResourceViewError) -> Error {
        Error::ResViewCreation(e)
    }
}

impl From<gfx::buffer::CreationError> for Error {
    fn from(e: gfx::buffer::CreationError) -> Error {
        Error::BufferCreation(e)
    }
}

impl From<gfx::shade::ProgramError> for Error {
    fn from(e: gfx::shade::ProgramError) -> Error {
        Error::ProgramCreation(e)
    }
}

impl From<gfx::texture::CreationError> for Error {
    fn from(e: gfx::texture::CreationError) -> Error {
        Error::TextureCreation(e)
    }
}

impl From<gfx_core::pso::CreationError> for Error {
    fn from(e: gfx_core::pso::CreationError) -> Error {
        Error::PipelineCreation(e)
    }
}

impl From<amethyst_core::specs::error::Error> for Error {
    fn from(e: amethyst_core::specs::error::Error) -> Error {
        Error::SpecsError(e)
    }
}