1 Star 0 Fork 0

愤怒的小白/test-vulkan

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
first_app.h 10.01 KB
一键复制 编辑 原始数据 按行查看 历史
李快 提交于 1年前 . 修正: 编译vulkan
#pragma once
#include "xb_device.h"
#include "xb_pipeline.h"
#include "xb_window.h"
#include "xb_swap_chain.h"
#include <array>
#include "memory.h"
namespace xb {
class FirstApp {
public:
static constexpr int WIDTH = 800;
static constexpr int HEIGHT = 600;
FirstApp();
~FirstApp();
void run();
private:
std::vector<VkCommandBuffer> commandBuffers;
VkPipelineLayout pipelineLayout;
VkBuffer indexBuffer;
VkDeviceMemory indexBufferMemory;
const std::vector<uint16_t> indices = {
0, 1, 2, 2, 3, 0
};
void createPipelineLayout();
void createPipeline();
void createCommandBuffers();
void drawFrame();
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
XbWindow xbWindow{ WIDTH, HEIGHT, "hello vulkan"};
XbDevice xbDevice{ xbWindow };
XbSwapChain xbSwapChain{ xbDevice, xbWindow};
XbPipeline xbPipline{ xbDevice };
XbModel xbModel{ xbDevice };
void loadModels(){
std::vector<XbModel::Vertex> vertices {
{{1.0f,-0.5f}, {1.0f, 0.0f,0.0f}},
{{0.5f,0.5f}, {0.0f, 1.0f,0.0f}},
{{-0.5f,0.5f}, {0.0f, 0.0f,1.0f}}
};
xbModel.createVertexBuffers(vertices);
}
// void createIndexBuffer() {
// VkDeviceSize bufferSize = sizeof(indices[0]) * indices.size();
// VkBuffer stagingBuffer;
// VkDeviceMemory stagingBufferMemory;
// createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
// void* data;
// vkMapMemory(xbDevice.device, stagingBufferMemory, 0, bufferSize, 0, &data);
// memcpy(data, indices.data(), (size_t) bufferSize);
// vkUnmapMemory(xbDevice.device, stagingBufferMemory);
// createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
// copyBuffer(stagingBuffer, indexBuffer, bufferSize);
// vkDestroyBuffer(xbDevice.device, stagingBuffer, nullptr);
// vkFreeMemory(xbDevice.device, stagingBufferMemory, nullptr);
// }
// void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
// VkCommandBuffer commandBuffer = beginSingleTimeCommands();
// VkBufferCopy copyRegion{};
// copyRegion.size = size;
// vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
// endSingleTimeCommands(commandBuffer);
// }
// VkCommandBuffer beginSingleTimeCommands() {
// VkCommandBufferAllocateInfo allocInfo{};
// allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
// allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
// allocInfo.commandPool = xbDevice.getCommandPool();
// allocInfo.commandBufferCount = 1;
// VkCommandBuffer commandBuffer;
// vkAllocateCommandBuffers(xbDevice.device, &allocInfo, &commandBuffer);
// VkCommandBufferBeginInfo beginInfo{};
// beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
// beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
// vkBeginCommandBuffer(commandBuffer, &beginInfo);
// return commandBuffer;
// }
// void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
// vkEndCommandBuffer(commandBuffer);
// VkSubmitInfo submitInfo{};
// submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
// submitInfo.commandBufferCount = 1;
// submitInfo.pCommandBuffers = &commandBuffer;
// vkQueueSubmit(xbDevice.graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
// vkQueueWaitIdle(xbDevice.graphicsQueue);
// vkFreeCommandBuffers(xbDevice.device, xbDevice.getCommandPool(), 1, &commandBuffer);
// }
};
inline FirstApp::FirstApp()
{
createPipelineLayout();
createPipeline();
loadModels();
//createIndexBuffer();
createCommandBuffers();
}
FirstApp::~FirstApp(){
vkDestroyPipelineLayout(xbDevice.device, pipelineLayout, nullptr);
}
void FirstApp::run()
{
while (!xbWindow.shouldClose())
{
glfwPollEvents();
drawFrame();
}
}
inline void FirstApp::createPipelineLayout()
{
VkDescriptorSetLayoutBinding uboLayoutBinding{};
uboLayoutBinding.binding = 0;
uboLayoutBinding.descriptorCount = 1;
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uboLayoutBinding.pImmutableSamplers = nullptr;
uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
VkDescriptorSetLayoutCreateInfo layoutInfo{};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = 1;
layoutInfo.pBindings = &uboLayoutBinding;
VkDescriptorSetLayout descriptorSetLayout;
if (vkCreateDescriptorSetLayout(xbDevice.device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
throw std::runtime_error("failed to create descriptor set layout!");
}
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
pipelineLayoutInfo.pushConstantRangeCount = 0;
pipelineLayoutInfo.pPushConstantRanges = nullptr;
if (vkCreatePipelineLayout(xbDevice.device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
throw std::runtime_error("failed to create pipeline layout!");
}
}
inline void FirstApp::createPipeline()
{
auto pipelineConfig = XbPipeline::defaultPipelineConfigInfo(xbSwapChain.width(), xbSwapChain.height());
pipelineConfig.renderPass = xbSwapChain.GetRenderPass();
pipelineConfig.pipelineLayout = pipelineLayout;
xbPipline.createGraphicsPipeline(pipelineConfig);
}
inline void FirstApp::createCommandBuffers()
{
commandBuffers.resize(xbSwapChain.imageCount());
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = xbDevice.getCommandPool();
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
if (vkAllocateCommandBuffers(xbDevice.device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate command buffers!");
}
for (int i = 0; i < commandBuffers.size(); i++)
{
recordCommandBuffer(commandBuffers[i], i);
}
}
void FirstApp::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) {
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) {
throw std::runtime_error("failed to begin recording command buffer!");
}
VkExtent2D swapChainExtent = xbSwapChain.getSwapChainExtent();
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = xbSwapChain.GetRenderPass();
renderPassInfo.framebuffer = xbSwapChain.getFramebuffers(imageIndex);
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = swapChainExtent;
std::array<VkClearValue,2> clearValues{};
clearValues[0].color = {0.0f, 0.0f, 0.0f, 0.2f};
clearValues[1].depthStencil = {1.0f, 0};
renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
xbPipline.bind(commandBuffer);
VkViewport viewport{};
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;
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
VkRect2D scissor{};
scissor.offset = {0, 0};
scissor.extent = swapChainExtent;
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
xbModel.bind(commandBuffer);
xbModel.draw(commandBuffer);
//vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
//vkCmdDraw(commandBuffer, 3, 1, 0, 0);
//vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices.size()), 1, 0, 0, 0);
vkCmdEndRenderPass(commandBuffer);
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
throw std::runtime_error("failed to record command buffer!");
}
}
inline void FirstApp::drawFrame()
{
uint32_t imageIndex;
VkResult result = xbSwapChain.acquireNextImage(&imageIndex);
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
throw std::runtime_error("failed to acquire swap chain image!");
}
result = xbSwapChain.submitCommandBuffers(&commandBuffers[imageIndex], &imageIndex);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fastCode2019/test-vulkan.git
git@gitee.com:fastCode2019/test-vulkan.git
fastCode2019
test-vulkan
test-vulkan
master

搜索帮助