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
/*!
Easy-to-use, high-level, OpenGL3+ wrapper.

Glium is based on glutin - a cross-platform crate for building an OpenGL window and handling
application events.

Glium provides a **Display** which extends the **glutin::WindowedContext** with a high-level, safe API.

# Initialization

The initialisation of a glium display occurs in several steps.

```no_run
extern crate glium;

fn main() {
    // 1. The **winit::EventsLoop** for handling events.
    let mut events_loop = glium::glutin::event_loop::EventLoop::new();
    // 2. Parameters for building the Window.
    let wb = glium::glutin::window::WindowBuilder::new()
        .with_inner_size(glium::glutin::dpi::LogicalSize::new(1024.0, 768.0))
        .with_title("Hello world");
    // 3. Parameters for building the OpenGL context.
    let cb = glium::glutin::ContextBuilder::new();
    // 4. Build the Display with the given window and OpenGL context parameters and register the
    //    window with the events_loop.
    let display = glium::Display::new(wb, cb, &events_loop).unwrap();
}
```

The `display` object is the most important object of this library and is used when you build
buffers, textures, etc. and when you draw.

You can clone it and pass it around. However it doesn't implement the `Send` and `Sync` traits,
meaning that you can't pass it to another thread.

The display has ownership of both the window and context, and also provides some methods related to
domains such as events handling.

# Overview

OpenGL is similar to a drawing software: you draw something, then draw over it, then over it
again, etc. until you are satisfied of the result.

Once you have a `display`, you can call `let mut frame = display.draw();` to start drawing. This
`frame` object implements [the `Surface` trait](trait.Surface.html) and provides some functions
such as `clear_color`, but also allows you to draw with the rendering pipeline.

In order to draw something, you will need to pass:

 - A source of vertices (see the [`vertex`](vertex/index.html) module)
 - A source of indices (see the [`index`](index/index.html) module)
 - A program that contains the shader that the GPU will execute (see the
   [`program`](program/index.html) module)
 - A list of uniforms for the program (see the [`uniforms`](uniforms/index.html) module)
 - Draw parameters to customize the drawing process (see the
   [`draw_parameters`](draw_parameters/index.html) module)

Once you have finished drawing, you can call `frame.finish()` to swap buffers and present the
result to the user.

# OpenGL equivalents in glium

 - **Bind points**: Glium automatically binds and unbinds buffers, textures, etc. in an optimized
   way.
 - **Buffers**: Buffers are strongly typed and can be used through `vertex::VertexBuffer`,
   `index::IndexBuffer` or `uniforms::UniformBuffer`.
 - **Debug output**: If you compile in debug mode, glium registers a debug output callback and
   panics if an OpenGL error happens.
 - **Framebuffer Objects**: FBOs are automatically managed by glium and are stored in the `Context`
   object. You can specify the attachments that you wish with the `framebuffer` module.
 - **Instancing**: Instancing is done either by passing a `vertex::EmptyInstanceAttributes` marker
   or one or several references to vertex buffers wrapped inside a `PerInstance` struct. See the
   `vertex` module for more infos.
 - **Memory barriers**: Calling `glMemoryBarrier` is automatically handled by glium, however you
   still need to call `memoryBarrier()` in your GLSL code in some situations.
 - **Programs**: See the `program` module.
 - **Query objects**: The corresponding structs are in the `draw_parameters` module. They are
   passed as draw parameters.
 - **Renderbuffer**: See the `framebuffer` module.
 - **Render to texture**: If you just want to draw on a texture, you can call
   `texture.as_surface()`. For more advanced options, see the `framebuffer` module.
 - **Samplers**: Samplers are automatically managed by glium and are stored in the `Context`
   object. You can specify how a texture should be sampled by using a `Sampler` dummy object
   in the `uniforms` module.
 - **Shaders**: You can't manually create individual shaders. Instead you must create whole
   programs at once.
 - **Textures**: Textures are strongly typed and are found in the `texture` module.
 - **Uniform blocks**: If your program uses uniform blocks, you must pass a reference to a
   uniform buffer for the name of the block when drawing.
 - **Vertex array objects**: VAOs are automatically managed by glium if the backend supports them.

*/
#![warn(missing_docs)]

// TODO: remove these when everything is implemented
#![allow(dead_code)]
#![allow(unused_variables)]

#[macro_use]
extern crate lazy_static;

extern crate memoffset;
extern crate backtrace;
extern crate smallvec;
extern crate fnv;
extern crate takeable_option;

#[cfg(feature = "glutin")]
pub use backend::glutin::glutin;
pub use context::Profile;
pub use draw_parameters::{Blend, BlendingFunction, LinearBlendingFactor, BackfaceCullingMode};
pub use draw_parameters::{Depth, DepthTest, PolygonMode, DrawParameters, StencilTest, StencilOperation};
pub use draw_parameters::{Smooth};
pub use index::IndexBuffer;
pub use vertex::{VertexBuffer, Vertex, VertexFormat};
pub use program::{Program, ProgramCreationError};
pub use program::ProgramCreationError::{CompilationError, LinkingError, ShaderTypeNotSupported};
pub use sync::{LinearSyncFence, SyncFence};
pub use texture::Texture2d;
pub use version::{Api, Version, get_supported_glsl_version};
pub use ops::ReadError;

use std::rc::Rc;
use std::thread;
use std::error::Error;
use std::fmt;
use std::hash::BuildHasherDefault;
use std::collections::HashMap;

use fnv::FnvHasher;

use context::Context;
use context::CommandContext;

#[macro_use]
mod macros;

pub mod backend;
pub mod buffer;
pub mod debug;
pub mod draw_parameters;
pub mod framebuffer;
pub mod index;
pub mod pixel_buffer;
pub mod program;
pub mod uniforms;
pub mod vertex;
pub mod texture;
pub mod field;

mod context;
mod fbo;
mod image_format;
mod ops;
mod sampler_object;
mod sync;
mod utils;
mod version;
mod vertex_array_object;

mod gl {
    include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
}

#[doc(hidden)]
pub use memoffset::offset_of as __glium_offset_of;

/// The main object of this library. Controls the whole display.
///
/// This object contains a smart pointer to the real implementation.
/// Cloning the display allows you to easily share the `Display` object throughout
/// your program.
#[cfg(feature = "glutin")]
pub use backend::glutin::Display;
#[cfg(feature = "glutin")]
pub use backend::glutin::headless::Headless as HeadlessRenderer;

/// Trait for objects that describe the capabilities of an OpenGL backend.
pub trait CapabilitiesSource {
    /// Returns the version of the backend.
    fn get_version(&self) -> &version::Version;

    /// Returns the list of extensions that are supported.
    fn get_extensions(&self) -> &context::ExtensionsList;

    /// Returns the capabilities of the backend.
    fn get_capabilities(&self) -> &context::Capabilities;
}

/// Trait for objects that are OpenGL objects.
pub trait GlObject {
    /// The type of identifier for this object.
    type Id;

    /// Returns the id of the object.
    fn get_id(&self) -> Self::Id;
}

/// Handle to a shader or a program.
// TODO: Handle(null()) is equal to Id(0)
#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
pub enum Handle {
    /// A numeric identifier.
    Id(gl::types::GLuint),
    /// A `GLhandleARB`.
    Handle(gl::types::GLhandleARB),
}

unsafe impl Send for Handle {}

/// Internal trait for enums that can be turned into GLenum.
trait ToGlEnum {
    /// Returns the value.
    fn to_glenum(&self) -> gl::types::GLenum;
}

/// Internal trait for subbuffers.
trait BufferExt {
    /// Returns the number of bytes from the start of the buffer to this subbuffer.
    fn get_offset_bytes(&self) -> usize;

    /// Calls `glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT)` if necessary.
    fn prepare_for_vertex_attrib_array(&self, &mut CommandContext);

    /// Calls `glMemoryBarrier(ELEMENT_ARRAY_BARRIER_BIT)` if necessary.
    fn prepare_for_element_array(&self, &mut CommandContext);

    /// Binds the buffer to `GL_ELEMENT_ARRAY_BUFFER` regardless of the current vertex array object.
    fn bind_to_element_array(&self, &mut CommandContext);

    /// Makes sure that the buffer is bound to the `GL_PIXEL_PACK_BUFFER` and calls
    /// `glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT)` if necessary.
    fn prepare_and_bind_for_pixel_pack(&self, &mut CommandContext);

    /// Makes sure that nothing is bound to `GL_PIXEL_PACK_BUFFER`.
    fn unbind_pixel_pack(&mut CommandContext);

    /// Makes sure that the buffer is bound to the `GL_PIXEL_UNPACK_BUFFER` and calls
    /// `glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT)` if necessary.
    fn prepare_and_bind_for_pixel_unpack(&self, &mut CommandContext);

    /// Makes sure that nothing is bound to `GL_PIXEL_UNPACK_BUFFER`.
    fn unbind_pixel_unpack(&mut CommandContext);

    /// Makes sure that the buffer is bound to the `GL_QUERY_BUFFER` and calls
    /// `glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT)` if necessary.
    fn prepare_and_bind_for_query(&self, &mut CommandContext);

    /// Makes sure that nothing is bound to `GL_QUERY_BUFFER`.
    fn unbind_query(&mut CommandContext);

    /// Makes sure that the buffer is bound to the `GL_DRAW_INDIRECT_BUFFER` and calls
    /// `glMemoryBarrier(GL_COMMAND_BARRIER_BIT)` if necessary.
    fn prepare_and_bind_for_draw_indirect(&self, &mut CommandContext);

    /// Makes sure that the buffer is bound to the `GL_DISPATCH_INDIRECT_BUFFER` and calls
    /// `glMemoryBarrier(GL_COMMAND_BARRIER_BIT)` if necessary.
    fn prepare_and_bind_for_dispatch_indirect(&self, &mut CommandContext);

    /// Makes sure that the buffer is bound to the indexed `GL_UNIFORM_BUFFER` point and calls
    /// `glMemoryBarrier(GL_UNIFORM_BARRIER_BIT)` if necessary.
    fn prepare_and_bind_for_uniform(&self, &mut CommandContext, index: gl::types::GLuint);

    /// Makes sure that the buffer is bound to the indexed `GL_SHARED_STORAGE_BUFFER` point and calls
    /// `glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT)` if necessary.
    fn prepare_and_bind_for_shared_storage(&self, &mut CommandContext, index: gl::types::GLuint);

    /// Makes sure that the buffer is bound to the indexed `GL_ATOMIC_COUNTER_BUFFER` point and calls
    /// `glMemoryBarrier(GL_ATOMIC_COUNTER_BARRIER_BIT)` if necessary.
    fn prepare_and_bind_for_atomic_counter(&self, &mut CommandContext, index: gl::types::GLuint);

    /// Binds the buffer to `GL_TRANSFORM_FEEDBACk_BUFFER` regardless of the current transform
    /// feedback object.
    fn bind_to_transform_feedback(&self, &mut CommandContext, index: gl::types::GLuint);
}

/// Internal trait for subbuffer slices.
trait BufferSliceExt<'a> {
    /// Tries to get an object where to write a fence.
    ///
    /// If this function returns `None`, no fence will be created nor written.
    fn add_fence(&self) -> Option<buffer::Inserter<'a>>;
}

/// Internal trait for contexts.
trait ContextExt {
    /// Sets whether the context's debug output callback should take errors into account.
    fn set_report_debug_output_errors(&self, value: bool);

    /// Start executing OpenGL commands by checking the current context.
    fn make_current(&self) -> context::CommandContext;

    /// Returns the capabilities of the backend.
    fn capabilities(&self) -> &context::Capabilities;
}

/// Internal trait for programs.
trait ProgramExt {
    /// Calls `glUseProgram` and enables/disables `GL_PROGRAM_POINT_SIZE` and
    /// `GL_FRAMEBUFFER_SRGB`.
    fn use_program(&self, ctxt: &mut context::CommandContext);

    /// Changes the value of a uniform of the program.
    fn set_uniform(&self, ctxt: &mut context::CommandContext, uniform_location: gl::types::GLint,
                   value: &RawUniformValue);

    /// Changes the uniform block binding of the program.
    fn set_uniform_block_binding(&self, ctxt: &mut context::CommandContext,
                                 block_location: gl::types::GLuint, value: gl::types::GLuint);

    /// Changes the shader storage block binding of the program.
    fn set_shader_storage_block_binding(&self, ctxt: &mut context::CommandContext,
                                        block_location: gl::types::GLuint,
                                        value: gl::types::GLuint);

    /// Changes the subroutine uniform bindings of a program.
    fn set_subroutine_uniforms_for_stage(&self, ctxt: &mut context::CommandContext,
                                         stage: program::ShaderStage,
                                         indices: &[gl::types::GLuint]);

    fn get_uniform(&self, name: &str) -> Option<&program::Uniform>;

    fn get_uniform_blocks(&self) -> &HashMap<String, program::UniformBlock, BuildHasherDefault<FnvHasher>>;

    fn get_shader_storage_blocks(&self) -> &HashMap<String, program::UniformBlock, BuildHasherDefault<FnvHasher>>;

    fn get_atomic_counters(&self) -> &HashMap<String, program::UniformBlock, BuildHasherDefault<FnvHasher>>;

    fn get_subroutine_data(&self) -> &program::SubroutineData;
}

/// Internal trait for queries.
trait QueryExt {
    fn begin_query(&self, ctxt: &mut CommandContext) -> Result<(), DrawError>;

    fn end_samples_passed_query(ctxt: &mut CommandContext);

    fn end_time_elapsed_query(ctxt: &mut CommandContext);

    fn end_primitives_generated_query(ctxt: &mut CommandContext);

    fn end_transform_feedback_primitives_written_query(ctxt: &mut CommandContext);

    fn begin_conditional_render(&self, ctxt: &mut CommandContext, wait: bool, per_region: bool);

    fn end_conditional_render(ctxt: &mut CommandContext);

    /// Returns true if the query has never been used.
    fn is_unused(&self) -> bool;
}

/// Internal trait for textures.
trait TextureExt {
    /// Returns the ID of the texture.
    fn get_texture_id(&self) -> gl::types::GLuint;

    /// Returns the context associated to this texture.
    fn get_context(&self) -> &Rc<Context>;

    /// Returns the bind point of the texture.
    fn get_bind_point(&self) -> gl::types::GLenum;

    /// Makes sure that the texture is bound to the current texture unit and returns the
    /// bind point to use to access the texture (eg. `GL_TEXTURE_2D`, `GL_TEXTURE_3D`, etc.).
    fn bind_to_current(&self, &mut CommandContext) -> gl::types::GLenum;
}

/// Internal trait for textures.
trait TextureMipmapExt {
    /// Changes some parts of the texture.
    fn upload_texture<'a, P>(&self, x_offset: u32, y_offset: u32, z_offset: u32,
                             (image_format::ClientFormatAny, std::borrow::Cow<'a, [P]>), width: u32,
                             height: Option<u32>, depth: Option<u32>,
                             regen_mipmaps: bool)
                             -> Result<(), ()>   // TODO return a better Result!?
                             where P: Send + Copy + Clone + 'a;

    fn download_compressed_data(&self) -> Option<(image_format::ClientFormatAny, Vec<u8>)>;
}

/// Internal trait for transform feedback sessions.
trait TransformFeedbackSessionExt {
    /// Updates the state of OpenGL to make the transform feedback session current.
    ///
    /// The second parameter must be the primitive type of the input vertex data.
    fn bind(&self, &mut CommandContext, index::PrimitiveType);

    /// Ensures that transform feedback is disabled.
    fn unbind(&mut CommandContext);

    /// Ensures that a buffer isn't used by transform feedback.
    fn ensure_buffer_out_of_transform_feedback(&mut CommandContext, gl::types::GLuint);
}

/// Internal trait for uniforms handling.
trait UniformsExt {
    /// Binds the uniforms to a given program.
    ///
    /// Will replace texture and buffer bind points.
    fn bind_uniforms<'a, P>(&'a self, &mut CommandContext, &P, &mut Vec<buffer::Inserter<'a>>)
                            -> Result<(), DrawError> where P: ProgramExt;
}


/// A raw value of a uniform. "Raw" means that it's passed directly with `glUniform`. Textures
/// for example are just passed as integers.
///
/// Blocks and subroutines are not included.
#[derive(Copy, Clone, Debug)]
#[allow(missing_docs)]
pub enum RawUniformValue {
    SignedInt(gl::types::GLint),
    UnsignedInt(gl::types::GLuint),
    Float(gl::types::GLfloat),
    /// 2x2 column-major matrix.
    Mat2([[gl::types::GLfloat; 2]; 2]),
    /// 3x3 column-major matrix.
    Mat3([[gl::types::GLfloat; 3]; 3]),
    /// 4x4 column-major matrix.
    Mat4([[gl::types::GLfloat; 4]; 4]),
    Vec2([gl::types::GLfloat; 2]),
    Vec3([gl::types::GLfloat; 3]),
    Vec4([gl::types::GLfloat; 4]),
    IntVec2([gl::types::GLint; 2]),
    IntVec3([gl::types::GLint; 3]),
    IntVec4([gl::types::GLint; 4]),
    UnsignedIntVec2([gl::types::GLuint; 2]),
    UnsignedIntVec3([gl::types::GLuint; 3]),
    UnsignedIntVec4([gl::types::GLuint; 4]),

    // Double precision primitives
    Double(gl::types::GLdouble),
    DoubleMat2([[gl::types::GLdouble; 2]; 2]),
    DoubleMat3([[gl::types::GLdouble; 3]; 3]),
    DoubleMat4([[gl::types::GLdouble; 4]; 4]),
    DoubleVec2([gl::types::GLdouble; 2]),
    DoubleVec3([gl::types::GLdouble; 3]),
    DoubleVec4([gl::types::GLdouble; 4]),
    Int64(gl::types::GLint64),
    Int64Vec2([gl::types::GLint64; 2]),
    Int64Vec3([gl::types::GLint64; 3]),
    Int64Vec4([gl::types::GLint64; 4]),
    UnsignedInt64(gl::types::GLuint64),
    UnsignedInt64Vec2([gl::types::GLuint64; 2]),
    UnsignedInt64Vec3([gl::types::GLuint64; 3]),
    UnsignedInt64Vec4([gl::types::GLuint64; 4]),
}

/// Area of a surface in pixels.
///
/// In the OpenGL ecosystem, the (0,0) coordinate is at the bottom-left hand corner of the images.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Rect {
    /// Number of pixels between the left border of the surface and the left border of
    /// the rectangle.
    pub left: u32,
    /// Number of pixels between the bottom border of the surface and the bottom border
    /// of the rectangle.
    pub bottom: u32,
    /// Width of the area in pixels.
    pub width: u32,
    /// Height of the area in pixels.
    pub height: u32,
}

/// Area of a surface in pixels. Similar to a `Rect` except that dimensions can be negative.
///
/// In the OpenGL ecosystem, the (0,0) coordinate is at the bottom-left hand corner of the images.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BlitTarget {
    /// Number of pixels between the left border of the surface and the left border of
    /// the rectangle.
    pub left: u32,
    /// Number of pixels between the bottom border of the surface and the bottom border
    /// of the rectangle.
    pub bottom: u32,
    /// Width of the area in pixels. Can be negative.
    pub width: i32,
    /// Height of the area in pixels. Can be negative.
    pub height: i32,
}

/// Object that can be drawn upon.
///
/// # What does the GPU do when you draw?
///
/// This is a summary of everything that happens when you call the `draw` function. Note that
/// this is not necessarily *exactly* what happens. Backends are free to do whatever they want
/// as long as it always matches the expected outcome.
///
/// ## Step 1: Vertex shader
///
/// For each vertex in the vertices source, the GPU invokes the vertex shader that is part
/// of the program, and passes the corresponding vertex's attributes to it.
///
/// The vertex shader *must* write the special `gl_Position` variable in order to indicate
/// the four-dimensions coordinates of the vertex. In order to understand what these coordinates
/// mean, see the "vertex post-processing" step below.
///
/// In addition to the position of the vertex, the vertex shader can also specify the values of
/// various vertex attributes.
///
/// ## Step 2: Tessellation (optional)
///
///
/// ## Step 3: Geometry shader (optional)
///
/// If you specify a geometry shader, then the GPU will invoke it once for each primitive.
///
/// The geometry shader can output multiple primitives.
///
/// ## Step 4: Transform feedback (optional)
///
/// TODO:
/// TODO: talk about `transform_feedback_primitives_written_query` as well
///
/// ## Step 5: Vertex post-processing
///
/// The vertex shader step told the GPU what the coordinates of each vertex are, but these
/// coordinates have four dimensions, named `x`, `y`, `z` and `w`.
///
/// The GPU then computes the position of the vertex on the 2D surface you are drawing on, and
/// the depth of this vertex:
///
/// ```notrust
/// window_x = viewport_left + viewport_width * ((x / w) + 1.0) / 2.0
/// window_y = viewport_bottom + viewport_height * ((y / w) + 1.0) / 2.0
/// depth = depth_near + (depth_far - depth_near) * ((z / w) + 1.0) / 2.0
/// ```
///
/// *`viewport_left`, `viewport_width`, `viewport_bottom` and `viewport_height` correspond to
/// the `viewport` member of the draw parameters, and `depth_near` and `depth_far` correspond
/// to the `depth_range` member*.
///
/// This means that if `x / w`, `y / w` or `z / w` are equal to `-1.0`, then the result will be
/// `viewport_left`, `viewport_bottom` or `depth_near`. If they are equal to `1.0`, the result
/// will be `viewport_left + viewport_width` (the right of the viewport),
/// `viewport_bottom + viewport_height` (the top of the viewport) or `depth_far`.
///
/// For example if you want to draw a rectangle that covers the whole screen, it should be made
/// of four vertices whose coordinates are `(-1.0, -1.0, 0.0, 1.0)` (bottom-left corner),
/// `(-1.0, 1.0, 0.0, 1.0)` (top-left corner), `(1.0, 1.0, 0.0, 1.0)` (top-right corner) and
/// `(1.0, -1.0, 0.0, 1.0)` (bottom-right corner).
///
/// ## Step 6: Primitive assembly
///
/// The next step consists in building the primitives. Triangle strips, triangle fans and line
/// strips are turned into individual triangles or lines.
///
/// Triangle strips obey certain rules for the order of indices. For example the triangle strip
/// `0, 1, 2, 3, 4, 5` does *not* correspond to `0, 1, 2`, `1, 2, 3`, `2, 3, 4`, `3, 4, 5` as you
/// would expect, but to `0, 1, 2`, `1, 3, 2`, `2, 3, 4`, `3, 5, 4` (some indices are reversed).
/// This is important with regards to the face culling step below.
///
/// Then, if you did specify `PrimitiveMode`, it is used. If you specified `Line`, triangles are
/// turned into lines. If specified `Point`, triangles and lines are turned into points.
///
/// The GPU then looks at the screen coordinates of each primitive, and discards primitives that
/// are entirely outside of the window.
///
/// Note that points whose centers are outside of the viewport are discarded, even if the point
/// width would be big enough for the point to be visible. However this standard behavior is not
/// respected by nVidia drivers, which show the points anyway.
///
/// If a query has been specified through `primitives_generated_query`, then its value is updated.
///
/// ## Step 7: Face culling (triangles only)
///
/// This step is purely an optimization step and only concerns triangles.
///
/// If you specify a value for `backface_culling` other than `CullingDisabled`, the GPU will
/// discard triangles depending on the way that the vertices are arranged on the window. You can
/// either discard triangles whose vertices are clockwise or counterclockwise.
///
/// For more information, see the `BackfaceCullingMode` documentation.
///
/// ## Step 8: Rasterization
///
/// Now that the GPU knows where on the window the various triangles, points or lines are, it will
/// determine which pixels of the surface are part of each primitive.
///
/// For points and lines, this step depends on the points width and line width that you specified
/// in the draw parameters. If you specify the `smooth` parameter, then the borders of the
/// primitives will see their alpha value adjusted.
///
/// <img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnYAAAEsCAYAAABOqf71AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAxOAAAMTgF/d4wjAAAAB3RJTUUH3wEIDBAooQVGygAAG4JJREFUeNrt3XuQXGWZx/HvmftMLjOZJJCLuZAEuYgJlIBKFBGQoNwCtQvlemPRVVaLhRUtlHG31KUVd7HEUgvwDy9LqaB7UVxL3dJV8a6sV5DLCkmASAwJyYRkMvfeP96ezJlmksxkumfOe/r7qUqR0wmZp5/z9ulfn/f0e0CSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJEnSDEq64BLgFmBNhPUXgcTdqByKdWxvTuBvb4JvuQsP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WSJIkGewkSZJksJMkSZLBTpIkSQY7SZIkg50kSZIMdpIkSTLYSZIkyWAnSZJksJMkSZLBTpIkSQY7SZIkGewkSZJksJMkSTLYSZIkyWAnSZIkg50kSZIMdpIkSQY7SZIkGewkSZJksJMkSZLBTpIkyWAnSZIkg50kSZIMdpIkSTLYSZIkyWAnSZJksJMkSZLBTpIkSdWVdEExtf0b4AcR1H0KcGZq+z7gxxHU/ZfAktT27UBfBHW/A2go/b6vVHfWLQBel9reAnw1grrPBl6Y2v4m8EhkdXcXoMPD6+F1wW6gHWAYHhmA72e95npY3QDnjGwPwUODcG/W626C1yTwvNRDXwB2RDBMrgaaS78fBD4VQc3NpbpHbAPujqDu9cCpqe17gV/HVnd5sJOkqTLYHUGwk6RKqAMetQ2SKuhJWzBhf7QFkipoax1wA7A10ifg2UblVaxj+0Hg3e6+CXsfsNkxKjm2K+BR4Fp3nyRJkiRJkiRJkiRJkiRJkiRJkiRJkiRJkiRJkiRJkiRJkiRJkiRJkiRJkiRN3f8DIrOQLp3kqkAAAAAASUVORK5CYII=" />
///
/// The attributes of each vertex that are marked as `smooth` (which is the default value) are
/// being interpolated, and the GPU assigns a value for each attribute for each pixel.
///
/// ## Step 9: Fragment shader
///
/// The GPU now executes the fragment shader once for each pixel of each primitive.
/// The vertex attributes that were interpolated at the previous step are passed to the fragment
/// shader.
///
/// The fragment shader must return the color to write by setting the value of `gl_FragColor`.
///
/// If the target framebuffer has multisampling enabled, then each pixel of the target image is
/// in turn split into four subpixels. The output of the fragment shader is copied and written into
/// each subpixel. If `multisampling` in the draw parameters is `true` (its default value), only
/// subpixels that belong to the triangle are written.
///
/// If a query has been specified through `samples_passed_query`, then its value is updated.
///
/// ## Step 10: Pixel ownership
///
/// This step is mostly an implementation detail. If the window you are drawing on is not on the
/// foreground, or if it is partially obstructed, then the pixels that are not on the
/// foreground will be discarded.
///
/// This is only relevant if you draw to the default framebuffer.
///
/// This step has to be taken into account in some situations. For example if you query the number
/// of samples that have been written, the ones that don't pass the pixel ownership test won't
/// count.
///
/// ## Step 11: Scissor test
///
/// If `scissor` has been specified, then all the pixels that are outside of this rect
/// are discarded.
///
/// ## Step 12: Depth test
///
/// In addition to the colors, surfaces can also have a depth buffer attached to it. In this
/// situation, just like each pixel has a color, each pixel of the surface also has an associated
/// depth value.
///
/// If a depth buffer is present, the GPU will compare the depth value of the pixel currently
/// being processed, with the existing depth value. Depending on the value of `depth_test`
/// in the draw parameters, the depth test will either pass, in which case the pipeline
/// continues, or fail, in which case the pixel is discarded. If the value of `depth_write`
/// is true and the test passed, it will then also write the depth value of the pixel on the
/// depth buffer.
///
/// The purpose of this test is to avoid drawing elements that are in the background of the
/// scene over elements that are in the foreground.
///
/// See the documentation of `DepthTest` for more informations.
///
/// ## Step 13: Stencil test
///
/// Similar to the depth buffer, surfaces can also have a stencil buffer.
///
/// The `stencil_test_clockwise` and `stencil_test_counter_clockwise` draw parameters specify
/// the operation to use to check whether or not each pixel passes the stencil test. Pixels that
/// fail the stencil test won't be drawn on the screen.
///
/// The `*_clockwise` members are relevant for polygons that are displayed clockwise, and the
/// `*_counter_clockwise` members are relevant for polygons that are displayed counterclockwise.
/// See also the `face culling` step for more infos. Lines and points always use the `*_clockwise`
/// members.
///
/// There are three possibilities for each pixel: either it fails the stencil test, or it passes
/// the stencil test but failed the depth test, or it passes both the stencil test and depth test.
/// You can specify for each of these three situations what to do with the value in the stencil
/// buffer with the draw parameters.
///
/// ## Step 14: Blending
///
/// For each pixel to write, the GPU takes the RGBA color that the fragment shader has returned
/// and the existing RGBA color already written on the surface, and merges the two.
///
/// The way they are merged depends on the value of `blending_function`. This allows you to choose
/// how alpha colors are merged together.
///
/// See the documentation of `BlendingFunction` fore more informations.
///
/// ## Step 15: Dithering (optional)
///
/// If `dithering` is `true` in the draw parameters, then a dithering algorithm is applied.
///
/// When you draw a gradient of colors, the boundary between each individual color value is
/// visible. Thanks to an optical illusion, a dithering algorithm will change the color values
/// of some pixels and hide the boundaries.
///
/// ## Step 16: Conversion to sRGB
///
/// If the target has sRGB enabled, then the output of the fragment will get some gamma correction.
///
/// Monitors don't show colors linearly. For example a value of `0.5` isn't shown half as bright
/// as a value of `1.0`. Instead the monitor will show colors as darker than they should be.
/// In order to fix this problem, each pixel is modified to be made slightly brighter with the same
/// factor as the monitor makes them darker.
///
/// ## Step 17: End
///
/// This is finally the step where colors are being written. The `color_mask` parameter allow you
/// to specify whether each color component (red, green, blue and alpha) is written to the color
/// buffer.
///
pub trait Surface {
    /// Clears some attachments of the target.
    fn clear(&mut self, rect: Option<&Rect>, color: Option<(f32, f32, f32, f32)>, color_srgb: bool,
             depth: Option<f32>, stencil: Option<i32>);

    /// Clears the color attachment of the target.
    fn clear_color(&mut self, red: f32, green: f32, blue: f32, alpha: f32) {
        self.clear(None, Some((red, green, blue, alpha)), false, None, None);
    }

    /// Clears the color attachment of the target. The color is in sRGB format.
    fn clear_color_srgb(&mut self, red: f32, green: f32, blue: f32, alpha: f32) {
        self.clear(None, Some((red, green, blue, alpha)), true, None, None);
    }

    /// Clears the depth attachment of the target.
    fn clear_depth(&mut self, value: f32) {
        self.clear(None, None, false, Some(value), None);
    }

    /// Clears the stencil attachment of the target.
    fn clear_stencil(&mut self, value: i32) {
        self.clear(None, None, false, None, Some(value));
    }

    /// Clears the color and depth attachments of the target.
    fn clear_color_and_depth(&mut self, color: (f32, f32, f32, f32), depth: f32) {
        self.clear(None, Some(color), false, Some(depth), None);
    }

    /// Clears the color and depth attachments of the target. The color is in sRGB format.
    fn clear_color_srgb_and_depth(&mut self, color: (f32, f32, f32, f32), depth: f32) {
        self.clear(None, Some(color), true, Some(depth), None);
    }

    /// Clears the color and stencil attachments of the target.
    fn clear_color_and_stencil(&mut self, color: (f32, f32, f32, f32), stencil: i32) {
        self.clear(None, Some(color), false, None, Some(stencil));
    }

    /// Clears the color and stencil attachments of the target. The color is in sRGB format.
    fn clear_color_srgb_and_stencil(&mut self, color: (f32, f32, f32, f32), stencil: i32) {
        self.clear(None, Some(color), true, None, Some(stencil));
    }

    /// Clears the depth and stencil attachments of the target.
    fn clear_depth_and_stencil(&mut self, depth: f32, stencil: i32) {
        self.clear(None, None, false, Some(depth), Some(stencil));
    }

    /// Clears the color, depth and stencil attachments of the target.
    fn clear_all(&mut self, color: (f32, f32, f32, f32), depth: f32, stencil: i32) {
        self.clear(None, Some(color), false, Some(depth), Some(stencil));
    }

    /// Clears the color, depth and stencil attachments of the target. The color is in sRGB format.
    fn clear_all_srgb(&mut self, color: (f32, f32, f32, f32), depth: f32, stencil: i32) {
        self.clear(None, Some(color), true, Some(depth), Some(stencil));
    }

    /// Returns the dimensions in pixels of the target.
    fn get_dimensions(&self) -> (u32, u32);

    /// Returns the number of bits of each pixel of the depth buffer.
    ///
    /// Returns `None` if there is no depth buffer.
    fn get_depth_buffer_bits(&self) -> Option<u16>;

    /// Returns true if the surface has a depth buffer available.
    fn has_depth_buffer(&self) -> bool {
        self.get_depth_buffer_bits().is_some()
    }

    /// Returns the number of bits of each pixel of the stencil buffer.
    ///
    /// Returns `None` if there is no stencil buffer.
    fn get_stencil_buffer_bits(&self) -> Option<u16>;

    /// Returns true if the surface has a stencil buffer available.
    fn has_stencil_buffer(&self) -> bool {
        self.get_stencil_buffer_bits().is_some()
    }

    /// Draws.
    ///
    /// This is probably the most complex function of glium. Check out the rest of the
    /// documentation for example how to use it.
    ///
    /// See above for what happens exactly on the GPU when you draw.
    fn draw<'a, 'b, V, I, U>(&mut self, V, I, program: &Program, uniforms: &U,
        draw_parameters: &DrawParameters) -> Result<(), DrawError> where
        V: vertex::MultiVerticesSource<'b>, I: Into<index::IndicesSource<'a>>,
        U: uniforms::Uniforms;

    /// Blits from the default framebuffer.
    fn blit_from_frame(&self, source_rect: &Rect, target_rect: &BlitTarget,
                       filter: uniforms::MagnifySamplerFilter);

    /// Blits from a simple framebuffer.
    fn blit_from_simple_framebuffer(&self, source: &framebuffer::SimpleFrameBuffer,
                                    source_rect: &Rect, target_rect: &BlitTarget,
                                    filter: uniforms::MagnifySamplerFilter);

    /// Blits from a multi-output framebuffer.
    fn blit_from_multioutput_framebuffer(&self, source: &framebuffer::MultiOutputFrameBuffer,
                                         source_rect: &Rect, target_rect: &BlitTarget,
                                         filter: uniforms::MagnifySamplerFilter);

    /// Copies a rectangle of pixels from this surface to another surface.
    ///
    /// The `source_rect` defines the area of the source (`self`) that will be copied, and the
    /// `target_rect` defines the area where the copied image will be pasted. If the source and
    /// target areas don't have the same dimensions, the image will be resized to match.
    /// The `filter` parameter is relevant only in this situation.
    ///
    /// It is possible for the source and the target to be the same surface. However if the
    /// rectangles overlap, then the behavior is undefined.
    ///
    /// Note that there is no alpha blending, depth/stencil checking, etc. This function just
    /// copies pixels.
    fn blit_color<S>(&self, source_rect: &Rect, target: &S, target_rect: &BlitTarget,
                     filter: uniforms::MagnifySamplerFilter) where S: Surface;

    /// Copies the entire surface to a target surface. See `blit_color`.
    #[inline]
    fn blit_whole_color_to<S>(&self, target: &S, target_rect: &BlitTarget,
        filter: uniforms::MagnifySamplerFilter) where S: Surface
    {
        let src_dim = self.get_dimensions();
        let src_rect = Rect { left: 0, bottom: 0, width: src_dim.0 as u32, height: src_dim.1 as u32 };
        self.blit_color(&src_rect, target, target_rect, filter)
    }

    /// Copies the entire surface to the entire target. See `blit_color`.
    #[inline]
    fn fill<S>(&self, target: &S, filter: uniforms::MagnifySamplerFilter) where S: Surface {
        let src_dim = self.get_dimensions();
        let src_rect = Rect { left: 0, bottom: 0, width: src_dim.0 as u32, height: src_dim.1 as u32 };
        let target_dim = target.get_dimensions();
        let target_rect = BlitTarget { left: 0, bottom: 0, width: target_dim.0 as i32, height: target_dim.1 as i32 };
        self.blit_color(&src_rect, target, &target_rect, filter)
    }
}

/// Private trait for framebuffer-like objects that provide attachments.
trait FboAttachments {
    /// Returns the list of attachments of this FBO, or `None` if it is the default framebuffer.
    fn get_attachments(&self) -> Option<&fbo::ValidatedAttachments>;
}

/// Error that can happen while drawing.
#[derive(Clone, Debug)]
pub enum DrawError {
    /// A depth function has been requested but no depth buffer is available.
    NoDepthBuffer,

    /// The type of a vertex attribute in the vertices source doesn't match what the
    /// program requires.
    AttributeTypeMismatch,

    /// One of the attributes required by the program is missing from the vertex format.
    ///
    /// Note that it is perfectly valid to have an attribute in the vertex format that is
    /// not used by the program.
    AttributeMissing,

    /// The viewport's dimensions are not supported by the backend.
    ViewportTooLarge,

    /// The depth range is outside of the `(0, 1)` range.
    InvalidDepthRange,

    /// The type of a uniform doesn't match what the program requires.
    UniformTypeMismatch {
        /// Name of the uniform you are trying to bind.
        name: String,
        /// The expected type.
        expected: uniforms::UniformType,
    },

    /// Tried to bind a uniform buffer to a single uniform value.
    UniformBufferToValue {
        /// Name of the uniform you are trying to bind.
        name: String,
    },

    /// Tried to bind a single uniform value to a uniform block.
    UniformValueToBlock {
        /// Name of the uniform you are trying to bind.
        name: String,
    },

    /// The layout of the content of the uniform buffer does not match the layout of the block.
    UniformBlockLayoutMismatch {
        /// Name of the block you are trying to bind.
        name: String,
        /// The error giving more details about the mismatch.
        err: uniforms::LayoutMismatchError,
    },

    /// Tried to bind a subroutine uniform like a regular uniform value.
    SubroutineUniformToValue {
        /// Name of the uniform you are trying to bind.
        name: String,
    },

    /// Not all subroutine uniforms of a shader stage were set.
    SubroutineUniformMissing {
        /// Shader stage with missing bindings.
        stage: program::ShaderStage,
        /// The expected number of bindings.
        expected_count: usize,
        /// The number of bindings defined by the user.
        real_count: usize,

    },

    /// A non-existent subroutine was referenced.
    SubroutineNotFound {
        /// The stage the subroutine was searched for.
        stage: program::ShaderStage,
        /// The invalid name of the subroutine.
        name: String
    },

    /// The number of vertices per patch that has been requested is not supported.
    UnsupportedVerticesPerPatch,

    /// Trying to use tessellation, but this is not supported by the underlying hardware.
    TessellationNotSupported,

    /// Using a program which contains tessellation shaders, but without submitting patches.
    TessellationWithoutPatches,

    /// Trying to use a sampler, but they are not supported by the backend.
    SamplersNotSupported,

    /// When you use instancing, all vertices sources must have the same size.
    InstancesCountMismatch,

    /// If you don't use indices, then all vertices sources must have the same size.
    VerticesSourcesLengthMismatch,

    /// You requested not to draw primitives, but this is not supported by the backend.
    TransformFeedbackNotSupported,

    /// See the documentation of the `draw_parameters` module for infos.
    WrongQueryOperation,

    /// You requested smoothing, but this is not supported by the backend.
    SmoothingNotSupported,

    /// The requested provoking vertex is not supported by the backend.
    ProvokingVertexNotSupported,

    /// Discarding rasterizer output isn't supported by the backend.
    RasterizerDiscardNotSupported,

    /// Depth clamping isn't supported by the backend.
    DepthClampNotSupported,

    /// One of the blending parameters is not supported by the backend.
    BlendingParameterNotSupported,

    /// Restarting indices (multiple objects per draw call) is not supported by the backend.
    FixedIndexRestartingNotSupported,

    /// Tried to enable a clip plane that does not exist.
    ClipPlaneIndexOutOfBounds,
}

impl Error for DrawError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        use self::DrawError::*;
        match *self {
            UniformBlockLayoutMismatch { ref err, .. } => Some(err),
            _ => None,
        }
    }
}


impl fmt::Display for DrawError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::DrawError::*;
        let desc = match self {
            NoDepthBuffer =>
                "A depth function has been requested but no depth buffer is available",
            AttributeTypeMismatch =>
                "The type of a vertex attribute in the vertices source doesn't match what the program requires",
            AttributeMissing =>
                "One of the attributes required by the program is missing from the vertex format",
            ViewportTooLarge =>
                "The viewport's dimensions are not supported by the backend",
            InvalidDepthRange =>
                "The depth range is outside of the `(0, 1)` range",
            UniformTypeMismatch { .. } =>
                "The type of a uniform doesn't match what the program requires",
            UniformBufferToValue { .. } =>
                "Tried to bind a uniform buffer to a single uniform value",
            UniformValueToBlock { .. } =>
                "Tried to bind a single uniform value to a uniform block",
            UniformBlockLayoutMismatch { .. } =>
                "The layout of the content of the uniform buffer does not match the layout of the block",
            SubroutineUniformToValue { .. } =>
                "Tried to bind a subroutine uniform like a regular uniform value",
            SubroutineUniformMissing { .. } =>
                "Not all subroutine uniforms of a shader stage were set",
            SubroutineNotFound { .. } =>
                "A non-existent subroutine was referenced",
            UnsupportedVerticesPerPatch =>
                "The number of vertices per patch that has been requested is not supported",
            TessellationNotSupported =>
                "Trying to use tessellation, but this is not supported by the underlying hardware",
            TessellationWithoutPatches =>
                "Using a program which contains tessellation shaders, but without submitting patches",
            SamplersNotSupported => "
                Trying to use a sampler, but they are not supported by the backend",
            InstancesCountMismatch =>
                "When you use instancing, all vertices sources must have the same size",
            VerticesSourcesLengthMismatch =>
                "If you don't use indices, then all vertices sources must have the same size",
            TransformFeedbackNotSupported =>
                "Requested not to draw primitives, but this is not supported by the backend",
            WrongQueryOperation =>
                "Wrong query operation",
            SmoothingNotSupported =>
                "Trying to use smoothing, but this is not supported by the backend",
            ProvokingVertexNotSupported =>
                "Trying to set the provoking vertex, but this is not supported by the backend",
            RasterizerDiscardNotSupported =>
                "Discarding rasterizer output is not supported by the backend",
            DepthClampNotSupported =>
                "The depth clamp mode is not supported by the backend",
            BlendingParameterNotSupported =>
                "One the blending parameters is not supported by the backend",
            FixedIndexRestartingNotSupported =>
                "Restarting indices (multiple objects per draw call) is not supported by the backend",
            ClipPlaneIndexOutOfBounds =>
                "Tried to enable a clip plane that does not exist."
        };
        match self {
            UniformTypeMismatch { name, expected } =>
                write!(
                    fmt,
                    "{}, got: {:?}, expected: {:?}",
                    desc,
                    name,
                    expected,
                ),
            UniformBufferToValue { name } =>
                write!(
                    fmt,
                    "{}: {}",
                    desc,
                    name,
                ),
            UniformValueToBlock { name } =>
                write!(
                    fmt,
                    "{}: {}",
                    desc,
                    name,
                ),
            UniformBlockLayoutMismatch { name, err } =>
                write!(
                    fmt,
                    "{}: {}, caused by {}",
                    desc,
                    name,
                    err,
                ),
            _ =>
                fmt.write_str(desc),
        }
    }
}

/// Error that can happen when swapping buffers.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SwapBuffersError {
    /// The OpenGL context has been lost and needs to be recreated. The `Display` and all the
    /// objects associated to it (textures, buffers, programs, etc.) need to be recreated from
    /// scratch.
    ///
    /// Operations will have no effect. Functions that read textures, buffers, etc. from OpenGL
    /// will return uninitialized data instead.
    ///
    /// A context loss usually happens on mobile devices when the user puts the application on
    /// sleep and wakes it up later. However any OpenGL implementation can theoretically lose the
    /// context at any time. Can only happen if calling `is_context_loss_possible()` returns true.
    ContextLost,
    /// The buffers have already been swapped.
    ///
    /// This error can be returned when `set_finish()` is called multiple times, or `finish()` is
    /// called after `set_finish()`.
    AlreadySwapped,
}

impl Error for SwapBuffersError {}

impl fmt::Display for SwapBuffersError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::SwapBuffersError::*;
        let desc = match *self {
            ContextLost =>
                "the OpenGL context has been lost and needs to be recreated",
            AlreadySwapped =>
                "the buffers have already been swapped",
        };
        fmt.write_str(desc)
    }
}

/// Implementation of `Surface`, targeting the default framebuffer.
///
/// The back- and front-buffers are swapped when you call `finish`.
///
/// You **must** call either `finish` or `set_finish` or else the destructor will panic.
pub struct Frame {
    context: Rc<Context>,
    dimensions: (u32, u32),
    destroyed: bool,        // TODO: use a linear type instead.
}

impl Frame {
    /// Builds a new `Frame`. Use the `draw` function on `Display` instead of this function.
    #[inline]
    pub fn new(context: Rc<Context>, dimensions: (u32, u32)) -> Frame {
        Frame {
            context: context,
            dimensions: dimensions,
            destroyed: false,
        }
    }

    /// Stop drawing, swap the buffers, and consume the Frame.
    ///
    /// See the documentation of `SwapBuffersError` about what is being returned.
    #[inline]
    pub fn finish(mut self) -> Result<(), SwapBuffersError> {
        self.set_finish()
    }

    /// Stop drawing, swap the buffers.
    ///
    /// The Frame can now be dropped regularly.  Calling `finish()` or `set_finish()` again will
    /// cause `Err(SwapBuffersError::AlreadySwapped)` to be returned.
    #[inline]
    pub fn set_finish(&mut self) -> Result<(), SwapBuffersError> {
        if self.destroyed {
            return Err(SwapBuffersError::AlreadySwapped);
        }

        self.destroyed = true;
        self.context.swap_buffers()
    }
}

impl Surface for Frame {
    #[inline]
    fn clear(&mut self, rect: Option<&Rect>, color: Option<(f32, f32, f32, f32)>, color_srgb: bool,
             depth: Option<f32>, stencil: Option<i32>)
    {
        ops::clear(&self.context, None, rect, color, color_srgb, depth, stencil);
    }

    fn get_dimensions(&self) -> (u32, u32) {
        self.dimensions
    }

    fn get_depth_buffer_bits(&self) -> Option<u16> {
        self.context.capabilities().depth_bits
    }

    fn get_stencil_buffer_bits(&self) -> Option<u16> {
        self.context.capabilities().stencil_bits
    }

    fn draw<'a, 'b, V, I, U>(&mut self, vertex_buffer: V,
                         index_buffer: I, program: &Program, uniforms: &U,
                         draw_parameters: &DrawParameters) -> Result<(), DrawError>
                         where I: Into<index::IndicesSource<'a>>, U: uniforms::Uniforms,
                         V: vertex::MultiVerticesSource<'b>
    {
        if !self.has_depth_buffer() && (draw_parameters.depth.test.requires_depth_buffer() ||
                draw_parameters.depth.write)
        {
            return Err(DrawError::NoDepthBuffer);
        }

        if let Some(viewport) = draw_parameters.viewport {
            if viewport.width > self.context.capabilities().max_viewport_dims.0
                    as u32
            {
                return Err(DrawError::ViewportTooLarge);
            }
            if viewport.height > self.context.capabilities().max_viewport_dims.1
                    as u32
            {
                return Err(DrawError::ViewportTooLarge);
            }
        }

        ops::draw(&self.context, None, vertex_buffer, index_buffer.into(), program,
                  uniforms, draw_parameters, (self.dimensions.0 as u32, self.dimensions.1 as u32))
    }

    #[inline]
    fn blit_color<S>(&self, source_rect: &Rect, target: &S, target_rect: &BlitTarget,
                     filter: uniforms::MagnifySamplerFilter) where S: Surface
    {
        target.blit_from_frame(source_rect, target_rect, filter)
    }

    #[inline]
    fn blit_from_frame(&self, source_rect: &Rect, target_rect: &BlitTarget,
                       filter: uniforms::MagnifySamplerFilter)
    {
        ops::blit(&self.context, None, self.get_attachments(),
                  gl::COLOR_BUFFER_BIT, source_rect, target_rect, filter.to_glenum())
    }

    #[inline]
    fn blit_from_simple_framebuffer(&self, source: &framebuffer::SimpleFrameBuffer,
                                    source_rect: &Rect, target_rect: &BlitTarget,
                                    filter: uniforms::MagnifySamplerFilter)
    {
        ops::blit(&self.context, source.get_attachments(), self.get_attachments(),
                  gl::COLOR_BUFFER_BIT, source_rect, target_rect, filter.to_glenum())
    }

    #[inline]
    fn blit_from_multioutput_framebuffer(&self, source: &framebuffer::MultiOutputFrameBuffer,
                                         source_rect: &Rect, target_rect: &BlitTarget,
                                         filter: uniforms::MagnifySamplerFilter)
    {
        ops::blit(&self.context, source.get_attachments(), self.get_attachments(),
                  gl::COLOR_BUFFER_BIT, source_rect, target_rect, filter.to_glenum())
    }
}

impl FboAttachments for Frame {
    #[inline]
    fn get_attachments(&self) -> Option<&fbo::ValidatedAttachments> {
        None
    }
}

impl Drop for Frame {
    #[inline]
    fn drop(&mut self) {
        if !thread::panicking() {
            assert!(self.destroyed, "The `Frame` object must be explicitly destroyed \
                                     by calling `.finish()`");
        }
    }
}

/// Returned during Context creation if the OpenGL implementation is too old.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IncompatibleOpenGl(pub String);

impl fmt::Display for IncompatibleOpenGl {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("The OpenGL implementation is too old to work with glium")
    }
}

impl Error for IncompatibleOpenGl {}

#[allow(dead_code)]
#[inline]
fn get_gl_error(ctxt: &mut context::CommandContext) -> Option<&'static str> {
    match unsafe { ctxt.gl.GetError() } {
        gl::NO_ERROR => None,
        gl::INVALID_ENUM => Some("GL_INVALID_ENUM"),
        gl::INVALID_VALUE => Some("GL_INVALID_VALUE"),
        gl::INVALID_OPERATION => Some("GL_INVALID_OPERATION"),
        gl::INVALID_FRAMEBUFFER_OPERATION => Some("GL_INVALID_FRAMEBUFFER_OPERATION"),
        gl::OUT_OF_MEMORY => Some("GL_OUT_OF_MEMORY"),
        gl::STACK_UNDERFLOW => Some("GL_STACK_UNDERFLOW"),
        gl::STACK_OVERFLOW => Some("GL_STACK_OVERFLOW"),
        gl::CONTEXT_LOST => Some("GL_CONTEXT_LOST"),
        _ => Some("Unknown glGetError return value")
    }
}