Search in sources :

Example 6 with VkRenderPassBeginInfo

use of org.lwjgl.vulkan.VkRenderPassBeginInfo in project lwjgl3-demos by LWJGL.

the class InstancedSpheresDemo method createRenderCommandBuffers.

private static VkCommandBuffer[] createRenderCommandBuffers(VkDevice device, long commandPool, long[] framebuffers, long renderPass, int width, int height, Pipeline pipeline, long descriptorSet, long verticesBuf) {
    // Create the render command buffers (one command buffer per framebuffer image)
    VkCommandBufferAllocateInfo cmdBufAllocateInfo = VkCommandBufferAllocateInfo.calloc().sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO).commandPool(commandPool).level(VK_COMMAND_BUFFER_LEVEL_PRIMARY).commandBufferCount(framebuffers.length);
    PointerBuffer pCommandBuffer = memAllocPointer(framebuffers.length);
    int err = vkAllocateCommandBuffers(device, cmdBufAllocateInfo, pCommandBuffer);
    if (err != VK_SUCCESS) {
        throw new AssertionError("Failed to allocate render command buffer: " + translateVulkanResult(err));
    }
    VkCommandBuffer[] renderCommandBuffers = new VkCommandBuffer[framebuffers.length];
    for (int i = 0; i < framebuffers.length; i++) {
        renderCommandBuffers[i] = new VkCommandBuffer(pCommandBuffer.get(i), device);
    }
    memFree(pCommandBuffer);
    cmdBufAllocateInfo.free();
    // Create the command buffer begin structure
    VkCommandBufferBeginInfo cmdBufInfo = VkCommandBufferBeginInfo.calloc().sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO).pNext(NULL);
    // Specify clear color (cornflower blue)
    VkClearValue.Buffer clearValues = VkClearValue.calloc(2);
    clearValues.get(0).color().float32(0, 100 / 255.0f).float32(1, 149 / 255.0f).float32(2, 237 / 255.0f).float32(3, 1.0f);
    // Specify clear depth-stencil
    clearValues.get(1).depthStencil().depth(1.0f).stencil(0);
    // Specify everything to begin a render pass
    VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo.calloc().sType(VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO).pNext(NULL).renderPass(renderPass).pClearValues(clearValues);
    VkRect2D renderArea = renderPassBeginInfo.renderArea();
    renderArea.offset().set(0, 0);
    renderArea.extent().set(width, height);
    for (int i = 0; i < renderCommandBuffers.length; ++i) {
        // Set target frame buffer
        renderPassBeginInfo.framebuffer(framebuffers[i]);
        err = vkBeginCommandBuffer(renderCommandBuffers[i], cmdBufInfo);
        if (err != VK_SUCCESS) {
            throw new AssertionError("Failed to begin render command buffer: " + translateVulkanResult(err));
        }
        vkCmdBeginRenderPass(renderCommandBuffers[i], renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
        // Update dynamic viewport state
        VkViewport.Buffer viewport = VkViewport.calloc(1).height(height).width(width).minDepth(0.0f).maxDepth(1.0f);
        vkCmdSetViewport(renderCommandBuffers[i], 0, viewport);
        viewport.free();
        // Update dynamic scissor state
        VkRect2D.Buffer scissor = VkRect2D.calloc(1);
        scissor.extent().set(width, height);
        scissor.offset().set(0, 0);
        vkCmdSetScissor(renderCommandBuffers[i], 0, scissor);
        scissor.free();
        // Bind descriptor sets describing shader binding points
        LongBuffer descriptorSets = memAllocLong(1).put(0, descriptorSet);
        vkCmdBindDescriptorSets(renderCommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.layout, 0, descriptorSets, null);
        memFree(descriptorSets);
        // Bind the rendering pipeline (including the shaders)
        vkCmdBindPipeline(renderCommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline);
        // Bind triangle vertices
        LongBuffer offsets = memAllocLong(1);
        offsets.put(0, 0L);
        LongBuffer pBuffers = memAllocLong(1);
        pBuffers.put(0, verticesBuf);
        vkCmdBindVertexBuffers(renderCommandBuffers[i], 0, pBuffers, offsets);
        memFree(pBuffers);
        memFree(offsets);
        // Draw triangle
        vkCmdDraw(renderCommandBuffers[i], SPHERE_TESSELATION * SPHERE_TESSELATION * 2, spheres.length, 0, 0);
        vkCmdEndRenderPass(renderCommandBuffers[i]);
        // Add a present memory barrier to the end of the command buffer
        // This will transform the frame buffer color attachment to a
        // new layout for presenting it to the windowing system integration
        VkImageMemoryBarrier.Buffer prePresentBarrier = createPrePresentBarrier(swapchain.images[i]);
        vkCmdPipelineBarrier(renderCommandBuffers[i], VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_FLAGS_NONE, // No memory barriers
        null, // No buffer memory barriers
        null, // One image memory barrier
        prePresentBarrier);
        prePresentBarrier.free();
        err = vkEndCommandBuffer(renderCommandBuffers[i]);
        if (err != VK_SUCCESS) {
            throw new AssertionError("Failed to begin render command buffer: " + translateVulkanResult(err));
        }
    }
    renderPassBeginInfo.free();
    clearValues.free();
    cmdBufInfo.free();
    return renderCommandBuffers;
}
Also used : VkRect2D(org.lwjgl.vulkan.VkRect2D) LongBuffer(java.nio.LongBuffer) VkCommandBuffer(org.lwjgl.vulkan.VkCommandBuffer) PointerBuffer(org.lwjgl.PointerBuffer) VkViewport(org.lwjgl.vulkan.VkViewport) VkCommandBufferAllocateInfo(org.lwjgl.vulkan.VkCommandBufferAllocateInfo) VkRenderPassBeginInfo(org.lwjgl.vulkan.VkRenderPassBeginInfo) VkClearValue(org.lwjgl.vulkan.VkClearValue) VkCommandBufferBeginInfo(org.lwjgl.vulkan.VkCommandBufferBeginInfo) VkImageMemoryBarrier(org.lwjgl.vulkan.VkImageMemoryBarrier)

Example 7 with VkRenderPassBeginInfo

use of org.lwjgl.vulkan.VkRenderPassBeginInfo in project lwjgl3-demos by LWJGL.

the class ClearScreenDemo method createRenderCommandBuffers.

private static VkCommandBuffer[] createRenderCommandBuffers(VkDevice device, long commandPool, long[] framebuffers, long renderPass, int width, int height) {
    // Create the render command buffers (one command buffer per framebuffer image)
    VkCommandBufferAllocateInfo cmdBufAllocateInfo = VkCommandBufferAllocateInfo.calloc().sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO).commandPool(commandPool).level(VK_COMMAND_BUFFER_LEVEL_PRIMARY).commandBufferCount(framebuffers.length);
    PointerBuffer pCommandBuffer = memAllocPointer(framebuffers.length);
    int err = vkAllocateCommandBuffers(device, cmdBufAllocateInfo, pCommandBuffer);
    if (err != VK_SUCCESS) {
        throw new AssertionError("Failed to allocate render command buffer: " + translateVulkanResult(err));
    }
    VkCommandBuffer[] renderCommandBuffers = new VkCommandBuffer[framebuffers.length];
    for (int i = 0; i < framebuffers.length; i++) {
        renderCommandBuffers[i] = new VkCommandBuffer(pCommandBuffer.get(i), device);
    }
    memFree(pCommandBuffer);
    cmdBufAllocateInfo.free();
    // Create the command buffer begin structure
    VkCommandBufferBeginInfo cmdBufInfo = VkCommandBufferBeginInfo.calloc().sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO).pNext(NULL);
    // Specify clear color (cornflower blue)
    VkClearValue.Buffer clearValues = VkClearValue.calloc(1);
    clearValues.color().float32(0, 100 / 255.0f).float32(1, 149 / 255.0f).float32(2, 237 / 255.0f).float32(3, 1.0f);
    // Specify everything to begin a render pass
    VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo.calloc().sType(VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO).pNext(NULL).renderPass(renderPass).pClearValues(clearValues);
    VkRect2D renderArea = renderPassBeginInfo.renderArea();
    renderArea.offset().x(0).y(0);
    renderArea.extent().width(width).height(height);
    for (int i = 0; i < renderCommandBuffers.length; ++i) {
        // Set target frame buffer
        renderPassBeginInfo.framebuffer(framebuffers[i]);
        err = vkBeginCommandBuffer(renderCommandBuffers[i], cmdBufInfo);
        if (err != VK_SUCCESS) {
            throw new AssertionError("Failed to begin render command buffer: " + translateVulkanResult(err));
        }
        vkCmdBeginRenderPass(renderCommandBuffers[i], renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
        // Update dynamic viewport state
        VkViewport.Buffer viewport = VkViewport.calloc(1).height(height).width(width).minDepth(0.0f).maxDepth(1.0f);
        vkCmdSetViewport(renderCommandBuffers[i], 0, viewport);
        viewport.free();
        // Update dynamic scissor state
        VkRect2D.Buffer scissor = VkRect2D.calloc(1);
        scissor.extent().width(width).height(height);
        scissor.offset().x(0).y(0);
        vkCmdSetScissor(renderCommandBuffers[i], 0, scissor);
        scissor.free();
        vkCmdEndRenderPass(renderCommandBuffers[i]);
        // Add a present memory barrier to the end of the command buffer
        // This will transform the frame buffer color attachment to a
        // new layout for presenting it to the windowing system integration
        VkImageMemoryBarrier.Buffer prePresentBarrier = createPrePresentBarrier(swapchain.images[i]);
        vkCmdPipelineBarrier(renderCommandBuffers[i], VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_FLAGS_NONE, // No memory barriers
        null, // No buffer memory barriers
        null, // One image memory barrier
        prePresentBarrier);
        prePresentBarrier.free();
        err = vkEndCommandBuffer(renderCommandBuffers[i]);
        if (err != VK_SUCCESS) {
            throw new AssertionError("Failed to begin render command buffer: " + translateVulkanResult(err));
        }
    }
    renderPassBeginInfo.free();
    clearValues.free();
    cmdBufInfo.free();
    return renderCommandBuffers;
}
Also used : VkRect2D(org.lwjgl.vulkan.VkRect2D) VkCommandBuffer(org.lwjgl.vulkan.VkCommandBuffer) PointerBuffer(org.lwjgl.PointerBuffer) VkViewport(org.lwjgl.vulkan.VkViewport) VkCommandBufferAllocateInfo(org.lwjgl.vulkan.VkCommandBufferAllocateInfo) VkRenderPassBeginInfo(org.lwjgl.vulkan.VkRenderPassBeginInfo) VkClearValue(org.lwjgl.vulkan.VkClearValue) VkCommandBufferBeginInfo(org.lwjgl.vulkan.VkCommandBufferBeginInfo) VkImageMemoryBarrier(org.lwjgl.vulkan.VkImageMemoryBarrier)

Aggregations

PointerBuffer (org.lwjgl.PointerBuffer)7 VkClearValue (org.lwjgl.vulkan.VkClearValue)7 VkCommandBuffer (org.lwjgl.vulkan.VkCommandBuffer)7 VkCommandBufferAllocateInfo (org.lwjgl.vulkan.VkCommandBufferAllocateInfo)7 VkCommandBufferBeginInfo (org.lwjgl.vulkan.VkCommandBufferBeginInfo)7 VkImageMemoryBarrier (org.lwjgl.vulkan.VkImageMemoryBarrier)7 VkRect2D (org.lwjgl.vulkan.VkRect2D)7 VkRenderPassBeginInfo (org.lwjgl.vulkan.VkRenderPassBeginInfo)7 VkViewport (org.lwjgl.vulkan.VkViewport)7 LongBuffer (java.nio.LongBuffer)6