1+ #include " cgeWaveformFilter.h"
2+
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 (rgba8, binding = 1 ) uniform writeonly image2D outputImageTexture;
19+ layout (location = 0 ) out vec4 fragColor;
20+
21+ void main () {
22+ fragColor = texture (inputImageTexture, textureCoordinate);
23+
24+ fragColor.r = 1.0 ;
25+ });
26+
27+ namespace CGE
28+ {
29+ CGEWaveformFilter::~CGEWaveformFilter ()
30+ {
31+ }
32+
33+ bool CGEWaveformFilter::init ()
34+ {
35+ if (initShadersFromString (s_vshWaveform, s_fshWaveform))
36+ {
37+ setFormPosition (0 .5f , 0 .5f );
38+ setFormSize (0 .4f , 0 .3f );
39+ setColor (0 .0f , 0 .0f , 0 .0f , 0 .5f );
40+ m_drawer.reset (TextureDrawer::create ());
41+ return true ;
42+ }
43+
44+ CGE_LOG_ERROR (R"( CGEWaveformFilter::init failed. This filter needs GLES3.1 and later!
45+ Only GLES 3.1+ support image store.
46+ You need to imp a fallback version which reading pixels every frame like `cgeColorMappingFilter`
47+ )" );
48+ CGE_LOG_ERROR (" Failed Vertex Shader: %s\n " , s_vshWaveform);
49+ CGE_LOG_ERROR (" Failed Fragment Shader: %s\n " , s_fshWaveform);
50+ return false ;
51+ }
52+
53+ void CGEWaveformFilter::render2Texture (CGEImageHandlerInterface* handler, GLuint srcTexture, GLuint vertexBufferID)
54+ {
55+ handler->setAsTarget ();
56+ m_program.bind ();
57+
58+ // / 渲染不写入, 使用 imageStore 写入.
59+ // glColorMask(false, false, false, false);
60+
61+ glEnableVertexAttribArray (0 );
62+ glVertexAttribPointer (0 , 2 , GL_FLOAT, GL_FALSE, 0 , 0 );
63+ glActiveTexture (GL_TEXTURE0);
64+ // glBindImageTexture(0, srcTexture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
65+ 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());
69+ glDrawArrays (GL_TRIANGLE_FAN, 0 , 4 );
70+ }
71+
72+ void CGEWaveformFilter::setFormPosition (float left, float top)
73+ {
74+ m_position = { left, top };
75+ }
76+
77+ void CGEWaveformFilter::setFormSize (float width, float height)
78+ {
79+ m_size = { width, height };
80+ }
81+
82+ void CGEWaveformFilter::setColor (float r, float g, float b, float a)
83+ {
84+ m_color = { r, g, b, a };
85+ }
86+ } // namespace CGE
0 commit comments