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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)
#include "Wm5GraphicsPCH.h"
#include "Wm5VertexFormat.h"
#include "Wm5Renderer.h"
using namespace Wm5;
WM5_IMPLEMENT_RTTI(Wm5, Object, VertexFormat);
WM5_IMPLEMENT_STREAM(VertexFormat);
WM5_IMPLEMENT_FACTORY(VertexFormat);
WM5_IMPLEMENT_DEFAULT_NAMES(Object, VertexFormat);
int VertexFormat::msComponentSize[AT_QUANTITY] =
{
0, // AT_NONE
4, // AT_FLOAT1
4, // AT_FLOAT2
4, // AT_FLOAT3
4, // AT_FLOAT4
2, // AT_HALF1
2, // AT_HALF2
2, // AT_HALF3
2, // AT_HALF4
1, // AT_UBYTE4
2, // AT_SHORT1
2, // AT_SHORT2
2 // AT_SHORT4
};
int VertexFormat::msNumComponents[AT_QUANTITY] =
{
0, // AT_NONE
1, // AT_FLOAT1
2, // AT_FLOAT2
3, // AT_FLOAT3
4, // AT_FLOAT4
1, // AT_HALF1
2, // AT_HALF2
3, // AT_HALF3
4, // AT_HALF4
4, // AT_UBYTE4
1, // AT_SHORT1
2, // AT_SHORT2
4 // AT_SHORT4
};
int VertexFormat::msTypeSize[AT_QUANTITY] =
{
0, // AT_NONE
4, // AT_FLOAT1
8, // AT_FLOAT2
12, // AT_FLOAT3
16, // AT_FLOAT4
2, // AT_HALF1
4, // AT_HALF2
6, // AT_HALF3
8, // AT_HALF4
4, // AT_UBYTE4
2, // AT_SHORT1
4, // AT_SHORT2
8 // AT_SHORT4
};
//----------------------------------------------------------------------------
VertexFormat::VertexFormat (int numAttributes)
:
mNumAttributes(numAttributes),
mStride(0)
{
assertion(mNumAttributes > 0, "Number of attributes must be positive\n");
for (int i = 0; i < AM_MAX_ATTRIBUTES; ++i)
{
mElements[i].StreamIndex = 0;
mElements[i].Offset = 0;
mElements[i].Type = AT_NONE;
mElements[i].Usage = AU_NONE;
mElements[i].UsageIndex = 0;
}
}
//----------------------------------------------------------------------------
VertexFormat::~VertexFormat ()
{
Renderer::UnbindAll(this);
}
//----------------------------------------------------------------------------
VertexFormat* VertexFormat::Create (int numAttributes, ...)
{
VertexFormat* vformat = new0 VertexFormat(numAttributes);
va_list arguments;
va_start(arguments, numAttributes);
unsigned int offset = 0;
for (int i = 0; i < numAttributes; ++i)
{
int usage = va_arg(arguments, int);
int type = va_arg(arguments, int);
unsigned int usageIndex = va_arg(arguments, unsigned int);
vformat->SetAttribute(i, 0, offset, (AttributeType)type,
(AttributeUsage)usage, usageIndex);
offset += msTypeSize[type];
}
vformat->SetStride((int)offset);
va_end(arguments);
return vformat;
}
//----------------------------------------------------------------------------
void VertexFormat::SetAttribute (int attribute, unsigned int streamIndex,
unsigned int offset, AttributeType type, AttributeUsage usage,
unsigned int usageIndex)
{
assertion(0 <= attribute && attribute < mNumAttributes,
"Invalid index in SetAttribute\n");
#ifdef _DEBUG
if (attribute > 0)
{
assertion(offset > mElements[attribute-1].Offset,
"Offsets must be increasing with attribute index.\n");
}
else
{
assertion(offset == 0,
"The offset of the first attribute must be zero.\n");
}
#endif
Element& element = mElements[attribute];
element.StreamIndex = streamIndex;
element.Offset = offset;
element.Type = type;
element.Usage = usage;
element.UsageIndex = usageIndex;
}
//----------------------------------------------------------------------------
void VertexFormat::SetStride (int stride)
{
assertion(stride > 0, "Stride must be positive\n");
mStride = stride;
}
//----------------------------------------------------------------------------
void VertexFormat::GetAttribute (int attribute, unsigned int& streamIndex,
unsigned int& offset, AttributeType& type, AttributeUsage& usage,
unsigned int& usageIndex) const
{
assertion(0 <= attribute && attribute < mNumAttributes,
"Invalid index in GetAttribute\n");
const Element& element = mElements[attribute];
streamIndex = element.StreamIndex;
offset = element.Offset;
type = element.Type;
usage = element.Usage;
usageIndex = element.UsageIndex;
}
//----------------------------------------------------------------------------
int VertexFormat::GetIndex (AttributeUsage usage, unsigned int usageIndex)
const
{
for (int i = 0; i < mNumAttributes; ++i)
{
if (mElements[i].Usage == usage
&& mElements[i].UsageIndex == usageIndex)
{
return i;
}
}
return -1;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Streaming support.
//----------------------------------------------------------------------------
VertexFormat::VertexFormat (LoadConstructor value)
:
Object(value),
mNumAttributes(0),
mStride(0)
{
for (int i = 0; i < AM_MAX_ATTRIBUTES; ++i)
{
mElements[i].StreamIndex = 0;
mElements[i].Offset = 0;
mElements[i].Type = AT_NONE;
mElements[i].Usage = AU_NONE;
mElements[i].UsageIndex = 0;
}
}
//----------------------------------------------------------------------------
void VertexFormat::Load (InStream& source)
{
WM5_BEGIN_DEBUG_STREAM_LOAD(source);
Object::Load(source);
source.Read(mNumAttributes);
for (int i = 0; i < AM_MAX_ATTRIBUTES; ++i)
{
source.Read(mElements[i].StreamIndex);
source.Read(mElements[i].Offset);
source.ReadEnum(mElements[i].Type);
source.ReadEnum(mElements[i].Usage);
source.Read(mElements[i].UsageIndex);
}
source.Read(mStride);
WM5_END_DEBUG_STREAM_LOAD(VertexFormat, source);
}
//----------------------------------------------------------------------------
void VertexFormat::Link (InStream& source)
{
Object::Link(source);
}
//----------------------------------------------------------------------------
void VertexFormat::PostLink ()
{
Object::PostLink();
}
//----------------------------------------------------------------------------
bool VertexFormat::Register (OutStream& target) const
{
return Object::Register(target);
}
//----------------------------------------------------------------------------
void VertexFormat::Save (OutStream& target) const
{
WM5_BEGIN_DEBUG_STREAM_SAVE(target);
Object::Save(target);
target.Write(mNumAttributes);
for (int i = 0; i < AM_MAX_ATTRIBUTES; ++i)
{
target.Write(mElements[i].StreamIndex);
target.Write(mElements[i].Offset);
target.WriteEnum(mElements[i].Type);
target.WriteEnum(mElements[i].Usage);
target.Write(mElements[i].UsageIndex);
}
target.Write(mStride);
WM5_END_DEBUG_STREAM_SAVE(VertexFormat, target);
}
//----------------------------------------------------------------------------
int VertexFormat::GetStreamingSize () const
{
int size = Object::GetStreamingSize();
size += sizeof(mNumAttributes);
for (int i = 0; i < AM_MAX_ATTRIBUTES; ++i)
{
size += sizeof(mElements[i].StreamIndex);
size += sizeof(mElements[i].Offset);
size += WM5_ENUMSIZE(mElements[i].Type);
size += WM5_ENUMSIZE(mElements[i].Usage);
size += sizeof(mElements[i].UsageIndex);
}
size += sizeof(mStride);
return size;
}
//----------------------------------------------------------------------------
|