1
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29
30 // Interface header.
31 #include "triangleencoder.h"
32
33 // appleseed.renderer headers.
34 #include "renderer/kernel/intersection/intersectionsettings.h"
35 #include "renderer/kernel/intersection/trianglevertexinfo.h"
36
37 // appleseed.foundation headers.
38 #include "foundation/platform/types.h"
39 #include "foundation/utility/memory.h"
40
41 using namespace foundation;
42 using namespace std;
43
44 namespace renderer
45 {
46
compute_size(const vector<TriangleVertexInfo> & triangle_vertex_infos,const vector<size_t> & triangle_indices,const size_t item_begin,const size_t item_count)47 size_t TriangleEncoder::compute_size(
48 const vector<TriangleVertexInfo>& triangle_vertex_infos,
49 const vector<size_t>& triangle_indices,
50 const size_t item_begin,
51 const size_t item_count)
52 {
53 size_t size = 0;
54
55 for (size_t i = 0; i < item_count; ++i)
56 {
57 const size_t triangle_index = triangle_indices[item_begin + i];
58 const TriangleVertexInfo& vertex_info = triangle_vertex_infos[triangle_index];
59
60 size += sizeof(uint32); // visibility flags
61 size += sizeof(uint32); // motion segment count
62
63 if (vertex_info.m_motion_segment_count == 0)
64 size += sizeof(GTriangleType);
65 else size += (vertex_info.m_motion_segment_count + 1) * 3 * sizeof(GVector3);
66 }
67
68 return size;
69 }
70
encode(const vector<TriangleVertexInfo> & triangle_vertex_infos,const vector<GVector3> & triangle_vertices,const vector<size_t> & triangle_indices,const size_t item_begin,const size_t item_count,MemoryWriter & writer)71 void TriangleEncoder::encode(
72 const vector<TriangleVertexInfo>& triangle_vertex_infos,
73 const vector<GVector3>& triangle_vertices,
74 const vector<size_t>& triangle_indices,
75 const size_t item_begin,
76 const size_t item_count,
77 MemoryWriter& writer)
78 {
79 for (size_t i = 0; i < item_count; ++i)
80 {
81 const size_t triangle_index = triangle_indices[item_begin + i];
82 const TriangleVertexInfo& vertex_info = triangle_vertex_infos[triangle_index];
83
84 writer.write(vertex_info.m_vis_flags);
85 writer.write(static_cast<uint32>(vertex_info.m_motion_segment_count));
86
87 if (vertex_info.m_motion_segment_count == 0)
88 {
89 writer.write(
90 GTriangleType(
91 triangle_vertices[vertex_info.m_vertex_index + 0],
92 triangle_vertices[vertex_info.m_vertex_index + 1],
93 triangle_vertices[vertex_info.m_vertex_index + 2]));
94 }
95 else
96 {
97 writer.write(
98 &triangle_vertices[vertex_info.m_vertex_index],
99 (vertex_info.m_motion_segment_count + 1) * 3 * sizeof(GVector3));
100 }
101 }
102 }
103
104 } // namespace renderer
105