|
| 1 | +#include <iostream> |
| 2 | +#include <cstring> |
| 3 | +#include <glm/mat4x4.hpp> |
| 4 | +#include <glm/gtx/transform.hpp> |
| 5 | +#include "VK_UniformBuffer.h" |
| 6 | +#include "VK_Context.h" |
| 7 | +#include "VK_Image.h" |
| 8 | +#include "VK_Texture.h" |
| 9 | +#include "VK_Pipeline.h" |
| 10 | +#include "VK_DynamicState.h" |
| 11 | +#include "VK_CommandPool.h" |
| 12 | +//#include "VK_Util.h" |
| 13 | +#include "tiff.h" |
| 14 | +#include <fstream> |
| 15 | + |
| 16 | +using namespace std; |
| 17 | + |
| 18 | +const std::vector<float> vertices = { |
| 19 | + -0.5f, -0.5, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.05f, 0.0f, |
| 20 | + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 21 | + 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.5f, |
| 22 | + -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.5f, 1.5f |
| 23 | + }; |
| 24 | + |
| 25 | +const std::vector<uint32_t> indices = { |
| 26 | + 0, 1, 2, 2, 3, 0 |
| 27 | +}; |
| 28 | + |
| 29 | +VK_Context *context = nullptr; |
| 30 | +VK_Pipeline *pipeline = nullptr; |
| 31 | + |
| 32 | +uint32_t updateUniformBufferData(char *&data, uint32_t size) |
| 33 | +{ |
| 34 | + glm::mat4 model = glm::identity<glm::mat4>(); |
| 35 | + memcpy(data, &model[0][0], size); |
| 36 | + return sizeof(model); |
| 37 | +} |
| 38 | + |
| 39 | +void onFrameSizeChanged(int width, int height) |
| 40 | +{ |
| 41 | + pipeline->getDynamicState()->applyDynamicViewport({0, 0, (float)width, (float)height, 0, 1}); |
| 42 | +} |
| 43 | + |
| 44 | +//void writeFile(VK_Context *context, VkImage image, uint32_t width, uint32_t height); |
| 45 | + |
| 46 | +int main() |
| 47 | +{ |
| 48 | + VK_ContextConfig config; |
| 49 | + config.debug = true; |
| 50 | + config.name = "Save Image"; |
| 51 | + |
| 52 | + context = createVkContext(config); |
| 53 | + context->createWindow(480, 480, true); |
| 54 | + context->setOnFrameSizeChanged(onFrameSizeChanged); |
| 55 | + |
| 56 | + VK_Context::VK_Config vkConfig; |
| 57 | + context->initVulkanDevice(vkConfig); |
| 58 | + |
| 59 | + auto shaderSet = context->createShaderSet(); |
| 60 | + shaderSet->addShader("../shader/texture/vert.spv", VK_SHADER_STAGE_VERTEX_BIT); |
| 61 | + shaderSet->addShader("../shader/texture/frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); |
| 62 | + |
| 63 | + shaderSet->appendVertexAttributeDescription(0, sizeof (float) * 3, VK_FORMAT_R32G32B32_SFLOAT, 0); |
| 64 | + shaderSet->appendVertexAttributeDescription(1, sizeof (float) * 4, VK_FORMAT_R32G32B32A32_SFLOAT, |
| 65 | + sizeof(float) * 3); |
| 66 | + shaderSet->appendVertexAttributeDescription(2, sizeof (float) * 2, VK_FORMAT_R32G32_SFLOAT, |
| 67 | + sizeof(float) * 7); |
| 68 | + |
| 69 | + shaderSet->appendVertexInputBindingDescription(9 * sizeof(float), 0, VK_VERTEX_INPUT_RATE_VERTEX); |
| 70 | + |
| 71 | + VkDescriptorSetLayoutBinding uniformBinding = VK_ShaderSet::createDescriptorSetLayoutBinding(0, |
| 72 | + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); |
| 73 | + shaderSet->addDescriptorSetLayoutBinding(uniformBinding); |
| 74 | + |
| 75 | + auto samplerBinding = VK_ShaderSet::createDescriptorSetLayoutBinding(1, |
| 76 | + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 77 | + VK_SHADER_STAGE_FRAGMENT_BIT); |
| 78 | + auto samplerCreateInfo = VK_Sampler::createSamplerCreateInfo(); |
| 79 | + auto samplerPtr = context->createSampler(samplerCreateInfo); |
| 80 | + VkSampler sampler = samplerPtr->getSampler(); |
| 81 | + samplerBinding.pImmutableSamplers = &sampler; |
| 82 | + |
| 83 | + shaderSet->addDescriptorSetLayoutBinding(samplerBinding); |
| 84 | + |
| 85 | + if (!shaderSet->isValid()) { |
| 86 | + std::cerr << "invalid shaderSet" << std::endl; |
| 87 | + shaderSet->release(); |
| 88 | + context->release(); |
| 89 | + return -1; |
| 90 | + } |
| 91 | + |
| 92 | + auto ubo = shaderSet->addUniformBuffer(0, sizeof(float) * 16); |
| 93 | + ubo->setWriteDataCallback(updateUniformBufferData); |
| 94 | + |
| 95 | + auto image = context->createImage("../images/cat.png"); |
| 96 | + |
| 97 | + auto cmd = context->getCommandPool()->beginSingleTimeCommands(); |
| 98 | + adjustImageLayout(cmd, image->getImage(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
| 99 | + context->getCommandPool()->endSingleTimeCommands(cmd, context->getGraphicQueue()); |
| 100 | + writeFile(context, "cat.ppm", image->getImage(), image->getWidth(), |
| 101 | + image->getHeight()); |
| 102 | + |
| 103 | + cmd = context->getCommandPool()->beginSingleTimeCommands(); |
| 104 | + adjustImageLayout(cmd, image->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
| 105 | + context->getCommandPool()->endSingleTimeCommands(cmd, context->getGraphicQueue()); |
| 106 | + |
| 107 | + auto imageViewCreateInfo = VK_ImageView::createImageViewCreateInfo(image->getImage(), |
| 108 | + VK_FORMAT_R8G8B8A8_SRGB); |
| 109 | + auto imageView = context->createImageView(imageViewCreateInfo); |
| 110 | + shaderSet->addImageView(imageView, 1); |
| 111 | + |
| 112 | + context->initVulkanContext(); |
| 113 | + pipeline = context->createPipeline(shaderSet); |
| 114 | + pipeline->getDynamicState()->addDynamicState(VK_DYNAMIC_STATE_VIEWPORT); |
| 115 | + pipeline->create(); |
| 116 | + pipeline->getDynamicState()->applyDynamicViewport({0, 0, 480, 480, 0, 1}); |
| 117 | + |
| 118 | + auto buffer = context->createVertexBuffer(vertices, 9, indices); |
| 119 | + pipeline->addRenderBuffer(buffer); |
| 120 | + |
| 121 | + context->createCommandBuffers(); |
| 122 | + |
| 123 | + context->run(); |
| 124 | + context->release(); |
| 125 | + |
| 126 | + return 0; |
| 127 | +} |
| 128 | + |
0 commit comments