vulkan draw cube

This commit is contained in:
ouczbs 2024-07-12 11:31:33 +08:00
parent 71df2efd4c
commit 6975d71a19
13 changed files with 41 additions and 30 deletions

View File

@ -1,8 +1,8 @@
/engine/assets/shader/simple.vert:
- 020486aa-8987-4bd1-9de9-eeb7ab82c437
/engine/assets/asset/mesh1.asset: /engine/assets/asset/mesh1.asset:
- 0a2ef3e8-db70-4d3f-94b1-09f9f92519cd - 0a2ef3e8-db70-4d3f-94b1-09f9f92519cd
- 7a01231c-c35d-4b74-b20f-4ca6823995d9 - 7a01231c-c35d-4b74-b20f-4ca6823995d9
/engine/assets/shader/simple.vert:
- 020486aa-8987-4bd1-9de9-eeb7ab82c437
/engine/assets/models/cube.obj: /engine/assets/models/cube.obj:
- 3f6a5afc-2f21-47c4-86ba-9ddb9852d8f4 - 3f6a5afc-2f21-47c4-86ba-9ddb9852d8f4
- b91ca918-6b3c-4647-8773-d93d2807cb98 - b91ca918-6b3c-4647-8773-d93d2807cb98

View File

@ -1,8 +1,7 @@
#version 450 #version 450
#extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_separate_shader_objects : enable
layout (location = 0) in vec4 vColor;
layout(location = 0) out vec4 outColor; layout (location = 0) out vec4 oColor;
void main() { void main() {
outColor = vec4(0.0, 1.0, 0.0, 1.0); oColor = vColor;
} }

View File

@ -1,7 +1,11 @@
#version 450 #version 450
layout (location = 0) in vec2 aPos; layout (location = 0) in vec3 iPos;
layout (location = 1) in vec2 iTex;
layout (location = 0) out vec4 vColor;
void main() void main()
{ {
gl_Position = vec4(aPos, 0.0, 1.0); gl_Position = vec4(iPos.x, iPos.y, iTex.x, 1.0);
vColor = vec4(gl_Position.x, gl_Position.y, gl_Position.z, 1.0);
} }

View File

@ -12,7 +12,7 @@ namespace engineapi {
const char* name = "zengine"; const char* name = "zengine";
SystemList.push_back(new FileManager()); SystemList.push_back(new FileManager());
SystemList.push_back(new ResourceManager()); SystemList.push_back(new ResourceManager());
SystemList.push_back(RenderAPI::API(GraphicsAPI::OpenGL)); SystemList.push_back(RenderAPI::API(GraphicsAPI::Vulkan));
SystemList.push_back(Window::MakeInstance(3, 640, 720, name)); SystemList.push_back(Window::MakeInstance(3, 640, 720, name));
SystemList.push_back(new SceneManager()); SystemList.push_back(new SceneManager());
} }

View File

@ -82,8 +82,7 @@ namespace openglapi {
glVertexMeta v_meta = field_meta.CastTo<glVertexMeta>(); glVertexMeta v_meta = field_meta.CastTo<glVertexMeta>();
GLvoid* offset = reinterpret_cast<GLvoid*>(field.GetOffset()); GLvoid* offset = reinterpret_cast<GLvoid*>(field.GetOffset());
glVertexAttribPointer(count, v_meta.size, v_meta.type, v_meta.normalized, size, offset); glVertexAttribPointer(count, v_meta.size, v_meta.type, v_meta.normalized, size, offset);
glEnableVertexAttribArray(count); glEnableVertexAttribArray(count++);
count++;
break; break;
} }
} }

View File

@ -7,6 +7,7 @@ namespace engineapi {
REFL_FRIEND(Material) REFL_FRIEND(Material)
UPROPERTY() UPROPERTY()
RscHandle<Shader> mShader; RscHandle<Shader> mShader;
friend class Scene; friend class Scene;
public: public:
Material(); Material();

View File

@ -15,12 +15,12 @@ namespace engineapi {
UPROPERTY_gl({}, glVertexMeta{ 3, GL_FLOAT, GL_FALSE }) UPROPERTY_gl({}, glVertexMeta{ 3, GL_FLOAT, GL_FALSE })
UPROPERTY_vk({}, uint32_t{ VK_FORMAT_R32G32B32_SFLOAT }) UPROPERTY_vk({}, uint32_t{ VK_FORMAT_R32G32B32_SFLOAT })
Vector3 Position = {}; Vector3 Position = {};
UPROPERTY_gl({}, glVertexMeta{ 3, GL_FLOAT, GL_FALSE })
UPROPERTY_vk()
Vector3 Normal = {};
UPROPERTY_gl({}, glVertexMeta{ 2, GL_FLOAT, GL_FALSE }) UPROPERTY_gl({}, glVertexMeta{ 2, GL_FLOAT, GL_FALSE })
UPROPERTY_vk() UPROPERTY_vk()
Vector2 TexCoords = {}; Vector2 TexCoords = {};
UPROPERTY_gl({}, glVertexMeta{ 3, GL_FLOAT, GL_FALSE })
UPROPERTY_vk()
Vector3 Normal = {};
}; };
struct BoneVertex : public Vertex struct BoneVertex : public Vertex
{ {

View File

@ -1,6 +1,7 @@
#include "glsl_to_spirv.h" #include "glsl_to_spirv.h"
#include <shaderc/shaderc.hpp> #include <shaderc/shaderc.hpp>
#include "meta/hash.h" #include "meta/hash.h"
#include "zlog.h"
using meta::string_hash; using meta::string_hash;
namespace engineapi namespace engineapi
{ {
@ -13,8 +14,12 @@ namespace engineapi
options.SetOptimizationLevel(shaderc_optimization_level_performance); options.SetOptimizationLevel(shaderc_optimization_level_performance);
options.SetTargetEnvironment(shaderc_target_env::shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_3); options.SetTargetEnvironment(shaderc_target_env::shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_3);
auto result = compiler.CompileGlslToSpv(glsl, kind, code_id.data(), options); auto result = compiler.CompileGlslToSpv(glsl, kind, code_id.data(), options);
if (result.GetCompilationStatus() != shaderc_compilation_status::shaderc_compilation_status_success) if (result.GetCompilationStatus() != shaderc_compilation_status::shaderc_compilation_status_success) {
auto err_m = result.GetErrorMessage();
auto err_msg = err_m.c_str();
zlog::error("load spirv failed!!! {}", err_msg);
return spirv_out; return spirv_out;
}
spirv_out = vector<uint32_t>{ result.cbegin(),result.cend() }; spirv_out = vector<uint32_t>{ result.cbegin(),result.cend() };
} }
return spirv_out; return spirv_out;

View File

@ -10,7 +10,7 @@ namespace vulkanapi {
,mWork(name, 64) ,mWork(name, 64)
,mImmediateExeCmd(mCommandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY) ,mImmediateExeCmd(mCommandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY)
{ {
mImmediateExeCmd.Fence() = mCommandPool.AllocateFence();
} }
void CommandWorker::Invoke(voidFn fn) void CommandWorker::Invoke(voidFn fn)
{ {

View File

@ -9,9 +9,10 @@ namespace vulkanapi {
public: public:
static RenderVulkanAPI* API; static RenderVulkanAPI* API;
RenderPass* Pass{ nullptr }; RenderPass* Pass{ nullptr };
uint32_t frame; uint32_t frame{0};
uint32_t presentFrame{0};
VkFence surfaceFence;
VkSemaphore surfaceSemaphore; VkSemaphore surfaceSemaphore;
VkSemaphore renderSemaphore;
public: public:
VulkanContext(RenderVulkanAPI* _API){ VulkanContext(RenderVulkanAPI* _API){
API = _API; API = _API;

View File

@ -77,7 +77,7 @@ namespace vulkanapi {
meshBuffer.vertexCount = Vertices.size(); meshBuffer.vertexCount = Vertices.size();
// ----------------------------------------------- Vertex Buffer ----------------------------------------------- // ----------------------------------------------- Vertex Buffer -----------------------------------------------
VkDeviceSize vertexBufferSize = Vertices.capicty(); VkDeviceSize vertexBufferSize = Vertices.data_size();
VmaAllocationCreateInfo vertexVmaStagingInfo = {}; VmaAllocationCreateInfo vertexVmaStagingInfo = {};
VmaAllocation vertexVmaStagingAlloc; VmaAllocation vertexVmaStagingAlloc;
VkBuffer vertexStagingBuffer = Buffer::CreateStageBuffer(vertexVmaStagingInfo, vertexVmaStagingAlloc, vertexBufferSize); VkBuffer vertexStagingBuffer = Buffer::CreateStageBuffer(vertexVmaStagingInfo, vertexVmaStagingAlloc, vertexBufferSize);
@ -148,7 +148,7 @@ namespace vulkanapi {
vkCmdBindIndexBuffer(ptr, vulkanVAO.indexBuffer, 0, VK_INDEX_TYPE_UINT32); vkCmdBindIndexBuffer(ptr, vulkanVAO.indexBuffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdBindPipeline(ptr, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline); vkCmdBindPipeline(ptr, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline);
vkCmdBindDescriptorSets(ptr, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipelineLayout, 0, 1, &pipeline.descriptorSet, 0, VK_NULL_HANDLE); vkCmdBindDescriptorSets(ptr, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipelineLayout, 0, 1, &pipeline.descriptorSet, 0, VK_NULL_HANDLE);
vkCmdDrawIndexed(ptr, 3, 1, 0, 0, 0); vkCmdDrawIndexed(ptr, vulkanVAO.indexCount, 1, 0, 0, 0);
context.Pass->EndPass(cmd); context.Pass->EndPass(cmd);
}); });
Backend::RenderWorker->Flush(); Backend::RenderWorker->Flush();

View File

@ -44,18 +44,19 @@ namespace vulkanapi {
args.Sharing = VK_SHARING_MODE_EXCLUSIVE; args.Sharing = VK_SHARING_MODE_EXCLUSIVE;
for (int i = 0; i < Creator.frames;i++) { for (int i = 0; i < Creator.frames;i++) {
mSurfaces.push_back(new Image(Creator.device,"swapchain" + to_string(i), swapchain_images[i], args)); mSurfaces.push_back(new Image(Creator.device,"swapchain" + to_string(i), swapchain_images[i], args));
mSemaphores.push_back(Creator.device.CreateSemaphore());
mFences.push_back(Creator.device.CreateFence());
} }
mFrames = Creator.frames; mFrames = Creator.frames;
mSurfaceSemaphore = Creator.device.CreateSemaphore();
mRenderSemaphore = Creator.device.CreateSemaphore();
} }
void Swapchain::Aquire(VulkanContext& ctx) void Swapchain::Aquire(VulkanContext& ctx)
{ {
ctx.surfaceSemaphore = mSurfaceSemaphore; ctx.surfaceFence = mFences[ctx.frame];
vkAcquireNextImageKHR(mDevice.Ptr(), mPtr, UINT64_MAX, ctx.surfaceSemaphore, VK_NULL_HANDLE, &ctx.frame); ctx.surfaceSemaphore = mSemaphores[ctx.frame];
if (ctx.frame >= mFrames) { //if (vkWaitForFences(mDevice.Ptr(), 1, &ctx.surfaceFence, VK_TRUE, UINT64_MAX) != VK_SUCCESS)
ctx.frame = 0; //throw std::runtime_error("Failed to wait for fence!");
} vkAcquireNextImageKHR(mDevice.Ptr(), mPtr, UINT64_MAX, ctx.surfaceSemaphore, VK_NULL_HANDLE, &ctx.presentFrame);
vkResetFences(mDevice.Ptr(), 1, &ctx.surfaceFence);
} }
void Swapchain::Present(VulkanContext& ctx) void Swapchain::Present(VulkanContext& ctx)
{ {
@ -67,8 +68,9 @@ namespace vulkanapi {
presentInfo.waitSemaphoreCount = 1; presentInfo.waitSemaphoreCount = 1;
presentInfo.pSwapchains = swapChains; presentInfo.pSwapchains = swapChains;
presentInfo.swapchainCount = 1; presentInfo.swapchainCount = 1;
presentInfo.pImageIndices = &ctx.frame; presentInfo.pImageIndices = &ctx.presentFrame;
presentInfo.pResults = VK_NULL_HANDLE; presentInfo.pResults = VK_NULL_HANDLE;
Backend::RenderWorker->Present(presentInfo); Backend::RenderWorker->Present(presentInfo);
ctx.frame = (ctx.frame + 1) % mFrames;
} }
} }

View File

@ -13,8 +13,8 @@ namespace vulkanapi {
Device& mDevice; Device& mDevice;
int mFrames; int mFrames;
vector<Image*> mSurfaces; vector<Image*> mSurfaces;
VkSemaphore mSurfaceSemaphore; vector<VkFence> mFences;
VkSemaphore mRenderSemaphore; vector<VkSemaphore> mSemaphores;
public: public:
Swapchain(SwapchainCreator& Creator); Swapchain(SwapchainCreator& Creator);
void Aquire(VulkanContext& ctx); void Aquire(VulkanContext& ctx);