~darkxst/ubuntu/raring/cogl/lp1163025

« back to all changes in this revision

Viewing changes to tests/conform/test-just-vertex-shader.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-03-13 19:11:11 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20120313191111-3hgk529qkh9m6uk2
Tags: 1.9.8-0ubuntu1
* New upstream release (LP: #941617)
* Updated symbols & library name for soname update
* debian/control.in: Bump minimum glib to 2.28
* debian/patches/02_disable_armv5t_specific_optimization.patch: Disabled

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cogl/cogl.h>
 
2
 
 
3
#include <string.h>
 
4
 
 
5
#include "test-utils.h"
 
6
 
 
7
typedef struct _TestState
 
8
{
 
9
  CoglContext *ctx;
 
10
} TestState;
 
11
 
 
12
static CoglHandle
 
13
create_dummy_texture (void)
 
14
{
 
15
  /* Create a dummy 1x1 green texture to replace the color from the
 
16
     vertex shader */
 
17
  static const guint8 data[4] = { 0x00, 0xff, 0x00, 0xff };
 
18
 
 
19
  return cogl_texture_new_from_data (1, 1, /* size */
 
20
                                     COGL_TEXTURE_NONE,
 
21
                                     COGL_PIXEL_FORMAT_RGB_888,
 
22
                                     COGL_PIXEL_FORMAT_ANY,
 
23
                                     4, /* rowstride */
 
24
                                     data);
 
25
}
 
26
 
 
27
static void
 
28
paint_legacy (TestState *state)
 
29
{
 
30
  CoglHandle material = cogl_material_new ();
 
31
  CoglHandle tex;
 
32
  CoglColor color;
 
33
  GError *error = NULL;
 
34
  CoglHandle shader, program;
 
35
 
 
36
  cogl_color_init_from_4ub (&color, 0, 0, 0, 255);
 
37
  cogl_clear (&color, COGL_BUFFER_BIT_COLOR);
 
38
 
 
39
  /* Set the primary vertex color as red */
 
40
  cogl_color_set_from_4ub (&color, 0xff, 0x00, 0x00, 0xff);
 
41
  cogl_material_set_color (material, &color);
 
42
 
 
43
  /* Override the vertex color in the texture environment with a
 
44
     constant green color provided by a texture */
 
45
  tex = create_dummy_texture ();
 
46
  cogl_material_set_layer (material, 0, tex);
 
47
  cogl_handle_unref (tex);
 
48
  if (!cogl_material_set_layer_combine (material, 0,
 
49
                                        "RGBA=REPLACE(TEXTURE)",
 
50
                                        &error))
 
51
    {
 
52
      g_warning ("Error setting layer combine: %s", error->message);
 
53
      g_assert_not_reached ();
 
54
    }
 
55
 
 
56
  /* Set up a dummy vertex shader that does nothing but the usual
 
57
     fixed function transform */
 
58
  shader = cogl_create_shader (COGL_SHADER_TYPE_VERTEX);
 
59
  cogl_shader_source (shader,
 
60
                      "void\n"
 
61
                      "main ()\n"
 
62
                      "{\n"
 
63
                      "  cogl_position_out = "
 
64
                      "cogl_modelview_projection_matrix * "
 
65
                      "cogl_position_in;\n"
 
66
                      "  cogl_color_out = cogl_color_in;\n"
 
67
                      "  cogl_tex_coord_out[0] = cogl_tex_coord_in;\n"
 
68
                      "}\n");
 
69
  cogl_shader_compile (shader);
 
70
  if (!cogl_shader_is_compiled (shader))
 
71
    {
 
72
      char *log = cogl_shader_get_info_log (shader);
 
73
      g_warning ("Shader compilation failed:\n%s", log);
 
74
      g_free (log);
 
75
      g_assert_not_reached ();
 
76
    }
 
77
 
 
78
  program = cogl_create_program ();
 
79
  cogl_program_attach_shader (program, shader);
 
80
  cogl_program_link (program);
 
81
 
 
82
  cogl_handle_unref (shader);
 
83
 
 
84
  /* Draw something using the material */
 
85
  cogl_set_source (material);
 
86
  cogl_rectangle (0, 0, 50, 50);
 
87
 
 
88
  /* Draw it again using the program. It should look exactly the same */
 
89
  cogl_program_use (program);
 
90
  cogl_rectangle (50, 0, 100, 50);
 
91
  cogl_program_use (COGL_INVALID_HANDLE);
 
92
 
 
93
  cogl_handle_unref (material);
 
94
  cogl_handle_unref (program);
 
95
}
 
96
 
 
97
static void
 
98
paint (TestState *state)
 
99
{
 
100
  CoglPipeline *pipeline = cogl_pipeline_new (state->ctx);
 
101
  CoglHandle tex;
 
102
  CoglColor color;
 
103
  GError *error = NULL;
 
104
  CoglHandle shader, program;
 
105
 
 
106
  cogl_color_init_from_4ub (&color, 0, 0, 0, 255);
 
107
  cogl_clear (&color, COGL_BUFFER_BIT_COLOR);
 
108
 
 
109
  /* Set the primary vertex color as red */
 
110
  cogl_color_set_from_4ub (&color, 0xff, 0x00, 0x00, 0xff);
 
111
  cogl_pipeline_set_color (pipeline, &color);
 
112
 
 
113
  /* Override the vertex color in the texture environment with a
 
114
     constant green color provided by a texture */
 
115
  tex = create_dummy_texture ();
 
116
  cogl_pipeline_set_layer_texture (pipeline, 0, tex);
 
117
  cogl_handle_unref (tex);
 
118
  if (!cogl_pipeline_set_layer_combine (pipeline, 0,
 
119
                                        "RGBA=REPLACE(TEXTURE)",
 
120
                                        &error))
 
121
    {
 
122
      g_warning ("Error setting layer combine: %s", error->message);
 
123
      g_assert_not_reached ();
 
124
    }
 
125
 
 
126
  /* Set up a dummy vertex shader that does nothing but the usual
 
127
     fixed function transform */
 
128
  shader = cogl_create_shader (COGL_SHADER_TYPE_VERTEX);
 
129
  cogl_shader_source (shader,
 
130
                      "void\n"
 
131
                      "main ()\n"
 
132
                      "{\n"
 
133
                      "  cogl_position_out = "
 
134
                      "cogl_modelview_projection_matrix * "
 
135
                      "cogl_position_in;\n"
 
136
                      "  cogl_color_out = cogl_color_in;\n"
 
137
                      "  cogl_tex_coord_out[0] = cogl_tex_coord_in;\n"
 
138
                      "}\n");
 
139
  cogl_shader_compile (shader);
 
140
  if (!cogl_shader_is_compiled (shader))
 
141
    {
 
142
      char *log = cogl_shader_get_info_log (shader);
 
143
      g_warning ("Shader compilation failed:\n%s", log);
 
144
      g_free (log);
 
145
      g_assert_not_reached ();
 
146
    }
 
147
 
 
148
  program = cogl_create_program ();
 
149
  cogl_program_attach_shader (program, shader);
 
150
  cogl_program_link (program);
 
151
 
 
152
  cogl_handle_unref (shader);
 
153
 
 
154
  /* Draw something without the program */
 
155
  cogl_set_source (pipeline);
 
156
  cogl_rectangle (0, 0, 50, 50);
 
157
 
 
158
  /* Draw it again using the program. It should look exactly the same */
 
159
  cogl_pipeline_set_user_program (pipeline, program);
 
160
  cogl_handle_unref (program);
 
161
 
 
162
  cogl_rectangle (50, 0, 100, 50);
 
163
  cogl_pipeline_set_user_program (pipeline, COGL_INVALID_HANDLE);
 
164
 
 
165
  cogl_object_unref (pipeline);
 
166
}
 
167
 
 
168
static void
 
169
validate_result (void)
 
170
{
 
171
  /* Non-shader version */
 
172
  test_utils_check_pixel (25, 25, 0x00ff0000);
 
173
  /* Shader version */
 
174
  test_utils_check_pixel (75, 25, 0x00ff0000);
 
175
}
 
176
 
 
177
void
 
178
test_cogl_just_vertex_shader (TestUtilsGTestFixture *fixture,
 
179
                              void *data)
 
180
{
 
181
  TestUtilsSharedState *shared_state = data;
 
182
  TestState state;
 
183
 
 
184
  state.ctx = shared_state->ctx;
 
185
 
 
186
  cogl_ortho (0, cogl_framebuffer_get_width (shared_state->fb), /* left, right */
 
187
              cogl_framebuffer_get_height (shared_state->fb), 0, /* bottom, top */
 
188
              -1, 100 /* z near, far */);
 
189
 
 
190
  /* If shaders aren't supported then we can't run the test */
 
191
  if (cogl_features_available (COGL_FEATURE_SHADERS_GLSL))
 
192
    {
 
193
      paint_legacy (&state);
 
194
      validate_result ();
 
195
 
 
196
      paint (&state);
 
197
      validate_result ();
 
198
 
 
199
      if (cogl_test_verbose ())
 
200
        g_print ("OK\n");
 
201
    }
 
202
  else if (cogl_test_verbose ())
 
203
    g_print ("Skipping\n");
 
204
}
 
205