From 5567e898d1052c1e2c2d32d3c37dfd957f4dc4bd Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Mon, 29 May 2017 22:11:33 -0300 Subject: Several fixes related to PBR and Environment --- drivers/gles3/rasterizer_scene_gles3.cpp | 9 +++-- drivers/gles3/rasterizer_scene_gles3.h | 4 +-- drivers/gles3/rasterizer_storage_gles3.cpp | 4 ++- drivers/gles3/shaders/copy.glsl | 9 ++++- drivers/gles3/shaders/scene.glsl | 53 ++++++++++++++++-------------- drivers/png/image_loader_png.cpp | 2 +- drivers/png/image_loader_png.h | 2 +- 7 files changed, 49 insertions(+), 34 deletions(-) (limited to 'drivers') diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp index 5214de6d06..7138363796 100644 --- a/drivers/gles3/rasterizer_scene_gles3.cpp +++ b/drivers/gles3/rasterizer_scene_gles3.cpp @@ -2120,7 +2120,7 @@ void RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry *p_geo } } -void RasterizerSceneGLES3::_draw_sky(RasterizerStorageGLES3::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_scale) { +void RasterizerSceneGLES3::_draw_sky(RasterizerStorageGLES3::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_scale, float p_energy) { if (!p_sky) return; @@ -2188,13 +2188,16 @@ void RasterizerSceneGLES3::_draw_sky(RasterizerStorageGLES3::Sky *p_sky, const C glBindVertexArray(state.sky_array); storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_PANORAMA, true); + storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_MULTIPLIER, true); storage->shaders.copy.bind(); + storage->shaders.copy.set_uniform(CopyShaderGLES3::MULTIPLIER, p_energy); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glBindVertexArray(0); glColorMask(1, 1, 1, 1); + storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_MULTIPLIER, false); storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_PANORAMA, false); } @@ -3885,7 +3888,7 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.fbo); //switch to alpha fbo for sky, only diffuse/ambient matters */ - _draw_sky(sky, p_cam_projection, p_cam_transform, storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP], env->sky_scale); + _draw_sky(sky, p_cam_projection, p_cam_transform, storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP], env->sky_scale, env->bg_energy); } //_render_list_forward(&alpha_render_list,camera_transform,camera_transform_inverse,camera_projection,false,fragment_lighting,true); @@ -4832,7 +4835,7 @@ void RasterizerSceneGLES3::initialize() { glGenTextures(1, &e.color); glBindTexture(GL_TEXTURE_2D, e.color); #ifdef IPHONE_ENABLED - ///@TODO ugly hack to get around iOS not supporting 32bit single channel floating point textures... + ///@TODO ugly hack to get around iOS not supporting 32bit single channel floating point textures... glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, max_exposure_shrink_size, max_exposure_shrink_size, 0, GL_RED, GL_FLOAT, NULL); #else glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, max_exposure_shrink_size, max_exposure_shrink_size, 0, GL_RED, GL_FLOAT, NULL); diff --git a/drivers/gles3/rasterizer_scene_gles3.h b/drivers/gles3/rasterizer_scene_gles3.h index 69a7e40604..322343bc89 100644 --- a/drivers/gles3/rasterizer_scene_gles3.h +++ b/drivers/gles3/rasterizer_scene_gles3.h @@ -131,8 +131,6 @@ public: struct EnvironmentRadianceUBO { float transform[16]; - float box_min[4]; //unused for now - float box_max[4]; float ambient_contribution; } env_radiance_data; @@ -700,7 +698,7 @@ public: _FORCE_INLINE_ void _add_geometry(RasterizerStorageGLES3::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES3::GeometryOwner *p_owner, int p_material, bool p_shadow); - void _draw_sky(RasterizerStorageGLES3::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_scale); + void _draw_sky(RasterizerStorageGLES3::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_scale, float p_energy); 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_transformm, bool p_use_shadows); diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp index 3f28ae25f2..8baa1c6043 100644 --- a/drivers/gles3/rasterizer_storage_gles3.cpp +++ b/drivers/gles3/rasterizer_storage_gles3.cpp @@ -5553,7 +5553,7 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt) { Image::Format image_format; bool hdr = rt->flags[RENDER_TARGET_HDR] && config.hdr_supported; - hdr = false; + //hdr = false; if (!hdr || rt->flags[RENDER_TARGET_NO_3D]) { @@ -6403,6 +6403,8 @@ void RasterizerStorageGLES3::initialize() { config.etc2_supported = true; config.hdr_supported = false; #endif + + print_line("hdr supported: " + itos(config.hdr_supported)); config.pvrtc_supported = config.extensions.has("GL_IMG_texture_compression_pvrtc"); config.srgb_decode_supported = config.extensions.has("GL_EXT_texture_sRGB_decode"); diff --git a/drivers/gles3/shaders/copy.glsl b/drivers/gles3/shaders/copy.glsl index f3c72a4e6f..c8985e6902 100644 --- a/drivers/gles3/shaders/copy.glsl +++ b/drivers/gles3/shaders/copy.glsl @@ -45,6 +45,11 @@ uniform samplerCube source_cube; //texunit:0 uniform sampler2D source; //texunit:0 #endif + +#ifdef USE_MULTIPLIER +uniform float multiplier; +#endif + #ifdef USE_PANORAMA vec4 texturePanorama(vec3 normal,sampler2D pano ) { @@ -130,7 +135,9 @@ void main() { color+=texture( source, uv_interp+vec2( 0.0,-2.0)*pixel_size )*0.06136; #endif - +#ifdef USE_MULTIPLIER + color.rgb*=multiplier; +#endif frag_color = color; } diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index 43a391631f..7750fd6293 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -380,8 +380,6 @@ uniform sampler2D radiance_map; //texunit:-2 layout(std140) uniform Radiance { //ubo:2 mat4 radiance_inverse_xform; - vec3 radiance_box_min; - vec3 radiance_box_max; float radiance_ambient_contribution; }; @@ -1133,6 +1131,23 @@ void gi_probes_compute(vec3 pos, vec3 normal, float roughness, vec3 specular, in #endif +vec3 textureDualParabolod(sampler2D p_tex, vec3 p_vec,float p_lod) { + + vec3 norm = normalize(p_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); + return textureLod(p_tex, norm.xy, p_lod).xyz; +} void main() { @@ -1264,6 +1279,8 @@ FRAGMENT_SHADER_CODE float ndotv = clamp(dot(normal,eye_vec),0.0,1.0); vec2 brdf = texture(brdf_texture, vec2(roughness, ndotv)).xy; + brdf.x=1.0; + brdf.y=0.0; #endif #ifdef USE_RADIANCE_MAP @@ -1274,28 +1291,15 @@ FRAGMENT_SHADER_CODE { - - float lod = roughness * 5.0; +#define RADIANCE_MAX_LOD 5.0 + float lod = roughness * RADIANCE_MAX_LOD; { //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); - specular_light = textureLod(radiance_map, norm.xy, lod).xyz * brdf.x + brdf.y; + ref_vec=normalize((radiance_inverse_xform * vec4(ref_vec,0.0)).xyz); + vec3 radiance = textureDualParabolod(radiance_map,ref_vec,lod) * bg_energy; + specular_light = radiance * brdf.x + brdf.y; } //no longer a cubemap @@ -1305,11 +1309,11 @@ FRAGMENT_SHADER_CODE { - /*vec3 ambient_dir=normalize((radiance_inverse_xform * vec4(normal,0.0)).xyz); - vec3 env_ambient=textureLod(radiance_cube, ambient_dir, 5.0).xyz; + vec3 ambient_dir=normalize((radiance_inverse_xform * vec4(normal,0.0)).xyz); + vec3 env_ambient=textureDualParabolod(radiance_map,ambient_dir,RADIANCE_MAX_LOD) * bg_energy; - ambient_light=mix(ambient_light_color.rgb,env_ambient,radiance_ambient_contribution);*/ - ambient_light=vec3(0.0,0.0,0.0); + ambient_light=mix(ambient_light_color.rgb,env_ambient,radiance_ambient_contribution); + //ambient_light=vec3(0.0,0.0,0.0); } } @@ -1322,6 +1326,7 @@ FRAGMENT_SHADER_CODE } #endif + ambient_light*=ambient_energy; #ifdef USE_LIGHT_DIRECTIONAL diff --git a/drivers/png/image_loader_png.cpp b/drivers/png/image_loader_png.cpp index fe2372e3af..25ab767bed 100644 --- a/drivers/png/image_loader_png.cpp +++ b/drivers/png/image_loader_png.cpp @@ -201,7 +201,7 @@ Error ImageLoaderPNG::_load_image(void *rf_up, png_rw_ptr p_func, Ref p_i return OK; } -Error ImageLoaderPNG::load_image(Ref p_image, FileAccess *f) { +Error ImageLoaderPNG::load_image(Ref p_image, FileAccess *f, bool p_force_linear) { Error err = _load_image(f, _read_png_data, p_image); f->close(); diff --git a/drivers/png/image_loader_png.h b/drivers/png/image_loader_png.h index f94b8bce6e..f0a525a9eb 100644 --- a/drivers/png/image_loader_png.h +++ b/drivers/png/image_loader_png.h @@ -43,7 +43,7 @@ class ImageLoaderPNG : public ImageFormatLoader { public: static Error _load_image(void *rf_up, png_rw_ptr p_func, Ref p_image); - virtual Error load_image(Ref p_image, FileAccess *f); + virtual Error load_image(Ref p_image, FileAccess *f, bool p_force_linear); virtual void get_recognized_extensions(List *p_extensions) const; ImageLoaderPNG(); }; -- cgit v1.2.3