summaryrefslogtreecommitdiff
path: root/drivers/gles2
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gles2')
-rw-r--r--drivers/gles2/rasterizer_gles2.cpp127
-rw-r--r--drivers/gles2/rasterizer_gles2.h4
-rw-r--r--drivers/gles2/shader_compiler_gles2.cpp40
-rw-r--r--drivers/gles2/shader_compiler_gles2.h5
-rw-r--r--drivers/gles2/shaders/canvas.glsl130
5 files changed, 301 insertions, 5 deletions
diff --git a/drivers/gles2/rasterizer_gles2.cpp b/drivers/gles2/rasterizer_gles2.cpp
index 59f844904e..5617f5bd0b 100644
--- a/drivers/gles2/rasterizer_gles2.cpp
+++ b/drivers/gles2/rasterizer_gles2.cpp
@@ -1411,6 +1411,9 @@ void RasterizerGLES2::shader_set_mode(RID p_shader,VS::ShaderMode p_mode) {
case VS::SHADER_MATERIAL: {
material_shader.free_custom_shader(shader->custom_code_id);
} break;
+ case VS::SHADER_CANVAS_ITEM: {
+ canvas_shader.free_custom_shader(shader->custom_code_id);
+ } break;
}
shader->custom_code_id=0;
@@ -1422,6 +1425,9 @@ void RasterizerGLES2::shader_set_mode(RID p_shader,VS::ShaderMode p_mode) {
case VS::SHADER_MATERIAL: {
shader->custom_code_id=material_shader.create_custom_shader();
} break;
+ case VS::SHADER_CANVAS_ITEM: {
+ shader->custom_code_id=canvas_shader.create_custom_shader();
+ } break;
}
_shader_make_dirty(shader);
@@ -4518,7 +4524,33 @@ void RasterizerGLES2::_update_shader( Shader* p_shader) const {
}
material_shader.set_custom_shader_code(p_shader->custom_code_id,vertex_code, vertex_globals,fragment_code, light_code, fragment_globals,uniform_names,enablers);
- } else {
+ } else if (p_shader->mode==VS::SHADER_CANVAS_ITEM) {
+
+ Vector<const char*> enablers;
+
+ if (light_flags.uses_time || fragment_flags.uses_time || vertex_flags.uses_time) {
+ enablers.push_back("#define USE_TIME\n");
+ uses_time=true;
+ }
+ if (fragment_flags.uses_normal) {
+ enablers.push_back("#define NORMAL_USED\n");
+ }
+ if (light_flags.uses_light) {
+ enablers.push_back("#define USE_LIGHT_SHADER_CODE\n");
+ }
+ if (fragment_flags.use_var1_interp || vertex_flags.use_var1_interp)
+ enablers.push_back("#define ENABLE_VAR1_INTERP\n");
+ if (fragment_flags.use_var2_interp || vertex_flags.use_var2_interp)
+ enablers.push_back("#define ENABLE_VAR2_INTERP\n");
+ if (fragment_flags.uses_texscreen) {
+ enablers.push_back("#define ENABLE_TEXSCREEN\n");
+ }
+ if (fragment_flags.uses_screen_uv) {
+ enablers.push_back("#define ENABLE_SCREEN_UV\n");
+ }
+
+ canvas_shader.set_custom_shader_code(p_shader->custom_code_id,vertex_code, vertex_globals,fragment_code, light_code, fragment_globals,uniform_names,enablers);
+
//postprocess_shader.set_custom_shader_code(p_shader->custom_code_id,vertex_code, vertex_globals,fragment_code, fragment_globals,uniform_names);
}
@@ -4529,6 +4561,7 @@ void RasterizerGLES2::_update_shader( Shader* p_shader) const {
p_shader->has_texscreen=fragment_flags.uses_texscreen;
p_shader->has_screen_uv=fragment_flags.uses_screen_uv;
p_shader->can_zpass=!fragment_flags.uses_discard && !vertex_flags.vertex_code_writes_vertex;
+ p_shader->uses_normal=fragment_flags.uses_normal || light_flags.uses_normal;
p_shader->uses_time=uses_time;
p_shader->version++;
@@ -7722,6 +7755,7 @@ void RasterizerGLES2::canvas_begin() {
canvas_tex=RID();
//material_shader.unbind();
canvas_shader.unbind();
+ canvas_shader.set_custom_shader(0);
canvas_shader.bind();
canvas_shader.set_uniform(CanvasShaderGLES2::TEXTURE, 0);
_set_color_attrib(Color(1,1,1));
@@ -8219,8 +8253,20 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) {
CanvasItem *current_clip=NULL;
+ RID last_shader;
+
+ Transform canvas_transform;
+ canvas_transform.translate(-(viewport.width / 2.0f), -(viewport.height / 2.0f), 0.0f);
+ float csy = 1.0;
+ if (current_rt && current_rt_vflip)
+ csy = -1.0;
+
+ canvas_transform.scale( Vector3( 2.0f / viewport.width, csy * -2.0f / viewport.height, 1.0f ) );
+ canvas_shader.set_uniform(CanvasShaderGLES2::PROJECTION_MATRIX,canvas_transform);
+ canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Matrix32());
+
while(p_item_list) {
CanvasItem *ci=p_item_list;
@@ -8231,6 +8277,7 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) {
}
memdelete(ci->vp_render);
ci->vp_render=NULL;
+ last_shader=RID();
}
if (current_clip!=ci->final_clip_owner) {
@@ -8248,10 +8295,86 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) {
glDisable(GL_SCISSOR_TEST);
}
}
+
+
//begin rect
+
+ if (ci->shader!=last_shader) {
+
+ Shader *shader = NULL;
+ if (ci->shader.is_valid()) {
+ shader = shader_owner.get(ci->shader);
+ if (shader && !shader->valid) {
+ shader=NULL;
+ }
+ }
+
+ if (shader) {
+ canvas_shader.set_custom_shader(shader->custom_code_id);
+ canvas_shader.bind();
+
+ if (ci->shader_version!=shader->version) {
+ //todo optimize uniforms
+ ci->shader_version=shader->version;
+ }
+ //this can be optimized..
+ int tex_id=1;
+ int idx=0;
+ for(Map<StringName,ShaderLanguage::Uniform>::Element *E=shader->uniforms.front();E;E=E->next()) {
+
+
+ Map<StringName,Variant>::Element *F=ci->shader_param.find(E->key());
+ Variant &v=F?F->get():E->get().default_value;
+ if (v.get_type()==Variant::_RID || v.get_type()==Variant::OBJECT) {
+ int loc = canvas_shader.get_custom_uniform_location(idx); //should be automatic..
+
+ glActiveTexture(GL_TEXTURE0+tex_id);
+ RID tex = v;
+ Texture *t=texture_owner.get(tex);
+ if (!t)
+ glBindTexture(GL_TEXTURE_2D,white_tex);
+ else
+ glBindTexture(t->target,t->tex_id);
+
+ glUniform1i(loc,tex_id);
+ tex_id++;
+
+ } else {
+ canvas_shader.set_custom_uniform(idx,v);
+ }
+
+ idx++;
+ }
+
+
+ if (shader->has_texscreen && framebuffer.active) {
+ canvas_shader.set_uniform(CanvasShaderGLES2::TEXSCREEN_SCREEN_MULT,Vector2(float(viewport.width)/framebuffer.width,float(viewport.height)/framebuffer.height));
+ canvas_shader.set_uniform(CanvasShaderGLES2::TEXSCREEN_TEX,tex_id);
+ glActiveTexture(GL_TEXTURE0+tex_id);
+ glBindTexture(GL_TEXTURE_2D,framebuffer.sample_color);
+
+ }
+ if (shader->has_screen_uv) {
+ canvas_shader.set_uniform(CanvasShaderGLES2::SCREEN_UV_MULT,Vector2(1.0/viewport.width,1.0/viewport.height));
+ }
+
+ if (shader->uses_time) {
+ canvas_shader.set_uniform(CanvasShaderGLES2::TIME,Math::fmod(last_time,300.0));
+ draw_next_frame=true;
+ }
+ //if uses TIME - draw_next_frame=true
+
+ } else {
+ canvas_shader.set_custom_shader(0);
+ }
+
+ canvas_shader.set_uniform(CanvasShaderGLES2::PROJECTION_MATRIX,canvas_transform);
+ }
+
canvas_shader.set_uniform(CanvasShaderGLES2::MODELVIEW_MATRIX,ci->final_transform);
canvas_shader.set_uniform(CanvasShaderGLES2::EXTRA_MATRIX,Matrix32());
+
bool reclip=false;
if (ci==p_item_list || ci->blend_mode!=canvas_blend_mode) {
@@ -8291,6 +8414,8 @@ void RasterizerGLES2::canvas_render_items(CanvasItem *p_item_list) {
int cc=ci->commands.size();
CanvasItem::Command **commands = ci->commands.ptr();
+ canvas_opacity = ci->final_opacity;
+
for(int i=0;i<cc;i++) {
CanvasItem::Command *c=commands[i];
diff --git a/drivers/gles2/rasterizer_gles2.h b/drivers/gles2/rasterizer_gles2.h
index f97d112395..f0dae00b53 100644
--- a/drivers/gles2/rasterizer_gles2.h
+++ b/drivers/gles2/rasterizer_gles2.h
@@ -191,6 +191,7 @@ class RasterizerGLES2 : public Rasterizer {
bool writes_vertex;
bool uses_discard;
bool uses_time;
+ bool uses_normal;
Map<StringName,ShaderLanguage::Uniform> uniforms;
StringName first_texture;
@@ -214,6 +215,7 @@ class RasterizerGLES2 : public Rasterizer {
writes_vertex=false;
uses_discard=false;
uses_time=false;
+ uses_normal=false;
}
@@ -1199,7 +1201,7 @@ class RasterizerGLES2 : public Rasterizer {
RID overdraw_material;
mutable MaterialShaderGLES2 material_shader;
- CanvasShaderGLES2 canvas_shader;
+ mutable CanvasShaderGLES2 canvas_shader;
BlurShaderGLES2 blur_shader;
CopyShaderGLES2 copy_shader;
diff --git a/drivers/gles2/shader_compiler_gles2.cpp b/drivers/gles2/shader_compiler_gles2.cpp
index 8eed423b8a..204a81d193 100644
--- a/drivers/gles2/shader_compiler_gles2.cpp
+++ b/drivers/gles2/shader_compiler_gles2.cpp
@@ -181,6 +181,9 @@ String ShaderCompilerGLES2::dump_node_code(SL::Node *p_node,int p_level,bool p_a
if (vnode->name==vname_normalmap) {
uses_normalmap=true;
}
+ if (vnode->name==vname_normal) {
+ uses_normal=true;
+ }
if (vnode->name==vname_screen_uv) {
uses_screen_uv=true;
}
@@ -552,6 +555,7 @@ Error ShaderCompilerGLES2::compile(const String& p_code, ShaderLanguage::ShaderT
uses_light=false;
uses_time=false;
uses_normalmap=false;
+ uses_normal=false;
vertex_code_writes_vertex=false;
uniforms=r_uniforms;
flags=&r_flags;
@@ -562,6 +566,7 @@ Error ShaderCompilerGLES2::compile(const String& p_code, ShaderLanguage::ShaderT
r_flags.use_var1_interp=false;
r_flags.use_var2_interp=false;
r_flags.uses_normalmap=false;
+ r_flags.uses_normal=false;
String error;
int errline,errcol;
@@ -584,6 +589,7 @@ Error ShaderCompilerGLES2::compile(const String& p_code, ShaderLanguage::ShaderT
r_flags.uses_light=uses_light;
r_flags.uses_time=uses_time;
r_flags.uses_normalmap=uses_normalmap;
+ r_flags.uses_normal=uses_normalmap;
r_code_line=code;
r_globals_line=global_code;
@@ -721,6 +727,39 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
mode_replace_table[2]["POINT_COORD"]="gl_PointCoord";
mode_replace_table[2]["TIME"]="time";
+ mode_replace_table[3]["SRC_VERTEX"]="src_vtx";
+ mode_replace_table[3]["VERTEX"]="outvec.xy";
+ mode_replace_table[3]["UV"]="uv_interp";
+ mode_replace_table[3]["COLOR"]="color_interp";
+ mode_replace_table[3]["VAR1"]="var1_interp";
+ mode_replace_table[3]["VAR2"]="var2_interp";
+ mode_replace_table[3]["POINT_SIZE"]="gl_PointSize";
+ mode_replace_table[3]["WORLD_MATRIX"]="modelview_matrix";
+ mode_replace_table[3]["PROJECTION_MATRIX"]="projection_matrix";
+ mode_replace_table[3]["EXTRA_MATRIX"]="extra_matrix";
+ mode_replace_table[3]["TIME"]="time";
+
+ mode_replace_table[4]["POSITION"]="gl_Position";
+ mode_replace_table[4]["NORMAL"]="normal";
+ mode_replace_table[4]["UV"]="uv_interp";
+ mode_replace_table[4]["COLOR"]="color_interp";
+ mode_replace_table[4]["TEXTURE"]="texture";
+ mode_replace_table[4]["VAR1"]="var1_interp";
+ mode_replace_table[4]["VAR2"]="var2_interp";
+ mode_replace_table[4]["SCREEN_UV"]="screen_uv";
+ mode_replace_table[4]["POINT_COORD"]="gl_PointCoord";
+ mode_replace_table[4]["TIME"]="time";
+
+ mode_replace_table[5]["COLOR"]="color";
+ mode_replace_table[5]["NORMAL"]="normal";
+ mode_replace_table[5]["LIGHT_DIR"]="light_dir";
+ mode_replace_table[5]["LIGHT_DISTANCE"]="light_distance";
+ mode_replace_table[5]["LIGHT"]="light";
+ mode_replace_table[5]["POINT_COORD"]="gl_PointCoord";
+ mode_replace_table[5]["TIME"]="time";
+
+
+
//mode_replace_table[2]["SCREEN_POS"]="SCREEN_POS";
//mode_replace_table[2]["SCREEN_TEXEL_SIZE"]="SCREEN_TEXEL_SIZE";
@@ -741,5 +780,6 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
vname_light="LIGHT";
vname_time="TIME";
vname_normalmap="NORMALMAP";
+ vname_normal="NORMAL";
}
diff --git a/drivers/gles2/shader_compiler_gles2.h b/drivers/gles2/shader_compiler_gles2.h
index 5012414c8b..47ba4aa474 100644
--- a/drivers/gles2/shader_compiler_gles2.h
+++ b/drivers/gles2/shader_compiler_gles2.h
@@ -51,6 +51,7 @@ private:
bool uses_time;
bool uses_screen_uv;
bool uses_normalmap;
+ bool uses_normal;
bool vertex_code_writes_vertex;
Flags *flags;
@@ -68,6 +69,7 @@ private:
StringName vname_light;
StringName vname_time;
StringName vname_normalmap;
+ StringName vname_normal;
Map<StringName,ShaderLanguage::Uniform> *uniforms;
@@ -79,7 +81,7 @@ private:
String replace_string(const StringName& p_string);
- Map<StringName,StringName> mode_replace_table[3];
+ Map<StringName,StringName> mode_replace_table[9];
Map<StringName,StringName> replace_table;
public:
@@ -101,6 +103,7 @@ public:
bool use_var2_interp;
bool uses_light;
bool uses_time;
+ bool uses_normal;
};
Error compile(const String& p_code, ShaderLanguage::ShaderType p_type, String& r_code_line, String& r_globals_line, Flags& r_flags, Map<StringName,ShaderLanguage::Uniform> *r_uniforms=NULL);
diff --git a/drivers/gles2/shaders/canvas.glsl b/drivers/gles2/shaders/canvas.glsl
index f36741d586..3e7e54e0fe 100644
--- a/drivers/gles2/shaders/canvas.glsl
+++ b/drivers/gles2/shaders/canvas.glsl
@@ -18,19 +18,56 @@ attribute highp vec2 uv_attrib; // attrib:4
varying vec2 uv_interp;
varying vec4 color_interp;
+#if defined(USE_TIME)
+uniform float time;
+#endif
+
+
+#ifdef USE_LIGHTING
+
+uniform highp mat4 light_matrix;
+varying vec4 light_tex_pos;
+
+#endif
+
+#if defined(ENABLE_VAR1_INTERP)
+varying vec4 var1_interp;
+#endif
+
+#if defined(ENABLE_VAR2_INTERP)
+varying vec4 var2_interp;
+#endif
+
//uniform bool snap_pixels;
+VERTEX_SHADER_GLOBALS
+
void main() {
color_interp = color_attrib;
uv_interp = uv_attrib;
highp vec4 outvec = vec4(vertex, 1.0);
+{
+ vec2 src_vtx=outvec.xy;
+VERTEX_SHADER_CODE
+
+}
outvec = extra_matrix * outvec;
outvec = modelview_matrix * outvec;
#ifdef USE_PIXEL_SNAP
- outvec.xy=floor(outvec.xy+0.5);
+ outvec.xy=floor(outvec.xy+0.5);
+#endif
+
+
+#ifdef USE_LIGHTING
+
+ light_tex_pos.xy = light_matrix * outvec;
+ light_tex_pos.zw=outvec.xy - light_matrix[4].xy; //likely wrong
+
#endif
+
+
gl_Position = projection_matrix * outvec;
}
@@ -54,17 +91,106 @@ varying vec4 color_interp;
#endif
+#if defined(ENABLE_SCREEN_UV)
+
+uniform vec2 screen_uv_mult;
+
+#endif
+
+#if defined(ENABLE_TEXSCREEN)
+
+uniform vec2 texscreen_screen_mult;
+uniform sampler2D texscreen_tex;
+
+#endif
+
+
+#if defined(ENABLE_VAR1_INTERP)
+varying vec4 var1_interp;
+#endif
+
+#if defined(ENABLE_VAR2_INTERP)
+varying vec4 var2_interp;
+#endif
+
+#if defined(USE_TIME)
+uniform float time;
+#endif
+
+
+#ifdef USE_LIGHTING
+
+uniform sampler2D light_texture;
+varying vec4 light_tex_pos;
+
+#ifdef USE_SHADOWS
+
+uniform sampler2D shadow_texture;
+uniform float shadow_attenuation;
+
+#endif
+
+#endif
+
+FRAGMENT_SHADER_GLOBALS
+
+
void main() {
vec4 color = color_interp;
-
+#if defined(NORMAL_USED)
+ vec3 normal = vec3(0,0,1);
+#endif
+
color *= texture2D( texture, uv_interp );
+#if defined(ENABLE_SCREEN_UV)
+ vec2 screen_uv = gl_FragCoord.xy*screen_uv_mult;
+#endif
+{
+FRAGMENT_SHADER_CODE
+}
#ifdef DEBUG_ENCODED_32
highp float enc32 = dot( color,highp vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0,1) );
color = vec4(vec3(enc32),1.0);
#endif
+#ifdef USE_LIGHTING
+
+ float att=1.0;
+
+ vec3 light = texture2D(light_texture,light_tex_pos).rgb;
+#ifdef USE_SHADOWS
+ //this might not be that great on mobile?
+ float light_dist = length(light_texture.zw);
+ float light_angle = atan2(light_texture.x,light_texture.z) + 1.0 * 0.5;
+ float shadow_dist = texture2D(shadow_texture,vec2(light_angle,0));
+ if (light_dist>shadow_dist) {
+ light*=shadow_attenuation;
+ }
+//use shadows
+#endif
+
+#if defined(USE_LIGHT_SHADER_CODE)
+//light is written by the light shader
+{
+ vec2 light_dir = normalize(light_tex_pos.zw);
+ float light_distance = length(light_tex_pos.zw);
+LIGHT_SHADER_CODE
+}
+#else
+
+#if defined(NORMAL_USED)
+ vec2 light_normal = normalize(light_tex_pos.zw);
+ light = color.rgb * light * max(dot(light_normal,normal),0);
+#endif
+
+ color.rgb=light;
+//light shader code
+#endif
+
+//use lighting
+#endif
// color.rgb*=color.a;
gl_FragColor = color;