Example #1
0
static void EXPORT_in_polygon(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) {
    ERROR_ASSERT(nargs == 3);

    ERROR_ASSERT(o[2].type == MSGPACK_OBJECT_ARRAY);

    msgpack_object *p = o[2].via.array.ptr;
    size_t len = o[2].via.array.size;
    uint64_t *vertexes = slab_alloc(len * sizeof(vertexes[0]));
    for (size_t i = 0; i < len; i++) {
        ERROR_ASSERT(p[i].type == MSGPACK_OBJECT_POSITIVE_INTEGER);
        vertexes[i] = p[i].via.u64;
    }

    struct polygon *polygon = polygon_make((struct point *) vertexes, len);
    char *binstr = dump_binstrescape((const char *) polygon, polygon_size(polygon));

    uts_printf_concat(s, "cb_point_in_polygon((uint64_t) ");
    crabql_generate_code(crabql, &o[1], s);
    uts_printf_concat(s, ", \"");
    utstring_bincpy(s, binstr, strlen(binstr));
    uts_printf_concat(s, "\")");

    slab_free(vertexes);
    slab_free(polygon);
    slab_free(binstr);
}
Example #2
0
static void EXPORT_get(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) {
    //    0          1             2             3          4
    // ['$get', 'source_key', 'bucket_name', table_id, 'target_field']

    ERROR_ASSERT(nargs == 5);

    ERROR_ASSERT(o[2].type == MSGPACK_OBJECT_RAW);
    ERROR_ASSERT(o[3].type == MSGPACK_OBJECT_POSITIVE_INTEGER || o[3].type == MSGPACK_OBJECT_NEGATIVE_INTEGER);
    ERROR_ASSERT(o[4].type == MSGPACK_OBJECT_RAW);

    char bucket_name[32];
    int64_t table_id;
    char field_name[32];

    mp_raw_strcpy(bucket_name, o[2], sizeof(bucket_name));
    table_id = o->via.i64;
    mp_raw_strcpy(field_name, o[4], sizeof(field_name));

    struct bucket *bucket = bucket_get(bucket_name, CAN_RETURN_NULL);
    struct table *table = bucket_get_table(bucket, table_id, CAN_RETURN_NULL);
    if (table) {
        lock_table(crabql, table);

        struct schema *schema = table->schema;
        struct field *field = schema_field_get(schema, field_name);

        // bsintableget

        (void) field;
        (void) s;
    }
}
//------------------------------------------------------------------------------
void GL::Program::SetMat4(const char* name, const GLfloat* data)
{
    ERROR_ASSERT(compiled_)
    
    this->Bind();
    GLint loc = glGetUniformLocation(program_, name);

    ERROR_ASSERT(loc != -1)

    glUniformMatrix4fv(loc, 1, GL_FALSE, data);
}
//------------------------------------------------------------------------------
void GL::Program::SetVec3(const char* name, const GLfloat* vec)
{
    ERROR_ASSERT(compiled_)
    
    this->Bind();
    GLint loc = glGetUniformLocation(program_, name);

    ERROR_ASSERT(loc != -1)

    glUniform3f(loc, vec[0], vec[1], vec[2]);    
}
//------------------------------------------------------------------------------
void GL::Program::SetIVec2(const char* name, const GLint* ivec)
{
    ERROR_ASSERT(compiled_)
    
    this->Bind();
    GLint loc = glGetUniformLocation(program_, name);

    ERROR_ASSERT(loc != -1)

    glUniform2i(loc, ivec[0], ivec[1]);    
}
//------------------------------------------------------------------------------
void GL::Program::SetInt(const char* name, GLint i)
{
    ERROR_ASSERT(compiled_)
    
    this->Bind();
    GLint loc = glGetUniformLocation(program_, name);

    ERROR_ASSERT(loc != -1)

    glUniform1i(loc, i);
}
Example #7
0
static void EXPORT_geo_from_latlon(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) {
    ERROR_ASSERT(nargs == 3);
    uts_printf_concat(s, "geo_from_latlon(");
    crabql_generate_code(crabql, &o[1], s);
    uts_printf_concat(s, ", ");
    crabql_generate_code(crabql, &o[2], s);
    uts_printf_concat(s, ")");
}
Example #8
0
static void EXPORT_in_circle(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) {
    ERROR_ASSERT(nargs == 3);
    ERROR_ASSERT(o[2].type == MSGPACK_OBJECT_ARRAY);
    ERROR_ASSERT(o[2].via.array.size == 2);
    ERROR_ASSERT(o[2].via.array.ptr[0].type == MSGPACK_OBJECT_POSITIVE_INTEGER);
    ERROR_ASSERT(o[2].via.array.ptr[1].type == MSGPACK_OBJECT_POSITIVE_INTEGER);

    struct circle *circle = circle_make(*(struct point *) &o[2].via.array.ptr[0].via.u64, o[2].via.array.ptr[1].via.u64);
    char *binstr = dump_binstrescape((const char *) circle, sizeof(struct circle));

    uts_printf_concat(s, "cb_point_in_circle((uint64_t) ");
    crabql_generate_code(crabql, &o[1], s);
    uts_printf_concat(s, ", \"");
    utstring_bincpy(s, binstr, strlen(binstr));
    uts_printf_concat(s, "\")");

    slab_free(circle);
    slab_free(binstr);
}
Example #9
0
static void EXPORT_mod(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) {
    ERROR_ASSERT(nargs == 3);
    uts_printf_concat(s, "(");
    crabql_generate_code(crabql, &o[2], s);
    uts_printf_concat(s, " ? ");
    crabql_generate_code(crabql, &o[1], s);
    uts_printf_concat(s, " %% ");
    crabql_generate_code(crabql, &o[2], s);
    uts_printf_concat(s, " : ");
    uts_printf_concat(s, " 0)");
}
Example #10
0
/* generate c code from crabql */
static void crabql_generate_code(struct crabql *crabql, msgpack_object *o, UT_string *s) {
    struct field *field;
    if (o == NULL) {
        uts_printf_concat(s, "-1LL");
        return;
    }
    switch (o->type) {
    case MSGPACK_OBJECT_NIL:
        uts_printf_concat(s, "0");
        break;
    case MSGPACK_OBJECT_BOOLEAN:
        uts_printf_concat(s, o->via.boolean ? "-1" : "0");
        break;
    case MSGPACK_OBJECT_POSITIVE_INTEGER:
        uts_printf_concat(s, "%"PRIu64"LL", o->via.u64);
        break;
    case MSGPACK_OBJECT_NEGATIVE_INTEGER:
        uts_printf_concat(s, "%"PRId64"LL", o->via.i64);
        break;
    case MSGPACK_OBJECT_DOUBLE:
        uts_printf_concat(s, "%.18lf", o->via.dec);
        break;
    case MSGPACK_OBJECT_RAW:
        if (mp_raw_eq(*o, "_table")) {
            uts_printf_concat(s, "(context->table)");
            break;
        }

        field = eval_field(crabql, o);
        ERROR_CHECK();
        make_field_data_get(field, s);
        break;
    case MSGPACK_OBJECT_ARRAY:
        if (o->via.array.size == 0) {
            uts_printf_concat(s, "0");
        }
        else {
            msgpack_object* p = o->via.array.ptr;

            if (p->type == MSGPACK_OBJECT_RAW && p->via.raw.size > 0 && p->via.raw.ptr[0] == '$') { // operator
                for (size_t i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
                    if (mp_raw_eq(*p, ops[i].name)) {
                        ops[i].func(crabql, p, s, o->via.raw.size);
                        return;
                    }
                }
                for (size_t i = 0; i < p->via.raw.size; i++)
                    putchar(p->via.raw.ptr[i]);
                putchar(10);
                ERROR_ASSERT(!"unknown operator");
            }
            else { // list
                uts_printf_concat(s, "(0");
                msgpack_object* const pend = o->via.array.ptr + o->via.array.size;
                for(; p < pend; ++p) {
                    uts_printf_concat(s, ", ");
                    crabql_generate_code(crabql, p, s);
                }
                uts_printf_concat(s, ")");
                return;
            }
        }
    case MSGPACK_OBJECT_MAP:
        ERROR_ASSERT(crabql->data_mode != DATA_MODE_NONE);

        msgpack_object_kv* p = o->via.map.ptr;
        msgpack_object_kv* const pend = o->via.map.ptr + o->via.map.size;

        uts_printf_concat(s, "(1");
        for(; p < pend; ++p) {
            ERROR_ASSERT(p->key.type == MSGPACK_OBJECT_RAW);

            ERROR_ASSERT(crabql->data_mode != DATA_MODE_NONE);
            ERROR_ASSERT(crabql->schema);
            field = eval_field(crabql, &p->key);
            ERROR_ASSERT(field);
            ERROR_CHECK();

            if (crabql->data_mode == DATA_MODE_READ) {
                uts_printf_concat(s, " && (");
                make_field_data_get(field, s);
                uts_printf_concat(s, " == ");
                crabql_generate_code(crabql, &p->val, s);
                uts_printf_concat(s, ")");
            }
            else if (crabql->data_mode == DATA_MODE_WRITE) { // WRITE
                uts_printf_concat(s, ", data_write(context, %p, ", field);
                crabql_generate_code(crabql, &p->val, s);
                uts_printf_concat(s, ")");
            }
        }
        uts_printf_concat(s, ")");
        return;
    default:
        // FIXME
        ERROR_ASSERT(0);
    }
}
Example #11
0
static void EXPORT_popcount(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) {
    ERROR_ASSERT(nargs == 2);
    uts_printf_concat(s, "popcountl(");
    crabql_generate_code(crabql, &o[1], s);
    uts_printf_concat(s, ")");
}
Example #12
0
static void EXPORT_in(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) {
    ERROR_ASSERT(nargs == 3);

    if (o[2].type == MSGPACK_OBJECT_ARRAY) {

        msgpack_object *p = o[2].via.array.ptr;
        uts_printf_concat(s, "bs64((int64_t) ");
        crabql_generate_code(crabql, &o[1], s);
        uts_printf_concat(s, ", \"");
        size_t len = o[2].via.array.size;
        int64_t *nums = slab_alloc(len * 8);
        for (size_t i = 0; i < len; i++) {
            ERROR_ASSERT(p[i].type == MSGPACK_OBJECT_POSITIVE_INTEGER || p[i].type == MSGPACK_OBJECT_NEGATIVE_INTEGER);
            if (p[i].type == MSGPACK_OBJECT_POSITIVE_INTEGER)
                ERROR_ASSERT(p[i].via.u64 < INT64_MAX);

            nums[i] = p[i].via.i64;
        }

        qsort(nums, len, 8, cmp64);

        //   \x01\x00\x00\x00\x00\x00\x00\x00

        char *binstr = dump_binstrescape((const char *) nums, len * sizeof(nums[0]));
        utstring_bincpy(s, binstr, strlen(binstr));
        slab_free(nums);
        slab_free(binstr);
        uts_printf_concat(s, "\", (size_t) %zu)", o[2].via.array.size);
    }
    else if (o[2].type == MSGPACK_OBJECT_RAW) {
        char *bucket_name;
        int64_t table_id;
        if (parse_bucket_and_table(o[2].via.raw.ptr, o[2].via.raw.size, &bucket_name, &table_id) == 0) {
            struct bucket *bucket = bucket_get(bucket_name, CAN_RETURN_NULL);
            free(bucket_name);

            if (bucket) {
                struct table *table = bucket_get_table(bucket, table_id, CAN_RETURN_NULL);
                if (table) {
                    lock_table(crabql, table);
                    struct schema *schema = table->schema;
                    if (schema) {
                        struct field *field = schema_field_get_primary(schema);

                        size_t datasize = ceildiv(schema->nbits, 8);

                        uts_printf_concat(s, "bsintable(");
                        crabql_generate_code(crabql, &o[1], s);
                        uts_printf_concat(s, ", (size_t) %pULL, (size_t) %pULL, (size_t) %zu, (size_t) %zu)", table->data, field, datasize, table->len);
                    }
                    else {
                        log_warn("schema is NULL, return 0.");
                        uts_printf_concat(s, "0");
                    }
                }
                else {
                    uts_printf_concat(s, "0");
                }
            }
            else {
                log_warn("bucket is NULL, return 0.");
                uts_printf_concat(s, "0");
            }
        }
        else {
            log_warn("cannot parse bucket and table");
            crabql->error = 34;
        }
    }
};
//------------------------------------------------------------------------------
void GL::Program::Unbind() const
{
    ERROR_ASSERT(compiled_)

    glUseProgram(0);
}
//------------------------------------------------------------------------------
void GL::Program::Bind() const
{
    ERROR_ASSERT(compiled_)

    glUseProgram(program_);
}
//------------------------------------------------------------------------------
APP::TextureAtlas::TextureAtlas(const std::string& font, unsigned int fontSize)
{
    // INIT FREETYPE AND CREATE FONT ATLAS

    // check if we have and OpenGL context
    ERROR_ASSERT(APP::IsInitialized())

    FT_Library library;

    // load the library
    FT_Error err = FT_Init_FreeType(&library);

    ERROR_ASSERT(err == 0)

    FT_Face face;

    // load the font (face)
    err = FT_New_Face(
            library,
            font.c_str(),
            0,
            &face 
        );

    ERROR_ASSERT(err == 0)

    FT_Set_Pixel_Sizes(face, 0, fontSize);    

    FT_GlyphSlot g = face->glyph;
    int atlasWidth = 0;
    int atlasHeight = 0;

    
    // compute the dimensions of the atlas
    for (unsigned int i = 32; i < 128; i++) 
    {
        if(FT_Load_Char(face, i, FT_LOAD_RENDER)) 
        {
            ERROR_WARNING("Loading character %c failed!")
            continue;
        }

        atlasWidth += g->bitmap.width;
        atlasHeight = std::max(atlasHeight, g->bitmap.rows);
    }

    unsigned char* atlas = new unsigned char[atlasWidth*atlasHeight];
    
    for (unsigned int i = 0; i < atlasHeight*atlasWidth; i++)
    {
        atlas[i] = 0;
    }

    int marker = 0;

    for (unsigned int i = 32; i < 128; i++) 
    {
        if(FT_Load_Char(face, i, FT_LOAD_RENDER)) 
        {
            continue;
        }
        
        int w = g->bitmap.width;
        int h = g->bitmap.rows;
        
        unsigned char* buffer = g->bitmap.buffer;
        
        // copy character to atlas
        for (unsigned int v = 0; v < h; v++)
        {
            for (unsigned int u = 0; u < w; u++)
            {
                int y = atlasHeight - 1 - v;
                int x = marker + u;
                atlas[y*atlasWidth + x] = buffer[v*w + u];
            }
        }

        // save character info
        bitmapWidth_[i]  = w;
        atlasOffX_[i] = marker;
        advances_[i] = Math::Vector2I(g->advance.x >> 6, g->advance.y >> 6);
        bearings_[i] = Math::Vector2I(g->bitmap_left, g->bitmap_top);

        // increase marker
        marker += w;
    }


    // initialize member
    atlas_ = new GL::Tex2DR8FR8UI(
            atlasWidth, 
            atlasHeight, 
            static_cast<void*>(atlas)
        );   

    width_ = atlasWidth;
    height_ = atlasHeight;


    // clean up
    delete[] atlas;
    FT_Done_FreeType(library);
}
//------------------------------------------------------------------------------
void createAtlasData(const std::string &font, unsigned int fontSize)
{
    // INIT FREETYPE AND CREATE FONT ATLAS

    ERROR_ASSERT(APP::IsInitialized())

    // load the library
    FT_Error err = FT_Init_FreeType(&library_);

    ERROR_ASSERT(err == 0)

    FT_Face face;

    // load the font (face)
    err = FT_New_Face(
            library_,
            font.c_str(),
            0,
            &face 
        );

    ERROR_ASSERT(err == 0)

    FT_Set_Pixel_Sizes(face, 0, fontSize);    

    FT_GlyphSlot g = face->glyph;
    int atlasWidth = 0;
    int atlasHeight = 0;
    int blMin = 0;
    int blMax = -1;
    
    // compute the dimensions of the atlas
    for (unsigned int i = 32; i < 128; i++) 
    {
        if(FT_Load_Char(face, i, FT_LOAD_RENDER)) 
        {
            ERROR_WARNING("Loading character %c failed!")
            continue;
        }
        
        atlasWidth += (g->bitmap.width + std::abs(g->bitmap_left));
        blMin = std::min(blMin, g->bitmap_top);

        if (g->bitmap_top > 0)
        {
            blMin = std::min(blMin, g->bitmap_top - g->bitmap.rows);
        }

        blMax = std::max(blMax, g->bitmap_top);

        atlasHeight = std::max(atlasHeight, g->bitmap.rows);
    }

    atlasHeight = blMax - blMin;

    int baseline = 0 - blMin;
    unsigned char* atlas = new unsigned char[atlasWidth*atlasHeight];
    int marker = 0;
    
    for (unsigned int i = 32; i < 128; i++) 
    {
        if(FT_Load_Char(face, i, FT_LOAD_RENDER)) 
        {
            continue;
        }
        
        int w = g->bitmap.width;
        int h = g->bitmap.rows;
        
        unsigned char* buffer = g->bitmap.buffer;
        
        // copy character to atlas
        for (unsigned int v = 0; v < h; v++)
        {
            for (unsigned int u = 0; u < w; u++)
            {
                int y = atlasHeight - (baseline + g->bitmap_top) + v;
                int x = marker + u  + std::abs(g->bitmap_left);
                atlas[y*atlasWidth + x] = buffer[v*w + u];
            }
        }

        // save character info
        characterInfo_[i].Width = w + std::abs(g->bitmap_left);
        characterInfo_[i].XOff0 = marker;

        // increase marker
        marker += (w + std::abs(g->bitmap_left));
    }

    // initialize the atlas
    atlas_.Atlas = new GL::Texture2DA(
        atlasWidth, 
        atlasHeight, 
        static_cast<void*>(atlas)
    );

    atlas_.Width = atlasWidth;
    atlas_.Height = atlasHeight;

    vertexArray_ = new GL::VertexArray();
    
    // clean up
    delete[] atlas;
    FT_Done_FreeType(library_);
}