This commit is contained in:
ouczbs 2024-01-26 17:32:46 +08:00
parent 5cd703df01
commit 3e95395cb2
21 changed files with 266 additions and 29 deletions

View File

@ -0,0 +1,24 @@
/*
* A coroutine framework aimed at high-concurrency io with reasonable latency,
* based on liburingcxx.
*
* Copyright 2022 Zifeng Deng
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <thread>
namespace co_context {
} // namespace co_context

View File

@ -0,0 +1,8 @@
#pragma once
#include <thread>
namespace co_context {
class [[nodiscard]] io_context final {
};
} // namespace co_context

5
engine/3rdparty/co_context/xmake.lua vendored Normal file
View File

@ -0,0 +1,5 @@
target("co_context")
set_kind("static")
add_includedirs("include")
add_files("src/**.cpp")
add_headerfiles("include/**.h")

View File

@ -1,22 +1,46 @@
#include "forwardpass.h"
#include "engine/vulkanapi/renderpass/renderpass.h"
#include "engine/vulkanapi/image/image.h"
namespace vulkanapi {
ForwardPass::ForwardPass(VkDevice device)
ForwardPass::ForwardPass(VkDevice device, GeometryBuffer& gBuffer)
:mPass(nullptr)
{
VkAttachmentDescription depthAttachment{};
//depthAttachment.format = findDepthFormat();
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkAttachmentDescription positionAttachment{};
positionAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
positionAttachment.format = gBuffer.positions[0]->Format();
positionAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
positionAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
positionAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
positionAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
positionAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
positionAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
std::vector<VkAttachmentDescription> attachments = { depthAttachment };
std::vector<VkSubpassDescription> subpasses;
VkAttachmentDescription normalAttachment{};
normalAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
normalAttachment.format = gBuffer.normals[0]->Format();
normalAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
normalAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
normalAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
normalAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
normalAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
normalAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
VkAttachmentReference colorAttachmentRef{};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
std::vector<VkAttachmentDescription> attachments = {positionAttachment , normalAttachment};
std::vector<VkSubpassDescription> subpasses = { subpass };
std::vector<VkSubpassDependency> dependencies;
mPass = new RenderPass(device,"forward",attachments, subpasses, dependencies);
}
void ForwardPass::Record()
{
}
}

View File

@ -1,10 +1,15 @@
#include "engine/vulkanapi/vulkan.h"
#include "gbuffer.h"
namespace vulkanapi {
class RenderPass;
class MaterialCache;
class ForwardPass {
protected:
RenderPass* mPass;
MaterialCache* mMaterials;
public:
ForwardPass(VkDevice device);
ForwardPass(VkDevice device, GeometryBuffer& gBuffer);
void Record();
};
}

View File

@ -0,0 +1,19 @@
#include "gbuffer.h"
#include "engine/vulkanapi/image/image.h"
namespace vulkanapi {
GeometryBuffer::GeometryBuffer(VkDevice device,int frames, int width, int height)
:width(width),
height(height)
{
VkFormat positionFmt = VK_FORMAT_R32G32B32A32_SFLOAT;
VkFormat normalFmt = VK_FORMAT_R8G8B8A8_UNORM;
VkFormat diffuseFmt = VK_FORMAT_R8G8B8A8_UNORM;
int usage = 1;
for (int i = 0; i < frames; i++) {
positions.push_back(new Image(device, "position", width , height, diffuseFmt, usage));
normals.push_back(new Image(device, "normal", width, height, diffuseFmt, usage));
diffuses.push_back(new Image(device, "diffuse", width, height, diffuseFmt, usage));
}
}
}

View File

@ -0,0 +1,15 @@
#include "engine/vulkanapi/vulkan.h"
#include <vector>
namespace vulkanapi {
class Image;
struct GeometryBuffer {
public:
std::vector<Image*> positions;
std::vector<Image*> normals;
std::vector<Image*> diffuses;
int width;
int height;
public:
GeometryBuffer(VkDevice device,int frames, int width, int height);
};
}

View File

@ -0,0 +1,23 @@
#include "cache.h"
namespace vulkanapi {
CachePool::CachePool()
{
}
CacheValue* CachePool::Fetch(uint32_t id)
{
CacheValue* v = get(id);
if (v != nullptr)
return v;
return nullptr;
}
CacheValue* CachePool::get(uint32_t id)
{
auto res = mData.find(id);
if(res == mData.end())
return nullptr;
return res->second;
}
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <string>
#include <map>
#include "engine/vulkanapi/vulkan.h"
namespace vulkanapi {
class CacheValue {
protected:
public:
};
class CachePool {
protected:
std::map<uint32_t, CacheValue*> mData;
bool mAsync = false;
int mMaxAge = 100;
protected:
CacheValue* get(uint32_t id);
public:
CachePool();
CacheValue* Fetch(uint32_t id);
};
};

View File

@ -0,0 +1,4 @@
#include "thread_worker.h"
namespace vulkanapi {
}

View File

@ -1,10 +1,15 @@
#pragma once
#include <string>
#include <vector>
#include <thread>
#include "pool.h"
#include "engine/vulkanapi/vulkan.h"
namespace vulkanapi {
class Device;
typedef void(*CommandWorker)();
class CommandThreadWorker {
protected:
CommandWorker* mWorkChanels;
public:
CommandThreadWorker(const char* name, int buffer) {};
};
};

View File

@ -1,7 +1,27 @@
#include "image.h"
namespace vulkanapi {
Image::Image(VkImage img)
:mPtr(img)
Image::Image(VkDevice device, const char* name, int width, int height, VkFormat format, VkImageUsageFlags usage)
:Image(device, Args{
1,
"",
width,height,
1,1,1,
format,usage,
1,1,1,1})
{
}
Image::Image(VkDevice device, const Args& args)
: mArgs(args)
, mDevice(device)
, mPtr(nullptr)
{
}
Image::Image(VkDevice device, VkImage ptr, const Args& args)
: mArgs(args)
, mDevice(device)
, mPtr(ptr)
{
}

View File

@ -1,12 +1,36 @@
#pragma once
#include "engine/vulkanapi/vulkan.h"
namespace vulkanapi {
class Image {
protected:
VkImage mPtr;
public:
Image(VkImage img);
struct Args {
int Type;
const char* Key;
int Width;
int Height;
int Depth;
int Layers;
int Levels;
VkFormat Format;
VkImageUsageFlags Usage;
int Tiling;
int Sharing;
int Layout;
int Memory;
};
protected:
Args mArgs;
VkImage mPtr;
VkDevice mDevice;
public:
Image(VkDevice device, const char* name, int width, int height, VkFormat format, VkImageUsageFlags usage);
Image(VkDevice device, const Args& args);
Image(VkDevice device, VkImage ptr, const Args& args);
VkFormat Format() {
return mArgs.Format;
};
};
};

View File

@ -0,0 +1,12 @@
#include "material.h"
#include "engine/vulkanapi/descriptor/pool.h"
namespace vulkanapi {
Material::Material(VkDevice device, const Args& args) {
}
void Material::Instantiate(DescriptorPool* pool)
{
}
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "engine/vulkanapi/vulkan.h"
namespace vulkanapi {
class DescriptorPool;
class Material {
public:
struct Args {
};
protected:
public:
Material(VkDevice device, const Args& args);
void Instantiate(DescriptorPool* pool);
};
};

View File

@ -10,11 +10,11 @@ namespace vulkanapi {
VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
nullptr,
0,
attachments.size(),
(uint32_t)attachments.size(),
attachments.data(),
subpasses.size(),
(uint32_t)subpasses.size(),
subpasses.data(),
dependencies.size(),
(uint32_t)dependencies.size(),
dependencies.data()
};
vkCreateRenderPass(device, &create_info, nullptr, &mPtr);

View File

@ -23,16 +23,22 @@ namespace vulkanapi {
//调用设备接口,创建交换链
VkSwapchainKHR old_swapchain = VK_NULL_HANDLE;
VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
CreateSwapchain(device->Ptr(), presentation_surface, frames, { image_format, image_color_space }, image_size
, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
VK_PRESENT_MODE_FIFO_KHR, old_swapchain, mPtr);
, imageUsage, VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,VK_PRESENT_MODE_FIFO_KHR, old_swapchain, mPtr);
std::vector<VkImage> swapchain_images;
GetHandlesOfSwapchainImages(device->Ptr(), mPtr, swapchain_images);
uint32_t image_count = swapchain_images.size();
for (auto img : swapchain_images) {
mImages.push_back(new Image(img));
mImages.push_back(new Image(device->Ptr(), img, Image::Args{
1,
"",
width, height,
1, 1, 1,
image_format, imageUsage,
1, 1, 1, 1}));
}
}
}

View File

@ -1,7 +1,8 @@
#include <iostream>
#include "engine/vulkanapi/vulkan/backend.h"
#include "engine/vulkanapi/vulkan/window.h"
#include "engine/vulkanapi/renderpass/renderpass.h"
#include "engine/vulkanapi/device/device.h"
#include "engine/render/pass/forwardpass.h"
using namespace std;
int main(int argc, char** argv)
@ -9,7 +10,8 @@ int main(int argc, char** argv)
const char* name = "hello";
auto app = vulkanapi::Backend(name);
auto wnd = vulkanapi::Window(&app, 3, 640, 720, name);
//auto pass = vulkanapi::RenderPass();
auto gBuffer = vulkanapi::GeometryBuffer(app.GetDevice()->Ptr(), 3, 640, 720);
auto pass = vulkanapi::ForwardPass(app.GetDevice()->Ptr(), gBuffer);
cout << name << endl;
return 0;
}

View File

@ -3,7 +3,7 @@ includes("test/**xmake.lua")
target("zengine")
set_kind("binary")
add_packages("vulkansdk","glfw","glm","tinyobjloader")
add_deps("co_context")
add_includedirs("src")
add_files("src/*.cpp", "src/**.cpp")
add_headerfiles("src/**.h", "src/**.inl")