11#include " cgeWaveformFilter.h"
22
3- #include < EGL/egl.h >
4-
5- static CGEConstString s_vshWaveform = " #version 310 es\n " CGE_SHADER_STRING_PRECISION_H(
6- layout (location = 0 ) in vec2 position;
7- layout (location = 0 ) out vec2 textureCoordinate;
8- void main () {
9- gl_Position = vec4 (position, 0.0 , 1.0 );
10- textureCoordinate = (position.xy + 1.0 ) / 2.0 ;
11- });
12-
13- static CGEConstString s_fshWaveform = " #version 310 es\n " CGE_SHADER_STRING(
14- precision highp float ;
15- precision highp int ;
16- layout (location = 0 ) in vec2 textureCoordinate;
17- layout (binding = 0 ) uniform sampler2D inputImageTexture;
18- layout (rgba8ui, binding = 1 ) uniform writeonly highp uimage2D outputImage;
19- layout (location = 0 ) out vec4 fragColor;
20-
21- void main () {
22- fragColor = vec4 (1.0 );
23- vec4 color = texture (inputImageTexture, textureCoordinate);
24- float lum = dot (color.rgb , vec3 (0.299 , 0.587 , 0.114 ));
25- ivec2 newLoc = ivec2 (vec2 (textureCoordinate.x , lum) * vec2 (imageSize (outputImage)));
26- // uint newLum = uint(lum * 255.0);
27- imageStore (outputImage, newLoc, uvec4 (255 , 255 , 255 , 255 ));
28-
29- // TODO: 直接使用 255 来描述最亮是没有问题的。 如果要实现颜色亮度叠加. 可以考虑使用 imageAtomicAdd.
30- });
3+ #define USING_ALPHA 1 // / 视图增加一个 80% 的半透明
4+
5+ // static CGEConstString s_vshWaveform = "#version 310 es\n" CGE_SHADER_STRING_PRECISION_H(
6+ // layout(location = 0) in vec2 position;
7+ // layout(location = 0) out vec2 textureCoordinate;
8+ // void main() {
9+ // gl_Position = vec4(position, 0.0, 1.0);
10+ // textureCoordinate = (position.xy + 1.0) / 2.0;
11+ // });
12+
13+ // static CGEConstString s_fshWaveform = "#version 310 es\n" CGE_SHADER_STRING(
14+ // precision highp float;
15+ // precision highp int;
16+ // layout(location = 0) in vec2 textureCoordinate;
17+ // layout(binding = 0) uniform sampler2D inputImageTexture;
18+ // layout(rgba8ui, binding = 1) uniform writeonly highp uimage2D outputImage;
19+ // layout(location = 0) out vec4 fragColor;
20+
21+ // void main() {
22+ // fragColor = vec4(1.0);
23+ // vec4 color = texture(inputImageTexture, textureCoordinate);
24+ // float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));
25+ // ivec2 newLoc = ivec2(vec2(textureCoordinate.x, lum) * vec2(imageSize(outputImage)));
26+ // // uint newLum = uint(lum * 255.0);
27+ // imageStore(outputImage, newLoc, uvec4(255, 255, 255, 255));
28+
29+ // // TODO: 直接使用 255 来描述最亮是没有问题的。 如果要实现颜色亮度叠加. 可以考虑使用 imageAtomicAdd.
30+ // });
3131
3232static CGEConstString s_cshWaveform = " #version 310 es\n " CGE_SHADER_STRING(
3333 precision highp float ;
@@ -55,31 +55,17 @@ bool CGEWaveformFilter::init()
5555 m_program.bind ();
5656 setFormPosition (0 .1f , 0 .1f );
5757 setFormSize (0 .3f , 0 .3f );
58- setColor (0 .0f , 0 .0f , 0 .0f , 0 .5f );
5958 m_drawer.reset (TextureDrawer::create ());
6059 m_drawer->setFlipScale (1 .0f , -1 .0f ); // flip upside down, meet the gl coord.
6160 m_renderTarget = std::make_unique<FrameBufferWithTexture>();
6261 return true ;
6362 }
6463
65- // if (initShadersFromString(s_vshWaveform, s_fshWaveform))
66- // {
67- // m_program.bind();
68- // setFormPosition(0.1f, 0.1f);
69- // setFormSize(0.3f, 0.3f);
70- // setColor(0.0f, 0.0f, 0.0f, 0.5f);
71- // m_drawer.reset(TextureDrawer::create());
72- // m_drawer->setFlipScale(1.0f, -1.0f); // flip upside down, meet the gl coord.
73- // m_renderTarget = std::make_unique<FrameBufferWithTexture>();
74- // return true;
75- // }
76-
7764 CGE_LOG_ERROR (R"( CGEWaveformFilter::init failed. This filter needs GLES3.1 and later!
7865 Only GLES 3.1+ support image store.
7966 You need to imp a fallback version which reading pixels every frame like `cgeColorMappingFilter`
8067)" );
81- CGE_LOG_ERROR (" Failed Vertex Shader: %s\n " , s_vshWaveform);
82- CGE_LOG_ERROR (" Failed Fragment Shader: %s\n " , s_fshWaveform);
68+ CGE_LOG_ERROR (" Failed Compute Shader: %s\n " , s_cshWaveform);
8369 return false ;
8470}
8571
@@ -106,9 +92,20 @@ void CGEWaveformFilter::render2Texture(CGEImageHandlerInterface* handler, GLuint
10692
10793 glMemoryBarrier (GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
10894
95+ #if USING_ALPHA
96+ glEnable (GL_BLEND);
97+ glBlendColor (1 , 1 , 1 , 0.8 );
98+ glBlendFunc (GL_ONE, GL_ONE_MINUS_CONSTANT_ALPHA);
99+ #endif
100+
109101 handler->setAsTarget ();
110102 glViewport (m_position[0 ] * sz.width , m_position[1 ] * sz.height , m_size[0 ] * sz.width , m_size[1 ] * sz.height );
111103 m_drawer->drawTexture (m_renderTarget->texture ());
104+
105+ #if USING_ALPHA
106+ glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
107+ glDisable (GL_BLEND);
108+ #endif
112109}
113110
114111void CGEWaveformFilter::setFormPosition (float left, float top)
@@ -121,8 +118,4 @@ void CGEWaveformFilter::setFormSize(float width, float height)
121118 m_size = { width, height };
122119}
123120
124- void CGEWaveformFilter::setColor (float r, float g, float b, float a)
125- {
126- m_color = { r, g, b, a };
127- }
128121} // namespace CGE
0 commit comments