Ejemplo n.º 1
0
static void
create_pipelines(cg_pipeline_t **pipelines, int n_pipelines)
{
    int i;

    for (i = 0; i < n_pipelines; i++) {
        char *source = c_strdup_printf("  cg_color_out = "
                                       "vec4 (%f, 0.0, 0.0, 1.0);\n",
                                       i / 255.0f);
        cg_snippet_t *snippet = cg_snippet_new(CG_SNIPPET_HOOK_FRAGMENT,
                                               NULL, /* declarations */
                                               source);

        c_free(source);

        pipelines[i] = cg_pipeline_new(test_dev);
        cg_pipeline_add_snippet(pipelines[i], snippet);
        cg_object_unref(snippet);
    }

    /* Test that drawing with them works. This should create the entries
     * in the cache */
    for (i = 0; i < n_pipelines; i++) {
        cg_framebuffer_draw_rectangle(test_fb, pipelines[i], i, 0, i + 1, 1);
        test_cg_check_pixel_rgb(test_fb, i, 0, i, 0, 0);
    }
}
Ejemplo n.º 2
0
static void
lots_snippets(TestState *state)
{
    cg_pipeline_t *pipeline;
    cg_snippet_t *snippet;
    int location;
    int i;

    /* Lots of snippets on one pipeline */
    pipeline = cg_pipeline_new(test_dev);

    cg_pipeline_set_color4ub(pipeline, 0, 0, 0, 255);

    for(i = 0; i < 3; i++) {
        char letter = 'x' + i;
        char *uniform_name = c_strdup_printf("%c_value", letter);
        char *declarations = c_strdup_printf("uniform float %s;\n",
                                             uniform_name);
        char *code = c_strdup_printf("cg_color_out.%c = %s;\n",
                                     letter,
                                     uniform_name);

        location = cg_pipeline_get_uniform_location(pipeline, uniform_name);
        cg_pipeline_set_uniform_1f(pipeline, location,(i + 1) * 0.1f);

        snippet = cg_snippet_new(CG_SNIPPET_HOOK_FRAGMENT,
                                 declarations,
                                 code);
        cg_pipeline_add_snippet(pipeline, snippet);
        cg_object_unref(snippet);

        c_free(code);
        c_free(uniform_name);
        c_free(declarations);
    }

    cg_framebuffer_draw_rectangle(test_fb, pipeline, 30, 0, 40, 10);

    cg_object_unref(pipeline);

    test_cg_check_pixel(test_fb, 35, 5, 0x19334cff);
}
Ejemplo n.º 3
0
static bool
_cg_winsys_display_setup(cg_display_t *display, cg_error_t **error)
{
    cg_webgl_display_t *webgl_display;
    int window;
    EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context;
    EmscriptenWebGLContextAttributes attribs;
    char *id;

    c_return_val_if_fail(display->winsys == NULL, false);

    init_webgl_attribs_from_framebuffer_config(&attribs,
                                               &display->onscreen_template->config);

    window = CGlib_Emscripten_CreateWindow(100, 100, 0, 0);

    if (window < 0) {
        _cg_set_error(error,
                      CG_WINSYS_ERROR,
                      CG_WINSYS_ERROR_INIT,
                      "create window failed");
        return false;
    }

    CGlib_Emscripten_HideWindow(window);

    id = c_strdup_printf("cg_window_%d", window);
    context = emscripten_webgl_create_context(id, &attribs);

    if (context < 0) {
        _cg_set_error(error,
                      CG_WINSYS_ERROR,
                      CG_WINSYS_ERROR_INIT,
                      "create context failed");
        CGlib_Emscripten_DestroyWindow(window);
        c_free(id);
        return false;
    }

    emscripten_webgl_make_context_current(context);

    webgl_display = c_slice_new0(cg_webgl_display_t);
    display->winsys = webgl_display;

    webgl_display->window = window;
    webgl_display->window_id = id;
    webgl_display->context = context;

    return true;
}
Ejemplo n.º 4
0
void
rig_pb_stream_accept_tcp_connection(rig_pb_stream_t *stream,
                                    uv_tcp_t *server)
{
    uv_loop_t *loop = rut_uv_shell_get_loop(stream->shell);
    struct sockaddr name;
    int namelen;
    int err;

    c_return_if_fail(stream->type == STREAM_TYPE_DISCONNECTED);
    c_return_if_fail(stream->hostname == NULL);
    c_return_if_fail(stream->port == NULL);
    c_return_if_fail(stream->resolving == false);

    uv_tcp_init(loop, &stream->tcp.socket);
    stream->tcp.socket.data = stream;

    err = uv_accept((uv_stream_t *)server, (uv_stream_t *)&stream->tcp.socket);
    if (err != 0) {
        c_warning("Failed to accept tcp connection: %s", uv_strerror(err));
        uv_close((uv_handle_t *)&stream->tcp.socket, NULL);
        return;
    }

    err = uv_tcp_getpeername(&stream->tcp.socket, &name, &namelen);
    if (err != 0) {
        c_warning("Failed to query peer address of tcp socket: %s",
                  uv_strerror(err));

        stream->hostname = c_strdup("unknown");
        stream->port = c_strdup("0");
    } else if (name.sa_family != AF_INET) {
        c_warning("Accepted connection isn't ipv4");

        stream->hostname = c_strdup("unknown");
        stream->port = c_strdup("0");
    } else {
        struct sockaddr_in *addr = (struct sockaddr_in *)&name;
        char ip_address[17] = {'\0'};

        uv_ip4_name(addr, ip_address, 16);

        stream->hostname = c_strdup(ip_address);
        stream->port = c_strdup_printf("%u", ntohs(addr->sin_port));
    }

    stream->type = STREAM_TYPE_TCP;
    set_connected(stream);
}
Ejemplo n.º 5
0
void
_cg_glsl_shader_set_source_with_boilerplate(cg_device_t *dev,
                                            GLuint shader_gl_handle,
                                            GLenum shader_gl_type,
                                            GLsizei count_in,
                                            const char **strings_in,
                                            const GLint *lengths_in)
{
    const char *vertex_boilerplate;
    const char *fragment_boilerplate;

    const char **strings = c_alloca(sizeof(char *) * (count_in + 6));
    GLint *lengths = c_alloca(sizeof(GLint) * (count_in + 6));
    char *version_string;
    int count = 0;

    vertex_boilerplate = _CG_VERTEX_SHADER_BOILERPLATE;
    fragment_boilerplate = _CG_FRAGMENT_SHADER_BOILERPLATE;

    version_string =
        c_strdup_printf("#version %i\n\n", dev->glsl_version_to_use);

    strings[count] = version_string;
    lengths[count++] = -1;

    if (_cg_has_private_feature(dev, CG_PRIVATE_FEATURE_GL_EMBEDDED) &&
        cg_has_feature(dev, CG_FEATURE_ID_TEXTURE_3D)) {
        static const char texture_3d_extension[] =
            "#extension GL_OES_texture_3D : enable\n";
        strings[count] = texture_3d_extension;
        lengths[count++] = sizeof(texture_3d_extension) - 1;
    }

    if (shader_gl_type == GL_VERTEX_SHADER) {
        if (dev->glsl_version_to_use < 130) {
            strings[count] = "#define in attribute\n"
                             "#define out varying\n";
            lengths[count++] = -1;
        } else {
            /* To support source compatibility with glsl >= 1.3 which has
             * replaced
             * all of the texture sampler functions with one polymorphic
             * texture()
             * function we use the preprocessor to map the old names onto the
             * new
             * name...
             */
            strings[count] = "#define texture2D texture\n"
                             "#define texture3D texture\n"
                             "#define textureRect texture\n";
            lengths[count++] = -1;
        }

        strings[count] = vertex_boilerplate;
        lengths[count++] = strlen(vertex_boilerplate);
    } else if (shader_gl_type == GL_FRAGMENT_SHADER) {
        if (dev->glsl_version_to_use < 130) {
            strings[count] = "#define in varying\n"
                             "\n"
                             "#define cg_color_out gl_FragColor\n"
                             "#define cg_depth_out gl_FragDepth\n";
            lengths[count++] = -1;
        }

        strings[count] = fragment_boilerplate;
        lengths[count++] = strlen(fragment_boilerplate);

        if (dev->glsl_version_to_use >= 130) {
            /* FIXME: Support cg_depth_out. */
            /* To support source compatibility with glsl >= 1.3 which has
             * replaced
             * all of the texture sampler functions with one polymorphic
             * texture()
             * function we use the preprocessor to map the old names onto the
             * new
             * name...
             */
            strings[count] = "#define texture2D texture\n"
                             "#define texture3D texture\n"
                             "#define textureRect texture\n"
                             "\n"
                             "out vec4 cg_color_out;\n";
            //"out vec4 cg_depth_out\n";
            lengths[count++] = -1;
        }
    }

    memcpy(strings + count, strings_in, sizeof(char *) * count_in);
    if (lengths_in)
        memcpy(lengths + count, lengths_in, sizeof(GLint) * count_in);
    else {
        int i;

        for (i = 0; i < count_in; i++)
            lengths[count + i] = -1; /* null terminated */
    }
    count += count_in;

    if (C_UNLIKELY(CG_DEBUG_ENABLED(CG_DEBUG_SHOW_SOURCE))) {
        c_string_t *buf = c_string_new(NULL);
        int i;

        c_string_append_printf(buf,
                               "%s shader:\n",
                               shader_gl_type == GL_VERTEX_SHADER ? "vertex"
                               : "fragment");
        for (i = 0; i < count; i++)
            if (lengths[i] != -1)
                c_string_append_len(buf, strings[i], lengths[i]);
            else
                c_string_append(buf, strings[i]);

        c_message("%s", buf->str);

        c_string_free(buf, true);
    }

    GE(dev,
       glShaderSource(
           shader_gl_handle, count, (const char **)strings, lengths));

    c_free(version_string);
}