Skip to content

Commit 933f907

Browse files
committed
finish work of: #510
1 parent c38f48e commit 933f907

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

cgeDemo/src/main/java/org/wysaid/cgeDemo/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class MainActivity extends AppCompatActivity {
2828

2929
public static final String EFFECT_CONFIGS[] = {
3030
"",
31-
"@style waveform 0.5 0.5 200. 150. 0.0 0.0 0.0 0.9",
32-
"@style hist 0.5 0.5 200. 150. 0.0 0.0 0.0 ",
31+
"@style waveform 0.01 0.01 0.4 0.4 0.0 0.0 0.0 0.9",
32+
"@style hist 0.5 0.5 0.5 0.5 0.0 0.0 0.0 ",
3333
"@curve RGB(0,255)(255,0) @style cm mapping0.jpg 80 80 8 3", // ASCII art (字符画效果)
3434
"@beautify face 1 480 640", //Beautify
3535
"@adjust lut edgy_amber.png",

library/src/main/jni/cge/common/cgeCommonDefine.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,26 @@ GLuint cgeGenTextureWithBuffer(const void* bufferData, GLint w, GLint h, GLenum
300300
assert(w != 0 && h != 0);
301301
GLuint tex;
302302
static const GLenum eArrs[] = { GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
303+
static const GLenum eArrsSized8[] = { GL_R8, GL_RG8, GL_RGB8, GL_RGBA8 };
303304
if (channel <= 0 || channel > 4)
304305
return 0;
305306
const GLenum& internalFormat = eArrs[channel - 1];
306307
glActiveTexture(GL_TEXTURE0 + bindID);
307308
glGenTextures(1, &tex);
308309
glBindTexture(GL_TEXTURE_2D, tex);
309310
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
311+
#ifdef ANDROID_NDK
312+
if (bufferData == nullptr && dataFmt == GL_UNSIGNED_BYTE)
313+
{
314+
glTexStorage2D(GL_TEXTURE_2D, 1, eArrsSized8[channel - 1], w, h);
315+
}
316+
else
317+
{
318+
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, channelFmt, dataFmt, bufferData);
319+
}
320+
#else
310321
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, channelFmt, dataFmt, bufferData);
322+
#endif
311323
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texFilter);
312324
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texFilter);
313325
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texWrap);

library/src/main/jni/cge/filters/cgeWaveformFilter.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
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

2732
namespace 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()
5360
void 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

7290
void CGEWaveformFilter::setFormPosition(float left, float top)

0 commit comments

Comments
 (0)