fna-workbench

fna-workbench Git Source Tree


Root/src/Graphics/Vertices/VertexDeclaration.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
 * Copyright 2009-2016 Ethan Lee and the MonoGame Team
 *
 * Released under the Microsoft Public License.
 * See LICENSE for details.
 */
#endregion
 
#region Using Statements
using System;
using System.Collections.Generic;
#endregion
 
namespace Microsoft.Xna.Framework.Graphics
{
    public class VertexDeclaration : GraphicsResource
    {
        #region Public Properties
 
        public int VertexStride
        {
            get;
            private set;
        }
 
        #endregion
 
        #region Internal Variables
 
        internal VertexElement[] elements;
 
        #endregion
 
        #region Public Constructors
 
        public VertexDeclaration(
            params VertexElement[] elements
        ) : this(GetVertexStride(elements), elements) {
        }
 
        public VertexDeclaration(
            int vertexStride,
            params VertexElement[] elements
        ) {
            if ((elements == null) || (elements.Length == 0))
            {
                throw new ArgumentNullException("elements", "Elements cannot be empty");
            }
 
            this.elements = (VertexElement[]) elements.Clone();
            VertexStride = vertexStride;
        }
 
        #endregion
 
        #region Public Methods
 
        public VertexElement[] GetVertexElements()
        {
            return (VertexElement[]) elements.Clone();
        }
 
        #endregion
 
        #region Internal Static Methods
 
        /// <summary>
        /// Returns the VertexDeclaration for Type.
        /// </summary>
        /// <param name="vertexType">A value type which implements the IVertexType interface.</param>
        /// <returns>The VertexDeclaration.</returns>
        /// <remarks>
        /// Prefer to use VertexDeclarationCache when the declaration lookup
        /// can be performed with a templated type.
        /// </remarks>
        internal static VertexDeclaration FromType(Type vertexType)
        {
            if (vertexType == null)
            {
                throw new ArgumentNullException("vertexType", "Cannot be null");
            }
 
            if (!vertexType.IsValueType)
            {
                throw new ArgumentException("vertexType", "Must be value type");
            }
 
            IVertexType type = Activator.CreateInstance(vertexType) as IVertexType;
            if (type == null)
            {
                throw new ArgumentException("vertexData does not inherit IVertexType");
            }
 
            VertexDeclaration vertexDeclaration = type.VertexDeclaration;
            if (vertexDeclaration == null)
            {
                throw new ArgumentException("vertexType's VertexDeclaration cannot be null");
            }
 
            return vertexDeclaration;
        }
 
        #endregion
 
        #region Private Static VertexElement Methods
 
        private static int GetVertexStride(VertexElement[] elements)
        {
            int max = 0;
 
            for (int i = 0; i < elements.Length; i += 1)
            {
                int start = elements[i].Offset + GetTypeSize(elements[i].VertexElementFormat);
                if (max < start)
                {
                    max = start;
                }
            }
 
            return max;
        }
 
        private static int GetTypeSize(VertexElementFormat elementFormat)
        {
            switch (elementFormat)
            {
                case VertexElementFormat.Single:
                    return 4;
                case VertexElementFormat.Vector2:
                    return 8;
                case VertexElementFormat.Vector3:
                    return 12;
                case VertexElementFormat.Vector4:
                    return 16;
                case VertexElementFormat.Color:
                    return 4;
                case VertexElementFormat.Byte4:
                    return 4;
                case VertexElementFormat.Short2:
                    return 4;
                case VertexElementFormat.Short4:
                    return 8;
                case VertexElementFormat.NormalizedShort2:
                    return 4;
                case VertexElementFormat.NormalizedShort4:
                    return 8;
                case VertexElementFormat.HalfVector2:
                    return 4;
                case VertexElementFormat.HalfVector4:
                    return 8;
            }
            return 0;
        }
 
        #endregion
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.19303s using 11 queries.