22
33#include < EGL/egl.h>
44
5- static CGEConstString s_vshWaveform = " #version 310 es\n " CGE_SHADER_STRING_PRECISION_H(
5+ static CGEConstString s_vshWaveform = " #version 320 es\n " CGE_SHADER_STRING_PRECISION_H(
66 layout (location = 0 ) in vec2 position;
77 layout (location = 0 ) out vec2 textureCoordinate;
88 void main () {
99 gl_Position = vec4 (position, 0.0 , 1.0 );
1010 textureCoordinate = (position.xy + 1.0 ) / 2.0 ;
1111 });
1212
13- static CGEConstString s_fshWaveform = " #version 310 es\n " CGE_SHADER_STRING(
13+ static CGEConstString s_fshWaveform = " #version 320 es\n " CGE_SHADER_STRING(
1414 precision highp float ;
1515 precision highp int ;
1616 layout (location = 0 ) in vec2 textureCoordinate;
1717 layout (binding = 0 ) uniform sampler2D inputImageTexture;
18- layout (rgba8 , binding = 1 ) uniform writeonly image2D outputImageTexture ;
18+ layout (rgba8ui , binding = 1 ) uniform writeonly highp uimage2D outputImage ;
1919 layout (location = 0 ) out vec4 fragColor;
2020
2121 void main () {
22- fragColor = texture (inputImageTexture, textureCoordinate);
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 (newLum, newLum, newLum, 255 ));
2328
24- fragColor. r = 1.0 ;
29+ // TODO: 考虑使用 imageAtomicAdd 保障原子操作 (不闪屏)
2530 });
2631
2732namespace CGE
@@ -34,10 +39,12 @@ bool CGEWaveformFilter::init()
3439{
3540 if (initShadersFromString (s_vshWaveform, s_fshWaveform))
3641 {
37- setFormPosition (0 .5f , 0 .5f );
38- setFormSize (0 .4f , 0 .3f );
42+ m_program.bind ();
43+ setFormPosition (0 .1f , 0 .1f );
44+ setFormSize (0 .3f , 0 .3f );
3945 setColor (0 .0f , 0 .0f , 0 .0f , 0 .5f );
4046 m_drawer.reset (TextureDrawer::create ());
47+ m_drawer->setFlipScale (1 .0f , -1 .0f ); // flip upside down, meet the gl coord.
4148 return true ;
4249 }
4350
@@ -53,20 +60,31 @@ bool CGEWaveformFilter::init()
5360void CGEWaveformFilter::render2Texture (CGEImageHandlerInterface* handler, GLuint srcTexture, GLuint vertexBufferID)
5461{
5562 handler->setAsTarget ();
63+ glClear (GL_COLOR_BUFFER_BIT);
5664 m_program.bind ();
5765
5866 // / 渲染不写入, 使用 imageStore 写入.
59- // glColorMask(false, false, false, false);
67+ glColorMask (false , false , false , false );
6068
69+ glBindBuffer (GL_ARRAY_BUFFER, vertexBufferID);
6170 glEnableVertexAttribArray (0 );
6271 glVertexAttribPointer (0 , 2 , GL_FLOAT, GL_FALSE, 0 , 0 );
6372 glActiveTexture (GL_TEXTURE0);
64- // glBindImageTexture(0, srcTexture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
6573 glBindTexture (GL_TEXTURE_2D, srcTexture);
66- // glActiveTexture(GL_TEXTURE1);
67- // glBindImageTexture(1, handler->getTargetTextureID(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
68- // glBindTexture(GL_TEXTURE_2D, handler->getTargetTextureID());
74+
75+ glActiveTexture (GL_TEXTURE1);
76+ glBindTexture (GL_TEXTURE_2D, 0 );
77+ glBindImageTexture (1 , handler->getTargetTextureID (), 0 , GL_FALSE, 0 , GL_WRITE_ONLY, GL_RGBA8UI);
78+
6979 glDrawArrays (GL_TRIANGLE_FAN, 0 , 4 );
80+ glColorMask (true , true , true , true );
81+ glMemoryBarrier (GL_TEXTURE_FETCH_BARRIER_BIT);
82+
83+ handler->swapBufferFBO ();
84+ handler->setAsTarget ();
85+ auto && sz = handler->getOutputFBOSize ();
86+ glViewport (m_position[0 ] * sz.width , m_position[1 ] * sz.height , m_size[0 ] * sz.width , m_size[1 ] * sz.height );
87+ m_drawer->drawTexture (handler->getBufferTextureID ());
7088}
7189
7290void CGEWaveformFilter::setFormPosition (float left, float top)
0 commit comments