Пример #1
0
        Program() : base(false)
        {
            vbo     = new HostBuffer <Vertex> (dev, VkBufferUsageFlags.VertexBuffer, vertices);
            ibo     = new HostBuffer <ushort> (dev, VkBufferUsageFlags.IndexBuffer, indices);
            uboMats = new HostBuffer(dev, VkBufferUsageFlags.UniformBuffer, matrices);

            descriptorPool = new DescriptorPool(dev, 1, new VkDescriptorPoolSize(VkDescriptorType.UniformBuffer));
            dsLayout       = new DescriptorSetLayout(dev,
                                                     new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer));

            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount1);

            cfg.Layout     = new PipelineLayout(dev, dsLayout);
            cfg.RenderPass = new RenderPass(dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat(), cfg.Samples);
            cfg.AddVertexBinding <Vertex> (0);
            cfg.AddVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat);

            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/triangle.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/triangle.frag.spv");

            pipeline = new GraphicPipeline(cfg);

            descriptorSet = descriptorPool.Allocate(dsLayout);
            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descriptorSet, dsLayout);

            uboUpdate.Write(dev, uboMats.Descriptor);

            uboMats.Map();
        }
Пример #2
0
        public Program()
        {
            instance = new Instance();
            phy      = instance.GetAvailablePhysicalDevice().FirstOrDefault();
            dev      = new Device(phy);
            computeQ = new Queue(dev, VkQueueFlags.Compute);

            dev.Activate(default(VkPhysicalDeviceFeatures));

            createRandomDatas();

            inBuff  = new HostBuffer <int> (dev, VkBufferUsageFlags.StorageBuffer, datas);
            outBuff = new HostBuffer <int> (dev, VkBufferUsageFlags.StorageBuffer, data_size);

            dsPool   = new DescriptorPool(dev, 1, new VkDescriptorPoolSize(VkDescriptorType.StorageBuffer, 2));
            dsLayout = new DescriptorSetLayout(dev,
                                               new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer),
                                               new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer)
                                               );

            plCompute = new ComputePipeline(new PipelineLayout(dev, dsLayout), "#shaders.compute.comp.spv");

            dset = dsPool.Allocate(dsLayout);
            DescriptorSetWrites dsUpdate = new DescriptorSetWrites(dset, dsLayout);

            dsUpdate.Write(dev, inBuff.Descriptor, outBuff.Descriptor);
        }
Пример #3
0
        public void LoadModel(Queue transferQ, string path)
        {
            dev.WaitIdle();
            model?.Dispose();

            if (TEXTURE_ARRAY)
            {
                PbrModelTexArray mod = new PbrModelTexArray(transferQ, path);
                if (mod.texArray != null)
                {
                    DescriptorSetWrites uboUpdate = new DescriptorSetWrites(dsMain, descLayoutMain.Bindings[5], descLayoutMain.Bindings[7]);
                    uboUpdate.Write(dev, mod.materialUBO.Descriptor, mod.texArray.Descriptor);
                }
                model = mod;
            }
            else
            {
                model = new PbrModelSeparatedTextures(transferQ, path,
                                                      descLayoutTextures,
                                                      AttachmentType.Color,
                                                      AttachmentType.PhysicalProps,
                                                      AttachmentType.Normal,
                                                      AttachmentType.AmbientOcclusion,
                                                      AttachmentType.Emissive);

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


            modelAABB = model.DefaultScene.AABB;
        }
Пример #4
0
        void createGBuff()
        {
            gbColorRough?.Dispose();
            gbEmitMetal?.Dispose();
            gbN?.Dispose();
            gbPos?.Dispose();

            gbColorRough = new Image(dev, VkFormat.R8g8b8a8Unorm, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment, VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height);
            gbEmitMetal  = new Image(dev, VkFormat.R8g8b8a8Unorm, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment, VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height);
            gbN          = new Image(dev, VkFormat.R16g16b16a16Sfloat, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment, VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height);
            gbPos        = new Image(dev, VkFormat.R16g16b16a16Sfloat, VkImageUsageFlags.InputAttachment | VkImageUsageFlags.ColorAttachment, VkMemoryPropertyFlags.DeviceLocal, swapChain.Width, swapChain.Height);

            gbColorRough.CreateView();
            gbColorRough.CreateSampler();
            gbEmitMetal.CreateView();
            gbEmitMetal.CreateSampler();
            gbN.CreateView();
            gbN.CreateSampler();
            gbPos.CreateView();
            gbPos.CreateSampler();

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descLayoutGBuff);

            uboUpdate.Write(dev, dsGBuff, gbColorRough.Descriptor,
                            gbEmitMetal.Descriptor,
                            gbN.Descriptor,
                            gbPos.Descriptor);
            gbColorRough.SetName("GBuffColorRough");
            gbEmitMetal.SetName("GBuffEmitMetal");
            gbN.SetName("GBuffN");
            gbPos.SetName("GBuffPos");
        }
Пример #5
0
        public PBRPipeline(Queue staggingQ, RenderPass renderPass, string cubeMapPath, PipelineCache pipelineCache = null) :
            base(renderPass, pipelineCache, "pbr pipeline")
        {
            descriptorPool = new DescriptorPool(Dev, 2,
                                                new VkDescriptorPoolSize(VkDescriptorType.UniformBuffer, 2),
                                                new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler, 8)
                                                );

            descLayoutMain = new DescriptorSetLayout(Dev,
                                                     new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),
                                                     new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer));

            descLayoutTextures = new DescriptorSetLayout(Dev,
                                                         new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                                         );

            using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, renderPass.Samples)) {
                cfg.Layout = new PipelineLayout(Dev, descLayoutMain, descLayoutTextures);
                cfg.Layout.AddPushConstants(
                    new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf <Matrix4x4> ()),
                    new VkPushConstantRange(VkShaderStageFlags.Fragment, sizeof(int), 64)
                    );
                cfg.RenderPass = renderPass;
                cfg.AddVertexBinding <PbrModel.Vertex> (0);
                cfg.AddVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat, VkFormat.R32g32Sfloat);

                cfg.AddShader(Dev, VkShaderStageFlags.Vertex, "#shaders.pbr.vert.spv");
                cfg.AddShader(Dev, VkShaderStageFlags.Fragment, "#shaders.pbr_khr.frag.spv");

                layout = cfg.Layout;

                init(cfg);
            }

            dsMain = descriptorPool.Allocate(descLayoutMain);

            envCube = new EnvironmentCube(cubeMapPath, layout, staggingQ, RenderPass);

            matrices.prefilteredCubeMipLevels = envCube.prefilterCube.CreateInfo.mipLevels;
            uboMats = new HostBuffer(Dev, VkBufferUsageFlags.UniformBuffer, matrices, true);

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descLayoutMain.Bindings.GetRange(0, 4).ToArray());

            uboUpdate.Write(Dev, dsMain,
                            uboMats.Descriptor,
                            envCube.irradianceCube.Descriptor,
                            envCube.prefilterCube.Descriptor,
                            envCube.lutBrdf.Descriptor);
        }
Пример #6
0
        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;
        }
Пример #7
0
        protected override void initVulkan()
        {
            base.initVulkan();

            //first create the needed buffers
            vbo = new HostBuffer <Vertex> (dev, VkBufferUsageFlags.VertexBuffer, vertices);
            ibo = new HostBuffer <ushort> (dev, VkBufferUsageFlags.IndexBuffer, indices);
            //because mvp matrice may be updated by mouse move, we keep it mapped after creation.
            uboMats = new HostBuffer(dev, VkBufferUsageFlags.UniformBuffer, mvp, true);

            //a descriptor pool to allocate the mvp matrice descriptor from.
            descriptorPool = new DescriptorPool(dev, 1, new VkDescriptorPoolSize(VkDescriptorType.UniformBuffer));

            //Graphic pipeline configuration are predefined by the GraphicPipelineConfig class, which ease sharing config for several pipelines having lots in common.
            using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount1, false)) {
                //Create the pipeline layout, it will be automatically activated on pipeline creation, so that sharing layout among different pipelines will benefit
                //from the reference counting to automatically dispose unused layout on pipeline clean up. It's the same for DescriptorSetLayout.
                cfg.Layout = new PipelineLayout(dev,
                                                new DescriptorSetLayout(dev, new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer)));
                //create a default renderpass with just a color attachment for the swapchain image, a default subpass is automatically created and the renderpass activation
                //will follow the pipeline life cicle and will be automatically disposed when no longuer used.
                cfg.RenderPass = new RenderPass(dev, swapChain.ColorFormat, cfg.Samples);
                //configuration of vertex bindings and attributes
                cfg.AddVertexBinding <Vertex> (0);
                cfg.AddVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat);                 //position + color

                //shader are automatically compiled by SpirVTasks if added to the project. The resulting shaders are automatically embedded in the assembly.
                //To specifiy that the shader path is a resource name, put the '#' prefix. Else the path will be search on disk.
                cfg.AddShaders(
                    new ShaderInfo(dev, VkShaderStageFlags.Vertex, "#shaders.main.vert.spv"),
                    new ShaderInfo(dev, VkShaderStageFlags.Fragment, "#shaders.main.frag.spv")
                    );

                //create and activate the pipeline with the configuration we've just done.
                pipeline = new GraphicPipeline(cfg);
            }

            //because descriptor layout used for a pipeline are only activated on pipeline activation, descriptor set must not be allocated before, except if the layout has been manually activated,
            //but in this case, layout will need also to be explicitly disposed.
            descriptorSet = descriptorPool.Allocate(pipeline.Layout.DescriptorSetLayouts[0]);

            //Write the content of the descriptor, the mvp matrice.
            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descriptorSet, pipeline.Layout.DescriptorSetLayouts[0]);

            //Descriptor property of the mvp buffer will return a default descriptor with no offset of the full size of the buffer.
            uboUpdate.Write(dev, uboMats.Descriptor);

            //allocate the default VkWindow buffers, one per swapchain image. Their will be only reset when rebuilding and not reallocated.
            cmds = cmdPool.AllocateCommandBuffer(swapChain.ImageCount);
        }
Пример #8
0
        //in the main vulkan thread
        void updateTextureSet()
        {
            nextTexture.CreateView(VkImageViewType.Cube, VkImageAspectFlags.Color, 6);
            nextTexture.CreateSampler();

            nextTexture.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descriptorSet, dsLayout.Bindings[1]);

            uboUpdate.Write(dev, nextTexture.Descriptor);

            texture?.Dispose();
            texture     = nextTexture;
            nextTexture = null;
        }
Пример #9
0
        public void LoadModel(Queue staggingQ, string path)
        {
            model?.Dispose();

            model = new PbrModel(staggingQ, path, descLayoutTextures,
                                 AttachmentType.Color,
                                 AttachmentType.PhysicalProps,
                                 AttachmentType.Normal,
                                 AttachmentType.AmbientOcclusion,
                                 AttachmentType.Emissive);

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descLayoutMain.Bindings[4]);

            uboUpdate.Write(Dev, dsMain, model.materialUBO.Descriptor);
        }
Пример #10
0
        //in the main vulkan thread
        void updateTextureSet()
        {
            nextTexture.CreateView();
            nextTexture.CreateSampler();
            nextTexture.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;

            dev.WaitIdle();

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descriptorSet, dsLayout.Bindings[1]);

            uboUpdate.Write(dev, nextTexture.Descriptor);

            texture?.Dispose();
            texture     = nextTexture;
            nextTexture = null;
        }
Пример #11
0
        public Program()
        {
            instance = new Instance();

#if DEBUG
            dbgReport = new DebugReport(instance,
                                        VkDebugReportFlagsEXT.ErrorEXT
                                        | VkDebugReportFlagsEXT.DebugEXT
                                        | VkDebugReportFlagsEXT.WarningEXT
                                        | VkDebugReportFlagsEXT.PerformanceWarningEXT

                                        );
#endif

            phy      = instance.GetAvailablePhysicalDevice().FirstOrDefault();
            dev      = new Device(phy);
            computeQ = new Queue(dev, VkQueueFlags.Compute);
            dev.Activate(enabledFeatures, enabledExtensions);

            datas = new float[data_size];
            Random rnd = new Random();
            for (uint i = 0; i < data_size; i++)
            {
                datas[i] = (float)rnd.NextDouble();
            }

            inBuff  = new HostBuffer <float> (dev, VkBufferUsageFlags.StorageBuffer, datas);
            outBuff = new HostBuffer <float> (dev, VkBufferUsageFlags.StorageBuffer, data_size);

            dsPool          = new DescriptorPool(dev, 2, new VkDescriptorPoolSize(VkDescriptorType.StorageBuffer, 4));
            dsLayoutCompute = new DescriptorSetLayout(dev,
                                                      new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer),
                                                      new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer)
                                                      );

            plCompute = new ComputePipeline(
                new PipelineLayout(dev, new VkPushConstantRange(VkShaderStageFlags.Compute, sizeof(int)), dsLayoutCompute),
                "shaders/computeTest.comp.spv");

            dsetPing = dsPool.Allocate(dsLayoutCompute);
            dsetPong = dsPool.Allocate(dsLayoutCompute);
            DescriptorSetWrites dsUpdate = new DescriptorSetWrites(dsetPing, dsLayoutCompute);
            dsUpdate.Write(dev, inBuff.Descriptor, outBuff.Descriptor);

            dsUpdate.Write(dev, dsetPong, outBuff.Descriptor, inBuff.Descriptor);
        }
Пример #12
0
        protected override void initVulkan()
        {
            base.initVulkan();

            cmds = cmdPool.AllocateCommandBuffer(swapChain.ImageCount);

            loadTexture(imgPathes[currentImgIndex]);

            vbo = new GPUBuffer <float> (presentQueue, cmdPool, VkBufferUsageFlags.VertexBuffer, vertices);
            ibo = new GPUBuffer <ushort> (presentQueue, cmdPool, VkBufferUsageFlags.IndexBuffer, indices);

            descriptorPool = new DescriptorPool(dev, 1,
                                                new VkDescriptorPoolSize(VkDescriptorType.UniformBuffer),
                                                new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler)
                                                );

            dsLayout = new DescriptorSetLayout(dev, 0,
                                               new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer),
                                               new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));

            using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount4)) {
                cfg.Layout     = new PipelineLayout(dev, dsLayout);
                cfg.RenderPass = new RenderPass(dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat(), cfg.Samples);

                cfg.AddVertexBinding(0, 5 * sizeof(float));
                cfg.AddVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);

                cfg.AddShader(dev, VkShaderStageFlags.Vertex, "#shaders.main.vert.spv");
                cfg.AddShader(dev, VkShaderStageFlags.Fragment, "#shaders.main.frag.spv");

                pipeline = new GraphicPipeline(cfg);
            }


            uboMats = new HostBuffer(dev, VkBufferUsageFlags.UniformBuffer, matrices);
            uboMats.Map();             //permanent map

            descriptorSet = descriptorPool.Allocate(dsLayout);

            updateTextureSet();

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descriptorSet, dsLayout.Bindings[0]);

            uboUpdate.Write(dev, uboMats.Descriptor);
        }
Пример #13
0
        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");
        }
Пример #14
0
        protected override void OnResize()
        {
            vkvgImage?.Dispose();
            vkvgSurf?.Dispose();
            vkvgSurf  = new vkvg.Surface(vkvgDev, (int)swapChain.Width, (int)swapChain.Height);
            vkvgImage = new Image(dev, new VkImage((ulong)vkvgSurf.VkImage.ToInt64()), VkFormat.B8g8r8a8Unorm,
                                  VkImageUsageFlags.ColorAttachment, (uint)vkvgSurf.Width, (uint)vkvgSurf.Height);
            vkvgImage.CreateView(VkImageViewType.ImageView2D, VkImageAspectFlags.Color);
            vkvgImage.CreateSampler(VkFilter.Nearest, VkFilter.Nearest, VkSamplerMipmapMode.Nearest, VkSamplerAddressMode.ClampToBorder);

            vkvgImage.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(dsMats, descLayoutMatrix.Bindings[1]);

            uboUpdate.Write(dev, vkvgImage.Descriptor);

            updateMatrices();

            if (frameBuffers != null)
            {
                for (int i = 0; i < swapChain.ImageCount; ++i)
                {
                    frameBuffers[i]?.Dispose();
                }
            }

            frameBuffers = new Framebuffer[swapChain.ImageCount];

            for (int i = 0; i < swapChain.ImageCount; ++i)
            {
                frameBuffers[i] = new Framebuffer(pipeline.RenderPass, swapChain.Width, swapChain.Height,
                                                  (pipeline.Samples == VkSampleCountFlags.SampleCount1) ? new Image[] {
                    swapChain.images[i],
                    null
                } : new Image[] {
                    null,
                    null,
                    swapChain.images[i]
                });
                frameBuffers[i].SetName("main FB " + i);
            }

            buildCommandBuffers();
        }
Пример #15
0
        protected override void OnResize()
        {
            base.OnResize();

            dev.WaitIdle();

            renderer.Resize(Width, Height);

            UpdateView();

            frameBuffers?.Dispose();
            frameBuffers = plToneMap.RenderPass.CreateFrameBuffers(swapChain);

            DescriptorSetWrites dsUpdate = new DescriptorSetWrites(plToneMap.Layout.DescriptorSetLayouts[0].Bindings[0]);

            dsUpdate.Write(dev, descriptorSet, renderer.hdrImgResolved.Descriptor);

            buildCommandBuffers();

            dev.WaitIdle();
        }
Пример #16
0
        protected override void OnResize()
        {
            dev.WaitIdle();
            recreateSurfaceStatus = true;
            while (recreateSurfaceStatus)
            {
                Thread.Sleep(1);
            }

            vkvgImage.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(dsVkvg, descLayout);

            uboUpdate.Write(dev, vkvgImage.Descriptor);

            if (frameBuffers != null)
            {
                for (int i = 0; i < swapChain.ImageCount; ++i)
                {
                    frameBuffers[i]?.Dispose();
                }
            }

            frameBuffers = new Framebuffer[swapChain.ImageCount];

            for (int i = 0; i < swapChain.ImageCount; ++i)
            {
                frameBuffers[i] = new Framebuffer(uiPipeline.RenderPass, swapChain.Width, swapChain.Height,
                                                  (uiPipeline.Samples == VkSampleCountFlags.SampleCount1) ? new Image[] {
                    swapChain.images[i],
                    null
                } : new Image[] {
                    null,
                    null,
                    swapChain.images[i]
                });
                frameBuffers[i].SetName("main FB " + i);
            }

            buildCommandBuffers();
        }
Пример #17
0
        protected override void OnResize()
        {
            initUISurface();

            uiImage.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(pbrPipeline.dsMain, pbrPipeline.Layout.DescriptorSetLayouts[0].Bindings[5]);

            uboUpdate.Write(dev, uiImage.Descriptor);

            UpdateView();

            if (frameBuffers != null)
            {
                for (int i = 0; i < swapChain.ImageCount; ++i)
                {
                    frameBuffers[i]?.Dispose();
                }
            }

            frameBuffers = new Framebuffer[swapChain.ImageCount];

            for (int i = 0; i < swapChain.ImageCount; ++i)
            {
                frameBuffers[i] = new Framebuffer(pbrPipeline.RenderPass, swapChain.Width, swapChain.Height,
                                                  (pbrPipeline.RenderPass.Samples == VkSampleCountFlags.SampleCount1) ? new Image[] {
                    swapChain.images[i],
                    null
                } : new Image[] {
                    null,
                    null,
                    swapChain.images[i]
                });
            }

            buildCommandBuffers();
        }
Пример #18
0
        Vector4 outlineColor = new Vector4(1.0f, 0.0f, 0.0f, 0.6f);      //alpha => 0:disabled 1:enabled

        protected override void initVulkan()
        {
            base.initVulkan();

            cmds = cmdPool.AllocateCommandBuffer(swapChain.ImageCount);

            font = new BMFont(vke.samples.Utils.GetDataFile("font.fnt"));

            vbo = new GPUBuffer <float> (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.TransferDst, 1024);
            ibo = new GPUBuffer <ushort> (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.TransferDst, 2048);

            descriptorPool = new DescriptorPool(dev, 1,
                                                new VkDescriptorPoolSize(VkDescriptorType.UniformBuffer),
                                                new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler)
                                                );

            dsLayout = new DescriptorSetLayout(dev, 0,
                                               new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer),
                                               new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));

            using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount4, false)) {
                cfg.Layout = new PipelineLayout(dev, dsLayout);
                cfg.Layout.AddPushConstants(new VkPushConstantRange(VkShaderStageFlags.Fragment, (uint)Marshal.SizeOf <Vector4> () * 2));

                cfg.RenderPass = new RenderPass(dev, swapChain.ColorFormat, cfg.Samples);

                cfg.blendAttachments[0] = new VkPipelineColorBlendAttachmentState(
                    true, VkBlendFactor.One, VkBlendFactor.OneMinusSrcAlpha, VkBlendOp.Add, VkBlendFactor.One, VkBlendFactor.Zero);

                cfg.AddVertexBinding(0, 5 * sizeof(float));
                cfg.AddVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);

                cfg.AddShader(dev, VkShaderStageFlags.Vertex, "#shaders.main.vert.spv");
                cfg.AddShader(dev, VkShaderStageFlags.Fragment, "#shaders.main.frag.spv");

                pipeline = new GraphicPipeline(cfg);
            }

            uboMats = new HostBuffer(dev, VkBufferUsageFlags.UniformBuffer, matrices);
            uboMats.Map();             //permanent map

            descriptorSet = descriptorPool.Allocate(dsLayout);

            fontTexture = font.GetPageTexture(0, presentQueue, cmdPool);
            fontTexture.CreateView();
            fontTexture.CreateSampler();
            fontTexture.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;

            DescriptorSetWrites dsUpdate = new DescriptorSetWrites(descriptorSet, dsLayout);

            dsUpdate.Write(dev, uboMats.Descriptor, fontTexture.Descriptor);

            generateText("Vulkan", out HostBuffer <Vertex> staggingVbo, out HostBuffer <ushort> staggingIbo);

            PrimaryCommandBuffer cmd = cmdPool.AllocateAndStart(VkCommandBufferUsageFlags.OneTimeSubmit);

            staggingVbo.CopyTo(cmd, vbo);
            staggingIbo.CopyTo(cmd, ibo);

            presentQueue.EndSubmitAndWait(cmd);

            staggingVbo.Dispose();
            staggingIbo.Dispose();

            UpdateFrequency = 10;
        }
Пример #19
0
        public Program() : base()
        {
            if (Instance.DEBUG_UTILS)
            {
                dbgReport = new DebugReport(instance,
                                            VkDebugReportFlagsEXT.ErrorEXT
                                            | VkDebugReportFlagsEXT.DebugEXT
                                            | VkDebugReportFlagsEXT.WarningEXT
                                            | VkDebugReportFlagsEXT.PerformanceWarningEXT

                                            );
            }
            imgResult = new Image(dev, VkFormat.R32g32b32a32Sfloat, VkImageUsageFlags.TransferDst | VkImageUsageFlags.Sampled, VkMemoryPropertyFlags.DeviceLocal,
                                  imgDim, imgDim);
            imgResult.CreateView();
            imgResult.CreateSampler(VkFilter.Nearest, VkFilter.Nearest, VkSamplerMipmapMode.Nearest, VkSamplerAddressMode.ClampToBorder);
            imgResult.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;

            datas = new float[data_size];

            addSeed(imgDim / 2 - 1, imgDim / 2 - 1);


            stagingDataBuff = new HostBuffer <float> (dev, VkBufferUsageFlags.TransferSrc, datas);
            stagingDataBuff.Map();

            inBuff  = new GPUBuffer <float> (dev, VkBufferUsageFlags.StorageBuffer | VkBufferUsageFlags.TransferSrc | VkBufferUsageFlags.TransferDst, (int)data_size);
            outBuff = new GPUBuffer <float> (dev, VkBufferUsageFlags.StorageBuffer | VkBufferUsageFlags.TransferSrc, (int)data_size);

            dsPool = new DescriptorPool(dev, 3,
                                        new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler),
                                        new VkDescriptorPoolSize(VkDescriptorType.StorageBuffer, 4));
            dslImage = new DescriptorSetLayout(dev,
                                               new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                               );
            dslCompute = new DescriptorSetLayout(dev,
                                                 new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer),
                                                 new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer)
                                                 );

            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount1);

            cfg.Layout     = new PipelineLayout(dev, dslImage);
            cfg.RenderPass = new RenderPass(dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat(), VkSampleCountFlags.SampleCount1);
            cfg.RenderPass.ClearValues[0] = new VkClearValue {
                color = new VkClearColorValue(0.0f, 0.1f, 0.0f)
            };

            cfg.ResetShadersAndVerticesInfos();
            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/FullScreenQuad.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/simpletexture.frag.spv");

            cfg.blendAttachments[0] = new VkPipelineColorBlendAttachmentState(true);

            grPipeline = new GraphicPipeline(cfg);

            plCompute = new ComputePipeline(
                new PipelineLayout(dev, new VkPushConstantRange(VkShaderStageFlags.Compute, 2 * sizeof(int)), dslCompute),
                "shaders/computeTest.comp.spv");
            plNormalize = new ComputePipeline(
                plCompute.Layout,
                "shaders/normalize.comp.spv");

            dsImage  = dsPool.Allocate(dslImage);
            dsetPing = dsPool.Allocate(dslCompute);
            dsetPong = dsPool.Allocate(dslCompute);

            DescriptorSetWrites dsUpdate = new DescriptorSetWrites(dsetPing, dslCompute);

            dsUpdate.Write(dev, inBuff.Descriptor, outBuff.Descriptor);
            dsUpdate.Write(dev, dsetPong, outBuff.Descriptor, inBuff.Descriptor);
            dsUpdate = new DescriptorSetWrites(dsImage, dslImage);
            dsUpdate.Write(dev, imgResult.Descriptor);

            UpdateFrequency = 5;
        }
Пример #20
0
        void init(float nearPlane, float farPlane)
        {
            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),
                                                     new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),  //lights
                                                     new VkDescriptorSetLayoutBinding(5, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer)); //materials
#if WITH_SHADOWS
            descLayoutMain.Bindings.Add(new VkDescriptorSetLayoutBinding(6, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));
#endif

            if (TEXTURE_ARRAY)
            {
                descLayoutMain.Bindings.Add(new VkDescriptorSetLayoutBinding(7, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));                  //texture array
                //descLayoutMain.Bindings.Add (new VkDescriptorSetLayoutBinding (8, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));//down sampled hdr
            }
            else
            {
                descLayoutTextures = new DescriptorSetLayout(dev,
                                                             new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                             new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                             new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                             new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                             new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                                             );
            }

            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



            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, NUM_SAMPLES);
            cfg.rasterizationState.cullMode = VkCullModeFlags.Back;
            if (NUM_SAMPLES != VkSampleCountFlags.SampleCount1)
            {
                cfg.multisampleState.sampleShadingEnable = true;
                cfg.multisampleState.minSampleShading    = 0.5f;
            }
            cfg.Cache = pipelineCache;
            if (TEXTURE_ARRAY)
            {
                cfg.Layout = new PipelineLayout(dev, descLayoutMain, descLayoutGBuff);
            }
            else
            {
                cfg.Layout = new PipelineLayout(dev, descLayoutMain, descLayoutGBuff, descLayoutTextures);
            }

            cfg.Layout.AddPushConstants(
                new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf <Matrix4x4> ()),
                new VkPushConstantRange(VkShaderStageFlags.Fragment, sizeof(int), 64)
                );
            cfg.RenderPass   = renderPass;
            cfg.SubpassIndex = SP_MODELS;
            cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
            cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
            cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
            //cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false));

            cfg.AddVertex <PbrModelTexArray.Vertex> ();

            using (SpecializationInfo constants = new SpecializationInfo(
                       new SpecializationConstant <float> (0, nearPlane),
                       new SpecializationConstant <float> (1, farPlane),
                       new SpecializationConstant <float> (2, MAX_MATERIAL_COUNT))) {
                cfg.AddShaders(new ShaderInfo(dev, VkShaderStageFlags.Vertex, "#shaders.GBuffPbr.vert.spv"));
                if (TEXTURE_ARRAY)
                {
                    cfg.AddShaders(new ShaderInfo(dev, VkShaderStageFlags.Fragment, "#shaders.GBuffPbrTexArray.frag.spv", constants));
                }
                else
                {
                    cfg.AddShaders(new ShaderInfo(dev, VkShaderStageFlags.Fragment, "#shaders.GBuffPbr.frag.spv", constants));
                }

                gBuffPipeline = new GraphicPipeline(cfg);
            }
            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))) {
                cfg.AddShaders(new ShaderInfo(dev, VkShaderStageFlags.Vertex, "#vke.FullScreenQuad.vert.spv"));
#if WITH_SHADOWS
                cfg.AddShaders(new ShaderInfo(dev, VkShaderStageFlags.Fragment, "#shaders.compose_with_shadows.frag.spv", constants));
#else
                cfg.AddShader(VkShaderStageFlags.Fragment, "#shaders.compose.frag.spv", constants);
#endif
                composePipeline = new GraphicPipeline(cfg);
            }
            cfg.Shaders[1].Dispose();
            //DEBUG DRAW use subpass of compose
            cfg.Shaders[1]   = new ShaderInfo(dev, VkShaderStageFlags.Fragment, "#shaders.show_gbuff.frag.spv");
            cfg.SubpassIndex = SP_COMPOSE;
            debugPipeline    = new GraphicPipeline(cfg);
            cfg.DisposeShaders();
            ////TONE MAPPING
            //cfg.shaders[1] = new ShaderInfo (VkShaderStageFlags.Fragment, "#shaders.tone_mapping.frag.spv");
            //cfg.SubpassIndex = SP_TONE_MAPPING;
            //toneMappingPipeline = new GraphicPipeline (cfg);

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

            envCube = new EnvironmentCube(cubemapPath, gBuffPipeline.Layout, gQueue, 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);

#if WITH_SHADOWS
            dsMainWrite = new DescriptorSetWrites(dsMain, descLayoutMain.Bindings[6]);
            dsMainWrite.Write(dev, shadowMapRenderer.shadowMap.Descriptor);
#endif
        }
Пример #21
0
        void init(VkSampleCountFlags samples = VkSampleCountFlags.SampleCount4)
        {
            descriptorPool = new DescriptorPool(dev, 2,
                                                new VkDescriptorPoolSize(VkDescriptorType.UniformBuffer),
                                                new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler)
                                                );

            descLayoutMatrix = new DescriptorSetLayout(dev,
                                                       new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),
                                                       new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                                       );

            descLayoutTextures = new DescriptorSetLayout(dev,
                                                         new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                                         );

            dsMats = descriptorPool.Allocate(descLayoutMatrix);

            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, samples);

            cfg.Layout = new PipelineLayout(dev, descLayoutMatrix, descLayoutTextures);
            cfg.Layout.AddPushConstants(
                new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf <Matrix4x4> ()),
                new VkPushConstantRange(VkShaderStageFlags.Fragment, (uint)Marshal.SizeOf <Model.PbrMaterial> (), 64)
                );
            cfg.RenderPass = new RenderPass(dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat(), samples);

            cfg.AddVertexBinding <Model.Vertex> (0);
            cfg.SetVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);

            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/pbrtest.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/pbrtest.frag.spv");

            pipeline = new GraphicPipeline(cfg);

            cfg.ResetShadersAndVerticesInfos();
            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/FullScreenQuad.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/simpletexture.frag.spv");

            cfg.blendAttachments[0] = new VkPipelineColorBlendAttachmentState(true);

            uiPipeline = new GraphicPipeline(cfg);

            uboMats = new HostBuffer(dev, VkBufferUsageFlags.UniformBuffer, (ulong)Marshal.SizeOf <Matrices>());
            uboMats.Map();             //permanent map

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(dsMats, descLayoutMatrix.Bindings[0]);

            uboUpdate.Write(dev, uboMats.Descriptor);

            cfg.Layout.SetName("Main Pipeline layout");
            uboMats.SetName("uboMats");
            descriptorPool.SetName("main pool");
            descLayoutTextures.SetName("descLayoutTextures");

            statPool = new PipelineStatisticsQueryPool(dev,
                                                       VkQueryPipelineStatisticFlags.InputAssemblyVertices |
                                                       VkQueryPipelineStatisticFlags.InputAssemblyPrimitives |
                                                       VkQueryPipelineStatisticFlags.ClippingInvocations |
                                                       VkQueryPipelineStatisticFlags.ClippingPrimitives |
                                                       VkQueryPipelineStatisticFlags.FragmentShaderInvocations);

            timestampQPool = new TimestampQueryPool(dev);
        }
Пример #22
0
        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.Back;
                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);
        }
Пример #23
0
        void init()
        {
            //Shadow map renderpass
            shadowPass = new RenderPass(dev, VkSampleCountFlags.SampleCount1);
            shadowPass.AddAttachment(SHADOWMAP_FORMAT, VkImageLayout.DepthStencilReadOnlyOptimal, SHADOWMAP_NUM_SAMPLES);
            shadowPass.ClearValues.Add(new VkClearValue {
                depthStencil = new VkClearDepthStencilValue(1.0f, 0)
            });

            SubPass subpass0 = new SubPass();

            subpass0.SetDepthReference(0);
            shadowPass.AddSubpass(subpass0);

            shadowPass.AddDependency(Vk.SubpassExternal, 0,
                                     VkPipelineStageFlags.FragmentShader, VkPipelineStageFlags.EarlyFragmentTests,
                                     VkAccessFlags.ShaderRead, VkAccessFlags.DepthStencilAttachmentWrite);
            shadowPass.AddDependency(0, Vk.SubpassExternal,
                                     VkPipelineStageFlags.LateFragmentTests, VkPipelineStageFlags.FragmentShader,
                                     VkAccessFlags.DepthStencilAttachmentWrite, VkAccessFlags.ShaderRead);

            descLayoutShadow = new DescriptorSetLayout(dev,
                                                       new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Geometry, VkDescriptorType.UniformBuffer),  //matrices
                                                       new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Geometry, VkDescriptorType.UniformBuffer)); //lights

            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList);

            cfg.rasterizationState.cullMode        = VkCullModeFlags.Back;
            cfg.rasterizationState.depthBiasEnable = true;
            cfg.dynamicStates.Add(VkDynamicState.DepthBias);

            cfg.RenderPass = shadowPass;

            cfg.Layout = new PipelineLayout(dev, descLayoutShadow);
            cfg.Layout.AddPushConstants(
                new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf <Matrix4x4> ())
                );

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

            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/shadow.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Geometry, "shaders/shadow.geom.spv");

            shadowPipeline = new GraphicPipeline(cfg);

            //shadow map image
            shadowMap = new Image(dev, SHADOWMAP_FORMAT, VkImageUsageFlags.DepthStencilAttachment | VkImageUsageFlags.Sampled, VkMemoryPropertyFlags.DeviceLocal, SHADOWMAP_SIZE, SHADOWMAP_SIZE,
                                  VkImageType.Image2D, SHADOWMAP_NUM_SAMPLES, VkImageTiling.Optimal, 1, (uint)renderer.lights.Length);
            shadowMap.CreateView(VkImageViewType.ImageView2DArray, VkImageAspectFlags.Depth, shadowMap.CreateInfo.arrayLayers);
            shadowMap.CreateSampler(VkSamplerAddressMode.ClampToBorder);
            shadowMap.Descriptor.imageLayout = VkImageLayout.DepthStencilReadOnlyOptimal;

            fbShadowMap = new Framebuffer(shadowPass, SHADOWMAP_SIZE, SHADOWMAP_SIZE, (uint)renderer.lights.Length);
            fbShadowMap.attachments.Add(shadowMap);
            fbShadowMap.Activate();

            dsShadow = descriptorPool.Allocate(descLayoutShadow);

            DescriptorSetWrites dsWrite = new DescriptorSetWrites(dsShadow, descLayoutShadow);

            dsWrite.Write(dev, renderer.uboMatrices.Descriptor, renderer.uboLights.Descriptor);
        }
Пример #24
0
        public Program() : base()
        {
#if DEBUG
            dbgReport = new DebugReport(instance,
                                        VkDebugReportFlagsEXT.ErrorEXT
                                        | VkDebugReportFlagsEXT.DebugEXT
                                        | VkDebugReportFlagsEXT.WarningEXT
                                        | VkDebugReportFlagsEXT.PerformanceWarningEXT

                                        );
#endif
            imgResult = new Image(dev, VkFormat.R32g32b32a32Sfloat, VkImageUsageFlags.TransferDst | VkImageUsageFlags.Sampled, VkMemoryPropertyFlags.DeviceLocal,
                                  IMG_DIM, IMG_DIM);
            imgResult.CreateView();
            imgResult.CreateSampler(VkFilter.Nearest, VkFilter.Nearest, VkSamplerMipmapMode.Nearest, VkSamplerAddressMode.ClampToBorder);
            imgResult.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;


            staggingVBO = new HostBuffer <Vector2> (dev, VkBufferUsageFlags.TransferSrc, MAX_VERTICES);
            staggingVBO.Map();

            vbo = new GPUBuffer <Vector2> (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.StorageBuffer | VkBufferUsageFlags.TransferDst, MAX_VERTICES);
            ibo = new GPUBuffer <uint> (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.StorageBuffer, MAX_VERTICES * 3);

            inBuff  = new GPUBuffer <float> (dev, VkBufferUsageFlags.StorageBuffer | VkBufferUsageFlags.TransferSrc | VkBufferUsageFlags.TransferDst, (int)data_size);
            outBuff = new GPUBuffer <float> (dev, VkBufferUsageFlags.StorageBuffer | VkBufferUsageFlags.TransferSrc | VkBufferUsageFlags.TransferDst, (int)data_size);

            dsPool = new DescriptorPool(dev, 4,
                                        new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler),
                                        new VkDescriptorPoolSize(VkDescriptorType.StorageBuffer, 6));
            dslImage = new DescriptorSetLayout(dev,
                                               new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                               );
            dslCompute = new DescriptorSetLayout(dev,
                                                 new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer),
                                                 new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer)
                                                 );
            dslVAO = new DescriptorSetLayout(dev,
                                             new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer),
                                             new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Compute, VkDescriptorType.StorageBuffer)
                                             );

            plInit = new ComputePipeline(
                new PipelineLayout(dev, new VkPushConstantRange(VkShaderStageFlags.Compute, 3 * sizeof(int)), dslCompute, dslVAO),
                "shaders/init.comp.spv");
            plCompute   = new ComputePipeline(plInit.Layout, "shaders/computeTest.comp.spv");
            plNormalize = new ComputePipeline(plInit.Layout, "shaders/normalize.comp.spv");

            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount1);

            cfg.Layout     = new PipelineLayout(dev, dslImage);
            cfg.RenderPass = new RenderPass(dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat(), VkSampleCountFlags.SampleCount1);
            cfg.RenderPass.ClearValues[0] = new VkClearValue {
                color = new VkClearColorValue(0.1f, 0.1f, 0.1f)
            };
            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/FullScreenQuad.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/simpletexture.frag.spv");

            cfg.blendAttachments[0] = new VkPipelineColorBlendAttachmentState(true);

            grPipeline = new GraphicPipeline(cfg);

            cfg.ResetShadersAndVerticesInfos();
            cfg.Layout = new PipelineLayout(dev, new VkPushConstantRange(VkShaderStageFlags.Vertex, 4 * sizeof(int)));
            cfg.inputAssemblyState.topology = VkPrimitiveTopology.LineStrip;
            cfg.AddVertexBinding <Vector2> (0);
            cfg.SetVertexAttributes(0, VkFormat.R32g32Sfloat);
            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/triangle.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/triangle.frag.spv");

            trianglesPipeline = new GraphicPipeline(cfg);

            dsImage = dsPool.Allocate(dslImage);
            dsPing  = dsPool.Allocate(dslCompute);
            dsPong  = dsPool.Allocate(dslCompute);
            dsVAO   = dsPool.Allocate(dslCompute);


            DescriptorSetWrites dsUpdate = new DescriptorSetWrites(dsPing, dslCompute);
            dsUpdate.Write(dev, inBuff.Descriptor, outBuff.Descriptor);
            dsUpdate.Write(dev, dsPong, outBuff.Descriptor, inBuff.Descriptor);
            dsUpdate = new DescriptorSetWrites(dsImage, dslImage);
            dsUpdate.Write(dev, imgResult.Descriptor);
            dsUpdate = new DescriptorSetWrites(dsVAO, dslVAO);
            dsUpdate.Write(dev, vbo.Descriptor, ibo.Descriptor);

            UpdateFrequency = 5;

            addPoint(IMG_DIM / 2 - 1, IMG_DIM / 2 - 1);
        }
Пример #25
0
        public Image generateCubeMap(Queue staggingQ, CommandPool cmdPool, CBTarget target)
        {
            const float deltaPhi   = (2.0f * (float)Math.PI) / 180.0f;
            const float deltaTheta = (0.5f * (float)Math.PI) / 64.0f;

            VkFormat format = VkFormat.R32g32b32a32Sfloat;
            uint     dim    = 64;

            if (target == CBTarget.PREFILTEREDENV)
            {
                format = VkFormat.R16g16b16a16Sfloat;
                dim    = 512;
            }

            uint numMips = (uint)Math.Floor(Math.Log(dim, 2)) + 1;

            Image imgFbOffscreen = new Image(Dev, format, VkImageUsageFlags.TransferSrc | VkImageUsageFlags.ColorAttachment,
                                             VkMemoryPropertyFlags.DeviceLocal, dim, dim);

            imgFbOffscreen.SetName("offscreenfb");
            imgFbOffscreen.CreateView();

            Image cmap = new Image(Dev, format, VkImageUsageFlags.TransferDst | VkImageUsageFlags.Sampled,
                                   VkMemoryPropertyFlags.DeviceLocal, dim, dim, VkImageType.Image2D, VkSampleCountFlags.SampleCount1, VkImageTiling.Optimal,
                                   numMips, 6, 1, VkImageCreateFlags.CubeCompatible);

            if (target == CBTarget.PREFILTEREDENV)
            {
                cmap.SetName("prefilterenvmap");
            }
            else
            {
                cmap.SetName("irradianceCube");
            }
            cmap.CreateView(VkImageViewType.Cube, VkImageAspectFlags.Color, 6, 0);
            cmap.CreateSampler(VkSamplerAddressMode.ClampToEdge);

            DescriptorPool dsPool = new DescriptorPool(Dev, 2, new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler));

            DescriptorSetLayout dsLayout = new DescriptorSetLayout(Dev,
                                                                   new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));


            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount1, false);

            cfg.Layout = new PipelineLayout(Dev, dsLayout);
            cfg.Layout.AddPushConstants(
                new VkPushConstantRange(VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, (uint)Marshal.SizeOf <Matrix4x4> () + 8));

            cfg.RenderPass = new RenderPass(Dev);
            cfg.RenderPass.AddAttachment(format, VkImageLayout.ColorAttachmentOptimal);
            cfg.RenderPass.ClearValues.Add(new VkClearValue {
                color = new VkClearColorValue(0, 0, 0)
            });
            cfg.RenderPass.AddSubpass(new SubPass(VkImageLayout.ColorAttachmentOptimal));

            cfg.AddVertexBinding(0, 3 * sizeof(float));
            cfg.AddVertexAttributes(0, VkFormat.R32g32b32Sfloat);

            cfg.AddShaders(new ShaderInfo(Dev, VkShaderStageFlags.Vertex, "#EnvironmentPipeline.filtercube.vert.spv"));
            if (target == CBTarget.PREFILTEREDENV)
            {
                cfg.AddShaders(new ShaderInfo(Dev, VkShaderStageFlags.Fragment, "#EnvironmentPipeline.prefilterenvmap.frag.spv"));
            }
            else
            {
                cfg.AddShaders(new ShaderInfo(Dev, VkShaderStageFlags.Fragment, "#EnvironmentPipeline.irradiancecube.frag.spv"));
            }

            Matrix4x4[] matrices =
            {
                // POSITIVE_X
                Matrix4x4.CreateRotationX(Utils.DegreesToRadians(180)) * Matrix4x4.CreateRotationY(Utils.DegreesToRadians(90)),
                // NEGATIVE_X
                Matrix4x4.CreateRotationX(Utils.DegreesToRadians(180)) * Matrix4x4.CreateRotationY(Utils.DegreesToRadians(-90)),
                // POSITIVE_Y
                Matrix4x4.CreateRotationX(Utils.DegreesToRadians(-90)),
                // NEGATIVE_Y
                Matrix4x4.CreateRotationX(Utils.DegreesToRadians(90)),
                // POSITIVE_Z
                Matrix4x4.CreateRotationX(Utils.DegreesToRadians(180)),
                // NEGATIVE_Z
                Matrix4x4.CreateRotationZ(Utils.DegreesToRadians(180))
            };

            VkImageSubresourceRange subRes = new VkImageSubresourceRange(VkImageAspectFlags.Color, 0, numMips, 0, 6);

            using (GraphicPipeline pl = new GraphicPipeline(cfg)) {
                cfg.Dispose();
                DescriptorSet       dset     = dsPool.Allocate(dsLayout);
                DescriptorSetWrites dsUpdate = new DescriptorSetWrites(dsLayout);
                dsUpdate.Write(Dev, dset, cubemap.Descriptor);
                Dev.WaitIdle();

                using (FrameBuffer fb = new FrameBuffer(pl.RenderPass, dim, dim, imgFbOffscreen)) {
                    PrimaryCommandBuffer cmd = cmdPool.AllocateCommandBuffer();
                    cmd.Start(VkCommandBufferUsageFlags.OneTimeSubmit);

                    cmap.SetLayout(cmd, VkImageLayout.Undefined, VkImageLayout.TransferDstOptimal, subRes);

                    float roughness = 0;

                    cmd.SetScissor(dim, dim);
                    cmd.SetViewport((float)(dim), (float)dim);

                    for (int m = 0; m < numMips; m++)
                    {
                        roughness = (float)m / ((float)numMips - 1f);

                        for (int f = 0; f < 6; f++)
                        {
                            pl.RenderPass.Begin(cmd, fb);

                            pl.Bind(cmd);

                            float viewPortSize = (float)Math.Pow(0.5, m) * dim;
                            cmd.SetViewport(viewPortSize, viewPortSize);
                            cmd.PushConstant(pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment,
                                             matrices[f] * Matrix4x4.CreatePerspectiveFieldOfView(Utils.DegreesToRadians(90), 1f, 0.1f, 512f));
                            if (target == CBTarget.IRRADIANCE)
                            {
                                cmd.PushConstant(pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, deltaPhi, (uint)Marshal.SizeOf <Matrix4x4> ());
                                cmd.PushConstant(pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, deltaTheta, (uint)Marshal.SizeOf <Matrix4x4> () + 4);
                            }
                            else
                            {
                                cmd.PushConstant(pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, roughness, (uint)Marshal.SizeOf <Matrix4x4> ());
                                cmd.PushConstant(pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, 64u, (uint)Marshal.SizeOf <Matrix4x4> () + 4);
                            }

                            cmd.BindDescriptorSet(pl.Layout, dset);
                            cmd.BindVertexBuffer(vboSkybox);
                            cmd.Draw(36);

                            pl.RenderPass.End(cmd);

                            imgFbOffscreen.SetLayout(cmd, VkImageAspectFlags.Color,
                                                     VkImageLayout.ColorAttachmentOptimal, VkImageLayout.TransferSrcOptimal);

                            VkImageCopy region = new VkImageCopy();
                            region.srcSubresource = new VkImageSubresourceLayers(VkImageAspectFlags.Color, 1);
                            region.dstSubresource = new VkImageSubresourceLayers(VkImageAspectFlags.Color, 1, (uint)m, (uint)f);
                            region.extent         = new VkExtent3D {
                                width = (uint)viewPortSize, height = (uint)viewPortSize, depth = 1
                            };

                            Vk.vkCmdCopyImage(cmd.Handle,
                                              imgFbOffscreen.Handle, VkImageLayout.TransferSrcOptimal,
                                              cmap.Handle, VkImageLayout.TransferDstOptimal,
                                              1, region.Pin());
                            region.Unpin();

                            imgFbOffscreen.SetLayout(cmd, VkImageAspectFlags.Color,
                                                     VkImageLayout.TransferSrcOptimal, VkImageLayout.ColorAttachmentOptimal);
                        }
                    }

                    cmap.SetLayout(cmd, VkImageLayout.TransferDstOptimal, VkImageLayout.ShaderReadOnlyOptimal, subRes);

                    cmd.End();

                    staggingQ.Submit(cmd);
                    staggingQ.WaitIdle();

                    cmd.Free();
                }
            }

            cmap.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;

            dsLayout.Dispose();
            imgFbOffscreen.Dispose();
            dsPool.Dispose();

            return(cmap);
        }
Пример #26
0
        void init()
        {
            renderPass = new RenderPass(dev);
            renderPass.AddAttachment(swapChain.ColorFormat, VkImageLayout.ColorAttachmentOptimal, VkSampleCountFlags.SampleCount1);
            renderPass.AddAttachment(dev.GetSuitableDepthFormat(), VkImageLayout.DepthStencilAttachmentOptimal, samples);
            renderPass.AddAttachment(VkFormat.R8g8b8a8Unorm, VkImageLayout.ColorAttachmentOptimal);
            renderPass.AddAttachment(VkFormat.R8g8b8a8Unorm, VkImageLayout.ColorAttachmentOptimal);
            renderPass.AddAttachment(VkFormat.R16g16b16a16Sfloat, VkImageLayout.ColorAttachmentOptimal);
            renderPass.AddAttachment(VkFormat.R16g16b16a16Sfloat, VkImageLayout.ColorAttachmentOptimal);

            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)
            });

            SubPass[] subpass = { new SubPass(), new SubPass() };
            subpass[0].AddColorReference(new VkAttachmentReference(2, VkImageLayout.ColorAttachmentOptimal),
                                         new VkAttachmentReference(3, VkImageLayout.ColorAttachmentOptimal),
                                         new VkAttachmentReference(4, VkImageLayout.ColorAttachmentOptimal),
                                         new VkAttachmentReference(5, VkImageLayout.ColorAttachmentOptimal));
            subpass[0].SetDepthReference(1, VkImageLayout.DepthStencilAttachmentOptimal);

            subpass[1].AddColorReference(0, VkImageLayout.ColorAttachmentOptimal);
            subpass[1].AddInputReference(new VkAttachmentReference(2, VkImageLayout.ShaderReadOnlyOptimal),
                                         new VkAttachmentReference(3, VkImageLayout.ShaderReadOnlyOptimal),
                                         new VkAttachmentReference(4, VkImageLayout.ShaderReadOnlyOptimal),
                                         new VkAttachmentReference(5, VkImageLayout.ShaderReadOnlyOptimal));
            renderPass.AddSubpass(subpass);

            renderPass.AddDependency(Vk.SubpassExternal, 0,
                                     VkPipelineStageFlags.BottomOfPipe, VkPipelineStageFlags.ColorAttachmentOutput,
                                     VkAccessFlags.MemoryRead, VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite);
            renderPass.AddDependency(0, 1,
                                     VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.FragmentShader,
                                     VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.ShaderRead);
            renderPass.AddDependency(1, Vk.SubpassExternal,
                                     VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.BottomOfPipe,
                                     VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.MemoryRead);


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

            descLayoutMain = new DescriptorSetLayout(dev,
                                                     new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),
                                                     new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));

            descLayoutModelTextures = new DescriptorSetLayout(dev,
                                                              new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                              new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                              new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                              new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                              new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                                              );

            descLayoutGBuff = new DescriptorSetLayout(dev,
                                                      new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),
                                                      new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),
                                                      new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),
                                                      new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment));

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

            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, samples);

            cfg.Layout = new PipelineLayout(dev, descLayoutMain, descLayoutModelTextures, descLayoutGBuff);
            cfg.Layout.AddPushConstants(
                new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf <Matrix4x4> ()),
                new VkPushConstantRange(VkShaderStageFlags.Fragment, sizeof(int), 64)
                );
            cfg.RenderPass = renderPass;
            cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
            cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
            cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));

            cfg.AddVertexBinding <Model.Vertex> (0);
            cfg.SetVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);
            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/pbrtest.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/GBuffPbr.frag.spv");

            gBuffPipeline = new GraphicPipeline(cfg);
            cfg.blendAttachments.Clear();
            cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
            cfg.ResetShadersAndVerticesInfos();
            cfg.SubpassIndex = 1;
            cfg.Layout       = gBuffPipeline.Layout;
            cfg.depthStencilState.depthTestEnable  = false;
            cfg.depthStencilState.depthWriteEnable = false;
            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/FullScreenQuad.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/pbrtest.frag.spv");
            composePipeline = new GraphicPipeline(cfg);

            envCube = new EnvironmentCube(presentQueue, renderPass);

            uboMats = new HostBuffer(dev, VkBufferUsageFlags.UniformBuffer, (ulong)Marshal.SizeOf <Matrices> () * 2);
            uboMats.Map();             //permanent map

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(descLayoutMain);

            uboUpdate.Write(dev, dsMain, uboMats.Descriptor,
                            envCube.lutBrdf.Descriptor,
                            envCube.irradianceCube.Descriptor,
                            envCube.prefilterCube.Descriptor);
            uboMats.Descriptor.offset = (ulong)Marshal.SizeOf <Matrices> ();
            envCube.WriteDesc(uboMats.Descriptor);
#if DEBUG
            debugDraw = new DebugDrawPipeline(dev, descLayoutMain, swapChain.ColorFormat);
            debugDraw.AddLine(Vector3.Zero, new Vector3(matrices.lightPos.X, matrices.lightPos.Y, matrices.lightPos.Z) * 3, 1, 1, 1);
            debugDraw.AddLine(Vector3.Zero, Vector3.UnitX, 1, 0, 0);
            debugDraw.AddLine(Vector3.Zero, Vector3.UnitY, 0, 1, 0);
            debugDraw.AddLine(Vector3.Zero, Vector3.UnitZ, 0, 0, 1);
#endif


            model = new Model(presentQueue, "../data/models/DamagedHelmet/glTF/DamagedHelmet.gltf");
            //model = new Model (presentQueue, "../data/models/chess.gltf");
            //model = new Model (presentQueue, "../data/models/Sponza/glTF/Sponza.gltf");
            //model = new Model (dev, presentQueue, "../data/models/icosphere.gltf");
            //model = new Model (dev, presentQueue, cmdPool, "../data/models/cube.gltf");
            model.WriteMaterialsDescriptorSets(descLayoutModelTextures,
                                               VK.AttachmentType.Color,
                                               VK.AttachmentType.Normal,
                                               VK.AttachmentType.AmbientOcclusion,
                                               VK.AttachmentType.PhysicalProps,
                                               VK.AttachmentType.Emissive);
        }