static void paint (void) { cg_pipeline_t *pipeline = cg_pipeline_new (test_dev); cg_texture_t *texture = make_texture (); int y, x; cg_pipeline_set_layer_texture (pipeline, 0, texture); /* Just render the texture in the top left corner */ /* Render the texture using four separate rectangles */ for (y = 0; y < 2; y++) for (x = 0; x < 2; x++) cg_framebuffer_draw_textured_rectangle (test_fb, pipeline, x * TEXTURE_RENDER_SIZE / 2, y * TEXTURE_RENDER_SIZE / 2, (x + 1) * TEXTURE_RENDER_SIZE / 2, (y + 1) * TEXTURE_RENDER_SIZE / 2, x / 2.0f, y / 2.0f, (x + 1) / 2.0f, (y + 1) / 2.0f); cg_object_unref (pipeline); cg_object_unref (texture); }
static cg_pipeline_t * create_texture_pipeline(TestState *state) { cg_pipeline_t *pipeline; cg_texture_t *tex; static const uint8_t tex_data[] = { 0xff, 0x00, 0x00, 0xff, /* red */ 0x00, 0xff, 0x00, 0xff, /* green */ 0x00, 0x00, 0xff, 0xff, /* blue */ 0xff, 0xff, 0x00, 0xff, /* yellow */ }; tex = test_cg_texture_new_from_data(test_dev, 2, 2, /* width/height */ TEST_CG_TEXTURE_NO_ATLAS, CG_PIXEL_FORMAT_RGBA_8888_PRE, 8, /* rowstride */ tex_data); pipeline = cg_pipeline_new(test_dev); cg_pipeline_set_layer_texture(pipeline, 0, tex); cg_pipeline_set_layer_filters(pipeline, 0, CG_PIPELINE_FILTER_NEAREST, CG_PIPELINE_FILTER_NEAREST); cg_object_unref(tex); return pipeline; }
static void test_naming_texture_units(TestState *state) { cg_pipeline_t *pipeline; cg_snippet_t *snippet; cg_texture_t *tex1, *tex2; /* Test that we can sample from an arbitrary texture unit by naming its layer number */ snippet = cg_snippet_new(CG_SNIPPET_HOOK_FRAGMENT, NULL, NULL); cg_snippet_set_replace(snippet, "cg_color_out = " "texture2D(cg_sampler100, vec2(0.0, 0.0)) + " "texture2D(cg_sampler200, vec2(0.0, 0.0));"); tex1 = test_cg_create_color_texture(test_dev, 0xff0000ff); tex2 = test_cg_create_color_texture(test_dev, 0x00ff00ff); pipeline = cg_pipeline_new(test_dev); cg_pipeline_set_layer_texture(pipeline, 100, tex1); cg_pipeline_set_layer_texture(pipeline, 200, tex2); cg_pipeline_add_snippet(pipeline, snippet); cg_framebuffer_draw_rectangle(test_fb, pipeline, 0, 0, 10, 10); cg_object_unref(pipeline); cg_object_unref(snippet); cg_object_unref(tex1); cg_object_unref(tex2); test_cg_check_pixel(test_fb, 5, 5, 0xffff00ff); }
rig_downsampler_t * rig_downsampler_new(rig_engine_t *engine) { rig_downsampler_t *downsampler = c_slice_new0(rig_downsampler_t); cg_pipeline_t *pipeline; downsampler->engine = engine; pipeline = cg_pipeline_new(engine->shell->cg_device); cg_pipeline_set_layer_texture(pipeline, 0, NULL); cg_pipeline_set_blend(pipeline, "RGBA=ADD(SRC_COLOR, 0)", NULL); downsampler->pipeline = pipeline; return downsampler; }
static void paint (cg_texture_t *texture) { cg_pipeline_t *pipeline = cg_pipeline_new (test_dev); int x = 0, y = 0, size = TEXTURE_SIZE; cg_pipeline_set_layer_texture (pipeline, 0, texture); cg_pipeline_set_layer_filters (pipeline, 0, CG_PIPELINE_FILTER_NEAREST_MIPMAP_NEAREST, CG_PIPELINE_FILTER_NEAREST); cg_framebuffer_draw_rectangle (test_fb, pipeline, x, y, x + size, y + size); x += size; size /= 2; cg_framebuffer_draw_rectangle (test_fb, pipeline, x, y, x + size, y + size); x += size; size /= 2; cg_framebuffer_draw_rectangle (test_fb, pipeline, x, y, x + size, y + size); cg_object_unref (pipeline); cg_object_unref (texture); }
cg_texture_t * rig_downsampler_downsample(rig_downsampler_t *downsampler, cg_texture_t *source, int scale_factor_x, int scale_factor_y) { cg_texture_components_t components; int src_w, src_h; int dest_width, dest_height; cg_pipeline_t *pipeline; /* validation */ src_w = cg_texture_get_width(source); src_h = cg_texture_get_height(source); if (src_w % scale_factor_x != 0) { c_warning("downsample: the width of the texture (%d) is not a " "multiple of the scale factor (%d)", src_w, scale_factor_x); } if (src_h % scale_factor_y != 0) { c_warning("downsample: the height of the texture (%d) is not a " "multiple of the scale factor (%d)", src_h, scale_factor_y); } /* create the destination texture up front */ dest_width = src_w / scale_factor_x; dest_height = src_h / scale_factor_y; components = cg_texture_get_components(source); if (downsampler->dest == NULL || cg_texture_get_width(downsampler->dest) != dest_width || cg_texture_get_height(downsampler->dest) != dest_height || cg_texture_get_components(downsampler->dest) != components) { cg_offscreen_t *offscreen; cg_texture_2d_t *texture_2d = cg_texture_2d_new_with_size( downsampler->engine->shell->cg_device, dest_width, dest_height); cg_texture_set_components(texture_2d, components); _rig_downsampler_reset(downsampler); downsampler->dest = texture_2d; /* create the FBO to render the downsampled texture */ offscreen = cg_offscreen_new_with_texture(downsampler->dest); downsampler->fb = offscreen; /* create the camera that will setup the scene for the render */ downsampler->camera = rig_camera_new(downsampler->engine, dest_width, /* ortho width */ dest_height, /* ortho height */ downsampler->fb); rut_camera_set_near_plane(downsampler->camera, -1.f); rut_camera_set_far_plane(downsampler->camera, 1.f); } pipeline = cg_pipeline_copy(downsampler->pipeline); cg_pipeline_set_layer_texture(pipeline, 0, source); rut_camera_flush(downsampler->camera); cg_framebuffer_draw_rectangle( downsampler->fb, pipeline, 0, 0, dest_width, dest_height); rut_camera_end_frame(downsampler->camera); cg_object_unref(pipeline); return cg_object_ref(downsampler->dest); }