dawn-cmake/src/utils/OpenGLBinding.cpp

149 lines
5.4 KiB
C++
Raw Normal View History

// Copyright 2017 The NXT Authors
//
// 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.
#include "utils/BackendBinding.h"
2017-07-28 01:30:57 +00:00
#include "common/Assert.h"
#include "common/Platform.h"
2017-07-28 01:30:57 +00:00
#include "nxt/nxt_wsi.h"
#include "utils/SwapChainImpl.h"
2017-07-28 01:30:57 +00:00
#include <cstdio>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
namespace backend {
namespace opengl {
void Init(void* (*getProc)(const char*), nxtProcTable* procs, nxtDevice* device);
}
}
namespace utils {
2017-07-28 01:30:57 +00:00
class SwapChainImplGL : SwapChainImpl {
public:
static nxtSwapChainImplementation Create(GLFWwindow* window) {
auto impl = GenerateSwapChainImplementation<SwapChainImplGL, nxtWSIContextGL>();
impl.userData = new SwapChainImplGL(window);
return impl;
}
private:
2017-11-23 18:49:58 +00:00
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
SwapChainImplGL(GLFWwindow* window)
2017-11-23 18:49:58 +00:00
: mWindow(window) {
2017-07-28 01:30:57 +00:00
}
~SwapChainImplGL() {
2017-11-23 18:49:58 +00:00
glDeleteTextures(1, &mBackTexture);
glDeleteFramebuffers(1, &mBackFBO);
2017-07-28 01:30:57 +00:00
}
// For GenerateSwapChainImplementation
friend class SwapChainImpl;
void Init(nxtWSIContextGL*) {
2017-11-23 18:49:58 +00:00
glGenTextures(1, &mBackTexture);
glBindTexture(GL_TEXTURE_2D, mBackTexture);
2017-07-28 01:30:57 +00:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0,
GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
2017-11-23 18:49:58 +00:00
glGenFramebuffers(1, &mBackFBO);
glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
2017-07-28 01:30:57 +00:00
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2017-11-23 18:49:58 +00:00
GL_TEXTURE_2D, mBackTexture, 0);
2017-07-28 01:30:57 +00:00
}
nxtSwapChainError Configure(nxtTextureFormat format, nxtTextureUsageBit,
2017-07-28 01:30:57 +00:00
uint32_t width, uint32_t height) {
if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
return "unsupported format";
}
ASSERT(width > 0);
ASSERT(height > 0);
2017-11-23 18:49:58 +00:00
mWidth = width;
mHeight = height;
2017-07-28 01:30:57 +00:00
2017-11-23 18:49:58 +00:00
glBindTexture(GL_TEXTURE_2D, mBackTexture);
2017-07-28 01:30:57 +00:00
// Reallocate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
return NXT_SWAP_CHAIN_NO_ERROR;
}
nxtSwapChainError GetNextTexture(nxtSwapChainNextTexture* nextTexture) {
2017-11-23 18:49:58 +00:00
nextTexture->texture = reinterpret_cast<void*>(static_cast<size_t>(mBackTexture));
2017-07-28 01:30:57 +00:00
return NXT_SWAP_CHAIN_NO_ERROR;
}
nxtSwapChainError Present() {
2017-11-23 18:49:58 +00:00
glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
2017-07-28 01:30:57 +00:00
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2017-11-23 18:49:58 +00:00
glBlitFramebuffer(0, 0, mWidth, mHeight, 0, 0, mWidth, mHeight,
2017-07-28 01:30:57 +00:00
GL_COLOR_BUFFER_BIT, GL_NEAREST);
2017-11-23 18:49:58 +00:00
glfwSwapBuffers(mWindow);
2017-07-28 01:30:57 +00:00
return NXT_SWAP_CHAIN_NO_ERROR;
}
};
class OpenGLBinding : public BackendBinding {
public:
void SetupGLFWWindowHints() override {
#if defined(NXT_PLATFORM_APPLE)
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
}
void GetProcAndDevice(nxtProcTable* procs, nxtDevice* device) override {
2017-11-23 18:49:58 +00:00
glfwMakeContextCurrent(mWindow);
backend::opengl::Init(reinterpret_cast<void*(*)(const char*)>(glfwGetProcAddress), procs, device);
2017-11-23 18:49:58 +00:00
mBackendDevice = *device;
}
2017-07-28 01:30:57 +00:00
uint64_t GetSwapChainImplementation() override {
2017-11-23 18:49:58 +00:00
if (mSwapchainImpl.userData == nullptr) {
mSwapchainImpl = SwapChainImplGL::Create(mWindow);
2017-07-28 01:30:57 +00:00
}
2017-11-23 18:49:58 +00:00
return reinterpret_cast<uint64_t>(&mSwapchainImpl);
}
nxtTextureFormat GetPreferredSwapChainTextureFormat() override {
return NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM;
}
private:
2017-11-23 18:49:58 +00:00
nxtDevice mBackendDevice = nullptr;
nxtSwapChainImplementation mSwapchainImpl = {};
};
BackendBinding* CreateOpenGLBinding() {
return new OpenGLBinding;
}
}