gl_program_unittest.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <GLES2/gl2.h>
  5. #include <GLES2/gl2ext.h>
  6. #include <GLES2/gl2extchromium.h>
  7. #include <stdint.h>
  8. #include "gpu/command_buffer/service/context_group.h"
  9. #include "gpu/command_buffer/tests/gl_manager.h"
  10. #include "gpu/command_buffer/tests/gl_test_utils.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #define SHADER(Src) #Src
  14. namespace gpu {
  15. class GLProgramTest : public testing::Test {
  16. protected:
  17. void SetUp() override { gl_.Initialize(GLManager::Options()); }
  18. void TearDown() override { gl_.Destroy(); }
  19. GLManager gl_;
  20. };
  21. class WebGLProgramTest : public GLProgramTest {
  22. protected:
  23. void SetUp() override {
  24. GLManager::Options options;
  25. options.context_type = CONTEXT_TYPE_WEBGL1;
  26. gl_.Initialize(options);
  27. }
  28. };
  29. TEST_F(GLProgramTest, GetSetUniform) {
  30. static const char* v_shader_str = SHADER(
  31. attribute vec4 a_vertex;
  32. attribute vec3 a_normal;
  33. uniform mat4 u_modelViewProjMatrix;
  34. struct MyStruct
  35. {
  36. int x;
  37. int y;
  38. };
  39. uniform MyStruct u_struct;
  40. uniform float u_array[4];
  41. varying vec3 v_normal;
  42. void main()
  43. {
  44. v_normal = a_normal;
  45. gl_Position = u_modelViewProjMatrix * a_vertex +
  46. vec4(u_struct.x, u_struct.y, 0, 1) +
  47. vec4(u_array[0], u_array[1], u_array[2], u_array[3]);
  48. }
  49. );
  50. static const char* f_shader_str = SHADER(
  51. varying mediump vec3 v_normal;
  52. void main()
  53. {
  54. gl_FragColor = vec4(v_normal/2.0+vec3(0.5), 1);
  55. }
  56. );
  57. // Load the program.
  58. GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
  59. glUseProgram(program);
  60. // Relink program.
  61. glLinkProgram(program);
  62. // These tests will fail on NVidia if not worked around by
  63. // command buffer.
  64. GLint location_sx = glGetUniformLocation(program, "u_struct.x");
  65. GLint location_array_0 = glGetUniformLocation(program, "u_array[0]");
  66. glUniform1i(location_sx, 3);
  67. glUniform1f(location_array_0, 123);
  68. GLint int_value = 0;
  69. GLfloat float_value = 0;
  70. glGetUniformiv(program, location_sx, &int_value);
  71. EXPECT_EQ(3, int_value);
  72. glGetUniformfv(program, location_array_0, &float_value);
  73. EXPECT_EQ(123.0f, float_value);
  74. GLTestHelper::CheckGLError("no errors", __LINE__);
  75. }
  76. TEST_F(GLProgramTest, NewShaderInCurrentProgram) {
  77. static const char* v_shader_str = SHADER(
  78. attribute vec4 a_position;
  79. void main()
  80. {
  81. gl_Position = a_position;
  82. }
  83. );
  84. static const char* f_red_shader_str = SHADER(
  85. void main()
  86. {
  87. gl_FragColor = vec4(1, 0, 0, 1);
  88. }
  89. );
  90. static const char* f_blue_shader_str = SHADER(
  91. void main()
  92. {
  93. gl_FragColor = vec4(0, 0, 1, 1);
  94. }
  95. );
  96. // Load the program.
  97. GLuint vs = GLTestHelper::LoadShader(GL_VERTEX_SHADER, v_shader_str);
  98. GLuint fs = GLTestHelper::LoadShader(GL_FRAGMENT_SHADER, f_red_shader_str);
  99. GLuint program = GLTestHelper::SetupProgram(vs, fs);
  100. glUseProgram(program);
  101. glShaderSource(fs, 1, &f_blue_shader_str, nullptr);
  102. glCompileShader(fs);
  103. glLinkProgram(program);
  104. // We specifically don't call UseProgram again.
  105. GLuint position_loc = glGetAttribLocation(program, "a_position");
  106. GLTestHelper::SetupUnitQuad(position_loc);
  107. glDrawArrays(GL_TRIANGLES, 0, 6);
  108. uint8_t expected_color[] = {
  109. 0, 0, 255, 255,
  110. };
  111. EXPECT_TRUE(
  112. GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color, nullptr));
  113. GLTestHelper::CheckGLError("no errors", __LINE__);
  114. }
  115. TEST_F(GLProgramTest, ShaderLengthSpecified) {
  116. const std::string valid_shader_str = SHADER(
  117. attribute vec4 a_position;
  118. void main()
  119. {
  120. gl_Position = a_position;
  121. }
  122. );
  123. const std::string invalid_shader = valid_shader_str + "invalid suffix";
  124. // Compiling invalid program should fail.
  125. const char* invalid_shader_strings[] = { invalid_shader.c_str() };
  126. GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  127. glShaderSource(vs, 1, invalid_shader_strings, nullptr);
  128. glCompileShader(vs);
  129. GLint compile_state = 0;
  130. glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_state);
  131. EXPECT_EQ(GL_FALSE, compile_state);
  132. // Compiling program cutting off invalid parts should succeed.
  133. const GLint lengths[] = {static_cast<GLint>(valid_shader_str.length())};
  134. glShaderSource(vs, 1, invalid_shader_strings, lengths);
  135. glCompileShader(vs);
  136. glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_state);
  137. EXPECT_EQ(GL_TRUE, compile_state);
  138. }
  139. TEST_F(GLProgramTest, UniformsInCurrentProgram) {
  140. static const char* v_shader_str = SHADER(
  141. attribute vec4 a_position;
  142. void main()
  143. {
  144. gl_Position = a_position;
  145. }
  146. );
  147. static const char* f_shader_str = SHADER(
  148. precision mediump float;
  149. uniform vec4 u_color;
  150. void main()
  151. {
  152. gl_FragColor = u_color;
  153. }
  154. );
  155. // Load the program.
  156. GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
  157. glUseProgram(program);
  158. // Relink.
  159. glLinkProgram(program);
  160. // This test will fail on NVidia Linux if not worked around.
  161. GLint color_location = glGetUniformLocation(program, "u_color");
  162. glUniform4f(color_location, 0.0f, 0.0f, 1.0f, 1.0f);
  163. // We specifically don't call UseProgram again.
  164. GLuint position_loc = glGetAttribLocation(program, "a_position");
  165. GLTestHelper::SetupUnitQuad(position_loc);
  166. glDrawArrays(GL_TRIANGLES, 0, 6);
  167. uint8_t expected_color[] = {
  168. 0, 0, 255, 255,
  169. };
  170. EXPECT_TRUE(
  171. GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color, nullptr));
  172. GLTestHelper::CheckGLError("no errors", __LINE__);
  173. }
  174. TEST_F(WebGLProgramTest, DeferCompileWithExt) {
  175. // This test must have extensions enabled.
  176. gles2::ContextGroup* context_group = gl_.decoder()->GetContextGroup();
  177. gles2::FeatureInfo* feature_info = context_group->feature_info();
  178. const gles2::FeatureInfo::FeatureFlags& flags = feature_info->feature_flags();
  179. if (!flags.ext_frag_depth)
  180. return;
  181. static const char* v_shdr_str = R"(
  182. attribute vec4 vPosition;
  183. void main()
  184. {
  185. gl_Position = vPosition;
  186. }
  187. )";
  188. static const char* f_shdr_str = R"(
  189. #extension GL_EXT_frag_depth : enable
  190. void main()
  191. {
  192. gl_FragDepthEXT = 1.0;
  193. }
  194. )";
  195. // Extension is disabled by default and be sure shader does not compile.
  196. GLuint vs_bad = GLTestHelper::CompileShader(GL_VERTEX_SHADER, v_shdr_str);
  197. GLuint fs_bad = GLTestHelper::CompileShader(GL_FRAGMENT_SHADER, f_shdr_str);
  198. GLuint program_bad = GLTestHelper::LinkProgram(vs_bad, fs_bad);
  199. GLint linked_bad = 0;
  200. glGetProgramiv(program_bad, GL_LINK_STATUS, &linked_bad);
  201. EXPECT_EQ(0, linked_bad);
  202. // linking bad compilations with extension enabled should still not link.
  203. glRequestExtensionCHROMIUM("GL_EXT_frag_depth");
  204. program_bad = GLTestHelper::LinkProgram(vs_bad, fs_bad);
  205. linked_bad = 0;
  206. glGetProgramiv(program_bad, GL_LINK_STATUS, &linked_bad);
  207. EXPECT_EQ(0, linked_bad);
  208. // Be sure extension was actually turned on by recompiling.
  209. GLuint vs_good = GLTestHelper::LoadShader(GL_VERTEX_SHADER, v_shdr_str);
  210. GLuint fs_good = GLTestHelper::LoadShader(GL_FRAGMENT_SHADER, f_shdr_str);
  211. GLuint program_good = GLTestHelper::SetupProgram(vs_good, fs_good);
  212. EXPECT_NE(0u, program_good);
  213. }
  214. TEST_F(GLProgramTest, DeleteAttachedShaderLinks) {
  215. static const char* v_shdr_str = R"(
  216. attribute vec4 vPosition;
  217. void main()
  218. {
  219. gl_Position = vPosition;
  220. }
  221. )";
  222. static const char* f_shdr_str = R"(
  223. void main()
  224. {
  225. gl_FragColor = vec4(1, 1, 1, 1);
  226. }
  227. )";
  228. // Compiling the shaders, attaching, then deleting before linking should work.
  229. GLuint vs = GLTestHelper::CompileShader(GL_VERTEX_SHADER, v_shdr_str);
  230. GLuint fs = GLTestHelper::CompileShader(GL_FRAGMENT_SHADER, f_shdr_str);
  231. GLuint program = glCreateProgram();
  232. glAttachShader(program, vs);
  233. glAttachShader(program, fs);
  234. glDeleteShader(vs);
  235. glDeleteShader(fs);
  236. glLinkProgram(program);
  237. GLint linked = 0;
  238. glGetProgramiv(program, GL_LINK_STATUS, &linked);
  239. EXPECT_NE(0, linked);
  240. }
  241. } // namespace gpu