diff --git a/projects/first_mesh/src/main.cpp b/projects/first_mesh/src/main.cpp index 107cd432e1bc902692bb5b1fa694120ddf24038b..93d8103d449ff6d3cccdee9298b816b0eb974b54 100644 --- a/projects/first_mesh/src/main.cpp +++ b/projects/first_mesh/src/main.cpp @@ -147,8 +147,10 @@ int main(int argc, const char** argv) { auto end = std::chrono::system_clock::now(); auto deltatime = end - start; start = end; - cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count()); - const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); +// cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count()); +// const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); + cameraManager.getTrackballCamera().updateView(std::chrono::duration<double>(deltatime).count()); + const glm::mat4 mvp = cameraManager.getTrackballCamera().getProjection() * cameraManager.getTrackballCamera().getView(); core.renderMesh( trianglePass, diff --git a/projects/first_triangle/src/main.cpp b/projects/first_triangle/src/main.cpp index 6b4a83c9769d88fcd0310ee3d646b225bc9f0615..5568a497a83be0f7b87f3d902020056886b8649f 100644 --- a/projects/first_triangle/src/main.cpp +++ b/projects/first_triangle/src/main.cpp @@ -5,161 +5,163 @@ #include <chrono> int main(int argc, const char** argv) { - const char* applicationName = "First Triangle"; - - const int windowWidth = 800; - const int windowHeight = 600; - vkcv::Window window = vkcv::Window::create( - applicationName, - windowWidth, - windowHeight, - false - ); - - vkcv::CameraManager cameraManager(window, windowWidth, windowHeight); - - window.initEvents(); - - vkcv::Core core = vkcv::Core::create( - window, - applicationName, - VK_MAKE_VERSION(0, 0, 1), - { vk::QueueFlagBits::eTransfer,vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eCompute }, - {}, - { "VK_KHR_swapchain" } - ); - - const auto& context = core.getContext(); - const vk::Instance& instance = context.getInstance(); - const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice(); - const vk::Device& device = context.getDevice(); - - struct vec3 { - float x, y, z; - }; - - const size_t n = 5027; - - auto testBuffer = core.createBuffer<vec3>(vkcv::BufferType::VERTEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL); - vec3 vec_data[n]; - - for (size_t i = 0; i < n; i++) { - vec_data[i] = { 42, static_cast<float>(i), 7 }; - } - - testBuffer.fill(vec_data); - - auto triangleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL); - uint16_t indices[3] = { 0, 1, 2 }; - triangleIndexBuffer.fill(&indices[0], sizeof(indices)); - - /*vec3* m = buffer.map(); - m[0] = { 0, 0, 0 }; - m[1] = { 0, 0, 0 }; - m[2] = { 0, 0, 0 }; - buffer.unmap();*/ - - vkcv::SamplerHandle sampler = core.createSampler( - vkcv::SamplerFilterType::NEAREST, - vkcv::SamplerFilterType::NEAREST, - vkcv::SamplerMipmapMode::NEAREST, - vkcv::SamplerAddressMode::REPEAT - ); - - std::cout << "Physical device: " << physicalDevice.getProperties().deviceName << std::endl; - - switch (physicalDevice.getProperties().vendorID) { - case 0x1002: std::cout << "Running AMD huh? You like underdogs, are you a Linux user?" << std::endl; break; - case 0x10DE: std::cout << "An NVidia GPU, how predictable..." << std::endl; break; - case 0x8086: std::cout << "Poor child, running on an Intel GPU, probably integrated..." - "or perhaps you are from the future with a dedicated one?" << std::endl; break; - case 0x13B5: std::cout << "ARM? What the hell are you running on, next thing I know you're trying to run Vulkan on a leg..." << std::endl; break; - default: std::cout << "Unknown GPU vendor?! Either you're on an exotic system or your driver is broken..." << std::endl; - } - - // an example attachment for passes that output to the window - const vkcv::AttachmentDescription present_color_attachment( - vkcv::AttachmentLayout::UNDEFINED, - vkcv::AttachmentLayout::COLOR_ATTACHMENT, - vkcv::AttachmentLayout::PRESENTATION, - vkcv::AttachmentOperation::STORE, - vkcv::AttachmentOperation::CLEAR, - core.getSwapchainImageFormat()); - - vkcv::PassConfig trianglePassDefinition({ present_color_attachment }); - vkcv::PassHandle trianglePass = core.createPass(trianglePassDefinition); - - if (!trianglePass) - { - std::cout << "Error. Could not create renderpass. Exiting." << std::endl; - return EXIT_FAILURE; - } - - vkcv::ShaderProgram triangleShaderProgram{}; - triangleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv")); - triangleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("shaders/frag.spv")); - triangleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX); - triangleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT); - - const vkcv::PipelineConfig trianglePipelineDefinition( - triangleShaderProgram, - windowWidth, - windowHeight, - trianglePass, - {}, - {}); - vkcv::PipelineHandle trianglePipeline = core.createGraphicsPipeline(trianglePipelineDefinition); - - if (!trianglePipeline) - { - std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl; - return EXIT_FAILURE; - } - - std::vector<vkcv::VertexBufferBinding> vertexBufferBindings; - - /* - * BufferHandle triangleVertices = core.createBuffer(vertices); - * BufferHandle triangleIndices = core.createBuffer(indices); - * - * // triangle Model creation goes here - * - * - * // attachment creation goes here - * PassHandle trianglePass = core.CreatePass(presentationPass); - * - * // shader creation goes here - * // material creation goes here - * - * PipelineHandle trianglePipeline = core.CreatePipeline(trianglePipeline); - */ + const char* applicationName = "First Triangle"; + + const int windowWidth = 800; + const int windowHeight = 600; + vkcv::Window window = vkcv::Window::create( + applicationName, + windowWidth, + windowHeight, + false + ); + + vkcv::CameraManager cameraManager(window, windowWidth, windowHeight); + cameraManager.getTrackballCamera().setPosition(glm::vec3(0.0f,0.5f,0.0f)); + cameraManager.getTrackballCamera().setCenter(glm::vec3(0.0f,0.0f,-1.0f)); + + window.initEvents(); + + vkcv::Core core = vkcv::Core::create( + window, + applicationName, + VK_MAKE_VERSION(0, 0, 1), + { vk::QueueFlagBits::eTransfer,vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eCompute }, + {}, + { "VK_KHR_swapchain" } + ); + + const auto& context = core.getContext(); + const vk::Instance& instance = context.getInstance(); + const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice(); + const vk::Device& device = context.getDevice(); + + struct vec3 { + float x, y, z; + }; + + const size_t n = 5027; + + auto testBuffer = core.createBuffer<vec3>(vkcv::BufferType::VERTEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL); + vec3 vec_data[n]; + + for (size_t i = 0; i < n; i++) { + vec_data[i] = { 42, static_cast<float>(i), 7 }; + } + + testBuffer.fill(vec_data); + + auto triangleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, n, vkcv::BufferMemoryType::DEVICE_LOCAL); + uint16_t indices[3] = { 0, 1, 2 }; + triangleIndexBuffer.fill(&indices[0], sizeof(indices)); + + /*vec3* m = buffer.map(); + m[0] = { 0, 0, 0 }; + m[1] = { 0, 0, 0 }; + m[2] = { 0, 0, 0 }; + buffer.unmap();*/ + + vkcv::SamplerHandle sampler = core.createSampler( + vkcv::SamplerFilterType::NEAREST, + vkcv::SamplerFilterType::NEAREST, + vkcv::SamplerMipmapMode::NEAREST, + vkcv::SamplerAddressMode::REPEAT + ); + + std::cout << "Physical device: " << physicalDevice.getProperties().deviceName << std::endl; + + switch (physicalDevice.getProperties().vendorID) { + case 0x1002: std::cout << "Running AMD huh? You like underdogs, are you a Linux user?" << std::endl; break; + case 0x10DE: std::cout << "An NVidia GPU, how predictable..." << std::endl; break; + case 0x8086: std::cout << "Poor child, running on an Intel GPU, probably integrated..." + "or perhaps you are from the future with a dedicated one?" << std::endl; break; + case 0x13B5: std::cout << "ARM? What the hell are you running on, next thing I know you're trying to run Vulkan on a leg..." << std::endl; break; + default: std::cout << "Unknown GPU vendor?! Either you're on an exotic system or your driver is broken..." << std::endl; + } + + // an example attachment for passes that output to the window + const vkcv::AttachmentDescription present_color_attachment( + vkcv::AttachmentLayout::UNDEFINED, + vkcv::AttachmentLayout::COLOR_ATTACHMENT, + vkcv::AttachmentLayout::PRESENTATION, + vkcv::AttachmentOperation::STORE, + vkcv::AttachmentOperation::CLEAR, + core.getSwapchainImageFormat()); + + vkcv::PassConfig trianglePassDefinition({ present_color_attachment }); + vkcv::PassHandle trianglePass = core.createPass(trianglePassDefinition); + + if (!trianglePass) + { + std::cout << "Error. Could not create renderpass. Exiting." << std::endl; + return EXIT_FAILURE; + } + + vkcv::ShaderProgram triangleShaderProgram{}; + triangleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv")); + triangleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("shaders/frag.spv")); + triangleShaderProgram.reflectShader(vkcv::ShaderStage::VERTEX); + triangleShaderProgram.reflectShader(vkcv::ShaderStage::FRAGMENT); + + const vkcv::PipelineConfig trianglePipelineDefinition( + triangleShaderProgram, + windowWidth, + windowHeight, + trianglePass, + {}, + {}); + vkcv::PipelineHandle trianglePipeline = core.createGraphicsPipeline(trianglePipelineDefinition); + + if (!trianglePipeline) + { + std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl; + return EXIT_FAILURE; + } + + std::vector<vkcv::VertexBufferBinding> vertexBufferBindings; + + /* + * BufferHandle triangleVertices = core.createBuffer(vertices); + * BufferHandle triangleIndices = core.createBuffer(indices); + * + * // triangle Model creation goes here + * + * + * // attachment creation goes here + * PassHandle trianglePass = core.CreatePass(presentationPass); + * + * // shader creation goes here + * // material creation goes here + * + * PipelineHandle trianglePipeline = core.CreatePipeline(trianglePipeline); + */ auto start = std::chrono::system_clock::now(); - while (window.isWindowOpen()) - { - core.beginFrame(); + while (window.isWindowOpen()) + { + core.beginFrame(); window.pollEvents(); auto end = std::chrono::system_clock::now(); auto deltatime = end - start; start = end; // cameraManager.getCamera().updateView(std::chrono::duration<double>(deltatime).count()); -// const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); +// const glm::mat4 mvp = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); cameraManager.getTrackballCamera().updateView(std::chrono::duration<double>(deltatime).count()); const glm::mat4 mvp = cameraManager.getTrackballCamera().getProjection() * cameraManager.getTrackballCamera().getView(); - core.renderMesh( - trianglePass, - trianglePipeline, - windowWidth, - windowHeight, - sizeof(mvp), - &mvp, - vertexBufferBindings, - triangleIndexBuffer.getHandle(), - 3, - vkcv::ResourcesHandle(), - 0); - - core.endFrame(); - } - return 0; + core.renderMesh( + trianglePass, + trianglePipeline, + windowWidth, + windowHeight, + sizeof(mvp), + &mvp, + vertexBufferBindings, + triangleIndexBuffer.getHandle(), + 3, + vkcv::ResourcesHandle(), + 0); + + core.endFrame(); + } + return 0; }