Repository navigation
Expand file tree
/
Copy pathrenderqueue.cpp
More file actions
183 lines (152 loc) · 5.2 KB
/
Copy pathrenderqueue.cpp
File metadata and controls
183 lines (152 loc) · 5.2 KB
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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "renderqueue.h"
#include "model.h"
#include "texture.h"
#include "gpustatemanager.h"
#include "shadermanager.h"
//#include "../grinliz/glperformance.h"
#include "../grinliz/glgeometry.h"
#include <limits.h>
using namespace grinliz;
RenderQueue::RenderQueue()
{
}
RenderQueue::~RenderQueue()
{
}
bool RenderQueue::CompareItem::Less( const Item* s0, const Item* s1 )
{
// Priority list:
GLASSERT( sizeof(Item) < 200 ); // just a sanity check.
int result = s1->state.StateFlags() - s0->state.StateFlags();
if ( result ) return result > 0;
result = (int)((SPTR)s1->atom - (SPTR)s0->atom);
if ( result ) return result > 0;
result = s1->state.ShaderFlags() - s0->state.ShaderFlags();
if ( result ) return result > 0;
result = (int)((SPTR)s1->atom->texture - (SPTR)s0->atom->texture);
return result > 0;
}
void RenderQueue::Add( Model* model,
const ModelAtom* atom,
const GPUState& state,
const Vector4F& color,
const Vector4F& filter,
const GPUControlParam& control,
const ModelAuxBone* auxBone,
const ModelAuxTex* auxTex)
{
GLASSERT( model );
GLASSERT( atom );
Item* item = itemPool.PushArr(1);
item->state = state;
item->model = model;
item->atom = atom;
item->color = color;
item->boneFilter = filter;
item->control = control;
item->auxBone = auxBone;
item->auxTex = auxTex;
GLASSERT( itemPool.Size() < 10*1000 ); // sanity, infinite loop detection
}
void RenderQueue::Submit( int modelRequired,
int modelExcluded,
const Matrix4* xform )
{
//GRINLIZ_PERFTRACK
itemArr.Clear();
for( int i=0; i<itemPool.Size(); ++i ) {
Item* item = &itemPool[i];
Model* model = item->model;
int modelFlags = model->Flags();
if ( ( (modelRequired & modelFlags) == modelRequired)
&& ( (modelExcluded & modelFlags) == 0 ) )
{
itemArr.Push( item );
}
}
Sort( itemArr.Mem(), itemArr.Size(), CompareItem::Less );
int start = 0;
int end = 0;
GPUStream stream;
GPUStreamData data;
Matrix4 instanceMatrix[EL_MAX_INSTANCE];
Vector4F instanceColorParam[EL_MAX_INSTANCE];
Vector4F instanceBoneFilter[EL_MAX_INSTANCE];
GPUControlParam instanceControlParam[EL_MAX_INSTANCE];
Vector4F instanceTexture0XForm[EL_MAX_INSTANCE];
Vector4F instanceTexture0Clip[EL_MAX_INSTANCE];
Matrix4 instanceTexture0ColorMap[EL_MAX_INSTANCE];
Matrix4 instanceBone[EL_MAX_INSTANCE*EL_MAX_BONES];
//GLOUTPUT(( "Batch:\n" ));
while( start < itemArr.Size() ) {
// Get a range;
end = start + 1;
while( end < itemArr.Size()
&& itemArr[end]->IsInstance( itemArr[start] ) )
{
++end;
}
//int delta = end - start;
const ModelAtom* atom = itemArr[start]->atom;
GPUState* state = &itemArr[start]->state;
//GLOUTPUT(( " n=%d state=%d shader=%d texture=%s atom=%x\n",
// delta, state->StateFlags(), state->ShaderFlags(),
// atom->texture->Name(), atom ));
// The 2 paths is a PITA, both
// for startup time and debugging. If instancing
// in use, always instance.
{
atom->Bind( &stream, &data );
data.matrix = instanceMatrix;
data.colorParam = instanceColorParam;
data.boneFilter = instanceBoneFilter;
data.controlParam = instanceControlParam;
data.texture0XForm = instanceTexture0XForm;
data.texture0Clip = instanceTexture0Clip;
data.texture0ColorMap = instanceTexture0ColorMap;
data.bones = instanceBone;
GLASSERT( data.texture0 ); // not required, but not sure it works without
int k=start;
while( k < end ) {
int delta = Min( end-k, (int)EL_MAX_INSTANCE );
for( int index=0; index<delta; ++index ) {
const Item* item = itemArr[k+index];
if ( xform ) {
instanceMatrix[index] = (*xform) * item->model->XForm();
}
else {
instanceMatrix[index] = item->model->XForm();
}
instanceColorParam[index] = item->color;
instanceBoneFilter[index] = item->boneFilter;
instanceControlParam[index] = item->control;
if (item->auxBone) {
for (int i = 0; i < EL_MAX_BONES; ++i) {
instanceBone[index*EL_MAX_BONES + i] = item->auxBone->boneMats[i];
}
}
if (item->auxTex) {
instanceTexture0XForm[index] = item->auxTex->texture0XForm;
instanceTexture0Clip[index] = item->auxTex->texture0Clip;
instanceTexture0ColorMap[index] = item->auxTex->texture0ColorMap;
}
}
GPUDevice::Instance()->Draw( *state, stream, data, 0, atom->nIndex, delta );
k += delta;
}
}
start = end;
}
}