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