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