/cogl/tests/conform/test-pipeline-uniforms.c

https://gitlab.com/tchaik/mutter · C · 415 lines · 318 code · 75 blank · 22 comment · 9 complexity · b025156632e804545fd641b814259a1b MD5 · raw file

  1. #include <cogl/cogl.h>
  2. #include <string.h>
  3. #include "test-utils.h"
  4. #define LONG_ARRAY_SIZE 128
  5. typedef struct _TestState
  6. {
  7. CoglPipeline *pipeline_red;
  8. CoglPipeline *pipeline_green;
  9. CoglPipeline *pipeline_blue;
  10. CoglPipeline *matrix_pipeline;
  11. CoglPipeline *vector_pipeline;
  12. CoglPipeline *int_pipeline;
  13. CoglPipeline *long_pipeline;
  14. int long_uniform_locations[LONG_ARRAY_SIZE];
  15. } TestState;
  16. static const char
  17. color_source[] =
  18. "uniform float red, green, blue;\n"
  19. "\n"
  20. "void\n"
  21. "main ()\n"
  22. "{\n"
  23. " cogl_color_out = vec4 (red, green, blue, 1.0);\n"
  24. "}\n";
  25. static const char
  26. matrix_source[] =
  27. "uniform mat4 matrix_array[4];\n"
  28. "\n"
  29. "void\n"
  30. "main ()\n"
  31. "{\n"
  32. " vec4 color = vec4 (0.0, 0.0, 0.0, 1.0);\n"
  33. " int i;\n"
  34. "\n"
  35. " for (i = 0; i < 4; i++)\n"
  36. " color = matrix_array[i] * color;\n"
  37. "\n"
  38. " cogl_color_out = color;\n"
  39. "}\n";
  40. static const char
  41. vector_source[] =
  42. "uniform vec4 vector_array[2];\n"
  43. "uniform vec3 short_vector;\n"
  44. "\n"
  45. "void\n"
  46. "main ()\n"
  47. "{\n"
  48. " cogl_color_out = (vector_array[0] +\n"
  49. " vector_array[1] +\n"
  50. " vec4 (short_vector, 1.0));\n"
  51. "}\n";
  52. static const char
  53. int_source[] =
  54. "uniform ivec4 vector_array[2];\n"
  55. "uniform int single_value;\n"
  56. "\n"
  57. "void\n"
  58. "main ()\n"
  59. "{\n"
  60. " cogl_color_out = (vec4 (vector_array[0]) +\n"
  61. " vec4 (vector_array[1]) +\n"
  62. " vec4 (float (single_value), 0.0, 0.0, 255.0)) / 255.0;\n"
  63. "}\n";
  64. static const char
  65. long_source[] =
  66. "uniform int long_array[" G_STRINGIFY (LONG_ARRAY_SIZE) "];\n"
  67. "const int last_index = " G_STRINGIFY (LONG_ARRAY_SIZE) " - 1;\n"
  68. "\n"
  69. "void\n"
  70. "main ()\n"
  71. "{\n"
  72. " cogl_color_out = vec4 (float (long_array[last_index]), 0.0, 0.0, 1.0);\n"
  73. "}\n";
  74. static CoglPipeline *
  75. create_pipeline_for_shader (TestState *state, const char *shader_source)
  76. {
  77. CoglPipeline *pipeline;
  78. CoglHandle shader;
  79. CoglHandle program;
  80. pipeline = cogl_pipeline_new (test_ctx);
  81. shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
  82. cogl_shader_source (shader, shader_source);
  83. program = cogl_create_program ();
  84. cogl_program_attach_shader (program, shader);
  85. cogl_pipeline_set_user_program (pipeline, program);
  86. cogl_handle_unref (shader);
  87. cogl_handle_unref (program);
  88. return pipeline;
  89. }
  90. static void
  91. init_state (TestState *state)
  92. {
  93. int uniform_location;
  94. state->pipeline_red = create_pipeline_for_shader (state, color_source);
  95. uniform_location =
  96. cogl_pipeline_get_uniform_location (state->pipeline_red, "red");
  97. cogl_pipeline_set_uniform_1f (state->pipeline_red, uniform_location, 1.0f);
  98. uniform_location =
  99. cogl_pipeline_get_uniform_location (state->pipeline_red, "green");
  100. cogl_pipeline_set_uniform_1f (state->pipeline_red, uniform_location, 0.0f);
  101. uniform_location =
  102. cogl_pipeline_get_uniform_location (state->pipeline_red, "blue");
  103. cogl_pipeline_set_uniform_1f (state->pipeline_red, uniform_location, 0.0f);
  104. state->pipeline_green = cogl_pipeline_copy (state->pipeline_red);
  105. uniform_location =
  106. cogl_pipeline_get_uniform_location (state->pipeline_green, "green");
  107. cogl_pipeline_set_uniform_1f (state->pipeline_green, uniform_location, 1.0f);
  108. state->pipeline_blue = cogl_pipeline_copy (state->pipeline_red);
  109. uniform_location =
  110. cogl_pipeline_get_uniform_location (state->pipeline_blue, "blue");
  111. cogl_pipeline_set_uniform_1f (state->pipeline_blue, uniform_location, 1.0f);
  112. state->matrix_pipeline = create_pipeline_for_shader (state, matrix_source);
  113. state->vector_pipeline = create_pipeline_for_shader (state, vector_source);
  114. state->int_pipeline = create_pipeline_for_shader (state, int_source);
  115. state->long_pipeline = NULL;
  116. }
  117. static void
  118. init_long_pipeline_state (TestState *state)
  119. {
  120. int i;
  121. state->long_pipeline = create_pipeline_for_shader (state, long_source);
  122. /* This tries to lookup a large number of uniform names to make sure
  123. that the bitmask of overriden uniforms flows over the size of a
  124. single long so that it has to resort to allocating it */
  125. for (i = 0; i < LONG_ARRAY_SIZE; i++)
  126. {
  127. char *uniform_name = g_strdup_printf ("long_array[%i]", i);
  128. state->long_uniform_locations[i] =
  129. cogl_pipeline_get_uniform_location (state->long_pipeline,
  130. uniform_name);
  131. g_free (uniform_name);
  132. }
  133. }
  134. static void
  135. destroy_state (TestState *state)
  136. {
  137. cogl_object_unref (state->pipeline_red);
  138. cogl_object_unref (state->pipeline_green);
  139. cogl_object_unref (state->pipeline_blue);
  140. cogl_object_unref (state->matrix_pipeline);
  141. cogl_object_unref (state->vector_pipeline);
  142. cogl_object_unref (state->int_pipeline);
  143. if (state->long_pipeline)
  144. cogl_object_unref (state->long_pipeline);
  145. }
  146. static void
  147. paint_pipeline (CoglPipeline *pipeline, int pos)
  148. {
  149. cogl_framebuffer_draw_rectangle (test_fb, pipeline,
  150. pos * 10, 0, pos * 10 + 10, 10);
  151. }
  152. static void
  153. paint_color_pipelines (TestState *state)
  154. {
  155. CoglPipeline *temp_pipeline;
  156. int uniform_location;
  157. int i;
  158. /* Paint with the first pipeline that sets the uniforms to bright
  159. red */
  160. paint_pipeline (state->pipeline_red, 0);
  161. /* Paint with the two other pipelines. These inherit from the red
  162. pipeline and only override one other component. The values for
  163. the two other components should be inherited from the red
  164. pipeline. */
  165. paint_pipeline (state->pipeline_green, 1);
  166. paint_pipeline (state->pipeline_blue, 2);
  167. /* Try modifying a single pipeline for multiple rectangles */
  168. temp_pipeline = cogl_pipeline_copy (state->pipeline_green);
  169. uniform_location = cogl_pipeline_get_uniform_location (temp_pipeline,
  170. "green");
  171. for (i = 0; i <= 8; i++)
  172. {
  173. cogl_pipeline_set_uniform_1f (temp_pipeline, uniform_location,
  174. i / 8.0f);
  175. paint_pipeline (temp_pipeline, i + 3);
  176. }
  177. cogl_object_unref (temp_pipeline);
  178. }
  179. static void
  180. paint_matrix_pipeline (CoglPipeline *pipeline)
  181. {
  182. CoglMatrix matrices[4];
  183. float matrix_floats[16 * 4];
  184. int uniform_location;
  185. int i;
  186. for (i = 0; i < 4; i++)
  187. cogl_matrix_init_identity (matrices + i);
  188. /* Use the first matrix to make the color red */
  189. cogl_matrix_translate (matrices + 0, 1.0f, 0.0f, 0.0f);
  190. /* Rotate the vertex so that it ends up green */
  191. cogl_matrix_rotate (matrices + 1, 90.0f, 0.0f, 0.0f, 1.0f);
  192. /* Scale the vertex so it ends up halved */
  193. cogl_matrix_scale (matrices + 2, 0.5f, 0.5f, 0.5f);
  194. /* Add a blue component in the final matrix. The final matrix is
  195. uploaded as transposed so we need to transpose first to cancel
  196. that out */
  197. cogl_matrix_translate (matrices + 3, 0.0f, 0.0f, 1.0f);
  198. cogl_matrix_transpose (matrices + 3);
  199. for (i = 0; i < 4; i++)
  200. memcpy (matrix_floats + i * 16,
  201. cogl_matrix_get_array (matrices + i),
  202. sizeof (float) * 16);
  203. /* Set the first three matrices as transposed */
  204. uniform_location =
  205. cogl_pipeline_get_uniform_location (pipeline, "matrix_array");
  206. cogl_pipeline_set_uniform_matrix (pipeline,
  207. uniform_location,
  208. 4, /* dimensions */
  209. 3, /* count */
  210. FALSE, /* not transposed */
  211. matrix_floats);
  212. /* Set the last matrix as untransposed */
  213. uniform_location =
  214. cogl_pipeline_get_uniform_location (pipeline, "matrix_array[3]");
  215. cogl_pipeline_set_uniform_matrix (pipeline,
  216. uniform_location,
  217. 4, /* dimensions */
  218. 1, /* count */
  219. TRUE, /* transposed */
  220. matrix_floats + 16 * 3);
  221. paint_pipeline (pipeline, 12);
  222. }
  223. static void
  224. paint_vector_pipeline (CoglPipeline *pipeline)
  225. {
  226. float vector_array_values[] = { 1.0f, 0.0f, 0.0f, 0.0f,
  227. 0.0f, 1.0f, 0.0f, 0.0f };
  228. float short_vector_values[] = { 0.0f, 0.0f, 1.0f };
  229. int uniform_location;
  230. uniform_location =
  231. cogl_pipeline_get_uniform_location (pipeline, "vector_array");
  232. cogl_pipeline_set_uniform_float (pipeline,
  233. uniform_location,
  234. 4, /* n_components */
  235. 2, /* count */
  236. vector_array_values);
  237. uniform_location =
  238. cogl_pipeline_get_uniform_location (pipeline, "short_vector");
  239. cogl_pipeline_set_uniform_float (pipeline,
  240. uniform_location,
  241. 3, /* n_components */
  242. 1, /* count */
  243. short_vector_values);
  244. paint_pipeline (pipeline, 13);
  245. }
  246. static void
  247. paint_int_pipeline (CoglPipeline *pipeline)
  248. {
  249. int vector_array_values[] = { 0x00, 0x00, 0xff, 0x00,
  250. 0x00, 0xff, 0x00, 0x00 };
  251. int single_value = 0x80;
  252. int uniform_location;
  253. uniform_location =
  254. cogl_pipeline_get_uniform_location (pipeline, "vector_array");
  255. cogl_pipeline_set_uniform_int (pipeline,
  256. uniform_location,
  257. 4, /* n_components */
  258. 2, /* count */
  259. vector_array_values);
  260. uniform_location =
  261. cogl_pipeline_get_uniform_location (pipeline, "single_value");
  262. cogl_pipeline_set_uniform_1i (pipeline,
  263. uniform_location,
  264. single_value);
  265. paint_pipeline (pipeline, 14);
  266. }
  267. static void
  268. paint_long_pipeline (TestState *state)
  269. {
  270. int i;
  271. for (i = 0; i < LONG_ARRAY_SIZE; i++)
  272. {
  273. int location = state->long_uniform_locations[i];
  274. cogl_pipeline_set_uniform_1i (state->long_pipeline,
  275. location,
  276. i == LONG_ARRAY_SIZE - 1);
  277. }
  278. paint_pipeline (state->long_pipeline, 15);
  279. }
  280. static void
  281. paint (TestState *state)
  282. {
  283. cogl_framebuffer_clear4f (test_fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1);
  284. paint_color_pipelines (state);
  285. paint_matrix_pipeline (state->matrix_pipeline);
  286. paint_vector_pipeline (state->vector_pipeline);
  287. paint_int_pipeline (state->int_pipeline);
  288. }
  289. static void
  290. check_pos (int pos, uint32_t color)
  291. {
  292. test_utils_check_pixel (test_fb, pos * 10 + 5, 5, color);
  293. }
  294. static void
  295. validate_result (void)
  296. {
  297. int i;
  298. check_pos (0, 0xff0000ff);
  299. check_pos (1, 0xffff00ff);
  300. check_pos (2, 0xff00ffff);
  301. for (i = 0; i <= 8; i++)
  302. {
  303. int green_value = i / 8.0f * 255.0f + 0.5f;
  304. check_pos (i + 3, 0xff0000ff + (green_value << 16));
  305. }
  306. check_pos (12, 0x0080ffff);
  307. check_pos (13, 0xffffffff);
  308. check_pos (14, 0x80ffffff);
  309. }
  310. static void
  311. validate_long_pipeline_result (void)
  312. {
  313. check_pos (15, 0xff0000ff);
  314. }
  315. void
  316. test_pipeline_uniforms (void)
  317. {
  318. TestState state;
  319. init_state (&state);
  320. cogl_framebuffer_orthographic (test_fb,
  321. 0, 0,
  322. cogl_framebuffer_get_width (test_fb),
  323. cogl_framebuffer_get_height (test_fb),
  324. -1,
  325. 100);
  326. paint (&state);
  327. validate_result ();
  328. /* Try the test again after querying the location of a large
  329. number of uniforms. This should verify that the bitmasks
  330. still work even if they have to allocate a separate array to
  331. store the bits */
  332. init_long_pipeline_state (&state);
  333. paint (&state);
  334. paint_long_pipeline (&state);
  335. validate_result ();
  336. validate_long_pipeline_result ();
  337. destroy_state (&state);
  338. if (cogl_test_verbose ())
  339. g_print ("OK\n");
  340. }