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
use gl::{self, Renderer2d};
use gl::types::*;
use gl::traits::Renderer;
use graphics::{self, CoordinateOrigin};
use texturepool::TexturePool;
use std::collections::HashMap;
use na::*;
use std::mem;
use util;
use gl::RenderSurface;

#[cfg(not(target_os="android"))]
use gst;

macro_rules! GST_ROUND_UP_8(
    ($num:expr) => ((($num)+7)&!7)
);

macro_rules! GST_ROUND_UP_4(
    ($num:expr) => ((($num)+3)&!3)
);

macro_rules! GST_ROUND_UP_2(
    ($num:expr) => ((($num)+1)&!1)
);

#[inline]
fn calc_stride_width(stride: i32, width: i32, stride_mult: i32) -> Option<i32>{
	unsafe{
		if GST_ROUND_UP_8!(width*stride_mult) == stride {
			gl::PixelStorei (gl::UNPACK_ALIGNMENT, 8);
		} else if GST_ROUND_UP_4!(width*stride_mult) == stride {
			gl::PixelStorei (gl::UNPACK_ALIGNMENT, 4);
		} else if GST_ROUND_UP_2!(width*stride_mult) == stride {
			gl::PixelStorei (gl::UNPACK_ALIGNMENT, 2);
		} else if width*stride_mult == stride {
			gl::PixelStorei (gl::UNPACK_ALIGNMENT, 1);
		} else {
			if GST_ROUND_UP_8!(stride*stride_mult) == stride {
				gl::PixelStorei (gl::UNPACK_ALIGNMENT, 8);
			} else if GST_ROUND_UP_4!(stride*stride_mult) == stride {
				gl::PixelStorei (gl::UNPACK_ALIGNMENT, 4);
			} else if GST_ROUND_UP_2!(stride*stride_mult) == stride {
				gl::PixelStorei (gl::UNPACK_ALIGNMENT, 2);
			} else if stride*stride_mult == stride {
				gl::PixelStorei (gl::UNPACK_ALIGNMENT, 1);
			} else {
				return None;
			}
			return Some(stride)
		}
	}
    Some(width)
}

#[cfg(not(target_os="android"))]
#[inline]
fn texture_from_plane(context_id: usize, gl: &gl::CreationProxy, plane: gst::VideoPlane, stride_mult: i32, internal: GLenum, format: GLenum) -> gl::Texture{
    let stride = plane.stride();
    let width = plane.width();
    let tex_w = calc_stride_width(stride,width,stride_mult).unwrap() as u32;
    let tex_h = plane.height() as u32;
    let mut texture = TexturePool::new_texture(context_id, gl, gl::texture::Format{
        target: gl::TEXTURE_2D,
        internal_format: internal,
        width: tex_w,
        height: tex_h,
        levels: 1,
        samples: 0,
    });
    texture.load_data(plane.data::<u8>(), gl::texture::DataFormat{
        format: format,
        ty: gl::UNSIGNED_BYTE,
        .. Default::default()
    }).unwrap();
    texture
}

#[cfg(target_os="android")]
#[inline]
unsafe fn texture_from_plane(pixels: &[u8], width: i32, height: i32, plane: i32, stride_mult: i32, internal: GLenum, format: GLenum, video_format: &str) -> (gl::Texture,f32){
    match video_format{
        "NV12" | "NV21" =>{
            calc_stride_width(width,width,width,1);
            let mut texture = TexturePool::new_texture(width/stride_mult,height/stride_mult,internal,format,gl::TEXTURE_2D);
            texture.update_data(pixels.slice_from((plane*width*height) as uint),width/stride_mult,height/stride_mult,format);
            (texture, 1.0 as f32)//(stride_width as f64 / c_w as f64) as f32)
        }

        "Y444" | "I420" | "YV12" | "Y42B" | "Y41B" =>{
            calc_stride_width(width,width,width,1);
            let mut texture = TexturePool::new_texture(width,height,internal,format,gl::TEXTURE_2D);
            let offset = match plane{
                0 => 0,
                1 => width*2*height*2,
                2 => width*2*height*2 + width*height,
                _ => fail!("this shouldn't happen")
            };
            texture.update_data(pixels.slice_from(offset as uint),width,height,format);
            (texture, 1.0 as f32)//(stride_width as f64 / c_w as f64) as f32)
        }

        _ => {fail!("this shouldn't happen")}
    }
}

pub struct Frame{
    textures: Vec<gl::Texture>,
    planes_index: HashMap<String,usize>,
    format: String,
    #[cfg(not(target_os="android"))]
    gst_frame: gst::VideoFrame,
    context_id: usize,
}

#[cfg(not(target_os="android"))]
impl Drop for Frame{
	fn drop(&mut self){
        let mut textures = Vec::new();
        mem::swap(&mut textures, &mut self.textures);
        for texture in textures{
            if self.format == "GRAY8".to_string(){
				unsafe{
					gl::TexParameteri(texture.target(), gl::TEXTURE_SWIZZLE_G, gl::GREEN as GLint);
					gl::TexParameteri(texture.target(), gl::TEXTURE_SWIZZLE_B, gl::BLUE as GLint);
				}
            }
            TexturePool::return_texture(self.context_id, texture);
        }
	}
}

#[cfg(target_os="android")]
impl Drop for Frame{
	fn drop(&mut self){
        for texture in self.textures.iter(){
            TexturePool::return_texture(texture.clone());
        }
	}
}

impl Frame{
    #[cfg(not(target_os="android"))]
    pub fn new(context_id: usize, gl: &gl::CreationProxy, sample: gst::Sample) -> util::Result<Frame>{
        let gstframe = match sample.video_frame(){
            Some(vf) => vf,
            None => return Err(util::Error::new("Sample without video frame"))
        };

        let format = unsafe{from_c_str!(gstframe.format_info().name)};

        let mut planes_index = HashMap::new();

        let textures = match format{
            "RGB"   => {
                let texture_rgb = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),3,gl::RGB8,gl::RGB);
                vec![texture_rgb]
            }

            "RGBA"   => {
                let texture_rgba = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),4,gl::RGBA8,gl::RGBA);
                vec![texture_rgba]
            }

            "BGR"   => {
                let texture_bgr = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),3,gl::RGB8,gl::BGR);
                vec![texture_bgr]
            }

            "BGRA"   => {
                let texture_bgra = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),4,gl::RGBA8,gl::BGRA);
                vec![texture_bgra]
            }

            "GRAY8"  =>{
                let texture = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),4,gl::R8,gl::RED);
                unsafe{
                	gl::BindTexture(texture.target(),texture.id());
	                gl::TexParameteri(texture.target(), gl::TEXTURE_SWIZZLE_G, gl::RED as GLint);
	                gl::TexParameteri(texture.target(), gl::TEXTURE_SWIZZLE_B, gl::RED as GLint);
	            }
                vec![texture]
            }

            "NV12" | "NV21" => {
                let texture_y = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),1,gl::R8,gl::RED);
                let texture_uv = texture_from_plane(context_id, gl, gstframe.plane(1).unwrap(),2,gl::RG8,gl::RG);

                planes_index.insert("Y".to_string(),0);
                planes_index.insert("UV".to_string(),1);
                vec![texture_y,texture_uv]
            }

            "Y444" | "I420" | "YV12" | "Y42B" | "Y41B" =>{

                let texture_y = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),1,gl::R8,gl::RED);
                let texture_u = texture_from_plane(context_id, gl, gstframe.plane(1).unwrap(),1,gl::R8,gl::RED);
                let texture_v = texture_from_plane(context_id, gl, gstframe.plane(2).unwrap(),1,gl::R8,gl::RED);

                planes_index.insert("Y".to_string(),0);
                planes_index.insert("U".to_string(),1);
                planes_index.insert("V".to_string(),2);
                vec![texture_y,texture_u,texture_v]
            }

            "YUY2" =>{
                let texture_yuv = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),2,gl::RG8,gl::RG);

                planes_index.insert("YUV".to_string(),0);
                vec![texture_yuv]
            }

            "AYUV" => {
                let texture_ayuv = texture_from_plane(context_id, gl, gstframe.plane(0).unwrap(),4,gl::RGBA8,gl::RGBA);

                planes_index.insert("AYUV".to_string(),0);
                vec![texture_ayuv]
            }
            _ => { return Err(util::Error::new(&format!("Format {} not supported", format))) }
        };

        Ok(Frame{
            gst_frame: gstframe,
		    textures: textures,
		    planes_index: planes_index,
		    format: format.to_string(),
            context_id,
        })
    }

    #[cfg(target_os="android")]
    pub fn new(pixels: &[u8], width: i32, height: i32, format: &str) -> util::Result<Frame>{
        match format{
            "NV12" | "NV21" => {
                unsafe{
                    let (textureY,strideY) = texture_from_plane(pixels,width,height,0,1,gl::ALPHA,gl::ALPHA,format);

                    let (textureUV,strideUV) = texture_from_plane(pixels,width,height,1,2,gl::LUMINANCE_ALPHA,gl::LUMINANCE_ALPHA,format);

                    let mut planes_index = HashMap::new();
                    planes_index.insert("Y".to_string(),0);
                    planes_index.insert("UV".to_string(),1);

                    Some(Frame{ textures: vec!(textureY,textureUV),
                            strides: vec!(strideY,strideUV),
                            planes_index: planes_index,
                            format: format.to_string(),
                            vi: vi
                        })
                }
            }

            "Y444" | "I420" | "YV12" | "Y42B" | "Y41B" =>{
                unsafe{
                    let (textureY,strideY) = texture_from_plane(pixels,width,height,0,1,gl::ALPHA,gl::ALPHA,format);

                    let (textureU,strideU) = texture_from_plane(pixels,width/2,height/2,1,1,gl::ALPHA,gl::ALPHA,format);

                    let (textureV,strideV) = texture_from_plane(pixels,width/2,height/2,2,1,gl::ALPHA,gl::ALPHA,format);

                    let mut planes_index = HashMap::new();
                    planes_index.insert("Y".to_string(),0);
                    planes_index.insert("U".to_string(),1);
                    planes_index.insert("V".to_string(),2);

                    Some(Frame{ textures: vec!(textureY,textureU,textureV),
                            strides: vec!(strideY,strideU,strideV),
                            planes_index: planes_index,
                            format: format.to_string(),
                            vi: vi
                        })
                }
            }

            _ => None
        }
    }

    #[cfg(not(target_os="android"))]
    pub fn len(&self) -> u64{
        self.gst_frame.buffer().size()
    }

    #[cfg(not(target_os="android"))]
    pub fn data<'a>(&'a self) -> &'a [u8]{
        self.gst_frame.buffer().map_read(|map| map.data()).unwrap()
    }

    pub fn texture(&self) -> Option<& gl::Texture>{
        if !self.textures.is_empty(){
            Some(&self.textures[0])
        }else{
            None
        }
    }

    pub fn tex_for_plane(&self, plane: &str) -> Option<& gl::Texture>{
        self.planes_index.get(&plane.to_string()).map(|idx| &self.textures[*idx])
    }

    pub fn format(&self) -> &str{
        &self.format
    }

    #[cfg(not(target_os="android"))]
    pub fn width(&self) -> i32{
        self.gst_frame.width()
    }

    #[cfg(not(target_os="android"))]
    pub fn height(&self) -> i32{
        self.gst_frame.height()
    }

    #[cfg(not(target_os="android"))]
    pub fn size(&self) -> Vec2<i32>{
        vec2(self.width(), self.height())
    }

    #[cfg(target_os="android")]
    pub fn width(&self) -> i32{
        self.textures[0].width()
    }

    #[cfg(target_os="android")]
    pub fn height(&self) -> i32{
        self.textures[0].height()
    }

    pub fn tex_scale(&self, plane: &str) -> Option<[f32;2]>{
        self.planes_index.get(&plane.to_string())
        	.map(|idx| {
                let plane = self.gst_frame.plane(*idx as u32).unwrap();
                let stride = plane.stride();
                let w = plane.width();
                [stride as f32 / w as f32, 1.0]
            })
    }
}

pub struct Material<'a>{
    _frame: &'a Frame,
    uniforms: Vec<gl::program::Uniform>,
    frame_format: String,
}

impl<'a> Material<'a>{
    pub fn new(frame: &Frame) -> Material{
        Material{
            _frame: frame,
            uniforms: get_uniforms_for(frame),
            frame_format: frame.format().to_owned()
        }
    }
}

impl<'a> gl::Material for Material<'a>{
    fn program<R: RenderSurface>(&self, gl: &gl::Renderer<R>) -> &gl::Program{
        get_program_for(gl, &self.frame_format)
    }

    fn uniforms<R: RenderSurface>(&self, gl: &gl::Renderer<R>) -> Vec<gl::program::Uniform>{
        self.uniforms.clone()
    }

    fn properties(&self) -> Vec<gl::Property>{
    	vec![]
    }
}

impl<'a> gl::Render2d for &'a Frame{
    type Material = Material<'a>;
    fn default_material(&self) -> Material<'a>{
        Material::new(self)
    }

    fn render_with_material<R: RenderSurface, M: gl::Material>(&self, gl: &gl::Renderer<R>, pos: &Pnt2, material: &M){
        self.render_size_with_material(gl, pos, &convert(self.size()), material);
    }

    fn render_size_with_material<R: RenderSurface, M: gl::Material>(&self, gl: &gl::Renderer<R>, pos: &Pnt2, size: &Vec2, material: &M){
        let quad = match gl.origin() {
            CoordinateOrigin::TopLeft | CoordinateOrigin::CenterDown =>
                graphics::rectangle_texcoords(pos.x,pos.y,size.x,size.y,0.0,1.0,1.0,0.0),
            CoordinateOrigin::BottomLeft | CoordinateOrigin::CenterUp =>
                graphics::rectangle_texcoords(pos.x,pos.y,size.x,size.y,0.0,0.0,1.0,1.0),
        };
        gl.draw_mesh_with_material(&quad, material);
    }
}

fn get_program_for<R: RenderSurface>(gl: &gl::Renderer<R>, frame_format: &str) -> &'static gl::Program{
    match frame_format{
        "NV12"  => get_program_nv(gl),
        "NV21"  => get_program_nv(gl),
		"Y444" | "I420" | "YV12" | "Y42B" | "Y41B" => get_program_yuv(gl),
        "YUY2" => get_program_yuy2(gl),
        "AYUV" => get_program_ayuv(gl),
        _       => get_program_rgba(gl),
    }
}

fn get_uniforms_for(frame: &Frame) -> Vec<gl::program::Uniform>{
    match &*frame.format(){
        "NV12"  => uniforms!{
            Ytex: (frame.tex_for_plane("Y").unwrap(),0),
            UVtex: (frame.tex_for_plane("UV").unwrap(),1),
            tex_scale0: frame.tex_scale("Y").unwrap(),
            tex_scale1: frame.tex_scale("UV").unwrap()
        },

        "NV21"  => uniforms!{
            Ytex: (frame.tex_for_plane("Y").unwrap(),0),
            UVtex: (frame.tex_for_plane("UV").unwrap(),1),
            tex_scale0: frame.tex_scale("Y").unwrap(),
            tex_scale1: frame.tex_scale("UV").unwrap()
        },

        "Y444" | "I420" | "YV12" | "Y42B" | "Y41B" => uniforms!{
            Ytex: (frame.tex_for_plane("Y").unwrap(),0),
            Utex: (frame.tex_for_plane("U").unwrap(),1),
            Vtex: (frame.tex_for_plane("V").unwrap(),2),
            tex_scale0: frame.tex_scale("Y").unwrap(),
            tex_scale1: frame.tex_scale("U").unwrap(),
            tex_scale2: frame.tex_scale("V").unwrap()
        },

        "YUY2" => uniforms!{
            YUVtex: (frame.tex_for_plane("YUV").unwrap(),0),
            onePixel: 1.0/frame.width() as f32,
            textureWidth: frame.width() as f32
        },

        "AYUV" => uniforms!{
            AYUVtex: frame.tex_for_plane("AYUV").unwrap(),
            tex_scale0: frame.tex_scale("AYUV").unwrap()
        },

        _  => uniforms!{tex: frame.texture().unwrap()}
    }
}

program_cache!("../gl/shaders/vertex_video_frame.glsl", "../gl/shaders/fragment_video_rgba.glsl", get_program_rgba, SHADER_RGBA);
program_cache!("../gl/shaders/vertex_video_frame.glsl", "../gl/shaders/fragment_video_AYUV.glsl", get_program_ayuv, SHADER_AYUV);
program_cache!("../gl/shaders/vertex_video_frame.glsl", "../gl/shaders/fragment_video_planar_YUV.glsl", get_program_yuv, SHADER_YUV);
program_cache!("../gl/shaders/vertex_video_frame.glsl", "../gl/shaders/fragment_video_NV12_NV21.glsl", get_program_nv, SHADER_NV);
program_cache!("../gl/shaders/vertex_video_frame.glsl", "../gl/shaders/fragment_video_YUY2.glsl", get_program_yuy2, SHADER_YUY2);



#[cfg(target_os="android")]
mod shaders{
    pub static VERTEX_SHADER_DEFAULT: &'static str = "
    precision lowp float;

    attribute vec3 position;
    attribute vec2 texcoord;
    attribute vec4 color;
    attribute vec3 normal;

    varying vec4 colorVarying;
    varying vec2 texcoordVarying;
    varying vec3 normalVarying;

    uniform mat4 modelViewProjectionMatrix;

    void main() {
        colorVarying = color;
        texcoordVarying = texcoord;
        normalVarying = normal;

        gl_Position = modelViewProjectionMatrix * vec4(position,1.0);
    }";

    pub static FRAGMENT_RGBA: &'static str = "
    precision lowp float;
    varying vec2 texcoordVarying;

    uniform sampler2D tex;

    void main() {
       vec4 color = texture2D(tex,texcoordVarying);
       gl_FragColor = color;
    }";

 /** AYUV to RGB conversion */
    pub static FRAGMENT_AYUV: &'static str="
            precision lowp float;
          varying vec2 texcoordVarying;
          uniform sampler2D AYUVtex;
          uniform vec2 tex_scale0;
          uniform vec2 tex_scale1;
          uniform vec2 tex_scale2;
          const vec3 offset = vec3(-0.0625, -0.5, -0.5);
          const vec3 rcoeff = vec3(1.164, 0.000, 1.596);
          const vec3 gcoeff = vec3(1.164,-0.391,-0.813);
          const vec3 bcoeff = vec3(1.164, 2.018, 0.000);
          void main(void) {
            float r,g,b;
            vec3 yuv;
            yuv  = texture2D(AYUVtex, texcoordVarying / tex_scale0).gba;
            yuv += offset;
            r = dot(yuv, rcoeff);
            g = dot(yuv, gcoeff);
            b = dot(yuv, bcoeff);
            gl_FragColor=vec4(r,g,b,texture2D(AYUVtex, texcoordVarying / tex_scale0).r);
          }";

    /* Planar YUV converters */

    /** YUV to RGB conversion */
    pub static FRAGMENT_PLANAR_YUV: &'static str="
        precision lowp float;
        varying vec2 texcoordVarying;
        uniform sampler2D Ytex,Utex,Vtex;
        uniform vec2 tex_scale0;
        uniform vec2 tex_scale1;
        uniform vec2 tex_scale2;
        const vec3 offset = vec3(-0.0625, -0.5, -0.5);
        const vec3 rcoeff = vec3(1.164, 0.000, 1.596);
        const vec3 gcoeff = vec3(1.164,-0.391,-0.813);
        const vec3 bcoeff = vec3(1.164, 2.018, 0.000);
        void main(void) {
            float r,g,b;
            vec3 yuv;
            yuv.x=texture2D(Ytex,texcoordVarying / tex_scale0).a;
            yuv.y=texture2D(Utex,texcoordVarying / tex_scale1).a;
            yuv.z=texture2D(Vtex,texcoordVarying / tex_scale2).a;
            yuv += offset;
            r = dot(yuv, rcoeff);
            g = dot(yuv, gcoeff);
            b = dot(yuv, bcoeff);
            gl_FragColor=vec4(r,g,b,1.0);
        }";

    /** NV12/NV21 to RGB conversion */
    pub static FRAGMENT_NV12_NV21_YUV: &'static str="
          precision lowp float;
          varying vec2 texcoordVarying;
          uniform sampler2D Ytex,UVtex;
          uniform vec2 tex_scale0;
          uniform vec2 tex_scale1;
          uniform vec2 tex_scale2;
          const vec3 offset = vec3(-0.0625, -0.5, -0.5);
          const vec3 rcoeff = vec3(1.164, 0.000, 1.596);
          const vec3 gcoeff = vec3(1.164,-0.391,-0.813);
          const vec3 bcoeff = vec3(1.164, 2.018, 0.000);
          void main(void) {
            float r,g,b;
            vec3 yuv;
            yuv.x=texture2D(Ytex,texcoordVarying / tex_scale0).a;
            yuv.yz=texture2D(UVtex,texcoordVarying / tex_scale1).{}{};
            yuv += offset;
            r = dot(yuv, rcoeff);
            g = dot(yuv, gcoeff);
            b = dot(yuv, bcoeff);
            gl_FragColor=vec4(r,g,b,1.0);
          }";

    pub static FRAGMENT_SHADER_VFRAME_EXTERNAL: &'static str = "
    //#extension GL_OES_EGL_image_external : require
    precision lowp float;
    varying vec2 texcoordVarying;

    uniform sampler2D tex;

    void main() {
       vec4 color = texture2D(tex,texcoordVarying);
       gl_FragColor = color;
    }";
}