Example #1
0
        protected override void buildCommandBuffers()
        {
            var cmdInfo = VkCommandBufferBeginInfo.Alloc();

            var clearValues = VkClearValue.Alloc(2);

            clearValues[0].color        = defaultClearColor;
            clearValues[1].depthStencil = new VkClearDepthStencilValue()
            {
                depth = 1.0f, stencil = 0
            };

            var info = VkRenderPassBeginInfo.Alloc();

            info[0].renderPass               = renderPass;
            info[0].renderArea.offset.x      = 0;
            info[0].renderArea.offset.y      = 0;
            info[0].renderArea.extent.width  = width;
            info[0].renderArea.extent.height = height;
            info[0].clearValues.count        = 2;
            info[0].clearValues.pointer      = clearValues;

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

                vkBeginCommandBuffer(drawCmdBuffers[i], cmdInfo);

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

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

                VkRect2D scissor = new VkRect2D(0, 0, width, height);
                vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
                {
                    VkDescriptorSet set = descriptorSet;
                    vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout,
                                            0, 1, &set, 0, null);
                }
                vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipeline);

                {
                    VkDeviceSize offsets = 0;
                    VkBuffer     buffer  = vertexBuffer.buffer;
                    vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1,
                                           &buffer, &offsets);
                }
                vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VkIndexType.Uint32);

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

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                vkEndCommandBuffer(drawCmdBuffers[i]);
            }
        }
Example #2
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));
        }
Example #3
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);

                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]));
            }
        }
        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]));
            }
        }
Example #5
0
        public void SetViewportScissor(RenderContext context)
        {
            VkViewport viewport = Initializers.viewport((float)context.currentFrameBuffer.swapchain.width, (float)context.currentFrameBuffer.swapchain.height, 0.0f, 1.0f);

            vkCmdSetViewport(vkCmd, 0, 1, &viewport);

            VkRect2D scissor = Initializers.rect2D(context.currentFrameBuffer.swapchain.width, context.currentFrameBuffer.swapchain.height, 0, 0);

            vkCmdSetScissor(vkCmd, 0, 1, &scissor);
        }
Example #6
0
        /// <inheritdoc />
        public override void BeginDrawing(ColorRgba backgroundColor)
        {
            var clearValue = new VkClearValue();

            clearValue.color.float32[0] = backgroundColor.Red;
            clearValue.color.float32[1] = backgroundColor.Green;
            clearValue.color.float32[2] = backgroundColor.Blue;
            clearValue.color.float32[3] = backgroundColor.Alpha;

            var graphicsDevice  = VulkanGraphicsDevice;
            var graphicsSurface = graphicsDevice.GraphicsSurface;

            var graphicsSurfaceWidth  = graphicsSurface.Width;
            var graphicsSurfaceHeight = graphicsSurface.Height;

            var renderPassBeginInfo = new VkRenderPassBeginInfo {
                sType       = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
                renderPass  = graphicsDevice.VulkanRenderPass,
                framebuffer = VulkanFramebuffer,
                renderArea  = new VkRect2D {
                    extent = new VkExtent2D {
                        width  = (uint)graphicsSurface.Width,
                        height = (uint)graphicsSurface.Height,
                    },
                },
                clearValueCount = 1,
                pClearValues    = &clearValue,
            };

            var commandBuffer = VulkanCommandBuffer;

            vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

            var viewport = new VkViewport {
                x        = 0,
                y        = graphicsSurface.Height,
                width    = graphicsSurface.Width,
                height   = -graphicsSurface.Height,
                minDepth = 0.0f,
                maxDepth = 1.0f,
            };

            vkCmdSetViewport(commandBuffer, firstViewport: 0, viewportCount: 1, &viewport);

            var scissorRect = new VkRect2D {
                extent = new VkExtent2D {
                    width  = (uint)graphicsSurface.Width,
                    height = (uint)graphicsSurface.Height,
                },
            };

            vkCmdSetScissor(commandBuffer, firstScissor: 0, scissorCount: 1, &scissorRect);
        }
Example #7
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]));
            }
        }
Example #8
0
        /// <summary>
        /// Update dynamic viewport state
        /// </summary>
        public void SetViewport(float width, float height, float x = 0f, float y = 0f, float minDepth = 0.0f, float maxDepth = 1.0f)
        {
            VkViewport viewport = new VkViewport {
                x        = x,
                y        = y,
                height   = height,
                width    = width,
                minDepth = minDepth,
                maxDepth = maxDepth,
            };

            vkCmdSetViewport(handle, 0, 1, ref viewport);
        }
Example #9
0
        public static VkViewport viewport(
            float width,
            float height,
            float minDepth,
            float maxDepth)
        {
            VkViewport viewport = new VkViewport();

            viewport.width    = width;
            viewport.height   = height;
            viewport.minDepth = minDepth;
            viewport.maxDepth = maxDepth;
            return(viewport);
        }
Example #10
0
        public override void SetViewport(uint index, ref Viewport viewport)
        {
            VkViewport vkViewport = new VkViewport
            {
                x        = viewport.X,
                y        = viewport.Y,
                width    = viewport.Width,
                height   = viewport.Height,
                minDepth = viewport.MinDepth,
                maxDepth = viewport.MaxDepth
            };

            vkCmdSetViewport(_cb, index, 1, ref vkViewport);
        }
Example #11
0
        public override void SetViewport(uint index, ref Viewport viewport)
        {
            if (index == 0 || _gd.Features.MultipleViewports)
            {
                VkViewport vkViewport = new VkViewport
                {
                    x        = viewport.X,
                    y        = viewport.Y,
                    width    = viewport.Width,
                    height   = viewport.Height,
                    minDepth = viewport.MinDepth,
                    maxDepth = viewport.MaxDepth
                };

                vkCmdSetViewport(_cb, index, 1, ref vkViewport);
            }
        }
Example #12
0
        public unsafe void SetViewport(int x, int y, uint width, uint height)
        {
            var viewport = new VkViewport
            {
                x        = x,
                y        = y,
                width    = width,
                height   = height,
                minDepth = 0,
                maxDepth = 1
            };

            VulkanNative.vkCmdSetViewport(
                _handle,
                0,
                1,
                &viewport
                );
        }
Example #13
0
        public void SetViewport(float Width, float Height, float X, float Y, float MinDepth = 0.0f, float MaxDepth = 1.0f)
        {
            VkViewport Viewport = new VkViewport()
            {
                width = Width,

                height = Height,

                x = X,

                y = Y,

                minDepth = MinDepth,

                maxDepth = MaxDepth
            };

            vkCmdSetViewport(CommandBuffer, 0, 1, &Viewport);
        }
Example #14
0
        private VkPipelineViewportStateCreateInfo CreateViewportState()
        {
            VkExtent2D currentExtent = vkctrl.GetCurrentExtent();
            var        viewport      = new VkViewport()
            {
                width    = currentExtent.width,
                height   = currentExtent.height,
                x        = 0,
                y        = 0,
                minDepth = 0.0f,
                maxDepth = 1.0f,
            };
            var scissor = new VkRect2D()
            {
                extent = currentExtent
            };
            var viewportState = new VkPipelineViewportStateCreateInfo();

            viewportState.viewports = new[] { viewport };
            viewportState.scissors  = new[] { scissor };
            return(viewportState);
        }
 public void SetViewport(uint x, uint y, uint w, uint h)
 {
     if (locked)
     {
         var vpt = new VkViewport()
         {
             x        = x,
             y        = y,
             width    = w,
             height   = h,
             minDepth = 0,
             maxDepth = 1,
         };
         var vpt_ptr = vpt.Pointer();
         vkCmdSetViewport(hndl, 0, 1, vpt_ptr);
         IsEmpty = false;
     }
     else
     {
         throw new Exception("Command buffer not built.");
     }
 }
Example #16
0
        public override void SetViewport(uint index, ref Viewport viewport)
        {
            if (index == 0 || _gd.Features.MultipleViewports)
            {
                float vpY = _gd.IsClipSpaceYInverted
                    ? viewport.Y
                    : viewport.Height + viewport.Y;
                float vpHeight = _gd.IsClipSpaceYInverted
                    ? viewport.Height
                    : -viewport.Height;

                VkViewport vkViewport = new VkViewport
                {
                    x        = viewport.X,
                    y        = vpY,
                    width    = viewport.Width,
                    height   = vpHeight,
                    minDepth = viewport.MinDepth,
                    maxDepth = viewport.MaxDepth
                };

                vkCmdSetViewport(_cb, index, 1, ref vkViewport);
            }
        }
Example #17
0
        /// <summary>
        /// Begin renderpass and clear the color and depth of the supplied framebuffer.
        /// </summary>
        /// <param name="renderPass"></param>
        /// <param name="frameBuffer"></param>
        /// <param name="clearColor"></param>
        /// <param name="clearDepth"></param>
        public void BeginRenderPassClearColorDepth(RenderPass renderPass, FrameBuffer frameBuffer, VkClearColorValue clearColor, VkClearDepthStencilValue clearDepth, bool useSecondaryCommandBuffers)
        {
            CheckBegun();
            CheckNotInRenderPass();
            FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>();

            clearValues.First.color         = clearColor;
            clearValues.Second.depthStencil = clearDepth;

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

            renderPassBeginInfo.renderPass               = renderPass.vkRenderPass;
            renderPassBeginInfo.renderArea.offset.x      = 0;
            renderPassBeginInfo.renderArea.offset.y      = 0;
            renderPassBeginInfo.renderArea.extent.width  = frameBuffer.swapchain.width;
            renderPassBeginInfo.renderArea.extent.height = frameBuffer.swapchain.height;
            renderPassBeginInfo.clearValueCount          = 2;
            renderPassBeginInfo.pClearValues             = &clearValues.First;
            renderPassBeginInfo.framebuffer              = frameBuffer.vkFrameBuffer;

            renderPassUseSecondaryBuffers = useSecondaryCommandBuffers;
            VkSubpassContents subPassContents = useSecondaryCommandBuffers
                                ? VkSubpassContents.SecondaryCommandBuffers
                                : VkSubpassContents.Inline;

            vkCmdBeginRenderPass(vkCmd, &renderPassBeginInfo, subPassContents);

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

            vkCmdSetViewport(vkCmd, 0, 1, &viewport);

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

            vkCmdSetScissor(vkCmd, 0, 1, &scissor);
            inRenderPass = true;
        }
        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);

                vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, ref descriptorSet, 0, null);

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

                // Left
                viewport.width = (float)width / 3.0f;
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
                vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_phong);

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

                // Center
                viewport.x = (float)width / 3.0f;
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
                vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_toon);
                vkCmdDrawIndexed(drawCmdBuffers[i], models_cube.indexCount, 1, 0, 0, 0);

                // Right
                viewport.x = (float)width / 3.0f + (float)width / 3.0f;
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
                vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines_textured);
                vkCmdDrawIndexed(drawCmdBuffers[i], models_cube.indexCount, 1, 0, 0, 0);

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i]));
            }
        }
Example #19
0
        private void CreateGraphicsPipeline()
        {
            // Shader stages
            var vertShaderCode = System.IO.File.ReadAllBytes("Shaders/vert.spv");
            var fragShaderCode = System.IO.File.ReadAllBytes("Shaders/frag.spv");

            var vertShaderModule = CreateShaderModule(vertShaderCode);
            var fragShaderModule = CreateShaderModule(fragShaderCode);

            string name      = "main";
            int    byteCount = System.Text.Encoding.UTF8.GetByteCount(name);
            byte * utf8Ptr   = stackalloc byte[byteCount];

            fixed(char *namePtr = name)
            {
                System.Text.Encoding.UTF8.GetBytes(namePtr, name.Length, utf8Ptr, byteCount);
            }

            var vertShaderStageInfo = new VkPipelineShaderStageCreateInfo()
            {
                sType  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
                stage  = VkShaderStageFlagBits.VK_SHADER_STAGE_VERTEX_BIT,
                module = vertShaderModule,
                pName  = utf8Ptr,
            };

            var fragShaderStageInfo = new VkPipelineShaderStageCreateInfo()
            {
                sType  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
                stage  = VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT,
                module = fragShaderModule,
                pName  = utf8Ptr,
            };

            var shaderStages = stackalloc VkPipelineShaderStageCreateInfo[] { vertShaderStageInfo, fragShaderStageInfo };

            // VertexInput
            var vertexInputInfo = new VkPipelineVertexInputStateCreateInfo()
            {
                sType = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
                vertexBindingDescriptionCount   = 0,
                pVertexBindingDescriptions      = null,
                vertexAttributeDescriptionCount = 0,
                pVertexAttributeDescriptions    = null,
            };

            var inputAssembly = new VkPipelineInputAssemblyStateCreateInfo()
            {
                sType    = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
                topology = VkPrimitiveTopology.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
                primitiveRestartEnable = false,
            };

            var viewport = new VkViewport()
            {
                x        = 0f,
                y        = 0f,
                width    = (float)vkSwapChainExtent.width,
                height   = (float)vkSwapChainExtent.height,
                minDepth = 0f,
                maxDepth = 1f,
            };

            var scissor = new VkRect2D()
            {
                offset = new VkOffset2D()
                {
                    x = 0, y = 0
                },
                extent = vkSwapChainExtent,
            };

            var viewportState = new VkPipelineViewportStateCreateInfo()
            {
                sType         = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
                viewportCount = 1,
                pViewports    = &viewport,
                scissorCount  = 1,
                pScissors     = &scissor,
            };

            var rasterizer = new VkPipelineRasterizationStateCreateInfo()
            {
                sType                   = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
                depthClampEnable        = false,
                rasterizerDiscardEnable = false,
                polygonMode             = VkPolygonMode.VK_POLYGON_MODE_FILL,
                lineWidth               = 1f,
                cullMode                = VkCullModeFlagBits.VK_CULL_MODE_BACK_BIT,
                frontFace               = VkFrontFace.VK_FRONT_FACE_CLOCKWISE,
                depthBiasEnable         = false,
                depthBiasConstantFactor = 0f,
                depthBiasClamp          = 0f,
                depthBiasSlopeFactor    = 0f,
            };

            var multisampling = new VkPipelineMultisampleStateCreateInfo()
            {
                sType = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
                sampleShadingEnable   = false,
                rasterizationSamples  = VkSampleCountFlagBits.VK_SAMPLE_COUNT_1_BIT,
                minSampleShading      = 1f,
                pSampleMask           = null,
                alphaToCoverageEnable = false,
                alphaToOneEnable      = false,
            };

            var colorBlendAttachment = new VkPipelineColorBlendAttachmentState()
            {
                colorWriteMask = VkColorComponentFlagBits.VK_COLOR_COMPONENT_R_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_G_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_B_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_A_BIT,
                blendEnable    = false,
            };


            var colorBlending = new VkPipelineColorBlendStateCreateInfo()
            {
                sType            = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
                logicOpEnable    = false,
                logicOp          = VkLogicOp.VK_LOGIC_OP_COPY,
                pAttachments     = &colorBlendAttachment,
                attachmentCount  = 1,
                blendConstants_0 = 0f,
                blendConstants_1 = 0f,
                blendConstants_2 = 0f,
                blendConstants_3 = 0f,
            };

            var pipelineLayoutInfo = new VkPipelineLayoutCreateInfo()
            {
                sType                  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
                setLayoutCount         = 0,
                pushConstantRangeCount = 0,
            };

            VkPipelineLayout newPipelineLayout;
            var result = VulkanNative.vkCreatePipelineLayout(vkDevice, &pipelineLayoutInfo, null, &newPipelineLayout);

            vkPipelineLayout = newPipelineLayout;
            Helpers.CheckErrors(result);

            var pipelineInfo = new VkGraphicsPipelineCreateInfo()
            {
                sType               = VkStructureType.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
                stageCount          = 2,
                pStages             = shaderStages,
                pVertexInputState   = &vertexInputInfo,
                pInputAssemblyState = &inputAssembly,
                pViewportState      = &viewportState,
                pRasterizationState = &rasterizer,
                pMultisampleState   = &multisampling,
                pDepthStencilState  = null,
                pColorBlendState    = &colorBlending,
                pDynamicState       = null,
                layout              = vkPipelineLayout,
                renderPass          = vkRenderPass,
                subpass             = 0,
                basePipelineHandle  = 0,
                basePipelineIndex   = -1,
            };

            VkPipeline newPipeline;

            result             = VulkanNative.vkCreateGraphicsPipelines(vkDevice, 0, 1, &pipelineInfo, null, &newPipeline);
            vkGraphicsPipeline = newPipeline;
            Helpers.CheckErrors(result);

            VulkanNative.vkDestroyShaderModule(vkDevice, vertShaderModule, null);
            VulkanNative.vkDestroyShaderModule(vkDevice, fragShaderModule, null);
        }
Example #20
0
 public static void vkCmdSetViewport(VkCommandBuffer commandBuffer, uint firstViewport, VkViewport viewport)
 {
     vkCmdSetViewport(commandBuffer, firstViewport, 1, &viewport);
 }
Example #21
0
        void CreateGraphicsPipeline()
        {
            var vert = CreateShaderModule(File.ReadAllBytes("vert.spv"));
            var frag = CreateShaderModule(File.ReadAllBytes("frag.spv"));

            var vertInfo = new VkPipelineShaderStageCreateInfo();

            vertInfo.stage  = VkShaderStageFlags.VertexBit;
            vertInfo.module = vert;
            vertInfo.name   = "main";

            var fragInfo = new VkPipelineShaderStageCreateInfo();

            fragInfo.stage  = VkShaderStageFlags.FragmentBit;
            fragInfo.module = frag;
            fragInfo.name   = "main";

            var shaderStages = new List <VkPipelineShaderStageCreateInfo> {
                vertInfo, fragInfo
            };

            var vertexInputInfo = new VkPipelineVertexInputStateCreateInfo();

            var inputAssembly = new VkPipelineInputAssemblyStateCreateInfo();

            inputAssembly.topology = VkPrimitiveTopology.TriangleList;

            var viewport = new VkViewport();

            viewport.width    = swapchainExtent.width;
            viewport.height   = swapchainExtent.height;
            viewport.minDepth = 0f;
            viewport.maxDepth = 1f;

            var scissor = new VkRect2D();

            scissor.extent = swapchainExtent;

            var viewportState = new VkPipelineViewportStateCreateInfo();

            viewportState.viewports = new List <VkViewport> {
                viewport
            };
            viewportState.scissors = new List <VkRect2D> {
                scissor
            };

            var rasterizer = new VkPipelineRasterizationStateCreateInfo();

            rasterizer.polygonMode = VkPolygonMode.Fill;
            rasterizer.lineWidth   = 1f;
            rasterizer.cullMode    = VkCullModeFlags.BackBit;
            rasterizer.frontFace   = VkFrontFace.Clockwise;

            var multisampling = new VkPipelineMultisampleStateCreateInfo();

            multisampling.rasterizationSamples = VkSampleCountFlags._1_Bit;
            multisampling.minSampleShading     = 1f;

            var colorBlendAttachment = new VkPipelineColorBlendAttachmentState();

            colorBlendAttachment.colorWriteMask = VkColorComponentFlags.RBit
                                                  | VkColorComponentFlags.GBit
                                                  | VkColorComponentFlags.BBit
                                                  | VkColorComponentFlags.ABit;
            colorBlendAttachment.srcColorBlendFactor = VkBlendFactor.One;
            colorBlendAttachment.dstColorBlendFactor = VkBlendFactor.Zero;
            colorBlendAttachment.colorBlendOp        = VkBlendOp.Add;
            colorBlendAttachment.srcAlphaBlendFactor = VkBlendFactor.One;
            colorBlendAttachment.dstAlphaBlendFactor = VkBlendFactor.Zero;
            colorBlendAttachment.alphaBlendOp        = VkBlendOp.Add;

            var colorBlending = new VkPipelineColorBlendStateCreateInfo();

            colorBlending.logicOp     = VkLogicOp.Copy;
            colorBlending.attachments = new List <VkPipelineColorBlendAttachmentState> {
                colorBlendAttachment
            };

            var pipelineLayoutInfo = new VkPipelineLayoutCreateInfo();

            pipelineLayout?.Dispose();

            pipelineLayout = new VkPipelineLayout(device, pipelineLayoutInfo);

            var info = new VkGraphicsPipelineCreateInfo();

            info.stages             = shaderStages;
            info.vertexInputState   = vertexInputInfo;
            info.inputAssemblyState = inputAssembly;
            info.viewportState      = viewportState;
            info.rasterizationState = rasterizer;
            info.multisampleState   = multisampling;
            info.colorBlendState    = colorBlending;
            info.layout             = pipelineLayout;
            info.renderPass         = renderPass;
            info.subpass            = 0;
            info.basePipelineHandle = null;
            info.basePipelineIndex  = -1;

            pipeline?.Dispose();

            pipeline = new VkGraphicsPipeline(device, info, null);

            vert.Dispose();
            frag.Dispose();
        }
Example #22
0
        private void CreateGraphicsPipeline()
        {
            byte[] vertShaderCode = File.ReadAllBytes("Shaders/vert.spv");
            byte[] fragShaderCode = File.ReadAllBytes("Shaders/frag.spv");

            VkShaderModule vertShaderModule = this.CreateShaderModule(vertShaderCode);
            VkShaderModule fragShaderModule = this.CreateShaderModule(fragShaderCode);

            VkPipelineShaderStageCreateInfo vertShaderStageInfo = new VkPipelineShaderStageCreateInfo()
            {
                sType  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
                stage  = VkShaderStageFlags.VK_SHADER_STAGE_VERTEX_BIT,
                module = vertShaderModule,
                pName  = "main".ToPointer(),
            };

            VkPipelineShaderStageCreateInfo fragShaderStageInfo = new VkPipelineShaderStageCreateInfo()
            {
                sType  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
                stage  = VkShaderStageFlags.VK_SHADER_STAGE_FRAGMENT_BIT,
                module = fragShaderModule,
                pName  = "main".ToPointer(),
            };

            VkPipelineShaderStageCreateInfo *shaderStages = stackalloc VkPipelineShaderStageCreateInfo[] { vertShaderStageInfo, fragShaderStageInfo };

            // Vertex Input
            VkPipelineVertexInputStateCreateInfo vertexInputInfo = new VkPipelineVertexInputStateCreateInfo()
            {
                sType = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
                vertexBindingDescriptionCount   = 0,
                pVertexBindingDescriptions      = null, // Optional
                vertexAttributeDescriptionCount = 0,
                pVertexAttributeDescriptions    = null, // Optional
            };

            // Input assembly
            VkPipelineInputAssemblyStateCreateInfo inputAssembly = new VkPipelineInputAssemblyStateCreateInfo()
            {
                sType    = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
                topology = VkPrimitiveTopology.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
                primitiveRestartEnable = false,
            };

            // Viewports and scissors
            VkViewport viewport = new VkViewport()
            {
                x        = 0.0f,
                y        = 0.0f,
                width    = (float)swapChainExtent.width,
                height   = (float)swapChainExtent.height,
                minDepth = 0.0f,
                maxDepth = 1.0f,
            };

            VkRect2D scissor = new VkRect2D()
            {
                offset = new VkOffset2D(0, 0),
                extent = swapChainExtent,
            };

            VkPipelineViewportStateCreateInfo viewportState = new VkPipelineViewportStateCreateInfo()
            {
                sType         = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
                viewportCount = 1,
                pViewports    = &viewport,
                scissorCount  = 1,
                pScissors     = &scissor,
            };

            // Rasterizer
            VkPipelineRasterizationStateCreateInfo rasterizer = new VkPipelineRasterizationStateCreateInfo()
            {
                sType                   = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
                depthClampEnable        = false,
                rasterizerDiscardEnable = false,
                polygonMode             = VkPolygonMode.VK_POLYGON_MODE_FILL,
                lineWidth               = 1.0f,
                cullMode                = VkCullModeFlags.VK_CULL_MODE_BACK_BIT,
                frontFace               = VkFrontFace.VK_FRONT_FACE_CLOCKWISE,
                depthBiasEnable         = false,
                depthBiasConstantFactor = 0.0f, // Optional
                depthBiasClamp          = 0.0f, // Optional
                depthBiasSlopeFactor    = 0.0f, // Optional
            };

            VkPipelineMultisampleStateCreateInfo multisampling = new VkPipelineMultisampleStateCreateInfo()
            {
                sType = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
                sampleShadingEnable   = false,
                rasterizationSamples  = VkSampleCountFlags.VK_SAMPLE_COUNT_1_BIT,
                minSampleShading      = 1.0f,  // Optional
                pSampleMask           = null,  // Optional
                alphaToCoverageEnable = false, // Optional
                alphaToOneEnable      = false, // Optional
            };

            // Depth and Stencil testing
            //VkPipelineDepthStencilStateCreateInfo

            // Color blending
            VkPipelineColorBlendAttachmentState colorBlendAttachment = new VkPipelineColorBlendAttachmentState()
            {
                colorWriteMask = VkColorComponentFlags.VK_COLOR_COMPONENT_R_BIT |
                                 VkColorComponentFlags.VK_COLOR_COMPONENT_G_BIT |
                                 VkColorComponentFlags.VK_COLOR_COMPONENT_B_BIT |
                                 VkColorComponentFlags.VK_COLOR_COMPONENT_A_BIT,
                blendEnable         = false,
                srcColorBlendFactor = VkBlendFactor.VK_BLEND_FACTOR_ONE,  // Optional
                dstColorBlendFactor = VkBlendFactor.VK_BLEND_FACTOR_ZERO, // Optional
                colorBlendOp        = VkBlendOp.VK_BLEND_OP_ADD,          // Optional
                srcAlphaBlendFactor = VkBlendFactor.VK_BLEND_FACTOR_ONE,  // Optional
                dstAlphaBlendFactor = VkBlendFactor.VK_BLEND_FACTOR_ZERO, // Optional
                alphaBlendOp        = VkBlendOp.VK_BLEND_OP_ADD,          // Optional
            };

            VkPipelineColorBlendStateCreateInfo colorBlending = new VkPipelineColorBlendStateCreateInfo()
            {
                sType            = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
                logicOpEnable    = false,
                logicOp          = VkLogicOp.VK_LOGIC_OP_COPY, // Optional
                attachmentCount  = 1,
                pAttachments     = &colorBlendAttachment,
                blendConstants_0 = 0.0f, // Optional
                blendConstants_1 = 0.0f, // Optional
                blendConstants_2 = 0.0f, // Optional
                blendConstants_3 = 0.0f, // Optional
            };

            VkPipelineLayoutCreateInfo pipelineLayoutInfo = new VkPipelineLayoutCreateInfo()
            {
                sType                  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
                setLayoutCount         = 0,    // Optional
                pSetLayouts            = null, // Optional
                pushConstantRangeCount = 0,    // Optional
                pPushConstantRanges    = null, // Optional
            };

            fixed(VkPipelineLayout *pipelineLayoutPtr = &pipelineLayout)
            {
                Helpers.CheckErrors(VulkanNative.vkCreatePipelineLayout(device, &pipelineLayoutInfo, null, pipelineLayoutPtr));
            }

            VkGraphicsPipelineCreateInfo pipelineInfo = new VkGraphicsPipelineCreateInfo()
            {
                sType               = VkStructureType.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
                stageCount          = 2,
                pStages             = shaderStages,
                pVertexInputState   = &vertexInputInfo,
                pInputAssemblyState = &inputAssembly,
                pViewportState      = &viewportState,
                pRasterizationState = &rasterizer,
                pMultisampleState   = &multisampling,
                pDepthStencilState  = null, // Optional
                pColorBlendState    = &colorBlending,
                pDynamicState       = null, // Optional
                layout              = this.pipelineLayout,
                renderPass          = this.renderPass,
                subpass             = 0,
                basePipelineHandle  = 0,  // Optional
                basePipelineIndex   = -1, // Optional
            };

            fixed(VkPipeline *graphicsPipelinePtr = &this.graphicsPipeline)
            {
                Helpers.CheckErrors(VulkanNative.vkCreateGraphicsPipelines(this.device, 0, 1, &pipelineInfo, null, graphicsPipelinePtr));
            }

            VulkanNative.vkDestroyShaderModule(device, fragShaderModule, null);
            VulkanNative.vkDestroyShaderModule(device, vertShaderModule, null);
        }
Example #23
0
        /// <summary>Begins a new frame for rendering.</summary>
        /// <param name="backgroundColor">A color to which the background should be cleared.</param>
        public void BeginFrame(ColorRgba backgroundColor)
        {
            uint frameIndex;
            var  result = vkAcquireNextImageKHR(Device, SwapChain, timeout: ulong.MaxValue, AcquireNextImageSemaphore, fence: VK_NULL_HANDLE, &frameIndex);

            _frameIndex = frameIndex;

            if (result != VK_SUCCESS)
            {
                ThrowExternalException(nameof(vkAcquireNextImageKHR), (int)result);
            }

            var fence = Fences[frameIndex];

            result = vkWaitForFences(Device, fenceCount: 1, &fence, waitAll: VK_TRUE, timeout: ulong.MaxValue);

            if (result != VK_SUCCESS)
            {
                ThrowExternalException(nameof(vkWaitForFences), (int)result);
            }

            result = vkResetFences(Device, 1, &fence);

            if (result != VK_SUCCESS)
            {
                ThrowExternalException(nameof(vkResetFences), (int)result);
            }

            var commandBufferBeginInfo = new VkCommandBufferBeginInfo {
                sType            = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
                pNext            = null,
                flags            = 0,
                pInheritanceInfo = null,
            };

            var commandBuffer = CommandBuffers[frameIndex];

            result = vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo);

            if (result != VK_SUCCESS)
            {
                ThrowExternalException(nameof(vkBeginCommandBuffer), (int)result);
            }

            var clearValue = new VkClearValue {
                color        = new VkClearColorValue {
                },
                depthStencil = new VkClearDepthStencilValue {
                    depth   = 0.0f,
                    stencil = 0,
                },
            };

            clearValue.color.float32[0] = backgroundColor.Red;
            clearValue.color.float32[1] = backgroundColor.Green;
            clearValue.color.float32[2] = backgroundColor.Blue;
            clearValue.color.float32[3] = backgroundColor.Alpha;

            var renderPassBeginInfo = new VkRenderPassBeginInfo {
                sType       = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
                pNext       = null,
                renderPass  = RenderPass,
                framebuffer = FrameBuffers[frameIndex],
                renderArea  = new VkRect2D {
                    offset = new VkOffset2D {
                        x = 0,
                        y = 0,
                    },
                    extent = new VkExtent2D {
                        width  = (uint)_graphicsSurface.Width,
                        height = (uint)_graphicsSurface.Height,
                    },
                },
                clearValueCount = 1,
                pClearValues    = &clearValue,
            };

            vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

            var viewport = new VkViewport {
                x        = 0.0f,
                y        = 0.0f,
                width    = _graphicsSurface.Width,
                height   = _graphicsSurface.Height,
                minDepth = 0.0f,
                maxDepth = 1.0f,
            };

            vkCmdSetViewport(commandBuffer, firstViewport: 0, viewportCount: 1, &viewport);

            var scissorRect = new VkRect2D {
                offset = new VkOffset2D {
                    x = 0,
                    y = 0,
                },
                extent = new VkExtent2D {
                    width  = (uint)_graphicsSurface.Width,
                    height = (uint)_graphicsSurface.Height,
                },
            };

            vkCmdSetScissor(commandBuffer, firstScissor: 0, scissorCount: 1, &scissorRect);
        }
Example #24
0
        private VkPipeline CreateVulkanGraphicsPipeline()
        {
            var  pipelineShaderStageCreateInfos      = stackalloc VkPipelineShaderStageCreateInfo[2];
            uint pipelineShaderStageCreateInfosCount = 0;

            try
            {
                VkPipeline vulkanPipeline;

                var graphicsDevice  = VulkanGraphicsDevice;
                var graphicsSurface = graphicsDevice.GraphicsSurface;

                var vertexInputBindingDescription = new VkVertexInputBindingDescription {
                    inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
                };

                var vertexInputAttributeDescriptions = Array.Empty <VkVertexInputAttributeDescription>();

                var pipelineVertexInputStateCreateInfo = new VkPipelineVertexInputStateCreateInfo {
                    sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
                    vertexBindingDescriptionCount = 1,
                    pVertexBindingDescriptions    = &vertexInputBindingDescription,
                };

                var pipelineInputAssemblyStateCreateInfo = new VkPipelineInputAssemblyStateCreateInfo {
                    sType    = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
                    topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
                };

                var viewport = new VkViewport {
                    width    = graphicsSurface.Width,
                    height   = graphicsSurface.Height,
                    minDepth = 0.0f,
                    maxDepth = 1.0f,
                };

                var scissorRect2D = new VkRect2D {
                    extent = new VkExtent2D {
                        width  = (uint)viewport.width,
                        height = (uint)viewport.height,
                    },
                };

                var pipelineViewportStateCreateInfo = new VkPipelineViewportStateCreateInfo {
                    sType         = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
                    viewportCount = 1,
                    pViewports    = &viewport,
                    scissorCount  = 1,
                    pScissors     = &scissorRect2D,
                };

                var pipelineRasterizationStateCreateInfo = new VkPipelineRasterizationStateCreateInfo {
                    sType     = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
                    frontFace = VK_FRONT_FACE_CLOCKWISE,
                    lineWidth = 1.0f,
                };

                var pipelineMultisampleStateCreateInfo = new VkPipelineMultisampleStateCreateInfo {
                    sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
                    rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
                };

                var pipelineDepthStencilStateCreateInfo = new VkPipelineDepthStencilStateCreateInfo {
                    sType          = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
                    depthCompareOp = VK_COMPARE_OP_ALWAYS,
                    front          = new VkStencilOpState {
                        compareOp = VK_COMPARE_OP_ALWAYS,
                    },
                    back = new VkStencilOpState {
                        compareOp = VK_COMPARE_OP_ALWAYS,
                    },
                };

                var pipelineColorBlendAttachmentState = new VkPipelineColorBlendAttachmentState {
                    colorWriteMask = 0xF,
                };

                var pipelineColorBlendStateCreateInfo = new VkPipelineColorBlendStateCreateInfo {
                    sType           = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
                    attachmentCount = 1,
                    pAttachments    = &pipelineColorBlendAttachmentState,
                };

                var graphicsPipelineCreateInfo = new VkGraphicsPipelineCreateInfo {
                    sType               = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
                    pViewportState      = &pipelineViewportStateCreateInfo,
                    pRasterizationState = &pipelineRasterizationStateCreateInfo,
                    pMultisampleState   = &pipelineMultisampleStateCreateInfo,
                    pDepthStencilState  = &pipelineDepthStencilStateCreateInfo,
                    pColorBlendState    = &pipelineColorBlendStateCreateInfo,
                    layout              = VulkanSignature.VulkanPipelineLayout,
                    renderPass          = graphicsDevice.VulkanRenderPass,
                };

                var vertexShader = VulkanVertexShader;

                if (vertexShader != null)
                {
                    var shaderIndex = pipelineShaderStageCreateInfosCount++;

                    var entryPointName       = MarshalStringToUtf8(vertexShader.EntryPointName);
                    var entryPointNameLength = entryPointName.Length + 1;

                    pipelineShaderStageCreateInfos[shaderIndex] = new VkPipelineShaderStageCreateInfo {
                        sType  = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
                        stage  = VK_SHADER_STAGE_VERTEX_BIT,
                        module = vertexShader.VulkanShaderModule,
                        pName  = (sbyte *)Allocate(entryPointNameLength),
                    };

                    var destination = new Span <sbyte>(pipelineShaderStageCreateInfos[shaderIndex].pName, entryPointNameLength);
                    entryPointName.CopyTo(destination);
                    destination[entryPointName.Length] = 0x00;

                    var inputs       = VulkanSignature.Inputs;
                    var inputsLength = inputs.Length;

                    var inputElementsCount = GetInputElementsCount(inputs);
                    var inputElementsIndex = 0;

                    if (inputElementsCount != 0)
                    {
                        vertexInputAttributeDescriptions = new VkVertexInputAttributeDescription[inputElementsCount];

                        for (var inputIndex = 0; inputIndex < inputsLength; inputIndex++)
                        {
                            var input = inputs[inputIndex];

                            var inputElements       = input.Elements;
                            var inputElementsLength = inputElements.Length;

                            uint inputBindingStride = 0;

                            for (var inputElementIndex = 0; inputElementIndex < inputElementsLength; inputElementIndex++)
                            {
                                var inputElement = inputElements[inputElementIndex];

                                vertexInputAttributeDescriptions[inputElementsIndex] = new VkVertexInputAttributeDescription {
                                    location = unchecked ((uint)inputElementIndex),
                                    binding  = unchecked ((uint)inputIndex),
                                    format   = GetInputElementFormat(inputElement.Type),
                                    offset   = inputBindingStride,
                                };

                                inputBindingStride += inputElement.Size;
                                inputElementsIndex++;
                            }

                            vertexInputBindingDescription.stride = inputBindingStride;
                        }
                    }

                    graphicsPipelineCreateInfo.pVertexInputState   = &pipelineVertexInputStateCreateInfo;
                    graphicsPipelineCreateInfo.pInputAssemblyState = &pipelineInputAssemblyStateCreateInfo;
                }

                var pixelShader = VulkanPixelShader;

                if (pixelShader != null)
                {
                    var shaderIndex = pipelineShaderStageCreateInfosCount++;

                    var entryPointName       = MarshalStringToUtf8(pixelShader.EntryPointName);
                    var entryPointNameLength = entryPointName.Length + 1;

                    pipelineShaderStageCreateInfos[shaderIndex] = new VkPipelineShaderStageCreateInfo {
                        sType  = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
                        stage  = VK_SHADER_STAGE_FRAGMENT_BIT,
                        module = pixelShader.VulkanShaderModule,
                        pName  = (sbyte *)Allocate(entryPointNameLength),
                    };

                    var destination = new Span <sbyte>(pipelineShaderStageCreateInfos[shaderIndex].pName, entryPointNameLength);
                    entryPointName.CopyTo(destination);
                    destination[entryPointName.Length] = 0x00;
                }

                if (pipelineShaderStageCreateInfosCount != 0)
                {
                    graphicsPipelineCreateInfo.stageCount = pipelineShaderStageCreateInfosCount;
                    graphicsPipelineCreateInfo.pStages    = pipelineShaderStageCreateInfos;
                }

                fixed(VkVertexInputAttributeDescription *pVertexInputAttributeDescriptions = vertexInputAttributeDescriptions)
                {
                    pipelineVertexInputStateCreateInfo.vertexAttributeDescriptionCount = unchecked ((uint)vertexInputAttributeDescriptions.Length);
                    pipelineVertexInputStateCreateInfo.pVertexAttributeDescriptions    = pVertexInputAttributeDescriptions;

                    ThrowExternalExceptionIfNotSuccess(nameof(vkCreateGraphicsPipelines), vkCreateGraphicsPipelines(graphicsDevice.VulkanDevice, pipelineCache: VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, pAllocator: null, (ulong *)&vulkanPipeline));
                }

                return(vulkanPipeline);
            }
            finally
            {
                for (uint index = 0; index < pipelineShaderStageCreateInfosCount; index++)
                {
                    var entryPointName = pipelineShaderStageCreateInfos[index].pName;

                    if (entryPointName != null)
                    {
                        Free(entryPointName);
                    }
                }
            }
Example #25
0
        protected override void buildCommandBuffers()
        {
            var cmdBufInfo = VkCommandBufferBeginInfo.Alloc();

            var clearValues = new VkClearValue[2];

            clearValues[0].color        = defaultClearColor;
            clearValues[1].depthStencil = new VkClearDepthStencilValue()
            {
                depth = 1.0f, stencil = 0
            };

            var renderPassBeginInfo = VkRenderPassBeginInfo.Alloc();

            renderPassBeginInfo->renderPass               = renderPass;
            renderPassBeginInfo->renderArea.offset.x      = 0;
            renderPassBeginInfo->renderArea.offset.y      = 0;
            renderPassBeginInfo->renderArea.extent.width  = width;
            renderPassBeginInfo->renderArea.extent.height = height;
            renderPassBeginInfo->clearValues              = clearValues;

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

                vkBeginCommandBuffer(drawCmdBuffers[i], cmdBufInfo);

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

                VkViewport viewport = new VkViewport(0, 0, width, height, 0, 1);
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);

                VkRect2D scissor = new VkRect2D();
                scissor.extent.width = width; scissor.extent.height = height; scissor.offset.x = 0; scissor.offset.y = 0;
                vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);

                {
                    VkDescriptorSet set = descriptorSet;
                    vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics,
                                            pipelineLayout, 0, 1, &set, 0, null);
                }
                vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, wireframe ? pipelines_wireframe : pipelines_solid);

                ulong offsets = 0;
                // Bind mesh vertex buffer
                {
                    VkBuffer     buffer = model_vertices_buffer;
                    VkDeviceSize offset = offsets;
                    vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &buffer, &offset);
                }
                // 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]);

                vkEndCommandBuffer(drawCmdBuffers[i]);
            }
        }
Example #26
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);

                // Update light positions
                // w component = light radius scale
                const float r     = 7.5f;
                float       sin_t = (float)Math.Sin(Util.DegreesToRadians(timer * 360));
                float       cos_t = (float)Math.Cos(Util.DegreesToRadians(timer * 360));
                const float y     = -4.0f;
                pushConstants[0] = new Vector4(r * 1.1f * sin_t, y, r * 1.1f * cos_t, 1.0f);
                pushConstants[1] = new Vector4(-r * sin_t, y, -r * cos_t, 1.0f);
                pushConstants[2] = new Vector4(r * 0.85f * sin_t, y, -sin_t * 2.5f, 1.5f);
                pushConstants[3] = new Vector4(0.0f, y, r * 1.25f * cos_t, 1.5f);
                pushConstants[4] = new Vector4(r * 2.25f * cos_t, y, 0.0f, 1.25f);
                pushConstants[5] = new Vector4(r * 2.5f * (float)Math.Cos(Util.DegreesToRadians(timer * 360)), y, r * 2.5f * sin_t, 1.25f);

                // Submit via push constant (rather than a UBO)
                vkCmdPushConstants(
                    drawCmdBuffers[i],
                    pipelineLayout,
                    VkShaderStageFlags.Vertex,
                    0,
                    pushConstants.Count * (uint)sizeof(Vector4),
                    pushConstants.Data.ToPointer());

                vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_solid);
                vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSet, 0, null);

                ulong offsets = 0;
                vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_scene.vertices.buffer, &offsets);
                vkCmdBindIndexBuffer(drawCmdBuffers[i], models_scene.indices.buffer, 0, VkIndexType.Uint32);

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

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i]));
            }
        }
Example #27
0
        private void createGraphicsPipeline()
        {
            var vertShaderModule = createShaderModule(typeof(Shader_Vert_03));
            var fragShaderModule = createShaderModule(typeof(Shader_Frag_03));

            VkPipelineShaderStageCreateInfo vertShaderStageInfo = new VkPipelineShaderStageCreateInfo();

            vertShaderStageInfo.sType  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
            vertShaderStageInfo.stage  = VkShaderStageFlagBits.VK_SHADER_STAGE_VERTEX_BIT;
            vertShaderStageInfo.module = vertShaderModule;
            vertShaderStageInfo.pName  = "main";

            VkPipelineShaderStageCreateInfo fragShaderStageInfo = new VkPipelineShaderStageCreateInfo();

            fragShaderStageInfo.sType  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
            fragShaderStageInfo.stage  = VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT;
            fragShaderStageInfo.module = fragShaderModule;
            fragShaderStageInfo.pName  = "main";

            VkPipelineShaderStageCreateInfo[] shaderStages = new VkPipelineShaderStageCreateInfo[2] {
                vertShaderStageInfo, fragShaderStageInfo
            };

            VkPipelineVertexInputStateCreateInfo vertexInputInfo = new VkPipelineVertexInputStateCreateInfo();

            vertexInputInfo.sType = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;

            var bindingDescription    = Vertex.getBindingDescription();
            var attributeDescriptions = Vertex.getAttributeDescriptions();

            vertexInputInfo.vertexBindingDescriptionCount   = 1;
            vertexInputInfo.vertexAttributeDescriptionCount = attributeDescriptions.Length;
            vertexInputInfo.pVertexBindingDescriptions      = new VkVertexInputBindingDescription[] { bindingDescription };
            vertexInputInfo.pVertexAttributeDescriptions    = attributeDescriptions;

            VkPipelineInputAssemblyStateCreateInfo inputAssembly = new VkPipelineInputAssemblyStateCreateInfo();

            inputAssembly.sType    = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
            inputAssembly.topology = VkPrimitiveTopology.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
            inputAssembly.primitiveRestartEnable = VkBool32.VK_FALSE;

            VkViewport viewport = new VkViewport();

            viewport.x        = 0.0f;
            viewport.y        = 0.0f;
            viewport.width    = (float)swapChainExtent.width;
            viewport.height   = (float)swapChainExtent.height;
            viewport.minDepth = 0.0f;
            viewport.maxDepth = 1.0f;

            VkRect2D scissor = new VkRect2D();

            scissor.offset = new VkOffset2D()
            {
                x = 0, y = 0
            };
            scissor.extent = swapChainExtent;

            VkPipelineViewportStateCreateInfo viewportState = new VkPipelineViewportStateCreateInfo();

            viewportState.sType         = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
            viewportState.viewportCount = 1;
            viewportState.pViewports    = new VkViewport[] { viewport };
            viewportState.scissorCount  = 1;
            viewportState.pSicssors     = new VkRect2D[] { scissor };

            VkPipelineRasterizationStateCreateInfo rasterizer = new VkPipelineRasterizationStateCreateInfo();

            rasterizer.sType                   = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
            rasterizer.depthClampEnable        = VkBool32.VK_FALSE;
            rasterizer.rasterizerDiscardEnable = VkBool32.VK_FALSE;
            rasterizer.polygonMode             = VkPolygonMode.VK_POLYGON_MODE_FILL;
            rasterizer.lineWidth               = 1.0f;
            rasterizer.cullMode                = VkCullModeFlagBits.VK_CULL_MODE_BACK_BIT;
            rasterizer.frontFace               = VkFrontFace.VK_FRONT_FACE_CLOCKWISE;
            rasterizer.depthBiasEnable         = VkBool32.VK_FALSE;

            VkPipelineMultisampleStateCreateInfo multisampling = new VkPipelineMultisampleStateCreateInfo();

            multisampling.sType = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
            multisampling.sampleShadingEnable  = VkBool32.VK_FALSE;
            multisampling.rasterizationSamples = VkSampleCountFlagBits.VK_SAMPLE_COUNT_1_BIT;

            VkPipelineColorBlendAttachmentState colorBlendAttachment = new VkPipelineColorBlendAttachmentState();

            colorBlendAttachment.colorWriteMask = VkColorComponentFlagBits.VK_COLOR_COMPONENT_R_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_G_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_B_BIT | VkColorComponentFlagBits.VK_COLOR_COMPONENT_A_BIT;
            colorBlendAttachment.blendEnable    = VkBool32.VK_FALSE;

            VkPipelineColorBlendStateCreateInfo colorBlending = VkPipelineColorBlendStateCreateInfo.Create();

            colorBlending.sType             = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
            colorBlending.logicOpEnable     = VkBool32.VK_FALSE;
            colorBlending.logicOp           = VkLogicOp.VK_LOGIC_OP_COPY;
            colorBlending.attachmentCount   = 1;
            colorBlending.pAttachments      = new VkPipelineColorBlendAttachmentState[] { colorBlendAttachment };
            colorBlending.blendConstants[0] = 0.0f;
            colorBlending.blendConstants[1] = 0.0f;
            colorBlending.blendConstants[2] = 0.0f;
            colorBlending.blendConstants[3] = 0.0f;

            VkPipelineLayoutCreateInfo pipelineLayoutInfo = new VkPipelineLayoutCreateInfo();

            pipelineLayoutInfo.sType                  = VkStructureType.VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
            pipelineLayoutInfo.setLayoutCount         = 0;
            pipelineLayoutInfo.pushConstantRangeCount = 0;

            VkResult result = Vulkan.vkCreatePipelineLayout(device, pipelineLayoutInfo, null, out pipelineLayout);

            if (result != VkResult.VK_SUCCESS)
            {
                throw Program.Throw("failed to create pipeline layout!");
            }

            VkGraphicsPipelineCreateInfo pipelineInfo = new VkGraphicsPipelineCreateInfo();

            pipelineInfo.sType               = VkStructureType.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
            pipelineInfo.stageCount          = shaderStages.Length;
            pipelineInfo.pStages             = shaderStages;
            pipelineInfo.pVertexInputState   = vertexInputInfo;
            pipelineInfo.pInputAssemblyState = inputAssembly;
            pipelineInfo.pViewportState      = viewportState;
            pipelineInfo.pRasterizationState = rasterizer;
            pipelineInfo.pMultisampleState   = multisampling;
            pipelineInfo.pColorBlendState    = colorBlending;
            pipelineInfo.layout              = pipelineLayout;
            pipelineInfo.renderPass          = renderPass;
            pipelineInfo.subpass             = 0;
            pipelineInfo.basePipelineHandle  = null;

            VkPipeline[] pipelineResult = new VkPipeline[1];
            result = Vulkan.vkCreateGraphicsPipelines(device, null, 1, new VkGraphicsPipelineCreateInfo[] { pipelineInfo }, null, pipelineResult);
            if (result != VkResult.VK_SUCCESS)
            {
                throw Program.Throw("failed to create graphics pipeline!", result);
            }

            graphicsPipeline = pipelineResult[0];

            Vulkan.vkDestroyShaderModule(device, fragShaderModule, null);
            Vulkan.vkDestroyShaderModule(device, vertShaderModule, null);
        }
Example #28
0
        protected override void buildCommandBuffers()
        {
            VkCommandBufferBeginInfo cmdBufInfo = VkCommandBufferBeginInfo.New();

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

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

            VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo.New();

            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             = (VkClearValue *)Unsafe.AsPointer(ref clearValues);

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

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

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

                VkViewport viewport = new VkViewport()
                {
                    width = Width, height = Height, minDepth = 0f, maxDepth = 1f
                };
                vkCmdSetViewport(DrawCmdBuffers[i], 0, 1, ref viewport);

                VkRect2D scissor = new VkRect2D()
                {
                    extent = new VkExtent2D()
                    {
                        width = Width, height = Height
                    }, offset = new VkOffset2D()
                };
                vkCmdSetScissor(DrawCmdBuffers[i], 0, 1, ref scissor);

                vkCmdBindDescriptorSets(DrawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSet, 0, null);

                ulong offsets = 0;
                vkCmdBindVertexBuffers(DrawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_cube.vertices.buffer, ref offsets);
                vkCmdBindIndexBuffer(DrawCmdBuffers[i], models_cube.indices.buffer, 0, VkIndexType.Uint32);

                // Left : Solid colored
                viewport.width = Width / 3.0f;
                vkCmdSetViewport(DrawCmdBuffers[i], 0, 1, &viewport);
                vkCmdBindPipeline(DrawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_phong);

                vkCmdDrawIndexed(DrawCmdBuffers[i], models_cube.indexCount, 1, 0, 0, 0);

                // Center : Toon
                viewport.x = Width / 3.0f;
                vkCmdSetViewport(DrawCmdBuffers[i], 0, 1, &viewport);
                vkCmdBindPipeline(DrawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_toon);
                // Line Width > 1.0f only if wide lines feature is supported
                if (DeviceFeatures.wideLines != 0)
                {
                    vkCmdSetLineWidth(DrawCmdBuffers[i], 2.0f);
                }
                vkCmdDrawIndexed(DrawCmdBuffers[i], models_cube.indexCount, 1, 0, 0, 0);

                if (DeviceFeatures.fillModeNonSolid != 0)
                {
                    // Right : Wireframe
                    viewport.x = Width / 3.0f + Width / 3.0f;
                    vkCmdSetViewport(DrawCmdBuffers[i], 0, 1, &viewport);
                    vkCmdBindPipeline(DrawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_wireframe);
                    vkCmdDrawIndexed(DrawCmdBuffers[i], models_cube.indexCount, 1, 0, 0, 0);
                }

                vkCmdEndRenderPass(DrawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(DrawCmdBuffers[i]));
            }
        }
Example #29
0
        void CreateGraphicsPipeline()
        {
            VkShaderModule vert = CreateShaderModule(File.ReadAllBytes("vert.spv"));
            VkShaderModule frag = CreateShaderModule(File.ReadAllBytes("frag.spv"));

            InteropString entry = new InteropString("main");

            var vertInfo = new VkPipelineShaderStageCreateInfo();

            vertInfo.sType  = CSGL.Vulkan.VkStructureType.PipelineShaderStageCreateInfo;
            vertInfo.stage  = CSGL.Vulkan.VkShaderStageFlags.VertexBit;
            vertInfo.module = vert;
            vertInfo.pName  = entry.Address;

            var fragInfo = new VkPipelineShaderStageCreateInfo();

            fragInfo.sType  = CSGL.Vulkan.VkStructureType.PipelineShaderStageCreateInfo;
            fragInfo.stage  = CSGL.Vulkan.VkShaderStageFlags.FragmentBit;
            fragInfo.module = frag;
            fragInfo.pName  = entry.Address;

            var shaderStages = new NativeArray <VkPipelineShaderStageCreateInfo>(2);

            shaderStages[0] = vertInfo;
            shaderStages[1] = fragInfo;

            var vertexInputInfo = new VkPipelineVertexInputStateCreateInfo();

            vertexInputInfo.sType = CSGL.Vulkan.VkStructureType.PipelineVertexInputStateCreateInfo;

            var vertexInputNative = new Native <VkPipelineVertexInputStateCreateInfo>(vertexInputInfo);

            var inputAssembly = new VkPipelineInputAssemblyStateCreateInfo();

            inputAssembly.sType    = CSGL.Vulkan.VkStructureType.PipelineInputAssemblyStateCreateInfo;
            inputAssembly.topology = CSGL.Vulkan.VkPrimitiveTopology.TriangleList;

            var inputAssemblyNative = new Native <VkPipelineInputAssemblyStateCreateInfo>(inputAssembly);

            var viewport = new VkViewport();

            viewport.width    = swapchainExtent.width;
            viewport.height   = swapchainExtent.height;
            viewport.minDepth = 0f;
            viewport.maxDepth = 1f;

            var viewportNative = new Native <VkViewport>(viewport);

            var scissor = new VkRect2D();

            scissor.extent = swapchainExtent;

            var scissorNative = new Native <VkRect2D>(scissor);

            var viewportState = new VkPipelineViewportStateCreateInfo();

            viewportState.sType         = CSGL.Vulkan.VkStructureType.PipelineViewportStateCreateInfo;
            viewportState.viewportCount = 1;
            viewportState.pViewports    = viewportNative.Address;
            viewportState.scissorCount  = 1;
            viewportState.pScissors     = scissorNative.Address;

            var viewportStateNative = new Native <VkPipelineViewportStateCreateInfo>(viewportState);

            var rasterizer = new VkPipelineRasterizationStateCreateInfo();

            rasterizer.sType       = CSGL.Vulkan.VkStructureType.PipelineRasterizationStateCreateInfo;
            rasterizer.polygonMode = CSGL.Vulkan.VkPolygonMode.Fill;
            rasterizer.lineWidth   = 1f;
            rasterizer.cullMode    = CSGL.Vulkan.VkCullModeFlags.BackBit;
            rasterizer.frontFace   = CSGL.Vulkan.VkFrontFace.Clockwise;

            var rasterizerNative = new Native <VkPipelineRasterizationStateCreateInfo>(rasterizer);

            var multisampling = new VkPipelineMultisampleStateCreateInfo();

            multisampling.sType = CSGL.Vulkan.VkStructureType.PipelineMultisampleStateCreateInfo;
            multisampling.rasterizationSamples = CSGL.Vulkan.VkSampleCountFlags._1_Bit;
            multisampling.minSampleShading     = 1f;

            var multisamplingNative = new Native <VkPipelineMultisampleStateCreateInfo>(multisampling);

            var colorBlendAttachment = new VkPipelineColorBlendAttachmentState();

            colorBlendAttachment.colorWriteMask = CSGL.Vulkan.VkColorComponentFlags.RBit
                                                  | CSGL.Vulkan.VkColorComponentFlags.GBit
                                                  | CSGL.Vulkan.VkColorComponentFlags.BBit
                                                  | CSGL.Vulkan.VkColorComponentFlags.ABit;
            colorBlendAttachment.srcColorBlendFactor = CSGL.Vulkan.VkBlendFactor.One;
            colorBlendAttachment.dstColorBlendFactor = CSGL.Vulkan.VkBlendFactor.Zero;
            colorBlendAttachment.colorBlendOp        = CSGL.Vulkan.VkBlendOp.Add;
            colorBlendAttachment.srcAlphaBlendFactor = CSGL.Vulkan.VkBlendFactor.One;
            colorBlendAttachment.dstAlphaBlendFactor = CSGL.Vulkan.VkBlendFactor.Zero;
            colorBlendAttachment.alphaBlendOp        = CSGL.Vulkan.VkBlendOp.Add;

            var colorBlendAttachmentNative = new Native <VkPipelineColorBlendAttachmentState>(colorBlendAttachment);

            var colorBlending = new VkPipelineColorBlendStateCreateInfo();

            colorBlending.sType           = CSGL.Vulkan.VkStructureType.PipelineColorBlendStateCreateInfo;
            colorBlending.logicOp         = CSGL.Vulkan.VkLogicOp.Copy;
            colorBlending.attachmentCount = 1;
            colorBlending.pAttachments    = colorBlendAttachmentNative.Address;

            var colorBlendingNative = new Native <VkPipelineColorBlendStateCreateInfo>(colorBlending);

            var pipelineLayoutInfo = new VkPipelineLayoutCreateInfo();

            pipelineLayoutInfo.sType = CSGL.Vulkan.VkStructureType.PipelineLayoutCreateInfo;

            if (pipelineLayout != VkPipelineLayout.Null)
            {
                VK.DestroyPipelineLayout(device, pipelineLayout, alloc);
            }
            var result = VK.CreatePipelineLayout(device, ref pipelineLayoutInfo, alloc, out pipelineLayout);

            var info = new VkGraphicsPipelineCreateInfo();

            info.sType               = CSGL.Vulkan.VkStructureType.GraphicsPipelineCreateInfo;
            info.stageCount          = 2;
            info.pStages             = shaderStages.Address;
            info.pVertexInputState   = vertexInputNative.Address;
            info.pInputAssemblyState = inputAssemblyNative.Address;
            info.pViewportState      = viewportStateNative.Address;
            info.pRasterizationState = rasterizerNative.Address;
            info.pMultisampleState   = multisamplingNative.Address;
            info.pColorBlendState    = colorBlendingNative.Address;
            info.layout              = pipelineLayout;
            info.renderPass          = renderPass;
            info.subpass             = 0;
            info.basePipelineHandle  = VkPipeline.Null;
            info.basePipelineIndex   = -1;

            var infoNative = new Native <VkGraphicsPipelineCreateInfo>(info);
            var temp       = new Native <VkPipeline>();

            if (pipeline != VkPipeline.Null)
            {
                VK.DestroyPipeline(device, pipeline, alloc);
            }

            result   = VK.CreateGraphicsPipelines(device, VkPipelineCache.Null, 1, infoNative.Address, alloc, temp.Address);
            pipeline = temp.Value;

            infoNative.Dispose();
            temp.Dispose();

            entry.Dispose();
            shaderStages.Dispose();
            vertexInputNative.Dispose();
            inputAssemblyNative.Dispose();
            viewportNative.Dispose();
            scissorNative.Dispose();
            viewportStateNative.Dispose();
            rasterizerNative.Dispose();
            multisamplingNative.Dispose();
            colorBlendingNative.Dispose();
            colorBlendAttachmentNative.Dispose();
            VK.DestroyShaderModule(device, vert, alloc);
            VK.DestroyShaderModule(device, frag, alloc);
        }
Example #30
0
        private void CreateGraphicsPipeline()
        {
            var vertShaderModule = CreateShaderModule(File.ReadAllBytes("../../Resources/Shaders/vert.spv"));
            var fragShaderModule = CreateShaderModule(File.ReadAllBytes("../../Resources/Shaders/frag.spv"));

            var vertShaderStageInfo = new VkPipelineShaderStageCreateInfo()
            {
                Stage  = VkShaderStage.Vertex,
                Module = vertShaderModule,
                Name   = "main"
            };

            var fragShaderStageInfo = new VkPipelineShaderStageCreateInfo()
            {
                Stage  = VkShaderStage.Fragment,
                Module = fragShaderModule,
                Name   = "main"
            };

            var shaderStages    = new[] { vertShaderStageInfo, fragShaderStageInfo };
            var vertexInputInfo = new VkPipelineVertexInputStateCreateInfo
            {
                VertexBindingDescriptions   = new[] { Vertex.GetBindingDescription(0) },
                VertexAttributeDescriptions = Vertex.GetAttributeDescriptions(0)
            };
            var inputAssemblyInfo = new VkPipelineInputAssemblyStateCreateInfo
            {
                Topology = VkPrimitiveTopology.TriangleList,
                PrimitiveRestartEnable = false
            };
            var viewport = new VkViewport
            {
                X        = 0,
                Y        = 0,
                Width    = swapChainExtent.Width,
                Height   = swapChainExtent.Height,
                MinDepth = 0,
                MaxDepth = 1
            };
            var scissor           = new VkRect2D(new VkOffset2D(0, 0), swapChainExtent);
            var viewportStateInfo = new VkPipelineViewportStateCreateInfo
            {
                Viewports = new [] { viewport },
                Scissors  = new [] { scissor }
            };
            var rasterizerInfo = new VkPipelineRasterizationStateCreateInfo
            {
                DepthClampEnable        = false,
                RasterizerDiscardEnable = false,
                PolygonMode             = VkPolygonMode.Fill,
                LineWidth               = 1f,
                CullMode                = VkCullMode.Back,
                FrontFace               = VkFrontFace.Clockwise,
                DepthBiasEnable         = false,
                DepthBiasConstantFactor = 0f,
                DepthBiasClamp          = 0f,
                DepthBiasSlopeFactor    = 0f
            };
            var multisamplingInfo = new VkPipelineMultisampleStateCreateInfo
            {
                SampleShadingEnable   = false,
                RasterizationSamples  = VkSampleCount.B1,
                MinSampleShading      = 1f,
                SampleMask            = null,
                AlphaToCoverageEnable = false,
                AlphaToOneEnable      = false
            };
            var colorBlendAttachment = new VkPipelineColorBlendAttachmentState
            {
                ColorWriteMask      = VkColorComponent.R | VkColorComponent.G | VkColorComponent.B | VkColorComponent.A,
                BlendEnable         = (VkBool32)false,
                SrcColorBlendFactor = VkBlendFactor.One,
                SrcAlphaBlendFactor = VkBlendFactor.One,
                ColorBlendOp        = VkBlendOp.Add,
                AlphaBlendOp        = VkBlendOp.Add,
                DstColorBlendFactor = VkBlendFactor.Zero,
                DstAlphaBlendFactor = VkBlendFactor.Zero
            };
            var colorBlendingInfo = new VkPipelineColorBlendStateCreateInfo
            {
                LogicOpEnable  = false,
                LogicOp        = VkLogicOp.Copy,
                Attachments    = new[] { colorBlendAttachment },
                BlendConstants = new VkColor4(0, 0, 0, 0)
            };
            var dynamicStates = new[]
            {
                VkDynamicState.Viewport,
                VkDynamicState.LineWidth,
            };
            var dynamicStateInfo = new VkPipelineDynamicStateCreateInfo
            {
                DynamicStates = dynamicStates
            };
            var pipelineLayoutInfo = new VkPipelineLayoutCreateInfo
            {
                SetLayouts         = null,
                PushConstantRanges = null
            };

            pipelineLayout = device.CreatePipelineLayout(pipelineLayoutInfo, null).Object;

            var pipelineInfo = new VkGraphicsPipelineCreateInfo
            {
                Stages             = shaderStages,
                VertexInputState   = vertexInputInfo,
                InputAssemblyState = inputAssemblyInfo,
                TessellationState  = null,
                ViewportState      = viewportStateInfo,
                RasterizationState = rasterizerInfo,
                MultisampleState   = multisamplingInfo,
                DepthStencilState  = null,
                ColorBlendState    = colorBlendingInfo,
                DynamicState       = null,
                Layout             = pipelineLayout,
                RenderPass         = renderPass,
                Subpass            = 0,
                BasePipelineHandle = null,
                BasePipelineIndex  = -1,
                Flags = VkPipelineCreateFlags.None
            };

            graphicsPipeline = device.CreateGraphicsPipelines(null, new[] { pipelineInfo }, null).Object.Single();

            vertShaderModule.Dispose();
            fragShaderModule.Dispose();
        }