Exemple #1
0
        // Prepare and initialize uniform buffer containing shader uniforms
        void prepareUniformBuffers()
        {
            // Phong and color pass vertex shader uniform buffer
            Util.CheckResult(vulkanDevice.createBuffer(
                                 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
                                 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                                 uniformBuffers_scene,
                                 (ulong)sizeof(UboVS)));

            // Fullscreen radial blur parameters
            var blurParamsLocal = uboBlurParams;

            Util.CheckResult(vulkanDevice.createBuffer(
                                 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
                                 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                                 uniformBuffers_blurParams,
                                 (ulong)sizeof(UboBlurParams),
                                 &blurParamsLocal));

            // Map persistent
            Util.CheckResult(uniformBuffers_scene.map());
            Util.CheckResult(uniformBuffers_blurParams.map());

            updateUniformBuffersScene();
        }
Exemple #2
0
        void setupDescriptorSetLayout()
        {
            // Binding 0 : Vertex shader uniform buffer
            VkDescriptorSetLayoutBinding setLayoutBinding = new VkDescriptorSetLayoutBinding()
            {
                descriptorType  = VkDescriptorType.UniformBuffer,
                stageFlags      = VkShaderStageFlags.Vertex,
                binding         = 0,
                descriptorCount = 1
            };

            VkDescriptorSetLayoutCreateInfo descriptorLayout = VkDescriptorSetLayoutCreateInfo.New();

            descriptorLayout.pBindings    = &setLayoutBinding;
            descriptorLayout.bindingCount = 1;

            descriptorSetLayout.Count = 1;
            Util.CheckResult(vkCreateDescriptorSetLayout(Device, &descriptorLayout, null, (VkDescriptorSetLayout *)descriptorSetLayout.GetAddress(0)));

            VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = VkPipelineLayoutCreateInfo.New();

            pPipelineLayoutCreateInfo.setLayoutCount = 1;
            pPipelineLayoutCreateInfo.pSetLayouts    = (VkDescriptorSetLayout *)descriptorSetLayout.Data.ToPointer();

            Util.CheckResult(vkCreatePipelineLayout(Device, &pPipelineLayoutCreateInfo, null, out pipelineLayout));
        }
Exemple #3
0
        void setupDescriptorSet()
        {
            var dsl = descriptorSetLayout;
            VkDescriptorSetAllocateInfo allocInfo =
                Initializers.descriptorSetAllocateInfo(
                    descriptorPool,
                    &dsl,
                    1);

            Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSet));

            VkDescriptorImageInfo texDescriptor =
                Initializers.descriptorImageInfo(
                    textures_colorMap.sampler,
                    textures_colorMap.view,
                    VkImageLayout.General);

            var temp = uniformBuffers_scene.descriptor;
            FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>(
                // Binding 0 : Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    descriptorSet,
                    VkDescriptorType.UniformBuffer,
                    0,
                    &temp),
                // Binding 1 : Color map
                Initializers.writeDescriptorSet(
                    descriptorSet,
                    VkDescriptorType.CombinedImageSampler,
                    1,
                    &texDescriptor));

            vkUpdateDescriptorSets(device, (writeDescriptorSets.Count), ref writeDescriptorSets.First, 0, null);
        }
Exemple #4
0
        void draw()
        {
            prepareFrame();

            // Offscreen rendering

            // Wait for swap chain presentation to finish
            submitInfo.pWaitSemaphores = (VkSemaphore *)Unsafe.AsPointer(ref semaphores[0].PresentComplete);
            // Signal ready with offscreen semaphore
            var signalSemaphore = offscreenPass.semaphore;

            submitInfo.pSignalSemaphores = &signalSemaphore;

            // Submit work
            submitInfo.commandBufferCount = 1;
            var commandBuffer = offscreenPass.commandBuffer;

            submitInfo.pCommandBuffers = &commandBuffer;
            Util.CheckResult(vkQueueSubmit(queue, 1, ref submitInfo, VkFence.Null));

            // Scene rendering

            // Wait for offscreen semaphore
            var semaphore = offscreenPass.semaphore;

            submitInfo.pWaitSemaphores = &semaphore;
            // Signal ready with render complete semaphpre
            submitInfo.pSignalSemaphores = (VkSemaphore *)Unsafe.AsPointer(ref semaphores[0].RenderComplete);

            // Submit work
            submitInfo.pCommandBuffers = (VkCommandBuffer *)drawCmdBuffers.GetAddress(currentBuffer);
            Util.CheckResult(vkQueueSubmit(queue, 1, ref submitInfo, VkFence.Null));

            submitFrame();
        }
        void setupDescriptorSetLayout()
        {
            FixedArray2<VkDescriptorSetLayoutBinding> setLayoutBindings = new FixedArray2<VkDescriptorSetLayoutBinding>(
                // Binding 0 : Vertex shader uniform buffer
                Initializers.descriptorSetLayoutBinding(
                    VkDescriptorType.UniformBuffer,
                    VkShaderStageFlags.Vertex,
                    0),
                // Binding 1 : Fragment shader image sampler
                Initializers.descriptorSetLayoutBinding(
                    VkDescriptorType.CombinedImageSampler,
                    VkShaderStageFlags.Fragment,
                    1));

            VkDescriptorSetLayoutCreateInfo descriptorLayout =
                Initializers.descriptorSetLayoutCreateInfo(
                    &setLayoutBindings.First,
                    setLayoutBindings.Count);

            Util.CheckResult(vkCreateDescriptorSetLayout(device, &descriptorLayout, null, out descriptorSetLayout));

            var dsl = descriptorSetLayout;
            VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
                Initializers.pipelineLayoutCreateInfo(
                    &dsl,
                    1);

            Util.CheckResult(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, null, out pipelineLayout));
        }
Exemple #6
0
        /**
         * Finish command buffer recording and submit it to a queue
         *
         * @param commandBuffer Command buffer to flush
         * @param queue Queue to submit the command buffer to
         * @param free (Optional) Free the command buffer once it has been submitted (Defaults to true)
         *
         * @note The queue that the command buffer is submitted to must be from the same family index as the pool it was allocated from
         * @note Uses a fence to ensure command buffer has finished executing
         */
        public void flushCommandBuffer(VkCommandBuffer commandBuffer, VkQueue queue, bool free = true)
        {
            if (commandBuffer.Handle == NullHandle)
            {
                return;
            }

            Util.CheckResult(vkEndCommandBuffer(commandBuffer));

            VkSubmitInfo submitInfo = VkSubmitInfo.New();

            submitInfo.commandBufferCount = 1;
            submitInfo.pCommandBuffers    = &commandBuffer;

            // Create fence to ensure that the command buffer has finished executing
            VkFenceCreateInfo fenceInfo = VkFenceCreateInfo.New();

            fenceInfo.flags = VkFenceCreateFlags.None;
            VkFence fence;

            Util.CheckResult(vkCreateFence(_logicalDevice, &fenceInfo, null, &fence));

            // Submit to the queue
            Util.CheckResult(vkQueueSubmit(queue, 1, &submitInfo, fence));
            // Wait for the fence to signal that command buffer has finished executing
            Util.CheckResult(vkWaitForFences(_logicalDevice, 1, &fence, True, DEFAULT_FENCE_TIMEOUT));

            vkDestroyFence(_logicalDevice, fence, null);

            if (free)
            {
                vkFreeCommandBuffers(_logicalDevice, CommandPool, 1, &commandBuffer);
            }
        }
Exemple #7
0
        protected virtual void SetupFrameBuffer()
        {
            using (NativeList <VkImageView> attachments = new NativeList <VkImageView>(2))
            {
                attachments.Count = 2;
                // Depth/Stencil attachment is the same for all frame buffers
                attachments[1] = DepthStencil.View;

                VkFramebufferCreateInfo frameBufferCreateInfo = VkFramebufferCreateInfo.New();
                frameBufferCreateInfo.renderPass      = renderPass;
                frameBufferCreateInfo.attachmentCount = 2;
                frameBufferCreateInfo.pAttachments    = (VkImageView *)attachments.Data;
                frameBufferCreateInfo.width           = width;
                frameBufferCreateInfo.height          = height;
                frameBufferCreateInfo.layers          = 1;

                // Create frame buffers for every swap chain image
                frameBuffers.Count = (Swapchain.ImageCount);
                for (uint i = 0; i < frameBuffers.Count; i++)
                {
                    attachments[0] = Swapchain.Buffers[i].View;
                    Util.CheckResult(vkCreateFramebuffer(device, ref frameBufferCreateInfo, null, (VkFramebuffer *)Unsafe.AsPointer(ref frameBuffers[i])));
                }
            }
        }
Exemple #8
0
        void generateQuad()
        {
            // Setup vertices for a single uv-mapped quad

            Vector3 defaultColor = new Vector3(1, 1, 1);
            Vector3 defaultNormal = new Vector3(0, 0, 1);

            FixedArray4<Vertex> vertexBuffer = new FixedArray4<Vertex>(
                new Vertex { pos = new Vector3(1, 1, 0), uv = new Vector2(1, 1), col = defaultColor, normal = defaultNormal },
                new Vertex { pos = new Vector3(0, 1, 0), uv = new Vector2(0, 1), col = defaultColor, normal = defaultNormal },
                new Vertex { pos = new Vector3(0, 0, 0), uv = new Vector2(0, 0), col = defaultColor, normal = defaultNormal },
                new Vertex { pos = new Vector3(1, 0, 0), uv = new Vector2(1, 0), col = defaultColor, normal = defaultNormal });

            Util.CheckResult(vulkanDevice.createBuffer(
                VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
                VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                (ulong)(vertexBuffer.Count * sizeof(Vertex)),
                out models_quad.vertices.buffer,
                out models_quad.vertices.memory,
                &vertexBuffer.First));

            // Setup indices
            FixedArray6<uint> indexBuffer = new FixedArray6<uint>(0, 1, 2, 2, 3, 0);
            models_quad.indexCount = indexBuffer.Count;

            Util.CheckResult(vulkanDevice.createBuffer(
                VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
                VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                indexBuffer.Count * sizeof(uint),
                out models_quad.indices.buffer,
                out models_quad.indices.memory,
                &indexBuffer.First));

            models_quad.device = device;
        }
Exemple #9
0
        protected override void buildCommandBuffers()
        {
            VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo();

            FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>();

            clearValues.First.color         = defaultClearColor;
            clearValues.Second.depthStencil = new VkClearDepthStencilValue {
                depth = 1.0f, stencil = 0
            };

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

            renderPassBeginInfo.renderPass               = renderPass;
            renderPassBeginInfo.renderArea.offset.x      = 0;
            renderPassBeginInfo.renderArea.offset.y      = 0;
            renderPassBeginInfo.renderArea.extent.width  = width;
            renderPassBeginInfo.renderArea.extent.height = height;
            renderPassBeginInfo.clearValueCount          = 2;
            renderPassBeginInfo.pClearValues             = &clearValues.First;

            for (int i = 0; i < drawCmdBuffers.Count; ++i)
            {
                // Set target frame buffer
                renderPassBeginInfo.framebuffer = frameBuffers[i];

                Util.CheckResult(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));

                vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

                VkViewport viewport = Initializers.viewport((float)width, (float)height, 0.0f, 1.0f);
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);

                VkRect2D scissor = Initializers.rect2D(width, height, 0, 0);
                vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);

                ulong offsets = 0;

                // 3D scene
                vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts_scene, 0, 1, ref descriptorSets_scene, 0, null);
                vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_phongPass);

                vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_example.vertices.buffer, &offsets);
                vkCmdBindIndexBuffer(drawCmdBuffers[i], models_example.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
                vkCmdDrawIndexed(drawCmdBuffers[i], models_example.indexCount, 1, 0, 0, 0);

                // Fullscreen triangle (clipped to a quad) with radial blur
                if (blur)
                {
                    vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts_radialBlur, 0, 1, ref descriptorSets_radialBlur, 0, null);
                    vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, (displayTexture) ? pipelines_offscreenDisplay : pipelines_radialBlur);
                    vkCmdDraw(drawCmdBuffers[i], 3, 1, 0, 0);
                }

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i]));
            }
        }
Exemple #10
0
        void setupDescriptorSets()
        {
            // Image descriptor for the cube map texture
            VkDescriptorImageInfo textureDescriptor =
                Initializers.descriptorImageInfo(
                    cubeMap.sampler,
                    cubeMap.view,
                    cubeMap.imageLayout);

            var dsl = descriptorSetLayout;
            VkDescriptorSetAllocateInfo allocInfo =
                Initializers.descriptorSetAllocateInfo(
                    descriptorPool,
                    &dsl,
                    1);

            // 3D object descriptor set
            Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSets_object));

            var bufferInfo = uniformBuffers_object.descriptor;
            FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>
                                                                     (
                // Binding 0 : Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    descriptorSets_object,
                    VkDescriptorType.UniformBuffer,
                    0,
                    &bufferInfo),
                // Binding 1 : Fragment shader cubemap sampler
                Initializers.writeDescriptorSet(
                    descriptorSets_object,
                    VkDescriptorType.CombinedImageSampler,
                    1,
                    &textureDescriptor));

            vkUpdateDescriptorSets(device, writeDescriptorSets.Count, ref writeDescriptorSets.First, 0, null);

            // Sky box descriptor set
            Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSets_skybox));

            var descriptor = uniformBuffers_skybox.descriptor;

            writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>
                                  (
                // Binding 0 : Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    descriptorSets_skybox,
                    VkDescriptorType.UniformBuffer,
                    0,
                    &descriptor),
                // Binding 1 : Fragment shader cubemap sampler
                Initializers.writeDescriptorSet(
                    descriptorSets_skybox,
                    VkDescriptorType.CombinedImageSampler,
                    1,
                    &textureDescriptor));

            vkUpdateDescriptorSets(device, writeDescriptorSets.Count, ref writeDescriptorSets.First, 0, null);
        }
Exemple #11
0
        private void CreateCommandPool()
        {
            VkCommandPoolCreateInfo cmdPoolInfo = VkCommandPoolCreateInfo.New();

            cmdPoolInfo.queueFamilyIndex = Swapchain.QueueNodeIndex;
            cmdPoolInfo.flags            = VkCommandPoolCreateFlags.ResetCommandBuffer;
            Util.CheckResult(vkCreateCommandPool(device, &cmdPoolInfo, null, out _cmdPool));
        }
Exemple #12
0
        // Sets up the command buffer that renders the scene to the offscreen frame buffer
        void buildOffscreenCommandBuffer()
        {
            if (offscreenPass.commandBuffer == NullHandle)
            {
                offscreenPass.commandBuffer = createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false);
            }
            if (offscreenPass.semaphore == NullHandle)
            {
                VkSemaphoreCreateInfo semaphoreCreateInfo = Initializers.semaphoreCreateInfo();
                Util.CheckResult(vkCreateSemaphore(device, &semaphoreCreateInfo, null, out offscreenPass.semaphore));
            }

            VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo();

            FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>();

            clearValues.First.color = new VkClearColorValue {
                float32_0 = 0.0f, float32_1 = 0.0f, float32_2 = 0.0f, float32_3 = 0.0f
            };
            clearValues.Second.depthStencil = new VkClearDepthStencilValue {
                depth = 1.0f, stencil = 0
            };

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

            renderPassBeginInfo.renderPass               = offscreenPass.renderPass;
            renderPassBeginInfo.framebuffer              = offscreenPass.frameBuffer;
            renderPassBeginInfo.renderArea.extent.width  = offscreenPass.width;
            renderPassBeginInfo.renderArea.extent.height = offscreenPass.height;
            renderPassBeginInfo.clearValueCount          = 2;
            renderPassBeginInfo.pClearValues             = &clearValues.First;

            Util.CheckResult(vkBeginCommandBuffer(offscreenPass.commandBuffer, &cmdBufInfo));

            VkViewport viewport = Initializers.viewport((float)offscreenPass.width, (float)offscreenPass.height, 0.0f, 1.0f);

            vkCmdSetViewport(offscreenPass.commandBuffer, 0, 1, &viewport);

            VkRect2D scissor = Initializers.rect2D(offscreenPass.width, offscreenPass.height, 0, 0);

            vkCmdSetScissor(offscreenPass.commandBuffer, 0, 1, &scissor);

            vkCmdBeginRenderPass(offscreenPass.commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

            vkCmdBindDescriptorSets(offscreenPass.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts_scene, 0, 1, ref descriptorSets_scene, 0, null);
            vkCmdBindPipeline(offscreenPass.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_colorPass);

            ulong offsets = 0;

            vkCmdBindVertexBuffers(offscreenPass.commandBuffer, VERTEX_BUFFER_BIND_ID, 1, ref models_example.vertices.buffer, &offsets);
            vkCmdBindIndexBuffer(offscreenPass.commandBuffer, models_example.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
            vkCmdDrawIndexed(offscreenPass.commandBuffer, models_example.indexCount, 1, 0, 0, 0);

            vkCmdEndRenderPass(offscreenPass.commandBuffer);

            Util.CheckResult(vkEndCommandBuffer(offscreenPass.commandBuffer));
        }
        protected override void buildCommandBuffers()
        {
            VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo();

            FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>();

            clearValues.First.color         = defaultClearColor;
            clearValues.Second.depthStencil = new VkClearDepthStencilValue {
                depth = 1.0f, stencil = 0
            };

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

            renderPassBeginInfo.renderPass               = renderPass;
            renderPassBeginInfo.renderArea.offset.x      = 0;
            renderPassBeginInfo.renderArea.offset.y      = 0;
            renderPassBeginInfo.renderArea.extent.width  = width;
            renderPassBeginInfo.renderArea.extent.height = height;
            renderPassBeginInfo.clearValueCount          = 2;
            renderPassBeginInfo.pClearValues             = &clearValues.First;

            for (int i = 0; i < drawCmdBuffers.Count; ++i)
            {
                renderPassBeginInfo.framebuffer = frameBuffers[i];

                Util.CheckResult(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));

                vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

                VkViewport viewport = Initializers.viewport((float)width, (float)height, 0.0f, 1.0f);
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);

                VkRect2D scissor = Initializers.rect2D(width, height, 0, 0);
                vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);

                vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);

                ulong offsets = 0;
                vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref vertexBuffer.buffer, &offsets);
                vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);

                // Render multiple objects using different model matrices by dynamically offsetting into one uniform buffer
                for (uint j = 0; j < OBJECT_INSTANCES; j++)
                {
                    // One dynamic offset per dynamic descriptor to offset into the ubo containing all model matrices
                    uint dynamicOffset = j * (uint)(dynamicAlignment);
                    // Bind the descriptor set for rendering a mesh using the dynamic offset
                    vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, ref descriptorSet, 1, &dynamicOffset);

                    vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);
                }

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i]));
            }
        }
        protected override void buildCommandBuffers()
        {
            VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo();

            FixedArray2<VkClearValue> clearValues = new FixedArray2<VkClearValue>();
            clearValues.First.color = defaultClearColor;
            clearValues.Second.depthStencil = new VkClearDepthStencilValue { depth = 1.0f, stencil = 0 };

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();
            renderPassBeginInfo.renderPass = renderPass;
            renderPassBeginInfo.renderArea.offset.x = 0;
            renderPassBeginInfo.renderArea.offset.y = 0;
            renderPassBeginInfo.renderArea.extent.width = width;
            renderPassBeginInfo.renderArea.extent.height = height;
            renderPassBeginInfo.clearValueCount = 2;
            renderPassBeginInfo.pClearValues = &clearValues.First;

            for (int i = 0; i < drawCmdBuffers.Count; ++i)
            {
                // Set target frame buffer
                renderPassBeginInfo.framebuffer = frameBuffers[i];

                Util.CheckResult(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));

                vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VkSubpassContents.Inline);

                VkViewport viewport = Initializers.viewport((float)width, (float)height, 0.0f, 1.0f);
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);

                VkRect2D scissor = Initializers.rect2D(width, height, 0, 0);
                vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);

                ulong offsets = 0;

                // Skybox
                if (displaySkybox)
                {
                    vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSets_skybox, 0, null);
                    vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_skybox.vertices.buffer, ref offsets);
                    vkCmdBindIndexBuffer(drawCmdBuffers[i], models_skybox.indices.buffer, 0, VkIndexType.Uint32);
                    vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_skybox);
                    vkCmdDrawIndexed(drawCmdBuffers[i], models_skybox.indexCount, 1, 0, 0, 0);
                }

                // 3D object
                vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSets_object, 0, null);
                var vbuffer = models_objects[(int)models_objectIndex].vertices.buffer;
                vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vbuffer, ref offsets);
                vkCmdBindIndexBuffer(drawCmdBuffers[i], models_objects[(int)models_objectIndex].indices.buffer, 0, VkIndexType.Uint32);
                vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_reflect);
                vkCmdDrawIndexed(drawCmdBuffers[i], models_objects[(int)models_objectIndex].indexCount, 1, 0, 0, 0);

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i]));
            }
        }
        void draw()
        {
            prepareFrame();

            submitInfo.commandBufferCount = 1;
            submitInfo.pCommandBuffers = (VkCommandBuffer*)drawCmdBuffers.GetAddress(currentBuffer);
            Util.CheckResult(vkQueueSubmit(queue, 1, ref submitInfo, VkFence.Null));

            submitFrame();
        }
Exemple #16
0
        void draw()
        {
            prepareFrame();

            SubmitInfo.commandBufferCount = 1;
            SubmitInfo.pCommandBuffers    = (VkCommandBuffer *)DrawCmdBuffers.GetAddress(currentBuffer);
            Util.CheckResult(vkQueueSubmit(Queue, 1, ref SubmitInfo, NullHandle));

            submitFrame();
        }
Exemple #17
0
        void draw()
        {
            base.prepareFrame();

            SubmitInfo.commandBufferCount = 1;
            SubmitInfo.pCommandBuffers    = (VkCommandBuffer *)DrawCmdBuffers.GetAddress(currentBuffer);
            Util.CheckResult(vkQueueSubmit(Queue, 1, ref SubmitInfo, new VkFence()));

            base.submitFrame();
        }
Exemple #18
0
        void setupDescriptorSet()
        {
            VkDescriptorSetAllocateInfo descriptorSetAllocInfo;

            // Scene rendering
            var dsl = descriptorSetLayouts_scene;

            descriptorSetAllocInfo = Initializers.descriptorSetAllocateInfo(descriptorPool, &dsl, 1);
            Util.CheckResult(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, out descriptorSets_scene));

            var descriptor0 = uniformBuffers_scene.descriptor;
            var descriptor1 = textures_gradient.descriptor;
            NativeList <VkWriteDescriptorSet> offScreenWriteDescriptorSets = new NativeList <VkWriteDescriptorSet>
            {
                // Binding 0: Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    descriptorSets_scene,
                    VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
                    0,
                    &descriptor0),
                // Binding 1: Color gradient sampler
                Initializers.writeDescriptorSet(
                    descriptorSets_scene,
                    VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
                    1,
                    &descriptor1),
            };

            vkUpdateDescriptorSets(device, offScreenWriteDescriptorSets.Count, offScreenWriteDescriptorSets.Data, 0, null);

            // Fullscreen radial blur
            dsl = descriptorSetLayouts_radialBlur;
            descriptorSetAllocInfo = Initializers.descriptorSetAllocateInfo(descriptorPool, &dsl, 1);
            Util.CheckResult(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, out descriptorSets_radialBlur));

            descriptor0 = uniformBuffers_blurParams.descriptor;
            descriptor1 = offscreenPass.descriptor;
            NativeList <VkWriteDescriptorSet> writeDescriptorSets = new NativeList <VkWriteDescriptorSet>
            {
                // Binding 0: Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    descriptorSets_radialBlur,
                    VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
                    0,
                    &descriptor0),
                // Binding 0: Fragment shader texture sampler
                Initializers.writeDescriptorSet(
                    descriptorSets_radialBlur,
                    VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
                    1,
                    &descriptor1),
            };

            vkUpdateDescriptorSets(device, writeDescriptorSets.Count, writeDescriptorSets.Data, 0, null);
        }
        void generateCube()
        {
            // Setup vertices indices for a colored cube
            NativeList <Vertex> vertices = new NativeList <Vertex>
            {
                { new Vertex {
                      pos = new Vector3(-1.0f, -1.0f, 1.0f), color = new Vector3(1.0f, 0.0f, 0.0f)
                  } },
                { new Vertex {
                      pos = new Vector3(1.0f, -1.0f, 1.0f), color = new Vector3(0.0f, 1.0f, 0.0f)
                  } },
                { new Vertex {
                      pos = new Vector3(1.0f, 1.0f, 1.0f), color = new Vector3(0.0f, 0.0f, 1.0f)
                  } },
                { new Vertex {
                      pos = new Vector3(-1.0f, 1.0f, 1.0f), color = new Vector3(0.0f, 0.0f, 0.0f)
                  } },
                { new Vertex {
                      pos = new Vector3(-1.0f, -1.0f, -1.0f), color = new Vector3(1.0f, 0.0f, 0.0f)
                  } },
                { new Vertex {
                      pos = new Vector3(1.0f, -1.0f, -1.0f), color = new Vector3(0.0f, 1.0f, 0.0f)
                  } },
                { new Vertex {
                      pos = new Vector3(1.0f, 1.0f, -1.0f), color = new Vector3(0.0f, 0.0f, 1.0f)
                  } },
                { new Vertex {
                      pos = new Vector3(-1.0f, 1.0f, -1.0f), color = new Vector3(0.0f, 0.0f, 0.0f)
                  } }
            };

            NativeList <uint> indices = new NativeList <uint>
            {
                0, 1, 2, 2, 3, 0, 1, 5, 6, 6, 2, 1, 7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4, 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3,
            };

            indexCount = indices.Count;

            // Create buffers
            // For the sake of simplicity we won't stage the vertex data to the gpu memory
            // Vertex buffer
            Util.CheckResult(vulkanDevice.createBuffer(
                                 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
                                 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                                 vertexBuffer,
                                 (ulong)(vertices.Count * sizeof(Vertex)),
                                 vertices.Data));
            // Index buffer
            Util.CheckResult(vulkanDevice.createBuffer(
                                 VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
                                 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                                 indexBuffer,
                                 indices.Count * sizeof(uint),
                                 indices.Data));
        }
Exemple #20
0
        protected void createCommandBuffers()
        {
            // Create one command buffer for each swap chain image and reuse for rendering
            drawCmdBuffers.Resize(Swapchain.ImageCount);
            drawCmdBuffers.Count = Swapchain.ImageCount;

            VkCommandBufferAllocateInfo cmdBufAllocateInfo =
                Initializers.CommandBufferAllocateInfo(cmdPool, VkCommandBufferLevel.Primary, drawCmdBuffers.Count);

            Util.CheckResult(vkAllocateCommandBuffers(device, ref cmdBufAllocateInfo, (VkCommandBuffer *)drawCmdBuffers.Data));
        }
Exemple #21
0
        protected override void buildCommandBuffers()
        {
            VkCommandBufferBeginInfo cmdBufInfo = Initializers.CommandBufferBeginInfo();

            FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>();

            clearValues.First.color         = defaultClearColor;
            clearValues.Second.depthStencil = new VkClearDepthStencilValue()
            {
                depth = 1.0f, stencil = 0
            };

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

            renderPassBeginInfo.renderPass               = RenderPass;
            renderPassBeginInfo.renderArea.offset.x      = 0;
            renderPassBeginInfo.renderArea.offset.y      = 0;
            renderPassBeginInfo.renderArea.extent.width  = Width;
            renderPassBeginInfo.renderArea.extent.height = Height;
            renderPassBeginInfo.clearValueCount          = 2;
            renderPassBeginInfo.pClearValues             = &clearValues.First;

            for (int i = 0; i < DrawCmdBuffers.Count; ++i)
            {
                // Set target frame buffer
                renderPassBeginInfo.framebuffer = Framebuffers[i];

                Util.CheckResult(vkBeginCommandBuffer(DrawCmdBuffers[i], &cmdBufInfo));

                vkCmdBeginRenderPass(DrawCmdBuffers[i], &renderPassBeginInfo, VkSubpassContents.Inline);

                VkViewport viewport = Initializers.viewport((float)Width, (float)Height, 0.0f, 1.0f);
                vkCmdSetViewport(DrawCmdBuffers[i], 0, 1, &viewport);

                VkRect2D scissor = Initializers.rect2D(Width, Height, 0, 0);
                vkCmdSetScissor(DrawCmdBuffers[i], 0, 1, &scissor);

                vkCmdBindDescriptorSets(DrawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSet, 0, null);
                vkCmdBindPipeline(DrawCmdBuffers[i], VkPipelineBindPoint.Graphics, wireframe ? pipelines_wireframe : pipelines_solid);

                ulong offsets = 0;
                // Bind mesh vertex buffer
                vkCmdBindVertexBuffers(DrawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref model_vertices_buffer, ref offsets);
                // Bind mesh index buffer
                vkCmdBindIndexBuffer(DrawCmdBuffers[i], model_indices_buffer, 0, VkIndexType.Uint32);
                // Render mesh vertex buffer using it's indices
                vkCmdDrawIndexed(DrawCmdBuffers[i], (uint)model_indices_count, 1, 0, 0, 0);

                vkCmdEndRenderPass(DrawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(DrawCmdBuffers[i]));
            }
        }
Exemple #22
0
        void setupDescriptorSetLayout()
        {
            NativeList <VkDescriptorSetLayoutBinding> setLayoutBindings;
            VkDescriptorSetLayoutCreateInfo           descriptorLayout;
            VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo;

            // Scene rendering
            setLayoutBindings = new NativeList <VkDescriptorSetLayoutBinding>
            {
                // Binding 0: Vertex shader uniform buffer
                Initializers.descriptorSetLayoutBinding(
                    VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
                    VK_SHADER_STAGE_VERTEX_BIT,
                    0),
                // Binding 1: Fragment shader image sampler
                Initializers.descriptorSetLayoutBinding(
                    VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
                    VK_SHADER_STAGE_FRAGMENT_BIT,
                    1),
                // Binding 2: Fragment shader uniform buffer
                Initializers.descriptorSetLayoutBinding(
                    VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
                    VK_SHADER_STAGE_FRAGMENT_BIT,
                    2)
            };

            descriptorLayout = Initializers.descriptorSetLayoutCreateInfo((VkDescriptorSetLayoutBinding *)setLayoutBindings.Data, setLayoutBindings.Count);
            Util.CheckResult(vkCreateDescriptorSetLayout(device, &descriptorLayout, null, out descriptorSetLayouts_scene));
            var dsl = descriptorSetLayouts_scene;

            pPipelineLayoutCreateInfo = Initializers.pipelineLayoutCreateInfo(&dsl, 1);
            Util.CheckResult(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, null, out pipelineLayouts_scene));

            // Fullscreen radial blur
            setLayoutBindings = new NativeList <VkDescriptorSetLayoutBinding>
            {
                // Binding 0 : Vertex shader uniform buffer
                Initializers.descriptorSetLayoutBinding(
                    VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
                    VK_SHADER_STAGE_FRAGMENT_BIT,
                    0),
                // Binding 0: Fragment shader image sampler
                Initializers.descriptorSetLayoutBinding(
                    VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
                    VK_SHADER_STAGE_FRAGMENT_BIT,
                    1)
            };
            descriptorLayout = Initializers.descriptorSetLayoutCreateInfo((VkDescriptorSetLayoutBinding *)setLayoutBindings.Data, setLayoutBindings.Count);
            Util.CheckResult(vkCreateDescriptorSetLayout(device, &descriptorLayout, null, out descriptorSetLayouts_radialBlur));
            dsl = descriptorSetLayouts_radialBlur;
            pPipelineLayoutCreateInfo = Initializers.pipelineLayoutCreateInfo(&dsl, 1);
            Util.CheckResult(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, null, out pipelineLayouts_radialBlur));
        }
Exemple #23
0
        void draw()
        {
            base.prepareFrame();

            // Command buffer to be sumitted to the queue
            submitInfo.commandBufferCount = 1;
            submitInfo.pCommandBuffers    = (VkCommandBuffer *)drawCmdBuffers.GetAddress(currentBuffer);

            // Submit to queue
            Util.CheckResult(vkQueueSubmit(queue, 1, ref submitInfo, VkFence.Null));

            submitFrame();
        }
Exemple #24
0
        void setupDescriptorPool()
        {
            // Example uses one ubo
            VkDescriptorPoolSize poolSizes = Initializers.descriptorPoolSize(VkDescriptorType.UniformBuffer, 1);

            VkDescriptorPoolCreateInfo descriptorPoolInfo =
                Initializers.descriptorPoolCreateInfo(
                    1,
                    &poolSizes,
                    2);

            Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool));
        }
        // Prepare and initialize uniform buffer containing shader uniforms
        void prepareUniformBuffers()
        {
            // Allocate data for the dynamic uniform buffer object
            // We allocate this manually as the alignment of the offset differs between GPUs

            // Calculate required alignment depending on device limits
            ulong uboAlignment = vulkanDevice.properties.limits.minUniformBufferOffsetAlignment;

            dynamicAlignment = (IntPtr)(((ulong)sizeof(Matrix4x4) / uboAlignment) * uboAlignment + (((ulong)sizeof(Matrix4x4) % uboAlignment) > 0 ? uboAlignment : 0));

            IntPtr bufferSize = (IntPtr)(OBJECT_INSTANCES * (ulong)dynamicAlignment);

            uboDataDynamic_model = (Matrix4x4 *)alignedAlloc(bufferSize, dynamicAlignment);
            Debug.Assert(uboDataDynamic_model != null);

            Console.WriteLine("minUniformBufferOffsetAlignment = " + uboAlignment);
            Console.WriteLine("dynamicAlignment = " + dynamicAlignment);

            // Vertex shader uniform buffer block

            // Static shared uniform buffer object with projection and view matrix
            Util.CheckResult(vulkanDevice.createBuffer(
                                 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
                                 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                                 uniformBuffers_view,
                                 (ulong)sizeof(UboVS)));

            // Uniform buffer object with per-object matrices
            Util.CheckResult(vulkanDevice.createBuffer(
                                 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
                                 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
                                 uniformBuffers_dynamic,
                                 (ulong)bufferSize));

            // Map persistent
            Util.CheckResult(uniformBuffers_view.map());
            Util.CheckResult(uniformBuffers_dynamic.map());

            // Prepare per-object matrices with offsets and random rotations
            Random rndGen = new Random();
            Func <Random, float> rndDist = rand => (float)(rand.NextDouble() * 2 - 1.0);

            for (uint i = 0; i < OBJECT_INSTANCES; i++)
            {
                rotations[i]      = new Vector3(rndDist(rndGen), rndDist(rndGen), rndDist(rndGen)) * 2.0f * (float)Math.PI;
                rotationSpeeds[i] = new Vector3(rndDist(rndGen), rndDist(rndGen), rndDist(rndGen));
            }

            updateUniformBuffers();
            updateDynamicUniformBuffer(true);
        }
        void setupDescriptorPool()
        {
            FixedArray2 <VkDescriptorPoolSize> poolSizes = new FixedArray2 <VkDescriptorPoolSize>(
                Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
                Initializers.descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1));

            VkDescriptorPoolCreateInfo descriptorPoolInfo =
                Initializers.descriptorPoolCreateInfo(
                    (uint)(poolSizes.Count),
                    &poolSizes.First,
                    1);

            Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool));
        }
        // Prepare and initialize uniform buffer containing shader uniforms
        void prepareUniformBuffers()
        {
            // Create the vertex shader uniform buffer block
            Util.CheckResult(vulkanDevice.createBuffer(
                                 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
                                 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
                                 uniformBuffer,
                                 (ulong)sizeof(UboVS)));

            // Map persistent
            Util.CheckResult(uniformBuffer.map());

            updateUniformBuffers();
        }
Exemple #28
0
        // Prepare and initialize uniform buffer containing shader uniforms
        void prepareUniformBuffers()
        {
            var localUboVS = uboVS;

            // Vertex shader uniform buffer block
            Util.CheckResult(vulkanDevice.createBuffer(
                                 VkBufferUsageFlags.UniformBuffer,
                                 VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent,
                                 uniformBufferVS,
                                 (uint)sizeof(UboVS),
                                 &localUboVS));

            updateUniformBuffers();
        }
        void setupDescriptorPool()
        {
            FixedArray2<VkDescriptorPoolSize> poolSizes = new FixedArray2<VkDescriptorPoolSize>(
                Initializers.descriptorPoolSize(VkDescriptorType.UniformBuffer, 2),
                Initializers.descriptorPoolSize(VkDescriptorType.CombinedImageSampler, 2));

            VkDescriptorPoolCreateInfo descriptorPoolInfo =
                Initializers.descriptorPoolCreateInfo(
                    poolSizes.Count,
                    &poolSizes.First,
                    2);

            Util.CheckResult(vkCreateDescriptorPool(device, &descriptorPoolInfo, null, out descriptorPool));
        }
Exemple #30
0
        // Prepare and initialize uniform buffer containing shader uniforms
        void prepareUniformBuffers()
        {
            // Vertex shader uniform buffer block
            Util.CheckResult(vulkanDevice.createBuffer(
                                 VkBufferUsageFlags.UniformBuffer,
                                 VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent,
                                 uniformBuffers_scene,
                                 (uint)sizeof(UboVS)));

            // Map persistent
            Util.CheckResult(uniformBuffers_scene.map());

            updateUniformBuffers();
        }