forked from nvpro-samples/gl_vk_chopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vukansandbox.cpp
334 lines (246 loc) · 8.17 KB
/
vukansandbox.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*-----------------------------------------------------------------------
Copyright (c) 2014-2016, NVIDIA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Neither the name of its contributors may be used to endorse
or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------*/
/* Contact chebert@nvidia.com (Chris Hebert) for feedback */
#define DEBUG_FILTER 1
#include <include_gl.h>
#include <nv_math/nv_math_glsltypes.h>
#include <nv_helpers/geometry.hpp>
#include <nv_helpers/misc.hpp>
#include <nv_helpers/cameracontrol.hpp>
#include <nv_helpers/tnulled.hpp>
#include <nv_helpers_gl/appwindowprofiler_gl.hpp>
#include <nv_helpers_gl/error_gl.hpp>
#include <nv_helpers_gl/programmanager_gl.hpp>
#include <nv_helpers_gl/base_gl.hpp>
#include <noise/mersennetwister1.h>
#include <stdint.h>
#include<iostream>
#include"VulkanDeviceContext.h"
#include"VulkanAppContext.h"
using namespace nv_helpers;
using namespace nv_helpers_gl;
using namespace nv_math;
extern bool vulkanInitLibrary();
namespace pathclipping
{
int const SAMPLE_SIZE_WIDTH(1024);
int const SAMPLE_SIZE_HEIGHT(768);
int const SAMPLE_MAJOR_VERSION(4);
int const SAMPLE_MINOR_VERSION(5);
int const PERLIN_GRID_U_SIZE(128);
int const PERLIN_GRID_V_SIZE(128);
class Sample;
class Sample : public nv_helpers_gl::AppWindowProfilerGL
{
ProgramManager progManager;
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():
samples(1),
curFile(0),
cmdBufferMode(0),
drawReflections(true),
drawShadows(false),
playAnimation(true),
drawClips(false){}
int samples;
int cmdBufferMode;
int curFile;
bool drawClips;
bool drawReflections;
bool drawShadows;
bool playAnimation;
};
Tweak tweak;
Tweak tweakLast;
struct {
GLuint
scene;
} fbos;
struct {
GLuint
scene_color,
scene_depthstencil;
} textures;
struct {
ProgramManager::ProgramID
draw_quad;
} programs;
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.
bool validated(true);
/*
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(progManager);
/*
Initialise the renderer.
*/
ctxt->initRenderer(progManager);
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_window.m_viewsize[0], m_window.m_viewsize[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_window.m_viewsize[0],m_window.m_viewsize[1],8);
initVulkan();
m_control.m_sceneOrbit = vec3(0.0f);
m_control.m_sceneDimension = float(1.0);
nv_math::vec3f scenePos = nv_math::vec3f(20.0, -1.0, 8.0);
m_control.m_viewMatrix = nv_math::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_window.m_viewsize, nv_math::vec2f(m_window.m_mouseCurrent[0], m_window.m_mouseCurrent[1]), m_window.m_mouseButtonFlags, m_window.m_wheel);
if (m_window.onPress(KEY_R)){
progManager.reloadPrograms();
}
if (!progManager.areProgramsValid()){
waitEvents();
return;
}
int width = m_window.m_viewsize[0];
int height = m_window.m_viewsize[1];
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);
{
NV_PROFILE_SECTION("Scene");
// glViewport(0, 0, width, height);
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();
}
}
using namespace pathclipping;
int sample_main(int argc, const char** argv)
{
SETLOGFILENAME();
Sample sample;
return sample.run(
PROJECT_NAME,
argc, argv,
SAMPLE_SIZE_WIDTH, SAMPLE_SIZE_HEIGHT,
SAMPLE_MAJOR_VERSION, SAMPLE_MINOR_VERSION);
}
void sample_print(int level, const char * fmt)
{
}