示例#1
0
static void
shared_variable_pre_post(TestState *state)
{
    cg_pipeline_t *pipeline;
    cg_snippet_t *snippet;

    /* Test that the pre string can declare variables used by the post
       string */
    pipeline = cg_pipeline_new(test_dev);

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

    snippet = cg_snippet_new(CG_SNIPPET_HOOK_FRAGMENT,
                             NULL, /* declarations */
                             "cg_color_out = redvec;");
    cg_snippet_set_pre(snippet, "vec4 redvec = vec4(1.0, 0.0, 0.0, 1.0);");
    cg_pipeline_add_snippet(pipeline, snippet);
    cg_object_unref(snippet);

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

    cg_object_unref(pipeline);

    test_cg_check_pixel(test_fb, 45, 5, 0xff0000ff);
}
示例#2
0
static void
shared_uniform(TestState *state)
{
    cg_pipeline_t *pipeline;
    cg_snippet_t *snippet;
    int location;

    /* Snippets sharing a uniform across the vertex and fragment
       hooks */
    pipeline = cg_pipeline_new(test_dev);

    location = cg_pipeline_get_uniform_location(pipeline, "a_value");
    cg_pipeline_set_uniform_1f(pipeline, location, 0.25f);

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

    snippet = cg_snippet_new(CG_SNIPPET_HOOK_VERTEX,
                             "uniform float a_value;",
                             "cg_color_out.b += a_value;");
    cg_pipeline_add_snippet(pipeline, snippet);
    cg_object_unref(snippet);
    snippet = cg_snippet_new(CG_SNIPPET_HOOK_FRAGMENT,
                             "uniform float a_value;",
                             "cg_color_out.b += a_value;");
    cg_pipeline_add_snippet(pipeline, snippet);
    cg_object_unref(snippet);

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

    cg_object_unref(pipeline);

    test_cg_check_pixel(test_fb, 25, 5, 0xff0080ff);
}
示例#3
0
static cg_pipeline_t *
create_pipeline (void)
{
  cg_pipeline_t *pipeline = cg_pipeline_new (test_dev);

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

  return pipeline;
}
示例#4
0
static void
test_vertex_transform_hook(TestState *state)
{
    cg_pipeline_t *pipeline;
    cg_snippet_t *snippet;
    c_matrix_t identity_matrix;
    c_matrix_t matrix;
    int location;

    /* Test the vertex transform hook */

    c_matrix_init_identity(&identity_matrix);

    pipeline = cg_pipeline_new(test_dev);

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

    snippet = cg_snippet_new(CG_SNIPPET_HOOK_VERTEX_TRANSFORM,
                             "uniform mat4 pmat;",
                             NULL);
    cg_snippet_set_replace(snippet, "cg_position_out = "
                           "pmat * cg_position_in;");
    cg_pipeline_add_snippet(pipeline, snippet);
    cg_object_unref(snippet);

    /* Copy the current projection matrix to a uniform */
    cg_framebuffer_get_projection_matrix(test_fb, &matrix);
    location = cg_pipeline_get_uniform_location(pipeline, "pmat");
    cg_pipeline_set_uniform_matrix(pipeline,
                                   location,
                                   4, /* dimensions */
                                   1, /* count */
                                   false, /* don't transpose */
                                   c_matrix_get_array(&matrix));

    /* Replace the real projection matrix with the identity. This should
       mess up the drawing unless the snippet replacement is working */
    cg_framebuffer_set_projection_matrix(test_fb, &identity_matrix);

    cg_framebuffer_draw_rectangle(test_fb, pipeline, 150, 0, 160, 10);
    cg_object_unref(pipeline);

    /* Restore the projection matrix */
    cg_framebuffer_set_projection_matrix(test_fb, &matrix);

    test_cg_check_pixel(test_fb, 155, 5, 0xff00ffff);
}
示例#5
0
static bool
draw_rectangle (TestState *state,
                int x,
                int y,
                TestDepthState *rect_state)
{
  uint8_t Cr = MASK_RED (rect_state->color);
  uint8_t Cg = MASK_GREEN (rect_state->color);
  uint8_t Cb = MASK_BLUE (rect_state->color);
  uint8_t Ca = MASK_ALPHA (rect_state->color);
  cg_pipeline_t *pipeline;
  cg_depth_state_t depth_state;

  cg_depth_state_init (&depth_state);
  cg_depth_state_set_test_enabled (&depth_state, rect_state->test_enable);
  cg_depth_state_set_test_function (&depth_state, rect_state->test_function);
  cg_depth_state_set_write_enabled (&depth_state, rect_state->write_enable);
  cg_depth_state_set_range (&depth_state,
                              rect_state->range_near,
                              rect_state->range_far);

  pipeline = cg_pipeline_new (test_dev);
  if (!cg_pipeline_set_depth_state (pipeline, &depth_state, NULL))
    {
      cg_object_unref (pipeline);
      return false;
    }

  cg_pipeline_set_color4ub (pipeline, Cr, Cg, Cb, Ca);

  cg_framebuffer_set_depth_write_enabled (test_fb, rect_state->fb_write_enable);
  cg_framebuffer_push_matrix (test_fb);
  cg_framebuffer_translate (test_fb, 0, 0, rect_state->depth);
  cg_framebuffer_draw_rectangle (test_fb,
                                   pipeline,
                                   x * QUAD_WIDTH,
                                   y * QUAD_WIDTH,
                                   x * QUAD_WIDTH + QUAD_WIDTH,
                                   y * QUAD_WIDTH + QUAD_WIDTH);
  cg_framebuffer_pop_matrix (test_fb);

  cg_object_unref (pipeline);

  return true;
}
示例#6
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);
}
示例#7
0
static void
simple_vertex_snippet(TestState *state)
{
    cg_pipeline_t *pipeline;
    cg_snippet_t *snippet;

    /* Simple vertex snippet */
    pipeline = cg_pipeline_new(test_dev);

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

    snippet = cg_snippet_new(CG_SNIPPET_HOOK_VERTEX,
                             NULL,
                             "cg_color_out.b += 1.0;");
    cg_pipeline_add_snippet(pipeline, snippet);
    cg_object_unref(snippet);

    cg_framebuffer_draw_rectangle(test_fb, pipeline, 10, 0, 20, 10);

    cg_object_unref(pipeline);

    test_cg_check_pixel(test_fb, 15, 5, 0xff00ffff);
}
示例#8
0
static void
simple_fragment_snippet(TestState *state)
{
    cg_pipeline_t *pipeline;
    cg_snippet_t *snippet;

    /* Simple fragment snippet */
    pipeline = cg_pipeline_new(test_dev);

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

    snippet = cg_snippet_new(CG_SNIPPET_HOOK_FRAGMENT,
                             NULL, /* declarations */
                             "cg_color_out.g += 1.0;");
    cg_pipeline_add_snippet(pipeline, snippet);
    cg_object_unref(snippet);

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

    cg_object_unref(pipeline);

    test_cg_check_pixel(test_fb, 5, 5, 0xffff00ff);
}
示例#9
0
static void
test_snippet_order(TestState *state)
{
    cg_pipeline_t *pipeline;
    cg_snippet_t *snippet;

    /* Verify that the snippets are executed in the right order. We'll
       replace the r component of the color in the pre sections of the
       snippets and the g component in the post. The pre sections should
       be executed in the reverse order they were added and the post
       sections in the same order as they were added. Therefore the r
       component should be taken from the the second snippet and the g
       component from the first */
    pipeline = cg_pipeline_new(test_dev);

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

    snippet = cg_snippet_new(CG_SNIPPET_HOOK_FRAGMENT,
                             NULL,
                             "cg_color_out.g = 0.5;\n");
    cg_snippet_set_pre(snippet, "cg_color_out.r = 0.5;\n");
    cg_snippet_set_replace(snippet, "cg_color_out.ba = vec2(0.0, 1.0);");
    cg_pipeline_add_snippet(pipeline, snippet);
    cg_object_unref(snippet);

    snippet = cg_snippet_new(CG_SNIPPET_HOOK_FRAGMENT,
                             NULL,
                             "cg_color_out.g = 1.0;\n");
    cg_snippet_set_pre(snippet, "cg_color_out.r = 1.0;\n");
    cg_pipeline_add_snippet(pipeline, snippet);
    cg_object_unref(snippet);

    cg_framebuffer_draw_rectangle(test_fb, pipeline, 160, 0, 170, 10);
    cg_object_unref(pipeline);

    test_cg_check_pixel(test_fb, 165, 5, 0x80ff00ff);
}