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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Copyright 2009, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This file contains the definition of the RendererGL class that provides
// low-level access for O3D to graphics hardware using the OpenGL API
// and Cg Runtime.
#ifndef O3D_CORE_CROSS_GL_RENDERER_GL_H_
#define O3D_CORE_CROSS_GL_RENDERER_GL_H_
#include <Cg/cg.h>
#include <build/build_config.h>
#include "core/cross/renderer.h"
#include "core/cross/renderer_platform.h"
#include "core/cross/types.h"
#include "core/cross/state.h"
namespace o3d {
class Material;
class Effect;
class DrawEffect;
class SemanticManager;
// Implements the genereric Renderer interface using OpenGL and the Cg Runtime.
class RendererGL : public Renderer {
public:
// Creates a default Renderer.
static RendererGL* CreateDefault(ServiceLocator* service_locator);
virtual ~RendererGL();
// Initialises the renderer for use, claiming hardware resources.
virtual InitStatus InitPlatformSpecific(const DisplayWindow& display,
bool off_screen);
// Released all hardware resources.
virtual void Destroy();
// Overridden from Renderer.
virtual bool GoFullscreen(const DisplayWindow& display,
int mode_id);
// Overridden from Renderer.
virtual bool CancelFullscreen(const DisplayWindow& display,
int width, int height);
// Tells whether we're currently displayed fullscreen or not.
virtual bool fullscreen() const {
return fullscreen_;
}
// Get a vector of the available fullscreen display modes.
// Clears *modes on error.
virtual void GetDisplayModes(std::vector<DisplayMode> *modes);
// Get a single fullscreen display mode by id.
// Returns true on success, false on error.
virtual bool GetDisplayMode(int id, DisplayMode *mode);
// Resizes the viewport in OpenGL.
virtual void Resize(int width, int height);
// Creates a StreamBank, returning a platform specific implementation class.
virtual StreamBank::Ref CreateStreamBank();
// Creates a Primitive, returning a platform specific implementation class.
virtual Primitive::Ref CreatePrimitive();
// Creates a DrawElement, returning a platform specific implementation
// class.
virtual DrawElement::Ref CreateDrawElement();
// Creates and returns a GL specific float buffer.
virtual VertexBuffer::Ref CreateVertexBuffer();
// Creates and returns a GL specific integer buffer.
virtual IndexBuffer::Ref CreateIndexBuffer();
// Creates and returns a GL specific Effect object.
virtual Effect::Ref CreateEffect();
// Creates and returns a GL specific Sampler object.
virtual Sampler::Ref CreateSampler();
// Creates and returns a platform-specific RenderDepthStencilSurface object
// for use as a depth-stencil render target.
virtual RenderDepthStencilSurface::Ref CreateDepthStencilSurface(
int width,
int height);
// Overridden from Renderer.
virtual const int* GetRGBAUByteNSwizzleTable();
// Makes this renderer active on the current thread if it is not active
// already.
void MakeCurrentLazy() {
if (!IsCurrent())
MakeCurrent();
}
// Returns whether or not this renderer is active on the current thread.
// Don't worry, the "get" calls are el cheapo.
bool IsCurrent() {
#if defined(OS_MACOSX)
if ((mac_agl_context_ != NULL) &&
(mac_agl_context_ == aglGetCurrentContext())) {
return true;
} else if ((mac_cgl_context_ != NULL) &&
(mac_cgl_context_ == CGLGetCurrentContext())) {
return true;
}
#elif defined(OS_WIN)
if ((gl_context_ != NULL) &&
(gl_context_ == wglGetCurrentContext())) {
return true;
}
#elif defined(OS_LINUX)
if ((context_ != NULL) &&
(context_ == glXGetCurrentContext())) {
return true;
}
#else
Error: must port RendererGL::IsCurrent() to your platform.
#endif
return false;
}
// Makes this renderer active on the current thread.
bool MakeCurrent();
inline CGcontext cg_context() const { return cg_context_; }
inline CGprofile cg_vertex_profile() const { return cg_vertex_profile_; }
inline CGprofile cg_fragment_profile() const { return cg_fragment_profile_; }
protected:
// Keep the constructor protected so only factory methods can create
// renderers.
explicit RendererGL(ServiceLocator* service_locator);
// Overridden from Renderer.
virtual bool PlatformSpecificBeginDraw();
// Overridden from Renderer.
virtual void PlatformSpecificEndDraw();
// Overridden from Renderer.
virtual bool PlatformSpecificStartRendering();
// Overridden from Renderer.
virtual void PlatformSpecificFinishRendering();
// Overridden from Renderer.
virtual void PlatformSpecificPresent();
// Overridden from Renderer.
virtual void PlatformSpecificClear(const Float4 &color,
bool color_flag,
float depth,
bool depth_flag,
int stencil,
bool stencil_flag);
// Overridden from Renderer.
virtual ParamCache* CreatePlatformSpecificParamCache();
// Sets the viewport. This is the platform specific version.
void SetViewportInPixels(int left,
int top,
int width,
int height,
float min_z,
float max_z);
// Overridden from Renderer.
virtual void SetBackBufferPlatformSpecific();
// Overridden from Renderer.
virtual void SetRenderSurfacesPlatformSpecific(
const RenderSurface* surface,
const RenderDepthStencilSurface* depth_surface);
// Overridden from Renderer.
virtual Texture2D::Ref CreatePlatformSpecificTexture2D(
int width,
int height,
Texture::Format format,
int levels,
bool enable_render_surfaces);
// Overridden from Renderer.
virtual TextureCUBE::Ref CreatePlatformSpecificTextureCUBE(
int edge_length,
Texture::Format format,
int levels,
bool enable_render_surfaces);
// Overridden from Renderer.
virtual void ApplyDirtyStates();
private:
// Platform-independent GL initialization
InitStatus InitCommonGL();
// Platform-independent GL destruction
void DestroyCommonGL();
// Updates the helper constant used to remap D3D clip coordinates to GL ones.
void UpdateHelperConstant(float width, float height);
ServiceDependency<SemanticManager> semantic_manager_;
// Indicates we're rendering fullscreen rather than in the plugin region.
bool fullscreen_;
#ifdef OS_WIN
// Handle to the GL device.
HWND window_;
HDC device_context_;
HGLRC gl_context_;
#endif
#ifdef OS_MACOSX
AGLContext mac_agl_context_;
CGLContextObj mac_cgl_context_;
#endif
#ifdef OS_LINUX
Display *display_;
Window window_;
GLXContext context_;
#endif
// Handle to the framebuffer-object used while rendering to off-screen
// targets.
GLuint render_surface_framebuffer_;
// Cg Runtime variables.
CGcontext cg_context_;
CGprofile cg_vertex_profile_;
CGprofile cg_fragment_profile_;
friend class AlphaReferenceHandler;
bool alpha_function_ref_changed_;
GLenum alpha_function_;
GLclampf alpha_ref_;
friend class BlendFunctionHandler;
friend class BlendEquationHandler;
bool alpha_blend_settings_changed_;
bool separate_alpha_blend_enable_;
enum {
RGB,
ALPHA,
};
enum {
SRC,
DST,
};
GLenum blend_function_[2][2]; // SRC/DST, RGB/ALPHA
GLenum blend_equation_[2]; // RGB/ALPHA
bool stencil_settings_changed_;
bool separate_stencil_settings_enable_;
// States for Stencils
friend class StencilOperationHandler;
friend class StencilRefHandler;
friend class StencilMaskHandler;
struct StencilStates {
GLenum func_;
enum {
FAIL_OP,
ZFAIL_OP,
PASS_OP,
};
int op_[3];
};
enum {
FRONT,
BACK,
};
StencilStates stencil_settings_[2];
enum {
READ_MASK,
WRITE_MASK,
};
int stencil_mask_[2];
int stencil_ref_;
// States for PolygonOffset
friend class PolygonOffset1Handler;
friend class PolygonOffset2Handler;
bool polygon_offset_changed_;
float polygon_offset_factor_;
float polygon_offset_bias_;
// Sets the stencils states for either front, back or both facing polys.
void SetStencilStates(GLenum face, const StencilStates& stencil_states);
};
} // namespace o3d
#endif // O3D_CORE_CROSS_GL_RENDERER_GL_H_
|