forked from Dexterp37/GPUImage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUImageTwoInputFilter.cpp
More file actions
238 lines (184 loc) · 7.5 KB
/
Copy pathGPUImageTwoInputFilter.cpp
File metadata and controls
238 lines (184 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Author: Alessio Placitelli
* Contact: a.placitelli _@_ a2p.it
*
*/
#include "GPUImageTwoInputFilter.h"
#include "GPUImageOpenGLESContext.h"
#include "GLProgram.h"
const std::string GPUImageTwoInputFilter::kGPUImageTwoInputTextureVertexShaderString("\
attribute vec4 position;\
attribute vec4 inputTextureCoordinate;\
attribute vec4 inputTextureCoordinate2;\
\
varying vec2 textureCoordinate;\
varying vec2 textureCoordinate2;\
\
void main()\
{\
gl_Position = position;\
textureCoordinate = inputTextureCoordinate.xy;\
textureCoordinate2 = inputTextureCoordinate2.xy;\
}"
);
GPUImageTwoInputFilter::GPUImageTwoInputFilter() :
GPUImageFilter(),
filterSecondTextureCoordinateAttribute_(0),
filterInputTextureUniform2_(0),
inputRotation2_(kGPUImageNoRotation),
filterSourceTexture2_(0),
//TODO: firstFrameTime_(0),
//TODO: secondFrameTime_(0),
hasSetFirstTexture_(false),
hasReceivedFirstFrame_(false),
hasReceivedSecondFrame_(false),
firstFrameWasVideo_(false),
secondFrameWasVideo_(false) {
}
GPUImageTwoInputFilter::~GPUImageTwoInputFilter() {
}
void GPUImageTwoInputFilter::initWithFragmentShaderFromString(const std::string& fragmentShaderString) {
initWithVertexShaderFromString(kGPUImageTwoInputTextureVertexShaderString, fragmentShaderString);
}
void GPUImageTwoInputFilter::initWithVertexShaderFromString(const std::string& vertexShaderString, const std::string& fragmentShaderString) {
GPUImageFilter::initWithVertexShaderFromString(vertexShaderString, fragmentShaderString);
inputRotation2_ = kGPUImageNoRotation;
hasSetFirstTexture_ = false;
hasReceivedFirstFrame_ = false;
hasReceivedSecondFrame_ = false;
firstFrameWasVideo_ = false;
secondFrameWasVideo_ = false;
//TODO: firstFrameTime_ = kCMTimeInvalid;
//TODO: secondFrameTime_ = kCMTimeInvalid;
// TODO: schedules that block job in the thread which owns the gles context and waits for its completition
// TODO: Was run in a runSynchronouslyOnVideoProcessingQueue block
/*runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageOpenGLESContext useImageProcessingContext];
filterSecondTextureCoordinateAttribute = [filterProgram attributeIndex:@"inputTextureCoordinate2"];
filterInputTextureUniform2 = [filterProgram uniformIndex:@"inputImageTexture2"]; // This does assume a name of "inputImageTexture2" for second input texture in the fragment shader
glEnableVertexAttribArray(filterSecondTextureCoordinateAttribute);
});*/
GPUImageOpenGLESContext::useImageProcessingContext();
filterSecondTextureCoordinateAttribute_ = filterProgram_->getAttributeIndex("inputTextureCoordinate2");
// This does assume a name of "inputImageTexture2" for second input texture in the fragment shader
filterInputTextureUniform2_ = filterProgram_->getUniformIndex("inputImageTexture2");
glEnableVertexAttribArray(filterSecondTextureCoordinateAttribute_);
// End runSynchronouslyOnVideoProcessingQueue block
}
void GPUImageTwoInputFilter::initializeAttributes() {
GPUImageFilter::initializeAttributes();
filterProgram_->addAttribute("inputTextureCoordinate2");
}
void GPUImageTwoInputFilter::renderToTextureWithVertices(const GLfloat* vertices, const GLfloat* textureCoordinates, GLuint sourceTexture) {
if (preventRendering_) {
return;
}
GPUImageOpenGLESContext::setActiveShaderProgram(filterProgram_);
setUniformsForProgramAtIndex(0);
setFilterFBO();
glClearColor(backgroundColorRed_, backgroundColorGreen_, backgroundColorBlue_, backgroundColorAlpha_);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, sourceTexture);
glUniform1i(filterInputTextureUniform_, 2);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, filterSourceTexture2_);
glUniform1i(filterInputTextureUniform2_, 3);
glVertexAttribPointer(filterPositionAttribute_, 2, GL_FLOAT, 0, 0, vertices);
glVertexAttribPointer(filterTextureCoordinateAttribute_, 2, GL_FLOAT, 0, 0, textureCoordinates);
glVertexAttribPointer(filterSecondTextureCoordinateAttribute_, 2, GL_FLOAT, 0, 0, textureCoordinatesForRotation(inputRotation2_));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
gpu_int GPUImageTwoInputFilter::nextAvailableTextureIndex() {
if (hasSetFirstTexture_) {
return 1;
}
else
{
return 0;
}
}
void GPUImageTwoInputFilter::setInputTexture(GLuint newInputTexture, gpu_int textureIndex) {
if (textureIndex == 0) {
filterSourceTexture_ = newInputTexture;
hasSetFirstTexture_ = true;
}
else
{
filterSourceTexture2_ = newInputTexture;
}
}
void GPUImageTwoInputFilter::setInputSize(gpu_float_size newSize, gpu_int textureIndex) {
if (textureIndex == 0) {
GPUImageFilter::setInputSize(newSize, textureIndex);
if (FloatSizeEqualToZero(newSize)) {
hasSetFirstTexture_ = false;
}
}
}
void GPUImageTwoInputFilter::setInputRotation(GPUImageRotationMode newInputRotation, gpu_int textureIndex) {
if (textureIndex == 0) {
inputRotation_ = newInputRotation;
}
else
{
inputRotation2_ = newInputRotation;
}
}
gpu_float_size GPUImageTwoInputFilter::rotatedSize(gpu_float_size sizeToRotate, gpu_int textureIndex) {
gpu_float_size rotatedSize = sizeToRotate;
GPUImageRotationMode rotationToCheck;
if (textureIndex == 0) {
rotationToCheck = inputRotation_;
}
else
{
rotationToCheck = inputRotation2_;
}
if (GPUImageRotationSwapsWidthAndHeight(rotationToCheck))
{
rotatedSize.width = sizeToRotate.height;
rotatedSize.height = sizeToRotate.width;
}
return rotatedSize;
}
void GPUImageTwoInputFilter::newFrameReadyAtTime(gpu_time frameTime, gpu_int textureIndex) {
// You can set up infinite update loops, so this helps to short circuit them
if (hasReceivedFirstFrame_ && hasReceivedSecondFrame_) {
return;
}
bool updatedMovieFrameOppositeStillImage = false;
// TODO: try to understand what's this if/else for. For now, set updatedMovieFrameOppositeStillImage = true;
if (textureIndex == 0) {
hasReceivedFirstFrame_ = true;
updatedMovieFrameOppositeStillImage = true; // TODO: remove me
/*firstFrameTime = frameTime;
if (!CMTIME_IS_INDEFINITE(frameTime))
{
if CMTIME_IS_INDEFINITE(secondFrameTime)
{
updatedMovieFrameOppositeStillImage = true;
}
}*/
}
else
{
hasReceivedSecondFrame_ = true;
updatedMovieFrameOppositeStillImage = true; // TODO: remove me
/*
secondFrameTime = frameTime;
if (!CMTIME_IS_INDEFINITE(frameTime))
{
if CMTIME_IS_INDEFINITE(firstFrameTime)
{
updatedMovieFrameOppositeStillImage = true;
}
}*/
}
if ((hasReceivedFirstFrame_ && hasReceivedSecondFrame_) || updatedMovieFrameOppositeStillImage)
{
GPUImageFilter::newFrameReadyAtTime(frameTime, 0);
hasReceivedFirstFrame_ = false;
hasReceivedSecondFrame_ = false;
}
}