-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderer.cpp
226 lines (193 loc) · 6.79 KB
/
renderer.cpp
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
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2019-2024 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#include <assert.h>
#include <algorithm>
#include "renderer.hpp"
#include <nvpwindow.hpp>
#include "common.h"
#pragma pack(1)
namespace generatedcmds {
//////////////////////////////////////////////////////////////////////////
static void AddItem(std::vector<Renderer::DrawItem>& drawItems, const Renderer::Config& config, const Renderer::DrawItem& di)
{
if(di.range.count)
{
drawItems.push_back(di);
}
}
static void FillSingle(std::vector<Renderer::DrawItem>& drawItems,
const Renderer::Config& config,
const CadScene::Object& obj,
const CadScene::Geometry& geo,
bool solid,
int objectIndex)
{
int begin = 0;
const CadScene::DrawRangeCache& cache = solid ? obj.cacheSolid : obj.cacheWire;
if(obj.parts.empty())
return;
const CadScene::ObjectPart& part = obj.parts[0];
const CadScene::GeometryPart& mesh = geo.parts[0];
if(!part.active)
return;
// evict
Renderer::DrawItem di;
di.geometryIndex = obj.geometryIndex;
di.matrixIndex = part.matrixIndex;
di.materialIndex = part.materialIndex;
di.shaderIndex = part.materialIndex % config.maxShaders;
di.solid = solid;
di.range.offset = solid ? 0 : geo.numIndexSolid * sizeof(unsigned int);
di.range.count = solid ? geo.numIndexSolid : geo.numIndexWire;
AddItem(drawItems, config, di);
}
static void FillCache(std::vector<Renderer::DrawItem>& drawItems,
const Renderer::Config& config,
const CadScene::Object& obj,
const CadScene::Geometry& geo,
bool solid,
int objectIndex)
{
int begin = 0;
const CadScene::DrawRangeCache& cache = solid ? obj.cacheSolid : obj.cacheWire;
for(size_t s = 0; s < cache.state.size(); s++)
{
const CadScene::DrawStateInfo& state = cache.state[s];
for(int d = 0; d < cache.stateCount[s]; d++)
{
// evict
Renderer::DrawItem di;
di.geometryIndex = obj.geometryIndex;
di.matrixIndex = state.matrixIndex;
di.materialIndex = state.materialIndex;
di.shaderIndex = state.materialIndex % config.maxShaders;
di.solid = solid;
di.range.offset = cache.offsets[begin + d];
di.range.count = cache.counts[begin + d];
AddItem(drawItems, config, di);
}
begin += cache.stateCount[s];
}
}
static void FillIndividual(std::vector<Renderer::DrawItem>& drawItems,
const Renderer::Config& config,
const CadScene::Object& obj,
const CadScene::Geometry& geo,
bool solid,
int objectIndex)
{
for(size_t p = 0; p < obj.parts.size(); p++)
{
const CadScene::ObjectPart& part = obj.parts[p];
const CadScene::GeometryPart& mesh = geo.parts[p];
if(!part.active)
continue;
Renderer::DrawItem di;
di.geometryIndex = obj.geometryIndex;
di.matrixIndex = part.matrixIndex;
di.materialIndex = part.materialIndex;
di.shaderIndex = part.materialIndex % config.maxShaders;
di.solid = solid;
di.range = solid ? mesh.indexSolid : mesh.indexWire;
AddItem(drawItems, config, di);
}
}
void Renderer::fillDrawItems(std::vector<DrawItem>& drawItems, const CadScene* scene, const Config& config, Stats& stats)
{
bool solid = true;
bool wire = false;
size_t maxObjects = scene->m_objects.size();
size_t from = std::min(maxObjects - 1, size_t(config.objectFrom));
maxObjects = std::min(maxObjects, from + size_t(config.objectNum));
for(size_t i = from; i < maxObjects; i++)
{
const CadScene::Object& obj = scene->m_objects[i];
const CadScene::Geometry& geo = scene->m_geometry[obj.geometryIndex];
if(config.strategy == STRATEGY_SINGLE)
{
if(solid)
FillSingle(drawItems, config, obj, geo, true, int(i));
if(wire)
FillSingle(drawItems, config, obj, geo, false, int(i));
}
else if(config.strategy == STRATEGY_GROUPS)
{
if(solid)
FillCache(drawItems, config, obj, geo, true, int(i));
if(wire)
FillCache(drawItems, config, obj, geo, false, int(i));
}
else if(config.strategy == STRATEGY_INDIVIDUAL)
{
if(solid)
FillIndividual(drawItems, config, obj, geo, true, int(i));
if(wire)
FillIndividual(drawItems, config, obj, geo, false, int(i));
}
}
if(config.sorted && !config.permutated)
{
std::sort(drawItems.begin(), drawItems.end(), DrawItem_compare_groups);
}
int shaderIndex = -1;
for(size_t i = 0; i < drawItems.size(); i++)
{
stats.drawCalls++;
stats.drawTriangles += drawItems[i].range.count / 3;
if(drawItems[i].shaderIndex != shaderIndex)
{
stats.shaderBindings++;
shaderIndex = drawItems[i].shaderIndex;
}
}
}
void Renderer::fillRandomPermutation(uint32_t drawCount, uint32_t* permutation, const DrawItem* drawItems, Stats& stats)
{
srand(634523);
for(uint32_t i = 0; i < drawCount; i++)
{
permutation[i] = i;
}
if(drawCount)
{
// not exactly a good way to generate random 32bit ;)
for(uint32_t i = drawCount - 1; i > 0; i--)
{
uint32_t r = 0;
r |= (rand() & 0xFF) << 0;
r |= (rand() & 0xFF) << 8;
r |= (rand() & 0xFF) << 16;
r |= (rand() & 0xFF) << 24;
uint32_t other = r % (i + 1);
std::swap(permutation[i], permutation[other]);
}
int shaderIndex = -1;
stats.shaderBindings = 0;
for(uint32_t i = 0; i < drawCount; i++)
{
uint32_t idx = permutation[i];
if(drawItems[idx].shaderIndex != shaderIndex)
{
stats.shaderBindings++;
shaderIndex = drawItems[idx].shaderIndex;
}
}
}
}
} // namespace generatedcmds