Compare commits

..

No commits in common. "392c4eaf8f2f4c9010d2b92cd487b53fffd4bfef" and "c6546b43b52cae002f64045b9626f80e2bafefc0" have entirely different histories.

12 changed files with 80 additions and 120 deletions

View File

@ -113,7 +113,18 @@ void Engine::Initialize() {
CreateRendererInternal(RendererType::kVulkan); CreateRendererInternal(RendererType::kVulkan);
CreateProjectionMatrix(); // Normalize viewport.
if (GetScreenWidth() > GetScreenHeight()) {
float aspect_ratio = (float)GetScreenWidth() / (float)GetScreenHeight();
LOG(0) << "aspect ratio: " << aspect_ratio;
screen_size_ = {aspect_ratio * 2.0f, 2.0f};
projection_.CreateOrthoProjection(-aspect_ratio, aspect_ratio, -1.0f, 1.0f);
} else {
float aspect_ratio = (float)GetScreenHeight() / (float)GetScreenWidth();
LOG(0) << "aspect_ratio: " << aspect_ratio;
screen_size_ = {2.0f, aspect_ratio * 2.0f};
projection_.CreateOrthoProjection(-1.0, 1.0, -aspect_ratio, aspect_ratio);
}
LOG(0) << "image scale factor: " << GetImageScaleFactor(); LOG(0) << "image scale factor: " << GetImageScaleFactor();
@ -447,11 +458,11 @@ TextureCompressor* Engine::GetTextureCompressor(bool opacity) {
} }
int Engine::GetScreenWidth() const { int Engine::GetScreenWidth() const {
return renderer_->GetScreenWidth(); return renderer_->screen_width();
} }
int Engine::GetScreenHeight() const { int Engine::GetScreenHeight() const {
return renderer_->GetScreenHeight(); return renderer_->screen_height();
} }
int Engine::GetDeviceDpi() const { int Engine::GetDeviceDpi() const {
@ -459,7 +470,7 @@ int Engine::GetDeviceDpi() const {
} }
float Engine::GetImageScaleFactor() const { float Engine::GetImageScaleFactor() const {
float width_inch = static_cast<float>(renderer_->GetScreenWidth()) / float width_inch = static_cast<float>(renderer_->screen_width()) /
static_cast<float>(platform_->GetDeviceDpi()); static_cast<float>(platform_->GetDeviceDpi());
return 2.57143f / width_inch; return 2.57143f / width_inch;
} }
@ -493,10 +504,10 @@ void Engine::OnWindowDestroyed() {
} }
void Engine::OnWindowResized(int width, int height) { void Engine::OnWindowResized(int width, int height) {
if (width != renderer_->GetScreenWidth() || if (width != renderer_->screen_width() ||
height != renderer_->GetScreenHeight()) { height != renderer_->screen_height()) {
renderer_->OnWindowResized(width, height); renderer_->Shutdown();
CreateProjectionMatrix(); renderer_->Initialize(platform_);
} }
} }
@ -604,20 +615,6 @@ void Engine::CreateTextureCompressors() {
} }
} }
void Engine::CreateProjectionMatrix() {
if (GetScreenWidth() > GetScreenHeight()) {
float aspect_ratio = (float)GetScreenWidth() / (float)GetScreenHeight();
LOG(0) << "aspect ratio: " << aspect_ratio;
screen_size_ = {aspect_ratio * 2.0f, 2.0f};
projection_.CreateOrthoProjection(-aspect_ratio, aspect_ratio, -1.0f, 1.0f);
} else {
float aspect_ratio = (float)GetScreenHeight() / (float)GetScreenWidth();
LOG(0) << "aspect_ratio: " << aspect_ratio;
screen_size_ = {2.0f, aspect_ratio * 2.0f};
projection_.CreateOrthoProjection(-1.0, 1.0, -aspect_ratio, aspect_ratio);
}
}
void Engine::ContextLost() { void Engine::ContextLost() {
CreateRenderResources(); CreateRenderResources();
WaitForAsyncWork(); WaitForAsyncWork();

View File

@ -239,8 +239,6 @@ class Engine : public PlatformObserver {
void CreateTextureCompressors(); void CreateTextureCompressors();
void CreateProjectionMatrix();
void ContextLost(); void ContextLost();
void CreateRenderResources(); void CreateRenderResources();

View File

@ -30,7 +30,7 @@ Platform::Platform() {
XSelectInput(display_, window_, XSelectInput(display_, window_,
KeyPressMask | Button1MotionMask | ButtonPressMask | KeyPressMask | Button1MotionMask | ButtonPressMask |
ButtonReleaseMask | FocusChangeMask | StructureNotifyMask); ButtonReleaseMask | FocusChangeMask);
Atom WM_DELETE_WINDOW = XInternAtom(display_, "WM_DELETE_WINDOW", false); Atom WM_DELETE_WINDOW = XInternAtom(display_, "WM_DELETE_WINDOW", false);
XSetWMProtocols(display_, window_, &WM_DELETE_WINDOW, 1); XSetWMProtocols(display_, window_, &WM_DELETE_WINDOW, 1);
} }
@ -91,10 +91,6 @@ void Platform::Update() {
should_exit_ = true; should_exit_ = true;
break; break;
} }
case ConfigureNotify: {
XConfigureEvent xce = e.xconfigure;
observer_->OnWindowResized(xce.width, xce.height);
}
} }
} }
} }

View File

@ -46,20 +46,6 @@ void RendererOpenGL::Shutdown() {
ShutdownInternal(); ShutdownInternal();
} }
void RendererOpenGL::OnWindowResized(int width, int height) {
screen_width_ = width;
screen_height_ = height;
glViewport(0, 0, screen_width_, screen_height_);
}
int RendererOpenGL::GetScreenWidth() const {
return screen_width_;
}
int RendererOpenGL::GetScreenHeight() const {
return screen_height_;
}
uint64_t RendererOpenGL::CreateGeometry(std::unique_ptr<Mesh> mesh) { uint64_t RendererOpenGL::CreateGeometry(std::unique_ptr<Mesh> mesh) {
// Verify that we have a valid layout and get the total byte size per vertex. // Verify that we have a valid layout and get the total byte size per vertex.
GLuint vertex_size = mesh->GetVertexSize(); GLuint vertex_size = mesh->GetVertexSize();
@ -497,6 +483,8 @@ bool RendererOpenGL::InitCommon() {
LOG(0) << " etc1: " << texture_compression_.etc1; LOG(0) << " etc1: " << texture_compression_.etc1;
LOG(0) << " s3tc: " << texture_compression_.s3tc; LOG(0) << " s3tc: " << texture_compression_.s3tc;
glViewport(0, 0, screen_width_, screen_height_);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

View File

@ -25,11 +25,6 @@ class RendererOpenGL final : public Renderer {
bool IsInitialzed() const final { return is_initialized_; } bool IsInitialzed() const final { return is_initialized_; }
void OnWindowResized(int width, int height) final;
int GetScreenWidth() const final;
int GetScreenHeight() const final;
uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) final; uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) final;
void DestroyGeometry(uint64_t resource_id) final; void DestroyGeometry(uint64_t resource_id) final;
void Draw(uint64_t resource_id) final; void Draw(uint64_t resource_id) final;
@ -114,9 +109,6 @@ class RendererOpenGL final : public Renderer {
// Stats. // Stats.
size_t fps_ = 0; size_t fps_ = 0;
int screen_width_ = 0;
int screen_height_ = 0;
#if defined(__ANDROID__) #if defined(__ANDROID__)
ANativeWindow* window_; ANativeWindow* window_;
#elif defined(__linux__) #elif defined(__linux__)

View File

@ -11,6 +11,11 @@ bool RendererOpenGL::Initialize(Platform* platform) {
display_ = platform->GetDisplay(); display_ = platform->GetDisplay();
window_ = platform->GetWindow(); window_ = platform->GetWindow();
XWindowAttributes xwa;
XGetWindowAttributes(display_, window_, &xwa);
screen_width_ = xwa.width;
screen_height_ = xwa.height;
GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER,
None}; None};
XVisualInfo* visual_info = glXChooseVisual(display_, 0, glx_attributes); XVisualInfo* visual_info = glXChooseVisual(display_, 0, glx_attributes);
@ -27,10 +32,6 @@ bool RendererOpenGL::Initialize(Platform* platform) {
return false; return false;
} }
XWindowAttributes xwa;
XGetWindowAttributes(display_, window_, &xwa);
OnWindowResized(xwa.width, xwa.height);
return InitCommon(); return InitCommon();
} }

View File

@ -30,11 +30,6 @@ class Renderer {
virtual bool IsInitialzed() const = 0; virtual bool IsInitialzed() const = 0;
virtual void OnWindowResized(int width, int height) = 0;
virtual int GetScreenWidth() const = 0;
virtual int GetScreenHeight() const = 0;
virtual uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) = 0; virtual uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) = 0;
virtual void DestroyGeometry(uint64_t resource_id) = 0; virtual void DestroyGeometry(uint64_t resource_id) = 0;
virtual void Draw(uint64_t resource_id) = 0; virtual void Draw(uint64_t resource_id) = 0;
@ -82,6 +77,9 @@ class Renderer {
bool SupportsDXT5() const { return texture_compression_.s3tc; } bool SupportsDXT5() const { return texture_compression_.s3tc; }
bool SupportsATC() const { return texture_compression_.atc; } bool SupportsATC() const { return texture_compression_.atc; }
int screen_width() const { return screen_width_; }
int screen_height() const { return screen_height_; }
virtual size_t GetAndResetFPS() = 0; virtual size_t GetAndResetFPS() = 0;
virtual const char* GetDebugName() = 0; virtual const char* GetDebugName() = 0;
@ -108,6 +106,9 @@ class Renderer {
TextureCompression texture_compression_; TextureCompression texture_compression_;
int screen_width_ = 0;
int screen_height_ = 0;
base::Closure context_lost_cb_; base::Closure context_lost_cb_;
Renderer(const Renderer&) = delete; Renderer(const Renderer&) = delete;

View File

@ -377,18 +377,6 @@ RendererVulkan::~RendererVulkan() {
Shutdown(); Shutdown();
} }
void RendererVulkan::OnWindowResized(int width, int height) {
context_.ResizeWindow(width, height);
}
int RendererVulkan::GetScreenWidth() const {
return context_.GetWindowWidth();
}
int RendererVulkan::GetScreenHeight() const {
return context_.GetWindowHeight();
}
uint64_t RendererVulkan::CreateGeometry(std::unique_ptr<Mesh> mesh) { uint64_t RendererVulkan::CreateGeometry(std::unique_ptr<Mesh> mesh) {
auto& geometry = geometries_[++last_resource_id_] = {}; auto& geometry = geometries_[++last_resource_id_] = {};
@ -974,21 +962,11 @@ bool RendererVulkan::InitializeInternal() {
LOG(0) << " etc1: " << texture_compression_.etc1; LOG(0) << " etc1: " << texture_compression_.etc1;
LOG(0) << " s3tc: " << texture_compression_.s3tc; LOG(0) << " s3tc: " << texture_compression_.s3tc;
current_staging_buffer_ = 0;
staging_buffer_used_ = false;
if (max_staging_buffer_size_ < staging_buffer_size_ * 4)
max_staging_buffer_size_ = staging_buffer_size_ * 4;
for (int i = 0; i < frame_count; i++) {
bool err = InsertStagingBuffer();
LOG_IF(0, !err) << "Failed to create staging buffer.";
}
// Use a background thread for filling up staging buffers and recording setup // Use a background thread for filling up staging buffers and recording setup
// commands. // commands.
quit_.store(false, std::memory_order_relaxed); quit_.store(false, std::memory_order_relaxed);
setup_thread_ = std::thread(&RendererVulkan::SetupThreadMain, this); setup_thread_ =
std::thread(&RendererVulkan::SetupThreadMain, this, frame_count);
// Begin the first command buffer for the first frame. // Begin the first command buffer for the first frame.
BeginFrame(); BeginFrame();
@ -1010,11 +988,6 @@ void RendererVulkan::Shutdown() {
semaphore_.release(); semaphore_.release();
setup_thread_.join(); setup_thread_.join();
for (size_t i = 0; i < staging_buffers_.size(); i++) {
auto [buffer, allocation] = staging_buffers_[i].buffer;
vmaDestroyBuffer(allocator_, buffer, allocation);
}
DestroyAllResources(); DestroyAllResources();
context_lost_ = true; context_lost_ = true;
@ -1080,8 +1053,7 @@ void RendererVulkan::BeginFrame() {
// Advance current frame. // Advance current frame.
frames_drawn_++; frames_drawn_++;
// Advance staging buffer if used. All tasks in bg thread are complete so this // Advance staging buffer if used.
// is thread safe.
if (staging_buffer_used_) { if (staging_buffer_used_) {
current_staging_buffer_ = current_staging_buffer_ =
(current_staging_buffer_ + 1) % staging_buffers_.size(); (current_staging_buffer_ + 1) % staging_buffers_.size();
@ -1943,8 +1915,8 @@ void RendererVulkan::DrawListBegin() {
render_pass_begin.renderPass = context_.GetRenderPass(); render_pass_begin.renderPass = context_.GetRenderPass();
render_pass_begin.framebuffer = context_.GetFramebuffer(); render_pass_begin.framebuffer = context_.GetFramebuffer();
render_pass_begin.renderArea.extent.width = context_.GetWindowWidth(); render_pass_begin.renderArea.extent.width = screen_width_;
render_pass_begin.renderArea.extent.height = context_.GetWindowHeight(); render_pass_begin.renderArea.extent.height = screen_height_;
render_pass_begin.renderArea.offset.x = 0; render_pass_begin.renderArea.offset.x = 0;
render_pass_begin.renderArea.offset.y = 0; render_pass_begin.renderArea.offset.y = 0;
@ -1960,9 +1932,9 @@ void RendererVulkan::DrawListBegin() {
VkViewport viewport; VkViewport viewport;
viewport.x = 0; viewport.x = 0;
viewport.y = (float)context_.GetWindowHeight(); viewport.y = (float)screen_height_;
viewport.width = (float)context_.GetWindowWidth(); viewport.width = (float)screen_width_;
viewport.height = -(float)context_.GetWindowHeight(); viewport.height = -(float)screen_height_;
viewport.minDepth = 0; viewport.minDepth = 0;
viewport.maxDepth = 1.0; viewport.maxDepth = 1.0;
@ -1971,8 +1943,8 @@ void RendererVulkan::DrawListBegin() {
VkRect2D scissor; VkRect2D scissor;
scissor.offset.x = 0; scissor.offset.x = 0;
scissor.offset.y = 0; scissor.offset.y = 0;
scissor.extent.width = context_.GetWindowWidth(); scissor.extent.width = screen_width_;
scissor.extent.height = context_.GetWindowHeight(); scissor.extent.height = screen_height_;
vkCmdSetScissor(command_buffer, 0, 1, &scissor); vkCmdSetScissor(command_buffer, 0, 1, &scissor);
} }
@ -1987,15 +1959,16 @@ void RendererVulkan::DrawListEnd() {
#ifdef FORCE_FULL_BARRIER #ifdef FORCE_FULL_BARRIER
FullBarrier(true); FullBarrier(true);
#else #else
MemoryBarrier( MemoryBarrier(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT |
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
VK_ACCESS_INDEX_READ_BIT | VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INDEX_READ_BIT | VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT |
VK_ACCESS_SHADER_WRITE_BIT); VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_SHADER_READ_BIT |
VK_ACCESS_SHADER_WRITE_BIT);
#endif #endif
} }
@ -2018,7 +1991,18 @@ void RendererVulkan::SwapBuffers() {
BeginFrame(); BeginFrame();
} }
void RendererVulkan::SetupThreadMain() { void RendererVulkan::SetupThreadMain(int preallocate) {
if (max_staging_buffer_size_ < staging_buffer_size_ * 4)
max_staging_buffer_size_ = staging_buffer_size_ * 4;
current_staging_buffer_ = 0;
staging_buffer_used_ = false;
for (int i = 0; i < preallocate; i++) {
bool err = InsertStagingBuffer();
LOG_IF(0, !err) << "Failed to create staging buffer.";
}
for (;;) { for (;;) {
semaphore_.acquire(); semaphore_.acquire();
if (quit_.load(std::memory_order_relaxed)) if (quit_.load(std::memory_order_relaxed))
@ -2026,6 +2010,11 @@ void RendererVulkan::SetupThreadMain() {
task_runner_.RunTasks(); task_runner_.RunTasks();
} }
for (size_t i = 0; i < staging_buffers_.size(); i++) {
auto [buffer, allocation] = staging_buffers_[i].buffer;
vmaDestroyBuffer(allocator_, buffer, allocation);
}
} }
template <typename T> template <typename T>

View File

@ -28,11 +28,6 @@ class RendererVulkan final : public Renderer {
bool IsInitialzed() const final { return device_ != VK_NULL_HANDLE; } bool IsInitialzed() const final { return device_ != VK_NULL_HANDLE; }
void OnWindowResized(int width, int height) final;
int GetScreenWidth() const final;
int GetScreenHeight() const final;
uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) final; uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) final;
void DestroyGeometry(uint64_t resource_id) final; void DestroyGeometry(uint64_t resource_id) final;
void Draw(uint64_t resource_id) final; void Draw(uint64_t resource_id) final;
@ -256,7 +251,7 @@ class RendererVulkan final : public Renderer {
void SwapBuffers(); void SwapBuffers();
void SetupThreadMain(); void SetupThreadMain(int preallocate);
template <typename T> template <typename T>
bool SetUniformInternal(ShaderVulkan& shader, const std::string& name, T val); bool SetUniformInternal(ShaderVulkan& shader, const std::string& name, T val);

View File

@ -10,14 +10,15 @@ namespace eng {
bool RendererVulkan::Initialize(Platform* platform) { bool RendererVulkan::Initialize(Platform* platform) {
LOG(0) << "Initializing renderer."; LOG(0) << "Initializing renderer.";
int width = ANativeWindow_getWidth(platform->GetWindow()); screen_width_ = ANativeWindow_getWidth(platform->GetWindow());
int height = ANativeWindow_getHeight(platform->GetWindow()); screen_height_ = ANativeWindow_getHeight(platform->GetWindow());
if (!context_.Initialize()) { if (!context_.Initialize()) {
LOG(0) << "Failed to initialize Vulkan context."; LOG(0) << "Failed to initialize Vulkan context.";
return false; return false;
} }
if (!context_.CreateWindow(platform->GetWindow(), width, height)) { if (!context_.CreateWindow(platform->GetWindow(), screen_width_,
screen_height_)) {
LOG(0) << "Vulkan context failed to create window."; LOG(0) << "Vulkan context failed to create window.";
return false; return false;
} }

View File

@ -10,13 +10,15 @@ bool RendererVulkan::Initialize(Platform* platform) {
XWindowAttributes xwa; XWindowAttributes xwa;
XGetWindowAttributes(platform->GetDisplay(), platform->GetWindow(), &xwa); XGetWindowAttributes(platform->GetDisplay(), platform->GetWindow(), &xwa);
screen_width_ = xwa.width;
screen_height_ = xwa.height;
if (!context_.Initialize()) { if (!context_.Initialize()) {
LOG(0) << "Failed to initialize Vulkan context."; LOG(0) << "Failed to initialize Vulkan context.";
return false; return false;
} }
if (!context_.CreateWindow(platform->GetDisplay(), platform->GetWindow(), if (!context_.CreateWindow(platform->GetDisplay(), platform->GetWindow(),
xwa.width, xwa.height)) { screen_width_, screen_height_)) {
LOG(0) << "Vulkan context failed to create window."; LOG(0) << "Vulkan context failed to create window.";
return false; return false;
} }

View File

@ -58,8 +58,8 @@ class VulkanContext {
VkPhysicalDeviceLimits GetDeviceLimits() const { return gpu_props_.limits; } VkPhysicalDeviceLimits GetDeviceLimits() const { return gpu_props_.limits; }
int GetWindowWidth() const { return window_.width; } int GetWidth() { return window_.width; }
int GetWindowHeight() const { return window_.height; } int GetHeight() { return window_.height; }
size_t GetAndResetFPS(); size_t GetAndResetFPS();