2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-06-19 16:53:38 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "utils/BackendBinding.h"
|
2017-06-19 16:53:38 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
#include "common/Assert.h"
|
2017-07-14 18:05:14 +00:00
|
|
|
#include "common/Platform.h"
|
2018-01-15 20:44:38 +00:00
|
|
|
#include "common/SwapChainUtils.h"
|
2018-07-18 12:32:45 +00:00
|
|
|
#include "dawn/dawn_wsi.h"
|
2018-07-25 11:37:28 +00:00
|
|
|
#include "dawn_native/OpenGLBackend.h"
|
2017-07-14 18:05:14 +00:00
|
|
|
|
2017-11-24 16:45:29 +00:00
|
|
|
// Glad needs to be included before GLFW otherwise it complain that GL.h was already included
|
2017-07-28 01:30:57 +00:00
|
|
|
#include "glad/glad.h"
|
2017-11-24 16:45:29 +00:00
|
|
|
|
|
|
|
#include <cstdio>
|
2017-06-19 16:53:38 +00:00
|
|
|
#include "GLFW/glfw3.h"
|
|
|
|
|
|
|
|
namespace utils {
|
2018-01-15 20:44:38 +00:00
|
|
|
class SwapChainImplGL {
|
2017-11-24 16:45:29 +00:00
|
|
|
public:
|
2018-07-18 12:38:34 +00:00
|
|
|
using WSIContext = dawnWSIContextGL;
|
2017-11-24 16:45:29 +00:00
|
|
|
|
|
|
|
SwapChainImplGL(GLFWwindow* window) : mWindow(window) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~SwapChainImplGL() {
|
|
|
|
glDeleteTextures(1, &mBackTexture);
|
|
|
|
glDeleteFramebuffers(1, &mBackFBO);
|
|
|
|
}
|
|
|
|
|
2018-07-18 12:38:34 +00:00
|
|
|
void Init(dawnWSIContextGL*) {
|
2017-11-24 16:45:29 +00:00
|
|
|
glGenTextures(1, &mBackTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mBackTexture);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
|
|
|
|
glGenFramebuffers(1, &mBackFBO);
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
|
|
|
|
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
|
|
|
mBackTexture, 0);
|
|
|
|
}
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnSwapChainError Configure(dawnTextureFormat format,
|
|
|
|
dawnTextureUsageBit,
|
2018-07-18 12:38:34 +00:00
|
|
|
uint32_t width,
|
|
|
|
uint32_t height) {
|
2018-07-18 13:12:52 +00:00
|
|
|
if (format != DAWN_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
|
2017-11-24 16:45:29 +00:00
|
|
|
return "unsupported format";
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2017-11-24 16:45:29 +00:00
|
|
|
ASSERT(width > 0);
|
|
|
|
ASSERT(height > 0);
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mBackTexture);
|
|
|
|
// Reallocate the texture
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
nullptr);
|
|
|
|
|
2018-07-18 12:38:34 +00:00
|
|
|
return DAWN_SWAP_CHAIN_NO_ERROR;
|
2017-11-24 16:45:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 12:38:34 +00:00
|
|
|
dawnSwapChainError GetNextTexture(dawnSwapChainNextTexture* nextTexture) {
|
2018-01-16 16:03:07 +00:00
|
|
|
nextTexture->texture.u32 = mBackTexture;
|
2018-07-18 12:38:34 +00:00
|
|
|
return DAWN_SWAP_CHAIN_NO_ERROR;
|
2017-11-24 16:45:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 12:38:34 +00:00
|
|
|
dawnSwapChainError Present() {
|
2017-11-24 16:45:29 +00:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
2018-02-12 16:01:58 +00:00
|
|
|
glBlitFramebuffer(0, 0, mWidth, mHeight, 0, mHeight, mWidth, 0, GL_COLOR_BUFFER_BIT,
|
2017-11-24 16:45:29 +00:00
|
|
|
GL_NEAREST);
|
|
|
|
glfwSwapBuffers(mWindow);
|
|
|
|
|
2018-07-18 12:38:34 +00:00
|
|
|
return DAWN_SWAP_CHAIN_NO_ERROR;
|
2017-11-24 16:45:29 +00:00
|
|
|
}
|
2018-01-15 20:44:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
GLFWwindow* mWindow = nullptr;
|
|
|
|
uint32_t mWidth = 0;
|
|
|
|
uint32_t mHeight = 0;
|
|
|
|
GLuint mBackFBO = 0;
|
|
|
|
GLuint mBackTexture = 0;
|
2017-07-28 01:30:57 +00:00
|
|
|
};
|
|
|
|
|
2017-06-19 16:53:38 +00:00
|
|
|
class OpenGLBinding : public BackendBinding {
|
2017-11-24 16:45:29 +00:00
|
|
|
public:
|
|
|
|
void SetupGLFWWindowHints() override {
|
2018-07-18 11:37:54 +00:00
|
|
|
#if defined(DAWN_PLATFORM_APPLE)
|
2017-11-24 16:45:29 +00:00
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
#else
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
#endif
|
|
|
|
}
|
2018-07-18 13:15:07 +00:00
|
|
|
void GetProcAndDevice(dawnProcTable* procs, dawnDevice* device) override {
|
2017-11-24 16:45:29 +00:00
|
|
|
glfwMakeContextCurrent(mWindow);
|
2018-08-01 13:03:58 +00:00
|
|
|
// Load the GL entry points in our copy of the glad static library
|
|
|
|
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
dawn_native::opengl::Init(reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress),
|
|
|
|
procs, device);
|
2017-11-24 16:45:29 +00:00
|
|
|
|
|
|
|
mBackendDevice = *device;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t GetSwapChainImplementation() override {
|
|
|
|
if (mSwapchainImpl.userData == nullptr) {
|
2018-01-15 20:44:38 +00:00
|
|
|
mSwapchainImpl = CreateSwapChainImplementation(new SwapChainImplGL(mWindow));
|
2017-06-19 16:53:38 +00:00
|
|
|
}
|
2017-11-24 16:45:29 +00:00
|
|
|
return reinterpret_cast<uint64_t>(&mSwapchainImpl);
|
|
|
|
}
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnTextureFormat GetPreferredSwapChainTextureFormat() override {
|
|
|
|
return DAWN_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM;
|
2017-11-24 16:45:29 +00:00
|
|
|
}
|
2017-09-21 16:54:53 +00:00
|
|
|
|
2017-11-24 16:45:29 +00:00
|
|
|
private:
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnDevice mBackendDevice = nullptr;
|
2018-07-18 12:38:34 +00:00
|
|
|
dawnSwapChainImplementation mSwapchainImpl = {};
|
2017-06-19 16:53:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BackendBinding* CreateOpenGLBinding() {
|
|
|
|
return new OpenGLBinding;
|
|
|
|
}
|
|
|
|
|
2017-11-24 16:45:29 +00:00
|
|
|
} // namespace utils
|