forked from nvpro-samples/gl_vk_chopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvulkansandbox.cpp
291 lines (216 loc) · 6.68 KB
/
vulkansandbox.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
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
/*
* Copyright (c) 2014-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) 2014-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
/* Contact chebert@nvidia.com (Chris Hebert) for feedback */
#define DEBUG_FILTER 1
#include <include_gl.h>
#include <nvmath/nvmath_glsltypes.h>
#include <nvh/cameracontrol.hpp>
#include <nvh/geometry.hpp>
#include <nvh/misc.hpp>
#include <nvgl/appwindowprofiler_gl.hpp>
#include <nvgl/base_gl.hpp>
#include <nvgl/error_gl.hpp>
#include <iostream>
#include <stdint.h>
#include "VulkanAppContext.h"
#include "VulkanDeviceContext.h"
using namespace nvh;
using namespace nvgl;
using namespace nvmath;
extern bool vulkanInitLibrary();
namespace pathclipping {
int const SAMPLE_SIZE_WIDTH(1024);
int const SAMPLE_SIZE_HEIGHT(768);
int const PERLIN_GRID_U_SIZE(128);
int const PERLIN_GRID_V_SIZE(128);
class Sample;
class Sample : public nvgl::AppWindowProfilerGL
{
CameraControl m_control;
void end()
{
// TwTerminate();
}
bool mouse_pos(int x, int y)
{
return false; // !!TwEventMousePosGLFW(x, y);
}
bool mouse_button(int button, int action)
{
return false; // !!TwEventMouseButtonGLFW(button, action);
}
bool mouse_wheel(int wheel)
{
return false; // !!TwEventMouseWheelGLFW(wheel);
}
bool key_button(int button, int action, int mods)
{
return false; // handleTwKeyPressed(button, action, mods);
}
struct Tweak
{
Tweak() {}
int samples = 1;
int cmdBufferMode = 0;
int curFile = 0;
bool drawClips = false;
bool drawReflections = true;
bool drawShadows = false;
bool playAnimation = true;
};
Tweak tweak;
Tweak tweakLast;
struct
{
GLuint scene = 0;
} fbos;
struct
{
GLuint scene_color = 0;
GLuint scene_depthstencil = 0;
} textures;
bool begin();
void think(double time);
void resize(int width, int height);
bool initProgram();
bool initVulkan();
bool initScene();
bool initMisc();
bool initFramebuffers(int width, int height, int samples);
public:
};
bool Sample::initProgram()
{
//No programs used for this demo.
/*
Programs for Gazelle are managed by the App Context.
Just return true here.
*/
return true;
}
bool Sample::initVulkan()
{
bool vulkanReady = true; // vulkanInitLibrary();
/*
Get and initialise the VulkanAppContext.
*/
VulkanAppContext* ctxt = VulkanAppContext::GetInstance();
ctxt->initAppContext();
/*
Load the programs
*/
bool progsValid = ctxt->initPrograms();
if(!progsValid)
return false;
/*
Initialise the renderer.
*/
ctxt->initRenderer();
return vulkanReady;
}
bool Sample::initMisc()
{
return true;
}
bool Sample::initScene()
{
return true;
}
bool Sample::initFramebuffers(int width, int height, int samples)
{
if(samples > 1)
{
newTexture(textures.scene_color, GL_TEXTURE_2D_MULTISAMPLE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures.scene_color);
glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA8, width, height, GL_FALSE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
newTexture(textures.scene_depthstencil, GL_TEXTURE_2D_MULTISAMPLE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures.scene_depthstencil);
glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_DEPTH24_STENCIL8, width, height, GL_FALSE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
}
else
{
newTexture(textures.scene_color, GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures.scene_color);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, width, height);
glBindTexture(GL_TEXTURE_2D, 0);
newTexture(textures.scene_depthstencil, GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures.scene_depthstencil);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH24_STENCIL8, width, height);
glBindTexture(GL_TEXTURE_2D, 0);
}
newFramebuffer(fbos.scene);
glBindFramebuffer(GL_FRAMEBUFFER, fbos.scene);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textures.scene_color, 0);
// glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, textures.scene_depthstencil, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return true;
}
bool Sample::begin()
{
// TwInit(TW_OPENGL_CORE,NULL);
// TwWindowSize(m_windowState.m_swapSize[0], m_windowState.m_swapSize[1]);
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
bool validated(true);
validated = validated && initMisc();
validated = validated && initScene();
validated = validated && initFramebuffers(m_windowState.m_swapSize[0], m_windowState.m_swapSize[1], 8);
initVulkan();
m_control.m_sceneOrbit = vec3(0.0f);
m_control.m_sceneDimension = float(1.0);
nvmath::vec3f scenePos = nvmath::vec3f(20.0, -1.0, 8.0);
m_control.m_viewMatrix = nvmath::look_at(m_control.m_sceneOrbit - (scenePos * m_control.m_sceneDimension * 0.5f),
m_control.m_sceneOrbit, vec3(0, 1, 0));
return validated;
}
void Sample::think(double time)
{
m_control.processActions(m_windowState.m_swapSize,
nvmath::vec2f(m_windowState.m_mouseCurrent[0], m_windowState.m_mouseCurrent[1]),
m_windowState.m_mouseButtonFlags, m_windowState.m_mouseWheel);
VulkanAppContext* ctxt = VulkanAppContext::GetInstance();
ctxt->setCameraMatrix(m_control.m_viewMatrix);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ctxt->render();
}
void Sample::resize(int width, int height)
{
VulkanDC* dc = VulkanDC::Get();
VulkanDC::Device* device = dc->getDefaultDevice();
VulkanAppContext* ctxt = VulkanAppContext::GetInstance();
device->waitIdle();
initFramebuffers(width, height, 1);
ctxt->render();
device->waitIdle();
ctxt->resize(width, height);
device->waitIdle();
ctxt->render();
}
} // namespace pathclipping
using namespace pathclipping;
int main(int argc, const char** argv)
{
NVPSystem system(PROJECT_NAME);
Sample sample;
return sample.run(PROJECT_NAME, argc, argv, SAMPLE_SIZE_WIDTH, SAMPLE_SIZE_HEIGHT);
}