2023-06-12 11:05:10 +08:00
|
|
|
|
|
2023-06-07 00:45:10 +08:00
|
|
|
|
|
2023-05-26 14:12:02 +08:00
|
|
|
|
|
2023-05-26 16:19:12 +08:00
|
|
|
|
#ifndef TINYGLTF_IMPLEMENTATION
|
|
|
|
|
#define TINYGLTF_IMPLEMENTATION
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef STB_IMAGE_IMPLEMENTATION
|
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
|
#endif
|
2023-05-26 14:12:02 +08:00
|
|
|
|
|
2023-05-26 16:19:12 +08:00
|
|
|
|
#ifndef TINYGLTF_NO_STB_IMAGE_WRITE
|
|
|
|
|
#define TINYGLTF_NO_STB_IMAGE_WRITE
|
|
|
|
|
#endif
|
2023-05-26 14:12:02 +08:00
|
|
|
|
|
|
|
|
|
|
2023-05-26 16:19:12 +08:00
|
|
|
|
#include "render.h"
|
2023-06-07 10:52:04 +08:00
|
|
|
|
//#include "VulkanUtils.hpp"
|
2023-06-07 00:45:10 +08:00
|
|
|
|
//#include "assetLoader.h"
|
|
|
|
|
|
2023-05-17 14:53:31 +08:00
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
void PlumageRender::renderMain::initWindow(int Width, int Height)
|
2024-04-01 13:45:00 +08:00
|
|
|
|
{
|
|
|
|
|
glfwInit();
|
|
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
2024-04-07 17:29:35 +08:00
|
|
|
|
|
2024-04-01 13:45:00 +08:00
|
|
|
|
window = glfwCreateWindow(Width, Height, "vulkan", nullptr, nullptr);
|
|
|
|
|
glfwSetWindowUserPointer(window, this);
|
|
|
|
|
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
void PlumageRender::renderMain::framebufferResizeCallback(GLFWwindow* window, int width, int height)
|
2024-04-01 13:45:00 +08:00
|
|
|
|
{
|
2024-04-07 17:29:35 +08:00
|
|
|
|
auto app = reinterpret_cast<renderMain*>(glfwGetWindowUserPointer(window));
|
2024-04-01 13:45:00 +08:00
|
|
|
|
app->framebufferResized = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 18:05:17 +08:00
|
|
|
|
|
|
|
|
|
// todo:重写成glfw的
|
2024-04-07 17:29:35 +08:00
|
|
|
|
void PlumageRender::renderMain::windowResized()
|
2023-06-06 15:52:39 +08:00
|
|
|
|
{
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
vkDeviceWaitIdle(device);
|
|
|
|
|
updateUniformBuffers();
|
2023-06-07 10:52:04 +08:00
|
|
|
|
//update UI
|
|
|
|
|
updateUIOverlay();
|
2023-05-17 14:49:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
void PlumageRender::renderMain::prepare()
|
|
|
|
|
{
|
|
|
|
|
//VulkanExampleBase::prepare();
|
2023-06-06 15:52:39 +08:00
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
setupCamera();
|
2023-06-06 15:52:39 +08:00
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
loadAssets();
|
|
|
|
|
generateBRDFLUT();
|
|
|
|
|
generateCubemaps();
|
2024-04-08 15:45:52 +08:00
|
|
|
|
//prepareUniformBuffers();
|
|
|
|
|
//setupDescriptors();
|
2024-04-07 17:29:35 +08:00
|
|
|
|
preparePipelines();
|
2024-04-08 11:10:30 +08:00
|
|
|
|
if (!PlumageRender::Setter::settings.headless)
|
2024-04-07 17:29:35 +08:00
|
|
|
|
{
|
2024-04-08 15:45:52 +08:00
|
|
|
|
gui = new UI(vulkanDevice, renderPass, queue, pipelineCache,PlumageRender::Setter::settings.sampleCount);
|
2024-04-07 17:29:35 +08:00
|
|
|
|
updateUIOverlay();
|
|
|
|
|
}
|
|
|
|
|
buildCommandBuffers();
|
2023-06-06 15:52:39 +08:00
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
prepared = true;
|
|
|
|
|
}
|
2023-06-06 15:52:39 +08:00
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
void PlumageRender::renderMain::setupCamera()
|
|
|
|
|
{
|
|
|
|
|
camera.type = Camera::CameraType::lookat;
|
2023-06-06 15:52:39 +08:00
|
|
|
|
|
2024-04-08 11:10:30 +08:00
|
|
|
|
camera.setPerspective(45.0f, (float)PlumageRender::Setter::settings.width / (float)PlumageRender::Setter::settings.height, 0.1f, 256.0f);
|
2024-04-07 17:29:35 +08:00
|
|
|
|
camera.rotationSpeed = 0.25f;
|
|
|
|
|
camera.movementSpeed = 0.1f;
|
|
|
|
|
camera.setPosition({ 0.0f, 0.0f, -1.0f });
|
|
|
|
|
camera.setRotation({ 0.0f, 0.0f, 0.0f });
|
|
|
|
|
}
|
2023-05-17 14:49:05 +08:00
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
void PlumageRender::renderMain::submitWork(VkCommandBuffer cmdBuffer, VkQueue queue)
|
2024-03-22 18:05:18 +08:00
|
|
|
|
{
|
|
|
|
|
VkSubmitInfo submitInfo = vks::initializers::submitInfo();
|
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &cmdBuffer;
|
2024-03-26 14:59:37 +08:00
|
|
|
|
VkFenceCreateInfo fenceInfo = vks::initializers::fenceCreateInfo();
|
|
|
|
|
VkFence fence;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateFence(device, &fenceInfo, nullptr, &fence));
|
2024-03-22 18:05:18 +08:00
|
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, fence));
|
|
|
|
|
VK_CHECK_RESULT(vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX));
|
2024-03-26 14:59:37 +08:00
|
|
|
|
vkDestroyFence(device, fence, nullptr);
|
2024-03-22 18:05:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
void PlumageRender::renderMain::render()
|
2023-05-17 14:49:05 +08:00
|
|
|
|
{
|
2023-06-06 15:52:39 +08:00
|
|
|
|
if (!prepared) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 11:10:30 +08:00
|
|
|
|
if (!PlumageRender::Setter::settings.headless)
|
2024-03-29 18:11:09 +08:00
|
|
|
|
{
|
|
|
|
|
updateUIOverlay();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-22 18:05:18 +08:00
|
|
|
|
//加入写到文件的函数
|
|
|
|
|
//swapChainImage = swapChain.images[frameIndex];
|
2024-03-26 14:59:37 +08:00
|
|
|
|
//outputImageSequeue(swapChainImage,filePath.imageSequenceFilePath);
|
|
|
|
|
|
2024-03-25 11:51:10 +08:00
|
|
|
|
|
2024-03-26 18:04:18 +08:00
|
|
|
|
outputImageSequence();
|
2024-03-29 18:11:09 +08:00
|
|
|
|
VK_CHECK_RESULT(vkWaitForFences(device, 1, &waitFences[frameIndex], VK_TRUE, UINT64_MAX));
|
|
|
|
|
|
2024-03-27 17:29:32 +08:00
|
|
|
|
imageSequenceToVideo();
|
2024-03-26 14:59:37 +08:00
|
|
|
|
VK_CHECK_RESULT(vkResetFences(device, 1, &waitFences[frameIndex]));
|
2023-06-06 15:52:39 +08:00
|
|
|
|
|
|
|
|
|
VkResult acquire = swapChain.acquireNextImage(presentCompleteSemaphores[frameIndex], ¤tBuffer);
|
|
|
|
|
if ((acquire == VK_ERROR_OUT_OF_DATE_KHR) || (acquire == VK_SUBOPTIMAL_KHR)) {
|
|
|
|
|
windowResize();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
VK_CHECK_RESULT(acquire);
|
2024-03-22 18:05:18 +08:00
|
|
|
|
|
2024-03-26 14:59:37 +08:00
|
|
|
|
|
2023-06-06 15:52:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 18:11:09 +08:00
|
|
|
|
|
2023-06-06 15:52:39 +08:00
|
|
|
|
// Update UBOs
|
|
|
|
|
updateUniformBuffers();
|
2024-04-07 17:29:35 +08:00
|
|
|
|
UniformBufferSet[currentUB] = uniformBuffers[currentBuffer];
|
2023-06-06 15:52:39 +08:00
|
|
|
|
memcpy(currentUB.scene.mapped, &shaderDataScene, sizeof(shaderDataScene));
|
|
|
|
|
memcpy(currentUB.params.mapped, &shaderData, sizeof(shaderData));
|
|
|
|
|
memcpy(currentUB.skybox.mapped, &shaderDataSkybox, sizeof(shaderDataSkybox));
|
|
|
|
|
|
|
|
|
|
const VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
|
VkSubmitInfo submitInfo{};
|
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
|
|
|
submitInfo.pWaitDstStageMask = &waitDstStageMask;
|
|
|
|
|
submitInfo.pWaitSemaphores = &presentCompleteSemaphores[frameIndex];
|
|
|
|
|
submitInfo.waitSemaphoreCount = 1;
|
|
|
|
|
submitInfo.pSignalSemaphores = &renderCompleteSemaphores[frameIndex];
|
|
|
|
|
submitInfo.signalSemaphoreCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &commandBuffers[currentBuffer];
|
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, waitFences[frameIndex]));
|
2024-03-26 14:59:37 +08:00
|
|
|
|
|
2024-03-22 18:05:18 +08:00
|
|
|
|
//显示队列
|
2024-03-29 18:11:09 +08:00
|
|
|
|
|
2023-06-06 15:52:39 +08:00
|
|
|
|
VkResult present = swapChain.queuePresent(queue, currentBuffer, renderCompleteSemaphores[frameIndex]);
|
|
|
|
|
if (!((present == VK_SUCCESS) || (present == VK_SUBOPTIMAL_KHR))) {
|
|
|
|
|
if (present == VK_ERROR_OUT_OF_DATE_KHR) {
|
|
|
|
|
windowResize();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
VK_CHECK_RESULT(present);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frameIndex += 1;
|
|
|
|
|
frameIndex %= renderAhead;
|
|
|
|
|
|
|
|
|
|
if (!paused) {
|
2024-04-08 11:10:30 +08:00
|
|
|
|
if (PlumageRender::Setter::settings.rotateModel) {
|
2023-06-06 15:52:39 +08:00
|
|
|
|
modelrot.y += frameTimer * 35.0f;
|
|
|
|
|
if (modelrot.y > 360.0f) {
|
|
|
|
|
modelrot.y -= 360.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ((animate) && (models.scene.animations.size() > 0)) {
|
|
|
|
|
animationTimer += frameTimer;
|
|
|
|
|
if (animationTimer > models.scene.animations[animationIndex].end) {
|
|
|
|
|
animationTimer -= models.scene.animations[animationIndex].end;
|
|
|
|
|
}
|
|
|
|
|
models.scene.updateAnimation(animationIndex, animationTimer);
|
|
|
|
|
}
|
|
|
|
|
updateShaderData();
|
2024-04-08 11:10:30 +08:00
|
|
|
|
if (PlumageRender::Setter::settings.rotateModel) {
|
2023-06-06 15:52:39 +08:00
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 14:49:05 +08:00
|
|
|
|
if (camera.updated) {
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
2024-03-22 18:05:18 +08:00
|
|
|
|
|
2023-05-17 14:49:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 17:29:35 +08:00
|
|
|
|
void PlumageRender::renderMain::fileDropped(std::string filename)
|
2023-06-07 16:16:53 +08:00
|
|
|
|
{
|
|
|
|
|
vkDeviceWaitIdle(device);
|
|
|
|
|
loadScene(filename);
|
|
|
|
|
setupDescriptors();
|
|
|
|
|
buildCommandBuffers();
|
2024-03-28 11:12:47 +08:00
|
|
|
|
|
2023-06-07 16:16:53 +08:00
|
|
|
|
}
|
2023-06-07 00:45:10 +08:00
|
|
|
|
|
2024-04-08 15:45:52 +08:00
|
|
|
|
|
2023-05-25 16:27:50 +08:00
|
|
|
|
|
2024-04-02 12:08:46 +08:00
|
|
|
|
|
2023-06-07 16:16:53 +08:00
|
|
|
|
|
2024-04-01 18:05:17 +08:00
|
|
|
|
// 完全重写,避免OS specific
|
2024-04-02 12:08:46 +08:00
|
|
|
|
/*
|
2024-04-01 18:05:17 +08:00
|
|
|
|
PlumageRender* plumageRender;
|
2024-04-02 12:08:46 +08:00
|
|
|
|
|
2023-06-06 17:18:45 +08:00
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
|
{
|
|
|
|
|
if (plumageRender != NULL)
|
|
|
|
|
{
|
|
|
|
|
plumageRender->handleMessages(hWnd, uMsg, wParam, lParam);
|
|
|
|
|
}
|
|
|
|
|
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
|
|
|
|
}
|
|
|
|
|
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
|
|
|
|
|
{
|
2024-04-01 13:45:00 +08:00
|
|
|
|
VulkanBackend::Setter setter;
|
|
|
|
|
for (int32_t i = 0; i < __argc; i++) {setter.args.push_back(__argv[i]); };
|
2023-06-06 17:18:45 +08:00
|
|
|
|
plumageRender = new PlumageRender();
|
2024-03-29 18:11:09 +08:00
|
|
|
|
|
2023-06-06 17:18:45 +08:00
|
|
|
|
plumageRender->initVulkan();
|
2024-04-08 11:10:30 +08:00
|
|
|
|
if (!plumageRender->PlumageRender::Setter::settings.headless)
|
2024-03-29 18:11:09 +08:00
|
|
|
|
{
|
|
|
|
|
plumageRender->setupWindow(hInstance, WndProc);
|
|
|
|
|
}
|
2023-06-06 17:18:45 +08:00
|
|
|
|
plumageRender->prepare();
|
|
|
|
|
plumageRender->renderLoop();
|
|
|
|
|
delete(plumageRender);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2024-04-02 12:08:46 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2024-04-08 15:45:52 +08:00
|
|
|
|
PlumageRender::renderMain plumageRender;
|
|
|
|
|
VulkanBackend::VulkanFoundation vkFoundation;
|
|
|
|
|
|
|
|
|
|
vkFoundation.initVulkan();
|
2024-04-08 11:10:30 +08:00
|
|
|
|
if (!PlumageRender::Setter::settings.headless)
|
2024-04-02 12:08:46 +08:00
|
|
|
|
{
|
2024-04-08 15:45:52 +08:00
|
|
|
|
plumageRender.initWindow(PlumageRender::Setter::settings.width,PlumageRender::Setter::settings.height);
|
2024-04-07 17:29:35 +08:00
|
|
|
|
|
2024-04-02 12:08:46 +08:00
|
|
|
|
}
|
2024-04-08 15:45:52 +08:00
|
|
|
|
|
2024-04-02 12:08:46 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|