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
|
#include "UnityPrefix.h"
#include "BatchRendering.h"
#include "GfxDevice.h"
#include "Runtime/GfxDevice/ChannelAssigns.h"
#include "Runtime/Filters/Mesh/TransformVertex.h"
#include "Runtime/Filters/Mesh/LodMesh.h"
#include "Runtime/Filters/Mesh/MeshRenderer.h"
#include "Runtime/Filters/Mesh/SpriteRenderer.h"
#include "Runtime/Camera/Renderqueue.h"
#include "Runtime/Utilities/Prefetch.h"
#include "Runtime/Utilities/LogAssert.h"
#include "Runtime/Graphics/ParticleSystem/ParticleSystemRenderer.h"
BatchRenderer::BatchRenderer()
:m_BatchInstances(kMemTempAlloc)
,m_ActiveChannels(0)
,m_ActiveType(kRendererUnknown)
{
m_BatchInstances.reserve(128);
}
void BatchRenderer::Add(BaseRenderer* renderer, int subsetIndex, ChannelAssigns const* channels, const Matrix4x4f& xform, int xformType)
{
const RendererType rendererType = renderer->GetRendererType();
if (m_ActiveChannels != channels || m_ActiveType != rendererType)
Flush();
m_ActiveChannels = channels;
m_ActiveType = rendererType;
#if GFX_ENABLE_DRAW_CALL_BATCHING
// batching requires position
bool channelSetupAllowsBatching = (channels->GetTargetMap() & (1<<kVertexCompVertex));
#if GFX_OPENGLESxx_ONLY
// we do not renormalize normals in gles11 so let's play safe here
channelSetupAllowsBatching &= !(GetGfxDevice().IsPositionRequiredForTexGen() || GetGfxDevice().IsNormalRequiredForTexGen());
#endif
bool rendererTypeAllowsBatching = (rendererType == kRendererMesh) || (rendererType == kRendererSprite);
if(rendererType == kRendererParticleSystem)
{
ParticleSystemRenderer* psRenderer = (ParticleSystemRenderer*)renderer;
if(kSRMMesh != psRenderer->GetRenderMode()) // We currently don't do batching for mesh particle emitters
rendererTypeAllowsBatching = true;
}
if (channelSetupAllowsBatching && rendererTypeAllowsBatching)
{
BatchInstanceData& rb = m_BatchInstances.push_back();
rb.renderer = (Renderer*)renderer;
rb.subsetIndex = subsetIndex;
rb.xform = xform;
rb.xformType = xformType;
}
else
#endif
{
Assert(channels);
SetupObjectMatrix (xform, xformType);
renderer->Render (subsetIndex, *channels);
}
}
void BatchRenderer::Flush()
{
size_t instanceCount = m_BatchInstances.size();
#if GFX_ENABLE_DRAW_CALL_BATCHING
if (instanceCount > 0)
{
Assert(m_ActiveChannels);
if (instanceCount == 1)
{
const BatchInstanceData* data = m_BatchInstances.begin();
SetupObjectMatrix (data->xform, data->xformType);
data->renderer->Render (data->subsetIndex, *m_ActiveChannels);
}
else if(m_ActiveType == kRendererMesh)
MeshRenderer::RenderMultiple (m_BatchInstances.begin(), instanceCount, *m_ActiveChannels);
else if(m_ActiveType == kRendererParticleSystem)
ParticleSystemRenderer::RenderMultiple (m_BatchInstances.begin(), instanceCount, *m_ActiveChannels);
#if ENABLE_SPRITES
else if (m_ActiveType == kRendererSprite)
SpriteRenderer::RenderMultiple (m_BatchInstances.begin(), instanceCount, *m_ActiveChannels);
#endif
else
Assert (!"Renderer type does not support batching");
}
#else
Assert(instanceCount == 0);
#endif
m_BatchInstances.resize_uninitialized(0);
m_ActiveChannels = 0;
m_ActiveType = kRendererUnknown;
}
void AppendMeshIndices(UInt16* dstIndices, size_t& dstIndexCount, const UInt16* srcIndices, size_t srcIndexCount, bool isTriStrip)
{
Prefetch(srcIndices, srcIndexCount * kVBOIndexSize);
if (isTriStrip && dstIndexCount > 0)
{
dstIndices[dstIndexCount] = dstIndices[dstIndexCount - 1];
dstIndexCount++;
dstIndices[dstIndexCount] = srcIndices[0];
dstIndexCount++;
}
memcpy(&dstIndices[dstIndexCount], srcIndices, srcIndexCount * kVBOIndexSize);
dstIndexCount += srcIndexCount;
if (isTriStrip && (srcIndexCount % 2 == 1))
{
Assert(dstIndexCount != 0);
dstIndices[dstIndexCount] = dstIndices[dstIndexCount - 1];
dstIndexCount++;
Assert(dstIndexCount % 2 == 0);
}
}
static inline void
TransformIndicesInternalImplPositive( int* __restrict dst, const int* __restrict src, unsigned count, int offset )
{
Assert( offset>=0 );
Assert( offset<(int)0xFFFF );
const int maskOffset = ((unsigned)offset << 16) | (unsigned)offset;
for( unsigned i = 0 ; i < count ; ++i )
*dst++ = *src++ + maskOffset;
}
static inline void
TransformIndicesInternalImplNegative( int* __restrict dst, const int* __restrict src, unsigned count, int offset )
{
Assert( offset>=0 );
Assert( offset<(int)0xFFFF );
const int maskOffset = ((unsigned)offset << 16) | (unsigned)offset;
for( unsigned i = 0 ; i < count ; ++i )
*dst++ = *src++ - maskOffset;
}
size_t TransformIndices(UInt16* dst, const void* srcIB, size_t firstByte, size_t indexCount, size_t firstVertex, size_t batchVertexOffset, bool isTriStrip)
{
UInt16* srcIndices = (UInt16*)((UInt8*)srcIB + firstByte);
Prefetch(srcIndices, indexCount * kVBOIndexSize);
UInt16* const baseDataPtr = dst;
if (isTriStrip && batchVertexOffset > 0)
{
Assert(srcIndices[0] >= firstVertex);
dst[0] = dst[-1];
dst[1] = srcIndices[0] - firstVertex + batchVertexOffset;
dst += 2;
}
int offset = (int)batchVertexOffset - (int)firstVertex;
int copyCount = indexCount/2;
if( offset >= 0 ) TransformIndicesInternalImplPositive((int*)dst, (int*)srcIndices, copyCount, offset);
else TransformIndicesInternalImplNegative((int*)dst, (int*)srcIndices, copyCount, -offset);
// leftover, as we copy ints
if(2*copyCount != indexCount)
dst[indexCount-1] = srcIndices[indexCount-1] - firstVertex + batchVertexOffset;
#ifdef CHECK_INDEX_TRANSFORM_RESULTS
const UInt16* dstCheck = dst;
for( unsigned i = 0 ; i < indexCount ; ++i )
Assert( dstCheck[i] == srcIndices[i] - submesh.firstVertex + batchVertexOffset );
#endif
dst += indexCount;
if (isTriStrip && indexCount % 2 == 1)
{
dst[0] = dst[-1];
++dst;
}
Assert ((dst - baseDataPtr) <= indexCount + 3);
return (dst - baseDataPtr);
}
size_t TransformVertices(UInt8* dst, Matrix4x4f const& m, const VertexBufferData& src, size_t firstVertex, size_t vertexCount, UInt32 channelsInVBO)
{
UInt8* inChannels[kShaderChannelCount];
UInt32 inStrides[kShaderChannelCount];
bool multiStream = false;
for (int i = 0; i < kShaderChannelCount; i++)
{
if (channelsInVBO & (1 << i))
{
const ChannelInfo& info = src.channels[i];
DebugAssert(info.IsValid());
UInt32 stride = info.CalcStride(src.streams);
UInt32 offset = info.CalcOffset(src.streams) + stride * firstVertex;
inChannels[i] = &src.buffer[offset];
inStrides[i] = stride;
if (info.stream > 0)
multiStream = true;
}
else
{
inChannels[i] = NULL;
inStrides[i] = 0;
}
}
TransformVerticesStrided(
StrideIterator<Vector3f>(inChannels[kShaderChannelVertex], inStrides[kShaderChannelVertex]),
StrideIterator<Vector3f>(inChannels[kShaderChannelNormal], inStrides[kShaderChannelNormal]),
StrideIterator<ColorRGBA32>(inChannels[kShaderChannelColor], inStrides[kShaderChannelColor]),
StrideIterator<Vector2f>(inChannels[kShaderChannelTexCoord0], inStrides[kShaderChannelTexCoord0]),
StrideIterator<Vector2f>(inChannels[kShaderChannelTexCoord1], inStrides[kShaderChannelTexCoord1]),
StrideIterator<Vector4f>(inChannels[kShaderChannelTangent], inStrides[kShaderChannelTangent]),
dst, m, vertexCount, multiStream);
return vertexCount;
}
#if ENABLE_SPRITES
void TransformSprite (UInt8* vb, UInt16* ib, const Matrix4x4f* m, const SpriteRenderData* rd, ColorRGBA32 color, size_t firstVertex)
{
UInt32 vertexCount = rd->vertices.size();
UInt32 indexCount = rd->indices.size();
UInt8* srcVertices = vertexCount > 0 ? (UInt8 *)&rd->vertices[0] : NULL;
UInt16* srcIndices = indexCount > 0 ? (UInt16 *)&rd->indices[0] : NULL;
#if (UNITY_SUPPORTS_NEON || UNITY_SUPPORTS_VFP) && !UNITY_DISABLE_NEON_SKINNING
int stride = sizeof(SpriteVertex);
UInt8* end = srcVertices + vertexCount * stride;
UInt8* uv = srcVertices + sizeof(Vector3f);
Matrix4x4f xform = (m) ? *m : Matrix4x4f::identity;
# if UNITY_SUPPORTS_NEON
if (CPUInfo::HasNEONSupport())
s_TransformVertices_Sprite_NEON(srcVertices, end, uv, xform.m_Data, (UInt8 *)vb, stride, color.GetUInt32());
else
# endif
# if UNITY_SUPPORTS_VFP
s_TransformVertices_Sprite_VFP (srcVertices, end, uv, xform.m_Data, (UInt8 *)vb, stride, color.GetUInt32());
# else
{
ErrorString("non-NEON path not enabled!");
}
# endif
#else
struct SpriteVBOLayout {
Vector3f pos;
ColorRGBA32 col;
Vector2f uv;
};
SpriteVertex *src = (SpriteVertex *)srcVertices;
SpriteVBOLayout *dst = (SpriteVBOLayout *)vb;
while (vertexCount-- > 0)
{
dst->pos = (m) ? m->MultiplyPoint3(src->pos) : src->pos;
dst->col = color;
dst->uv = src->uv;
dst++;
src++;
}
#endif
Prefetch(srcIndices, indexCount * kVBOIndexSize);
if (firstVertex)
{
int maskOffset = ((unsigned)firstVertex << 16) | (unsigned)firstVertex;
int copyCount = indexCount>>1;
if(2*copyCount != indexCount)
ib[indexCount-1] = srcIndices[indexCount-1] + firstVertex;
int *dst = (int *)ib;
int *src = (int *)srcIndices;
while (copyCount-- > 0)
*(dst++) = *(src++) + maskOffset;
}
else
memcpy(ib, srcIndices, sizeof(UInt16) * indexCount);
}
#endif
|