Add support for set/reset viewport

This commit is contained in:
Attila Uygun 2023-10-29 22:35:16 +01:00
parent f144bfdb2d
commit e4f020d359
7 changed files with 54 additions and 29 deletions

View File

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

View File

@ -135,6 +135,8 @@ void ImguiBackend::Draw() {
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,
@ -169,6 +171,7 @@ void ImguiBackend::Draw() {
renderer_->DestroyGeometry(geometry_id);
}
renderer_->ResetScissor();
renderer_->ResetViewport();
}
} // namespace eng

View File

@ -47,7 +47,6 @@ 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 {
@ -58,6 +57,14 @@ 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);
@ -436,6 +443,7 @@ void RendererOpenGL::SetUniform(uint64_t resource_id,
}
void RendererOpenGL::PrepareForDrawing() {
glViewport(0, 0, screen_width_, screen_height_);
glDisable(GL_SCISSOR_TEST);
}

View File

@ -32,6 +32,9 @@ 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,6 +39,9 @@ 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,6 +390,30 @@ 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;
@ -1990,8 +2014,6 @@ 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;
@ -2010,26 +2032,11 @@ void RendererVulkan::DrawListBegin() {
render_pass_begin.clearValueCount = clear_values.size();
render_pass_begin.pClearValues = clear_values.data();
vkCmdBeginRenderPass(command_buffer, &render_pass_begin,
VK_SUBPASS_CONTENTS_INLINE);
vkCmdBeginRenderPass(frames_[current_frame_].draw_command_buffer,
&render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
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);
ResetViewport();
ResetScissor();
}
void RendererVulkan::DrawListEnd() {

View File

@ -33,6 +33,9 @@ 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;