2015年4月8日 星期三

YUY420toRGB24 Shader

//shader
uniform sampler2D y_tex;
uniform sampler2D u_tex;
uniform sampler2D v_tex;
varying mediump vec2 vTexCoord;

const mediump vec3 R_cf = vec3(1.164383,  0.000000,  1.596027);
const mediump vec3 G_cf = vec3(1.164383, -0.391762, -0.812968);
const mediump vec3 B_cf = vec3(1.164383,  2.017232,  0.000000);
const mediump vec3 offset = vec3(-0.0625, -0.5, -0.5);
  
void main()
{
    precision mediump float;
    float y = texture2D(y_tex, vTexCoord).a;
    float u = texture2D(u_tex, vTexCoord).a;
    float v = texture2D(v_tex, vTexCoord).a;
    vec3 yuv = vec3(y,u,v);
    yuv += offset;
    gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
    gl_FragColor.r = dot(yuv, R_cf);
    gl_FragColor.g = dot(yuv, G_cf);
    gl_FragColor.b = dot(yuv, B_cf);
} 

//init
 result = kzuSharedImageTextureCreate(kzuUIDomainGetResourceManager(kzuObjectNodeGetUIDomain(layerNode)), "video Y texture", 
   KZU_TEXTURE_CHANNELS_ALPHA, VideoGetWidth(video), VideoGetHeight(video), KZ_NULL,
   KZ_NULL, KZ_FALSE, &videoPlayer->y_texture);

 kzuTextureSetFilter(kzuSharedImageTextureToTexture(videoPlayer->y_texture), KZU_TEXTURE_FILTER_POINT_SAMPLE);

 result = kzuSharedImageTextureCreate(kzuUIDomainGetResourceManager(kzuObjectNodeGetUIDomain(layerNode)), "video U texture", 
   KZU_TEXTURE_CHANNELS_ALPHA, VideoGetWidth(video)/2, VideoGetHeight(video)/2, KZ_NULL,
   KZ_NULL, KZ_FALSE, &videoPlayer->u_texture);

 kzuTextureSetFilter(kzuSharedImageTextureToTexture(videoPlayer->u_texture), KZU_TEXTURE_FILTER_POINT_SAMPLE);

 result = kzuSharedImageTextureCreate(kzuUIDomainGetResourceManager(kzuObjectNodeGetUIDomain(layerNode)), "video V texture", 
   KZU_TEXTURE_CHANNELS_ALPHA, VideoGetWidth(video)/2, VideoGetHeight(video)/2, KZ_NULL,
   KZ_NULL, KZ_FALSE, &videoPlayer->v_texture);

 kzuTextureSetFilter(kzuSharedImageTextureToTexture(videoPlayer->v_texture), KZU_TEXTURE_FILTER_POINT_SAMPLE);

 struct KzuPropertyType* y_tex = kzuPropertyRegistryFindPropertyType("y_tex");
 struct KzuPropertyType* u_tex = kzuPropertyRegistryFindPropertyType("u_tex");
 struct KzuPropertyType* v_tex = kzuPropertyRegistryFindPropertyType("v_tex");
 
 result = kzuObjectNodeSetResourceIDResourceProperty(layerNode, y_tex,  kzuSharedImageTextureToResource(videoPlayer->y_texture));
 result = kzuObjectNodeSetResourceIDResourceProperty(layerNode, u_tex,  kzuSharedImageTextureToResource(videoPlayer->u_texture));
 result = kzuObjectNodeSetResourceIDResourceProperty(layerNode, v_tex,  kzuSharedImageTextureToResource(videoPlayer->v_texture));


//update
  result = kzuSharedImageTextureLock(videoplayer->y_texture);
  result = kzuSharedImageTextureLock(videoplayer->u_texture);
  result = kzuSharedImageTextureLock(videoplayer->v_texture);
  kzsErrorForward(result);
  result = kzuSharedImageTextureUpdate(videoplayer->y_texture, (kzByte*)data[0], videoWidth * videoHeight);
  kzsErrorForward(result);

  result = kzuSharedImageTextureUpdate(videoplayer->u_texture, (kzByte*)data[1], videoWidth * videoHeight / 4);
  kzsErrorForward(result);
  result = kzuSharedImageTextureUpdate(videoplayer->v_texture, (kzByte*)data[2], videoWidth * videoHeight / 4);
  kzsErrorForward(result);
  result = kzuSharedImageTextureUnlock(videoplayer->y_texture);
  result = kzuSharedImageTextureUnlock(videoplayer->u_texture);
  result = kzuSharedImageTextureUnlock(videoplayer->v_texture);
  kzsErrorForward(result);