Cogs.Core
GraphicsDeviceGLES30.cpp
1#include "GraphicsDeviceGLES30.h"
2
3#include "FormatsGLES30.h"
4
5#include "Foundation/Platform/WindowData.h"
6
7#include "../Base/Utilities.h"
8
9namespace {
10 Cogs::Logging::Log logger = Cogs::Logging::getLogger("GraphicsDeviceGLES30");
11}
12
13Cogs::GraphicsDeviceGLES30::GraphicsDeviceGLES30(RenderingAllocatorInfo*)
14{
15}
16
17Cogs::GraphicsDeviceGLES30::~GraphicsDeviceGLES30()
18{
19 if (glContext.makeCurrent()) {
20 releaseResources();
21 glContext.destroy();
22 }
23}
24
26{
27 if (reinterpret_cast<void*>(settings.windowData->windowHandle) == nullptr) {
28 LOG_ERROR(logger, "No window handle");
29 return false;
30 }
31 bool enableDiagnostics = isDebug() || ((settings.flags & GraphicsDeviceFlags::EnableDiagnostics) == GraphicsDeviceFlags::EnableDiagnostics);
32
33#ifdef __linux__
34 GLContext::NativeDisplay display = settings.windowData->display;
35#else
36 GLContext::NativeDisplay display = (GLContext::NativeDisplay)nullptr;
37#endif
38 if (!glContext.create(display, settings.windowData, nullptr, &settings, GLContext::Platform::ES3, enableDiagnostics)) {
39 return false;
40 }
41 glContext.makeCurrent();
42
43 bool useClipControl = (settings.flags & GraphicsDeviceFlags::UseOpenGLClipControl) != 0;
44#ifdef __EMSCRIPTEN__
45 if(!has_glClipControl()) useClipControl = false;
46#elif defined(__APPLE__)
47 useClipControl = false;
48#else
49 if(!glClipControl) useClipControl = false;
50#endif
51
52 capabilities.initialize(glContext.platform == GLContext::Platform::GL, useClipControl);
53 buffers.initialize(&capabilities, &context, &effects);
54 textures.initialize(&context, &capabilities);
55 context.initialize(&buffers, &textures, &effects, &renderTargets, &capabilities, &syncObjects);
56 renderTargets.initialize(&capabilities, &textures);
57 effects.initialize(&context);
58
59 if (settings.ioHandler) effects.setIOHandler(settings.ioHandler);
60
61#ifndef __APPLE__
62 if(useClipControl){
63 LOG_DEBUG(logger, "glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE)");
64 glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
65 }
66#endif
67
68 return true;
69}
70
71void Cogs::GraphicsDeviceGLES30::setSize(int width, int height)
72{
73 if (width == this->width && height == this->height) return;
74
75 this->width = width;
76 this->height = height;
77
78 LOG_TRACE(logger, "Resize(%d, %d)", width, height);
79
80 CHECKED(glViewport(0, 0, width, height));
81}
82
84{
85 glContext.makeCurrent();
86 glEnable(GL_DEPTH_TEST);
87
88 context.frameStatisticsBeginFrame();
89 context.setDefaults();
90}
91
92void Cogs::GraphicsDeviceGLES30::endFrame(uint32_t syncInterval, PresentFlags presentFlags)
93{
94 context.clearCachedState();
95 glContext.swapBuffers(syncInterval, presentFlags);
96}
97
99{
100 effects.releaseResources();
101 renderTargets.releaseResources();
102 textures.releaseResources();
103 buffers.releaseResources();
104}
105
106Cogs::ResourceStatistics Cogs::GraphicsDeviceGLES30::getResourceStatistics()
107{
108 ResourceStatistics stats{};
109
110 stats.bufferMemoryConsumption = buffers.bufferMemoryConsumption;
111 stats.textureMemoryConsumption = textures.textureMemoryConsumption;
112 stats.bufferCount = static_cast<uint32_t>(buffers.buffers.size());
113 stats.vertexArrayObjectCount = static_cast<uint32_t>(buffers.vertexArrayObjects.size());
114 stats.inputLayoutCount = static_cast<uint32_t>(buffers.inputLayouts.size());
115 stats.textureCount = static_cast<uint32_t>(textures.textures.size());
116 stats.samplerStateCount = 0;// static_cast<uint32_t>(textures.samplerStates.size());
117 stats.effectCount = static_cast<uint32_t>(effects.effects.size());
118 stats.blendStateCount = static_cast<uint32_t>(renderTargets.blendStates.size());
119 stats.rasterizerStateCount = static_cast<uint32_t>(renderTargets.rasterizerStates.size());
120 stats.depthStencilStateCount = static_cast<uint32_t>(renderTargets.depthStencilStates.size());
121 stats.rendertargetsCount = static_cast<uint32_t>(renderTargets.renderTargets.size());
122 stats.framebufferCount = 0;// static_cast<uint32_t>(renderTargets.framebuffers.size());
123
124 return stats;
125}
Log implementation class.
Definition: LogManager.h:140
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:181
PresentFlags
Flags controlling presentation.
Definition: Common.h:166
@ EnableDiagnostics
If available, the device will print diagnostic information to the log.
@ UseOpenGLClipControl
For OpenGL / OpenGLES backends.
bool initialize()
Initializes the graphics device with the settings previous set through calling setSettings.
void setSize(int width, int height) override
Set the size of the main drawing buffer used by the graphics device in pixels.
void endFrame(uint32_t syncInterval=0, PresentFlags presentFlags=PresentFlags::None) override
Signal the end of a frame to the graphics device.
void releaseResources() override
Release all resources allocated.
void beginFrame() override
Signal the beginning of a new frame to the graphics device.