OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 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_apply_framebuffer_attachment_cmaa _intel.h" | 5 #include "gpu/command_buffer/service/gles2_cmd_apply_framebuffer_attachment_cmaa _intel.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "gpu/command_buffer/service/framebuffer_manager.h" | 8 #include "gpu/command_buffer/service/framebuffer_manager.h" |
9 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 9 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
10 #include "ui/gl/gl_context.h" | 10 #include "ui/gl/gl_context.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 : initialized_(false), | 21 : initialized_(false), |
22 textures_initialized_(false), | 22 textures_initialized_(false), |
23 is_in_gamma_correct_mode_(false), | 23 is_in_gamma_correct_mode_(false), |
24 supports_usampler_(true), | 24 supports_usampler_(true), |
25 supports_r8_image_(true), | 25 supports_r8_image_(true), |
26 supports_r8_read_format_(true), | 26 supports_r8_read_format_(true), |
27 is_gles31_compatible_(false), | 27 is_gles31_compatible_(false), |
28 frame_id_(0), | 28 frame_id_(0), |
29 width_(0), | 29 width_(0), |
30 height_(0), | 30 height_(0), |
31 copy_to_framebuffer_shader_(0), | |
32 copy_to_image_shader_(0), | |
33 edges0_shader_(0), | 31 edges0_shader_(0), |
34 edges1_shader_(0), | 32 edges1_shader_(0), |
35 edges_combine_shader_(0), | 33 edges_combine_shader_(0), |
36 process_and_apply_shader_(0), | 34 process_and_apply_shader_(0), |
37 debug_display_edges_shader_(0), | 35 debug_display_edges_shader_(0), |
38 cmaa_framebuffer_(0), | 36 cmaa_framebuffer_(0), |
39 copy_framebuffer_(0), | 37 copy_framebuffer_(0), |
40 rgba8_texture_(0), | 38 rgba8_texture_(0), |
41 working_color_texture_(0), | 39 working_color_texture_(0), |
42 edges0_texture_(0), | 40 edges0_texture_(0), |
43 edges1_texture_(0), | 41 edges1_texture_(0), |
44 mini4_edge_texture_(0), | 42 mini4_edge_texture_(0), |
45 mini4_edge_depth_texture_(0), | 43 mini4_edge_depth_texture_(0), |
46 edges1_shader_result_texture_float4_slot1_(0), | 44 edges1_shader_result_texture_float4_slot1_(0), |
47 edges1_shader_result_texture_(0), | 45 edges1_shader_result_texture_(0), |
48 edges_combine_shader_result_texture_float4_slot1_(0), | 46 edges_combine_shader_result_texture_float4_slot1_(0), |
49 process_and_apply_shader_result_texture_float4_slot1_(0), | 47 process_and_apply_shader_result_texture_float4_slot1_(0), |
50 edges_combine_shader_result_texture_slot2_(0), | 48 edges_combine_shader_result_texture_slot2_(0) {} |
51 copy_to_image_shader_outTexture_(0) {} | |
52 | 49 |
53 ApplyFramebufferAttachmentCMAAINTELResourceManager:: | 50 ApplyFramebufferAttachmentCMAAINTELResourceManager:: |
54 ~ApplyFramebufferAttachmentCMAAINTELResourceManager() { | 51 ~ApplyFramebufferAttachmentCMAAINTELResourceManager() { |
55 Destroy(); | 52 Destroy(); |
56 } | 53 } |
57 | 54 |
58 void ApplyFramebufferAttachmentCMAAINTELResourceManager::Initialize( | 55 void ApplyFramebufferAttachmentCMAAINTELResourceManager::Initialize( |
59 gles2::GLES2Decoder* decoder) { | 56 gles2::GLES2Decoder* decoder) { |
60 DCHECK(decoder); | 57 DCHECK(decoder); |
61 is_gles31_compatible_ = | 58 is_gles31_compatible_ = |
62 decoder->GetGLContext()->GetVersionInfo()->IsAtLeastGLES(3, 1); | 59 decoder->GetGLContext()->GetVersionInfo()->IsAtLeastGLES(3, 1); |
63 | 60 |
64 copy_to_image_shader_ = CreateProgram("", vert_str_, copy_frag_str_); | |
65 copy_to_framebuffer_shader_ = | |
66 CreateProgram("#define OUT_FBO 1\n", vert_str_, copy_frag_str_); | |
67 | |
68 // Check if RGBA8UI is supported as an FBO colour target with depth. | 61 // Check if RGBA8UI is supported as an FBO colour target with depth. |
69 // If not supported, GLSL needs to convert the data to/from float so there is | 62 // If not supported, GLSL needs to convert the data to/from float so there is |
70 // a small extra cost. | 63 // a small extra cost. |
71 { | 64 { |
72 GLuint rgba8ui_texture = 0, depth_texture = 0; | 65 GLuint rgba8ui_texture = 0, depth_texture = 0; |
73 glGenTextures(1, &rgba8ui_texture); | 66 glGenTextures(1, &rgba8ui_texture); |
74 glBindTexture(GL_TEXTURE_2D, rgba8ui_texture); | 67 glBindTexture(GL_TEXTURE_2D, rgba8ui_texture); |
75 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8UI, 4, 4); | 68 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8UI, 4, 4); |
76 | 69 |
77 glGenTextures(1, &depth_texture); | 70 glGenTextures(1, &depth_texture); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 edges1_shader_result_texture_float4_slot1_ = | 176 edges1_shader_result_texture_float4_slot1_ = |
184 glGetUniformLocation(edges0_shader_, "g_resultTextureFlt4Slot1"); | 177 glGetUniformLocation(edges0_shader_, "g_resultTextureFlt4Slot1"); |
185 edges1_shader_result_texture_ = | 178 edges1_shader_result_texture_ = |
186 glGetUniformLocation(edges1_shader_, "g_resultTexture"); | 179 glGetUniformLocation(edges1_shader_, "g_resultTexture"); |
187 edges_combine_shader_result_texture_float4_slot1_ = | 180 edges_combine_shader_result_texture_float4_slot1_ = |
188 glGetUniformLocation(edges_combine_shader_, "g_resultTextureFlt4Slot1"); | 181 glGetUniformLocation(edges_combine_shader_, "g_resultTextureFlt4Slot1"); |
189 edges_combine_shader_result_texture_slot2_ = | 182 edges_combine_shader_result_texture_slot2_ = |
190 glGetUniformLocation(edges_combine_shader_, "g_resultTextureSlot2"); | 183 glGetUniformLocation(edges_combine_shader_, "g_resultTextureSlot2"); |
191 process_and_apply_shader_result_texture_float4_slot1_ = glGetUniformLocation( | 184 process_and_apply_shader_result_texture_float4_slot1_ = glGetUniformLocation( |
192 process_and_apply_shader_, "g_resultTextureFlt4Slot1"); | 185 process_and_apply_shader_, "g_resultTextureFlt4Slot1"); |
193 copy_to_image_shader_outTexture_ = | |
194 glGetUniformLocation(copy_to_image_shader_, "outTexture"); | |
195 | 186 |
196 initialized_ = true; | 187 initialized_ = true; |
197 } | 188 } |
198 | 189 |
199 void ApplyFramebufferAttachmentCMAAINTELResourceManager::Destroy() { | 190 void ApplyFramebufferAttachmentCMAAINTELResourceManager::Destroy() { |
200 if (!initialized_) | 191 if (!initialized_) |
201 return; | 192 return; |
202 | 193 |
203 ReleaseTextures(); | 194 ReleaseTextures(); |
204 | 195 |
205 glDeleteProgram(copy_to_image_shader_); | |
206 glDeleteProgram(copy_to_framebuffer_shader_); | |
207 glDeleteProgram(process_and_apply_shader_); | 196 glDeleteProgram(process_and_apply_shader_); |
208 glDeleteProgram(edges_combine_shader_); | 197 glDeleteProgram(edges_combine_shader_); |
209 glDeleteProgram(edges1_shader_); | 198 glDeleteProgram(edges1_shader_); |
210 glDeleteProgram(edges0_shader_); | 199 glDeleteProgram(edges0_shader_); |
211 glDeleteProgram(debug_display_edges_shader_); | 200 glDeleteProgram(debug_display_edges_shader_); |
212 | 201 |
213 initialized_ = false; | 202 initialized_ = false; |
214 } | 203 } |
215 | 204 |
216 // Apply CMAA(Conservative Morphological Anti-Aliasing) algorithm to the | 205 // Apply CMAA(Conservative Morphological Anti-Aliasing) algorithm to the |
217 // color attachments of currently bound draw framebuffer. | 206 // color attachments of currently bound draw framebuffer. |
218 // Reference GL_INTEL_framebuffer_CMAA for details. | 207 // Reference GL_INTEL_framebuffer_CMAA for details. |
219 void ApplyFramebufferAttachmentCMAAINTELResourceManager:: | 208 void ApplyFramebufferAttachmentCMAAINTELResourceManager:: |
220 ApplyFramebufferAttachmentCMAAINTEL(gles2::GLES2Decoder* decoder, | 209 ApplyFramebufferAttachmentCMAAINTEL(gles2::GLES2Decoder* decoder, |
221 gles2::Framebuffer* framebuffer) { | 210 gles2::Framebuffer* framebuffer) { |
222 DCHECK(decoder); | 211 DCHECK(decoder); |
223 DCHECK(initialized_); | 212 DCHECK(initialized_); |
224 if (!framebuffer) | 213 if (!framebuffer) |
225 return; | 214 return; |
226 | 215 |
227 GLuint last_framebuffer = framebuffer->service_id(); | 216 glDisable(GL_SCISSOR_TEST); |
217 glDisable(GL_STENCIL_TEST); | |
218 glDisable(GL_CULL_FACE); | |
219 glDisable(GL_BLEND); | |
228 | 220 |
229 // Process each color attachment of the current draw framebuffer. | 221 // Process each color attachment of the current draw framebuffer. |
230 uint32_t max_draw_buffers = decoder->GetContextGroup()->max_draw_buffers(); | 222 uint32_t max_draw_buffers = decoder->GetContextGroup()->max_draw_buffers(); |
231 for (uint32_t i = 0; i < max_draw_buffers; i++) { | 223 for (uint32_t i = 0; i < max_draw_buffers; i++) { |
232 const gles2::Framebuffer::Attachment* attachment = | 224 const gles2::Framebuffer::Attachment* attachment = |
233 framebuffer->GetAttachment(GL_COLOR_ATTACHMENT0 + i); | 225 framebuffer->GetAttachment(GL_COLOR_ATTACHMENT0 + i); |
234 if (attachment && attachment->IsTextureAttachment()) { | 226 if (attachment && attachment->IsTextureAttachment()) { |
235 // Get the texture info. | 227 // Get the texture info. |
236 GLuint source_texture_client_id = attachment->object_name(); | 228 GLuint source_texture_client_id = attachment->object_name(); |
237 GLuint source_texture = 0; | 229 GLuint source_texture = 0; |
238 if (!decoder->GetServiceTextureId(source_texture_client_id, | 230 if (!decoder->GetServiceTextureId(source_texture_client_id, |
239 &source_texture)) | 231 &source_texture)) |
240 continue; | 232 continue; |
241 GLsizei width = attachment->width(); | 233 GLsizei width = attachment->width(); |
242 GLsizei height = attachment->height(); | 234 GLsizei height = attachment->height(); |
243 GLenum internal_format = attachment->internal_format(); | 235 GLenum internal_format = attachment->internal_format(); |
244 | 236 |
245 // Resize internal structures - only if needed. | 237 // Resize internal structures - only if needed. |
246 OnSize(width, height); | 238 OnSize(width, height); |
247 | 239 |
248 // CMAA internally expects GL_RGBA8 textures. | 240 // CMAA internally expects GL_RGBA8 textures. |
249 // Process using a GL_RGBA8 copy if this is not the case. | 241 // Process using a GL_RGBA8 copy if this is not the case. |
250 bool do_copy = internal_format != GL_RGBA8; | 242 bool do_copy = internal_format != GL_RGBA8; |
251 | 243 |
252 // Copy source_texture to rgba8_texture_ | 244 // Copy source_texture to rgba8_texture_ |
253 if (do_copy) { | 245 if (do_copy) { |
254 CopyTexture(source_texture, rgba8_texture_, false); | 246 glBindFramebufferEXT(GL_FRAMEBUFFER, copy_framebuffer_); |
247 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
248 GL_TEXTURE_2D, source_texture, 0); | |
249 CopyTexture(rgba8_texture_); | |
255 } | 250 } |
256 | 251 |
257 // CMAA Effect | 252 // CMAA Effect |
258 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, last_framebuffer); | |
259 if (do_copy) { | 253 if (do_copy) { |
260 ApplyCMAAEffectTexture(rgba8_texture_, rgba8_texture_); | 254 ApplyCMAAEffectTexture(rgba8_texture_, rgba8_texture_); |
261 } else { | 255 } else { |
262 ApplyCMAAEffectTexture(source_texture, source_texture); | 256 ApplyCMAAEffectTexture(source_texture, source_texture); |
263 } | 257 } |
264 | 258 |
265 // Copy rgba8_texture_ to source_texture | 259 // Copy rgba8_texture_ to source_texture |
266 if (do_copy) { | 260 if (do_copy) { |
267 // Move source_texture to the first color attachment of the copy fbo. | |
268 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, last_framebuffer); | |
269 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, | |
270 GL_TEXTURE_2D, 0, 0); | |
271 glBindFramebufferEXT(GL_FRAMEBUFFER, copy_framebuffer_); | |
272 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
273 GL_TEXTURE_2D, source_texture, 0); | |
274 | |
275 CopyTexture(rgba8_texture_, source_texture, true); | |
276 | |
277 // Restore color attachments | |
278 glBindFramebufferEXT(GL_FRAMEBUFFER, copy_framebuffer_); | 261 glBindFramebufferEXT(GL_FRAMEBUFFER, copy_framebuffer_); |
279 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | 262 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
280 GL_TEXTURE_2D, rgba8_texture_, 0); | 263 GL_TEXTURE_2D, rgba8_texture_, 0); |
281 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, last_framebuffer); | 264 CopyTexture(source_texture); |
282 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, | |
283 GL_TEXTURE_2D, source_texture, 0); | |
284 } | 265 } |
266 decoder->RestoreTextureState(source_texture); | |
285 } | 267 } |
286 } | 268 } |
287 | 269 |
288 // Restore state | 270 // Restore state |
289 decoder->RestoreAllAttributes(); | 271 decoder->RestoreAllAttributes(); |
290 decoder->RestoreTextureUnitBindings(0); | 272 decoder->RestoreTextureUnitBindings(0); |
291 decoder->RestoreTextureUnitBindings(1); | 273 decoder->RestoreTextureUnitBindings(1); |
292 decoder->RestoreActiveTexture(); | 274 decoder->RestoreActiveTexture(); |
293 decoder->RestoreProgramBindings(); | 275 decoder->RestoreProgramBindings(); |
294 decoder->RestoreBufferBindings(); | 276 decoder->RestoreBufferBindings(); |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
558 glDeleteTextures(1, &edges0_texture_); | 540 glDeleteTextures(1, &edges0_texture_); |
559 glDeleteTextures(1, &edges1_texture_); | 541 glDeleteTextures(1, &edges1_texture_); |
560 glDeleteTextures(1, &mini4_edge_texture_); | 542 glDeleteTextures(1, &mini4_edge_texture_); |
561 glDeleteTextures(1, &mini4_edge_depth_texture_); | 543 glDeleteTextures(1, &mini4_edge_depth_texture_); |
562 glDeleteTextures(1, &working_color_texture_); | 544 glDeleteTextures(1, &working_color_texture_); |
563 } | 545 } |
564 textures_initialized_ = false; | 546 textures_initialized_ = false; |
565 } | 547 } |
566 | 548 |
567 void ApplyFramebufferAttachmentCMAAINTELResourceManager::CopyTexture( | 549 void ApplyFramebufferAttachmentCMAAINTELResourceManager::CopyTexture( |
568 GLint source, | 550 GLint dest) { |
569 GLint dest, | |
570 bool via_fbo) { | |
571 glViewport(0, 0, width_, height_); | |
572 glActiveTexture(GL_TEXTURE0); | 551 glActiveTexture(GL_TEXTURE0); |
573 glBindTexture(GL_TEXTURE_2D, source); | 552 glBindTexture(GL_TEXTURE_2D, dest); |
553 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
554 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
555 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
556 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
574 | 557 |
575 if (!via_fbo) { | 558 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, width_, height_); |
dshwang
2016/09/02 15:38:11
Use glCopyTexSubImage2D, because we know the sourc
| |
576 glUseProgram(copy_to_image_shader_); | |
577 if (!is_gles31_compatible_) { | |
578 glUniform1i(copy_to_image_shader_outTexture_, 0); | |
579 } | |
580 glBindImageTextureEXT(0, dest, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8); | |
581 } else { | |
582 glDisable(GL_DEPTH_TEST); | |
583 glDisable(GL_STENCIL_TEST); | |
584 glDisable(GL_CULL_FACE); | |
585 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); | |
586 glDepthMask(GL_FALSE); | |
587 glDisable(GL_BLEND); | |
588 glUseProgram(copy_to_framebuffer_shader_); | |
589 } | |
590 | |
591 glDrawArrays(GL_TRIANGLES, 0, 3); | |
592 glUseProgram(0); | |
593 glBindTexture(GL_TEXTURE_2D, 0); | |
594 } | 559 } |
595 | 560 |
596 GLuint ApplyFramebufferAttachmentCMAAINTELResourceManager::CreateProgram( | 561 GLuint ApplyFramebufferAttachmentCMAAINTELResourceManager::CreateProgram( |
597 const char* defines, | 562 const char* defines, |
598 const char* vs_source, | 563 const char* vs_source, |
599 const char* fs_source) { | 564 const char* fs_source) { |
600 GLuint program = glCreateProgram(); | 565 GLuint program = glCreateProgram(); |
601 | 566 |
602 GLuint vs = CreateShader(GL_VERTEX_SHADER, defines, vs_source); | 567 GLuint vs = CreateShader(GL_VERTEX_SHADER, defines, vs_source); |
603 GLuint fs = CreateShader(GL_FRAGMENT_SHADER, defines, fs_source); | 568 GLuint fs = CreateShader(GL_FRAGMENT_SHADER, defines, fs_source); |
(...skipping 1271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1875 CombineEdges(); | 1840 CombineEdges(); |
1876 \n#endif\n | 1841 \n#endif\n |
1877 \n#if defined BLUR_EDGES\n | 1842 \n#if defined BLUR_EDGES\n |
1878 BlurEdges(); | 1843 BlurEdges(); |
1879 \n#endif\n | 1844 \n#endif\n |
1880 \n#if defined DISPLAY_EDGES\n | 1845 \n#if defined DISPLAY_EDGES\n |
1881 DisplayEdges(); | 1846 DisplayEdges(); |
1882 \n#endif\n | 1847 \n#endif\n |
1883 } | 1848 } |
1884 ); | 1849 ); |
1885 | |
1886 const char | |
1887 ApplyFramebufferAttachmentCMAAINTELResourceManager::copy_frag_str_[] = | |
1888 SHADER( | |
1889 precision highp float; | |
1890 layout(binding = 0) uniform highp sampler2D inTexture; | |
1891 layout(location = 0) out vec4 outColor; | |
1892 \n#ifdef GL_ES\n | |
1893 layout(binding = 0, rgba8) restrict writeonly uniform highp | |
1894 image2D outTexture; | |
1895 \n#else\n | |
1896 layout(rgba8) restrict writeonly uniform highp image2D outTexture; | |
1897 \n#endif\n | |
1898 | |
1899 void main() { | |
1900 ivec2 screenPosI = ivec2( gl_FragCoord.xy ); | |
1901 vec4 pixel = texelFetch(inTexture, screenPosI, 0); | |
1902 \n#ifdef OUT_FBO\n | |
1903 outColor = pixel; | |
1904 \n#else\n | |
1905 imageStore(outTexture, screenPosI, pixel); | |
1906 \n#endif\n | |
1907 } | |
1908 ); | |
1909 /* clang-format on */ | 1850 /* clang-format on */ |
1910 | 1851 |
1911 } // namespace gles2 | 1852 } // namespace gles2 |
1912 } // namespace gpu | 1853 } // namespace gpu |
OLD | NEW |