diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/gles3/rasterizer_canvas_gles3.cpp | 4 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_gles3.cpp | 2 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_scene_gles3.cpp | 730 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_scene_gles3.h | 103 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_storage_gles3.cpp | 333 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_storage_gles3.h | 57 | ||||
-rw-r--r-- | drivers/gles3/shaders/cubemap_filter.glsl | 40 | ||||
-rw-r--r-- | drivers/gles3/shaders/scene.glsl | 206 |
8 files changed, 1376 insertions, 99 deletions
diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp index c455d4bd11..4573f94d22 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.cpp +++ b/drivers/gles3/rasterizer_canvas_gles3.cpp @@ -1382,10 +1382,6 @@ void RasterizerCanvasGLES3::reset_canvas() { glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glDisable(GL_SCISSOR_TEST); -#ifdef GLEW_ENABLED - glDisable(GL_POINT_SPRITE); - glDisable(GL_VERTEX_PROGRAM_POINT_SIZE); -#endif glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { diff --git a/drivers/gles3/rasterizer_gles3.cpp b/drivers/gles3/rasterizer_gles3.cpp index c799cd12af..dabb7918a9 100644 --- a/drivers/gles3/rasterizer_gles3.cpp +++ b/drivers/gles3/rasterizer_gles3.cpp @@ -122,11 +122,13 @@ void RasterizerGLES3::begin_frame(){ storage->frame.time[1]=Math::fmod(time_total,3600); storage->frame.time[2]=Math::fmod(time_total,900); storage->frame.time[3]=Math::fmod(time_total,60); + storage->frame.count++; storage->update_dirty_shaders(); storage->update_dirty_materials(); scene->iteration(); + } void RasterizerGLES3::set_current_render_target(RID p_render_target){ diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp index 4ae257abc4..e04908ea67 100644 --- a/drivers/gles3/rasterizer_scene_gles3.cpp +++ b/drivers/gles3/rasterizer_scene_gles3.cpp @@ -446,6 +446,338 @@ int RasterizerSceneGLES3::get_directional_light_shadow_size(RID p_light_intance) return shadow_size; } +////////////////////////////////////////////////////// + +RID RasterizerSceneGLES3::reflection_atlas_create() { + + ReflectionAtlas *reflection_atlas = memnew( ReflectionAtlas ); + reflection_atlas->subdiv=0; + reflection_atlas->color=0; + for(int i=0;i<6;i++) { + reflection_atlas->fbo[i]=0; + } + + return reflection_atlas_owner.make_rid(reflection_atlas); +} + +void RasterizerSceneGLES3::reflection_atlas_set_size(RID p_ref_atlas,int p_size) { + + ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_ref_atlas); + ERR_FAIL_COND(!reflection_atlas); + + int size = nearest_power_of_2(p_size); + + if (size==reflection_atlas->size) + return; + if (reflection_atlas->size) { + for(int i=0;i<6;i++) { + glDeleteFramebuffers(1,&reflection_atlas->fbo[i]); + reflection_atlas->fbo[i]=0; + } + glDeleteTextures(1,&reflection_atlas->color); + reflection_atlas->color=0; + } + + reflection_atlas->size=size; + + for(int i=0;i<reflection_atlas->reflections.size();i++) { + //erase probes reference to this + if (reflection_atlas->reflections[i].owner.is_valid()) { + ReflectionProbeInstance *reflection_probe_instance = reflection_probe_instance_owner.getornull(reflection_atlas->reflections[i].owner); + reflection_atlas->reflections[i].owner=RID(); + + ERR_CONTINUE(!reflection_probe_instance); + reflection_probe_instance->reflection_atlas_index=-1; + reflection_probe_instance->atlas=RID(); + reflection_probe_instance->render_step=-1; + } + } + + + if (reflection_atlas->size) { + + bool use_float=true; + + + GLenum internal_format = use_float?GL_RGBA16F:GL_RGB10_A2; + GLenum format = GL_RGBA; + GLenum type = use_float?GL_HALF_FLOAT:GL_UNSIGNED_INT_2_10_10_10_REV; + + + // Create a texture for storing the color + glActiveTexture(GL_TEXTURE0); + glGenTextures(1, &reflection_atlas->color); + glBindTexture(GL_TEXTURE_2D, reflection_atlas->color); + + int mmsize=reflection_atlas->size; + + for(int i=0;i<6;i++) { + glTexImage2D(GL_TEXTURE_2D, i, internal_format, mmsize, mmsize, 0, + format, type, NULL); + + mmsize>>=1; + } + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + mmsize=reflection_atlas->size; + + for(int i=0;i<6;i++) { + glGenFramebuffers(1, &reflection_atlas->fbo[i]); + glBindFramebuffer(GL_FRAMEBUFFER, reflection_atlas->fbo[i]); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, reflection_atlas->color, i); + glDisable(GL_SCISSOR_TEST); + glViewport(0,0,mmsize,mmsize); + glClearColor(0,0,0,0); + glClear(GL_COLOR_BUFFER_BIT); //it needs to be cleared, to avoid generating garbage + + mmsize>>=1; + + } + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5); + + } + + + +} +void RasterizerSceneGLES3::reflection_atlas_set_subdivision(RID p_ref_atlas,int p_subdiv) { + + ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_ref_atlas); + ERR_FAIL_COND(!reflection_atlas); + + uint32_t subdiv = nearest_power_of_2(p_subdiv); + if (subdiv&0xaaaaaaaa) { //sqrt(subdiv) must be integer + subdiv<<=1; + } + + subdiv=int(Math::sqrt(subdiv)); + + if (reflection_atlas->subdiv==subdiv) + return; + + + if (subdiv) { + + for(int i=0;i<reflection_atlas->reflections.size();i++) { + //erase probes reference to this + if (reflection_atlas->reflections[i].owner.is_valid()) { + ReflectionProbeInstance *reflection_probe_instance = reflection_probe_instance_owner.getornull(reflection_atlas->reflections[i].owner); + reflection_atlas->reflections[i].owner=RID(); + + ERR_CONTINUE(!reflection_probe_instance); + reflection_probe_instance->reflection_atlas_index=-1; + reflection_probe_instance->atlas=RID(); + reflection_probe_instance->render_step=-1; + } + } + } + + reflection_atlas->subdiv=subdiv; + + reflection_atlas->reflections.resize(subdiv*subdiv); +} + + +//////////////////////////////////////////////////// + +RID RasterizerSceneGLES3::reflection_probe_instance_create(RID p_probe) { + + RasterizerStorageGLES3::ReflectionProbe *probe = storage->reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!probe,RID()); + + ReflectionProbeInstance *rpi = memnew( ReflectionProbeInstance ); + + rpi->probe_ptr=probe; + rpi->self=reflection_probe_instance_owner.make_rid(rpi); + rpi->probe=p_probe; + rpi->reflection_atlas_index=-1; + rpi->render_step=-1; + rpi->last_pass=0; + + return rpi->self; +} + +void RasterizerSceneGLES3::reflection_probe_instance_set_transform(RID p_instance,const Transform& p_transform) { + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND(!rpi); + rpi->transform=p_transform; + +} + +void RasterizerSceneGLES3::reflection_probe_release_atlas_index(RID p_instance) { + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND(!rpi); + if (rpi->reflection_atlas_index==-1) + return; + + ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(rpi->atlas); + ERR_FAIL_COND(!reflection_atlas); + + ERR_FAIL_INDEX(rpi->reflection_atlas_index,reflection_atlas->reflections.size()); + + ERR_FAIL_COND(reflection_atlas->reflections[rpi->reflection_atlas_index].owner!=rpi->self); + + reflection_atlas->reflections[rpi->reflection_atlas_index].owner=RID(); + + rpi->reflection_atlas_index=-1; + rpi->atlas=RID(); + rpi->render_step=-1; + +} + +bool RasterizerSceneGLES3::reflection_probe_instance_needs_redraw(RID p_instance) { + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND_V(!rpi,false); + + return rpi->reflection_atlas_index==-1 || rpi->probe_ptr->update_mode==VS::REFLECTION_PROBE_UPDATE_ALWAYS; +} + +bool RasterizerSceneGLES3::reflection_probe_instance_has_reflection(RID p_instance){ + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND_V(!rpi,false); + + return rpi->reflection_atlas_index!=-1; +} + +bool RasterizerSceneGLES3::reflection_probe_instance_begin_render(RID p_instance,RID p_reflection_atlas) { + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND_V(!rpi,false); + + rpi->render_step=0; + + if (rpi->reflection_atlas_index!=-1) { + return true; //got one already + } + + ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_reflection_atlas); + ERR_FAIL_COND_V(!reflection_atlas,false); + + + if (reflection_atlas->size==0 || reflection_atlas->subdiv==0) { + return false; + } + + + int best_free=-1; + int best_used=-1; + uint64_t best_used_frame; + + for(int i=0;i<reflection_atlas->reflections.size();i++) { + if (reflection_atlas->reflections[i].owner==RID()) { + best_free=i; + break; + } + + if (rpi->render_step<0 && reflection_atlas->reflections[i].last_frame<storage->frame.count && + (best_used==-1 || reflection_atlas->reflections[i].last_frame<best_used_frame)) { + best_used=i; + best_used_frame=reflection_atlas->reflections[i].last_frame; + } + } + + if (best_free==-1 && best_used==-1) { + return false ;// sorry, can not do. Try again next frame. + } + + if (best_free==-1) { + //find best from what is used + best_free=best_used; + + ReflectionProbeInstance *victim_rpi = reflection_probe_instance_owner.getornull(reflection_atlas->reflections[best_free].owner); + ERR_FAIL_COND_V(!victim_rpi,false); + victim_rpi->atlas=RID(); + victim_rpi->reflection_atlas_index=-1; + + } + + reflection_atlas->reflections[best_free].owner=p_instance; + reflection_atlas->reflections[best_free].last_frame=storage->frame.count; + + rpi->reflection_atlas_index=best_free; + rpi->atlas=p_reflection_atlas; + rpi->render_step=0; + + return true; +} + +bool RasterizerSceneGLES3::reflection_probe_instance_postprocess_step(RID p_instance) { + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND_V(!rpi,true); + + ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(rpi->atlas); + ERR_FAIL_COND_V(!reflection_atlas,false); + + ERR_FAIL_COND_V(rpi->render_step>=6,true); + + glBindFramebuffer(GL_FRAMEBUFFER,reflection_atlas->fbo[rpi->render_step]); + state.cube_to_dp_shader.bind(); + + int target_size=reflection_atlas->size/reflection_atlas->subdiv; + + int cubemap_index=reflection_cubemaps.size()-1; + + for(int i=reflection_cubemaps.size()-1;i>=0;i--) { + //find appropriate cubemap to render to + if (reflection_cubemaps[i].size>target_size*2) + break; + + cubemap_index=i; + } + + glDisable(GL_BLEND); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_CUBE_MAP,reflection_cubemaps[cubemap_index].cubemap); + glDisable(GL_CULL_FACE); + + storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID,true); + storage->shaders.cubemap_filter.bind(); + + int cell_size = reflection_atlas->size / reflection_atlas->subdiv; + for(int i=0;i<rpi->render_step;i++) { + cell_size>>=1; //mipmaps! + } + int x = (rpi->reflection_atlas_index % reflection_atlas->subdiv) * cell_size; + int y = (rpi->reflection_atlas_index / reflection_atlas->subdiv) * cell_size; + int width=cell_size; + int height=cell_size; + + storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DIRECT_WRITE,rpi->render_step==0); + storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::LOW_QUALITY,rpi->probe_ptr->update_mode==VS::REFLECTION_PROBE_UPDATE_ALWAYS); + for(int i=0;i<2;i++) { + + storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::Z_FLIP,i>0); + storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::ROUGHNESS,rpi->render_step/5.0); + + uint32_t local_width=width,local_height=height; + uint32_t local_x=x,local_y=y; + + local_height/=2; + local_y+=i*local_height; + + glViewport(local_x,local_y,local_width,local_height); + + _copy_screen(); + } + storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DIRECT_WRITE,false); + storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::LOW_QUALITY,false); + + + rpi->render_step++; + + return rpi->render_step==6; +} /* ENVIRONMENT API */ @@ -464,26 +796,12 @@ void RasterizerSceneGLES3::environment_set_background(RID p_env,VS::EnvironmentB env->bg_mode=p_bg; } -void RasterizerSceneGLES3::environment_set_skybox(RID p_env, RID p_skybox, int p_radiance_size){ +void RasterizerSceneGLES3::environment_set_skybox(RID p_env, RID p_skybox){ Environment *env=environment_owner.getornull(p_env); ERR_FAIL_COND(!env); - if (env->skybox_color.is_valid()) { - env->skybox_color=RID(); - } - if (env->skybox_radiance.is_valid()) { - storage->free(env->skybox_radiance); - env->skybox_radiance=RID(); - } - - - if (p_skybox.is_valid()) { - - env->skybox_color=p_skybox; - env->skybox_radiance=storage->texture_create_radiance_cubemap(p_skybox,p_radiance_size); - //env->skybox_irradiance=storage->texture_create_pbr_cubemap(p_skybox,VS::PBR_CUBEMAP_IRRADIANCE,p_irradiance_size); - } + env->skybox=p_skybox; } @@ -775,6 +1093,7 @@ void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e) { RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface*>(e->geometry); glBindVertexArray(s->array_id); // everything is so easy nowadays + } break; } @@ -821,6 +1140,8 @@ void RasterizerSceneGLES3::_setup_light(RenderList::Element *e) { int omni_count=0; int spot_indices[16]; int spot_count=0; + int reflection_indices[16]; + int reflection_count=0; int maxobj = MIN(16,state.max_forward_lights_per_object); @@ -859,6 +1180,32 @@ void RasterizerSceneGLES3::_setup_light(RenderList::Element *e) { glUniform1iv(state.scene_shader.get_uniform(SceneShaderGLES3::SPOT_LIGHT_INDICES),spot_count,spot_indices); } + + int rc = e->instance->reflection_probe_instances.size(); + + + if (rc) { + + + const RID* reflections=e->instance->reflection_probe_instances.ptr(); + + for(int i=0;i<rc;i++) { + ReflectionProbeInstance *rpi=reflection_probe_instance_owner.getptr(reflections[i]); + if (rpi->last_pass!=render_pass) //not visible + continue; + + if (reflection_count<maxobj) { + reflection_indices[reflection_count++]=rpi->reflection_index; + } + } + } + + state.scene_shader.set_uniform(SceneShaderGLES3::REFLECTION_COUNT,reflection_count); + if (reflection_count) { + glUniform1iv(state.scene_shader.get_uniform(SceneShaderGLES3::REFLECTION_INDICES),reflection_count,reflection_indices); + } + + } @@ -932,9 +1279,9 @@ void RasterizerSceneGLES3::_set_cull(bool p_front,bool p_reverse_cull) { -void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_element_count,const Transform& p_view_transform,const CameraMatrix& p_projection,RasterizerStorageGLES3::Texture* p_base_env,bool p_reverse_cull,bool p_alpha_pass,bool p_shadow,bool p_directional_add) { +void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_element_count,const Transform& p_view_transform,const CameraMatrix& p_projection,GLuint p_base_env,bool p_reverse_cull,bool p_alpha_pass,bool p_shadow,bool p_directional_add,bool p_directional_shadows) { - if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) { + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) { //p_reverse_cull=!p_reverse_cull; glFrontFace(GL_CCW); } else { @@ -951,15 +1298,15 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_e if (p_base_env) { glActiveTexture(GL_TEXTURE0+storage->config.max_texture_image_units-2); - glBindTexture(p_base_env->target,p_base_env->tex_id); - state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_CUBEMAP,true); + glBindTexture(GL_TEXTURE_2D,p_base_env); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP,true); } else { - state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_CUBEMAP,false); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP,false); } } else { - state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_CUBEMAP,false); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP,false); } @@ -1038,7 +1385,7 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_e if (p_directional_add || (directional_light && (e->sort_key&RenderList::SORT_KEY_NO_DIRECTIONAL_FLAG)==0)) { state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL,true); - if (directional_light->light_ptr->shadow) { + if (p_directional_shadows && directional_light->light_ptr->shadow) { state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW,true); switch(directional_light->light_ptr->directional_shadow_mode) { @@ -1076,7 +1423,7 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_e case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MIX: { glBlendEquation(GL_FUNC_ADD); - if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } else { @@ -1097,7 +1444,7 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_e } break; case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MUL: { glBlendEquation(GL_FUNC_ADD); - if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } else { @@ -1154,7 +1501,7 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_e glFrontFace(GL_CW); glBindVertexArray(0); - state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_CUBEMAP,false); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP,false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_FORWARD_LIGHTING,false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL,false); state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW,false); @@ -1290,9 +1637,12 @@ void RasterizerSceneGLES3::_add_geometry( RasterizerStorageGLES3::Geometry* p_g } } -void RasterizerSceneGLES3::_draw_skybox(RID p_skybox,const CameraMatrix& p_projection,const Transform& p_transform,bool p_vflip,float p_scale) { +void RasterizerSceneGLES3::_draw_skybox(RasterizerStorageGLES3::SkyBox *p_skybox,const CameraMatrix& p_projection,const Transform& p_transform,bool p_vflip,float p_scale) { + + if (!p_skybox) + return; - RasterizerStorageGLES3::Texture *tex = storage->texture_owner.getornull(p_skybox); + RasterizerStorageGLES3::Texture *tex = storage->texture_owner.getornull(p_skybox->cubemap); ERR_FAIL_COND(!tex); glActiveTexture(GL_TEXTURE0); @@ -1461,7 +1811,7 @@ void RasterizerSceneGLES3::_setup_environment(Environment *env,const CameraMatri } -void RasterizerSceneGLES3::_setup_directional_light(int p_index,const Transform& p_camera_inverse_transform) { +void RasterizerSceneGLES3::_setup_directional_light(int p_index,const Transform& p_camera_inverse_transform,bool p_use_shadows) { LightInstance *li = directional_lights[p_index]; @@ -1499,7 +1849,7 @@ void RasterizerSceneGLES3::_setup_directional_light(int p_index,const Transform& ubo_data.light_shadow_color[3]=1.0; - if (li->light_ptr->shadow) { + if (p_use_shadows && li->light_ptr->shadow) { int shadow_count=0; @@ -1568,7 +1918,6 @@ void RasterizerSceneGLES3::_setup_directional_light(int p_index,const Transform& Rect2 atlas_rect = Rect2(float(x)/directional_shadow.size,float(y)/directional_shadow.size,float(width)/directional_shadow.size,float(height)/directional_shadow.size); rectm.set_light_atlas_rect(atlas_rect); -// print_line("atlas rect: "+atlas_rect); CameraMatrix shadow_mtx = rectm * bias * li->shadow_transform[j].camera * modelview; @@ -1844,6 +2193,103 @@ void RasterizerSceneGLES3::_setup_lights(RID *p_light_cull_result,int p_light_cu } +void RasterizerSceneGLES3::_setup_reflections(RID *p_reflection_probe_cull_result,int p_reflection_probe_cull_count,const Transform& p_camera_inverse_transform,const CameraMatrix& p_camera_projection,RID p_reflection_atlas,Environment *p_env) { + + state.reflection_probe_count=0; + + for(int i=0;i<p_reflection_probe_cull_count;i++) { + + ReflectionProbeInstance *rpi=reflection_probe_instance_owner.getornull(p_reflection_probe_cull_result[i]); + ERR_CONTINUE(!rpi); + + ReflectionAtlas *reflection_atlas=reflection_atlas_owner.getornull(p_reflection_atlas); + ERR_CONTINUE(!reflection_atlas); + + ERR_CONTINUE(rpi->reflection_atlas_index<0); + + + if (state.reflection_probe_count>=state.max_ubo_reflections) + break; + + rpi->last_pass=render_pass; + + + ReflectionProbeDataUBO reflection_ubo; + + reflection_ubo.box_extents[0]=rpi->probe_ptr->extents.x; + reflection_ubo.box_extents[1]=rpi->probe_ptr->extents.y; + reflection_ubo.box_extents[2]=rpi->probe_ptr->extents.z; + reflection_ubo.box_extents[3]=0; + + + + reflection_ubo.box_ofs[0]=rpi->probe_ptr->origin_offset.x; + reflection_ubo.box_ofs[1]=rpi->probe_ptr->origin_offset.y; + reflection_ubo.box_ofs[2]=rpi->probe_ptr->origin_offset.z; + reflection_ubo.box_ofs[3]=0; + + reflection_ubo.params[0]=rpi->probe_ptr->intensity; + reflection_ubo.params[1]=0; + reflection_ubo.params[2]=rpi->probe_ptr->interior?1.0:0.0; + reflection_ubo.params[3]=rpi->probe_ptr->box_projection?1.0:0.0; + + if (rpi->probe_ptr->interior) { + Color ambient_linear = rpi->probe_ptr->interior_ambient.to_linear(); + reflection_ubo.ambient[0]=ambient_linear.r*rpi->probe_ptr->interior_ambient_energy; + reflection_ubo.ambient[1]=ambient_linear.g*rpi->probe_ptr->interior_ambient_energy; + reflection_ubo.ambient[2]=ambient_linear.b*rpi->probe_ptr->interior_ambient_energy; + reflection_ubo.ambient[3]=rpi->probe_ptr->interior_ambient_probe_contrib; + } else { + Color ambient_linear; + float contrib=0; + if (p_env) { + ambient_linear=p_env->ambient_color.to_linear(); + ambient_linear.r*=p_env->ambient_energy; + ambient_linear.g*=p_env->ambient_energy; + ambient_linear.b*=p_env->ambient_energy; + contrib=p_env->ambient_skybox_contribution; + } + + reflection_ubo.ambient[0]=ambient_linear.r; + reflection_ubo.ambient[1]=ambient_linear.g; + reflection_ubo.ambient[2]=ambient_linear.b; + reflection_ubo.ambient[3]=0; + } + + int cell_size = reflection_atlas->size / reflection_atlas->subdiv; + int x = (rpi->reflection_atlas_index % reflection_atlas->subdiv) * cell_size; + int y = (rpi->reflection_atlas_index / reflection_atlas->subdiv) * cell_size; + int width=cell_size; + int height=cell_size; + + reflection_ubo.atlas_clamp[0]=float(x)/reflection_atlas->size; + reflection_ubo.atlas_clamp[1]=float(y)/reflection_atlas->size; + reflection_ubo.atlas_clamp[2]=float(width)/reflection_atlas->size; + reflection_ubo.atlas_clamp[3]=float(height/2)/reflection_atlas->size; + + Transform proj = (p_camera_inverse_transform * rpi->transform).inverse(); + store_transform(proj,reflection_ubo.local_matrix); + + rpi->reflection_index=state.reflection_probe_count; + copymem(&state.reflection_array_tmp[rpi->reflection_index*sizeof(ReflectionProbeDataUBO)],&reflection_ubo,sizeof(ReflectionProbeDataUBO)); + state.reflection_probe_count++; + + } + + + if (state.reflection_probe_count) { + + + glBindBuffer(GL_UNIFORM_BUFFER, state.reflection_array_ubo); + glBufferSubData(GL_UNIFORM_BUFFER, 0, state.reflection_probe_count*sizeof(ReflectionProbeDataUBO), state.reflection_array_tmp); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + + glBindBufferBase(GL_UNIFORM_BUFFER,6,state.reflection_array_ubo); + } + +} + + void RasterizerSceneGLES3::_copy_screen() { glBindVertexArray(storage->resources.quadie_array); @@ -1959,7 +2405,7 @@ void RasterizerSceneGLES3::_fill_render_list(InstanceBase** p_cull_result,int p_ } -void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const CameraMatrix& p_cam_projection,bool p_cam_ortogonal,InstanceBase** p_cull_result,int p_cull_count,RID* p_light_cull_result,int p_light_cull_count,RID p_environment,RID p_shadow_atlas){ +void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const CameraMatrix& p_cam_projection,bool p_cam_ortogonal,InstanceBase** p_cull_result,int p_cull_count,RID* p_light_cull_result,int p_light_cull_count,RID* p_reflection_probe_cull_result,int p_reflection_probe_cull_count,RID p_environment,RID p_shadow_atlas,RID p_reflection_atlas,RID p_reflection_probe,int p_reflection_probe_pass){ //first of all, make a new render pass render_pass++; @@ -1969,6 +2415,7 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C Environment *env = environment_owner.getornull(p_environment); ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas); + ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_reflection_atlas); if (shadow_atlas && shadow_atlas->size) { glActiveTexture(GL_TEXTURE0+storage->config.max_texture_image_units-3); @@ -1979,9 +2426,21 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C state.ubo_data.shadow_atlas_pixel_size[1]=1.0/shadow_atlas->size; } + + if (reflection_atlas && reflection_atlas->size) { + glActiveTexture(GL_TEXTURE0+storage->config.max_texture_image_units-5); + glBindTexture(GL_TEXTURE_2D,reflection_atlas->color); + } + + if (p_reflection_probe.is_valid()) { + state.ubo_data.reflection_multiplier=0.0; + } else { + state.ubo_data.reflection_multiplier=1.0; + } _setup_environment(env,p_cam_projection,p_cam_transform); _setup_lights(p_light_cull_result,p_light_cull_count,p_cam_transform.affine_inverse(),p_cam_projection,p_shadow_atlas); + _setup_reflections(p_reflection_probe_cull_result,p_reflection_probe_cull_count,p_cam_transform.affine_inverse(),p_cam_projection,p_reflection_atlas,env); render_list.clear(); @@ -1998,24 +2457,55 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C glEnable(GL_DEPTH_TEST); glDisable(GL_SCISSOR_TEST); - RasterizerStorageGLES3::Texture* env_radiance_tex=NULL; - glViewport(0,0,storage->frame.current_rt->width,storage->frame.current_rt->height); + ReflectionProbeInstance *probe = reflection_probe_instance_owner.getornull(p_reflection_probe); + GLuint current_fbo; - if (use_mrt) { + if (probe) { - glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.fbo); - state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS,true); + ReflectionAtlas *ref_atlas = reflection_atlas_owner.getptr(probe->atlas); + ERR_FAIL_COND(!ref_atlas); - Color black(0,0,0,0); - glClearBufferfv(GL_COLOR,1,black.components); // specular - glClearBufferfv(GL_COLOR,2,black.components); // normal metal rough + int target_size=ref_atlas->size/ref_atlas->subdiv; - } else { + int cubemap_index=reflection_cubemaps.size()-1; - glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.alpha_fbo); + for(int i=reflection_cubemaps.size()-1;i>=0;i--) { + //find appropriate cubemap to render to + if (reflection_cubemaps[i].size>target_size*2) + break; + + cubemap_index=i; + } + + current_fbo=reflection_cubemaps[cubemap_index].fbo_id[p_reflection_probe_pass]; + use_mrt=false; state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS,false); + glViewport(0,0,reflection_cubemaps[cubemap_index].size,reflection_cubemaps[cubemap_index].size); + glBindFramebuffer(GL_FRAMEBUFFER,current_fbo); + } else { + + glViewport(0,0,storage->frame.current_rt->width,storage->frame.current_rt->height); + + if (use_mrt) { + + current_fbo=storage->frame.current_rt->buffers.fbo; + + glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.fbo); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS,true); + + Color black(0,0,0,0); + glClearBufferfv(GL_COLOR,1,black.components); // specular + glClearBufferfv(GL_COLOR,2,black.components); // normal metal rough + + } else { + + current_fbo = storage->frame.current_rt->buffers.alpha_fbo; + glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.alpha_fbo); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS,false); + + } } @@ -2024,6 +2514,9 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C Color clear_color(0,0,0,0); + RasterizerStorageGLES3::SkyBox *skybox=NULL; + GLuint env_radiance_tex=0; + if (!env || env->bg_mode==VS::ENV_BG_CLEAR_COLOR) { if (storage->frame.clear_request) { @@ -2039,8 +2532,10 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C storage->frame.clear_request=false; } else if (env->bg_mode==VS::ENV_BG_SKYBOX) { - if (env->skybox_radiance.is_valid()) { - env_radiance_tex = storage->texture_owner.getornull(env->skybox_radiance); + skybox = storage->skybox_owner.getornull(env->skybox); + + if (skybox) { + env_radiance_tex=skybox->radiance; } storage->frame.clear_request=false; @@ -2055,7 +2550,7 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C glBlendEquation(GL_FUNC_ADD); - if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } else { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -2065,23 +2560,25 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C render_list.sort_by_key(false); - if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } else { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } + + if (state.directional_light_count==0) { directional_light=NULL; - _render_list(render_list.elements,render_list.element_count,p_cam_transform,p_cam_projection,env_radiance_tex,false,false,false,false); + _render_list(render_list.elements,render_list.element_count,p_cam_transform,p_cam_projection,env_radiance_tex,false,false,false,false,shadow_atlas!=NULL); } else { for(int i=0;i<state.directional_light_count;i++) { directional_light=directional_lights[i]; if (i>0) { glEnable(GL_BLEND); } - _setup_directional_light(i,p_cam_transform.affine_inverse()); - _render_list(render_list.elements,render_list.element_count,p_cam_transform,p_cam_projection,env_radiance_tex,false,false,false,i>0); + _setup_directional_light(i,p_cam_transform.affine_inverse(),shadow_atlas!=NULL); + _render_list(render_list.elements,render_list.element_count,p_cam_transform,p_cam_projection,env_radiance_tex,false,false,false,i>0,shadow_atlas!=NULL); } } @@ -2095,7 +2592,7 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.alpha_fbo); //switch to alpha fbo for skybox, only diffuse/ambient matters } - _draw_skybox(env->skybox_color,p_cam_projection,p_cam_transform,storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP],env->skybox_scale); + _draw_skybox(skybox,p_cam_projection,p_cam_transform,storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP],env->skybox_scale); } @@ -2112,24 +2609,30 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); glDisable(GL_SCISSOR_TEST); - glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.alpha_fbo); - - render_list.sort_by_depth(true); + if (use_mrt) { + glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.alpha_fbo); + } + render_list.sort_by_depth(true); if (state.directional_light_count==0) { directional_light=NULL; - _render_list(&render_list.elements[render_list.max_elements-render_list.alpha_element_count],render_list.alpha_element_count,p_cam_transform,p_cam_projection,env_radiance_tex,false,true,false,false); + _render_list(&render_list.elements[render_list.max_elements-render_list.alpha_element_count],render_list.alpha_element_count,p_cam_transform,p_cam_projection,env_radiance_tex,false,true,false,false,shadow_atlas!=NULL); } else { for(int i=0;i<state.directional_light_count;i++) { directional_light=directional_lights[i]; - _setup_directional_light(i,p_cam_transform.affine_inverse()); - _render_list(&render_list.elements[render_list.max_elements-render_list.alpha_element_count],render_list.alpha_element_count,p_cam_transform,p_cam_projection,env_radiance_tex,false,true,false,i>0); + _setup_directional_light(i,p_cam_transform.affine_inverse(),shadow_atlas!=NULL); + _render_list(&render_list.elements[render_list.max_elements-render_list.alpha_element_count],render_list.alpha_element_count,p_cam_transform,p_cam_projection,env_radiance_tex,false,true,false,i>0,shadow_atlas!=NULL); } } + if (probe) { + //rendering a probe, do no more! + return; + } + _copy_to_front_buffer(env); /* if (shadow_atlas) { @@ -2143,6 +2646,17 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C } */ + + if (false && reflection_atlas && storage->frame.current_rt) { + + //_copy_texture_to_front_buffer(shadow_atlas->depth); + storage->canvas->canvas_begin(); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D,reflection_atlas->color); + storage->canvas->draw_generic_textured_rect(Rect2(0,0,storage->frame.current_rt->width/2,storage->frame.current_rt->height/2),Rect2(0,0,1,1)); + + } + if (false && directional_shadow.fbo) { //_copy_texture_to_front_buffer(shadow_atlas->depth); @@ -2154,6 +2668,16 @@ void RasterizerSceneGLES3::render_scene(const Transform& p_cam_transform,const C } + if (false && env_radiance_tex) { + + //_copy_texture_to_front_buffer(shadow_atlas->depth); + storage->canvas->canvas_begin(); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D,env_radiance_tex); + storage->canvas->draw_generic_textured_rect(Rect2(0,0,storage->frame.current_rt->width/2,storage->frame.current_rt->height/2),Rect2(0,0,1,1)); + + } + #if 0 if (use_fb) { @@ -2534,7 +3058,7 @@ void RasterizerSceneGLES3::render_shadow(RID p_light,RID p_shadow_atlas,int p_pa state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_SHADOW,true); - _render_list(render_list.elements,render_list.element_count,light_transform,light_projection,NULL,!flip_facing,false,true,false); + _render_list(render_list.elements,render_list.element_count,light_transform,light_projection,NULL,!flip_facing,false,true,false,false); state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_SHADOW,false); state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_SHADOW_DUAL_PARABOLOID,false); @@ -2624,6 +3148,19 @@ bool RasterizerSceneGLES3::free(RID p_rid) { shadow_atlas_set_size(p_rid,0); shadow_atlas_owner.free(p_rid); memdelete(shadow_atlas); + } else if (reflection_atlas_owner.owns(p_rid)) { + + ReflectionAtlas *reflection_atlas = reflection_atlas_owner.get(p_rid); + reflection_atlas_set_size(p_rid,0); + reflection_atlas_owner.free(p_rid); + memdelete(reflection_atlas); + } else if (reflection_probe_instance_owner.owns(p_rid)) { + + ReflectionProbeInstance *reflection_instance = reflection_probe_instance_owner.get(p_rid); + + reflection_probe_release_atlas_index(p_rid); + reflection_probe_instance_owner.free(p_rid); + memdelete(reflection_instance); } else { return false; @@ -2890,6 +3427,7 @@ void RasterizerSceneGLES3::initialize() { const int ubo_light_size=160; state.ubo_light_size=ubo_light_size; state.max_ubo_lights=max_ubo_size/ubo_light_size; + print_line("max ubo light: "+itos(state.max_ubo_lights)); state.spot_array_tmp = (uint8_t*)memalloc(ubo_light_size*state.max_ubo_lights); state.omni_array_tmp = (uint8_t*)memalloc(ubo_light_size*state.max_ubo_lights); @@ -2912,10 +3450,21 @@ void RasterizerSceneGLES3::initialize() { state.max_forward_lights_per_object=8; + state.scene_shader.add_custom_define("#define MAX_LIGHT_DATA_STRUCTS "+itos(state.max_ubo_lights)+"\n"); state.scene_shader.add_custom_define("#define MAX_FORWARD_LIGHTS "+itos(state.max_forward_lights_per_object)+"\n"); + state.max_ubo_reflections=max_ubo_size/sizeof(ReflectionProbeDataUBO); + print_line("max ubo reflections: "+itos(state.max_ubo_reflections)+" ubo size: "+itos(sizeof(ReflectionProbeDataUBO))); + state.reflection_array_tmp = (uint8_t*)memalloc(sizeof(ReflectionProbeDataUBO)*state.max_ubo_reflections); + + glGenBuffers(1, &state.reflection_array_ubo); + glBindBuffer(GL_UNIFORM_BUFFER, state.reflection_array_ubo); + glBufferData(GL_UNIFORM_BUFFER, sizeof(ReflectionProbeDataUBO)*state.max_ubo_reflections, NULL, GL_DYNAMIC_DRAW); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + + state.scene_shader.add_custom_define("#define MAX_REFLECTION_DATA_STRUCTS "+itos(state.max_ubo_reflections)+"\n"); } @@ -2923,6 +3472,73 @@ void RasterizerSceneGLES3::initialize() { Globals::get_singleton()->set_custom_property_info("rendering/gles3/shadow_filter_mode",PropertyInfo(Variant::INT,"rendering/gles3/shadow_filter_mode",PROPERTY_HINT_ENUM,"Disabled,PCF5,PCF13")); shadow_filter_mode=SHADOW_FILTER_NEAREST; + { //reflection cubemaps + int max_reflection_cubemap_sampler_size=512; + + int cube_size = max_reflection_cubemap_sampler_size; + + glActiveTexture(GL_TEXTURE0); + + bool use_float=true; + + GLenum internal_format = use_float?GL_RGBA16F:GL_RGB10_A2; + GLenum format = GL_RGBA; + GLenum type = use_float?GL_HALF_FLOAT:GL_UNSIGNED_INT_2_10_10_10_REV; + + while(cube_size>=32) { + + ReflectionCubeMap cube; + cube.size=cube_size; + + glGenTextures(1,&cube.depth); + glBindTexture(GL_TEXTURE_2D,cube.depth); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, cube.size, cube.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + + glGenTextures(1,&cube.cubemap); + glBindTexture(GL_TEXTURE_CUBE_MAP,cube.cubemap); + //gen cubemap first + for(int i=0;i<6;i++) { + + glTexImage2D(_cube_side_enum[i], 0, internal_format, cube.size, cube.size, 0, format, type, NULL); + } + + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + // Remove artifact on the edges of the reflectionmap + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + + //gen renderbuffers second, because it needs a complete cubemap + for(int i=0;i<6;i++) { + + glGenFramebuffers(1, &cube.fbo_id[i]); + glBindFramebuffer(GL_FRAMEBUFFER, cube.fbo_id[i]); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,_cube_side_enum[i], cube.cubemap, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, cube.depth, 0); + + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + ERR_CONTINUE(status!=GL_FRAMEBUFFER_COMPLETE); + } + + reflection_cubemaps.push_back(cube); + + cube_size>>=1; + } + } + + +#ifdef GLEW_ENABLED +//"desktop" opengl needs this. + glEnable(GL_POINT_SPRITE); + glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); + +#endif } void RasterizerSceneGLES3::iteration() { diff --git a/drivers/gles3/rasterizer_scene_gles3.h b/drivers/gles3/rasterizer_scene_gles3.h index 24b6a36fcc..81c13bdc08 100644 --- a/drivers/gles3/rasterizer_scene_gles3.h +++ b/drivers/gles3/rasterizer_scene_gles3.h @@ -62,6 +62,7 @@ public: float shadow_dual_paraboloid_render_side; float shadow_atlas_pixel_size[2]; float shadow_directional_pixel_size[2]; + float reflection_multiplier; } ubo_data; @@ -87,17 +88,23 @@ public: GLuint spot_array_ubo; GLuint omni_array_ubo; + GLuint reflection_array_ubo; uint32_t ubo_light_size; uint8_t *spot_array_tmp; uint8_t *omni_array_tmp; + uint8_t *reflection_array_tmp; int max_ubo_lights; int max_forward_lights_per_object; + int max_ubo_reflections; + + int spot_light_count; int omni_light_count; int directional_light_count; + int reflection_probe_count; bool cull_front; @@ -176,6 +183,87 @@ public: virtual int get_directional_light_shadow_size(RID p_light_intance); virtual void set_directional_shadow_count(int p_count); + /* REFLECTION PROBE ATLAS API */ + + struct ReflectionAtlas : public RID_Data { + + int subdiv; + int size; + + struct Reflection { + RID owner; + uint64_t last_frame; + }; + + GLuint fbo[6]; + GLuint color; + + Vector<Reflection> reflections; + }; + + mutable RID_Owner<ReflectionAtlas> reflection_atlas_owner; + + virtual RID reflection_atlas_create(); + virtual void reflection_atlas_set_size(RID p_ref_atlas,int p_size); + virtual void reflection_atlas_set_subdivision(RID p_ref_atlas,int p_subdiv); + + /* REFLECTION CUBEMAPS */ + + struct ReflectionCubeMap { + + GLuint fbo_id[6]; + GLuint cubemap; + GLuint depth; + int size; + }; + + Vector<ReflectionCubeMap> reflection_cubemaps; + + /* REFLECTION PROBE INSTANCE */ + + struct ReflectionProbeInstance : public RID_Data { + + RasterizerStorageGLES3::ReflectionProbe *probe_ptr; + RID probe; + RID self; + RID atlas; + + int reflection_atlas_index; + + int render_step; + + + + uint64_t last_pass; + int reflection_index; + + Transform transform; + }; + + struct ReflectionProbeDataUBO { + + float box_extents[4]; + float box_ofs[4]; + float params[4]; // intensity, 0, 0, boxproject + float ambient[4]; //color, probe contrib + float atlas_clamp[4]; + float local_matrix[16]; //up to here for spot and omni, rest is for directional + //notes: for ambientblend, use distance to edge to blend between already existing global environment + }; + + + mutable RID_Owner<ReflectionProbeInstance> reflection_probe_instance_owner; + + virtual RID reflection_probe_instance_create(RID p_probe); + virtual void reflection_probe_instance_set_transform(RID p_instance,const Transform& p_transform); + virtual void reflection_probe_release_atlas_index(RID p_instance); + virtual bool reflection_probe_instance_needs_redraw(RID p_instance); + virtual bool reflection_probe_instance_has_reflection(RID p_instance); + virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas); + virtual bool reflection_probe_instance_postprocess_step(RID p_instance); + + + /* ENVIRONMENT API */ @@ -183,8 +271,7 @@ public: VS::EnvironmentBG bg_mode; - RID skybox_color; - RID skybox_radiance; + RID skybox; float skybox_scale; Color bg_color; @@ -214,7 +301,7 @@ public: virtual RID environment_create(); virtual void environment_set_background(RID p_env,VS::EnvironmentBG p_bg); - virtual void environment_set_skybox(RID p_env,RID p_skybox,int p_radiance_size); + virtual void environment_set_skybox(RID p_env,RID p_skybox); virtual void environment_set_skybox_scale(RID p_env,float p_scale); virtual void environment_set_bg_color(RID p_env,const Color& p_color); virtual void environment_set_bg_energy(RID p_env,float p_energy); @@ -441,23 +528,25 @@ public: _FORCE_INLINE_ void _render_geometry(RenderList::Element *e); _FORCE_INLINE_ void _setup_light(RenderList::Element *e); - void _render_list(RenderList::Element **p_elements, int p_element_count, const Transform& p_view_transform, const CameraMatrix& p_projection, RasterizerStorageGLES3::Texture *p_base_env, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add); + void _render_list(RenderList::Element **p_elements, int p_element_count, const Transform& p_view_transform, const CameraMatrix& p_projection, GLuint p_base_env, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add, bool p_directional_shadows); _FORCE_INLINE_ void _add_geometry( RasterizerStorageGLES3::Geometry* p_geometry, InstanceBase *p_instance, RasterizerStorageGLES3::GeometryOwner *p_owner,int p_material,bool p_shadow); - void _draw_skybox(RID p_skybox, const CameraMatrix& p_projection, const Transform& p_transform, bool p_vflip, float p_scale); + void _draw_skybox(RasterizerStorageGLES3::SkyBox *p_skybox, const CameraMatrix& p_projection, const Transform& p_transform, bool p_vflip, float p_scale); void _setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform& p_cam_transform); - void _setup_directional_light(int p_index, const Transform &p_camera_inverse_transform); + void _setup_directional_light(int p_index, const Transform &p_camera_inverse_transformm, bool p_use_shadows); void _setup_lights(RID *p_light_cull_result, int p_light_cull_count, const Transform &p_camera_inverse_transform, const CameraMatrix& p_camera_projection, RID p_shadow_atlas); + void _setup_reflections(RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, const Transform& p_camera_inverse_transform, const CameraMatrix& p_camera_projection, RID p_reflection_atlas, Environment *p_env); + void _copy_screen(); void _copy_to_front_buffer(Environment *env); void _copy_texture_to_front_buffer(GLuint p_texture); //used for debug void _fill_render_list(InstanceBase** p_cull_result,int p_cull_count,bool p_shadow); - virtual void render_scene(const Transform& p_cam_transform,const CameraMatrix& p_cam_projection,bool p_cam_ortogonal,InstanceBase** p_cull_result,int p_cull_count,RID* p_light_cull_result,int p_light_cull_count,RID p_environment,RID p_shadow_atlas); + virtual void render_scene(const Transform& p_cam_transform,const CameraMatrix& p_cam_projection,bool p_cam_ortogonal,InstanceBase** p_cull_result,int p_cull_count,RID* p_light_cull_result,int p_light_cull_count,RID* p_reflection_probe_cull_result,int p_reflection_probe_cull_count,RID p_environment,RID p_shadow_atlas,RID p_reflection_atlas,RID p_reflection_probe,int p_reflection_probe_pass); virtual void render_shadow(RID p_light,RID p_shadow_atlas,int p_pass,InstanceBase** p_cull_result,int p_cull_count); virtual bool free(RID p_rid); diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp index 6e278e563b..8188fe3adb 100644 --- a/drivers/gles3/rasterizer_storage_gles3.cpp +++ b/drivers/gles3/rasterizer_storage_gles3.cpp @@ -1015,10 +1015,6 @@ RID RasterizerStorageGLES3::texture_create_radiance_cubemap(RID p_source,int p_r glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glDisable(GL_SCISSOR_TEST); -#ifdef GLEW_ENABLED - glDisable(GL_POINT_SPRITE); - glDisable(GL_VERTEX_PROGRAM_POINT_SIZE); -#endif glDisable(GL_BLEND); @@ -1086,6 +1082,8 @@ RID RasterizerStorageGLES3::texture_create_radiance_cubemap(RID p_source,int p_r size = p_resolution; + shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID,false); + while(mm_level) { for(int i=0;i<6;i++) { @@ -1156,6 +1154,147 @@ RID RasterizerStorageGLES3::texture_create_radiance_cubemap(RID p_source,int p_r } +RID RasterizerStorageGLES3::skybox_create() { + + SkyBox *skybox = memnew( SkyBox ); + skybox->radiance=0; + return skybox_owner.make_rid(skybox); +} + +void RasterizerStorageGLES3::skybox_set_texture(RID p_skybox, RID p_cube_map, int p_radiance_size){ + + SkyBox *skybox = skybox_owner.getornull(p_skybox); + ERR_FAIL_COND(!skybox); + + if (skybox->cubemap.is_valid()) { + skybox->cubemap=RID(); + glDeleteTextures(1,&skybox->radiance); + skybox->radiance=0; + } + + skybox->cubemap=p_cube_map; + if (!skybox->cubemap.is_valid()) + return; //cleared + + Texture *texture = texture_owner.getornull(skybox->cubemap); + if (!texture || !(texture->flags&VS::TEXTURE_FLAG_CUBEMAP)) { + skybox->cubemap=RID(); + ERR_FAIL_COND(!texture || !(texture->flags&VS::TEXTURE_FLAG_CUBEMAP)); + } + + glBindVertexArray(0); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glDisable(GL_SCISSOR_TEST); + glDisable(GL_BLEND); + + + glActiveTexture(GL_TEXTURE0); + glBindTexture(texture->target, texture->tex_id); + + if (config.srgb_decode_supported && texture->srgb && !texture->using_srgb) { + + glTexParameteri(texture->target,_TEXTURE_SRGB_DECODE_EXT,_DECODE_EXT); + texture->using_srgb=true; +#ifdef TOOLS_ENABLED + if (!(texture->flags&VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) { + texture->flags|=VS::TEXTURE_FLAG_CONVERT_TO_LINEAR; + //notify that texture must be set to linear beforehand, so it works in other platforms when exported + } +#endif + } + + + glActiveTexture(GL_TEXTURE1); + glGenTextures(1, &skybox->radiance); + glBindTexture(GL_TEXTURE_2D, skybox->radiance); + + GLuint tmp_fb; + + glGenFramebuffers(1, &tmp_fb); + glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb); + + + int size = p_radiance_size; + + int lod=0; + + + int mipmaps=6; + + int mm_level=mipmaps; + + bool use_float=true; + + GLenum internal_format = use_float?GL_RGBA16F:GL_RGB10_A2; + GLenum format = GL_RGBA; + GLenum type = use_float?GL_HALF_FLOAT:GL_UNSIGNED_INT_2_10_10_10_REV; + + while(mm_level) { + + glTexImage2D(GL_TEXTURE_2D, lod, internal_format, size, size*2, 0, format, type, NULL); + lod++; + mm_level--; + + if (size>1) + size>>=1; + } + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, lod-1); + + lod=0; + mm_level=mipmaps; + + size = p_radiance_size; + + shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID,true); + shaders.cubemap_filter.bind(); + + while(mm_level) { + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, skybox->radiance, lod); +#ifdef DEBUG_ENABLED + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + ERR_CONTINUE(status!=GL_FRAMEBUFFER_COMPLETE); +#endif + + for(int i=0;i<2;i++) { + glViewport(0,i*size,size,size); + glBindVertexArray(resources.quadie_array); + + shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::Z_FLIP,i>0); + shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::ROUGHNESS,lod/float(mipmaps-1)); + + + glDrawArrays(GL_TRIANGLE_FAN,0,4); + glBindVertexArray(0); + } + + if (size>1) + size>>=1; + lod++; + mm_level--; + + } + shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID,false); + + + //restore ranges + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, lod-1); + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glBindFramebuffer(GL_FRAMEBUFFER, config.system_fbo); + glDeleteFramebuffers(1, &tmp_fb); + +} + + /* SHADER API */ @@ -2144,7 +2283,7 @@ void RasterizerStorageGLES3::_update_material(Material* material) { Map<StringName,Variant>::Element *V = material->params.find(E->key()); if (V) { - //user provided + //user provided _fill_std140_variant_ubo_value(E->get().type,V->get(),data,material->shader->mode==VS::SHADER_SPATIAL); } else if (E->get().default_value.size()){ //default value @@ -3405,46 +3544,197 @@ AABB RasterizerStorageGLES3::light_get_aabb(RID p_light) const { RID RasterizerStorageGLES3::reflection_probe_create(){ - return RID(); + ReflectionProbe *reflection_probe = memnew( ReflectionProbe ); + + reflection_probe->intensity=1.0; + reflection_probe->interior_ambient=Color(); + reflection_probe->interior_ambient_energy=1.0; + reflection_probe->max_distance=0; + reflection_probe->extents=Vector3(1,1,1); + reflection_probe->origin_offset=Vector3(0,0,0); + reflection_probe->interior=false; + reflection_probe->box_projection=false; + reflection_probe->enable_shadows=false; + reflection_probe->cull_mask=(1<<20)-1; + reflection_probe->update_mode=VS::REFLECTION_PROBE_UPDATE_ONCE; + + return reflection_probe_owner.make_rid(reflection_probe); } -void RasterizerStorageGLES3::reflection_probe_set_intensity(RID p_probe, float p_intensity){ +void RasterizerStorageGLES3::reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode) { + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->update_mode=p_mode; + reflection_probe->instance_change_notify(); } -void RasterizerStorageGLES3::reflection_probe_set_clip(RID p_probe, float p_near, float p_far){ +void RasterizerStorageGLES3::reflection_probe_set_intensity(RID p_probe, float p_intensity) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->intensity=p_intensity; + +} + +void RasterizerStorageGLES3::reflection_probe_set_interior_ambient(RID p_probe, const Color& p_ambient) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->interior_ambient=p_ambient; + +} + +void RasterizerStorageGLES3::reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->interior_ambient_energy=p_energy; } -void RasterizerStorageGLES3::reflection_probe_set_min_blend_distance(RID p_probe, float p_distance){ +void RasterizerStorageGLES3::reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->interior_ambient_probe_contrib=p_contrib; + +} + + +void RasterizerStorageGLES3::reflection_probe_set_max_distance(RID p_probe, float p_distance){ + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->max_distance=p_distance; + reflection_probe->instance_change_notify(); } void RasterizerStorageGLES3::reflection_probe_set_extents(RID p_probe, const Vector3& p_extents){ + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->extents=p_extents; + reflection_probe->instance_change_notify(); } void RasterizerStorageGLES3::reflection_probe_set_origin_offset(RID p_probe, const Vector3& p_offset){ + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->origin_offset=p_offset; + reflection_probe->instance_change_notify(); } -void RasterizerStorageGLES3::reflection_probe_set_enable_parallax_correction(RID p_probe, bool p_enable){ +void RasterizerStorageGLES3::reflection_probe_set_as_interior(RID p_probe, bool p_enable){ + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->interior=p_enable; } -void RasterizerStorageGLES3::reflection_probe_set_resolution(RID p_probe, int p_resolution){ +void RasterizerStorageGLES3::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable){ + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + reflection_probe->box_projection=p_enable; } -void RasterizerStorageGLES3::reflection_probe_set_hide_skybox(RID p_probe, bool p_hide){ +void RasterizerStorageGLES3::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable){ + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->enable_shadows=p_enable; + reflection_probe->instance_change_notify(); } void RasterizerStorageGLES3::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers){ + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->cull_mask=p_layers; + reflection_probe->instance_change_notify(); } +AABB RasterizerStorageGLES3::reflection_probe_get_aabb(RID p_probe) const { + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe,AABB()); + + AABB aabb; + aabb.pos=-reflection_probe->extents; + aabb.size=reflection_probe->extents*2.0; + + return aabb; + + +} +VS::ReflectionProbeUpdateMode RasterizerStorageGLES3::reflection_probe_get_update_mode(RID p_probe) const{ + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe,VS::REFLECTION_PROBE_UPDATE_ALWAYS); + + return reflection_probe->update_mode; +} + +uint32_t RasterizerStorageGLES3::reflection_probe_get_cull_mask(RID p_probe) const { + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe,0); + + return reflection_probe->cull_mask; + +} + +Vector3 RasterizerStorageGLES3::reflection_probe_get_extents(RID p_probe) const { + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe,Vector3()); + + return reflection_probe->extents; + +} +Vector3 RasterizerStorageGLES3::reflection_probe_get_origin_offset(RID p_probe) const{ + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe,Vector3()); + + return reflection_probe->origin_offset; + +} + +bool RasterizerStorageGLES3::reflection_probe_renders_shadows(RID p_probe) const { + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe,false); + + return reflection_probe->enable_shadows; + +} + +float RasterizerStorageGLES3::reflection_probe_get_origin_max_distance(RID p_probe) const{ + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe,0); + + return reflection_probe->max_distance; + +} /* ROOM API */ @@ -3495,6 +3785,10 @@ void RasterizerStorageGLES3::instance_add_dependency(RID p_base,RasterizerScene: inst = mesh_owner.getornull(p_base); ERR_FAIL_COND(!inst); } break; + case VS::INSTANCE_REFLECTION_PROBE: { + inst = reflection_probe_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; case VS::INSTANCE_LIGHT: { inst = light_owner.getornull(p_base); ERR_FAIL_COND(!inst); @@ -3517,6 +3811,10 @@ void RasterizerStorageGLES3::instance_remove_dependency(RID p_base,RasterizerSce ERR_FAIL_COND(!inst); } break; + case VS::INSTANCE_REFLECTION_PROBE: { + inst = reflection_probe_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; case VS::INSTANCE_LIGHT: { inst = light_owner.getornull(p_base); ERR_FAIL_COND(!inst); @@ -4005,6 +4303,9 @@ VS::InstanceType RasterizerStorageGLES3::get_base_type(RID p_rid) const { if (light_owner.owns(p_rid)) { return VS::INSTANCE_LIGHT; } + if (reflection_probe_owner.owns(p_rid)) { + return VS::INSTANCE_REFLECTION_PROBE; + } return VS::INSTANCE_NONE; } @@ -4028,6 +4329,12 @@ bool RasterizerStorageGLES3::free(RID p_rid){ info.texture_mem-=texture->total_data_size; texture_owner.free(p_rid); memdelete(texture); + } else if (skybox_owner.owns(p_rid)) { + // delete the skybox + SkyBox *skybox = skybox_owner.get(p_rid); + skybox_set_texture(p_rid,RID(),256); + skybox_owner.free(p_rid); + memdelete(skybox); } else if (shader_owner.owns(p_rid)) { @@ -4259,6 +4566,8 @@ void RasterizerStorageGLES3::initialize() { shaders.cubemap_filter.init(); glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS); + + frame.count=0; } void RasterizerStorageGLES3::finalize() { diff --git a/drivers/gles3/rasterizer_storage_gles3.h b/drivers/gles3/rasterizer_storage_gles3.h index 0e3d47b61f..5f07efeb0a 100644 --- a/drivers/gles3/rasterizer_storage_gles3.h +++ b/drivers/gles3/rasterizer_storage_gles3.h @@ -226,6 +226,19 @@ public: virtual RID texture_create_radiance_cubemap(RID p_source,int p_resolution=-1) const; + /* SKYBOX API */ + + struct SkyBox : public RID_Data { + + RID cubemap; + GLuint radiance; + int radiance_size; + }; + + mutable RID_Owner<SkyBox> skybox_owner; + + virtual RID skybox_create(); + virtual void skybox_set_texture(RID p_skybox,RID p_cube_map,int p_radiance_size); /* SHADER API */ @@ -679,18 +692,51 @@ public: /* PROBE API */ + struct ReflectionProbe : Instantiable { + + VS::ReflectionProbeUpdateMode update_mode; + float intensity; + Color interior_ambient; + float interior_ambient_energy; + float interior_ambient_probe_contrib; + float max_distance; + Vector3 extents; + Vector3 origin_offset; + bool interior; + bool box_projection; + bool enable_shadows; + uint32_t cull_mask; + + }; + + mutable RID_Owner<ReflectionProbe> reflection_probe_owner; + virtual RID reflection_probe_create(); + virtual void reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode); virtual void reflection_probe_set_intensity(RID p_probe, float p_intensity); - virtual void reflection_probe_set_clip(RID p_probe, float p_near, float p_far); - virtual void reflection_probe_set_min_blend_distance(RID p_probe, float p_distance); + virtual void reflection_probe_set_interior_ambient(RID p_probe, const Color& p_ambient); + virtual void reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy); + virtual void reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib); + virtual void reflection_probe_set_max_distance(RID p_probe, float p_distance); virtual void reflection_probe_set_extents(RID p_probe, const Vector3& p_extents); virtual void reflection_probe_set_origin_offset(RID p_probe, const Vector3& p_offset); - virtual void reflection_probe_set_enable_parallax_correction(RID p_probe, bool p_enable); - virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution); - virtual void reflection_probe_set_hide_skybox(RID p_probe, bool p_hide); + virtual void reflection_probe_set_as_interior(RID p_probe, bool p_enable); + virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable); + virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable); virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers); + virtual AABB reflection_probe_get_aabb(RID p_probe) const; + virtual VS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const; + virtual uint32_t reflection_probe_get_cull_mask(RID p_probe) const; + + virtual Vector3 reflection_probe_get_extents(RID p_probe) const; + virtual Vector3 reflection_probe_get_origin_offset(RID p_probe) const; + virtual float reflection_probe_get_origin_max_distance(RID p_probe) const; + virtual bool reflection_probe_renders_shadows(RID p_probe) const; + + + /* ROOM API */ @@ -813,6 +859,7 @@ public: Color clear_request_color; int canvas_draw_commands; float time[4]; + uint64_t count; } frame; void initialize(); diff --git a/drivers/gles3/shaders/cubemap_filter.glsl b/drivers/gles3/shaders/cubemap_filter.glsl index 998a59833e..768d20ad22 100644 --- a/drivers/gles3/shaders/cubemap_filter.glsl +++ b/drivers/gles3/shaders/cubemap_filter.glsl @@ -151,14 +151,47 @@ vec2 Hammersley(uint i, uint N) { return vec2(float(i)/float(N), radicalInverse_VdC(i)); } -#define SAMPLE_COUNT 1024u + + +#ifdef LOW_QUALITY + +#define SAMPLE_COUNT 64u + +#else + +#define SAMPLE_COUNT 512u + +#endif + +uniform bool z_flip; void main() { +#ifdef USE_DUAL_PARABOLOID + + vec3 N = vec3( uv_interp * 2.0 - 1.0, 0.0 ); + N.z = 0.5 - 0.5*((N.x * N.x) + (N.y * N.y)); + N = normalize(N); + + if (!z_flip) { + N.y=-N.y; //y is flipped to improve blending between both sides + } else { + N.z=-N.z; + } + + +#else vec2 uv = (uv_interp * 2.0) - 1.0; vec3 N = texelCoordToVec(uv, face_id); - +#endif //vec4 color = color_interp; + +#ifdef USE_DIRECT_WRITE + + frag_color=vec4(texture(N,source_cube).rgb,1.0); + +#else + vec4 sum = vec4(0.0, 0.0, 0.0, 0.0); for(uint sampleNum = 0u; sampleNum < SAMPLE_COUNT; sampleNum++) { @@ -178,5 +211,8 @@ void main() { sum /= sum.a; frag_color = vec4(sum.rgb, 1.0); + +#endif + } diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index ceb5a72118..a923c130dc 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -64,6 +64,8 @@ layout(std140) uniform SceneData { //ubo:0 vec2 shadow_atlas_pixel_size; vec2 directional_shadow_pixel_size; + float reflection_multiplier; + }; uniform highp mat4 world_transform; @@ -288,11 +290,11 @@ in vec3 normal_interp; //used on forward mainly uniform bool no_ambient_light; +uniform sampler2D brdf_texture; //texunit:-1 -#ifdef USE_RADIANCE_CUBEMAP +#ifdef USE_RADIANCE_MAP -uniform sampler2D brdf_texture; //texunit:-1 -uniform samplerCube radiance_cube; //texunit:-2 +uniform sampler2D radiance_map; //texunit:-2 layout(std140) uniform Radiance { //ubo:2 @@ -343,6 +345,8 @@ layout(std140) uniform SceneData { vec2 shadow_atlas_pixel_size; vec2 directional_shadow_pixel_size; + float reflection_multiplier; + }; //directional light data @@ -398,6 +402,24 @@ layout(std140) uniform SpotLightData { //ubo:5 uniform highp sampler2DShadow shadow_atlas; //texunit:-3 +struct ReflectionData { + + mediump vec4 box_extents; + mediump vec4 box_offset; + mediump vec4 params; // intensity, 0, interior , boxproject + mediump vec4 ambient; //ambient color, energy + mediump vec4 atlas_clamp; + highp mat4 local_matrix; //up to here for spot and omni, rest is for directional + //notes: for ambientblend, use distance to edge to blend between already existing global environment +}; + +layout(std140) uniform ReflectionProbeData { //ubo:6 + + ReflectionData reflections[MAX_REFLECTION_DATA_STRUCTS]; +}; +uniform mediump sampler2D reflection_atlas; //texunit:-5 + + #ifdef USE_FORWARD_LIGHTING uniform int omni_light_indices[MAX_FORWARD_LIGHTS]; @@ -406,6 +428,9 @@ uniform int omni_light_count; uniform int spot_light_indices[MAX_FORWARD_LIGHTS]; uniform int spot_light_count; +uniform int reflection_indices[MAX_FORWARD_LIGHTS]; +uniform int reflection_count; + #endif @@ -578,6 +603,120 @@ void light_process_spot(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 al } +void reflection_process(int idx, vec3 vertex, vec3 normal,float roughness,vec3 ambient,vec3 skybox,vec2 brdf, inout highp vec4 reflection_accum,inout highp vec4 ambient_accum) { + + vec3 ref_vec = normalize(reflect(vertex,normal)); + vec3 local_pos = (reflections[idx].local_matrix * vec4(vertex,1.0)).xyz; + vec3 box_extents = reflections[idx].box_extents.xyz; + + if (any(greaterThan(abs(local_pos),box_extents))) { //out of the reflection box + return; + } + + vec3 inner_pos = abs(local_pos / box_extents); + float blend = max(inner_pos.x,max(inner_pos.y,inner_pos.z)); + //make blend more rounded + blend=mix(length(inner_pos),blend,blend); + blend*=blend; + blend=1.001-blend; + + if (reflections[idx].params.x>0.0){// compute reflection + + vec3 local_ref_vec = (reflections[idx].local_matrix * vec4(ref_vec,0.0)).xyz; + + if (reflections[idx].params.w > 0.5) { //box project + + vec3 nrdir = normalize(local_ref_vec); + vec3 rbmax = (box_extents - local_pos)/nrdir; + vec3 rbmin = (-box_extents - local_pos)/nrdir; + + + vec3 rbminmax = mix(rbmin,rbmax,greaterThan(nrdir,vec3(0.0,0.0,0.0))); + + float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z); + vec3 posonbox = local_pos + nrdir * fa; + local_ref_vec = posonbox - reflections[idx].box_offset.xyz; + } + + + + vec3 splane=normalize(local_ref_vec); + vec4 clamp_rect=reflections[idx].atlas_clamp; + + splane.z*=-1.0; + if (splane.z>=0.0) { + splane.z+=1.0; + clamp_rect.y+=clamp_rect.w; + } else { + splane.z=1.0 - splane.z; + splane.y=-splane.y; + } + + splane.xy/=splane.z; + splane.xy=splane.xy * 0.5 + 0.5; + + splane.xy = splane.xy * clamp_rect.zw + clamp_rect.xy; + splane.xy = clamp(splane.xy,clamp_rect.xy,clamp_rect.xy+clamp_rect.zw); + + highp vec4 reflection; + reflection.rgb = textureLod(reflection_atlas,splane.xy,roughness*5.0).rgb * ( brdf.x + brdf.y); + if (reflections[idx].params.z < 0.5) { + reflection.rgb = mix(skybox,reflection.rgb,blend); + } + reflection.rgb*=reflections[idx].params.x; + reflection.a = blend; + reflection.rgb*=reflection.a; + + reflection_accum+=reflection; + } + + if (reflections[idx].ambient.a>0.0) { //compute ambient using skybox + + + vec3 local_amb_vec = (reflections[idx].local_matrix * vec4(normal,0.0)).xyz; + + vec3 splane=normalize(local_amb_vec); + vec4 clamp_rect=reflections[idx].atlas_clamp; + + splane.z*=-1.0; + if (splane.z>=0.0) { + splane.z+=1.0; + clamp_rect.y+=clamp_rect.w; + } else { + splane.z=1.0 - splane.z; + splane.y=-splane.y; + } + + splane.xy/=splane.z; + splane.xy=splane.xy * 0.5 + 0.5; + + splane.xy = splane.xy * clamp_rect.zw + clamp_rect.xy; + splane.xy = clamp(splane.xy,clamp_rect.xy,clamp_rect.xy+clamp_rect.zw); + + highp vec4 ambient_out; + ambient_out.a=blend; + ambient_out.rgb = textureLod(reflection_atlas,splane.xy,5.0).rgb; + ambient_out.rgb=mix(reflections[idx].ambient.rgb,ambient_out.rgb,reflections[idx].ambient.a); + if (reflections[idx].params.z < 0.5) { + ambient_out.rgb = mix(ambient,ambient_out.rgb,blend); + } + + ambient_out.rgb *= ambient_out.a; + ambient_accum+=ambient_out; + } else { + + highp vec4 ambient_out; + ambient_out.a=blend; + ambient_out.rgb=reflections[idx].ambient.rgb; + if (reflections[idx].params.z < 0.5) { + ambient_out.rgb = mix(ambient,ambient_out.rgb,blend); + } + ambient_out.rgb *= ambient_out.a; + ambient_accum+=ambient_out; + + } +} + void main() { #ifdef RENDER_SHADOW_DUAL_PARABOLOID @@ -666,31 +805,56 @@ FRAGMENT_SHADER_CODE vec3 eye_vec = -normalize( vertex_interp ); -#ifdef USE_RADIANCE_CUBEMAP +#ifndef RENDER_SHADOW + float ndotv = clamp(dot(normal,eye_vec),0.0,1.0); + vec2 brdf = texture(brdf_texture, vec2(roughness, ndotv)).xy; +#endif + +#ifdef USE_RADIANCE_MAP if (no_ambient_light) { ambient_light=vec3(0.0,0.0,0.0); } else { { - float ndotv = clamp(dot(normal,eye_vec),0.0,1.0); - vec2 brdf = texture(brdf_texture, vec2(roughness, ndotv)).xy; + float lod = roughness * 5.0; - vec3 r = reflect(-eye_vec,normal); //2.0 * ndotv * normal - view; // reflect(v, n); - r=normalize((radiance_inverse_xform * vec4(r,0.0)).xyz); - vec3 radiance = textureLod(radiance_cube, r, lod).xyz * ( brdf.x + brdf.y); - specular_light=mix(albedo,radiance,specular); + { //read radiance from dual paraboloid + + vec3 ref_vec = reflect(-eye_vec,normal); //2.0 * ndotv * normal - view; // reflect(v, n); + ref_vec=normalize((radiance_inverse_xform * vec4(ref_vec,0.0)).xyz); + + vec3 norm = normalize(ref_vec); + float y_ofs=0.0; + if (norm.z>=0.0) { + + norm.z+=1.0; + y_ofs+=0.5; + } else { + norm.z=1.0 - norm.z; + norm.y=-norm.y; + } + + norm.xy/=norm.z; + norm.xy=norm.xy * vec2(0.5,0.25) + vec2(0.5,0.25+y_ofs); + vec3 radiance = textureLod(radiance_map, norm.xy, lod).xyz * ( brdf.x + brdf.y); + specular_light=mix(albedo,radiance,specular); + + } + //no longer a cubemap + //vec3 radiance = textureLod(radiance_cube, r, lod).xyz * ( brdf.x + brdf.y); } { - vec3 ambient_dir=normalize((radiance_inverse_xform * vec4(normal,0.0)).xyz); + /*vec3 ambient_dir=normalize((radiance_inverse_xform * vec4(normal,0.0)).xyz); vec3 env_ambient=textureLod(radiance_cube, ambient_dir, 5.0).xyz; - ambient_light=mix(ambient_light_color.rgb,env_ambient,radiance_ambient_contribution); + ambient_light=mix(ambient_light_color.rgb,env_ambient,radiance_ambient_contribution);*/ + ambient_light=vec3(0.0,0.0,0.0); } } @@ -840,6 +1004,21 @@ FRAGMENT_SHADER_CODE #ifdef USE_FORWARD_LIGHTING + highp vec4 reflection_accum = vec4(0.0,0.0,0.0,0.0); + highp vec4 ambient_accum = vec4(0.0,0.0,0.0,0.0); + + for(int i=0;i<reflection_count;i++) { + reflection_process(reflection_indices[i],vertex,normal,roughness,ambient_light,specular_light,brdf,reflection_accum,ambient_accum); + } + + if (reflection_accum.a>0.0) { + specular_light=reflection_accum.rgb/reflection_accum.a; + specular_light*=specular; + } + if (ambient_accum.a>0.0) { + ambient_light=ambient_accum.rgb/ambient_accum.a; + } + for(int i=0;i<omni_light_count;i++) { light_process_omni(omni_light_indices[i],vertex,eye_vec,normal,albedo,specular,roughness,diffuse_light,specular_light); } @@ -848,6 +1027,8 @@ FRAGMENT_SHADER_CODE light_process_spot(spot_light_indices[i],vertex,eye_vec,normal,albedo,specular,roughness,diffuse_light,specular_light); } + + #endif @@ -866,6 +1047,7 @@ LIGHT_SHADER_CODE //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) #else + specular_light*=reflection_multiplier; #ifdef USE_MULTIPLE_RENDER_TARGETS |