1 // Copyright 2019 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/opengl/NativeSwapChainImplGL.h" 16 17 #include "dawn_native/opengl/DeviceGL.h" 18 19 namespace dawn_native { namespace opengl { 20 NativeSwapChainImpl(Device * device,PresentCallback present,void * presentUserdata)21 NativeSwapChainImpl::NativeSwapChainImpl(Device* device, 22 PresentCallback present, 23 void* presentUserdata) 24 : mPresentCallback(present), mPresentUserdata(presentUserdata), mDevice(device) { 25 } 26 ~NativeSwapChainImpl()27 NativeSwapChainImpl::~NativeSwapChainImpl() { 28 const OpenGLFunctions& gl = mDevice->gl; 29 gl.DeleteTextures(1, &mBackTexture); 30 gl.DeleteFramebuffers(1, &mBackFBO); 31 } 32 Init(DawnWSIContextGL *)33 void NativeSwapChainImpl::Init(DawnWSIContextGL* /*context*/) { 34 const OpenGLFunctions& gl = mDevice->gl; 35 gl.GenTextures(1, &mBackTexture); 36 gl.BindTexture(GL_TEXTURE_2D, mBackTexture); 37 gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); 38 39 gl.GenFramebuffers(1, &mBackFBO); 40 gl.BindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO); 41 gl.FramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 42 mBackTexture, 0); 43 } 44 Configure(WGPUTextureFormat format,WGPUTextureUsage usage,uint32_t width,uint32_t height)45 DawnSwapChainError NativeSwapChainImpl::Configure(WGPUTextureFormat format, 46 WGPUTextureUsage usage, 47 uint32_t width, 48 uint32_t height) { 49 if (format != WGPUTextureFormat_RGBA8Unorm) { 50 return "unsupported format"; 51 } 52 ASSERT(width > 0); 53 ASSERT(height > 0); 54 mWidth = width; 55 mHeight = height; 56 57 const OpenGLFunctions& gl = mDevice->gl; 58 gl.BindTexture(GL_TEXTURE_2D, mBackTexture); 59 // Reallocate the texture 60 gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 61 nullptr); 62 63 return DAWN_SWAP_CHAIN_NO_ERROR; 64 } 65 GetNextTexture(DawnSwapChainNextTexture * nextTexture)66 DawnSwapChainError NativeSwapChainImpl::GetNextTexture(DawnSwapChainNextTexture* nextTexture) { 67 nextTexture->texture.u32 = mBackTexture; 68 return DAWN_SWAP_CHAIN_NO_ERROR; 69 } 70 Present()71 DawnSwapChainError NativeSwapChainImpl::Present() { 72 const OpenGLFunctions& gl = mDevice->gl; 73 gl.BindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO); 74 gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); 75 gl.Scissor(0, 0, mWidth, mHeight); 76 gl.BlitFramebuffer(0, 0, mWidth, mHeight, 0, mHeight, mWidth, 0, GL_COLOR_BUFFER_BIT, 77 GL_NEAREST); 78 79 mPresentCallback(mPresentUserdata); 80 81 return DAWN_SWAP_CHAIN_NO_ERROR; 82 } 83 GetPreferredFormat() const84 wgpu::TextureFormat NativeSwapChainImpl::GetPreferredFormat() const { 85 return wgpu::TextureFormat::RGBA8Unorm; 86 } 87 88 }} // namespace dawn_native::opengl 89