Home | History | Annotate | Download | only in test
      1 #include <stdio.h>
      2 #include <math.h>
      3 #include <assert.h>
      4 #include <stdlib.h>
      5 
      6 #include <pixelflinger2/pixelflinger2_interface.h>
      7 
      8 int ApproximatelyEqual(const Vector4 lhs, const Vector4 rhs, const float t)
      9 {
     10    if (fabs(lhs.x - rhs.x) > t)
     11       return 0;
     12    if (fabs(lhs.y - rhs.y) > t)
     13       return 0;
     14    if (fabs(lhs.z - rhs.z) > t)
     15       return 0;
     16    if (fabs(lhs.w - rhs.w) > t)
     17       return 0;
     18    return 1;
     19 }
     20 
     21 extern void * llvmCtx;
     22 
     23 void contextless_test()
     24 {
     25    static const char vsGLSL [] =
     26       "uniform vec4 uVec4; \n"
     27       "uniform sampler2D sampler2d; \n"
     28       "attribute vec4 aPosition; \n"
     29       "attribute vec4 aTexCoord; \n"
     30       "varying vec4 vTexCoord; \n"
     31       "varying vec4 vTexColor; \n"
     32       "void main() { \n"
     33       "   gl_Position = aPosition; \n"
     34       "   vTexCoord = aTexCoord; \n"
     35       "   vTexColor = texture2D(sampler2d, aTexCoord.zw); \n"
     36       "   gl_PointSize = 432.0; \n"
     37       "}";
     38    gl_shader_t * vs = GGLShaderCreate(GL_VERTEX_SHADER);
     39    const char * infoLog = NULL;
     40    if (!GGLShaderCompile(vs, vsGLSL, &infoLog)) {
     41       printf("GGLShaderCompile vs failed:\n%s\n", infoLog);
     42       assert(0);
     43    }
     44    static const char fsGLSL [] =
     45       "uniform vec4 uVec4; \n"
     46       "uniform sampler2D sampler2d; \n"
     47       "varying vec4 vTexCoord; \n"
     48       "varying vec4 vTexColor; \n"
     49       "void main() { \n"
     50       "   gl_FragColor = texture2D(sampler2d, vTexCoord.zw); \n"
     51       "}";
     52    gl_shader_t * fs = GGLShaderCreate(GL_FRAGMENT_SHADER);
     53    if (!GGLShaderCompile(fs, fsGLSL, &infoLog)) {
     54       printf("GGLShaderCompile fs failed:\n%s\n", infoLog);
     55       assert(0);
     56    }
     57    gl_shader_program_t * prog = GGLShaderProgramCreate();
     58    unsigned glError = GL_NO_ERROR;
     59    glError = GGLShaderAttach(prog, vs);
     60    assert(GL_NO_ERROR == glError);
     61    glError = GGLShaderAttach(prog, fs);
     62    assert(GL_NO_ERROR == glError);
     63    GGLShaderAttributeBind(prog, 4, "aPosition");
     64    GGLShaderAttributeBind(prog, 5, "aTexCoord");
     65    if (!GGLShaderProgramLink(prog, &infoLog)) {
     66       printf("GGLShaderProgramLink failed:\n%s\n", infoLog);
     67       assert(0);
     68    }
     69 //   llvm::LLVMContext * llvmCtx = new llvm::LLVMContext();
     70    GGLState_t gglState = {0};
     71    unsigned texels0 [] = {0xff10ffff, 0x22222222, 0x66666666, 0xffffffff};
     72    GGLTexture_t texture0 = {GL_TEXTURE_2D, GGL_PIXEL_FORMAT_RGBA_8888,
     73                             2, 2, 1, // width, height, levelCount
     74                             texels0, GGL_CLAMP_TO_EDGE, GGL_CLAMP_TO_EDGE,
     75                             GGL_NEAREST, GGL_NEAREST
     76                            };
     77    gglState.textureState.textures[0] = texture0;
     78    gglState.textureState.textureData[0] = gglState.textureState.textures[0].levels;
     79    gglState.textureState.textureDimensions[0 * 2 + 0] = gglState.textureState.textures[0].width;
     80    gglState.textureState.textureDimensions[0 * 2 + 1] = gglState.textureState.textures[0].height;
     81    GGLShaderUse(llvmCtx, &gglState, prog);
     82 
     83    VertexInput_t input = {0, 0, 0, 0};
     84    input.attributes[4] = VECTOR4_CTR(0,0,0,1);
     85    input.attributes[5] = VECTOR4_CTR(0,0,0,0);
     86    VertexOutput_t output = {0};
     87    GGLProcessVertex(prog, &input, &output, NULL);
     88    int vTexColor = -1;
     89    GGLShaderVaryingLocation(prog, "vTexColor", &vTexColor);
     90    if (vTexColor >= 0) {
     91       if (memcmp(((Vector4 *)&output) + vTexColor, &VECTOR4_CTR(1,1,16/255.0f,1), sizeof(Vector4))) {
     92          puts("((Vector4 *)&output)[vTexColor] != Vector4(1,1,0,1)");
     93          assert(0);
     94       }
     95    } else {
     96       puts("vTexColor < 0");
     97       assert(0);
     98    }
     99 
    100    static const char fsGLSL1 [] =
    101       "uniform vec4 uVec4; \n"
    102       "uniform sampler2D sampler2d; \n"
    103       "varying vec4 vTexCoord; \n"
    104       "varying vec4 vTexColor; \n"
    105       "void main() { \n"
    106       "   gl_FragColor = vTexColor; \n"
    107       "}";
    108    gl_shader_t * fs1 = GGLShaderCreate(GL_FRAGMENT_SHADER);
    109    if (!GGLShaderCompile(fs1, fsGLSL1, &infoLog)) {
    110       printf("GGLShaderCompile fs failed:\n%s\n", infoLog);
    111       assert(0);
    112    }
    113    gl_shader_program_t * prog1 = GGLShaderProgramCreate();
    114    glError = GGLShaderAttach(prog1, vs);
    115    assert(GL_NO_ERROR == glError);
    116    glError = GGLShaderAttach(prog1, fs1);
    117    assert(GL_NO_ERROR == glError);
    118    GGLShaderAttributeBind(prog1, 1, "aPosition");
    119    GGLShaderAttributeBind(prog1, 2, "aTexCoord");
    120    if (!GGLShaderProgramLink(prog1, &infoLog)) {
    121       printf("GGLShaderProgramLink failed:\n%s\n", infoLog);
    122       assert(0);
    123    }
    124 
    125    GGLShaderUse(llvmCtx, &gglState, prog1);
    126    VertexInput_t input1 = {0};
    127    input1.attributes[1] = VECTOR4_CTR(1,1,0,1);
    128    input1.attributes[2] = VECTOR4_CTR(1,1,0,0);
    129    VertexOutput_t output1 = {0};
    130    GGLProcessVertex(prog1, &input1, &output1, NULL);
    131    int vTexCoord = -1;
    132    assert(2 == GGLShaderAttributeLocation(prog1, "aTexCoord"));
    133    GGLShaderVaryingLocation(prog1, "vTexCoord", &vTexCoord);
    134    if (vTexCoord >= 0) {
    135       if (memcmp(((Vector4 *)&output1) + vTexCoord, input1.attributes + 2, sizeof(Vector4))) {
    136          puts("((Vector4 *)&output1)[vTexCoord] != input1.attributes[1]");
    137          assert(0);
    138       }
    139    } else {
    140       puts("vTexCoord < 0");
    141       assert(0);
    142    }
    143 
    144    puts("***\n finished contextless_test \n***");
    145 
    146    GGLShaderProgramDelete(prog);
    147    GGLShaderProgramDelete(prog1);
    148 
    149    GLContextDctr();
    150 }
    151 
    152 int cmain(int argc, char **argv)
    153 {
    154    contextless_test();
    155 
    156    const char * infoLog = NULL;
    157 
    158    GGLInterface_t * ggl = CreateGGLInterface();
    159 
    160    gl_shader_t * shader0 = ggl->ShaderCreate(ggl, GL_VERTEX_SHADER);
    161    assert(shader0);
    162    const char * glsl0 =
    163       "uniform vec4 uVec4; \n"
    164       "uniform sampler2D sampler2d; \n"
    165       "attribute vec4 aPosition; \n"
    166       "attribute vec4 aTexCoord; \n"
    167       "varying vec4 vTexCoord; \n"
    168       "varying vec4 vTexColor; \n"
    169       "void main() { \n"
    170       "   gl_Position = aPosition; \n"
    171       "   vTexCoord = aTexCoord + uVec4; \n"
    172       "   vTexColor = texture2D(sampler2d, aTexCoord.zw); \n"
    173       "   gl_PointSize = 432; \n"
    174       "}";
    175    puts(glsl0);
    176    GLboolean compileStatus = ggl->ShaderCompile(ggl, shader0, glsl0, &infoLog);
    177    if (!compileStatus)
    178       fprintf(stderr, "failed to compile vertex shader 0, infoLog: \n %s \n", infoLog);
    179    assert(compileStatus);
    180 
    181    gl_shader_t * shader1 = ggl->ShaderCreate(ggl, GL_FRAGMENT_SHADER);
    182    assert(shader1);
    183    const char * glsl1 =
    184       "uniform vec4 uVec4; \n"
    185       "uniform sampler2D sampler2d; \n"
    186       "varying vec4 vTexCoord; \n"
    187       "varying vec4 vTexColor; \n"
    188       "void main() { \n"
    189       "   gl_FragColor = vTexCoord + vTexColor; \n"
    190       "}";
    191    puts(glsl1);
    192    compileStatus = ggl->ShaderCompile(ggl, shader1, glsl1, &infoLog);
    193    if (!compileStatus)
    194       fprintf(stderr, "failed to compile fragment shader 0, infoLog: \n %s \n", infoLog);
    195    assert(compileStatus);
    196 
    197    gl_shader_program_t * program0 = ggl->ShaderProgramCreate(ggl);
    198    assert(program0);
    199 
    200    ggl->ShaderAttach(ggl, program0, shader0);
    201    ggl->ShaderAttach(ggl, program0, shader1);
    202    ggl->ShaderAttributeBind(program0, 2, "aTexCoord");
    203    ggl->ShaderAttributeBind(program0, 3, "aPosition");
    204 
    205    GLboolean linkStatus = ggl->ShaderProgramLink(program0, &infoLog);
    206    if (!linkStatus)
    207       fprintf(stderr, "failed to link program 0, infoLog: \n %s \n", infoLog);
    208    assert(linkStatus);
    209 
    210    ggl->ShaderUse(ggl, program0);
    211 
    212    unsigned texels0 [] = {0xffffffff, 0x22222222, 0x66666666, 0xffffffff};
    213    GGLTexture_t texture0 = {GL_TEXTURE_2D, GGL_PIXEL_FORMAT_RGBA_8888,
    214                             2, 2, 1, // width, height, levelCount
    215                             texels0, GGL_CLAMP_TO_EDGE, GGL_MIRRORED_REPEAT, GGL_LINEAR, GGL_LINEAR
    216                            }; // levels, wrapS, wrapT, minFilter, magFilter
    217 
    218    int sampler2dLoc = ggl->ShaderUniformLocation(program0, "sampler2d");
    219    if (0 <= sampler2dLoc) {
    220       int samplerUnit = -1;
    221       //ggl->ShaderUniformGetiv(ggl, program0, sampler2dLoc, &samplerUnit);
    222       samplerUnit = sampler2dLoc;
    223       ggl->SetSampler(ggl, samplerUnit, &texture0);
    224    }
    225 
    226    Vector4 uVec4 = {1.125f, 1.5f, 1.75f, 1.75f};
    227    int uVec4Loc = ggl->ShaderUniformLocation(program0, "uVec4");
    228    ggl->ShaderUniform(program0, uVec4Loc, 1, &uVec4, GL_FLOAT_VEC4);
    229 
    230    VertexInput_t v0 = {0};
    231    v0.attributes[2] = VECTOR4_CTR(0,0,1,1); // aTexCoord
    232    v0.attributes[3] = VECTOR4_CTR(0.25f, 0.25f, 0.5f,1); // aPosition
    233 
    234    VertexOutput_t vout0 = {0};
    235    ggl->ProcessVertex(ggl, &v0, &vout0);
    236    if (memcmp(&vout0.position,&v0.attributes[3],sizeof(vout0.position))) {
    237       fprintf(stderr, "gl_Position != aPosition \n");
    238       assert(0);
    239    }
    240 
    241    int vTexCoordIndex = ggl->ShaderVaryingLocation(program0, "vTexCoord", NULL) - 2;
    242    VECTOR4_OP_UNARY(vout0.varyings[vTexCoordIndex],-=,uVec4);
    243    if (memcmp(&vout0.varyings[vTexCoordIndex],&v0.attributes[2],sizeof uVec4)) {
    244       fprintf(stderr, "vTexCoord != aTexCoord + uVec4 \n");
    245       assert(0);
    246    }
    247    Vector4 ones = {1,1,1,1};
    248    int vTexColorIndex = ggl->ShaderVaryingLocation(program0, "vTexColor", NULL) - 2;
    249    if (memcmp(&vout0.varyings[vTexColorIndex],&ones,sizeof ones)) { // should be the last texel color
    250       fprintf(stderr, "vTexColor != Vector4(1,1,1,1) \n");
    251       assert(0);
    252    }
    253    if (vout0.pointSize.x != 432) {
    254       fprintf(stderr, "gl_PointSize != 432 \n");
    255       assert(0);
    256    }
    257 
    258    v0.attributes[2] = VECTOR4_CTR(0,0, 1.5f, 1.5f);
    259    texture0.wrapS = GGL_REPEAT;
    260    texture0.wrapT = GGL_REPEAT;
    261 
    262    sampler2dLoc = ggl->ShaderUniformLocation(program0, "sampler2d");
    263    if (0 <= sampler2dLoc) {
    264       int samplerUnit = -1;
    265       //ggl->ShaderUniformGetiv(ggl, program0, sampler2dLoc, &samplerUnit);
    266       samplerUnit = sampler2dLoc;
    267       ggl->SetSampler(ggl, samplerUnit, &texture0);
    268    }
    269    ggl->ShaderUse(ggl, program0);
    270    ggl->ProcessVertex(ggl, &v0, &vout0);
    271    const float filtered = (float)(0xff + 0x22 + 0x66 + 0xff) / (4 * 0xff);
    272    if (!ApproximatelyEqual(vout0.varyings[vTexColorIndex],
    273                            VECTOR4_CTR(filtered, filtered, filtered, filtered), 1.0f / 255)) {
    274       fprintf(stderr, "failed linear filter and/or wrapS and wrapT test");
    275       assert(0);
    276    }
    277 
    278    const unsigned width = 60, height = 100;
    279 
    280    GGLSurface_t colorSurface = {width, height, GGL_PIXEL_FORMAT_RGBA_8888, malloc(width * height * 4)};
    281    assert(colorSurface.data);
    282    ggl->SetBuffer(ggl, GL_COLOR_BUFFER_BIT, &colorSurface);
    283 
    284    GGLSurface_t depthSurface = {width, height, GGL_PIXEL_FORMAT_Z_32, malloc(width * height * 4)};
    285    assert(depthSurface.data);
    286    ggl->SetBuffer(ggl, GL_DEPTH_BUFFER_BIT, &depthSurface);
    287 
    288    GGLSurface_t stencilSurface = {width, height, GGL_PIXEL_FORMAT_S_8, malloc(width * height * 1)};
    289    assert(stencilSurface.data);
    290    ggl->SetBuffer(ggl, GL_STENCIL_BUFFER_BIT, &stencilSurface);
    291 
    292    ggl->ClearColor(ggl, 0.1f, 0.1f, 0.1f, 1.0f);
    293    ggl->ClearDepthf(ggl, 0.5f);
    294 
    295 // TODO DXL test scanline and fs
    296 
    297    free(colorSurface.data);
    298    colorSurface.data = NULL;
    299    free(depthSurface.data);
    300    depthSurface.data = NULL;
    301    free(stencilSurface.data);
    302    stencilSurface.data = NULL;
    303 
    304    ggl->ShaderProgramDelete(ggl, program0);
    305 
    306    puts("*******************");
    307    puts("*** end of test ***");
    308    puts("*******************");
    309 
    310    DestroyGGLInterface(ggl);
    311    return 0;
    312 }
    313