-
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathgfxGLDevice.sdl.cpp
More file actions
295 lines (244 loc) · 8.7 KB
/
gfxGLDevice.sdl.cpp
File metadata and controls
295 lines (244 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#if defined( TORQUE_SDL ) && !defined( TORQUE_DEDICATED )
#include "gfx/gfxCubemap.h"
#include "gfx/screenshot.h"
#include "gfx/gl/gfxGLDevice.h"
#include "gfx/gl/gfxGLEnumTranslate.h"
#include "gfx/gl/gfxGLVertexBuffer.h"
#include "gfx/gl/gfxGLPrimitiveBuffer.h"
#include "gfx/gl/gfxGLTextureTarget.h"
#include "gfx/gl/gfxGLWindowTarget.h"
#include "gfx/gl/gfxGLTextureManager.h"
#include "gfx/gl/gfxGLTextureObject.h"
#include "gfx/gl/gfxGLCubemap.h"
#include "gfx/gl/gfxGLCardProfiler.h"
#include "windowManager/sdl/sdlWindow.h"
#include "platform/platformGL.h"
#include "SDL.h"
extern void loadGLCore();
extern void loadGLExtensions(void* context);
void EnumerateVideoModes(Vector<GFXVideoMode>& outModes)
{
int count = SDL_GetNumDisplayModes( 0 );
if( count < 0)
{
AssertFatal(0, "");
return;
}
SDL_DisplayMode mode;
for(int i = 0; i < count; ++i)
{
SDL_GetDisplayMode( 0, i, &mode);
GFXVideoMode outMode;
outMode.resolution.set( mode.w, mode.h );
outMode.refreshRate = mode.refresh_rate;
// BBP = 32 for some reason the engine knows it should be 32, but then we
// add some extra code to break what the engine knows.
//outMode.bitDepth = SDL_BYTESPERPIXEL( mode.format ); // sets bitdepths to 4
//outMode.bitDepth = SDL_BITSPERPIXEL(mode.format); // sets bitdepth to 24
// hardcoded magic numbers ftw
// This value is hardcoded in DX, probably to avoid the shenanigans going on here
outMode.bitDepth = 32;
outMode.wideScreen = (mode.w / mode.h) > (4 / 3);
outMode.fullScreen = true;
outModes.push_back( outMode );
}
}
void GFXGLDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
{
AssertFatal( SDL_WasInit(SDL_INIT_VIDEO), "");
PlatformGL::init(); // for hints about context creation
// Create a dummy window & openGL context so that gl functions can be used here
SDL_Window* tempWindow = SDL_CreateWindow(
"", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN // flags - see below
);
if (!tempWindow)
{
const char* err = SDL_GetError();
Con::printf(err);
AssertFatal(0, err);
return;
}
SDL_ClearError();
U32 debugFlag = 0;
#ifdef TORQUE_DEBUG
debugFlag |= SDL_GL_CONTEXT_DEBUG_FLAG;
#endif
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, debugFlag);
SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
#ifdef TORQUE_GL_SOFTWARE
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 0);
#endif
SDL_GLContext tempContext = SDL_GL_CreateContext( tempWindow );
if( !tempContext )
{
const char *err = SDL_GetError();
Con::printf( err );
AssertFatal(0, err );
return;
}
SDL_ClearError();
SDL_GL_MakeCurrent( tempWindow, tempContext );
const char *err = SDL_GetError();
if( err && err[0] )
{
Con::printf( err );
AssertFatal(0, err );
}
// Init GL
loadGLCore();
loadGLExtensions(tempContext);
//check minimun Opengl 3.3
int major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
if( major < 3 || ( major == 3 && minor < 3 ) )
{
return;
}
// Set our sdl attribute to use this version.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
//check for required extensions
if (!gglHasExtension(ARB_texture_cube_map_array))
{
Con::warnf("Adapater supports OpenGL 3.3 but doesnt support GL_ARB_texture_cube_map_array");
return;
}
if (!gglHasExtension(ARB_gpu_shader5))
{
Con::warnf("Adapater supports OpenGL 3.3 but doesnt support GL_ARB_gpu_shader5");
return;
}
GFXAdapter *toAdd = new GFXAdapter;
toAdd->mIndex = 0;
const char* renderer = (const char*) glGetString( GL_RENDERER );
AssertFatal( renderer != NULL, "GL_RENDERER returned NULL!" );
if (renderer)
{
dStrcpy(toAdd->mName, renderer, GFXAdapter::MaxAdapterNameLen);
dStrcat(toAdd->mName, " OpenGL", GFXAdapter::MaxAdapterNameLen);
}
else
dStrcpy(toAdd->mName, "OpenGL", GFXAdapter::MaxAdapterNameLen);
toAdd->mType = OpenGL;
F32 shaderModel = 3.3f;
if (major == 4)
{
if (minor == 0)
shaderModel = 4.00f; // GLSL 4.00
else if (minor == 1)
shaderModel = 4.10f; // GLSL 4.10
else if (minor == 2)
shaderModel = 4.20f; // GLSL 4.20
else if (minor == 3)
shaderModel = 4.30f; // GLSL 4.30
else if (minor == 4)
shaderModel = 4.40f; // GLSL 4.40
else if (minor == 5)
shaderModel = 4.50f; // GLSL 4.50
else if (minor == 6)
shaderModel = 4.60f; // GLSL 4.60
}
toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;
// Enumerate all available resolutions:
EnumerateVideoModes(toAdd->mAvailableModes);
// Add to the list of available adapters.
adapterList.push_back(toAdd);
// Cleanup window & open gl context
SDL_DestroyWindow( tempWindow );
SDL_GL_DeleteContext( tempContext );
}
void GFXGLDevice::enumerateVideoModes()
{
mVideoModes.clear();
EnumerateVideoModes(mVideoModes);
}
void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
{
AssertFatal(window, "GFXGLDevice::init - no window specified, can't init device without a window!");
PlatformWindowSDL* sdlWindow = dynamic_cast<PlatformWindowSDL*>(window);
AssertFatal(sdlWindow, "Window is not a valid PlatformWindowSDL object");
// Create OpenGL context
mContext = PlatformGL::CreateContextGL( sdlWindow );
PlatformGL::MakeCurrentGL( sdlWindow, mContext );
loadGLCore();
loadGLExtensions(mContext);
// It is very important that extensions be loaded before we call initGLState()
initGLState();
mProjectionMatrix.identity();
mInitialized = true;
deviceInited();
}
bool GFXGLDevice::beginSceneInternal()
{
mCanCurrentlyRender = true;
return true;
}
U32 GFXGLDevice::getTotalVideoMemory()
{
return getTotalVideoMemory_GL_EXT();
}
//------------------------------------------------------------------------------
GFXWindowTarget *GFXGLDevice::allocWindowTarget( PlatformWindow *window )
{
GFXGLWindowTarget* ggwt = new GFXGLWindowTarget(window, this);
//first window
if (!mContext)
{
init(window->getVideoMode(), window);
ggwt->mSecondaryWindow = false;
}
else
ggwt->mSecondaryWindow = true;
ggwt->registerResourceWithDevice(this);
ggwt->mContext = mContext;
return ggwt;
}
GFXFence* GFXGLDevice::_createPlatformSpecificFence()
{
return NULL;
}
//-----------------------------------------------------------------------------
void GFXGLWindowTarget::_WindowPresent()
{
SDL_GL_SwapWindow( static_cast<PlatformWindowSDL*>( getWindow() )->getSDLWindow() );
}
void GFXGLWindowTarget::_teardownCurrentMode()
{
}
void GFXGLWindowTarget::_setupNewMode()
{
}
void GFXGLWindowTarget::_makeContextCurrent()
{
PlatformGL::MakeCurrentGL(mWindow, mContext);
}
#endif