Skip to content

Commit

Permalink
Several fixes related to PBR and Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
reduz committed May 30, 2017
1 parent 0a6faeb commit 5567e89
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 98 deletions.
4 changes: 2 additions & 2 deletions core/io/image_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool ImageFormatLoader::recognize(const String &p_extension) const {
return false;
}

Error ImageLoader::load_image(String p_file, Ref<Image> p_image, FileAccess *p_custom) {
Error ImageLoader::load_image(String p_file, Ref<Image> p_image, FileAccess *p_custom, bool p_force_linear) {
ERR_FAIL_COND_V(p_image.is_null(), ERR_INVALID_PARAMETER);

FileAccess *f = p_custom;
Expand All @@ -62,7 +62,7 @@ Error ImageLoader::load_image(String p_file, Ref<Image> p_image, FileAccess *p_c

if (!loader[i]->recognize(extension))
continue;
Error err = loader[i]->load_image(p_image, f);
Error err = loader[i]->load_image(p_image, f, p_force_linear);

if (err != ERR_FILE_UNRECOGNIZED) {

Expand Down
4 changes: 2 additions & 2 deletions core/io/image_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ImageFormatLoader {
friend class ImageLoader;

protected:
virtual Error load_image(Ref<Image> p_image, FileAccess *p_fileaccess) = 0;
virtual Error load_image(Ref<Image> p_image, FileAccess *p_fileaccess, bool p_force_linear) = 0;
virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
bool recognize(const String &p_extension) const;

Expand All @@ -75,7 +75,7 @@ class ImageLoader {

protected:
public:
static Error load_image(String p_file, Ref<Image> p_image, FileAccess *p_custom = NULL);
static Error load_image(String p_file, Ref<Image> p_image, FileAccess *p_custom = NULL, bool p_force_linear = false);
static void get_recognized_extensions(List<String> *p_extensions);
static bool recognize(const String &p_extension);

Expand Down
9 changes: 6 additions & 3 deletions drivers/gles3/rasterizer_scene_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions drivers/gles3/rasterizer_scene_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ class RasterizerSceneGLES3 : public RasterizerScene {
struct EnvironmentRadianceUBO {

float transform[16];
float box_min[4]; //unused for now
float box_max[4];
float ambient_contribution;

} env_radiance_data;
Expand Down Expand Up @@ -700,7 +698,7 @@ class RasterizerSceneGLES3 : public RasterizerScene {

_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);
Expand Down
4 changes: 3 additions & 1 deletion drivers/gles3/rasterizer_storage_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {

Expand Down Expand Up @@ -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");

Expand Down
9 changes: 8 additions & 1 deletion drivers/gles3/shaders/copy.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}

53 changes: 29 additions & 24 deletions drivers/gles3/shaders/scene.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

};
Expand Down Expand Up @@ -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() {

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}

Expand All @@ -1322,6 +1326,7 @@ FRAGMENT_SHADER_CODE
}
#endif

ambient_light*=ambient_energy;

#ifdef USE_LIGHT_DIRECTIONAL

Expand Down
2 changes: 1 addition & 1 deletion drivers/png/image_loader_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Error ImageLoaderPNG::_load_image(void *rf_up, png_rw_ptr p_func, Ref<Image> p_i
return OK;
}

Error ImageLoaderPNG::load_image(Ref<Image> p_image, FileAccess *f) {
Error ImageLoaderPNG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {

Error err = _load_image(f, _read_png_data, p_image);
f->close();
Expand Down
2 changes: 1 addition & 1 deletion drivers/png/image_loader_png.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ImageLoaderPNG : public ImageFormatLoader {

public:
static Error _load_image(void *rf_up, png_rw_ptr p_func, Ref<Image> p_image);
virtual Error load_image(Ref<Image> p_image, FileAccess *f);
virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderPNG();
};
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ void EditorNode::_property_editor_back() {

void EditorNode::_save_default_environment() {

Ref<Environment> fallback = get_scene_root()->get_world()->get_fallback_environment();
Ref<Environment> fallback = get_tree()->get_root()->get_world()->get_fallback_environment();

if (fallback.is_valid() && fallback->get_path().is_resource_file()) {
Map<RES, bool> processed;
Expand Down
4 changes: 3 additions & 1 deletion editor/import/resource_importer_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ void ResourceImporterTexture::get_import_options(List<ImportOption> *r_options,
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "flags/srgb", PROPERTY_HINT_ENUM, "Disable,Enable,Detect"), 2));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/fix_alpha_border"), p_preset != PRESET_3D ? true : false));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/premult_alpha"), true));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "process/HDR_as_SRGB"), false));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "stream"), false));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "size_limit", PROPERTY_HINT_RANGE, "0,4096,1"), 0));
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "detect_3d"), p_preset == PRESET_DETECT));
Expand Down Expand Up @@ -327,10 +328,11 @@ Error ResourceImporterTexture::import(const String &p_source_file, const String
bool stream = p_options["stream"];
int size_limit = p_options["size_limit"];
bool force_rgbe = int(p_options["compress/hdr_mode"]) == 1;
bool hdr_as_srgb = p_options["process/HDR_as_SRGB"];

Ref<Image> image;
image.instance();
Error err = ImageLoader::load_image(p_source_file, image);
Error err = ImageLoader::load_image(p_source_file, image, NULL, hdr_as_srgb);
if (err != OK)
return err;

Expand Down
24 changes: 17 additions & 7 deletions modules/hdr/image_loader_hdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@

#include "thirdparty/tinyexr/tinyexr.h"

Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f) {
Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {

String header = f->get_token();

print_line("HEADER: " + header);
ERR_FAIL_COND_V(header != "#?RADIANCE", ERR_FILE_UNRECOGNIZED);

String format = f->get_token();
print_line("FORMAT: " + format);

ERR_FAIL_COND_V(format != "FORMAT=32-bit_rle_rgbe", ERR_FILE_UNRECOGNIZED);
ERR_FAIL_COND_V(header != "#?RADIANCE" && header != "#?RGBE", ERR_FILE_UNRECOGNIZED);

while (true) {
String format = f->get_token();
ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_UNRECOGNIZED);
if (format.begins_with("FORMAT=") && format != "FORMAT=32-bit_rle_rgbe") {
ERR_EXPLAIN("Only 32-bit_rle_rgbe is supported for .hdr files.");
return ERR_FILE_UNRECOGNIZED;
}
if (format == "FORMAT=32-bit_rle_rgbe")
break;
}

String token = f->get_token();

Expand Down Expand Up @@ -132,6 +138,10 @@ Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f) {
ptr[1] * exp / 255.0,
ptr[2] * exp / 255.0);

if (p_force_linear) {
c = c.to_linear();
}

*(uint32_t *)ptr = c.to_rgbe9995();
ptr += 4;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/hdr/image_loader_hdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
class ImageLoaderHDR : public ImageFormatLoader {

public:
virtual Error load_image(Ref<Image> p_image, FileAccess *f);
virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderHDR();
};
Expand Down
2 changes: 1 addition & 1 deletion modules/jpg/image_loader_jpegd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Error jpeg_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p
return OK;
}

Error ImageLoaderJPG::load_image(Ref<Image> p_image, FileAccess *f) {
Error ImageLoaderJPG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {

PoolVector<uint8_t> src_image;
int src_image_len = f->get_len();
Expand Down
2 changes: 1 addition & 1 deletion modules/jpg/image_loader_jpegd.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
class ImageLoaderJPG : public ImageFormatLoader {

public:
virtual Error load_image(Ref<Image> p_image, FileAccess *f);
virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderJPG();
};
Expand Down
16 changes: 12 additions & 4 deletions modules/tinyexr/image_loader_tinyexr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include "thirdparty/tinyexr/tinyexr.h"

Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, FileAccess *f) {
Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {

PoolVector<uint8_t> src_image;
int src_image_len = f->get_len();
Expand Down Expand Up @@ -133,9 +133,17 @@ Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, FileAccess *f) {
// Assume `out_rgba` have enough memory allocated.
for (int i = 0; i < exr_image.width * exr_image.height; i++) {

*iw++ = Math::make_half_float(reinterpret_cast<float **>(exr_image.images)[idxR][i]);
*iw++ = Math::make_half_float(reinterpret_cast<float **>(exr_image.images)[idxG][i]);
*iw++ = Math::make_half_float(reinterpret_cast<float **>(exr_image.images)[idxB][i]);
Color color(
reinterpret_cast<float **>(exr_image.images)[idxR][i],
reinterpret_cast<float **>(exr_image.images)[idxG][i],
reinterpret_cast<float **>(exr_image.images)[idxB][i]);

if (p_force_linear)
color = color.to_linear();

*iw++ = Math::make_half_float(color.r);
*iw++ = Math::make_half_float(color.g);
*iw++ = Math::make_half_float(color.b);

if (idxA > 0) {
*iw++ = Math::make_half_float(reinterpret_cast<float **>(exr_image.images)[idxA][i]);
Expand Down
2 changes: 1 addition & 1 deletion modules/tinyexr/image_loader_tinyexr.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
class ImageLoaderTinyEXR : public ImageFormatLoader {

public:
virtual Error load_image(Ref<Image> p_image, FileAccess *f);
virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderTinyEXR();
};
Expand Down
Loading

0 comments on commit 5567e89

Please sign in to comment.