假设 Vulkan
已经初始化,您正在录制到命令缓冲区,您有一个帧缓冲区和一个渲染通道,如何使用 Vulkan 绘制原始的 2D 几何形状?
vkCmdBeginRenderPass(command_buffer, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
// Draw primitives here
vkCmdEndRenderPass(command_buffer);
绘图设置非常复杂,但绘制所需的实际调用相当简单。从tri.c
(绘制 2D 三角形):
// ...
vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
demo->pipeline);
vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
0, 1, & demo->desc_set, 0, NULL);
VkViewport viewport = {};
viewport.height = (float) demo->height;
viewport.width = (float) demo->width;
viewport.minDepth = (float) 0.0f;
viewport.maxDepth = (float) 1.0f;
vkCmdSetViewport(demo->draw_cmd, 0, 1, &viewport);
VkRect2D scissor = {};
scissor.extent.width = demo->width;
scissor.extent.height = demo->height;
scissor.offset.x = 0;
scissor.offset.y = 0;
vkCmdSetScissor(demo->draw_cmd, 0, 1, &scissor);
VkDeviceSize offsets[1] = {0};
vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
vkCmdDraw(demo->draw_cmd, 3, 1, 0, 0);
vkCmdEndRenderPass(demo->draw_cmd);
// ...
此代码段假定你已获得处于适当状态的VkPipeline
(包括着色器等)、VkBuffer
(用于顶点缓冲区)和VkCommandBuffer
。执行包含VkCommandBuffer
后,实际发出要绘制的命令的是vkCmdDraw
。