| 1 |
void OpenGL::shader(const char* pathname) { |
| 2 |
for(auto& program : programs) program.release(); |
| 3 |
programs.reset(); |
| 4 |
|
| 5 |
settings.reset(); |
| 6 |
|
| 7 |
format = inputFormat; |
| 8 |
filter = GL_LINEAR; |
| 9 |
wrap = GL_CLAMP_TO_BORDER; |
| 10 |
absoluteWidth = 0, absoluteHeight = 0; |
| 11 |
relativeWidth = 0, relativeHeight = 0; |
| 12 |
|
| 13 |
unsigned historySize = 0; |
| 14 |
if(pathname) { |
| 15 |
auto document = Markup::Document(file::read({pathname, "manifest.bml"})); |
| 16 |
|
| 17 |
for(auto& node : document["settings"]) { |
| 18 |
settings.insert({node.name, node.text()}); |
| 19 |
} |
| 20 |
|
| 21 |
for(auto& node : document["input"]) { |
| 22 |
if(node.name == "history") historySize = node.decimal(); |
| 23 |
if(node.name == "format") format = glrFormat(node.text()); |
| 24 |
if(node.name == "filter") filter = glrFilter(node.text()); |
| 25 |
if(node.name == "wrap") wrap = glrWrap(node.text()); |
| 26 |
} |
| 27 |
|
| 28 |
for(auto& node : document["output"]) { |
| 29 |
string text = node.text(); |
| 30 |
if(node.name == "width") { |
| 31 |
if(text.endsWith("%")) relativeWidth = real(text.rtrim<1>("%")) / 100.0; |
| 32 |
else absoluteWidth = decimal(text); |
| 33 |
} |
| 34 |
if(node.name == "height") { |
| 35 |
if(text.endsWith("%")) relativeHeight = real(text.rtrim<1>("%")) / 100.0; |
| 36 |
else absoluteHeight = decimal(text); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
for(auto& node : document.find("program")) { |
| 41 |
unsigned n = programs.size(); |
| 42 |
programs(n).bind(this, node, pathname); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
//changing shaders may change input format, which requires the input texture to be recreated |
| 47 |
if(texture) { glDeleteTextures(1, &texture); texture = 0; } |
| 48 |
glGenTextures(1, &texture); |
| 49 |
glBindTexture(GL_TEXTURE_2D, texture); |
| 50 |
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, getFormat(), getType(), buffer); |
| 51 |
allocateHistory(historySize); |
| 52 |
} |
| 53 |
|
| 54 |
void OpenGL::allocateHistory(unsigned size) { |
| 55 |
for(auto& frame : history) glDeleteTextures(1, &frame.texture); |
| 56 |
history.reset(); |
| 57 |
while(size--) { |
| 58 |
OpenGLTexture frame; |
| 59 |
frame.filter = filter; |
| 60 |
frame.wrap = wrap; |
| 61 |
glGenTextures(1, &frame.texture); |
| 62 |
glBindTexture(GL_TEXTURE_2D, frame.texture); |
| 63 |
glTexImage2D(GL_TEXTURE_2D, 0, format, frame.width = width, frame.height = height, 0, getFormat(), getType(), buffer); |
| 64 |
history.append(frame); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
bool OpenGL::lock(uint32_t*& data, unsigned& pitch) { |
| 69 |
pitch = width * sizeof(uint32_t); |
| 70 |
return data = buffer; |
| 71 |
} |
| 72 |
|
| 73 |
void OpenGL::clear() { |
| 74 |
for(auto& p : programs) { |
| 75 |
glUseProgram(p.program); |
| 76 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, p.framebuffer); |
| 77 |
glClearColor(0, 0, 0, 1); |
| 78 |
glClear(GL_COLOR_BUFFER_BIT); |
| 79 |
} |
| 80 |
glUseProgram(0); |
| 81 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 82 |
glClearColor(0, 0, 0, 1); |
| 83 |
glClear(GL_COLOR_BUFFER_BIT); |
| 84 |
} |
| 85 |
|
| 86 |
void OpenGL::refresh() { |
| 87 |
clear(); |
| 88 |
|
| 89 |
glActiveTexture(GL_TEXTURE0); |
| 90 |
glBindTexture(GL_TEXTURE_2D, texture); |
| 91 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, getFormat(), getType(), buffer); |
| 92 |
|
| 93 |
struct Source { |
| 94 |
GLuint texture; |
| 95 |
unsigned width, height; |
| 96 |
GLuint filter, wrap; |
| 97 |
}; |
| 98 |
vector<Source> sources; |
| 99 |
sources.prepend({texture, width, height, filter, wrap}); |
| 100 |
|
| 101 |
for(auto& p : programs) { |
| 102 |
unsigned targetWidth = p.absoluteWidth ? p.absoluteWidth : outputWidth; |
| 103 |
unsigned targetHeight = p.absoluteHeight ? p.absoluteHeight : outputHeight; |
| 104 |
if(p.relativeWidth) targetWidth = sources[0].width * p.relativeWidth; |
| 105 |
if(p.relativeHeight) targetHeight = sources[0].height * p.relativeHeight; |
| 106 |
|
| 107 |
p.size(targetWidth, targetHeight); |
| 108 |
glUseProgram(p.program); |
| 109 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, p.framebuffer); |
| 110 |
|
| 111 |
glrUniform1i("phase", p.phase); |
| 112 |
glrUniform1i("historyLength", history.size()); |
| 113 |
glrUniform1i("sourceLength", sources.size()); |
| 114 |
glrUniform1i("pixmapLength", p.pixmaps.size()); |
| 115 |
glrUniform4f("targetSize", targetWidth, targetHeight, 1.0 / targetWidth, 1.0 / targetHeight); |
| 116 |
glrUniform4f("outputSize", outputWidth, outputHeight, 1.0 / outputWidth, 1.0 / outputHeight); |
| 117 |
|
| 118 |
unsigned aid = 0; |
| 119 |
for(auto& frame : history) { |
| 120 |
glrUniform1i({"history[", aid, "]"}, aid); |
| 121 |
glrUniform4f({"historySize[", aid, "]"}, frame.width, frame.height, 1.0 / frame.width, 1.0 / frame.height); |
| 122 |
glActiveTexture(GL_TEXTURE0 + (aid++)); |
| 123 |
glBindTexture(GL_TEXTURE_2D, frame.texture); |
| 124 |
glrParameters(frame.filter, frame.wrap); |
| 125 |
} |
| 126 |
|
| 127 |
unsigned bid = 0; |
| 128 |
for(auto& source : sources) { |
| 129 |
glrUniform1i({"source[", bid, "]"}, aid + bid); |
| 130 |
glrUniform4f({"sourceSize[", bid, "]"}, source.width, source.height, 1.0 / source.width, 1.0 / source.height); |
| 131 |
glActiveTexture(GL_TEXTURE0 + aid + (bid++)); |
| 132 |
glBindTexture(GL_TEXTURE_2D, source.texture); |
| 133 |
glrParameters(source.filter, source.wrap); |
| 134 |
} |
| 135 |
|
| 136 |
unsigned cid = 0; |
| 137 |
for(auto& pixmap : p.pixmaps) { |
| 138 |
glrUniform1i({"pixmap[", cid, "]"}, aid + bid + cid); |
| 139 |
glrUniform4f({"pixmapSize[", bid, "]"}, pixmap.width, pixmap.height, 1.0 / pixmap.width, 1.0 / pixmap.height); |
| 140 |
glActiveTexture(GL_TEXTURE0 + aid + bid + (cid++)); |
| 141 |
glBindTexture(GL_TEXTURE_2D, pixmap.texture); |
| 142 |
glrParameters(pixmap.filter, pixmap.wrap); |
| 143 |
} |
| 144 |
|
| 145 |
glActiveTexture(GL_TEXTURE0); |
| 146 |
glrParameters(sources[0].filter, sources[0].wrap); |
| 147 |
p.render(sources[0].width, sources[0].height, targetWidth, targetHeight); |
| 148 |
glBindTexture(GL_TEXTURE_2D, p.texture); |
| 149 |
|
| 150 |
p.phase = (p.phase + 1) % p.modulo; |
| 151 |
sources.prepend({p.texture, p.width, p.height, p.filter, p.wrap}); |
| 152 |
} |
| 153 |
|
| 154 |
unsigned targetWidth = absoluteWidth ? absoluteWidth : outputWidth; |
| 155 |
unsigned targetHeight = absoluteHeight ? absoluteHeight : outputHeight; |
| 156 |
if(relativeWidth) targetWidth = sources[0].width * relativeWidth; |
| 157 |
if(relativeHeight) targetHeight = sources[0].height * relativeHeight; |
| 158 |
|
| 159 |
glUseProgram(program); |
| 160 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 161 |
|
| 162 |
glrUniform1i("source[0]", 0); |
| 163 |
glrUniform4f("targetSize", targetWidth, targetHeight, 1.0 / targetWidth, 1.0 / targetHeight); |
| 164 |
glrUniform4f("outputSize", outputWidth, outputHeight, 1.0 / outputWidth, 1.0 / outputHeight); |
| 165 |
|
| 166 |
glrParameters(sources[0].filter, sources[0].wrap); |
| 167 |
render(sources[0].width, sources[0].height, outputWidth, outputHeight); |
| 168 |
|
| 169 |
if(history.size() > 0) { |
| 170 |
OpenGLTexture frame = history.takeLast(); |
| 171 |
|
| 172 |
glBindTexture(GL_TEXTURE_2D, frame.texture); |
| 173 |
if(width == frame.width && height == frame.height) { |
| 174 |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, getFormat(), getType(), buffer); |
| 175 |
} else { |
| 176 |
glTexImage2D(GL_TEXTURE_2D, 0, format, frame.width = width, frame.height = height, 0, getFormat(), getType(), buffer); |
| 177 |
} |
| 178 |
|
| 179 |
history.prepend(frame); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
bool OpenGL::init() { |
| 184 |
if(!OpenGLBind()) return false; |
| 185 |
|
| 186 |
glDisable(GL_ALPHA_TEST); |
| 187 |
glDisable(GL_BLEND); |
| 188 |
glDisable(GL_DEPTH_TEST); |
| 189 |
glDisable(GL_POLYGON_SMOOTH); |
| 190 |
glDisable(GL_STENCIL_TEST); |
| 191 |
|
| 192 |
glEnable(GL_DITHER); |
| 193 |
glEnable(GL_TEXTURE_2D); |
| 194 |
|
| 195 |
program = glCreateProgram(); |
| 196 |
vertex = glrCreateShader(program, GL_VERTEX_SHADER, OpenGLOutputVertexShader); |
| 197 |
//geometry = glrCreateShader(program, GL_GEOMETRY_SHADER, OpenGLGeometryShader); |
| 198 |
fragment = glrCreateShader(program, GL_FRAGMENT_SHADER, OpenGLFragmentShader); |
| 199 |
OpenGLSurface::allocate(); |
| 200 |
glrLinkProgram(program); |
| 201 |
|
| 202 |
shader(nullptr); |
| 203 |
return true; |
| 204 |
} |
| 205 |
|
| 206 |
void OpenGL::term() { |
| 207 |
shader(nullptr); //release shader resources (eg frame[] history) |
| 208 |
OpenGLSurface::release(); |
| 209 |
if(buffer) { delete[] buffer; buffer = nullptr; } |
| 210 |
} |