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
// Copyright 2015 The Gfx-rs Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(missing_docs)]

use gl;
use gfx_core::{self as c, command, state as s};
use gfx_core::target::{ColorValue, Depth, Mirror, Rect, Stencil};
use gfx_core::texture::TextureCopyRegion;
use {Buffer, BufferElement, Program, FrameBuffer, Texture,
     NewTexture, Resources, PipelineState, ResourceView, TargetView};


fn primitive_to_gl(primitive: c::Primitive) -> gl::types::GLenum {
    use gfx_core::Primitive::*;
    match primitive {
        PointList => gl::POINTS,
        LineList => gl::LINES,
        LineStrip => gl::LINE_STRIP,
        TriangleList => gl::TRIANGLES,
        TriangleStrip => gl::TRIANGLE_STRIP,
        LineListAdjacency => gl::LINES_ADJACENCY,
        LineStripAdjacency => gl::LINE_STRIP_ADJACENCY,
        TriangleListAdjacency => gl::TRIANGLES_ADJACENCY,
        TriangleStripAdjacency => gl::TRIANGLE_STRIP_ADJACENCY,
        //TriangleFan => gl::TRIANGLE_FAN,
        PatchList(_) => gl::PATCHES
    }
}

pub type Access = gl::types::GLenum;

#[derive(Clone, Copy, Debug)]
pub struct RawOffset(pub *const gl::types::GLvoid);
unsafe impl Send for RawOffset {}

/// The place of some data in the data buffer.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct DataPointer {
    offset: u32,
    size: u32,
}

pub struct DataBuffer(Vec<u8>);
impl DataBuffer {
    /// Create a new empty data buffer.
    pub fn new() -> DataBuffer {
        DataBuffer(Vec::new())
    }
    /// Copy a given vector slice into the buffer.
    fn add(&mut self, data: &[u8]) -> DataPointer {
        self.0.extend_from_slice(data);
        DataPointer {
            offset: (self.0.len() - data.len()) as u32,
            size: data.len() as u32,
        }
    }
    /// Return a reference to a stored data object.
    pub fn get(&self, ptr: DataPointer) -> &[u8] {
        &self.0[ptr.offset as usize..(ptr.offset + ptr.size) as usize]
    }
}


/// Serialized device command.
#[derive(Clone, Debug)]
pub enum Command {
    // states
    BindProgram(Program),
    BindConstantBuffer(c::pso::ConstantBufferParam<Resources>),
    BindResourceView(c::pso::ResourceViewParam<Resources>),
    BindUnorderedView(c::pso::UnorderedViewParam<Resources>),
    BindSampler(c::pso::SamplerParam<Resources>, Option<gl::types::GLenum>),
    BindPixelTargets(c::pso::PixelTargetSet<Resources>),
    BindVao,
    BindAttribute(c::AttributeSlot, Buffer, BufferElement),
    UnbindAttribute(c::AttributeSlot),
    BindIndex(Buffer),
    BindFrameBuffer(Access, FrameBuffer),
    BindUniform(c::shade::Location, c::shade::UniformValue),
    SetDrawColorBuffers(c::ColorSlot),
    SetRasterizer(s::Rasterizer),
    SetViewport(Rect),
    SetScissor(Option<Rect>),
    SetDepthState(Option<s::Depth>),
    SetStencilState(Option<s::Stencil>, (Stencil, Stencil), s::CullFace),
    SetBlendState(c::ColorSlot, s::Color),
    SetBlendColor(ColorValue),
    SetPatches(c::PatchSize),
    SetOutputMasks { color: bool, depth: bool, stencil: bool },
    CopyBuffer(Buffer, Buffer,
               gl::types::GLintptr, gl::types::GLintptr,
               gl::types::GLsizeiptr),
    CopyBufferToTexture(Buffer, gl::types::GLintptr,
                        TextureCopyRegion<Texture>),
    CopyTextureToBuffer(TextureCopyRegion<NewTexture>,
                        Buffer, gl::types::GLintptr,
                        FrameBuffer),
    CopyTextureToTexture(TextureCopyRegion<NewTexture>,
                         TextureCopyRegion<Texture>,
                         FrameBuffer),
    // resource updates
    UpdateBuffer(Buffer, DataPointer, usize),
    UpdateTexture(TextureCopyRegion<Texture>, DataPointer),
    GenerateMipmap(ResourceView),
    // drawing
    Clear(Option<command::ClearColor>, Option<Depth>, Option<Stencil>),
    Draw(gl::types::GLenum, c::VertexCount, c::VertexCount, Option<command::InstanceParams>),
    DrawIndexed(gl::types::GLenum,
                gl::types::GLenum,
                RawOffset,
                c::VertexCount,
                c::VertexCount,
                Option<command::InstanceParams>),
    _Blit(Rect, Rect, Mirror, usize),
}

pub fn generate_reset() -> Vec<Command> {
    let color = s::Color {
        mask: s::ColorMask::all(),
        blend: None,
    };
    vec![
        Command::BindProgram(0),
        Command::BindVao,
        // Command::UnbindAttribute, //not needed, handled by the cache
        Command::BindIndex(0),
        Command::BindFrameBuffer(gl::FRAMEBUFFER, 0),
        Command::SetRasterizer(s::Rasterizer {
            front_face: s::FrontFace::CounterClockwise,
            cull_face: s::CullFace::Back,
            method: s::RasterMethod::Fill,
            offset: None,
            samples: None,
        }),
        Command::SetViewport(Rect {
            x: 0,
            y: 0,
            w: 0,
            h: 0,
        }),
        Command::SetScissor(None),
        Command::SetDepthState(None),
        Command::SetStencilState(None, (0, 0), s::CullFace::Nothing),
        Command::SetBlendState(0, color),
        Command::SetBlendState(1, color),
        Command::SetBlendState(2, color),
        Command::SetBlendState(3, color),
        Command::SetBlendColor([0f32; 4]),
    ]
}

struct Cache {
    primitive: gl::types::GLenum,
    index_type: c::IndexType,
    current_vbs: Option<c::pso::VertexBufferSet<Resources>>,
    attributes: [Option<BufferElement>; c::MAX_VERTEX_ATTRIBUTES],
    resource_binds: [Option<gl::types::GLenum>; c::MAX_RESOURCE_VIEWS],
    scissor: bool,
    target_dim: (u16, u16, u16),
    stencil: Option<s::Stencil>,
    // blend: Option<s::Blend>,
    cull_face: s::CullFace,
    draw_mask: u32,

    program: Program,
    constant_buffer: Option<c::pso::ConstantBufferParam<Resources>>,
    resource_view: Option<c::pso::ResourceViewParam<Resources>>,
    scissor_test: Option<Rect>,
    depth_state: Option<s::Depth>,
    blend_state: Option<(c::ColorSlot, s::Color)>,
    blend_color: Option<ColorValue>,
    viewport: Option<Rect>,
    rasterizer: Option<s::Rasterizer>,
    framebuffer: Option<(Access, FrameBuffer)>,
    index: Buffer,
}

impl Cache {
    pub fn new() -> Cache {
        Cache {
            primitive: 0,
            index_type: c::IndexType::U16,
            current_vbs: None,
            attributes: [None; c::MAX_VERTEX_ATTRIBUTES],
            resource_binds: [None; c::MAX_RESOURCE_VIEWS],
            scissor: false,
            target_dim: (0, 0, 0),
            stencil: None,
            cull_face: s::CullFace::Nothing,
            // blend: None,
            draw_mask: 0,

            program: 0,
            constant_buffer: None,
            resource_view: None,
            scissor_test: None,
            depth_state: None,
            blend_state: None,
            blend_color: None,
            viewport: None,
            rasterizer: None,
            framebuffer: None,
            index: 0,
        }
    }

    fn bind_program(&mut self, program: Program) -> Option<Command> {
        if program == self.program {
            return None;
        }
        self.program = program;
        Some(Command::BindProgram(program))
    }

    fn bind_constant_buffer(&mut self, constant_buffer: c::pso::ConstantBufferParam<Resources>) -> Option<Command> {
        if self.constant_buffer == Some(constant_buffer) {
            return None;
        }
        self.constant_buffer = Some(constant_buffer);
        Some(Command::BindConstantBuffer(constant_buffer))
    }

    fn bind_resource_view(&mut self, resource_view: c::pso::ResourceViewParam<Resources>) -> Option<Command> {
        if self.resource_view == Some(resource_view) {
            return None;
        }
        self.resource_view = Some(resource_view);
        Some(Command::BindResourceView(resource_view))
    }

    fn bind_index(&mut self, buffer: Buffer, itype: c::IndexType) -> Option<Command> {
        if self.index == buffer && itype == self.index_type {
            return None;
        }
        self.index_type = itype;
        self.index = buffer;
        Some(Command::BindIndex(buffer))
    }

    fn bind_framebuffer(&mut self, access: Access, fb: FrameBuffer) -> Option<Command> {
        if self.framebuffer == Some((access, fb)) {
            return None;
        }
        self.framebuffer = Some((access, fb));
        Some(Command::BindFrameBuffer(access, fb))
    }

    fn set_rasterizer(&mut self, rasterizer: s::Rasterizer) -> Option<Command> {
        if self.rasterizer == Some(rasterizer) {
            return None;
        }
        self.rasterizer = Some(rasterizer);
        Some(Command::SetRasterizer(rasterizer))
    }

    fn set_viewport(&mut self, rect: Rect) -> Option<Command> {
        if self.viewport == Some(rect) {
            return None;
        }
        self.viewport = Some(rect);
        Some(Command::SetViewport(rect))
    }

    fn set_scissor(&mut self, rect: Option<Rect>) -> Option<Command> {
        if self.scissor_test == rect {
            return None;
        }
        self.scissor_test = rect;
        Some(Command::SetScissor(rect))
    }
    fn set_depth_state(&mut self, depth: Option<s::Depth>) -> Option<Command> {
        if self.depth_state == depth {
            return None;
        }
        self.depth_state = depth;
        Some(Command::SetDepthState(depth))
    }
    fn set_stencil_state(&mut self, option_stencil: Option<s::Stencil>, stencils: (Stencil, Stencil), cullface: s:: CullFace) -> Option<Command> {
        // This is a little more complex 'cause if option_stencil
        // is None the stencil state is disabled, it it's Some
        // then it's enabled and parameters are set.
        // That's actually bad because it makes it impossible
        // to completely remove all redundant calls if the
        // stencil is enabled;
        // we'll be re-enabling it over and over.
        // For now though, we just don't handle it.
        Some(Command::SetStencilState(option_stencil, stencils, cullface))
    }
    fn set_blend_state(&mut self, color_slot: c::ColorSlot, color: s::Color) -> Option<Command> {
        if self.blend_state == Some((color_slot, color)) {
            return None;
        }
        self.blend_state = Some((color_slot, color));
        Some(Command::SetBlendState(color_slot, color))
    }
    fn set_blend_color(&mut self, color_value: ColorValue) -> Option<Command> {
        if self.blend_color == Some(color_value) {
            return None;
        }
        self.blend_color = Some(color_value);
        Some(Command::SetBlendColor(color_value))
    }
}

#[derive(Clone, Debug, Default)]
pub struct Workarounds {
    // On some systems (OSX), the driver may still output something even if the shader does not,
    // so we need to explicitly mask out the outputs according to the views being bound.
    pub main_fbo_mask: bool,
}

/// A command buffer abstraction for OpenGL.
///
/// Manages a list of commands that will be executed when submitted to a `Device`. Usually it is
/// best to use a `Encoder` to manage the command buffer which implements `From<CommandBuffer>`.
///
/// If you want to display your rendered results to a framebuffer created externally, see the
/// `display_fb` field.
pub struct CommandBuffer {
    pub buf: Vec<Command>,
    pub data: DataBuffer,
    fbo: FrameBuffer,
    /// The framebuffer to use for rendering to the main targets (0 by default).
    ///
    /// Use this to set the framebuffer that will be used for the screen display targets created
    /// with `create_main_targets_raw`. Usually you don't need to set this field directly unless
    /// your OS doesn't provide a default framebuffer with name 0 and you have to render to a
    /// different framebuffer object that can be made visible on the screen (iOS/tvOS need this).
    ///
    /// This framebuffer must exist and be configured correctly (with renderbuffer attachments,
    /// etc.) so that rendering to it can occur immediately.
    pub display_fb: FrameBuffer,
    cache: Cache,
    active_attribs: usize,
    workarounds: Workarounds,
}

impl CommandBuffer {
    pub fn new(fbo: FrameBuffer, workarounds: Workarounds) -> CommandBuffer {
        CommandBuffer {
            buf: Vec::new(),
            data: DataBuffer::new(),
            fbo,
            display_fb: 0 as FrameBuffer,
            cache: Cache::new(),
            active_attribs: 0,
            workarounds,
        }
    }
    fn is_main_target(&self, tv: Option<TargetView>) -> bool {
        match tv {
            Some(TargetView::Surface(0)) |
            None => true,
            Some(_) => false,
        }
    }
}

impl command::Buffer<Resources> for CommandBuffer {
    fn reset(&mut self) {
        self.buf.clear();
        self.data.0.clear();
        self.cache = Cache::new();
        self.active_attribs = (1 << c::MAX_VERTEX_ATTRIBUTES) - 1;
    }

    fn bind_pipeline_state(&mut self, pso: PipelineState) {
        let cull = pso.rasterizer.cull_face;
        self.cache.primitive = primitive_to_gl(pso.primitive);
        self.cache.attributes = pso.input;
        self.cache.stencil = pso.output.stencil;
        self.cache.cull_face = cull;
        self.cache.draw_mask = pso.output.draw_mask;
        self.buf.extend(self.cache.bind_program(pso.program));
        self.cache.scissor = pso.scissor;
        self.buf.extend(self.cache.set_rasterizer(pso.rasterizer));
        self.buf.extend(self.cache.set_depth_state(pso.output.depth));
        self.buf.extend(self.cache.set_stencil_state(pso.output.stencil, (0, 0), cull));
        for i in 0..c::MAX_COLOR_TARGETS {
            if pso.output.draw_mask & (1 << i) != 0 {
                self.buf.extend(self.cache.set_blend_state(i as c::ColorSlot,
                                           pso.output.colors[i]));
            }
        }
        if let c::Primitive::PatchList(num) = pso.primitive {
            self.buf.push(Command::SetPatches(num));
        }
    }

    fn bind_vertex_buffers(&mut self, vbs: c::pso::VertexBufferSet<Resources>) {
        if self.cache.current_vbs == Some(vbs) {
            return;
        }
        self.cache.current_vbs = Some(vbs);
        for i in 0..c::MAX_VERTEX_ATTRIBUTES {
            match (vbs.0[i], self.cache.attributes[i]) {
                (None, Some(fm)) => {
                    error!("No vertex input provided for slot {} of format {:?}", i, fm)
                }
                (Some((buffer, offset)), Some(mut bel)) => {
                    bel.elem.offset += offset as gl::types::GLuint;
                    self.buf.push(Command::BindAttribute(
                        i as c::AttributeSlot,
                        buffer,
                        bel));
                    self.active_attribs |= 1 << i;
                }
                (_, None) if self.active_attribs & (1 << i) != 0 => {
                    self.buf.push(Command::UnbindAttribute(i as c::AttributeSlot));
                    self.active_attribs ^= 1 << i;
                }
                (_, None) => (),
            }
        }
    }

    fn bind_constant_buffers(&mut self, cbs: &[c::pso::ConstantBufferParam<Resources>]) {
        for param in cbs.iter() {
            self.buf.extend(self.cache.bind_constant_buffer(param.clone()));
        }
    }

    fn bind_global_constant(&mut self, loc: c::shade::Location, value: c::shade::UniformValue) {
        self.buf.push(Command::BindUniform(loc, value));
    }

    fn bind_resource_views(&mut self, srvs: &[c::pso::ResourceViewParam<Resources>]) {
        for i in 0..c::MAX_RESOURCE_VIEWS {
            self.cache.resource_binds[i] = None;
        }
        for param in srvs.iter() {
            self.cache.resource_binds[param.2 as usize] = Some(param.0.bind);
            self.buf.extend(self.cache.bind_resource_view(param.clone()));
        }
    }

    fn bind_unordered_views(&mut self, uavs: &[c::pso::UnorderedViewParam<Resources>]) {
        for param in uavs.iter() {
            self.buf.push(Command::BindUnorderedView(param.clone()));
        }
    }

    fn bind_samplers(&mut self, ss: &[c::pso::SamplerParam<Resources>]) {
        for param in ss.iter() {
            let bind = self.cache.resource_binds[param.2 as usize];
            self.buf.push(Command::BindSampler(param.clone(), bind));
        }
    }

    fn bind_pixel_targets(&mut self, pts: c::pso::PixelTargetSet<Resources>) {
        let is_main = pts.colors.iter().skip(1).find(|c| c.is_some()).is_none() &&
                      self.is_main_target(pts.colors[0]) &&
                      self.is_main_target(pts.depth) &&
                      self.is_main_target(pts.stencil);
        if is_main {
            self.buf.extend(self.cache.bind_framebuffer(gl::DRAW_FRAMEBUFFER, self.display_fb));
            if self.workarounds.main_fbo_mask {
                // Note: this will only allow less stuff to reach the framebuffer, not more
                self.buf.push(Command::SetOutputMasks {
                    color: pts.colors[0].is_some(),
                    depth: pts.depth.is_some(),
                    stencil: pts.stencil.is_some(),
                });
                // Now that we bound a different mask, invalidate the cached states.
                if pts.colors[0].is_none() {
                    self.cache.blend_state = None;
                }
                if pts.depth.is_none() {
                    self.cache.depth_state = None;
                }
                if pts.stencil.is_none() {
                    //self.cache.stencil_state = None;
                }
            }
        } else {
            let num = pts.colors
                .iter()
                .position(|c| c.is_none())
                .unwrap_or(pts.colors.len()) as c::ColorSlot;
            self.buf.extend(self.cache.bind_framebuffer(gl::DRAW_FRAMEBUFFER, self.fbo));
            self.buf.push(Command::BindPixelTargets(pts));
            self.buf.push(Command::SetDrawColorBuffers(num));
        }
        let view = pts.get_view();
        self.cache.target_dim = view;
        self.buf.extend(
            self.cache.set_viewport(Rect {
                                    x: 0,
                                    y: 0,
                                    w: view.0,
                                    h: view.1,
                                }));
    }

    fn bind_index(&mut self, buf: Buffer, itype: c::IndexType) {
        self.buf.extend(self.cache.bind_index(buf, itype));
    }

    fn set_scissor(&mut self, rect: Rect) {
        use std::cmp;
        let scissor = self.cache.scissor;
        let target_dim = self.cache.target_dim;
        let scissor_rect = if scissor {
           Some(Rect {
               // inverting the Y axis in order to match D3D11
               y: cmp::max(target_dim.1, rect.y + rect.h) -
                   rect.y -
                   rect.h,
               ..rect
           })
       } else {
            None //TODO: assert?
       };
        self.buf.extend(self.cache.set_scissor(scissor_rect));
    }

    fn set_ref_values(&mut self, rv: s::RefValues) {
        let stencil = self.cache.stencil;
        let cull_face = self.cache.cull_face;
        self.buf.extend(self.cache.set_stencil_state(stencil,
                                                     rv.stencil,
                                                     cull_face));
        self.buf.extend(self.cache.set_blend_color(rv.blend));
    }

    fn copy_buffer(&mut self,
                   src: Buffer,
                   dst: Buffer,
                   src_offset_bytes: usize,
                   dst_offset_bytes: usize,
                   size_bytes: usize) {
        self.buf.push(Command::CopyBuffer(src, dst,
                                          src_offset_bytes as gl::types::GLintptr,
                                          dst_offset_bytes as gl::types::GLintptr,
                                          size_bytes as gl::types::GLsizeiptr));
    }

    fn copy_buffer_to_texture(&mut self,
                              src: Buffer, src_offset_bytes: usize,
                              dst: TextureCopyRegion<NewTexture>) {
        match dst.texture {
            NewTexture::Texture(raw) => {
                let dst = dst.with_texture(raw);
                self.buf.push(Command::CopyBufferToTexture(
                    src, src_offset_bytes as _, dst
                ));
            }
            NewTexture::Surface(s) => {
                error!("GL: Cannot copy to a Surface({})", s)
            }
        }
    }

    fn copy_texture_to_buffer(&mut self,
                              src: TextureCopyRegion<NewTexture>,
                              dst: Buffer, dst_offset_bytes: usize) {
        self.buf.push(Command::CopyTextureToBuffer(
            src, dst, dst_offset_bytes as _, self.fbo
        ));
        self.cache.framebuffer = None; // reset in the cache
    }

    fn copy_texture_to_texture(&mut self,
                               src: TextureCopyRegion<NewTexture>,
                               dst: TextureCopyRegion<NewTexture>) {
        match dst.texture {
            NewTexture::Texture(raw) => {
                let dst = dst.with_texture(raw);
                self.buf.push(Command::CopyTextureToTexture(src, dst, self.fbo));
                self.cache.framebuffer = None; // reset in the cache
            }
            NewTexture::Surface(s) => {
                error!("GL: Cannot copy to a Surface({})", s)
            }
        }
    }

    fn update_buffer(&mut self, buf: Buffer, data: &[u8], offset_bytes: usize) {
        let ptr = self.data.add(data);
        self.buf.push(Command::UpdateBuffer(buf, ptr, offset_bytes));
    }

    fn update_texture(&mut self,
                      dst: TextureCopyRegion<NewTexture>,
                      data: &[u8]) {
        let ptr = self.data.add(data);
        match dst.texture {
            NewTexture::Texture(raw) => {
                let dst = dst.with_texture(raw);
                self.buf.push(Command::UpdateTexture(dst, ptr))
            }
            NewTexture::Surface(s) => {
                error!("GL: unable to update the contents of a Surface({})", s)
            }
        }
    }

    fn generate_mipmap(&mut self, srv: ResourceView) {
        self.buf.push(Command::GenerateMipmap(srv));
    }

    fn clear_color(&mut self, target: TargetView, value: command::ClearColor) {
        // this could be optimized by deferring the actual clear call
        let mut pts = c::pso::PixelTargetSet::new();
        pts.colors[0] = Some(target);
        self.bind_pixel_targets(pts);
        self.buf.push(Command::Clear(Some(value), None, None));
    }

    fn clear_depth_stencil(&mut self,
                           target: TargetView,
                           depth: Option<Depth>,
                           stencil: Option<Stencil>) {
        let mut pts = c::pso::PixelTargetSet::new();
        if depth.is_some() {
            pts.depth = Some(target);
        }
        if stencil.is_some() {
            pts.stencil = Some(target);
        }
        self.bind_pixel_targets(pts);
        self.buf.push(Command::Clear(None, depth, stencil));
    }

    fn call_draw(&mut self,
                 start: c::VertexCount,
                 count: c::VertexCount,
                 instances: Option<command::InstanceParams>) {
        self.buf.push(Command::Draw(self.cache.primitive, start, count, instances));
    }

    fn call_draw_indexed(&mut self,
                         start: c::VertexCount,
                         count: c::VertexCount,
                         base: c::VertexCount,
                         instances: Option<command::InstanceParams>) {
        let (offset, gl_index) = match self.cache.index_type {
            c::IndexType::U16 => (start * 2u32, gl::UNSIGNED_SHORT),
            c::IndexType::U32 => (start * 4u32, gl::UNSIGNED_INT),
        };
        self.buf.push(
                  Command::DrawIndexed(
                      self.cache.primitive,
                      gl_index,
                      RawOffset(offset as *const gl::types::GLvoid),
                      count,
                      base,
                      instances));
    }
}