aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/deferredPbrRenderer.cs
blob: 133708972d268ae9627c4d82317f7a6d6b79be59 (plain)
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
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using vke;
using vke.glTF;
using Vulkan;

namespace vkChess
{
	public class DeferredPbrRenderer : IDisposable {
        Device dev;
        SwapChain swapChain;
        public PresentQueue presentQueue;

        public static int MAX_MATERIAL_COUNT = 8;
        public static VkSampleCountFlags NUM_SAMPLES = VkSampleCountFlags.SampleCount1;
        public static VkFormat HDR_FORMAT = VkFormat.R32g32b32a32Sfloat;
        public static VkFormat MRT_FORMAT = VkFormat.R32g32b32a32Sfloat;

        public enum DebugView {
            none,
            color,
            normal,
            pos,
            occlusion,
            emissive,
            metallic,
            roughness,
            depth,
            prefill,
            irradiance,
            shadowMap
        }

        public DebugView currentDebugView = DebugView.none;
        public int lightNumDebug = 0;
        public int debugMip = 0;
        public int debugFace = 0;

        public struct Matrices {
            public Matrix4x4 projection;
            public Matrix4x4 model;
            public Matrix4x4 view;
            public Vector4 camPos;
            public float exposure;
            public float gamma;
            public float prefilteredCubeMipLevels;
            public float scaleIBLAmbient;
        }
        public struct Light {
            public Vector4 position;
            public Vector4 color;
            public Matrix4x4 mvp;
        }

        public Matrices matrices = new Matrices {
            gamma = 1.2f,
            exposure = 2.0f,
            scaleIBLAmbient = 0.5f,
        };
		//public Light[] lights = {
		//    new Light {
		//        position = new Vector4(3.5f,12.5f,2,0f),
		//        color = new Vector4(1,0.8f,0.8f,1)
		//    },
		//    new Light {
		//        position = new Vector4(-3.5f,12.5f,2,0f),
		//        color = new Vector4(0.8f,0.8f,1,1)
		//    }
		//};
		public Light [] lights = {
			new Light {
				position = new Vector4(5.5f,12.5f,2,0f),
				color = new Vector4(1,1,1,1)
			},
			new Light {
				position = new Vector4(-5.5f,12.5f,-2,0f),
				color = new Vector4(1,1,1,1)
			}
		};

		//public DescriptorSetWrites UiImageUpdate => new DescriptorSetWrites (dsMain, descLayoutMain.Bindings [8]);

		const float lightMoveSpeed = 0.1f;

        FrameBuffers frameBuffers;
        Image gbColorRough, gbEmitMetal, gbN_AO, gbPos, hdrImg;

        DescriptorPool descriptorPool;
        DescriptorSetLayout descLayoutMain, descLayoutGBuff;
        DescriptorSet dsMain, dsGBuff;

        public PipelineCache pipelineCache;
        Pipeline depthPrepassPipeline, gBuffPipeline, composePipeline, toneMappingPipeline, debugPipeline;

        public HostBuffer uboMatrices { get; private set; }
        public HostBuffer<Light> uboLights { get; private set; }

        RenderPass renderPass;

        public PbrModel model { get; private set; }
        public vke.Environment.EnvironmentCube envCube;
        public ShadowMapRenderer shadowMapRenderer;

        public BoundingBox modelAABB;

        const int SP_SKYBOX         = 0;
		const int SP_DEPTH_PREPASS	= 1;
		const int SP_MODELS         = 2;
        const int SP_COMPOSE        = 3;
        const int SP_TONE_MAPPING   = 4;

        public Model.Scene mainScene;

        public DeferredPbrRenderer (Device dev, SwapChain swapChain, PresentQueue presentQueue, string cubemapPath, float nearPlane, float farPlane) {
            this.dev = dev;
            this.swapChain = swapChain;
            this.presentQueue = presentQueue;            
            pipelineCache = new PipelineCache (dev);

            descriptorPool = new DescriptorPool (dev, 3,
                new VkDescriptorPoolSize (VkDescriptorType.UniformBuffer, 3),
                new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler, 6),
                new VkDescriptorPoolSize (VkDescriptorType.InputAttachment, 5)
            );                

            uboMatrices = new HostBuffer (dev, VkBufferUsageFlags.UniformBuffer, matrices, true);
            uboLights = new HostBuffer<Light> (dev, VkBufferUsageFlags.UniformBuffer, lights, true);
            shadowMapRenderer = new ShadowMapRenderer (dev, this, 32);

            init (nearPlane, farPlane, cubemapPath);
        }

        void init_renderpass () {
            renderPass = new RenderPass (dev, NUM_SAMPLES);

            renderPass.AddAttachment (swapChain.ColorFormat, VkImageLayout.PresentSrcKHR, VkSampleCountFlags.SampleCount1);//swapchain image
            renderPass.AddAttachment (dev.GetSuitableDepthFormat (), VkImageLayout.DepthStencilAttachmentOptimal, NUM_SAMPLES);
            renderPass.AddAttachment (swapChain.ColorFormat, VkImageLayout.ColorAttachmentOptimal, NUM_SAMPLES, VkAttachmentLoadOp.Clear, VkAttachmentStoreOp.DontCare);//GBuff0 (color + roughness) and final color before resolve
            renderPass.AddAttachment (VkFormat.R8g8b8a8Unorm, VkImageLayout.ColorAttachmentOptimal, NUM_SAMPLES, VkAttachmentLoadOp.Clear, VkAttachmentStoreOp.DontCare);//GBuff1 (emit + metal)
            renderPass.AddAttachment (MRT_FORMAT, VkImageLayout.ColorAttachmentOptimal, NUM_SAMPLES, VkAttachmentLoadOp.Clear, VkAttachmentStoreOp.DontCare);//GBuff2 (normals + AO)
            renderPass.AddAttachment (MRT_FORMAT, VkImageLayout.ColorAttachmentOptimal, NUM_SAMPLES, VkAttachmentLoadOp.Clear, VkAttachmentStoreOp.DontCare);//GBuff3 (Pos + depth)
            renderPass.AddAttachment (HDR_FORMAT, VkImageLayout.ColorAttachmentOptimal, NUM_SAMPLES);//hdr color

            renderPass.ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.0f) });
            renderPass.ClearValues.Add (new VkClearValue { depthStencil = new VkClearDepthStencilValue (1.0f, 0) });
            renderPass.ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.0f) });
            renderPass.ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.0f) });
            renderPass.ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.0f) });
            renderPass.ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.0f) });
            renderPass.ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.5f, 0.0f, 0.0f) });

            SubPass[] subpasses = { new SubPass (), new SubPass (), new SubPass (), new SubPass (), new SubPass () };
            //skybox
            subpasses[SP_SKYBOX].AddColorReference (6, VkImageLayout.ColorAttachmentOptimal);

			//depth pre pass
			subpasses [SP_DEPTH_PREPASS].SetDepthReference (1, VkImageLayout.DepthStencilAttachmentOptimal);
			subpasses [SP_DEPTH_PREPASS].AddPreservedReference (6);
			//models
			subpasses [SP_MODELS].AddColorReference (new VkAttachmentReference (2, VkImageLayout.ColorAttachmentOptimal),
                                    new VkAttachmentReference (3, VkImageLayout.ColorAttachmentOptimal),
                                    new VkAttachmentReference (4, VkImageLayout.ColorAttachmentOptimal),
                                    new VkAttachmentReference (5, VkImageLayout.ColorAttachmentOptimal));
            subpasses[SP_MODELS].SetDepthReference (1, VkImageLayout.DepthStencilAttachmentOptimal);
            subpasses[SP_MODELS].AddPreservedReference (6);

            //compose
            subpasses[SP_COMPOSE].AddColorReference (6, VkImageLayout.ColorAttachmentOptimal);
            subpasses[SP_COMPOSE].AddInputReference (new VkAttachmentReference (2, VkImageLayout.ShaderReadOnlyOptimal),
                                    new VkAttachmentReference (3, VkImageLayout.ShaderReadOnlyOptimal),
                                    new VkAttachmentReference (4, VkImageLayout.ShaderReadOnlyOptimal),
                                    new VkAttachmentReference (5, VkImageLayout.ShaderReadOnlyOptimal));
            //tone mapping
            subpasses[SP_TONE_MAPPING].AddColorReference ((NUM_SAMPLES == VkSampleCountFlags.SampleCount1) ? 0u : 2u, VkImageLayout.ColorAttachmentOptimal);
            subpasses[SP_TONE_MAPPING].AddInputReference (new VkAttachmentReference (6, VkImageLayout.ShaderReadOnlyOptimal));
            if (NUM_SAMPLES != VkSampleCountFlags.SampleCount1)
                subpasses[SP_TONE_MAPPING].AddResolveReference (0, VkImageLayout.ColorAttachmentOptimal);

            renderPass.AddSubpass (subpasses);

            renderPass.AddDependency (Vk.SubpassExternal, SP_SKYBOX,
                VkPipelineStageFlags.BottomOfPipe, VkPipelineStageFlags.ColorAttachmentOutput,
                VkAccessFlags.MemoryRead, VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite);
			renderPass.AddDependency (SP_SKYBOX, SP_DEPTH_PREPASS,
				VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.FragmentShader,
				VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.ShaderRead);
			renderPass.AddDependency (SP_DEPTH_PREPASS, SP_MODELS,
				VkPipelineStageFlags.EarlyFragmentTests, VkPipelineStageFlags.FragmentShader,
				VkAccessFlags.DepthStencilAttachmentWrite, VkAccessFlags.ShaderRead);
			renderPass.AddDependency (SP_MODELS, SP_COMPOSE,
                VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.FragmentShader,
                VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.ShaderRead);
            renderPass.AddDependency (SP_COMPOSE, SP_TONE_MAPPING,
                VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.FragmentShader,
                VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.ShaderRead);
            renderPass.AddDependency (SP_TONE_MAPPING, Vk.SubpassExternal,
                    VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.BottomOfPipe,
                    VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.MemoryRead);
        }

        void init (float nearPlane, float farPlane, string cubemapPath) {
            init_renderpass ();

            descLayoutMain = new DescriptorSetLayout (dev,
                new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),//matrices and params
                new VkDescriptorSetLayoutBinding (1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),//irradiance
                new VkDescriptorSetLayoutBinding (2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),//prefil
                new VkDescriptorSetLayoutBinding (3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),//lut brdf
                new VkDescriptorSetLayoutBinding (4, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),//lights
                new VkDescriptorSetLayoutBinding (5, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),//materials
            	new VkDescriptorSetLayoutBinding (6, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),//shadow map
				new VkDescriptorSetLayoutBinding (7, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)//texture array
				//new VkDescriptorSetLayoutBinding (8, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                );//uiImage

			descLayoutGBuff = new DescriptorSetLayout (dev,
                new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),//color + roughness
                new VkDescriptorSetLayoutBinding (1, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),//emit + metal
                new VkDescriptorSetLayoutBinding (2, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),//normals + AO
                new VkDescriptorSetLayoutBinding (3, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),//Pos + depth
                new VkDescriptorSetLayoutBinding (4, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment));//hdr

            descLayoutMain.SetName ("main");
            descLayoutGBuff.SetName ("GBuff");

            using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, NUM_SAMPLES)) {
                
                cfg.rasterizationState.cullMode = VkCullModeFlags.None;

                if (NUM_SAMPLES != VkSampleCountFlags.SampleCount1) {
                    cfg.multisampleState.sampleShadingEnable = true;
                    cfg.multisampleState.minSampleShading = 0.5f;
                }
                cfg.Cache = pipelineCache;
                cfg.Layout = new PipelineLayout (dev,
                    new VkPushConstantRange[] {
                    new VkPushConstantRange (VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf<Matrix4x4> ()),
                    new VkPushConstantRange (VkShaderStageFlags.Fragment, sizeof (int), 64)},
                    descLayoutMain, descLayoutGBuff);

                cfg.RenderPass = renderPass;
                //cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false));

                cfg.AddVertexBinding<PbrModelTexArray.Vertex> (0);
                cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat, VkFormat.R32g32Sfloat);

                cfg.AddVertexBinding<VkChess.InstanceData> (1, VkVertexInputRate.Instance);
                cfg.AddVertexAttributes (1, VkFormat.R32g32b32a32Sfloat, VkFormat.R32g32b32a32Sfloat, VkFormat.R32g32b32a32Sfloat, VkFormat.R32g32b32a32Sfloat, VkFormat.R32g32b32a32Sfloat);

                using (SpecializationInfo constants = new SpecializationInfo (
                            new SpecializationConstant<float> (0, nearPlane),
                            new SpecializationConstant<float> (1, farPlane),
                            new SpecializationConstant<int> (2, MAX_MATERIAL_COUNT))) {
                    cfg.AddShader (dev, VkShaderStageFlags.Vertex, "#vkChess.net.GBuffPbrInstanced.vert.spv");

                    cfg.SubpassIndex = SP_DEPTH_PREPASS;
                    depthPrepassPipeline = new GraphicPipeline (cfg, "Depth prepass");

                    cfg.depthStencilState.depthCompareOp = VkCompareOp.Equal;
                    cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false));
                    cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false));
                    cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false));

                    cfg.SubpassIndex = SP_MODELS;

                    cfg.AddShader (dev, VkShaderStageFlags.Fragment, "#vkChess.net.GBuffPbrTexArray.frag.spv", constants);

                    gBuffPipeline = new GraphicPipeline (cfg, "GBuff");
                }
                cfg.rasterizationState.cullMode = VkCullModeFlags.Front;
                //COMPOSE PIPELINE
                cfg.blendAttachments.Clear ();
                cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false));

                cfg.ResetShadersAndVerticesInfos ();

                cfg.SubpassIndex = SP_COMPOSE;
                cfg.Layout = gBuffPipeline.Layout;
                cfg.depthStencilState.depthTestEnable = false;
                cfg.depthStencilState.depthWriteEnable = false;
                using (SpecializationInfo constants = new SpecializationInfo (
                    new SpecializationConstant<uint> (0, (uint)lights.Length),
                    new SpecializationConstant<float> (1, 0.25f))) {
                    cfg.AddShader (dev, VkShaderStageFlags.Vertex, "#vke.FullScreenQuad.vert.spv");
                    cfg.AddShader (dev, VkShaderStageFlags.Fragment, "#vkChess.net.compose_with_shadows.frag.spv", constants);
                    composePipeline = new GraphicPipeline (cfg, "compose");
                }
                //DEBUG DRAW use subpass of compose
                cfg.ReplaceShader (1, new ShaderInfo (dev, VkShaderStageFlags.Fragment, "#vkChess.net.show_gbuff.frag.spv"));
                cfg.SubpassIndex = SP_COMPOSE;
                debugPipeline = new GraphicPipeline (cfg, "debug");
                //TONE MAPPING
                cfg.ReplaceShader (1, new ShaderInfo (dev, VkShaderStageFlags.Fragment, "#vkChess.net.tone_mapping.frag.spv"));
                cfg.SubpassIndex = SP_TONE_MAPPING;
                toneMappingPipeline = new GraphicPipeline (cfg, "tonne mapping");
            }

            dsMain = descriptorPool.Allocate (descLayoutMain);
            dsGBuff = descriptorPool.Allocate (descLayoutGBuff);

			envCube = new vke.Environment.EnvironmentCube (cubemapPath, gBuffPipeline.Layout, presentQueue, renderPass);

            matrices.prefilteredCubeMipLevels = envCube.prefilterCube.CreateInfo.mipLevels;

            DescriptorSetWrites dsMainWrite = new DescriptorSetWrites (dsMain, descLayoutMain.Bindings.GetRange (0, 5).ToArray ());
            dsMainWrite.Write (dev, 
                uboMatrices.Descriptor,
                envCube.irradianceCube.Descriptor,
                envCube.prefilterCube.Descriptor,
                envCube.lutBrdf.Descriptor,
                uboLights.Descriptor);

            dsMainWrite = new DescriptorSetWrites (dsMain, descLayoutMain.Bindings[6]);
            dsMainWrite.Write (dev, shadowMapRenderer.shadowMap.Descriptor);
        }

        public void LoadModel (Queue transferQ, string path) {
            dev.WaitIdle ();

            model?.Dispose ();
            model = new PbrModelTexArray (transferQ, path);

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites (dsMain, descLayoutMain.Bindings[5], descLayoutMain.Bindings[7]);
            uboUpdate.Write (dev, model.materialUBO.Descriptor, (model as PbrModelTexArray).texArray.Descriptor);

            modelAABB = model.DefaultScene.AABB;
        }

        public void recordDraw (PrimaryCommandBuffer cmd, int imageIndex, vke.Buffer instanceBuf = null, params Model.InstancedCmd[] instances) {
            FrameBuffer fb = frameBuffers[imageIndex];
            renderPass.Begin(cmd, fb);

            cmd.SetViewport(fb.Width, fb.Height);
            cmd.SetScissor(fb.Width, fb.Height);

            cmd.BindDescriptorSet(gBuffPipeline.Layout, dsMain);

            envCube.RecordDraw(cmd);
            renderPass.BeginSubPass(cmd);

            if (model != null) {
				model.Bind (cmd);

				depthPrepassPipeline.Bind (cmd);
				if (instanceBuf != null)
					model.Draw (cmd, gBuffPipeline.Layout, instanceBuf, false, instances);

				renderPass.BeginSubPass (cmd);
				gBuffPipeline.Bind(cmd);
                
                if (instanceBuf != null)
                	model.Draw(cmd, gBuffPipeline.Layout, instanceBuf, false, instances);
            }
            renderPass.BeginSubPass(cmd);

            cmd.BindDescriptorSet(composePipeline.Layout, dsGBuff, 1);

            if (currentDebugView == DebugView.none)
                composePipeline.Bind(cmd);
            else {
                debugPipeline.Bind(cmd);
                uint debugValue = (uint)currentDebugView - 1;
                if (currentDebugView == DebugView.shadowMap)
                    debugValue += (uint)((lightNumDebug << 8));
                else
                    debugValue += (uint)((debugFace << 8) + (debugMip << 16));
                cmd.PushConstant(debugPipeline.Layout, VkShaderStageFlags.Fragment, debugValue, (uint)Marshal.SizeOf<Matrix4x4>());
            }

            cmd.Draw(3, 1, 0, 0);

            renderPass.BeginSubPass(cmd);
            toneMappingPipeline.Bind(cmd);
            cmd.Draw(3, 1, 0, 0);

            renderPass.End(cmd);
        }

        public void MoveLight (Vector4 dir) {
            lights[lightNumDebug].position += dir * lightMoveSpeed;
            shadowMapRenderer.updateShadowMap = true;
        }

        #region update
        public void UpdateView (Camera camera) {
            camera.AspectRatio = (float)swapChain.Width / swapChain.Height;

            matrices.projection = camera.Projection;
            matrices.view = camera.View;
            matrices.model = camera.Model;

			Matrix4x4.Invert (camera.View, out Matrix4x4 inv);
			matrices.camPos = new Vector4 (inv.M41, inv.M42, inv.M43, 0);

			uboMatrices.Update (matrices, (uint)Marshal.SizeOf<Matrices> ());
			shadowMapRenderer.updateShadowMap = true;
		}

        #endregion

        void createGBuff () {
            gbColorRough?.Dispose ();
            gbEmitMetal?.Dispose ();
            gbN_AO?.Dispose ();
            gbPos?.Dispose ();
            hdrImg?.Dispose ();

            gbColorRough = new Image (dev, swapChain.ColorFormat, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment | VkImageUsageFlags.TransientAttachment, VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height, VkImageType.Image2D, NUM_SAMPLES);
            gbEmitMetal = new Image (dev, VkFormat.R8g8b8a8Unorm, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment | VkImageUsageFlags.TransientAttachment, VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height, VkImageType.Image2D, NUM_SAMPLES);
            gbN_AO = new Image (dev, MRT_FORMAT, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment | VkImageUsageFlags.TransientAttachment, VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height, VkImageType.Image2D, NUM_SAMPLES);
            gbPos = new Image (dev, MRT_FORMAT, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment | VkImageUsageFlags.TransientAttachment, VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height, VkImageType.Image2D, NUM_SAMPLES);
            hdrImg = new Image (dev, HDR_FORMAT, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment | VkImageUsageFlags.TransientAttachment, VkMemoryPropertyFlags.DeviceLocal | VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height, VkImageType.Image2D, NUM_SAMPLES);

            gbColorRough.CreateView ();
            gbColorRough.CreateSampler ();
            gbColorRough.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            gbEmitMetal.CreateView ();
            gbEmitMetal.CreateSampler ();
            gbEmitMetal.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            gbN_AO.CreateView ();
            gbN_AO.CreateSampler ();
            gbN_AO.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            gbPos.CreateView ();
            gbPos.CreateSampler ();
            gbPos.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            hdrImg.CreateView ();
            hdrImg.CreateSampler ();
            hdrImg.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites (descLayoutGBuff);
            uboUpdate.Write (dev, dsGBuff,    gbColorRough.Descriptor,
                                        gbEmitMetal.Descriptor,
                                        gbN_AO.Descriptor,
                                        gbPos.Descriptor,
                                        hdrImg.Descriptor);
            gbColorRough.SetName ("GBuffColorRough");
            gbEmitMetal.SetName ("GBuffEmitMetal");
            gbN_AO.SetName ("GBuffN");
            gbPos.SetName ("GBuffPos");
            hdrImg.SetName ("HDRimg");
        }

        public void Resize () {
			frameBuffers?.Dispose ();
			frameBuffers = new FrameBuffers ();

            createGBuff ();

			for (int i = 0; i < swapChain.ImageCount; ++i) {
				frameBuffers.Add (new FrameBuffer (renderPass, swapChain.Width, swapChain.Height, new Image [] {
					swapChain.images[i], null, gbColorRough, gbEmitMetal, gbN_AO, gbPos, hdrImg}));
			}
			//frameBuffers = renderPass.CreateFrameBuffers (swapChain);
        }

        public void Dispose () {
            dev.WaitIdle ();
            
            frameBuffers?.Dispose ();

            gbColorRough.Dispose ();
            gbEmitMetal.Dispose ();
            gbN_AO.Dispose ();
            gbPos.Dispose ();
            hdrImg.Dispose ();

            gBuffPipeline.Dispose ();
            composePipeline.Dispose ();
            toneMappingPipeline.Dispose ();
            debugPipeline.Dispose ();
			depthPrepassPipeline.Dispose ();

            descLayoutMain.Dispose ();            
            descLayoutGBuff.Dispose ();

            uboMatrices.Dispose ();
            uboLights.Dispose ();
            model.Dispose ();
            envCube.Dispose ();
            shadowMapRenderer.Dispose ();

            descriptorPool.Dispose ();
        }
    }
}