Ejemplo n.º 1
0
        protected override void buildCommandBuffers()
        {
            VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo();

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

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

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

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

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

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

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

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

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

                ulong offsets = 0;

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

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

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

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i]));
            }
        }
Ejemplo n.º 2
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]));
            }
        }
Ejemplo n.º 3
0
        // Sets up the command buffer that renders the scene to the offscreen frame buffer
        void buildOffscreenCommandBuffer()
        {
            if (offscreenPass.commandBuffer == NullHandle)
            {
                offscreenPass.commandBuffer = createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false);
            }
            if (offscreenPass.semaphore == NullHandle)
            {
                VkSemaphoreCreateInfo semaphoreCreateInfo = Initializers.semaphoreCreateInfo();
                Util.CheckResult(vkCreateSemaphore(device, &semaphoreCreateInfo, null, out offscreenPass.semaphore));
            }

            VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo();

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

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

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

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

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

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

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

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

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

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

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

            ulong offsets = 0;

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

            vkCmdEndRenderPass(offscreenPass.commandBuffer);

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

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

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

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

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

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

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

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

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

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

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

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

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

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

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i]));
            }
        }
Ejemplo n.º 5
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]));
            }
        }
Ejemplo n.º 6
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]));
            }
        }