fyrox_graphics/lib.rs
1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#![allow(clippy::too_many_arguments)]
22
23pub use fyrox_core as core;
24use std::fmt::Debug;
25
26use crate::core::{reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*};
27use serde::{Deserialize, Serialize};
28use strum_macros::{AsRefStr, EnumString, VariantNames};
29
30pub mod buffer;
31pub mod error;
32pub mod framebuffer;
33pub mod geometry_buffer;
34pub mod gl;
35pub mod gpu_program;
36pub mod gpu_texture;
37pub mod query;
38pub mod read_buffer;
39pub mod server;
40pub mod stats;
41pub mod uniform;
42
43/// A set of possible polygon filling modes.
44#[derive(
45 Copy,
46 Clone,
47 PartialOrd,
48 PartialEq,
49 Hash,
50 Debug,
51 Deserialize,
52 Visit,
53 Eq,
54 Reflect,
55 AsRefStr,
56 EnumString,
57 VariantNames,
58 TypeUuidProvider,
59 Default,
60)]
61#[type_uuid(id = "47aff01a-7daa-427c-874c-87464a7ffe28")]
62pub enum PolygonFillMode {
63 /// Only vertices of polygons are rendered. Their size is 1px by default.
64 Point,
65 /// Only edges of polygons are rendered using 1px lines. This mode is useful for wireframe
66 /// drawing.
67 Line,
68 /// The entire polygon surface is rendered. This is default rendering mode.
69 #[default]
70 Fill,
71}
72
73/// A function used to compare two values. Usually it is used for depth and stencil testing.
74#[derive(
75 Copy,
76 Clone,
77 PartialOrd,
78 PartialEq,
79 Eq,
80 Ord,
81 Hash,
82 Visit,
83 Serialize,
84 Deserialize,
85 Debug,
86 Reflect,
87 AsRefStr,
88 EnumString,
89 VariantNames,
90 Default,
91)]
92pub enum CompareFunc {
93 /// Never passes.
94 Never,
95
96 /// Passes if the incoming value is less than the stored value.
97 Less,
98
99 /// Passes if the incoming value is equal to the stored value.
100 Equal,
101
102 /// Passes if the incoming value is less than or equal to the stored value.
103 #[default]
104 LessOrEqual,
105
106 /// Passes if the incoming value is greater than the stored value.
107 Greater,
108
109 /// Passes if the incoming value is not equal to the stored value.
110 NotEqual,
111
112 /// Passes if the incoming value is greater than or equal to the stored value.
113 GreaterOrEqual,
114
115 /// Always passes.
116 Always,
117}
118
119/// Defines a set values (per color) for blending operation.
120#[derive(
121 Copy,
122 Clone,
123 Hash,
124 PartialOrd,
125 PartialEq,
126 Eq,
127 Ord,
128 Serialize,
129 Deserialize,
130 Visit,
131 Debug,
132 Reflect,
133 AsRefStr,
134 EnumString,
135 VariantNames,
136 Default,
137)]
138pub enum BlendFactor {
139 #[default]
140 Zero,
141 One,
142 SrcColor,
143 OneMinusSrcColor,
144 DstColor,
145 OneMinusDstColor,
146 SrcAlpha,
147 OneMinusSrcAlpha,
148 DstAlpha,
149 OneMinusDstAlpha,
150 ConstantColor,
151 OneMinusConstantColor,
152 ConstantAlpha,
153 OneMinusConstantAlpha,
154 SrcAlphaSaturate,
155 Src1Color,
156 OneMinusSrc1Color,
157 Src1Alpha,
158 OneMinusSrc1Alpha,
159}
160
161/// Defines an operation used in blending equation.
162#[derive(
163 Copy,
164 Clone,
165 Hash,
166 PartialOrd,
167 PartialEq,
168 Eq,
169 Ord,
170 Serialize,
171 Deserialize,
172 Visit,
173 Debug,
174 Reflect,
175 Default,
176)]
177pub enum BlendMode {
178 /// Addition of two operands (`Source + Dest`). This is default operation.
179 #[default]
180 Add,
181 /// Subtraction of two operands (`Source - Dest`).
182 Subtract,
183 /// Reverse subtraction of two operands (`Dest - Source`).
184 ReverseSubtract,
185 /// Per-component min function of two operands (`min(Source, Dest)`).
186 Min,
187 /// Per-component max function of two operands (`max(Source, Dest)`).
188 Max,
189}
190
191/// An equation used for blending a source pixel color with the destination color (the one that is
192/// already in a frame buffer). This equation has different modes for rgb/alpha parts.
193#[derive(
194 Copy,
195 Clone,
196 Default,
197 PartialOrd,
198 PartialEq,
199 Ord,
200 Eq,
201 Hash,
202 Serialize,
203 Deserialize,
204 Visit,
205 Debug,
206 Reflect,
207)]
208pub struct BlendEquation {
209 /// An operation for RGB part.
210 pub rgb: BlendMode,
211 /// An operation for alpha part.
212 pub alpha: BlendMode,
213}
214
215/// Blending function defines sources of data for both operands in blending equation (separately
216/// for RGB and Alpha parts). Default blending function is replacing destination values with the
217/// source ones.
218#[derive(
219 Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Serialize, Deserialize, Visit, Debug, Reflect,
220)]
221pub struct BlendFunc {
222 /// Data for source (the value that is produced by a shader) in the blending equation (RGB part).
223 pub sfactor: BlendFactor,
224 /// Data for destination (the value that is already in a frame buffer) in the blending equation
225 /// (RGB part).
226 pub dfactor: BlendFactor,
227 /// Data for source (the value that is produced by a shader) in the blending equation (alpha part).
228 pub alpha_sfactor: BlendFactor,
229 /// Data for destination (the value that is already in a frame buffer) in the blending equation
230 /// (alpha part).
231 pub alpha_dfactor: BlendFactor,
232}
233
234impl BlendFunc {
235 /// Creates a new blending function where both RGB and Alpha parts have the same blending factor.
236 pub fn new(sfactor: BlendFactor, dfactor: BlendFactor) -> Self {
237 Self {
238 sfactor,
239 dfactor,
240 alpha_sfactor: sfactor,
241 alpha_dfactor: dfactor,
242 }
243 }
244
245 /// Creates a new blending function where RGB and Alpha parts have different blending factor.
246 pub fn new_separate(
247 sfactor: BlendFactor,
248 dfactor: BlendFactor,
249 alpha_sfactor: BlendFactor,
250 alpha_dfactor: BlendFactor,
251 ) -> Self {
252 Self {
253 sfactor,
254 dfactor,
255 alpha_sfactor,
256 alpha_dfactor,
257 }
258 }
259}
260
261impl Default for BlendFunc {
262 fn default() -> Self {
263 Self {
264 sfactor: BlendFactor::One,
265 dfactor: BlendFactor::Zero,
266 alpha_sfactor: BlendFactor::One,
267 alpha_dfactor: BlendFactor::Zero,
268 }
269 }
270}
271
272/// A mask that defines which colors will be stored in a frame buffer during rendering operation.
273/// By default, all colors are stored (every field is set to `true`).
274#[derive(
275 Copy, Clone, PartialOrd, PartialEq, Hash, Debug, Serialize, Deserialize, Visit, Eq, Reflect,
276)]
277pub struct ColorMask {
278 /// A flag, that defines whether the red channel is written or not in a frame buffer.
279 pub red: bool,
280 /// A flag, that defines whether the green channel is written or not in a frame buffer.
281 pub green: bool,
282 /// A flag, that defines whether the blue channel is written or not in a frame buffer.
283 pub blue: bool,
284 /// A flag, that defines whether the alpha channel is written or not in a frame buffer.
285 pub alpha: bool,
286}
287
288impl Default for ColorMask {
289 fn default() -> Self {
290 Self {
291 red: true,
292 green: true,
293 blue: true,
294 alpha: true,
295 }
296 }
297}
298
299impl ColorMask {
300 /// Creates a new color mask where all the components will have the specified value.
301 pub fn all(value: bool) -> Self {
302 Self {
303 red: value,
304 green: value,
305 blue: value,
306 alpha: value,
307 }
308 }
309}
310
311/// Defines a polygon face that will be rendered. This is usually used for back face culling.
312/// Default value is [`PolygonFace::FrontAndBack`].
313#[derive(
314 Copy,
315 Clone,
316 PartialOrd,
317 PartialEq,
318 Hash,
319 Debug,
320 Deserialize,
321 Visit,
322 Eq,
323 Reflect,
324 AsRefStr,
325 EnumString,
326 VariantNames,
327 Default,
328)]
329pub enum PolygonFace {
330 /// Only front faces will be rendered.
331 Front,
332 /// Only back faces will be rendered.
333 Back,
334 /// Both, back and front faces will be rendered.
335 #[default]
336 FrontAndBack,
337}
338
339/// Defines a function that used in a stencil test.
340#[derive(
341 Copy, Clone, PartialOrd, PartialEq, Hash, Debug, Serialize, Deserialize, Visit, Eq, Reflect,
342)]
343pub struct StencilFunc {
344 /// A function that is used to compare two values. Default value is [`CompareFunc::Always`].
345 pub func: CompareFunc,
346 /// Reference value that is used to compare against the current value in the stencil buffer.
347 /// Default value is 0.
348 pub ref_value: u32,
349 /// A mask value that is used to filter out some bits (using logical AND operation) of a value
350 /// in the stencil buffer and the ref value (for example if a [`CompareFunc::Less`] is used
351 /// then the entire equation will look like `(ref & mask) < (stencil & mask)`). Default value
352 /// is `0xFFFFFFFF`.
353 pub mask: u32,
354}
355
356impl Default for StencilFunc {
357 fn default() -> Self {
358 Self {
359 func: CompareFunc::Always,
360 ref_value: 0,
361 mask: 0xFFFF_FFFF,
362 }
363 }
364}
365
366/// An action with the stencil value in the stencil buffer is the stencil test passed.
367#[derive(
368 Copy,
369 Clone,
370 PartialOrd,
371 PartialEq,
372 Hash,
373 Debug,
374 Serialize,
375 Deserialize,
376 Visit,
377 Eq,
378 Reflect,
379 AsRefStr,
380 EnumString,
381 VariantNames,
382 Default,
383)]
384pub enum StencilAction {
385 /// Keeps the current value. This is the default variant.
386 #[default]
387 Keep,
388
389 /// Sets the stencil buffer value to 0.
390 Zero,
391
392 /// Sets the stencil buffer value to ref value.
393 Replace,
394
395 /// Increments the current stencil buffer value.
396 /// Clamps to the maximum representable unsigned value.
397 Incr,
398
399 /// Increments the current stencil buffer value.
400 /// Wraps stencil buffer value to zero when incrementing the maximum representable
401 /// unsigned value.
402 IncrWrap,
403
404 /// Decrements the current stencil buffer value.
405 /// Clamps to 0.
406 Decr,
407
408 /// Decrements the current stencil buffer value.
409 /// Wraps stencil buffer value to the maximum representable unsigned value when
410 /// decrementing a stencil buffer value of zero.
411 DecrWrap,
412
413 /// Bitwise inverts the current stencil buffer value.
414 Invert,
415}
416
417/// A set of actions that will be performed with the stencil buffer during various testing stages.
418#[derive(
419 Copy, Clone, PartialOrd, PartialEq, Hash, Debug, Serialize, Deserialize, Visit, Eq, Reflect,
420)]
421pub struct StencilOp {
422 /// An action that happens when the stencil test has failed.
423 pub fail: StencilAction,
424 /// An action that happens when the depth test has failed.
425 pub zfail: StencilAction,
426 /// An action that happens when the depth test has passed.
427 pub zpass: StencilAction,
428 /// A mask that is used to filter out some bits (using `AND` logical operation) from the source
429 /// value before writing it to the stencil buffer.
430 pub write_mask: u32,
431}
432
433impl Default for StencilOp {
434 fn default() -> Self {
435 Self {
436 fail: Default::default(),
437 zfail: Default::default(),
438 zpass: Default::default(),
439 write_mask: 0xFFFF_FFFF,
440 }
441 }
442}
443
444/// A face side to cull.
445#[derive(
446 Copy,
447 Clone,
448 PartialOrd,
449 PartialEq,
450 Hash,
451 Debug,
452 Serialize,
453 Deserialize,
454 Visit,
455 Eq,
456 Reflect,
457 Default,
458)]
459pub enum CullFace {
460 /// Cull only back faces.
461 #[default]
462 Back,
463 /// Cull only front faces.
464 Front,
465}
466
467/// Blending parameters (such as blending function and its equation).
468#[derive(Serialize, Deserialize, Default, Visit, Debug, PartialEq, Clone, Eq, Reflect)]
469pub struct BlendParameters {
470 /// Blending function, see [`BlendFunc`] for more info.
471 pub func: BlendFunc,
472 /// Blending equation, see [`BlendEquation`] for more info.
473 pub equation: BlendEquation,
474}
475
476/// A rectangular area that defines which pixels will be rendered in a frame buffer or not.
477#[derive(Serialize, Deserialize, Default, Visit, Debug, PartialEq, Clone, Copy, Eq, Reflect)]
478pub struct ScissorBox {
479 /// X coordinate of the box's origin.
480 pub x: i32,
481 /// Y coordinate of the box's origin. Located at the bottom of the rectangle.
482 pub y: i32,
483 /// Width of the box.
484 pub width: i32,
485 /// Height of the box.
486 pub height: i32,
487}
488
489/// A set of drawing parameters, that are used during draw call. It defines pretty much all pipeline
490/// settings all at once.
491#[derive(Serialize, Deserialize, Visit, Debug, PartialEq, Clone, Eq, Reflect)]
492pub struct DrawParameters {
493 /// An optional cull face. If [`None`], then the culling is disabled.
494 pub cull_face: Option<CullFace>,
495 /// Color write mask.
496 pub color_write: ColorMask,
497 /// A flag, that defines whether the depth values should be written to the depth buffer or not.
498 pub depth_write: bool,
499 /// Stencil test options. If [`None`], then the stencil test is disabled.
500 pub stencil_test: Option<StencilFunc>,
501 /// Depth test options. If [`None`], then the depth test is disabled.
502 pub depth_test: Option<CompareFunc>,
503 /// Blending options. If [`None`], then the blending is disabled.
504 pub blend: Option<BlendParameters>,
505 /// Stencil operation.
506 pub stencil_op: StencilOp,
507 /// Optional scissor box. If [`None`], then the scissor test is disabled.
508 pub scissor_box: Option<ScissorBox>,
509}
510
511impl Default for DrawParameters {
512 fn default() -> Self {
513 Self {
514 cull_face: Some(CullFace::Back),
515 color_write: Default::default(),
516 depth_write: true,
517 stencil_test: None,
518 depth_test: Some(CompareFunc::Less),
519 blend: None,
520 stencil_op: Default::default(),
521 scissor_box: None,
522 }
523 }
524}
525
526/// A range of elements (usually it's triangles) to draw in a draw call.
527#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
528pub enum ElementRange {
529 /// All available elements. This is the default option.
530 #[default]
531 Full,
532 /// Specific range of elements. Useful if you have a large buffer that contains multiple smaller
533 /// elements at once.
534 Specific {
535 /// Offset (in elements) from the beginning of the buffer.
536 offset: usize,
537 /// Total count of elements to draw.
538 count: usize,
539 },
540}
541
542/// Element kind of geometry.
543#[derive(Copy, Clone, PartialEq, Eq, Debug)]
544pub enum ElementKind {
545 /// Triangles.
546 Triangle,
547 /// Lines.
548 Line,
549 /// Points.
550 Point,
551}
552
553impl ElementKind {
554 fn index_per_element(self) -> usize {
555 match self {
556 ElementKind::Triangle => 3,
557 ElementKind::Line => 2,
558 ElementKind::Point => 1,
559 }
560 }
561}