OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gpu/command_buffer/service/gles2_cmd_srgb_converter.h" | 5 #include "gpu/command_buffer/service/gles2_cmd_srgb_converter.h" |
6 | 6 |
7 #include "gpu/command_buffer/service/texture_manager.h" | 7 #include "gpu/command_buffer/service/texture_manager.h" |
8 #include "ui/gl/gl_version_info.h" | 8 #include "ui/gl/gl_version_info.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 | 35 |
36 | 36 |
37 void SRGBConverter::InitializeSRGBConverterProgram() { | 37 void SRGBConverter::InitializeSRGBConverterProgram() { |
38 if (srgb_converter_program_) { | 38 if (srgb_converter_program_) { |
39 return; | 39 return; |
40 } | 40 } |
41 | 41 |
42 srgb_converter_program_ = glCreateProgram(); | 42 srgb_converter_program_ = glCreateProgram(); |
43 | 43 |
| 44 const char* kShaderPrecisionPreamble = |
| 45 "#ifdef GL_ES\n" |
| 46 "precision mediump float;\n" |
| 47 "#define TexCoordPrecision mediump\n" |
| 48 "#else\n" |
| 49 "#define TexCoordPrecision\n" |
| 50 "#endif\n"; |
| 51 |
| 52 std::string vs_source; |
| 53 if (feature_info_->gl_version_info().is_es) { |
| 54 if (feature_info_->gl_version_info().is_es3) { |
| 55 vs_source += "#version 300 es\n"; |
| 56 vs_source += |
| 57 "#define ATTRIBUTE in\n" |
| 58 "#define VARYING out\n"; |
| 59 } else { |
| 60 vs_source += |
| 61 "#define ATTRIBUTE attribute\n" |
| 62 "#define VARYING varying\n"; |
| 63 } |
| 64 } else { |
| 65 vs_source += "#version 150\n"; |
| 66 vs_source += |
| 67 "#define ATTRIBUTE in\n" |
| 68 "#define VARYING out\n"; |
| 69 } |
| 70 |
| 71 vs_source += kShaderPrecisionPreamble; |
| 72 |
| 73 // TODO(yizhou): gles 2.0 does not support gl_VertexID. |
44 // Compile the vertex shader | 74 // Compile the vertex shader |
45 const char* vs_source = | 75 vs_source += |
46 "#version 150\n" | 76 "VARYING TexCoordPrecision vec2 v_texcoord;\n" |
47 "out vec2 v_texcoord;\n" | |
48 "\n" | 77 "\n" |
49 "void main()\n" | 78 "void main()\n" |
50 "{\n" | 79 "{\n" |
51 " const vec2 quad_positions[6] = vec2[6]\n" | 80 " const vec2 quad_positions[6] = vec2[6]\n" |
52 " (\n" | 81 " (\n" |
53 " vec2(0.0f, 0.0f),\n" | 82 " vec2(0.0f, 0.0f),\n" |
54 " vec2(0.0f, 1.0f),\n" | 83 " vec2(0.0f, 1.0f),\n" |
55 " vec2(1.0f, 0.0f),\n" | 84 " vec2(1.0f, 0.0f),\n" |
56 "\n" | 85 "\n" |
57 " vec2(0.0f, 1.0f),\n" | 86 " vec2(0.0f, 1.0f),\n" |
58 " vec2(1.0f, 0.0f),\n" | 87 " vec2(1.0f, 0.0f),\n" |
59 " vec2(1.0f, 1.0f)\n" | 88 " vec2(1.0f, 1.0f)\n" |
60 " );\n" | 89 " );\n" |
61 "\n" | 90 "\n" |
62 " vec2 xy = vec2((quad_positions[gl_VertexID] * 2.0) - 1.0);\n" | 91 " vec2 xy = vec2((quad_positions[gl_VertexID] * 2.0) - 1.0);\n" |
63 " gl_Position = vec4(xy, 0.0, 1.0);\n" | 92 " gl_Position = vec4(xy, 0.0, 1.0);\n" |
64 " v_texcoord = quad_positions[gl_VertexID];\n" | 93 " v_texcoord = quad_positions[gl_VertexID];\n" |
65 "}\n"; | 94 "}\n"; |
66 GLuint vs = glCreateShader(GL_VERTEX_SHADER); | 95 GLuint vs = glCreateShader(GL_VERTEX_SHADER); |
67 CompileShader(vs, vs_source); | 96 CompileShader(vs, vs_source.c_str()); |
68 glAttachShader(srgb_converter_program_, vs); | 97 glAttachShader(srgb_converter_program_, vs); |
69 glDeleteShader(vs); | 98 glDeleteShader(vs); |
70 | 99 |
71 // Compile the fragment shader | 100 // Compile the fragment shader |
72 | 101 |
73 // Sampling texels from a srgb texture to a linear image, it will convert | 102 // Sampling texels from a srgb texture to a linear image, it will convert |
74 // the srgb color space to linear color space automatically as a part of | 103 // the srgb color space to linear color space automatically as a part of |
75 // filtering. See the section <sRGB Texture Color Conversion> in GLES and | 104 // filtering. See the section <sRGB Texture Color Conversion> in GLES and |
76 // OpenGL spec. So during decoding, we don't need to use the equation to | 105 // OpenGL spec. So during decoding, we don't need to use the equation to |
77 // explicitly decode srgb to linear in fragment shader. | 106 // explicitly decode srgb to linear in fragment shader. |
78 // Drawing to a srgb image, it will convert linear to srgb automatically. | 107 // Drawing to a srgb image, it will convert linear to srgb automatically. |
79 // See the section <sRGB Conversion> in GLES and OpenGL spec. So during | 108 // See the section <sRGB Conversion> in GLES and OpenGL spec. So during |
80 // encoding, we don't need to use the equation to explicitly encode linear | 109 // encoding, we don't need to use the equation to explicitly encode linear |
81 // to srgb in fragment shader. | 110 // to srgb in fragment shader. |
82 // As a result, we just use a simple fragment shader to do srgb conversion. | 111 // As a result, we just use a simple fragment shader to do srgb conversion. |
83 const char* fs_source = | 112 std::string fs_source; |
84 "#version 150\n" | 113 if (feature_info_->gl_version_info().is_es) { |
85 "uniform sampler2D u_source_texture;\n" | 114 if (feature_info_->gl_version_info().is_es3) { |
86 "in vec2 v_texcoord;\n" | 115 fs_source += "#version 300 es\n"; |
87 "out vec4 output_color;\n" | 116 } |
| 117 } else { |
| 118 fs_source += "#version 150\n"; |
| 119 } |
| 120 |
| 121 fs_source += kShaderPrecisionPreamble; |
| 122 |
| 123 if (feature_info_->gl_version_info().is_es) { |
| 124 if (feature_info_->gl_version_info().is_es3) { |
| 125 fs_source += |
| 126 "#define VARYING in\n" |
| 127 "out vec4 frag_color;\n" |
| 128 "#define FRAGCOLOR frag_color\n" |
| 129 "#define TextureLookup texture\n"; |
| 130 } else { |
| 131 fs_source += |
| 132 "#define VARYING varying\n" |
| 133 "#define FRAGCOLOR gl_FragColor\n" |
| 134 "#define TextureLookup texture2D\n"; |
| 135 } |
| 136 } else { |
| 137 fs_source += |
| 138 "#define VARYING in\n" |
| 139 "out vec4 frag_color;\n" |
| 140 "#define FRAGCOLOR frag_color\n" |
| 141 "#define TextureLookup texture\n"; |
| 142 } |
| 143 |
| 144 fs_source += |
| 145 "uniform mediump sampler2D u_source_texture;\n" |
| 146 "VARYING TexCoordPrecision vec2 v_texcoord;\n" |
88 "\n" | 147 "\n" |
89 "void main()\n" | 148 "void main()\n" |
90 "{\n" | 149 "{\n" |
91 " vec4 c = texture(u_source_texture, v_texcoord);\n" | 150 " vec4 c = TextureLookup(u_source_texture, v_texcoord);\n" |
92 " output_color = c;\n" | 151 " FRAGCOLOR = c;\n" |
93 "}\n"; | 152 "}\n"; |
94 | 153 |
95 GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); | 154 GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); |
96 CompileShader(fs, fs_source); | 155 CompileShader(fs, fs_source.c_str()); |
97 glAttachShader(srgb_converter_program_, fs); | 156 glAttachShader(srgb_converter_program_, fs); |
98 glDeleteShader(fs); | 157 glDeleteShader(fs); |
99 | 158 |
100 glLinkProgram(srgb_converter_program_); | 159 glLinkProgram(srgb_converter_program_); |
101 #ifndef NDEBUG | 160 #ifndef NDEBUG |
102 GLint linked = 0; | 161 GLint linked = 0; |
103 glGetProgramiv(srgb_converter_program_, GL_LINK_STATUS, &linked); | 162 glGetProgramiv(srgb_converter_program_, GL_LINK_STATUS, &linked); |
104 if (!linked) { | 163 if (!linked) { |
105 DLOG(ERROR) << "BlitFramebuffer: program link failure."; | 164 DLOG(ERROR) << "BlitFramebuffer: program link failure."; |
106 } | 165 } |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 xoffset = c.x() - x; | 295 xoffset = c.x() - x; |
237 yoffset = c.y() - y; | 296 yoffset = c.y() - y; |
238 glCopyTexImage2D(GL_TEXTURE_2D, 0, src_framebuffer_internal_format, | 297 glCopyTexImage2D(GL_TEXTURE_2D, 0, src_framebuffer_internal_format, |
239 c.x(), c.y(), c.width(), c.height(), 0); | 298 c.x(), c.y(), c.width(), c.height(), 0); |
240 | 299 |
241 // Make a temporary linear texture as the 2nd texture, where we | 300 // Make a temporary linear texture as the 2nd texture, where we |
242 // render the converted (srgb to linear) result to. | 301 // render the converted (srgb to linear) result to. |
243 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); | 302 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
244 | 303 |
245 glBindTexture(GL_TEXTURE_2D, srgb_converter_textures_[1]); | 304 glBindTexture(GL_TEXTURE_2D, srgb_converter_textures_[1]); |
246 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, | 305 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, c.width(), c.height(), 0, |
247 c.width(), c.height(), | 306 GL_RGBA, GL_FLOAT, nullptr); |
248 0, GL_RGBA, GL_FLOAT, nullptr); | |
249 glBindFramebufferEXT(GL_FRAMEBUFFER, srgb_decoder_fbo_); | 307 glBindFramebufferEXT(GL_FRAMEBUFFER, srgb_decoder_fbo_); |
250 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | 308 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
251 GL_TEXTURE_2D, srgb_converter_textures_[1], 0); | 309 GL_TEXTURE_2D, srgb_converter_textures_[1], 0); |
252 | 310 |
253 // Sampling from the 1st texture(srgb) and drawing to the | 311 // Sampling from the 1st texture(srgb) and drawing to the |
254 // 2nd texture(linear), | 312 // 2nd texture(linear), |
255 glUseProgram(srgb_converter_program_); | 313 glUseProgram(srgb_converter_program_); |
256 glViewport(0, 0, width_read, height_read); | 314 glViewport(0, 0, width_read, height_read); |
257 | 315 |
258 glBindTexture(GL_TEXTURE_2D, srgb_converter_textures_[0]); | 316 glBindTexture(GL_TEXTURE_2D, srgb_converter_textures_[0]); |
(...skipping 11 matching lines...) Expand all Loading... |
270 // during bliting. Note that the src and dst coordinates may be reversed. | 328 // during bliting. Note that the src and dst coordinates may be reversed. |
271 GLuint width_draw = 0, height_draw = 0; | 329 GLuint width_draw = 0, height_draw = 0; |
272 if (encode) { | 330 if (encode) { |
273 glBindTexture(GL_TEXTURE_2D, srgb_converter_textures_[0]); | 331 glBindTexture(GL_TEXTURE_2D, srgb_converter_textures_[0]); |
274 | 332 |
275 width_draw = dstX1 > dstX0 ? dstX1 - dstX0 : dstX0 - dstX1; | 333 width_draw = dstX1 > dstX0 ? dstX1 - dstX0 : dstX0 - dstX1; |
276 height_draw = dstY1 > dstY0 ? dstY1 - dstY0 : dstY0 - dstY1; | 334 height_draw = dstY1 > dstY0 ? dstY1 - dstY0 : dstY0 - dstY1; |
277 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); | 335 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
278 glTexImage2D( | 336 glTexImage2D( |
279 GL_TEXTURE_2D, 0, decode ? GL_RGBA32F : src_framebuffer_internal_format, | 337 GL_TEXTURE_2D, 0, decode ? GL_RGBA32F : src_framebuffer_internal_format, |
280 width_draw, height_draw, 0, | 338 width_draw, height_draw, 0, decode ? GL_RGBA : src_framebuffer_format, |
281 decode ? GL_RGBA : src_framebuffer_format, | 339 decode ? GL_FLOAT : src_framebuffer_type, nullptr); |
282 decode ? GL_FLOAT : src_framebuffer_type, | |
283 nullptr); | |
284 | 340 |
285 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, srgb_encoder_fbo_); | 341 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, srgb_encoder_fbo_); |
286 glFramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | 342 glFramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
287 GL_TEXTURE_2D, srgb_converter_textures_[0], 0); | 343 GL_TEXTURE_2D, srgb_converter_textures_[0], 0); |
288 } else { | 344 } else { |
289 // Set approriate draw framebuffer if encoding is skipped. | 345 // Set approriate draw framebuffer if encoding is skipped. |
290 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, dst_framebuffer); | 346 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, dst_framebuffer); |
291 | 347 |
292 if (enable_scissor_test) { | 348 if (enable_scissor_test) { |
293 glEnable(GL_SCISSOR_TEST); | 349 glEnable(GL_SCISSOR_TEST); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 decoder->RestoreActiveTexture(); | 486 decoder->RestoreActiveTexture(); |
431 decoder->RestoreProgramBindings(); | 487 decoder->RestoreProgramBindings(); |
432 decoder->RestoreBufferBindings(); | 488 decoder->RestoreBufferBindings(); |
433 decoder->RestoreFramebufferBindings(); | 489 decoder->RestoreFramebufferBindings(); |
434 decoder->RestoreGlobalState(); | 490 decoder->RestoreGlobalState(); |
435 decoder->RestoreTextureState(tex->service_id()); | 491 decoder->RestoreTextureState(tex->service_id()); |
436 } | 492 } |
437 | 493 |
438 } // namespace gles2. | 494 } // namespace gles2. |
439 } // namespace gpu | 495 } // namespace gpu |
OLD | NEW |