Skip to content

Commit 58f359e

Browse files
committed
render: fixed filter working with screen
1 parent fe98337 commit 58f359e

File tree

8 files changed

+77
-37
lines changed

8 files changed

+77
-37
lines changed

ext/gpupixel/filter/filter.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ void Filter::update(int64_t frameTime) {
240240
proceed(false);
241241

242242
_framebuffer->active();
243-
GPUPixelContext::getInstance()->capturedFrameData =
244-
new unsigned char[captureWidth * captureHeight * 4];
243+
GPUPixelContext::getInstance()->capturedFrameData = new unsigned char[captureWidth * captureHeight * 4];
245244
CHECK_GL(glReadPixels(0, 0, captureWidth, captureHeight, GL_RGBA, GL_UNSIGNED_BYTE, GPUPixelContext::getInstance()->capturedFrameData));
246245
_framebuffer->inactive();
247246
} else {

ext/gpupixel/source/source_image.cc

+50-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616

1717
USING_NS_GPUPIXEL
1818

19+
SourceImage::SourceImage() {
20+
_displayProgram = GLProgram::createByShaderString(kDefaultVertexShader, kDefaultFragmentShader);
21+
_positionAttribLocation = _displayProgram->getAttribLocation("position");
22+
_texCoordAttribLocation = _displayProgram->getAttribLocation("inputTextureCoordinate");
23+
_colorMapUniformLocation = _displayProgram->getUniformLocation("textureCoordinate");
24+
GPUPixelContext::getInstance()->setActiveShaderProgram(_displayProgram);
25+
CHECK_GL(glEnableVertexAttribArray(_positionAttribLocation));
26+
CHECK_GL(glEnableVertexAttribArray(_texCoordAttribLocation));
27+
_backgroundColor = { 0.f, 1.f, 0.f, 0.f };
28+
}
29+
30+
SourceImage::~SourceImage() {
31+
delete _displayProgram;
32+
_displayProgram = nullptr;
33+
}
34+
1935
std::shared_ptr<SourceImage> SourceImage::create_from_memory(int width, int height, int channel_count, const unsigned char *pixels) {
2036
auto sourceImage = std::shared_ptr<SourceImage>(new SourceImage());
2137
sourceImage->init(width, height, channel_count, pixels);
@@ -74,7 +90,38 @@ void SourceImage::init(int width, int height, int channel_count, int texid) {
7490
this->setFramebuffer(_framebuffer);
7591
}
7692

77-
CHECK_GL(glCopyImageSubData(_framebuffer->getTexture(), GL_TEXTURE_2D, 0, /*src_x*/0, /*src_y*/0, 0,
78-
texid, GL_TEXTURE_2D, 0, /*dst_x*/0, /*dst_y*/0, 0,
79-
width, height, 1));
93+
GPUPixelContext::getInstance()->setActiveShaderProgram(_displayProgram);
94+
_framebuffer->active();
95+
96+
static const GLfloat noRotationTextureCoordinates[] = {
97+
0.0f, 0.0f,
98+
1.0f, 0.0f,
99+
0.0f, 1.0f,
100+
1.0f, 1.0f,
101+
};
102+
103+
float scaledWidth = 1.0;
104+
float scaledHeight = 1.0;
105+
106+
const GLfloat _displayVertices[8] = {
107+
-scaledWidth,
108+
-scaledHeight,
109+
scaledWidth,
110+
-scaledHeight,
111+
-scaledWidth,
112+
scaledHeight,
113+
scaledWidth,
114+
scaledHeight
115+
};
116+
117+
CHECK_GL(glClearColor(_backgroundColor.r, _backgroundColor.g, _backgroundColor.b, _backgroundColor.a));
118+
CHECK_GL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
119+
CHECK_GL(glActiveTexture(GL_TEXTURE0));
120+
CHECK_GL(glBindTexture(GL_TEXTURE_2D, texid));
121+
CHECK_GL(glUniform1i(_colorMapUniformLocation, 0));
122+
CHECK_GL(glVertexAttribPointer(_positionAttribLocation, 2, GL_FLOAT, 0, 0, _displayVertices));
123+
CHECK_GL(glVertexAttribPointer(_texCoordAttribLocation, 2, GL_FLOAT, 0, 0, noRotationTextureCoordinates));
124+
CHECK_GL(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
125+
126+
_framebuffer->inactive();
80127
}

ext/gpupixel/source/source_image.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,31 @@
1010
#include <string>
1111

1212
#include "source.h"
13+
#include "../core/gl_program.h"
1314

1415
NS_GPUPIXEL_BEGIN
1516

1617
class SourceImage : public Source {
1718
public:
18-
SourceImage() {}
19-
~SourceImage() {};
19+
SourceImage();
20+
~SourceImage();
2021

2122
void init(int width, int height, int channel_count, const unsigned char *pixels);
2223
void init(int width, int height, int channel_count, int texid);
2324
static std::shared_ptr<SourceImage> create(const std::string name);
2425

2526
static std::shared_ptr<SourceImage> create_from_memory(int width, int height, int channel_count, const unsigned char *pixels);
26-
void Render();
2727
private:
2828

2929
#if defined(GPUPIXEL_ANDROID)
3030
static std::shared_ptr<SourceImage> createImageForAndroid(std::string name);
3131
#endif
32+
33+
GLProgram *_displayProgram = nullptr;
34+
GLuint _positionAttribLocation;
35+
GLuint _texCoordAttribLocation;
36+
GLuint _colorMapUniformLocation;
37+
struct { float r, g, b, a; } _backgroundColor;
3238
};
3339

3440
NS_GPUPIXEL_END

ext/gpupixel/target/target_view.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ TargetView::TargetView()
2727
TargetView::~TargetView() {
2828
delete _displayProgram;
2929
_displayProgram = nullptr;
30-
31-
delete[] _capturedFrameData;
3230
}
3331

3432
void TargetView::init() {
@@ -81,7 +79,7 @@ void TargetView::onSizeChanged(int width, int height) {
8179
_viewWidth = width;
8280
_viewHeight = height;
8381
_framebuffer = GPUPixelContext::getInstance()->getFramebufferCache()->fetchFramebuffer(_viewWidth, _viewHeight, false);
84-
_capturedFrameData = new unsigned char[_viewWidth * _viewHeight * 4];
82+
_capturedFrameData.resize(_viewWidth * _viewHeight * 4);
8583
_updateDisplayVertices();
8684
}
8785
}
@@ -105,7 +103,7 @@ void TargetView::update(int64_t frameTime) {
105103
CHECK_GL(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
106104

107105
CHECK_GL(glReadBuffer(GL_COLOR_ATTACHMENT0));
108-
CHECK_GL(glReadPixels(0, 0, _framebuffer->getWidth(), _framebuffer->getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, _capturedFrameData));
106+
CHECK_GL(glReadPixels(0, 0, _framebuffer->getWidth(), _framebuffer->getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, _capturedFrameData.data()));
109107
//char bmpname[32] = { 0 };
110108
//snprintf(bmpname, 32, "gtgimg%u.bmp", testindex);
111109
//stbi_write_bmp(bmpname, _framebuffer->getWidth(), _framebuffer->getHeight(), 4, (const void *)_capturedFrameData);
@@ -129,8 +127,7 @@ void TargetView::_updateDisplayVertices() {
129127
rotatedFramebufferHeight = inputFramebuffer->getWidth();
130128
}
131129

132-
float framebufferAspectRatio =
133-
rotatedFramebufferHeight / (float)rotatedFramebufferWidth;
130+
float framebufferAspectRatio = rotatedFramebufferHeight / (float)rotatedFramebufferWidth;
134131
float viewAspectRatio = _viewHeight / (float)_viewWidth;
135132

136133
float insetFramebufferWidth = 0.0;
@@ -145,6 +142,7 @@ void TargetView::_updateDisplayVertices() {
145142

146143
float scaledWidth = 1.0;
147144
float scaledHeight = 1.0;
145+
148146
if (_fillMode == FillMode::PreserveAspectRatio) {
149147
scaledWidth = insetFramebufferWidth / _viewWidth;
150148
scaledHeight = insetFramebufferHeight / _viewHeight;

ext/gpupixel/target/target_view.h

+3-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TargetView : public Target {
3535
void onSizeChanged(int width, int height);
3636
uint32_t getWidth() const { return _viewWidth; }
3737
uint32_t getHeight() const { return _viewHeight; }
38-
unsigned char *getPixels() { return _capturedFrameData; }
38+
unsigned char *getPixels() { return _capturedFrameData.data(); }
3939
uint32_t getDataSize() { return _viewWidth * _viewHeight * 4; }
4040
virtual void update(int64_t frameTime) override;
4141

@@ -49,15 +49,10 @@ class TargetView : public Target {
4949
GLuint _positionAttribLocation;
5050
GLuint _texCoordAttribLocation;
5151
GLuint _colorMapUniformLocation;
52-
struct {
53-
float r;
54-
float g;
55-
float b;
56-
float a;
57-
} _backgroundColor;
52+
struct { float r, g, b, a; } _backgroundColor;
5853

5954
GLfloat _displayVertices[8];
60-
unsigned char *_capturedFrameData;
55+
std::vector<uint8_t> _capturedFrameData;
6156

6257
void _updateDisplayVertices();
6358
const GLfloat* _getTexureCoordinate(RotationMode rotationMode);

src/platform/renderer.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,6 @@ int platform_renderer_init(SDL_Window* window, std::string renderer) {
10061006
IMG_InitPNG();
10071007

10081008
platform_render_make_current_context();
1009-
platform_render_init_filter_context();
10101009
platform_render_init_filters();
10111010

10121011
return 1;
@@ -1146,11 +1145,13 @@ void platform_render_apply_filter() {
11461145
{
11471146
OZZY_PROFILER_SECTION("Game/Run/Renderer/Render/Filter/ReadPixels");
11481147
error = SDL_RenderReadPixels(data.renderer, NULL, format, data.filter_pixels.data(), w * SDL_BYTESPERPIXEL(format));
1148+
//SDL_RenderFlush(data.renderer);
1149+
//error = 0;
11491150
}
11501151

11511152
if (!error) {
11521153
SDL_GL_MakeCurrent(data.window, data.main_gl_context);
1153-
platform_render_proceed_filter(w, h, format, data.filter_pixels, data.filter_texture);
1154+
platform_render_proceed_filter(w, h, format, data.filter_pixels, data.filter_texture, data.render_texture);
11541155
}
11551156
}
11561157

src/platform/renderer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool platform_render_support_filters();
4141
void platform_render_apply_filter();
4242
bool platform_render_any_filter_active();
4343
void platform_render_create_context();
44-
void platform_render_proceed_filter(int w, int h, int format, const std::vector<uint8_t> &pixels, SDL_Texture *filter_texture);
44+
void platform_render_proceed_filter(int w, int h, int format, const std::vector<uint8_t> &pixels, SDL_Texture *filter_texture, SDL_Texture *source_texture);
4545
void platform_render_init_filter_context();
4646

4747
struct video_mode : public vec2i {

src/platform/renderer_filters.cpp

+6-12
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,8 @@ bool platform_render_support_filters() {
3535
void platform_render_init_filters() {
3636
auto &data = g_renderer_filter;
3737

38-
if (platform_render_any_filter_active()) {
39-
return;
40-
}
41-
4238
std::string driver = get_video_driver();
4339
data.render_support_filters = (driver == "opengl");
44-
}
45-
46-
void platform_render_init_filter_context() {
47-
auto &data = g_renderer_filter;
4840

4941
if (!platform_render_support_filters()) {
5042
return;
@@ -101,14 +93,16 @@ bool platform_render_bilaterial_options() {
10193
return (save_active != data.bilaterial_active);
10294
}
10395

104-
void platform_render_proceed_filter(int w, int h, int format, const std::vector<uint8_t>&pixels, SDL_Texture *filter_texture) {
96+
void platform_render_proceed_filter(int w, int h, int format, const std::vector<uint8_t>&pixels, SDL_Texture *filter_texture, SDL_Texture *source_texture) {
10597
auto &data = g_renderer_filter;
10698

10799
// this not work, wtf?
108-
//SDL_GL_BindTexture(data.render_texture, &texw, &texh);
100+
//float texw, texh;
101+
//GLint render_texture_id;
102+
//SDL_GL_BindTexture(source_texture, &texw, &texh);
109103
//glGetIntegerv(GL_TEXTURE_BINDING_2D, &render_texture_id);
110-
//SDL_GL_UnbindTexture(data.render_texture);
111-
//sourceImage->init(w, h, SDL_BYTESPERPIXEL(format), render_texture_id);
104+
//SDL_GL_UnbindTexture(source_texture);
105+
//data.sourceImage->init(w, h, SDL_BYTESPERPIXEL(format), render_texture_id);
112106
data.sourceImage->init(w, h, SDL_BYTESPERPIXEL(format), (uint8_t *)pixels.data());
113107
data.sourceImage->proceed();
114108

0 commit comments

Comments
 (0)