Exemplo n.º 1
0
        private void BeginCurrentRenderPass()
        {
            Debug.Assert(_activeRenderPass == VkRenderPass.Null);

            VkRenderPassBeginInfo renderPassBI = VkRenderPassBeginInfo.New();

            renderPassBI.framebuffer = _currentFramebuffer.CurrentFramebuffer;
            renderPassBI.renderPass  = _currentFramebuffer.RenderPass;
            renderPassBI.renderArea  = new VkRect2D(_currentFramebuffer.Width, _currentFramebuffer.Height);
            vkCmdBeginRenderPass(_cb, ref renderPassBI, VkSubpassContents.Inline);
            _activeRenderPass = _currentFramebuffer.RenderPass;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Begin Render pass with custom render area
        /// </summary>
        public void Begin(CommandBuffer cmd, Framebuffer frameBuffer, uint width, uint height)
        {
            VkRenderPassBeginInfo info = VkRenderPassBeginInfo.New();

            info.renderPass = handle;
            info.renderArea.extent.width  = width;
            info.renderArea.extent.height = height;
            info.clearValueCount          = (uint)ClearValues.Count;
            info.pClearValues             = ClearValues.Pin();
            info.framebuffer = frameBuffer.handle;

            vkCmdBeginRenderPass(cmd.Handle, ref info, VkSubpassContents.Inline);

            ClearValues.Unpin();
        }
Exemplo n.º 3
0
        private void BeginCurrentRenderPass()
        {
            Debug.Assert(_activeRenderPass == VkRenderPass.Null);
            Debug.Assert(_currentFramebuffer != null);
            _currentFramebufferEverActive = true;

            uint attachmentCount    = _currentFramebuffer.AttachmentCount;
            bool haveAnyAttachments = _currentFramebuffer.ColorTargets.Count > 0 || _currentFramebuffer.DepthTarget != null;
            bool haveAllClearValues = _depthClearValue.HasValue || _currentFramebuffer.DepthTarget == null;
            bool haveAnyClearValues = _depthClearValue.HasValue;

            for (int i = 0; i < _currentFramebuffer.ColorTargets.Count; i++)
            {
                if (!_validColorClearValues[i])
                {
                    haveAllClearValues = false;
                    haveAnyClearValues = true;
                }
                else
                {
                    haveAnyClearValues = true;
                }
            }

            VkRenderPassBeginInfo renderPassBI = VkRenderPassBeginInfo.New();

            renderPassBI.renderArea  = new VkRect2D(_currentFramebuffer.RenderableWidth, _currentFramebuffer.RenderableHeight);
            renderPassBI.framebuffer = _currentFramebuffer.CurrentFramebuffer;

            if (!haveAnyAttachments || !haveAllClearValues)
            {
                renderPassBI.renderPass = _newFramebuffer
                    ? _currentFramebuffer.RenderPassNoClear_Init
                    : _currentFramebuffer.RenderPassNoClear_Load;
                vkCmdBeginRenderPass(_cb, ref renderPassBI, VkSubpassContents.Inline);
                _activeRenderPass = renderPassBI.renderPass;

                if (haveAnyClearValues)
                {
                    if (_depthClearValue.HasValue)
                    {
                        ClearDepthStencil(_depthClearValue.Value.depthStencil.depth, (byte)_depthClearValue.Value.depthStencil.stencil);
                        _depthClearValue = null;
                    }

                    for (uint i = 0; i < _currentFramebuffer.ColorTargets.Count; i++)
                    {
                        if (_validColorClearValues[i])
                        {
                            _validColorClearValues[i] = false;
                            VkClearValue vkClearValue = _clearValues[i];
                            RgbaFloat    clearColor   = new RgbaFloat(
                                vkClearValue.color.float32_0,
                                vkClearValue.color.float32_1,
                                vkClearValue.color.float32_2,
                                vkClearValue.color.float32_3);
                            ClearColorTarget(i, clearColor);
                        }
                    }
                }
            }
            else
            {
                // We have clear values for every attachment.
                renderPassBI.renderPass = _currentFramebuffer.RenderPassClear;
                fixed(VkClearValue *clearValuesPtr = &_clearValues[0])
                {
                    renderPassBI.clearValueCount = attachmentCount;
                    renderPassBI.pClearValues    = clearValuesPtr;
                    if (_depthClearValue.HasValue)
                    {
                        _clearValues[_currentFramebuffer.ColorTargets.Count] = _depthClearValue.Value;
                        _depthClearValue = null;
                    }
                    vkCmdBeginRenderPass(_cb, ref renderPassBI, VkSubpassContents.Inline);
                    _activeRenderPass = _currentFramebuffer.RenderPassClear;
                    Util.ClearArray(_validColorClearValues);
                }
            }

            _newFramebuffer = false;
        }
Exemplo n.º 4
0
        private void BeginCurrentRenderPass()
        {
            Debug.Assert(_activeRenderPass == VkRenderPass.Null);
            Debug.Assert(_currentFramebuffer != null);
            _currentFramebufferEverActive = true;

            uint attachmentCount    = _currentFramebuffer.AttachmentCount;
            bool haveAnyAttachments = _framebuffer.ColorTargets.Count > 0 || _framebuffer.DepthTarget != null;
            bool haveAllClearValues = _depthClearValue.HasValue || _framebuffer.DepthTarget == null;
            bool haveAnyClearValues = _depthClearValue.HasValue;

            for (int i = 0; i < _currentFramebuffer.ColorTargets.Count; i++)
            {
                if (!_validColorClearValues[i])
                {
                    haveAllClearValues = false;
                    haveAnyClearValues = true;
                }
                else
                {
                    haveAnyClearValues = true;
                }
            }

            VkRenderPassBeginInfo renderPassBI = VkRenderPassBeginInfo.New();

            renderPassBI.renderArea  = new VkRect2D(_currentFramebuffer.RenderableWidth, _currentFramebuffer.RenderableHeight);
            renderPassBI.framebuffer = _currentFramebuffer.CurrentFramebuffer;

            if (!haveAnyAttachments || !haveAllClearValues)
            {
                renderPassBI.renderPass = _currentFramebuffer.RenderPassNoClear;
                vkCmdBeginRenderPass(_cb, ref renderPassBI, VkSubpassContents.Inline);
                _activeRenderPass = _currentFramebuffer.RenderPassNoClear;

                if (haveAnyClearValues)
                {
                    if (_depthClearValue.HasValue)
                    {
                        ClearDepthStencil(_depthClearValue.Value.depthStencil.depth, (byte)_depthClearValue.Value.depthStencil.stencil);
                        _depthClearValue = null;
                    }

                    for (uint i = 0; i < _currentFramebuffer.ColorTargets.Count; i++)
                    {
                        if (_validColorClearValues[i])
                        {
                            _validColorClearValues[i] = false;
                            VkClearValue vkClearValue = _clearValues[i];
                            RgbaFloat    clearColor   = new RgbaFloat(
                                vkClearValue.color.float32_0,
                                vkClearValue.color.float32_1,
                                vkClearValue.color.float32_2,
                                vkClearValue.color.float32_3);
                            ClearColorTarget(i, clearColor);
                        }
                    }
                }
            }
            else
            {
                // We have clear values for every attachment.
                renderPassBI.renderPass = _currentFramebuffer.RenderPassClear;
                fixed(VkClearValue *clearValuesPtr = &_clearValues[0])
                {
                    renderPassBI.clearValueCount = attachmentCount;
                    renderPassBI.pClearValues    = clearValuesPtr;
                    if (_depthClearValue.HasValue)
                    {
                        _clearValues[_currentFramebuffer.ColorTargets.Count] = _depthClearValue.Value;
                    }
                    vkCmdBeginRenderPass(_cb, ref renderPassBI, VkSubpassContents.Inline);
                    _activeRenderPass = _currentFramebuffer.RenderPassClear;
                    Util.ClearArray(_validColorClearValues);
                }
            }

            // Set new image layouts
            foreach (FramebufferAttachment colorTarget in _currentFramebuffer.ColorTargets)
            {
                VkTexture     vkTex  = Util.AssertSubtype <Texture, VkTexture>(colorTarget.Target);
                VkImageLayout layout = (vkTex.Usage & TextureUsage.Sampled) != 0
                    ? VkImageLayout.ShaderReadOnlyOptimal
                    : VkImageLayout.ColorAttachmentOptimal;
                vkTex.SetImageLayout(colorTarget.ArrayLayer, layout);
            }

            if (_currentFramebuffer.DepthTarget != null)
            {
                VkTexture     vkDepthTex = Util.AssertSubtype <Texture, VkTexture>(_currentFramebuffer.DepthTarget.Value.Target);
                VkImageLayout layout     = (vkDepthTex.Usage & TextureUsage.Sampled) != 0
                    ? VkImageLayout.ShaderReadOnlyOptimal
                    : VkImageLayout.DepthStencilAttachmentOptimal;

                vkDepthTex.SetImageLayout(_currentFramebuffer.DepthTarget.Value.ArrayLayer, layout);
            }
        }
Exemplo n.º 5
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]));
            }
        }
Exemplo n.º 6
0
        public static VkRenderPassBeginInfo renderPassBeginInfo()
        {
            VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo.New();

            return(renderPassBeginInfo);
        }
Exemplo n.º 7
0
        private void FlushFrameDrawCommands()
        {
            _scInfo.AcquireNextImage(_device, _imageAvailableSemaphore);
            for (int i = 0; i < _renderPassStates.Count; i++)
            {
                RenderPassInfo renderPassState = _renderPassStates[i];

                VkCommandBuffer primaryCommandBuffer = GetPrimaryCommandBuffer();
                renderPassState.PrimaryCommandBuffer = primaryCommandBuffer;
                VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.New();
                beginInfo.flags = VkCommandBufferUsageFlags.OneTimeSubmit;
                vkBeginCommandBuffer(primaryCommandBuffer, ref beginInfo);
                VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo.New();
                VkFramebufferBase     fbInfo = renderPassState.Framebuffer;
                renderPassBeginInfo.framebuffer = fbInfo.VkFramebuffer;
                renderPassBeginInfo.renderPass  = renderPassState.ClearBuffer
                    ? fbInfo.RenderPassClearBuffer
                    : fbInfo.RenderPassNoClear;

                if (renderPassState.ClearBuffer)
                {
                    VkClearColorValue colorClear = new VkClearColorValue
                    {
                        float32_0 = renderPassState.ClearColor.R,
                        float32_1 = renderPassState.ClearColor.G,
                        float32_2 = renderPassState.ClearColor.B,
                        float32_3 = renderPassState.ClearColor.A
                    };
                    VkClearDepthStencilValue depthClear = new VkClearDepthStencilValue()
                    {
                        depth = 1f, stencil = 0
                    };
                    StackList <VkClearValue, Size512Bytes> clearValues = new StackList <VkClearValue, Size512Bytes>();
                    if (fbInfo.ColorTexture != null)
                    {
                        clearValues.Add(new VkClearValue()
                        {
                            color = colorClear
                        });
                    }
                    if (fbInfo.DepthTexture != null)
                    {
                        clearValues.Add(new VkClearValue()
                        {
                            depthStencil = depthClear
                        });
                    }

                    renderPassBeginInfo.clearValueCount = clearValues.Count;
                    renderPassBeginInfo.pClearValues    = (VkClearValue *)clearValues.Data;
                }
                renderPassBeginInfo.renderArea.extent = new VkExtent2D(fbInfo.Width, fbInfo.Height);

                vkCmdBeginRenderPass(primaryCommandBuffer, ref renderPassBeginInfo, VkSubpassContents.SecondaryCommandBuffers);
                RawList <VkCommandBuffer> secondaryCBs = renderPassState.SecondaryCommandBuffers;
                if (secondaryCBs.Count > 0)
                {
                    vkCmdExecuteCommands(primaryCommandBuffer, secondaryCBs.Count, ref secondaryCBs[0]);
                }
                vkCmdEndRenderPass(primaryCommandBuffer);
                vkEndCommandBuffer(primaryCommandBuffer);

                VkSubmitInfo         submitInfo    = VkSubmitInfo.New();
                VkSemaphore          waitSemaphore = (i == 0) ? _imageAvailableSemaphore : _renderPassSemaphores[i - 1];
                VkPipelineStageFlags waitStages    = VkPipelineStageFlags.ColorAttachmentOutput;
                submitInfo.waitSemaphoreCount = 1;
                submitInfo.pWaitSemaphores    = &waitSemaphore;
                submitInfo.pWaitDstStageMask  = &waitStages;
                VkCommandBuffer cb = primaryCommandBuffer;
                submitInfo.commandBufferCount = 1;
                submitInfo.pCommandBuffers    = &cb;
                VkSemaphore signalSemaphore = _renderPassSemaphores[i];
                submitInfo.signalSemaphoreCount = 1;
                submitInfo.pSignalSemaphores    = &signalSemaphore;
                vkQueueSubmit(_graphicsQueue, 1, ref submitInfo, VkFence.Null);
            }
        }
Exemplo n.º 8
0
        public void Execute(VkCommandBuffer buffer)
        {
            VkImageMemoryBarrier imageMemoryBarrier = VkImageMemoryBarrier.New();

            imageMemoryBarrier.srcAccessMask       = VkAccessFlags.None;
            imageMemoryBarrier.dstAccessMask       = VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite;
            imageMemoryBarrier.oldLayout           = VkImageLayout.Undefined;
            imageMemoryBarrier.newLayout           = VkImageLayout.ColorAttachmentOptimal;
            imageMemoryBarrier.srcQueueFamilyIndex = VulkanNative.QueueFamilyIgnored;
            imageMemoryBarrier.dstQueueFamilyIndex = VulkanNative.QueueFamilyIgnored;
            imageMemoryBarrier.image            = swapchainImage;
            imageMemoryBarrier.subresourceRange = new VkImageSubresourceRange()
            {
                baseMipLevel   = 0,
                levelCount     = 1,
                baseArrayLayer = 0,
                layerCount     = 1,
                aspectMask     = VkImageAspectFlags.Color
            };

            vkCmdPipelineBarrier(buffer, VkPipelineStageFlags.AllGraphics, VkPipelineStageFlags.AllGraphics, VkDependencyFlags.ByRegion,
                                 0, null, 0, null, 1, &imageMemoryBarrier);

            VkClearColorValue clearColorValue = new VkClearColorValue {
                float32_0 = 3f / 255f, float32_1 = 2f / 255, float32_2 = 12f / 255, float32_3 = 1
            };
            VkClearDepthStencilValue clearDepthStencilValue = new VkClearDepthStencilValue(1, 0);

            VkClearValue[] clearValues = new VkClearValue[2];
            clearValues[0].color        = clearColorValue;
            clearValues[1].depthStencil = clearDepthStencilValue;

            VkRenderPassBeginInfo passBeginInfo = VkRenderPassBeginInfo.New();

            passBeginInfo.renderPass               = renderPass;
            passBeginInfo.framebuffer              = swapchainFramebuffer;
            passBeginInfo.renderArea.extent.width  = (uint)Program.width;
            passBeginInfo.renderArea.extent.height = (uint)Program.height;
            passBeginInfo.clearValueCount          = (uint)clearValues.Length;

            fixed(VkClearValue *ptr = clearValues)
            passBeginInfo.pClearValues = ptr;

            vkCmdBeginRenderPass(buffer, &passBeginInfo, VkSubpassContents.Inline);

            VkViewport viewport = new VkViewport();

            viewport.x      = 0;
            viewport.y      = (float)Program.height;
            viewport.width  = (float)Program.width;
            viewport.height = -(float)Program.height;

            VkRect2D scissor = new VkRect2D();

            scissor.offset.x      = 0;
            scissor.offset.y      = 0;
            scissor.extent.width  = (uint)Program.width;
            scissor.extent.height = (uint)Program.height;

            vkCmdSetViewport(buffer, 0, 1, &viewport);
            vkCmdSetScissor(buffer, 0, 1, &scissor);

            vkCmdBindPipeline(buffer, VkPipelineBindPoint.Graphics, pipeline);
            if (mesh.IsReady)
            {
                mesh.Use();
                VkBuffer[] databuffers = new VkBuffer[] { mesh.Mesh.vertices.Buffer, mesh.Mesh.vertices.Buffer, mesh.Mesh.vertices.Buffer, mesh.Mesh.vertices.Buffer };
                ulong[]    offsets     = new ulong[] { 0, 3 * 4, 6 * 4, 6 * 4 + 2 * 4 };

                fixed(VkBuffer *bptr = databuffers)
                fixed(ulong *optr = offsets)
                vkCmdBindVertexBuffers(buffer, 0, 4, bptr, optr);

                vkCmdBindIndexBuffer(buffer, mesh.Mesh.indices.Buffer, 0, VkIndexType.Uint32);

                uniformdata.Write(0, camera.View.ToFloatArray());
                uniformdata.Write(16, camera.Projection.ToFloatArray());

                UpdateUniformData(uniformdata, swapchainImageIndex);

                VkDescriptorSet  sets   = descriptorSets[swapchainImageIndex];
                VkPipelineLayout layout = pipelineLayout;
                vkCmdBindDescriptorSets(buffer, VkPipelineBindPoint.Graphics, layout, 0, 1, &sets, 0, null);

                vkCmdDrawIndexed(buffer, (uint)mesh.Mesh.indices.Length, 1, 0, 0, 0);
                mesh.StopUse();
            }

            vkCmdEndRenderPass(buffer);
        }