~elementary-os/elementaryos/os-patch-mutter-bionic

« back to all changes in this revision

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

  • Committer: RabbitBot
  • Date: 2018-04-11 14:49:36 UTC
  • Revision ID: rabbitbot@elementary.io-20180411144936-hgymqa9d8d1xfpbh
Initial import, version 3.28.0-2

Show diffs side-by-side

added added

removed removed

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