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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
|
/*
* 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
// implements the abstract Renderer API using OpenGL and the Cg
// Runtime.
#include "core/cross/gl/renderer_gl.h"
#include "core/cross/error.h"
#include "core/cross/gl/buffer_gl.h"
#include "core/cross/gl/draw_element_gl.h"
#include "core/cross/gl/effect_gl.h"
#include "core/cross/gl/param_cache_gl.h"
#include "core/cross/gl/primitive_gl.h"
#include "core/cross/gl/render_surface_gl.h"
#include "core/cross/gl/sampler_gl.h"
#include "core/cross/gl/stream_bank_gl.h"
#include "core/cross/gl/texture_gl.h"
#include "core/cross/gl/utils_gl-inl.h"
#include "core/cross/gl/utils_gl.h"
#include "core/cross/material.h"
#include "core/cross/semantic_manager.h"
#include "core/cross/features.h"
#include "core/cross/shape.h"
#include "core/cross/types.h"
namespace o3d {
namespace {
GLenum ConvertCmpFunc(State::Comparison cmp) {
switch (cmp) {
case State::CMP_ALWAYS:
return GL_ALWAYS;
case State::CMP_NEVER:
return GL_NEVER;
case State::CMP_LESS:
return GL_LESS;
case State::CMP_GREATER:
return GL_GREATER;
case State::CMP_LEQUAL:
return GL_LEQUAL;
case State::CMP_GEQUAL:
return GL_GEQUAL;
case State::CMP_EQUAL:
return GL_EQUAL;
case State::CMP_NOTEQUAL:
return GL_NOTEQUAL;
default:
break;
}
return GL_ALWAYS;
}
GLenum ConvertFillMode(State::Fill mode) {
switch (mode) {
case State::POINT:
return GL_POINT;
case State::WIREFRAME:
return GL_LINE;
case State::SOLID:
return GL_FILL;
default:
break;
}
return GL_FILL;
}
GLenum ConvertBlendFunc(State::BlendingFunction blend_func) {
switch (blend_func) {
case State::BLENDFUNC_ZERO:
return GL_ZERO;
case State::BLENDFUNC_ONE:
return GL_ONE;
case State::BLENDFUNC_SOURCE_COLOR:
return GL_SRC_COLOR;
case State::BLENDFUNC_INVERSE_SOURCE_COLOR:
return GL_ONE_MINUS_SRC_COLOR;
case State::BLENDFUNC_SOURCE_ALPHA:
return GL_SRC_ALPHA;
case State::BLENDFUNC_INVERSE_SOURCE_ALPHA:
return GL_ONE_MINUS_SRC_ALPHA;
case State::BLENDFUNC_DESTINATION_ALPHA:
return GL_DST_ALPHA;
case State::BLENDFUNC_INVERSE_DESTINATION_ALPHA:
return GL_ONE_MINUS_DST_ALPHA;
case State::BLENDFUNC_DESTINATION_COLOR:
return GL_DST_COLOR;
case State::BLENDFUNC_INVERSE_DESTINATION_COLOR:
return GL_ONE_MINUS_DST_COLOR;
case State::BLENDFUNC_SOURCE_ALPHA_SATUTRATE:
return GL_SRC_ALPHA_SATURATE;
default:
break;
}
return GL_ONE;
}
GLenum ConvertBlendEquation(State::BlendingEquation blend_equation) {
switch (blend_equation) {
case State::BLEND_ADD:
return GL_FUNC_ADD;
case State::BLEND_SUBTRACT:
return GL_FUNC_SUBTRACT;
case State::BLEND_REVERSE_SUBTRACT:
return GL_FUNC_REVERSE_SUBTRACT;
case State::BLEND_MIN:
return GL_MIN;
case State::BLEND_MAX:
return GL_MAX;
default:
break;
}
return GL_FUNC_ADD;
}
GLenum ConvertStencilOp(State::StencilOperation stencil_func) {
switch (stencil_func) {
case State::STENCIL_KEEP:
return GL_KEEP;
case State::STENCIL_ZERO:
return GL_ZERO;
case State::STENCIL_REPLACE:
return GL_REPLACE;
case State::STENCIL_INCREMENT_SATURATE:
return GL_INCR;
case State::STENCIL_DECREMENT_SATURATE:
return GL_DECR;
case State::STENCIL_INVERT:
return GL_INVERT;
case State::STENCIL_INCREMENT:
return GL_INCR_WRAP;
case State::STENCIL_DECREMENT:
return GL_DECR_WRAP;
default:
break;
}
return GL_KEEP;
}
// Helper routine that will bind the surfaces stored in the RenderSurface and
// RenderDepthStencilSurface arguments to the current OpenGL context.
// Returns true upon success.
// Note: This routine assumes that a frambuffer object is presently bound
// to the context.
bool InstallFramebufferObjects(const RenderSurface* surface,
const RenderDepthStencilSurface* surface_depth) {
#ifdef _DEBUG
GLint bound_framebuffer;
::glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &bound_framebuffer);
DCHECK(bound_framebuffer != 0);
#endif
// Reset the bound attachments to the current framebuffer object.
::glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT,
0);
::glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT,
0);
::glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_STENCIL_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT,
0);
if (surface) {
const RenderSurfaceGL *gl_surface =
down_cast<const RenderSurfaceGL*>(surface);
Texture *texture = gl_surface->texture();
if (texture->IsA(Texture2D::GetApparentClass())) {
::glFramebufferTexture2DEXT(
GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D,
reinterpret_cast<GLuint>(texture->GetTextureHandle()),
gl_surface->mip_level());
} else if (texture->IsA(TextureCUBE::GetApparentClass())) {
::glFramebufferTexture2DEXT(
GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
gl_surface->cube_face(),
reinterpret_cast<GLuint>(texture->GetTextureHandle()),
gl_surface->mip_level());
}
}
if (surface_depth) {
// Bind both the depth and stencil attachments.
const RenderDepthStencilSurfaceGL* gl_surface =
down_cast<const RenderDepthStencilSurfaceGL*>(surface_depth);
::glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT,
gl_surface->depth_buffer());
::glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_STENCIL_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT,
gl_surface->stencil_buffer());
}
GLenum framebuffer_status = ::glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (GL_FRAMEBUFFER_COMPLETE_EXT != framebuffer_status) {
return false;
}
CHECK_GL_ERROR();
return true;
}
// Helper routine that returns a pointer to the non-NULL entry in the renderer's
// stack of bound surfaces.
const RenderSurfaceBase* GetValidRenderSurface(
const std::pair<RenderSurface*, RenderDepthStencilSurface*> &stack_entry) {
if (stack_entry.first) {
return stack_entry.first;
} else {
return stack_entry.second;
}
}
} // unnamed namespace
// This class wraps StateHandler to make it typesafe.
template <typename T>
class TypedStateHandler : public RendererGL::StateHandler {
public:
// Override this function to set a specific state.
// Parameters:
// renderer: The platform specific renderer.
// param: A concrete param with state data.
virtual void SetStateFromTypedParam(RendererGL* renderer, T* param) const = 0;
// Gets Class of State's Parameter
virtual const ObjectBase::Class* GetClass() const {
return T::GetApparentClass();
}
private:
// Calls SetStateFromTypedParam if the Param type is the correct type.
// Parameters:
// renderer: The platform specific renderer.
// param: A param with state data.
virtual void SetState(Renderer* renderer, Param* param) const {
RendererGL *renderer_gl = down_cast<RendererGL *>(renderer);
// This is safe because State guarntees Params match by type.
DCHECK(param->IsA(T::GetApparentClass()));
SetStateFromTypedParam(renderer_gl, down_cast<T*>(param));
}
};
// A template the generates a handler for enable/disable states.
// Parameters:
// state_constant: GLenum of state we want to enable/disable
template <GLenum state_constant>
class StateEnableHandler : public TypedStateHandler<ParamBoolean> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamBoolean* param) const {
if (param->value()) {
::glEnable(state_constant);
} else {
::glDisable(state_constant);
}
}
};
class BoolHandler : public TypedStateHandler<ParamBoolean> {
public:
explicit BoolHandler(bool* var, bool* changed_var)
: var_(*var),
changed_var_(*changed_var) {
}
virtual void SetStateFromTypedParam(RendererGL *renderer,
ParamBoolean *param) const {
var_ = param->value();
}
private:
bool& var_;
bool& changed_var_;
};
class ZWriteEnableHandler : public TypedStateHandler<ParamBoolean> {
public:
virtual void SetStateFromTypedParam(RendererGL *renderer,
ParamBoolean *param) const {
::glDepthMask(param->value());
}
};
class AlphaReferenceHandler : public TypedStateHandler<ParamFloat> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamFloat* param) const {
float refFloat = param->value();
// cap the float to the required range
if (refFloat < 0.0f) {
refFloat = 0.0f;
} else if (refFloat > 1.0f) {
refFloat = 1.0f;
}
renderer->alpha_function_ref_changed_ = true;
renderer->alpha_ref_ = refFloat;
}
};
class CullModeHandler : public TypedStateHandler<ParamInteger> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
State::Cull cull = static_cast<State::Cull>(param->value());
switch (cull) {
case State::CULL_CW:
::glEnable(GL_CULL_FACE);
::glCullFace(GL_BACK);
break;
case State::CULL_CCW:
::glEnable(GL_CULL_FACE);
::glCullFace(GL_FRONT);
break;
default:
::glDisable(GL_CULL_FACE);
break;
}
}
};
class PolygonOffset1Handler : public TypedStateHandler<ParamFloat> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamFloat* param) const {
renderer->polygon_offset_factor_ = param->value();
renderer->polygon_offset_changed_ = true;
}
};
class PolygonOffset2Handler : public TypedStateHandler<ParamFloat> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamFloat* param) const {
renderer->polygon_offset_bias_ = param->value();
renderer->polygon_offset_changed_ = true;
}
};
class FillModeHandler : public TypedStateHandler<ParamInteger> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
::glPolygonMode(GL_FRONT_AND_BACK,
ConvertFillMode(static_cast<State::Fill>(param->value())));
}
};
class ZFunctionHandler : public TypedStateHandler<ParamInteger> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
::glDepthFunc(
ConvertCmpFunc(static_cast<State::Comparison>(param->value())));
}
};
class BlendEquationHandler : public TypedStateHandler<ParamInteger> {
public:
explicit BlendEquationHandler(GLenum* var)
: var_(*var) {
}
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
renderer->alpha_blend_settings_changed_ = true;
var_ = ConvertBlendEquation(
static_cast<State::BlendingEquation>(param->value()));
}
private:
GLenum& var_;
};
class BlendFunctionHandler : public TypedStateHandler<ParamInteger> {
public:
explicit BlendFunctionHandler(GLenum* var)
: var_(*var) {
}
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
renderer->alpha_blend_settings_changed_ = true;
var_ = ConvertBlendFunc(
static_cast<State::BlendingFunction>(param->value()));
}
private:
GLenum& var_;
};
class StencilOperationHandler : public TypedStateHandler<ParamInteger> {
public:
StencilOperationHandler(int face, int condition)
: face_(face) ,
condition_(condition) {
}
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
renderer->stencil_settings_changed_ = true;
renderer->stencil_settings_[face_].op_[condition_] = ConvertStencilOp(
static_cast<State::StencilOperation>(param->value()));
}
private:
int face_;
int condition_;
};
class ComparisonFunctionHandler : public TypedStateHandler<ParamInteger> {
public:
ComparisonFunctionHandler(GLenum* var, bool* changed_var)
: var_(*var),
changed_var_(*changed_var) {
}
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
changed_var_ = true;
var_ = ConvertCmpFunc(static_cast<State::Comparison>(param->value()));
}
private:
GLenum& var_;
bool& changed_var_;
};
class StencilRefHandler : public TypedStateHandler<ParamInteger> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
renderer->stencil_settings_changed_ = true;
renderer->stencil_ref_ = param->value();
}
};
class StencilMaskHandler : public TypedStateHandler<ParamInteger> {
public:
explicit StencilMaskHandler(int mask_index)
: mask_index_(mask_index) {
}
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
renderer->stencil_settings_changed_ = true;
renderer->stencil_mask_[mask_index_] = param->value();
}
private:
int mask_index_;
};
class ColorWriteEnableHandler : public TypedStateHandler<ParamInteger> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamInteger* param) const {
int mask = param->value();
::glColorMask((mask & 0x1) != 0,
(mask & 0x2) != 0,
(mask & 0x4) != 0,
(mask & 0x8) != 0);
renderer->SetWriteMask(mask);
}
};
class PointSpriteEnableHandler : public TypedStateHandler<ParamBoolean> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamBoolean* param) const {
if (param->value()) {
::glEnable(GL_POINT_SPRITE);
// TODO: It's not clear from D3D docs that point sprites affect
// TEXCOORD0, but that's my guess. Check that.
::glActiveTextureARB(GL_TEXTURE0);
::glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
} else {
::glActiveTextureARB(GL_TEXTURE0);
::glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_FALSE);
::glDisable(GL_POINT_SPRITE);
}
}
};
class PointSizeHandler : public TypedStateHandler<ParamFloat> {
public:
virtual void SetStateFromTypedParam(RendererGL* renderer,
ParamFloat* param) const {
::glPointSize(param->value());
}
};
RendererGL* RendererGL::CreateDefault(ServiceLocator* service_locator) {
return new RendererGL(service_locator);
}
RendererGL::RendererGL(ServiceLocator* service_locator)
: Renderer(service_locator),
semantic_manager_(service_locator),
#ifdef OS_WIN
gl_context_(NULL),
#endif
fullscreen_(0),
#ifdef OS_LINUX
display_(NULL),
window_(0),
context_(0),
#endif
#ifdef OS_MACOSX
mac_agl_context_(0),
mac_cgl_context_(0),
#endif
render_surface_framebuffer_(0),
cg_context_(NULL),
alpha_function_ref_changed_(true),
alpha_function_(GL_ALWAYS),
alpha_ref_(0.f),
alpha_blend_settings_changed_(true),
separate_alpha_blend_enable_(false),
stencil_settings_changed_(true),
separate_stencil_settings_enable_(false),
stencil_ref_(0),
polygon_offset_changed_(true),
polygon_offset_factor_(0.f),
polygon_offset_bias_(0.f) {
DLOG(INFO) << "RendererGL Construct";
// Setup default state values.
for (int ii = 0; ii < 2; ++ii) {
stencil_settings_[ii].func_ = GL_ALWAYS;
stencil_settings_[ii].op_[StencilStates::FAIL_OP] = GL_KEEP;
stencil_settings_[ii].op_[StencilStates::ZFAIL_OP] = GL_KEEP;
stencil_settings_[ii].op_[StencilStates::PASS_OP] = GL_KEEP;
stencil_mask_[ii] = -1;
blend_function_[ii][FRONT] = GL_ONE;
blend_function_[ii][BACK] = GL_ZERO;
blend_equation_[ii] = GL_FUNC_ADD;
}
// Setup state handlers
AddStateHandler(State::kAlphaTestEnableParamName,
new StateEnableHandler<GL_ALPHA_TEST>);
AddStateHandler(State::kAlphaReferenceParamName,
new AlphaReferenceHandler);
AddStateHandler(State::kAlphaComparisonFunctionParamName,
new ComparisonFunctionHandler(&alpha_function_,
&alpha_function_ref_changed_));
AddStateHandler(State::kCullModeParamName,
new CullModeHandler);
AddStateHandler(State::kDitherEnableParamName,
new StateEnableHandler<GL_DITHER>);
AddStateHandler(State::kLineSmoothEnableParamName,
new StateEnableHandler<GL_LINE_SMOOTH>);
AddStateHandler(State::kPointSpriteEnableParamName,
new PointSpriteEnableHandler);
AddStateHandler(State::kPointSizeParamName,
new PointSizeHandler);
AddStateHandler(State::kPolygonOffset1ParamName,
new PolygonOffset1Handler);
AddStateHandler(State::kPolygonOffset2ParamName,
new PolygonOffset2Handler);
AddStateHandler(State::kFillModeParamName,
new FillModeHandler);
AddStateHandler(State::kZEnableParamName,
new StateEnableHandler<GL_DEPTH_TEST>);
AddStateHandler(State::kZWriteEnableParamName,
new ZWriteEnableHandler);
AddStateHandler(State::kZComparisonFunctionParamName,
new ZFunctionHandler);
AddStateHandler(State::kAlphaBlendEnableParamName,
new StateEnableHandler<GL_BLEND>);
AddStateHandler(State::kSourceBlendFunctionParamName,
new BlendFunctionHandler(&blend_function_[SRC][RGB]));
AddStateHandler(State::kDestinationBlendFunctionParamName,
new BlendFunctionHandler(&blend_function_[DST][RGB]));
AddStateHandler(State::kStencilEnableParamName,
new StateEnableHandler<GL_STENCIL_TEST>);
AddStateHandler(State::kStencilFailOperationParamName,
new StencilOperationHandler(FRONT, StencilStates::FAIL_OP));
AddStateHandler(State::kStencilZFailOperationParamName,
new StencilOperationHandler(FRONT, StencilStates::ZFAIL_OP));
AddStateHandler(State::kStencilPassOperationParamName,
new StencilOperationHandler(FRONT, StencilStates::PASS_OP));
AddStateHandler(State::kStencilComparisonFunctionParamName,
new ComparisonFunctionHandler(
&stencil_settings_[FRONT].func_,
&stencil_settings_changed_));
AddStateHandler(State::kStencilReferenceParamName,
new StencilRefHandler);
AddStateHandler(State::kStencilMaskParamName,
new StencilMaskHandler(READ_MASK));
AddStateHandler(State::kStencilWriteMaskParamName,
new StencilMaskHandler(WRITE_MASK));
AddStateHandler(State::kColorWriteEnableParamName,
new ColorWriteEnableHandler);
AddStateHandler(State::kBlendEquationParamName,
new BlendEquationHandler(&blend_equation_[RGB]));
AddStateHandler(State::kTwoSidedStencilEnableParamName,
new BoolHandler(&separate_stencil_settings_enable_,
&stencil_settings_changed_));
AddStateHandler(State::kCCWStencilFailOperationParamName,
new StencilOperationHandler(BACK, StencilStates::FAIL_OP));
AddStateHandler(State::kCCWStencilZFailOperationParamName,
new StencilOperationHandler(BACK, StencilStates::ZFAIL_OP));
AddStateHandler(State::kCCWStencilPassOperationParamName,
new StencilOperationHandler(BACK, StencilStates::PASS_OP));
AddStateHandler(State::kCCWStencilComparisonFunctionParamName,
new ComparisonFunctionHandler(
&stencil_settings_[BACK].func_,
&stencil_settings_changed_));
AddStateHandler(State::kSeparateAlphaBlendEnableParamName,
new BoolHandler(&separate_alpha_blend_enable_,
&alpha_blend_settings_changed_));
AddStateHandler(State::kSourceBlendAlphaFunctionParamName,
new BlendFunctionHandler(&blend_function_[SRC][ALPHA]));
AddStateHandler(State::kDestinationBlendAlphaFunctionParamName,
new BlendFunctionHandler(&blend_function_[DST][ALPHA]));
AddStateHandler(State::kBlendAlphaEquationParamName,
new BlendEquationHandler(&blend_equation_[ALPHA]));
}
RendererGL::~RendererGL() {
Destroy();
}
// platform neutral initialization code
//
Renderer::InitStatus RendererGL::InitCommonGL() {
GLenum glew_error = glewInit();
if (glew_error != GLEW_OK) {
DLOG(ERROR) << "Unable to initialise GLEW : "
<< ::glewGetErrorString(glew_error);
return INITIALIZATION_ERROR;
}
// Check to see that we can use the OpenGL vertex attribute APIs
// TODO: We should return false if this check fails, but because some
// Intel hardware does not support OpenGL 2.0, yet does support all of the
// extensions we require, we only log an error. A future CL should change
// this check to ensure that all of the extension strings we require are
// present.
if (!GLEW_VERSION_2_0) {
DLOG(ERROR) << "GL drivers do not have OpenGL 2.0 functionality.";
}
if (!GLEW_ARB_vertex_buffer_object) {
// NOTE: Linux NVidia drivers claim to support OpenGL 2.0 when using
// indirect rendering (e.g. remote X), but it is actually lying. The
// ARB_vertex_buffer_object functions silently no-op (!) when using
// indirect rendering, leading to crashes. Fortunately, in that case, the
// driver claims to not support ARB_vertex_buffer_object, so fail in that
// case.
DLOG(ERROR) << "GL drivers do not support vertex buffer objects.";
return GPU_NOT_UP_TO_SPEC;
}
if (!GLEW_EXT_framebuffer_object) {
DLOG(ERROR) << "GL drivers do not support framebuffer objects.";
return GPU_NOT_UP_TO_SPEC;
}
SetSupportsNPOT(GLEW_ARB_texture_non_power_of_two != 0);
#ifdef OS_MACOSX
// The Radeon X1600 says it supports NPOT, but in most situations it doesn't.
if (supports_npot() &&
!strcmp("ATI Radeon X1600 OpenGL Engine",
reinterpret_cast<const char*>(::glGetString(GL_RENDERER))))
SetSupportsNPOT(false);
#endif
// Check for necessary extensions
if (!GLEW_VERSION_2_0 && !GLEW_EXT_stencil_two_side) {
DLOG(ERROR) << "Two sided stencil extension missing.";
}
if (!GLEW_VERSION_1_4 && !GLEW_EXT_blend_func_separate) {
DLOG(ERROR) << "Separate blend func extension missing.";
}
if (!GLEW_VERSION_2_0 && !GLEW_EXT_blend_equation_separate) {
DLOG(ERROR) << "Separate blend function extension missing.";
}
// create a Cg Runtime.
cg_context_ = cgCreateContext();
DLOG_CG_ERROR("Creating Cg context");
// NOTE: the first CGerror number after the recreation of a
// CGcontext (the second time through) seems to be trashed. Please
// ignore any "CG ERROR: Invalid context handle." message on this
// function - Invalid context handle isn't one of therror states of
// cgCreateContext().
DLOG(INFO) << "OpenGL Vendor: " << ::glGetString(GL_VENDOR);
DLOG(INFO) << "OpenGL Renderer: " << ::glGetString(GL_RENDERER);
DLOG(INFO) << "OpenGL Version: " << ::glGetString(GL_VERSION);
DLOG(INFO) << "Cg Version: " << cgGetString(CG_VERSION);
cg_vertex_profile_ = cgGLGetLatestProfile(CG_GL_VERTEX);
cgGLSetOptimalOptions(cg_vertex_profile_);
DLOG(INFO) << "Best Cg vertex profile = "
<< cgGetProfileString(cg_vertex_profile_);
cg_fragment_profile_ = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgGLSetOptimalOptions(cg_fragment_profile_);
DLOG(INFO) << "Best Cg fragment profile = "
<< cgGetProfileString(cg_fragment_profile_);
// Set up all Cg State Assignments for OpenGL.
cgGLRegisterStates(cg_context_);
DLOG_CG_ERROR("Registering GL StateAssignments");
cgGLSetDebugMode(CG_FALSE);
// Enable the profiles we use.
cgGLEnableProfile(CG_PROFILE_ARBVP1);
cgGLEnableProfile(CG_PROFILE_ARBFP1);
// get some limits for this profile.
GLint max_vertex_attribs = 0;
::glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs);
DLOG(INFO) << "Max Vertex Attribs = " << max_vertex_attribs;
// Initialize global GL settings.
// Tell GL that texture buffers can be single-byte aligned.
::glPixelStorei(GL_PACK_ALIGNMENT, 1);
::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
::glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
::glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
CHECK_GL_ERROR();
GLint viewport[4];
::glGetIntegerv(GL_VIEWPORT, &viewport[0]);
SetClientSize(viewport[2], viewport[3]);
CHECK_GL_ERROR();
::glGenFramebuffersEXT(1, &render_surface_framebuffer_);
CHECK_GL_ERROR();
return SUCCESS;
}
// platform neutral destruction code
void RendererGL::DestroyCommonGL() {
MakeCurrentLazy();
if (render_surface_framebuffer_) {
::glDeleteFramebuffersEXT(1, &render_surface_framebuffer_);
}
if (cg_context_) {
cgDestroyContext(cg_context_);
cg_context_ = NULL;
}
}
#ifdef OS_WIN
namespace {
PIXELFORMATDESCRIPTOR kPixelFormatDescriptor = {
sizeof(kPixelFormatDescriptor), // Size of structure.
1, // Default version.
PFD_DRAW_TO_WINDOW | // Window drawing support.
PFD_SUPPORT_OPENGL | // OpenGL support.
PFD_DOUBLEBUFFER, // Double buffering support (not stereo).
PFD_TYPE_RGBA, // RGBA color mode (not indexed).
24, // 24 bit color mode.
0, 0, 0, 0, 0, 0, // Don't set RGB bits & shifts.
8, 0, // 8 bit alpha
0, // No accumulation buffer.
0, 0, 0, 0, // Ignore accumulation bits.
24, // 24 bit z-buffer size.
8, // 8-bit stencil buffer.
0, // No aux buffer.
PFD_MAIN_PLANE, // Main drawing plane (not overlay).
0, // Reserved.
0, 0, 0, // Layer masks ignored.
};
LRESULT CALLBACK IntermediateWindowProc(HWND window,
UINT message,
WPARAM w_param,
LPARAM l_param) {
return ::DefWindowProc(window, message, w_param, l_param);
}
// Helper routine that returns the highest quality pixel format supported on
// the current platform. Returns true upon success.
Renderer::InitStatus GetWindowsPixelFormat(HWND window,
Features* features,
int* pixel_format) {
// We must initialize a GL context before we can determine the multi-sampling
// supported on the current hardware, so we create an intermediate window
// and context here.
HINSTANCE module_handle;
if (!::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<wchar_t*>(IntermediateWindowProc),
&module_handle)) {
return Renderer::INITIALIZATION_ERROR;
}
WNDCLASS intermediate_class;
intermediate_class.style = CS_HREDRAW | CS_VREDRAW;
intermediate_class.lpfnWndProc = IntermediateWindowProc;
intermediate_class.cbClsExtra = 0;
intermediate_class.cbWndExtra = 0;
intermediate_class.hInstance = module_handle;
intermediate_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
intermediate_class.hCursor = LoadCursor(NULL, IDC_ARROW);
intermediate_class.hbrBackground = NULL;
intermediate_class.lpszMenuName = NULL;
intermediate_class.lpszClassName = L"Intermediate GL Window";
ATOM class_registration = ::RegisterClass(&intermediate_class);
if (!class_registration) {
return Renderer::INITIALIZATION_ERROR;
}
HWND intermediate_window = ::CreateWindow(
reinterpret_cast<wchar_t*>(class_registration),
L"",
WS_OVERLAPPEDWINDOW,
0, 0,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL);
if (!intermediate_window) {
::UnregisterClass(reinterpret_cast<wchar_t*>(class_registration),
module_handle);
return Renderer::INITIALIZATION_ERROR;
}
HDC intermediate_dc = ::GetDC(intermediate_window);
int format_index = ::ChoosePixelFormat(intermediate_dc,
&kPixelFormatDescriptor);
if (format_index == 0) {
DLOG(ERROR) << "Unable to get the pixel format for GL context.";
::ReleaseDC(intermediate_window, intermediate_dc);
::DestroyWindow(intermediate_window);
::UnregisterClass(reinterpret_cast<wchar_t*>(class_registration),
module_handle);
return Renderer::INITIALIZATION_ERROR;
}
if (!::SetPixelFormat(intermediate_dc, format_index,
&kPixelFormatDescriptor)) {
DLOG(ERROR) << "Unable to set the pixel format for GL context.";
::ReleaseDC(intermediate_window, intermediate_dc);
::DestroyWindow(intermediate_window);
::UnregisterClass(reinterpret_cast<wchar_t*>(class_registration),
module_handle);
return Renderer::INITIALIZATION_ERROR;
}
// Store the pixel format without multisampling.
*pixel_format = format_index;
HGLRC gl_context = ::wglCreateContext(intermediate_dc);
if (::wglMakeCurrent(intermediate_dc, gl_context)) {
// GL context was successfully created and applied to the window's DC.
// Startup GLEW, the GL extensions wrangler.
GLenum glew_error = ::glewInit();
if (glew_error == GLEW_OK) {
DLOG(INFO) << "Initialized GLEW " << ::glewGetString(GLEW_VERSION);
} else {
DLOG(ERROR) << "Unable to initialise GLEW : "
<< ::glewGetErrorString(glew_error);
::wglMakeCurrent(intermediate_dc, NULL);
::wglDeleteContext(gl_context);
::ReleaseDC(intermediate_window, intermediate_dc);
::DestroyWindow(intermediate_window);
::UnregisterClass(reinterpret_cast<wchar_t*>(class_registration),
module_handle);
return Renderer::INITIALIZATION_ERROR;
}
// If the multi-sample extensions are present, query the api to determine
// the pixel format.
if (!features->not_anti_aliased() &&
WGLEW_ARB_pixel_format && WGLEW_ARB_multisample) {
int pixel_attributes[] = {
WGL_SAMPLES_ARB, 4,
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_COLOR_BITS_ARB, 24,
WGL_ALPHA_BITS_ARB, 8,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
0, 0};
float pixel_attributes_f[] = {0, 0};
int msaa_pixel_format;
unsigned int num_formats;
// Query for the highest sampling rate supported, starting at 4x.
static const int kSampleCount[] = {4, 2};
static const int kNumSamples = 2;
for (int sample = 0; sample < kNumSamples; ++sample) {
pixel_attributes[1] = kSampleCount[sample];
if (GL_TRUE == ::wglChoosePixelFormatARB(intermediate_dc,
pixel_attributes,
pixel_attributes_f,
1,
&msaa_pixel_format,
&num_formats)) {
*pixel_format = msaa_pixel_format;
break;
}
}
}
}
::wglMakeCurrent(intermediate_dc, NULL);
::wglDeleteContext(gl_context);
::ReleaseDC(intermediate_window, intermediate_dc);
::DestroyWindow(intermediate_window);
::UnregisterClass(reinterpret_cast<wchar_t*>(class_registration),
module_handle);
return Renderer::SUCCESS;
}
} // unnamed namespace
Renderer::InitStatus RendererGL::InitPlatformSpecific(
const DisplayWindow& display,
bool off_screen) {
const DisplayWindowWindows &display_platform =
static_cast<const DisplayWindowWindows&>(display);
DLOG(INFO) << "RendererGL Init";
// TODO: Add support for off-screen rendering using OpenGL.
if (off_screen) {
return INITIALIZATION_ERROR;
}
int pixel_format;
InitStatus init_status;
init_status = GetWindowsPixelFormat(display_platform.hwnd(),
features(),
&pixel_format);
if (init_status != SUCCESS) {
return init_status;
}
window_ = display_platform.hwnd();
device_context_ = ::GetDC(window_);
if (!::SetPixelFormat(device_context_, pixel_format,
&kPixelFormatDescriptor)) {
DLOG(ERROR) << "Unable to set the pixel format for GL context.";
return INITIALIZATION_ERROR;
}
gl_context_ = ::wglCreateContext(device_context_);
if (MakeCurrent()) {
// Ensure that glew has been initialized for the created rendering context.
init_status = InitCommonGL();
if (init_status != SUCCESS) {
DLOG(ERROR) << "Failed to initialize GL rendering context.";
return init_status;
}
if (WGLEW_ARB_multisample) {
::glEnable(GL_MULTISAMPLE_ARB);
}
} else {
DLOG(ERROR) << "Failed to create the GL Context.";
return INITIALIZATION_ERROR;
}
CHECK_GL_ERROR();
return SUCCESS;
}
// Releases the Cg Context and deletes the GL device.
void RendererGL::Destroy() {
DLOG(INFO) << "Destroy RendererGL";
DestroyCommonGL();
if (device_context_) {
CHECK_GL_ERROR();
// Release the OpenGL rendering context.
::wglMakeCurrent(device_context_, NULL);
if (gl_context_) {
::wglDeleteContext(gl_context_);
gl_context_ = NULL;
}
// release the hDC obtained through GetDC().
::ReleaseDC(window_, device_context_);
device_context_ = NULL;
window_ = NULL;
}
DLOG(INFO) << "Renderer destroyed.";
}
#endif // OS_WIN
#ifdef OS_MACOSX
Renderer::InitStatus RendererGL::InitPlatformSpecific(
const DisplayWindow& display,
bool /*off_screen*/) {
const DisplayWindowMac &display_platform =
static_cast<const DisplayWindowMac&>(display);
// TODO: Add support for off screen rendering on the Mac.
mac_agl_context_ = display_platform.agl_context();
mac_cgl_context_ = display_platform.cgl_context();
return InitCommonGL();
}
void RendererGL::Destroy() {
DestroyCommonGL();
// We only have to destroy agl contexts,
// cgl contexts are not owned by us.
if (mac_agl_context_) {
::aglDestroyContext(mac_agl_context_);
mac_agl_context_ = NULL;
}
}
#endif // OS_MACOSX
#ifdef OS_LINUX
Renderer::InitStatus RendererGL::InitPlatformSpecific(
const DisplayWindow& display_window,
bool off_screen) {
const DisplayWindowLinux &display_platform =
static_cast<const DisplayWindowLinux&>(display_window);
Display *display = display_platform.display();
Window window = display_platform.window();
XWindowAttributes attributes;
::XGetWindowAttributes(display, window, &attributes);
XVisualInfo visual_info_template;
visual_info_template.visualid = ::XVisualIDFromVisual(attributes.visual);
int visual_info_count = 0;
XVisualInfo *visual_info_list = ::XGetVisualInfo(display, VisualIDMask,
&visual_info_template,
&visual_info_count);
DCHECK(visual_info_list);
DCHECK_GT(visual_info_count, 0);
context_ = 0;
for (int i = 0; i < visual_info_count; ++i) {
context_ = ::glXCreateContext(display, visual_info_list + i, 0,
True);
if (context_) break;
}
::XFree(visual_info_list);
if (!context_) {
DLOG(ERROR) << "Couldn't create GL context.";
return INITIALIZATION_ERROR;
}
display_ = display;
window_ = window;
if (!MakeCurrent()) {
::glXDestroyContext(display, context_);
context_ = 0;
display_ = NULL;
window_ = 0;
DLOG(ERROR) << "Couldn't create GL context.";
return INITIALIZATION_ERROR;
}
InitStatus init_status = InitCommonGL();
if (init_status != SUCCESS) {
::glXDestroyContext(display, context_);
context_ = 0;
display_ = NULL;
window_ = 0;
}
return init_status;
}
void RendererGL::Destroy() {
DestroyCommonGL();
if (display_) {
::glXMakeCurrent(display_, 0, 0);
if (context_) {
::glXDestroyContext(display_, context_);
context_ = 0;
}
display_ = NULL;
window_ = 0;
}
}
#endif
bool RendererGL::MakeCurrent() {
#ifdef OS_WIN
if (!device_context_ || !gl_context_) return false;
bool result = ::wglMakeCurrent(device_context_, gl_context_) != 0;
return result;
#endif
#ifdef OS_MACOSX
if (mac_cgl_context_ != NULL) {
::CGLSetCurrentContext(mac_cgl_context_);
return true;
} else if (mac_agl_context_ != NULL) {
::aglSetCurrentContext(mac_agl_context_);
return true;
} else {
return false;
}
#endif
#ifdef OS_LINUX
if (context_ != NULL) {
bool result = ::glXMakeCurrent(display_, window_, context_) == True;
return result;
} else {
return false;
}
#endif
}
void RendererGL::PlatformSpecificClear(const Float4 &color,
bool color_flag,
float depth,
bool depth_flag,
int stencil,
bool stencil_flag) {
MakeCurrentLazy();
::glClearColor(color[0], color[1], color[2], color[3]);
::glClearDepth(depth);
::glClearStencil(stencil);
::glClear((color_flag ? GL_COLOR_BUFFER_BIT : 0) |
(depth_flag ? GL_DEPTH_BUFFER_BIT : 0) |
(stencil_flag ? GL_STENCIL_BUFFER_BIT : 0));
CHECK_GL_ERROR();
}
// Updates the helper constant used for the D3D -> GL remapping.
// See effect_gl.cc for details.
void RendererGL::UpdateHelperConstant(float width, float height) {
MakeCurrentLazy();
// If render-targets are active, pass -1 to invert the Y axis. OpenGL uses
// a different viewport orientation than DX. Without the inversion, the
// output of render-target rendering will be upside down.
if (RenderSurfaceActive()) {
::glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB,
0,
1.0f / width,
-1.0f / height,
2.0f,
-1.0f);
} else {
// Only apply the origin offset when rendering to the client area.
::glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB,
0,
(1.0f + (2.0f * -dest_x_offset())) / width,
(-1.0f + (2.0f * dest_y_offset())) / height,
2.0f,
1.0f);
}
CHECK_GL_ERROR();
}
void RendererGL::SetViewportInPixels(int left,
int top,
int width,
int height,
float min_z,
float max_z) {
MakeCurrentLazy();
int vieport_top =
RenderSurfaceActive() ? top : display_height() - top - height;
::glViewport(left, vieport_top, width, height);
UpdateHelperConstant(static_cast<float>(width), static_cast<float>(height));
// If it's the full client area turn off scissor test for speed.
if (left == 0 &&
top == 0 &&
width == display_width() &&
height == display_height()) {
::glDisable(GL_SCISSOR_TEST);
} else {
::glScissor(left, vieport_top, width, height);
::glEnable(GL_SCISSOR_TEST);
}
::glDepthRange(min_z, max_z);
}
// Resizes the viewport.
void RendererGL::Resize(int width, int height) {
MakeCurrentLazy();
SetClientSize(width, height);
CHECK_GL_ERROR();
}
bool RendererGL::GoFullscreen(const DisplayWindow& display,
int mode_id) {
fullscreen_ = true;
return true;
}
bool RendererGL::CancelFullscreen(const DisplayWindow& display,
int width, int height) {
fullscreen_ = false;
return true;
}
bool RendererGL::PlatformSpecificStartRendering() {
DLOG_FIRST_N(INFO, 10) << "RendererGL StartRendering";
MakeCurrentLazy();
// Currently always returns true.
// Should be modified if current behavior changes.
CHECK_GL_ERROR();
return true;
}
// Clears the color, depth and stncil buffers and prepares GL for rendering
// the frame.
// Returns true on success.
bool RendererGL::PlatformSpecificBeginDraw() {
DLOG_FIRST_N(INFO, 10) << "RendererGL BeginDraw";
MakeCurrentLazy();
// Currently always returns true.
// Should be modified if current behavior changes.
CHECK_GL_ERROR();
return true;
}
// Assign the surface arguments to the renderer, and update the stack
// of pushed surfaces.
void RendererGL::SetRenderSurfacesPlatformSpecific(
const RenderSurface* surface,
const RenderDepthStencilSurface* surface_depth) {
// TODO: This routine re-uses a single global framebuffer object for
// all RenderSurface rendering. Because of the validation checks performed
// at attachment-change time, it may be more performant to create a pool
// of framebuffer objects with different attachment characterists and
// switch between them here.
MakeCurrentLazy();
::glBindFramebufferEXT(GL_FRAMEBUFFER, render_surface_framebuffer_);
if (!InstallFramebufferObjects(surface, surface_depth)) {
O3D_ERROR(service_locator())
<< "Failed to bind OpenGL render target objects:"
<< surface->name() <<", "<< surface_depth->name();
}
// RenderSurface rendering is performed with an inverted Y, so the front
// face winding must be changed to clock-wise. See comments for
// UpdateHelperConstant.
glFrontFace(GL_CW);
}
void RendererGL::SetBackBufferPlatformSpecific() {
MakeCurrentLazy();
// Bind the default context, and restore the default front-face winding.
::glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
glFrontFace(GL_CCW);
}
// Executes a post rendering step
void RendererGL::PlatformSpecificEndDraw() {
DLOG_FIRST_N(INFO, 10) << "RendererGL EndDraw";
DCHECK(IsCurrent());
}
// Swaps the buffers.
void RendererGL::PlatformSpecificFinishRendering() {
DLOG_FIRST_N(INFO, 10) << "RendererGL FinishRendering";
DCHECK(IsCurrent());
::glFlush();
CHECK_GL_ERROR();
}
void RendererGL::PlatformSpecificPresent() {
DLOG_FIRST_N(INFO, 10) << "RendererGL Present";
DCHECK(IsCurrent());
#ifdef OS_WIN
::SwapBuffers(device_context_);
#endif
#ifdef OS_MACOSX
#ifdef USE_AGL_DOUBLE_BUFFER
if (mac_agl_context_) {
::aglSwapBuffers(mac_agl_context_);
}
#endif
#endif
#ifdef OS_LINUX
::glXSwapBuffers(display_, window_);
#endif
}
StreamBank::Ref RendererGL::CreateStreamBank() {
return StreamBank::Ref(new StreamBankGL(service_locator()));
}
Primitive::Ref RendererGL::CreatePrimitive() {
return Primitive::Ref(new PrimitiveGL(service_locator()));
}
DrawElement::Ref RendererGL::CreateDrawElement() {
return DrawElement::Ref(new DrawElementGL(service_locator()));
}
void RendererGL::SetStencilStates(GLenum face,
const StencilStates& stencil_state) {
DCHECK(IsCurrent());
if (face == GL_FRONT_AND_BACK) {
::glStencilFunc(stencil_state.func_,
stencil_ref_,
stencil_mask_[READ_MASK]);
::glStencilOp(stencil_state.op_[StencilStates::FAIL_OP],
stencil_state.op_[StencilStates::ZFAIL_OP],
stencil_state.op_[StencilStates::PASS_OP]);
::glStencilMask(stencil_mask_[WRITE_MASK]);
} else if (GLEW_VERSION_2_0) {
::glStencilFuncSeparate(face,
stencil_state.func_,
stencil_ref_,
stencil_mask_[READ_MASK]);
::glStencilOpSeparate(face,
stencil_state.op_[StencilStates::FAIL_OP],
stencil_state.op_[StencilStates::ZFAIL_OP],
stencil_state.op_[StencilStates::PASS_OP]);
::glStencilMaskSeparate(face,
stencil_mask_[WRITE_MASK]);
} else if (GLEW_EXT_stencil_two_side) {
::glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
::glActiveStencilFaceEXT(face);
::glStencilFunc(stencil_state.func_,
stencil_ref_,
stencil_mask_[READ_MASK]);
::glStencilOp(stencil_state.op_[StencilStates::FAIL_OP],
stencil_state.op_[StencilStates::ZFAIL_OP],
stencil_state.op_[StencilStates::PASS_OP]);
::glStencilMask(stencil_mask_[WRITE_MASK]);
::glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
}
CHECK_GL_ERROR();
}
void RendererGL::ApplyDirtyStates() {
MakeCurrentLazy();
DCHECK(IsCurrent());
// Set blend settings.
if (alpha_blend_settings_changed_) {
if (separate_alpha_blend_enable_) {
if (GLEW_VERSION_1_4) {
::glBlendFuncSeparate(blend_function_[SRC][RGB],
blend_function_[DST][RGB],
blend_function_[SRC][ALPHA],
blend_function_[DST][ALPHA]);
} else if (GLEW_EXT_blend_func_separate) {
::glBlendFuncSeparateEXT(blend_function_[SRC][RGB],
blend_function_[DST][RGB],
blend_function_[SRC][ALPHA],
blend_function_[DST][ALPHA]);
}
if (GLEW_VERSION_2_0) {
::glBlendEquationSeparate(blend_equation_[RGB],
blend_equation_[ALPHA]);
} else if (GLEW_EXT_blend_equation_separate) {
::glBlendEquationSeparateEXT(blend_equation_[RGB],
blend_equation_[ALPHA]);
}
} else {
::glBlendFunc(blend_function_[SRC][RGB],
blend_function_[DST][RGB]);
if (::glBlendEquation != NULL)
::glBlendEquation(blend_equation_[RGB]);
}
alpha_blend_settings_changed_ = false;
}
// Set alpha settings.
if (alpha_function_ref_changed_) {
::glAlphaFunc(alpha_function_, alpha_ref_);
alpha_function_ref_changed_ = false;
}
// Set stencil settings.
if (stencil_settings_changed_) {
if (separate_stencil_settings_enable_) {
SetStencilStates(GL_FRONT, stencil_settings_[FRONT]);
SetStencilStates(GL_BACK, stencil_settings_[BACK]);
} else {
SetStencilStates(GL_FRONT_AND_BACK, stencil_settings_[FRONT]);
}
stencil_settings_changed_ = false;
}
if (polygon_offset_changed_) {
bool enable = (polygon_offset_factor_ != 0.f) ||
(polygon_offset_bias_ != 0.f);
if (enable) {
::glEnable(GL_POLYGON_OFFSET_POINT);
::glEnable(GL_POLYGON_OFFSET_LINE);
::glEnable(GL_POLYGON_OFFSET_FILL);
::glPolygonOffset(polygon_offset_factor_, polygon_offset_bias_);
} else {
::glDisable(GL_POLYGON_OFFSET_POINT);
::glDisable(GL_POLYGON_OFFSET_LINE);
::glDisable(GL_POLYGON_OFFSET_FILL);
}
polygon_offset_changed_ = false;
}
CHECK_GL_ERROR();
}
VertexBuffer::Ref RendererGL::CreateVertexBuffer() {
DLOG(INFO) << "RendererGL CreateVertexBuffer";
MakeCurrentLazy();
return VertexBuffer::Ref(new VertexBufferGL(service_locator()));
}
IndexBuffer::Ref RendererGL::CreateIndexBuffer() {
DLOG(INFO) << "RendererGL CreateIndexBuffer";
MakeCurrentLazy();
return IndexBuffer::Ref(new IndexBufferGL(service_locator()));
}
Effect::Ref RendererGL::CreateEffect() {
DLOG(INFO) << "RendererGL CreateEffect";
MakeCurrentLazy();
return Effect::Ref(new EffectGL(service_locator(), cg_context_));
}
Sampler::Ref RendererGL::CreateSampler() {
return Sampler::Ref(new SamplerGL(service_locator()));
}
ParamCache* RendererGL::CreatePlatformSpecificParamCache() {
return new ParamCacheGL(semantic_manager_.Get(), this);
}
Texture2D::Ref RendererGL::CreatePlatformSpecificTexture2D(
int width,
int height,
Texture::Format format,
int levels,
bool enable_render_surfaces) {
DLOG(INFO) << "RendererGL CreateTexture2D";
MakeCurrentLazy();
return Texture2D::Ref(Texture2DGL::Create(service_locator(),
format,
levels,
width,
height,
enable_render_surfaces));
}
TextureCUBE::Ref RendererGL::CreatePlatformSpecificTextureCUBE(
int edge_length,
Texture::Format format,
int levels,
bool enable_render_surfaces) {
DLOG(INFO) << "RendererGL CreateTextureCUBE";
MakeCurrentLazy();
return TextureCUBE::Ref(TextureCUBEGL::Create(service_locator(),
format,
levels,
edge_length,
enable_render_surfaces));
}
RenderDepthStencilSurface::Ref RendererGL::CreateDepthStencilSurface(
int width,
int height) {
return RenderDepthStencilSurface::Ref(
new RenderDepthStencilSurfaceGL(service_locator(),
width,
height));
}
const int* RendererGL::GetRGBAUByteNSwizzleTable() {
static int swizzle_table[] = { 0, 1, 2, 3, };
return swizzle_table;
}
// This is a factory function for creating Renderer objects. Since
// we're implementing GL, we only ever return a GL renderer.
Renderer* Renderer::CreateDefaultRenderer(ServiceLocator* service_locator) {
return RendererGL::CreateDefault(service_locator);
}
} // namespace o3d
|