fyrox_impl/renderer/framework/
mod.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//! Rendering framework.
22
23use crate::{
24    renderer::framework::{
25        buffer::BufferUsage,
26        error::FrameworkError,
27        geometry_buffer::{
28            AttributeDefinition, AttributeKind, GeometryBuffer, GeometryBufferDescriptor,
29            VertexBufferData, VertexBufferDescriptor,
30        },
31        server::GraphicsServer,
32    },
33    scene::mesh::{buffer::VertexAttributeDataType, surface::SurfaceData},
34};
35use fyrox_graphics::geometry_buffer::ElementsDescriptor;
36pub use fyrox_graphics::*;
37
38/// Extension trait for [`GeometryBuffer`].
39pub trait GeometryBufferExt {
40    /// Creates [`GeometryBuffer`] from [`SurfaceData`].
41    fn from_surface_data(
42        data: &SurfaceData,
43        usage: BufferUsage,
44        server: &dyn GraphicsServer,
45    ) -> Result<Box<dyn GeometryBuffer>, FrameworkError>;
46}
47
48impl GeometryBufferExt for dyn GeometryBuffer {
49    fn from_surface_data(
50        data: &SurfaceData,
51        usage: BufferUsage,
52        server: &dyn GraphicsServer,
53    ) -> Result<Box<dyn GeometryBuffer>, FrameworkError> {
54        let attributes = data
55            .vertex_buffer
56            .layout()
57            .iter()
58            .map(|a| AttributeDefinition {
59                location: a.shader_location as u32,
60                kind: match a.data_type {
61                    VertexAttributeDataType::F32 => AttributeKind::Float,
62                    VertexAttributeDataType::U32 => AttributeKind::UnsignedInt,
63                    VertexAttributeDataType::U16 => AttributeKind::UnsignedShort,
64                    VertexAttributeDataType::U8 => AttributeKind::UnsignedByte,
65                },
66                component_count: a.size as usize,
67                normalized: a.normalized,
68                divisor: a.divisor as u32,
69            })
70            .collect::<Vec<_>>();
71
72        let geometry_buffer_desc = GeometryBufferDescriptor {
73            buffers: &[VertexBufferDescriptor {
74                usage,
75                attributes: &attributes,
76                data: VertexBufferData {
77                    element_size: data.vertex_buffer.vertex_size() as usize,
78                    bytes: Some(data.vertex_buffer.raw_data()),
79                },
80            }],
81            usage,
82            elements: ElementsDescriptor::Triangles(data.geometry_buffer.triangles_ref()),
83        };
84
85        server.create_geometry_buffer(geometry_buffer_desc)
86    }
87}