1#include "GraphicsDeviceGL20.h"
3#include "Foundation/Logging/Logger.h"
4#include "Foundation/Platform/Unicode.h"
11#include "Foundation/Platform/WindowData.h"
19Cogs::GraphicsDeviceGL20::GraphicsDeviceGL20(RenderingAllocatorInfo*) : defaultSwapChain(this, true) {
22Cogs::GraphicsDeviceGL20::~GraphicsDeviceGL20() {
24 defaultSwapChain.destroy();
32 assert(!initialized &&
"Initialize invoked multiple times.");
34 if (settings.windowData->windowHandle == (WindowData::nativeHandleType)
nullptr) {
35 LOG_ERROR(logger,
"No window handle");
40#if defined( __APPLE__ )
41 glShareContext = GLContext(settings.windowData->defaultContext);
42 glShareContext.makeCurrent();
45 GLContext::NativeDisplay display = settings.windowData->display;
47 GLContext::NativeDisplay display =
nullptr;
49 if (!glShareContext.create(display,
nullptr,
nullptr, &settings, GLContext::Platform::GL, enableDiagnostics)) {
50 LOG_ERROR(logger,
"Failed to create share context.");
55 defaultSwapChain.initialize(settings.windowData, enableDiagnostics);
59 useClipControl =
false;
61 if(!glClipControl) useClipControl =
false;
64 capabilities.initialize(useClipControl);
65 context.initialize(&capabilities, &buffers, &textures, &effects, &renderTargets, &syncObjects);
66 effects.initialize(
this, &capabilities, &buffers);
67 renderTargets.initialize(&textures);
68 textures.initialize(
this);
70 glEnable(GL_PROGRAM_POINT_SIZE);
72 if (settings.colorFormat == TextureFormat::R8G8B8A8_UNORM_SRGB) {
73 glEnable(GL_FRAMEBUFFER_SRGB);
78 LOG_DEBUG(logger,
"glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE)");
79 glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
94 if (settings.ioHandler) {
95 effects.setIOHandler(settings.ioHandler);
108 if (swapChain->
initialize(windowData, enableDiagnostics)) {
109 swapChains.push_back(swapChain);
120 auto e = swapChains.end();
121 auto i = std::find(swapChains.begin(), e, swapChain);
134 defaultSwapChain.setSize(newWidth, newHeight);
141 w = defaultSwapChain.getWidth();
142 h = defaultSwapChain.getHeight();
150 defaultSwapChain.beginFrame();
153 swapChain->beginFrame();
160 context.setDefaults();
168 swapChain->endFrame(0, presentFlags);
170 defaultSwapChain.endFrame(syncInterval, presentFlags);
180 GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
182 glClientWaitSync(sync, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
195 defaultSwapChain.beginFrame();
197 context.releaseResources();
198 effects.releaseResources();
199 renderTargets.releaseResources();
200 textures.releaseResources();
214 stats.bufferMemoryConsumption = buffers.bufferMemoryConsumption;
215 stats.textureMemoryConsumption = 0;
216 stats.bufferCount =
static_cast<uint32_t
>(buffers.buffers.size());
217 stats.inputLayoutCount =
static_cast<uint32_t
>(buffers.inputLayouts.size());
218 stats.textureCount =
static_cast<uint32_t
>(textures.textures.size());
219 stats.samplerStateCount = ~0u;
220 stats.effectCount =
static_cast<uint32_t
>(effects.effects.size());
221 stats.blendStateCount =
static_cast<uint32_t
>(renderTargets.blendStates.size());
222 stats.rasterizerStateCount =
static_cast<uint32_t
>(renderTargets.rasterizerStates.size());
223 stats.depthStencilStateCount =
static_cast<uint32_t
>(renderTargets.depthStencilStates.size());
224 stats.rendertargetsCount =
static_cast<uint32_t
>(renderTargets.renderTargets.size());
225 stats.framebufferCount = ~0u;
241 if (sharedContext->context.create(
nullptr,
nullptr, &glShareContext, &settings, GLContext::Platform::GL, enableDiagnostics)) {
242 return sharedContext;
244 sharedContext->context.destroy();
245 delete sharedContext;
252 sharedContext->context.makeCurrent();
259 sharedContext->context.destroy();
260 delete sharedContext;
virtual bool initialize() override
Initialises this graphics device.
virtual void activateSharedContext(SharedGraphicsContext *) override
Activates the given shared context on the calling thread.
virtual void endFrame(uint32_t syncInterval=0, uint32_t presentFlags=PresentFlags::None) override
Finalises frame rendering and presents the new frames to the display (if applicable).
virtual SharedGraphicsContext * createSharedContext() override
Create a shared graphics context that can be used to activate another thread for rendering.
virtual void releaseResources() override
Release all resources used by this graphics device.
virtual void beginFrame() override
Prepares the current frame for rendering.
virtual bool getSize(int &w, int &h) const override
Retrieve the current size of this graphics device's default swap chain.
virtual bool setSettings(const GraphicsDeviceSettings &newSettings) override
Updates settings for this graphics device.
virtual void waitForCommandSync() override
Forces the calling thread to synchronise with the GPU.
virtual void deleteSwapChain(ISwapChain *swapChain) override
Delete the specified SwapChain.
virtual void releaseSharedContext(SharedGraphicsContext *) override
Releases the given shared context.
virtual ISwapChain * createSwapChain(struct WindowData *windowData) override
Create a new SwapChain and attach it to the provided window.
virtual void setSize(int width, int height) override
Resizes the output of this graphics device.
virtual ResourceStatistics getResourceStatistics() override
Retrieve statistics about resources currently in use by this graphics device instance.
virtual bool setSettings(const GraphicsDeviceSettings &newSettings)
Updates the settings associated with this device.
Log implementation class.
bool initialize(WindowData *windowData, bool debugLogging)
Initialises this swap chain and attaches it to the window provided.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
@ EnableDiagnostics
If available, the device will print diagnostic information to the log.
@ UseOpenGLClipControl
For OpenGL / OpenGLES backends.
Settings for graphics device initialization.
static const Handle_t NoHandle
Represents a handle to nothing.
@ FireGL
AMD FireGL series.