1#include "SwapChainGL20.h"
4#include "FormatsGL20.h"
5#include "GraphicsDeviceGL20.h"
7#include "Foundation/Logging/Logger.h"
8#include "Foundation/Platform/WindowData.h"
15Cogs::SwapChainGL20::SwapChainGL20(GraphicsDeviceGL20* device,
bool isPrimary)
16 : graphicsDevice(device),
17 isPrimary(isPrimary) {
20Cogs::SwapChainGL20::~SwapChainGL20() {
41 glContext = graphicsDevice->getShareContext();
43 else if (windowData && windowData->defaultContext) {
44 glContext = GLContext(windowData->defaultContext);
47 LOG_ERROR(logger,
"No share or window context .");
51 if (!glContext.create(
nullptr, winData, &graphicsDevice->getShareContext(), &settings, GLContext::Platform::GL, debugLogging)) {
52 LOG_ERROR(logger,
"Can't retrieve window's device context.");
56 glContext.makeCurrent();
60 glGenFramebuffers(1, &framebuffer);
63 graphicsDevice->getDefaultSwapChain()->glContext.makeCurrent();
69void Cogs::SwapChainGL20::destroy()
80 glContext.makeCurrent();
81 glBindFramebuffer(GL_FRAMEBUFFER, 0);
82 glDrawBuffer(GL_BACK);
86 recreateOffscreenBuffers();
102 acquire = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
104 glContext.makeCurrent();
106 glWaitSync(acquire, 0, GL_TIMEOUT_IGNORED);
107 glDeleteSync(acquire);
108 glDisable(GL_SCISSOR_TEST);
109 glViewport(0, 0, width, height);
110 glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
111 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
112 glDrawBuffer(GL_BACK);
113 glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
114 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
120 glContext.swapBuffers(syncInterval, presentFlags);
124 graphicsDevice->getDefaultSwapChain()->glContext.makeCurrent();
128void Cogs::SwapChainGL20::ReleaseFence()
130 if(release) glDeleteSync(release);
131 release = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
134void Cogs::SwapChainGL20::ReleaseWait()
137 glWaitSync(release, 0, GL_TIMEOUT_IGNORED);
138 glDeleteSync(release);
148 if ((width != newWidth) || (height != newHeight) || forceIt) {
159 return graphicsDevice->getSettings().numSamples;
178 desc.height = height;
181 desc.target = Cogs::ResourceDimensions::Texture2DMS;
184 if (Cogs::HandleIsValid(depthStencil)) {
188 if (Cogs::HandleIsValid(renderTarget)) {
192 if (Cogs::HandleIsValid(textureHandle)) {
197 textureHandle = textures->
loadTexture(desc,
nullptr);
198 if (!Cogs::HandleIsValid(this->textureHandle)) {
199 LOG_ERROR(logger,
"Unable to create offscreen texture.");
204 rt_desc.
texture = textureHandle;
207 if (!Cogs::HandleIsValid(renderTarget)) {
208 LOG_ERROR(logger,
"Error creating render target.");
213 if (!Cogs::HandleIsValid(depthStencil)) {
214 LOG_ERROR(logger,
"Error creating depth stencil.");
219 GLsync acquire = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
220 glContext.makeCurrent();
221 glWaitSync(acquire, 0, GL_TIMEOUT_IGNORED);
222 glDeleteSync(acquire);
224 const TextureGL20& texture = graphicsDevice->getTextures()->textures[textureHandle];
225 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
226 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture.type, texture.textureId, 0);
227 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
229 case GL_FRAMEBUFFER_COMPLETE:
231 case GL_FRAMEBUFFER_UNDEFINED: [[fallthrough]];
232 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: [[fallthrough]];
233 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: [[fallthrough]];
234 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: [[fallthrough]];
235 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: [[fallthrough]];
236 case GL_FRAMEBUFFER_UNSUPPORTED: [[fallthrough]];
237 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: [[fallthrough]];
238 case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: [[fallthrough]];
240 LOG_ERROR(logger,
"Failed to create swapchain FBO, glCheckFramebufferStatus()=0x%x", status);
241 glDeleteFramebuffers(1, &framebuffer);
248 graphicsDevice->getDefaultSwapChain()->glContext.makeCurrent();
Log implementation class.
bool recreateOffscreenBuffers()
Recreates the textures and render targets used for offscreen rendering.
virtual void setSize(int newWidth, int newHeight, bool forceIt=false) override
Changes the size of this swap chain's viewport.
virtual void setFullscreen(bool enabled) override
Switch this swap chain to fullscreen or windowed depending on the parameter.
virtual void beginFrame() override
Prepares this swap chain for rendering.
virtual int getSamples() const override
Returns the number of samples this swap chain has.
bool initialize(WindowData *windowData, bool debugLogging)
Initialises this swap chain and attaches it to the window provided.
virtual void endFrame(uint32_t syncInterval=0, PresentFlags presentFlags=PresentFlags::None) override
Finalises rendering and swaps the front and back buffers of this swap chain instance.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
PresentFlags
Flags controlling presentation.
Settings for graphics device initialization.
int numSamples
Number of samples to use for back buffer MSAA.
TextureFormat colorFormat
Back buffer format.
Describes a single render target view and which resources to use from the underlying texture.
uint16_t numLayers
Number of available layers to render to.
TextureHandle texture
Texture handle.
void releaseRenderTarget(RenderTargetHandle renderTargetHandle) override
Release the render target with the given renderTargetHandle.
void releaseDepthStencilTarget(DepthStencilHandle depthStencilHandle) override
Release the depth target with the given depthStencilHandle.
RenderTargetHandle createRenderTarget(const RenderTargetViewDescription *renderTargetViews, const size_t numViews) override
Create a render target using the given view descriptions.
DepthStencilHandle createDepthStencilTarget(const RenderTargetHandle handle) override
Creates a depth/stencil target to back the render target with the given handle.
@ RenderTarget
The texture can be used as a render target and drawn into.
TextureHandle loadTexture(const TextureDescription &desc, const TextureData *data) override
Load a texture from the given description.
void releaseTexture(TextureHandle textureHandle) override
Release the texture with the given textureHandle.