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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! GPU render state.
//!
//! Such a state controls how the GPU must operate some fixed pipeline functionality, such as the
//! blending, depth test or face culling operations.

use crate::{
  blending::{Blending, BlendingMode},
  depth_stencil::{Comparison, StencilOperations, StencilTest, Write},
  face_culling::FaceCulling,
  scissor::ScissorRegion,
};

/// GPU render state.
///
/// You can get a default value with `RenderState::default` and set the operations you want with the
/// various `RenderState::set_*` methods.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RenderState {
  /// Blending configuration.
  blending: Option<BlendingMode>,
  /// Depth test configuration.
  depth_test: Option<Comparison>,
  /// Depth write configuration.
  depth_write: Write,
  /// Stencil test configuration.
  stencil_test: Option<StencilTest>,
  /// Stencil operations.
  stencil_operations: StencilOperations,
  /// Face culling configuration.
  face_culling: Option<FaceCulling>,
  /// Scissor region configuration.
  scissor: Option<ScissorRegion>,
}

impl RenderState {
  /// Override the blending configuration.
  pub fn set_blending<B>(self, blending: B) -> Self
  where
    B: Into<Option<Blending>>,
  {
    RenderState {
      blending: blending.into().map(|x| x.into()),
      ..self
    }
  }

  /// Override the blending configuration using separate blending.
  pub fn set_blending_separate(self, blending_rgb: Blending, blending_alpha: Blending) -> Self {
    RenderState {
      blending: Some(BlendingMode::Separate {
        rgb: blending_rgb,
        alpha: blending_alpha,
      }),
      ..self
    }
  }

  /// Blending configuration.
  pub fn blending(&self) -> Option<BlendingMode> {
    self.blending
  }

  /// Override the depth test configuration.
  pub fn set_depth_test<D>(self, depth_test: D) -> Self
  where
    D: Into<Option<Comparison>>,
  {
    let depth_test = depth_test.into();
    RenderState { depth_test, ..self }
  }

  /// Depth test configuration.
  pub fn depth_test(&self) -> Option<Comparison> {
    self.depth_test
  }

  /// Override the depth write configuration.
  pub fn set_depth_write(self, depth_write: Write) -> Self {
    RenderState {
      depth_write,
      ..self
    }
  }

  /// Depth write configuration.
  pub fn depth_write(&self) -> Write {
    self.depth_write
  }

  /// Override the stencil test configuration.
  pub fn set_stencil_test(self, stencil_test: impl Into<Option<StencilTest>>) -> Self {
    let stencil_test = stencil_test.into();

    RenderState {
      stencil_test,
      ..self
    }
  }

  /// Stencil test configuration.
  pub fn stencil_test(&self) -> Option<&StencilTest> {
    self.stencil_test.as_ref()
  }

  /// Override the stencil operations.
  pub fn set_stencil_operations(self, stencil_operations: StencilOperations) -> Self {
    RenderState {
      stencil_operations,
      ..self
    }
  }

  /// Stencil test operations.
  pub fn stencil_operations(&self) -> &StencilOperations {
    &self.stencil_operations
  }

  /// Override the face culling configuration.
  pub fn set_face_culling<FC>(self, face_culling: FC) -> Self
  where
    FC: Into<Option<FaceCulling>>,
  {
    RenderState {
      face_culling: face_culling.into(),
      ..self
    }
  }

  /// Face culling configuration.
  pub fn face_culling(&self) -> Option<FaceCulling> {
    self.face_culling
  }

  /// Override the scissor configuration.
  pub fn set_scissor<SR>(self, scissor: SR) -> Self
  where
    SR: Into<Option<ScissorRegion>>,
  {
    RenderState {
      scissor: scissor.into(),
      ..self
    }
  }

  /// Get the scissor configuration.
  pub fn scissor(&self) -> &Option<ScissorRegion> {
    &self.scissor
  }
}

impl Default for RenderState {
  /// The default `RenderState`.
  ///
  ///   - `blending`: `None`
  ///   - `depth_test`: `Some(Comparison::Less)`
  ///   - `depth_write`: `Write::On`
  ///   - `stencil_test`: `None`
  ///   - `stencil_operations`: `StencilOperations::default()`
  ///   - `face_culling`: `None`
  ///   - 'scissor_region`: `None`
  fn default() -> Self {
    RenderState {
      blending: None,
      depth_test: Some(Comparison::Less),
      depth_write: Write::On,
      stencil_test: None,
      stencil_operations: StencilOperations::default(),
      face_culling: None,
      scissor: None,
    }
  }
}