aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/framework/src/ApexVertexFormat.cpp
blob: beb99bea1502b628661a4bbe26b58fe05077844e (plain) (blame)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Copyright (c) 2008-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */


#include "ApexVertexFormat.h"
#include "ApexSDKIntl.h"

#include <ParamArray.h>

namespace nvidia
{
namespace apex
{

// Local functions and definitions

PX_INLINE char* apex_strdup(const char* input)
{
	if (input == NULL)
	{
		return NULL;
	}

	size_t len = strlen(input);

	char* result = (char*)PX_ALLOC(sizeof(char) * (len + 1), PX_DEBUG_EXP("apex_strdup"));
#ifdef WIN32
	strncpy_s(result, len + 1, input, len);
#else
	strncpy(result, input, len);
#endif

	return result;
}

PX_INLINE uint32_t hash(const char* string)
{
	// "DJB" string hash
	uint32_t h = 5381;
	char c;
	while ((c = *string++) != '\0')
	{
		h = ((h << 5) + h) ^ c;
	}
	return h;
}

struct SemanticNameAndID
{
	SemanticNameAndID(const char* name, VertexFormat::BufferID id) : m_name(name), m_id(id)
	{
		PX_ASSERT(m_id != 0 || nvidia::strcmp(m_name, "SEMANTIC_INVALID") == 0);
	}
	const char*					m_name;
	VertexFormat::BufferID	m_id;
};

#define SEMANTIC_NAME_AND_ID( name )	SemanticNameAndID( name, (VertexFormat::BufferID)hash( name ) )

static const SemanticNameAndID sSemanticNamesAndIDs[] =
{
	SEMANTIC_NAME_AND_ID("SEMANTIC_POSITION"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_NORMAL"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_TANGENT"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_BINORMAL"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_COLOR"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_TEXCOORD0"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_TEXCOORD1"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_TEXCOORD2"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_TEXCOORD3"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_BONE_INDEX"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_BONE_WEIGHT"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_DISPLACEMENT_TEXCOORD"),
	SEMANTIC_NAME_AND_ID("SEMANTIC_DISPLACEMENT_FLAGS"),

	SemanticNameAndID("SEMANTIC_INVALID", (VertexFormat::BufferID)0)
};


// VertexFormat implementation
void ApexVertexFormat::reset()
{
	if (mParams != NULL)
	{
		mParams->winding = 0;
		mParams->hasSeparateBoneBuffer = 0;
	}
	clearBuffers();
}

void ApexVertexFormat::setWinding(RenderCullMode::Enum winding)
{
	mParams->winding = winding;
}

void ApexVertexFormat::setHasSeparateBoneBuffer(bool hasSeparateBoneBuffer)
{
	mParams->hasSeparateBoneBuffer = hasSeparateBoneBuffer;
}

RenderCullMode::Enum ApexVertexFormat::getWinding() const
{
	return (RenderCullMode::Enum)mParams->winding;
}

bool ApexVertexFormat::hasSeparateBoneBuffer() const
{
	return mParams->hasSeparateBoneBuffer;
}

const char* ApexVertexFormat::getSemanticName(RenderVertexSemantic::Enum semantic) const
{
	PX_ASSERT((uint32_t)semantic < RenderVertexSemantic::NUM_SEMANTICS);
	return (uint32_t)semantic < RenderVertexSemantic::NUM_SEMANTICS ? sSemanticNamesAndIDs[semantic].m_name : NULL;
}

VertexFormat::BufferID ApexVertexFormat::getSemanticID(RenderVertexSemantic::Enum semantic) const
{
	PX_ASSERT((uint32_t)semantic < RenderVertexSemantic::NUM_SEMANTICS);
	return (uint32_t)semantic < RenderVertexSemantic::NUM_SEMANTICS ? sSemanticNamesAndIDs[semantic].m_id : (BufferID)0;
}

VertexFormat::BufferID ApexVertexFormat::getID(const char* name) const
{
	if (name == NULL)
	{
		return (BufferID)0;
	}
	const BufferID id = hash(name);
	return id ? id : (BufferID)1;	// We reserve 0 for an invalid ID
}

int32_t ApexVertexFormat::addBuffer(const char* name)
{
	if (name == NULL)
	{
		return -1;
	}

	const BufferID id = getID(name);

	int32_t index = getBufferIndexFromID(id);
	if (index >= 0)
	{
		return index;
	}

	int32_t semantic = 0;
	for (; semantic < RenderVertexSemantic::NUM_SEMANTICS; ++semantic)
	{
		if (getSemanticID((RenderVertexSemantic::Enum)semantic) == id)
		{
			break;
		}
	}
	if (semantic == RenderVertexSemantic::NUM_SEMANTICS)
	{
		semantic = RenderVertexSemantic::CUSTOM;
	}

	NvParameterized::Handle handle(*mParams);
	mParams->getParameterHandle("bufferFormats", handle);

	mParams->getArraySize(handle, index);

	mParams->resizeArray(handle, index + 1);

	NvParameterized::Handle elementHandle(*mParams);
	handle.getChildHandle(index, elementHandle);
	NvParameterized::Handle subElementHandle(*mParams);
	elementHandle.getChildHandle(mParams, "name", subElementHandle);
	mParams->setParamString(subElementHandle, name);
	elementHandle.getChildHandle(mParams, "semantic", subElementHandle);
	mParams->setParamI32(subElementHandle, semantic);
	elementHandle.getChildHandle(mParams, "id", subElementHandle);
	mParams->setParamU32(subElementHandle, (uint32_t)id);
	elementHandle.getChildHandle(mParams, "format", subElementHandle);
	mParams->setParamU32(subElementHandle, (uint32_t)RenderDataFormat::UNSPECIFIED);
	elementHandle.getChildHandle(mParams, "access", subElementHandle);
	mParams->setParamU32(subElementHandle, (uint32_t)RenderDataAccess::STATIC);
	elementHandle.getChildHandle(mParams, "serialize", subElementHandle);
	mParams->setParamBool(subElementHandle, true);

	return index;
}

bool ApexVertexFormat::bufferReplaceWithLast(uint32_t index)
{
	PX_ASSERT((int32_t)index < mParams->bufferFormats.arraySizes[0]);
	if ((int32_t)index < mParams->bufferFormats.arraySizes[0])
	{
		ParamArray<VertexFormatParametersNS::BufferFormat_Type> bufferFormats(mParams, "bufferFormats", reinterpret_cast<ParamDynamicArrayStruct*>(&mParams->bufferFormats));
		bufferFormats.replaceWithLast(index);
		return true;
	}

	return false;
}

bool ApexVertexFormat::setBufferFormat(uint32_t index, RenderDataFormat::Enum format)
{
	if (index < getBufferCount())
	{
		mParams->bufferFormats.buf[index].format = format;
		return true;
	}

	return false;
}

bool ApexVertexFormat::setBufferAccess(uint32_t index, RenderDataAccess::Enum access)
{
	if (index < getBufferCount())
	{
		mParams->bufferFormats.buf[index].access = access;
		return true;
	}

	return false;
}

bool ApexVertexFormat::setBufferSerialize(uint32_t index, bool serialize)
{
	if (index < getBufferCount())
	{
		mParams->bufferFormats.buf[index].serialize = serialize;
		return true;
	}

	return false;
}

const char* ApexVertexFormat::getBufferName(uint32_t index) const
{
	return index < getBufferCount() ? (const char*)mParams->bufferFormats.buf[index].name : NULL;
}

RenderVertexSemantic::Enum ApexVertexFormat::getBufferSemantic(uint32_t index) const
{
	return index < getBufferCount() ? (RenderVertexSemantic::Enum)mParams->bufferFormats.buf[index].semantic : RenderVertexSemantic::NUM_SEMANTICS;
}

VertexFormat::BufferID ApexVertexFormat::getBufferID(uint32_t index) const
{
	return index < getBufferCount() ? (BufferID)mParams->bufferFormats.buf[index].id : (BufferID)0;
}

RenderDataFormat::Enum ApexVertexFormat::getBufferFormat(uint32_t index) const
{
	return index < getBufferCount() ? (RenderDataFormat::Enum)mParams->bufferFormats.buf[index].format : RenderDataFormat::UNSPECIFIED;
}

RenderDataAccess::Enum ApexVertexFormat::getBufferAccess(uint32_t index) const
{
	return index < getBufferCount() ? (RenderDataAccess::Enum)mParams->bufferFormats.buf[index].access : RenderDataAccess::ACCESS_TYPE_COUNT;
}

bool ApexVertexFormat::getBufferSerialize(uint32_t index) const
{
	return index < getBufferCount() ? mParams->bufferFormats.buf[index].serialize : false;
}

uint32_t ApexVertexFormat::getBufferCount() const
{
	return (uint32_t)mParams->bufferFormats.arraySizes[0];
}

uint32_t ApexVertexFormat::getCustomBufferCount() const
{
	PX_ASSERT(mParams != NULL);
	uint32_t customBufferCount = 0;
	for (int32_t i = 0; i < mParams->bufferFormats.arraySizes[0]; ++i)
	{
		if (mParams->bufferFormats.buf[i].semantic == RenderVertexSemantic::CUSTOM)
		{
			++customBufferCount;
		}
	}
	return customBufferCount;
}

int32_t ApexVertexFormat::getBufferIndexFromID(BufferID id) const
{
	for (int32_t i = 0; i < mParams->bufferFormats.arraySizes[0]; ++i)
	{
		if (mParams->bufferFormats.buf[i].id == (uint32_t)id)
		{
			return i;
		}
	}

	return -1;
}



// ApexVertexFormat functions

ApexVertexFormat::ApexVertexFormat()
{
	NvParameterized::Traits* traits = GetInternalApexSDK()->getParameterizedTraits();
	mParams = DYNAMIC_CAST(VertexFormatParameters*)(traits->createNvParameterized(VertexFormatParameters::staticClassName()));
	mOwnsParams = mParams != NULL;
}

ApexVertexFormat::ApexVertexFormat(VertexFormatParameters* params) : mParams(params), mOwnsParams(false)
{
}

ApexVertexFormat::ApexVertexFormat(const ApexVertexFormat& f) : VertexFormat(f)
{
	NvParameterized::Traits* traits = GetInternalApexSDK()->getParameterizedTraits();
	mParams = DYNAMIC_CAST(VertexFormatParameters*)(traits->createNvParameterized(VertexFormatParameters::staticClassName()));
	mOwnsParams = mParams != NULL;
	if (mParams)
	{
		copy(f);
	}
}

ApexVertexFormat::~ApexVertexFormat()
{
	if (mOwnsParams && mParams != NULL)
	{
		mParams->destroy();
	}
}

bool ApexVertexFormat::operator == (const VertexFormat& format) const
{
	if (getWinding() != format.getWinding())
	{
		return false;
	}

	if (hasSeparateBoneBuffer() != format.hasSeparateBoneBuffer())
	{
		return false;
	}

	if (getBufferCount() != format.getBufferCount())
	{
		return false;
	}

	for (uint32_t thisIndex = 0; thisIndex < getBufferCount(); ++thisIndex)
	{
		BufferID id = getBufferID(thisIndex);
		const int32_t thatIndex = format.getBufferIndexFromID(id);
		if (thatIndex < 0)
		{
			return false;
		}
		if (getBufferFormat(thisIndex) != format.getBufferFormat((uint32_t)thatIndex))
		{
			return false;
		}
		if (getBufferAccess(thisIndex) != format.getBufferAccess((uint32_t)thatIndex))
		{
			return false;
		}
	}

	return true;
}

void ApexVertexFormat::copy(const ApexVertexFormat& other)
{
	reset();

	setWinding(other.getWinding());
	setHasSeparateBoneBuffer(other.hasSeparateBoneBuffer());

	for (uint32_t i = 0; i < other.getBufferCount(); ++i)
	{
		const char* name = other.getBufferName(i);
		const uint32_t index = (uint32_t)addBuffer(name);
		setBufferFormat(index, other.getBufferFormat(i));
		setBufferAccess(index, other.getBufferAccess(i));
		setBufferSerialize(index, other.getBufferSerialize(i));
	}
}

void ApexVertexFormat::clearBuffers()
{
	if (mParams)
	{
		NvParameterized::Handle handle(*mParams);

		mParams->getParameterHandle("bufferFormats", handle);
		handle.resizeArray(0);
	}
}


}
} // end namespace nvidia::apex