fix shader for iOS, remove switch to if-else sequence

This commit is contained in:
2017-12-06 00:32:15 +00:00
parent 9978709645
commit d18b1103bb
4 changed files with 36 additions and 23 deletions

View File

@@ -101,18 +101,27 @@ void App::initShaders()
" sum += textureOffset(t, uv, ivec2( 1, 1));\n" " sum += textureOffset(t, uv, ivec2( 1, 1));\n"
" return sum / vec4(9.0);\n" " return sum / vec4(9.0);\n"
"}\n" "}\n"
"mediump vec3 blend_normal(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot) { return mix(base.rgb, stroke.rgb, stroke.a/alpha_tot); }\n"
"mediump vec3 blend_multiply(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot) { return mix(stroke.rgb, mix(base.rgb, base.rgb*stroke.rgb, stroke.a/alpha_tot), base.a/alpha_tot); }\n" "mediump vec3 blend_normal(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"
"mediump vec3 blend_screen(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot) { return mix(stroke.rgb, mix(base.rgb, 1.0-(1.0-base.rgb)*(1.0-stroke.rgb), stroke.a/alpha_tot), base.a/alpha_tot); }\n" "{ return mix(base.rgb, stroke.rgb, stroke.a/alpha_tot); }\n"
"mediump vec3 blend_colorDodge(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot) { return mix(stroke.rgb, mix(base.rgb, base.rgb/(1.0-stroke.rgb), stroke.a/alpha_tot), base.a/alpha_tot); }\n" "mediump vec3 blend_multiply(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"
"mediump vec3 blend_overlay(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot) { return mix(stroke.rgb, mix(base.rgb, mix(2.0*base.rgb*stroke.rgb, 1.0-2.0*(1.0-base.rgb)*(1.0-stroke.rgb), floor(base.rgb*2.0)), stroke.a/alpha_tot), base.a/alpha_tot); }\n" "{ return mix(stroke.rgb, mix(base.rgb, base.rgb*stroke.rgb, stroke.a/alpha_tot), base.a/alpha_tot); }\n"
"mediump vec3 blend(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot, int mode) { switch(mode){\n" "mediump vec3 blend_screen(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"
" case 0: return blend_normal(base, stroke, alpha_tot);" "{ return mix(stroke.rgb, mix(base.rgb, 1.0-(1.0-base.rgb)*(1.0-stroke.rgb), stroke.a/alpha_tot), base.a/alpha_tot); }\n"
" case 1: return blend_multiply(base, stroke, alpha_tot);" "mediump vec3 blend_colorDodge(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"
" case 2: return blend_screen(base, stroke, alpha_tot);" "{ return mix(stroke.rgb, mix(base.rgb, base.rgb/(1.0-stroke.rgb), stroke.a/alpha_tot), base.a/alpha_tot); }\n"
" case 3: return blend_colorDodge(base, stroke, alpha_tot);" "mediump vec3 blend_overlay(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot)"
" case 4: return blend_overlay(base, stroke, alpha_tot);" "{ return mix(stroke.rgb, mix(base.rgb, mix(2.0*base.rgb*stroke.rgb, 1.0-2.0*(1.0-base.rgb)*(1.0-stroke.rgb), floor(base.rgb*2.0)), stroke.a/alpha_tot), base.a/alpha_tot); }\n"
"}}\n"
"mediump vec3 blend(mediump vec4 base, mediump vec4 stroke, mediump float alpha_tot, int mode) {\n"
" if (mode == 0) return blend_normal(base, stroke, alpha_tot);\n"
" else if (mode == 1) return blend_multiply(base, stroke, alpha_tot);\n"
" else if (mode == 2) return blend_screen(base, stroke, alpha_tot);\n"
" else if (mode == 3) return blend_colorDodge(base, stroke, alpha_tot);\n"
" else if (mode == 4) return blend_overlay(base, stroke, alpha_tot);\n"
" else return blend_multiply(base, stroke, alpha_tot);\n"
"}\n"
"void main(){\n" "void main(){\n"
" mediump vec4 base = texture(tex, uv.xy);\n" " mediump vec4 base = texture(tex, uv.xy);\n"
" mediump vec4 stroke = texture(tex_stroke, uv.xy);\n" " mediump vec4 stroke = texture(tex_stroke, uv.xy);\n"

View File

@@ -240,7 +240,9 @@ void ui::Canvas::stroke_draw()
glDisable(GL_BLEND); glDisable(GL_BLEND);
ShaderManager::use(ui::kShader::Stroke); ShaderManager::use(ui::kShader::Stroke);
ShaderManager::u_int(kShaderUniform::Tex, 0); // brush ShaderManager::u_int(kShaderUniform::Tex, 0); // brush
#ifndef __IOS__
ShaderManager::u_int(kShaderUniform::TexBG, 1); // bg ShaderManager::u_int(kShaderUniform::TexBG, 1); // bg
#endif
//ShaderManager::u_int(kShaderUniform::TexStencil, 3); // stencil //ShaderManager::u_int(kShaderUniform::TexStencil, 3); // stencil
ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color); ShaderManager::u_vec4(kShaderUniform::Col, m_brush.m_tip_color);
ShaderManager::u_vec2(kShaderUniform::Resolution, { m_width, m_height }); ShaderManager::u_vec2(kShaderUniform::Resolution, { m_width, m_height });

View File

@@ -143,7 +143,7 @@ void NodeCanvas::draw()
} }
else if(m_canvas->m_show_tmp && m_canvas->m_current_layer_idx == layer_index) else if(m_canvas->m_show_tmp && m_canvas->m_current_layer_idx == layer_index)
{ {
m_sampler_linear.bind(0); m_sampler.bind(0);
auto& paper = TextureManager::get(const_hash("data/paper.jpg")); auto& paper = TextureManager::get(const_hash("data/paper.jpg"));
ui::ShaderManager::use(kShader::CompDraw); ui::ShaderManager::use(kShader::CompDraw);
ui::ShaderManager::u_int(kShaderUniform::Tex, 0); ui::ShaderManager::u_int(kShaderUniform::Tex, 0);

View File

@@ -112,30 +112,32 @@ void Shader::use()
} }
void Shader::u_vec4(kShaderUniform id, const glm::vec4& v) void Shader::u_vec4(kShaderUniform id, const glm::vec4& v)
{ {
if (m_umap.count(id) == 0) LOG("UNIFORM %d NOT FOUND in shader %d", (int)id, (int)name); if (m_umap.count(id) == 0) LOG("UNIFORM vec4 %d NOT FOUND in shader %d", (int)id, (int)name)
glUniform4fv(m_umap[id], 1, glm::value_ptr(v)); else glUniform4fv(m_umap[id], 1, glm::value_ptr(v));
} }
void Shader::u_vec2(kShaderUniform id, const glm::vec2& v) void Shader::u_vec2(kShaderUniform id, const glm::vec2& v)
{ {
if (m_umap.count(id) == 0) LOG("UNIFORM %d NOT FOUND in shader %d", (int)id, (int)name); if (m_umap.count(id) == 0) LOG("UNIFORM vec2 %d NOT FOUND in shader %d", (int)id, (int)name)
glUniform2fv(m_umap[id], 1, glm::value_ptr(v)); else glUniform2fv(m_umap[id], 1, glm::value_ptr(v));
} }
void Shader::u_mat4(kShaderUniform id, const glm::mat4& m) void Shader::u_mat4(kShaderUniform id, const glm::mat4& m)
{ {
if (m_umap.count(id) == 0) LOG("UNIFORM %d NOT FOUND in shader %d", (int)id, (int)name); if (m_umap.count(id) == 0) LOG("UNIFORM mat4 %d NOT FOUND in shader %d", (int)id, (int)name)
glUniformMatrix4fv(m_umap[id], 1, GL_FALSE, glm::value_ptr(m)); else glUniformMatrix4fv(m_umap[id], 1, GL_FALSE, glm::value_ptr(m));
} }
void Shader::u_int(kShaderUniform id, int i) void Shader::u_int(kShaderUniform id, int i)
{ {
if (m_umap.count(id) == 0) LOG("UNIFORM %d NOT FOUND in shader %d", (int)id, (int)name); if (m_umap.count(id) == 0)
glUniform1i(m_umap[id], i); LOG("UNIFORM int %d NOT FOUND in shader %d", (int)id, (int)name)
else
glUniform1i(m_umap[id], i);
} }
void Shader::u_float(kShaderUniform id, float f) void Shader::u_float(kShaderUniform id, float f)
{ {
if (m_umap.count(id) == 0) LOG("UNIFORM %d NOT FOUND in shader %d", (int)id, (int)name); if (m_umap.count(id) == 0) LOG("UNIFORM float %d NOT FOUND in shader %d", (int)id, (int)name)
glUniform1f(m_umap[id], f); else glUniform1f(m_umap[id], f);
} }
GLint ui::Shader::GetAttribLocation(const char* name) GLint ui::Shader::GetAttribLocation(const char* name)
{ {