Compare commits

..

No commits in common. "874fec434afa2d18c2d3454eef9a723d2d154196" and "f144bfdb2dfbde409b50f5b204e07d7800754a6a" have entirely different histories.

8 changed files with 48 additions and 79 deletions

View File

@ -70,9 +70,12 @@ void Engine::Run() {
Initialize();
DeltaTimer timer;
float accumulator = 0.0f;
float accumulator = 0.0;
float frame_frac = 0.0f;
for (;;) {
TaskRunner::GetThreadLocalTaskRunner()->RunTasks();
platform_->Update();
if (platform_->should_exit())
return;
@ -80,6 +83,8 @@ void Engine::Run() {
if (!renderer_->IsInitialzed())
continue;
Draw(frame_frac);
// Accumulate time.
accumulator += timer.Delta();
@ -89,11 +94,8 @@ void Engine::Run() {
accumulator -= time_step_;
};
TaskRunner::GetThreadLocalTaskRunner()->RunTasks();
// Calculate frame fraction from remainder of the frame time.
float frame_frac = accumulator / time_step_;
Draw(frame_frac);
frame_frac = accumulator / time_step_;
}
}
@ -143,8 +145,6 @@ void Engine::Update(float delta_time) {
if (stats_visible_)
ShowStats();
imgui_backend_.Render();
}
void Engine::Draw(float frame_frac) {

View File

@ -65,6 +65,8 @@ void ImguiBackend::Initialize() {
Engine::Get().RefreshImage("imgui_atlas");
ImGui::GetIO().Fonts->SetTexID(
(ImTextureID)(intptr_t)Engine::Get().AcquireTexture("imgui_atlas"));
NewFrame(0);
}
void ImguiBackend::Shutdown() {
@ -115,9 +117,9 @@ std::unique_ptr<InputEvent> ImguiBackend::OnInputEvent(
}
void ImguiBackend::NewFrame(float delta_time) {
for (auto& id : geometries_)
renderer_->DestroyGeometry(id);
geometries_.clear();
if (is_new_frame_)
ImGui::EndFrame();
is_new_frame_ = true;
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)renderer_->GetScreenWidth(),
@ -126,28 +128,13 @@ void ImguiBackend::NewFrame(float delta_time) {
ImGui::NewFrame();
}
void ImguiBackend::Render() {
void ImguiBackend::Draw() {
is_new_frame_ = false;
ImGui::Render();
ImDrawData* draw_data = ImGui::GetDrawData();
geometries_.resize(draw_data->CmdListsCount);
for (int n = 0; n < draw_data->CmdListsCount; n++) {
const ImDrawList* cmd_list = draw_data->CmdLists[n];
auto mesh = std::make_unique<Mesh>();
mesh->Create(kPrimitive_Triangles, vertex_description,
cmd_list->VtxBuffer.Size, cmd_list->VtxBuffer.Data,
kDataType_UShort, cmd_list->IdxBuffer.Size,
cmd_list->IdxBuffer.Data);
geometries_[n] = renderer_->CreateGeometry(std::move(mesh));
}
}
void ImguiBackend::Draw() {
ImDrawData* draw_data = ImGui::GetDrawData();
if (!draw_data || draw_data->CmdListsCount <= 0)
if (draw_data->CmdListsCount <= 0)
return;
renderer_->SetViewport(0, 0, draw_data->DisplaySize.x, draw_data->DisplaySize.y);
base::Matrix4f ortho_projection;
ortho_projection.CreateOrthoProjection(
draw_data->DisplayPos.x,
@ -160,20 +147,28 @@ void ImguiBackend::Draw() {
for (int n = 0; n < draw_data->CmdListsCount; n++) {
const ImDrawList* cmd_list = draw_data->CmdLists[n];
auto mesh = std::make_unique<Mesh>();
mesh->Create(kPrimitive_Triangles, vertex_description,
cmd_list->VtxBuffer.Size, cmd_list->VtxBuffer.Data,
kDataType_UShort, cmd_list->IdxBuffer.Size,
cmd_list->IdxBuffer.Data);
auto geometry_id = renderer_->CreateGeometry(std::move(mesh));
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
if (pcmd->ClipRect.z <= pcmd->ClipRect.x ||
pcmd->ClipRect.w <= pcmd->ClipRect.y)
continue;
reinterpret_cast<Texture*>(pcmd->GetTexID())->Activate(0);
renderer_->SetScissor(int(pcmd->ClipRect.x), int(pcmd->ClipRect.y),
int(pcmd->ClipRect.z - pcmd->ClipRect.x),
int(pcmd->ClipRect.w - pcmd->ClipRect.y));
renderer_->Draw(geometries_[n], pcmd->ElemCount, pcmd->IdxOffset);
renderer_->Draw(geometry_id, pcmd->ElemCount, pcmd->IdxOffset);
}
renderer_->DestroyGeometry(geometry_id);
}
renderer_->ResetScissor();
renderer_->ResetViewport();
}
} // namespace eng

View File

@ -2,7 +2,6 @@
#define ENGINE_IMGUI_BACKEND_H
#include <memory>
#include <vector>
namespace eng {
@ -23,13 +22,12 @@ class ImguiBackend {
std::unique_ptr<InputEvent> OnInputEvent(std::unique_ptr<InputEvent> event);
void NewFrame(float delta_time);
void Render();
void Draw();
private:
std::vector<uint64_t> geometries_;
std::unique_ptr<Shader> shader_;
Renderer* renderer_ = nullptr;
bool is_new_frame_ = false;
};
} // namespace eng

View File

@ -47,6 +47,7 @@ RendererOpenGL::~RendererOpenGL() {
void RendererOpenGL::OnWindowResized(int width, int height) {
screen_width_ = width;
screen_height_ = height;
glViewport(0, 0, screen_width_, screen_height_);
}
int RendererOpenGL::GetScreenWidth() const {
@ -57,14 +58,6 @@ int RendererOpenGL::GetScreenHeight() const {
return screen_height_;
}
void RendererOpenGL::SetViewport(int x, int y, int width, int height) {
glViewport(0, 0, width, height);
}
void RendererOpenGL::ResetViewport() {
glViewport(0, 0, screen_width_, screen_height_);
}
void RendererOpenGL::SetScissor(int x, int y, int width, int height) {
glScissor(x, screen_height_ - y - height, width, height);
glEnable(GL_SCISSOR_TEST);
@ -443,7 +436,6 @@ void RendererOpenGL::SetUniform(uint64_t resource_id,
}
void RendererOpenGL::PrepareForDrawing() {
glViewport(0, 0, screen_width_, screen_height_);
glDisable(GL_SCISSOR_TEST);
}

View File

@ -32,9 +32,6 @@ class RendererOpenGL final : public Renderer {
int GetScreenWidth() const final;
int GetScreenHeight() const final;
void SetViewport(int x, int y, int width, int height) final;
void ResetViewport() final;
void SetScissor(int x, int y, int width, int height) final;
void ResetScissor() final;

View File

@ -39,9 +39,6 @@ class Renderer {
virtual int GetScreenWidth() const = 0;
virtual int GetScreenHeight() const = 0;
virtual void SetViewport(int x, int y, int width, int height) = 0;
virtual void ResetViewport() = 0;
virtual void SetScissor(int x, int y, int width, int height) = 0;
virtual void ResetScissor() = 0;

View File

@ -390,30 +390,6 @@ int RendererVulkan::GetScreenHeight() const {
return context_.GetWindowHeight();
}
void RendererVulkan::SetViewport(int x, int y, int width, int height) {
VkViewport viewport;
viewport.x = 0;
viewport.y = (float)height;
viewport.width = (float)width;
viewport.height = -(float)height;
viewport.minDepth = 0;
viewport.maxDepth = 1.0;
vkCmdSetViewport(frames_[current_frame_].draw_command_buffer, 0, 1,
&viewport);
}
void RendererVulkan::ResetViewport() {
VkViewport viewport;
viewport.x = 0;
viewport.y = (float)context_.GetWindowHeight();
viewport.width = (float)context_.GetWindowWidth();
viewport.height = -(float)context_.GetWindowHeight();
viewport.minDepth = 0;
viewport.maxDepth = 1.0;
vkCmdSetViewport(frames_[current_frame_].draw_command_buffer, 0, 1,
&viewport);
}
void RendererVulkan::SetScissor(int x, int y, int width, int height) {
if (x < 0)
x = 0;
@ -2014,6 +1990,8 @@ bool RendererVulkan::CreatePipelineLayout(
}
void RendererVulkan::DrawListBegin() {
VkCommandBuffer command_buffer = frames_[current_frame_].draw_command_buffer;
VkRenderPassBeginInfo render_pass_begin;
render_pass_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
render_pass_begin.pNext = nullptr;
@ -2032,11 +2010,26 @@ void RendererVulkan::DrawListBegin() {
render_pass_begin.clearValueCount = clear_values.size();
render_pass_begin.pClearValues = clear_values.data();
vkCmdBeginRenderPass(frames_[current_frame_].draw_command_buffer,
&render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBeginRenderPass(command_buffer, &render_pass_begin,
VK_SUBPASS_CONTENTS_INLINE);
ResetViewport();
ResetScissor();
VkViewport viewport;
viewport.x = 0;
viewport.y = (float)context_.GetWindowHeight();
viewport.width = (float)context_.GetWindowWidth();
viewport.height = -(float)context_.GetWindowHeight();
viewport.minDepth = 0;
viewport.maxDepth = 1.0;
vkCmdSetViewport(command_buffer, 0, 1, &viewport);
VkRect2D scissor;
scissor.offset.x = 0;
scissor.offset.y = 0;
scissor.extent.width = context_.GetWindowWidth();
scissor.extent.height = context_.GetWindowHeight();
vkCmdSetScissor(command_buffer, 0, 1, &scissor);
}
void RendererVulkan::DrawListEnd() {

View File

@ -33,9 +33,6 @@ class RendererVulkan final : public Renderer {
int GetScreenWidth() const final;
int GetScreenHeight() const final;
void SetViewport(int x, int y, int width, int height) final;
void ResetViewport() final;
void SetScissor(int x, int y, int width, int height) final;
void ResetScissor() final;