-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgl_vk_bk3dthreaded.h
214 lines (177 loc) · 6.7 KB
/
gl_vk_bk3dthreaded.h
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
/*
* Copyright (c) 2016-2021, 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) 2016-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#define USEWORKERS
#define MAXCMDBUFFERS 100
#ifdef USEWORKERS
#define CCRITICALSECTIONHOLDER(c) CCriticalSectionHolder _cs(c);
#else
#define CCRITICALSECTIONHOLDER(c)
#endif
#include <assert.h>
#include "nvpwindow.hpp"
#include <glm/glm.hpp>
using namespace glm;
#include "GLSLShader.h"
#include "gl_nv_command_list.h"
#include <nvh/profiler.hpp>
#include <nvh/appwindowcamerainertia.hpp>
#include "helper_fbo.h"
#ifdef NVP_SUPPORTS_GZLIB
#include "zlib.h"
#endif
#include "bk3dEx.h" // a baked binary format for few models
#define PROFILE_SECTION(name) nvh::Profiler::Section _tempTimer(g_profiler, name)
//
// For the case where we work with Descriptor Sets (Vulkan)
//
#define DSET_GLOBAL 0
#define BINDING_MATRIX 0
#define BINDING_LIGHT 1
#define BINDING_NOISE 2
#define DSET_OBJECT 1
#define BINDING_MATRIXOBJ 0
#define BINDING_MATERIAL 1
#define DSET_TOTALAMOUNT 2
//
// For the case where we just assign UBO bindings (cmd-list)
//
#define UBO_MATRIX 0
#define UBO_MATRIXOBJ 1
#define UBO_MATERIAL 2
#define UBO_LIGHT 3
#define NUM_UBOS 4
#define TOSTR_(x) #x
#define TOSTR(x) TOSTR_(x)
//
// Let's assume we would put any matrix that don't get impacted by the local object transformation
//
NV_ALIGN(
256,
struct MatrixBufferGlobal {
mat4 mW;
mat4 mVP;
vec3 eyePos;
});
//
// Let's assume these are the ones that can change for each object
// will used at an array of MatrixBufferObject
//
NV_ALIGN(
256,
struct MatrixBufferObject { mat4 mO; });
//
// if we create arrays with a structure, we must be aligned according to
// GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT (to query)
//
NV_ALIGN(
256,
struct MaterialBuffer {
vec3 diffuse;
float a;
});
NV_ALIGN(
256,
struct LightBuffer { vec3 dir; });
//
// Externs
//
extern nvh::Profiler g_profiler;
extern bool g_bDisplayObject;
extern GLuint g_MaxBOSz;
extern bool g_bDisplayGrid;
extern MatrixBufferGlobal g_globalMatrices;
//------------------------------------------------------------------------------
class Bk3dModel;
//------------------------------------------------------------------------------
// Renderer: can be OpenGL or other
//------------------------------------------------------------------------------
class Renderer
{
public:
Renderer() {}
virtual ~Renderer() {}
virtual const char* getName() = 0;
virtual bool valid() = 0;
virtual bool initGraphics(int w, int h, int MSAA) = 0;
virtual bool terminateGraphics() = 0;
virtual bool initThreadLocalVars(int threadId) = 0;
virtual void releaseThreadLocalVars() = 0;
virtual void destroyCommandBuffers(bool bAll) = 0;
virtual void resetCommandBuffersPool() {}
virtual void waitForGPUIdle() = 0;
virtual bool attachModel(Bk3dModel* pModel) = 0;
virtual bool detachModels() = 0;
virtual bool initResourcesModel(Bk3dModel* pModel) = 0;
virtual bool buildPrimaryCmdBuffer() = 0;
// bufIdx: index of cmdBuffer to create, containing mesh mstart to mend-1 (for testing concurrent cmd buffer creation)
virtual bool buildCmdBufferModel(Bk3dModel* pModelcmd, int bufIdx = 0, int mstart = 0, int mend = -1) = 0;
virtual void consolidateCmdBuffersModel(Bk3dModel* pModelcmd, int numCmdBuffers) = 0;
virtual bool deleteCmdBufferModel(Bk3dModel* pModel) = 0;
virtual bool updateForChangedRenderTarget(Bk3dModel* pModel) = 0;
virtual void displayStart(const mat4& world, const InertiaCamera& camera, const mat4& projection, bool bTimingGlitch) = 0;
virtual void displayEnd() {}
virtual void displayGrid(const InertiaCamera& camera, const mat4 projection) = 0;
// topologies: bits for each primitive type (Lines:1, linestrip:2, triangles:4, tristrips:8, trifans:16)
virtual void displayBk3dModel(Bk3dModel* pModel, const mat4& cameraView, const mat4 projection, unsigned char topologies = 0xFF) = 0;
virtual void blitToBackbuffer() = 0;
virtual void updateViewport(GLint x, GLint y, GLsizei width, GLsizei height) = 0;
virtual bool bFlipViewport() { return false; }
};
extern Renderer* g_renderers[10];
extern int g_numRenderers;
//------------------------------------------------------------------------------
// Class for Object (made of 1 to N meshes)
// This class is agnostic to any renderer: just contains the data of geometry
//------------------------------------------------------------------------------
class Bk3dModel
{
public:
Bk3dModel(const char* name, vec3* pPos = NULL, float* pScale = NULL);
~Bk3dModel();
vec3 m_posOffset;
float m_scale;
std::string m_name;
struct Stats
{
unsigned int primitives;
unsigned int drawcalls;
unsigned int attr_update;
unsigned int uniform_update;
};
MatrixBufferObject* m_objectMatrices;
int m_objectMatricesNItems;
MaterialBuffer* m_material;
int m_materialNItems;
bk3d::FileHeader* m_meshFile;
Stats m_stats;
Renderer* m_pRenderer;
void* m_pRendererData;
bool updateForChangedRenderTarget();
bool loadModel();
void printPosition();
void addStats(Stats& stats);
}; //Class Bk3dModel
extern std::vector<Bk3dModel*> g_bk3dModels;
#define FOREACHMODEL(cmd) \
{ \
for(int m = 0; m < g_bk3dModels.size(); m++) \
{ \
g_bk3dModels[m]->cmd; \
} \
}