58 lines
4.6 KiB
C
58 lines
4.6 KiB
C
/* Executes the production vertex/fragment strings with a 2D body surrogate.
|
|
* This tests shader UV/Fit/blend math, not Android OES decoder or Bitmap colors. */
|
|
#include <EGL/egl.h>
|
|
#include <EGL/eglext.h>
|
|
#include <GLES2/gl2.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
#include <assert.h>
|
|
static char *readfile(const char *path) {
|
|
FILE *f=fopen(path,"rb");assert(f);fseek(f,0,SEEK_END);long n=ftell(f);rewind(f);
|
|
char *p=calloc(n+1,1);assert(fread(p,1,n,f)==(size_t)n);fclose(f);return p;
|
|
}
|
|
static GLuint shader(GLenum type,const char *path) {
|
|
char *source=readfile(path);GLuint s=glCreateShader(type);glShaderSource(s,1,(const char **)&source,NULL);glCompileShader(s);free(source);
|
|
GLint ok;glGetShaderiv(s,GL_COMPILE_STATUS,&ok);if(!ok) {char log[4096];glGetShaderInfoLog(s,sizeof(log),NULL,log);fprintf(stderr,"%s\n",log);exit(2);}return s;
|
|
}
|
|
static void texture(GLuint id,int w,int h,const char *path) {
|
|
unsigned char *data=(unsigned char*)readfile(path);glBindTexture(GL_TEXTURE_2D,id);
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
|
|
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,w,h,0,GL_RGBA,GL_UNSIGNED_BYTE,data);free(data);
|
|
}
|
|
int main(int argc,char **v) {
|
|
assert(argc==16);
|
|
int bw=atoi(v[14]),bh=atoi(v[15]);assert(bw>0&&bh>0);
|
|
int pw=atoi(v[6]),ph=atoi(v[7]),w=atoi(v[11]),h=atoi(v[12]);float x=atof(v[8]),y=atof(v[9]),opacity=atof(v[10]);
|
|
PFNEGLGETPLATFORMDISPLAYEXTPROC platform=(void*)eglGetProcAddress("eglGetPlatformDisplayEXT");assert(platform);
|
|
EGLDisplay d=platform(EGL_PLATFORM_SURFACELESS_MESA,EGL_DEFAULT_DISPLAY,NULL);assert(d!=EGL_NO_DISPLAY);
|
|
EGLint major,minor;assert(eglInitialize(d,&major,&minor));assert(eglBindAPI(EGL_OPENGL_ES_API));
|
|
EGLint attrs[]={EGL_SURFACE_TYPE,EGL_PBUFFER_BIT,EGL_RENDERABLE_TYPE,EGL_OPENGL_ES2_BIT,EGL_RED_SIZE,8,EGL_GREEN_SIZE,8,EGL_BLUE_SIZE,8,EGL_ALPHA_SIZE,8,EGL_NONE};
|
|
EGLConfig config;EGLint count;assert(eglChooseConfig(d,attrs,&config,1,&count)&&count);
|
|
EGLint ca[]={EGL_CONTEXT_CLIENT_VERSION,2,EGL_NONE};EGLContext c=eglCreateContext(d,config,EGL_NO_CONTEXT,ca);assert(c!=EGL_NO_CONTEXT);
|
|
EGLint pa[]={EGL_WIDTH,w,EGL_HEIGHT,h,EGL_NONE};EGLSurface surface=eglCreatePbufferSurface(d,config,pa);assert(surface!=EGL_NO_SURFACE);
|
|
assert(eglMakeCurrent(d,surface,surface,c));
|
|
GLuint vertex=shader(GL_VERTEX_SHADER,v[1]),fragment=shader(GL_FRAGMENT_SHADER,v[2]);GLuint p=glCreateProgram();glAttachShader(p,vertex);glAttachShader(p,fragment);glLinkProgram(p);
|
|
GLint linked;glGetProgramiv(p,GL_LINK_STATUS,&linked);assert(linked);glUseProgram(p);
|
|
GLuint ids[2];glGenTextures(2,ids);glActiveTexture(GL_TEXTURE0);texture(ids[0],bw,bh,v[3]);glUniform1i(glGetUniformLocation(p,"uBody"),0);
|
|
glActiveTexture(GL_TEXTURE1);texture(ids[1],pw,ph,v[4]);glUniform1i(glGetUniformLocation(p,"uPatch"),1);
|
|
glUniform2f(glGetUniformLocation(p,"uBodySize"),bw,bh);
|
|
float scale=fminf((float)w/bw,(float)h/bh),mw=bw*scale,mh=bh*scale,left=(w-mw)/2,top=(h-mh)/2;
|
|
GLfloat pos[]={left/w*2-1,1-(top+mh)/h*2,(left+mw)/w*2-1,1-(top+mh)/h*2,left/w*2-1,1-top/h*2,(left+mw)/w*2-1,1-top/h*2};
|
|
GLfloat uv[]={0,0,1,0,0,1,1,1};
|
|
GLint a=glGetAttribLocation(p,"aPosition"),b=glGetAttribLocation(p,"aUv");glVertexAttribPointer(a,2,GL_FLOAT,GL_FALSE,0,pos);glEnableVertexAttribArray(a);glVertexAttribPointer(b,2,GL_FLOAT,GL_FALSE,0,uv);glEnableVertexAttribArray(b);
|
|
/* OES surrogate data arrives top-down; flip body Y just as SurfaceTexture does.
|
|
* A second case exercises a nontrivial crop transform; patch stays logical. */
|
|
GLfloat m[]={1,0,0,0,0,-1,0,0,0,0,1,0,0,1,0,1};
|
|
if(atoi(v[13])) {m[0]=.98f;m[5]=-.98f;m[12]=.01f;m[13]=.99f;}
|
|
glUniformMatrix4fv(glGetUniformLocation(p,"uTextureMatrix"),1,GL_FALSE,m);
|
|
glUniform4f(glGetUniformLocation(p,"uRect"),x,y,pw,ph);glUniform1f(glGetUniformLocation(p,"uOpacity"),opacity);
|
|
glViewport(0,0,w,h);glClearColor(1,1,1,1);glClear(GL_COLOR_BUFFER_BIT);glDrawArrays(GL_TRIANGLE_STRIP,0,4);glFinish();assert(glGetError()==GL_NO_ERROR);
|
|
unsigned char *out=malloc(w*h*4);glReadPixels(0,0,w,h,GL_RGBA,GL_UNSIGNED_BYTE,out);assert(glGetError()==GL_NO_ERROR);
|
|
FILE *f=fopen(v[5],"wb");assert(f&&fwrite(out,1,w*h*4,f)==(size_t)w*h*4);fclose(f);free(out);
|
|
printf("GLES vendor=%s renderer=%s\n",glGetString(GL_VENDOR),glGetString(GL_RENDERER));
|
|
glDeleteTextures(2,ids);glDeleteProgram(p);glDeleteShader(vertex);glDeleteShader(fragment);
|
|
eglMakeCurrent(d,EGL_NO_SURFACE,EGL_NO_SURFACE,EGL_NO_CONTEXT);eglDestroySurface(d,surface);eglDestroyContext(d,c);eglTerminate(d);return 0;
|
|
}
|