From a8ac1dbe1a35e02c66bcba6fa51f945df42ec27a Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Fri, 4 Aug 2023 21:33:14 +0100 Subject: [PATCH] support shader draw calls without vertex data --- include/components/renderable.h | 2 ++ include/gfx_device.h | 4 ++++ src/application.cpp | 6 ------ src/gfx_device_vulkan.cpp | 7 +++++++ src/systems/render.cpp | 26 ++++++++++++++++++-------- test/src/game.cpp | 21 ++++----------------- 6 files changed, 35 insertions(+), 31 deletions(-) diff --git a/include/components/renderable.h b/include/components/renderable.h index 455a9de..937f21e 100644 --- a/include/components/renderable.h +++ b/include/components/renderable.h @@ -12,6 +12,8 @@ struct RenderableComponent { std::shared_ptr mesh; std::shared_ptr material; bool shown = true; + uint32_t index_count_override = + 0; // for shaders that don't use vertex/index buffers, 0 means ignored }; } // namespace engine diff --git a/include/gfx_device.h b/include/gfx_device.h index 2ad99fc..cd36de6 100644 --- a/include/gfx_device.h +++ b/include/gfx_device.h @@ -45,6 +45,10 @@ class GFXDevice { uint32_t instance_count, uint32_t first_index, int32_t vertex_offset, uint32_t first_instance); + void CmdDraw(gfx::DrawBuffer* drawBuffer, uint32_t vertex_count, + uint32_t instance_count, uint32_t first_vertex, + uint32_t first_instance); + void CmdPushConstants(gfx::DrawBuffer* draw_buffer, const gfx::Pipeline* pipeline, uint32_t offset, uint32_t size, const void* data); diff --git a/src/application.cpp b/src/application.cpp index c3d8347..b0dbeb0 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -225,12 +225,6 @@ void Application::GameLoop() { /* logic */ scene_manager_->UpdateActiveScene(window_->dt()); - if (window_->GetKeyPress(inputs::Key::K_F)) [[unlikely]] { - window_->InfoBox("fps", std::to_string(window_->GetFPS()) + " fps " + - std::to_string(window_->dt() * 1000.0f) + - " ms"); - } - uint64_t now = window_->GetNanos(); if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] { lastTick = now; diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 2db08ce..7ebceae 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -844,6 +844,13 @@ void GFXDevice::CmdDrawIndexed(gfx::DrawBuffer* drawBuffer, uint32_t indexCount, firstIndex, vertexOffset, firstInstance); } +void GFXDevice::CmdDraw(gfx::DrawBuffer* drawBuffer, uint32_t vertex_count, + uint32_t instance_count, uint32_t first_vertex, + uint32_t first_instance) { + assert(drawBuffer != nullptr); + vkCmdDraw(drawBuffer->frameData.drawBuf, vertex_count, instance_count, first_vertex, first_instance); +} + void GFXDevice::CmdPushConstants(gfx::DrawBuffer* drawBuffer, const gfx::Pipeline* pipeline, uint32_t offset, uint32_t size, const void* data) { diff --git a/src/systems/render.cpp b/src/systems/render.cpp index fd63153..c054934 100644 --- a/src/systems/render.cpp +++ b/src/systems/render.cpp @@ -79,7 +79,6 @@ void RenderSystem::OnUpdate(float ts) { assert(r != nullptr); assert(r->material != nullptr); assert(r->material->texture_ != nullptr); - assert(r->mesh != nullptr); if (r->shown == false) continue; auto t = scene_->GetComponent(entity); @@ -90,10 +89,17 @@ void RenderSystem::OnUpdate(float ts) { const gfx::Pipeline* pipeline = r->material->GetShader()->GetPipeline(); DrawCallData data{}; - data.vb = r->mesh->GetVB(); - data.ib = r->mesh->GetIB(); + if (r->mesh) { + data.vb = r->mesh->GetVB(); + data.ib = r->mesh->GetIB(); + } data.material_set = r->material->texture_->GetDescriptorSet(); - data.index_count = r->mesh->GetCount(); + if (r->index_count_override == 0) { + assert(r->mesh != nullptr); + data.index_count = r->mesh->GetCount(); + } else { + data.index_count = r->index_count_override; + } data.push_constants.model = t->world_matrix; unsorted_draw_calls.emplace_back( @@ -134,10 +140,14 @@ void RenderSystem::OnUpdate(float ts) { draw_call.material_set, 2); gfx_->CmdPushConstants(render_data.draw_buffer, pipeline, 0, sizeof(PushConstants), &draw_call.push_constants); - gfx_->CmdBindVertexBuffer(render_data.draw_buffer, 0, draw_call.vb); - gfx_->CmdBindIndexBuffer(render_data.draw_buffer, draw_call.ib); - gfx_->CmdDrawIndexed(render_data.draw_buffer, draw_call.index_count, 1, 0, - 0, 0); + if (draw_call.ib) { + gfx_->CmdBindVertexBuffer(render_data.draw_buffer, 0, draw_call.vb); + gfx_->CmdBindIndexBuffer(render_data.draw_buffer, draw_call.ib); + gfx_->CmdDrawIndexed(render_data.draw_buffer, draw_call.index_count, 1, 0, + 0, 0); + } else { + gfx_->CmdDraw(render_data.draw_buffer, draw_call.index_count, 1, 0, 0); + } } /* draw */ diff --git a/test/src/game.cpp b/test/src/game.cpp index 918b610..1aba4ea 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -154,7 +154,8 @@ void PlayGame(GameSettings settings) { std::make_unique( &app.render_data_, bitmap->data(), width, height, engine::resources::Texture::Filtering::kAnisotropic); - textbox_renderable->mesh = GenSphereMesh(app.gfxdev(), 1.0f, 5); + textbox_renderable->mesh = nullptr; + textbox_renderable->index_count_override = 6; auto textTransform = my_scene->GetComponent(textbox); textTransform->scale.y = @@ -169,26 +170,12 @@ void PlayGame(GameSettings settings) { LOG_INFO("Textbox custom component initialised!"); }; - textboxComponent->onUpdate = [&](float ts) { - (void)ts; + textboxComponent->onUpdate = [](float ts) { static float time_elapsed; time_elapsed += ts; if (time_elapsed >= 1.0f) { time_elapsed = 0.0f; - - LOG_INFO("COMPONENT UPDATE"); - - // LOG_INFO("Creating new bitmap..."); - // auto fpsBitmap = - // app.GetResource("builtin.mono") - // ->GetTextBitmap(std::to_string(ts), 768.0f, fpsWidth, - // fpsHeight); - // LOG_INFO("Bitmap created! Loading into new texture..."); - // textbox_renderable->material->texture_ = - // std::make_unique( - // &app.render_data_, fpsBitmap->data(), fpsWidth, fpsHeight, - // engine::resources::Texture::Filtering::kBilinear); - // LOG_INFO("Texture created!"); + LOG_INFO("COMPONENT UPDATE"); } }; }