-
Notifications
You must be signed in to change notification settings - Fork 17
/
NVFBOBoxVK.cpp
774 lines (725 loc) · 29.8 KB
/
NVFBOBoxVK.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
* 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
*/
//
// Implementation of different antialiasing methods through FBOs
// - typical MSAA
// - CSAA
// - Hardware AA mixed with FBO for supersampling pass
// - simple downsampling
// - downsampling with 1 or 2 kernel filters
//
// NVFBOBox is the class that will handle everything related to supersampling through
// an offscreen surface defined thanks to FBO
//
//--------------------------------------------------------------------------------------
#define EXTERNSVCUI
#define WINDOWINERTIACAMERA_EXTERN
#if defined(_MSC_VER)
# include <windows.h>
# include <direct.h>
#endif
#include <assert.h>
#include <float.h>
#include <math.h>
#include <string>
#include <vector>
#include "nvh/nvprint.hpp"
#include <glm/glm.hpp>
using namespace glm;
// declared in renderer_vulkan.cpp
namespace vk {
extern bool load_binary(const std::string &name, std::string &data);
}
std::string fsArray[3];
std::string vsPassthrough;
#ifdef USE_UNMANAGED
# pragma managed(push,off)
#endif
///////////////////////////////////////////////////////////////////////////////
// VULKAN: NVK.h > vkfnptrinline.h > vulkannv.h > vulkan.h
//
#include "NVK.h"
#include "NVFBOBoxVK.h"
#include <map>
/////////////////////////////////////////////
//
// Methods
//
/////////////////////////////////////////////
NVFBOBoxVK::NVFBOBoxVK() :
bOneFBOPerTile(false),
scaleFactor(1.0),
depthSamples(0), coverageSamples(0),
bValid(false),
vpx(0), vpy(0), vpw(0), vph(0),
bCSAA(false),
bufw(0), bufh(0),
width(0), height(0),
curtilex(0), curtiley(0),
pngData(NULL),
pngDataTile(NULL),
pngDataSz(0)
{
}
NVFBOBoxVK::~NVFBOBoxVK()
{
pngDataSz = 0;
if(pngData)
delete []pngData;
pngData = NULL;
if(pngDataTile)
delete []pngDataTile;
pngDataTile = NULL;
}
void NVFBOBoxVK::Finish()
{
pngDataSz = 0;
if(pngData)
delete []pngData;
pngData = NULL;
if(pngDataTile)
delete []pngDataTile;
pngDataTile = NULL;
//
// Free Vulkan resources
//
deleteRenderPass();
deleteFramebufferAndRelated();
if(m_sampler)
m_pnvk->destroySampler(m_sampler);
m_sampler = NULL;
if(m_cmdPool)
m_cmdPool.destroyCommandPool();
if(m_descriptorSetLayout)
vkDestroyDescriptorSetLayout(m_pnvk->m_device, m_descriptorSetLayout, NULL); // general layout and objects layout
m_descriptorSetLayout = 0;
// Not calling it because VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT not set at creation time
//if(m_descriptorSet)
// vkFreeDescriptorSets(m_pnvk->m_device, m_descPool, 1, &m_descriptorSet); // no really necessary: we will destroy the pool after that
m_descriptorSet = NULL;
if(m_descPool)
vkDestroyDescriptorPool(m_pnvk->m_device, m_descPool, NULL);
m_descPool = NULL;
if(m_pipelineLayout)
vkDestroyPipelineLayout(m_pnvk->m_device, m_pipelineLayout, NULL);
m_pipelineLayout = NULL;
release(m_texInfo);
release(m_quadBuffer);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBoxVK::deleteRenderPass()
{
if (m_scenePass)
vkDestroyRenderPass(m_pnvk->m_device, m_scenePass, NULL);
m_scenePass = NULL;
if (m_downsamplePass)
vkDestroyRenderPass(m_pnvk->m_device, m_downsamplePass, NULL);
m_downsamplePass = NULL;
for (int i = 0; i<3; i++)
{
if(m_pipelines[i])
m_pnvk->destroyPipeline(m_pipelines[i], NULL);
m_pipelines[i] = NULL;
}
return true;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBoxVK::initRenderPass()
{
deleteRenderPass();
bool multisample = depthSamples > 1;
//
// Create the render passes for the scene-render
//
NVK::AttachmentReference color(0/*attachment*/, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL/*layout*/);
NVK::AttachmentReference dst(1/*attachment*/, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL/*layout*/);
NVK::AttachmentReference colorResolved(2/*attachment*/, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL/*layout*/);
NVK::RenderPassCreateInfo rpinfo;
if (multisample)
{
//
// Multisample case: have a color buffer as the resolve-target
//
rpinfo = NVK::RenderPassCreateInfo(
NVK::AttachmentDescription
(VK_FORMAT_R8G8B8A8_UNORM, (VkSampleCountFlagBits)depthSamples, //format, samples
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE, //loadOp, storeOp
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, //stencilLoadOp, stencilStoreOp
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL //initialLayout, finalLayout
)
(VK_FORMAT_D24_UNORM_S8_UINT, (VkSampleCountFlagBits)depthSamples,
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE,
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE,
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
)
(VK_FORMAT_R8G8B8A8_UNORM, (VkSampleCountFlagBits)1, //format, samples
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE, //loadOp, storeOp
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, //stencilLoadOp, stencilStoreOp
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL //initialLayout, finalLayout
),
NVK::SubpassDescription
(VK_PIPELINE_BIND_POINT_GRAPHICS,//pipelineBindPoint
NULL, //inputAttachments
&color, //colorAttachments
&colorResolved, //resolveAttachments
&dst, //depthStencilAttachment
NULL, //preserveAttachments
0 //flags
),
NVK::SubpassDependency(/*NONE*/)
);
}
else {
//
// NON-Multisample case: no need for intermediate resolve target
//
rpinfo = NVK::RenderPassCreateInfo(
NVK::AttachmentDescription
(VK_FORMAT_R8G8B8A8_UNORM, VK_SAMPLE_COUNT_1_BIT, //format, samples
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE, //loadOp, storeOp
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, //stencilLoadOp, stencilStoreOp
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL //initialLayout, finalLayout
)
(VK_FORMAT_D24_UNORM_S8_UINT, VK_SAMPLE_COUNT_1_BIT,
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE,
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE,
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
),
NVK::SubpassDescription
(VK_PIPELINE_BIND_POINT_GRAPHICS,//pipelineBindPoint
NULL, //inputAttachments
&color, //colorAttachments
NULL, //resolveAttachments
&dst, //depthStencilAttachment
NULL, //preserveAttachments
0 //flags
),
NVK::SubpassDependency(/*NONE*/)
);
}
m_scenePass = m_pnvk->createRenderPass(rpinfo);
//
// Create the render pass for downsampling step: just a color buffer
//
rpinfo = NVK::RenderPassCreateInfo(
NVK::AttachmentDescription
(VK_FORMAT_R8G8B8A8_UNORM, VK_SAMPLE_COUNT_1_BIT, //format, samples
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE, //loadOp, storeOp
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, //stencilLoadOp, stencilStoreOp
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL //initialLayout, finalLayout
),
NVK::SubpassDescription
(VK_PIPELINE_BIND_POINT_GRAPHICS,//pipelineBindPoint
NULL, //inputAttachments
&color, //colorAttachments
NULL, //resolveAttachments
NULL, //depthStencilAttachment
NULL, //preserveAttachments
0 //flags
),
NVK::SubpassDependency(/*NONE*/)
);
m_downsamplePass = m_pnvk->createRenderPass(rpinfo);
NVK::PipelineViewportStateCreateInfo vkPipelineViewportStateCreateInfo(
NVK::Viewport(0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f),
NVK::Rect2DArray(0.0f, 0.0f, (float)width, (float)height)
);
//
// GRID gfx pipelines
//
for(int i=0; i<3; i++)
{
m_pipelines[i] = m_pnvk->createGraphicsPipeline(NVK::GraphicsPipelineCreateInfo
(m_pipelineLayout, m_downsamplePass,/*subpass*/0,/*basePipelineHandle*/0,/*basePipelineIndex*/0,/*flags*/0)
(NVK::PipelineVertexInputStateCreateInfo(
NVK::VertexInputBindingDescription (0/*binding*/, 2*sizeof(glm::vec3)/*stride*/, VK_VERTEX_INPUT_RATE_VERTEX),
NVK::VertexInputAttributeDescription (0/*location*/, 0/*binding*/, VK_FORMAT_R32G32B32_SFLOAT, 0 /*offset*/ ) // pos
(1/*location*/, 0/*binding*/, VK_FORMAT_R32G32B32_SFLOAT, sizeof(glm::vec3)/*offset*/ )
) )
(NVK::PipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN, VK_FALSE/*primitiveRestartEnable*/) )
(NVK::PipelineShaderStageCreateInfo(VK_SHADER_STAGE_VERTEX_BIT, m_pnvk->createShaderModule(vsPassthrough.c_str(), vsPassthrough.size() ), "main") )
(vkPipelineViewportStateCreateInfo)
(m_vkPipelineRasterStateCreateInfo)
(m_vkPipelineMultisampleStateCreateInfo)
(NVK::PipelineShaderStageCreateInfo(VK_SHADER_STAGE_FRAGMENT_BIT, m_pnvk->createShaderModule(fsArray[i].c_str(), fsArray[i].size() ), "main") )
(m_vkPipelineColorBlendStateCreateInfo)
(m_vkPipelineDepthStencilStateCreateInfo)
(NVK::PipelineDynamicStateCreateInfo(NVK::DynamicState
(VK_DYNAMIC_STATE_VIEWPORT)(VK_DYNAMIC_STATE_SCISSOR) ) )
);
}
return true;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBoxVK::deleteFramebufferAndRelated()
{
//
//loop in tiles
//
for(unsigned int i=0; i<m_tileData.size(); i++)
{
if(m_tileData[i].FBSS)
vkDestroyFramebuffer(m_pnvk->m_device, m_tileData[i].FBSS, NULL);
m_tileData[i].FBSS = NULL;
if(m_tileData[i].FBDS)
vkDestroyFramebuffer(m_pnvk->m_device, m_tileData[i].FBDS, NULL);
m_tileData[i].FBDS = NULL;
if(m_tileData[i].color_texture_DS.img)
release(m_tileData[i].color_texture_DS);
if(m_tileData[i].color_texture_SS.img)
release(m_tileData[i].color_texture_SS);
if(m_tileData[i].color_texture_SSMS.img)
release(m_tileData[i].color_texture_SSMS);
}
if(m_depth_texture_SSMS.img)
release(m_depth_texture_SSMS);
if(m_depth_texture_SS.img)
release(m_depth_texture_SS);
for(int i=0; i<3; i++)
{
if(m_cmdDownsample[i])
m_cmdPool.utFreeCommandBuffer(m_cmdDownsample[i]);
m_cmdDownsample[i] = NULL;
}
return true;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBoxVK::initFramebufferAndRelated()
{
deleteFramebufferAndRelated();
bool multisample = depthSamples > 1;
bool csaa = false;
bool ret = true;
if(bOneFBOPerTile)
m_tileData.resize(tilesw*tilesh);
else
m_tileData.resize(1);
//
//loop in tiles
//
for(unsigned int i=0; i<m_tileData.size(); i++)
{
//
// init the texture that will also be the buffer to render to
//
m_tileData[i].color_texture_SS.img = m_pnvk->utCreateImage2D(bufw, bufh, m_tileData[i].color_texture_SS.imgMem, VK_FORMAT_R8G8B8A8_UNORM);
m_tileData[i].color_texture_SS.imgView = m_pnvk->createImageView(NVK::ImageViewCreateInfo(
m_tileData[i].color_texture_SS.img, // image
VK_IMAGE_VIEW_TYPE_2D, //viewType
VK_FORMAT_R8G8B8A8_UNORM, //format
NVK::ComponentMapping(),//channels
NVK::ImageSubresourceRange()//subresourceRange
) );
//
// Handle multisample FBO's first
//
if (multisample)
{
// initialize color texture
m_tileData[i].color_texture_SSMS.img = m_pnvk->utCreateImage2D(bufw, bufh, m_tileData[i].color_texture_SSMS.imgMem, VK_FORMAT_R8G8B8A8_UNORM, (VkSampleCountFlagBits)depthSamples, (VkSampleCountFlagBits)coverageSamples);
m_tileData[i].color_texture_SSMS.imgView = m_pnvk->createImageView(NVK::ImageViewCreateInfo(
m_tileData[i].color_texture_SSMS.img, // image
VK_IMAGE_VIEW_TYPE_2D, //viewType
VK_FORMAT_R8G8B8A8_UNORM, //format
NVK::ComponentMapping(),//channels
NVK::ImageSubresourceRange()//subresourceRange
) );
// bind the multisampled depth buffer
if(m_depth_texture_SSMS.img == 0)
{
m_depth_texture_SSMS.img = m_pnvk->utCreateImage2D(bufw, bufh, m_depth_texture_SSMS.imgMem, VK_FORMAT_D24_UNORM_S8_UINT, (VkSampleCountFlagBits)depthSamples, (VkSampleCountFlagBits)(bCSAA ? coverageSamples:0));
m_depth_texture_SSMS.imgView = m_pnvk->createImageView(NVK::ImageViewCreateInfo(
m_depth_texture_SSMS.img, // image
VK_IMAGE_VIEW_TYPE_2D, //viewType
VK_FORMAT_D24_UNORM_S8_UINT, //format
NVK::ComponentMapping(),//channels
NVK::ImageSubresourceRange(VK_IMAGE_ASPECT_DEPTH_BIT|VK_IMAGE_ASPECT_STENCIL_BIT)//subresourceRange
) );
}
//
// create the framebuffer
//
m_tileData[i].FBSS = m_pnvk->createFramebuffer(
NVK::FramebufferCreateInfo
( m_scenePass, //renderPass
bufw, bufh, 1, //w, h, Layers
(m_tileData[i].color_texture_SSMS.imgView) ) // first VkImageView
(m_depth_texture_SSMS.imgView) // additional VkImageView via functor
(m_tileData[i].color_texture_SS.imgView)
);
} // if (multisample)
else // Depth buffer created without the need to resolve MSAA
{
// Create it one for many FBOs
if(m_depth_texture_SS.img == NULL)
{
m_depth_texture_SS.img = m_pnvk->utCreateImage2D(bufw, bufh, m_depth_texture_SS.imgMem, VK_FORMAT_D24_UNORM_S8_UINT);
m_depth_texture_SS.imgView = m_pnvk->createImageView(NVK::ImageViewCreateInfo(
m_depth_texture_SS.img, // image
VK_IMAGE_VIEW_TYPE_2D, //viewType
VK_FORMAT_D24_UNORM_S8_UINT, //format
NVK::ComponentMapping(),//channels
NVK::ImageSubresourceRange()//subresourceRange
) );
}
//
// create the framebuffer
//
m_tileData[i].FBSS = m_pnvk->createFramebuffer(
NVK::FramebufferCreateInfo
( m_scenePass, //renderPass
bufw, bufh, 1, //width, height, layers
(m_tileData[i].color_texture_SS.imgView) )
(m_depth_texture_SS.imgView)
);
}
//
// create the framebuffer for downsampling
//
m_tileData[i].color_texture_DS.img = m_pnvk->utCreateImage2D(width, height, m_tileData[i].color_texture_DS.imgMem, VK_FORMAT_R8G8B8A8_UNORM);
m_tileData[i].color_texture_DS.imgView = m_pnvk->createImageView(NVK::ImageViewCreateInfo(
m_tileData[i].color_texture_DS.img, // image
VK_IMAGE_VIEW_TYPE_2D, //viewType
VK_FORMAT_R8G8B8A8_UNORM, //format
NVK::ComponentMapping(),//channels
NVK::ImageSubresourceRange()//subresourceRange
) );
m_tileData[i].FBDS = m_pnvk->createFramebuffer(
NVK::FramebufferCreateInfo
( m_downsamplePass, //renderPass
width, height, 1, //width, height, layers
(m_tileData[i].color_texture_DS.imgView)
)
);
} // for i
//
// update the descriptorset used for Global
// later we will update the ones local to objects
//
NVK::DescriptorImageInfo bufferImageViews = NVK::DescriptorImageInfo
(m_sampler, m_tileData[0].color_texture_SS.imgView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); // texImage sampler
NVK::DescriptorBufferInfo descBuffer = NVK::DescriptorBufferInfo
(m_texInfo.buffer, 0, m_texInfo.Sz);
m_pnvk->updateDescriptorSets(NVK::WriteDescriptorSet
//(descSetDest, binding, arrayIndex, VkDescriptorImageInfo/BufferInfo, kDescriptorType)
(m_descriptorSet, 0, 0, bufferImageViews, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
(m_descriptorSet, 1, 0, descBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
);
//
// command buffer
// Warning: depends on the render-pass and frame-buffers
//
for(int i=0; i<3; i++)
{
if(m_cmdDownsample[i])
m_cmdPool.utFreeCommandBuffer(m_cmdDownsample[i]);
m_cmdDownsample[i] = m_cmdPool.utAllocateCommandBuffer(true);
{
m_cmdDownsample[i].beginCommandBuffer(false, NVK::CommandBufferInheritanceInfo(m_downsamplePass, 0, m_tileData[0].FBDS, 0/*occlusionQueryEnable*/, 0/*queryFlags*/, 0/*pipelineStatistics*/) );
VkRect2D viewRect = NVK::Rect2D(NVK::Offset2D(0,0), NVK::Extent2D(width, height));
float v2[2] = {1.0f/(float)bufw, 1.0f/(float)bufh };
vkCmdUpdateBuffer (m_cmdDownsample[i], m_texInfo.buffer, 0, sizeof(float)*2, (uint32_t*)&v2[0]);
vkCmdBeginRenderPass (m_cmdDownsample[i],
NVK::RenderPassBeginInfo(m_downsamplePass, m_tileData[0].FBDS, viewRect,
NVK::ClearValue(NVK::ClearColorValue(0.8f,0.2f,0.2f,0.0f)) ),
VK_SUBPASS_CONTENTS_INLINE );
vkCmdBindPipeline(m_cmdDownsample[i], VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelines[i]);
vkCmdSetViewport(m_cmdDownsample[i], 0, 1, NVK::Viewport(0,0,width, height, 0.0f, 1.0f) );
vkCmdSetScissor( m_cmdDownsample[i], 0, 1, NVK::Rect2D(0.0,0.0, width, height) );
VkDeviceSize vboffsets[1] = {0};
vkCmdBindVertexBuffers(m_cmdDownsample[i], 0, 1, &m_quadBuffer.buffer, vboffsets);
uint32_t offsets = 0;
vkCmdBindDescriptorSets(m_cmdDownsample[i], VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &m_descriptorSet, 0, &offsets);
vkCmdDraw(m_cmdDownsample[i], 4, 1, 0, 0);
vkCmdEndRenderPass(m_cmdDownsample[i]);
vkEndCommandBuffer(m_cmdDownsample[i]);
}
}
return ret;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBoxVK::resize(int w, int h, float ssfact)
{
if(ssfact >= 1.0)
scaleFactor = ssfact;
if(w > 0)
width = w;
if(h > 0)
height = h;
bufw = (int)(scaleFactor*(float)width);
bufh = (int)(scaleFactor*(float)height);
//
// resizing only require to reallocate resources :
//
return initFramebufferAndRelated();
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBoxVK::setMSAA(int depthSamples_, int coverageSamples_)
{
bool bRes = true;
if (depthSamples_ >= 0)
depthSamples = depthSamples_;
if (coverageSamples_ >= 0)
coverageSamples = coverageSamples_;
//
// New MSAA requires to re-create the render-passes
// New renderpasses requires re-creating render-targets
//
if (!initRenderPass() ) return false;
if (!initFramebufferAndRelated() ) return false;
return true;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBoxVK::Initialize(NVK &nvk, int w, int h, float ssfact, int depthSamples_, int coverageSamples_, int tilesW, int tilesH, bool bOneFBOPerTile_)
{
VkResult result;
Finish();
m_pnvk = &nvk;
if(depthSamples_ >= 0)
depthSamples = depthSamples_;
if(coverageSamples_ >= 0)
coverageSamples = coverageSamples_;
if(tilesW > 0)
tilesw = tilesW;
if(tilesH > 0)
tilesh = tilesH;
if(ssfact >= 1.0)
{
scaleFactor = ssfact;
}
bValid = false;
if(w > 0)
width = w;
if(h > 0)
height = h;
bufw = (int)(scaleFactor*(float)width);
bufh = (int)(scaleFactor*(float)height);
bOneFBOPerTile = bOneFBOPerTile_;
//
// other Vulkan stuff
//
//--------------------------------------------------------------------------
// Command pool
//
VkCommandPoolCreateInfo cmdPoolInfo = { VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
cmdPoolInfo.queueFamilyIndex = 0;
result = nvk.createCommandPool(&cmdPoolInfo, NULL, &m_cmdPool);
//--------------------------------------------------------------------------
// descriptor set
//
// descriptor layout for general things (projection matrix; view matrix...)
m_descriptorSetLayout = m_pnvk->createDescriptorSetLayout(
NVK::DescriptorSetLayoutCreateInfo(NVK::DescriptorSetLayoutBinding
//binding descriptorType, arraySize, stageFlags
(0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT)
(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_FRAGMENT_BIT)
(2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_FRAGMENT_BIT) )
);
//
// PipelineLayout
//
m_pipelineLayout = nvk.createPipelineLayout(&m_descriptorSetLayout, 1);
//
// Create a sampler
//
m_sampler = nvk.createSampler(NVK::SamplerCreateInfo(
VK_FILTER_LINEAR, //magFilter
VK_FILTER_LINEAR, //minFilter
VK_SAMPLER_MIPMAP_MODE_LINEAR, //mipMode
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, //addressU
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, //addressV
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, //addressW
0.0, //mipLodBias
VK_FALSE,
1, //maxAnisotropy
VK_FALSE, //compareEnable
VK_COMPARE_OP_NEVER, //compareOp
0.0, //minLod
16.0, //maxLod
VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, //borderColor
VK_FALSE
));
//--------------------------------------------------------------------------
// MSAA state
//
::VkSampleMask sampleMask = 0xFFFF;
m_vkPipelineMultisampleStateCreateInfo = NVK::PipelineMultisampleStateCreateInfo(
VK_SAMPLE_COUNT_1_BIT /*rasterSamples*/, VK_FALSE /*sampleShadingEnable*/, 1.0 /*minSampleShading*/, &sampleMask /*sampleMask*/, VK_FALSE, VK_FALSE);
//--------------------------------------------------------------------------
// Init 'pipelines'
//
m_vkPipelineRasterStateCreateInfo = NVK::PipelineRasterizationStateCreateInfo(
VK_TRUE, //depthClipEnable
VK_FALSE, //rasterizerDiscardEnable
VK_POLYGON_MODE_FILL, //VkPolygonMode
VK_CULL_MODE_NONE, //cullMode
VK_FRONT_FACE_COUNTER_CLOCKWISE, //frontFace
VK_FALSE, //depthBiasEnable
0.0, //depthBias
0.0, //depthBiasClamp
0.0, //slopeScaledDepthBias
1.0 //lineWidth
);
m_vkPipelineColorBlendStateCreateInfo = NVK::PipelineColorBlendStateCreateInfo(
VK_FALSE/*logicOpEnable*/,
VK_LOGIC_OP_NO_OP,
NVK::PipelineColorBlendAttachmentState(
VK_FALSE /*blendEnable*/,
VK_BLEND_FACTOR_ZERO /*srcColorBlendFactor*/,
VK_BLEND_FACTOR_ZERO /*dstColorBlendFactor*/,
VK_BLEND_OP_ADD /*colorBlendOp*/,
VK_BLEND_FACTOR_ZERO /*srcAlphaBlendFactor*/,
VK_BLEND_FACTOR_ZERO /*dstAlphaBlendFactor*/,
VK_BLEND_OP_ADD /*alphaBlendOp*/,
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT/*colorWriteMask*/),
NVK::Float4() //blendConst[4]
);
m_vkPipelineDepthStencilStateCreateInfo = NVK::PipelineDepthStencilStateCreateInfo(
VK_FALSE, //depthTestEnable
VK_FALSE, //depthWriteEnable
VK_COMPARE_OP_NEVER, //depthCompareOp
VK_FALSE, //depthBoundsTestEnable
VK_FALSE, //stencilTestEnable
NVK::StencilOpState(), //front
NVK::StencilOpState(), //back
0.0f, //minDepthBounds
1.0f //maxDepthBounds
);
//
// Load SpirV shaders
//
if (vk::load_binary(std::string("GLSL_passthrough_vert.spv"), vsPassthrough))
bValid = true;
if (vk::load_binary(std::string("GLSL_ds1_frag.spv"), fsArray[0]))
bValid = true;
if (vk::load_binary(std::string("GLSL_ds2_frag.spv"), fsArray[1]))
bValid = true;
if (vk::load_binary(std::string("GLSL_ds3_frag.spv"), fsArray[2]))
bValid = true;
if (bValid == false)
{
LOGE("Failed loading some SPV files\n");
nvk.utDestroy();
bValid = false;
return false;
}
//
// Descriptor Pool: size is 3 to have enough for global; object and ...
// TODO: try other VkDescriptorType
//
m_descPool = nvk.createDescriptorPool(NVK::DescriptorPoolCreateInfo(
2, NVK::DescriptorPoolSize
(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2)
(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2) )
);
//
// DescriptorSet allocation
//
nvk.allocateDescriptorSets( NVK::DescriptorSetAllocateInfo(m_descPool,1, &m_descriptorSetLayout), &m_descriptorSet);
//
// Buffers for general UBOs
//
glm::vec2 texinfo(w,h);
m_texInfo.Sz = sizeof(glm::vec2);
m_texInfo.buffer = nvk.utCreateAndFillBuffer(&m_cmdPool, m_texInfo.Sz, &texinfo, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, m_texInfo.bufferMem);
//
// Create buffer for fullscreen quad
//
static glm::vec3 quad[] = {
glm::vec3(-1.0, -1.0, 0.0),
glm::vec3(0,0,0),
glm::vec3( 1.0, -1.0, 0.0),
glm::vec3(1,0,0),
glm::vec3( 1.0, 1.0, 0.0),
glm::vec3(1,1,0),
glm::vec3(-1.0, 1.0, 0.0),
glm::vec3(0,1,0) };
m_quadBuffer.buffer = nvk.utCreateAndFillBuffer(&m_cmdPool, 4*2*sizeof(glm::vec3), quad, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, m_quadBuffer.bufferMem);
//
// FBO and related resources
//
initRenderPass();
initFramebufferAndRelated();
bValid = true;
return true;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
VkCommandBuffer NVFBOBoxVK::Draw(DownSamplingTechnique technique, int tilex, int tiley)//, int windowW, int windowH, float *offset)
{
if(tilex < 0)
tilex = curtilex;
else
curtilex = tilex;
if(tiley < 0)
tiley = curtiley;
else
curtiley = tiley;
if((scaleFactor > 1.0) || (tilesw > 1) || (tilesh > 1))
{
return m_cmdDownsample[technique].m_cmdbuffer;
}
return VK_NULL_HANDLE;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
VkRenderPass NVFBOBoxVK::getScenePass()
{
return m_scenePass;
}
VkFramebuffer NVFBOBoxVK::getFramebuffer()
{
return m_tileData[0].FBSS;
}
VkRect2D NVFBOBoxVK::getViewRect()
{
VkRect2D r;
r.extent.height = bufh;
r.extent.width = bufw;
r.offset.x = 0;
r.offset.y = 0;
return r;
}
VkImage NVFBOBoxVK::getColorImage()
{
// if ever there was NO super-sampling, let's take directly the resolved image
if(scaleFactor == 1.0)
return m_tileData[0].color_texture_SS.img;
// otherwise, take the result of down-sampling
return m_tileData[0].color_texture_DS.img;
}
VkImage NVFBOBoxVK::getColorImageSSMS()
{
return m_tileData[0].color_texture_SSMS.img;
}
VkImage NVFBOBoxVK::getDSTImageSSMS()
{
return m_depth_texture_SSMS.img;
}
VkFramebuffer NVFBOBoxVK::GetFBO(int i)
{
return depthSamples > 1 ? m_tileData[i].FBSS : m_tileData[i].FBDS;
}