Some BRDF fixes

This commit is contained in:
Juan Linietsky 2016-12-21 14:20:35 -03:00
parent 72b844c349
commit 37f558cd7b
11 changed files with 95 additions and 20 deletions

View file

@ -612,6 +612,8 @@ String String::get_slicec(CharType p_splitter, int p_slice) const {
if (p_slice==count) {
return substr(prev,i-prev);
} else if (c[i]==0) {
return String();
} else {
count++;
prev=i+1;

View file

@ -130,9 +130,9 @@ void RasterizerGLES3::initialize() {
ERR_PRINT("Error initializing GLAD");
}
glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
// glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
// glDebugMessageCallbackARB(_gl_debug_print, NULL);
glEnable(_EXT_DEBUG_OUTPUT);
// glEnable(_EXT_DEBUG_OUTPUT);
#endif
@ -169,6 +169,14 @@ void RasterizerGLES3::begin_frame(){
storage->update_dirty_skeletons();
storage->update_dirty_shaders();
storage->update_dirty_materials();
storage->info.render_object_count=0;
storage->info.render_material_switch_count=0;
storage->info.render_surface_switch_count=0;
storage->info.render_shader_rebind_count=0;
storage->info.render_vertices_count=0;
scene->iteration();
@ -277,6 +285,13 @@ void RasterizerGLES3::end_frame(){
canvas->draw_generic_textured_rect(Rect2(0,0,15,15),Rect2(0,0,1,1));
#endif
OS::get_singleton()->swap_buffers();
/* print_line("objects: "+itos(storage->info.render_object_count));
print_line("material chages: "+itos(storage->info.render_material_switch_count));
print_line("surface changes: "+itos(storage->info.render_surface_switch_count));
print_line("shader changes: "+itos(storage->info.render_shader_rebind_count));
print_line("vertices: "+itos(storage->info.render_vertices_count));
*/
}
void RasterizerGLES3::finalize(){

View file

@ -1331,10 +1331,14 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) {
glDrawElements(gl_primitive[s->primitive],s->index_array_len, (s->array_len>=(1<<16))?GL_UNSIGNED_INT:GL_UNSIGNED_SHORT,0);
storage->info.render_vertices_count+=s->index_array_len;
} else {
glDrawArrays(gl_primitive[s->primitive],0,s->array_len);
storage->info.render_vertices_count+=s->array_len;
}
} break;
@ -1349,10 +1353,14 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) {
glDrawElementsInstanced(gl_primitive[s->primitive],s->index_array_len, (s->array_len>=(1<<16))?GL_UNSIGNED_INT:GL_UNSIGNED_SHORT,0,amount);
storage->info.render_vertices_count+=s->index_array_len * amount;
} else {
glDrawArraysInstanced(gl_primitive[s->primitive],0,s->array_len,amount);
storage->info.render_vertices_count+=s->array_len * amount;
}
} break;
@ -1379,6 +1387,8 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) {
int vertices = c.vertices.size();
uint32_t buf_ofs=0;
storage->info.render_vertices_count+=vertices;
if (c.texture.is_valid() && storage->texture_owner.owns(c.texture)) {
const RasterizerStorageGLES3::Texture *t = storage->texture_owner.get(c.texture);
@ -1704,6 +1714,8 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_e
bool first=true;
storage->info.render_object_count+=p_element_count;
for (int i=0;i<p_element_count;i++) {
RenderList::Element *e = p_elements[i];
@ -1864,8 +1876,13 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_e
if (material!=prev_material || rebind) {
storage->info.render_material_switch_count++;
rebind = _setup_material(material,p_alpha_pass);
// _rinfo.mat_change_count++;
if (rebind) {
storage->info.render_shader_rebind_count++;
}
}
if (!(e->sort_key&RenderList::SORT_KEY_UNSHADED_FLAG) && !p_directional_add && !p_shadow) {
@ -1877,6 +1894,8 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements,int p_e
if (prev_base_type != e->instance->base_type || prev_geometry!=e->geometry) {
_setup_geometry(e);
storage->info.render_surface_switch_count++;
}
_set_cull(e->sort_key&RenderList::SORT_KEY_MIRROR_FLAG,p_reverse_cull);

View file

@ -148,6 +148,7 @@ public:
} state;
/* SHADOW ATLAS API */
struct ShadowAtlas : public RID_Data {

View file

@ -826,15 +826,20 @@ Image RasterizerStorageGLES3::texture_get_data(RID p_texture,VS::CubeMapSide p_c
DVector<uint8_t> data;
int data_size = Image::get_image_data_size(texture->width,texture->height,texture->format,texture->mipmaps>1?-1:0);
int data_size = Image::get_image_data_size(texture->alloc_width,texture->alloc_height,texture->format,texture->mipmaps>1?-1:0);
data.resize(data_size);
data.resize(data_size*2); //add some memory at the end, just in case for buggy drivers
DVector<uint8_t>::Write wb = data.write();
glActiveTexture(GL_TEXTURE0);
glBindTexture(texture->target,texture->tex_id);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
print_line("GET FORMAT: "+Image::get_format_name(texture->format)+" mipmaps: "+itos(texture->mipmaps));
for(int i=0;i<texture->mipmaps;i++) {
int ofs=0;
@ -848,7 +853,9 @@ Image RasterizerStorageGLES3::texture_get_data(RID p_texture,VS::CubeMapSide p_c
glGetCompressedTexImage(texture->target,i,&wb[ofs]);
} else {
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glGetTexImage(texture->target,i,texture->gl_format_cache,texture->gl_type_cache,&wb[ofs]);
}
}
@ -856,6 +863,8 @@ Image RasterizerStorageGLES3::texture_get_data(RID p_texture,VS::CubeMapSide p_c
wb=DVector<uint8_t>::Write();
data.resize(data_size);
Image img(texture->alloc_width,texture->alloc_height,texture->mipmaps>1?true:false,texture->format,data);
return img;

View file

@ -91,6 +91,12 @@ public:
uint64_t texture_mem;
uint32_t render_object_count;
uint32_t render_material_switch_count;
uint32_t render_surface_switch_count;
uint32_t render_shader_rebind_count;
uint32_t render_vertices_count;
} info;

View file

@ -569,7 +569,7 @@ void light_compute(vec3 N, vec3 L,vec3 V,vec3 B, vec3 T,vec3 light_color,vec3 di
float speci = dotNL * D * F * vis;
specular += speci * light_color * specular_color * specular_blob_intensity;
specular += speci * light_color /* specular_color*/ * specular_blob_intensity;
#if defined(LIGHT_USE_CLEARCOAT)
float Dr = GTR1(dotNH, mix(.1,.001,clearcoat_gloss));
@ -780,7 +780,8 @@ void reflection_process(int idx, vec3 vertex, vec3 normal,vec3 binormal, vec3 ta
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);
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);
}
@ -893,6 +894,7 @@ void gi_probe_compute(sampler3D probe, mat4 probe_xform, vec3 bounds,vec3 cell_s
blend=1.0;
//radiance
#ifdef VCT_QUALITY_HIGH
#define MAX_CONE_DIRS 6
vec3 cone_dirs[MAX_CONE_DIRS] = vec3[] (
@ -905,13 +907,28 @@ void gi_probe_compute(sampler3D probe, mat4 probe_xform, vec3 bounds,vec3 cell_s
);
float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.15, 0.15, 0.15, 0.15, 0.15);
float cone_angle_tan = 0.577;
#else
#define MAX_CONE_DIRS 4
vec3 cone_dirs[MAX_CONE_DIRS] = vec3[] (
vec3(0.707107, 0, 0.707107),
vec3(0, 0.707107, 0.707107),
vec3(-0.707107, 0, 0.707107),
vec3(0, -0.707107, 0.707107)
);
float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.25, 0.25, 0.25);
float cone_angle_tan = 0.98269;
#endif
float max_distance = length(bounds);
vec3 light=vec3(0.0);
for(int i=0;i<MAX_CONE_DIRS;i++) {
vec3 dir = normalize( (probe_xform * vec4(pos + normal_mtx * cone_dirs[i],1.0)).xyz - probe_pos);
light+=cone_weights[i] * voxel_cone_trace(probe,cell_size,probe_pos,dir,0.577,max_distance);
light+=cone_weights[i] * voxel_cone_trace(probe,cell_size,probe_pos,dir,cone_angle_tan,max_distance);
}
@ -928,6 +945,7 @@ void gi_probe_compute(sampler3D probe, mat4 probe_xform, vec3 bounds,vec3 cell_s
void gi_probes_compute(vec3 pos, vec3 normal, float roughness, vec3 specular, inout vec3 out_specular, inout vec3 out_ambient) {
roughness = roughness * roughness;
vec3 ref_vec = normalize(reflect(normalize(pos),normal));
@ -1073,7 +1091,7 @@ FRAGMENT_SHADER_CODE
#endif
#ifdef ENABLE_CLIP_ALPHA
if (diffuse.a<0.99) {
if (albedo.a<0.99) {
//used for doublepass and shadowmapping
discard;
}
@ -1082,8 +1100,6 @@ FRAGMENT_SHADER_CODE
/////////////////////// LIGHTING //////////////////////////////
//apply energy conservation
vec3 diffuse=mix(albedo,vec3(0.0),specular);
specular = max(vec3(0.04),specular);
vec3 specular_light = vec3(0.0,0.0,0.0);
vec3 ambient_light;
@ -1093,6 +1109,7 @@ FRAGMENT_SHADER_CODE
#ifndef RENDER_SHADOW
float ndotv = clamp(dot(normal,eye_vec),0.0,1.0);
vec2 brdf = texture(brdf_texture, vec2(roughness, ndotv)).xy;
#endif
@ -1125,7 +1142,7 @@ FRAGMENT_SHADER_CODE
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);
specular_light = textureLod(radiance_map, norm.xy, lod).xyz * brdf.x + brdf.y;
}
//no longer a cubemap
@ -1219,7 +1236,6 @@ FRAGMENT_SHADER_CODE
} else {
highp vec4 splane=(shadow_matrix4 * vec4(vertex,1.0));
pssm_coord=splane.xyz/splane.w;
diffuse_light*=vec3(1.0,0.4,1.0);
#if defined(LIGHT_USE_PSSM_BLEND)
use_blend=false;
@ -1281,7 +1297,7 @@ FRAGMENT_SHADER_CODE
#endif //LIGHT_DIRECTIONAL_SHADOW
light_compute(normal,-light_direction_attenuation.xyz,eye_vec,binormal,tangent,light_color_energy.rgb*light_attenuation,diffuse,specular,light_params.z,roughness,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light);
light_compute(normal,-light_direction_attenuation.xyz,eye_vec,binormal,tangent,light_color_energy.rgb*light_attenuation,albedo,specular,light_params.z,roughness,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light);
#endif //#USE_LIGHT_DIRECTIONAL
@ -1310,11 +1326,11 @@ FRAGMENT_SHADER_CODE
}
for(int i=0;i<omni_light_count;i++) {
light_process_omni(omni_light_indices[i],vertex,eye_vec,normal,binormal,tangent,diffuse,specular,roughness,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light);
light_process_omni(omni_light_indices[i],vertex,eye_vec,normal,binormal,tangent,albedo,specular,roughness,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light);
}
for(int i=0;i<spot_light_count;i++) {
light_process_spot(spot_light_indices[i],vertex,eye_vec,normal,binormal,tangent,diffuse,specular,roughness,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light);
light_process_spot(spot_light_indices[i],vertex,eye_vec,normal,binormal,tangent,albedo,specular,roughness,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light);
}
@ -1338,13 +1354,17 @@ LIGHT_SHADER_CODE
#else
specular_light*=reflection_multiplier;
specular_light*=specular;
ambient_light*=albedo; //ambient must be multiplied by albedo at the end
#if defined(ENABLE_AO)
ambient_light*=ao;
#endif
//energy conservation
diffuse_light=mix(diffuse_light,vec3(0.0),specular);
ambient_light=mix(ambient_light,vec3(0.0),specular);
specular_light *= max(vec3(0.04),specular);
#ifdef USE_MULTIPLE_RENDER_TARGETS
#if defined(ENABLE_AO)

View file

@ -151,7 +151,7 @@ Error ContextGL_X11::initialize() {
static int context_attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 3,
GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB|GLX_CONTEXT_DEBUG_BIT_ARB,
GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB/*|GLX_CONTEXT_DEBUG_BIT_ARB*/,
None
};

View file

@ -2060,7 +2060,7 @@ void VisualServerScene::_render_scene(const Transform p_cam_transform,const Came
// directional lights
{
Instance** lights_with_shadow = (Instance**)alloca(sizeof(Instance*)*light_cull_count);
Instance** lights_with_shadow = (Instance**)alloca(sizeof(Instance*)*scenario->directional_lights.size());
int directional_shadow_count=0;
for (List<Instance*>::Element *E=scenario->directional_lights.front();E;E=E->next()) {

View file

@ -35,10 +35,11 @@ void editor_initialize_certificates() {
ByteArray data;
data.resize(_certs_uncompressed_size);
data.resize(_certs_uncompressed_size+1);
{
ByteArray::Write w = data.write();
Compression::decompress(w.ptr(),_certs_uncompressed_size,_certs_compressed,_certs_compressed_size,Compression::MODE_DEFLATE);
w[_certs_uncompressed_size]=0; //make sure it ends at zero
}
StreamPeerSSL::load_certs_from_memory(data);

View file

@ -267,6 +267,8 @@ void EditorResourcePreview::_thread() {
memdelete(f);
}
cache_valid=false;
if (cache_valid) {
texture = ResourceLoader::load(cache_base+".png","ImageTexture",true);